Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,30 @@ it.each(["hasPendingApprovals", "hasPendingUserInput"] as const)(
},
);

it.each([
["sound", "granted"],
["notifications", "denied"],
] as const)(
"badges an unseen event without a system notification (mode %s, permission %s)",
async (mode, permission) => {
await using app = setup();
state.mode = mode;
TestNotification.permission = permission;
app.start();
state.shells.set(
"one",
shell({
latestTurn: { turnId: "turn", state: "completed", completedAt: "2026-09-13T07:00:01Z" },
}),
);
state.shells.set("two", shell({ hasPendingUserInput: true }));
expect(TestNotification.sent).toEqual([]);
expect(state.badge).toHaveBeenLastCalledWith(2);
window.dispatchEvent(new Event("focus"));
expect(state.badge).toHaveBeenLastCalledWith(0);
},
);

it.each(["hasPendingApprovals", "hasPendingUserInput"] as const)(
"notifies once per %s request, not again as queued runs start behind it",
async (flag) => {
Expand Down Expand Up @@ -295,7 +319,12 @@ it("presents completion notifications and sound as soon as the shell completes",
});

it.each([
{ client: "desktop app", when: "at startup", bridge: {}, beforeGesture: 1 },
{
client: "desktop app",
when: "at startup",
bridge: { onNotificationBadgeClear: () => () => {} },
beforeGesture: 1,
},
{ client: "browser", when: "on the first gesture", bridge: undefined, beforeGesture: 0 },
])("readies notification sound in the $client $when", async ({ bridge, beforeGesture }) => {
await using app = setup();
Expand Down Expand Up @@ -433,6 +462,18 @@ it.each([false, true])(
},
);

it("turning off in-app notifications with mode off clears the badge", async () => {
await using app = setup();
state.mode = "off";
state.inApp = true;
app.start();
state.shells.set("one", shell({ hasPendingUserInput: true }));
expect(state.badge).toHaveBeenLastCalledWith(1);
state.inApp = false;
app.changeSettings();
expect(state.badge).toHaveBeenLastCalledWith(0);
});

it("opening a thread or removing its environment closes only its in-app toasts, and disabling notifications closes the rest", async () => {
await using app = setup();
state.inApp = true;
Expand Down
49 changes: 28 additions & 21 deletions apps/web/src/components/ThreadNotificationCoordinator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,18 @@ export function startThreadNotifications(
): () => void {
const pending = new Map<
string,
{ environmentId: EnvironmentId; threadId: ThreadId; notification: Notification }
{ environmentId: EnvironmentId; threadId: ThreadId; notification: Notification | null }
>();
const watched = new Map<EnvironmentId, WatchedEnvironment>();

const trackNotification = (
environmentId: EnvironmentId,
threadId: ThreadId,
notification: Notification,
tag: string,
notification: Notification | null,
) => {
pending.get(notification.tag)?.notification.close();
pending.set(notification.tag, { environmentId, threadId, notification });
pending.get(tag)?.notification?.close();
pending.set(tag, { environmentId, threadId, notification });
setNotificationBadge(pending.size);
};
// Toast ids by thread key, so hiding or opening a thread, or removing its environment, closes them.
Expand Down Expand Up @@ -119,13 +120,13 @@ export function startThreadNotifications(
const count = pending.size;
for (const [tag, entry] of pending) {
if (!shouldClose(threadKey(entry))) continue;
entry.notification.close();
entry.notification?.close();
pending.delete(tag);
}
if (count !== pending.size) setNotificationBadge(pending.size);
};
const clearPending = () => {
for (const { notification } of pending.values()) notification.close();
for (const { notification } of pending.values()) notification?.close();
pending.clear();
setNotificationBadge(0);
};
Expand Down Expand Up @@ -202,22 +203,26 @@ export function startThreadNotifications(
openToasts.set(toastKey, [...(openToasts.get(toastKey) ?? []), toastId]);
return;
}
if (document.visibilityState === "visible" && document.hasFocus()) return;
const tag = threadNotificationTag(environmentId, thread.id);
// The badge counts unseen events even when system notifications are off or not permitted.
if (
!hasDesktopNotifications(mode) ||
(document.visibilityState === "visible" && document.hasFocus()) ||
typeof Notification === "undefined" ||
Notification.permission !== "granted"
)
) {
trackNotification(environmentId, thread.id, tag, null);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
return;
}
try {
const notification = new Notification(title, {
body: thread.title,
tag: threadNotificationTag(environmentId, thread.id),
tag,
silent: true,
// Windows drops clicks after a toast moves into Notification Center.
requireInteraction: kind === "completion" && isWindowsPlatform(navigator.platform),
});
trackNotification(environmentId, thread.id, notification);
trackNotification(environmentId, thread.id, tag, notification);
notification.addEventListener("click", () => {
notification.close();
window.focus();
Expand All @@ -229,6 +234,7 @@ export function startThreadNotifications(
});
} catch {
// Some browsers expose Notification but reject desktop presentation.
trackNotification(environmentId, thread.id, tag, null);
}
};

Expand Down Expand Up @@ -363,7 +369,10 @@ export function startThreadNotifications(
const syncEnvironments = () => {
const { notificationMode, inAppNotificationsEnabled } = getClientSettings();
const enabled = notificationMode !== "off" || inAppNotificationsEnabled;
if (!enabled) closeToasts(() => true);
if (!enabled) {
closeToasts(() => true);
clearPending();
}
const environmentIds = enabled ? registry.get(environmentSummaries.environmentIdsAtom) : [];
const activeIds = new Set(environmentIds);
for (const [environmentId, entry] of watched) {
Expand All @@ -382,7 +391,7 @@ export function startThreadNotifications(
const count = pending.size;
for (const [tag, entry] of pending) {
if (activeIds.has(entry.environmentId)) continue;
entry.notification.close();
entry.notification?.close();
pending.delete(tag);
}
if (count !== pending.size) setNotificationBadge(pending.size);
Expand All @@ -398,15 +407,13 @@ export function startThreadNotifications(
stopModeListeners();
clearPending();
const stops: Array<() => void> = [];
if (hasDesktopNotifications(nextMode)) {
const unsubscribeBadgeClear = window.desktopBridge?.onNotificationBadgeClear(clearPending);
window.addEventListener("focus", clearPending);
stops.push(() => {
unsubscribeBadgeClear?.();
window.removeEventListener("focus", clearPending);
clearPending();
});
}
const unsubscribeBadgeClear = window.desktopBridge?.onNotificationBadgeClear(clearPending);
window.addEventListener("focus", clearPending);
stops.push(() => {
unsubscribeBadgeClear?.();
window.removeEventListener("focus", clearPending);
clearPending();
});
if (hasNotificationSound(nextMode)) {
// Electron's default autoplay policy needs no gesture, unlike browsers.
if (window.desktopBridge) unlockNotificationAudio();
Expand Down
Loading