Files
archived-orca/src/main/runtime/runtime-folder-worktree-create.test.ts
T
Brennan Benson a375936c04 feat(agent-launch): let a caller reserve the pane its terminal launch creates (#22291)
* feat(agent-launch): let a caller reserve the pane its terminal launch creates

* fix(agent-launch): refuse a launch whose reserved pane is already live

* fix(agent-launch): refuse a live reserved pane before it is revealed

The live-pane refusal used to fire in the executor, after createTerminal
had already issued a handle, published the mobile snapshot and revealed
the tab. The reveal re-registered a fresh launch config over the running
agent's. agent.launch now passes requireFreshPane with a reserved pane,
and createTerminal throws AgentLaunchPaneAlreadyLiveError as soon as
spawn reports it attached to a live pane. That is before any handle,
snapshot or reveal. The spawn reattach itself is the one terminal.create
already uses, so the live PTY is never killed, and the stable-pane
create claim is still released in finally. The isReattach plumbing
added to the launch factory for the old check is gone.

A replay-safe launch refused this way on an existing workspace now
records a failed ledger row, the same way a name collision does.
Before, the row stayed claimed, so every retry got
agent_session_operation_unknown. agent.launchReplay passes the code
through. On create-worktree the workspace already exists when the
terminal is refused, so the row stays unknown. The code is added to
the runtime passthrough list so callers can branch on it.

The pane key is now in the replay fingerprint, deliberately. It is not
placement: group, anchor and focus still stay out of the request and
out of the ledger. It is identity. It is written into the pane's PTY
environment and names the tab the caller has placed. A retry that
reserved a different pane is therefore a different request. Replaying
the first answer would return a key the new reservation can never
find. This matches terminal.createAgentSession, which also fingerprints
its tab and leaf ids. The key is only folded in when present, so every
existing digest is unchanged, and a test pins that.

The wire schema now refuses a tab id the runtime would not adopt as
sent: one with surrounding whitespace, which the runtime trims, and one
longer than 512 characters, which the spawn reservation does not key
on. It reuses the tab-id schema that Placement uses.

* test(agent-launch): pin that a refused live pane issues no handle

The refusal test named handle issuance but only asserted the reveal, so a
throw moved to just before the reveal would still pass. Assert no terminal
is registered, with the attach test as the positive control.
2026-09-23 10:18:16 -07:00

75 lines
2.5 KiB
TypeScript

/** The startup terminal a folder workspace's create spawns; this path forwards a reserved pane itself. */
import { describe, expect, it, vi } from 'vitest'
import type { Repo } from '../../shared/repo-types'
import { createRuntimeFolderWorktree } from './runtime-folder-worktree-create'
type CreateArgs = Parameters<typeof createRuntimeFolderWorktree>[0]
const repo: Repo = {
id: 'repo-1',
path: '/folder',
displayName: 'folder',
badgeColor: 'blue',
addedAt: 1
}
function createDeps() {
const createTerminal = vi
.fn<CreateArgs['deps']['createTerminal']>()
.mockResolvedValue({ handle: 'term-1', worktreeId: 'folder-1', title: null })
const store = {
getSettings: () => ({ workspaceDir: '/ws', nestWorkspaces: false }),
setWorktreeMeta: (_id: string, meta: Record<string, unknown>) => meta,
getProjectHostSetups: () => []
}
const deps: CreateArgs['deps'] = {
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: the create reads only these three store methods; any other would throw on call rather than read a wrong value.
store: store as unknown as CreateArgs['deps']['store'],
ptySpawnAvailable: true,
createTerminal,
markTrusted: vi.fn(),
pasteDraft: vi.fn(),
sendFollowup: vi.fn(),
invalidateResolvedWorktrees: vi.fn(),
notifyWorktreesChanged: vi.fn(),
emitCreated: vi.fn(),
activate: vi.fn()
}
return { createTerminal, deps }
}
const TAB_ID = '9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d'
const LEAF_ID = '3f2504e0-4f89-41d3-9a0c-0305e82c3301'
async function startupTerminalOptions(startupPaneKey?: string): Promise<Record<string, unknown>> {
const { createTerminal, deps } = createDeps()
await createRuntimeFolderWorktree({
request: {
repoSelector: `id:${repo.id}`,
name: 'task',
...(startupPaneKey ? { startupPaneKey } : {})
},
repo,
createdWithAgent: 'codex',
startup: { command: 'codex' },
deps
})
return createTerminal.mock.calls[0]?.[1] ?? {}
}
describe('a folder workspace create with a startup agent', () => {
it('creates the startup terminal under the pane the caller reserved', async () => {
expect(await startupTerminalOptions(`${TAB_ID}:${LEAF_ID}`)).toMatchObject({
tabId: TAB_ID,
leafId: LEAF_ID
})
})
it('leaves the pane to the runtime when none was reserved', async () => {
const options = await startupTerminalOptions()
expect(options).not.toHaveProperty('tabId')
expect(options).not.toHaveProperty('leafId')
})
})