Skip to content
TypeScript

fix(server): settling a finished thread removes it from the Live Activity (#18604)

alcpereira404c6e712e3f
diff --git a/apps/server/src/relay/AgentAwarenessRelay.test.ts b/apps/server/src/relay/AgentAwarenessRelay.test.ts
index 8811bd3d6d8..3ae05f309ad 100644
--- a/apps/server/src/relay/AgentAwarenessRelay.test.ts
+++ b/apps/server/src/relay/AgentAwarenessRelay.test.ts
@@ -11,6 +11,7 @@ import {
ProjectId,
ProviderInstanceId,
RunId,
+ RuntimeRequestId,
ThreadId,
TurnItemId,
} from "@t3tools/contracts";
@@ -771,6 +772,116 @@ describe("AgentAwarenessRelay", () => {
}),
);
+ it.effect.each(["completed", "failed"] as const)(
+ "settling a %s thread publishes a tombstone without re-alerting on unsettle",
+ (status) =>
+ Effect.gen(function* () {
+ const events = yield* Queue.unbounded<OrchestrationV2DomainEvent>();
+ const pulls = yield* Queue.unbounded<void>();
+ const { relay, currentShell, publications } = yield* makeTestRelay({
+ domainEvents: Stream.fromEffectRepeat(
+ Queue.offer(pulls, undefined).pipe(Effect.andThen(Queue.take(events))),
+ ),
+ });
+ yield* relay.start();
+ yield* Queue.take(pulls);
+ const finished = shell({
+ status,
+ latestRunCompletedAt: DateTime.add(yield* DateTime.now, { seconds: 1 }),
+ });
+ yield* Ref.set(currentShell, finished);
+ yield* relay.publishThread(THREAD_ID);
+ yield* TestClock.adjust("5 seconds");
+ yield* relay.drain;
+ assert.deepEqual(
+ publications.map(({ state }) => state?.phase),
+ [status],
+ );
+
+ for (const type of ["thread.settled", "thread.unsettled"] as const) {
+ const updated = {
+ ...finished,
+ settledOverride: type === "thread.settled" ? ("settled" as const) : ("active" as const),
+ updatedAt: yield* DateTime.now,
+ };
+ yield* Ref.set(currentShell, updated);
+ yield* Queue.offer(events, {
+ id: EventId.make(type),
+ type,
+ threadId: THREAD_ID,
+ occurredAt: updated.updatedAt,
+ payload: { ...updated, lastVisitedAt: updated.lastVisitedAt ?? null },
+ });
+ yield* Queue.take(pulls);
+ yield* relay.drain;
+ yield* TestClock.adjust("5 seconds");
+ yield* relay.drain;
+ assert.deepEqual(
+ publications.map(({ state }) => state?.phase ?? null),
+ [status, null],
+ );
+ }
+ }).pipe(Effect.scoped),
+ );
+
+ it.effect.each([
+ { work: "running", status: "running", phase: "running" },
+ { work: "user input", status: "completed", phase: "waiting_for_input" },
+ { work: "monitor", status: "completed", phase: "running" },
+ { work: "monitor", status: "failed", phase: "failed" },
+ ] as const)(
+ "keeps settled $work visible until it finishes ($status run)",
+ ({ work, status, phase }) =>
+ Effect.gen(function* () {
+ const { relay, currentShell, publications } = yield* makeTestRelay();
+ const active = shell({
+ status,
+ latestRunCompletedAt: DateTime.add(yield* DateTime.now, { seconds: 1 }),
+ pendingRuntimeRequest:
+ work === "user input"
+ ? {
+ id: RuntimeRequestId.make("waiting"),
+ kind: "user_input",
+ createdAt: yield* DateTime.now,
+ }
+ : null,
+ pendingBackgroundTasks: work === "monitor" ? [{ taskId: "watch", kind: "monitor" }] : [],
+ });
+ yield* Ref.set(currentShell, active);
+ yield* relay.publishThread(THREAD_ID);
+ yield* TestClock.adjust("5 seconds");
+ yield* relay.drain;
+ assert.deepEqual(
+ publications.map(({ state }) => state?.phase),
+ [phase],
+ );
+
+ yield* Ref.set(currentShell, { ...active, settledOverride: "settled" });
+ yield* relay.publishThread(THREAD_ID);
+ yield* TestClock.adjust("5 seconds");
+ yield* relay.drain;
+ assert.deepEqual(
+ publications.map(({ state }) => state?.phase),
+ [phase],
+ );
+
+ yield* Ref.set(currentShell, {
+ ...active,
+ settledOverride: "settled",
+ status: status === "running" ? "completed" : status,
+ pendingRuntimeRequest: null,
+ pendingBackgroundTasks: [],
+ });
+ yield* relay.publishThread(THREAD_ID);
+ yield* TestClock.adjust("5 seconds");
+ yield* relay.drain;
+ assert.deepEqual(
+ publications.map(({ state }) => state?.phase ?? null),
+ [phase, null],
+ );
+ }),
+ );
+
it.effect("confirms a first completed state and respects disabling during confirmation", () =>
Effect.gen(function* () {
const { relay, secrets, currentShell, publications } = yield* makeTestRelay();
diff --git a/apps/server/src/relay/AgentAwarenessRelay.ts b/apps/server/src/relay/AgentAwarenessRelay.ts
index 87ce4afb08c..6fdad0ced38 100644
--- a/apps/server/src/relay/AgentAwarenessRelay.ts
+++ b/apps/server/src/relay/AgentAwarenessRelay.ts
@@ -93,6 +93,7 @@ export function shouldPublishAgentAwarenessEvent(
case "thread.metadata-updated":
case "thread.pull-request-link-updated":
case "thread.pull-request-synced":
+ case "thread.settled":
case "thread.model-selection-updated":
case "thread.provider-switched":
case "run.created":
@@ -101,7 +102,7 @@ export function shouldPublishAgentAwarenessEvent(
case "subagent.updated":
case "provider-thread.updated":
return true;
- case "thread.settled":
+ // Unsettling must not re-alert a dismissed result.
case "thread.unsettled":
case "thread.snoozed":
case "thread.unsnoozed":
diff --git a/packages/shared/src/agentAwareness.test.ts b/packages/shared/src/agentAwareness.test.ts
index 85b8d614150..e325fa263e9 100644
--- a/packages/shared/src/agentAwareness.test.ts
+++ b/packages/shared/src/agentAwareness.test.ts
@@ -28,6 +28,7 @@ describe("projectThreadAwarenessV2", () => {
| "pendingBackgroundTasks"
| "pendingRuntimeRequest"
| "lineage"
+ | "settledOverride"
>
> = {},
) => ({
@@ -41,6 +42,7 @@ describe("projectThreadAwarenessV2", () => {
modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.4" },
status: "running" as const,
pendingRuntimeRequest: null,
+ settledOverride: null,
updatedAt,
...overrides,
});
diff --git a/packages/shared/src/agentAwareness.ts b/packages/shared/src/agentAwareness.ts
index b8f6a194a9d..1c22deb9ff5 100644
--- a/packages/shared/src/agentAwareness.ts
+++ b/packages/shared/src/agentAwareness.ts
@@ -48,6 +48,7 @@ export interface ProjectThreadAwarenessV2Input {
| "modelSelection"
| "pendingBackgroundTasks"
| "pendingRuntimeRequest"
+ | "settledOverride"
| "status"
| "title"
| "updatedAt"
@@ -63,6 +64,14 @@ export function projectThreadAwarenessV2(
if (phase === null) {
return null;
}
+ // Resolve the phase first so live or waiting work stays visible after settlement.
+ if (
+ thread.settledOverride === "settled" &&
+ (phase === "completed" || phase === "failed") &&
+ !backgroundWorkHoldsCompletion(thread.pendingBackgroundTasks ?? [])
+ ) {
+ return null;
+ }
const detail =
phase === "completed"
? "Review the completed task."