Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

test(plugins): observable gate unit, production scope filter, widened view contract

liuzixiangc419e999be4b
diff --git a/apps/desktop/electron/main/ipc/plugin-ui-ipc.ts b/apps/desktop/electron/main/ipc/plugin-ui-ipc.ts
index 1d4f2860f..c0d516a95 100644
--- a/apps/desktop/electron/main/ipc/plugin-ui-ipc.ts
+++ b/apps/desktop/electron/main/ipc/plugin-ui-ipc.ts
@@ -11,6 +11,7 @@ import { PluginPanelHost } from "../plugin-panel-host";
import type { PluginPanelTheme } from "../../shared/plugin-panel-chrome";
import type { IpcRegistrar } from "./types";
import { pluginProviderCatalogEntries } from "../plugin-provider-catalog";
+import { createSessionPushGate } from "../plugin-session-push-gate";
export type PluginUiIpcDependencies = {
registrar: IpcRegistrar;
@@ -48,13 +49,7 @@ export function registerPluginUiIpc({
* The newest selection the shell reported, so a slower invoke that lands
* after a newer one cannot restore a session the user already left.
*/
- let activeSessionSeq = 0;
- /** The renderer load the high-water mark belongs to. A reload restarts the
- * renderer's counter from one, so a changed boot id resets the mark and
- * pushes keep flowing afterwards (#1753). The boot id is per renderer
- * load, so with two windows open the out-of-order guarantee is per window
- * — only one window's session is on screen at a time. */
- let activeSessionBootId: string | null = null;
+ const sessionPushGate = createSessionPushGate();
/**
* The session on screen, as the shell sees it.
*
@@ -74,15 +69,8 @@ export function registerPluginUiIpc({
) => {
const seq = typeof payload?.seq === "number" ? payload.seq : 0;
const bootId = typeof payload?.bootId === "string" ? payload.bootId : null;
- if (seq > 0) {
- if (bootId && bootId !== activeSessionBootId) {
- activeSessionBootId = bootId;
- activeSessionSeq = seq;
- } else if (seq >= activeSessionSeq) {
- activeSessionSeq = seq;
- } else {
- return { ok: true, ignored: true };
- }
+ if (sessionPushGate({ seq, bootId }).ignored) {
+ return { ok: true, ignored: true };
}
const sessionId = typeof payload?.sessionId === "string" ? payload.sessionId.trim() : "";
if (!sessionId) {
diff --git a/apps/desktop/electron/main/plugin-session-push-gate.ts b/apps/desktop/electron/main/plugin-session-push-gate.ts
new file mode 100644
index 000000000..2e6f03ca2
--- /dev/null
+++ b/apps/desktop/electron/main/plugin-session-push-gate.ts
@@ -0,0 +1,27 @@
+/**
+ * The out-of-order gate for plugin session pushes (#1753): within one
+ * renderer load a slower push must never undo a newer one, and a renderer
+ * reload (the automatic recovery for render-process-gone) restarts the
+ * renderer's counter — so a changed boot id resets the high-water mark
+ * instead of silently dropping every push afterwards. The boot id is per
+ * renderer load, so with two windows open the ordering guarantee is per
+ * window; only one window's session is on screen at a time. Kept dependency
+ * free so `plugin-session-follow.test.mjs` can drive it directly.
+ */
+export function createSessionPushGate() {
+ let activeSessionSeq = 0;
+ let activeSessionBootId: string | null = null;
+ return (push: { seq: number; bootId?: string | null }): { ok: boolean; ignored?: boolean } => {
+ if (push.seq > 0) {
+ if (push.bootId && push.bootId !== activeSessionBootId) {
+ activeSessionBootId = push.bootId;
+ activeSessionSeq = push.seq;
+ } else if (push.seq >= activeSessionSeq) {
+ activeSessionSeq = push.seq;
+ } else {
+ return { ok: true, ignored: true };
+ }
+ }
+ return { ok: true };
+ };
+}
diff --git a/apps/desktop/electron/main/plugin-session-scope.ts b/apps/desktop/electron/main/plugin-session-scope.ts
new file mode 100644
index 000000000..0f534fbe6
--- /dev/null
+++ b/apps/desktop/electron/main/plugin-session-scope.ts
@@ -0,0 +1,28 @@
+/**
+ * Scope rules for plugin session events (#1754). Selection events carry
+ * another project's session identity, so their recipients are scope-checked
+ * against the announced session's project — the same line `session.getActive`
+ * enforces on the pull side. Kept dependency free so
+ * `plugin-services.test.mjs` can drive the filter directly.
+ */
+
+/** Session events whose payload describes the user's current selection. */
+export const SESSION_SCOPED_EVENTS = new Set(["session:activated"]);
+
+/**
+ * The recipient filter for a session event, or `undefined` when the event
+ * fans out unconditionally.
+ */
+export function sessionEventScopeFilter(
+ event: string,
+ sessionId: string | undefined,
+ sessionProjects: ReadonlyMap<string, string | null>,
+ pluginActiveInProject: (pluginId: string, projectPath: string | null | undefined) => boolean,
+): ((pluginId: string) => boolean) | undefined {
+ if (!SESSION_SCOPED_EVENTS.has(event)) return undefined;
+ return (pluginId: string) =>
+ pluginActiveInProject(
+ pluginId,
+ (sessionId && sessionProjects.get(sessionId)) ?? null,
+ );
+}
diff --git a/apps/desktop/electron/main/services/plugin-services.ts b/apps/desktop/electron/main/services/plugin-services.ts
index a989584f2..9fb135f36 100644
--- a/apps/desktop/electron/main/services/plugin-services.ts
+++ b/apps/desktop/electron/main/services/plugin-services.ts
@@ -48,6 +48,7 @@ import {
import { McpOAuthManager } from "../mcp-oauth";
import { PluginPanelHost } from "../plugin-panel-host";
import { PluginViewHost } from "../plugin-view-host";
+import { SESSION_SCOPED_EVENTS, sessionEventScopeFilter } from "../plugin-session-scope";
import { BrowserPane } from "../browser-view";
import { BrowserHost, BROWSER_PLUGIN_ID } from "../browser-host";
import { OAUTH_AUTH_KIND, type VendorOAuth } from "../oauth";
@@ -98,8 +99,6 @@ export type PluginServicesDependencies = {
* detached `ui.panel` window is application-scoped — it does not belong to a
* session — so it never receives them; plugin processes and docked views do.
*/
-const SESSION_SCOPED_EVENTS = new Set(["session:activated"]);
-
export function createPluginServices({
dataDir,
logger,
@@ -587,13 +586,12 @@ export function createPluginServices({
// ui.revealView) already enforces: a project-scoped plugin does not
// receive a session its scope excludes (#1754). Sessions without a
// project fall back to the workspace-relative check.
- const scopeFilter = SESSION_SCOPED_EVENTS.has(event)
- ? (pluginId: string) =>
- pluginActiveInProject(
- pluginId,
- (sessionId && sessionProjects.get(sessionId)) ?? null,
- )
- : undefined;
+ const scopeFilter = sessionEventScopeFilter(
+ event,
+ sessionId,
+ sessionProjects,
+ pluginActiveInProject,
+ );
try {
plugins.broadcastEvent(event, [payload], scopeFilter);
} catch (error) {
diff --git a/apps/desktop/test/plugin-services.test.mjs b/apps/desktop/test/plugin-services.test.mjs
index 1a1a86256..9a1ead662 100644
--- a/apps/desktop/test/plugin-services.test.mjs
+++ b/apps/desktop/test/plugin-services.test.mjs
@@ -522,3 +522,41 @@ test("capability badges include every declared plugin capability", () => {
"bus",
]);
});
+
+test("the session:activated scope filter derives from the announced session's project", async (t) => {
+ const { sessionEventScopeFilter, SESSION_SCOPED_EVENTS } = await import(
+ "../electron/main/plugin-session-scope.ts"
+ );
+ const sessionProjects = new Map([["s-a", "/work/a"]]);
+ const activeInProject = (pluginId, projectPath) =>
+ projectPath === "/work/a" && pluginId === "demo.in-scope";
+
+ // A selection event produces a filter that keys on the announced session's
+ // project: the in-scope plugin passes, another project's plugin does not.
+ const filter = sessionEventScopeFilter(
+ "session:activated",
+ "s-a",
+ sessionProjects,
+ activeInProject,
+ );
+ assert.equal(typeof filter, "function");
+ assert.equal(filter("demo.in-scope"), true);
+ assert.equal(filter("demo.out-of-scope"), false);
+
+ // A session without a project checks against null, not against whatever
+ // the map happens to hold for another session.
+ const noProject = sessionEventScopeFilter(
+ "session:activated",
+ "s-global",
+ sessionProjects,
+ activeInProject,
+ );
+ assert.equal(noProject("demo.in-scope"), false, "null project is passed through, not the map's value");
+
+ // Other session events fan out unconditionally.
+ assert.equal(
+ sessionEventScopeFilter("session:turnEnded", "s-a", sessionProjects, activeInProject),
+ undefined,
+ );
+ assert.ok(SESSION_SCOPED_EVENTS.has("session:activated"));
+});
diff --git a/apps/desktop/test/plugin-session-follow.test.mjs b/apps/desktop/test/plugin-session-follow.test.mjs
index a6c5ae338..39106521b 100644
--- a/apps/desktop/test/plugin-session-follow.test.mjs
+++ b/apps/desktop/test/plugin-session-follow.test.mjs
@@ -381,17 +381,29 @@ test("broadcastEvent's filter keeps an out-of-scope plugin from receiving the se
test("the session-push gate restarts with the renderer's boot id and the payload carries it", async () => {
const { readMainSource, readStoreModule } = await import("./helpers/source-contracts.mjs");
- const ipcSource = await readMainSource("ipc/plugin-ui-ipc.ts");
+ const { readFile } = await import("node:fs/promises");
+ const gateSource = await readFile(
+ new URL("../electron/main/plugin-session-push-gate.ts", import.meta.url),
+ "utf8",
+ );
- // Main (#1753): a changed boot id resets the high-water mark, so pushes
- // keep flowing after a renderer reload; within one boot the old
- // out-of-order guard still applies.
+ // The gate module owns the boot-id reset (#1753): a new renderer boot
+ // restarts the high-water mark, and within a boot the out-of-order guard
+ // still ignores older sequences.
assert.match(
- ipcSource,
- /const\s+bootId\s*=\s*typeof\s+payload\?\.bootId\s*===\s*"string"[\s\S]*?bootId\s*!==\s*activeSessionBootId[\s\S]*?activeSessionSeq\s*=\s*seq;[\s\S]*?return\s+\{\s*ok:\s*true,\s*ignored:\s*true\s*\};/,
+ gateSource,
+ /push\.bootId !== activeSessionBootId[\s\S]*?activeSessionSeq = push\.seq;[\s\S]*?return \{ ok: true, ignored: true \};/,
"the gate must reset the high-water mark on a new renderer boot and keep the out-of-order guard within a boot",
);
+ // The production handler gates every push through the shared gate.
+ const ipcSource = await readMainSource("ipc/plugin-ui-ipc.ts");
+ assert.match(
+ ipcSource,
+ /const sessionPushGate = createSessionPushGate\(\);[\s\S]*?sessionPushGate\(\{ seq, bootId \}\)\.ignored/,
+ "plugin-ui-ipc must route session pushes through the shared gate",
+ );
+
// Renderer: the push carries the boot id the gate compares.
const runtimeSource = await readStoreModule("../../src/features/app/useAppShellRuntime.tsx");
assert.match(
@@ -405,3 +417,31 @@ test("the session-push gate restarts with the renderer's boot id and the payload
"the push must carry the boot id",
);
});
+
+test("the session-push gate accepts a new renderer boot and still rejects an older sequence within it (#1753)", async (t) => {
+ const { createSessionPushGate } = await import(
+ "../electron/main/plugin-session-push-gate.ts"
+ );
+ const gate = createSessionPushGate();
+
+ // Within one boot: in-order accepted, out-of-order ignored.
+ assert.deepEqual(gate({ seq: 3, bootId: "boot-1" }), { ok: true });
+ assert.deepEqual(gate({ seq: 5, bootId: "boot-1" }), { ok: true });
+ assert.deepEqual(gate({ seq: 4, bootId: "boot-1" }), {
+ ok: true,
+ ignored: true,
+ });
+
+ // A reload restarts the renderer counter: the new boot resets the mark and
+ // the low sequences flow again.
+ assert.deepEqual(gate({ seq: 1, bootId: "boot-2" }), { ok: true });
+ assert.deepEqual(gate({ seq: 2, bootId: "boot-2" }), { ok: true });
+ assert.deepEqual(gate({ seq: 1, bootId: "boot-2" }), {
+ ok: true,
+ ignored: true,
+ });
+
+ // Zero-sequence pushes (no selection) never touch the mark.
+ assert.deepEqual(gate({ seq: 0, bootId: "boot-2" }), { ok: true });
+ assert.deepEqual(gate({ seq: 2, bootId: "boot-2" }), { ok: true });
+});
diff --git a/apps/desktop/test/plugin-work-panel-views.test.mjs b/apps/desktop/test/plugin-work-panel-views.test.mjs
index c802ac0f1..f659e1558 100644
--- a/apps/desktop/test/plugin-work-panel-views.test.mjs
+++ b/apps/desktop/test/plugin-work-panel-views.test.mjs
@@ -282,7 +282,7 @@ test("opening a different project refreshes the scope-filtered view list", () =>
});
test("host panel events reach docked views as well as detached windows", () => {
- assert.match(viewHostSource, /broadcast\(event: string, payload: unknown\)/);
+ assert.match(viewHostSource, /broadcast\(event: string, payload: unknown(?:, filter\?:[^)]*)?\)/);
assert.match(viewHostSource, /pi-plugin-panel-event:\$\{event\}/);
assert.match(mainSource, /function broadcastPluginPanelEvent/);
assert.match(mainSource, /broadcastPluginPanelEvent\("appearance:changed"/);