|
| 1 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | + |
| 3 | +const mocks = vi.hoisted(() => ({ |
| 4 | + getCurrentPrePrCheckConfig: vi.fn(), |
| 5 | + runPrePrChecksWithFixes: vi.fn(), |
| 6 | + info: vi.fn(), |
| 7 | + warn: vi.fn(), |
| 8 | + error: vi.fn(), |
| 9 | +})); |
| 10 | + |
| 11 | +vi.mock("../db/client.js", () => ({ getDb: () => ({ kind: "db" }) })); |
| 12 | +vi.mock("../pre-pr-checks/store.js", () => ({ |
| 13 | + getCurrentPrePrCheckConfig: (...args: any[]) => |
| 14 | + mocks.getCurrentPrePrCheckConfig(...args), |
| 15 | +})); |
| 16 | +vi.mock("../pre-pr-checks/runner.js", () => ({ |
| 17 | + runPrePrChecksWithFixes: (...args: any[]) => |
| 18 | + mocks.runPrePrChecksWithFixes(...args), |
| 19 | +})); |
| 20 | +vi.mock("../lib/logger.js", () => ({ |
| 21 | + logger: { info: mocks.info, warn: mocks.warn, error: mocks.error }, |
| 22 | +})); |
| 23 | +vi.mock("../../env.js", () => ({ |
| 24 | + env: { DASHBOARD_ORIGIN: "https://dashboard.example.com" }, |
| 25 | +})); |
| 26 | + |
| 27 | +import { |
| 28 | + PRE_PR_CHECKS_FAILURE_CAUSE_MAX_LENGTH, |
| 29 | + PRE_PR_CHECKS_FAILURE_STACK_TAIL_MAX_LENGTH, |
| 30 | + prePrChecksFailureMustPropagate, |
| 31 | + prePrChecksFailureReport, |
| 32 | + runPrePrChecksStep, |
| 33 | +} from "./agent.js"; |
| 34 | +import { isDurationAbortError } from "./run-budget.js"; |
| 35 | +import { isRunControlError } from "./run-control-error.js"; |
| 36 | +import { runControlErrorCases } from "./blocks/test-support.js"; |
| 37 | + |
| 38 | +const MESSAGE_LEAD = "The Pre-PR checks step failed: "; |
| 39 | + |
| 40 | +function namedError(name: string, message: string): Error { |
| 41 | + const error = new Error(message); |
| 42 | + error.name = name; |
| 43 | + return error; |
| 44 | +} |
| 45 | + |
| 46 | +function runStep() { |
| 47 | + return runPrePrChecksStep("sbx-test-123", "codex", "gpt-5"); |
| 48 | +} |
| 49 | + |
| 50 | +describe("pre-PR checks step failure cause", () => { |
| 51 | + beforeEach(() => { |
| 52 | + vi.clearAllMocks(); |
| 53 | + mocks.getCurrentPrePrCheckConfig.mockResolvedValue({ |
| 54 | + version: 7, |
| 55 | + config: { repositories: [] }, |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + it("returns the checks result and the loaded version when nothing throws", async () => { |
| 60 | + mocks.runPrePrChecksWithFixes.mockResolvedValue({ |
| 61 | + outcome: "passed", |
| 62 | + passed: true, |
| 63 | + fixCycles: 0, |
| 64 | + fixCycleUsages: [], |
| 65 | + budgetFailure: null, |
| 66 | + summary: "All checks passed.", |
| 67 | + }); |
| 68 | + |
| 69 | + await expect(runStep()).resolves.toEqual({ |
| 70 | + outcome: "passed", |
| 71 | + passed: true, |
| 72 | + fixCycles: 0, |
| 73 | + fixCycleUsages: [], |
| 74 | + budgetFailure: null, |
| 75 | + summary: "All checks passed.", |
| 76 | + configurationVersion: 7, |
| 77 | + }); |
| 78 | + expect(mocks.error).not.toHaveBeenCalled(); |
| 79 | + }); |
| 80 | + |
| 81 | + it("names the thrown cause instead of leaving Workflow's wrapper to speak alone", async () => { |
| 82 | + // Production runs wrun_01M0CBQNAX24STRMN5SGCKKGB2 and |
| 83 | + // wrun_01M0CAZKV3YMNFBCZJA8MT95GW died here reading only "exceeded max |
| 84 | + // retries", with no sanitized output captured and nothing in the runtime |
| 85 | + // logs. Whatever the step throws has to reach the operator-facing text. |
| 86 | + mocks.runPrePrChecksWithFixes.mockRejectedValue( |
| 87 | + new Error("sandbox connection reset"), |
| 88 | + ); |
| 89 | + |
| 90 | + await expect(runStep()).rejects.toThrow( |
| 91 | + `${MESSAGE_LEAD}sandbox connection reset`, |
| 92 | + ); |
| 93 | + expect(mocks.error).toHaveBeenCalledTimes(1); |
| 94 | + const [record, msg] = mocks.error.mock.calls[0] as [ |
| 95 | + { version: number | null; name: string; cause: string; stackTail: string }, |
| 96 | + string, |
| 97 | + ]; |
| 98 | + expect(msg).toBe("pre_pr_checks_step_failed"); |
| 99 | + expect(record.version).toBe(7); |
| 100 | + expect(record.name).toBe("Error"); |
| 101 | + expect(record.cause).toBe("sandbox connection reset"); |
| 102 | + // The tail, so a very deep stack keeps its innermost-to-outermost end |
| 103 | + // rather than growing the log record without bound. The message itself is |
| 104 | + // already in `cause`, so nothing is lost when the head is clipped. |
| 105 | + expect(record.stackTail).toMatch(/\bat\b/); |
| 106 | + expect(record.stackTail.length).toBeLessThanOrEqual( |
| 107 | + PRE_PR_CHECKS_FAILURE_STACK_TAIL_MAX_LENGTH, |
| 108 | + ); |
| 109 | + }); |
| 110 | + |
| 111 | + it("prefers a system error code over a class name that says nothing", async () => { |
| 112 | + mocks.runPrePrChecksWithFixes.mockRejectedValue( |
| 113 | + Object.assign(new Error("connect ECONNREFUSED 10.0.0.1:443"), { |
| 114 | + code: "ECONNREFUSED", |
| 115 | + }), |
| 116 | + ); |
| 117 | + |
| 118 | + await expect(runStep()).rejects.toThrow( |
| 119 | + `${MESSAGE_LEAD}ECONNREFUSED: connect ECONNREFUSED 10.0.0.1:443`, |
| 120 | + ); |
| 121 | + }); |
| 122 | + |
| 123 | + it("bounds a runaway cause instead of embedding it whole", async () => { |
| 124 | + const thrownMessage = "sandbox refused the launch. ".repeat(200); |
| 125 | + mocks.runPrePrChecksWithFixes.mockRejectedValue(new Error(thrownMessage)); |
| 126 | + |
| 127 | + const thrown = await runStep().then( |
| 128 | + () => null, |
| 129 | + (err: unknown) => err as Error, |
| 130 | + ); |
| 131 | + |
| 132 | + expect(thrown?.message).toContain(MESSAGE_LEAD); |
| 133 | + expect(thrown?.message).not.toContain(thrownMessage); |
| 134 | + // Sentence plus the bound the cause is clamped to, and nothing more, so a |
| 135 | + // runaway error text cannot become the run status. |
| 136 | + expect(thrown?.message.length).toBeLessThanOrEqual( |
| 137 | + MESSAGE_LEAD.length + PRE_PR_CHECKS_FAILURE_CAUSE_MAX_LENGTH, |
| 138 | + ); |
| 139 | + const logged = mocks.error.mock.calls[0]?.[0] as { cause: string }; |
| 140 | + expect(logged.cause.length).toBe(PRE_PR_CHECKS_FAILURE_CAUSE_MAX_LENGTH); |
| 141 | + }); |
| 142 | + |
| 143 | + it.each(runControlErrorCases())( |
| 144 | + "rethrows %s untouched so the call site still recognizes it", |
| 145 | + async (_label, error) => { |
| 146 | + mocks.runPrePrChecksWithFixes.mockRejectedValue(error); |
| 147 | + |
| 148 | + await expect(runStep()).rejects.toBe(error); |
| 149 | + const thrown = await runStep().then( |
| 150 | + () => null, |
| 151 | + (err: unknown) => err, |
| 152 | + ); |
| 153 | + expect(isRunControlError(thrown)).toBe(true); |
| 154 | + expect(mocks.error).not.toHaveBeenCalled(); |
| 155 | + }, |
| 156 | + ); |
| 157 | + |
| 158 | + it.each([["AbortError"], ["TimeoutError"]])( |
| 159 | + "rethrows a %s untouched so the duration budget stop survives", |
| 160 | + async (name) => { |
| 161 | + const error = namedError(name, "The operation was aborted."); |
| 162 | + mocks.runPrePrChecksWithFixes.mockRejectedValue(error); |
| 163 | + |
| 164 | + await expect(runStep()).rejects.toBe(error); |
| 165 | + const thrown = await runStep().then( |
| 166 | + () => null, |
| 167 | + (err: unknown) => err, |
| 168 | + ); |
| 169 | + expect(isDurationAbortError(thrown)).toBe(true); |
| 170 | + expect(mocks.error).not.toHaveBeenCalled(); |
| 171 | + }, |
| 172 | + ); |
| 173 | + |
| 174 | + it("wraps only what wrapping cannot break", () => { |
| 175 | + // Why the two predicates gate the wrap at all: both match structurally on |
| 176 | + // `name`, so a control error re-thrown as `new Error(message)` stops being |
| 177 | + // one, and a duration budget stop would report as a generic failure. |
| 178 | + const budgetStop = namedError("RunBudgetError", "budget exceeded"); |
| 179 | + const abort = namedError("AbortError", "The operation was aborted."); |
| 180 | + const identity = (value: string) => value; |
| 181 | + |
| 182 | + expect(prePrChecksFailureMustPropagate(budgetStop)).toBe(true); |
| 183 | + expect(prePrChecksFailureMustPropagate(abort)).toBe(true); |
| 184 | + expect(prePrChecksFailureMustPropagate(new Error("sandbox died"))).toBe(false); |
| 185 | + |
| 186 | + const rewrappedBudget = new Error( |
| 187 | + prePrChecksFailureReport(budgetStop, identity).message, |
| 188 | + ); |
| 189 | + expect(isRunControlError(rewrappedBudget)).toBe(false); |
| 190 | + const rewrappedAbort = new Error( |
| 191 | + prePrChecksFailureReport(abort, identity).message, |
| 192 | + ); |
| 193 | + expect(isDurationAbortError(rewrappedAbort)).toBe(false); |
| 194 | + }); |
| 195 | + |
| 196 | + it("redacts the cause and the stack tail through the caller's redactor", () => { |
| 197 | + const report = prePrChecksFailureReport( |
| 198 | + new Error("auth failed for token=abcd1234"), |
| 199 | + (value) => value.replace("abcd1234", "[REDACTED]"), |
| 200 | + ); |
| 201 | + |
| 202 | + expect(report.cause).toBe("auth failed for token=[REDACTED]"); |
| 203 | + expect(report.stackTail).not.toContain("abcd1234"); |
| 204 | + }); |
| 205 | + |
| 206 | + it("names a non-Error throw rather than dropping it", () => { |
| 207 | + const report = prePrChecksFailureReport("sandbox vanished", (v) => v); |
| 208 | + |
| 209 | + expect(report.message).toBe(`${MESSAGE_LEAD}sandbox vanished`); |
| 210 | + expect(report.name).toBe("string"); |
| 211 | + expect(report.stackTail).toBe(""); |
| 212 | + }); |
| 213 | +}); |
0 commit comments