diff --git a/src/main/runtime/orchestration/db/worker-dispatch/worker-dispatch-start.ts b/src/main/runtime/orchestration/db/worker-dispatch/worker-dispatch-start.ts index 7a01323841b..b65da6e2f61 100644 --- a/src/main/runtime/orchestration/db/worker-dispatch/worker-dispatch-start.ts +++ b/src/main/runtime/orchestration/db/worker-dispatch/worker-dispatch-start.ts @@ -12,7 +12,6 @@ import { taskNotStartableError } from '../../coordinator/dispatch/task-dispatch-refusal' import { getFollowUpDispatch, takeFollowUpMail } from '../dispatch-context/worker-follow-up-lineage' -import { refuseClaimedWorkerTerminal, type WorkerTerminalClaim } from './worker-terminal-claim' export function createStartingWorkerDispatch( this: OrchestrationDb, @@ -54,8 +53,6 @@ export function createStartingWorkerDispatch( acceptedBy?: { callerFingerprint: string; requestId: string } /** The Dispatch whose unread mail this follow-up's Task tells it to check, moved to it in the claim. */ takesMailOf?: string - /** The existing terminal this start takes; claimed atomically with the Dispatch row. */ - claimsTerminal?: WorkerTerminalClaim } ): { dispatch: DispatchContextRow; worker: WorkerDispatchRow; task: TaskRow } { this.db.exec('BEGIN IMMEDIATE') @@ -92,9 +89,6 @@ export function createStartingWorkerDispatch( { followUpDispatchId: claimedFollowUp.id } ) } - if (params.claimsTerminal) { - refuseClaimedWorkerTerminal(this, params.claimsTerminal) - } const task = params.taskId ? this.getTask(params.taskId) : params.taskSpec diff --git a/src/main/runtime/orchestration/db/worker-dispatch/worker-terminal-claim.ts b/src/main/runtime/orchestration/db/worker-dispatch/worker-terminal-claim.ts index d6945836df5..0bc900e063a 100644 --- a/src/main/runtime/orchestration/db/worker-dispatch/worker-terminal-claim.ts +++ b/src/main/runtime/orchestration/db/worker-dispatch/worker-terminal-claim.ts @@ -1,13 +1,7 @@ import { OrchestrationError } from '../../orchestration-error' import type { OrchestrationDb } from '../orchestration-db' import type { DispatchContextRow } from '../orchestration-records' - -export type WorkerTerminalClaim = { - handle: string - paneKey?: string - /** A racing follow-up of the same settled Dispatch is left to `follow_up_claimed`, which steers the send into it. */ - followUpOf?: string -} +import type { OrcaRuntimeService } from '../../../orca-runtime' // A starting Dispatch names its terminal only in its start options until its authority attaches. const STARTING_ON_TERMINAL_SQL = `SELECT d.* FROM dispatch_contexts d @@ -18,13 +12,22 @@ const STARTING_ON_TERMINAL_SQL = `SELECT d.* FROM dispatch_contexts d /** Refuse a worker-start or follow-up onto a terminal another open Dispatch holds or is starting on. * worker-start and the follow-up a send starts both claim through here, so whichever comes second is refused before any wait. */ -export function refuseClaimedWorkerTerminal(db: OrchestrationDb, claim: WorkerTerminalClaim): void { +export function refuseClaimedWorkerTerminal( + db: OrchestrationDb, + handle: string | undefined, + runtime: Pick, + /** A racing follow-up of the same settled Dispatch is left to `follow_up_claimed`, which steers the send into it. */ + followUpOf: string | undefined +): void { + if (!handle) { + return + } const open = - db.findActiveDispatchForAssignee(claim.handle, claim.paneKey) ?? + db.findActiveDispatchForAssignee(handle, runtime.getTerminalPaneKey(handle) ?? undefined) ?? // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: selects whole dispatch_contexts rows. - (db.db.prepare(STARTING_ON_TERMINAL_SQL).get(claim.handle) as DispatchContextRow | undefined) - if (open && !(claim.followUpOf && open.follow_up_of_dispatch_id === claim.followUpOf)) { - throw terminalBusyError(db, claim.handle, open) + (db.db.prepare(STARTING_ON_TERMINAL_SQL).get(handle) as DispatchContextRow | undefined) + if (open && !(followUpOf && open.follow_up_of_dispatch_id === followUpOf)) { + throw terminalBusyError(db, handle, open) } } diff --git a/src/main/runtime/rpc/methods/orchestration/worker/explicit-worker-terminal-validation.ts b/src/main/runtime/rpc/methods/orchestration/worker/explicit-worker-terminal-validation.ts index d5918f77a60..a2d53e01de2 100644 --- a/src/main/runtime/rpc/methods/orchestration/worker/explicit-worker-terminal-validation.ts +++ b/src/main/runtime/rpc/methods/orchestration/worker/explicit-worker-terminal-validation.ts @@ -6,10 +6,6 @@ import type { OrchestrationPartyIdentity as OrchestrationParty } from '../../../../../../shared/orchestration/mailbox/orchestration-party-identity' import type { OrchestrationDb } from '../../../../orchestration/db/orchestration-db' -import { - refuseClaimedWorkerTerminal, - type WorkerTerminalClaim -} from '../../../../orchestration/db/worker-dispatch/worker-terminal-claim' import type { OrcaSessionId } from '../../../../../../shared/orca-session-address' import { formatOrcaSessionAddress } from '../../../../../../shared/orca-session-address' import { @@ -60,8 +56,7 @@ export async function assertExplicitWorkerTerminalUsable(args: { } } -/** A `--terminal` party: a terminal pane, or a chat named by its Orca session ID. - * Returns the claim its Dispatch insert repeats atomically; refused here first, before a Run binds or anything waits. */ +/** A `--terminal` party: a terminal pane, or a chat named by its Orca session ID. */ export async function assertExplicitWorkerUsable(args: { runtime: OrcaRuntimeService db: OrchestrationDb @@ -69,20 +64,11 @@ export async function assertExplicitWorkerUsable(args: { from: string coordinator: OrchestrationCallerIdentity | null resolvedWorktreeId: string | undefined - followUpOf: string | undefined -}): Promise { +}): Promise { const chat = chatAssigneeOf(args.terminal) await (chat ? assertExplicitWorkerChatUsable({ ...args, chat }) : assertExplicitWorkerTerminalUsable({ ...args, terminal: args.terminal.address })) - const handle = args.terminal.address - const claim = { - handle, - paneKey: args.runtime.getTerminalPaneKey(handle) ?? undefined, - followUpOf: args.followUpOf - } - refuseClaimedWorkerTerminal(args.db, claim) - return claim } /** The same refusals for a chat named by its Orca session ID: itself, unreachable, another worktree. */ diff --git a/src/main/runtime/rpc/methods/orchestration/worker/local-worker-start-dispatch.ts b/src/main/runtime/rpc/methods/orchestration/worker/local-worker-start-dispatch.ts index e336eb99252..5d26db76cb2 100644 --- a/src/main/runtime/rpc/methods/orchestration/worker/local-worker-start-dispatch.ts +++ b/src/main/runtime/rpc/methods/orchestration/worker/local-worker-start-dispatch.ts @@ -5,7 +5,6 @@ import type { WorkerStartInput } from './worker-start-schema' import type { RunRow } from '../../../../orchestration/db/orchestration-records' import type { startLocalWorker } from './local-worker-start' import { resolveDispatchCreator } from '../runs/dispatch-creator' -import type { WorkerTerminalClaim } from '../../../../orchestration/db/worker-dispatch/worker-terminal-claim' export function createLocalWorkerStartDispatch( { @@ -28,8 +27,7 @@ export function createLocalWorkerStartDispatch( resolvedWorktreeId, repoSelector, agent, - launchReceipt, - claimsTerminal + launchReceipt }: { params: WorkerStartInput run: RunRow @@ -40,7 +38,6 @@ export function createLocalWorkerStartDispatch( repoSelector: string | undefined agent: TuiAgent | undefined launchReceipt: OrchestrationWorkerLaunchReceipt - claimsTerminal: WorkerTerminalClaim | undefined } ) { const startOptions = { @@ -85,7 +82,6 @@ export function createLocalWorkerStartDispatch( mutationReceipt: orchestrationMutation, followUpOf: followUp?.of, acceptedBy: followUp?.acceptedBy, - takesMailOf: followUp?.takesMailOf, - claimsTerminal + takesMailOf: followUp?.takesMailOf }) } diff --git a/src/main/runtime/rpc/methods/orchestration/worker/local-worker-start.ts b/src/main/runtime/rpc/methods/orchestration/worker/local-worker-start.ts index b70b8e072a9..1af01cf6710 100644 --- a/src/main/runtime/rpc/methods/orchestration/worker/local-worker-start.ts +++ b/src/main/runtime/rpc/methods/orchestration/worker/local-worker-start.ts @@ -1,4 +1,5 @@ import { createLocalWorkerStartDispatch } from './local-worker-start-dispatch' +import { refuseClaimedWorkerTerminal } from '../../../../orchestration/db/worker-dispatch/worker-terminal-claim' import { probeWorkerOpenCodeModelLaunchSupport } from './worker-opencode-model-preflight' import { resolveWorkerConfiguredAgentParams } from './worker-configured-agent-preflight' import { waitForWorkerAgentReady } from '../../../../terminal/wait/launched-agent-composer-readiness' @@ -125,20 +126,21 @@ export async function startLocalWorker(args: { : requestedWorktree === 'current' ? await runtime.showManagedTerminalWorkspace(`id:${coordinatorWorktreeId}`) : await runtime.showManagedTerminalWorkspace(requestedWorktree) - const claimsTerminal = terminal - ? await assertExplicitWorkerUsable({ - runtime, - db, - terminal, - from: params.from, - coordinator, - resolvedWorktreeId: resolvedWorktree?.id, - followUpOf: args.followUp?.of - }) - : undefined + if (terminal) { + await assertExplicitWorkerUsable({ + runtime, + db, + terminal, + from: params.from, + coordinator, + resolvedWorktreeId: resolvedWorktree?.id + }) + } const hostMode = resolveWorkerStartModeOnHost(runtime, args.mode, resolvedWorktree?.id, agent) let mode = chatWorkerMode(await hostMode, chat) + // Synchronous through the Dispatch insert, so nothing else claims the terminal in between. + refuseClaimedWorkerTerminal(db, params.terminal, runtime, args.followUp?.of) const run = args.bindRun() const started = createLocalWorkerStartDispatch(args, { params, @@ -149,8 +151,7 @@ export async function startLocalWorker(args: { resolvedWorktreeId: resolvedWorktree?.id, repoSelector: creationTarget?.repoSelector, agent, - launchReceipt: launch.receipt, - claimsTerminal + launchReceipt: launch.receipt }) releaseRetriedWorkerTerminal(runtime, db, params) const effects: WorkerEffect[] = []