From 8e95d6e936bc1a2ee83adbf48283d17b6ee68cee Mon Sep 17 00:00:00 2001 From: Kainoa Newton Date: Tue, 17 Nov 2026 10:04:16 -0800 Subject: [PATCH 1/2] fix(web): done alerts arrive with the thread instead of two seconds before it The sidebar kept a finished thread in Working for 2s while the completion toast, sound and desktop notification fired at once, so the alert named a thread that was not in the inbox yet. A run that continued inside those 2s still announced "Thread completed". Completion alerts now wait out the same Working exit grace as the sidebar, and the grace drops from 2s to 0.7s. A run that continues or asks for input inside the grace no longer announces as done. --- .../src/features/threads/threadListV2.test.ts | 9 +- ...ificationCoordinator.badge.browser.test.ts | 22 +++- ...hreadNotificationCoordinator.logic.test.ts | 114 ++++++++++++------ .../ThreadNotificationCoordinator.logic.ts | 34 +++--- .../src/state/threadInbox.test.ts | 25 ++-- .../client-runtime/src/state/threadInbox.ts | 4 +- 6 files changed, 140 insertions(+), 68 deletions(-) diff --git a/apps/mobile/src/features/threads/threadListV2.test.ts b/apps/mobile/src/features/threads/threadListV2.test.ts index 16ef19a32993f..4a7f2de74295b 100644 --- a/apps/mobile/src/features/threads/threadListV2.test.ts +++ b/apps/mobile/src/features/threads/threadListV2.test.ts @@ -1,5 +1,8 @@ import { threadSearchMatchKey } from "@t3tools/client-runtime/state/thread-search"; -import { createInboxReturnTracker } from "@t3tools/client-runtime/state/thread-inbox"; +import { + createInboxReturnTracker, + WORKING_EXIT_GRACE_MS, +} from "@t3tools/client-runtime/state/thread-inbox"; import { presentThreadShell } from "@t3tools/client-runtime/state/models"; import * as DateTime from "effect/DateTime"; import * as Schema from "effect/Schema"; @@ -1776,10 +1779,10 @@ describe("Working section beta", () => { tracker.observe([inactive]); expect(render().workingCount).toBe(1); expect(render().items[0]?.thread.runtime).toBeNull(); - vi.advanceTimersByTime(2_000); + vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS); expect(render().workingCount).toBe(0); expect(ids(render())).toEqual(["grace"]); - expect(tracker.returnedAt(inactive)).toBe(Date.parse(NOW) + 2_000); + expect(tracker.returnedAt(inactive)).toBe(Date.parse(NOW) + WORKING_EXIT_GRACE_MS); } finally { tracker.observe(null); vi.useRealTimers(); diff --git a/apps/web/src/components/ThreadNotificationCoordinator.badge.browser.test.ts b/apps/web/src/components/ThreadNotificationCoordinator.badge.browser.test.ts index 7da8504990fe6..c79d76925ea4f 100644 --- a/apps/web/src/components/ThreadNotificationCoordinator.badge.browser.test.ts +++ b/apps/web/src/components/ThreadNotificationCoordinator.badge.browser.test.ts @@ -3,6 +3,7 @@ import { EnvironmentId } from "@t3tools/contracts"; import * as DateTime from "effect/DateTime"; import * as Option from "effect/Option"; import { Atom, AtomRegistry } from "effect/reactivity"; +import { WORKING_EXIT_GRACE_MS } from "@t3tools/client-runtime/state/thread-inbox"; import { expect, it, vi } from "vite-plus/test"; import { fakeTimers, stubGlobal } from "../../test/render"; @@ -279,8 +280,9 @@ it.each(["hasPendingApprovals", "hasPendingUserInput"] as const)( }, ); -it("presents completion notifications and sound as soon as the shell completes", async () => { +it("presents completion notifications and sound once the Working shelf's exit grace passes", async () => { await using app = setup(); + using _timers = fakeTimers(); state.mode = "notifications-and-sound"; app.start(); state.shells.set( @@ -289,6 +291,8 @@ it("presents completion notifications and sound as soon as the shell completes", latestTurn: { turnId: "turn", state: "completed", completedAt: "2026-09-13T07:00:01.000Z" }, }), ); + expect(TestNotification.sent).toEqual([]); + vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS); expect(TestNotification.sent.map((sent) => sent.title)).toEqual(["Thread completed"]); expect(state.sound).toHaveBeenCalledWith("completion", expect.any(Function)); }); @@ -327,6 +331,7 @@ it.each([false, true])( "one", watching("turn", "completed", "2026-09-13T07:00:01.000Z", false, monitor), ); + vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS); expect(titles()).toEqual(["Thread completed"]); state.shells.set("one", watching("wake", "running", null, true, monitor)); @@ -351,6 +356,7 @@ it.each([false, true])( state.shells.set("one", watching("final", "running", null, false, [])); vi.advanceTimersByTime(RELEASED_COMPLETION_GRACE_MS); state.shells.set("one", watching("final", "completed", "2026-09-13T07:00:11.000Z", false, [])); + vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS); expect(titles()).toEqual(["Thread completed", "Thread completed"]); expect(state.sound).toHaveBeenCalledTimes(2); }, @@ -360,6 +366,7 @@ it.each(["hasPendingApprovals", "hasPendingUserInput"] as const)( "notifies for offline completion and %s once across reconnects", async (flag) => { await using app = setup(); + using _timers = fakeTimers(); const asking = { id: "asking", hasPendingApprovals: true }; const offline = { id: "offline", [flag]: true }; const done = { @@ -387,18 +394,20 @@ it.each(["hasPendingApprovals", "hasPendingUserInput"] as const)( status: "synchronizing", }); state.shells.set("one", shell(completed, asking, offline, done)); + vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS); expect(TestNotification.sent.map(({ title }) => title)).toEqual([ "Approval needed", - "Thread completed", flag === "hasPendingApprovals" ? "Approval needed" : "Input needed", + "Thread completed", ]); state.shells.set("one", { ...shell(completed, asking, offline, done), status: "cached" }); state.shells.set("one", shell(completed, asking, offline, done)); + vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS); expect(TestNotification.sent.map(({ title }) => title)).toEqual([ "Approval needed", - "Thread completed", flag === "hasPendingApprovals" ? "Approval needed" : "Input needed", + "Thread completed", ]); }, ); @@ -407,20 +416,24 @@ it.each([false, true])( "does not replay a notified completion after reconnect (continued: %s)", async (continued) => { await using app = setup(); + using _timers = fakeTimers(); app.start(); const completed = shell({ latestTurn: { turnId: "turn", state: "completed", completedAt: "2026-09-13T07:05:00.000Z" }, }); state.shells.set("one", completed); + vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS); state.shells.set("one", { ...completed, status: "cached" }); expect(TestNotification.sent.map(({ title }) => title)).toEqual(["Thread completed"]); state.shells.set("one", continued ? shell() : completed); + vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS); expect(TestNotification.sent.map(({ title }) => title)).toEqual(["Thread completed"]); }, ); it("opening a thread or removing its environment closes only its in-app toasts, and disabling notifications closes the rest", async () => { await using app = setup(); + using _timers = fakeTimers(); state.inApp = true; focused = true; state.toast @@ -434,12 +447,15 @@ it("opening a thread or removing its environment closes only its in-app toasts, latestTurn: { turnId: "turn", state: "completed", completedAt: "2026-09-13T07:01:00Z" }, }; state.shells.set("one", shell(completed, { id: "other" })); + vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS); state.shells.set("one", shell({ ...completed, hasPendingApprovals: true }, { id: "other" })); state.shells.set( "one", shell({ ...completed, hasPendingApprovals: true }, { ...completed, id: "other" }), ); + vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS); state.shells.set("two", shell(completed)); + vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS); expect(state.toast).toHaveBeenCalledTimes(4); app.openThread("one", "thread"); diff --git a/apps/web/src/components/ThreadNotificationCoordinator.logic.test.ts b/apps/web/src/components/ThreadNotificationCoordinator.logic.test.ts index 0efca647316ba..d0d2793cfa848 100644 --- a/apps/web/src/components/ThreadNotificationCoordinator.logic.test.ts +++ b/apps/web/src/components/ThreadNotificationCoordinator.logic.test.ts @@ -1,3 +1,4 @@ +import { WORKING_EXIT_GRACE_MS } from "@t3tools/client-runtime/state/thread-inbox"; import { describe, expect, it, vi } from "vite-plus/test"; import { @@ -28,6 +29,10 @@ const woke = (at: number): ThreadNotificationObservation => ({ }); const visible = { archived: false, quiet: false, snoozed: false }; const snoozed = { ...visible, snoozed: true }; +const graces = [ + ["an ordinary", working, completed, WORKING_EXIT_GRACE_MS], + ["a wake's", waking, woke, RELEASED_COMPLETION_GRACE_MS], +] as const; function fakeTimers(): Disposable { vi.useFakeTimers(); @@ -47,41 +52,59 @@ function harness() { describe("createThreadNotificationGate", () => { it("skips a completion seen while snoozed, but still notifies attention", () => { + using _timers = fakeTimers(); const { notify, observe } = harness(); observe(working); observe(completed(1), snoozed); observe({ ...completed(1) }); + vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS); expect(notify).not.toHaveBeenCalled(); observe({ ...working, attention: "run-2:approval" }, snoozed); expect(notify.mock.calls).toEqual([["thread", "input"]]); }); - it("notifies a completion immediately and only once", () => { + it("notifies a completion once, after the Working shelf's exit grace", () => { + using _timers = fakeTimers(); const { notify, observe } = harness(); observe(working); observe(completed(1)); + vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS - 1); + observe({ ...completed(1) }); + expect(notify).not.toHaveBeenCalled(); + vi.advanceTimersByTime(1); expect(notify.mock.calls).toEqual([["thread", "completion"]]); observe({ ...completed(1) }); observe(working); observe(completed(1)); + vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS); expect(notify).toHaveBeenCalledTimes(1); }); - it("notifies each newer completion immediately", () => { + it("drops a completion whose run continues inside the grace, and notifies each newer one", () => { + using _timers = fakeTimers(); const { notify, observe } = harness(); observe(working); observe(completed(1)); + vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS - 1); observe(working); + vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS); + expect(notify).not.toHaveBeenCalled(); observe(completed(2)); + vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS); + observe(working); + observe(completed(3)); + vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS); expect(notify.mock.calls).toEqual([ ["thread", "completion"], ["thread", "completion"], ]); observe(completed(1)); + vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS); expect(notify).toHaveBeenCalledTimes(2); }); it("notifies each attention once per stretch and still notifies the subsequent completion", () => { + using _timers = fakeTimers(); const { notify, observe } = harness(); observe(working); const attention = { ...working, attention: "run-1:approval:a", requestId: "a" }; @@ -102,6 +125,7 @@ describe("createThreadNotificationGate", () => { expect(notify).toHaveBeenCalledTimes(5); observe(working); observe(completed(1)); + vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS); expect(notify.mock.calls).toEqual([ ["thread", "input"], ["thread", "input"], @@ -112,19 +136,30 @@ describe("createThreadNotificationGate", () => { ]); }); - it("does not replay a completion after attention resolves", () => { + it.each([ + [ + "after", + WORKING_EXIT_GRACE_MS, + [ + ["thread", "completion"], + ["thread", "input"], + ], + ], + ["inside", WORKING_EXIT_GRACE_MS - 1, [["thread", "input"]]], + ])("does not replay a completion after attention %s the grace resolves", (_when, wait, calls) => { + using _timers = fakeTimers(); const { notify, observe } = harness(); observe(working); observe(completed(1)); + vi.advanceTimersByTime(wait); observe({ ...working, attention: "run-1:limited" }); observe(completed(1)); - expect(notify.mock.calls).toEqual([ - ["thread", "completion"], - ["thread", "input"], - ]); + vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS); + expect(notify.mock.calls).toEqual(calls); }); it("never notifies existing, archived or stopped completions, including when unarchived", () => { + using _timers = fakeTimers(); const { notify, observe } = harness(); observe(completed(1)); observe(working); @@ -133,13 +168,16 @@ describe("createThreadNotificationGate", () => { observe(working); observe(completed(3), { ...visible, archived: true }); observe(completed(3)); + vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS); expect(notify).not.toHaveBeenCalled(); observe(working); observe(completed(4)); + vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS); expect(notify.mock.calls).toEqual([["thread", "completion"]]); }); it("forgets handled completions when a thread is removed or the gate is cleared", () => { + using _timers = fakeTimers(); for (const reset of [ (gate: ReturnType>) => gate.retain(new Set()), (gate: ReturnType>) => gate.clear(), @@ -149,6 +187,7 @@ describe("createThreadNotificationGate", () => { reset(gate); gate.observe("thread", undefined, working, visible); gate.observe("thread", working, completed(1), visible); + vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS); expect(notify.mock.calls).toEqual([["thread", "completion"]]); } }); @@ -158,6 +197,7 @@ describe("createThreadNotificationGate", () => { const { notify, observe } = harness(); observe(working); observe(completed(1)); + vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS); // Held until the monitor left, or completing after it already had: either way unheld here. observe(waking); observe(woke(2)); @@ -173,34 +213,40 @@ describe("createThreadNotificationGate", () => { ]); }); - it("notifies a wake's completion once after the grace when no run follows, even across a pause", () => { - using _timers = fakeTimers(); - const { gate, notify, observe } = harness(); - observe(waking); - observe(woke(1)); - gate.pause(); - vi.advanceTimersByTime(RELEASED_COMPLETION_GRACE_MS); - expect(notify).not.toHaveBeenCalled(); - observe({ ...woke(1) }); - vi.advanceTimersByTime(RELEASED_COMPLETION_GRACE_MS - 1); - observe({ ...woke(1) }); - expect(notify).not.toHaveBeenCalled(); - vi.advanceTimersByTime(1); - expect(notify.mock.calls).toEqual([["thread", "completion"]]); - observe({ ...woke(1) }); - vi.advanceTimersByTime(RELEASED_COMPLETION_GRACE_MS); - expect(notify).toHaveBeenCalledTimes(1); - }); + it.each(graces)( + "notifies %s completion once after its own grace when no run follows, even across a pause", + (_kind, running, done, graceMs) => { + using _timers = fakeTimers(); + const { gate, notify, observe } = harness(); + observe(running); + observe(done(1)); + gate.pause(); + vi.advanceTimersByTime(RELEASED_COMPLETION_GRACE_MS); + expect(notify).not.toHaveBeenCalled(); + observe({ ...done(1) }); + vi.advanceTimersByTime(graceMs - 1); + observe({ ...done(1) }); + expect(notify).not.toHaveBeenCalled(); + vi.advanceTimersByTime(1); + expect(notify.mock.calls).toEqual([["thread", "completion"]]); + observe({ ...done(1) }); + vi.advanceTimersByTime(RELEASED_COMPLETION_GRACE_MS); + expect(notify).toHaveBeenCalledTimes(1); + }, + ); - it("drops a pending wake completion when the thread is snoozed", () => { - using _timers = fakeTimers(); - const { notify, observe } = harness(); - observe(waking); - observe(woke(1)); - observe({ ...woke(1) }, snoozed); - vi.advanceTimersByTime(RELEASED_COMPLETION_GRACE_MS); - expect(notify).not.toHaveBeenCalled(); - }); + it.each(graces)( + "drops %s pending completion when the thread is snoozed", + (_kind, running, done) => { + using _timers = fakeTimers(); + const { notify, observe } = harness(); + observe(running); + observe(done(1)); + observe({ ...done(1) }, snoozed); + vi.advanceTimersByTime(RELEASED_COMPLETION_GRACE_MS); + expect(notify).not.toHaveBeenCalled(); + }, + ); it("drops a pending wake completion when the thread is removed or the gate is cleared", () => { using _timers = fakeTimers(); diff --git a/apps/web/src/components/ThreadNotificationCoordinator.logic.ts b/apps/web/src/components/ThreadNotificationCoordinator.logic.ts index 057d976a17837..be2a8fa33c3d1 100644 --- a/apps/web/src/components/ThreadNotificationCoordinator.logic.ts +++ b/apps/web/src/components/ThreadNotificationCoordinator.logic.ts @@ -1,3 +1,4 @@ +import { WORKING_EXIT_GRACE_MS } from "@t3tools/client-runtime/state/thread-inbox"; import { effectiveSnoozed, type ThreadSnoozeShell, @@ -41,18 +42,22 @@ export function createThreadNotificationGate( ) { // The newest completion already notified, or deliberately skipped. const handled = new Map(); - // Wake completions waiting out the grace, by completion time; no timer while paused. + // Completions waiting out their grace, by completion time; no timer while paused. const releases = new Map< Key, - { readonly at: number; readonly timer: ReturnType | undefined } + { + readonly at: number; + readonly graceMs: number; + readonly timer: ReturnType | undefined; + } >(); - const release = (key: Key, at: number) => { + const release = (key: Key, at: number, graceMs: number) => { const timer = setTimeout(() => { releases.delete(key); handled.set(key, at); notify(key, "completion"); - }, RELEASED_COMPLETION_GRACE_MS); - releases.set(key, { at, timer }); + }, graceMs); + releases.set(key, { at, graceMs, timer }); }; const skip = (key: Key, at: number | null) => { if (at !== null && at > (handled.get(key) ?? -Infinity)) handled.set(key, at); @@ -86,16 +91,18 @@ export function createThreadNotificationGate( next: ThreadNotificationObservation, options: { readonly archived: boolean; readonly quiet: boolean; readonly snoozed: boolean }, ): void { - // The grace survives updates that keep the wake's completion; anything else cancels it. + // The grace survives updates that keep the same completion; anything else cancels it. const pending = releases.get(key); const silenced = options.archived || options.quiet || options.snoozed; if (pending?.at === next.completedAt && !silenced) { - if (pending.timer === undefined) release(key, pending.at); + if (pending.timer === undefined) release(key, pending.at, pending.graceMs); return; } cancel(key); // The first sighting, archived or snoozed threads, and stopped or silent runs never notify completion. if (!prior || silenced) skip(key, next.completedAt); + // A request that arrives inside the grace is the news; its run does not announce later. + if (pending && next.attention !== null) skip(key, pending.at); const fresh = attend(key, prior, next); if (!prior || options.archived) return; if (fresh) { @@ -104,18 +111,15 @@ export function createThreadNotificationGate( } const at = next.completedAt; if (!next.settled || at === null || at <= (handled.get(key) ?? -Infinity)) return; - if (next.backgroundWake) { - release(key, at); - return; - } - handled.set(key, at); - notify(key, "completion"); + // Completion waits out the Working shelf's exit grace so both land together, and a + // run that continues or asks inside it never announces. + release(key, at, next.backgroundWake ? RELEASED_COMPLETION_GRACE_MS : WORKING_EXIT_GRACE_MS); }, /** Stops grace timers while the snapshot is stale; the next observation restarts or drops them. */ pause(): void { - for (const [key, { at, timer }] of releases) { + for (const [key, { at, graceMs, timer }] of releases) { clearTimeout(timer); - releases.set(key, { at, timer: undefined }); + releases.set(key, { at, graceMs, timer: undefined }); } }, /** Forgets threads that left the snapshot. */ diff --git a/packages/client-runtime/src/state/threadInbox.test.ts b/packages/client-runtime/src/state/threadInbox.test.ts index 4e4a39d2c8a2d..7f2d605192c14 100644 --- a/packages/client-runtime/src/state/threadInbox.test.ts +++ b/packages/client-runtime/src/state/threadInbox.test.ts @@ -6,6 +6,7 @@ import { isThreadWorking, sortInboxThreadsByReturn, sortWorkingThreadsBySend, + WORKING_EXIT_GRACE_MS, } from "./threadInbox.ts"; const environmentId = EnvironmentId.make("environment-1"); @@ -50,7 +51,7 @@ describe("createInboxReturnTracker", () => { expect(first.returnedAt(thread("b", false))).toBeUndefined(); const held = tracker.observe([thread("a", false), thread("b", false)]); - vi.advanceTimersByTime(2_000); + vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS); const second = tracker.observe([thread("a", false), thread("b", false)]); expect(second.returnedAt(thread("a", false))).toBeDefined(); expect(second.returnedAt(thread("b", false))).toBeUndefined(); @@ -68,13 +69,13 @@ describe("createInboxReturnTracker", () => { tracker.observe([thread("a", false)]); expect(isThreadWorking(thread("a", false))).toBe(false); expect(tracker.isWorking(thread("a", false))).toBe(true); - vi.advanceTimersByTime(1_999); + vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS - 1); tracker.observe([thread("a", false)]); expect(tracker.isWorking(thread("a", false))).toBe(true); expect(tracker.returnedAt(thread("a", false))).toBeUndefined(); vi.advanceTimersByTime(1); expect(tracker.isWorking(thread("a", false))).toBe(false); - expect(tracker.returnedAt(thread("a", false))).toBe(12_000); + expect(tracker.returnedAt(thread("a", false))).toBe(10_000 + WORKING_EXIT_GRACE_MS); expect(notify).toHaveBeenCalledTimes(1); unsubscribe(); }); @@ -106,15 +107,17 @@ describe("createInboxReturnTracker", () => { tracker.observe([thread("a", true)]); expect(tracker.isWorking(thread("a", true))).toBe(true); tracker.observe([thread("a", false)]); - vi.advanceTimersByTime(1_500); + vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS - 1); tracker.observe([thread("a", true)]); vi.advanceTimersByTime(1_000); expect(tracker.returnedAt(thread("a", true))).toBeUndefined(); tracker.observe([thread("a", false)]); - vi.advanceTimersByTime(1_999); + vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS - 1); expect(tracker.isWorking(thread("a", false))).toBe(true); vi.advanceTimersByTime(1); - expect(tracker.returnedAt(thread("a", false))).toBe(14_500); + expect(tracker.returnedAt(thread("a", false))).toBe( + 10_000 + WORKING_EXIT_GRACE_MS - 1 + 1_000 + WORKING_EXIT_GRACE_MS, + ); }); it.each([ @@ -182,11 +185,11 @@ describe("createInboxReturnTracker", () => { tracker.observe([finished]); expect(tracker.isWorking(finished)).toBe(true); expect(tracker.returnedAt(finished)).toBeUndefined(); - vi.advanceTimersByTime(1_999); + vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS - 1); expect(tracker.isWorking(finished)).toBe(true); vi.advanceTimersByTime(1); expect(tracker.isWorking(finished)).toBe(false); - expect(tracker.returnedAt(finished)).toBe(12_000); + expect(tracker.returnedAt(finished)).toBe(10_000 + WORKING_EXIT_GRACE_MS); }); it("does not grant grace to a future snooze after background completion", () => { @@ -210,8 +213,8 @@ describe("createInboxReturnTracker", () => { tracker.observe([a, thread("b", true)]); vi.advanceTimersByTime(500); tracker.observe([a, b]); - vi.advanceTimersByTime(1_500); - expect(tracker.returnedAt(a)).toBe(12_000); + vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS - 500); + expect(tracker.returnedAt(a)).toBe(10_000 + WORKING_EXIT_GRACE_MS); expect(tracker.returnedAt(b)).toBeUndefined(); vi.advanceTimersByTime(500); expect(sortInboxThreadsByReturn([a, b], tracker.returnedAt).map((entry) => entry.id)).toEqual([ @@ -231,7 +234,7 @@ describe("createInboxReturnTracker", () => { tracker.observe([thread("a", true)]); tracker.observe([thread("a", false)]); tracker.observe(null); - vi.advanceTimersByTime(2_000); + vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS); expect(notify).not.toHaveBeenCalled(); expect(tracker.returnedAt(thread("a", false))).toBeUndefined(); }); diff --git a/packages/client-runtime/src/state/threadInbox.ts b/packages/client-runtime/src/state/threadInbox.ts index ac820acc9d1ea..0bbe7dd5f17b6 100644 --- a/packages/client-runtime/src/state/threadInbox.ts +++ b/packages/client-runtime/src/state/threadInbox.ts @@ -129,9 +129,9 @@ type ThreadKeyInput = Pick; /** * How long a thread that stopped working keeps reading as working. An orchestrator * run can end moments before its continuation starts, so clients wait this long - * before treating the thread as done. + * before treating the thread as done. Completion alerts wait the same time. */ -const WORKING_EXIT_GRACE_MS = 2_000; +export const WORKING_EXIT_GRACE_MS = 700; /** * A run that ended this long ago has no continuation about to start, so its thread gets no From 60f2fa2e79a7c9d2d008edf42a78c828af5663ca Mon Sep 17 00:00:00 2001 From: Kainoa Newton Date: Tue, 17 Nov 2026 14:52:59 -0800 Subject: [PATCH 2/2] test(web): keep the completion grace check at the gate unit layer --- .../ThreadNotificationCoordinator.badge.browser.test.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/apps/web/src/components/ThreadNotificationCoordinator.badge.browser.test.ts b/apps/web/src/components/ThreadNotificationCoordinator.badge.browser.test.ts index c79d76925ea4f..5014eabd5336d 100644 --- a/apps/web/src/components/ThreadNotificationCoordinator.badge.browser.test.ts +++ b/apps/web/src/components/ThreadNotificationCoordinator.badge.browser.test.ts @@ -291,7 +291,6 @@ it("presents completion notifications and sound once the Working shelf's exit gr latestTurn: { turnId: "turn", state: "completed", completedAt: "2026-09-13T07:00:01.000Z" }, }), ); - expect(TestNotification.sent).toEqual([]); vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS); expect(TestNotification.sent.map((sent) => sent.title)).toEqual(["Thread completed"]); expect(state.sound).toHaveBeenCalledWith("completion", expect.any(Function));