The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 22, 02:00 UTC
fix(server): phones get the approval push when a subagent waits (#24079)
Co-authored-by: PR Batch Tester <agent@local.test> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: GPT-6.1 Sol <noreply@openai.com> Co-authored-by: maria-rcks <maria@kuuro.net>
shivam committed
Nov 9, 2026 at 12:52 UTC
60514be91175f648634a7b8dd00fe45a3483feee
4 files changed
+321
-16
apps/server/src/orchestration-v2/ProjectionStore.ts
+2
@@ -1012,6 +1012,7 @@ export type AgentAwarenessThread = ProjectThreadAwarenessV2Input["thread"] &
1012
Pick<
1013
OrchestrationV2ThreadShell,
1014
| "projectId"
1015
+ | "creationSource"
1016
| "latestRunCompletedAt"
1017
| "archivedAt"
1018
| "activeRunId"
@@ -7173,6 +7174,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
7174
Effect.map((state): AgentAwarenessThread => ({
7175
id: state.thread.id,
7176
projectId: state.thread.projectId,
7177
+ creationSource: state.thread.creationSource,
7178
archivedAt: state.thread.archivedAt,
7179
activeRunId: state.activeRunId,
7180
latestRunId: state.latestRunId,
apps/server/src/relay/AgentAwarenessRelay.test.ts
+132
@@ -11,6 +11,7 @@ import {
11
ProjectId,
12
ProviderInstanceId,
13
RunId,
14
+ RuntimeRequestId,
15
ThreadId,
16
TurnItemId,
17
} from "@t3tools/contracts";
@@ -93,6 +94,31 @@ function shell(overrides: Partial<OrchestrationV2ThreadShell> = {}): Orchestrati
94
};
95
}
96
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
+
122
const PublishPayload = Schema.Struct({ state: Schema.NullOr(RelayAgentActivityState) });
123
const decodePublishPayload = Schema.decodeUnknownSync(Schema.fromJsonString(PublishPayload));
124
const unused = () => Effect.die("Unexpected test dependency call");
@@ -305,6 +331,112 @@ const makeTestRelay = Effect.fnUntraced(function* (
331
});
332
333
describe("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
+
440
it.effect("coalesces queued updates and reruns a thread dirtied during publishing", () =>
441
Effect.gen(function* () {
442
const started = yield* Deferred.make<void>();
apps/server/src/relay/AgentAwarenessRelay.ts
+145
-14
@@ -5,6 +5,7 @@ import {
5
type OrchestrationV2DomainEvent,
6
type OrchestrationV2TurnItem,
7
type Project,
8
+ type RuntimeRequestId,
9
type ThreadId,
10
} from "@t3tools/contracts";
11
import {
@@ -12,7 +13,11 @@ import {
13
type RelayAgentActivityPublishProofPayload,
14
type RelayAgentActivityState,
15
} from "@t3tools/contracts/relay";
15
-import { projectThreadAwarenessV2 } from "@t3tools/shared/agentAwareness";
16
+import {
17
+ type SubagentRequestAwareness,
18
+ projectThreadAwarenessV2,
19
+ subagentAwaitsApproval,
20
+} from "@t3tools/shared/agentAwareness";
21
import { turnItemUpdateCanEndBackgroundWork } from "@t3tools/shared/orchestrationV2PendingBackgroundWork";
22
import { makeDrainableWorker } from "@t3tools/shared/DrainableWorker";
23
import { withRelayClientTracing } from "@t3tools/shared/relayTracing";
@@ -153,12 +158,19 @@ export const makeAgentAwarenessPublishWorker = Effect.fnUntraced(function* <R>(
158
return { enqueue, drain: worker.drain };
159
});
160
156
-function agentAwarenessPublishIdentity(state: RelayAgentActivityState | null): string {
161
+function agentAwarenessPublishIdentity(
162
+ state: RelayAgentActivityState | null,
163
+ subagentRequest: SubagentRequestAwareness | null = null,
164
+): string {
165
if (state === null) {
166
return "null";
167
}
168
const { updatedAt: _updatedAt, ...meaningfulState } = state;
161
- return JSON.stringify(meaningfulState);
169
+ // A child's next request reads the same as its last one, but it is a new
170
+ // wait and must reach the phone.
171
+ return JSON.stringify(
172
+ subagentRequest === null ? meaningfulState : [meaningfulState, subagentRequest.requestId],
173
+ );
174
}
175
176
const CLEARED_PUBLISH_IDENTITY = agentAwarenessPublishIdentity(null);
@@ -285,19 +297,38 @@ function describeThreadShellForAwareness(
297
};
298
}
299
300
+interface SubagentRequest {
301
+ readonly topLevelThreadId: ThreadId;
302
+ readonly requestId: RuntimeRequestId;
303
+}
304
+
305
+interface SubagentAlert {
306
+ readonly requestId: RuntimeRequestId;
307
+ readonly updatedAt: string;
308
+}
309
+
310
+const waitingApprovalId = (thread: AgentAwarenessThread | null) =>
311
+ thread !== null && thread.archivedAt === null && subagentAwaitsApproval(thread)
312
+ ? thread.pendingRuntimeRequest?.id
313
+ : undefined;
314
+
315
function resolveAgentAwarenessRelayPublishSnapshot(input: {
316
readonly environmentId: EnvironmentId;
317
readonly thread: Option.Option<AgentAwarenessThread>;
318
readonly project: Option.Option<Project>;
319
+ readonly pendingSubagentRequest: SubagentRequestAwareness | null;
320
}): {
321
readonly projectId: string | null;
322
readonly state: RelayAgentActivityState | null;
323
+ /** The child request behind the state's wait, when the parent isn't waiting itself. */
324
+ readonly subagentWait: SubagentRequestAwareness | null;
325
readonly reason: "snapshot" | "thread-not-found" | "project-not-found";
326
} {
327
if (Option.isNone(input.thread)) {
328
return {
329
projectId: null,
330
state: null,
331
+ subagentWait: null,
332
reason: "thread-not-found",
333
};
334
}
@@ -305,18 +336,26 @@ function resolveAgentAwarenessRelayPublishSnapshot(input: {
336
return {
337
projectId: input.thread.value.projectId,
338
state: null,
339
+ subagentWait: null,
340
reason: "project-not-found",
341
};
342
}
343
+ const awarenessInput = {
344
+ environmentId: input.environmentId,
345
+ project: input.project.value,
346
+ thread: input.thread.value,
347
+ };
348
+ const state = projectThreadAwarenessV2({
349
+ ...awarenessInput,
350
+ pendingSubagentRequest: input.pendingSubagentRequest,
351
+ });
352
+ const childDrivesWait =
353
+ input.pendingSubagentRequest !== null &&
354
+ state?.phase !== projectThreadAwarenessV2(awarenessInput)?.phase;
355
return {
356
projectId: input.thread.value.projectId,
313
- state: sanitizeRelayAgentActivityState(
314
- projectThreadAwarenessV2({
315
- environmentId: input.environmentId,
316
- project: input.project.value,
317
- thread: input.thread.value,
318
- }),
319
- ),
357
+ state: sanitizeRelayAgentActivityState(state),
358
+ subagentWait: childDrivesWait ? input.pendingSubagentRequest : null,
359
reason: "snapshot",
360
};
361
}
@@ -337,6 +376,8 @@ export function resolveAgentAwarenessRelayActiveThreadIds(input: {
376
const projectById = new Map(input.projects.map((project) => [project.id, project]));
377
return input.threads
378
.filter((thread) => {
379
+ // Publishing a waiting subagent republishes its top-level thread.
380
+ if (subagentAwaitsApproval(thread)) return true;
381
const project = projectById.get(thread.projectId);
382
if (!project) {
383
return false;
@@ -368,6 +409,10 @@ export const make = Effect.gen(function* () {
409
const startedAt = (yield* DateTime.now).epochMilliseconds;
410
const catchUpRequests = yield* Queue.dropping<void>(1);
411
const publishedStateByThreadRef = yield* Ref.make(new Map<ThreadId, string>());
412
+ // Android keys alerts by phase and time, so a child's unchanged request keeps
413
+ // its first alert time through parent renames, relinks and the parent's own requests.
414
+ const childAlertByThread = new Map<ThreadId, SubagentAlert>();
415
+ const subagentRequests = new Map<ThreadId, SubagentRequest>();
416
417
const readSecretString = (name: string) =>
418
secrets
@@ -423,6 +468,7 @@ export const make = Effect.gen(function* () {
468
Effect.forEach([...publishRetries.keys()], cancelPublishRetry, { discard: true }),
469
);
470
let schedulePublishRetry: (threadId: ThreadId) => Effect.Effect<void> = () => Effect.void;
471
+ let enqueueTopLevelPublish: (threadId: ThreadId) => Effect.Effect<void> = () => Effect.void;
472
const resetPublishedConnection = Effect.gen(function* () {
473
publishedRelayConfig = undefined;
474
cachedRelayClient = undefined;
@@ -508,7 +554,8 @@ export const make = Effect.gen(function* () {
554
});
555
556
// Read only this thread; full shell reads would make each activity event scale with all threads.
511
- const threadShell = (yield* threads.getAgentAwarenessSnapshot(threadId)).threads[0] ?? null;
557
+ const threadShell = yield* readAwarenessThread(threadId);
558
+ yield* trackSubagentRequest(threadId, threadShell);
559
if (
560
threadShell !== null &&
561
!isListedThreadLineage(threadShell.lineage) &&
@@ -528,8 +575,9 @@ export const make = Effect.gen(function* () {
575
environmentId,
576
thread,
577
project,
578
+ pendingSubagentRequest: yield* currentSubagentRequestFor(threadId),
579
});
532
- const publishIdentity = agentAwarenessPublishIdentity(snapshot.state);
580
+ const publishIdentity = agentAwarenessPublishIdentity(snapshot.state, snapshot.subagentWait);
581
const publishedStateByThread = yield* Ref.get(publishedStateByThreadRef);
582
if (
583
(snapshot.state?.phase === "completed" || snapshot.state?.phase === "failed") &&
@@ -594,6 +642,7 @@ export const make = Effect.gen(function* () {
642
environmentId,
643
thread: Option.some({ ...thread.value, settledOverride: null }),
644
project,
645
+ pendingSubagentRequest: null,
646
})
647
: null;
648
if (unsettled?.state?.phase === "completed") {
@@ -629,11 +678,22 @@ export const make = Effect.gen(function* () {
678
});
679
}
680
681
+ const childRequestId = snapshot.subagentWait?.requestId;
682
+ const childAlert = childAlertByThread.get(threadId);
683
+ const state =
684
+ snapshot.state !== null &&
685
+ childRequestId !== undefined &&
686
+ childAlert?.requestId === childRequestId
687
+ ? { ...snapshot.state, updatedAt: childAlert.updatedAt }
688
+ : snapshot.state;
689
yield* publishState({
690
projectId: snapshot.projectId,
634
- state: snapshot.state,
691
+ state,
692
reason: snapshot.reason,
693
}).pipe(withRelayClientTracing);
694
+ if (state !== null && childRequestId !== undefined) {
695
+ childAlertByThread.set(threadId, { requestId: childRequestId, updatedAt: state.updatedAt });
696
+ }
697
publishConfirmDeadlines.delete(threadId);
698
heldCompletions.delete(threadId);
699
yield* Ref.update(publishedStateByThreadRef, (publishedStates) =>
@@ -641,6 +701,69 @@ export const make = Effect.gen(function* () {
701
);
702
});
703
704
+ const readAwarenessThread = (threadId: ThreadId) =>
705
+ threads
706
+ .getAgentAwarenessSnapshot(threadId)
707
+ .pipe(Effect.map((snapshot) => snapshot.threads[0] ?? null));
708
+
709
+ // The subagent helpers below are untraced: they run per event inside
710
+ // publishThreadUnsafe, whose span already covers them.
711
+ const findTopLevelThreadId = Effect.fnUntraced(function* (child: AgentAwarenessThread) {
712
+ const seen = new Set<ThreadId>([child.id]);
713
+ let current: AgentAwarenessThread | null = child;
714
+ while (
715
+ current?.lineage.relationshipToParent === "subagent" ||
716
+ current?.lineage.sideChat === true
717
+ ) {
718
+ const parentId: ThreadId | null = current.lineage.parentThreadId;
719
+ if (parentId === null || seen.has(parentId)) return null;
720
+ seen.add(parentId);
721
+ current = yield* readAwarenessThread(parentId);
722
+ }
723
+ return current?.id ?? null;
724
+ });
725
+
726
+ const trackSubagentRequest = Effect.fnUntraced(function* (
727
+ threadId: ThreadId,
728
+ threadShell: AgentAwarenessThread | null,
729
+ ) {
730
+ const requestId = waitingApprovalId(threadShell);
731
+ const previous = subagentRequests.get(threadId);
732
+ // An unchanged wait still republishes its parent: a relink catch-up reaches it only here.
733
+ if (threadShell === null || requestId === undefined) {
734
+ if (previous === undefined) return;
735
+ subagentRequests.delete(threadId);
736
+ yield* enqueueTopLevelPublish(previous.topLevelThreadId);
737
+ return;
738
+ }
739
+ const topLevelThreadId =
740
+ previous?.topLevelThreadId ?? (yield* findTopLevelThreadId(threadShell));
741
+ if (topLevelThreadId === null) return;
742
+ subagentRequests.set(threadId, { topLevelThreadId, requestId });
743
+ yield* enqueueTopLevelPublish(topLevelThreadId);
744
+ });
745
+
746
+ // The map can miss a child's update while publishing is off or the relay is
747
+ // unlinked, so each waiting child is re-read before its parent is published.
748
+ const currentSubagentRequestFor = Effect.fnUntraced(function* (topLevelThreadId: ThreadId) {
749
+ let pending: SubagentRequestAwareness | null = null;
750
+ for (const [childId, entry] of subagentRequests) {
751
+ if (entry.topLevelThreadId !== topLevelThreadId) continue;
752
+ const child = yield* readAwarenessThread(childId);
753
+ const requestId = waitingApprovalId(child);
754
+ if (child === null || requestId === undefined) {
755
+ subagentRequests.delete(childId);
756
+ continue;
757
+ }
758
+ if (requestId !== entry.requestId) subagentRequests.set(childId, { ...entry, requestId });
759
+ // The newest wait is the one to announce.
760
+ if (pending === null || DateTime.isGreaterThan(child.updatedAt, pending.updatedAt)) {
761
+ pending = { requestId, updatedAt: child.updatedAt };
762
+ }
763
+ }
764
+ return pending;
765
+ });
766
+
767
const processThreadPublish = Effect.fnUntraced(function* (threadId: ThreadId) {
768
const retry = publishRetries.get(threadId);
769
if (retry?.timer !== undefined) {
@@ -664,6 +787,7 @@ export const make = Effect.gen(function* () {
787
const worker = yield* makeAgentAwarenessPublishWorker(processThreadPublish);
788
const enqueueThreadPublish = (threadId: ThreadId) =>
789
cancelPublishRetry(threadId).pipe(Effect.andThen(worker.enqueue(threadId)));
790
+ enqueueTopLevelPublish = enqueueThreadPublish;
791
const publishThread: AgentAwarenessRelay["Service"]["publishThread"] = (threadId) =>
792
enqueueThreadPublish(threadId).pipe(Effect.andThen(worker.drain));
793
@@ -720,11 +844,18 @@ export const make = Effect.gen(function* () {
844
const environmentId = yield* serverEnvironment.getEnvironmentId;
845
const projectSnapshot = yield* projects.snapshot;
846
const activitySnapshot = yield* threads.getAgentAwarenessSnapshot();
847
+ // Live children aren't listed, but a waiting one makes its top-level thread wait.
848
+ const liveChildren = yield* Effect.forEach(
849
+ (yield* threads.listActiveShellIndex()).filter(
850
+ (thread) => thread.lineage.relationshipToParent === "subagent" && thread.mayBeLive,
851
+ ),
852
+ (thread) => readAwarenessThread(thread.id),
853
+ );
854
const activeThreadIds = resolveAgentAwarenessRelayActiveThreadIds({
855
environmentId,
856
startedAt,
857
projects: projectSnapshot.projects,
727
- threads: activitySnapshot.threads,
858
+ threads: [...activitySnapshot.threads, ...liveChildren.filter((thread) => thread !== null)],
859
});
860
if (activeThreadIds.length === 0) {
861
yield* Effect.logDebug("agent activity snapshot has no publishable threads");
packages/shared/src/agentAwareness.ts
+42
-2
@@ -1,8 +1,10 @@
1
import {
2
isListedThreadLineage,
3
type EnvironmentId,
4
+ isProviderNativeSubagentThread,
5
type OrchestrationV2ThreadShell,
6
type Project,
7
+ type RuntimeRequestId,
8
type ThreadId,
9
} from "@t3tools/contracts";
10
import * as DateTime from "effect/DateTime";
@@ -18,6 +20,31 @@ export type AgentAwarenessPhase =
20
| "failed"
21
| "stale";
22
23
+export interface SubagentRequestAwareness {
24
+ readonly requestId: RuntimeRequestId;
25
+ /** The child's own update time, which moves whenever its request changes. */
26
+ readonly updatedAt: DateTime.Utc;
27
+}
28
+
29
+/** Provider-native subagents ask on their parent; the coordinator answers child questions. */
30
+export function subagentAwaitsApproval(
31
+ thread: Pick<
32
+ OrchestrationV2ThreadShell,
33
+ "activityRunStatus" | "creationSource" | "lineage" | "pendingRuntimeRequest"
34
+ >,
35
+): boolean {
36
+ const kind = thread.pendingRuntimeRequest?.kind;
37
+ return (
38
+ kind !== undefined &&
39
+ kind !== "auth_refresh" &&
40
+ kind !== "user_input" &&
41
+ thread.lineage.relationshipToParent === "subagent" &&
42
+ !isProviderNativeSubagentThread(thread) &&
43
+ // The activity run stays set when a follow-up is queued behind a blocked run.
44
+ thread.activityRunStatus != null
45
+ );
46
+}
47
+
48
export interface AgentAwarenessState {
49
readonly environmentId: EnvironmentId;
50
readonly threadId: ThreadId;
@@ -55,6 +82,8 @@ export interface ProjectThreadAwarenessV2Input {
82
| "title"
83
| "updatedAt"
84
>;
85
+ /** The approval a live subagent below this thread is waiting on. */
86
+ readonly pendingSubagentRequest?: SubagentRequestAwareness | null;
87
}
88
89
export function projectThreadAwarenessV2(
@@ -62,7 +91,8 @@ export function projectThreadAwarenessV2(
91
): AgentAwarenessState | null {
92
const { environmentId, project, thread } = input;
93
if (!isListedThreadLineage(thread.lineage)) return null;
65
- const phase = resolveThreadAwarenessPhaseV2(thread);
94
+ const subagentRequest = input.pendingSubagentRequest ?? null;
95
+ const phase = resolveThreadAwarenessPhaseV2(thread, subagentRequest !== null);
96
if (phase === null) {
97
return null;
98
}
@@ -90,13 +120,20 @@ export function projectThreadAwarenessV2(
120
headline: headlineForPhase(phase),
121
...(detail === undefined ? {} : { detail }),
122
modelTitle: thread.modelSelection.model,
93
- updatedAt: DateTime.formatIso(thread.updatedAt),
123
+ // A child's request leaves the parent's own timestamp alone. Alerts are
124
+ // keyed by phase and time, so a second wait needs the child's time.
125
+ updatedAt: DateTime.formatIso(
126
+ subagentRequest === null
127
+ ? thread.updatedAt
128
+ : DateTime.max(thread.updatedAt, subagentRequest.updatedAt),
129
+ ),
130
deepLink: buildAgentAwarenessDeepLink({ environmentId, threadId: thread.id }),
131
};
132
}
133
134
function resolveThreadAwarenessPhaseV2(
135
thread: ProjectThreadAwarenessV2Input["thread"],
136
+ childAwaitsApproval: boolean,
137
): AgentAwarenessPhase | null {
138
if (thread.pendingRuntimeRequest?.kind === "user_input") return "waiting_for_input";
139
if (
@@ -105,6 +142,9 @@ function resolveThreadAwarenessPhaseV2(
142
) {
143
return "waiting_for_approval";
144
}
145
+ // The parent can finish its own turn while a delegated child keeps working,
146
+ // so a child's request outranks the parent's run status.
147
+ if (childAwaitsApproval) return "waiting_for_approval";
148
switch (thread.activityRunStatus ?? thread.status) {
149
case "preparing":
150
case "starting":