From f139d70aadf2d33c060ab826cbcf7ba3a30bc8cd Mon Sep 17 00:00:00 2001 From: Jinwoo Hong <73622457+Jinwoo-H@users.noreply.github.com> Date: Sun, 1 Nov 2026 04:26:52 -0500 Subject: [PATCH] Keep a kept-open worker open across follow-ups (#36954) --- .../worker-terminal-resource-store.ts | 14 +++++++------- .../worker/worker-auto-release.test.ts | 14 +++++++++++--- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/main/runtime/orchestration/db/worker-terminal/worker-terminal-resource-store.ts b/src/main/runtime/orchestration/db/worker-terminal/worker-terminal-resource-store.ts index df2ab3fd573..fc93f833e22 100644 --- a/src/main/runtime/orchestration/db/worker-terminal/worker-terminal-resource-store.ts +++ b/src/main/runtime/orchestration/db/worker-terminal/worker-terminal-resource-store.ts @@ -2,10 +2,7 @@ import type { WorkerTerminalResourceRow, WorkerTerminalOwnershipState } from '../../worker-terminal-ownership' -import { - USER_KEPT_WORKER_TERMINAL_SQL, - WORKER_SETTLED_STATES -} from '../../worker-terminal-ownership' +import { WORKER_SETTLED_STATES } from '../../worker-terminal-ownership' import { OrchestrationError } from '../../orchestration-error' import { generateId } from '../generated-id' import type { OrchestrationDb } from '../orchestration-db' @@ -169,6 +166,9 @@ export function recordWorkerTerminalRecoveryAttempt( return this.getWorkerTerminalResource(resourceId) } +const KEPT_OPEN_SQL = + "release_state = 'retained' AND retained_reason IN ('user_kept', 'user_requested')" + // Transfer an exact settled resource and fence the prior owner; caller owns the transaction. export function transferWorkerTerminalResourceStatement( this: OrchestrationDb, @@ -192,14 +192,14 @@ export function transferWorkerTerminalResourceStatement( } const priorOwners = JSON.parse(resource.prior_owner_dispatch_ids) as string[] priorOwners.push(resource.owner_dispatch_id) - // A coordinator's retain belongs to its Dispatch; the user's Keep open belongs to the worker. + // Keep open, from the user or the coordinator, belongs to the worker until it is released. this.db .prepare( `UPDATE worker_terminal_resources SET owner_dispatch_id = ?, prior_owner_dispatch_ids = ?, - release_state = CASE WHEN ${USER_KEPT_WORKER_TERMINAL_SQL} + release_state = CASE WHEN ${KEPT_OPEN_SQL} THEN release_state ELSE 'not_requested' END, - retained_reason = CASE WHEN ${USER_KEPT_WORKER_TERMINAL_SQL} + retained_reason = CASE WHEN ${KEPT_OPEN_SQL} THEN retained_reason END, release_requested_at = NULL, release_completed_at = NULL, release_error = NULL, auto_release_at = NULL, auto_release_dispatch_id = NULL, diff --git a/src/main/runtime/rpc/methods/orchestration/worker/worker-auto-release.test.ts b/src/main/runtime/rpc/methods/orchestration/worker/worker-auto-release.test.ts index 44fdd33bcd4..0ddee8ff638 100644 --- a/src/main/runtime/rpc/methods/orchestration/worker/worker-auto-release.test.ts +++ b/src/main/runtime/rpc/methods/orchestration/worker/worker-auto-release.test.ts @@ -81,18 +81,26 @@ describe('orchestration worker automatic release', () => { }) }) - it('keeps a terminal worker-retain claimed before the deadline', async () => { + it('keeps a worker-retain terminal through follow-up work until the coordinator releases it', async () => { setupWithFakeClock() const { dispatchId } = await h.startSettledWorker() await h.call('orchestration.workerRetain', { dispatch: dispatchId }) await vi.advanceTimersByTimeAsync(2 * GRACE_MS) - expect(h.runtime.closeTerminal).not.toHaveBeenCalled() - expect(h.db.getWorkerTerminalResourceByOwner(dispatchId)).toMatchObject({ + + const followUp = await h.startWorker({ terminal: 'term_worker' }) + h.settle(followUp.taskId, followUp.dispatchId, 'succeeded') + await vi.advanceTimersByTimeAsync(2 * GRACE_MS) + expect(h.runtime.closeTerminal).not.toHaveBeenCalled() + expect(h.db.getWorkerTerminalResourceByOwner(followUp.dispatchId)).toMatchObject({ release_state: 'retained', + retained_reason: 'user_requested', auto_release_at: null }) + + await h.call('orchestration.workerRelease', { dispatch: followUp.dispatchId }) + await vi.waitFor(() => expect(h.runtime.closeTerminal).toHaveBeenCalledTimes(1)) }) it("closes a worker the user typed into on time; an older client's typing report keeps nothing", async () => {