@@ -11,6 +11,7 @@ import {
1111 ProjectId ,
1212 ProviderInstanceId ,
1313 RunId ,
14+ RuntimeRequestId ,
1415 ThreadId ,
1516 TurnItemId ,
1617} from "@t3tools/contracts" ;
@@ -93,6 +94,31 @@ function shell(overrides: Partial<OrchestrationV2ThreadShell> = {}): Orchestrati
9394 } ;
9495}
9596
97+ const CHILD_THREAD_ID = ThreadId . make ( "relay-child-thread" ) ;
98+
99+ function waitingChild (
100+ overrides : Partial < OrchestrationV2ThreadShell > = { } ,
101+ ) : OrchestrationV2ThreadShell {
102+ return shell ( {
103+ id : CHILD_THREAD_ID ,
104+ title : "Child" ,
105+ lineage : {
106+ rootThreadId : THREAD_ID ,
107+ parentThreadId : THREAD_ID ,
108+ relationshipToParent : "subagent" ,
109+ } ,
110+ createdBy : "agent" ,
111+ creationSource : "mcp" ,
112+ activityRunStatus : "running" ,
113+ pendingRuntimeRequest : {
114+ id : RuntimeRequestId . make ( "child-request" ) ,
115+ kind : "command" ,
116+ createdAt : DateTime . makeUnsafe ( NOW ) ,
117+ } ,
118+ ...overrides ,
119+ } ) ;
120+ }
121+
96122const PublishPayload = Schema . Struct ( { state : Schema . NullOr ( RelayAgentActivityState ) } ) ;
97123const decodePublishPayload = Schema . decodeUnknownSync ( Schema . fromJsonString ( PublishPayload ) ) ;
98124const unused = ( ) => Effect . die ( "Unexpected test dependency call" ) ;
@@ -305,6 +331,112 @@ const makeTestRelay = Effect.fnUntraced(function* (
305331} ) ;
306332
307333describe ( "AgentAwarenessRelay" , ( ) => {
334+ it . effect ( "publishes the parent as needing approval while its subagent waits" , ( ) =>
335+ Effect . gen ( function * ( ) {
336+ const shells = yield * Ref . make (
337+ new Map < ThreadId , OrchestrationV2ThreadShell > ( [
338+ [ THREAD_ID , shell ( { status : "running" } ) ] ,
339+ [ CHILD_THREAD_ID , waitingChild ( { pendingRuntimeRequest : null } ) ] ,
340+ ] ) ,
341+ ) ;
342+ const setShell = ( next : OrchestrationV2ThreadShell ) =>
343+ Ref . update ( shells , ( current ) => new Map ( current ) . set ( next . id , next ) ) ;
344+ const { relay, publications } = yield * makeTestRelay ( {
345+ readShell : ( threadId ) =>
346+ Ref . get ( shells ) . pipe ( Effect . map ( ( current ) => current . get ( threadId ) ?? null ) ) ,
347+ } ) ;
348+ yield * relay . publishThread ( THREAD_ID ) ;
349+ yield * setShell ( shell ( { status : "completed" } ) ) ;
350+ yield * setShell ( waitingChild ( ) ) ;
351+ yield * relay . publishThread ( CHILD_THREAD_ID ) ;
352+ assert . equal ( publications . length , 2 ) ;
353+ assert . isTrue ( publications [ 1 ] ?. url . includes ( `/threads/${ THREAD_ID } /` ) ?? false ) ;
354+ assert . equal ( publications [ 1 ] ?. state ?. phase , "waiting_for_approval" ) ;
355+
356+ // A rename keeps the same request's alert time, so the phone doesn't re-alert.
357+ yield * setShell (
358+ shell ( {
359+ status : "completed" ,
360+ title : "Renamed thread" ,
361+ updatedAt : DateTime . makeUnsafe ( "2026-09-04T12:01:00.000Z" ) ,
362+ } ) ,
363+ ) ;
364+ yield * relay . publishThread ( THREAD_ID ) ;
365+ assert . equal ( publications . length , 3 ) ;
366+ assert . equal ( publications [ 2 ] ?. state ?. threadTitle , "Renamed thread" ) ;
367+ assert . equal ( publications [ 2 ] ?. state ?. updatedAt , publications [ 1 ] ?. state ?. updatedAt ) ;
368+
369+ yield * setShell ( waitingChild ( { pendingRuntimeRequest : null } ) ) ;
370+ yield * relay . publishThread ( CHILD_THREAD_ID ) ;
371+ yield * TestClock . adjust ( "5 seconds" ) ;
372+ yield * relay . drain ;
373+ assert . equal ( publications . length , 4 ) ;
374+ assert . equal ( publications [ 3 ] ?. state ?. phase , "completed" ) ;
375+ } ) ,
376+ ) ;
377+
378+ it . effect ( "republishes a completed parent for its waiting child after a relink" , ( ) =>
379+ Effect . gen ( function * ( ) {
380+ const parent = shell ( { status : "completed" } ) ;
381+ const child = waitingChild ( ) ;
382+ const { relay, secrets, publications } = yield * makeTestRelay ( {
383+ catchUpThreads : [ parent , child ] ,
384+ readShell : ( id ) => Effect . succeed ( id === child . id ? child : parent ) ,
385+ } ) ;
386+ yield * relay . start ( ) ;
387+ yield * TestClock . adjust ( "1 second" ) ;
388+ yield * relay . drain ;
389+ assert . equal ( publications . length , 1 ) ;
390+ assert . equal ( publications [ 0 ] ?. state ?. phase , "waiting_for_approval" ) ;
391+
392+ yield * secrets . set (
393+ RELAY_ENVIRONMENT_CREDENTIAL_SECRET ,
394+ new TextEncoder ( ) . encode ( "credential-2" ) ,
395+ ) ;
396+ yield * relay . requestCatchUp ( ) ;
397+ yield * TestClock . adjust ( "1 second" ) ;
398+ yield * relay . drain ;
399+ assert . equal ( publications . length , 2 ) ;
400+ assert . equal ( publications [ 1 ] ?. state ?. phase , "waiting_for_approval" ) ;
401+ } ) . pipe ( Effect . scoped ) ,
402+ ) ;
403+
404+ it . effect ( "keeps a child's alert time when the parent's own request comes and goes" , ( ) =>
405+ Effect . gen ( function * ( ) {
406+ const shells = yield * Ref . make (
407+ new Map < ThreadId , OrchestrationV2ThreadShell > ( [
408+ [ THREAD_ID , shell ( ) ] ,
409+ [ CHILD_THREAD_ID , waitingChild ( ) ] ,
410+ ] ) ,
411+ ) ;
412+ const setParent = ( next : OrchestrationV2ThreadShell ) =>
413+ Ref . update ( shells , ( current ) => new Map ( current ) . set ( THREAD_ID , next ) ) ;
414+ const { relay, publications } = yield * makeTestRelay ( {
415+ readShell : ( threadId ) =>
416+ Ref . get ( shells ) . pipe ( Effect . map ( ( current ) => current . get ( threadId ) ?? null ) ) ,
417+ } ) ;
418+ yield * relay . publishThread ( CHILD_THREAD_ID ) ;
419+ yield * setParent (
420+ shell ( {
421+ updatedAt : DateTime . makeUnsafe ( "2026-09-04T12:01:00.000Z" ) ,
422+ pendingRuntimeRequest : {
423+ id : RuntimeRequestId . make ( "parent-request" ) ,
424+ kind : "command" ,
425+ createdAt : DateTime . makeUnsafe ( "2026-09-04T12:01:00.000Z" ) ,
426+ } ,
427+ } ) ,
428+ ) ;
429+ yield * relay . publishThread ( THREAD_ID ) ;
430+ yield * setParent ( shell ( { updatedAt : DateTime . makeUnsafe ( "2026-09-04T12:02:00.000Z" ) } ) ) ;
431+ yield * relay . publishThread ( THREAD_ID ) ;
432+ assert . deepStrictEqual (
433+ publications . map ( ( { state } ) => state ?. phase ) ,
434+ [ "waiting_for_approval" , "waiting_for_approval" , "waiting_for_approval" ] ,
435+ ) ;
436+ assert . equal ( publications [ 2 ] ?. state ?. updatedAt , publications [ 0 ] ?. state ?. updatedAt ) ;
437+ } ) ,
438+ ) ;
439+
308440 it . effect ( "coalesces queued updates and reruns a thread dirtied during publishing" , ( ) =>
309441 Effect . gen ( function * ( ) {
310442 const started = yield * Deferred . make < void > ( ) ;
0 commit comments