Skip to content

Commit dfda928

Browse files
authored
fix(server): report missing Node on SSH device hosts instead of a connection failure (pingdotgg#20377)
1 parent c803a80 commit dfda928

2 files changed

Lines changed: 118 additions & 4 deletions

File tree

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

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
1313
import * as NodeServices from "@effect/platform-node/NodeServices";
1414
import * as NetService from "@t3tools/shared/Net";
15+
import { SshCommandError } from "@t3tools/ssh/errors";
1516
import * as Effect from "effect/Effect";
1617
import * as Exit from "effect/Exit";
1718
import * as Deferred from "effect/Deferred";
@@ -694,6 +695,84 @@ it.effect("host retry exposes actionable failure without internal IDs or diagnos
694695
}).pipe(Effect.scoped),
695696
);
696697

698+
it.effect.each([
699+
{
700+
name: "missing Node among private diagnostic lines",
701+
exitCode: 1,
702+
stderr:
703+
"private diagnostics\r\nNode is missing from the non-interactive SSH PATH\r\nprivate diagnostics",
704+
expected:
705+
"Connected over SSH, but Node is not on this host's PATH for non-interactive SSH commands.",
706+
},
707+
{
708+
name: "another remote command failure",
709+
exitCode: 2,
710+
stderr: "private diagnostics",
711+
expected: "Connected over SSH, but the device host check failed (exit code 2).",
712+
},
713+
{
714+
name: "an unrecognized exit-one diagnostic",
715+
exitCode: 1,
716+
stderr: "private diagnostics: Node is missing from the non-interactive SSH PATH",
717+
expected: "Connected over SSH, but the device host check failed (exit code 1).",
718+
},
719+
{
720+
name: "a connection failure",
721+
exitCode: 255,
722+
stderr: "Node is missing from the non-interactive SSH PATH",
723+
expected: "Could not connect to this host over SSH.",
724+
},
725+
{
726+
name: "a command that could not start",
727+
exitCode: null,
728+
stderr: "private diagnostics",
729+
expected: "Could not connect to this host over SSH.",
730+
},
731+
{
732+
name: "an untyped offline cause",
733+
exitCode: undefined,
734+
stderr: "private diagnostics",
735+
expected: "Could not connect to this host over SSH.",
736+
},
737+
])("reports $name through manual and agent host readiness", ({ exitCode, stderr, expected }) =>
738+
Effect.gen(function* () {
739+
const failure = new DeviceHost.DeviceHostError({
740+
hostId: LOCAL_DEVICE_HOST_ID,
741+
step: "probe",
742+
cause:
743+
exitCode === undefined
744+
? "offline"
745+
: new SshCommandError({
746+
command: ["ssh", "private arguments"],
747+
exitCode,
748+
stderr,
749+
stdout: "private output",
750+
message: "private diagnostics",
751+
}),
752+
});
753+
const { service, settings } = yield* fixture(Effect.void, undefined, false, failure);
754+
yield* Ref.update(settings, (current) => ({
755+
...current,
756+
enableDeviceSupport: true,
757+
enableAgentDeviceAccess: true,
758+
}));
759+
for (const readiness of [service.readiness(), service.agentReadinessIfSupported()]) {
760+
const error = yield* readiness.pipe(Effect.flip);
761+
expect(error).toMatchObject({ reason: expected, cause: failure });
762+
expect((yield* service.state).hostStatuses[LOCAL_DEVICE_HOST_ID]).toEqual({
763+
status: "failed",
764+
detail: expected,
765+
});
766+
}
767+
for (const state of [yield* service.list, yield* service.retryHost(LOCAL_DEVICE_HOST_ID)]) {
768+
expect(state.hostStatuses[LOCAL_DEVICE_HOST_ID]).toEqual({
769+
status: "failed",
770+
detail: expected,
771+
});
772+
}
773+
}).pipe(Effect.scoped),
774+
);
775+
697776
it.effect("version discovery does not grant consent or start device tools", () =>
698777
Effect.gen(function* () {
699778
const { service, starts, agentStarts, requests } = yield* fixture();

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

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
LOCAL_DEVICE_HOST_ID,
2424
type ThreadId,
2525
} from "@t3tools/contracts";
26+
import { SshCommandError } from "@t3tools/ssh/errors";
2627
import * as FileSystem from "effect/FileSystem";
2728
import { resolveNodeExecutable, nodeRuntimeUnavailableMessage } from "@t3tools/shared/nodeRuntime";
2829
import * as Path from "effect/Path";
@@ -59,6 +60,28 @@ import * as LocalDeviceHost from "./LocalDeviceHost.ts";
5960

6061
export const DEVICE_HUB_ROUTE_PREFIX = "/api/device-hub";
6162

63+
const sshProbeFailureMessage = (error: DeviceHost.DeviceHostError): string => {
64+
const cause = error.cause;
65+
if (
66+
!(cause instanceof SshCommandError) ||
67+
cause.exitCode === null ||
68+
cause.exitCode < 1 ||
69+
cause.exitCode > 254
70+
)
71+
return "Could not connect to this host over SSH.";
72+
73+
// Only the known bootstrap diagnostic is safe to expose as Node guidance.
74+
if (
75+
cause.exitCode === 1 &&
76+
cause.stderr
77+
.split(/\r?\n/u)
78+
.some((line) => line.trim() === "Node is missing from the non-interactive SSH PATH")
79+
)
80+
return "Connected over SSH, but Node is not on this host's PATH for non-interactive SSH commands.";
81+
82+
return `Connected over SSH, but the device host check failed (exit code ${cause.exitCode}).`;
83+
};
84+
6285
const BOOT_TIMEOUT = Duration.minutes(3);
6386
const SCREENSHOT_TIMEOUT = Duration.seconds(20);
6487

@@ -250,7 +273,13 @@ export const makeWithHosts = Effect.fn("DeviceService.makeWithHosts")(function*
250273
)
251274
.pipe(
252275
Effect.tapError((error) =>
253-
setHostStatus(host.id, { status: "failed", detail: error.message }),
276+
setHostStatus(host.id, {
277+
status: "failed",
278+
detail:
279+
error._tag === "DeviceHostError" && error.step === "probe"
280+
? sshProbeFailureMessage(error)
281+
: error.message,
282+
}),
254283
),
255284
Effect.mapError(
256285
(error) =>
@@ -260,7 +289,7 @@ export const makeWithHosts = Effect.fn("DeviceService.makeWithHosts")(function*
260289
error._tag === "NodeRuntimeUnavailableError"
261290
? nodeRuntimeUnavailableMessage("Local device support")
262291
: error.step === "probe"
263-
? "Could not connect to this host over SSH."
292+
? sshProbeFailureMessage(error)
264293
: `Device support failed during ${error.step}.`,
265294
cause: error,
266295
}),
@@ -305,7 +334,13 @@ export const makeWithHosts = Effect.fn("DeviceService.makeWithHosts")(function*
305334
)
306335
.pipe(
307336
Effect.tapError((error) =>
308-
setHostStatus(host.id, { status: "failed", detail: error.message }),
337+
setHostStatus(host.id, {
338+
status: "failed",
339+
detail:
340+
error._tag === "DeviceHostError" && error.step === "probe"
341+
? sshProbeFailureMessage(error)
342+
: error.message,
343+
}),
309344
),
310345
Effect.mapError(
311346
(error) =>
@@ -319,7 +354,7 @@ export const makeWithHosts = Effect.fn("DeviceService.makeWithHosts")(function*
319354
: error._tag === "AgentDeviceStartError"
320355
? error.message
321356
: error.step === "probe"
322-
? "Could not connect to this host over SSH."
357+
? sshProbeFailureMessage(error)
323358
: `Device support failed during ${error.step}.`,
324359
cause: error,
325360
}),

0 commit comments

Comments
 (0)