diff --git a/server/src/services/native-runtime/native-failure-redaction.test.ts b/server/src/services/native-runtime/native-failure-redaction.test.ts new file mode 100644 index 00000000000..a734c024cc3 --- /dev/null +++ b/server/src/services/native-runtime/native-failure-redaction.test.ts @@ -0,0 +1,40 @@ +import { describe, expect, it } from "vitest"; +import { redactNativeFailureError } from "./native-failure-redaction.js"; + +const redact = (text: string) => text.replaceAll("agent-secret", "[redacted]"); + +describe("redactNativeFailureError", () => { + it("keeps a timed-out transfer's own cause when its message cannot be reassigned", async () => { + const signal = AbortSignal.timeout(1); + await new Promise(resolve => setTimeout(resolve, 20)); + const timeout = signal.reason as Error; + + const redacted = redactNativeFailureError(timeout, redact); + + expect(redacted).toBeInstanceOf(Error); + expect(redacted.name).toBe("TimeoutError"); + expect(redacted.message).toBe("The operation was aborted due to timeout"); + }); + + it("redacts a read-only message and stack without throwing", () => { + const failure = new DOMException("upload for agent-secret stalled", "AbortError"); + + const redacted = redactNativeFailureError(failure, redact); + + expect(redacted.name).toBe("AbortError"); + expect(redacted.message).toBe("upload for [redacted] stalled"); + expect(redacted.stack ?? "").not.toContain("agent-secret"); + }); + + it("redacts an ordinary error in place and keeps its type", () => { + class TypedFailure extends Error {} + const failure = new TypedFailure("provider rejected agent-secret"); + + const redacted = redactNativeFailureError(failure, redact); + + expect(redacted).toBe(failure); + expect(redacted).toBeInstanceOf(TypedFailure); + expect(redacted.message).toBe("provider rejected [redacted]"); + expect(redacted.stack).not.toContain("agent-secret"); + }); +}); diff --git a/server/src/services/native-runtime/native-failure-redaction.ts b/server/src/services/native-runtime/native-failure-redaction.ts new file mode 100644 index 00000000000..cf1be0fce9b --- /dev/null +++ b/server/src/services/native-runtime/native-failure-redaction.ts @@ -0,0 +1,17 @@ +export function redactNativeFailureError(error: Error, redact: (text: string) => string): Error { + const originalMessage = error.message; + const message = redact(originalMessage); + const stack = error.stack + ? redact(originalMessage ? error.stack.split(originalMessage).join(message) : error.stack) + : undefined; + try { + error.message = message; + if (stack !== undefined) error.stack = stack; + return error; + } catch { + const replacement = new Error(message); + replacement.name = error.name; + replacement.stack = stack ?? `${error.name}: ${message}`; + return replacement; + } +} diff --git a/server/src/services/native-runtime/native-session-executor.ts b/server/src/services/native-runtime/native-session-executor.ts index db4c578406e..853df3f50b6 100644 --- a/server/src/services/native-runtime/native-session-executor.ts +++ b/server/src/services/native-runtime/native-session-executor.ts @@ -100,6 +100,7 @@ import { copyBackGrokAuth } from "@paperclipai/adapter-grok-local/server"; import { inferOpenAiCompatibleBiller, type AdapterUsageCheckpoint } from "@paperclipai/adapter-utils"; import { createAgentIdentityRedactor } from "../agent-identity-redaction.js"; +import { redactNativeFailureError } from "./native-failure-redaction.js"; import { parseCodexCliVersion } from "./codex-runtime-compatibility.js"; import { createNativeToolTrace, type NativeToolTrace } from "./native-tool-trace.js"; import { createNativeProviderFailureObservation } from "./native-provider-failure.js"; @@ -12729,10 +12730,7 @@ async function executePaperclipNativeSessionAttempt( // Preserve typed failure semantics while removing credentials before any // persistence, reporting, logging, or rethrow at this boundary. if (error instanceof Error) { - const originalMessage = error.message; - error.message = redactIdentityText(originalMessage); - if (error.stack) error.stack = redactIdentityText(originalMessage - ? error.stack.split(originalMessage).join(error.message) : error.stack); + error = redactNativeFailureError(error, redactIdentityText); } else { error = identityRedactor.redact(error); } diff --git a/server/src/services/native-runtime/native-startup-retry.integration.test.ts b/server/src/services/native-runtime/native-startup-retry.integration.test.ts index 4d75db6db41..f32a6bfd65f 100644 --- a/server/src/services/native-runtime/native-startup-retry.integration.test.ts +++ b/server/src/services/native-runtime/native-startup-retry.integration.test.ts @@ -432,6 +432,33 @@ describe("native provider startup retry", () => { } }); + it("records a timed-out step's own cause and settles the run through the failure classifier", async () => { + const seeded = await seedRun(await createAgent()); + const expired = AbortSignal.timeout(1); + await new Promise(resolve => setTimeout(resolve, 20)); + provider.execute.mockReset().mockImplementation(async () => { throw expired.reason; }); + await expect(executePaperclipNativeSession({ db, execution: seeded.execution, + runnerInstanceId: seeded.runnerInstanceId, + backend: { + descriptor: async () => { throw new Error("Unexpected provider descriptor call"); }, + openSession: async () => { throw new Error("Unexpected provider session call"); }, + } })).rejects.toMatchObject({ name: "TimeoutError", message: "The operation was aborted due to timeout" }); + + const state = await read(seeded); + expect(state.coordinator).toMatchObject({ + attempt: 1, + leaseOwner: null, + failureDetail: expect.objectContaining({ + message: "The operation was aborted due to timeout", + originalFailureCode: "native_session_interrupted", + providerSessionEstablished: false, + providerEventsExist: false, + }), + }); + expect(state.run).toMatchObject({ status: "failed", errorCode: state.coordinator.failureCode }); + expect(state.run.errorCode).not.toBe("native_continuation_requires_reconciliation"); + }); + it("retries a rejected session.open with growing backoff instead of blocking the task", async () => { const seeded = await seedRun(await createAgent());