TypeScript
fix(server): settled threads no longer re-send relay clears on every PR update (#29369)
Julius Marminge3ddd3989e03e
diff --git a/apps/server/src/relay/AgentAwarenessRelay.test.ts b/apps/server/src/relay/AgentAwarenessRelay.test.tsindex 4bbaa85fc3b..2a259114b09 100644--- a/apps/server/src/relay/AgentAwarenessRelay.test.ts+++ b/apps/server/src/relay/AgentAwarenessRelay.test.ts@@ -482,6 +482,113 @@ describe("AgentAwarenessRelay", () => {}),);+ it.effect("publishes a cleared thread's tombstone once while it stays cleared", () =>+ Effect.gen(function* () {+ const { relay, currentShell, publications } = yield* makeTestRelay();+ yield* relay.publishThread(THREAD_ID);+ const completed = shell({ status: "completed" });+ yield* Ref.set(currentShell, completed);+ yield* relay.publishThread(THREAD_ID);+ yield* TestClock.adjust("5 seconds");+ yield* relay.drain;+ yield* Ref.set(currentShell, {+ ...completed,+ settledOverride: "settled",+ settledAt: yield* DateTime.now,+ lastVisitedAt: null,+ });+ yield* relay.publishThread(THREAD_ID);+ yield* TestClock.adjust("5 seconds");+ yield* relay.drain;+ assert.deepEqual(+ publications.map((publication) => publication.state?.phase ?? null),+ ["running", "completed", null],+ );++ for (let i = 0; i < 3; i++) {+ yield* relay.publishThread(THREAD_ID);+ yield* TestClock.adjust("5 seconds");+ yield* relay.drain;+ }+ assert.deepEqual(+ publications.map((publication) => publication.state?.phase ?? null),+ ["running", "completed", null],+ );+ }),+ );++ it.effect("does not alert an old completion after publishing its thread's tombstone", () =>+ Effect.gen(function* () {+ const oldCompleted = shell({+ status: "completed",+ latestRunCompletedAt: DateTime.makeUnsafe("2026-09-04T11:59:00.000Z"),+ });+ yield* TestClock.setTime(DateTime.toEpochMillis(DateTime.makeUnsafe(NOW)));+ const { relay, currentShell, publications } = yield* makeTestRelay();+ yield* Ref.set(currentShell, {+ ...oldCompleted,+ settledOverride: "settled",+ settledAt: DateTime.makeUnsafe(NOW),+ lastVisitedAt: null,+ });+ yield* relay.publishThread(THREAD_ID);+ yield* TestClock.adjust("5 seconds");+ yield* relay.drain;+ assert.deepEqual(+ publications.map((publication) => publication.state),+ [null],+ );++ yield* Ref.set(currentShell, oldCompleted);+ yield* relay.publishThread(THREAD_ID);+ yield* TestClock.adjust("5 seconds");+ yield* relay.drain;+ assert.deepEqual(+ publications.map((publication) => publication.state),+ [null],+ );+ }),+ );++ it.effect("alerts a completion settled during its hold after an earlier tombstone", () =>+ Effect.gen(function* () {+ yield* TestClock.setTime(DateTime.toEpochMillis(DateTime.makeUnsafe(NOW)));+ const { relay, currentShell, publications } = yield* makeTestRelay();+ const settledAt = DateTime.makeUnsafe(NOW);+ yield* Ref.set(currentShell, {+ ...shell({ status: "completed" }),+ settledOverride: "settled",+ settledAt,+ lastVisitedAt: null,+ });+ yield* relay.publishThread(THREAD_ID);+ yield* TestClock.adjust("5 seconds");+ yield* relay.drain;++ const completed = shell({+ status: "completed",+ latestRunCompletedAt: DateTime.makeUnsafe("2026-09-04T12:00:06.000Z"),+ });+ yield* Ref.set(currentShell, completed);+ yield* relay.publishThread(THREAD_ID);+ yield* Ref.set(currentShell, {+ ...completed,+ settledOverride: "settled",+ settledAt,+ lastVisitedAt: null,+ });+ yield* relay.publishThread(THREAD_ID);+ yield* TestClock.adjust("5 seconds");+ yield* relay.drain;+ yield* TestClock.adjust("5 seconds");+ yield* relay.drain;+ assert.deepEqual(+ publications.map((publication) => publication.state?.phase ?? null),+ [null, "completed", null],+ );+ }),+ );+it.effect("drops a held completion when a queued continuation hides the thread", () =>Effect.gen(function* () {const { relay, currentShell, publications } = yield* makeTestRelay();diff --git a/apps/server/src/relay/AgentAwarenessRelay.ts b/apps/server/src/relay/AgentAwarenessRelay.tsindex 66e5a3def7c..076afc92d73 100644--- a/apps/server/src/relay/AgentAwarenessRelay.ts+++ b/apps/server/src/relay/AgentAwarenessRelay.ts@@ -165,6 +165,17 @@ function agentAwarenessPublishIdentity(state: RelayAgentActivityState | null): sreturn JSON.stringify(meaningfulState);}+const CLEARED_PUBLISH_IDENTITY = agentAwarenessPublishIdentity(null);++// A published tombstone stays recorded so repeats are skipped, but it is not live state.+function hasLivePublishedState(+ publishedStateByThread: ReadonlyMap<ThreadId, string>,+ threadId: ThreadId,+): boolean {+ const identity = publishedStateByThread.get(threadId);+ return identity !== undefined && identity !== CLEARED_PUBLISH_IDENTITY;+}+function resolveAgentActivityPublishingStartupState(input: {readonly relayConfigured: boolean;readonly publishEnabled: boolean;@@ -517,7 +528,7 @@ export const make = Effect.gen(function* () {if (threadShell !== null &&!isListedThreadLineage(threadShell.lineage) &&- !(yield* Ref.get(publishedStateByThreadRef)).has(threadId)+ !hasLivePublishedState(yield* Ref.get(publishedStateByThreadRef), threadId)) {// Subagents and side chats have no relay rows to clear; per-event tombstones would needlessly redeliver user aggregates.return;@@ -538,11 +549,15 @@ export const make = Effect.gen(function* () {const publishedStateByThread = yield* Ref.get(publishedStateByThreadRef);if ((snapshot.state?.phase === "completed" || snapshot.state?.phase === "failed") &&- !publishedStateByThread.has(threadId)+ !hasLivePublishedState(publishedStateByThread, threadId)) {if (Option.isNone(thread) || !terminalWorkSinceStart(thread.value, startedAt)) return;}- if (publishedStateByThread.get(threadId) === publishIdentity) {+ if (+ publishedStateByThread.get(threadId) === publishIdentity &&+ // A held completion settled after an earlier clear still has to alert.+ !(snapshot.state === null && heldCompletions.has(threadId))+ ) {// Clear moot deadlines so later transient nulls cannot skip their deferral window.publishConfirmDeadlines.delete(threadId);heldCompletions.delete(threadId);@@ -557,10 +572,7 @@ export const make = Effect.gen(function* () {// Confirm tombstones over live state to avoid removing active cards.// Confirm completion: a run can end moments before its continuation starts, and the relay// alerts on every move into completed.- const requiresConfirmation =- (snapshot.state === null &&- publishedStateByThread.get(threadId) !== agentAwarenessPublishIdentity(null)) ||- snapshot.state?.phase === "completed";+ const requiresConfirmation = snapshot.state === null || snapshot.state?.phase === "completed";if (requiresConfirmation) {if (snapshot.state?.phase === "completed") heldCompletions.add(threadId);const nowMs = (yield* DateTime.now).epochMilliseconds;@@ -640,15 +652,9 @@ export const make = Effect.gen(function* () {}).pipe(withRelayClientTracing);publishConfirmDeadlines.delete(threadId);heldCompletions.delete(threadId);- yield* Ref.update(publishedStateByThreadRef, (publishedStates) => {- const nextPublishedStates = new Map(publishedStates);- if (snapshot.state === null) {- nextPublishedStates.delete(threadId);- } else {- nextPublishedStates.set(threadId, publishIdentity);- }- return nextPublishedStates;- });+ yield* Ref.update(publishedStateByThreadRef, (publishedStates) =>+ new Map(publishedStates).set(threadId, publishIdentity),+ );});const processThreadPublish = Effect.fnUntraced(function* (threadId: ThreadId) {