The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 22, 07:00 UTC
fix(server): settled threads no longer re-send relay clears on every PR update (#29369)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Nov 2, 2026 at 11:44 UTC
3ddd3989e03e44c0106c6e3e44033a2f5b8b50ae
2 files changed
+129
-16
apps/server/src/relay/AgentAwarenessRelay.test.ts
+107
@@ -482,6 +482,113 @@ describe("AgentAwarenessRelay", () => {
482
}),
483
);
484
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
+
592
it.effect("drops a held completion when a queued continuation hides the thread", () =>
593
Effect.gen(function* () {
594
const { relay, currentShell, publications } = yield* makeTestRelay();
apps/server/src/relay/AgentAwarenessRelay.ts
+22
-16
@@ -165,6 +165,17 @@ function agentAwarenessPublishIdentity(state: RelayAgentActivityState | null): s
165
return JSON.stringify(meaningfulState);
166
}
167
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
+
179
function resolveAgentActivityPublishingStartupState(input: {
180
readonly relayConfigured: boolean;
181
readonly publishEnabled: boolean;
@@ -517,7 +528,7 @@ export const make = Effect.gen(function* () {
528
if (
529
threadShell !== null &&
530
!isListedThreadLineage(threadShell.lineage) &&
520
- !(yield* Ref.get(publishedStateByThreadRef)).has(threadId)
531
+ !hasLivePublishedState(yield* Ref.get(publishedStateByThreadRef), threadId)
532
) {
533
// Subagents and side chats have no relay rows to clear; per-event tombstones would needlessly redeliver user aggregates.
534
return;
@@ -538,11 +549,15 @@ export const make = Effect.gen(function* () {
549
const publishedStateByThread = yield* Ref.get(publishedStateByThreadRef);
550
if (
551
(snapshot.state?.phase === "completed" || snapshot.state?.phase === "failed") &&
541
- !publishedStateByThread.has(threadId)
552
+ !hasLivePublishedState(publishedStateByThread, threadId)
553
) {
554
if (Option.isNone(thread) || !terminalWorkSinceStart(thread.value, startedAt)) return;
555
}
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
+ ) {
561
// Clear moot deadlines so later transient nulls cannot skip their deferral window.
562
publishConfirmDeadlines.delete(threadId);
563
heldCompletions.delete(threadId);
@@ -557,10 +572,7 @@ export const make = Effect.gen(function* () {
572
// Confirm tombstones over live state to avoid removing active cards.
573
// Confirm completion: a run can end moments before its continuation starts, and the relay
574
// 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";
576
if (requiresConfirmation) {
577
if (snapshot.state?.phase === "completed") heldCompletions.add(threadId);
578
const nowMs = (yield* DateTime.now).epochMilliseconds;
@@ -640,15 +652,9 @@ export const make = Effect.gen(function* () {
652
}).pipe(withRelayClientTracing);
653
publishConfirmDeadlines.delete(threadId);
654
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
+ );
658
});
659
660
const processThreadPublish = Effect.fnUntraced(function* (threadId: ThreadId) {