From e93ac42489c0bac3f5ccfefa09d44f84d054edfa Mon Sep 17 00:00:00 2001 From: Vadim Lazuk Date: Thu, 19 Nov 2026 14:28:02 +0300 Subject: [PATCH] fix(workbench): coalesce system toasts for external edit bursts (#4502) Every balloon raised while OpenIDEA was in the background became its own OS toast, so an external agent editing many files produced a stream of toasts. The first toast still shows at once; balloons that follow within a 3-second quiet period become one toast naming the last balloon and how many more arrived, and a steady stream raises such a summary at most every 30 seconds. In-IDE balloons and the Notifications tool window keep every notification. Closes #4466 Co-authored-by: Inf1mum <8135225+Inf1mum@users.noreply.github.com> --- src/platform/locales/text/en.json | 2 + src/platform/locales/text/zh.json | 2 + src/workbench/notificationStore.test.ts | 78 +++++++++++++++++++++++++ src/workbench/notificationStore.ts | 50 +++++++++++++++- 4 files changed, 130 insertions(+), 2 deletions(-) create mode 100644 src/workbench/notificationStore.test.ts diff --git a/src/platform/locales/text/en.json b/src/platform/locales/text/en.json index 0f77d3fe7..cfb196131 100644 --- a/src/platform/locales/text/en.json +++ b/src/platform/locales/text/en.json @@ -1258,6 +1258,8 @@ "Plan {done}/{length}": "Plan {done}/{length}", "1 changed file": "1 changed file", "{count} changed files": "{count} changed files", + "{count} more notification": "{count} more notification", + "{count} more notifications": "{count} more notifications", "Send into the agent's turn": "Send into the agent's turn", "Queue a message for when the agent is done (⌘⇧↵ sends it now)": "Queue a message for when the agent is done (⌘⇧↵ sends it now)", "Queue a message for when the agent is done": "Queue a message for when the agent is done", diff --git a/src/platform/locales/text/zh.json b/src/platform/locales/text/zh.json index 300ec741f..8ac4d7965 100644 --- a/src/platform/locales/text/zh.json +++ b/src/platform/locales/text/zh.json @@ -7648,6 +7648,8 @@ "Plan {done}/{length}": "计划 {done}/{length}", "1 changed file": "改动 1 个文件", "{count} changed files": "改动 {count} 个文件", + "{count} more notification": "还有 {count} 条新通知", + "{count} more notifications": "还有 {count} 条新通知", "Send into the agent's turn": "发送到当前回合", "Queue a message for when the agent is done (⌘⇧↵ sends it now)": "排队一条消息,agent 做完后发送(⌘⇧↵ 立即发送)", "Queue a message for when the agent is done": "排队一条消息,agent 做完后发送", diff --git a/src/workbench/notificationStore.test.ts b/src/workbench/notificationStore.test.ts new file mode 100644 index 000000000..4f0609968 --- /dev/null +++ b/src/workbench/notificationStore.test.ts @@ -0,0 +1,78 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { notify } from "./notificationStore"; + +const { showSystemNotification } = vi.hoisted(() => ({ + showSystemNotification: vi.fn(async (_title: string, _body: string) => ({ status: "ok", data: null })), +})); + +vi.mock("@/platform/ipc", async (orig) => { + const actual = await orig(); + return { ...actual, isTauri: () => true, commands: { ...actual.commands, showSystemNotification } }; +}); + +beforeEach(() => { + vi.useFakeTimers(); + showSystemNotification.mockClear(); +}); + +afterEach(() => { + vi.runAllTimers(); + vi.useRealTimers(); +}); + +describe("system toast coalescing", () => { + it("shows the first toast at once and the rest of a burst as one, naming the last and how many more", () => { + notify({ level: "info", title: "File a.ts changed" }); + expect(showSystemNotification).toHaveBeenCalledTimes(1); + expect(showSystemNotification.mock.calls[0]).toEqual(["File a.ts changed", ""]); + notify({ level: "info", title: "File b.ts changed", body: "written by an agent" }); + notify({ level: "info", title: "File c.ts changed", body: "written by an agent" }); + expect(showSystemNotification).toHaveBeenCalledTimes(1); + vi.advanceTimersByTime(3000); + expect(showSystemNotification).toHaveBeenCalledTimes(2); + expect(showSystemNotification.mock.calls[1]).toEqual([ + "File c.ts changed", + "written by an agent\n1 more notification", + ]); + }); + + it("shows a lone balloon's toast as written, without waiting", () => { + notify({ level: "info", title: "Git refresh failed", body: "no remote" }); + expect(showSystemNotification).toHaveBeenCalledTimes(1); + expect(showSystemNotification.mock.calls[0]).toEqual(["Git refresh failed", "no remote"]); + vi.advanceTimersByTime(10_000); + expect(showSystemNotification).toHaveBeenCalledTimes(1); + }); + + it("keeps waiting while balloons keep arriving", () => { + notify({ level: "info", title: "First" }); + notify({ level: "info", title: "Second" }); + vi.advanceTimersByTime(2000); + notify({ level: "info", title: "Third" }); + vi.advanceTimersByTime(2000); + expect(showSystemNotification).toHaveBeenCalledTimes(1); + vi.advanceTimersByTime(1000); + expect(showSystemNotification).toHaveBeenCalledTimes(2); + expect(showSystemNotification.mock.calls[1]).toEqual(["Third", "1 more notification"]); + }); + + it("still summarises a steady stream every 30 seconds", () => { + notify({ level: "info", title: "Start" }); + for (let i = 0; i < 30; i++) { + vi.advanceTimersByTime(1000); + notify({ level: "info", title: `Edit ${i}` }); + } + expect(showSystemNotification).toHaveBeenCalledTimes(1); + vi.advanceTimersByTime(1000); + expect(showSystemNotification).toHaveBeenCalledTimes(2); + expect(showSystemNotification.mock.calls[1]![0]).toBe("Edit 29"); + }); + + it("falls back to the count alone when a balloon has no body", () => { + notify({ level: "warning", title: "Cannot reload Maven projects" }); + notify({ level: "warning", title: "Cannot reload Gradle projects" }); + notify({ level: "warning", title: "Cannot reload sbt projects" }); + vi.advanceTimersByTime(3000); + expect(showSystemNotification.mock.calls[1]).toEqual(["Cannot reload sbt projects", "1 more notification"]); + }); +}); diff --git a/src/workbench/notificationStore.ts b/src/workbench/notificationStore.ts index 9748a6535..da85a52ed 100644 --- a/src/workbench/notificationStore.ts +++ b/src/workbench/notificationStore.ts @@ -3,7 +3,7 @@ import type { ReactNode } from "react"; import { create } from "zustand"; import { call, commands, isTauri, onIdeEvent } from "@/platform/ipc"; import { buildToolRecovery } from "@/platform/buildToolRecovery"; -import { txTemplate } from "@/platform/i18n"; +import { tx, txTemplate } from "@/platform/i18n"; import { useProject } from "@/platform/project"; import type { NotificationService } from "./contributions"; @@ -127,6 +127,52 @@ export function balloonTimeout(n: Pick | undefined; +let toastBatch: Array<{ title: string; body: string }> = []; +let toastBatchStart = 0; + +function showToast(title: string, body: string) { + void call(commands.showSystemNotification(title, body)).catch(() => {}); +} + +/** Ends a quiet period: the toasts held back during it, as one. */ +function flushToasts() { + const batch = toastBatch; + toastBatch = []; + if (!batch.length) { + toastTimer = undefined; + return; + } + const last = batch[batch.length - 1]!; + const more = batch.length - 1; + const count = more === 1 ? tx`${more} more notification` : tx`${more} more notifications`; + showToast(last.title, more === 0 ? last.body : [last.body, count].filter(Boolean).join("\n")); + // What arrives right after the summary joins the next one. + toastTimer = setTimeout(flushToasts, SYSTEM_TOAST_QUIET_MS); +} + +function systemToast(title: string, body: string) { + if (toastTimer === undefined) { + showToast(title, body); + toastTimer = setTimeout(flushToasts, SYSTEM_TOAST_QUIET_MS); + return; + } + if (!toastBatch.length) toastBatchStart = Date.now(); + toastBatch.push({ title, body }); + clearTimeout(toastTimer); + const waited = Date.now() - toastBatchStart; + toastTimer = setTimeout(flushToasts, Math.max(0, Math.min(SYSTEM_TOAST_QUIET_MS, SYSTEM_TOAST_MAX_WAIT_MS - waited))); +} + export function notify(n: Parameters[0]): number { const id = ++seq; const group = groupOf(n); @@ -163,7 +209,7 @@ export function notify(n: Parameters[0]): number // In the UI language, as the balloon shows it; a plugin's own text stays as written. const shown = (text: string) => (n.verbatim ? text : txTemplate(text)); const plain = typeof n.body === "string" ? shown(n.body) : ""; - void call(commands.showSystemNotification(shown(n.title), plain)).catch(() => {}); + systemToast(shown(n.title), plain); } return id; }