@devries-cameron20 / t3code Mirror · read-only / commits / ac5e48c612
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 22, 00:45 UTC

fix(web): notify when a new request replaces the pending one in a run (#32719)

Julius Marminge committed Nov 10, 2026 at 05:47 UTC ac5e48c612061895511f549d363a2186a6a005ea
4 files changed +73 -11
apps/web/src/components/ThreadNotificationCoordinator.badge.browser.test.ts
+15 -2
@@ -106,6 +106,7 @@ const thread = {
106 archivedAt: null as string | null,
107 hasPendingApprovals: false,
108 hasPendingUserInput: false,
109 + pendingRequestId: "request-1",
110 session: null as { status: string } | null,
111 latestTurn: { turnId: "turn", state: "running", completedAt: null as string | null },
112 };
@@ -144,9 +145,9 @@ function toV2ThreadShell(input: typeof thread) {
145 ? "failed"
146 : "running",
147 pendingRuntimeRequest: input.hasPendingUserInput
147 - ? { id: "request-1", kind: "user_input", createdAt: SHELL_NOW }
148 + ? { id: input.pendingRequestId, kind: "user_input", createdAt: SHELL_NOW }
149 : input.hasPendingApprovals
149 - ? { id: "request-1", kind: "command", createdAt: SHELL_NOW }
150 + ? { id: input.pendingRequestId, kind: "command", createdAt: SHELL_NOW }
151 : null,
152 latestVisibleMessage: null,
153 latestUserMessageAt: null,
@@ -240,6 +241,18 @@ it.each(["hasPendingApprovals", "hasPendingUserInput"] as const)(
241 },
242 );
243
244 +it.each(["hasPendingApprovals", "hasPendingUserInput"] as const)(
245 + "notifies again when a new %s request replaces the pending one in the same run",
246 + async (flag) => {
247 + await using app = setup();
248 + app.start();
249 + state.shells.set("one", shell({ [flag]: true, pendingRequestId: "request-1" }));
250 + state.shells.set("one", shell({ [flag]: true, pendingRequestId: "request-2" }));
251 + const title = flag === "hasPendingApprovals" ? "Approval needed" : "Input needed";
252 + expect(TestNotification.sent.map(({ title }) => title)).toEqual([title, title]);
253 + },
254 +);
255 +
256 it("presents completion notifications and sound as soon as the shell completes", async () => {
257 await using app = setup();
258 state.mode = "notifications-and-sound";
apps/web/src/components/ThreadNotificationCoordinator.logic.test.ts
+25 -3
@@ -7,11 +7,13 @@ import {
7
8 const working: ThreadNotificationObservation = {
9 attention: null,
10 + requestId: null,
11 settled: false,
12 completedAt: null,
13 };
14 const completed = (at: number): ThreadNotificationObservation => ({
15 attention: null,
16 + requestId: null,
17 settled: true,
18 completedAt: at,
19 });
@@ -54,17 +56,37 @@ describe("createThreadNotificationGate", () => {
56 expect(notify).toHaveBeenCalledTimes(2);
57 });
58
57 - it("notifies attention once and still notifies the subsequent completion", () => {
59 + it("notifies each attention once per stretch and still notifies the subsequent completion", () => {
60 const { notify, observe } = harness();
61 observe(working);
60 - const attention = { attention: "run-1:approval", settled: false, completedAt: null };
62 + const attention = {
63 + attention: "run-1:approval:a",
64 + requestId: "a",
65 + settled: false,
66 + completedAt: null,
67 + };
68 observe(attention);
69 expect(notify.mock.calls).toEqual([["thread", "input"]]);
70 observe({ ...attention });
71 expect(notify).toHaveBeenCalledTimes(1);
72 + observe({ ...attention, attention: "run-1:approval:b", requestId: "b" });
73 + observe(attention);
74 + expect(notify).toHaveBeenCalledTimes(2);
75 + observe(working);
76 + observe(attention);
77 + expect(notify).toHaveBeenCalledTimes(3);
78 + const subagent = { ...attention, attention: "run-1:approval:", requestId: null };
79 + observe(subagent);
80 + observe(attention);
81 + observe(subagent);
82 + expect(notify).toHaveBeenCalledTimes(5);
83 observe(working);
84 observe(completed(1));
85 expect(notify.mock.calls).toEqual([
86 + ["thread", "input"],
87 + ["thread", "input"],
88 + ["thread", "input"],
89 + ["thread", "input"],
90 ["thread", "input"],
91 ["thread", "completion"],
92 ]);
@@ -74,7 +96,7 @@ describe("createThreadNotificationGate", () => {
96 const { notify, observe } = harness();
97 observe(working);
98 observe(completed(1));
77 - observe({ attention: "run-1:limited", settled: false, completedAt: null });
99 + observe({ attention: "run-1:limited", requestId: null, settled: false, completedAt: null });
100 observe(completed(1));
101 expect(notify.mock.calls).toEqual([
102 ["thread", "completion"],
apps/web/src/components/ThreadNotificationCoordinator.logic.ts
+25 -2
@@ -1,8 +1,10 @@
1 export type ThreadNotificationKind = "input" | "completion";
2
3 export interface ThreadNotificationObservation {
4 - /** Run and status key while the thread needs attention, otherwise null. */
4 + /** Run, status and request key while the thread needs attention, otherwise null. */
5 readonly attention: string | null;
6 + /** The pending request's id, when it has one; null whenever attention is. */
7 + readonly requestId: string | null;
8 /** The thread is idle and its latest run completed. */
9 readonly settled: boolean;
10 /** When the latest run completed, if it did. */
@@ -17,6 +19,24 @@ export function createThreadNotificationGate<Key>(
19 const skip = (key: Key, at: number | null) => {
20 if (at !== null && at > (handled.get(key) ?? -Infinity)) handled.set(key, at);
21 };
22 + // Attention keys with a request id seen since the thread last stopped needing attention.
23 + const attended = new Map<Key, Set<string>>();
24 + const attend = (
25 + key: Key,
26 + prior: ThreadNotificationObservation | undefined,
27 + next: ThreadNotificationObservation,
28 + ): boolean => {
29 + if (next.attention === null) {
30 + attended.delete(key);
31 + return false;
32 + }
33 + if (next.requestId === null) return next.attention !== prior?.attention;
34 + const seen = attended.get(key);
35 + if (seen?.has(next.attention)) return false;
36 + if (seen) seen.add(next.attention);
37 + else attended.set(key, new Set([next.attention]));
38 + return true;
39 + };
40 return {
41 observe(
42 key: Key,
@@ -26,8 +46,9 @@ export function createThreadNotificationGate<Key>(
46 ): void {
47 // The first sighting, archived threads, and stopped or silent runs never notify completion.
48 if (!prior || options.archived || options.quiet) skip(key, next.completedAt);
49 + const fresh = attend(key, prior, next);
50 if (!prior || options.archived) return;
30 - if (next.attention && next.attention !== prior.attention) {
51 + if (fresh) {
52 notify(key, "input");
53 return;
54 }
@@ -39,9 +60,11 @@ export function createThreadNotificationGate<Key>(
60 /** Forgets threads that left the snapshot. */
61 retain(keys: ReadonlySet<Key>): void {
62 for (const key of handled.keys()) if (!keys.has(key)) handled.delete(key);
63 + for (const key of attended.keys()) if (!keys.has(key)) attended.delete(key);
64 },
65 clear(): void {
66 handled.clear();
67 + attended.clear();
68 },
69 };
70 }
apps/web/src/components/ThreadNotificationCoordinator.tsx
+8 -4
@@ -30,6 +30,7 @@ import {
30 threadNotificationTag,
31 unlockNotificationAudio,
32 } from "../threadNotifications";
33 +import { resolveBackgroundThreadRequest } from "./backgroundThreadAnnouncement";
34 import { resolveSidebarThreadStatus, type SidebarThreadStatus } from "./Sidebar.logic";
35 import {
36 createThreadNotificationGate,
@@ -252,14 +253,16 @@ export function startThreadNotifications(
253 const thread = presentThreadShell(environmentId, rawThread, subagentRequest);
254 let status = resolveSidebarThreadStatus(thread);
255 if (status === "ready" && thread.latestRun?.status === "failed") status = "failed";
255 - const attention =
256 + const needsAttention =
257 status === "input" ||
258 status === "request" ||
259 status === "approval" ||
260 status === "failed" ||
260 - status === "limited"
261 - ? `${thread.latestRun?.runId ?? ""}:${status}`
262 - : null;
261 + status === "limited";
262 + const requestId = needsAttention ? resolveBackgroundThreadRequest(thread).requestId : null;
263 + const attention = needsAttention
264 + ? `${thread.latestRun?.runId ?? ""}:${status}:${requestId ?? ""}`
265 + : null;
266 const completedAt = Date.parse(thread.latestRun?.completedAt ?? "");
267 const settled = status === "ready" && thread.latestRun?.status === "completed";
268 const state = {
@@ -268,6 +271,7 @@ export function startThreadNotifications(
271 thread,
272 status,
273 attention,
274 + requestId,
275 settled,
276 completedAt: settled && Number.isFinite(completedAt) ? completedAt : null,
277 };