@@ -14,6 +14,7 @@ import { resolveSelfInvocation } from "@t3tools/shared/nodeRuntime";
1414import { htmlRenderFromToolItem } from "@t3tools/shared/toolOutput" ;
1515import * as Crypto from "effect/Crypto" ;
1616import * as DateTime from "effect/DateTime" ;
17+ import * as Deferred from "effect/Deferred" ;
1718import * as Effect from "effect/Effect" ;
1819import * as Exit from "effect/Exit" ;
1920import * as FileSystem from "effect/FileSystem" ;
@@ -125,7 +126,10 @@ const layerSession = Layer.mergeAll(
125126) ;
126127
127128describe ( "AntigravityAdapterV2 harness recovery" , ( ) => {
128- it . effect ( "fails despite end_turn and resumes the saved session in a fresh process" , ( ) =>
129+ const recoverHarnessFailure = (
130+ harnessFailure : "1" | "after-end-turn" ,
131+ firstTurnStatus : "failed" | "completed" ,
132+ ) =>
129133 Effect . gen ( function * ( ) {
130134 const childProcessSpawner = yield * ChildProcessSpawner . ChildProcessSpawner ;
131135 const fileSystem = yield * FileSystem . FileSystem ;
@@ -137,15 +141,25 @@ describe("AntigravityAdapterV2 harness recovery", () => {
137141 const workspace = yield * fileSystem . makeTempDirectoryScoped ( ) ;
138142 const requestLogPath = path . join ( workspace , "requests.jsonl" ) ;
139143 let processCount = 0 ;
144+ const firstRuntimeTerminated = yield * Deferred . make < void > ( ) ;
140145 const instanceId = ProviderInstanceId . make ( "antigravity-recovery-test" ) ;
141146 const adapter = yield * makeAntigravityAdapterV2 ( {
142147 instanceId,
143148 selfInvocation : yield * resolveSelfInvocation ( ) ,
144149 makeRuntime : ( input ) =>
145150 Effect . suspend ( ( ) => {
146151 processCount += 1 ;
152+ const processNumber = processCount ;
147153 return makeAntigravityAcpRuntime ( {
148154 ...input ,
155+ onTermination : ( error ) =>
156+ ( input . onTermination ?.( error ) ?? Effect . void ) . pipe (
157+ Effect . andThen (
158+ processNumber === 1
159+ ? Deferred . succeed ( firstRuntimeTerminated , undefined )
160+ : Effect . void ,
161+ ) ,
162+ ) ,
149163 childProcessSpawner,
150164 spawn : {
151165 command : process . execPath ,
@@ -154,7 +168,9 @@ describe("AntigravityAdapterV2 harness recovery", () => {
154168 env : {
155169 T3_ACP_ANTIGRAVITY : "1" ,
156170 T3_ACP_REQUEST_LOG_PATH : requestLogPath ,
157- ...( processCount === 1 ? { T3_ACP_ANTIGRAVITY_HARNESS_FAILURE : "1" } : { } ) ,
171+ ...( processNumber === 1
172+ ? { T3_ACP_ANTIGRAVITY_HARNESS_FAILURE : harnessFailure }
173+ : { } ) ,
158174 } ,
159175 } ,
160176 } ) . pipe ( Effect . provideService ( Crypto . Crypto , crypto ) ) ;
@@ -193,9 +209,11 @@ describe("AntigravityAdapterV2 harness recovery", () => {
193209 yield * session . startTurn (
194210 makeTurnInput ( { threadId, providerThread, instanceId, runtimePolicy, modelSelection, now } ) ,
195211 ) ;
196- const failed = yield * terminal ;
197- assert . equal ( failed . _tag , "Some" ) ;
198- if ( failed . _tag === "Some" ) assert . equal ( failed . value . status , "failed" ) ;
212+ const first = yield * terminal ;
213+ assert . equal ( first . _tag , "Some" ) ;
214+ if ( first . _tag === "Some" ) assert . equal ( first . value . status , firstTurnStatus ) ;
215+ // The adapter must retire the first process even when end_turn won the race.
216+ yield * Deferred . await ( firstRuntimeTerminated ) ;
199217 yield * session . startTurn (
200218 makeTurnInput ( {
201219 threadId,
@@ -211,10 +229,21 @@ describe("AntigravityAdapterV2 harness recovery", () => {
211229 assert . equal ( completed . _tag , "Some" ) ;
212230 if ( completed . _tag === "Some" ) assert . equal ( completed . value . status , "completed" ) ;
213231 assert . equal ( processCount , 2 ) ;
214- const requests = yield * fileSystem . readFileString ( requestLogPath ) ;
215- assert . include ( requests , "session/resume" ) ;
216- assert . include ( requests , "mock-session-1" ) ;
217- } ) . pipe ( Effect . provide ( layerSession ) , Effect . scoped ) ,
232+ const requests = ( yield * fileSystem . readFileString ( requestLogPath ) )
233+ . trim ( )
234+ . split ( "\n" )
235+ . map ( ( line ) => JSON . parse ( line ) as { method ?: string ; params ?: { sessionId ?: string } } ) ;
236+ const resumeRequests = requests . filter ( ( request ) => request . method === "session/resume" ) ;
237+ assert . equal ( resumeRequests . length , 1 ) ;
238+ assert . equal ( resumeRequests [ 0 ] ?. params ?. sessionId , "mock-session-1" ) ;
239+ } ) . pipe ( Effect . provide ( layerSession ) , Effect . scoped ) ;
240+
241+ it . live ( "fails despite end_turn and resumes the saved session in a fresh process" , ( ) =>
242+ recoverHarnessFailure ( "1" , "failed" ) ,
243+ ) ;
244+
245+ it . live ( "resumes in a fresh process when the native failure arrives after end_turn" , ( ) =>
246+ recoverHarnessFailure ( "after-end-turn" , "completed" ) ,
218247 ) ;
219248} ) ;
220249
0 commit comments