@@ -4,6 +4,7 @@ import type {
44 OrchestrationV2ThreadShell ,
55 OrchestrationV2TurnItem ,
66 Project ,
7+ RuntimeRequestId ,
78 ThreadId ,
89} from "@t3tools/contracts" ;
910import {
@@ -160,12 +161,21 @@ export const makeAgentAwarenessPublishWorker = Effect.fnUntraced(function* <R>(
160161 return { enqueue, drain : worker . drain } ;
161162} ) ;
162163
163- function agentAwarenessPublishIdentity ( state : RelayAgentActivityState | null ) : string {
164+ function agentAwarenessPublishIdentity (
165+ state : RelayAgentActivityState | null ,
166+ subagentRequest : SubagentRequestAwareness | null = null ,
167+ ) : string {
164168 if ( state === null ) {
165169 return "null" ;
166170 }
167171 const { updatedAt : _updatedAt , ...meaningfulState } = state ;
168- return JSON . stringify ( meaningfulState ) ;
172+ // A child's next request reads the same as its last one, but it is a new
173+ // wait and must reach the phone.
174+ return JSON . stringify (
175+ subagentRequest === null
176+ ? meaningfulState
177+ : [ meaningfulState , DateTime . formatIso ( subagentRequest . requestedAt ) ] ,
178+ ) ;
169179}
170180
171181function resolveAgentActivityPublishingStartupState ( input : {
@@ -295,6 +305,7 @@ function describeThreadShellForAwareness(
295305interface SubagentRequest {
296306 readonly topLevelThreadId : ThreadId ;
297307 readonly request : PendingSubagentRequest ;
308+ readonly requestId : RuntimeRequestId ;
298309}
299310
300311function resolveAgentAwarenessRelayPublishSnapshot ( input : {
@@ -553,14 +564,15 @@ export const make = Effect.gen(function* () {
553564 const project = Option . isSome ( thread )
554565 ? yield * projects . getById ( thread . value . projectId )
555566 : Option . none < Project > ( ) ;
567+ const pendingSubagentRequest = yield * currentSubagentRequestFor ( threadId ) ;
556568 const snapshot = resolveAgentAwarenessRelayPublishSnapshot ( {
557569 environmentId,
558570 threadId,
559571 thread,
560572 project,
561- pendingSubagentRequest : yield * currentSubagentRequestFor ( threadId ) ,
573+ pendingSubagentRequest,
562574 } ) ;
563- const publishIdentity = agentAwarenessPublishIdentity ( snapshot . state ) ;
575+ const publishIdentity = agentAwarenessPublishIdentity ( snapshot . state , pendingSubagentRequest ) ;
564576 const publishedStateByThread = yield * Ref . get ( publishedStateByThreadRef ) ;
565577 if (
566578 ( snapshot . state ?. phase === "completed" || snapshot . state ?. phase === "failed" ) &&
@@ -674,8 +686,9 @@ export const make = Effect.gen(function* () {
674686 ? null
675687 : subagentPendingRequest ( threadShell ) ;
676688 const previous = subagentRequests . get ( threadId ) ;
677- if ( previous ?. request === ( request ?? undefined ) ) return ;
678- if ( request === null || threadShell === null ) {
689+ const requestId = request === null ? undefined : threadShell ?. pendingRuntimeRequest ?. id ;
690+ if ( previous ?. request === ( request ?? undefined ) && previous ?. requestId === requestId ) return ;
691+ if ( request === null || threadShell === null || requestId === undefined ) {
679692 if ( previous === undefined ) return ;
680693 subagentRequests . delete ( threadId ) ;
681694 yield * enqueueTopLevelPublish ( previous . topLevelThreadId ) ;
@@ -684,7 +697,7 @@ export const make = Effect.gen(function* () {
684697 const topLevelThreadId =
685698 previous ?. topLevelThreadId ?? ( yield * findTopLevelThreadId ( threadShell ) ) ;
686699 if ( topLevelThreadId === null ) return ;
687- subagentRequests . set ( threadId , { topLevelThreadId, request } ) ;
700+ subagentRequests . set ( threadId , { topLevelThreadId, request, requestId } ) ;
688701 yield * enqueueTopLevelPublish ( topLevelThreadId ) ;
689702 } ) ;
690703
@@ -704,12 +717,15 @@ export const make = Effect.gen(function* () {
704717 const child = yield * threads . getThreadShell ( childId ) ;
705718 const request =
706719 child === null || child . archivedAt !== null ? null : subagentPendingRequest ( child ) ;
707- const requestedAt = child ?. pendingRuntimeRequest ?. createdAt ;
708- if ( request === null || requestedAt === undefined ) {
720+ const pendingRequest = child ?. pendingRuntimeRequest ;
721+ if ( request === null || pendingRequest == null ) {
709722 subagentRequests . delete ( childId ) ;
710723 continue ;
711724 }
712- if ( request !== entry . request ) subagentRequests . set ( childId , { ...entry , request } ) ;
725+ const requestedAt = pendingRequest . createdAt ;
726+ if ( request !== entry . request || pendingRequest . id !== entry . requestId ) {
727+ subagentRequests . set ( childId , { ...entry , request, requestId : pendingRequest . id } ) ;
728+ }
713729 if ( request === "approval" ) return { kind : request , requestedAt } ;
714730 pending = { kind : request , requestedAt } ;
715731 }
0 commit comments