Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(ios-simulator): sync cached sidebar access

JiangJiahaoc0734033a585
diff --git a/apps/desktop/src/main/bootstrap-electron.ts b/apps/desktop/src/main/bootstrap-electron.ts
index b9cca600ef..397fd92874 100644
--- a/apps/desktop/src/main/bootstrap-electron.ts
+++ b/apps/desktop/src/main/bootstrap-electron.ts
@@ -327,6 +327,7 @@ import {
configureIOSSimulatorRendererAccessConfirmation,
configureIOSSimulatorRendererTargets,
inheritIOSSimulatorRendererSessionAccess,
+ syncIOSSimulatorRendererAccessForSessionChange,
} from './mcp-integrations/ios-simulator-renderer-access';
import {
parseIOSSimulatorReleaseGateArgs,
@@ -1451,6 +1452,9 @@ const rsbWindowController = new RsbWindowController({
: null;
if (mainTarget) {
inheritIOSSimulatorRendererSessionAccess(mainTarget, window.webContents);
+ // This renderer is still hidden for prewarm. Keep its Viewer buckets,
+ // but pause the inherited active mutation grant until it is shown.
+ syncIOSSimulatorRendererAccessForSessionChange(window.webContents, null);
}
return window;
},
@@ -1472,6 +1476,23 @@ const rsbWindowController = new RsbWindowController({
// window torn down mid-send — ignore
}
},
+ onWindowWillShow: (window) => {
+ const mainTarget =
+ mainWindowRef && !mainWindowRef.isDestroyed() && !mainWindowRef.webContents.isDestroyed()
+ ? mainWindowRef.webContents
+ : null;
+ const inherited = mainTarget
+ ? inheritIOSSimulatorRendererSessionAccess(mainTarget, window.webContents)
+ : false;
+ if (!inherited) {
+ // No authoritative Main snapshot is available. Never leave a cached
+ // sidebar's previous active mutation grant usable when it becomes visible.
+ syncIOSSimulatorRendererAccessForSessionChange(window.webContents, null);
+ }
+ },
+ onWindowHidden: (window) => {
+ syncIOSSimulatorRendererAccessForSessionChange(window.webContents, null);
+ },
contextChannel: MAKER_PUSH.RSB_WINDOW_CONTEXT_CHANGED,
commandChannel: MAKER_PUSH.RSB_WINDOW_COMMAND,
tabHandoffChannel: MAKER_PUSH.RSB_WINDOW_TAB_HANDOFF,
diff --git a/apps/desktop/src/main/cindy-brain/__tests__/iosSimulatorHostBindingContract.test.ts b/apps/desktop/src/main/cindy-brain/__tests__/iosSimulatorHostBindingContract.test.ts
index 3aebb190be..7475d1c348 100644
--- a/apps/desktop/src/main/cindy-brain/__tests__/iosSimulatorHostBindingContract.test.ts
+++ b/apps/desktop/src/main/cindy-brain/__tests__/iosSimulatorHostBindingContract.test.ts
@@ -12,6 +12,10 @@ describe('iOS Simulator plugin Host binding', () => {
resolve(process.cwd(), 'src/main/cindy-brain/index.ts'),
'utf8',
).replace(/\r\n/g, '\n');
+ const bootstrapSource = readFileSync(
+ resolve(process.cwd(), 'src/main/bootstrap-electron.ts'),
+ 'utf8',
+ ).replace(/\r\n/g, '\n');
it('derives plugin task authority only from a Main-owned renderer grant', () => {
const start = source.indexOf(
@@ -43,4 +47,41 @@ describe('iOS Simulator plugin Host binding', () => {
expect(body).toContain('revision: access.generation');
expect(body).not.toContain('sessionId: candidate.sessionHint');
});
+
+ it('refreshes cached detached-sidebar grants from Main before the window is shown', () => {
+ const createStart = bootstrapSource.indexOf('createWindow: () => {');
+ const createEnd = bootstrapSource.indexOf('\n getMainWindow:', createStart);
+ const createBody = bootstrapSource.slice(createStart, createEnd);
+
+ expect(createStart).toBeGreaterThan(-1);
+ expect(createEnd).toBeGreaterThan(createStart);
+ expect(createBody).toContain(
+ 'inheritIOSSimulatorRendererSessionAccess(mainTarget, window.webContents)',
+ );
+ expect(createBody).toContain(
+ 'syncIOSSimulatorRendererAccessForSessionChange(window.webContents, null)',
+ );
+
+ const start = bootstrapSource.indexOf('onWindowWillShow: (window) => {');
+ const end = bootstrapSource.indexOf('\n onWindowHidden:', start);
+ const body = bootstrapSource.slice(start, end);
+
+ expect(start).toBeGreaterThan(-1);
+ expect(end).toBeGreaterThan(start);
+ expect(body).toContain(
+ 'inheritIOSSimulatorRendererSessionAccess(mainTarget, window.webContents)',
+ );
+ expect(body).toContain(
+ 'syncIOSSimulatorRendererAccessForSessionChange(window.webContents, null)',
+ );
+
+ const hiddenStart = bootstrapSource.indexOf('onWindowHidden: (window) => {');
+ const hiddenEnd = bootstrapSource.indexOf('\n contextChannel:', hiddenStart);
+ const hiddenBody = bootstrapSource.slice(hiddenStart, hiddenEnd);
+ expect(hiddenStart).toBeGreaterThan(-1);
+ expect(hiddenEnd).toBeGreaterThan(hiddenStart);
+ expect(hiddenBody).toContain(
+ 'syncIOSSimulatorRendererAccessForSessionChange(window.webContents, null)',
+ );
+ });
});
diff --git a/apps/desktop/src/main/mcp-integrations/__tests__/ios-simulator-renderer-access.test.ts b/apps/desktop/src/main/mcp-integrations/__tests__/ios-simulator-renderer-access.test.ts
index 6a1485056a..661530df7a 100644
--- a/apps/desktop/src/main/mcp-integrations/__tests__/ios-simulator-renderer-access.test.ts
+++ b/apps/desktop/src/main/mcp-integrations/__tests__/ios-simulator-renderer-access.test.ts
@@ -261,6 +261,37 @@ describe('IOSSimulatorRendererAccessRegistry', () => {
expect(registry.accessSnapshot(sidebar.target)?.sessionId).toBe('session-b');
});
+ it('refreshes a cached hidden sidebar from the current Main grant before reuse', () => {
+ const registry = new IOSSimulatorRendererAccessRegistry();
+ const main = fakeWebContents(551);
+ const sidebar = fakeWebContents(552);
+ let sidebarVisible = true;
+ registry.configureResolver((preferred) => {
+ if (!sidebarVisible && preferred === sidebar.target) return null;
+ return {
+ grantTargets: sidebarVisible ? [main.target, sidebar.target] : [main.target],
+ focusTarget: sidebarVisible ? sidebar.target : main.target,
+ };
+ });
+ registry.grantAndFocus('session-a');
+
+ sidebarVisible = false;
+ registry.grantAndFocus('session-b');
+ expect(registry.accessSnapshot(main.target)?.sessionId).toBe('session-b');
+ expect(registry.accessSnapshot(sidebar.target)?.sessionId).toBe('session-a');
+
+ expect(registry.syncForSessionChange(sidebar.target, null)).toBe(1);
+ expect(registry.accessSnapshot(main.target)?.sessionId).toBe('session-b');
+ expect(registry.accessSnapshot(sidebar.target)).toBeNull();
+ expect(registry.hasAccess(sidebar.target, 'session-a')).toBe(true);
+
+ sidebarVisible = true;
+ expect(registry.inheritAccess(main.target, sidebar.target)).toBe(true);
+ expect(registry.hasAccess(sidebar.target, 'session-a')).toBe(true);
+ expect(registry.hasAccess(sidebar.target, 'session-b')).toBe(true);
+ expect(registry.accessSnapshot(sidebar.target)?.sessionId).toBe('session-b');
+ });
+
it('removes the inherited active grant when its session is revoked', () => {
const registry = new IOSSimulatorRendererAccessRegistry();
const main = fakeWebContents(57);
diff --git a/apps/desktop/src/main/right-sidebar-window/__tests__/controller.test.ts b/apps/desktop/src/main/right-sidebar-window/__tests__/controller.test.ts
index 6599bbb67f..5585a0a1ce 100644
--- a/apps/desktop/src/main/right-sidebar-window/__tests__/controller.test.ts
+++ b/apps/desktop/src/main/right-sidebar-window/__tests__/controller.test.ts
@@ -108,6 +108,8 @@ function makeHarness(initial: Partial<RsbWindowSettings> = {}) {
const broadcasts: Array<{ detached: boolean; open: boolean }> = [];
const sends: Array<{ channel: string; payload: unknown }> = [];
const sendTargets: number[] = [];
+ const windowWillShowTargets: number[] = [];
+ const windowHiddenTargets: number[] = [];
const deps: RsbWindowControllerDeps = {
settings: {
@@ -126,6 +128,16 @@ function makeHarness(initial: Partial<RsbWindowSettings> = {}) {
sends.push({ channel, payload });
sendTargets.push((win.webContents as unknown as FakeWindow['webContents']).id);
},
+ onWindowWillShow: (win) => {
+ windowWillShowTargets.push(
+ (win.webContents as unknown as FakeWindow['webContents']).id,
+ );
+ },
+ onWindowHidden: (win) => {
+ windowHiddenTargets.push(
+ (win.webContents as unknown as FakeWindow['webContents']).id,
+ );
+ },
contextChannel: 'ctx-channel',
commandChannel: 'cmd-channel',
tabHandoffChannel: 'handoff-channel',
@@ -141,6 +153,8 @@ function makeHarness(initial: Partial<RsbWindowSettings> = {}) {
broadcasts,
sends,
sendTargets,
+ windowWillShowTargets,
+ windowHiddenTargets,
getSettings: () => settings,
setQuitting: (v: boolean) => { quitting = v; },
};
@@ -300,10 +314,12 @@ describe('open / close (hide-reuse)', () => {
win.emitWindowEvent('minimize');
expect(h.controller.getState()).toEqual({ detached: true, lastOpen: true, open: false });
expect(h.broadcasts.at(-1)).toEqual({ detached: true, open: false });
+ expect(h.windowHiddenTargets).toEqual([win.webContents.id]);
win.show.mockClear();
win.focus.mockClear();
h.controller.open();
+ expect(h.windowWillShowTargets).toEqual([win.webContents.id, win.webContents.id]);
expect(h.windows).toHaveLength(1);
expect(win.restore).toHaveBeenCalledOnce();
expect(win.show).toHaveBeenCalledOnce();
@@ -318,8 +334,10 @@ describe('open / close (hide-reuse)', () => {
markReady(h.controller, win);
h.controller.open();
win.emitWindowEvent('minimize');
+ expect(h.windowHiddenTargets).toEqual([win.webContents.id]);
win.emitWindowEvent('restore');
+ expect(h.windowWillShowTargets).toEqual([win.webContents.id, win.webContents.id]);
expect(h.broadcasts.at(-1)).toEqual({ detached: true, open: true });
expect(h.controller.getState().open).toBe(true);
});
@@ -419,10 +437,12 @@ describe('renderer navigation lifecycle', () => {
true,
);
expect(win.hide).toHaveBeenCalledOnce();
+ expect(h.windowHiddenTargets).toEqual([win.webContents.id]);
expect(win.webContents.setBackgroundThrottling).toHaveBeenLastCalledWith(false);
expect(h.controller.getState().open).toBe(true);
markReady(h.controller, win);
+ expect(h.windowWillShowTargets).toEqual([win.webContents.id, win.webContents.id]);
expect(win.webContents.setBackgroundThrottling).toHaveBeenLastCalledWith(true);
expect(win.show).toHaveBeenCalledTimes(2);
expect(h.controller.getState().open).toBe(true);
@@ -806,6 +826,23 @@ describe('getVisibleSidebarWebContents', () => {
h.controller.close();
expect(h.controller.getVisibleSidebarWebContents()).toBeNull();
});
+
+ it('runs the Host show hook whenever a cached hidden window is reused', () => {
+ const h = makeHarness({ detached: true });
+ h.controller.prewarm();
+ const win = h.windows[0];
+ markReady(h.controller, win);
+
+ h.controller.open({ userInitiated: false });
+ expect(h.windowWillShowTargets).toEqual([win.webContents.id]);
+ expect(h.controller.getVisibleSidebarWebContents()).toBe(win.webContents);
+
+ h.controller.close();
+ expect(h.windowHiddenTargets).toEqual([win.webContents.id]);
+ h.controller.open({ userInitiated: false });
+ expect(h.windowWillShowTargets).toEqual([win.webContents.id, win.webContents.id]);
+ expect(h.windows).toHaveLength(1);
+ });
});
// ═══════════════════════════════════════════════════════════════════════
diff --git a/apps/desktop/src/main/right-sidebar-window/controller.ts b/apps/desktop/src/main/right-sidebar-window/controller.ts
index 8469835b15..a99b9dae7d 100644
--- a/apps/desktop/src/main/right-sidebar-window/controller.ts
+++ b/apps/desktop/src/main/right-sidebar-window/controller.ts
@@ -58,6 +58,13 @@ export interface RsbWindowControllerDeps {
broadcastState: (state: { detached: boolean; open: boolean }) => void;
/** 向裁决后的 renderer host 推送 context / command;窗口有效性由 controller 保证。 */
sendToWindow: (win: BrowserWindow, channel: string, payload: unknown) => void;
+ /**
+ * 缓存窗口每次重新显示前的 Host 同步钩子。调用时窗口仍标记为 hidden,
+ * capability 同步必须以该精确 WebContents 为目标;原生 show 前完成。
+ */
+ onWindowWillShow?: (win: BrowserWindow) => void;
+ /** 缓存窗口隐藏后立即暂停 Host 侧交互能力,但保留 renderer 与分桶状态。 */
+ onWindowHidden?: (win: BrowserWindow) => void;
contextChannel: string;
commandChannel: string;
/** main → renderer host;用于内嵌 / 分离宿主之间交接内存态 tab。 */
@@ -517,6 +524,10 @@ export class RsbWindowController {
this.clearPrewarmTimeout();
this.pendingOpen = false;
this.pendingOpenShouldFocus = shouldFocus;
+ // 先同步 Host capability,再真正展示 renderer;保持 hidden 状态可确保同步
+ // 失败时只 fail-close 该缓存子窗口,不会误清当前主窗口 family。
+ this.deps.onWindowWillShow?.(win);
+ this.visible = true;
if (win.isMinimized()) win.restore();
win.webContents.setBackgroundThrottling(true);
if (shouldFocus) {
@@ -525,7 +536,6 @@ export class RsbWindowController {
} else {
win.showInactive();
}
- this.visible = true;
// lastOpen 由 open() 在外层写,这里只负责展示
this.deps.sendToWindow(win, RSB_WINDOW_VISIBILITY_CHANGED_CHANNEL, { visible: true });
// 隐藏复用期间的 passive 命令不能在用户看不见时改动子窗口 store;
@@ -539,8 +549,9 @@ export class RsbWindowController {
this.clearPrewarmTimeout();
this.pendingOpen = false;
this.pendingOpenShouldFocus = true;
- if (win.isVisible()) win.hide();
this.visible = false;
+ this.deps.onWindowHidden?.(win);
+ if (win.isVisible()) win.hide();
try {
this.deps.sendToWindow(win, RSB_WINDOW_VISIBILITY_CHANGED_CHANNEL, { visible: false });
} catch {
@@ -554,8 +565,17 @@ export class RsbWindowController {
private onNativeVisibilityChanged(win: BrowserWindow, visible: boolean): void {
if (win !== this.winRef || win.isDestroyed() || this.destroyingWindow || this.disposed) return;
if (this.visible === visible && !this.pendingOpen) return;
- this.visible = visible;
- if (visible) this.pendingOpen = false;
+ // 原生 show / restore 可能绕过 showWindow;仍需在 renderer 收到 visible=true
+ // 之前完成同一轮 Host capability 同步。恢复时保持 hidden 到同步结束,失败
+ // 回退才只会清理该精确 WebContents;隐藏时则先退出可见 family 再暂停能力。
+ if (visible) {
+ this.deps.onWindowWillShow?.(win);
+ this.visible = true;
+ this.pendingOpen = false;
+ } else {
+ this.visible = false;
+ this.deps.onWindowHidden?.(win);
+ }
try {
this.deps.sendToWindow(win, RSB_WINDOW_VISIBILITY_CHANGED_CHANNEL, { visible });
} catch {
@@ -660,7 +680,11 @@ export class RsbWindowController {
if (shouldRestore) {
this.pendingOpen = true;
}
- if (this.visible) win.hide();
+ if (this.visible) {
+ this.visible = false;
+ this.deps.onWindowHidden?.(win);
+ win.hide();
+ }
win.webContents.setBackgroundThrottling(false);
this.rendererReady = false;
this.presentationReady = false;