Keep a kept-open worker open across follow-ups (#36954)

This commit is contained in:
Jinwoo Hong
2026-11-01 04:26:52 -05:00
committed by GitHub
parent 57454e6d7e
commit f139d70aad
2 changed files with 18 additions and 10 deletions
@@ -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,
@@ -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 () => {