Skip to content

Commit 0f08b3b

Browse files
Merge remote-tracking branch 'origin/main' into fix/issue-29153
2 parents 5d1592e + ec3ebdb commit 0f08b3b

9 files changed

Lines changed: 140 additions & 205 deletions

‎apps/web/src/components/ThreadNotificationCoordinator.badge.browser.test.ts‎

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
import { WORKING_EXIT_GRACE_MS } from "@t3tools/client-runtime/state/thread-inbox";
21
import type { AnyRouter } from "@tanstack/react-router";
32
import { EnvironmentId } from "@t3tools/contracts";
43
import * as DateTime from "effect/DateTime";
54
import * as Option from "effect/Option";
65
import { Atom, AtomRegistry } from "effect/reactivity";
76
import { expect, it, vi } from "vite-plus/test";
87

9-
import { fakeTimers, stubGlobal } from "../../test/render";
8+
import { stubGlobal } from "../../test/render";
109

1110
const state = vi.hoisted(() => ({
1211
mode: "notifications",
@@ -241,24 +240,18 @@ it.each(["hasPendingApprovals", "hasPendingUserInput"] as const)(
241240
},
242241
);
243242

244-
it("holds a completion through a continuation gap and notifies once it settles", async () => {
243+
it("presents completion notifications and sound as soon as the shell completes", async () => {
245244
await using app = setup();
245+
state.mode = "notifications-and-sound";
246246
app.start();
247-
using _timers = fakeTimers();
248-
const finished = (turnId: string, completedAt: string) => ({
249-
latestTurn: { turnId, state: "completed", completedAt },
250-
});
251-
state.shells.set("one", shell(finished("turn-1", "2026-09-13T07:00:01.000Z")));
252247
state.shells.set(
253248
"one",
254-
shell({ latestTurn: { turnId: "turn-2", state: "running", completedAt: null } }),
249+
shell({
250+
latestTurn: { turnId: "turn", state: "completed", completedAt: "2026-09-13T07:00:01.000Z" },
251+
}),
255252
);
256-
state.shells.set("one", shell(finished("turn-2", "2026-09-13T07:00:02.000Z")));
257-
expect(TestNotification.sent).toEqual([]);
258-
await vi.advanceTimersByTimeAsync(WORKING_EXIT_GRACE_MS - 1);
259-
expect(TestNotification.sent).toEqual([]);
260-
await vi.advanceTimersByTimeAsync(1);
261253
expect(TestNotification.sent.map((sent) => sent.title)).toEqual(["Thread completed"]);
254+
expect(state.sound).toHaveBeenCalledWith("completion", expect.any(Function));
262255
});
263256

264257
it.each(["hasPendingApprovals", "hasPendingUserInput"] as const)(
@@ -277,7 +270,6 @@ it.each(["hasPendingApprovals", "hasPendingUserInput"] as const)(
277270
};
278271
state.shells.set("one", shell({ id: "working" }, { id: "asking" }, { id: "offline" }, done));
279272
app.start();
280-
using _timers = fakeTimers();
281273
state.shells.set("one", shell({ id: "working" }, asking, { id: "offline" }, done));
282274
state.shells.set("one", {
283275
...shell({ id: "working" }, asking, { id: "offline" }, done),
@@ -296,37 +288,31 @@ it.each(["hasPendingApprovals", "hasPendingUserInput"] as const)(
296288

297289
expect(TestNotification.sent.map(({ title }) => title)).toEqual([
298290
"Approval needed",
291+
"Thread completed",
299292
flag === "hasPendingApprovals" ? "Approval needed" : "Input needed",
300293
]);
301-
await vi.advanceTimersByTimeAsync(WORKING_EXIT_GRACE_MS);
302294
state.shells.set("one", { ...shell(completed, asking, offline, done), status: "cached" });
303295
state.shells.set("one", shell(completed, asking, offline, done));
304-
await vi.advanceTimersByTimeAsync(WORKING_EXIT_GRACE_MS);
305296
expect(TestNotification.sent.map(({ title }) => title)).toEqual([
306297
"Approval needed",
307-
flag === "hasPendingApprovals" ? "Approval needed" : "Input needed",
308298
"Thread completed",
299+
flag === "hasPendingApprovals" ? "Approval needed" : "Input needed",
309300
]);
310301
},
311302
);
312303

313304
it.each([false, true])(
314-
"rechecks a pending completion after reconnect (continued: %s)",
305+
"does not replay a notified completion after reconnect (continued: %s)",
315306
async (continued) => {
316307
await using app = setup();
317308
app.start();
318-
using _timers = fakeTimers();
319309
const completed = shell({
320310
latestTurn: { turnId: "turn", state: "completed", completedAt: "2026-09-13T07:05:00.000Z" },
321311
});
322312
state.shells.set("one", completed);
323313
state.shells.set("one", { ...completed, status: "cached" });
324-
await vi.advanceTimersByTimeAsync(WORKING_EXIT_GRACE_MS);
325-
expect(TestNotification.sent).toEqual([]);
314+
expect(TestNotification.sent.map(({ title }) => title)).toEqual(["Thread completed"]);
326315
state.shells.set("one", continued ? shell() : completed);
327-
await vi.advanceTimersByTimeAsync(WORKING_EXIT_GRACE_MS);
328-
expect(TestNotification.sent.map(({ title }) => title)).toEqual(
329-
continued ? [] : ["Thread completed"],
330-
);
316+
expect(TestNotification.sent.map(({ title }) => title)).toEqual(["Thread completed"]);
331317
},
332318
);
Lines changed: 37 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { WORKING_EXIT_GRACE_MS } from "@t3tools/client-runtime/state/thread-inbox";
2-
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
1+
import { describe, expect, it, vi } from "vite-plus/test";
32

43
import {
54
createThreadNotificationGate,
@@ -30,164 +29,85 @@ function harness() {
3029
}
3130

3231
describe("createThreadNotificationGate", () => {
33-
beforeEach(() => {
34-
vi.useFakeTimers();
35-
});
36-
afterEach(() => {
37-
vi.useRealTimers();
38-
});
39-
40-
it("drops a completion when the thread resumes within the grace", () => {
41-
const { notify, observe } = harness();
42-
observe(working);
43-
observe(completed(1));
44-
vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS - 1);
45-
observe(working);
46-
vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS * 2);
47-
expect(notify).not.toHaveBeenCalled();
48-
});
49-
50-
it("notifies a real completion once after the grace", () => {
32+
it("notifies a completion immediately and only once", () => {
5133
const { notify, observe } = harness();
5234
observe(working);
5335
observe(completed(1));
54-
vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS - 1);
55-
expect(notify).not.toHaveBeenCalled();
56-
vi.advanceTimersByTime(1);
5736
expect(notify.mock.calls).toEqual([["thread", "completion"]]);
5837
observe({ ...completed(1) });
59-
vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS * 2);
60-
expect(notify).toHaveBeenCalledTimes(1);
61-
});
62-
63-
it("notifies attention at once and replaces a pending completion", () => {
64-
const { notify, observe } = harness();
6538
observe(working);
6639
observe(completed(1));
67-
vi.advanceTimersByTime(500);
68-
observe({ attention: "run-1:approval", settled: false, completedAt: null });
69-
expect(notify.mock.calls).toEqual([["thread", "input"]]);
70-
vi.advanceTimersByTime(60_000);
71-
// The request resolves and the same run reads as completed again.
72-
observe(completed(1));
73-
vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS * 2);
74-
expect(notify.mock.calls).toEqual([["thread", "input"]]);
40+
expect(notify).toHaveBeenCalledTimes(1);
7541
});
7642

77-
it("notifies one completion across continuation gaps", () => {
43+
it("notifies each newer completion immediately", () => {
7844
const { notify, observe } = harness();
7945
observe(working);
8046
observe(completed(1));
81-
vi.advanceTimersByTime(300);
8247
observe(working);
83-
vi.advanceTimersByTime(5_000);
8448
observe(completed(2));
85-
vi.advanceTimersByTime(300);
86-
observe(working);
87-
vi.advanceTimersByTime(5_000);
88-
observe(completed(3));
89-
vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS);
90-
expect(notify.mock.calls).toEqual([["thread", "completion"]]);
49+
expect(notify.mock.calls).toEqual([
50+
["thread", "completion"],
51+
["thread", "completion"],
52+
]);
53+
observe(completed(1));
54+
expect(notify).toHaveBeenCalledTimes(2);
9155
});
9256

93-
it("keeps a completion interrupted by attention after a working gap from firing", () => {
57+
it("notifies attention once and still notifies the subsequent completion", () => {
9458
const { notify, observe } = harness();
9559
observe(working);
96-
observe(completed(1));
97-
vi.advanceTimersByTime(500);
60+
const attention = { attention: "run-1:approval", settled: false, completedAt: null };
61+
observe(attention);
62+
expect(notify.mock.calls).toEqual([["thread", "input"]]);
63+
observe({ ...attention });
64+
expect(notify).toHaveBeenCalledTimes(1);
9865
observe(working);
99-
observe({ attention: "run-1:approval", settled: false, completedAt: null });
100-
vi.advanceTimersByTime(60_000);
10166
observe(completed(1));
102-
vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS * 2);
103-
expect(notify.mock.calls).toEqual([["thread", "input"]]);
67+
expect(notify.mock.calls).toEqual([
68+
["thread", "input"],
69+
["thread", "completion"],
70+
]);
10471
});
10572

106-
it("adds nothing when attention follows a completion that already fired", () => {
73+
it("does not replay a completion after attention resolves", () => {
10774
const { notify, observe } = harness();
10875
observe(working);
10976
observe(completed(1));
110-
vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS);
11177
observe({ attention: "run-1:limited", settled: false, completedAt: null });
11278
observe(completed(1));
113-
vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS * 2);
11479
expect(notify.mock.calls).toEqual([
11580
["thread", "completion"],
11681
["thread", "input"],
11782
]);
11883
});
11984

120-
it("still notifies a newer completion after attention", () => {
85+
it("never notifies existing, archived or stopped completions, including when unarchived", () => {
12186
const { notify, observe } = harness();
122-
observe(working);
12387
observe(completed(1));
124-
vi.advanceTimersByTime(500);
125-
observe({ attention: "run-2:approval", settled: false, completedAt: null });
12688
observe(working);
89+
observe(completed(2), { ...visible, quiet: true });
12790
observe(completed(2));
128-
vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS);
129-
expect(notify.mock.calls).toEqual([
130-
["thread", "input"],
131-
["thread", "completion"],
132-
]);
133-
});
134-
135-
it("ignores unrelated updates while a completion waits", () => {
136-
const { notify, observe } = harness();
13791
observe(working);
138-
observe(completed(1));
139-
vi.advanceTimersByTime(1_500);
140-
observe({ ...completed(1) });
141-
vi.advanceTimersByTime(500);
142-
expect(notify).toHaveBeenCalledTimes(1);
143-
});
144-
145-
it("notifies a run that settles again after a brief runtime blip", () => {
146-
const { notify, observe } = harness();
147-
observe(working);
148-
observe(completed(1));
149-
vi.advanceTimersByTime(500);
92+
observe(completed(3), { ...visible, archived: true });
93+
observe(completed(3));
94+
expect(notify).not.toHaveBeenCalled();
15095
observe(working);
151-
observe(completed(1));
152-
vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS);
96+
observe(completed(4));
15397
expect(notify.mock.calls).toEqual([["thread", "completion"]]);
15498
});
15599

156-
it("restarts the grace when a newer completion replaces a pending one", () => {
157-
const { notify, observe } = harness();
158-
observe(working);
159-
observe(completed(1));
160-
vi.advanceTimersByTime(1_500);
161-
observe(completed(2));
162-
vi.advanceTimersByTime(1_500);
163-
expect(notify).not.toHaveBeenCalled();
164-
vi.advanceTimersByTime(500);
165-
expect(notify).toHaveBeenCalledTimes(1);
166-
});
167-
168-
it("drops a pending completion on archive, stop request, removal or clear", () => {
169-
for (const end of [
170-
(h: ReturnType<typeof harness>) => h.observe(completed(1), { ...visible, archived: true }),
171-
(h: ReturnType<typeof harness>) =>
172-
h.observe({ ...completed(1) }, { ...visible, quiet: true }),
173-
(h: ReturnType<typeof harness>) => h.gate.retain(new Set()),
174-
(h: ReturnType<typeof harness>) => h.gate.clear(),
100+
it("forgets handled completions when a thread is removed or the gate is cleared", () => {
101+
for (const reset of [
102+
(gate: ReturnType<typeof createThreadNotificationGate<string>>) => gate.retain(new Set()),
103+
(gate: ReturnType<typeof createThreadNotificationGate<string>>) => gate.clear(),
175104
]) {
176-
const h = harness();
177-
h.observe(working);
178-
h.observe(completed(1));
179-
end(h);
180-
vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS * 2);
181-
expect(h.notify).not.toHaveBeenCalled();
105+
const { gate, notify, observe } = harness();
106+
observe(completed(1));
107+
reset(gate);
108+
gate.observe("thread", undefined, working, visible);
109+
gate.observe("thread", working, completed(1), visible);
110+
expect(notify.mock.calls).toEqual([["thread", "completion"]]);
182111
}
183112
});
184-
185-
it("never notifies a stopped run or the first observation", () => {
186-
const { notify, observe } = harness();
187-
observe(completed(1));
188-
observe(working);
189-
observe(completed(2), { ...visible, quiet: true });
190-
vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS * 2);
191-
expect(notify).not.toHaveBeenCalled();
192-
});
193113
});
Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { WORKING_EXIT_GRACE_MS } from "@t3tools/client-runtime/state/thread-inbox";
2-
31
export type ThreadNotificationKind = "input" | "completion";
42

53
export interface ThreadNotificationObservation {
@@ -11,23 +9,11 @@ export interface ThreadNotificationObservation {
119
readonly completedAt: number | null;
1210
}
1311

14-
/**
15-
* Attention notifies at once. Completion waits out the Working shelf's exit grace,
16-
* because an orchestrator run can end moments before its continuation starts. Any
17-
* observation that is not settled during the wait cancels it.
18-
*/
1912
export function createThreadNotificationGate<Key>(
2013
notify: (key: Key, kind: ThreadNotificationKind) => void,
2114
) {
22-
const pending = new Map<Key, { at: number; timer: ReturnType<typeof setTimeout> }>();
2315
// The newest completion already notified, or deliberately skipped.
2416
const handled = new Map<Key, number>();
25-
// The newest completion seen settled, kept while the thread works or waits.
26-
const lastSettled = new Map<Key, number>();
27-
const cancel = (key: Key) => {
28-
clearTimeout(pending.get(key)?.timer);
29-
pending.delete(key);
30-
};
3117
const skip = (key: Key, at: number | null) => {
3218
if (at !== null && at > (handled.get(key) ?? -Infinity)) handled.set(key, at);
3319
};
@@ -38,44 +24,24 @@ export function createThreadNotificationGate<Key>(
3824
next: ThreadNotificationObservation,
3925
options: { readonly archived: boolean; readonly quiet: boolean },
4026
): void {
41-
if (next.settled && next.completedAt !== null) lastSettled.set(key, next.completedAt);
42-
if (!next.settled || options.archived || options.quiet) cancel(key);
4327
// The first sighting, archived threads, and stopped or silent runs never notify completion.
4428
if (!prior || options.archived || options.quiet) skip(key, next.completedAt);
4529
if (!prior || options.archived) return;
4630
if (next.attention && next.attention !== prior.attention) {
47-
// Attention replaces the completion before it, so that run must not notify later.
48-
skip(key, lastSettled.get(key) ?? null);
49-
cancel(key);
5031
notify(key, "input");
5132
return;
5233
}
5334
const at = next.completedAt;
5435
if (!next.settled || at === null || at <= (handled.get(key) ?? -Infinity)) return;
55-
if (pending.get(key)?.at === at) return;
56-
cancel(key);
57-
pending.set(key, {
58-
at,
59-
timer: setTimeout(() => {
60-
pending.delete(key);
61-
handled.set(key, at);
62-
notify(key, "completion");
63-
}, WORKING_EXIT_GRACE_MS),
64-
});
36+
handled.set(key, at);
37+
notify(key, "completion");
6538
},
6639
/** Forgets threads that left the snapshot. */
6740
retain(keys: ReadonlySet<Key>): void {
68-
for (const key of pending.keys()) if (!keys.has(key)) cancel(key);
6941
for (const key of handled.keys()) if (!keys.has(key)) handled.delete(key);
70-
for (const key of lastSettled.keys()) if (!keys.has(key)) lastSettled.delete(key);
71-
},
72-
cancelPending(): void {
73-
for (const key of pending.keys()) cancel(key);
7442
},
7543
clear(): void {
76-
for (const key of pending.keys()) cancel(key);
7744
handled.clear();
78-
lastSettled.clear();
7945
},
8046
};
8147
}

‎apps/web/src/components/ThreadNotificationCoordinator.tsx‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ export function startThreadNotifications(
111111
setNotificationBadge(0);
112112
};
113113

114-
// Completions fire from a timer, so read settings, focus and route when presenting.
115114
const present = (
116115
environmentId: EnvironmentId,
117116
state: NotificationState,
@@ -308,7 +307,6 @@ export function startThreadNotifications(
308307
observeThreads(environmentId, entry, threads);
309308
return;
310309
}
311-
entry.gate.cancelPending();
312310
entry.reconnecting = true;
313311
},
314312
{ immediate: true },

0 commit comments

Comments
 (0)