Skip to content

Commit 8f14215

Browse files
committed
Merge remote-tracking branch 'origin/main' into pr-29111
2 parents 2ad27a3 + ac5e48c commit 8f14215

4 files changed

Lines changed: 73 additions & 11 deletions

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ const thread = {
106106
archivedAt: null as string | null,
107107
hasPendingApprovals: false,
108108
hasPendingUserInput: false,
109+
pendingRequestId: "request-1",
109110
session: null as { status: string } | null,
110111
latestTurn: { turnId: "turn", state: "running", completedAt: null as string | null },
111112
};
@@ -144,9 +145,9 @@ function toV2ThreadShell(input: typeof thread) {
144145
? "failed"
145146
: "running",
146147
pendingRuntimeRequest: input.hasPendingUserInput
147-
? { id: "request-1", kind: "user_input", createdAt: SHELL_NOW }
148+
? { id: input.pendingRequestId, kind: "user_input", createdAt: SHELL_NOW }
148149
: input.hasPendingApprovals
149-
? { id: "request-1", kind: "command", createdAt: SHELL_NOW }
150+
? { id: input.pendingRequestId, kind: "command", createdAt: SHELL_NOW }
150151
: null,
151152
latestVisibleMessage: null,
152153
latestUserMessageAt: null,
@@ -240,6 +241,18 @@ it.each(["hasPendingApprovals", "hasPendingUserInput"] as const)(
240241
},
241242
);
242243

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+
243256
it("presents completion notifications and sound as soon as the shell completes", async () => {
244257
await using app = setup();
245258
state.mode = "notifications-and-sound";

‎apps/web/src/components/ThreadNotificationCoordinator.logic.test.ts‎

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ import {
77

88
const working: ThreadNotificationObservation = {
99
attention: null,
10+
requestId: null,
1011
settled: false,
1112
completedAt: null,
1213
};
1314
const completed = (at: number): ThreadNotificationObservation => ({
1415
attention: null,
16+
requestId: null,
1517
settled: true,
1618
completedAt: at,
1719
});
@@ -54,17 +56,37 @@ describe("createThreadNotificationGate", () => {
5456
expect(notify).toHaveBeenCalledTimes(2);
5557
});
5658

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", () => {
5860
const { notify, observe } = harness();
5961
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+
};
6168
observe(attention);
6269
expect(notify.mock.calls).toEqual([["thread", "input"]]);
6370
observe({ ...attention });
6471
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);
6583
observe(working);
6684
observe(completed(1));
6785
expect(notify.mock.calls).toEqual([
86+
["thread", "input"],
87+
["thread", "input"],
88+
["thread", "input"],
89+
["thread", "input"],
6890
["thread", "input"],
6991
["thread", "completion"],
7092
]);
@@ -74,7 +96,7 @@ describe("createThreadNotificationGate", () => {
7496
const { notify, observe } = harness();
7597
observe(working);
7698
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 });
78100
observe(completed(1));
79101
expect(notify.mock.calls).toEqual([
80102
["thread", "completion"],

‎apps/web/src/components/ThreadNotificationCoordinator.logic.ts‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
export type ThreadNotificationKind = "input" | "completion";
22

33
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. */
55
readonly attention: string | null;
6+
/** The pending request's id, when it has one; null whenever attention is. */
7+
readonly requestId: string | null;
68
/** The thread is idle and its latest run completed. */
79
readonly settled: boolean;
810
/** When the latest run completed, if it did. */
@@ -17,6 +19,24 @@ export function createThreadNotificationGate<Key>(
1719
const skip = (key: Key, at: number | null) => {
1820
if (at !== null && at > (handled.get(key) ?? -Infinity)) handled.set(key, at);
1921
};
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+
};
2040
return {
2141
observe(
2242
key: Key,
@@ -26,8 +46,9 @@ export function createThreadNotificationGate<Key>(
2646
): void {
2747
// The first sighting, archived threads, and stopped or silent runs never notify completion.
2848
if (!prior || options.archived || options.quiet) skip(key, next.completedAt);
49+
const fresh = attend(key, prior, next);
2950
if (!prior || options.archived) return;
30-
if (next.attention && next.attention !== prior.attention) {
51+
if (fresh) {
3152
notify(key, "input");
3253
return;
3354
}
@@ -39,9 +60,11 @@ export function createThreadNotificationGate<Key>(
3960
/** Forgets threads that left the snapshot. */
4061
retain(keys: ReadonlySet<Key>): void {
4162
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);
4264
},
4365
clear(): void {
4466
handled.clear();
67+
attended.clear();
4568
},
4669
};
4770
}

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {
3030
threadNotificationTag,
3131
unlockNotificationAudio,
3232
} from "../threadNotifications";
33+
import { resolveBackgroundThreadRequest } from "./backgroundThreadAnnouncement";
3334
import { resolveSidebarThreadStatus, type SidebarThreadStatus } from "./Sidebar.logic";
3435
import {
3536
createThreadNotificationGate,
@@ -252,14 +253,16 @@ export function startThreadNotifications(
252253
const thread = presentThreadShell(environmentId, rawThread, subagentRequest);
253254
let status = resolveSidebarThreadStatus(thread);
254255
if (status === "ready" && thread.latestRun?.status === "failed") status = "failed";
255-
const attention =
256+
const needsAttention =
256257
status === "input" ||
257258
status === "request" ||
258259
status === "approval" ||
259260
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;
263266
const completedAt = Date.parse(thread.latestRun?.completedAt ?? "");
264267
const settled = status === "ready" && thread.latestRun?.status === "completed";
265268
const state = {
@@ -268,6 +271,7 @@ export function startThreadNotifications(
268271
thread,
269272
status,
270273
attention,
274+
requestId,
271275
settled,
272276
completedAt: settled && Number.isFinite(completedAt) ? completedAt : null,
273277
};

0 commit comments

Comments
 (0)