Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
docs(browser): 澄清常驻宿主与停车区的生命周期契约
shenjiacheng965975871554
diff --git a/apps/desktop/src/renderer/features/right-sidebar/hooks/useBrowserWebview.ts b/apps/desktop/src/renderer/features/right-sidebar/hooks/useBrowserWebview.tsindex fd2e8ca8a5..7cb99ff80f 100644--- a/apps/desktop/src/renderer/features/right-sidebar/hooks/useBrowserWebview.ts+++ b/apps/desktop/src/renderer/features/right-sidebar/hooks/useBrowserWebview.ts@@ -4,8 +4,9 @@* 给定 tabId,从模块级 BrowserWebviewPool 取 / 创建对应的 wrapper + webview 实例,* 返回:* - `wrapper`: webview 外层 DOM 节点;caller 应在自己的 body slot 里用- * `useLayoutEffect` + `slot.appendChild(wrapper)` 挂上,return cleanup 时- * 把 wrapper 挪回 pool 的停车区(`pool.acquire().wrapper.parentNode = pool.container`)。+ * `useLayoutEffect` + `slot.appendChild(wrapper)` 首次附着。切换时保持宿主+ * 常驻、wrapper 留在原 slot;真正卸载时仅把仍归 pool 的 wrapper 停回+ * 停车区暂存 DOM,不能以此保证 guest / 页面状态保留。* - 命令式 actions:`navigate / reload / goBack / goForward / stop`* - 反应式 state:`url / title / favicon / isLoading / canGoBack / canGoForward`,* 变更触发 React re-render@@ -57,7 +58,7 @@ export interface UseBrowserWebviewResult {/** webview 外层 wrapper DOM;caller appendChild 到自己的 body slot。 */wrapper: HTMLDivElement | null;/**- * 当前 Pool entry 的 WebView 代际句柄。隐藏但仍由 Pool 保活时保持非空;+ * 当前 Pool entry 的 WebView DOM 代际句柄。隐藏且 Pool 仍持有 entry 时非空;* entry 被淘汰 / 释放时变 null,重建后变为新的对象。依赖 guest 事件或* 通信的功能必须以它为 effect dependency,不能只按 tabId 绑定。*/@@ -429,8 +430,9 @@ export function useBrowserWebview(entry.webview.removeEventListener('unresponsive', onUnresponsive);entry.webview.removeEventListener('responsive', onResponsive);unsubResourceEvent();- // **不**释放 pool entry —— webview DOM 节点继续保活,切回该 tab 时可直接- // 复用。释放是 plugin 在用户主动关闭 tab 时显式调 pool.release(tabId)。+ // 不在监听器 cleanup 中释放 pool entry。DOM 仍可复用,但 guest 状态+ // 只有宿主/祖先不被移走时才能保留;真正卸载停回 parking 不提供该保证。+ // 释放仍由关闭、LRU 或宿主交接等 pool 生命周期路径负责。};}, [enabled, tabId, sessionId, setObservedUrl, entryEpoch, visible]);diff --git a/apps/desktop/src/renderer/features/right-sidebar/lib/browserWebviewPool.ts b/apps/desktop/src/renderer/features/right-sidebar/lib/browserWebviewPool.tsindex 60cafaca91..17536988a8 100644--- a/apps/desktop/src/renderer/features/right-sidebar/lib/browserWebviewPool.ts+++ b/apps/desktop/src/renderer/features/right-sidebar/lib/browserWebviewPool.ts@@ -2,12 +2,10 @@* BrowserWebviewPool —— RSB web-browser plugin 的 webview 实例池(LRU, K=5)。** 为什么要 pool:每个 web-browser tab 在 DOM 上有自己的 body slot,但 `<webview>`- * 一旦被 React unmount(切到别的 tab / 切 RSB 整体卸载/重挂),guest webContents- * 会被 detach + 销毁,下次切回来要重新加载,Cookie / form state / 浏览历史全丢。- * 业界做法:把 webview DOM 节点保活在脱离当前 tab 子树的"停车区" container 里,- * 首次物化时把 wrapper 放进目标 slot,之后由 Shell 保持仍存活的浏览器 body- * 挂载。跨父节点搬动即使用 vanilla DOM 也会重建 Electron guest;池里保留同一个- * DOM 对象不等于保留页面运行上下文,正常切换不能经过停车区。+ * 被 React unmount 或跨父节点搬动时,guest webContents 会被销毁/重建,+ * 未提交表单、页面内存和浏览历史不能靠复用 DOM 对象保留。池用"停车区"+ * 暂存没有 body slot 的 wrapper;已附着页面的 guest 保留依靠 Shell 让宿主+ * 常驻并保持祖先位置稳定。正常切换只隐藏宿主,不能经过停车区。** **关键:不要用 React Portal**。Portal 内部跨 reconcile 的搬动会触发 React 把* 节点 remove + insert,这中间一帧会让 webview detach 丢 webContents。所以@@ -20,7 +18,7 @@** 生命周期:* - acquire(tabId):取/创建 entry,LRU 标 touch,超 K 淘汰最旧- * - getWrapper(tabId):返回 wrapper DOM(caller appendChild 到自己 slot)+ * - acquire / peek 的 wrapper:首次附着到 body slot,切换时留在原 slot* - touch(tabId):tab 被激活时显式触发 LRU 时间戳更新* - release(tabId):tab 关闭时显式销毁*@@ -39,8 +37,8 @@ export type BrowserWebviewGuestFailure =interface PoolEntry {tabId: string;- /** webview 的外层 wrapper 容器,LRU 在它身上(caller 把它 appendChild 到自己- * slot;切走时 caller 应把它 appendChild 回 pool container)。 */+ /** webview 的外层 wrapper。首次附着到 caller slot 后,切换只隐藏常驻宿主。+ * 真正卸载时可停回 pool container 暂存 DOM,但跨父节点移动不保留 guest。 */wrapper: HTMLDivElement;/** 真 `<webview>` 元素,挂在 wrapper 内部。 */webview: WebviewTag;@@ -74,8 +72,8 @@ class BrowserWebviewPoolImpl {/*** pool 的"停车区" off-screen 容器 —— 没挂到任何 tab body slot 的 wrapper- * 暂存在这里。位置 `left:-9999px;top:0;` 让浏览器知道节点 still attached(- * webview 才会保活),但用户看不见。+ * 暂存在这里。位置 `left:-9999px;top:0;` 让节点接入 document 但用户看不见,+ * 供初始 guest 物化;移入/移出这里不保证原 guest 或页面状态保留。** lazy 创建:首次 acquire 时才挂到 document.body。*/@@ -88,10 +86,8 @@ class BrowserWebviewPoolImpl {}const div = document.createElement('div');div.id = POOL_CONTAINER_ID;- // off-screen but attached:webview webContents 只有 host wrapper attached 到- // document 的 render tree 才不被销毁。`position: fixed; left: -9999px` 是- // 标准的 off-screen 技巧,visibility: hidden 会触发部分 webview 实现把- // webContents 暂停 paint,不用。+ // off-screen but attached:供尚无 body slot 的 wrapper 物化 guest。+ // 接入 document 不代表跨父节点搬动能保留同一个 webContents。div.style.cssText = 'position:fixed;left:-9999px;top:0;width:1px;height:1px;pointer-events:none;';document.body.appendChild(div);this.container = div;diff --git a/apps/desktop/src/renderer/features/right-sidebar/lib/rsbBrowserBridge.ts b/apps/desktop/src/renderer/features/right-sidebar/lib/rsbBrowserBridge.tsindex 5a537db06a..c1ed9d6215 100644--- a/apps/desktop/src/renderer/features/right-sidebar/lib/rsbBrowserBridge.ts+++ b/apps/desktop/src/renderer/features/right-sidebar/lib/rsbBrowserBridge.ts@@ -468,35 +468,29 @@ async function handleTabOpRequest(const newTab = await addTab(req.sessionId, 'web-browser', initialState);// ── Eager webview spawn ─────────────────────────────────────────────- // The tab is now in the store, but `<webview>` only exists once- // `BrowserTabBody` mounts — which doesn't happen until the user is- // looking at this session. If the agent operates on a session OTHER- // than the currently-focused one (cross-session automation), the- // webview never comes into being, no webContentsId is ever reported- // to main, and the very next `backend.navigate` / `evaluate` fails- // with "tab not found".+ // A newly added background tab may not have a BrowserTabBody yet.+ // Without eager materialization, cross-session automation can reach+ // backend.navigate / evaluate before main has a webContentsId.//// Fix: synchronously acquire the pool entry NOW. That creates the// <webview> DOM node inside the off-screen parking container (already// attached to document.body — see browserWebviewPool.ensureContainer),// which is enough for Electron to create the guest webContents.- // Setting `src` kicks off navigation. We wait for `dom-ready`, report- // (sessionId, tabId, webContentsId) to main, and ONLY THEN ack the- // tab-op back to the backend — so by the time the agent's next call- // arrives, the registry has the mapping.+ // Setting `src` kicks off navigation. did-attach / dom-ready attempt+ // to report the guest id; completion also has an 8s timeout. Reports+ // are best-effort, so completion does not guarantee a registry entry.//// When the user later switches into this session, BrowserTabBody- // mounts and `useBrowserWebview` acquires the SAME pool entry (key- // is tabId) — the loaded page is preserved verbatim (the entire- // point of the pool's parking-region design, see browserWebviewPool- // line 4-13). Its own dom-ready listener won't re-fire (the event- // already passed), but main already has the registry entry, and- // BrowserTabBody.tsx's initial-URL-read code (line 175-181 of- // useBrowserWebview) syncs the displayed URL from `webview.getURL()`.+ // mounts and useBrowserWebview may reuse the same pool DOM entry.+ // Moving its wrapper from parking into the slot can recreate the+ // guest and lose page state; the hook observes attach / dom-ready+ // and reports the current id. Retaining already-mounted hosts avoids+ // reparenting on task switches; it does not cover this first mount+ // of an eager-spawned background tab or promise its guest survives.await eagerSpawnAndReport(req.sessionId, newTab.id, initialState.url);// Visibility command runs AFTER the eager-spawn so that by the time- // MainLayout reads the new collapsed-state archive (when user later- // switches to this session), the webview is already prepared.+ // MainLayout reads the new collapsed-state archive, the initial+ // materialization attempt has finished (possibly via timeout).// userInitiated:false —— agent 自动化开的标签,不是用户点的:侧边栏该展开// 就展开,但 detached 子窗口不得抢走用户当前前台应用的焦点。requestRightSidebarVisibility('open', { sessionId: req.sessionId, userInitiated: false });@@ -587,14 +581,13 @@ function pickDefaultBrowserTab(}/**- * open / ensure 共用的 webview 物化:acquire pool entry → setAttribute('src')- * → 等 dom-ready → report (sessionId, tabId, webContentsId) 给 main。+ * open / ensure 共用的 webview 物化:acquire pool entry,已 attach 时尝试重报;+ * 否则 setAttribute('src'),在 did-attach / dom-ready 尝试 report 给 main。*- * 为什么要 eager:tab 在 store 里 ≠ `<webview>` 存在 —— `BrowserTabBody` 只在- * 用户看着这个 session 时才 mount。agent 跨 session 自动化时 webview 永远不会- * 自然出生,main 的 registry 拿不到 webContentsId,下一个 backend 动作就会报- * "tab not found"。所以在停车区容器里同步 acquire(创建 guest webContents),- * report 完成后才让调用方 ack —— backend 下一次解析时 registry 已有映射。+ * 为什么要 eager:store 中新建的后台 tab 不一定已有 BrowserTabBody,需先+ * 在停车区物化,避免自动化动作直接遇到 "tab not found"。上报是 best-effort,+ * 等待也有超时;返回不保证 main 已登记。随后首次附着到 body slot 可能重建+ * guest,不能把停车区的 DOM 复用当作页面状态保留;该路径未由常驻宿主策略覆盖。** 已 attach 过的 entry(getWebContentsId 可取)走幂等 re-report 快路径,* 不再动 src(避免把用户正在看的页面重新导航)。diff --git a/apps/desktop/src/renderer/features/right-sidebar/plugins/web-browser/BrowserTabBody.tsx b/apps/desktop/src/renderer/features/right-sidebar/plugins/web-browser/BrowserTabBody.tsxindex f87e06ae23..8e7f2aaad5 100644--- a/apps/desktop/src/renderer/features/right-sidebar/plugins/web-browser/BrowserTabBody.tsx+++ b/apps/desktop/src/renderer/features/right-sidebar/plugins/web-browser/BrowserTabBody.tsx@@ -13,8 +13,9 @@ import { Button } from '@/components/ui/button';** webview slot 是一个 `<div ref>` —— useBrowserWebview 给我们 wrapper(模块级* pool 持有的同一个 DOM 节点),useLayoutEffect 里用 vanilla `appendChild` 把- * wrapper 搬过来。TabBody 卸载时 cleanup 把 wrapper 挪回 pool container(- * off-screen),保证 webContents 不被销毁。+ * wrapper 首次附着到 slot。之后由 Shell 保持池内存活浏览器的宿主与祖先位置,+ * 切换只隐藏宿主。真正卸载时 cleanup 把仍归 pool 的 wrapper 停回 container+ * 暂存 DOM;这次跨父节点移动可能重建 guest,不保证页面运行上下文保留。** webview 事件 → 同步 plugin state:* - did-navigate / did-navigate-in-page → state.url@@ -137,8 +138,8 @@ export function BrowserTabBody({ state, ctx, active, shellVisible }: BrowserTabB});// 把 pool 的 wrapper 挂进 slot —— useLayoutEffect(在 paint 前移 DOM,避免闪)。- // Shell 会常驻挂载所有 TabBody,并用父节点 display:none 隐藏后台 tab;wrapper- // 一旦进入自己的 slot 就不要再随 active 切换搬回 parking。Electron 的 webview+ // Shell 常驻挂载池内存活的浏览器 TabBody,后台仅隐藏;wrapper 一旦进入+ // 自己的 slot 就不要再随 active 切换搬回 parking。Electron 的 webview// 在跨父节点 reparent 时可能重建 guest WebContents,这会销毁 window.open 的// opener browsing context,连带 outlivesOpener:false 的原生 popup 一起销毁。// 只有 TabBody 真正卸载或 wrapper 代际变化时 effect cleanup 才停回 parking。