diff --git a/packages/adapter-utils/src/workspace-restore-diagnostics.test.ts b/packages/adapter-utils/src/workspace-restore-diagnostics.test.ts index 936b68e302c..492d09ed8ee 100644 --- a/packages/adapter-utils/src/workspace-restore-diagnostics.test.ts +++ b/packages/adapter-utils/src/workspace-restore-diagnostics.test.ts @@ -113,6 +113,13 @@ describe("workspace restore diagnostics", () => { }, ); + it("keeps an allowlisted ENAMETOOLONG code", async () => { + const sink = vi.fn(); + const error = Object.assign(new Error("private"), { code: "ENAMETOOLONG" }); + await expect(withWorkspaceRestoreDiagnostics("workspace", async () => { throw error; }, sink)).rejects.toBe(error); + expect(sink.mock.calls[0]?.[0]).toContain('"errorCode":"ENAMETOOLONG"'); + }); + it("bounds cause traversal even when it cycles", async () => { const error = Object.assign(new Error("private"), { cause: null as unknown, code: "ENOENT" }); error.cause = error; diff --git a/packages/adapter-utils/src/workspace-restore-diagnostics.ts b/packages/adapter-utils/src/workspace-restore-diagnostics.ts index 8ac378374f8..ab5d42d509d 100644 --- a/packages/adapter-utils/src/workspace-restore-diagnostics.ts +++ b/packages/adapter-utils/src/workspace-restore-diagnostics.ts @@ -40,7 +40,7 @@ export interface WorkspaceRestoreDiagnostic { rpcCode?: number; } const ERROR_CODES = new Set([ - "ENOENT", "EACCES", "EPERM", "ENOSPC", "EIO", "EXDEV", "ENOTDIR", "EISDIR", + "ENOENT", "EACCES", "EPERM", "ENOSPC", "EIO", "EXDEV", "ENOTDIR", "EISDIR", "ENAMETOOLONG", "ECONNRESET", "ECONNREFUSED", "ETIMEDOUT", "EPIPE", "ENOTFOUND", "EAI_AGAIN", "ABORT_ERR", "UND_ERR_CONNECT_TIMEOUT", "UND_ERR_SOCKET", ]); diff --git a/packages/plugins/sandbox-providers/daytona/src/file-sync-diagnostics.test.ts b/packages/plugins/sandbox-providers/daytona/src/file-sync-diagnostics.test.ts index 4e5e2da1cee..e761e460fbd 100644 --- a/packages/plugins/sandbox-providers/daytona/src/file-sync-diagnostics.test.ts +++ b/packages/plugins/sandbox-providers/daytona/src/file-sync-diagnostics.test.ts @@ -54,6 +54,53 @@ it.each([ }); }); +it.each([ + ["tar: ./private-file: Cannot open: Permission denied\ntar: Exiting with failure status due to previous errors", "EACCES"], + ["tar: ./private-dir: Cannot savedir: Operation not permitted", "EPERM"], + ["tar: ./private-file: Cannot stat: No such file or directory", "ENOENT"], + ["tar: ./private-dir/private-file: Cannot open: File name too long", "ENAMETOOLONG"], + ["tar: ./private-file: Read error at byte 0, while reading 512 bytes: Input/output error", "EIO"], + // A vanished file must not hide the error that stopped tar. + ["tar: ./private-file: Cannot stat: No such file or directory\ntar: ./private-dir: Cannot open: Permission denied", "EACCES"], + ["private-command-output mentions Permission denied mid-line only", "unknown"], +] as const)("classifies a failed sandbox tar's output as a bounded errno: %s", async (output, errorCode) => { + const sandbox = { + process: { executeCommand: async (command: string) => + command.includes("tar -c") ? { exitCode: 2, result: output } : { exitCode: 0 } }, + fs: { downloadFiles: async () => [], deleteFile: async () => undefined }, + }; + await withEnvironmentSyncErrorCapture(async () => { + let caught: unknown; + try { + await performSyncOut({ sandbox: sandbox as never, remoteDir: "/workspace", timeoutSeconds: 30, + operations: [{ operationId: "fixture", files: [{ sourcePath: "/workspace/private-path", targetPath: "/unused", kind: "directory" }] }], + }); + } catch (error) { caught = error; } + expect(caught).toBeInstanceOf(Error); + const diagnostic = readEnvironmentSyncErrorDiagnostic({ data: environmentSyncErrorData(caught) }); + expect(diagnostic).toEqual({ errorCode, transferStep: "archive_create", transferFailureKind: "command_failed", exitCode: 2 }); + expect(JSON.stringify(diagnostic)).not.toContain("private"); + }); +}); + +it("classifies a full sandbox disk at the guard step without recording output", async () => { + const sandbox = { + process: { executeCommand: async () => ({ exitCode: 1, result: "mkdir: cannot create directory '/workspace/private-path': No space left on device" }) }, + fs: { downloadFiles: async () => [], deleteFile: async () => undefined }, + }; + await withEnvironmentSyncErrorCapture(async () => { + let caught: unknown; + try { + await performSyncOut({ sandbox: sandbox as never, remoteDir: "/workspace", timeoutSeconds: 30, + operations: [{ operationId: "fixture", files: [{ sourcePath: "/workspace/private-path", targetPath: "/unused", kind: "directory" }] }], + }); + } catch (error) { caught = error; } + const diagnostic = readEnvironmentSyncErrorDiagnostic({ data: environmentSyncErrorData(caught) }); + expect(diagnostic).toMatchObject({ errorCode: "ENOSPC", transferFailureKind: "command_failed", exitCode: 1 }); + expect(JSON.stringify(diagnostic)).not.toContain("private"); + }); +}); + it("keeps a thrown download exception identical and its nested safe code", async () => { const original = Object.freeze(new Error("private-download-error", { cause: { code: "ECONNRESET", token: "private-token" } })); const sandbox = { diff --git a/packages/plugins/sandbox-providers/daytona/src/file-sync.ts b/packages/plugins/sandbox-providers/daytona/src/file-sync.ts index f067a29130d..8ea41f23363 100644 --- a/packages/plugins/sandbox-providers/daytona/src/file-sync.ts +++ b/packages/plugins/sandbox-providers/daytona/src/file-sync.ts @@ -418,6 +418,31 @@ async function countHostFiles(root: string, exclude?: string[]): Promise return total; } +// Fatal causes first: tar can warn about a vanished file before the error that +// stopped it. Daytona's executeCommand result already includes stderr. +const SANDBOX_ERRNO_MESSAGES: ReadonlyArray = [ + ["ENOSPC", "No space left on device"], + ["EIO", "Input/output error"], + ["ENAMETOOLONG", "File name too long"], + ["EACCES", "Permission denied"], + ["EPERM", "Operation not permitted"], + ["ENOTDIR", "Not a directory"], + ["EISDIR", "Is a directory"], + ["ENOENT", "No such file or directory"], +]; + +/** + * Map the strerror suffix of a failed command's output to an allowlisted errno. + * Reads only a bounded tail in memory; the output text itself is never recorded. + */ +function sandboxCommandErrorCode(output: string): string | undefined { + const lines = output.slice(-16 * 1024).split(/\r?\n/).map((line) => line.trimEnd()); + for (const [code, message] of SANDBOX_ERRNO_MESSAGES) { + if (lines.some((line) => line.endsWith(`: ${message}`))) return code; + } + return undefined; +} + /** * Run one sandbox command and return its stdout on success. Throws the same * shaped error as {@link assertSandboxCommandOk} on a non-zero exit. Used by @@ -434,7 +459,7 @@ async function assertSandboxCommandOkWithOutput( if ((result.exitCode ?? 1) !== 0) { const detail = (result.result ?? result.artifacts?.stdout ?? "").toString().trim(); throw recordEnvironmentSyncError(new Error(`Daytona ${label} command failed (exit ${result.exitCode ?? "unknown"})${detail ? `: ${detail}` : ""}`), { - transferFailureKind: "command_failed", exitCode: result.exitCode, + transferFailureKind: "command_failed", exitCode: result.exitCode, errorCode: sandboxCommandErrorCode(detail), }); } return (result.result ?? result.artifacts?.stdout ?? "").toString(); diff --git a/packages/plugins/sdk/src/environment-sync-error.ts b/packages/plugins/sdk/src/environment-sync-error.ts index a630bb126e1..7d6a51a95da 100644 --- a/packages/plugins/sdk/src/environment-sync-error.ts +++ b/packages/plugins/sdk/src/environment-sync-error.ts @@ -68,7 +68,7 @@ const SCHEMA = "paperclip/environment-sync-error/v1"; // Match the host's workspace restore diagnostic allowlist. Both sides validate // independently: plugin-supplied data must never widen the host's log surface. const ERROR_CODES = new Set([ - "ENOENT", "EACCES", "EPERM", "ENOSPC", "EIO", "EXDEV", "ENOTDIR", "EISDIR", + "ENOENT", "EACCES", "EPERM", "ENOSPC", "EIO", "EXDEV", "ENOTDIR", "EISDIR", "ENAMETOOLONG", "ECONNRESET", "ECONNREFUSED", "ETIMEDOUT", "EPIPE", "ENOTFOUND", "EAI_AGAIN", "ABORT_ERR", "UND_ERR_CONNECT_TIMEOUT", "UND_ERR_SOCKET", ]); diff --git a/packages/plugins/sdk/tests/environment-sync-error.test.ts b/packages/plugins/sdk/tests/environment-sync-error.test.ts index c2480a707ce..f86196f0ba3 100644 --- a/packages/plugins/sdk/tests/environment-sync-error.test.ts +++ b/packages/plugins/sdk/tests/environment-sync-error.test.ts @@ -34,6 +34,7 @@ describe("environment sync diagnostic envelope", () => { const wrap = (cause: unknown) => ({ cause }); expect(environmentSyncErrorData(wrap(wrap(wrap({ code: "EIO" }))))).toEqual({ schema, diagnostic: { errorCode: "EIO" } }); expect(environmentSyncErrorData(wrap(wrap(wrap(wrap({ code: "EIO" })))))).toBeUndefined(); + expect(environmentSyncErrorData({ code: "ENAMETOOLONG" })).toEqual({ schema, diagnostic: { errorCode: "ENAMETOOLONG" } }); }); it("revalidates worker fields and ignores arbitrary payload and protocol versions", () => {