Record bounded workspace restore failure stages (#15005)

Capture allowlisted restore substep and failure metadata for future diagnosis while preserving workspace recovery behavior, error identity, cleanup ordering, and privacy.

Validation: exact-head Greptile 5/5, passing CI, no unresolved review threads, and a clean merge. Detailed verification and limitations are recorded in the pull request.

Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
Devin FoleyandPaperclip authored and GitHub committed 2026-10-05 10:58:32 -07:00
1 parent 2e67ea8dc7
commit 3b47a6befd
18 files changed
+647 -109

No files matched your search

@@ -2,6 +2,12 @@ import { createRequire } from "node:module";
import { afterEach, describe, expect, it, vi } from "vitest";
import { collectRunFailureDiagnostics, sanitizeRunFailureDiagnostics } from "../services/run-failure-diagnostics.js";
import type { heartbeatRuns } from "@paperclipai/db";
import {
preserveWorkspaceRestoreErrorDiagnostic,
withWorkspaceRestoreDiagnostics,
withWorkspaceRestoreStep,
} from "@paperclipai/adapter-utils/workspace-restore-diagnostics";
import { createWorkspaceRestoreTeardown } from "@paperclipai/adapter-utils/workspace-restore-teardown";
// Sentry is an optional peer. When installed, exercise the real SDK with an
// in-memory transport, including its context behavior without an OTel manager.
@@ -205,4 +211,80 @@ describe.skipIf(!sentryPackage)("run failure context with the real Sentry SDK",
}
}
});
it("reports the failing restore step without exposing the wrapped Git error", async () => {
const Sentry = sentryPackage!;
const events: Array<Record<string, unknown>> = [];
vi.stubEnv("SENTRY_DSN_BACKEND", "https://public@example.invalid/1");
vi.doMock("../peer-version-check.js", () => ({ checkExactPeerVersions: () => ({ ok: true }) }));
vi.doMock("@sentry/node", () => ({
...Sentry,
init: (options: Record<string, unknown>) => Sentry.init({
...options,
transport: () => ({ send: async () => ({}), flush: async () => true }),
beforeSend: (event: Record<string, unknown>) => { events.push(event); return event; },
}),
}));
vi.resetModules();
const { sentryReady, captureRunFailure, captureException } = await import("../sentry.js");
await sentryReady;
const source = Object.assign(new Error("private-restore-Git command failed in /private-restore-workspace", {
cause: new Error("private-restore-provider cause"),
}), {
code: 1, statusCode: 503, stdout: "private-restore-file contents", stderr: "private-restore-Git output",
command: "private-restore-command", path: "/private-restore-workspace", response: { body: "private-restore-response" },
});
const wrapper = preserveWorkspaceRestoreErrorDiagnostic(new Error("private-restore-Git wrapper"), source);
expect(wrapper).not.toHaveProperty("cause");
const logs: string[] = [];
const restore = createWorkspaceRestoreTeardown({
stagedRuntime: {
restoreWorkspace: (onProgress) => withWorkspaceRestoreDiagnostics("workspace", () =>
withWorkspaceRestoreStep("directory_merge", () =>
withWorkspaceRestoreStep("git_integration", async () => { throw wrapper; })), onProgress),
},
onLog: async (_stream, line) => { logs.push(line); },
startMessage: "Restoring workspace\n",
failurePrefix: "Workspace restore failed",
});
const outcome = await restore();
expect(outcome).toEqual({
ok: false, code: "restore_failed",
diagnostic: { phase: "workspace", step: "git_integration", errorCode: "unknown", httpStatus: 503, exitCode: 1 },
});
if (outcome.ok) throw new Error("Expected the restore fixture to fail");
expect(JSON.stringify({ outcome, logs })).not.toContain("private-restore-");
const diagnostics = sanitizeRunFailureDiagnostics(collectRunFailureDiagnostics({
resultJson: {
workspaceRestoreFailure: outcome.code,
workspaceRestoreDiagnostic: {
...outcome.diagnostic,
message: "private-restore-persisted message", cause: source, command: "private-restore-persisted command",
},
},
} as unknown as typeof heartbeatRuns.$inferSelect, {}));
captureRunFailure({
taskId: "11111111-1111-4111-8111-111111111111", runId: "22222222-2222-4222-8222-222222222222",
errorMessage: "Workspace restore failed. Workspace files need recovery.",
errorCode: "workspace_restore_failed", agentAdapter: "fixture-adapter", runStatus: "failed",
exitCode: 1, signal: null, diagnostics,
});
captureException(new Error("unrelated restore diagnostic fixture"));
await Sentry.flush(2000);
expect(events).toHaveLength(2);
const restoreEvent = events.find((event) => (event.tags as Record<string, unknown>)?.error_code === "workspace_restore_failed");
expect(restoreEvent).toMatchObject({ contexts: { run_execution: {
workspaceRestoreFailure: "restore_failed", workspaceRestorePhase: "workspace",
workspaceRestoreStep: "git_integration", workspaceRestoreErrorCode: "unknown",
workspaceRestoreHttpStatus: 503, workspaceRestoreExitCode: 1,
} } });
expect((restoreEvent?.exception as { values: unknown[] }).values).toHaveLength(1);
expect(JSON.stringify(events)).not.toContain("private-restore-");
const unrelated = events.find((event) => event !== restoreEvent);
expect(unrelated).not.toHaveProperty("contexts.run_execution");
expect(unrelated).not.toHaveProperty("contexts.run_failure");
expect(unrelated).not.toHaveProperty("tags.error_code");
});
});
@@ -55,6 +55,78 @@ describe("run failure diagnostics", () => {
},
);
it("copies only classified restore diagnostics from the saved result", () => {
const result = sanitizeRunFailureDiagnostics(collectRunFailureDiagnostics(run({ resultJson: {
workspaceRestoreFailure: "restore_failed",
workspaceRestoreDiagnostic: {
phase: "workspace", step: "git_integration", errorCode: "unknown", httpStatus: 503, exitCode: 1,
message: "private command failed", path: "/private/workspace", stdout: "private file contents",
cause: { code: "EIO", message: "private nested cause" },
},
} }), {}));
expect(result.execution).toEqual({
workspaceRestoreFailure: "restore_failed", workspaceRestorePhase: "workspace",
workspaceRestoreStep: "git_integration", workspaceRestoreErrorCode: "unknown",
workspaceRestoreHttpStatus: 503, workspaceRestoreExitCode: 1,
});
expect(JSON.stringify(result)).not.toContain("private");
expect(result.exceptions).toEqual([]);
});
it("requires a known restore failure before reading its diagnostic", () => {
const diagnostic = { phase: "workspace", step: "git_import", errorCode: "EIO", exitCode: 1 };
for (const code of [undefined, null, "private unknown classification"]) {
const result = collectRunFailureDiagnostics(run({ resultJson: {
workspaceRestoreFailure: code, workspaceRestoreDiagnostic: diagnostic,
} }), {});
expect(result.execution).toEqual({});
}
const resultJson = Object.defineProperty({}, "workspaceRestoreDiagnostic", {
get() { throw new Error("must not read unrelated diagnostic"); },
});
expect(collectRunFailureDiagnostics(run({ resultJson }), {}).execution).toEqual({});
});
it.each([null, false, 1, "private payload", [], { phase: "private phase", errorCode: "EIO" }])(
"omits malformed workspace restore diagnostics (%j)", (value) => {
const result = collectRunFailureDiagnostics(run({ resultJson: {
workspaceRestoreFailure: "restore_failed", workspaceRestoreDiagnostic: value,
} }), {});
expect(result.execution).toEqual({ workspaceRestoreFailure: "restore_failed" });
expect(JSON.stringify(result)).not.toContain("private");
},
);
it.each([null, -1, Infinity, NaN, 1.5, 600, "private status", {}])(
"omits invalid restore status and exit values (%j)", (value) => {
const result = collectRunFailureDiagnostics(run({ resultJson: {
workspaceRestoreFailure: "restore_failed",
workspaceRestoreDiagnostic: {
phase: "asset", step: "private step", errorCode: "private code", httpStatus: value, exitCode: value,
},
} }), {});
expect(result.execution).toEqual({
workspaceRestoreFailure: "restore_failed", workspaceRestorePhase: "asset", workspaceRestoreErrorCode: "unknown",
});
},
);
it("does not walk restore causes or expose errors from diagnostic getters", () => {
const diagnostic = Object.defineProperties({ phase: "workspace", errorCode: "EIO" }, {
step: { get() { throw new Error("private getter"); } },
httpStatus: { get() { throw new Error("private getter"); } },
exitCode: { get() { throw new Error("private getter"); } },
cause: { get() { throw new Error("must not walk cause"); } },
});
const result = collectRunFailureDiagnostics(run({ resultJson: {
workspaceRestoreFailure: "restore_failed", workspaceRestoreDiagnostic: diagnostic,
} }), {});
expect(result.execution).toEqual({
workspaceRestoreFailure: "restore_failed", workspaceRestorePhase: "workspace", workspaceRestoreErrorCode: "EIO",
});
expect(result.exceptions).toEqual([]);
});
it("includes only bounded ACP activity fields, not tool names, identities, or output", () => {
const result = sanitizeRunFailureDiagnostics(collectRunFailureDiagnostics(run({ resultJson: {
acpLastEventAgeMs: 14_000_000, acpObservedEventCount: 9, acpPendingToolCount: 2,
+12 -1
View File
@@ -2,6 +2,7 @@ import type { heartbeatRuns } from "@paperclipai/db";
import { readRunCancellation } from "./run-cancellation.js";
import { WORKSPACE_RESTORE_FAILURE_CODES } from "@paperclipai/shared";
import { redactDiagnosticText } from "@paperclipai/adapter-utils/command-redaction";
import { sanitizeWorkspaceRestoreDiagnostic } from "@paperclipai/adapter-utils/workspace-restore-diagnostics";
import { redactCurrentUserText } from "../log-redaction.js";
import { redactSensitiveText, REDACTED_EVENT_VALUE } from "../redaction.js";
@@ -165,7 +166,17 @@ export function collectRunFailureDiagnostics(run: Run, options: RunFailureReport
if (typeof toolInventoryComplete === "boolean") execution.acpToolInventoryComplete = toolInventoryComplete;
const restoreFailure = read(result, "workspaceRestoreFailure");
const restoreCode = WORKSPACE_RESTORE_FAILURE_CODES.find((code) => code === restoreFailure);
if (restoreCode) execution.workspaceRestoreFailure = restoreCode;
if (restoreCode) {
execution.workspaceRestoreFailure = restoreCode;
const diagnostic = sanitizeWorkspaceRestoreDiagnostic(read(result, "workspaceRestoreDiagnostic"));
if (diagnostic) {
execution.workspaceRestorePhase = diagnostic.phase;
execution.workspaceRestoreErrorCode = diagnostic.errorCode;
if (diagnostic.step) execution.workspaceRestoreStep = diagnostic.step;
if (diagnostic.httpStatus !== undefined) execution.workspaceRestoreHttpStatus = diagnostic.httpStatus;
if (diagnostic.exitCode !== undefined) execution.workspaceRestoreExitCode = diagnostic.exitCode;
}
}
const adapter = scalars(options.adapterErrorMeta, [
"category", "phase", "errorName", "acpCode", "causeMessage", "retryable",
"stackPreview", "status", "statusCode", "requestId",