|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | + |
| 3 | +// Import the engine SOURCE directly (not the built dist) -- coverage.include lists |
| 4 | +// packages/loopover-engine/src/**, so only a source-path import exercises the .ts these branches live in |
| 5 | +// (the dist-importing twin in packages/loopover-engine/test/ covers the built barrel for the workspace |
| 6 | +// suite). Same pattern as backtest-corpus-engine.test.ts / repo-corpus-engine.test.ts. |
| 7 | +import { |
| 8 | + computeProviderTrackRecords, |
| 9 | + type ProviderReviewSignal, |
| 10 | +} from "../../packages/loopover-engine/src/calibration/provider-track-record"; |
| 11 | +import type { BacktestCase } from "../../packages/loopover-engine/src/calibration/backtest-corpus"; |
| 12 | + |
| 13 | +function labeled(targetKey: string, label: BacktestCase["label"]): BacktestCase { |
| 14 | + return { |
| 15 | + ruleId: "ai_consensus_defect", |
| 16 | + targetKey, |
| 17 | + outcome: "close", |
| 18 | + label, |
| 19 | + firedAt: "2026-07-01T00:00:00.000Z", |
| 20 | + decidedAt: "2026-07-02T00:00:00.000Z", |
| 21 | + }; |
| 22 | +} |
| 23 | + |
| 24 | +function signal(provider: string, targetKey: string, vote: ProviderReviewSignal["vote"], repoFullName = "acme/widgets"): ProviderReviewSignal { |
| 25 | + return { provider, repoFullName, targetKey, vote }; |
| 26 | +} |
| 27 | + |
| 28 | +describe("computeProviderTrackRecords (#8228)", () => { |
| 29 | + it("computes precision, agreement, and consensus/split rates for a both-provider corpus, per repo and overall", () => { |
| 30 | + const cases = [ |
| 31 | + labeled("acme/widgets#1", "confirmed"), |
| 32 | + labeled("acme/widgets#2", "reversed"), |
| 33 | + labeled("acme/widgets#3", "confirmed"), |
| 34 | + ]; |
| 35 | + const signals = [ |
| 36 | + // #1: both fail on a confirmed firing — consensus, both correct. |
| 37 | + signal("provider-a", "acme/widgets#1", "fail"), |
| 38 | + signal("provider-b", "acme/widgets#1", "fail"), |
| 39 | + // #2: split — a fails (wrong: label reversed), b passes (right). |
| 40 | + signal("provider-a", "acme/widgets#2", "fail"), |
| 41 | + signal("provider-b", "acme/widgets#2", "pass"), |
| 42 | + // #3: only a reviews it, warns (non-fail on a confirmed firing — disagreed with the human). |
| 43 | + signal("provider-a", "acme/widgets#3", "warn"), |
| 44 | + ]; |
| 45 | + const records = computeProviderTrackRecords(signals, cases); |
| 46 | + |
| 47 | + const aOverall = records.find((r) => r.provider === "provider-a" && r.repoFullName === null)!; |
| 48 | + expect(aOverall).toMatchObject({ |
| 49 | + signals: 3, |
| 50 | + decided: 3, |
| 51 | + confirmed: 2, |
| 52 | + reversed: 1, |
| 53 | + precision: 0.5, // of a's 2 fail votes, 1 hit a confirmed firing |
| 54 | + agreementRate: 1 / 3, // matched the human only on #1 |
| 55 | + consensusRate: 0.5, // shared #1 (agreed) and #2 (split) |
| 56 | + splitRate: 0.5, |
| 57 | + }); |
| 58 | + const bOverall = records.find((r) => r.provider === "provider-b" && r.repoFullName === null)!; |
| 59 | + expect(bOverall).toMatchObject({ signals: 2, decided: 2, precision: 1, agreementRate: 1, consensusRate: 0.5, splitRate: 0.5 }); |
| 60 | + |
| 61 | + // Single-repo corpus: each provider's per-repo row equals its overall rollup. |
| 62 | + const aRepo = records.find((r) => r.provider === "provider-a" && r.repoFullName === "acme/widgets")!; |
| 63 | + expect(aRepo).toMatchObject({ signals: aOverall.signals, decided: aOverall.decided, precision: aOverall.precision }); |
| 64 | + }); |
| 65 | + |
| 66 | + it("keeps a one-provider corpus's consensus/split rates null — no shared targets, no consensus to measure", () => { |
| 67 | + const records = computeProviderTrackRecords( |
| 68 | + [signal("solo", "acme/widgets#1", "fail"), signal("solo", "acme/widgets#2", "pass")], |
| 69 | + [labeled("acme/widgets#1", "confirmed"), labeled("acme/widgets#2", "reversed")], |
| 70 | + ); |
| 71 | + const overall = records.find((r) => r.repoFullName === null)!; |
| 72 | + expect(overall.consensusRate).toBeNull(); |
| 73 | + expect(overall.splitRate).toBeNull(); |
| 74 | + expect(overall.precision).toBe(1); |
| 75 | + expect(overall.agreementRate).toBe(1); |
| 76 | + }); |
| 77 | + |
| 78 | + it("reports null (never 0) precision below the sample floor: undecided targets and providers that never voted fail", () => { |
| 79 | + const records = computeProviderTrackRecords( |
| 80 | + [ |
| 81 | + signal("quiet", "acme/widgets#9", "pass"), // undecided target — no label exists |
| 82 | + signal("quiet", "acme/widgets#1", "warn"), // decided, but never a fail vote |
| 83 | + ], |
| 84 | + [labeled("acme/widgets#1", "reversed")], |
| 85 | + ); |
| 86 | + const overall = records.find((r) => r.repoFullName === null)!; |
| 87 | + expect(overall).toMatchObject({ signals: 2, decided: 1, precision: null, agreementRate: 1 }); |
| 88 | + }); |
| 89 | + |
| 90 | + it("rolls per-repo rows up into the overall row exactly, with deterministic provider→overall→repo ordering", () => { |
| 91 | + const cases = [labeled("acme/widgets#1", "confirmed"), labeled("acme/gadgets#2", "confirmed")]; |
| 92 | + const signals = [ |
| 93 | + signal("zeta", "acme/widgets#1", "fail", "acme/widgets"), |
| 94 | + signal("zeta", "acme/gadgets#2", "fail", "acme/gadgets"), |
| 95 | + signal("alpha", "acme/widgets#1", "fail", "acme/widgets"), |
| 96 | + ]; |
| 97 | + const records = computeProviderTrackRecords(signals, cases); |
| 98 | + expect(records.map((r) => [r.provider, r.repoFullName])).toEqual([ |
| 99 | + ["alpha", null], |
| 100 | + ["alpha", "acme/widgets"], |
| 101 | + ["zeta", null], |
| 102 | + ["zeta", "acme/gadgets"], |
| 103 | + ["zeta", "acme/widgets"], |
| 104 | + ]); |
| 105 | + const zetaOverall = records.find((r) => r.provider === "zeta" && r.repoFullName === null)!; |
| 106 | + const zetaRepos = records.filter((r) => r.provider === "zeta" && r.repoFullName !== null); |
| 107 | + expect(zetaRepos.reduce((sum, r) => sum + r.decided, 0)).toBe(zetaOverall.decided); |
| 108 | + expect(zetaRepos.reduce((sum, r) => sum + r.signals, 0)).toBe(zetaOverall.signals); |
| 109 | + // Determinism: identical inputs yield the identical result. |
| 110 | + expect(computeProviderTrackRecords(signals, cases)).toEqual(records); |
| 111 | + }); |
| 112 | + |
| 113 | + it("never leaks target keys into any returned shape — provider ids, repo names, and numbers only", () => { |
| 114 | + const records = computeProviderTrackRecords( |
| 115 | + [signal("provider-a", "acme/widgets#42", "fail")], |
| 116 | + [labeled("acme/widgets#42", "confirmed")], |
| 117 | + ); |
| 118 | + expect(JSON.stringify(records)).not.toContain("#42"); |
| 119 | + }); |
| 120 | + |
| 121 | + it("reports null agreement (never 0) for a provider whose every signal is undecided", () => { |
| 122 | + const records = computeProviderTrackRecords([signal("unjoined", "acme/widgets#404", "fail")], []); |
| 123 | + const overall = records.find((r) => r.repoFullName === null)!; |
| 124 | + expect(overall).toMatchObject({ signals: 1, decided: 0, precision: null, agreementRate: null }); |
| 125 | + }); |
| 126 | + |
| 127 | + it("returns an empty list for empty inputs", () => { |
| 128 | + expect(computeProviderTrackRecords([], [])).toEqual([]); |
| 129 | + }); |
| 130 | +}); |
0 commit comments