Skip to content

Commit 3ddd398

Browse files
fix(server): settled threads no longer re-send relay clears on every PR update (#29369)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent a497f7b commit 3ddd398

2 files changed

Lines changed: 129 additions & 16 deletions

File tree

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

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,113 @@ describe("AgentAwarenessRelay", () => {
482482
}),
483483
);
484484

485+
it.effect("publishes a cleared thread's tombstone once while it stays cleared", () =>
486+
Effect.gen(function* () {
487+
const { relay, currentShell, publications } = yield* makeTestRelay();
488+
yield* relay.publishThread(THREAD_ID);
489+
const completed = shell({ status: "completed" });
490+
yield* Ref.set(currentShell, completed);
491+
yield* relay.publishThread(THREAD_ID);
492+
yield* TestClock.adjust("5 seconds");
493+
yield* relay.drain;
494+
yield* Ref.set(currentShell, {
495+
...completed,
496+
settledOverride: "settled",
497+
settledAt: yield* DateTime.now,
498+
lastVisitedAt: null,
499+
});
500+
yield* relay.publishThread(THREAD_ID);
501+
yield* TestClock.adjust("5 seconds");
502+
yield* relay.drain;
503+
assert.deepEqual(
504+
publications.map((publication) => publication.state?.phase ?? null),
505+
["running", "completed", null],
506+
);
507+
508+
for (let i = 0; i < 3; i++) {
509+
yield* relay.publishThread(THREAD_ID);
510+
yield* TestClock.adjust("5 seconds");
511+
yield* relay.drain;
512+
}
513+
assert.deepEqual(
514+
publications.map((publication) => publication.state?.phase ?? null),
515+
["running", "completed", null],
516+
);
517+
}),
518+
);
519+
520+
it.effect("does not alert an old completion after publishing its thread's tombstone", () =>
521+
Effect.gen(function* () {
522+
const oldCompleted = shell({
523+
status: "completed",
524+
latestRunCompletedAt: DateTime.makeUnsafe("2026-09-04T11:59:00.000Z"),
525+
});
526+
yield* TestClock.setTime(DateTime.toEpochMillis(DateTime.makeUnsafe(NOW)));
527+
const { relay, currentShell, publications } = yield* makeTestRelay();
528+
yield* Ref.set(currentShell, {
529+
...oldCompleted,
530+
settledOverride: "settled",
531+
settledAt: DateTime.makeUnsafe(NOW),
532+
lastVisitedAt: null,
533+
});
534+
yield* relay.publishThread(THREAD_ID);
535+
yield* TestClock.adjust("5 seconds");
536+
yield* relay.drain;
537+
assert.deepEqual(
538+
publications.map((publication) => publication.state),
539+
[null],
540+
);
541+
542+
yield* Ref.set(currentShell, oldCompleted);
543+
yield* relay.publishThread(THREAD_ID);
544+
yield* TestClock.adjust("5 seconds");
545+
yield* relay.drain;
546+
assert.deepEqual(
547+
publications.map((publication) => publication.state),
548+
[null],
549+
);
550+
}),
551+
);
552+
553+
it.effect("alerts a completion settled during its hold after an earlier tombstone", () =>
554+
Effect.gen(function* () {
555+
yield* TestClock.setTime(DateTime.toEpochMillis(DateTime.makeUnsafe(NOW)));
556+
const { relay, currentShell, publications } = yield* makeTestRelay();
557+
const settledAt = DateTime.makeUnsafe(NOW);
558+
yield* Ref.set(currentShell, {
559+
...shell({ status: "completed" }),
560+
settledOverride: "settled",
561+
settledAt,
562+
lastVisitedAt: null,
563+
});
564+
yield* relay.publishThread(THREAD_ID);
565+
yield* TestClock.adjust("5 seconds");
566+
yield* relay.drain;
567+
568+
const completed = shell({
569+
status: "completed",
570+
latestRunCompletedAt: DateTime.makeUnsafe("2026-09-04T12:00:06.000Z"),
571+
});
572+
yield* Ref.set(currentShell, completed);
573+
yield* relay.publishThread(THREAD_ID);
574+
yield* Ref.set(currentShell, {
575+
...completed,
576+
settledOverride: "settled",
577+
settledAt,
578+
lastVisitedAt: null,
579+
});
580+
yield* relay.publishThread(THREAD_ID);
581+
yield* TestClock.adjust("5 seconds");
582+
yield* relay.drain;
583+
yield* TestClock.adjust("5 seconds");
584+
yield* relay.drain;
585+
assert.deepEqual(
586+
publications.map((publication) => publication.state?.phase ?? null),
587+
[null, "completed", null],
588+
);
589+
}),
590+
);
591+
485592
it.effect("drops a held completion when a queued continuation hides the thread", () =>
486593
Effect.gen(function* () {
487594
const { relay, currentShell, publications } = yield* makeTestRelay();

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

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,17 @@ function agentAwarenessPublishIdentity(state: RelayAgentActivityState | null): s
165165
return JSON.stringify(meaningfulState);
166166
}
167167

168+
const CLEARED_PUBLISH_IDENTITY = agentAwarenessPublishIdentity(null);
169+
170+
// A published tombstone stays recorded so repeats are skipped, but it is not live state.
171+
function hasLivePublishedState(
172+
publishedStateByThread: ReadonlyMap<ThreadId, string>,
173+
threadId: ThreadId,
174+
): boolean {
175+
const identity = publishedStateByThread.get(threadId);
176+
return identity !== undefined && identity !== CLEARED_PUBLISH_IDENTITY;
177+
}
178+
168179
function resolveAgentActivityPublishingStartupState(input: {
169180
readonly relayConfigured: boolean;
170181
readonly publishEnabled: boolean;
@@ -517,7 +528,7 @@ export const make = Effect.gen(function* () {
517528
if (
518529
threadShell !== null &&
519530
!isListedThreadLineage(threadShell.lineage) &&
520-
!(yield* Ref.get(publishedStateByThreadRef)).has(threadId)
531+
!hasLivePublishedState(yield* Ref.get(publishedStateByThreadRef), threadId)
521532
) {
522533
// Subagents and side chats have no relay rows to clear; per-event tombstones would needlessly redeliver user aggregates.
523534
return;
@@ -538,11 +549,15 @@ export const make = Effect.gen(function* () {
538549
const publishedStateByThread = yield* Ref.get(publishedStateByThreadRef);
539550
if (
540551
(snapshot.state?.phase === "completed" || snapshot.state?.phase === "failed") &&
541-
!publishedStateByThread.has(threadId)
552+
!hasLivePublishedState(publishedStateByThread, threadId)
542553
) {
543554
if (Option.isNone(thread) || !terminalWorkSinceStart(thread.value, startedAt)) return;
544555
}
545-
if (publishedStateByThread.get(threadId) === publishIdentity) {
556+
if (
557+
publishedStateByThread.get(threadId) === publishIdentity &&
558+
// A held completion settled after an earlier clear still has to alert.
559+
!(snapshot.state === null && heldCompletions.has(threadId))
560+
) {
546561
// Clear moot deadlines so later transient nulls cannot skip their deferral window.
547562
publishConfirmDeadlines.delete(threadId);
548563
heldCompletions.delete(threadId);
@@ -557,10 +572,7 @@ export const make = Effect.gen(function* () {
557572
// Confirm tombstones over live state to avoid removing active cards.
558573
// Confirm completion: a run can end moments before its continuation starts, and the relay
559574
// alerts on every move into completed.
560-
const requiresConfirmation =
561-
(snapshot.state === null &&
562-
publishedStateByThread.get(threadId) !== agentAwarenessPublishIdentity(null)) ||
563-
snapshot.state?.phase === "completed";
575+
const requiresConfirmation = snapshot.state === null || snapshot.state?.phase === "completed";
564576
if (requiresConfirmation) {
565577
if (snapshot.state?.phase === "completed") heldCompletions.add(threadId);
566578
const nowMs = (yield* DateTime.now).epochMilliseconds;
@@ -640,15 +652,9 @@ export const make = Effect.gen(function* () {
640652
}).pipe(withRelayClientTracing);
641653
publishConfirmDeadlines.delete(threadId);
642654
heldCompletions.delete(threadId);
643-
yield* Ref.update(publishedStateByThreadRef, (publishedStates) => {
644-
const nextPublishedStates = new Map(publishedStates);
645-
if (snapshot.state === null) {
646-
nextPublishedStates.delete(threadId);
647-
} else {
648-
nextPublishedStates.set(threadId, publishIdentity);
649-
}
650-
return nextPublishedStates;
651-
});
655+
yield* Ref.update(publishedStateByThreadRef, (publishedStates) =>
656+
new Map(publishedStates).set(threadId, publishIdentity),
657+
);
652658
});
653659

654660
const processThreadPublish = Effect.fnUntraced(function* (threadId: ThreadId) {

0 commit comments

Comments
 (0)