|
| 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 { assembleCounterfactualFixtures } from "../../packages/loopover-engine/src/calibration/counterfactual-fixtures"; |
| 8 | +import { COUNTERFACTUAL_SAMPLE_SEED_PREFIX } from "../../packages/loopover-engine/src/calibration/counterfactual-contract"; |
| 9 | +import type { BacktestCase } from "../../packages/loopover-engine/src/calibration/backtest-corpus"; |
| 10 | + |
| 11 | +const SEED = `${COUNTERFACTUAL_SAMPLE_SEED_PREFIX}:test-campaign`; |
| 12 | + |
| 13 | +function replayable(targetKey: string, label: BacktestCase["label"] = "confirmed", extraMetadata: Record<string, unknown> = {}): 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 | + metadata: { diff: `@@ diff for ${targetKey}`, ...extraMetadata }, |
| 22 | + }; |
| 23 | +} |
| 24 | + |
| 25 | +describe("assembleCounterfactualFixtures (#8220)", () => { |
| 26 | + it("shapes replayable cases into fixtures with era provenance, skipping context-less cases with accounting", () => { |
| 27 | + const cases: BacktestCase[] = [ |
| 28 | + replayable("acme/widgets#1", "reversed"), |
| 29 | + { ...replayable("acme/widgets#2"), metadata: { confidence: 0.9 } }, // no diff — not replayable |
| 30 | + { ...replayable("acme/widgets#3"), metadata: { diff: "" } }, // empty diff — not replayable |
| 31 | + replayable("acme/widgets#4", "confirmed", { rawContextProvenance: "github_raw_context_refetch" }), |
| 32 | + ]; |
| 33 | + const { fixtures, skipped } = assembleCounterfactualFixtures(cases, { seed: SEED, maxFixtures: 10 }); |
| 34 | + expect(fixtures).toEqual([ |
| 35 | + { |
| 36 | + fixtureId: "acme/widgets#1", |
| 37 | + label: "reversed", |
| 38 | + boundedInputs: { diff: "@@ diff for acme/widgets#1" }, |
| 39 | + provenance: "live_capture", |
| 40 | + }, |
| 41 | + { |
| 42 | + fixtureId: "acme/widgets#4", |
| 43 | + label: "confirmed", |
| 44 | + boundedInputs: { diff: "@@ diff for acme/widgets#4" }, |
| 45 | + provenance: "raw_context_refetch", |
| 46 | + }, |
| 47 | + ]); |
| 48 | + expect(skipped).toEqual({ no_raw_context: 2, sampled_out: 0 }); |
| 49 | + // Sum invariant: every input case is a fixture or an accounted skip. |
| 50 | + expect(fixtures.length + skipped.no_raw_context + skipped.sampled_out).toBe(cases.length); |
| 51 | + }); |
| 52 | + |
| 53 | + it("applies the seeded sample only when the eligible set exceeds the budget, preserving corpus order", () => { |
| 54 | + const cases = Array.from({ length: 20 }, (_, i) => replayable(`acme/widgets#${i + 1}`)); |
| 55 | + const under = assembleCounterfactualFixtures(cases, { seed: SEED, maxFixtures: 20 }); |
| 56 | + expect(under.fixtures).toHaveLength(20); |
| 57 | + expect(under.skipped.sampled_out).toBe(0); |
| 58 | + |
| 59 | + const sampled = assembleCounterfactualFixtures(cases, { seed: SEED, maxFixtures: 7 }); |
| 60 | + expect(sampled.fixtures).toHaveLength(7); |
| 61 | + expect(sampled.skipped.sampled_out).toBe(13); |
| 62 | + // Corpus order is preserved within the sample — fixture ids ascend by original position. |
| 63 | + const positions = sampled.fixtures.map((fixture) => cases.findIndex((c) => c.targetKey === fixture.fixtureId)); |
| 64 | + expect(positions).toEqual([...positions].sort((a, b) => a - b)); |
| 65 | + }); |
| 66 | + |
| 67 | + it("is deterministic per seed, differs across seeds, and never selects 'the first N'", () => { |
| 68 | + const cases = Array.from({ length: 30 }, (_, i) => replayable(`acme/widgets#${i + 1}`)); |
| 69 | + const first = assembleCounterfactualFixtures(cases, { seed: SEED, maxFixtures: 10 }); |
| 70 | + expect(assembleCounterfactualFixtures(cases, { seed: SEED, maxFixtures: 10 })).toEqual(first); |
| 71 | + |
| 72 | + const otherSeed = assembleCounterfactualFixtures(cases, { seed: `${SEED}-b`, maxFixtures: 10 }); |
| 73 | + expect(otherSeed.fixtures.map((f) => f.fixtureId)).not.toEqual(first.fixtures.map((f) => f.fixtureId)); |
| 74 | + // Hash-ranked membership, not positional truncation. |
| 75 | + expect(first.fixtures.map((f) => f.fixtureId)).not.toEqual(cases.slice(0, 10).map((c) => c.targetKey)); |
| 76 | + }); |
| 77 | + |
| 78 | + it("breaks a same-target rank tie toward the earlier case, deterministically", () => { |
| 79 | + // Two firings of the SAME target share a sample hash — a two-element sort MUST compare exactly that |
| 80 | + // tied pair, forcing the position tie-break: the earlier firing wins the single slot. |
| 81 | + const cases = [replayable("acme/widgets#7", "confirmed"), replayable("acme/widgets#7", "reversed")]; |
| 82 | + const { fixtures, skipped } = assembleCounterfactualFixtures(cases, { seed: SEED, maxFixtures: 1 }); |
| 83 | + expect(fixtures).toHaveLength(1); |
| 84 | + expect(fixtures[0]).toMatchObject({ fixtureId: "acme/widgets#7", label: "confirmed" }); |
| 85 | + expect(skipped.sampled_out).toBe(1); |
| 86 | + // Reproducible byte-for-byte. |
| 87 | + expect(assembleCounterfactualFixtures(cases, { seed: SEED, maxFixtures: 1 })).toEqual({ fixtures, skipped }); |
| 88 | + }); |
| 89 | + |
| 90 | + it("returns an empty assembly for an empty corpus", () => { |
| 91 | + expect(assembleCounterfactualFixtures([], { seed: SEED, maxFixtures: 5 })).toEqual({ |
| 92 | + fixtures: [], |
| 93 | + skipped: { no_raw_context: 0, sampled_out: 0 }, |
| 94 | + }); |
| 95 | + }); |
| 96 | +}); |
0 commit comments