The best way to control Claude Code, Codex and more from any computer or phone
fix(server): replace the device hub when emulator camera support changes (#39764)
Julius Marminge committed
Nov 20, 2026 at 07:05 UTC
71adede0f931822e3662f36d89d14b2bcb090164
2 files changed
+122
-13
apps/server/src/device/LocalDeviceHost.test.ts
+66
-3
@@ -48,7 +48,14 @@ const agentFixture = Effect.fn("agentFixture")(function* (
48
const fs = yield* FileSystem.FileSystem;
49
const path = yield* Path.Path;
50
const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-device-agent-start-" });
51
- const hub = { running: true, spawnFails: false, cleanup: Effect.void };
51
+ const hub = {
52
+ running: true,
53
+ spawnFails: false,
54
+ cleanup: Effect.void,
55
+ cameraHelp: "",
56
+ cameraProbes: 0,
57
+ spawnedEnvironments: [] as Array<NodeJS.ProcessEnv | undefined>,
58
+ };
59
const daemonStopRequested = yield* Deferred.make<ReadonlyArray<string>>();
60
const stateDir = agentDeviceStateDir(path, path.join(baseDir, "userdata"));
61
const daemonHealthy = yield* Ref.make(true);
@@ -74,7 +81,7 @@ const agentFixture = Effect.fn("agentFixture")(function* (
81
Effect.provideService(HostProcess.Platform, "darwin"),
82
Effect.provideService(
83
ChildProcessSpawner.ChildProcessSpawner,
77
- ChildProcessSpawner.make(() =>
84
+ ChildProcessSpawner.make((command) =>
85
hub.spawnFails
86
? Effect.fail(
87
PlatformError.systemError({
@@ -85,6 +92,17 @@ const agentFixture = Effect.fn("agentFixture")(function* (
92
}),
93
)
94
: Effect.addFinalizer(() => hub.cleanup).pipe(
95
+ Effect.tap(() =>
96
+ Effect.sync(() =>
97
+ hub.spawnedEnvironments.push(
98
+ (
99
+ command as unknown as {
100
+ readonly options: { readonly env?: NodeJS.ProcessEnv };
101
+ }
102
+ ).options.env,
103
+ ),
104
+ ),
105
+ ),
106
Effect.as(
107
ChildProcessSpawner.makeHandle({
108
pid: ChildProcessSpawner.ProcessId(123),
@@ -110,7 +128,13 @@ const agentFixture = Effect.fn("agentFixture")(function* (
128
? startup(stateDir)
129
: input.args.includes("daemon")
130
? Deferred.succeed(daemonStopRequested, input.args).pipe(Effect.as(processOutput(0)))
113
- : Effect.succeed(input.args.includes("-list-avds") ? avdOutput : processOutput(0));
131
+ : Effect.succeed(
132
+ input.args.includes("-list-avds")
133
+ ? avdOutput
134
+ : input.args[0]?.startsWith("-help-camera-")
135
+ ? (hub.cameraProbes++, processOutput(0, hub.cameraHelp))
136
+ : processOutput(0),
137
+ );
138
},
139
}),
140
Effect.provideService(
@@ -216,6 +240,45 @@ describe("AgentDevice startup", () => {
240
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
241
);
242
243
+ it.effect(
244
+ "drops emulated camera overrides once a running hub's emulator supports image files",
245
+ () =>
246
+ Effect.gen(function* () {
247
+ const fs = yield* FileSystem.FileSystem;
248
+ const sdk = yield* fs.makeTempDirectoryScoped({ prefix: "t3-device-camera-sdk-" });
249
+ for (const tool of ["platform-tools/adb", "emulator/emulator"]) {
250
+ yield* fs.makeDirectory(`${sdk}/${tool.split("/")[0]}`, { recursive: true });
251
+ yield* fs.writeFileString(`${sdk}/${tool}`, "");
252
+ }
253
+ const environment: NodeJS.ProcessEnv = { HOME: sdk, PATH: "", ANDROID_HOME: sdk };
254
+ const { host, hub } = yield* agentFixture(
255
+ () => Effect.succeed(processOutput(0)),
256
+ processOutput(0),
257
+ undefined,
258
+ environment,
259
+ );
260
+ // ensureReady is the hub's first start; the probe ran with the fixture's empty help.
261
+ hub.cameraHelp = "emulated none";
262
+ hub.running = false;
263
+ yield* host.ensureReady(() => Effect.void);
264
+ hub.running = true;
265
+ expect(hub.spawnedEnvironments.at(-1)?.EXPO_DEVICE_HUB_EMULATOR_EXTRA_ARGS).toBe(
266
+ "-camera-front emulated -camera-back emulated",
267
+ );
268
+
269
+ // A live hub with an unchanged emulator answers without probing it.
270
+ const probes = hub.cameraProbes;
271
+ yield* host.ensureReady(() => Effect.void);
272
+ expect(hub.cameraProbes).toBe(probes);
273
+
274
+ // The SDK's emulator is updated to one that renders image files while the hub keeps running.
275
+ yield* fs.writeFileString(`${sdk}/emulator/emulator`, "updated");
276
+ hub.cameraHelp = "emulated imagefile:<filename> none";
277
+ yield* host.ensureReady(() => Effect.void);
278
+ expect(hub.spawnedEnvironments.at(-1)?.EXPO_DEVICE_HUB_EMULATOR_EXTRA_ARGS).toBeUndefined();
279
+ }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
280
+ );
281
+
282
it.effect("reports an incompatible lock immediately and retains it for manual repair", () =>
283
Effect.gen(function* () {
284
const result = processOutput(
apps/server/src/device/LocalDeviceHost.ts
+56
-10
@@ -94,6 +94,8 @@ interface HubProcess {
94
readonly scope: Scope.Closeable;
95
readonly origin: string;
96
readonly startedAtMillis: number;
97
+ readonly emulatorExtraArgs: string | undefined;
98
+ readonly emulatorStamp: string | undefined;
99
}
100
101
interface RunningHost {
@@ -395,11 +397,36 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () {
397
Effect.ignore,
398
);
399
400
+ const currentHubEnvironment = Effect.gen(function* () {
401
+ return yield* hubEnvironment(yield* currentHostEnvironment).pipe(
402
+ Effect.provideService(FileSystem.FileSystem, fs),
403
+ Effect.provideService(Path.Path, path),
404
+ Effect.provideService(ProcessRunner.ProcessRunner, runner),
405
+ Effect.provideService(HostProcess.Platform, hostPlatform),
406
+ );
407
+ });
408
+
409
+ // The camera probe spawns the emulator; re-run it for a live hub only after the binary changes.
410
+ const emulatorStamp = Effect.gen(function* () {
411
+ const sdk = yield* currentSdk;
412
+ if (!sdk.root) return undefined;
413
+ const binary = path.join(
414
+ sdk.root,
415
+ "emulator",
416
+ hostPlatform === "win32" ? "emulator.exe" : "emulator",
417
+ );
418
+ const info = yield* fs.stat(binary).pipe(Effect.option);
419
+ if (Option.isNone(info)) return undefined;
420
+ return `${Option.getOrUndefined(info.value.mtime)?.getTime()}:${info.value.size}`;
421
+ });
422
+
423
const spawnHub = Effect.fn("LocalDeviceHost.spawnHub")(function* (
424
hubTool: DeviceToolPaths,
425
nodePath: string,
426
+ env: NodeJS.ProcessEnv,
427
): Effect.fn.Return<HubProcess, DeviceHost.DeviceHostError> {
428
yield* reapStaleHub;
429
+ const stamp = yield* emulatorStamp;
430
yield* fs
431
.makeDirectory(agentDeviceStateDir(path, config.stateDir), { recursive: true })
432
.pipe(Effect.ignore);
@@ -437,12 +464,7 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () {
464
shell: false,
465
stdout: "pipe",
466
stderr: "pipe",
440
- env: yield* hubEnvironment(yield* currentHostEnvironment).pipe(
441
- Effect.provideService(FileSystem.FileSystem, fs),
442
- Effect.provideService(Path.Path, path),
443
- Effect.provideService(ProcessRunner.ProcessRunner, runner),
444
- Effect.provideService(HostProcess.Platform, hostPlatform),
445
- ),
467
+ env,
468
},
469
),
470
)
@@ -458,7 +480,15 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () {
480
),
481
);
482
const startedAtMillis = yield* Clock.currentTimeMillis;
461
- const hub: HubProcess = { child, scope, origin, startedAtMillis, nodePath };
483
+ const hub: HubProcess = {
484
+ child,
485
+ scope,
486
+ origin,
487
+ startedAtMillis,
488
+ nodePath,
489
+ emulatorExtraArgs: env.EXPO_DEVICE_HUB_EMULATOR_EXTRA_ARGS,
490
+ emulatorStamp: stamp,
491
+ };
492
yield* Effect.forkIn(observeHubOutput(hub), scope);
493
yield* waitForHttpReady({
494
baseUrl: origin,
@@ -511,7 +541,11 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () {
541
Effect.gen(function* () {
542
const current = yield* Ref.get(runningRef);
543
if (current?.hub.child.pid !== hub.child.pid) return;
514
- const replacement = yield* spawnHub(hubTool, hub.nodePath);
544
+ const replacement = yield* spawnHub(
545
+ hubTool,
546
+ hub.nodePath,
547
+ yield* currentHubEnvironment,
548
+ );
549
yield* Ref.set(runningRef, { ...current, hub: replacement });
550
yield* Effect.forkDetach(superviseHub(replacement, hubTool));
551
}),
@@ -674,11 +708,23 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () {
708
onPhase: (phase: "installing" | "starting", detail?: string) => Effect.Effect<void>,
709
): Effect.fn.Return<RunningHost, DeviceHost.DeviceHostError | NodeRuntimeUnavailableError> {
710
const running = yield* Ref.get(runningRef);
711
+ let env: NodeJS.ProcessEnv | undefined;
712
// A dead hub's record stays until a replacement is published: the agent
713
// daemon outlives the hub, and close needs its endpoint if the restart fails.
714
if (running) {
715
const alive = yield* running.hub.child.isRunning.pipe(Effect.orElseSucceed(() => false));
681
- if (alive) return running;
716
+ if (alive) {
717
+ const stamp = yield* emulatorStamp;
718
+ if (stamp === running.hub.emulatorStamp) return running;
719
+ // An updated emulator can change the camera overrides; replace the hub only if they did.
720
+ env = yield* currentHubEnvironment;
721
+ if (env.EXPO_DEVICE_HUB_EMULATOR_EXTRA_ARGS === running.hub.emulatorExtraArgs) {
722
+ const next = { ...running, hub: { ...running.hub, emulatorStamp: stamp } };
723
+ yield* Ref.set(runningRef, next);
724
+ return next;
725
+ }
726
+ yield* stopHub(running.hub);
727
+ }
728
}
729
const nodePath = yield* resolveNodeExecutable(
730
"Local device support",
@@ -710,7 +756,7 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () {
756
),
757
);
758
yield* onPhase("starting");
713
- const hub = yield* spawnHub(hubTool, nodePath);
759
+ const hub = yield* spawnHub(hubTool, nodePath, env ?? (yield* currentHubEnvironment));
760
const next = yield* Effect.gen(function* () {
761
yield* pruneLocalDeviceTools(config.baseDir, nodePath, "hub").pipe(
762
Effect.provideService(Path.Path, path),