@@ -2270,6 +2270,182 @@ describe("AcpAdapterV2", () => {
22702270 } ) . pipe ( Effect . provide ( layerTest ) , Effect . scoped ) ,
22712271 ) ;
22722272
2273+ const textChunk = (
2274+ sessionUpdate : "agent_message_chunk" | "agent_thought_chunk" ,
2275+ messageId : string | undefined ,
2276+ text : string ,
2277+ ) => ( {
2278+ sessionUpdate,
2279+ ...( messageId === undefined ? { } : { messageId } ) ,
2280+ content : { type : "text" as const , text } ,
2281+ } ) ;
2282+ const answer = ( messageId : string | undefined , text : string ) =>
2283+ textChunk ( "agent_message_chunk" , messageId , text ) ;
2284+ const thought = ( messageId : string | undefined , text : string ) =>
2285+ textChunk ( "agent_thought_chunk" , messageId , text ) ;
2286+ const agentMessage = ( messageId : string , text : string ) => ( {
2287+ sessionUpdate : "agent_message" as const ,
2288+ messageId,
2289+ content : [ { type : "text" as const , text } ] ,
2290+ } ) ;
2291+ type ScriptedTextStep = EffectAcpSchema . SessionUpdate | { readonly advance : Duration . Input } ;
2292+
2293+ const runScriptedTextTurn = Effect . fnUntraced ( function * ( steps : ReadonlyArray < ScriptedTextStep > ) {
2294+ type RuntimeService = AcpSessionRuntime . AcpSessionRuntime [ "Service" ] ;
2295+ let handler : Parameters < RuntimeService [ "handleSessionUpdate" ] > [ 0 ] | undefined ;
2296+ const instanceId = ProviderInstanceId . make ( "acp-named-text-resume" ) ;
2297+ const path = yield * Path . Path ;
2298+ const adapter = yield * makeAcpAdapterV2 ( {
2299+ instanceId,
2300+ selfInvocation : yield * resolveSelfInvocation ( ) ,
2301+ flavor : {
2302+ driver : ACP_TEST_DRIVER ,
2303+ capabilities : AcpProviderCapabilitiesV2 ,
2304+ makeRuntime : makeMockRuntime ( {
2305+ childProcessSpawner : yield * ChildProcessSpawner . ChildProcessSpawner ,
2306+ mockAgentPath : yield * path . fromFileUrl (
2307+ new URL ( "../../../scripts/acp-mock-agent.ts" , import . meta. url ) ,
2308+ ) ,
2309+ wrapRuntime : ( runtime ) => ( {
2310+ ...runtime ,
2311+ handleSessionUpdate : ( next ) =>
2312+ Effect . sync ( ( ) => {
2313+ handler = next ;
2314+ } ) . pipe ( Effect . andThen ( runtime . handleSessionUpdate ( next ) ) ) ,
2315+ prompt : ( ) =>
2316+ Effect . gen ( function * ( ) {
2317+ assert . isDefined ( handler ) ;
2318+ for ( const step of steps ) {
2319+ if ( "advance" in step ) yield * TestClock . adjust ( step . advance ) ;
2320+ else yield * handler ! ( { sessionId : "mock-session-1" , update : step } ) ;
2321+ }
2322+ return { stopReason : "end_turn" as const } ;
2323+ } ) ,
2324+ } ) ,
2325+ } ) ,
2326+ } ,
2327+ } ) ;
2328+ const threadId = ThreadId . make ( "thread-acp-named-text-resume" ) ;
2329+ const modelSelection = { instanceId, model : "default" } ;
2330+ const runtimePolicy = ProviderAdapter . ProviderAdapterV2RuntimePolicy . make ( {
2331+ runtimeMode : "full-access" ,
2332+ interactionMode : "default" ,
2333+ cwd : process . cwd ( ) ,
2334+ } ) ;
2335+ const runtime = yield * adapter . openSession ( {
2336+ threadId,
2337+ providerSessionId : ProviderSessionId . make ( "session-acp-named-text-resume" ) ,
2338+ modelSelection,
2339+ runtimePolicy,
2340+ } ) ;
2341+ const providerThread = yield * runtime . ensureThread ( { threadId, modelSelection, runtimePolicy } ) ;
2342+ yield * runtime . startTurn (
2343+ makeTurnInput ( {
2344+ threadId,
2345+ providerThread,
2346+ instanceId,
2347+ runtimePolicy,
2348+ now : yield * DateTime . now ,
2349+ ordinal : 1 ,
2350+ } ) ,
2351+ ) ;
2352+ return yield * runtime . events . pipe (
2353+ Stream . takeUntil ( ( event ) => event . type === "turn.terminal" ) ,
2354+ Stream . runCollect ,
2355+ ) ;
2356+ } ) ;
2357+
2358+ it . effect . each ( [
2359+ {
2360+ label : "a thought with its own id interrupts a named assistant message" ,
2361+ steps : [
2362+ answer ( "answer" , "First. " ) ,
2363+ { advance : "1 second" } ,
2364+ thought ( "thought" , "Thinking" ) ,
2365+ answer ( "answer" , "Second." ) ,
2366+ ] ,
2367+ expected : [
2368+ [ "assistant_message" , "First. Second." ] ,
2369+ [ "reasoning" , "Thinking" ] ,
2370+ ] ,
2371+ } ,
2372+ {
2373+ label : "named assistant messages interleave" ,
2374+ steps : [ answer ( "a" , "A1 " ) , answer ( "b" , "B1" ) , answer ( "a" , "A2" ) ] ,
2375+ expected : [
2376+ [ "assistant_message" , "A1 A2" ] ,
2377+ [ "assistant_message" , "B1" ] ,
2378+ ] ,
2379+ } ,
2380+ {
2381+ label : "a resumed message continues from authoritative text" ,
2382+ steps : [
2383+ answer ( "answer" , "stale" ) ,
2384+ thought ( "thought" , "Thinking" ) ,
2385+ agentMessage ( "answer" , "fresh " ) ,
2386+ thought ( "thought" , " more" ) ,
2387+ answer ( "answer" , "tail" ) ,
2388+ ] ,
2389+ expected : [
2390+ [ "assistant_message" , "fresh tail" ] ,
2391+ [ "reasoning" , "Thinking more" ] ,
2392+ ] ,
2393+ } ,
2394+ ] satisfies ReadonlyArray < {
2395+ readonly label : string ;
2396+ readonly steps : ReadonlyArray < ScriptedTextStep > ;
2397+ readonly expected : ReadonlyArray < readonly [ string , string ] > ;
2398+ } > ) ( "resumes ACP text segments when $label" , ( { steps, expected } ) =>
2399+ Effect . gen ( function * ( ) {
2400+ const events = yield * runScriptedTextTurn ( steps ) ;
2401+ const history = new Map < string , Array < OrchestrationV2TurnItem > > ( ) ;
2402+ for ( const event of events ) {
2403+ if ( event . type !== "turn_item.updated" ) continue ;
2404+ const item = event . turnItem ;
2405+ if ( item . nativeItemRef ?. nativeId ?. includes ( ":turn:" ) !== true ) continue ;
2406+ history . set ( item . id , [ ...( history . get ( item . id ) ?? [ ] ) , item ] ) ;
2407+ }
2408+ const texts = [ ...history . values ( ) ] . flatMap ( ( snapshots ) => {
2409+ const last = snapshots . at ( - 1 ) ;
2410+ return last !== undefined && "text" in last ? [ [ last . type , last . text ] ] : [ ] ;
2411+ } ) ;
2412+ assert . deepEqual ( texts , expected ) ;
2413+ for ( const snapshots of history . values ( ) ) {
2414+ assert . equal ( new Set ( snapshots . map ( ( item ) => item . ordinal ) ) . size , 1 ) ;
2415+ assert . equal ( new Set ( snapshots . map ( ( item ) => DateTime . formatIso ( item . startedAt ! ) ) ) . size , 1 ) ;
2416+ assert . equal ( snapshots . at ( - 1 ) ?. status , "completed" ) ;
2417+ }
2418+ } ) . pipe ( Effect . provide ( layerTest ) , Effect . scoped ) ,
2419+ ) ;
2420+
2421+ it . effect ( "ignores a text flush scheduled before an ACP segment resumed" , ( ) =>
2422+ Effect . gen ( function * ( ) {
2423+ const events = yield * runScriptedTextTurn ( [
2424+ answer ( "answer" , "A" ) ,
2425+ answer ( "answer" , "B" ) ,
2426+ thought ( "thought" , "Thinking" ) ,
2427+ { advance : "30 millis" } ,
2428+ answer ( "answer" , "C" ) ,
2429+ answer ( "answer" , "D" ) ,
2430+ { advance : "20 millis" } ,
2431+ answer ( "answer" , "E" ) ,
2432+ { advance : "30 millis" } ,
2433+ ] ) ;
2434+ const snapshots = events . flatMap ( ( event ) =>
2435+ event . type === "message.updated" && event . message . role === "assistant"
2436+ ? [ [ event . message . text , event . message . streaming ] ]
2437+ : [ ] ,
2438+ ) ;
2439+ assert . deepEqual ( snapshots , [
2440+ [ "A" , true ] ,
2441+ [ "AB" , false ] ,
2442+ [ "ABC" , true ] ,
2443+ [ "ABCDE" , true ] ,
2444+ [ "ABCDE" , false ] ,
2445+ ] ) ;
2446+ } ) . pipe ( Effect . provide ( layerTest ) , Effect . scoped ) ,
2447+ ) ;
2448+
22732449 it . effect ( "starts a new replay message after ACP v2 plan boundaries" , ( ) =>
22742450 Effect . gen ( function * ( ) {
22752451 const childProcessSpawner = yield * ChildProcessSpawner . ChildProcessSpawner ;
0 commit comments