fix(orchestration): refuse a busy terminal with no await before the Run binds

This commit is contained in:
Jinwoo-H
2026-11-08 15:17:07 -05:00
parent 3ee53c89d5
commit 2888cf4e36
5 changed files with 33 additions and 53 deletions
@@ -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
@@ -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<OrcaRuntimeService, 'getTerminalPaneKey'>,
/** 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)
}
}
@@ -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<WorkerTerminalClaim> {
}): Promise<void> {
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. */
@@ -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
})
}
@@ -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[] = []