diff --git a/src/lib/adapters/openshell/grpc-sandbox-control.test.ts b/src/lib/adapters/openshell/grpc-sandbox-control.test.ts index 081a121f12d..242d271459b 100644 --- a/src/lib/adapters/openshell/grpc-sandbox-control.test.ts +++ b/src/lib/adapters/openshell/grpc-sandbox-control.test.ts @@ -205,7 +205,7 @@ describe("gRPC OpenShell sandbox control", () => { ).toEqual(["printf", boundaryArgument]); }); - it("resolves a sandbox id and normalizes streamed output", async () => { + it("resolves a sandbox id and preserves exit status 255 with normalized output", async () => { const euro = Buffer.from("€"); const fake = fakeApi({ emit(stream) { @@ -213,7 +213,7 @@ describe("gRPC OpenShell sandbox control", () => { stream.emit("data", { stdout: { data: euro.subarray(0, 1) } }); stream.emit("data", { stdout: { data: euro.subarray(1) } }); stream.emit("data", { stderr: { data: Buffer.from("warning\n") } }); - stream.emit("data", { exit: { exitCode: 7 } }); + stream.emit("data", { exit: { exitCode: 255 } }); stream.emit("end"); }, }); @@ -229,7 +229,7 @@ describe("gRPC OpenShell sandbox control", () => { stdin: "request body", }), ).resolves.toEqual({ - status: 7, + status: 255, stdout: "hello €", stderr: "warning\n", }); diff --git a/src/lib/adapters/openshell/sandbox-control.test.ts b/src/lib/adapters/openshell/sandbox-control.test.ts index 55131e2ce1a..98c1f58c6ab 100644 --- a/src/lib/adapters/openshell/sandbox-control.test.ts +++ b/src/lib/adapters/openshell/sandbox-control.test.ts @@ -226,10 +226,10 @@ describe("OpenShell exec request validation", () => { }); describe("CLI OpenShell sandbox control", () => { - it("maps a typed exec request to the existing CLI contract", async () => { + it("maps a typed exec request and preserves exit status 255", async () => { const capture = vi.fn( (): CaptureOpenshellBinaryResult => ({ - status: 0, + status: 255, stdout: Buffer.from("hello\n"), stderr: Buffer.from("warning\n"), }), @@ -253,7 +253,7 @@ describe("CLI OpenShell sandbox control", () => { }, ); expect(result).toEqual({ - status: 0, + status: 255, stdout: "hello\n", stderr: "warning\n", }); diff --git a/src/lib/state/sandbox-backup-grpc.test.ts b/src/lib/state/sandbox-backup-grpc.test.ts index 8b684641f93..7f8e7de89cf 100644 --- a/src/lib/state/sandbox-backup-grpc.test.ts +++ b/src/lib/state/sandbox-backup-grpc.test.ts @@ -510,9 +510,9 @@ describe("backupSandboxState OpenShell directory transport", () => { }); it.each([ - ["error", { error: new Error("sandbox exec stream reset") }], - ["signal", { signal: "SIGTERM" as const }], - ])("rejects tar bytes returned with a transport %s even when tar reports success", async (_failureKind, transportFailure) => { + ["transport error", { error: new Error("sandbox exec stream reset") }, true], + ["stale signal", { signal: "SIGTERM" as const }, false], + ])("rejects tar bytes returned with a %s even when tar reports success", async (_failureKind, transportFailure, expectedUnreachable) => { const { archive } = createBinaryTarArchive(); queueSuccessfulProbeAndAudit(); mocks.execSandboxReadOnlyWithGrpcFallback.mockResolvedValueOnce({ @@ -525,7 +525,7 @@ describe("backupSandboxState OpenShell directory transport", () => { expect(result).toMatchObject({ success: false, - unreachable: true, + unreachable: expectedUnreachable, backedUpDirs: [], failedDirs: ["state"], }); diff --git a/src/lib/state/sandbox-state-file-backup.test.ts b/src/lib/state/sandbox-state-file-backup.test.ts index bed1677fba1..6df3afb1891 100644 --- a/src/lib/state/sandbox-state-file-backup.test.ts +++ b/src/lib/state/sandbox-state-file-backup.test.ts @@ -166,12 +166,40 @@ describe("state-file backup exec request", () => { expect(isSandboxExecTransportFailure({ status: null, error: outputLimit })).toBe(false); expect(isSandboxExecTransportFailure({ status: null, error: invalidRequest })).toBe(false); + expect(isSandboxExecTransportFailure({ status: 255 })).toBe(false); + expect(isSandboxExecTransportFailure({ status: 0, signal: "SIGKILL" })).toBe(false); + expect(isSandboxExecTransportFailure({ status: 1, signal: "SIGINT" })).toBe(false); + expect(isSandboxExecTransportFailure({ status: null })).toBe(true); expect(isSandboxExecTransportFailure({ status: null, error: new Error("UNAVAILABLE") })).toBe( true, ); expect(isSandboxExecTransportFailure({ status: null, signal: "SIGKILL" })).toBe(true); }); + it.each([ + ["a non-zero status", "terminal-status", { status: 255, stdout: "", stderr: "" }], + [ + "a terminal signal", + "terminal-signal", + { status: 0, stdout: "", stderr: "", signal: "SIGKILL" as const }, + ], + ])("keeps state-file backup failure from %s reachable without persisting bytes", async (_failureKind, backupName, execResult) => { + execReadOnly.mockResolvedValue(execResult); + + const result = await backupSandboxState("hermes", { name: backupName }); + + expect(result).toMatchObject({ + success: false, + backedUpFiles: [], + failedFiles: ["runtime/state.db"], + unreachable: false, + }); + expect(execReadOnly).toHaveBeenCalledTimes(1); + expect(fs.existsSync(path.join(result.manifest!.backupPath, "runtime", "state.db"))).toBe( + false, + ); + }); + it("persists binary state-file bytes with private mode and complete backup bookkeeping", async () => { const bytes = Buffer.from([0x53, 0x51, 0x4c, 0x00, 0xff, 0xfe, 0x80, 0x0a]); execReadOnly.mockResolvedValue({ diff --git a/src/lib/state/sandbox.ts b/src/lib/state/sandbox.ts index 5b63b4583a3..80e00bf2522 100644 --- a/src/lib/state/sandbox.ts +++ b/src/lib/state/sandbox.ts @@ -1081,9 +1081,11 @@ export function isSandboxExecTransportFailure(result: { const code = (result.error as NodeJS.ErrnoException | undefined)?.code; // Both transports use ENOBUFS for the shared raw-output cap. The canonical // request validator uses OPENSHELL_EXEC_INVALID_ARGUMENT. Neither can be - // repaired by retrying or by treating the sandbox as unreachable. + // repaired by retrying or by treating the sandbox as unreachable. OpenShell + // status 255 and stale signal fields are terminal remote results, not SSH + // transport conventions. if (code === "ENOBUFS" || code === "OPENSHELL_EXEC_INVALID_ARGUMENT") return false; - return Boolean(result.error || result.signal || result.status === null); + return Boolean(result.error || result.status === null); } /** @internal Pin the binary state-file result contract independently of filesystem writes. */ @@ -1146,8 +1148,6 @@ async function backupStateFile( * Uses the agent manifest to determine which directories contain state. */ -export { isSshTransportFailure } from "./ssh-transport.js"; - export async function backupSandboxState( sandboxName: string, options: BackupOptions = {}, diff --git a/src/lib/state/ssh-transport.test.ts b/src/lib/state/ssh-transport.test.ts deleted file mode 100644 index b982bbcbc01..00000000000 --- a/src/lib/state/ssh-transport.test.ts +++ /dev/null @@ -1,42 +0,0 @@ -// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -// SPDX-License-Identifier: Apache-2.0 - -import { describe, expect, it } from "vitest"; - -import { isSshTransportFailure } from "./ssh-transport"; - -describe("isSshTransportFailure", () => { - it("treats an ssh transport failure exit code (255) as unreachable", () => { - expect(isSshTransportFailure({ status: 255 })).toBe(true); - }); - - it("treats a timed-out or signal-killed probe (null status) as unreachable", () => { - expect(isSshTransportFailure({ status: null })).toBe(true); - }); - - it("treats a spawn error as unreachable", () => { - expect(isSshTransportFailure({ status: null, error: new Error("spawn ETIMEDOUT") })).toBe(true); - }); - - it("does not treat a reachable non-zero remote exit as unreachable", () => { - expect(isSshTransportFailure({ status: 1 })).toBe(false); - expect(isSshTransportFailure({ status: 2 })).toBe(false); - }); - - it("does not treat a successful probe as unreachable", () => { - expect(isSshTransportFailure({ status: 0 })).toBe(false); - }); - - it("treats SIGHUP/SIGPIPE terminated probes as transport failures", () => { - // spawnSync surfaces signal-killed processes with status=null + signal set. - // Match connect.ts by naming the transport-level signals explicitly. - expect(isSshTransportFailure({ status: null, signal: "SIGHUP" })).toBe(true); - expect(isSshTransportFailure({ status: null, signal: "SIGPIPE" })).toBe(true); - }); - - it("does not treat a reachable exit accompanied by a benign signal as unreachable", () => { - // A remote exit-1 with a stale/benign signal field must still be classified - // by exit code, not by the signal field. - expect(isSshTransportFailure({ status: 1, signal: "SIGINT" })).toBe(false); - }); -}); diff --git a/src/lib/state/ssh-transport.ts b/src/lib/state/ssh-transport.ts deleted file mode 100644 index 4f1bbb14165..00000000000 --- a/src/lib/state/ssh-transport.ts +++ /dev/null @@ -1,33 +0,0 @@ -// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -// SPDX-License-Identifier: Apache-2.0 - -/** - * Pure classifiers for SSH probe outcomes into transport-level failures - * (unreachable) vs application-level failures (reachable but exit-non-zero). - * - * A "transport-level" failure means the SSH tunnel itself could not carry - * data — the process never reached a shell that could return a real exit - * code. Typical shapes reported by `spawnSync("ssh", …)`: - * - * - `error` set on the result (spawn-time failure: ENOENT, EACCES, …) - * - `status === 255` (ssh's own transport-error convention) - * - `status === null` with a signal (killed by SIGHUP/SIGPIPE from a - * dying gateway) or with no signal (timed out / SIGTERM) - * - * Callers use this to promote a sandbox-level `unreachable` flag so the - * NEMOCLAW_SKIP_UNREACHABLE_SANDBOX_BACKUP=1 opt-in can activate. See #6188. - */ -export function isSshTransportFailure(result: { - status: number | null; - error?: Error; - signal?: NodeJS.Signals | null; -}): boolean { - if (result.error) return true; - // Signal termination (e.g. SIGHUP/SIGPIPE from a dying gateway) reports - // status=null; match connect.ts and treat these as transport-level - // failures explicitly so the diagnostic path is unambiguous. Any other - // null-status result (timeout, killed) is also transport-level. - if (result.signal === "SIGHUP" || result.signal === "SIGPIPE") return true; - if (result.status === null) return true; - return result.status === 255; -}