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): 非 macOS 上 iOS Simulator 入口报 UNSUPPORTED_PLATFORM 而非误导性的 DEVICE_BUSY (#3919)

fico-hub38b1aada14ac
diff --git a/apps/desktop/src/main/mcp-integrations/__tests__/ios-simulator-unsupported-platform.test.ts b/apps/desktop/src/main/mcp-integrations/__tests__/ios-simulator-unsupported-platform.test.ts
new file mode 100644
index 0000000000..5bfc5d67fb
--- /dev/null
+++ b/apps/desktop/src/main/mcp-integrations/__tests__/ios-simulator-unsupported-platform.test.ts
@@ -0,0 +1,98 @@
+/**
+ * #3914: on non-macOS the module singleton must not try to own the Darwin
+ * ownership registry (its writer lease is never granted there), otherwise the
+ * Host reports DEVICE_BUSY before the runtime can report UNSUPPORTED_PLATFORM
+ * — and no reboot can clear a lease that was never taken.
+ *
+ * Lives in its own file: the default Host is a module singleton whose disposal
+ * is one-way, so it cannot share a module instance with the darwin suites.
+ */
+import { existsSync, mkdtempSync, readdirSync, rmSync } from 'node:fs';
+import os from 'node:os';
+import path from 'node:path';
+import { app } from 'electron';
+import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest';
+
+import { IOSSimulatorOwnershipRegistryFile } from '@cindy/ios-simulator-runtime';
+
+// Same owner-boundary override as ios-simulator.test.ts: these cases exercise
+// platform gating, not owner transitions.
+vi.mock('../../appSessionState.js', async (importOriginal) => {
+ const actual = await importOriginal<typeof import('../../appSessionState.js')>();
+ return { ...actual, isAppSessionBoundaryPending: () => false };
+});
+
+import { disposeIOSSimulatorHost, initializeIOSSimulatorHost } from '../ios-simulator';
+
+const originalPlatform = Object.getOwnPropertyDescriptor(process, 'platform');
+
+function setPlatform(value: NodeJS.Platform): void {
+ Object.defineProperty(process, 'platform', { value, configurable: true });
+}
+
+function localSession(id: string) {
+ return { id, workDir: `/tmp/${id}`, remoteHostId: null };
+}
+
+describe('iOS Simulator host on a non-macOS platform (#3914)', () => {
+ beforeAll(() => {
+ setPlatform('win32');
+ });
+
+ afterAll(async () => {
+ await disposeIOSSimulatorHost();
+ if (originalPlatform) Object.defineProperty(process, 'platform', originalPlatform);
+ });
+
+ it('reports UNSUPPORTED_PLATFORM without touching the ownership registry or its lock', async () => {
+ const acquireWriter = vi.spyOn(IOSSimulatorOwnershipRegistryFile.prototype, 'acquireWriterSync');
+ // Point the profile at a fresh directory: the non-macOS Host may still resolve
+ // userData for unrelated cache roots (WDA archive cache), but it must never
+ // materialise the Darwin ownership registry, its writer lock, device grants or
+ // interrupted-create evidence there.
+ const root = mkdtempSync(path.join(os.tmpdir(), 'cindy-ios-sim-win32-'));
+ const getPath = vi.spyOn(app, 'getPath').mockReturnValue(root);
+ try {
+ const host = initializeIOSSimulatorHost({
+ getSession: async (id) => localSession(id),
+ });
+
+ const environment = await host.callTool('check_environment', {}, {
+ sessionId: 'win-session',
+ origin: 'user',
+ });
+ expect(environment).toMatchObject({
+ ok: true,
+ data: {
+ supported: false,
+ ready: false,
+ issue: 'UNSUPPORTED_PLATFORM',
+ error: 'iOS Simulator is available only for local macOS sessions.',
+ },
+ });
+ expect(JSON.stringify(environment)).not.toContain('Another Cindy process');
+
+ const tools = await host.describeTools('win-session');
+ expect(tools.ready).toBe(false);
+ expect(tools.tools.check_environment).toMatchObject({ state: 'available' });
+ expect(tools.tools.list_simulator_devices).toMatchObject({
+ state: 'unavailable',
+ reasonCode: 'UNSUPPORTED_PLATFORM',
+ });
+
+ // The Darwin-only registry must stay untouched: no writer lease attempt and
+ // no registry / lock / grants / evidence files under the profile.
+ expect(acquireWriter).not.toHaveBeenCalled();
+ const profileDir = path.join(root, 'ios-simulator');
+ const materialised = existsSync(profileDir) ? readdirSync(profileDir) : [];
+ expect(materialised).not.toContain('ownership-registry.json');
+ expect(materialised).not.toContain('ownership-registry.json.writer.lock');
+ expect(materialised).not.toContain('device-grants.json');
+ expect(materialised).not.toContain('pending-create-evidence.json');
+ } finally {
+ acquireWriter.mockRestore();
+ getPath.mockRestore();
+ rmSync(root, { recursive: true, force: true });
+ }
+ });
+});
diff --git a/apps/desktop/src/main/mcp-integrations/ios-simulator.ts b/apps/desktop/src/main/mcp-integrations/ios-simulator.ts
index a71db40d26..5059b6a4e3 100644
--- a/apps/desktop/src/main/mcp-integrations/ios-simulator.ts
+++ b/apps/desktop/src/main/mcp-integrations/ios-simulator.ts
@@ -7364,11 +7364,17 @@ async function readPassiveIOSSimulatorPluginStatus(
}
}
+/** Test seams for the module singleton; production callers pass nothing. */
+export interface IOSSimulatorHostInitializeOptions {
+ getSession?: IOSSimulatorHostOptions['getSession'];
+}
+
function installDefaultIOSSimulatorHost(
lifecycle: IOSSimulatorSimctlLifecycle,
persistedActor: ReturnType<typeof createDefaultActor>,
- registry: IOSSimulatorOwnershipRegistryFile,
- pendingCreateEvidence: IOSSimulatorPendingCreateEvidenceStore,
+ grantStore: IOSSimulatorDeviceGrantStore,
+ pendingCreateEvidence: IOSSimulatorPendingCreateEvidenceStore | null,
+ seams: IOSSimulatorHostInitializeOptions = {},
): IOSSimulatorHost {
if (defaultIOSSimulatorRuntime) {
persistedActor.release();
@@ -7384,9 +7390,10 @@ function installDefaultIOSSimulatorHost(
const host = createIOSSimulatorHost({
lifecycle,
actor: persistedActor.actor,
- grantStore: createRegistryBackedIOSSimulatorDeviceGrantStore(registry),
+ grantStore,
canReconcilePendingCreates: persistedActor.canReconcilePendingCreates,
- pendingCreateEvidence,
+ ...(pendingCreateEvidence ? { pendingCreateEvidence } : {}),
+ ...(seams.getSession ? { getSession: seams.getSession } : {}),
driverManager: createDefaultDriverManager(),
});
configureIOSSimulatorRendererAccessRevocationObserver((grants) => {
@@ -7418,17 +7425,50 @@ function installDefaultIOSSimulatorHost(
* Simulator must not prevent the instance that actually needs it from taking
* ownership.
*/
-export function initializeIOSSimulatorHost(): IOSSimulatorHost {
+export function initializeIOSSimulatorHost(
+ options: IOSSimulatorHostInitializeOptions = {},
+): IOSSimulatorHost {
if (defaultIOSSimulatorRuntime) return defaultIOSSimulatorRuntime.host;
if (defaultIOSSimulatorRuntimeClosing) {
throw new Error('The iOS Simulator host is shutting down.');
}
+ if (process.platform !== 'darwin') {
+ // Apple Simulator is macOS-only. The profile ownership registry only exists
+ // for Darwin (its writer lease is an O_EXLOCK kernel lock and
+ // acquireDarwinWriterLease() returns null elsewhere), so a registry-backed
+ // actor would report DEVICE_BUSY ("another Cindy process…") before the
+ // runtime ever gets to say UNSUPPORTED_PLATFORM — and no reboot can clear a
+ // lease that was never taken (#3914). Install an in-memory Host that never
+ // touches the registry, lock, grants or interrupted-create evidence so
+ // every entry (tool catalog, check_environment, doctor, instance queries)
+ // surfaces the real platform issue through runtime.inspect().
+ const lifecycle = createIOSSimulatorSimctlLifecycle();
+ return installDefaultIOSSimulatorHost(
+ lifecycle,
+ {
+ actor: createInMemoryActor(lifecycle),
+ flush: async () => {},
+ release: () => {},
+ canReconcilePendingCreates: () => false,
+ },
+ new IOSSimulatorDeviceGrantStore(),
+ null,
+ options,
+ );
+ }
+
const registry = createDefaultOwnershipRegistry();
const pendingCreateEvidence = createDefaultPendingCreateEvidence(registry);
const lifecycle = createProfileScopedIOSSimulatorLifecycle(registry, pendingCreateEvidence);
const persistedActor = createDefaultActor(lifecycle, registry);
- return installDefaultIOSSimulatorHost(lifecycle, persistedActor, registry, pendingCreateEvidence);
+ return installDefaultIOSSimulatorHost(
+ lifecycle,
+ persistedActor,
+ createRegistryBackedIOSSimulatorDeviceGrantStore(registry),
+ pendingCreateEvidence,
+ options,
+ );
}
function currentIOSSimulatorHost(): IOSSimulatorHost | null {
@@ -7618,7 +7658,7 @@ export async function reconcilePersistedIOSSimulatorOwnership(
const host = installDefaultIOSSimulatorHost(
lifecycle,
persistedActor,
- registry,
+ createRegistryBackedIOSSimulatorDeviceGrantStore(registry),
pendingCreateEvidence,
);
registryTransferred = true;
@@ -7688,7 +7728,7 @@ export function cleanupIOSSimulatorRemovedSession(sessionId: string): Promise<vo
const host = installDefaultIOSSimulatorHost(
lifecycle,
persistedActor,
- registry,
+ createRegistryBackedIOSSimulatorDeviceGrantStore(registry),
pendingCreateEvidence,
);
registryTransferred = true;