Skip to content

Commit e17084b

Browse files
PR Batch Testerclaude
andcommitted
fix(server): re-read waiting subagents before publishing their parent
The cached child request can miss an update while publishing is off or the relay is unlinked, so the parent re-reads each waiting child first. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 69a7f1c commit e17084b

2 files changed

Lines changed: 48 additions & 14 deletions

File tree

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,33 @@ describe("AgentAwarenessRelay", () => {
355355
}),
356356
);
357357

358+
it.effect("drops a subagent request that cleared while publishing was off", () =>
359+
Effect.gen(function* () {
360+
const shells = yield* Ref.make(
361+
new Map<ThreadId, OrchestrationV2ThreadShell>([
362+
[THREAD_ID, shell()],
363+
[CHILD_THREAD_ID, waitingChild()],
364+
]),
365+
);
366+
const { relay, secrets, publications } = yield* makeTestRelay({
367+
readShell: (threadId) =>
368+
Ref.get(shells).pipe(Effect.map((current) => current.get(threadId) ?? null)),
369+
});
370+
yield* relay.publishThread(CHILD_THREAD_ID);
371+
assert.equal(publications[0]?.state?.phase, "waiting_for_approval");
372+
373+
yield* secrets.set(PUBLISH_AGENT_ACTIVITY_SECRET, new TextEncoder().encode("false"));
374+
yield* Ref.update(shells, (current) =>
375+
new Map(current).set(CHILD_THREAD_ID, waitingChild({ pendingRuntimeRequest: null })),
376+
);
377+
yield* relay.publishThread(CHILD_THREAD_ID);
378+
yield* secrets.set(PUBLISH_AGENT_ACTIVITY_SECRET, new TextEncoder().encode("true"));
379+
yield* relay.publishThread(THREAD_ID);
380+
assert.equal(publications.length, 2);
381+
assert.equal(publications[1]?.state?.phase, "running");
382+
}),
383+
);
384+
358385
it.effect("coalesces queued updates and reruns a thread dirtied during publishing", () =>
359386
Effect.gen(function* () {
360387
const started = yield* Deferred.make<void>();

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

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -296,19 +296,6 @@ interface SubagentRequest {
296296
readonly request: PendingSubagentRequest;
297297
}
298298

299-
function pendingSubagentRequestFor(
300-
requests: ReadonlyMap<ThreadId, SubagentRequest>,
301-
topLevelThreadId: ThreadId,
302-
): PendingSubagentRequest | null {
303-
let pending: PendingSubagentRequest | null = null;
304-
for (const entry of requests.values()) {
305-
if (entry.topLevelThreadId !== topLevelThreadId) continue;
306-
if (entry.request === "approval") return "approval";
307-
pending = entry.request;
308-
}
309-
return pending;
310-
}
311-
312299
function resolveAgentAwarenessRelayPublishSnapshot(input: {
313300
readonly environmentId: EnvironmentId;
314301
readonly threadId: ThreadId;
@@ -565,7 +552,7 @@ export const make = Effect.gen(function* () {
565552
threadId,
566553
thread,
567554
project,
568-
pendingSubagentRequest: pendingSubagentRequestFor(subagentRequests, threadId),
555+
pendingSubagentRequest: yield* currentSubagentRequestFor(threadId),
569556
});
570557
const publishIdentity = agentAwarenessPublishIdentity(snapshot.state);
571558
const publishedStateByThread = yield* Ref.get(publishedStateByThreadRef);
@@ -694,6 +681,26 @@ export const make = Effect.gen(function* () {
694681
yield* enqueueWorkerPublish(topLevelThreadId);
695682
});
696683

684+
// The map can miss a child's update while publishing is off or the relay is
685+
// unlinked, so each waiting child is re-read before its parent is published.
686+
const currentSubagentRequestFor = Effect.fnUntraced(function* (topLevelThreadId: ThreadId) {
687+
let pending: PendingSubagentRequest | null = null;
688+
for (const [childId, entry] of [...subagentRequests]) {
689+
if (entry.topLevelThreadId !== topLevelThreadId) continue;
690+
const child = yield* threads.getThreadShell(childId);
691+
const request =
692+
child === null || child.archivedAt !== null ? null : subagentPendingRequest(child);
693+
if (request === null) {
694+
subagentRequests.delete(childId);
695+
continue;
696+
}
697+
if (request !== entry.request) subagentRequests.set(childId, { ...entry, request });
698+
if (request === "approval") return request;
699+
pending = request;
700+
}
701+
return pending;
702+
});
703+
697704
const processThreadPublish = Effect.fnUntraced(function* (threadId: ThreadId) {
698705
const retry = publishRetries.get(threadId);
699706
if (retry?.timer !== undefined) {

0 commit comments

Comments
 (0)