From e80641b6cdf0b324031c6917e40e64d6b0bc97ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hanno=20J=2E=20G=C3=B6decke?= Date: Sat, 21 Nov 2026 14:49:57 +0100 Subject: [PATCH] fix(agent-device): create the configured state directory before launch --- packages/core/src/agent-device-runtime.ts | 11 +++--- .../core/test/agent-device-runtime.test.ts | 35 +++++++++++++++++++ 2 files changed, 42 insertions(+), 4 deletions(-) create mode 100644 packages/core/test/agent-device-runtime.test.ts diff --git a/packages/core/src/agent-device-runtime.ts b/packages/core/src/agent-device-runtime.ts index bb2d2e31..c3ddf4b5 100644 --- a/packages/core/src/agent-device-runtime.ts +++ b/packages/core/src/agent-device-runtime.ts @@ -4,7 +4,7 @@ import type { RequestOptions } from "@modelcontextprotocol/sdk/shared/protocol.j import { CallToolResultSchema, type CallToolRequest, type CallToolResult } from "@modelcontextprotocol/sdk/types.js"; import * as Sentry from "@sentry/node"; import { execFile } from "node:child_process"; -import { mkdtemp } from "node:fs/promises"; +import { mkdir, mkdtemp } from "node:fs/promises"; import { homedir, tmpdir } from "node:os"; import { join, resolve } from "node:path"; import { fileURLToPath } from "node:url"; @@ -51,7 +51,7 @@ export async function claimAgentDeviceStateDirectory( export class AgentDeviceRuntime { private readonly cli: string; private readonly config: string; - private state?: Promise; + private state?: State; private starting?: Promise; /** The connected client; only its unexpected exit restarts the runtime. */ private client?: Client; @@ -93,6 +93,8 @@ export class AgentDeviceRuntime { private async createState(): Promise { const directory = await claimAgentDeviceStateDirectory(); + // App Flow and Testing can pin this as the daemon's working directory before it exists. + await mkdir(directory, { recursive: true, mode: 0o700 }); console.error(`[mobile-dev] agent-device state directory: ${directory}`); const stopStorageMetrics = startStorageMetrics({ agent_device_state: directory, apple_runner_cache: join(homedir(), ".agent-device/apple-runner") }); return { directory, stopStorageMetrics }; @@ -100,7 +102,7 @@ export class AgentDeviceRuntime { private async start() { const startedAt = performance.now(); - const { directory } = await (this.state ??= this.createState()); + const { directory } = this.state ??= await this.createState(); const client = new Client({ name: "mobile-dev-agent-device", version: "1" }); const transport = new StdioClientTransport({ command: process.execPath, args: [this.cli, "mcp"], cwd: directory, env: this.environment(directory), stderr: "inherit" }); // A start that fails closes its client too; connect reports that instead. @@ -119,7 +121,8 @@ export class AgentDeviceRuntime { async close() { if (this.closing) return; this.closing = true; - const [client, state] = await Promise.all([this.starting?.catch(() => undefined), this.state?.catch(() => undefined)]); + const client = await this.starting?.catch(() => undefined); + const state = this.state; try { await client?.close(); } finally { state?.stopStorageMetrics(); diff --git a/packages/core/test/agent-device-runtime.test.ts b/packages/core/test/agent-device-runtime.test.ts new file mode 100644 index 00000000..0a610da5 --- /dev/null +++ b/packages/core/test/agent-device-runtime.test.ts @@ -0,0 +1,35 @@ +import assert from "node:assert/strict"; +import { mkdtemp, rm, stat, unlink, writeFile } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import test from "node:test"; +import { AgentDeviceRuntime } from "../src/agent-device-runtime.ts"; + +test("the runtime can retry and start in a newly configured state directory", async t => { + const root = await mkdtemp(join(tmpdir(), "mobile-dev-state-test-")); + const parent = join(root, "App Flow"); + const directory = join(parent, "agent-device"); + const previous = process.env.AGENT_DEVICE_STATE_DIR; + process.env.AGENT_DEVICE_STATE_DIR = directory; + const runtime = new AgentDeviceRuntime(new URL("../../runtimes/agent-device/", import.meta.url)); + t.after(async () => { + try { await runtime.close(); } + finally { + if (previous === undefined) delete process.env.AGENT_DEVICE_STATE_DIR; + else process.env.AGENT_DEVICE_STATE_DIR = previous; + await rm(root, { recursive: true, force: true }); + } + }); + + await writeFile(parent, "temporarily blocks directory creation"); + await assert.rejects(runtime.connect()); + await unlink(parent); + + const client = await runtime.connect(); + const state = await client.callTool({ name: "session", arguments: { action: "state-dir" } }, {}); + assert.equal(state.isError, false); + assert.equal(state.structuredContent?.stateDir, directory); + assert.equal(process.env.AGENT_DEVICE_STATE_DIR, directory); + await runtime.close(); + assert.ok((await stat(directory)).isDirectory(), "Closing the runtime keeps the configured state directory"); +});