@@ -482,6 +482,155 @@ it.effect.each(deliveries)(
482482 ) ,
483483) ;
484484
485+ it . effect ( "persists root assistant text before the Agent card it introduces" , ( ) =>
486+ Effect . scoped (
487+ Effect . gen ( function * ( ) {
488+ const cwd = yield * checkpointWorkspace ( "claude-transcript-order" ) ;
489+ const sdkMessages = yield * Queue . unbounded < SDKMessage > ( ) ;
490+ const adapter = yield * ClaudeAdapterV2 . makeClaudeAdapterV2 ( {
491+ instanceId : modelSelection . instanceId ,
492+ settings,
493+ environment : { } ,
494+ attachmentsDir : cwd ,
495+ fileSystem : yield * FileSystem . FileSystem ,
496+ path : yield * Path . Path ,
497+ crypto : yield * Crypto . Crypto ,
498+ idAllocator : yield * IdAllocator . IdAllocatorV2 ,
499+ queryRunner : {
500+ allocateSessionId : Effect . succeed ( sessionId ) ,
501+ open : ( ) =>
502+ Effect . succeed ( {
503+ messages : Stream . fromQueue ( sdkMessages ) ,
504+ offer : ( ) => Effect . void ,
505+ setModel : ( ) => Effect . void ,
506+ applyFlagSettings : ( ) => Effect . void ,
507+ setPermissionMode : ( ) => Effect . void ,
508+ backgroundTasks : ( ) => Effect . succeed ( false ) ,
509+ stopTask : ( ) => Effect . void ,
510+ interrupt : Effect . void ,
511+ close : Effect . void ,
512+ } ) ,
513+ forkSession : ( ) => Effect . die ( "unused" ) ,
514+ subagentLaunchToolUseId : ( ) => Effect . succeed ( null ) ,
515+ assertComplete : Effect . void ,
516+ } ,
517+ } ) ;
518+ yield * Effect . gen ( function * ( ) {
519+ const orchestrator = yield * Orchestrator . OrchestratorV2 ;
520+ const worker = yield * EffectWorker . OrchestrationEffectWorkerV2 ;
521+ const threadId = ThreadId . make ( "thread:transcript-order" ) ;
522+ const watch = ( predicate : ( event : OrchestrationV2DomainEvent ) => boolean ) =>
523+ orchestrator . streamDomainEvents . pipe (
524+ Stream . filter ( predicate ) ,
525+ Stream . take ( 1 ) ,
526+ Stream . runDrain ,
527+ Effect . forkScoped ,
528+ ) ;
529+ yield * orchestrator . dispatch ( {
530+ type : "thread.create" ,
531+ commandId : CommandId . make ( "create-transcript-order" ) ,
532+ threadId,
533+ projectId : ProjectId . make ( "project:transcript-order" ) ,
534+ title : "Transcript order" ,
535+ modelSelection,
536+ runtimeMode : "full-access" ,
537+ interactionMode : "default" ,
538+ branch : null ,
539+ worktreePath : cwd ,
540+ createdBy : "user" ,
541+ creationSource : "web" ,
542+ } ) ;
543+ const running = yield * watch (
544+ ( event ) => event . type === "provider-turn.updated" && event . payload . status === "running" ,
545+ ) ;
546+ yield * orchestrator . dispatch ( {
547+ type : "message.dispatch" ,
548+ commandId : CommandId . make ( "start-transcript-order" ) ,
549+ threadId,
550+ messageId : MessageId . make ( "start-transcript-order" ) ,
551+ text : "Start background research." ,
552+ attachments : [ ] ,
553+ dispatchMode : { type : "start_immediately" } ,
554+ createdBy : "user" ,
555+ creationSource : "web" ,
556+ } ) ;
557+ yield * worker . drain ( ) ;
558+ yield * Fiber . join ( running ) ;
559+ const settled = yield * watch (
560+ ( event ) => event . type === "run.updated" && event . payload . status === "waiting" ,
561+ ) ;
562+ const introduction = "I will start a research agent." ;
563+ yield * Queue . offerAll ( sdkMessages , [
564+ frame ( {
565+ type : "assistant" ,
566+ uuid : "research-introduction" ,
567+ session_id : sessionId ,
568+ parent_tool_use_id : null ,
569+ message : {
570+ id : "research-introduction" ,
571+ role : "assistant" ,
572+ type : "message" ,
573+ model : modelSelection . model ,
574+ content : [
575+ { type : "text" , text : introduction } ,
576+ {
577+ type : "tool_use" ,
578+ id : "research-launch" ,
579+ name : "Agent" ,
580+ input : {
581+ description : "Research" ,
582+ prompt : "Research the task." ,
583+ subagent_type : "general-purpose" ,
584+ run_in_background : true ,
585+ } ,
586+ } ,
587+ ] ,
588+ stop_reason : "tool_use" ,
589+ stop_sequence : null ,
590+ usage : { input_tokens : 1 , output_tokens : 1 } ,
591+ } ,
592+ } ) ,
593+ frame ( {
594+ type : "system" ,
595+ subtype : "task_started" ,
596+ task_id : "research" ,
597+ tool_use_id : "research-launch" ,
598+ description : "Research" ,
599+ task_type : "local_agent" ,
600+ subagent_type : "general-purpose" ,
601+ is_backgrounded : true ,
602+ uuid : "research-started" ,
603+ session_id : sessionId ,
604+ } ) ,
605+ result ( "research-root-finished" , false ) ,
606+ ] ) ;
607+ yield * Fiber . join ( settled ) ;
608+ yield * worker . drain ( ) ;
609+ const projection = yield * orchestrator . getThreadProjection ( threadId ) ;
610+ const text = projection . turnItems . find (
611+ ( item ) => item . type === "assistant_message" && item . text === introduction ,
612+ ) ;
613+ const card = projection . turnItems . find ( ( item ) => item . type === "subagent" ) ;
614+ assert . ok ( text , "the root introduction is persisted" ) ;
615+ assert . ok ( card , "the Agent card is persisted" ) ;
616+ assert . isBelow ( text . ordinal , card . ordinal , "the introduction precedes its Agent card" ) ;
617+ } ) . pipe (
618+ Effect . provide (
619+ ProviderReplayHarness . layerWithRegistry (
620+ { name : "claude-transcript-order" } ,
621+ ProviderAdapterRegistry . layerSingle ( adapter ) ,
622+ { runEffectWorker : false } ,
623+ ) ,
624+ ) ,
625+ ) ;
626+ } ) . pipe (
627+ Effect . provide (
628+ Layer . mergeAll ( IdAllocator . layer , McpProviderSessions . layer , NodeServices . layer ) ,
629+ ) ,
630+ ) ,
631+ ) ,
632+ ) ;
633+
485634it . effect ( "persists a resumed subagent outcome when its buffered wake is dropped" , ( ) =>
486635 Effect . scoped (
487636 Effect . gen ( function * ( ) {
0 commit comments