@@ -909,7 +909,11 @@ describe("AcpAdapterV2", () => {
909909 flavor : {
910910 driver : ACP_TEST_DRIVER ,
911911 capabilities : AcpProviderCapabilitiesV2 ,
912- makeRuntime : makeMockRuntime ( { childProcessSpawner, mockAgentPath, protocolEvents } ) ,
912+ makeRuntime : makeMockRuntime ( {
913+ childProcessSpawner,
914+ mockAgentPath,
915+ protocolEvents,
916+ } ) ,
913917 } ,
914918 selfInvocation,
915919 } ) ;
@@ -2324,6 +2328,99 @@ describe("AcpAdapterV2", () => {
23242328 } ) . pipe ( Effect . provide ( layerTest ) , Effect . scoped ) ,
23252329 ) ;
23262330
2331+ it . live ( "creates a fresh ACP identity when recovering an exhausted target" , ( ) =>
2332+ Effect . gen ( function * ( ) {
2333+ const childProcessSpawner = yield * ChildProcessSpawner . ChildProcessSpawner ;
2334+ const path = yield * Path . Path ;
2335+ const selfInvocation = yield * resolveSelfInvocation ( ) ;
2336+ const mockAgentPath = yield * path . fromFileUrl (
2337+ new URL ( "../../../scripts/acp-mock-agent.ts" , import . meta. url ) ,
2338+ ) ;
2339+ const protocolEvents = yield * Queue . unbounded < EffectAcpProtocol . AcpProtocolLogEvent > ( ) ;
2340+ const instanceId = ProviderInstanceId . make ( "acp-test-exhausted-target" ) ;
2341+ const adapter = yield * makeAcpAdapterV2 ( {
2342+ instanceId,
2343+ flavor : {
2344+ driver : ACP_TEST_DRIVER ,
2345+ capabilities : AcpProviderCapabilitiesV2 ,
2346+ makeRuntime : makeMockRuntime ( {
2347+ childProcessSpawner,
2348+ mockAgentPath,
2349+ protocolEvents,
2350+ wrapRuntime : ( runtime , ordinal ) => ( {
2351+ ...runtime ,
2352+ start : ( ) =>
2353+ runtime . start ( ) . pipe (
2354+ Effect . map ( ( started ) => ( {
2355+ ...started ,
2356+ sessionId : `mock-recovery-session-${ ordinal } ` ,
2357+ } ) ) ,
2358+ ) ,
2359+ } ) ,
2360+ } ) ,
2361+ } ,
2362+ selfInvocation,
2363+ } ) ;
2364+ const threadId = ThreadId . make ( "thread-acp-exhausted-target" ) ;
2365+ const runtimePolicy = ProviderAdapter . ProviderAdapterV2RuntimePolicy . make ( {
2366+ runtimeMode : "full-access" ,
2367+ interactionMode : "default" ,
2368+ cwd : process . cwd ( ) ,
2369+ } ) ;
2370+ const modelSelection = { instanceId, model : "default" } as const ;
2371+ const runtime = yield * adapter . openSession ( {
2372+ threadId,
2373+ providerSessionId : ProviderSessionId . make ( "provider-session-acp-exhausted-target" ) ,
2374+ modelSelection,
2375+ runtimePolicy,
2376+ } ) ;
2377+ const original = yield * runtime . ensureThread ( { threadId, modelSelection, runtimePolicy } ) ;
2378+ yield * runtime . startTurn (
2379+ makeTurnInput ( {
2380+ threadId,
2381+ providerThread : original ,
2382+ instanceId,
2383+ runtimePolicy,
2384+ now : yield * DateTime . now ,
2385+ ordinal : 1 ,
2386+ } ) ,
2387+ ) ;
2388+ yield * runtime . events . pipe (
2389+ Stream . takeUntil ( ( event ) => event . type === "turn.terminal" ) ,
2390+ Stream . runDrain ,
2391+ ) ;
2392+ const replacement = yield * runtime . ensureThread ( {
2393+ threadId,
2394+ modelSelection,
2395+ runtimePolicy,
2396+ existingProviderThread : { ...original , nativeThreadRef : null } ,
2397+ forceNewNativeThread : true ,
2398+ } ) ;
2399+ assert . notEqual ( replacement . nativeThreadRef ?. nativeId , original . nativeThreadRef ?. nativeId ) ;
2400+ assert . equal ( replacement . id , original . id ) ;
2401+ assert . equal ( replacement . nativeMetadata ?. itemIdentityVersion , 2 ) ;
2402+ yield * runtime . startTurn (
2403+ makeTurnInput ( {
2404+ threadId,
2405+ providerThread : replacement ,
2406+ instanceId,
2407+ runtimePolicy,
2408+ now : yield * DateTime . now ,
2409+ ordinal : 2 ,
2410+ } ) ,
2411+ ) ;
2412+ const events = yield * runtime . events . pipe (
2413+ Stream . takeUntil ( ( event ) => event . type === "turn.terminal" ) ,
2414+ Stream . runCollect ,
2415+ ) ;
2416+ assert . isTrue (
2417+ events . some ( ( event ) => event . type === "turn.terminal" && event . status === "completed" ) ,
2418+ ) ;
2419+ const methods = yield * pollProtocolMethods ( protocolEvents ) ;
2420+ assert . equal ( methods . filter ( ( method ) => method === "session/new" ) . length , 2 ) ;
2421+ } ) . pipe ( Effect . provide ( layerTest ) , Effect . scoped ) ,
2422+ ) ;
2423+
23272424 it . live ( "preserves new-session fallback when an eager ACP session load is stale" , ( ) =>
23282425 Effect . gen ( function * ( ) {
23292426 const childProcessSpawner = yield * ChildProcessSpawner . ChildProcessSpawner ;
@@ -5158,6 +5255,19 @@ describe("AcpAdapterV2", () => {
51585255 yield * hasPendingBackgroundWork ,
51595256 "carryover live subagent must pin hasPendingBackgroundWork after root settle" ,
51605257 ) ;
5258+ const refusedReplacement = yield * runtime
5259+ . ensureThread ( {
5260+ threadId,
5261+ modelSelection,
5262+ runtimePolicy,
5263+ existingProviderThread : { ...providerThread , nativeThreadRef : null } ,
5264+ forceNewNativeThread : true ,
5265+ } )
5266+ . pipe ( Effect . flip ) ;
5267+ assert . equal ( refusedReplacement . _tag , "ProviderAdapterEnsureThreadError" ) ;
5268+ const preserved = yield * runtime . ensureThread ( { threadId, modelSelection, runtimePolicy } ) ;
5269+ assert . equal ( preserved . nativeThreadRef ?. nativeId , providerThread . nativeThreadRef ?. nativeId ) ;
5270+ assert . isTrue ( yield * hasPendingBackgroundWork ) ;
51615271 assert . isDefined ( sessionUpdateHandler , "session update handler must be wired" ) ;
51625272
51635273 subagentPhase = "complete" ;
0 commit comments