Skip to content

A local-first multi-agent app development platform, built on PenguinHarness

fix(desktop): 任务完成通知在 Windows 上从不弹出,桌面端根本没开通知权限

lmliheng5ad8590ac864
diff --git a/packages/desktop/src/app-permissions.ts b/packages/desktop/src/app-permissions.ts
new file mode 100644
index 00000000..d61ebd34
--- /dev/null
+++ b/packages/desktop/src/app-permissions.ts
@@ -0,0 +1,36 @@
+/**
+ * What the App window's own session is allowed to ask the platform for. Pure decisions only —
+ * the handlers that call them are installed in main.ts, because an Electron import cannot be
+ * loaded by this package's vitest run.
+ *
+ * The App page is a plain browser on the default session, and Electron's permission handlers
+ * are per session. Nothing installed one there, so a `notifications` check never settled: the
+ * page's `Notification.permission` stayed "default" (the Preferences file carried no
+ * notification grant), the task-completion notification's `permission === "granted"` gate
+ * refused every time, and on Windows no toast was ever handed to the system (req-42).
+ *
+ * Notifications are therefore granted for the App's own origin — the one origin allowed to
+ * navigate in this window is also the one allowed to notify — and nothing else changes: every
+ * other permission keeps the answer this shell has always given (granted), so hardening one
+ * permission cannot take away the page's clipboard, fullscreen or pointer lock. The built-in
+ * browser's guests never reach this: they live on their own partition, with their own
+ * handlers (builtin-browser.ts).
+ */
+import { isAppUrl } from "./util.js";
+
+/** The one permission this module decides for itself. */
+export const NOTIFICATIONS_PERMISSION = "notifications";
+
+/**
+ * Whether one permission check (or request) is granted. `requestingUrl` is the page that
+ * asked — an origin for the check handler, a full URL for the request handler — and a
+ * missing one counts as outside the App.
+ */
+export function grantsPermission(
+ permission: string,
+ requestingUrl: string,
+ appOrigin: string | null,
+): boolean {
+ if (permission === NOTIFICATIONS_PERMISSION) return isAppUrl(requestingUrl, appOrigin);
+ return true;
+}
diff --git a/packages/desktop/src/main.ts b/packages/desktop/src/main.ts
index 0db64423..f413feea 100644
--- a/packages/desktop/src/main.ts
+++ b/packages/desktop/src/main.ts
@@ -65,6 +65,7 @@ import { mintApiToken } from "@adelie/server/auth-token";
import { liveServerLock } from "@adelie/server/lock";
import type { ServerLock } from "@adelie/server/lock";
import { appIdentity, desktopDataRoot, resolveProfile } from "./app-identity.js";
+import { grantsPermission } from "./app-permissions.js";
import {
createSignInGuard,
isLoginPageUrl,
@@ -174,6 +175,25 @@ function fatal(context: string, err: unknown): void {
app.exit(1);
}
+/**
+ * Installs the App window's permission handlers on the default session (see app-permissions.ts
+ * for why notifications need one at all).
+ *
+ * Called once, before the window exists, and before boot() resolves an origin: both handlers
+ * read `appOrigin` at every check, so they answer for whatever origin this launch ended up on.
+ * `requestingUrl` is the page that asked; the check handler's `requestingOrigin` is already an
+ * origin, which is the same comparison isAppUrl makes.
+ */
+function allowAppNotifications(): void {
+ session.defaultSession.setPermissionCheckHandler((_contents, permission, requestingOrigin) =>
+ grantsPermission(permission, requestingOrigin, appOrigin),
+ );
+ session.defaultSession.setPermissionRequestHandler((contents, permission, callback, details) => {
+ const asked = details.requestingUrl ?? contents?.getURL() ?? "";
+ callback(grantsPermission(permission, asked, appOrigin));
+ });
+}
+
function createWindow(url: string): void {
// Linux window/taskbar icon (and Windows dev runs); packaged Windows uses the exe
// resources and macOS its bundle icns, so those ignore it (see app-icon.ts).
@@ -749,6 +769,9 @@ if (!app.requestSingleInstanceLock()) {
shell: process.env.SHELL,
log: (line) => logLine(`[shell] ${line}`),
});
+ // The App page may ask for notifications and nothing new; installed before the window
+ // exists, so the very first request already finds a handler.
+ allowAppNotifications();
// Standard menu plus native desktop-only actions; the window gets no IPC channel.
// The install item is the release profile's, like the per-launch repair below: the
// `penguin` command has one owner, and a `--dev` instance of the same install would
diff --git a/packages/desktop/test/app-permissions.test.ts b/packages/desktop/test/app-permissions.test.ts
new file mode 100644
index 00000000..9cf78652
--- /dev/null
+++ b/packages/desktop/test/app-permissions.test.ts
@@ -0,0 +1,30 @@
+import { describe, expect, it } from "vitest";
+import { grantsPermission } from "../src/app-permissions.js";
+
+const ORIGIN = "http://localhost:51234";
+
+describe("grantsPermission", () => {
+ it("grants notifications to the App's own origin", () => {
+ expect(grantsPermission("notifications", ORIGIN, ORIGIN)).toBe(true);
+ });
+
+ it("grants notifications for a page URL on the App origin (the request handler's shape)", () => {
+ expect(grantsPermission("notifications", `${ORIGIN}/chat/abc`, ORIGIN)).toBe(true);
+ });
+
+ it("refuses notifications from another origin, a preview host included", () => {
+ expect(grantsPermission("notifications", "http://127.0.0.1:51234/", ORIGIN)).toBe(false);
+ expect(grantsPermission("notifications", "https://example.com/", ORIGIN)).toBe(false);
+ });
+
+ it("refuses notifications while the App origin is not known yet", () => {
+ expect(grantsPermission("notifications", ORIGIN, null)).toBe(false);
+ });
+
+ it("leaves every other permission as it was (granted)", () => {
+ for (const permission of ["fullscreen", "clipboard-sanitized-write", "media", "geolocation"]) {
+ expect(grantsPermission(permission, "https://example.com/", ORIGIN)).toBe(true);
+ expect(grantsPermission(permission, ORIGIN, null)).toBe(true);
+ }
+ });
+});
diff --git a/packages/web/src/state/use-completion-notifications.ts b/packages/web/src/state/use-completion-notifications.ts
index 821694f8..64ba7e25 100644
--- a/packages/web/src/state/use-completion-notifications.ts
+++ b/packages/web/src/state/use-completion-notifications.ts
@@ -52,7 +52,16 @@ export function useCompletionNotifications(): void {
if (!enabled) return;
if (completed.length === 0) return;
if (!document.hidden && document.hasFocus()) return;
- if (notificationPermission() !== "granted") return;
+ if (notificationPermission() !== "granted") {
+ // Warned, not swallowed: nothing else on screen reports this state after the settings
+ // switch was turned on, and a notification dropped here looks exactly like a completion
+ // the tracker never saw — which is what made the Windows report impossible to narrow
+ // down (task 42). The devtools console is where the desktop shell's log drawer can see it.
+ console.warn(
+ `[notifications] ${completed.length} task(s) finished while away, but the platform grants no notifications (${notificationPermission()})`,
+ );
+ return;
+ }
for (const sessionId of completed) {
const session = sessions.find((s) => s.sessionId === sessionId);
const title = session?.title ?? S.chat.defaultSessionTitle;
@@ -71,9 +80,10 @@ export function useCompletionNotifications(): void {
if (agentId !== null) setCurrentAgentIdRef.current(agentId);
navigateRef.current(`/chat/${sessionId}`);
};
- } catch {
- // Notification construction is best-effort; a platform refusing it must not
- // break the app.
+ } catch (err) {
+ // Notification construction is best-effort; a platform refusing it must not break the
+ // app — but it has to say so, or a shell that never shows one gives no clue why.
+ console.warn(`[notifications] the notification for ${sessionId} was refused:`, err);
}
}
}, [enabled, sessions]);