|
| 1 | +/** |
| 2 | + * Round 2 adversarial regression for the v1.4.0 bridge merge. |
| 3 | + * |
| 4 | + * Distinct from v140-merge-adversarial.test.ts (round 1, static invariants). |
| 5 | + * This file exercises: |
| 6 | + * - Property-based fuzzing of pure helpers (1000+ random inputs each) |
| 7 | + * - Failure injection through deriveAgentOutcomeReason with synthetic provider errors |
| 8 | + * - Documented [KNOWN GAP] tests for classifyError pattern coverage holes |
| 9 | + * |
| 10 | + * Run on every future bridge merge to catch: |
| 11 | + * - maskString redaction regressions (API key / Bearer token leaks) |
| 12 | + * - deriveAgentOutcomeReason crashes on malformed inputs |
| 13 | + * - Reason length blowups |
| 14 | + * - classifyError gaps that would reduce telemetry diagnostic value |
| 15 | + */ |
| 16 | +import { describe, expect, test } from "bun:test" |
| 17 | +import { Telemetry } from "../../src/altimate/telemetry" |
| 18 | +import { randomBytes } from "crypto" |
| 19 | + |
| 20 | +const FUZZ_ITERATIONS = 500 // halved vs scratch run — keeps CI fast while still catching regressions |
| 21 | + |
| 22 | +// ---------- helpers ---------- |
| 23 | +function randString(maxLen = 200): string { |
| 24 | + const len = Math.floor(Math.random() * maxLen) |
| 25 | + const chars: string[] = [] |
| 26 | + for (let i = 0; i < len; i++) { |
| 27 | + const r = Math.random() |
| 28 | + if (r < 0.4) chars.push(String.fromCharCode(0x20 + Math.floor(Math.random() * 95))) |
| 29 | + else if (r < 0.6) chars.push(String.fromCharCode(Math.floor(Math.random() * 32))) |
| 30 | + else if (r < 0.85) chars.push(String.fromCharCode(0x4e00 + Math.floor(Math.random() * 0x500))) |
| 31 | + else chars.push("🔥") |
| 32 | + } |
| 33 | + return chars.join("") |
| 34 | +} |
| 35 | + |
| 36 | +function randAPIKey(): string { |
| 37 | + const prefix = ["sk-ant-", "sk-proj-", "sk-", "Bearer "][Math.floor(Math.random() * 4)] |
| 38 | + const len = 25 + Math.floor(Math.random() * 50) |
| 39 | + return prefix + randomBytes(len).toString("hex").slice(0, len) |
| 40 | +} |
| 41 | + |
| 42 | +const OUTCOMES = ["completed", "abandoned", "aborted", "error"] as const |
| 43 | +type Outcome = (typeof OUTCOMES)[number] |
| 44 | + |
| 45 | +// ---------- maskString fuzz ---------- |
| 46 | +describe("v1.4.0 fuzz — maskString hardness", () => { |
| 47 | + test("never produces invalid UTF-8 across random inputs", () => { |
| 48 | + for (let i = 0; i < FUZZ_ITERATIONS; i++) { |
| 49 | + const input = randString(500) |
| 50 | + const output = Telemetry.maskString(input) |
| 51 | + expect(Buffer.from(output, "utf8").toString("utf8")).toBe(output) |
| 52 | + } |
| 53 | + }) |
| 54 | + |
| 55 | + test("never lets a 25+ char API-key prefix leak", () => { |
| 56 | + let leaks = 0 |
| 57 | + for (let i = 0; i < FUZZ_ITERATIONS; i++) { |
| 58 | + const key = randAPIKey() |
| 59 | + const input = `${randString(50)} ${key} ${randString(50)}` |
| 60 | + const output = Telemetry.maskString(input) |
| 61 | + if (/sk-(?:ant-|proj-)?[A-Za-z0-9]{20,}/.test(output)) leaks++ |
| 62 | + if (/Bearer\s+[A-Za-z0-9]{20,}/i.test(output)) leaks++ |
| 63 | + } |
| 64 | + expect(leaks).toBe(0) |
| 65 | + }) |
| 66 | + |
| 67 | + test("output never explodes in length", () => { |
| 68 | + for (let i = 0; i < FUZZ_ITERATIONS; i++) { |
| 69 | + const input = randString(500) |
| 70 | + const output = Telemetry.maskString(input) |
| 71 | + expect(output.length).toBeLessThanOrEqual(input.length * 2 + 50) |
| 72 | + } |
| 73 | + }) |
| 74 | + |
| 75 | + test("never throws on any input", () => { |
| 76 | + for (let i = 0; i < FUZZ_ITERATIONS; i++) { |
| 77 | + const input = randString(2000) |
| 78 | + expect(() => Telemetry.maskString(input)).not.toThrow() |
| 79 | + } |
| 80 | + }) |
| 81 | + |
| 82 | + test("idempotent: running maskString twice on a redacted string is a no-op", () => { |
| 83 | + for (let i = 0; i < 100; i++) { |
| 84 | + const input = `Authentication failed: ${randAPIKey()}` |
| 85 | + const once = Telemetry.maskString(input) |
| 86 | + const twice = Telemetry.maskString(once) |
| 87 | + expect(twice).toBe(once) |
| 88 | + } |
| 89 | + }) |
| 90 | +}) |
| 91 | + |
| 92 | +// ---------- deriveAgentOutcomeReason fuzz ---------- |
| 93 | +describe("v1.4.0 fuzz — deriveAgentOutcomeReason invariants", () => { |
| 94 | + test("returns 3 string fields for any combination of valid inputs", () => { |
| 95 | + for (let i = 0; i < FUZZ_ITERATIONS; i++) { |
| 96 | + const out = Telemetry.deriveAgentOutcomeReason({ |
| 97 | + outcome: OUTCOMES[Math.floor(Math.random() * OUTCOMES.length)], |
| 98 | + lastToolName: Math.random() < 0.5 ? randString(30) : null, |
| 99 | + lastMessageError: Math.random() < 0.5 ? randString(800) : null, |
| 100 | + abortReason: Math.random() < 0.5 ? randString(500) : null, |
| 101 | + lastErrorClass: Math.random() < 0.5 ? randString(30) : "", |
| 102 | + }) |
| 103 | + expect(typeof out.final_tool).toBe("string") |
| 104 | + expect(typeof out.error_class).toBe("string") |
| 105 | + expect(typeof out.reason).toBe("string") |
| 106 | + } |
| 107 | + }) |
| 108 | + |
| 109 | + test("reason is bounded (≤500 for error, ≤200 for aborted)", () => { |
| 110 | + for (let i = 0; i < FUZZ_ITERATIONS; i++) { |
| 111 | + const outcome = OUTCOMES[Math.floor(Math.random() * OUTCOMES.length)] |
| 112 | + const out = Telemetry.deriveAgentOutcomeReason({ |
| 113 | + outcome, |
| 114 | + lastToolName: null, |
| 115 | + lastMessageError: randString(10000), |
| 116 | + abortReason: randString(10000), |
| 117 | + lastErrorClass: "", |
| 118 | + }) |
| 119 | + if (outcome === "error") expect(out.reason.length).toBeLessThanOrEqual(500) |
| 120 | + if (outcome === "aborted") expect(out.reason.length).toBeLessThanOrEqual(200) |
| 121 | + } |
| 122 | + }) |
| 123 | + |
| 124 | + test("never throws even with garbage outcome (cast)", () => { |
| 125 | + for (let i = 0; i < 100; i++) { |
| 126 | + expect(() => |
| 127 | + Telemetry.deriveAgentOutcomeReason({ |
| 128 | + outcome: randString(20) as Outcome, |
| 129 | + lastToolName: randString(30), |
| 130 | + lastMessageError: randString(500), |
| 131 | + abortReason: randString(500), |
| 132 | + lastErrorClass: randString(30), |
| 133 | + }), |
| 134 | + ).not.toThrow() |
| 135 | + } |
| 136 | + }) |
| 137 | + |
| 138 | + test("error reason: API keys stripped even when key is in input", () => { |
| 139 | + let leaks = 0 |
| 140 | + for (let i = 0; i < 200; i++) { |
| 141 | + const key = randAPIKey() |
| 142 | + const out = Telemetry.deriveAgentOutcomeReason({ |
| 143 | + outcome: "error", |
| 144 | + lastToolName: null, |
| 145 | + lastMessageError: `failure: ${key}`, |
| 146 | + abortReason: null, |
| 147 | + lastErrorClass: "", |
| 148 | + }) |
| 149 | + if (/sk-(?:ant-|proj-)?[A-Za-z0-9]{20,}/.test(out.reason)) leaks++ |
| 150 | + if (/Bearer\s+[A-Za-z0-9]{20,}/i.test(out.reason)) leaks++ |
| 151 | + } |
| 152 | + expect(leaks).toBe(0) |
| 153 | + }) |
| 154 | + |
| 155 | + test("aborted reason: API keys stripped even when key is in abortReason", () => { |
| 156 | + let leaks = 0 |
| 157 | + for (let i = 0; i < 200; i++) { |
| 158 | + const key = randAPIKey() |
| 159 | + const out = Telemetry.deriveAgentOutcomeReason({ |
| 160 | + outcome: "aborted", |
| 161 | + lastToolName: null, |
| 162 | + lastMessageError: null, |
| 163 | + abortReason: `cancel: ${key}`, |
| 164 | + lastErrorClass: "", |
| 165 | + }) |
| 166 | + if (/sk-(?:ant-|proj-)?[A-Za-z0-9]{20,}/.test(out.reason)) leaks++ |
| 167 | + } |
| 168 | + expect(leaks).toBe(0) |
| 169 | + }) |
| 170 | +}) |
| 171 | + |
| 172 | +// ---------- failure injection ---------- |
| 173 | +describe("v1.4.0 failure injection — synthetic provider errors flow through diagnostics", () => { |
| 174 | + test("AuthError with embedded sk-ant- key never leaks the key", () => { |
| 175 | + const out = Telemetry.deriveAgentOutcomeReason({ |
| 176 | + outcome: "error", |
| 177 | + lastToolName: null, |
| 178 | + lastMessageError: "AuthError: invalid api key sk-ant-1234567890abcdef0123456789", |
| 179 | + abortReason: null, |
| 180 | + lastErrorClass: "", |
| 181 | + }) |
| 182 | + expect(out.reason).toContain("sk-***") |
| 183 | + expect(out.reason).not.toContain("sk-ant-1234") |
| 184 | + }) |
| 185 | + |
| 186 | + test("Bearer token in error: redacted", () => { |
| 187 | + const out = Telemetry.deriveAgentOutcomeReason({ |
| 188 | + outcome: "error", |
| 189 | + lastToolName: null, |
| 190 | + lastMessageError: "Auth header rejected: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.payload", |
| 191 | + abortReason: null, |
| 192 | + lastErrorClass: "", |
| 193 | + }) |
| 194 | + expect(out.reason).toContain("Bearer ***") |
| 195 | + expect(out.reason).not.toContain("eyJhbGciOi") |
| 196 | + }) |
| 197 | + |
| 198 | + test("aborted with non-string reason normalized to 'non_string_reason' (not [object Object])", () => { |
| 199 | + const out = Telemetry.deriveAgentOutcomeReason({ |
| 200 | + outcome: "aborted", |
| 201 | + lastToolName: "bash", |
| 202 | + lastMessageError: null, |
| 203 | + abortReason: "non_string_reason", |
| 204 | + lastErrorClass: "", |
| 205 | + }) |
| 206 | + expect(out.reason).toBe("non_string_reason") |
| 207 | + expect(out.reason).not.toContain("[object") |
| 208 | + }) |
| 209 | + |
| 210 | + test("aborted preserves last tool error class (e.g. tool_timeout)", () => { |
| 211 | + const out = Telemetry.deriveAgentOutcomeReason({ |
| 212 | + outcome: "aborted", |
| 213 | + lastToolName: "edit", |
| 214 | + lastMessageError: null, |
| 215 | + abortReason: "user_cancelled", |
| 216 | + lastErrorClass: "tool_timeout", |
| 217 | + }) |
| 218 | + expect(out.error_class).toBe("tool_timeout") |
| 219 | + }) |
| 220 | + |
| 221 | + test("'connection refused' classifies via connection class (not 'unknown')", () => { |
| 222 | + const out = Telemetry.deriveAgentOutcomeReason({ |
| 223 | + outcome: "error", |
| 224 | + lastToolName: null, |
| 225 | + lastMessageError: "ECONNREFUSED 127.0.0.1:5432 connection refused", |
| 226 | + abortReason: null, |
| 227 | + lastErrorClass: "", |
| 228 | + }) |
| 229 | + expect(out.error_class).not.toBe("unknown") |
| 230 | + }) |
| 231 | + |
| 232 | + // KNOWN GAP: classifyError patterns lack "503" / "Service unavailable" / |
| 233 | + // "Rate limit" / "Retry after" keywords. Real provider errors get |
| 234 | + // classified as "unknown" — diagnostic value lost. Fix: extend |
| 235 | + // ERROR_PATTERNS in altimate/telemetry/index.ts. Tests below pin the |
| 236 | + // current behavior so a future fix flips them automatically. |
| 237 | + test("[KNOWN GAP] APIError 503 classifies as 'unknown' (pattern coverage hole)", () => { |
| 238 | + const out = Telemetry.deriveAgentOutcomeReason({ |
| 239 | + outcome: "error", |
| 240 | + lastToolName: "edit", |
| 241 | + lastMessageError: "APIError: Service unavailable (503)", |
| 242 | + abortReason: null, |
| 243 | + lastErrorClass: "", |
| 244 | + }) |
| 245 | + expect(out.error_class).toBe("unknown") // flip when pattern added |
| 246 | + expect(out.reason).toContain("Service unavailable") |
| 247 | + }) |
| 248 | + |
| 249 | + test("[KNOWN GAP] rate-limit error classifies as 'unknown' (pattern coverage hole)", () => { |
| 250 | + const out = Telemetry.deriveAgentOutcomeReason({ |
| 251 | + outcome: "error", |
| 252 | + lastToolName: null, |
| 253 | + lastMessageError: "Rate limit exceeded. Retry after 60s", |
| 254 | + abortReason: null, |
| 255 | + lastErrorClass: "", |
| 256 | + }) |
| 257 | + expect(out.error_class).toBe("unknown") // flip when pattern added |
| 258 | + }) |
| 259 | +}) |
0 commit comments