跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

提交记录
修改internal/cage/browser-clipboard.mjs+5 −1看文件
@@ -579,7 +579,7 @@ export class BrowserClipboard {
579579 ) {
580580 void this.attach(p.sessionId, state.tabId).catch(() => {});
581581 } else if (event.method === "Target.detachedFromTarget") {
582删除: this.sessions.delete(p.sessionId);
582新增: this.detachSession(p.sessionId);
583583 } else if (event.method === "Runtime.executionContextCreated") {
584584 if (p.context?.auxData?.isDefault)
585585 state.contexts.set(p.context.id, p.context.auxData.frameId);
@@ -625,6 +625,10 @@ export class BrowserClipboard {
625625 }
626626 }
627627
628新增: detachSession(id) {
629新增: this.sessions.delete(id);
630新增: }
631新增:
628632 detachTab(id) {
629633 this.copyContexts.delete(id);
630634 for (const [session, state] of this.sessions)
修改scripts/guest-os/tests/windows-browser.test.mjs+33 −0看文件
@@ -111,6 +111,39 @@ integration("interactive browser identity is native in normal and private docume
111111 assert.equal(f.launchOptions().chromiumSandbox, true);
112112});
113113
114新增:integration("closed browser tabs release their controller sessions", async t => {
115新增: const f = await fixture(t);
116新增: const original = (await f.call("health", {}, "GET")).resources;
117新增: for (let index = 0; index < 12; index++) {
118新增: const { id, page } = await f.open("/lifecycle-" + index);
119新增: await f.call("close", { id });
120新增: assert.equal(page.isClosed(), true);
121新增: assert.deepEqual((await f.call("health", {}, "GET")).resources, original, "closed tabs must release their sessions");
122新增: }
123新增:});
124新增:
125新增:integration("removed browser frames release clipboard and controller sessions", async t => {
126新增: const f = await fixture(t);
127新增: const { id, page } = await f.open("/frame-lifecycle");
128新增: const original = (await f.call("health", {}, "GET")).resources;
129新增: for (let index = 0; index < 5; index++) {
130新增: await page.evaluate(async url => {
131新增: const frame = document.createElement("iframe");
132新增: frame.src = url;
133新增: const loaded = new Promise(resolve => { frame.onload = resolve; });
134新增: document.body.append(frame);
135新增: await loaded;
136新增: }, f.url.replace("127.0.0.1", "localhost") + "/child-" + index);
137新增: await page.waitForTimeout(100);
138新增: const attached = (await f.call("health", {}, "GET")).resources;
139新增: assert.ok(attached.sessions > original.sessions);
140新增: await page.evaluate(() => document.querySelector("iframe").remove());
141新增: await page.waitForTimeout(100);
142新增: assert.deepEqual((await f.call("health", {}, "GET")).resources, original, "detached frames must release clipboard state");
143新增: await f.call("copy-context", { id, context: "a".repeat(32) });
144新增: }
145新增:});
146新增:
114147integration("actual Chromium tabs restore and project switching retains unsaved editor buffers", async t => {
115148 const f = await fixture(t);
116149 const one = await f.open("/one"), two = await f.open("/two");
修改scripts/guest-os/windows-browser.mjs+35 −7看文件
@@ -360,25 +360,47 @@ export function createNativeBrowserService(options = {}) {
360360 if (method === "Target.setAutoAttach") return {};
361361 return session.send(method, params);
362362 }
363新增: function detachFrame(tab, frame, expected) {
364新增: const id = tab.frameSessions.get(frame);
365新增: if (!id || expected && expected !== id) return;
366新增: tab.frameSessions.delete(frame);
367新增: frameSessions.delete(frame);
368新增: const connection = sessions.get(id);
369新增: sessions.delete(id);
370新增: clipboard.detachSession(id);
371新增: // Explicit detach also releases Playwright's transport and event listeners.
372新增: void connection?.detach().catch(() => {});
373新增: }
374新增:
363375 async function attachFrame(tab, frame) {
364376 if (frameSessions.has(frame) || frame.isDetached()) return;
377新增: const id = token();
378新增: // Navigation and frameattached can race while newCDPSession is pending.
379新增: frameSessions.set(frame, id);
380新增: tab.frameSessions.set(frame, id);
365381 try {
366删除: const connection = await tab.page.context().newCDPSession(frame), id = token();
367删除: frameSessions.set(frame, id); sessions.set(id, connection);
382新增: const connection = await tab.page.context().newCDPSession(frame);
383新增: if (tab.page.isClosed() || frame.isDetached() || frameSessions.get(frame) !== id) {
384新增: detachFrame(tab, frame, id);
385新增: await connection.detach().catch(() => {});
386新增: return;
387新增: }
388新增: sessions.set(id, connection);
368389 for (const method of ["Runtime.executionContextCreated", "Runtime.executionContextDestroyed", "Runtime.executionContextsCleared", "Runtime.bindingCalled", "Page.frameDetached", "Page.frameNavigated"]) {
369390 connection.on(method, params => clipboard.event({ sessionId: id, method, params }));
370391 }
371392 await clipboard.attach(id, tab.id);
372393 if (tab.copyContext) await clipboard.setContext(tab.id, tab.copyContext);
373删除: } catch { /* A navigation can destroy a frame while its session attaches. */ }
394新增: } catch { detachFrame(tab, frame, id); }
374395 }
375396 async function addPage(page, { id = token(), incognito = false, login = false, kind = "web", workspace, blocked = false } = {}) {
376397 if (byPage.has(page)) { const existing = byPage.get(page); await existing.ready; return existing; }
377398 if (tabs.size >= 100) { await page.close(); throw failure("tab_limit", "打开的标签页过多,请先关闭部分标签页"); }
378删除: const tab = { id, page, incognito, login, kind, workspace, blocked, document: token(), geometry: token(), title: "", copyContext: "" };
399新增: const tab = { id, page, incognito, login, kind, workspace, blocked, document: token(), geometry: token(), title: "", copyContext: "", frameSessions: new Map() };
379400 tabs.set(id, tab); byPage.set(page, tab);
380401 page.on("close", () => {
381402 void stopCapture(page);
403新增: for (const frame of tab.frameSessions.keys()) detachFrame(tab, frame);
382404 tabs.delete(id); clipboard?.detachTab(id); uploads?.cancelTab(id);
383405 if (active === id) invalidateCopies();
384406 if (vault) for (const [sid, entry] of vault.sessions) if (entry.tab === id) vault.sessions.delete(sid);
@@ -411,17 +433,19 @@ export function createNativeBrowserService(options = {}) {
411433 });
412434 page.on("frameattached", frame => { void attachFrame(tab, frame); });
413435 page.on("domcontentloaded", changed);
414删除: page.on("framedetached", frame => { const sid = frameSessions.get(frame); sessions.delete(sid); });
436新增: page.on("framedetached", frame => detachFrame(tab, frame));
415437 tab.ready = (async () => {
416438 // A persistent context keeps its launch viewport for newly opened pages.
417439 // Each tab must join the workbench's current viewport before capture starts.
418440 await page.setViewportSize(viewport);
419441 const connection = await page.context().newCDPSession(page), sid = token();
442新增: if (page.isClosed()) { await connection.detach().catch(() => {}); return; }
443新增: tab.session = sid; sessions.set(sid, connection); frameSessions.set(page.mainFrame(), sid);
444新增: tab.frameSessions.set(page.mainFrame(), sid);
420445 // Playwright enables focus emulation for every page. Native input proofs
421446 // must observe the real selected document and native Chrome UI focus.
422447 if (!mac && !(options.testing === true && options.headless !== false)) await connection.send("Emulation.setFocusEmulationEnabled", { enabled: false });
423448 tab.browserContextId = (await connection.send("Target.getTargetInfo")).targetInfo.browserContextId;
424删除: tab.session = sid; sessions.set(sid, connection); frameSessions.set(page.mainFrame(), sid);
425449 tab.nativeTarget = (await connection.send("Target.getTargetInfo")).targetInfo.targetId;
426450 for (const method of ["Runtime.executionContextCreated", "Runtime.executionContextDestroyed", "Runtime.executionContextsCleared", "Runtime.bindingCalled", "Page.frameDetached", "Page.frameNavigated"]) {
427451 connection.on(method, params => { clipboard.event({ sessionId: sid, method, params }); vault?.event({ sessionId: sid, method, params }); });
@@ -1018,7 +1042,11 @@ export function createNativeBrowserService(options = {}) {
10181042 if (method === "GET") {
10191043 if (action === "frame-proof") return nativeFrameProof();
10201044 if (action === "focus-proof") return nativeFocusProof();
1021删除: if (action === "health") return { ready: true, native: !options.testing, capabilities: (await snapshot()).capabilities, ...(!mac ? { capture: { ...captureMetrics } } : {}) };
1045新增: if (action === "health") return {
1046新增: ready: true, native: !options.testing, capabilities: (await snapshot()).capabilities,
1047新增: resources: { tabs: tabs.size, sessions: sessions.size, clipboard_sessions: clipboard.sessions.size, captures: captures.size },
1048新增: ...(!mac ? { capture: { ...captureMetrics } } : {}),
1049新增: };
10221050 if (action === "tabs") return snapshot();
10231051 if (action === "events") {
10241052 const after = Number.isSafeInteger(input.after) && input.after >= 0 ? input.after : 0;