@@ -1336,6 +1336,10 @@ describe("AcpAdapterV2", () => {
13361336 ) ;
13371337 it . live ( "fails an end_turn the outcome check rejects and restarts before the next turn" , ( ) =>
13381338 Effect . gen ( function * ( ) {
1339+ const checking = yield * Deferred . make < void > ( ) ;
1340+ const releaseCheck = yield * Deferred . make < void > ( ) ;
1341+ const cancelled = yield * Deferred . make < void > ( ) ;
1342+ const idAllocator = yield * IdAllocator . IdAllocatorV2 ;
13391343 const instanceId = ProviderInstanceId . make ( "acp-test-prompt-outcome" ) ;
13401344 const threadId = ThreadId . make ( "thread-acp-prompt-outcome" ) ;
13411345 const quota = {
@@ -1345,6 +1349,12 @@ describe("AcpAdapterV2", () => {
13451349 retryable : true ,
13461350 resetAt : "2030-01-01T00:00:00.000Z" ,
13471351 } as const ;
1352+ const poisoned = {
1353+ class : "provider_error" ,
1354+ message : "Saved history is unusable." ,
1355+ code : "INVALID_THINKING_SIGNATURE" ,
1356+ retryable : true ,
1357+ } as const ;
13481358 // The runtime ordinal of each prompt, and the session each check watched.
13491359 const promptRuntimes : Array < number > = [ ] ;
13501360 const promptTexts : Array < string > = [ ] ;
@@ -1358,14 +1368,27 @@ describe("AcpAdapterV2", () => {
13581368 watchPromptOutcome : ( sessionId ) =>
13591369 Effect . sync ( ( ) => {
13601370 watchedSessions . push ( sessionId ) ;
1361- const failed = watchedSessions . length === 1 ;
1362- return ( ) => Effect . succeed ( failed ? quota : undefined ) ;
1371+ const outcome =
1372+ watchedSessions . length === 1
1373+ ? { failure : quota , threadDisposition : "reusable" as const }
1374+ : watchedSessions . length >= 3
1375+ ? { failure : poisoned , threadDisposition : "broken" as const }
1376+ : undefined ;
1377+ const interrupted = watchedSessions . length === 4 ;
1378+ return ( ) =>
1379+ interrupted
1380+ ? Deferred . succeed ( checking , undefined ) . pipe (
1381+ Effect . andThen ( Deferred . await ( releaseCheck ) ) ,
1382+ Effect . as ( outcome ) ,
1383+ )
1384+ : Effect . succeed ( outcome ) ;
13631385 } ) ,
13641386 makeRuntime : makeMockRuntime ( {
13651387 childProcessSpawner : yield * ChildProcessSpawner . ChildProcessSpawner ,
13661388 mockAgentPath : yield * bundledMockAgentPath ,
13671389 wrapRuntime : ( runtime , runtimeOrdinal ) => ( {
13681390 ...runtime ,
1391+ cancel : Deferred . succeed ( cancelled , undefined ) . pipe ( Effect . asVoid ) ,
13691392 prompt : ( payload ) =>
13701393 Effect . sync ( ( ) => {
13711394 promptRuntimes . push ( runtimeOrdinal ) ;
@@ -1409,26 +1432,82 @@ describe("AcpAdapterV2", () => {
14091432 ordinal,
14101433 } ) ,
14111434 ) ;
1412- const events = yield * runtime . events . pipe (
1413- Stream . takeUntil ( ( event ) => event . type === "turn.terminal" ) ,
1414- Stream . runCollect ,
1435+ return Array . from (
1436+ yield * runtime . events . pipe (
1437+ Stream . takeUntil ( ( event ) => event . type === "turn.terminal" ) ,
1438+ Stream . runCollect ,
1439+ ) ,
14151440 ) ;
1416- return events . at ( - 1 ) ;
14171441 } ) ;
14181442
1419- const failed = yield * runTurn ( 1 ) ;
1443+ const failed = ( yield * runTurn ( 1 ) ) . at ( - 1 ) ;
14201444 if ( failed ?. type !== "turn.terminal" || failed . status !== "failed" ) {
14211445 return assert . fail ( "expected a failed terminal" ) ;
14221446 }
14231447 assert . deepEqual ( failed . failure , quota ) ;
1424- const completed = yield * runTurn ( 2 ) ;
1448+ const completed = ( yield * runTurn ( 2 ) ) . at ( - 1 ) ;
14251449 assert . equal ( completed ?. type === "turn.terminal" ? completed . status : null , "completed" ) ;
14261450 assert . deepEqual ( promptRuntimes , [ 1 , 2 ] , "the next turn runs on a fresh runtime" ) ;
14271451 // The fresh runtime has no record of what reached the agent, so it sends T3's notes again.
14281452 assert . include ( promptTexts [ 1 ] ?? "" , "<runtime_info>" ) ;
14291453 assert . include ( promptTexts [ 1 ] ?? "" , "T3 Code interaction mode: Default" ) ;
14301454 assert . equal ( watchedSessions . length , 2 ) ;
14311455 assert . equal ( watchedSessions [ 0 ] , watchedSessions [ 1 ] ) ;
1456+
1457+ // Poisoned native history marks the thread broken; its persisted error row is never resumed.
1458+ const poisonedEvents = yield * runTurn ( 3 ) ;
1459+ const broken = poisonedEvents . at ( - 1 ) ;
1460+ if ( broken ?. type !== "turn.terminal" || broken . status !== "failed" ) {
1461+ return assert . fail ( "expected a failed terminal" ) ;
1462+ }
1463+ assert . deepEqual ( broken . failure , poisoned ) ;
1464+ assert . equal ( broken . threadDisposition , "broken" ) ;
1465+ const brokenThread = poisonedEvents . findLast (
1466+ ( event ) => event . type === "provider_thread.updated" ,
1467+ ) ;
1468+ assert . equal (
1469+ brokenThread ?. type === "provider_thread.updated"
1470+ ? brokenThread . providerThread . status
1471+ : null ,
1472+ "error" ,
1473+ ) ;
1474+ const resumed = yield * Effect . result (
1475+ runtime . resumeThread ( {
1476+ threadId,
1477+ providerThread : { ...providerThread , status : "error" } ,
1478+ modelSelection,
1479+ runtimePolicy,
1480+ } ) ,
1481+ ) ;
1482+ assert . equal (
1483+ resumed . _tag === "Failure" ? resumed . failure . _tag : null ,
1484+ "ProviderAdapterResumeThreadError" ,
1485+ ) ;
1486+
1487+ const turn = yield * Effect . forkChild ( runTurn ( 4 ) ) ;
1488+ yield * Deferred . await ( checking ) ;
1489+ const stop = yield * Effect . forkChild (
1490+ runtime . interruptTurn ( {
1491+ providerThread,
1492+ providerTurnId : idAllocator . derive . providerTurn ( {
1493+ driver : ACP_TEST_DRIVER ,
1494+ nativeTurnId : acpScopedNativeId ( instanceId , `${ watchedSessions [ 3 ] } :turn:4` ) ,
1495+ } ) ,
1496+ } ) ,
1497+ ) ;
1498+ yield * Deferred . await ( cancelled ) ;
1499+ yield * Deferred . succeed ( releaseCheck , undefined ) ;
1500+ const interruptedEvents = yield * Fiber . join ( turn ) ;
1501+ yield * Fiber . join ( stop ) ;
1502+ const interrupted = interruptedEvents . at ( - 1 ) ;
1503+ assert . equal (
1504+ interrupted ?. type === "turn.terminal" ? interrupted . status : null ,
1505+ "interrupted" ,
1506+ ) ;
1507+ assert . equal (
1508+ interrupted ?. type === "turn.terminal" ? interrupted . threadDisposition : null ,
1509+ "broken" ,
1510+ ) ;
14321511 } ) . pipe ( Effect . provide ( layerTest ) ) ,
14331512 ) ;
14341513 it . live ( "retains returned usage when Stop finalizes before the prompt callback" , ( ) =>
0 commit comments