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(desktop): authorize simulator plugin cold open

JiangJiahaobb1681c8fa0f
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 58d8b51a4b..3aebb190be 100644
--- a/apps/desktop/src/main/cindy-brain/__tests__/iosSimulatorHostBindingContract.test.ts
+++ b/apps/desktop/src/main/cindy-brain/__tests__/iosSimulatorHostBindingContract.test.ts
@@ -17,10 +17,7 @@ describe('iOS Simulator plugin Host binding', () => {
const start = source.indexOf(
'function focusedIOSSimulatorContext(): IOSSimulatorSlotFocusContext | null {',
);
- const end = source.indexOf(
- '\n}\n\nfunction isIOSSimulatorContextCurrent',
- start,
- );
+ const end = source.indexOf('\n}\n\nfunction focusedIOSSimulatorAuthorizationCandidate', start);
const body = source.slice(start, end);
expect(start).toBeGreaterThan(-1);
@@ -30,4 +27,20 @@ describe('iOS Simulator plugin Host binding', () => {
expect(body).toContain('revision: access.generation');
expect(body).not.toContain('ghostSessionFocusByWebContents');
});
+
+ it('uses renderer focus only as an explicitly confirmed cold-open hint', () => {
+ const start = source.indexOf(
+ 'async function authorizeFocusedIOSSimulatorContext(): Promise<IOSSimulatorSlotFocusContext | null> {',
+ );
+ const end = source.indexOf('\n}\n\nfunction isIOSSimulatorContextCurrent', start);
+ const body = source.slice(start, end);
+
+ expect(start).toBeGreaterThan(-1);
+ expect(end).toBeGreaterThan(start);
+ expect(body).toContain('requestIOSSimulatorRendererSessionAccess(');
+ expect(body).toContain('getIOSSimulatorRendererSessionAccess(candidate.window.webContents)');
+ expect(body).toContain('sessionId: access.sessionId');
+ expect(body).toContain('revision: access.generation');
+ expect(body).not.toContain('sessionId: candidate.sessionHint');
+ });
});
diff --git a/apps/desktop/src/main/cindy-brain/__tests__/iosSimulatorSlot.test.ts b/apps/desktop/src/main/cindy-brain/__tests__/iosSimulatorSlot.test.ts
index ef5cc6a1d3..2716ab1861 100644
--- a/apps/desktop/src/main/cindy-brain/__tests__/iosSimulatorSlot.test.ts
+++ b/apps/desktop/src/main/cindy-brain/__tests__/iosSimulatorSlot.test.ts
@@ -79,6 +79,7 @@ function makeSlot(overrides: Partial<IOSSimulatorSlotDeps> = {}) {
const deps: IOSSimulatorSlotDeps = {
getGhost: () => simulatorGhost(),
focusedContext: () => context,
+ authorizeFocusedContext: vi.fn(async () => context),
isContextCurrent: (candidate) => sameContext(context, candidate),
getStatus: vi.fn(async () => readyStatus()),
focusViewer: vi.fn(() => true),
@@ -191,6 +192,56 @@ describe('GhostIOSSimulatorSlot', () => {
).resolves.toMatchObject({ ok: false, errorCode: 'INSTANCE_NOT_OWNED' });
});
+ it('explicitly authorizes a cold open-panel request without prompting for status', async () => {
+ const grantedContext: IOSSimulatorSlotFocusContext = {
+ sessionId: 'session-a',
+ windowWebContentsId: 101,
+ revision: 2,
+ };
+ let context: IOSSimulatorSlotFocusContext | null = null;
+ const authorizeFocusedContext = vi.fn(async () => {
+ context = grantedContext;
+ return grantedContext;
+ });
+ const test = makeSlot({
+ focusedContext: () => context,
+ authorizeFocusedContext,
+ isContextCurrent: (candidate) => sameContext(context, candidate),
+ });
+
+ await expect(test.slot.handleRequest('ios-simulator', { kind: 'open-panel' })).resolves.toEqual(
+ { ok: true, apiVersion: 1, kind: 'open-panel' },
+ );
+ expect(authorizeFocusedContext).toHaveBeenCalledTimes(1);
+ expect(test.deps.getStatus).toHaveBeenCalledWith('session-a');
+ expect(test.deps.focusViewer).toHaveBeenCalledWith(grantedContext, undefined);
+
+ context = null;
+ await expect(
+ test.slot.handleRequest('ios-simulator', { kind: 'status' }),
+ ).resolves.toMatchObject({ ok: false, errorCode: 'HOST_NOT_READY' });
+ expect(authorizeFocusedContext).toHaveBeenCalledTimes(1);
+ });
+
+ it('rate-limits cold open attempts before requesting another authorization', async () => {
+ const authorizeFocusedContext = vi.fn(async () => null);
+ const test = makeSlot({
+ focusedContext: () => null,
+ authorizeFocusedContext,
+ });
+
+ await expect(
+ test.slot.handleRequest('ios-simulator', { kind: 'open-panel' }),
+ ).resolves.toMatchObject({ ok: false, errorCode: 'HOST_NOT_READY' });
+ expect(authorizeFocusedContext).toHaveBeenCalledTimes(1);
+
+ test.advance(GHOST_IOS_SIMULATOR_OPEN_MIN_INTERVAL_MS - 1);
+ await expect(
+ test.slot.handleRequest('ios-simulator', { kind: 'open-panel' }),
+ ).resolves.toMatchObject({ ok: false, errorCode: 'RATE_LIMITED' });
+ expect(authorizeFocusedContext).toHaveBeenCalledTimes(1);
+ });
+
it('rate-limits opening before doing another Host status probe', async () => {
const test = makeSlot();
expect((await test.slot.handleRequest('ios-simulator', { kind: 'open-panel' })).ok).toBe(true);
diff --git a/apps/desktop/src/main/cindy-brain/index.ts b/apps/desktop/src/main/cindy-brain/index.ts
index dc195dbeab..e234fe4102 100644
--- a/apps/desktop/src/main/cindy-brain/index.ts
+++ b/apps/desktop/src/main/cindy-brain/index.ts
@@ -199,6 +199,7 @@ import {
clearIOSSimulatorRendererAccess,
focusIOSSimulatorRendererSession,
getIOSSimulatorRendererSessionAccess,
+ requestIOSSimulatorRendererSessionAccess,
revokeIOSSimulatorRendererAccessForSessionChange,
} from '../mcp-integrations/ios-simulator-renderer-access.js';
import { getIOSSimulatorPluginStatus } from '../mcp-integrations/ios-simulator.js';
@@ -1901,6 +1902,54 @@ function focusedIOSSimulatorContext(): IOSSimulatorSlotFocusContext | null {
};
}
+function focusedIOSSimulatorAuthorizationCandidate(): {
+ window: BrowserWindow;
+ sessionHint: string;
+} | null {
+ const candidates = mainShellWindows();
+ const focused = BrowserWindow.getFocusedWindow();
+ const focusedMainShell =
+ focused && !focused.isDestroyed() && candidates.includes(focused) ? focused : null;
+ // If a detached plugin/sidebar window has focus, only a single main shell is
+ // unambiguous. The renderer report below is an untrusted hint for the native
+ // confirmation flow, never the resulting authorization context.
+ const window = focusedMainShell ?? (candidates.length === 1 ? candidates[0]! : null);
+ if (!window || window.webContents.isDestroyed()) return null;
+ const sessionHint = ghostSessionFocusByWebContents.get(window.webContents.id)?.trim();
+ return sessionHint ? { window, sessionHint } : null;
+}
+
+async function authorizeFocusedIOSSimulatorContext(): Promise<IOSSimulatorSlotFocusContext | null> {
+ const existing = focusedIOSSimulatorContext();
+ if (existing) return existing;
+
+ const candidate = focusedIOSSimulatorAuthorizationCandidate();
+ if (!candidate) return null;
+ const granted = await requestIOSSimulatorRendererSessionAccess(
+ candidate.window.webContents,
+ candidate.sessionHint,
+ );
+ if (!granted) return null;
+
+ // Confirmation can outlive a focus/route transition. The registry invalidates
+ // those pending grants; this second identity check also keeps the candidate
+ // window and its route hint stable before reading the authoritative snapshot.
+ const currentCandidate = focusedIOSSimulatorAuthorizationCandidate();
+ if (
+ currentCandidate?.window !== candidate.window ||
+ currentCandidate.sessionHint !== candidate.sessionHint
+ ) {
+ return null;
+ }
+ const access = getIOSSimulatorRendererSessionAccess(candidate.window.webContents);
+ if (!access || access.sessionId !== candidate.sessionHint) return null;
+ return {
+ sessionId: access.sessionId,
+ windowWebContentsId: candidate.window.webContents.id,
+ revision: access.generation,
+ };
+}
+
function isIOSSimulatorContextCurrent(context: IOSSimulatorSlotFocusContext): boolean {
const current = focusedIOSSimulatorContext();
return (
@@ -1921,6 +1970,7 @@ export function getGhostIOSSimulatorSlot(): GhostIOSSimulatorSlot {
iosSimulatorSlotSingleton = new GhostIOSSimulatorSlot({
getGhost: findAvailableGhost,
focusedContext: focusedIOSSimulatorContext,
+ authorizeFocusedContext: authorizeFocusedIOSSimulatorContext,
isContextCurrent: isIOSSimulatorContextCurrent,
getStatus: getIOSSimulatorPluginStatus,
focusViewer: (context, instanceId) => {
diff --git a/apps/desktop/src/main/cindy-brain/iosSimulatorSlot.ts b/apps/desktop/src/main/cindy-brain/iosSimulatorSlot.ts
index e37a2681fd..c527442b99 100644
--- a/apps/desktop/src/main/cindy-brain/iosSimulatorSlot.ts
+++ b/apps/desktop/src/main/cindy-brain/iosSimulatorSlot.ts
@@ -31,6 +31,11 @@ export interface IOSSimulatorSlotFocusContext {
export interface IOSSimulatorSlotDeps {
getGhost(id: string): InstalledGhost | null;
focusedContext(): IOSSimulatorSlotFocusContext | null;
+ /**
+ * User-initiated cold-start path. The Host must explicitly authorize the
+ * exact foreground window/task and return a fresh Main-owned grant snapshot.
+ */
+ authorizeFocusedContext(): Promise<IOSSimulatorSlotFocusContext | null>;
isContextCurrent(context: IOSSimulatorSlotFocusContext): boolean;
/** Read-only, redacted snapshot; must not reconcile or renew ownership. */
getStatus(sessionId: string): Promise<GhostIOSSimulatorStatusProbeResult>;
@@ -155,11 +160,6 @@ export class GhostIOSSimulatorSlot {
instanceId = payload.instanceId.trim();
}
- const context = this.deps.focusedContext();
- if (!context?.sessionId.trim()) {
- return fail('HOST_NOT_READY', '当前没有打开的 Cindy 任务;先打开一个任务再使用内置模拟器');
- }
-
if (payload.kind === 'open-panel') {
const now = this.now();
const lastAttemptAt = this.lastOpenAttemptAt.get(ghostId);
@@ -172,6 +172,14 @@ export class GhostIOSSimulatorSlot {
}
}
+ let context = this.deps.focusedContext();
+ if (!context?.sessionId.trim() && payload.kind === 'open-panel') {
+ context = await this.deps.authorizeFocusedContext();
+ }
+ if (!context?.sessionId.trim()) {
+ return fail('HOST_NOT_READY', '当前没有打开的 Cindy 任务;先打开一个任务再使用内置模拟器');
+ }
+
let probe: GhostIOSSimulatorStatusProbeResult;
try {
probe = await this.readStatus(ghostId, context);