Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 51 additions & 37 deletions apps/web/src/components/ThreadNotificationCoordinator.logic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
32 changes: 22 additions & 10 deletions apps/web/src/components/ThreadNotificationCoordinator.logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -55,18 +57,24 @@ export function createThreadNotificationGate<Key>(
) {
// The newest completion already notified, or deliberately skipped.
const handled = new Map<Key, number>();
const wasHeld = new Set<Key>();
// Wake completions waiting out the grace, by completion time; no timer while paused.
const releases = new Map<
Key,
{ readonly at: number; readonly timer: ReturnType<typeof setTimeout> | undefined }
{
readonly at: number;
readonly grace: number;
readonly timer: ReturnType<typeof setTimeout> | 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);
Expand Down Expand Up @@ -105,11 +113,13 @@ export function createThreadNotificationGate<Key>(
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);
Expand All @@ -123,29 +133,31 @@ export function createThreadNotificationGate<Key>(
}
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);
notify(key, "completion");
},
/** 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<Key>): 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();
},
};
Expand Down
8 changes: 7 additions & 1 deletion apps/web/src/components/ThreadNotificationCoordinator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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]);
Expand Down