diff --git a/server/src/__tests__/run-failure-sentry.test.ts b/server/src/__tests__/run-failure-sentry.test.ts index b4be99438ee..61ff2314e60 100644 --- a/server/src/__tests__/run-failure-sentry.test.ts +++ b/server/src/__tests__/run-failure-sentry.test.ts @@ -233,6 +233,31 @@ describe("captureRunFailure", () => { expect(JSON.stringify(fingerprints)).not.toMatch(/workspace|timed out|plain failure/i); }); + it("separates coded setup conflicts from each other and from uncoded ones", async () => { + const { sentryModule, captureException } = await importFreshSentryWithGateOpen(); + const { conflict } = await import("../errors.js"); + const { collectRunFailureDiagnostics } = await import("../services/run-failure-diagnostics.js"); + const setupFailure = (error: Error) => baseEvent({ + errorCode: "setup_failed", agentAdapter: "paperclip_runner", errorMessage: error.message, + diagnostics: collectRunFailureDiagnostics({ resultJson: null } as Parameters[0], + { error, phase: "setup" }), + }); + + sentryModule.captureRunFailure(setupFailure(conflict("The workspace still has an unfinished writer or save operation.", + { code: "remote_workspace_writer_pending" }))); + sentryModule.captureRunFailure(setupFailure(conflict("This workspace requires a supported remote execution environment.", + { code: "remote_workspace_execution_required" }))); + sentryModule.captureRunFailure(setupFailure(conflict("Workspace ownership changed."))); + + const prefix = ["setup_failed", "paperclip_runner", "setup"]; + expect(captureException.mock.calls.map(([, context]) => context?.fingerprint)).toEqual([ + [...prefix, "remote_workspace_writer_pending"], + [...prefix, "remote_workspace_execution_required"], + [...prefix, "exception:Error", "status:409"], + ]); + expect(captureException.mock.calls[0]![1]?.tags).toMatchObject({ root_error_code: "remote_workspace_writer_pending" }); + }); + it("separates native turn failures by their runner failure code", async () => { const { sentryModule, captureException } = await importFreshSentryWithGateOpen(); const turnFailure = (provider: NonNullable["provider"]) => baseEvent({ diff --git a/server/src/services/__tests__/run-failure-diagnostics.test.ts b/server/src/services/__tests__/run-failure-diagnostics.test.ts index df627d1ad52..9cc2e2add84 100644 --- a/server/src/services/__tests__/run-failure-diagnostics.test.ts +++ b/server/src/services/__tests__/run-failure-diagnostics.test.ts @@ -1,3 +1,4 @@ +import { conflict } from "../../errors.js"; import { captureEnvironmentAcquisitionDiagnostic } from "../environment-acquisition-diagnostics.js"; import { describe, expect, it } from "vitest"; import type { heartbeatRuns } from "@paperclipai/db"; @@ -23,6 +24,20 @@ describe("run failure diagnostics", () => { expect(diagnostic.exceptions[0]).toMatchObject({ code: "tool_name_collision" }); expect(JSON.stringify(diagnostic)).not.toContain("private-token"); }); + it("reads a bounded HTTP error detail code when the error has no own code", () => { + const code = (details?: unknown, extra: Record = {}) => + collect(Object.assign(conflict("Setup refused", details), extra)).exceptions[0]?.code; + expect(code({ code: "remote_workspace_writer_pending" })).toBe("remote_workspace_writer_pending"); + expect(code({ code: "remote_workspace_execution_required", token: "private-token" })).toBe("remote_workspace_execution_required"); + expect(code({ code: "remote_workspace_writer_pending" }, { code: "ECONNRESET" })).toBe("ECONNRESET"); + expect(code({ code: "remote_workspace_writer_pending" }, { reasonCode: "tool_name_collision" })).toBe("tool_name_collision"); + for (const details of [undefined, null, "remote_workspace_writer_pending", { code: "Bearer private-token" }, + { code: "x".repeat(101) }, { code: 409 }, Object.defineProperty({}, "code", { get() { throw new Error("private"); } })]) { + expect(code(details)).toBeUndefined(); + } + expect(JSON.stringify(collect(conflict("Setup refused", { code: "remote_workspace_writer_pending", token: "private-token" })))) + .not.toContain("private-token"); + }); it("reports only private acquisition observations for the exact failed setup run", () => { const scope = { companyId: "company-fixture", environmentId: "environment-fixture", runId: "run-fixture" }; const diagnostic = { phase: "workspace", elapsedMs: 100, budgetMs: 2_000 } as const; diff --git a/server/src/services/run-failure-diagnostics.ts b/server/src/services/run-failure-diagnostics.ts index 548cd3ea0c8..4945d76e735 100644 --- a/server/src/services/run-failure-diagnostics.ts +++ b/server/src/services/run-failure-diagnostics.ts @@ -426,6 +426,10 @@ export function collectRunFailureDiagnostics(run: Run, options: RunFailureReport const reasonCode = read(error, "reasonCode"); if (typeof reasonCode === "string") entry.code = reasonCode; } + if (entry.code === undefined) { + const detailCode = read(read(error, "details"), "code"); + if (typeof detailCode === "string" && /^[a-z][a-z0-9_]{1,99}$/.test(detailCode)) entry.code = detailCode; + } const status = read(error, "status") ?? read(error, "statusCode"); if (typeof status === "number" && Number.isInteger(status) && status >= 100 && status <= 599) entry.status = status; const requestId = read(error, "requestId") ?? read(error, "request_id");