Skip to content

Commit e98fac6

Browse files
PR Batch Testerclaude
andcommitted
fix(server): a subagent's next request of the same kind re-alerts
With several pending requests, answering one shows the next with no clear state between, and both read the same. The relay now tracks the child's request id and keys the parent's publish on the child request's time, so the next wait reaches the phone. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent b48f1c9 commit e98fac6

2 files changed

Lines changed: 41 additions & 10 deletions

File tree

‎apps/server/src/relay/AgentAwarenessRelay.test.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,21 @@ describe("AgentAwarenessRelay", () => {
368368
assert.equal(publications[3]?.state?.phase, "waiting_for_approval");
369369
assert.equal(publications[3]?.state?.updatedAt, "2026-09-04T12:05:00.000Z");
370370
assert.notEqual(publications[3]?.state?.updatedAt, publications[1]?.state?.updatedAt);
371+
372+
// With two pending requests, answering one shows the next with no clear between.
373+
yield* setShell(
374+
waitingChild({
375+
pendingRuntimeRequest: {
376+
id: RuntimeRequestId.make("child-request-3"),
377+
kind: "command",
378+
createdAt: DateTime.makeUnsafe("2026-09-04T12:06:00.000Z"),
379+
},
380+
}),
381+
);
382+
yield* relay.publishThread(CHILD_THREAD_ID);
383+
assert.equal(publications.length, 5);
384+
assert.equal(publications[4]?.state?.phase, "waiting_for_approval");
385+
assert.equal(publications[4]?.state?.updatedAt, "2026-09-04T12:06:00.000Z");
371386
}),
372387
);
373388

‎apps/server/src/relay/AgentAwarenessRelay.ts‎

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type {
44
OrchestrationV2ThreadShell,
55
OrchestrationV2TurnItem,
66
Project,
7+
RuntimeRequestId,
78
ThreadId,
89
} from "@t3tools/contracts";
910
import {
@@ -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

171181
function resolveAgentActivityPublishingStartupState(input: {
@@ -295,6 +305,7 @@ function describeThreadShellForAwareness(
295305
interface SubagentRequest {
296306
readonly topLevelThreadId: ThreadId;
297307
readonly request: PendingSubagentRequest;
308+
readonly requestId: RuntimeRequestId;
298309
}
299310

300311
function 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

Comments
 (0)