From 9631ce5793fc0e5dc443c685952af64336a142d2 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Wed, 18 Nov 2026 13:27:21 +0000 Subject: [PATCH 1/8] test(server): bound agent-device launcher gives one workspace across shell directories Reproduces #38603: the bound launcher spawns agent-device in the caller's cwd, which agent-device records as the device claim's workspace, so install from a second repository is refused with DEVICE_IN_USE by the same session. Co-Authored-By: Claude Opus 5.5 --- .../src/device/AgentDeviceTarget.test.ts | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/apps/server/src/device/AgentDeviceTarget.test.ts b/apps/server/src/device/AgentDeviceTarget.test.ts index bf180c9004dfd..ccd44e29113a1 100644 --- a/apps/server/src/device/AgentDeviceTarget.test.ts +++ b/apps/server/src/device/AgentDeviceTarget.test.ts @@ -255,6 +255,61 @@ exec '${process.execPath.replaceAll("'", "'\"'\"'")}' "$@" ), ); + // agent-device records a device claim's workspace from its own cwd (the nearest `.git` above it) + // and refuses install/uninstall/push from a different workspace, even for the same session. + it.effect( + "gives agent-device one workspace for its session whatever shell directory runs it", + () => + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const dir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-device-cwd-" }); + const entryPath = path.join(dir, "cli.mjs"); + yield* fs.writeFileString(entryPath, "console.log(process.cwd());"); + const command = yield* bindAgentDeviceLauncher({ + entryPath, + stateDir: dir, + session: "t3-a", + targetArgs: [ + "--platform", + "ios", + "--udid", + "UDID-1", + "--config", + "/h.json", + "--session", + "t3-a", + ], + }); + const launchFrom = Effect.fn(function* (repo: string, args: ReadonlyArray) { + const cwd = path.join(dir, repo); + yield* fs.makeDirectory(path.join(cwd, ".git"), { recursive: true }); + const { stdout } = yield* Effect.promise(() => + exec( + process.execPath, + [path.join(path.dirname(command), "agent-device-launcher.mjs"), ...args], + { + cwd, + }, + ), + ); + return stdout.trim(); + }); + const opened = yield* launchFrom("mobile-app", ["open", "com.example.app"]); + const installed = yield* launchFrom("coordinator", [ + "install", + "com.example.app", + "/b/App.app", + ]); + expect(installed).toBe(opened); + }).pipe( + Effect.scoped, + Effect.provideService(HostProcess.IsExecutable, false), + Effect.provideService(HostProcess.ExecutablePath, process.execPath), + Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)), + ), + ); + it("tells a released session from a close that failed or timed out", () => { const result = (code: number | null, stderr = "", timedOut = false) => ({ stdout: "", From e6244c645214a2c6fbd8e028e9b4179ca6198c0a Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Wed, 18 Nov 2026 13:39:05 +0000 Subject: [PATCH 2/8] fix(server): bound agent-device launcher acts as one session from any directory agent-device records a device claim's workspace from its own cwd (the nearest .git root) and refuses install, uninstall, push and prepare from any other workspace, even for the same session. The launcher passed the caller's cwd through, so a thread's shell in a second repository got DEVICE_IN_USE naming its own session. The launcher now starts agent-device in its own directory, which is stable per session, and resolves relative path flags and path operands against the caller's directory so they keep pointing where the agent meant. Fixes pingdotgg/t3code#38603 Co-Authored-By: Claude Opus 5.5 --- apps/server/src/device/AgentDeviceShim.ts | 56 ++++++++++++++++++- .../src/device/AgentDeviceTarget.test.ts | 15 +++++ 2 files changed, 70 insertions(+), 1 deletion(-) diff --git a/apps/server/src/device/AgentDeviceShim.ts b/apps/server/src/device/AgentDeviceShim.ts index e5b42557a1e10..826d24a3ad40a 100644 --- a/apps/server/src/device/AgentDeviceShim.ts +++ b/apps/server/src/device/AgentDeviceShim.ts @@ -151,6 +151,32 @@ const VALUE_FLAGS: ReadonlyArray = [ "--wait", ]; +/** + * agent-device 0.21.36's flags whose value is a path. It ties a session's device claim to its cwd's + * workspace and refuses install, push and the like from another, so the launcher runs it in its own + * directory and resolves these, and path operands, against the caller's. + */ +const PATH_FLAGS: ReadonlyArray = [ + "--artifact", + "--artifacts-dir", + "-b", + "--baseline", + "--config", + "--dsym", + "--ios-simulator-device-set", + "--ios-xctest-derived-data-path", + "--ios-xctest-env-dir", + "--ios-xctestrun-file", + "--out", + "--project-root", + "--remote-config", + "--report-junit", + "--runtime-file", + "--search-path", + "--state-dir", + "--steps-file", +]; + // Help gets no bound flags (agent-device reads --config even for help); only args before `--` are flags. // agent-device 0.21.31 refuses some bound flags per command; connect/disconnect/auth/daemon would // replace T3's connection; device status reads this machine's claims. It reports a sandboxed shell and a @@ -159,6 +185,8 @@ const launcherSource = (node: string, entryPath: string, boundArgs: ReadonlyArra `import { spawn } from "node:child_process"; import { existsSync, readFileSync } from "node:fs"; import { connect } from "node:net"; +import { dirname, isAbsolute, resolve } from "node:path"; +import { fileURLToPath } from "node:url"; const entry = ${JSON.stringify(entryPath)}; const bound = ${JSON.stringify(boundArgs)}; const refused = new Map([["cdp", ["--platform", "--udid", "--config", "--session"]], ["connection", ["--platform", "--udid"]]]); @@ -214,11 +242,37 @@ if (local) { process.exit(1); } } +const pathFlags = new Set(${JSON.stringify(PATH_FLAGS)}); +const absolute = value => value && value !== "-" && !isAbsolute(value) ? resolve(value) : value; +const positionals = []; +for (let index = 0; index < command.length && command[index] !== "--"; index++) { + const arg = command[index]; + const equals = arg.indexOf("="); + if (!arg.startsWith("-") || arg === "-") positionals.push(index); + else if (equals > 0) { if (pathFlags.has(arg.slice(0, equals))) command[index] = arg.slice(0, equals + 1) + absolute(arg.slice(equals + 1)); } + else if (valued.has(arg) && ++index < command.length && pathFlags.has(arg)) command[index] = absolute(command[index]); +} +const [, ...operands] = positionals; +const operand = position => operands[position] === undefined ? undefined : command[operands[position]]; +const pathOperands = { + install: [operands.length - 1], + reinstall: [1], + screenshot: [0], + replay: [operand(0) === "export" ? 1 : 0], + test: operands.map((_, position) => position), + record: ["start", "contact-sheet"].includes(operand(0)) ? [1] : [], + trace: [1], + diff: operand(0) === "screenshot" ? [1] : [], + session: operand(0) === "save-script" ? [1] : [], +}[name] ?? []; +for (const position of pathOperands) if (operands[position] !== undefined) command[operands[position]] = absolute(command[operands[position]]); +// A push payload is a file or inline JSON. +if (name === "push" && operands[1] !== undefined && existsSync(absolute(command[operands[1]]))) command[operands[1]] = absolute(command[operands[1]]); const env = { ...process.env, ELECTRON_RUN_AS_NODE: "1" }; delete env.AGENT_DEVICE_DAEMON_BASE_URL; delete env.AGENT_DEVICE_DAEMON_AUTH_TOKEN; delete env.AGENT_DEVICE_CONFIG; -const child = spawn(${JSON.stringify(node)}, [entry, ...command], { stdio: "inherit", env }); +const child = spawn(${JSON.stringify(node)}, [entry, ...command], { stdio: "inherit", env, cwd: dirname(fileURLToPath(import.meta.url)) }); child.on("error", error => { console.error(error.message); process.exitCode = 1; }); child.on("exit", code => { process.exitCode = code ?? 1; }); `; diff --git a/apps/server/src/device/AgentDeviceTarget.test.ts b/apps/server/src/device/AgentDeviceTarget.test.ts index ccd44e29113a1..11262814c0c9d 100644 --- a/apps/server/src/device/AgentDeviceTarget.test.ts +++ b/apps/server/src/device/AgentDeviceTarget.test.ts @@ -144,6 +144,21 @@ exec '${process.execPath.replaceAll("'", "'\"'\"'")}' "$@" "--config", "/h.json", ]); + // agent-device runs in the launcher's directory, so relative paths resolve against the caller's. + expect( + yield* Effect.promise(() => + launch(bound, ["install", "com.example.app", "b/App.app", "--out=o.png"]), + ), + ).toEqual([ + "install", + "com.example.app", + path.resolve("b/App.app"), + `--out=${path.resolve("o.png")}`, + ...target, + ]); + expect( + yield* Effect.promise(() => launch(bound, ["push", "com.example.app", '{"a":1}'])), + ).toEqual(["push", "com.example.app", '{"a":1}', ...target]); // After `--` everything is text: no help, no overrides, and bound flags go before it. for (const text of [["--help"], ["literal text", "--session", "mine"]]) { expect(yield* Effect.promise(() => launch(bound, ["fill", "@e5", "--", ...text]))).toEqual([ From 431f190c95c1e63a51fc2e23d8e21a46ac05a9bf Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Wed, 18 Nov 2026 13:48:32 +0000 Subject: [PATCH 3/8] fix(server): keep relative test reporters working from the bound launcher agent-device loads a custom reporter (a --reporter spec starting with ".") and writes junit: reports relative to its own cwd, which the launcher now pins to its own directory. Resolve both against the caller's directory; a module path becomes a file URL so agent-device still treats it as custom on Windows. Built-in reporter names are left alone. Co-Authored-By: Claude Opus 5.5 --- apps/server/src/device/AgentDeviceShim.ts | 9 +++++--- .../src/device/AgentDeviceTarget.test.ts | 23 +++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/apps/server/src/device/AgentDeviceShim.ts b/apps/server/src/device/AgentDeviceShim.ts index 826d24a3ad40a..cdfd0282740ca 100644 --- a/apps/server/src/device/AgentDeviceShim.ts +++ b/apps/server/src/device/AgentDeviceShim.ts @@ -186,7 +186,7 @@ const launcherSource = (node: string, entryPath: string, boundArgs: ReadonlyArra import { existsSync, readFileSync } from "node:fs"; import { connect } from "node:net"; import { dirname, isAbsolute, resolve } from "node:path"; -import { fileURLToPath } from "node:url"; +import { fileURLToPath, pathToFileURL } from "node:url"; const entry = ${JSON.stringify(entryPath)}; const bound = ${JSON.stringify(boundArgs)}; const refused = new Map([["cdp", ["--platform", "--udid", "--config", "--session"]], ["connection", ["--platform", "--udid"]]]); @@ -244,13 +244,16 @@ if (local) { } const pathFlags = new Set(${JSON.stringify(PATH_FLAGS)}); const absolute = value => value && value !== "-" && !isAbsolute(value) ? resolve(value) : value; +// A reporter starting with "." is a module path; a file URL keeps it one on Windows too. +const reporter = spec => spec.trim().startsWith(".") ? pathToFileURL(resolve(spec.trim())).href : spec.trim().startsWith("junit:") ? "junit:" + absolute(spec.trim().slice(6)) : spec; +const flagValue = (flag, value) => flag === "--reporter" ? reporter(value) : pathFlags.has(flag) ? absolute(value) : value; const positionals = []; for (let index = 0; index < command.length && command[index] !== "--"; index++) { const arg = command[index]; const equals = arg.indexOf("="); if (!arg.startsWith("-") || arg === "-") positionals.push(index); - else if (equals > 0) { if (pathFlags.has(arg.slice(0, equals))) command[index] = arg.slice(0, equals + 1) + absolute(arg.slice(equals + 1)); } - else if (valued.has(arg) && ++index < command.length && pathFlags.has(arg)) command[index] = absolute(command[index]); + else if (equals > 0) command[index] = arg.slice(0, equals + 1) + flagValue(arg.slice(0, equals), arg.slice(equals + 1)); + else if (valued.has(arg) && ++index < command.length) command[index] = flagValue(arg, command[index]); } const [, ...operands] = positionals; const operand = position => operands[position] === undefined ? undefined : command[operands[position]]; diff --git a/apps/server/src/device/AgentDeviceTarget.test.ts b/apps/server/src/device/AgentDeviceTarget.test.ts index 11262814c0c9d..4826c6d861759 100644 --- a/apps/server/src/device/AgentDeviceTarget.test.ts +++ b/apps/server/src/device/AgentDeviceTarget.test.ts @@ -3,6 +3,7 @@ import * as HostProcess from "@t3tools/shared/HostProcess"; import { describe, expect, it } from "@effect/vitest"; import * as NodeChildProcess from "node:child_process"; import * as NodeNet from "node:net"; +import * as NodeURL from "node:url"; import * as NodeUtil from "node:util"; import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; import * as NodeServices from "@effect/platform-node/NodeServices"; @@ -156,6 +157,28 @@ exec '${process.execPath.replaceAll("'", "'\"'\"'")}' "$@" `--out=${path.resolve("o.png")}`, ...target, ]); + expect( + yield* Effect.promise(() => + launch(bound, [ + "test", + "sample.ad", + "--reporter", + "./reporter.mjs", + "--reporter=junit:out/j.xml", + "--reporter", + "default", + ]), + ), + ).toEqual([ + "test", + path.resolve("sample.ad"), + "--reporter", + NodeURL.pathToFileURL(path.resolve("reporter.mjs")).href, + `--reporter=junit:${path.resolve("out/j.xml")}`, + "--reporter", + "default", + ...target, + ]); expect( yield* Effect.promise(() => launch(bound, ["push", "com.example.app", '{"a":1}'])), ).toEqual(["push", "com.example.app", '{"a":1}', ...target]); From 375958287a37abb4eff337e21bc7a92244ecc73c Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Wed, 18 Nov 2026 14:00:00 +0000 Subject: [PATCH 4/8] fix(server): run a Simlock lease's launcher from the directory that opened it A Simlock lease opens its agent-device session from the thread's workspace (installDirectory), and agent-device ties the device claim to that workspace. The bound launcher ran agent-device from its own directory, so install was refused for every Simlock lease. The bound launcher now runs agent-device from the directory that opened the session: installDirectory for a Simlock lease, its own directory otherwise. The unbound shim keeps the caller's cwd and passes paths through unchanged, as before. Co-Authored-By: Claude Opus 5.5 --- apps/server/src/device/AgentDeviceShim.ts | 32 ++++++++++++++----- .../src/device/AgentDeviceTarget.test.ts | 21 ++++++++++-- apps/server/src/device/DeviceService.ts | 5 ++- .../src/mcp/toolkits/device/handlers.ts | 6 +++- 4 files changed, 52 insertions(+), 12 deletions(-) diff --git a/apps/server/src/device/AgentDeviceShim.ts b/apps/server/src/device/AgentDeviceShim.ts index cdfd0282740ca..d20e77ed284a2 100644 --- a/apps/server/src/device/AgentDeviceShim.ts +++ b/apps/server/src/device/AgentDeviceShim.ts @@ -153,8 +153,8 @@ const VALUE_FLAGS: ReadonlyArray = [ /** * agent-device 0.21.36's flags whose value is a path. It ties a session's device claim to its cwd's - * workspace and refuses install, push and the like from another, so the launcher runs it in its own - * directory and resolves these, and path operands, against the caller's. + * workspace and refuses install, push and the like from another, so a bound launcher runs it in the + * directory that opened the session and resolves these, and path operands, against the caller's. */ const PATH_FLAGS: ReadonlyArray = [ "--artifact", @@ -181,13 +181,19 @@ const PATH_FLAGS: ReadonlyArray = [ // agent-device 0.21.31 refuses some bound flags per command; connect/disconnect/auth/daemon would // replace T3's connection; device status reads this machine's claims. It reports a sandboxed shell and a // dropped tunnel alike, so check first, at --daemon-base-url if given, as agent-device prefers it. -const launcherSource = (node: string, entryPath: string, boundArgs: ReadonlyArray) => +const launcherSource = ( + node: string, + entryPath: string, + boundArgs: ReadonlyArray, + cwd: string | undefined, +) => `import { spawn } from "node:child_process"; import { existsSync, readFileSync } from "node:fs"; import { connect } from "node:net"; -import { dirname, isAbsolute, resolve } from "node:path"; -import { fileURLToPath, pathToFileURL } from "node:url"; +import { isAbsolute, resolve } from "node:path"; +import { pathToFileURL } from "node:url"; const entry = ${JSON.stringify(entryPath)}; +const workspace = ${JSON.stringify(cwd ?? null)}; const bound = ${JSON.stringify(boundArgs)}; const refused = new Map([["cdp", ["--platform", "--udid", "--config", "--session"]], ["connection", ["--platform", "--udid"]]]); const managed = ["connect", "disconnect", "auth", "daemon"]; @@ -242,6 +248,7 @@ if (local) { process.exit(1); } } +if (workspace) { const pathFlags = new Set(${JSON.stringify(PATH_FLAGS)}); const absolute = value => value && value !== "-" && !isAbsolute(value) ? resolve(value) : value; // A reporter starting with "." is a module path; a file URL keeps it one on Windows too. @@ -271,11 +278,12 @@ const pathOperands = { for (const position of pathOperands) if (operands[position] !== undefined) command[operands[position]] = absolute(command[operands[position]]); // A push payload is a file or inline JSON. if (name === "push" && operands[1] !== undefined && existsSync(absolute(command[operands[1]]))) command[operands[1]] = absolute(command[operands[1]]); +} const env = { ...process.env, ELECTRON_RUN_AS_NODE: "1" }; delete env.AGENT_DEVICE_DAEMON_BASE_URL; delete env.AGENT_DEVICE_DAEMON_AUTH_TOKEN; delete env.AGENT_DEVICE_CONFIG; -const child = spawn(${JSON.stringify(node)}, [entry, ...command], { stdio: "inherit", env, cwd: dirname(fileURLToPath(import.meta.url)) }); +const child = spawn(${JSON.stringify(node)}, [entry, ...command], { stdio: "inherit", env, ...(workspace ? { cwd: workspace } : {}) }); child.on("error", error => { console.error(error.message); process.exitCode = 1; }); child.on("exit", code => { process.exitCode = code ?? 1; }); `; @@ -285,6 +293,7 @@ const writeLauncher = Effect.fn("AgentDeviceShim.writeLauncher")(function* (inpu readonly dir: string; readonly entryPath: string; readonly boundArgs: ReadonlyArray; + readonly cwd?: string; }) { const fs = yield* FileSystem.FileSystem; const path = yield* Path.Path; @@ -292,7 +301,10 @@ const writeLauncher = Effect.fn("AgentDeviceShim.writeLauncher")(function* (inpu const node = yield* resolveNodeExecutable("Device automation"); yield* fs.makeDirectory(input.dir, { recursive: true }); const launcherPath = path.join(input.dir, "agent-device-launcher.mjs"); - yield* fs.writeFileString(launcherPath, launcherSource(node, input.entryPath, input.boundArgs)); + yield* fs.writeFileString( + launcherPath, + launcherSource(node, input.entryPath, input.boundArgs, input.cwd), + ); if (platform === "win32") { const script = `@echo off\r\nsetlocal EnableExtensions DisableDelayedExpansion\r\nif not exist "${node}" (echo ${STALE_RUNTIME} 1>&2 & exit /b 1)\r\nset "ELECTRON_RUN_AS_NODE=1"\r\n"${node}" "${launcherPath}" %*\r\n`; const scriptPath = path.join(input.dir, "agent-device.cmd"); @@ -326,12 +338,16 @@ export const bindAgentDeviceLauncher = Effect.fn("AgentDeviceShim.bind")(functio readonly stateDir: string; readonly session: string; readonly targetArgs: ReadonlyArray; + /** The directory that opened the session, whose workspace agent-device ties the device claim to. */ + readonly cwd?: string | undefined; }) { const path = yield* Path.Path; + const dir = path.join(input.stateDir, TARGETS_DIR, input.session); return yield* writeLauncher({ - dir: path.join(input.stateDir, TARGETS_DIR, input.session), + dir, entryPath: input.entryPath, boundArgs: input.targetArgs, + cwd: input.cwd ?? dir, }); }); diff --git a/apps/server/src/device/AgentDeviceTarget.test.ts b/apps/server/src/device/AgentDeviceTarget.test.ts index 4826c6d861759..76fe122862c0a 100644 --- a/apps/server/src/device/AgentDeviceTarget.test.ts +++ b/apps/server/src/device/AgentDeviceTarget.test.ts @@ -319,13 +319,17 @@ exec '${process.execPath.replaceAll("'", "'\"'\"'")}' "$@" "t3-a", ], }); - const launchFrom = Effect.fn(function* (repo: string, args: ReadonlyArray) { + const launchFrom = Effect.fn(function* ( + repo: string, + args: ReadonlyArray, + launcher = command, + ) { const cwd = path.join(dir, repo); yield* fs.makeDirectory(path.join(cwd, ".git"), { recursive: true }); const { stdout } = yield* Effect.promise(() => exec( process.execPath, - [path.join(path.dirname(command), "agent-device-launcher.mjs"), ...args], + [path.join(path.dirname(launcher), "agent-device-launcher.mjs"), ...args], { cwd, }, @@ -340,6 +344,19 @@ exec '${process.execPath.replaceAll("'", "'\"'\"'")}' "$@" "/b/App.app", ]); expect(installed).toBe(opened); + // A Simlock lease opened its claim from the thread's workspace, so its launcher runs there. + const workspace = path.join(dir, "workspace"); + yield* fs.makeDirectory(workspace); + const leased = yield* bindAgentDeviceLauncher({ + entryPath, + stateDir: dir, + session: "t3-lease", + targetArgs: ["--config", "/h.json", "--session", "t3-lease"], + cwd: workspace, + }); + expect( + yield* launchFrom("coordinator", ["install", "com.example.app", "/b/App.app"], leased), + ).toBe(workspace); }).pipe( Effect.scoped, Effect.provideService(HostProcess.IsExecutable, false), diff --git a/apps/server/src/device/DeviceService.ts b/apps/server/src/device/DeviceService.ts index cf8aece6745e0..fdb6c4ee4361a 100644 --- a/apps/server/src/device/DeviceService.ts +++ b/apps/server/src/device/DeviceService.ts @@ -198,6 +198,7 @@ export class DeviceService extends Context.Service< readonly hostId: DeviceHostId; readonly deviceId: DeviceId; readonly targetArgs: ReadonlyArray; + readonly installDirectory?: string | undefined; }) => Effect.Effect, DeviceError>; readonly state: Effect.Effect; readonly subscribe: Effect.Effect, never, Scope.Scope>; @@ -261,6 +262,7 @@ export interface AgentDeviceLaunchers { readonly bind: (input: { readonly session: string; readonly targetArgs: ReadonlyArray; + readonly cwd?: string | undefined; }) => Effect.Effect; /** Removes every launcher whose session is not in `live`. */ readonly prune: (live: ReadonlySet) => Effect.Effect; @@ -1197,6 +1199,7 @@ export const makeWithHosts = Effect.fn("DeviceService.makeWithHosts")(function* }), agentLauncher: Effect.fn("DeviceService.agentLauncher")(function* ({ targetArgs, + installDirectory, ...target }) { if (!launchers) @@ -1221,7 +1224,7 @@ export const makeWithHosts = Effect.fn("DeviceService.makeWithHosts")(function* ) ) return Option.none(); - const launcher = yield* launchers.bind({ session, targetArgs }); + const launcher = yield* launchers.bind({ session, targetArgs, cwd: installDirectory }); boundLaunchers.set(session, launcher); return Option.some(launcher); }), diff --git a/apps/server/src/mcp/toolkits/device/handlers.ts b/apps/server/src/mcp/toolkits/device/handlers.ts index 099a9bae9e2d3..5b650f5f76be4 100644 --- a/apps/server/src/mcp/toolkits/device/handlers.ts +++ b/apps/server/src/mcp/toolkits/device/handlers.ts @@ -290,7 +290,11 @@ const handlers = { : agentDeviceTargetArgs({ ...device, id: agent.agentDeviceId })), ...agent.args, ]; - const launcher = yield* devices.agentLauncher({ ...opened, targetArgs }); + const launcher = yield* devices.agentLauncher({ + ...opened, + targetArgs, + installDirectory: agent.installDirectory, + }); if (Option.isNone(launcher)) { return yield* new DeviceToolUnavailableError({ reason: From afb9426d0f2ad58adefc68d839d7ebe84130a6a7 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Wed, 18 Nov 2026 14:25:39 +0000 Subject: [PATCH 5/8] fix(server): keep -e/--env and --pick values intact in the agent-device launcher The launcher's list of value-taking flags missed -e/--env and --pick, so in `test flow.ad --env APP_ID=x` the KEY=VALUE was read as a test path and made absolute. Co-Authored-By: Claude Opus 5.5 --- apps/server/src/device/AgentDeviceShim.ts | 3 +++ apps/server/src/device/AgentDeviceTarget.test.ts | 8 ++++++++ 2 files changed, 11 insertions(+) diff --git a/apps/server/src/device/AgentDeviceShim.ts b/apps/server/src/device/AgentDeviceShim.ts index d20e77ed284a2..03e7abd641249 100644 --- a/apps/server/src/device/AgentDeviceShim.ts +++ b/apps/server/src/device/AgentDeviceShim.ts @@ -36,6 +36,7 @@ const VALUE_FLAGS: ReadonlyArray = [ "--crop-on", "--custom-network", "-d", + "-e", "--daemon-auth-token", "--daemon-base-url", "--daemon-server-mode", @@ -46,6 +47,7 @@ const VALUE_FLAGS: ReadonlyArray = [ "--device-orientation", "--dsym", "--duration-ms", + "--env", "--fps", "--from", "--geo-location", @@ -85,6 +87,7 @@ const VALUE_FLAGS: ReadonlyArray = [ "--out", "--pattern", "--pause-ms", + "--pick", "--pixel-density", "--pixels", "--plan-digest", diff --git a/apps/server/src/device/AgentDeviceTarget.test.ts b/apps/server/src/device/AgentDeviceTarget.test.ts index 76fe122862c0a..2fbeaaa00a909 100644 --- a/apps/server/src/device/AgentDeviceTarget.test.ts +++ b/apps/server/src/device/AgentDeviceTarget.test.ts @@ -162,6 +162,10 @@ exec '${process.execPath.replaceAll("'", "'\"'\"'")}' "$@" launch(bound, [ "test", "sample.ad", + "--env", + "APP_ID=a", + "-e", + "B=b", "--reporter", "./reporter.mjs", "--reporter=junit:out/j.xml", @@ -172,6 +176,10 @@ exec '${process.execPath.replaceAll("'", "'\"'\"'")}' "$@" ).toEqual([ "test", path.resolve("sample.ad"), + "--env", + "APP_ID=a", + "-e", + "B=b", "--reporter", NodeURL.pathToFileURL(path.resolve("reporter.mjs")).href, `--reporter=junit:${path.resolve("out/j.xml")}`, From 013714d8587402d3fc9217c3091a4e87820ed84b Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 19 Nov 2026 22:34:02 +0000 Subject: [PATCH 6/8] fix(server): route the shared agent-device launcher through a bound session's launcher The shared launcher ran agent-device in the caller's directory while the bound one runs it in the session's directory, so an install through one after an open through the other met a different workspace. Given a --session with a bound launcher, the shared one now hands off to it. Co-Authored-By: Claude Opus 5.5 --- apps/server/src/device/AgentDeviceShim.ts | 22 ++++++++++++++++--- .../src/device/AgentDeviceTarget.test.ts | 20 +++++++++++++++-- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/apps/server/src/device/AgentDeviceShim.ts b/apps/server/src/device/AgentDeviceShim.ts index 03e7abd641249..6dc47e39520c1 100644 --- a/apps/server/src/device/AgentDeviceShim.ts +++ b/apps/server/src/device/AgentDeviceShim.ts @@ -189,11 +189,12 @@ const launcherSource = ( entryPath: string, boundArgs: ReadonlyArray, cwd: string | undefined, + targetsDir: string | undefined, ) => `import { spawn } from "node:child_process"; import { existsSync, readFileSync } from "node:fs"; import { connect } from "node:net"; -import { isAbsolute, resolve } from "node:path"; +import { isAbsolute, join, resolve } from "node:path"; import { pathToFileURL } from "node:url"; const entry = ${JSON.stringify(entryPath)}; const workspace = ${JSON.stringify(cwd ?? null)}; @@ -203,6 +204,15 @@ const managed = ["connect", "disconnect", "auth", "daemon"]; const args = process.argv.slice(2); const end = args.indexOf("--"); const options = end < 0 ? args : args.slice(0, end); +// The unbound launcher hands a bound session to its own launcher, so both run in one workspace. +const targets = ${JSON.stringify(targetsDir ?? null)}; +const sessionAt = options.indexOf("--session"); +const session = options.find(arg => arg.startsWith("--session="))?.slice(10) ?? (sessionAt >= 0 ? options[sessionAt + 1] : undefined); +const delegate = targets && session && /^[\\w.-]+$/.test(session) && !/^\\.+$/.test(session) ? join(targets, session, "agent-device-launcher.mjs") : undefined; +if (delegate && existsSync(delegate)) { + const child = spawn(process.execPath, [delegate, ...args], { stdio: "inherit" }); + process.exit(await new Promise(done => { child.on("error", error => { console.error(error.message); done(1); }); child.on("exit", code => done(code ?? 1)); })); +} const valued = new Set(${JSON.stringify(VALUE_FLAGS)}); let name = ""; for (let index = 0; index < args.length; index++) { @@ -297,6 +307,7 @@ const writeLauncher = Effect.fn("AgentDeviceShim.writeLauncher")(function* (inpu readonly entryPath: string; readonly boundArgs: ReadonlyArray; readonly cwd?: string; + readonly targetsDir?: string; }) { const fs = yield* FileSystem.FileSystem; const path = yield* Path.Path; @@ -306,7 +317,7 @@ const writeLauncher = Effect.fn("AgentDeviceShim.writeLauncher")(function* (inpu const launcherPath = path.join(input.dir, "agent-device-launcher.mjs"); yield* fs.writeFileString( launcherPath, - launcherSource(node, input.entryPath, input.boundArgs, input.cwd), + launcherSource(node, input.entryPath, input.boundArgs, input.cwd, input.targetsDir), ); if (platform === "win32") { const script = `@echo off\r\nsetlocal EnableExtensions DisableDelayedExpansion\r\nif not exist "${node}" (echo ${STALE_RUNTIME} 1>&2 & exit /b 1)\r\nset "ELECTRON_RUN_AS_NODE=1"\r\n"${node}" "${launcherPath}" %*\r\n`; @@ -328,7 +339,12 @@ export const ensureAgentDeviceShim = Effect.fn("AgentDeviceShim.ensure")(functio }) { const path = yield* Path.Path; const shimDir = path.join(input.stateDir, SHIM_DIR); - yield* writeLauncher({ dir: shimDir, entryPath: input.entryPath, boundArgs: [] }); + yield* writeLauncher({ + dir: shimDir, + entryPath: input.entryPath, + boundArgs: [], + targetsDir: path.join(input.stateDir, TARGETS_DIR), + }); return shimDir; }); diff --git a/apps/server/src/device/AgentDeviceTarget.test.ts b/apps/server/src/device/AgentDeviceTarget.test.ts index 2fbeaaa00a909..878b6b8d421b3 100644 --- a/apps/server/src/device/AgentDeviceTarget.test.ts +++ b/apps/server/src/device/AgentDeviceTarget.test.ts @@ -202,9 +202,9 @@ exec '${process.execPath.replaceAll("'", "'\"'\"'")}' "$@" } expect( yield* Effect.promise(() => - launch(shared, ["snapshot", "--config=/h.json", "--session=t3-a"]), + launch(shared, ["snapshot", "--config=/h.json", "--session=t3-unbound"]), ), - ).toEqual(["snapshot", "--config=/h.json", "--session=t3-a"]); + ).toEqual(["snapshot", "--config=/h.json", "--session=t3-unbound"]); yield* Effect.promise(() => expect( launch(shared, ["fill", "@e5", "--", "--config", "/h.json", "--session", "t3-a"]), @@ -352,6 +352,22 @@ exec '${process.execPath.replaceAll("'", "'\"'\"'")}' "$@" "/b/App.app", ]); expect(installed).toBe(opened); + const shared = yield* ensureAgentDeviceShim({ entryPath, stateDir: dir }); + expect( + yield* launchFrom( + "other", + [ + "install", + "com.example.app", + "/b/App.app", + "--config", + "/h.json", + "--session", + "t3-a", + ], + path.join(shared, "agent-device"), + ), + ).toBe(opened); // A Simlock lease opened its claim from the thread's workspace, so its launcher runs there. const workspace = path.join(dir, "workspace"); yield* fs.makeDirectory(workspace); From 1acad2f1f4c861bbfb7013223ccc667044308462 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 19 Nov 2026 22:50:27 +0000 Subject: [PATCH 7/8] fix(server): metro prepare through the bound launcher uses the caller's project agent-device's metro prepare defaults --project-root to its own cwd, which the bound launcher now pins to the session's directory. Pass the caller's directory when neither --project-root nor --remote-config is given. Co-Authored-By: Claude Opus 5.5 --- apps/server/src/device/AgentDeviceShim.ts | 3 +++ apps/server/src/device/AgentDeviceTarget.test.ts | 13 +++++++++++++ 2 files changed, 16 insertions(+) diff --git a/apps/server/src/device/AgentDeviceShim.ts b/apps/server/src/device/AgentDeviceShim.ts index 6dc47e39520c1..24bb0499353c2 100644 --- a/apps/server/src/device/AgentDeviceShim.ts +++ b/apps/server/src/device/AgentDeviceShim.ts @@ -291,6 +291,9 @@ const pathOperands = { for (const position of pathOperands) if (operands[position] !== undefined) command[operands[position]] = absolute(command[operands[position]]); // A push payload is a file or inline JSON. if (name === "push" && operands[1] !== undefined && existsSync(absolute(command[operands[1]]))) command[operands[1]] = absolute(command[operands[1]]); +// metro prepare defaults --project-root to its cwd, which is no longer the caller's. +const given = flag => options.some(arg => arg === flag || arg.startsWith(flag + "=")); +if (name === "metro" && operand(0) === "prepare" && !given("--project-root") && !given("--remote-config")) command.splice(end < 0 ? command.length : command.indexOf("--"), 0, "--project-root", resolve(".")); } const env = { ...process.env, ELECTRON_RUN_AS_NODE: "1" }; delete env.AGENT_DEVICE_DAEMON_BASE_URL; diff --git a/apps/server/src/device/AgentDeviceTarget.test.ts b/apps/server/src/device/AgentDeviceTarget.test.ts index 878b6b8d421b3..63b9c4f008aef 100644 --- a/apps/server/src/device/AgentDeviceTarget.test.ts +++ b/apps/server/src/device/AgentDeviceTarget.test.ts @@ -190,6 +190,19 @@ exec '${process.execPath.replaceAll("'", "'\"'\"'")}' "$@" expect( yield* Effect.promise(() => launch(bound, ["push", "com.example.app", '{"a":1}'])), ).toEqual(["push", "com.example.app", '{"a":1}', ...target]); + expect( + yield* Effect.promise(() => + launch(bound, ["metro", "prepare", "--public-base-url", "https://h:8081"]), + ), + ).toEqual([ + "metro", + "prepare", + "--public-base-url", + "https://h:8081", + ...target, + "--project-root", + path.resolve("."), + ]); // After `--` everything is text: no help, no overrides, and bound flags go before it. for (const text of [["--help"], ["literal text", "--session", "mine"]]) { expect(yield* Effect.promise(() => launch(bound, ["fill", "@e5", "--", ...text]))).toEqual([ From 45253cae012bf60dd73ae105e0155d35d7cd27d4 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 19 Nov 2026 23:08:58 +0000 Subject: [PATCH 8/8] fix(server): bound agent-device launcher rebases path operands after -- The launcher stopped collecting operands at the separator, so a relative file after it resolved inside the session directory. Flags are still not read past --, but operands there are. Co-Authored-By: Claude Opus 5.5 --- apps/server/src/device/AgentDeviceShim.ts | 6 +++++- apps/server/src/device/AgentDeviceTarget.test.ts | 5 +++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/apps/server/src/device/AgentDeviceShim.ts b/apps/server/src/device/AgentDeviceShim.ts index 24bb0499353c2..580c186f8c146 100644 --- a/apps/server/src/device/AgentDeviceShim.ts +++ b/apps/server/src/device/AgentDeviceShim.ts @@ -268,8 +268,12 @@ const absolute = value => value && value !== "-" && !isAbsolute(value) ? resolve const reporter = spec => spec.trim().startsWith(".") ? pathToFileURL(resolve(spec.trim())).href : spec.trim().startsWith("junit:") ? "junit:" + absolute(spec.trim().slice(6)) : spec; const flagValue = (flag, value) => flag === "--reporter" ? reporter(value) : pathFlags.has(flag) ? absolute(value) : value; const positionals = []; -for (let index = 0; index < command.length && command[index] !== "--"; index++) { +// After the first \`--\` every argument is an operand, and agent-device still reads paths there. +let literal = false; +for (let index = 0; index < command.length; index++) { const arg = command[index]; + if (literal) { positionals.push(index); continue; } + if (arg === "--") { literal = true; continue; } const equals = arg.indexOf("="); if (!arg.startsWith("-") || arg === "-") positionals.push(index); else if (equals > 0) command[index] = arg.slice(0, equals + 1) + flagValue(arg.slice(0, equals), arg.slice(equals + 1)); diff --git a/apps/server/src/device/AgentDeviceTarget.test.ts b/apps/server/src/device/AgentDeviceTarget.test.ts index 63b9c4f008aef..4ade6ebe93075 100644 --- a/apps/server/src/device/AgentDeviceTarget.test.ts +++ b/apps/server/src/device/AgentDeviceTarget.test.ts @@ -190,6 +190,11 @@ exec '${process.execPath.replaceAll("'", "'\"'\"'")}' "$@" expect( yield* Effect.promise(() => launch(bound, ["push", "com.example.app", '{"a":1}'])), ).toEqual(["push", "com.example.app", '{"a":1}', ...target]); + expect( + yield* Effect.promise(() => + launch(bound, ["replay", "export", "--out", "/o.yaml", "--", "flow.ad"]), + ), + ).toEqual(["replay", "export", "--out", "/o.yaml", ...target, "--", path.resolve("flow.ad")]); expect( yield* Effect.promise(() => launch(bound, ["metro", "prepare", "--public-base-url", "https://h:8081"]),