@devries-cameron20 / t3code-1 / commits / d90ac5c764
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(ssh): timed-out SSH commands keep the output they already printed (#35589)

Julius Marminge committed Nov 12, 2026 at 13:07 UTC d90ac5c764ad46aa41ea37cf40858a1490755c02
2 files changed +71 -9
packages/ssh/src/command.test.ts
+43
@@ -3,11 +3,13 @@ import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
3 import * as NodeServices from "@effect/platform-node/NodeServices";
4 import * as HostProcess from "@t3tools/shared/HostProcess";
5 import * as Effect from "effect/Effect";
6 +import * as Fiber from "effect/Fiber";
7 import * as Layer from "effect/Layer";
8 import * as Result from "effect/Result";
9 import * as Schema from "effect/Schema";
10 import * as Sink from "effect/Sink";
11 import * as Stream from "effect/Stream";
12 +import * as TestClock from "effect/testing/TestClock";
13 import { ChildProcess, ChildProcessSpawner } from "effect/process";
14
15 import { parseSshResolveOutput, remoteStateKey, runSshCommand } from "./command.ts";
@@ -188,6 +190,47 @@ describe("ssh command", () => {
190 }).pipe(Effect.provide(layerProcess));
191 });
192
193 + it.effect("keeps redacted output the command emitted before timing out", () => {
194 + const spawner = ChildProcessSpawner.make(() =>
195 + Effect.succeed(
196 + ChildProcessSpawner.makeHandle({
197 + pid: ChildProcessSpawner.ProcessId(123),
198 + stdout: Stream.concat(
199 + Stream.make(encoder.encode('PROGRESS: extracting\n{"credential":"pairing-secret"}\n')),
200 + Stream.never,
201 + ),
202 + stderr: Stream.concat(
203 + Stream.make(encoder.encode("REMOTE: waiting for archive extraction\n")),
204 + Stream.never,
205 + ),
206 + all: Stream.empty,
207 + exitCode: Effect.never,
208 + isRunning: Effect.succeed(true),
209 + kill: () => Effect.void,
210 + stdin: Sink.drain,
211 + getInputFd: () => Sink.drain,
212 + getOutputFd: () => Stream.empty,
213 + unref: Effect.succeed(Effect.void),
214 + }),
215 + ),
216 + );
217 + const layerSpawner = Layer.succeed(ChildProcessSpawner.ChildProcessSpawner, spawner);
218 +
219 + return Effect.gen(function* () {
220 + const fiber = yield* runSshCommand(
221 + { alias: "devbox", hostname: "devbox", username: null, port: null },
222 + { remoteCommandArgs: ["sh", "-s"], timeoutMs: 1_000, keepErrorOutputTail: true },
223 + ).pipe(Effect.flip, Effect.forkChild);
224 + yield* TestClock.adjust("1 second");
225 + const failure = yield* Fiber.join(fiber);
226 +
227 + assert.instanceOf(failure, SshCommandError);
228 + assert.equal(failure.exitCode, null);
229 + assert.equal(failure.stdout, 'PROGRESS: extracting\n{"credential":"[redacted]"}\n');
230 + assert.equal(failure.stderr, "REMOTE: waiting for archive extraction\n");
231 + }).pipe(Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer, layerSpawner)));
232 + });
233 +
234 const changedKeyStderr =
235 "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\r\n" +
236 "@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @\r\n" +
packages/ssh/src/command.ts
+28 -9
@@ -127,12 +127,23 @@ export function getLastNonEmptyOutputLine(stdout: string): string | null {
127 );
128 }
129
130 -const collectProcessOutput = <E>(stream: Stream.Stream<Uint8Array, E>): Effect.Effect<string, E> =>
130 +interface SshCommandOutput {
131 + stdout: string;
132 + stderr: string;
133 +}
134 +
135 +// Appends as chunks arrive so a timeout can still report what was printed.
136 +const collectProcessOutput = <E>(
137 + stream: Stream.Stream<Uint8Array, E>,
138 + output: SshCommandOutput,
139 + key: keyof SshCommandOutput,
140 +): Effect.Effect<void, E> =>
141 stream.pipe(
142 Stream.decodeText(),
133 - Stream.runFold(
134 - () => "",
135 - (acc, chunk) => acc + chunk,
143 + Stream.runForEach((chunk) =>
144 + Effect.sync(() => {
145 + output[key] += chunk;
146 + }),
147 ),
148 );
149
@@ -214,6 +225,7 @@ const runSshCommandInScope = Effect.fn("ssh/command.runSshCommand.inScope")(func
225 target: DesktopSshEnvironmentTarget,
226 input: RunSshCommandOptions,
227 commandScope: Scope.Scope,
228 + output: SshCommandOutput,
229 ): Effect.fn.Return<
230 SshCommandResult,
231 SshCommandError | SshInvalidTargetError,
@@ -285,10 +297,10 @@ const runSshCommandInScope = Effect.fn("ssh/command.runSshCommand.inScope")(func
297 ),
298 );
299
288 - const [stdout, stderr, exitCode] = yield* Effect.all(
300 + const [, , exitCode] = yield* Effect.all(
301 [
290 - collectProcessOutput(child.stdout),
291 - collectProcessOutput(child.stderr),
302 + collectProcessOutput(child.stdout, output, "stdout"),
303 + collectProcessOutput(child.stderr, output, "stderr"),
304 child.exitCode.pipe(Effect.map(Number)),
305 ],
306 { concurrency: "unbounded" },
@@ -306,6 +318,7 @@ const runSshCommandInScope = Effect.fn("ssh/command.runSshCommand.inScope")(func
318 ),
319 );
320
321 + const { stdout, stderr } = output;
322 if (exitCode !== 0) {
323 const diagnosticStdout = redactSshErrorOutput(stdout, input.keepErrorOutputTail);
324 yield* Effect.logWarning("ssh.command.failed", {
@@ -350,8 +363,9 @@ export const runSshCommand = Effect.fn("ssh/command.runSshCommand")(function* (
363 SshCommandError | SshInvalidTargetError,
364 ChildProcessSpawner.ChildProcessSpawner | FileSystem.FileSystem | Path.Path
365 > {
366 + const output: SshCommandOutput = { stdout: "", stderr: "" };
367 return yield* Effect.scopedWith((commandScope) =>
354 - runSshCommandInScope(target, input, commandScope),
368 + runSshCommandInScope(target, input, commandScope, output),
369 ).pipe(
370 Effect.timeoutOption(Duration.millis(input.timeoutMs ?? DEFAULT_SSH_COMMAND_TIMEOUT_MS)),
371 Effect.flatMap((result) =>
@@ -359,17 +373,22 @@ export const runSshCommand = Effect.fn("ssh/command.runSshCommand")(function* (
373 onSome: Effect.succeed,
374 // The enclosing operation owns the span for this inline handler.
375 onNone: Effect.fnUntraced(function* () {
376 + const diagnosticStdout = redactSshErrorOutput(output.stdout, input.keepErrorOutputTail);
377 + const { stderr } = output;
378 yield* Effect.logWarning("ssh.command.timedOut", {
379 ...sshTargetLogFields(target),
380 timeoutMs: input.timeoutMs ?? DEFAULT_SSH_COMMAND_TIMEOUT_MS,
381 remoteCommandArgs: input.remoteCommandArgs ?? [],
382 preHostArgs: input.preHostArgs ?? [],
383 hasStdin: input.stdin !== undefined,
384 + stdout: diagnosticStdout,
385 + stderr,
386 });
387 return yield* new SshCommandError({
388 command: ["ssh"],
389 exitCode: null,
372 - stderr: "",
390 + stdout: diagnosticStdout,
391 + stderr,
392 message: `SSH command timed out after ${input.timeoutMs ?? DEFAULT_SSH_COMMAND_TIMEOUT_MS}ms.`,
393 });
394 }),