Skip to content

Commit 36b0cce

Browse files
fix(server): reject runtime hard links and preserve helper error causes
1 parent ae39cfb commit 36b0cce

12 files changed

Lines changed: 165 additions & 34 deletions

File tree

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ import type {
1818
import * as Context from "effect/Context";
1919
import * as Effect from "effect/Effect";
2020
import * as Schema from "effect/Schema";
21+
import type { NodeRuntimeUnavailableError } from "@t3tools/shared/nodeRuntime";
2122

2223
export class DeviceHostError extends Schema.TaggedError<DeviceHostError>()("DeviceHostError", {
2324
hostId: Schema.String,
2425
step: Schema.String,
2526
cause: Schema.Defect(),
26-
detail: Schema.optional(Schema.String),
2727
}) {
2828
override get message(): string {
29-
return this.detail ?? `Device host ${this.hostId} failed while ${this.step}.`;
29+
return `Device host ${this.hostId} failed while ${this.step}.`;
3030
}
3131
}
3232

@@ -89,11 +89,14 @@ export class DeviceHost extends Context.Service<
8989
*/
9090
readonly ensureReady: (
9191
onPhase: (phase: "installing" | "starting") => Effect.Effect<void>,
92-
) => Effect.Effect<DeviceHostReady, DeviceHostError>;
92+
) => Effect.Effect<DeviceHostReady, DeviceHostError | NodeRuntimeUnavailableError>;
9393
/** Installs and starts agent-device after the user grants agent access. */
9494
readonly ensureAgentReady: (
9595
onPhase: (phase: "installing" | "starting") => Effect.Effect<void>,
96-
) => Effect.Effect<DeviceHostAgentReady, DeviceHostError | DeviceHostTimeoutError>;
96+
) => Effect.Effect<
97+
DeviceHostAgentReady,
98+
DeviceHostError | DeviceHostTimeoutError | NodeRuntimeUnavailableError
99+
>;
97100
/** Current endpoints when already running, without starting anything. */
98101
readonly current: Effect.Effect<DeviceHostReady | null>;
99102
/** Stops only agent-device. Manual viewing through the hub stays available. */

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import * as Stream from "effect/Stream";
1515
import { HttpClient, HttpClientResponse } from "effect/unstable/http";
1616
import { ServerSettingsService } from "../serverSettings.ts";
1717
import * as DeviceHost from "./DeviceHost.ts";
18+
import { NodeRuntimeUnavailableError } from "@t3tools/shared/nodeRuntime";
1819

1920
import { type DeviceService, makeWithHosts, stateStream } from "./DeviceService.ts";
2021

@@ -61,6 +62,7 @@ const fixture = Effect.fn("fixture")(function* (
6162
onBoot: Effect.Effect<void> = Effect.void,
6263
bootError?: string,
6364
failListAfterShutdown = false,
65+
runtimeFailure?: NodeRuntimeUnavailableError,
6466
) {
6567
const settings = yield* Ref.make(DEFAULT_SERVER_SETTINGS);
6668
const starts: string[] = [];
@@ -88,12 +90,14 @@ const fixture = Effect.fn("fixture")(function* (
8890
platformAvailability: (platform) => Effect.succeed({ platform, available: true }),
8991
ensureReady: (onPhase) =>
9092
Effect.gen(function* () {
93+
if (runtimeFailure) return yield* runtimeFailure;
9194
starts.push("start");
9295
yield* onPhase("starting");
9396
return ready;
9497
}),
9598
ensureAgentReady: (onPhase) =>
9699
Effect.gen(function* () {
100+
if (runtimeFailure) return yield* runtimeFailure;
97101
agentStarts.push("start");
98102
yield* onPhase("starting");
99103
return {
@@ -188,6 +192,43 @@ const fixture = Effect.fn("fixture")(function* (
188192
});
189193

190194
describe("device setup consent", () => {
195+
it.effect(
196+
"preserves missing-runtime guidance and causes through manual and agent readiness",
197+
() =>
198+
Effect.gen(function* () {
199+
const underlying = new Error("private lookup diagnostics");
200+
const runtimeFailure = new NodeRuntimeUnavailableError({
201+
feature: "Local device support",
202+
cause: underlying,
203+
});
204+
const { service, settings, requests } = yield* fixture(
205+
Effect.void,
206+
undefined,
207+
false,
208+
runtimeFailure,
209+
);
210+
yield* Ref.update(settings, (current) => ({
211+
...current,
212+
enableDeviceSupport: true,
213+
enableAgentDeviceAccess: true,
214+
}));
215+
for (const readiness of [service.readiness(), service.agentReadinessIfSupported()]) {
216+
const error = yield* readiness.pipe(Effect.flip);
217+
expect(error).toMatchObject({
218+
_tag: "DeviceHostUnavailableError",
219+
reason: expect.stringContaining("Install Node.js"),
220+
cause: runtimeFailure,
221+
});
222+
expect(error.message).not.toContain(underlying.message);
223+
}
224+
expect((yield* service.state).hostStatuses[LOCAL_DEVICE_HOST_ID]).toMatchObject({
225+
status: "failed",
226+
detail: expect.stringContaining("Install Node.js"),
227+
});
228+
expect(requests).toEqual([]);
229+
}).pipe(Effect.scoped),
230+
);
231+
191232
it.effect("listing and provider startup do not start helpers before consent", () =>
192233
Effect.gen(function* () {
193234
const { service, starts, requests } = yield* fixture();

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

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import {
3636
type ThreadId,
3737
} from "@t3tools/contracts";
3838
import * as FileSystem from "effect/FileSystem";
39-
import { resolveNodeExecutable } from "@t3tools/shared/nodeRuntime";
39+
import { resolveNodeExecutable, nodeRuntimeUnavailableMessage } from "@t3tools/shared/nodeRuntime";
4040
import * as Path from "effect/Path";
4141
import { ensureAgentDevice } from "./DeviceToolchain.ts";
4242
import * as ServerConfig from "../config.ts";
@@ -263,7 +263,15 @@ export const makeWithHosts = Effect.fn("DeviceService.makeWithHosts")(function*
263263
setHostStatus(host.id, { status: "failed", detail: error.message }),
264264
),
265265
Effect.mapError(
266-
(error) => new DeviceHostUnavailableError({ hostId: host.id, reason: error.message }),
266+
(error) =>
267+
new DeviceHostUnavailableError({
268+
hostId: host.id,
269+
reason:
270+
error._tag === "NodeRuntimeUnavailableError"
271+
? nodeRuntimeUnavailableMessage("Local device support")
272+
: `Device host ${error.hostId} failed while ${error.step}.`,
273+
cause: error,
274+
}),
267275
),
268276
);
269277
if (hosts.get(host.id) !== host)
@@ -306,7 +314,17 @@ export const makeWithHosts = Effect.fn("DeviceService.makeWithHosts")(function*
306314
setHostStatus(host.id, { status: "failed", detail: error.message }),
307315
),
308316
Effect.mapError(
309-
(error) => new DeviceHostUnavailableError({ hostId: host.id, reason: error.message }),
317+
(error) =>
318+
new DeviceHostUnavailableError({
319+
hostId: host.id,
320+
reason:
321+
error._tag === "NodeRuntimeUnavailableError"
322+
? nodeRuntimeUnavailableMessage("Local device support")
323+
: error._tag === "DeviceHostTimeoutError"
324+
? `Device host ${error.hostId} did not start agent tools within ${error.timeoutMs} ms.`
325+
: `Device host ${error.hostId} failed while ${error.step}.`,
326+
cause: error,
327+
}),
310328
),
311329
);
312330
const hostSummaries = yield* Effect.forEach(hosts.values(), (candidate) => candidate.summary);
@@ -1012,7 +1030,11 @@ export const make = Effect.gen(function* () {
10121030
Effect.map((tool) => tool.entryPath),
10131031
Effect.mapError((error) =>
10141032
error._tag === "NodeRuntimeUnavailableError"
1015-
? new DeviceHostUnavailableError({ hostId: LOCAL_DEVICE_HOST_ID, reason: error.message })
1033+
? new DeviceHostUnavailableError({
1034+
hostId: LOCAL_DEVICE_HOST_ID,
1035+
reason: nodeRuntimeUnavailableMessage("Device automation"),
1036+
cause: error,
1037+
})
10161038
: new DeviceOperationError({
10171039
operation: "install agent CLI",
10181040
reason: "command_failed",

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

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ 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";
21+
import {
22+
resolveNodeExecutable,
23+
type NodeRuntimeUnavailableError,
24+
} from "@t3tools/shared/nodeRuntime";
2225
import * as NetService from "@t3tools/shared/Net";
2326
import { isCommandAvailable } from "@t3tools/shared/shell";
2427
import * as Clock from "effect/Clock";
@@ -531,7 +534,7 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () {
531534

532535
const ensureHubReady = Effect.fn("LocalDeviceHost.ensureHubReady")(function* (
533536
onPhase: (phase: "installing" | "starting") => Effect.Effect<void>,
534-
): Effect.fn.Return<RunningHost, DeviceHost.DeviceHostError> {
537+
): Effect.fn.Return<RunningHost, DeviceHost.DeviceHostError | NodeRuntimeUnavailableError> {
535538
const running = yield* Ref.get(runningRef);
536539
if (running) {
537540
const alive = yield* running.hub.child.isRunning.pipe(Effect.orElseSucceed(() => false));
@@ -542,15 +545,6 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () {
542545
Effect.provideService(FileSystem.FileSystem, fs),
543546
Effect.provideService(Path.Path, path),
544547
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-
),
554548
);
555549
const installed = yield* isDeviceHubInstalled(config.baseDir).pipe(
556550
Effect.provideService(FileSystem.FileSystem, fs),
@@ -597,7 +591,12 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () {
597591
const ensureAgentReady: DeviceHost.DeviceHost["Service"]["ensureAgentReady"] = (onPhase) =>
598592
startLock.withPermits(1)(
599593
Effect.gen(function* (): Generator<
600-
Effect.Effect<unknown, DeviceHost.DeviceHostError | DeviceHost.DeviceHostTimeoutError>,
594+
Effect.Effect<
595+
unknown,
596+
| DeviceHost.DeviceHostError
597+
| DeviceHost.DeviceHostTimeoutError
598+
| NodeRuntimeUnavailableError
599+
>,
601600
DeviceHost.DeviceHostAgentReady
602601
> {
603602
const running = yield* ensureHubReady(onPhase);

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import * as Path from "effect/Path";
1212
import { HostProcessPlatform } from "@t3tools/shared/hostProcess";
1313
import { ServerConfig } from "../../../config.ts";
1414
import { ensureAgentDeviceShim } from "../../../device/AgentDeviceShim.ts";
15+
import { nodeRuntimeUnavailableMessage } from "@t3tools/shared/nodeRuntime";
1516

1617
import * as DeviceService from "../../../device/DeviceService.ts";
1718
import * as McpInvocationContext from "../../McpInvocationContext.ts";
@@ -186,8 +187,9 @@ const handlers = {
186187
new DeviceToolUnavailableError({
187188
reason:
188189
error._tag === "NodeRuntimeUnavailableError"
189-
? error.message
190+
? nodeRuntimeUnavailableMessage("Device automation")
190191
: "Could not prepare the agent-device launcher.",
192+
cause: error,
191193
}),
192194
),
193195
);

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
HostProcessEnvironment,
77
HostProcessPlatform,
88
} from "@t3tools/shared/hostProcess";
9-
import { resolveNodeExecutable } from "@t3tools/shared/nodeRuntime";
9+
import { resolveNodeExecutable, nodeRuntimeUnavailableMessage } from "@t3tools/shared/nodeRuntime";
1010
import * as Clock from "effect/Clock";
1111
import * as Cause from "effect/Cause";
1212
import * as Context from "effect/Context";
@@ -522,7 +522,9 @@ export const makeAntigravityInstallation = Effect.fn("AntigravityInstallation.ma
522522
Effect.provideService(FileSystem.FileSystem, fs),
523523
Effect.provideService(Path.Path, path),
524524
Effect.provideService(HostProcessPlatform, platform),
525-
Effect.mapError((cause) => installationError("verify", cause.message, cause)),
525+
Effect.mapError((cause) =>
526+
installationError("verify", nodeRuntimeUnavailableMessage("Antigravity"), cause),
527+
),
526528
);
527529
const report = (phase: ProviderInstallState["phase"], message: string | null) =>
528530
SubscriptionRef.update(state, (current) => ({ ...current, phase, message }));

‎apps/server/src/provider/Drivers/AntigravityDriver.ts‎

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { withAgentDeviceEnvironment } from "../../mcp/McpProviderSession.ts";
22
import { AntigravitySettings, ProviderDriverKind, ProviderSetupError } from "@t3tools/contracts";
33
import { HostProcessPlatform } from "@t3tools/shared/hostProcess";
4+
import {
5+
NodeRuntimeUnavailableError,
6+
nodeRuntimeUnavailableMessage,
7+
} from "@t3tools/shared/nodeRuntime";
48
import * as Crypto from "effect/Crypto";
59
import * as Deferred from "effect/Deferred";
610
import * as Effect from "effect/Effect";
@@ -59,6 +63,7 @@ import { discoverAntigravitySkills, resolveAntigravityUserHome } from "./Antigra
5963

6064
const DRIVER = ProviderDriverKind.make("antigravity");
6165
const decodeSettings = Schema.decodeSync(AntigravitySettings);
66+
const isNodeRuntimeUnavailableError = Schema.is(NodeRuntimeUnavailableError);
6267

6368
export type AntigravityDriverEnv =
6469
| AntigravityInstallation
@@ -164,14 +169,15 @@ export const AntigravityDriver: ProviderDriver<AntigravitySettings, AntigravityD
164169
Effect.provideService(FileSystem.FileSystem, fileSystem),
165170
Effect.provideService(Path.Path, path),
166171
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
167-
Effect.mapError(
168-
(cause) =>
169-
new ProviderSetupError({
170-
instanceId,
171-
operation: "start",
172-
detail: cause.detail ?? cause.message,
173-
cause,
174-
}),
172+
Effect.mapError((cause) =>
173+
isNodeRuntimeUnavailableError(cause.cause)
174+
? new ProviderSetupError({
175+
instanceId,
176+
operation: "start",
177+
detail: nodeRuntimeUnavailableMessage("Antigravity sign-in"),
178+
cause,
179+
})
180+
: cause,
175181
),
176182
);
177183
// Each process unpacks into its own directory that dies with the

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,10 @@ it.layer(NodeServices.layer)("Antigravity profile preparation", (it) => {
567567
expect(result.failure).toMatchObject({
568568
_tag: "AcpTransportError",
569569
detail: expect.stringContaining("Install Node.js"),
570+
cause: {
571+
_tag: "NodeRuntimeUnavailableError",
572+
cause: { _tag: "CommandResolutionError" },
573+
},
570574
});
571575
}
572576
expect(yield* fs.exists(profileDirectory)).toBe(false);

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as NodePath from "node:path";
66

77
import type { AntigravityAuthMethod, ProviderInstanceId } from "@t3tools/contracts";
88
import { HostProcessPlatform } from "@t3tools/shared/hostProcess";
9-
import { resolveNodeExecutable } from "@t3tools/shared/nodeRuntime";
9+
import { resolveNodeExecutable, nodeRuntimeUnavailableMessage } from "@t3tools/shared/nodeRuntime";
1010
import * as Effect from "effect/Effect";
1111
import * as FileSystem from "effect/FileSystem";
1212
import * as Path from "effect/Path";
@@ -311,7 +311,13 @@ export const prepareAntigravityProfile = Effect.fn("prepareAntigravityProfile")(
311311
const runtimeExecutablePath =
312312
input.runtimeExecutablePath ??
313313
(yield* resolveNodeExecutable("Antigravity sign-in", input.baseEnv).pipe(
314-
Effect.mapError((error) => authSupportError(error.message)),
314+
Effect.mapError(
315+
(cause) =>
316+
new AcpErrors.AcpTransportError({
317+
detail: nodeRuntimeUnavailableMessage("Antigravity sign-in"),
318+
cause,
319+
}),
320+
),
315321
));
316322
const helperExecutable =
317323
platform === "win32" ? runtimeExecutablePath.replaceAll("\\", "/") : runtimeExecutablePath;

‎packages/contracts/src/device.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ export class DeviceHostUnavailableError extends Schema.TaggedError<DeviceHostUna
340340
{
341341
hostId: DeviceHostId,
342342
reason: Schema.String,
343+
cause: Schema.optional(Schema.Defect()),
343344
},
344345
) {
345346
override get message(): string {
@@ -527,6 +528,7 @@ export class DeviceToolUnavailableError extends Schema.TaggedError<DeviceToolUna
527528
"DeviceToolUnavailableError",
528529
{
529530
reason: Schema.String,
531+
cause: Schema.optional(Schema.Defect()),
530532
},
531533
) {
532534
override get message(): string {

0 commit comments

Comments
 (0)