11import { assert } from "@effect/vitest" ;
2- import type { OrchestrationV2ThreadProjection , ProviderReplayTranscript } from "@t3tools/contracts" ;
2+ import type {
3+ OrchestrationV2DomainEvent ,
4+ OrchestrationV2ThreadProjection ,
5+ ProviderReplayTranscript ,
6+ } from "@t3tools/contracts" ;
37
48import type { OrchestratorV2ScenarioResult } from "../../OrchestratorScenario.ts" ;
59import {
@@ -12,6 +16,11 @@ import { CLAUDE_BACKGROUND_SUBAGENT_AFTER_ROOT_PROMPT } from "./input.ts";
1216
1317const SUBAGENT_TEXTS = [ "SUB_STEP_1" , "SUB_STEP_2" , "SUB_FINAL_REPORT" ] ;
1418const SUBAGENT_COMMANDS = [ "sleep 3 && echo SUB_DONE_1" , "sleep 3 && echo SUB_DONE_2" ] ;
19+ // The subagent's own task_progress, one per step, as the recording reports it.
20+ const SUBAGENT_PROGRESS = [
21+ "Running Sleep 3 seconds then echo SUB_DONE_1" ,
22+ "Running Sleep 3 seconds then echo SUB_DONE_2" ,
23+ ] ;
1524
1625function assistantTexts ( projection : OrchestrationV2ThreadProjection ) : ReadonlyArray < string > {
1726 return projection . turnItems . flatMap ( ( item ) =>
@@ -97,26 +106,62 @@ export function assertClaudeBackgroundSubagentAfterRootOutput(
97106 `subagent command ${ command } must be in the child thread` ,
98107 ) ;
99108 }
100- // The continuation is queued as soon as the wake arrives; it drains the
101- // wake buffer when it starts.
109+ // The subagent works while the root is idle, so its steps and progress are
110+ // stored as they happen. Its end starts continuation run 2, which drains
111+ // the wake buffer; none of the subagent's work may wait there for it.
102112 const continuationStartIndex = result . domainEvents . findIndex (
103113 ( event ) =>
104114 event . type === "run.updated" &&
105115 event . payload . id === projection . runs [ 1 ] ?. id &&
106116 event . payload . status === "starting" ,
107117 ) ;
108- const firstCommandDoneIndex = result . domainEvents . findIndex (
109- ( event ) =>
110- event . type === "turn-item.updated" &&
111- event . payload . threadId === subagent . childThreadId &&
112- event . payload . type === "command_execution" &&
113- event . payload . status === "completed" &&
114- JSON . stringify ( event . payload . input ) . includes ( SUBAGENT_COMMANDS [ 0 ] ?? "" ) ,
115- ) ;
116- assert . isAtLeast ( firstCommandDoneIndex , 0 ) ;
117- assert . isBelow (
118- firstCommandDoneIndex ,
119- continuationStartIndex ,
120- "the subagent's work is stored while the root is idle, not when its end wakes the root" ,
118+ assert . isAtLeast ( continuationStartIndex , 0 ) ;
119+ const assertStoredWhileIdle = (
120+ label : string ,
121+ matches : ( event : OrchestrationV2DomainEvent ) => boolean ,
122+ ) => {
123+ const index = result . domainEvents . findIndex ( matches ) ;
124+ assert . isAtLeast ( index , 0 , `${ label } is stored` ) ;
125+ assert . isBelow ( index , continuationStartIndex , `${ label } is stored before the root wakes` ) ;
126+ } ;
127+ for ( const text of SUBAGENT_TEXTS ) {
128+ assertStoredWhileIdle (
129+ `subagent text ${ text } ` ,
130+ ( event ) =>
131+ event . type === "message.updated" &&
132+ event . payload . threadId === subagent . childThreadId &&
133+ event . payload . text . trim ( ) === text ,
134+ ) ;
135+ }
136+ for ( const command of SUBAGENT_COMMANDS ) {
137+ assertStoredWhileIdle (
138+ `completed subagent command ${ command } ` ,
139+ ( event ) =>
140+ event . type === "turn-item.updated" &&
141+ event . payload . threadId === subagent . childThreadId &&
142+ event . payload . type === "command_execution" &&
143+ event . payload . status === "completed" &&
144+ event . payload . input . includes ( command ) ,
145+ ) ;
146+ }
147+ for ( const progress of SUBAGENT_PROGRESS ) {
148+ assertStoredWhileIdle (
149+ `subagent progress "${ progress } "` ,
150+ ( event ) =>
151+ event . type === "subagent.updated" &&
152+ event . payload . id === subagent . id &&
153+ event . payload . progress === progress ,
154+ ) ;
155+ }
156+ // The drain stores nothing in the child thread, so nothing is stored twice.
157+ assert . isFalse (
158+ result . domainEvents
159+ . slice ( continuationStartIndex )
160+ . some (
161+ ( event ) =>
162+ ( event . type === "message.updated" || event . type === "turn-item.updated" ) &&
163+ event . payload . threadId === subagent . childThreadId ,
164+ ) ,
165+ "the continuation run stores none of the subagent's work" ,
121166 ) ;
122167}
0 commit comments