Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

feat(grid): let agent_retarget say 'own login', deliberately

Dev jacobddbfec8c0b79
diff --git a/cli/src/backendSocket.spec.ts b/cli/src/backendSocket.spec.ts
index 20f19d705..04efc7538 100644
--- a/cli/src/backendSocket.spec.ts
+++ b/cli/src/backendSocket.spec.ts
@@ -717,3 +717,53 @@ describe('adapter-ws dial url', () => {
expect(dialUrl(new BackendSocket('token', undefined, () => {}, 'computer-1'))).not.toContain('&machine=')
})
})
+
+describe('agent_retarget clearGrid', () => {
+ afterEach(() => {
+ wsMock.instances.length = 0
+ vi.restoreAllMocks()
+ })
+
+ const WIRE_GRID = {
+ networkId: 'grid-abc',
+ networkName: 'autonomous.ai',
+ baseUrl: 'https://grid.autonomous.ai/grid-abc/relay/v1',
+ apiKey: 'gridkey-abc123',
+ }
+
+ async function retarget(payload: Record<string, unknown>) {
+ const seen: Array<{ agentId: string; grid: unknown }> = []
+ const socket = new BackendSocket('token')
+ socket.onRetargetAgent = async (input) => { seen.push(input); return { ok: true } }
+ socket.connect()
+ const ws = wsMock.instances[0]
+ ws.open()
+ ws.message({ t: 'down', connId: 'web-1', frame: { type: 'agent_retarget', payload } })
+ await vi.waitFor(() => expect(ws.sent.length).toBeGreaterThan(0))
+ const reply = parseSent(ws)
+ .map((item) => item.frame as { type?: string; payload?: Record<string, unknown> } | undefined)
+ .find((frame) => frame?.type === 'agent_retarget_result')
+ await socket.stop()
+ return { seen, reply }
+ }
+
+ it('passes a null grid when clearGrid is true', async () => {
+ const { seen } = await retarget({ requestId: 'r', agentId: 'a1', clearGrid: true })
+ expect(seen).toEqual([{ agentId: 'a1', grid: null }])
+ })
+
+ // Both is a contradiction, and answering it would mean guessing which one the client meant.
+ it('refuses grid and clearGrid together', async () => {
+ const { seen, reply } = await retarget({ requestId: 'r', agentId: 'a1', clearGrid: true, grid: WIRE_GRID })
+ expect(reply?.payload?.error).toBe('INVALID_GRID')
+ expect(seen).toHaveLength(0)
+ })
+
+ // Unchanged: a client that simply forgot the field is still an error, which is the whole reason
+ // clearGrid is a separate field rather than `grid: null`.
+ it('still refuses a frame with neither', async () => {
+ const { seen, reply } = await retarget({ requestId: 'r', agentId: 'a1' })
+ expect(reply?.payload?.error).toBe('INVALID_GRID')
+ expect(seen).toHaveLength(0)
+ })
+})
diff --git a/cli/src/backendSocket.ts b/cli/src/backendSocket.ts
index ab859e685..8550f49a4 100644
--- a/cli/src/backendSocket.ts
+++ b/cli/src/backendSocket.ts
@@ -313,7 +313,7 @@ export class BackendSocket {
* Separate from `onRestartAgent` because that one puts the agent back exactly as it was; this one
* puts it back somewhere else. They share the swap underneath and differ in what they hand it.
*/
- onRetargetAgent: ((input: { agentId: string; grid: GridLaunchOverride }) =>
+ onRetargetAgent: ((input: { agentId: string; grid: GridLaunchOverride | null }) =>
Promise<{ ok: true } | { ok: false; error: string; detail?: string }>) | null = null
/** Called on `agent_restart` — cli.ts stops the agent's live engine process and relaunches it in the
* SAME tmux pane, keeping the SAME agentId and (best-effort) resuming the same engine session.
@@ -1353,17 +1353,23 @@ export class BackendSocket {
const agentId = (payload.agentId as string | undefined) || (payload.sessionId as string | undefined)
if (!agentId) { reply(type, requestId, { error: 'MISSING_AGENT_ID' }); return }
if (!this.onRetargetAgent) { reply(type, requestId, { error: 'UNSUPPORTED_ON_REMOTE' }); return }
+ const clear = payload.clearGrid === true
const target = parseGridLaunchOverride(payload.grid)
- // Unlike agent_create, an absent grid is meaningless here: this frame exists only to move an
- // agent somewhere, so nowhere is a malformed request rather than "leave it alone".
- if (target.state !== 'ok') {
+ // Exactly one, and `clearGrid` is a separate field rather than `grid: null` on purpose:
+ // parseGridLaunchOverride already answers `absent` for both undefined and null, so
+ // overloading null would make "I forgot the field" and "I mean own login" the same frame.
+ if (clear && target.state !== 'absent') {
+ reply(type, requestId, { error: 'INVALID_GRID', detail: 'clearGrid and grid are mutually exclusive' })
+ return
+ }
+ if (!clear && target.state !== 'ok') {
reply(type, requestId, {
error: 'INVALID_GRID',
detail: target.state === 'invalid' ? target.reason : 'grid is required',
})
return
}
- const moved = await this.onRetargetAgent({ agentId, grid: target.override })
+ const moved = await this.onRetargetAgent({ agentId, grid: target.state === 'ok' ? target.override : null })
if (!moved.ok) {
reply(type, requestId, moved.detail ? { error: moved.error, detail: moved.detail } : { error: moved.error })
return