Skip to content

Commit ae39cfb

Browse files
fix(server): resolve Node for standalone helper scripts
1 parent 0f5a151 commit ae39cfb

17 files changed

Lines changed: 342 additions & 33 deletions

‎apps/server/src/device/AgentDeviceShim.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
// @effect-diagnostics preferSchemaOverJson:off - JSON string literals embed paths safely into generated JavaScript.
22
/**
33
* A directory holding an `agent-device` launcher that runs the pinned install
4-
* with the server's Node. Prepended to provider subprocess PATHs so the agent
4+
* with a Node runtime. Prepended to provider subprocess PATHs so the agent
55
* types `agent-device …` and gets the version the injected instructions were
66
* written for, regardless of what is or is not globally installed.
77
*/
88
import { HostProcessPlatform } from "@t3tools/shared/hostProcess";
9+
import { resolveNodeExecutable } from "@t3tools/shared/nodeRuntime";
910
import * as Effect from "effect/Effect";
1011
import * as FileSystem from "effect/FileSystem";
1112
import * as Path from "effect/Path";
@@ -20,9 +21,9 @@ export const ensureAgentDeviceShim = Effect.fn("AgentDeviceShim.ensure")(functio
2021
const fs = yield* FileSystem.FileSystem;
2122
const path = yield* Path.Path;
2223
const platform = yield* HostProcessPlatform;
24+
const node = yield* resolveNodeExecutable("Device automation");
2325
const shimDir = path.join(input.stateDir, SHIM_DIR);
2426
yield* fs.makeDirectory(shimDir, { recursive: true });
25-
const node = process.execPath;
2627
const launcherPath = path.join(shimDir, "agent-device-launcher.mjs");
2728
yield* fs.writeFileString(
2829
launcherPath,

‎apps/server/src/device/AgentDeviceTarget.test.ts‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
// @effect-diagnostics nodeBuiltinImport:off - exercises concurrent real CLI subprocesses.
2-
import { HostProcessPlatform } from "@t3tools/shared/hostProcess";
2+
import {
3+
HostProcessExecutablePath,
4+
HostProcessIsExecutable,
5+
HostProcessPlatform,
6+
} from "@t3tools/shared/hostProcess";
37
import { describe, expect, it } from "@effect/vitest";
48
import * as NodeChildProcess from "node:child_process";
59
import * as NodeUtil from "node:util";
@@ -84,6 +88,11 @@ if (process.env.AGENT_DEVICE_DAEMON_BASE_URL) process.exit(2);`,
8488
).rejects.toThrow("Call device_open first"),
8589
);
8690
}
87-
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
91+
}).pipe(
92+
Effect.scoped,
93+
Effect.provideService(HostProcessIsExecutable, true),
94+
Effect.provideService(HostProcessExecutablePath, "/packaged/t3"),
95+
Effect.provide(NodeServices.layer),
96+
),
8897
);
8998
});

‎apps/server/src/device/DeviceHost.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ export class DeviceHostError extends Schema.TaggedError<DeviceHostError>()("Devi
2323
hostId: Schema.String,
2424
step: Schema.String,
2525
cause: Schema.Defect(),
26+
detail: Schema.optional(Schema.String),
2627
}) {
2728
override get message(): string {
28-
return `Device host ${this.hostId} failed while ${this.step}.`;
29+
return this.detail ?? `Device host ${this.hostId} failed while ${this.step}.`;
2930
}
3031
}
3132

‎apps/server/src/device/DeviceService.ts‎

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import {
3636
type ThreadId,
3737
} from "@t3tools/contracts";
3838
import * as FileSystem from "effect/FileSystem";
39+
import { resolveNodeExecutable } from "@t3tools/shared/nodeRuntime";
3940
import * as Path from "effect/Path";
4041
import { ensureAgentDevice } from "./DeviceToolchain.ts";
4142
import * as ServerConfig from "../config.ts";
@@ -1003,18 +1004,20 @@ export const make = Effect.gen(function* () {
10031004
);
10041005
return {
10051006
...service,
1006-
agentCli: ensureAgentDevice(config.baseDir).pipe(
1007+
agentCli: resolveNodeExecutable("Device automation").pipe(
1008+
Effect.flatMap(() => ensureAgentDevice(config.baseDir)),
10071009
Effect.provideService(FileSystem.FileSystem, fs),
10081010
Effect.provideService(Path.Path, path),
10091011
Effect.provideService(ProcessRunner.ProcessRunner, runner),
10101012
Effect.map((tool) => tool.entryPath),
1011-
Effect.mapError(
1012-
(error) =>
1013-
new DeviceOperationError({
1014-
operation: "install agent CLI",
1015-
reason: "command_failed",
1016-
cause: error,
1017-
}),
1013+
Effect.mapError((error) =>
1014+
error._tag === "NodeRuntimeUnavailableError"
1015+
? new DeviceHostUnavailableError({ hostId: LOCAL_DEVICE_HOST_ID, reason: error.message })
1016+
: new DeviceOperationError({
1017+
operation: "install agent CLI",
1018+
reason: "command_failed",
1019+
cause: error,
1020+
}),
10181021
),
10191022
),
10201023
};

‎apps/server/src/device/DeviceToolchain.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* `expo-device-hub` streams simulator and emulator screens and `agent-device`
55
* drives them. Each is npm-installed separately after its matching consent
66
* step into `<baseDir>/tools/<name>/<version>` and executed from there with the
7-
* server's own Node, never `npx`: an ephemeral
7+
* resolved Node runtime, never `npx`: an ephemeral
88
* npx cache would make every first `device_open` after a reboot depend on the
99
* registry, and the pinned versions are part of the contract the injected
1010
* agent instructions describe.
@@ -34,7 +34,7 @@ const installLock = Semaphore.makeUnsafe(1);
3434

3535
export interface DeviceToolPaths {
3636
readonly installDir: string;
37-
/** Absolute path of the tool's entry script, run with the server's Node. */
37+
/** Absolute path of the tool's entry script, run with a resolved Node runtime. */
3838
readonly entryPath: string;
3939
readonly sentinelPath: string;
4040
}

‎apps/server/src/device/LocalDeviceHost.test.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { describe, expect, it } from "@effect/vitest";
22
import * as NodePath from "@effect/platform-node/NodePath";
3-
import { HostProcessEnvironment, HostProcessPlatform } from "@t3tools/shared/hostProcess";
3+
import {
4+
HostProcessEnvironment,
5+
HostProcessPlatform,
6+
HostProcessIsExecutable,
7+
} from "@t3tools/shared/hostProcess";
48
import * as Effect from "effect/Effect";
59
import * as Layer from "effect/Layer";
610
import * as Path from "effect/Path";
@@ -157,6 +161,11 @@ it.effect(
157161
),
158162
);
159163
expect(yield* host.current).toBeNull();
164+
const error = yield* host
165+
.ensureReady(() => Effect.die("Must not install without Node"))
166+
.pipe(Effect.flip, Effect.provideService(HostProcessIsExecutable, true));
167+
expect(error.message).toContain("Local device support requires Node.js");
168+
expect(error.message).toContain("Install Node.js");
160169
yield* host.stop;
161170
expect(yield* fs.exists(`${baseDir}/tools`)).toBe(false);
162171
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),

‎apps/server/src/device/LocalDeviceHost.ts‎

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
} from "@t3tools/contracts";
1919
import { waitForHttpReady } from "@t3tools/shared/httpReadiness";
2020
import { HostProcessEnvironment, HostProcessPlatform } from "@t3tools/shared/hostProcess";
21+
import { resolveNodeExecutable } from "@t3tools/shared/nodeRuntime";
2122
import * as NetService from "@t3tools/shared/Net";
2223
import { isCommandAvailable } from "@t3tools/shared/shell";
2324
import * as Clock from "effect/Clock";
@@ -76,6 +77,7 @@ const AgentDeviceDaemonFile = Schema.Struct({
7677
const decodeDaemonFile = Schema.decodeUnknownEffect(Schema.fromJsonString(AgentDeviceDaemonFile));
7778

7879
interface HubProcess {
80+
readonly nodePath: string;
7981
readonly child: ChildProcessSpawner.ChildProcessHandle;
8082
readonly scope: Scope.Closeable;
8183
readonly origin: string;
@@ -310,6 +312,7 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () {
310312

311313
const spawnHub = Effect.fn("LocalDeviceHost.spawnHub")(function* (
312314
hubTool: DeviceToolPaths,
315+
nodePath: string,
313316
): Effect.fn.Return<HubProcess, DeviceHost.DeviceHostError> {
314317
yield* reapStaleHub;
315318
yield* fs
@@ -330,7 +333,7 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () {
330333
const child = yield* spawner
331334
.spawn(
332335
ChildProcess.make(
333-
process.execPath,
336+
nodePath,
334337
[
335338
hubTool.entryPath,
336339
"--port",
@@ -361,7 +364,7 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () {
361364
),
362365
);
363366
const startedAtMillis = yield* Clock.currentTimeMillis;
364-
const hub: HubProcess = { child, scope, origin, startedAtMillis };
367+
const hub: HubProcess = { child, scope, origin, startedAtMillis, nodePath };
365368
yield* Effect.forkIn(observeHubOutput(hub), scope);
366369
yield* waitForHttpReady({
367370
baseUrl: origin,
@@ -418,7 +421,7 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () {
418421
Effect.gen(function* () {
419422
const current = yield* Ref.get(runningRef);
420423
if (current?.hub.child.pid !== hub.child.pid) return;
421-
const replacement = yield* spawnHub(hubTool);
424+
const replacement = yield* spawnHub(hubTool, hub.nodePath);
422425
yield* Ref.set(runningRef, { ...current, hub: replacement });
423426
yield* Effect.forkDetach(superviseHub(replacement, hubTool));
424427
}),
@@ -441,6 +444,7 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () {
441444
*/
442445
const startAgentDeviceDaemon = Effect.fn("LocalDeviceHost.startAgentDeviceDaemon")(function* (
443446
agentTool: DeviceToolPaths,
447+
nodePath: string,
444448
): Effect.fn.Return<DeviceHost.AgentDeviceEndpoint, DeviceHost.DeviceHostTimeoutError> {
445449
const stateDir = agentDeviceStateDir(path, config.stateDir);
446450
yield* fs.makeDirectory(stateDir, { recursive: true }).pipe(Effect.ignore);
@@ -481,7 +485,7 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () {
481485
// daemon and blocks until it answers. `devices` is the cheapest one.
482486
yield* runner
483487
.run({
484-
command: process.execPath,
488+
command: nodePath,
485489
args: [agentTool.entryPath, "devices", "--json"],
486490
env: daemonEnvironment,
487491
timeout: Duration.millis(DAEMON_READY_TIMEOUT_MS),
@@ -502,11 +506,13 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () {
502506
}
503507
});
504508

505-
const stopAgentDeviceDaemon = (agentTool: DeviceToolPaths | null) =>
509+
const stopAgentDeviceDaemon = (
510+
agentTool: { readonly entryPath: string; readonly nodePath: string } | null,
511+
) =>
506512
agentTool
507513
? runner
508514
.run({
509-
command: process.execPath,
515+
command: agentTool.nodePath,
510516
args: [
511517
agentTool.entryPath,
512518
"daemon",
@@ -521,7 +527,7 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () {
521527
.pipe(Effect.ignore)
522528
: Effect.void;
523529

524-
let agentToolRef: DeviceToolPaths | null = null;
530+
let agentToolRef: { readonly entryPath: string; readonly nodePath: string } | null = null;
525531

526532
const ensureHubReady = Effect.fn("LocalDeviceHost.ensureHubReady")(function* (
527533
onPhase: (phase: "installing" | "starting") => Effect.Effect<void>,
@@ -532,6 +538,20 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () {
532538
if (alive) return running;
533539
yield* Ref.set(runningRef, null);
534540
}
541+
const nodePath = yield* resolveNodeExecutable("Local device support", hostEnvironment).pipe(
542+
Effect.provideService(FileSystem.FileSystem, fs),
543+
Effect.provideService(Path.Path, path),
544+
Effect.provideService(HostProcessPlatform, hostPlatform),
545+
Effect.mapError(
546+
(cause) =>
547+
new DeviceHost.DeviceHostError({
548+
hostId,
549+
step: "finding Node.js",
550+
detail: cause.message,
551+
cause,
552+
}),
553+
),
554+
);
535555
const installed = yield* isDeviceHubInstalled(config.baseDir).pipe(
536556
Effect.provideService(FileSystem.FileSystem, fs),
537557
Effect.provideService(Path.Path, path),
@@ -551,7 +571,7 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () {
551571
),
552572
);
553573
yield* onPhase("starting");
554-
const hub = yield* spawnHub(hubTool);
574+
const hub = yield* spawnHub(hubTool, nodePath);
555575
const candidate = helperPaths(hubTool);
556576
const [axExists, cliExists] = yield* Effect.all([
557577
fs.exists(candidate.serveSimAxSettings).pipe(Effect.orElseSucceed(() => false)),
@@ -600,9 +620,9 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () {
600620
}),
601621
),
602622
);
603-
agentToolRef = agentTool;
623+
agentToolRef = { entryPath: agentTool.entryPath, nodePath: running.hub.nodePath };
604624
yield* onPhase("starting");
605-
const agentDevice = yield* startAgentDeviceDaemon(agentTool);
625+
const agentDevice = yield* startAgentDeviceDaemon(agentTool, running.hub.nodePath);
606626
const next = { ...running, agentDevice };
607627
yield* Ref.set(runningRef, next);
608628
return { ...toReady(next), agentDevice };
@@ -652,7 +672,7 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () {
652672

653673
const toReady = (running: RunningHost): DeviceHost.DeviceHostReady => ({
654674
hub: { origin: running.hub.origin } satisfies DeviceHost.DeviceHubEndpoint,
655-
nodePath: process.execPath,
675+
nodePath: running.hub.nodePath,
656676
run,
657677
helpers: running.helpers,
658678
});

‎apps/server/src/mcp/toolkits/device/handlers.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,12 @@ const handlers = {
182182
stateDir: config.stateDir,
183183
}).pipe(
184184
Effect.mapError(
185-
() =>
185+
(error) =>
186186
new DeviceToolUnavailableError({
187-
reason: "Could not prepare the agent-device launcher.",
187+
reason:
188+
error._tag === "NodeRuntimeUnavailableError"
189+
? error.message
190+
: "Could not prepare the agent-device launcher.",
188191
}),
189192
),
190193
);

‎apps/server/src/provider/AntigravityInstallation.test.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { expect, it } from "@effect/vitest";
33
import {
44
HostProcessArchitecture,
55
HostProcessEnvironment,
6+
HostProcessIsExecutable,
67
HostProcessPlatform,
78
} from "@t3tools/shared/hostProcess";
89
import * as Deferred from "effect/Deferred";
@@ -242,6 +243,19 @@ const expectPreviousRelease = Effect.fn("test.expectPreviousAntigravityRelease")
242243
});
243244

244245
it.layer(NodeServices.layer)("Antigravity installation", (it) => {
246+
it.effect("reports missing Node before downloading the standalone provider runtime", () =>
247+
Effect.gen(function* () {
248+
const { installation, requests, validations } = yield* makeHarness();
249+
yield* installation.start;
250+
expect(yield* terminalState(installation)).toMatchObject({
251+
phase: "failed",
252+
message: expect.stringContaining("Install Node.js"),
253+
});
254+
expect(requests).toEqual([]);
255+
expect(validations).toEqual([]);
256+
}).pipe(Effect.provideService(HostProcessIsExecutable, true)),
257+
);
258+
245259
it.effect("verifies both files before activating a streamed download", () =>
246260
Effect.gen(function* () {
247261
const enteredValidation = yield* Deferred.make<void>();

‎apps/server/src/provider/AntigravityInstallation.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
HostProcessEnvironment,
77
HostProcessPlatform,
88
} from "@t3tools/shared/hostProcess";
9+
import { resolveNodeExecutable } from "@t3tools/shared/nodeRuntime";
910
import * as Clock from "effect/Clock";
1011
import * as Cause from "effect/Cause";
1112
import * as Context from "effect/Context";
@@ -517,6 +518,12 @@ export const makeAntigravityInstallation = Effect.fn("AntigravityInstallation.ma
517518

518519
const install = Effect.fn("AntigravityInstallation.install")(
519520
function* (asset: AntigravityReleaseAsset) {
521+
yield* resolveNodeExecutable("Antigravity", environment).pipe(
522+
Effect.provideService(FileSystem.FileSystem, fs),
523+
Effect.provideService(Path.Path, path),
524+
Effect.provideService(HostProcessPlatform, platform),
525+
Effect.mapError((cause) => installationError("verify", cause.message, cause)),
526+
);
520527
const report = (phase: ProviderInstallState["phase"], message: string | null) =>
521528
SubscriptionRef.update(state, (current) => ({ ...current, phase, message }));
522529
yield* fs.makeDirectory(versionsDirectory, { recursive: true });

0 commit comments

Comments
 (0)