@@ -7,6 +7,7 @@ import { createDeferred } from "../../../test/helpers/promise.js";
77import { makeAgentAssistantMessage } from "../../agents/test-helpers/agent-message-fixtures.js" ;
88import type { SpawnResult } from "../../process/exec.js" ;
99import { WORKER_PROVIDER_REPLAY_LOCAL_RETRY_MESSAGE } from "../../worker/transcript-message.js" ;
10+ import { STALE_WORKER_BUILD_REASON } from "./admission.js" ;
1011import type { WorkerSessionPlacementStore } from "./placement-store.js" ;
1112import {
1213 WorkerRunnerCapacityError ,
@@ -39,6 +40,80 @@ describe("worker turn launcher failure recovery", () => {
3940 beforeEach ( setupWorkerTurnLauncherTest ) ;
4041 afterEach ( cleanupWorkerTurnLauncherTest ) ;
4142
43+ it ( "projects a stale Gateway build teardown and records its durable placement reason" , async ( ) => {
44+ seedActivePlacement ( ) ;
45+ const terminalReason = `cloud worker disappeared: ${ STALE_WORKER_BUILD_REASON } ` ;
46+ const staleEnvironment : NonNullable < ReturnType < WorkerTurnEnvironmentService [ "get" ] > > = {
47+ ...attachedEnvironment ( ) ,
48+ state : "failed" as const ,
49+ leaseId : null ,
50+ sshEndpoint : null ,
51+ sharedHost : null ,
52+ ownerEpoch : OWNER_EPOCH + 1 ,
53+ attachedSessionIds : [ ] ,
54+ tunnelStatus : "stopped" ,
55+ error : STALE_WORKER_BUILD_REASON ,
56+ } ;
57+ const environments : WorkerTurnEnvironmentService = {
58+ ...unusedEnvironments ( ) ,
59+ get : vi . fn ( ( ) => staleEnvironment ) ,
60+ } ;
61+ const reconcileActivePlacement = vi . fn ( async ( ) => {
62+ const active = placements . get ( SESSION_ID ) ;
63+ if ( active ?. state !== "active" ) {
64+ throw new Error ( "expected active stale-build placement" ) ;
65+ }
66+ const draining = placements . startDrain ( {
67+ sessionId : active . sessionId ,
68+ environmentId : active . environmentId ,
69+ ownerEpoch : active . activeOwnerEpoch ,
70+ expectedGeneration : active . generation ,
71+ } ) ;
72+ if ( draining . state !== "draining" ) {
73+ throw new Error ( "expected draining stale-build placement" ) ;
74+ }
75+ const reconciling = placements . startReconcile ( {
76+ sessionId : draining . sessionId ,
77+ environmentId : draining . environmentId ,
78+ ownerEpoch : draining . activeOwnerEpoch ,
79+ expectedGeneration : draining . generation ,
80+ } ) ;
81+ if ( reconciling . state !== "reconciling" ) {
82+ throw new Error ( "expected reconciling stale-build placement" ) ;
83+ }
84+ placements . fail ( {
85+ sessionId : reconciling . sessionId ,
86+ expectedGeneration : reconciling . generation ,
87+ recoveryError : terminalReason ,
88+ } ) ;
89+ } ) ;
90+ const provider = createWorkerSessionTurnPlacementProvider ( {
91+ environments,
92+ placements,
93+ reconcileActivePlacement,
94+ } ) ;
95+
96+ await expect (
97+ provider . executeTurn (
98+ {
99+ sessionId : SESSION_ID ,
100+ sessionKey : SESSION_KEY ,
101+ agentId : "main" ,
102+ runId : "run-stale-worker-build" ,
103+ } ,
104+ turn ( "run-stale-worker-build" ) ,
105+ async ( ) => ( { meta : { durationMs : 1 } } ) ,
106+ ) ,
107+ ) . rejects . toThrow ( `Worker turn rejected in placement failed: ${ terminalReason } ` ) ;
108+ expect ( reconcileActivePlacement ) . toHaveBeenCalledWith ( ENVIRONMENT_ID ) ;
109+ expect ( placements . get ( SESSION_ID ) ) . toMatchObject ( {
110+ state : "failed" ,
111+ recoveryError : terminalReason ,
112+ terminalReason,
113+ turnClaim : null ,
114+ } ) ;
115+ } ) ;
116+
42117 it ( "keeps an active placement when tunnel startup fails before remote handoff" , async ( ) => {
43118 seedActivePlacement ( ) ;
44119 const acknowledgeCredentialDelivery = vi . fn ( ( ) => true ) ;
0 commit comments