From 2d5b5d8c973075a466839516a0be0333af195fb6 Mon Sep 17 00:00:00 2001 From: Mike Olson Date: Wed, 11 Nov 2026 19:16:07 -0500 Subject: [PATCH] fix(web): A thread's completion alert waits to confirm no wake run follows held work Background work that holds a thread in Waiting ends just before the run that wakes the agent is created, so the thread read as completed for a few tens of milliseconds and the web app played the completion sound for a thread that was about to resume. A completion that follows held work now waits one second and is dropped when a wake run takes over. Other completions keep their existing timing. --- ...hreadNotificationCoordinator.logic.test.ts | 88 +++++++++++-------- .../ThreadNotificationCoordinator.logic.ts | 32 ++++--- .../ThreadNotificationCoordinator.tsx | 8 +- 3 files changed, 80 insertions(+), 48 deletions(-) diff --git a/apps/web/src/components/ThreadNotificationCoordinator.logic.test.ts b/apps/web/src/components/ThreadNotificationCoordinator.logic.test.ts index 1b09031de1ee7..227a3e050b23a 100644 --- a/apps/web/src/components/ThreadNotificationCoordinator.logic.test.ts +++ b/apps/web/src/components/ThreadNotificationCoordinator.logic.test.ts @@ -188,44 +188,58 @@ describe("createThreadNotificationGate", () => { } }); - it("waits out the grace before notifying a wake's completion, and a final wake replaces it", () => { - using _timers = fakeTimers(); - const { notify, observe } = harness(); - observe(working); - observe(completed(1)); - // Held until the monitor left, or completing after it already had: either way unheld here. - observe(waking); - observe(woke(2)); - expect(notify).toHaveBeenCalledTimes(1); - observe(working); - vi.advanceTimersByTime(RELEASED_COMPLETION_GRACE_MS); - expect(notify).toHaveBeenCalledTimes(1); - observe(completed(3)); - vi.advanceTimersByTime(RELEASED_COMPLETION_GRACE_MS); - expect(notify.mock.calls).toEqual([ - ["thread", "completion"], - ["thread", "completion"], - ]); - }); + it.each([false, true])( + "confirms a completion and drops it when a wake follows (held=%s)", + (held) => { + using _timers = fakeTimers(); + const { notify, observe } = harness(); + observe(working); + observe(completed(1)); + // Held until the monitor left, or completing after it already had: either way unheld here. + observe(held ? { ...working, held: true } : waking); + observe(held ? completed(2) : woke(2)); + expect(notify).toHaveBeenCalledTimes(1); + observe(working); + vi.advanceTimersByTime(RELEASED_COMPLETION_GRACE_MS); + expect(notify).toHaveBeenCalledTimes(1); + observe(completed(3)); + vi.advanceTimersByTime(RELEASED_COMPLETION_GRACE_MS); + expect(notify.mock.calls).toEqual([ + ["thread", "completion"], + ["thread", "completion"], + ]); + }, + ); - 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([false, true])( + "confirms a completion once across a pause and attention (held=%s)", + (held) => { + using _timers = fakeTimers(); + const { gate, notify, observe } = harness(); + const waiting = held ? { ...working, held: true } : waking; + const finished = held ? completed(1) : woke(1); + const grace = held ? 1_000 : RELEASED_COMPLETION_GRACE_MS; + observe(waiting); + if (held) { + observe({ ...waiting, attention: "run-1:approval" }); + expect(notify.mock.calls).toEqual([["thread", "input"]]); + notify.mockClear(); + } + observe(finished); + gate.pause(); + vi.advanceTimersByTime(grace); + expect(notify).not.toHaveBeenCalled(); + observe({ ...finished }); + vi.advanceTimersByTime(grace - 1); + observe({ ...finished }); + expect(notify).not.toHaveBeenCalled(); + vi.advanceTimersByTime(1); + expect(notify.mock.calls).toEqual([["thread", "completion"]]); + observe({ ...finished }); + vi.advanceTimersByTime(grace); + expect(notify).toHaveBeenCalledTimes(1); + }, + ); it("drops a pending wake completion when the thread is snoozed", () => { using _timers = fakeTimers(); diff --git a/apps/web/src/components/ThreadNotificationCoordinator.logic.ts b/apps/web/src/components/ThreadNotificationCoordinator.logic.ts index 35614ca303114..cbd6b6474942c 100644 --- a/apps/web/src/components/ThreadNotificationCoordinator.logic.ts +++ b/apps/web/src/components/ThreadNotificationCoordinator.logic.ts @@ -18,6 +18,8 @@ export interface ThreadNotificationObservation { readonly completedAt: number | null; /** The latest run answers a background-work progress wake, like a Monitor line. */ readonly backgroundWake: boolean; + /** The completed run is still waiting on background work. */ + readonly held?: boolean; } /** Matches the push relay's completion confirmation: a final wake can start right after a progress wake. */ @@ -55,18 +57,24 @@ export function createThreadNotificationGate( ) { // The newest completion already notified, or deliberately skipped. const handled = new Map(); + const wasHeld = new Set(); // Wake completions waiting out the grace, by completion time; no timer while paused. const releases = new Map< Key, - { readonly at: number; readonly timer: ReturnType | undefined } + { + readonly at: number; + readonly grace: number; + readonly timer: ReturnType | undefined; + } >(); - const release = (key: Key, at: number) => { + const release = (key: Key, at: number, grace = RELEASED_COMPLETION_GRACE_MS) => { const timer = setTimeout(() => { releases.delete(key); + wasHeld.delete(key); handled.set(key, at); notify(key, "completion"); - }, RELEASED_COMPLETION_GRACE_MS); - releases.set(key, { at, timer }); + }, grace); + releases.set(key, { at, grace, timer }); }; const skip = (key: Key, at: number | null) => { if (at !== null && at > (handled.get(key) ?? -Infinity)) handled.set(key, at); @@ -105,11 +113,13 @@ export function createThreadNotificationGate( readonly snoozed: boolean; }, ): void { + if (next.held === true) wasHeld.add(key); + else if (!next.settled && next.attention === null) wasHeld.delete(key); // The grace survives updates that keep the wake's 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?.at === next.completedAt && next.settled && !silenced) { + if (pending.timer === undefined) release(key, pending.at, pending.grace); return; } cancel(key); @@ -123,8 +133,8 @@ export function createThreadNotificationGate( } const at = next.completedAt; if (!next.settled || at === null || at <= (handled.get(key) ?? -Infinity)) return; - if (next.backgroundWake) { - release(key, at); + if (next.backgroundWake || wasHeld.has(key)) { + release(key, at, next.backgroundWake ? RELEASED_COMPLETION_GRACE_MS : 1_000); return; } handled.set(key, at); @@ -132,20 +142,22 @@ export function createThreadNotificationGate( }, /** 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, grace, timer }] of releases) { clearTimeout(timer); - releases.set(key, { at, timer: undefined }); + releases.set(key, { at, grace, timer: undefined }); } }, /** Forgets threads that left the snapshot. */ retain(keys: ReadonlySet): void { for (const key of handled.keys()) if (!keys.has(key)) handled.delete(key); + for (const key of wasHeld) if (!keys.has(key)) wasHeld.delete(key); for (const key of releases.keys()) if (!keys.has(key)) cancel(key); for (const key of attended.keys()) if (!keys.has(key)) attended.delete(key); }, clear(): void { for (const key of releases.keys()) cancel(key); handled.clear(); + wasHeld.clear(); attended.clear(); }, }; diff --git a/apps/web/src/components/ThreadNotificationCoordinator.tsx b/apps/web/src/components/ThreadNotificationCoordinator.tsx index e97b5c9e8a77e..e1746b89549e2 100644 --- a/apps/web/src/components/ThreadNotificationCoordinator.tsx +++ b/apps/web/src/components/ThreadNotificationCoordinator.tsx @@ -12,7 +12,10 @@ import { type OrchestrationV2ThreadShell, type ThreadId, } from "@t3tools/contracts"; -import { monitorHoldsWakeCompletion } from "@t3tools/shared/orchestrationV2PendingBackgroundWork"; +import { + backgroundWorkHoldsCompletion, + monitorHoldsWakeCompletion, +} from "@t3tools/shared/orchestrationV2PendingBackgroundWork"; import type { ClientSettings } from "@t3tools/contracts/settings"; import * as Option from "effect/Option"; import type { AtomRegistry } from "effect/reactivity"; @@ -300,6 +303,9 @@ export function startThreadNotifications( settled, completedAt: settled ? completedAt : null, backgroundWake: rawThread.latestRunBackgroundWake === true, + held: + thread.latestRun?.status === "completed" && + backgroundWorkHoldsCompletion(thread.pendingBackgroundTasks), }; next.set(thread.id, state); changed.push([prior, state]);