Skip to content

Commit cd6c07f

Browse files
shivamhwpclaude
andcommitted
fix(server): an unchanged child approval keeps its alert time
A parent rename or relink re-advanced the alert time of a child's unchanged approval, so Android showed it again. Also drop the unused child-question path and trim overlapping tests. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent a9fdb0f commit cd6c07f

4 files changed

Lines changed: 114 additions & 234 deletions

File tree

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

Lines changed: 36 additions & 151 deletions
Original file line numberDiff line numberDiff line change
@@ -162,16 +162,6 @@ describe("startup agent activity", () => {
162162
});
163163
assert.deepStrictEqual(ids, [THREAD_ID, newCompleted, newFailed]);
164164
});
165-
166-
it("publishes a subagent waiting on the user so its parent is republished", () => {
167-
const ids = AgentAwarenessRelay.resolveAgentAwarenessRelayActiveThreadIds({
168-
environmentId: EnvironmentId.make("relay-env"),
169-
startedAt: DateTime.toEpochMillis(DateTime.makeUnsafe(NOW)),
170-
projects: [{ id: PROJECT_ID, title: "Project" }],
171-
threads: [shell({ status: "idle" }), waitingChild()],
172-
});
173-
assert.deepStrictEqual(ids, [CHILD_THREAD_ID]);
174-
});
175165
});
176166

177167
const makeTestRelay = Effect.fnUntraced(function* (
@@ -344,7 +334,7 @@ const makeTestRelay = Effect.fnUntraced(function* (
344334
});
345335

346336
describe("AgentAwarenessRelay", () => {
347-
it.effect.each([false, true])("publishes a startup approval through side chat: %s", (sideChat) =>
337+
it.effect("publishes a startup approval through a side chat", () =>
348338
Effect.gen(function* () {
349339
const parent = shell({ status: "idle" });
350340
const intermediate = shell({
@@ -359,7 +349,7 @@ describe("AgentAwarenessRelay", () => {
359349
const child = waitingChild({
360350
lineage: {
361351
rootThreadId: THREAD_ID,
362-
parentThreadId: sideChat ? intermediate.id : parent.id,
352+
parentThreadId: intermediate.id,
363353
relationshipToParent: "subagent",
364354
},
365355
});
@@ -421,61 +411,54 @@ describe("AgentAwarenessRelay", () => {
421411
assert.equal(publications[1]?.state?.phase, "waiting_for_approval");
422412
assert.equal(publications[1]?.state?.threadId, THREAD_ID);
423413

424-
yield* setShell(waitingChild({ pendingRuntimeRequest: null }));
425-
yield* relay.publishThread(CHILD_THREAD_ID);
426-
assert.equal(publications.length, 2);
427-
yield* TestClock.adjust("5 seconds");
428-
yield* relay.drain;
429-
assert.equal(publications.length, 3);
430-
assert.equal(publications[2]?.state?.phase, "completed");
431-
432-
// Phones key alerts by phase and time, so a second wait needs a new time.
414+
// A rename keeps the same request's alert time, so the phone doesn't re-alert.
433415
yield* setShell(
434-
waitingChild({
435-
updatedAt: DateTime.makeUnsafe("2026-09-04T12:05:00.000Z"),
436-
pendingRuntimeRequest: {
437-
id: RuntimeRequestId.make("child-request-2"),
438-
kind: "command",
439-
createdAt: DateTime.makeUnsafe("2026-09-04T12:05:00.000Z"),
440-
},
416+
shell({
417+
status: "completed",
418+
title: "Renamed thread",
419+
updatedAt: DateTime.makeUnsafe("2026-09-04T12:01:00.000Z"),
441420
}),
442421
);
443-
yield* relay.publishThread(CHILD_THREAD_ID);
444-
assert.equal(publications.length, 4);
445-
assert.equal(publications[3]?.state?.phase, "waiting_for_approval");
446-
assert.equal(publications[3]?.state?.updatedAt, "2026-09-04T12:05:00.000Z");
447-
assert.notEqual(publications[3]?.state?.updatedAt, publications[1]?.state?.updatedAt);
422+
yield* relay.publishThread(THREAD_ID);
423+
assert.equal(publications.length, 3);
424+
assert.equal(publications[2]?.state?.threadTitle, "Renamed thread");
425+
assert.equal(publications[2]?.state?.updatedAt, publications[1]?.state?.updatedAt);
448426

449427
// With two pending requests, answering one shows the next, possibly an
450428
// older one, with no clear between.
451429
yield* setShell(
452430
waitingChild({
453431
updatedAt: DateTime.makeUnsafe("2026-09-04T12:06:00.000Z"),
454432
pendingRuntimeRequest: {
455-
id: RuntimeRequestId.make("child-request-3"),
433+
id: RuntimeRequestId.make("child-request-2"),
456434
kind: "command",
457-
createdAt: DateTime.makeUnsafe("2026-09-04T12:04:00.000Z"),
435+
createdAt: DateTime.makeUnsafe("2026-09-04T11:59:00.000Z"),
458436
},
459437
}),
460438
);
461439
yield* relay.publishThread(CHILD_THREAD_ID);
440+
assert.equal(publications.length, 4);
441+
assert.equal(publications[3]?.state?.phase, "waiting_for_approval");
442+
assert.equal(publications[3]?.state?.updatedAt, "2026-09-04T12:06:00.000Z");
443+
444+
yield* setShell(waitingChild({ pendingRuntimeRequest: null }));
445+
yield* relay.publishThread(CHILD_THREAD_ID);
446+
assert.equal(publications.length, 4);
447+
yield* TestClock.adjust("5 seconds");
448+
yield* relay.drain;
462449
assert.equal(publications.length, 5);
463-
assert.equal(publications[4]?.state?.phase, "waiting_for_approval");
464-
assert.equal(publications[4]?.state?.updatedAt, "2026-09-04T12:06:00.000Z");
450+
assert.equal(publications[4]?.state?.phase, "completed");
465451
}),
466452
);
467453

468-
it.effect.each([
469-
{ parentUpdatedAt: NOW, alertTime: "2026-09-04T12:05:00.000Z" },
470-
{ parentUpdatedAt: "2026-09-04T12:10:00.000Z", alertTime: "2026-09-04T12:10:00.001Z" },
471-
])(
472-
"announces overlapping child approvals with parent updated at $parentUpdatedAt",
473-
({ parentUpdatedAt, alertTime }) =>
454+
it.effect(
455+
"announces an overlapping child approval from the same millisecond under a newer parent",
456+
() =>
474457
Effect.gen(function* () {
475458
const secondChildId = ThreadId.make("relay-second-child-thread");
476459
const shells = yield* Ref.make(
477460
new Map<ThreadId, OrchestrationV2ThreadShell>([
478-
[THREAD_ID, shell({ updatedAt: DateTime.makeUnsafe(parentUpdatedAt) })],
461+
[THREAD_ID, shell({ updatedAt: DateTime.makeUnsafe("2026-09-04T12:10:00.000Z") })],
479462
[CHILD_THREAD_ID, waitingChild()],
480463
[secondChildId, waitingChild({ id: secondChildId, pendingRuntimeRequest: null })],
481464
]),
@@ -496,18 +479,17 @@ describe("AgentAwarenessRelay", () => {
496479
secondChildId,
497480
waitingChild({
498481
id: secondChildId,
499-
updatedAt: DateTime.makeUnsafe("2026-09-04T12:05:00.000Z"),
500482
pendingRuntimeRequest: {
501483
id: RuntimeRequestId.make("second-child-request"),
502484
kind: "command",
503-
createdAt: DateTime.makeUnsafe("2026-09-04T12:05:00.000Z"),
485+
createdAt: DateTime.makeUnsafe(NOW),
504486
},
505487
}),
506488
),
507489
);
508490
yield* relay.publishThread(secondChildId);
509491
assert.equal(publications.length, 2);
510-
assert.equal(publications[1]?.state?.updatedAt, alertTime);
492+
assert.equal(publications[1]?.state?.updatedAt, "2026-09-04T12:10:00.001Z");
511493
assert.notEqual(publications[1]?.state?.updatedAt, publications[0]?.state?.updatedAt);
512494

513495
yield* TestClock.adjust("1 second");
@@ -556,56 +538,6 @@ describe("AgentAwarenessRelay", () => {
556538
}),
557539
);
558540

559-
it.effect("announces equal-timestamp child requests in request-change order", () =>
560-
Effect.gen(function* () {
561-
const secondChildId = ThreadId.make("relay-second-child-thread");
562-
const shells = yield* Ref.make(
563-
new Map<ThreadId, OrchestrationV2ThreadShell>([
564-
[THREAD_ID, shell()],
565-
[CHILD_THREAD_ID, waitingChild()],
566-
[secondChildId, waitingChild({ id: secondChildId, pendingRuntimeRequest: null })],
567-
]),
568-
);
569-
const setRequest = (childId: ThreadId, requestId: string, updatedAt = NOW) =>
570-
Ref.update(shells, (current) =>
571-
new Map(current).set(
572-
childId,
573-
waitingChild({
574-
id: childId,
575-
updatedAt: DateTime.makeUnsafe(updatedAt),
576-
pendingRuntimeRequest: {
577-
id: RuntimeRequestId.make(requestId),
578-
kind: "command",
579-
createdAt: DateTime.makeUnsafe(updatedAt),
580-
},
581-
}),
582-
),
583-
);
584-
const { relay, publications } = yield* makeTestRelay({
585-
readShell: (threadId) =>
586-
Ref.get(shells).pipe(Effect.map((current) => current.get(threadId) ?? null)),
587-
});
588-
yield* relay.publishThread(CHILD_THREAD_ID);
589-
yield* setRequest(secondChildId, "second-child-request");
590-
yield* relay.publishThread(secondChildId);
591-
assert.equal(publications.length, 2);
592-
593-
yield* setRequest(CHILD_THREAD_ID, "first-child-replacement");
594-
yield* relay.publishThread(CHILD_THREAD_ID);
595-
assert.equal(publications.length, 3);
596-
597-
yield* setRequest(secondChildId, "second-child-replacement");
598-
yield* relay.publishThread(THREAD_ID);
599-
assert.equal(publications.length, 4);
600-
assert.equal(new Set(publications.map(({ state }) => state?.updatedAt)).size, 4);
601-
602-
yield* relay.publishThread(CHILD_THREAD_ID);
603-
yield* setRequest(CHILD_THREAD_ID, "older-request", "2026-09-04T11:59:00.000Z");
604-
yield* relay.publishThread(CHILD_THREAD_ID);
605-
assert.equal(publications.length, 4);
606-
}),
607-
);
608-
609541
it.effect(
610542
"does not republish a parent waiting on its own request when a child's comes and goes",
611543
() =>
@@ -685,74 +617,28 @@ describe("AgentAwarenessRelay", () => {
685617
}),
686618
);
687619

688-
it.effect("keeps a subagent request that started while publishing was off", () =>
620+
it.effect("tracks subagent requests that start and clear while publishing is off", () =>
689621
Effect.gen(function* () {
690622
const shells = yield* Ref.make(
691623
new Map<ThreadId, OrchestrationV2ThreadShell>([
692624
[THREAD_ID, shell()],
693625
[CHILD_THREAD_ID, waitingChild({ pendingRuntimeRequest: null })],
694626
]),
695627
);
696-
const { relay, secrets, publications } = yield* makeTestRelay({
628+
const { relay, secrets, publications, catchUp } = yield* makeTestRelay({
697629
readShell: (threadId) =>
698630
Ref.get(shells).pipe(Effect.map((current) => current.get(threadId) ?? null)),
699631
});
700-
yield* relay.publishThread(THREAD_ID);
701-
assert.equal(publications[0]?.state?.phase, "running");
632+
yield* relay.start();
633+
yield* TestClock.adjust("1 second");
634+
assert.equal(catchUp.activitySnapshotReads, 1);
635+
assert.equal(publications.length, 0);
702636

703637
yield* secrets.set(PUBLISH_AGENT_ACTIVITY_SECRET, new TextEncoder().encode("false"));
704638
yield* Ref.update(shells, (current) => new Map(current).set(CHILD_THREAD_ID, waitingChild()));
705639
yield* relay.publishThread(CHILD_THREAD_ID);
706640
yield* secrets.set(PUBLISH_AGENT_ACTIVITY_SECRET, new TextEncoder().encode("true"));
707641
yield* relay.publishThread(THREAD_ID);
708-
assert.equal(publications.length, 2);
709-
assert.equal(publications[1]?.state?.phase, "waiting_for_approval");
710-
}),
711-
);
712-
713-
it.effect(
714-
"keeps a subagent request that started while publishing was off after an empty startup",
715-
() =>
716-
Effect.gen(function* () {
717-
const shells = yield* Ref.make(
718-
new Map<ThreadId, OrchestrationV2ThreadShell>([
719-
[THREAD_ID, shell()],
720-
[CHILD_THREAD_ID, waitingChild({ pendingRuntimeRequest: null })],
721-
]),
722-
);
723-
const { relay, secrets, publications, catchUp } = yield* makeTestRelay({
724-
readShell: (threadId) =>
725-
Ref.get(shells).pipe(Effect.map((current) => current.get(threadId) ?? null)),
726-
});
727-
yield* relay.start();
728-
yield* TestClock.adjust("1 second");
729-
assert.equal(catchUp.activitySnapshotReads, 1);
730-
assert.equal(publications.length, 0);
731-
732-
yield* secrets.set(PUBLISH_AGENT_ACTIVITY_SECRET, new TextEncoder().encode("false"));
733-
yield* Ref.update(shells, (current) =>
734-
new Map(current).set(CHILD_THREAD_ID, waitingChild()),
735-
);
736-
yield* relay.publishThread(CHILD_THREAD_ID);
737-
yield* secrets.set(PUBLISH_AGENT_ACTIVITY_SECRET, new TextEncoder().encode("true"));
738-
yield* relay.publishThread(THREAD_ID);
739-
assert.equal(publications[0]?.state?.phase, "waiting_for_approval");
740-
}).pipe(Effect.scoped),
741-
);
742-
743-
it.effect("drops a subagent request that cleared while publishing was off", () =>
744-
Effect.gen(function* () {
745-
const shells = yield* Ref.make(
746-
new Map<ThreadId, OrchestrationV2ThreadShell>([
747-
[THREAD_ID, shell()],
748-
[CHILD_THREAD_ID, waitingChild()],
749-
]),
750-
);
751-
const { relay, secrets, publications } = yield* makeTestRelay({
752-
readShell: (threadId) =>
753-
Ref.get(shells).pipe(Effect.map((current) => current.get(threadId) ?? null)),
754-
});
755-
yield* relay.publishThread(CHILD_THREAD_ID);
756642
assert.equal(publications[0]?.state?.phase, "waiting_for_approval");
757643

758644
yield* secrets.set(PUBLISH_AGENT_ACTIVITY_SECRET, new TextEncoder().encode("false"));
@@ -762,9 +648,8 @@ describe("AgentAwarenessRelay", () => {
762648
yield* relay.publishThread(CHILD_THREAD_ID);
763649
yield* secrets.set(PUBLISH_AGENT_ACTIVITY_SECRET, new TextEncoder().encode("true"));
764650
yield* relay.publishThread(THREAD_ID);
765-
assert.equal(publications.length, 2);
766651
assert.equal(publications[1]?.state?.phase, "running");
767-
}),
652+
}).pipe(Effect.scoped),
768653
);
769654

770655
it.effect("coalesces queued updates and reruns a thread dirtied during publishing", () =>

0 commit comments

Comments
 (0)