diff --git a/dashboard/e2e/views.test.ts b/dashboard/e2e/views.test.ts index 018e89c..cb9e962 100644 --- a/dashboard/e2e/views.test.ts +++ b/dashboard/e2e/views.test.ts @@ -309,3 +309,27 @@ test('a stopped box is started from its info view, and stopped again', async () await close(); } }); + +test('a box is renamed from its info view, and the list shows the new name', async () => { + const { page, errors, close } = await openPage(stub.url, '/boxes/e5f6a7b8/info'); + try { + const field = page.getByLabel('Name'); + const button = page.getByRole('button', { name: 'Rename' }); + await expect.poll(() => field.isVisible()).toBe(true); + // The button is off while the field shows the name of the box. + expect(await button.isDisabled()).toBe(true); + + await field.fill('a renamed box'); + await button.click(); + await expect + .poll(() => page.getByRole('heading', { name: 'a renamed box' }).isVisible()) + .toBe(true); + await expect.poll(() => button.isDisabled()).toBe(true); + + await page.goto(`${stub.url}/`); + await expect.poll(() => page.getByText('a renamed box').isVisible()).toBe(true); + expect(errors).toEqual([]); + } finally { + await close(); + } +}); diff --git a/dashboard/src/api.ts b/dashboard/src/api.ts index 782123b..7e40692 100644 --- a/dashboard/src/api.ts +++ b/dashboard/src/api.ts @@ -333,6 +333,18 @@ export const api = { */ deleteReview: (id: string) => request(`/api/boxes/${id}/review`, { method: 'DELETE' }), + /** + * Changes the name of a box. + * + * @param id The id of the box. + * @param name The new name. + * @returns The box after the change. + */ + renameBox: (id: string, name: string) => + request(`/api/boxes/${id}/name`, { + method: 'PUT', + body: JSON.stringify({ name }), + }), /** * Selects the agent sets of a box. * diff --git a/dashboard/src/views/BoxInfo.tsx b/dashboard/src/views/BoxInfo.tsx index 2681dc2..8395e46 100644 --- a/dashboard/src/views/BoxInfo.tsx +++ b/dashboard/src/views/BoxInfo.tsx @@ -1,4 +1,4 @@ -import { useCallback, useEffect, useState } from 'react'; +import { type FormEvent, useCallback, useEffect, useState } from 'react'; import { Link, useNavigate, useParams } from 'react-router'; import { AgentSetPicker } from '@/components/AgentSetPicker'; import { BackLink } from '@/components/BackLink'; @@ -10,6 +10,8 @@ import { Notice } from '@/components/Notice'; import { StatusBadge } from '@/components/StatusBadge'; import { Button } from '@/components/ui/button'; import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'; +import { Input } from '@/components/ui/input'; +import { Label } from '@/components/ui/label'; import { Select, SelectContent, @@ -62,6 +64,8 @@ export function BoxInfo() { const [error, setError] = useState(null); const [busy, setBusy] = useState(false); const [confirmDelete, setConfirmDelete] = useState(false); + /** The name in the name field. Null while the field shows the name of the box. */ + const [nameDraft, setNameDraft] = useState(null); /** The named agent sets, for the picker. Null while loading or when the request failed. */ const [agentSets, setAgentSets] = useState(null); /** The egress profiles, for the select. Null while loading or when the request failed. */ @@ -154,6 +158,16 @@ export function BoxInfo() { } const running = box.runtimeState === 'running'; + const name = (nameDraft ?? box.name).trim(); + + /** Saves the name from the name field. */ + const rename = async (e: FormEvent): Promise => { + e.preventDefault(); + await act(async () => { + await api.renameBox(id, name); + setNameDraft(null); + }); + }; return (
@@ -181,7 +195,21 @@ export function BoxInfo() { Details - + +
void rename(e)}> +
+ + setNameDraft(e.target.value)} + /> +
+ +
diff --git a/doc/boxes.md b/doc/boxes.md index d038146..dadae45 100644 --- a/doc/boxes.md +++ b/doc/boxes.md @@ -41,8 +41,8 @@ The info icon on a card opens the box's details page. The details page shows the box's identity and runtime state: the facts of the runtime (on Docker the container id, the network name with its subnet, and the image id), last activity, disk use, and the ACP session id of the most recently -active thread. It shows the agent sets and the egress profile of the box, which the user can change there, and the -[refused hosts](egress-profiles.md#refused-hosts). It lists the dev tunnels the box hosts and the processes the +active thread. It shows the name, the agent sets and the egress profile of the box, which the user can change there, and +the [refused hosts](egress-profiles.md#refused-hosts). It lists the dev tunnels the box hosts and the processes the orchestrator last read in the box. For a running box whose network isolation is not complete, it shows a warning. diff --git a/doc/rest.md b/doc/rest.md index 213e7db..d01190b 100644 --- a/doc/rest.md +++ b/doc/rest.md @@ -38,6 +38,7 @@ adapter's last advertised modes and config options (when one has answered), and | POST | `/api/boxes/:id/start` | `BoxDetail` | | POST | `/api/boxes/:id/stop` | `BoxDetail` | | DELETE | `/api/boxes/:id` | 204 | +| PUT | `/api/boxes/:id/name` | `BoxDetail` | | PUT | `/api/boxes/:id/agent-sets` | `BoxDetail` | | PUT | `/api/boxes/:id/egress-profile` | `BoxDetail` | | GET | `/api/boxes/:id/denials` | `EgressDenial[]` | @@ -56,6 +57,9 @@ adapter's last advertised modes and config options (when one has answered), and Every [box](boxes.md) is created with one thread. `thread` selects its harness and its initial mode and config; absent, it is Claude on its defaults. `profile` is accepted but ignored. An unknown `egressProfile` gets a 400. +The orchestrator trims the `name`. An empty name or a name of more than 100 characters gets a 400. `PUT …/name` accepts +`{ "name": "…" }` with the same rules. + `agentSets` lists the [agent sets](agent-sets.md) of the box in the order of the merge. An absent or empty list selects the global set only. A list with the global set, an unknown set, or one set twice gets a 400. diff --git a/orchestrator/src/app.test.ts b/orchestrator/src/app.test.ts index 197a8a2..e29f149 100644 --- a/orchestrator/src/app.test.ts +++ b/orchestrator/src/app.test.ts @@ -1156,6 +1156,42 @@ test('a box changes its egress profile, and moves to the default when its profil ); }); +test('a box is renamed over the API', async () => { + insertBox('abc123'); + fakeDocker(); + + const renamed = await orchestrator.app.inject({ + method: 'PUT', + url: '/api/boxes/abc123/name', + payload: { name: ' new name ' }, + }); + assert.equal(renamed.statusCode, 200); + assert.equal(renamed.json().name, 'new name'); + const list = await orchestrator.app.inject({ url: '/api/boxes' }); + assert.deepEqual( + (list.json() as Array<{ name: string }>).map((b) => b.name), + ['new name'], + ); + + for (const name of [' ', 'x'.repeat(101)]) { + const refused = await orchestrator.app.inject({ + method: 'PUT', + url: '/api/boxes/abc123/name', + payload: { name }, + }); + assert.equal(refused.statusCode, 400); + } + const after = await orchestrator.app.inject({ url: '/api/boxes/abc123' }); + assert.equal(after.json().name, 'new name'); + + const missingBox = await orchestrator.app.inject({ + method: 'PUT', + url: '/api/boxes/nope/name', + payload: { name: 'new name' }, + }); + assert.equal(missingBox.statusCode, 404); +}); + /** The fields of a pushed policy that the tests read. */ interface PushedPolicy { /** The policy of each box. */ diff --git a/orchestrator/src/app.ts b/orchestrator/src/app.ts index fb28e71..f596764 100644 --- a/orchestrator/src/app.ts +++ b/orchestrator/src/app.ts @@ -32,6 +32,7 @@ import { backgroundStopBody, boxAgentSetsBody, boxEgressProfileBody, + boxNameBody, createAgentSetBody, createBoxBody, createEgressProfileBody, @@ -721,6 +722,12 @@ export function buildApp(cfg: Config, db: Db, opts: BuildOptions = {}): Orchestr return reply.code(204).send(); }); + app.put('/api/boxes/:id/name', async (req) => { + const { id } = req.params as { id: string }; + const body = parseBody(boxNameBody, req.body); + return manager.rename(id, body.name); + }); + app.put('/api/boxes/:id/agent-sets', async (req) => { const { id } = req.params as { id: string }; const body = parseBody(boxAgentSetsBody, req.body); diff --git a/orchestrator/src/bodies.ts b/orchestrator/src/bodies.ts index ba09554..1092d55 100644 --- a/orchestrator/src/bodies.ts +++ b/orchestrator/src/bodies.ts @@ -53,6 +53,11 @@ export const createBoxBody = z.object({ thread: threadOptions.optional(), }); +/** The body of PUT /api/boxes/:id/name, with the new name of the box. */ +export const boxNameBody = z.object({ + name: z.string(), +}); + /** * The body of PUT /api/boxes/:id/egress-profile, with the profile of the box * or null for the default profile. diff --git a/orchestrator/src/boxes.ts b/orchestrator/src/boxes.ts index 966550b..2038ced 100644 --- a/orchestrator/src/boxes.ts +++ b/orchestrator/src/boxes.ts @@ -463,9 +463,7 @@ export class BoxManager { * not available, or a 500 when a step of the create fails. */ async create(body: CreateBoxBody, owner: string): Promise { - const name = body.name?.trim(); - if (!name) throw new HttpError(400, 'name is required'); - if (name.length > 100) throw new HttpError(400, 'name must be 100 characters or fewer'); + const name = boxName(body.name); const agentSetIds = this.selectedAgentSets(owner, body.agentSets); const egressProfileId = this.selectedProfile(owner, body.egressProfile); @@ -587,6 +585,20 @@ export class BoxManager { return this.detail(id); } + /** + * Changes the display name of a box. + * + * @param id The id of the box. + * @param requested The new name. + * @returns The detail of the box. + * @throws {HttpError} A 404 when the box does not exist, or a 400 when the name is not valid. + */ + async rename(id: string, requested: string): Promise { + this.mustGet(id); + this.db.prepare('UPDATE boxes SET name = ? WHERE id = ?').run(boxName(requested), id); + return this.detail(id); + } + /** * Checks the egress profile that a request selects for a box of a user. * @@ -1411,6 +1423,20 @@ function toThreadSummary( }; } +/** + * Checks the display name of a box from a request. + * + * @param name The name from the request. + * @returns The name without the surrounding whitespace. + * @throws {HttpError} A 400 for an empty name or a name of more than 100 characters. + */ +function boxName(name: string | undefined): string { + const trimmed = name?.trim(); + if (!trimmed) throw new HttpError(400, 'name is required'); + if (trimmed.length > 100) throw new HttpError(400, 'name must be 100 characters or fewer'); + return trimmed; +} + /** * Checks the thread options of a request. *