Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

SshDeviceHost.ts

593 lines21.0 KB

1import {
2 type DeviceHostSummary,
3 DevicePlatformAvailability,
4 DeviceToolVersions,
5 deviceToolInstallMessage,
6 type SshDeviceHostConfig,
7} from "@t3tools/contracts";
8import { runSshCommand, baseSshArgs, resolveSshCommand } from "@t3tools/ssh/command";
9import { SshCommandError } from "@t3tools/ssh/errors";
10import * as NetService from "@t3tools/shared/Net";
11import { waitForHttpReady } from "@t3tools/shared/httpReadiness";
12import * as Digest from "effect/crypto/Digest";
13import * as Exit from "effect/Exit";
14import * as Effect from "effect/Effect";
15import * as FileSystem from "effect/FileSystem";
16import * as Path from "effect/Path";
17import * as Scope from "effect/Scope";
18import * as Semaphore from "effect/Semaphore";
19import * as Schema from "effect/Schema";
20import * as Stream from "effect/Stream";
21import * as Hex from "effect/encoding/Hex";
22import * as HttpClient from "effect/http/HttpClient";
23import * as ChildProcess from "effect/process/ChildProcess";
24import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
25import * as ServerConfig from "../config.ts";
26import { releaseAgentDeviceSession } from "./AgentDeviceTarget.ts";
27import * as DeviceHost from "./DeviceHost.ts";
28import { makeDeviceHubOperations } from "./DeviceHubOperations.ts";
29import { quoteRemoteArg, remoteDeviceEnvironment, remoteDeviceScript } from "./sshDeviceScript.ts";
30​
31const UNHEALTHY_PROBE_LIMIT = 3;
32​
33/** The host bootstrap failed on a host identified as Windows. */
34export class SshWindowsHostError extends Schema.TaggedError<SshWindowsHostError>()(
35 "SshWindowsHostError",
36 { cause: Schema.Defect() },
37) {
38 override get message(): string {
39 return "Connected over SSH, but this host runs Windows, and SSH device hosts currently need macOS or Linux. To use this machine's devices, run T3 Code on it and connect to that environment.";
40 }
42export const isSshWindowsHostError = Schema.is(SshWindowsHostError);
43​
44const Probe = Schema.Struct({
45 nodePath: Schema.String,
46 tools: Schema.optional(DeviceToolVersions),
47 platforms: Schema.Array(DevicePlatformAvailability),
48});
49const Started = Schema.Struct({
50 ...Probe.fields,
51 hubPort: Schema.Int,
52 daemonPort: Schema.optionalKey(Schema.Int),
53 token: Schema.optionalKey(Schema.String),
54 entryPath: Schema.optionalKey(Schema.String),
55 helpers: Schema.Struct({
56 serveSimAxSettings: Schema.NullOr(Schema.String),
57 serveSimCli: Schema.NullOr(Schema.String),
58 }),
59});
60const decodeProbe = Schema.decodeUnknownEffect(Schema.fromJsonString(Probe));
61const decodeStarted = Schema.decodeUnknownEffect(Schema.fromJsonString(Started));
62const targetFor = (config: SshDeviceHostConfig) => ({
63 alias: config.target,
64 hostname: config.target,
65 username: null,
66 port: config.port ?? null,
67});
68const identityArgs = (config: SshDeviceHostConfig) =>
69 config.identityFile ? ["-i", config.identityFile] : [];
70const commandArgs = (script: string) => [
71 "sh",
72 "-c",
73 quoteRemoteArg(remoteDeviceEnvironment + script),
74];
75const bootstrap = (
76 config: SshDeviceHostConfig,
77 owner: string,
78 mode: "probe" | "start" | "agent-start" | "stop-agent" | "stop",
79) =>
80 runSshCommand(targetFor(config), {
81 preHostArgs: identityArgs(config),
82 remoteCommandArgs: commandArgs(
83 'command -v node >/dev/null 2>&1 || { echo "Node is missing from the non-interactive SSH PATH" >&2; exit 1; }; exec node',
84 ),
85 stdin: remoteDeviceScript(owner, mode),
86 timeoutMs: mode === "start" || mode === "agent-start" ? 1_300_000 : 45_000,
87 }).pipe(
88 Effect.mapError(
89 (cause) => new DeviceHost.DeviceHostError({ hostId: config.id, step: mode, cause }),
90 ),
91 );
92​
93const isSshCommandError = Schema.is(SshCommandError);
94​
95// cmd expands only %OS% and PowerShell only $env:OS; POSIX shells expand neither to Windows_NT.
96const explainWindowsHost = Effect.fn("SshDeviceHost.explainWindowsHost")(function* (
97 config: SshDeviceHostConfig,
98 error: DeviceHost.DeviceHostError,
99) {
100 const cause = error.cause;
101 // Windows ssh.exe preserves full remote exit statuses such as 9009.
102 if (
103 !isSshCommandError(cause) ||
104 cause.exitCode === null ||
105 cause.exitCode < 1 ||
106 (cause.exitCode > 254 && cause.exitCode !== 9009)
107 )
108 return yield* error;
109 let windows = yield* runSshCommand(targetFor(config), {
110 preHostArgs: identityArgs(config),
111 remoteCommandArgs: ["echo %OS% $env:OS"],
112 timeoutMs: 15_000,
113 }).pipe(
114 Effect.map(({ stdout }) => /(?:^|\s)Windows_NT(?:\s|$)/u.test(stdout)),
115 Effect.orElseSucceed(() => false),
116 );
117 if (!windows) {
118 // Git Bash does not expand Windows shell variables, but its Node still reports win32.
119 windows = yield* runSshCommand(targetFor(config), {
120 preHostArgs: identityArgs(config),
121 remoteCommandArgs: ["node", "-p", "process.platform"],
122 timeoutMs: 15_000,
123 }).pipe(
124 Effect.map(({ stdout }) => stdout.trim() === "win32"),
125 Effect.orElseSucceed(() => false),
126 );
127 }
128 if (!windows) return yield* error;
129 return yield* new DeviceHost.DeviceHostError({
130 hostId: config.id,
131 step: error.step,
132 cause: new SshWindowsHostError({ cause }),
133 });
134});
135​
136const ownerFor = Effect.fn("SshDeviceHost.ownerFor")(function* (hostId: string) {
137 const fs = yield* FileSystem.FileSystem;
138 const server = yield* ServerConfig.ServerConfig;
139 const environmentId = yield* fs
140 .readFileString(server.environmentIdPath)
141 .pipe(Effect.orElseSucceed(() => server.stateDir));
142 const digest = yield* Digest.Digest;
143 const owner = yield* digest
144 .digest(
145 Digest.sha256,
146 new TextEncoder().encode(`${environmentId}\0${server.stateDir}\0${hostId}`),
147 )
148 .pipe(Effect.map(Hex.encode), Effect.orDie);
149 return owner.slice(0, 24);
150});
151​
152export const probe = Effect.fn("SshDeviceHost.probe")(function* (
153 config: SshDeviceHostConfig,
154 owner?: string,
155) {
156 const result = yield* bootstrap(config, owner ?? (yield* ownerFor(config.id)), "probe").pipe(
157 Effect.catchTags({ DeviceHostError: (error) => explainWindowsHost(config, error) }),
158 );
159 const value = yield* decodeProbe(result.stdout.trim()).pipe(
160 Effect.mapError(
161 (cause) =>
162 new DeviceHost.DeviceHostError({ hostId: config.id, step: "reading probe result", cause }),
163 ),
164 );
165 return {
166 id: config.id,
167 label: config.label,
168 kind: "ssh",
169 tools: value.tools,
170 hubInstalled:
171 value.tools?.hub.installedVersions.includes(value.tools.hub.requiredVersion) ?? false,
172 agentDeviceInstalled:
173 value.tools?.agent.installedVersions.includes(value.tools.agent.requiredVersion) ?? false,
174 platforms: value.platforms,
175 } satisfies DeviceHostSummary;
176});
177​
178export const make = Effect.fn("SshDeviceHost.make")(function* (
179 config: SshDeviceHostConfig,
180 onReady: (
181 ready: DeviceHost.DeviceHostAgentReady,
182 ) => Effect.Effect<void, DeviceHost.DeviceHostError> = () => Effect.void,
183 onStatus: (
184 status: "starting" | "ready" | "failed",
185 detail?: string,
186 ) => Effect.Effect<void> = () => Effect.void,
187) {
188 const fs = yield* FileSystem.FileSystem;
189 const path = yield* Path.Path;
190 const server = yield* ServerConfig.ServerConfig;
191 const net = yield* NetService.NetService;
192 const http = yield* HttpClient.HttpClient;
193 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
194 const digest = yield* Digest.Digest;
195 const parentScope = yield* Scope.Scope;
196 const ssh = yield* resolveSshCommand;
197 const owner = yield* ownerFor(config.id);
198 const provide = <A, E>(
199 effect: Effect.Effect<
200 A,
201 E,
202 | FileSystem.FileSystem
203 | Path.Path
204 | ChildProcessSpawner.ChildProcessSpawner
205 | Digest.Digest
206 | ServerConfig.ServerConfig
207 >,
208 ) =>
209 effect.pipe(
210 Effect.provideService(FileSystem.FileSystem, fs),
211 Effect.provideService(Path.Path, path),
212 Effect.provideService(ServerConfig.ServerConfig, server),
213 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
214 Effect.provideService(Digest.Digest, digest),
215 );
216 const lock = yield* Semaphore.make(1);
217 let stopped = false;
218 let activated = false;
219 let wantsAgent = false;
220 let ready:
221 | (DeviceHost.DeviceHostReady & {
222 agentDevice?: DeviceHost.DeviceHostAgentReady["agentDevice"];
223 })
224 | null = null;
225 let connectionScope: Scope.Closeable | null = null;
226 let summary: DeviceHostSummary = {
227 id: config.id,
228 label: config.label,
229 kind: "ssh",
230 hubInstalled: false,
231 agentDeviceInstalled: false,
232 platforms: [],
233 };
234​
235 const run: DeviceHost.DeviceHostReady["run"] = (command, args, options) =>
236 provide(
237 runSshCommand(targetFor(config), {
238 preHostArgs: identityArgs(config),
239 // Match the hub cwd so relative executables and PATH entries resolve identically.
240 remoteCommandArgs: commandArgs(
241 `cd "$HOME/.t3/device/hosts/${owner}" && exec ${[command, ...args].map(quoteRemoteArg).join(" ")}`,
242 ),
243 keepErrorOutputTail: true,
244 ...(options?.stdin === undefined ? {} : { stdin: options.stdin }),
245 ...(options?.timeoutMs === undefined ? {} : { timeoutMs: options.timeoutMs }),
246 }),
247 ).pipe(
248 Effect.map((result) => ({ ...result, code: 0 })),
249 Effect.catch((error) =>
250 Effect.succeed({
251 stdout: "stdout" in error ? (error.stdout ?? "") : "",
252 stderr: error.message,
253 code: "exitCode" in error ? (error.exitCode ?? 127) : 127,
254 }),
255 ),
256 );
257​
258 const platformAvailability: DeviceHost.DeviceHost["Service"]["platformAvailability"] = (
259 platform,
260 ) =>
261 provide(probe(config, owner)).pipe(
262 Effect.map((value) => {
263 summary = { ...summary, platforms: value.platforms };
264 return value.platforms.find((p) => p.platform === platform)!;
265 }),
266 Effect.orElseSucceed(() => ({
267 platform,
268 available: false,
269 reason: "Cannot reach device host. Test its SSH connection in Settings.",
270 })),
271 );
272​
273 const connectOnce = Effect.fn("SshDeviceHost.connectOnce")(function* (): Effect.fn.Return<
274 DeviceHost.DeviceHostReady & { agentDevice?: DeviceHost.DeviceHostAgentReady["agentDevice"] },
275 DeviceHost.DeviceHostError
276 > {
277 activated = true;
278 const result = yield* provide(bootstrap(config, owner, wantsAgent ? "agent-start" : "start"));
279 yield* onStatus("starting");
280 const remote = yield* decodeStarted(result.stdout.trim()).pipe(
281 Effect.mapError(
282 (cause) =>
283 new DeviceHost.DeviceHostError({
284 hostId: config.id,
285 step: "reading host endpoints",
286 cause,
287 }),
288 ),
289 );
290 summary = {
291 ...summary,
292 platforms: remote.platforms,
293 tools: remote.tools,
294 hubInstalled: true,
295 agentDeviceInstalled: wantsAgent || summary.agentDeviceInstalled,
296 };
297 const hubPort = yield* net.reserveLoopbackPort("127.0.0.1").pipe(
298 Effect.mapError(
299 (cause) =>
300 new DeviceHost.DeviceHostError({
301 hostId: config.id,
302 step: "reserving hub port",
303 cause,
304 }),
305 ),
306 );
307 const daemonPort = yield* net.reserveLoopbackPort("127.0.0.1").pipe(
308 Effect.mapError(
309 (cause) =>
310 new DeviceHost.DeviceHostError({
311 hostId: config.id,
312 step: "reserving daemon port",
313 cause,
314 }),
315 ),
316 );
317 const scope = yield* Scope.make();
318 connectionScope = scope;
319 const child = yield* spawner
320 .spawn(
321 ChildProcess.make(
322 ssh,
323 [
324 ...baseSshArgs(targetFor(config), { batchMode: "yes" }),
325 ...identityArgs(config),
326 "-o",
327 "ExitOnForwardFailure=yes",
328 // Multiplexed clients exit after handing forwards to the master; own this connection.
329 "-o",
330 "ControlMaster=no",
331 "-o",
332 "ControlPath=none",
333 "-o",
334 "ControlPersist=no",
335 "-o",
336 "ServerAliveInterval=10",
337 "-o",
338 "ServerAliveCountMax=3",
339 "-T",
340 "-L",
341 `127.0.0.1:${hubPort}:127.0.0.1:${remote.hubPort}`,
342 ...(remote.daemonPort === undefined
343 ? []
344 : ["-L", `127.0.0.1:${daemonPort}:127.0.0.1:${remote.daemonPort}`]),
345 config.target,
346 // The server holds the only write end of this stdin, so when it exits for any
347 // reason the remote `cat` sees EOF and ssh exits once its forwarded connections
348 // close, instead of being orphaned.
349 "sh",
350 "-c",
351 quoteRemoteArg("exec cat >/dev/null"),
352 ],
353 { stdin: "pipe", stdout: "ignore", stderr: "pipe" },
354 ),
355 )
356 .pipe(
357 Effect.provideService(Scope.Scope, scope),
358 Effect.mapError(
359 (cause) =>
360 new DeviceHost.DeviceHostError({ hostId: config.id, step: "forwarding ports", cause }),
361 ),
362 );
363 let stderr = "";
364 yield* child.stderr.pipe(
365 Stream.decodeText(),
366 Stream.runForEach((chunk) =>
367 Effect.sync(() => {
368 stderr = (stderr + chunk).slice(-2000);
369 }),
370 ),
371 Effect.forkIn(scope),
372 );
373 const hubOrigin = `http://127.0.0.1:${hubPort}`;
374 const next = {
375 nodePath: remote.nodePath,
376 hub: { origin: hubOrigin },
377 devices: makeDeviceHubOperations({
378 hostId: config.id,
379 origin: hubOrigin,
380 http,
381 run,
382 availablePlatforms: Effect.suspend(() => {
383 const ios = summary.platforms.some((p) => p.platform === "ios" && p.available);
384 const android = summary.platforms.some((p) => p.platform === "android" && p.available);
385 if (ios && android) return Effect.succeed({ ios, android });
386 return provide(probe(config, owner)).pipe(
387 Effect.map((value) => {
388 summary = { ...summary, platforms: value.platforms };
389 return {
390 ios: value.platforms.some((p) => p.platform === "ios" && p.available),
391 android: value.platforms.some((p) => p.platform === "android" && p.available),
392 };
393 }),
394 Effect.orElseSucceed(() => ({ ios, android, probeFailed: true })),
395 );
396 }),
397 }),
398 ...(remote.daemonPort !== undefined &&
399 remote.token !== undefined &&
400 remote.entryPath !== undefined
401 ? {
402 agentDevice: {
403 baseUrl: `http://127.0.0.1:${daemonPort}`,
404 token: remote.token,
405 entryPath: remote.entryPath,
406 },
407 }
408 : {}),
409 helpers: remote.helpers,
410 run,
411 };
412 for (const [baseUrl, route] of [
413 [next.hub.origin, "/readyz"],
414 ...(next.agentDevice ? [[next.agentDevice.baseUrl, "/health"]] : []),
415 ]) {
416 yield* waitForHttpReady({
417 baseUrl: baseUrl!,
418 path: route!,
419 timeoutMs: 15000,
420 makeError: () =>
421 new DeviceHost.DeviceHostError({
422 hostId: config.id,
423 step: "waiting for SSH forward",
424 cause: new Error(stderr || "Forwarded endpoint did not answer."),
425 }),
426 }).pipe(Effect.provideService(HttpClient.HttpClient, http));
427 }
428 if (next.agentDevice) yield* onReady({ ...next, agentDevice: next.agentDevice });
429 ready = next.agentDevice
430 ? { ...next, releaseAgentSession: releaseAgentDeviceSession(http, next.agentDevice) }
431 : next;
432 yield* onStatus("ready");
433 // Reconnect also repairs helpers that died while SSH itself stayed connected.
434 // A loaded host can miss a probe or two while still working, and reconnecting
435 // then drops the forward in use, so only consecutive misses count.
436 const unhealthy = Effect.gen(function* () {
437 let misses = 0;
438 while (true) {
439 yield* Effect.sleep("10 seconds");
440 const alive = yield* http.get(`${next.hub.origin}/readyz`).pipe(
441 Effect.timeout("5 seconds"),
442 Effect.map((r) => r.status === 200),
443 Effect.orElseSucceed(() => false),
444 );
445 const daemonAlive = next.agentDevice
446 ? yield* http.get(`${next.agentDevice!.baseUrl}/health`).pipe(
447 Effect.timeout("5 seconds"),
448 Effect.map((r) => r.status === 200),
449 Effect.orElseSucceed(() => false),
450 )
451 : true;
452 misses = alive && daemonAlive ? 0 : misses + 1;
453 if (misses >= UNHEALTHY_PROBE_LIMIT) return;
454 }
455 });
456 yield* Effect.gen(function* () {
457 yield* Effect.raceFirst(child.exitCode.pipe(Effect.ignore), unhealthy);
458 if (stopped || connectionScope !== scope) return;
459 ready = null;
460 yield* onStatus("starting", "Reconnecting to device host…");
461 yield* Scope.close(scope, Exit.void);
462 let delay = 1000;
463 while (true) {
464 if (stopped || connectionScope !== scope) return;
465 yield* Effect.sleep(delay);
466 const result = yield* lock
467 .withPermit(
468 Effect.suspend(() => (stopped || ready ? Effect.void : connect().pipe(Effect.asVoid))),
469 )
470 .pipe(Effect.result);
471 if (result._tag === "Success") return;
472 yield* onStatus("failed", result.failure.message);
473 if (connectionScope && connectionScope !== scope)
474 yield* Scope.close(connectionScope, Exit.void);
475 connectionScope = scope;
476 delay = Math.min(delay * 2, 30000);
477 }
478 }).pipe(Effect.forkIn(parentScope));
479 return next;
480 });
481​
482 const connect = Effect.fn("SshDeviceHost.connect")(function* () {
483 for (let attempt = 0; ; attempt++) {
484 const result = yield* connectOnce().pipe(Effect.result);
485 if (result._tag === "Success") return result.success;
486 const failedScope = connectionScope;
487 connectionScope = null;
488 if (failedScope) yield* Scope.close(failedScope, Exit.void);
489 // SSH binds after the reservation is released, so a competing bind needs fresh ports.
490 if (
491 attempt >= 2 ||
492 !["forwarding ports", "waiting for SSH forward"].includes(result.failure.step)
493 )
494 return yield* result.failure;
495 }
496 });
497​
498 const ensureReady: DeviceHost.DeviceHost["Service"]["ensureReady"] = (onPhase) =>
499 lock.withPermit(
500 Effect.gen(function* () {
501 stopped = false;
502 if (ready) return ready;
503 summary = yield* provide(probe(config, owner));
504 yield* onPhase(
505 summary.hubInstalled ? "starting" : "installing",
506 summary.hubInstalled
507 ? undefined
508 : deviceToolInstallMessage("device hub", summary.tools?.hub),
509 );
510 return yield* connect().pipe(
511 Effect.tapError(() =>
512 connectionScope ? Scope.close(connectionScope, Exit.void) : Effect.void,
513 ),
514 );
515 }),
516 );
517 const stop = lock.withPermit(
518 Effect.gen(function* () {
519 stopped = true;
520 ready = null;
521 if (connectionScope) yield* Scope.close(connectionScope, Exit.void);
522 connectionScope = null;
523 if (activated) yield* provide(bootstrap(config, owner, "stop")).pipe(Effect.ignore);
524 activated = false;
525 wantsAgent = false;
526 }),
527 );
528 const changeAgent = (enabled: boolean) =>
529 lock.withPermit(
530 Effect.gen(function* () {
531 wantsAgent = enabled;
532 if (enabled && ready?.agentDevice) return { ...ready, agentDevice: ready.agentDevice };
533 if (!enabled && !ready?.agentDevice) return null;
534 ready = null;
535 const previousScope = connectionScope;
536 connectionScope = null;
537 if (previousScope) yield* Scope.close(previousScope, Exit.void);
538 if (!enabled) yield* provide(bootstrap(config, owner, "stop-agent"));
539 return yield* connect().pipe(
540 Effect.onError(() =>
541 Effect.gen(function* () {
542 const failedScope = connectionScope;
543 connectionScope = null;
544 if (failedScope) yield* Scope.close(failedScope, Exit.void);
545 if (enabled)
546 yield* provide(bootstrap(config, owner, "stop-agent")).pipe(Effect.ignore);
547 }),
548 ),
549 );
550 }),
551 );
552 yield* Effect.addFinalizer(() => stop);
553 return {
554 id: config.id,
555 label: config.label,
556 summary: Effect.sync(() => summary),
557 inspect: provide(probe(config, owner)).pipe(
558 Effect.tap((value) =>
559 Effect.sync(() => {
560 summary = value;
561 }),
562 ),
563 ),
564 current: Effect.sync(() => ready),
565 ensureReady,
566 ensureAgentReady: (onPhase) =>
567 ensureReady(onPhase).pipe(
568 Effect.flatMap(() =>
569 onPhase(
570 summary.agentDeviceInstalled ? "starting" : "installing",
571 summary.agentDeviceInstalled
572 ? undefined
573 : deviceToolInstallMessage("agent tools", summary.tools?.agent),
574 ),
575 ),
576 Effect.flatMap(() => changeAgent(true)),
577 Effect.flatMap((value) =>
578 value?.agentDevice
579 ? Effect.succeed({ ...value, agentDevice: value.agentDevice })
580 : Effect.fail(
581 new DeviceHost.DeviceHostError({
582 hostId: config.id,
583 step: "starting agent tools",
584 cause: new Error("Daemon endpoint missing"),
585 }),
586 ),
587 ),
588 ),
589 stopAgent: changeAgent(false).pipe(Effect.asVoid, Effect.ignore),
590 stop,
591 platformAvailability,
592 } satisfies DeviceHost.DeviceHost["Service"];
593});