Skip to content

Commit 2e78c26

Browse files
PR Batch Testercodex
andcommitted
fix(server): alert for child requests with matching timestamps
Use observed request-change order to break timestamp ties, including replacements discovered while rereading a waiting child. Preserve approval priority, newer timestamp selection, and unchanged-request deduplication. Co-Authored-By: GPT-6.1 Sol <[email protected]>
1 parent 347c7e5 commit 2e78c26

2 files changed

Lines changed: 72 additions & 4 deletions

File tree

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,56 @@ describe("AgentAwarenessRelay", () => {
443443
}),
444444
);
445445

446+
it.effect("announces equal-timestamp child requests in request-change order", () =>
447+
Effect.gen(function* () {
448+
const secondChildId = ThreadId.make("relay-second-child-thread");
449+
const shells = yield* Ref.make(
450+
new Map<ThreadId, OrchestrationV2ThreadShell>([
451+
[THREAD_ID, shell()],
452+
[CHILD_THREAD_ID, waitingChild()],
453+
[secondChildId, waitingChild({ id: secondChildId, pendingRuntimeRequest: null })],
454+
]),
455+
);
456+
const setRequest = (childId: ThreadId, requestId: string, updatedAt = NOW) =>
457+
Ref.update(shells, (current) =>
458+
new Map(current).set(
459+
childId,
460+
waitingChild({
461+
id: childId,
462+
updatedAt: DateTime.makeUnsafe(updatedAt),
463+
pendingRuntimeRequest: {
464+
id: RuntimeRequestId.make(requestId),
465+
kind: "command",
466+
createdAt: DateTime.makeUnsafe(updatedAt),
467+
},
468+
}),
469+
),
470+
);
471+
const { relay, publications } = yield* makeTestRelay({
472+
readShell: (threadId) =>
473+
Ref.get(shells).pipe(Effect.map((current) => current.get(threadId) ?? null)),
474+
});
475+
yield* relay.publishThread(CHILD_THREAD_ID);
476+
yield* setRequest(secondChildId, "second-child-request");
477+
yield* relay.publishThread(secondChildId);
478+
assert.equal(publications.length, 2);
479+
480+
yield* setRequest(CHILD_THREAD_ID, "first-child-replacement");
481+
yield* relay.publishThread(CHILD_THREAD_ID);
482+
assert.equal(publications.length, 3);
483+
484+
yield* setRequest(secondChildId, "second-child-replacement");
485+
yield* relay.publishThread(THREAD_ID);
486+
assert.equal(publications.length, 4);
487+
assert.equal(new Set(publications.map(({ state }) => state?.updatedAt)).size, 4);
488+
489+
yield* relay.publishThread(CHILD_THREAD_ID);
490+
yield* setRequest(CHILD_THREAD_ID, "older-request", "2026-09-04T11:59:00.000Z");
491+
yield* relay.publishThread(CHILD_THREAD_ID);
492+
assert.equal(publications.length, 4);
493+
}),
494+
);
495+
446496
it.effect(
447497
"does not republish a parent waiting on its own request when a child's comes and goes",
448498
() =>

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

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@ interface SubagentRequest {
304304
readonly topLevelThreadId: ThreadId;
305305
readonly request: PendingSubagentRequest;
306306
readonly requestId: RuntimeRequestId;
307+
readonly changeOrder: number;
307308
}
308309

309310
function resolveAgentAwarenessRelayPublishSnapshot(input: {
@@ -408,6 +409,7 @@ export const make = Effect.gen(function* () {
408409
new Map<ThreadId, { readonly identity: string; readonly updatedAt: string }>(),
409410
);
410411
const subagentRequests = new Map<ThreadId, SubagentRequest>();
412+
let subagentRequestChangeOrder = 0;
411413
// Environments that never publish skip the extra shell read while paused.
412414
// Set by the first publish or by a startup snapshot, even an empty one.
413415
let hasPublished = false;
@@ -727,7 +729,12 @@ export const make = Effect.gen(function* () {
727729
const topLevelThreadId =
728730
previous?.topLevelThreadId ?? (yield* findTopLevelThreadId(threadShell));
729731
if (topLevelThreadId === null) return;
730-
subagentRequests.set(threadId, { topLevelThreadId, request, requestId });
732+
subagentRequests.set(threadId, {
733+
topLevelThreadId,
734+
request,
735+
requestId,
736+
changeOrder: ++subagentRequestChangeOrder,
737+
});
731738
yield* enqueueTopLevelPublish(topLevelThreadId);
732739
});
733740

@@ -742,6 +749,7 @@ export const make = Effect.gen(function* () {
742749
// unlinked, so each waiting child is re-read before its parent is published.
743750
const currentSubagentRequestFor = Effect.fnUntraced(function* (topLevelThreadId: ThreadId) {
744751
let pending: SubagentRequestAwareness | null = null;
752+
let pendingChangeOrder = 0;
745753
for (const [childId, entry] of [...subagentRequests]) {
746754
if (entry.topLevelThreadId !== topLevelThreadId) continue;
747755
const child = yield* threads.getThreadShell(childId);
@@ -752,19 +760,29 @@ export const make = Effect.gen(function* () {
752760
subagentRequests.delete(childId);
753761
continue;
754762
}
763+
let changeOrder = entry.changeOrder;
755764
if (request !== entry.request || pendingRequest.id !== entry.requestId) {
756-
subagentRequests.set(childId, { ...entry, request, requestId: pendingRequest.id });
765+
changeOrder = ++subagentRequestChangeOrder;
766+
subagentRequests.set(childId, {
767+
...entry,
768+
request,
769+
requestId: pendingRequest.id,
770+
changeOrder,
771+
});
757772
}
758773
const current = { kind: request, requestId: pendingRequest.id, updatedAt: child.updatedAt };
759774
// Approval outranks input; within a kind the newest wait is the one to
760-
// announce, so a second child asking still reaches the phone.
775+
// announce. Request-change order breaks ties within one millisecond.
761776
if (
762777
pending === null ||
763778
(current.kind === "approval" && pending.kind !== "approval") ||
764779
(current.kind === pending.kind &&
765-
DateTime.isGreaterThan(current.updatedAt, pending.updatedAt))
780+
(DateTime.isGreaterThan(current.updatedAt, pending.updatedAt) ||
781+
(current.updatedAt.epochMilliseconds === pending.updatedAt.epochMilliseconds &&
782+
changeOrder > pendingChangeOrder)))
766783
) {
767784
pending = current;
785+
pendingChangeOrder = changeOrder;
768786
}
769787
}
770788
return pending;

0 commit comments

Comments
 (0)