From 32d592daea33685c29284b47166c2bfb84700288 Mon Sep 17 00:00:00 2001 From: Jinwoo-H Date: Tue, 10 Nov 2026 05:20:10 -0500 Subject: [PATCH] fix(orchestration): review fixes for the forwarder: retry-of stays local, legacy retries stay legacy, unknown outcomes stay fenced, refused starts leave no Task --- .../db/remote-owned/remote-placements.ts | 18 ++++++++++-- .../remote-owned/remote-owned-route.test.ts | 27 +++++++++++++++++ .../remote-owned/remote-owned-route.ts | 29 ++++++++++++++----- .../remote-owned/remote-owned-start.ts | 22 ++++++++++---- 4 files changed, 79 insertions(+), 17 deletions(-) diff --git a/src/main/runtime/orchestration/db/remote-owned/remote-placements.ts b/src/main/runtime/orchestration/db/remote-owned/remote-placements.ts index 63a28ba4975..ba39c273e2b 100644 --- a/src/main/runtime/orchestration/db/remote-owned/remote-placements.ts +++ b/src/main/runtime/orchestration/db/remote-owned/remote-placements.ts @@ -127,16 +127,28 @@ export function reserveRemotePlacement( } } -/** The server said the start applied nothing, so the Task goes back to where it was and is free to start again. */ +/** + * The server said the start applied nothing, so the Task goes back to where it was and is free to + * start again. A Task the start itself created goes away, as a refused local start creates none. + */ export function releaseRemotePlacement( db: OrchestrationDb, taskId: string, - requestKey: string + requestKey: string, + { createdByStart = false }: { createdByStart?: boolean } = {} ): void { const transaction = beginLifecycleWriteTransaction(db.db, 'release_remote_placement') try { const placement = getRemotePlacement(db, taskId) - if (placement?.state === 'reserved' && placement.request_key === requestKey) { + if ( + createdByStart && + placement?.state === 'reserved' && + placement.request_key === requestKey && + !placement.dispatch_id + ) { + db.db.prepare('DELETE FROM remote_placements WHERE task_id = ?').run(taskId) + db.db.prepare('DELETE FROM tasks WHERE id = ?').run(taskId) + } else if (placement?.state === 'reserved' && placement.request_key === requestKey) { // A refused retry leaves the attempt it retried as the Task's current one. if (placement.dispatch_id) { db.db diff --git a/src/main/runtime/orchestration/federation/remote-owned/remote-owned-route.test.ts b/src/main/runtime/orchestration/federation/remote-owned/remote-owned-route.test.ts index 246c9433726..fba0ac13967 100644 --- a/src/main/runtime/orchestration/federation/remote-owned/remote-owned-route.test.ts +++ b/src/main/runtime/orchestration/federation/remote-owned/remote-owned-route.test.ts @@ -189,6 +189,8 @@ describe('commands about a worker a paired server owns', () => { ) const refusedTask = String((sent[0].params as { adoptTaskId: string }).adoptTaskId) expect(getRemotePlacement(db, refusedTask)).toBeUndefined() + // As a refused local start creates no Task, a refused start there leaves none here. + expect(db.getTask(refusedTask)).toBeUndefined() const lost = await call( 'orchestration.workerStart', @@ -218,6 +220,31 @@ describe('commands about a worker a paired server owns', () => { expect(getRemoteDispatch(db, 'ctx_b1')).toBeUndefined() }) + it('keeps the start fenced for the orphan lookup when the server says its outcome is unknown', async () => { + answers['orchestration.workerStart'] = [{ error: 'operation_unknown' }] + await call( + 'orchestration.workerStart', + { from: 'term_coord', on: 'devB', spec: 'unknown', worktree: 'new-top-level' }, + { requestId: REQUEST } + ) + + const task = String(sent[0].params.adoptTaskId) + expect(getRemotePlacement(db, task)).toMatchObject({ state: 'reserved', outcome_unknown: 1 }) + }) + + it('never sends a start that names a remote Dispatch to retry without --on', async () => { + const receipt = await startOnB() + const retry = await call('orchestration.workerStart', { + from: 'term_coord', + task: receipt.taskId, + retryOf: 'ctx_b1', + agent: 'claude' + }) + + expect(retry.ok).toBe(false) + expect(sent.filter((s) => s.method === 'orchestration.workerStart')).toHaveLength(1) + }) + it('scopes the retry key to the caller: two terminals reusing an ID never collide, one retrying gets its own key', async () => { const receipt = await startOnB() answers['orchestration.workerRelease'] = [ diff --git a/src/main/runtime/orchestration/federation/remote-owned/remote-owned-route.ts b/src/main/runtime/orchestration/federation/remote-owned/remote-owned-route.ts index 15c496bf43a..dc72bb81d56 100644 --- a/src/main/runtime/orchestration/federation/remote-owned/remote-owned-route.ts +++ b/src/main/runtime/orchestration/federation/remote-owned/remote-owned-route.ts @@ -65,13 +65,12 @@ export async function routeRemoteOwnedCall( } const values: Values = params && typeof params === 'object' ? { ...params } : {} const db = runtime.getOrchestrationDb() - const on = access.server && stringField(values, access.server) - if (on !== undefined) { - return routeServerCall(runtime, request, access, values, ctx, on) - } - const subject = remoteSubject(db, access, values) + // A start goes to a server only by --on; a remote Dispatch it names (--retry-of) stays this Orca's claim to check. + const subject = + request.method === 'orchestration.workerStart' ? undefined : remoteSubject(db, access, values) if (!subject) { - return LOCAL + const on = access.server && stringField(values, access.server) + return on === undefined ? LOCAL : routeServerCall(runtime, request, access, values, ctx, on) } checkDeclaredOrchestrationAccess(access, params, ctx) const forward = remoteForwardKey( @@ -89,7 +88,7 @@ export async function routeRemoteOwnedCall( } } -/** `--on `: a start, a list, or a receipt lookup there. */ +/** `--on `: a start, a list, or a receipt lookup there; send and reply --on to anything else stay peer mail. */ async function routeServerCall( runtime: OrcaRuntimeService, request: RpcRequest, @@ -106,7 +105,10 @@ async function routeServerCall( const server = runtime.resolveOrchestrationWorkerServer(on) const db = runtime.getOrchestrationDb() if (request.method === 'orchestration.workerStart') { - if (!(await serverOwnsPlacedWorkers(runtime, server))) { + if ( + holdsLocalReceipt(db, ctx, request.orchestrationRequestId) || + !(await serverOwnsPlacedWorkers(runtime, server)) + ) { // An older server: today's remote attachment, the one legacy branch for new work. return LOCAL } @@ -309,6 +311,17 @@ function openLinkFor( .get(runId, environmentId) as RemoteRunLinkRow | undefined } +/** A retry of a start this Orca already took on today's path stays there, whatever the server runs now. */ +function holdsLocalReceipt( + db: OrchestrationDb, + ctx: RpcContext, + requestId: string | undefined +): boolean { + const caller = + ctx.authenticatedCallerFingerprint ?? db.getOrCreateLocalMutationCallerFingerprint() + return requestId !== undefined && db.getMutationReceipt(caller, requestId) !== undefined +} + function isSessionAddress(value: unknown): boolean { return typeof value === 'string' && value.startsWith(ORCA_SESSION_ADDRESS_PREFIX) } diff --git a/src/main/runtime/orchestration/federation/remote-owned/remote-owned-start.ts b/src/main/runtime/orchestration/federation/remote-owned/remote-owned-start.ts index 78188deedf7..febdbc38e4b 100644 --- a/src/main/runtime/orchestration/federation/remote-owned/remote-owned-start.ts +++ b/src/main/runtime/orchestration/federation/remote-owned/remote-owned-start.ts @@ -141,7 +141,8 @@ export async function startRemoteOwnedWorker( conversationId: link.conversation_id, taskId, error, - key: forward.key + key: forward.key, + createdByStart: !existingTask }) throw inServerTerms(error, { server: server.name, forward }) } @@ -190,11 +191,19 @@ export async function startRemoteOwnedWorker( /** What a start's failure says about its reservation: adopt the Dispatch it names, keep it for the orphan lookup, or free it. */ function settleUnansweredStart( runtime: OrcaRuntimeService, - start: { conversationId: string; taskId: string; key: string; error: unknown } + start: { + conversationId: string + taskId: string + key: string + error: unknown + createdByStart: boolean + } ): void { const db = runtime.getOrchestrationDb() const data = errorData(start.error) const named = typeof data?.dispatchId === 'string' ? data.dispatchId : undefined + const outcomeUnknown = + start.error instanceof OrchestrationError && start.error.code === 'operation_unknown' if (named) { recordRemoteStart(db, { conversationId: start.conversationId, @@ -202,12 +211,13 @@ function settleUnansweredStart( dispatchId: named, requestKey: start.key }) - } else if (serverNeverAnswered(start.error) && data?.effectsApplied !== false) { - markRemoteStartOutcomeUnknown(db, start.taskId, start.key) } else if ( - !(start.error instanceof OrchestrationError && start.error.code === 'operation_unknown') + outcomeUnknown || + (serverNeverAnswered(start.error) && data?.effectsApplied !== false) ) { + markRemoteStartOutcomeUnknown(db, start.taskId, start.key) + } else { // The server's ordinary start refused it, which a start that had launched answers with a receipt. - releaseRemotePlacement(db, start.taskId, start.key) + releaseRemotePlacement(db, start.taskId, start.key, { createdByStart: start.createdByStart }) } }