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: 清理关闭浏览器标签后的工具连接
geng yue93bee3bb26cc
diff --git a/apps/desktop/electron/main/browser-host.ts b/apps/desktop/electron/main/browser-host.tsindex 50fb18e89..57c9b7708 100644--- a/apps/desktop/electron/main/browser-host.ts+++ b/apps/desktop/electron/main/browser-host.ts@@ -595,6 +595,7 @@ export class BrowserHost {}this.locations.delete(key);if (this.selectedTabs.get(sessionId) === tabId) this.selectedTabs.delete(sessionId);+ if (this.connectedTabs.get(sessionId) === tabId) this.connectedTabs.delete(sessionId);}closeSession(sessionId: string): void {diff --git a/apps/desktop/test/browser-background-session.test.mjs b/apps/desktop/test/browser-background-session.test.mjsindex f9104043d..cc4e426db 100644--- a/apps/desktop/test/browser-background-session.test.mjs+++ b/apps/desktop/test/browser-background-session.test.mjs@@ -226,9 +226,45 @@ test("an established connection stays on its page when the user views another taassert.equal(second.wc.clicks, 0);assert.equal(h.host.getState().tabId, "second");h.host.closeTab("A", "first");+ assert.equal((await h.host.snapshot("A")).url, "https://fixture.invalid/second");+ await h.host.click("e1", "A");+ assert.equal(second.wc.clicks, 1);+ await assert.rejects(h.host.snapshot("A", "first"), { code: "UNAVAILABLE" });+});++test("closing a preview lets tools connect to a newly opened visible tab", async (t) => {+ const h = harness();+ t.after(() => h.host.dispose());+ const preview = h.host.openTab({ url: "https://fixture.invalid/preview" }, "A");+ h.roots.at(-1).resolve(["/projects/A"]);+ await settled();+ h.loads.at(-1).finish();+ const { tabId } = await preview;+ h.host.closeTab("A", tabId);+ const visible = await open(h, "A", "replacement", "https://fixture.invalid/replacement");+ assert.equal((await h.host.snapshot("A")).url, "https://fixture.invalid/replacement");+ await h.host.fill("e1", "replacement draft", "A");+ assert.equal(visible.wc.value, "replacement draft");+ const navigation = h.host.navigate({ url: "https://fixture.invalid/next" }, "A");+ h.roots.at(-1).resolve(["/projects/A"]);+ await settled();+ h.loads.at(-1).finish();+ assert.equal((await navigation).tabId, "replacement");+ assert.deepEqual(h.host.getPages("A").map((page) => page.tabId), ["replacement"]);+ h.host.closeTab("A", "replacement");await assert.rejects(h.host.snapshot("A"), { code: "UNAVAILABLE" });});+test("closing an unconnected tab preserves the established connection", async (t) => {+ const h = harness();+ t.after(() => h.host.dispose());+ await open(h, "A", "connected", "https://fixture.invalid/connected");+ await h.host.snapshot("A");+ await open(h, "A", "other", "https://fixture.invalid/other");+ h.host.closeTab("A", "other");+ assert.equal((await h.host.snapshot("A")).url, "https://fixture.invalid/connected");+});+test("background page metadata never repaints the visible browser chrome", async (t) => {const h = harness();t.after(() => h.host.dispose());diff --git a/docs/spec/04-ux/08-component-spec.md b/docs/spec/04-ux/08-component-spec.mdindex 5ec7b2ca2..a53c6a3a4 100644--- a/docs/spec/04-ux/08-component-spec.md+++ b/docs/spec/04-ux/08-component-spec.md@@ -4741,3 +4741,7 @@ introducing nested vertical scrolling. Unsupportedhistorical context has an explicit explanation and retains recorded evidence.Remote Review retains its existing presentation. Chat inline review rows retaintheir default layout and hash-guarded rollback; no Git or model behavior changes.++### Issue #2162 correction++Closing the connected Browser tab clears its connection. Subsequent session-only tools use that session's selected replacement page. Explicit closed targets fail; navigation never revives an invisible old tab. Verify close/reopen/snapshot/fill/click/navigate and final closure with `browser-background-session.test.mjs` and `BROWSER_CLOSE_CONNECTION_ONLY=1 node scripts/e2e-browser-background.mjs`.diff --git a/docs/spec/06-delivery/04-e2e-test-plan.md b/docs/spec/06-delivery/04-e2e-test-plan.mdindex 65cb9b8cc..b976987ba 100644--- a/docs/spec/06-delivery/04-e2e-test-plan.md+++ b/docs/spec/06-delivery/04-e2e-test-plan.md@@ -20341,3 +20341,7 @@ Scenario: E2E-REVIEW-ROUNDS (local desktop, implemented)Existing Windows child/grandchild cleanup tests remain required; record nativefailures separately from protocol success.- Exercise a late stop with an already reaped child before the supervisor drains the pipes. Verify the original exit outcome, final output and stable elapsed time across reads. Stall capability discovery or the first Host observation to exercise the overall wait deadline. The Electron transcript fixture renders a pending Bash stop and a mixed job list.++#### E2E-ISSUE-2162++Closing the connected Browser tab clears its connection. Subsequent session-only tools use that session's selected replacement page. Explicit closed targets fail; navigation never revives an invisible old tab. Verify close/reopen/snapshot/fill/click/navigate and final closure with `browser-background-session.test.mjs` and `BROWSER_CLOSE_CONNECTION_ONLY=1 node scripts/e2e-browser-background.mjs`.diff --git a/docs/zh-CN/spec/04-ux/08-component-spec.md b/docs/zh-CN/spec/04-ux/08-component-spec.mdindex 4e7af8b91..fdb1ae612 100644--- a/docs/zh-CN/spec/04-ux/08-component-spec.md+++ b/docs/zh-CN/spec/04-ux/08-component-spec.md@@ -3098,3 +3098,7 @@ default nor provider configuration. OAuth accounts remain in their separate sect项目删除说明和插件弹窗标题、安装结果中的长名称也在现有宽度内换行。项目指令、项目记忆和 OAuth 弹窗维持已有的边界处理。++### Issue #2162 修正++关闭已连接浏览器标签时清除连接;会话级工具使用同一会话选中的替代页,显式已关闭目标仍失败,导航不复活不可见旧标签。验证关闭、重开、snapshot、fill、click、navigate 和最终关闭。diff --git a/docs/zh-CN/spec/06-delivery/04-e2e-test-plan.md b/docs/zh-CN/spec/06-delivery/04-e2e-test-plan.mdindex eeb95cae6..c9b8ce5ab 100644--- a/docs/zh-CN/spec/06-delivery/04-e2e-test-plan.md+++ b/docs/zh-CN/spec/06-delivery/04-e2e-test-plan.md@@ -10390,3 +10390,7 @@ Electron 一侧——工具自动签发、指示器、"停止"中止轮次、绘- runtime 测试补充新输入和父回合取消、延迟 RPC 错误、旧 Host/远程能力缺失、混合选择器拒绝、原委派控制及无子代理配置的工具注册。- Host 测试覆盖先完成后等待、重复读取、Unicode 截断、归属查找、幂等停止与停止请求待确认。Windows 子孙进程清理测试仍须执行,原生失败单独报告。- 在子进程已退出、监管任务尚未排空管道时请求停止,检查原退出结果、最终输出和重复读取的耗时。延迟能力探测或第一次 Host 观察,检查等待总时限。Electron 会话记录用例渲染尚待确认的 Bash 停止请求和混合作业列表。++#### E2E-ISSUE-2162++关闭已连接浏览器标签时清除连接;会话级工具使用同一会话选中的替代页,显式已关闭目标仍失败,导航不复活不可见旧标签。验证关闭、重开、snapshot、fill、click、navigate 和最终关闭。diff --git a/scripts/e2e/browser-background-session.ts b/scripts/e2e/browser-background-session.tsindex eefc90433..418a2788c 100644--- a/scripts/e2e/browser-background-session.ts+++ b/scripts/e2e/browser-background-session.ts@@ -63,6 +63,28 @@ async function probe(output: string) {const a = panes.at(-1)!;const aId = a.getWebContents()!.id;const snapshot = await host.snapshot("A");+ if (process.env.BROWSER_CLOSE_CONNECTION_ONLY === "1") {+ host.closeTab("A", "a");+ host.setChromeSession("A", "replacement");+ await host.navigate({ url: origin + "/c" }, "A", "replacement");+ const replacement = await host.snapshot("A");+ assert.equal(replacement.title, "Page C");+ const input = replacement.tree.match(/- (e\d+) textbox "Note C"/)?.[1];+ const button = replacement.tree.match(/- (e\d+) button "Increment C"/)?.[1];+ assert(input && button, replacement.tree);+ await host.fill(input, "Replacement draft", "A");+ await host.click(button, "A");+ assert.equal(await host.evaluate("window.count", "A"), 1);+ assert.equal(await host.evaluate("document.querySelector('#note').value", "A"), "Replacement draft");+ assert.equal((await host.navigate({ url: origin + "/a" }, "A"))?.tabId, "replacement");+ assert.deepEqual(host.getPages("A").map((page) => page.tabId), ["replacement"]);+ await assert.rejects(host.snapshot("A", "a"), { code: "UNAVAILABLE" });+ assert.deepEqual(reveals, []);+ host.closeTab("A", "replacement");+ await assert.rejects(host.snapshot("A"), { code: "UNAVAILABLE" });+ console.log("BROWSER_BACKGROUND " + JSON.stringify({ ok: true, scope: "close-connection", passed: ["close, reopen, snapshot, fill, click, navigate, explicit closed target, final close"] }));+ return;+ }host.setChromeSession("B", "b");await host.navigate({ url: origin + "/b" }, "B");const bId = panes.at(-1)!.getWebContents()!.id;@@ -113,6 +135,18 @@ async function probe(output: string) {assert.equal((await host.snapshot("A")).title, "Page A");assert.deepEqual(reveals, ["C"]);passed.push("unknown and closed sessions cannot fall back to another page");++ host.closeTab("A", "a");+ host.setChromeSession("A", "replacement");+ await host.navigate({ url: origin + "/c" }, "A", "replacement");+ assert.equal((await host.snapshot("A")).title, "Page C");+ await host.evaluate("document.querySelector('#note').value = 'Replacement draft'", "A");+ assert.equal(await host.evaluate("document.querySelector('#note').value", "A"), "Replacement draft");+ assert.equal((await host.navigate({ url: origin + "/a" }, "A"))?.tabId, "replacement");+ assert.deepEqual(host.getPages("A").map((page) => page.tabId), ["replacement"]);+ await assert.rejects(host.snapshot("A", "a"), { code: "UNAVAILABLE" });+ assert.deepEqual(reveals, ["C"]);+ passed.push("closed connections yield to the replacement page without creating invisible tabs");mkdirSync(output, { recursive: true });writeFileSync(join(output, "result.json"), JSON.stringify({ ok: true, passed }, null, 2));console.log("BROWSER_BACKGROUND " + JSON.stringify({ ok: true, passed }));