Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins
TypeScript ai-agentcoding-agentdesktop-appelectronglobali18nlocal-firstmcppipi-agentpi-desktoppluginsreactrusttypescript
fix(browser): bind delayed chrome actions to their source tab
zszz36b339b618d0f
diff --git a/apps/desktop/electron/main/browser-host.ts b/apps/desktop/electron/main/browser-host.tsindex 8c192d206..e3aa94e9a 100644--- a/apps/desktop/electron/main/browser-host.ts+++ b/apps/desktop/electron/main/browser-host.ts@@ -218,10 +218,14 @@ export class BrowserHost {}}- async navigate(input: BrowserNavigateInput, sessionId?: string): Promise<BrowserState | null> {+ async navigate(input: BrowserNavigateInput, sessionId?: string, tabId?: string): Promise<BrowserState | null> {const target = String(input.path ?? input.url ?? "").trim();if (!target) return this.getState();const id = sessionId?.trim() || this.active?.sessionId || "";+ if (tabId !== undefined) {+ const page = this.pages.get(this.key(id, tabId));+ return page ? this.navigatePage(page, target) : null;+ }if (!this.active) {this.active = this.pageFor(id, this.selectedTabs.get(id) ?? null);this.selectedTabs.set(id, this.active.tabId);@@ -265,16 +269,20 @@ export class BrowserHost {return state ? { ...state, ...this.contextFor(page) } : null;}- action(action: "back" | "forward" | "reload" | "stop"): void {- if (this.active?.state) this.active.pane.action(action);+ action(action: "back" | "forward" | "reload" | "stop", sessionId?: string, tabId?: string): void {+ const page = sessionId !== undefined && tabId !== undefined+ ? this.pages.get(this.key(sessionId, tabId)) : this.active;+ if (page?.state) page.pane.action(action);}getState(): BrowserState | null {return this.active?.state ? { ...this.active.state, ...this.getContext() } : null;}- openExternal(): void {- if (this.active?.started) this.active.pane.openExternal();+ openExternal(sessionId?: string, tabId?: string): void {+ const page = sessionId !== undefined && tabId !== undefined+ ? this.pages.get(this.key(sessionId, tabId)) : this.active;+ if (page?.started) page.pane.openExternal();}private currentPage(): BrowserPage {diff --git a/apps/desktop/electron/main/plugin-runtime.ts b/apps/desktop/electron/main/plugin-runtime.tsindex 89ac117eb..2ebaf26fa 100644--- a/apps/desktop/electron/main/plugin-runtime.ts+++ b/apps/desktop/electron/main/plugin-runtime.ts@@ -413,12 +413,13 @@ export type PluginHostServices = {navigate: (input: { url?: string; path?: string },sessionId?: string,+ tabId?: string,) => Promise<unknown>;- action: (action: "back" | "forward" | "reload" | "stop") => void;+ action: (action: "back" | "forward" | "reload" | "stop", sessionId?: string, tabId?: string) => void;setBounds: (pluginId: string, hole: unknown) => unknown;setVisible: (pluginId: string, visible: boolean) => void;getState: () => unknown;- openExternal: () => void;+ openExternal: (sessionId?: string, tabId?: string) => void;snapshot: () => Promise<unknown>;screenshot: (input?: { fullPage?: boolean },@@ -4122,7 +4123,9 @@ export class PluginRuntime {payload?: Record<string, unknown>,): Promise<unknown> {this.assertPermission(loaded, "browser.cdp");- const api = this.hostApi(loaded).browser;+ const context = loaded.manifest.id === "pi.browser" && typeof payload?.sessionId === "string" && typeof payload?.tabId === "string"+ ? { sessionId: payload.sessionId, tabId: payload.tabId } : undefined;+ const api = this.hostApi(loaded, context).browser;switch (method) {case "navigate":return api.navigate({@@ -4614,7 +4617,7 @@ export class PluginRuntime {throw apiError("UNSUPPORTED", `host api not available: ${api}`);}- private hostApi(loaded: LoadedPlugin) {+ private hostApi(loaded: LoadedPlugin, browserContext?: { sessionId: string; tabId: string }) {const pluginId = loaded.manifest.id;const pluginPath = loaded.path;@@ -5572,7 +5575,8 @@ export class PluginRuntime {}const result = await this.services.browser.navigate(input,- this.browserSessionId(pluginId),+ browserContext?.sessionId ?? this.browserSessionId(pluginId),+ browserContext?.tabId,);this.services.audit?.({pluginId,@@ -5594,7 +5598,7 @@ export class PluginRuntime {action === "reload" ||action === "stop") {- this.services.browser.action(action);+ this.services.browser.action(action, browserContext?.sessionId, browserContext?.tabId);}},setBounds: (hole: unknown) => {@@ -5624,7 +5628,7 @@ export class PluginRuntime {if (!this.services.browser) {throw apiError("UNAVAILABLE", "browser host missing");}- this.services.browser.openExternal();+ this.services.browser.openExternal(browserContext?.sessionId, browserContext?.tabId);this.services.audit?.({pluginId,api: "browser.openExternal",diff --git a/apps/desktop/electron/main/services/plugin-services.ts b/apps/desktop/electron/main/services/plugin-services.tsindex f866a732f..ac1776e87 100644--- a/apps/desktop/electron/main/services/plugin-services.ts+++ b/apps/desktop/electron/main/services/plugin-services.ts@@ -610,12 +610,12 @@ export function createPluginServices({agentExtensionsChanged: () =>sendToRenderer(IPC.event.pluginChanged, { reason: "agentExtensions" }),browser: {- navigate: (input, sessionId) => browserHost.navigate(input, sessionId),- action: (action) => browserHost.action(action),+ navigate: (input, sessionId, tabId) => browserHost.navigate(input, sessionId, tabId),+ action: (action, sessionId, tabId) => browserHost.action(action, sessionId, tabId),setBounds: (pluginId, hole) => browserHost.setGuestHole(pluginId, hole),setVisible: (pluginId, visible) => browserHost.setGuestVisible(pluginId, visible),getState: () => browserHost.getState(),- openExternal: () => browserHost.openExternal(),+ openExternal: (sessionId, tabId) => browserHost.openExternal(sessionId, tabId),snapshot: () => browserHost.snapshot(),screenshot: (input, sessionId) => browserHost.screenshot(input, sessionId),click: (uid) => browserHost.click(uid),diff --git a/apps/desktop/resources/plugins/pi.browser/views/browser.html b/apps/desktop/resources/plugins/pi.browser/views/browser.htmlindex 9de4e9a31..8fbc76edf 100644--- a/apps/desktop/resources/plugins/pi.browser/views/browser.html+++ b/apps/desktop/resources/plugins/pi.browser/views/browser.html@@ -192,6 +192,7 @@let navigationRequest = 0;let displayedContext = "";+ let navigationContext;const t = (key) => copy[locale]?.[key] ?? copy.en[key] ?? key;const applyCopy = () => {@@ -223,6 +224,10 @@? JSON.stringify([state.sessionId, state.tabId]) : displayedContext;const contextChanged = context !== displayedContext;displayedContext = context;+ if (contextChanged) {+ navigationRequest += 1;+ navigationContext = { sessionId: state.sessionId, tabId: state.tabId };+ }started = Boolean(state?.url) && !state?.loadError;empty.hidden = started && !state?.isLoading;const phase = state?.loadError === "ERR_ABORTED" ? "stopped" : state?.loadError ? "failed" : state?.isLoading ? "loading" : "empty";@@ -256,18 +261,18 @@if (!value) return;const request = ++navigationRequest;paint({ url: value, isLoading: true });- void bridge.invoke("browser.navigate", { url: value }).then((state) => {+ void bridge.invoke("browser.navigate", { url: value, ...navigationContext }).then((state) => {if (request === navigationRequest && state) paint(state);}).catch(() => {if (request === navigationRequest) paint({ url: value, loadError: "navigation failed" });});});- back.addEventListener("click", () => void bridge.invoke("browser.action", { action: "back" }));- forward.addEventListener("click", () => void bridge.invoke("browser.action", { action: "forward" }));+ back.addEventListener("click", () => void bridge.invoke("browser.action", { action: "back", ...navigationContext }));+ forward.addEventListener("click", () => void bridge.invoke("browser.action", { action: "forward", ...navigationContext }));reload.addEventListener("click", () => {- void bridge.invoke("browser.action", { action: reload.title === t("stop") ? "stop" : "reload" });+ void bridge.invoke("browser.action", { action: reload.title === t("stop") ? "stop" : "reload", ...navigationContext });});- external.addEventListener("click", () => void bridge.invoke("browser.openExternal"));+ external.addEventListener("click", () => void bridge.invoke("browser.openExternal", navigationContext));input.addEventListener("focus", () => {inputFocused = true;input.select();diff --git a/apps/desktop/test/browser-host-session.test.mjs b/apps/desktop/test/browser-host-session.test.mjsindex 2d41bdf6b..f67263293 100644--- a/apps/desktop/test/browser-host-session.test.mjs+++ b/apps/desktop/test/browser-host-session.test.mjs@@ -131,6 +131,12 @@ test("same-tab navigation supersedes an older request and retains sibling pages"oldRoot.resolve("/projects/A"); await older;assert.equal(h.loads.length, 2);assert.equal(h.host.getState().url, "https://fixture.invalid/newer");+ await open(h, "B", "b", "B.html");+ const fromOldChrome = h.host.navigate({ url: "https://fixture.invalid/a-late" }, "A", "a");+ h.roots.at(-1).resolve("/projects/A"); await settled(); h.loads.at(-1).finish(); await fromOldChrome;+ assert.equal(h.host.getState().url, "B.html");+ h.host.setChromeSession("A", "a");+ assert.equal(h.host.getState().url, "https://fixture.invalid/a-late");});test("closing the panel keeps a pending page hidden until the panel returns", async () => {diff --git a/docs/adr/retained-browser-pages-per-tab.md b/docs/adr/retained-browser-pages-per-tab.mdindex 6b7cf9605..43a44485e 100644--- a/docs/adr/retained-browser-pages-per-tab.md+++ b/docs/adr/retained-browser-pages-per-tab.md@@ -24,7 +24,10 @@ BrowserPreview validates the request and asks the renderer to open a new resourctab. Only that tab starts its navigation. Background browser.navigate records anintent for the session's last selected tab; before a session has a tab, its firsttab consumes that intent once. Older load completion cannot overwrite an intent.-An empty tab has no inherited native page state.+An empty tab has no inherited native page state. Bundled browser chrome tags+its navigation/action requests with the owning session/tab; a delayed reply+cannot repaint another tab. Agent-facing methods retain runtime-owned session+identity rather than accepting these UI routing fields.Closing a tab releases its page and CDP state. Session deletion, plugin disposal,renderer reload, window destruction and application shutdown release the corresponding pages.