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>
This commit is contained in:
Vadim Lazuk
2026-11-19 19:28:02 +08:00
committed by GitHub
co-authored by Inf1mum
parent ae604b7933
commit e93ac42489
4 changed files with 130 additions and 2 deletions
+2
View File
@@ -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",
+2
View File
@@ -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 做完后发送",
+78
View File
@@ -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<typeof import("@/platform/ipc")>();
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"]);
});
});
+48 -2
View File
@@ -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<IdeNotification, "level" | "sticky" | "lo
const MAX_HISTORY = 200;
let seq = 0;
/**
* OS toasts for a burst of balloons (an external agent editing files while OpenIDEA
* is in the background, a build) are coalesced: the first shows at once, the ones
* that follow within a quiet period become one toast naming the last and how many
* more arrived. Balloons and the Notifications tool window keep every notification.
*/
const SYSTEM_TOAST_QUIET_MS = 3000;
/** A steady stream still raises a summary this often. */
const SYSTEM_TOAST_MAX_WAIT_MS = 30_000;
let toastTimer: ReturnType<typeof setTimeout> | 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<NotificationService["notify"]>[0]): number {
const id = ++seq;
const group = groupOf(n);
@@ -163,7 +209,7 @@ export function notify(n: Parameters<NotificationService["notify"]>[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;
}