@@ -50,6 +50,11 @@ import * as RunFinalizationService from "./RunFinalizationService.ts";
5050import * as TurnItemPositionStore from "./TurnItemPositionStore.ts" ;
5151
5252const driver = ProviderDriverKind . make ( "codex" ) ;
53+ const rootAppThread = ( id : ThreadId ) =>
54+ ( {
55+ id,
56+ lineage : { parentThreadId : null , relationshipToParent : null , rootThreadId : id } ,
57+ } ) as OrchestrationV2AppThread ;
5358
5459const layerTurnItemPositionsTest = Layer . mock ( TurnItemPositionStore . TurnItemPositionStoreV2 ) ( {
5560 allocate : ( ) => Effect . succeed ( 1 ) ,
@@ -775,7 +780,7 @@ it.effect("rechecks run ownership immediately before calling the provider", () =
775780
776781 yield * runExecution . startRootRun ( {
777782 commandId : CommandId . make ( "command:run-execution-start-guard" ) ,
778- appThread : { id : threadId } as OrchestrationV2AppThread ,
783+ appThread : rootAppThread ( threadId ) ,
779784 providerSessionId,
780785 session,
781786 run,
@@ -830,7 +835,7 @@ it.effect("passes the thread's MCP app context to the provider under a safe key"
830835
831836 yield * runExecution . startRootRun ( {
832837 commandId : CommandId . make ( "command:run-execution-app-context" ) ,
833- appThread : { id : threadId } as OrchestrationV2AppThread ,
838+ appThread : rootAppThread ( threadId ) ,
834839 providerSessionId : ProviderSessionId . make ( "session:run-execution-app-context" ) ,
835840 session,
836841 run : {
@@ -934,7 +939,7 @@ it.effect("fails the run when its ownership check cannot be read before calling
934939 const runExecution = yield * RunExecutionService . RunExecutionServiceV2 ;
935940 yield * runExecution . startRootRun ( {
936941 commandId : CommandId . make ( "command:run-execution-start-guard-read" ) ,
937- appThread : { id : threadId } as OrchestrationV2AppThread ,
942+ appThread : rootAppThread ( threadId ) ,
938943 providerSessionId : ProviderSessionId . make ( "session:run-execution-start-guard-read" ) ,
939944 session : {
940945 events : Stream . never ,
@@ -1029,7 +1034,7 @@ it.effect(
10291034 const attemptId = RunAttemptId . make ( `attempt:compact-routing:${ index } ` ) ;
10301035 yield * runExecution . startRootRun ( {
10311036 commandId : CommandId . make ( `command:compact-routing:${ index } ` ) ,
1032- appThread : { id : threadId } as OrchestrationV2AppThread ,
1037+ appThread : rootAppThread ( threadId ) ,
10331038 providerSessionId : ProviderSessionId . make ( `session:compact-routing:${ index } ` ) ,
10341039 session : {
10351040 events : Stream . never ,
@@ -1145,7 +1150,7 @@ it.effect("starts the provider when checkpoint baseline capture fails", () =>
11451150 const runExecution = yield * RunExecutionService . RunExecutionServiceV2 ;
11461151 yield * runExecution . startRootRun ( {
11471152 commandId : CommandId . make ( "command:run-execution-baseline-failure" ) ,
1148- appThread : { id : threadId } as OrchestrationV2AppThread ,
1153+ appThread : rootAppThread ( threadId ) ,
11491154 providerSessionId,
11501155 session : {
11511156 events : Stream . never ,
@@ -1283,7 +1288,7 @@ it.effect.each(["failure", "interruption", "stale-attempt", "start-guard"] as co
12831288 const runExecution = yield * RunExecutionService . RunExecutionServiceV2 ;
12841289 yield * runExecution . startRootRun ( {
12851290 commandId : CommandId . make ( "command:run-execution-settings-failure" ) ,
1286- appThread : { id : threadId } as OrchestrationV2AppThread ,
1291+ appThread : rootAppThread ( threadId ) ,
12871292 providerSessionId,
12881293 session : {
12891294 events : Stream . never ,
@@ -1399,6 +1404,7 @@ it.effect("ingests new member threads of an owned child after the root turn term
13991404 const subagentNodeId = NodeId . make ( "node:run-execution-late-child:subagent" ) ;
14001405 const childMessageIngested = yield * Deferred . make < void > ( ) ;
14011406 const order = yield * Ref . make < ReadonlyArray < string > > ( [ ] ) ;
1407+ const origins = yield * Ref . make < ReadonlyArray < readonly [ string , unknown ] > > ( [ ] ) ;
14021408 const layerTest = RunExecutionService . layer . pipe (
14031409 Layer . provide (
14041410 Layer . mergeAll (
@@ -1424,6 +1430,11 @@ it.effect("ingests new member threads of an owned child after the root turn term
14241430 Layer . mock ( ProviderEventIngestor . ProviderEventIngestorV2 ) ( {
14251431 ingestNormalized : ( input ) =>
14261432 Effect . gen ( function * ( ) {
1433+ const eventType = input . event . type ;
1434+ yield * Ref . update ( origins , ( current ) => [
1435+ ...current ,
1436+ [ eventType , input . analyticsContext ?. origin ] as const ,
1437+ ] ) ;
14271438 if (
14281439 input . event . type === "app_thread.created" &&
14291440 input . event . appThread . id === memberThreadId
@@ -1559,7 +1570,7 @@ it.effect("ingests new member threads of an owned child after the root turn term
15591570 const runExecution = yield * RunExecutionService . RunExecutionServiceV2 ;
15601571 yield * runExecution . startRootRun ( {
15611572 commandId : CommandId . make ( "command:run-execution-late-child" ) ,
1562- appThread : { id : threadId } as OrchestrationV2AppThread ,
1573+ appThread : rootAppThread ( threadId ) ,
15631574 providerSessionId,
15641575 session : {
15651576 events : Stream . fromIterable ( events ) ,
@@ -1612,6 +1623,19 @@ it.effect("ingests new member threads of an owned child after the root turn term
16121623 ) ;
16131624 assert . isTrue ( Option . isSome ( observed ) , "child message was not ingested after root terminal" ) ;
16141625 assert . deepEqual ( yield * Ref . get ( order ) , [ "root-finalized" , "member-thread" , "child-message" ] ) ;
1626+ // The run's message started the root; the parent agent started the child's turn.
1627+ const recorded = yield * Ref . get ( origins ) ;
1628+ assert . deepEqual ( recorded [ 0 ] , [
1629+ "app_thread.created" ,
1630+ { createdBy : "user" , creationSource : "web" , scheduled : false , subagent : false } ,
1631+ ] ) ;
1632+ assert . deepEqual (
1633+ recorded . find ( ( [ type ] ) => type === "provider_turn.updated" ) ,
1634+ [
1635+ "provider_turn.updated" ,
1636+ { createdBy : "agent" , creationSource : "provider" , scheduled : false , subagent : true } ,
1637+ ] ,
1638+ ) ;
16151639 } ) ,
16161640) ;
16171641
@@ -1704,7 +1728,7 @@ it.effect("ends a nested native subagent and its thread when the root run is int
17041728 const runExecution = yield * RunExecutionService . RunExecutionServiceV2 ;
17051729 yield * runExecution . startRootRun ( {
17061730 commandId : CommandId . make ( `command:${ key } ` ) ,
1707- appThread : { id : threadId } as OrchestrationV2AppThread ,
1731+ appThread : rootAppThread ( threadId ) ,
17081732 providerSessionId : ProviderSessionId . make ( `session:${ key } ` ) ,
17091733 session : {
17101734 events : Stream . fromIterable ( events ) ,
@@ -2031,7 +2055,7 @@ it.effect(
20312055 const runExecution = yield * RunExecutionService . RunExecutionServiceV2 ;
20322056 yield * runExecution . startRootRun ( {
20332057 commandId : CommandId . make ( "command:subagent-interrupt-cascade" ) ,
2034- appThread : { id : ids . threadId } as OrchestrationV2AppThread ,
2058+ appThread : rootAppThread ( ids . threadId ) ,
20352059 providerSessionId : ProviderSessionId . make ( "session:subagent-interrupt-cascade" ) ,
20362060 session : {
20372061 events : Stream . empty ,
@@ -2816,7 +2840,7 @@ function captureRootRunTermination(input: {
28162840 const runExecution = yield * RunExecutionService . RunExecutionServiceV2 ;
28172841 const start = runExecution . startRootRun ( {
28182842 commandId : CommandId . make ( `command:${ input . key } ` ) ,
2819- appThread : { id : ids . threadId } as OrchestrationV2AppThread ,
2843+ appThread : rootAppThread ( ids . threadId ) ,
28202844 providerSessionId : ProviderSessionId . make ( `session:${ input . key } ` ) ,
28212845 session : {
28222846 events : Stream . empty ,
@@ -3312,7 +3336,7 @@ function runBackgroundItemScenario(
33123336 const runExecution = yield * RunExecutionService . RunExecutionServiceV2 ;
33133337 yield * runExecution . startRootRun ( {
33143338 commandId : CommandId . make ( `command:${ key } ` ) ,
3315- appThread : { id : ids . threadId } as OrchestrationV2AppThread ,
3339+ appThread : rootAppThread ( ids . threadId ) ,
33163340 providerSessionId : ProviderSessionId . make ( `session:${ key } ` ) ,
33173341 session : {
33183342 events : Stream . empty ,
0 commit comments