diff --git a/pm-stack/aggregator/src/aa.ts b/pm-stack/aggregator/src/aa.ts index 8f257a2160..2be728bece 100644 --- a/pm-stack/aggregator/src/aa.ts +++ b/pm-stack/aggregator/src/aa.ts @@ -410,15 +410,36 @@ interface MetricRow { /** Non-dominated keys on one axis pair (both axes "lower wins" after * the caller's sign flips). Rows missing either axis never qualify. */ function paretoKeys(rows: MetricRow[], pick: (r: MetricRow) => [number, number] | null): Set { - const pts = rows + const picked = rows .map((r) => ({ key: r.key, xy: pick(r) })) .filter((p): p is { key: string; xy: [number, number] } => p.xy !== null); const out = new Set(); - for (const p of pts) { - const dominated = pts.some( - (q) => q !== p && q.xy[0] <= p.xy[0] && q.xy[1] <= p.xy[1] && (q.xy[0] < p.xy[0] || q.xy[1] < p.xy[1]), - ); - if (!dominated) out.add(p.key); + // A NaN coordinate is incomparable under the old <= dominance rule. + const pts = picked.filter((p) => { + if (Number.isNaN(p.xy[0]) || Number.isNaN(p.xy[1])) { + out.add(p.key); + return false; + } + return true; + }); + // Sort once, then sweep equal-x groups: only the lowest y in a group can + // survive, unless a strictly lower-x group already has an equal/better y. + // This replaces the previous all-pairs dominance scan with O(n log n). + const compare = (a: number, b: number) => (a < b ? -1 : a > b ? 1 : 0); + pts.sort((a, b) => compare(a.xy[0], b.xy[0]) || compare(a.xy[1], b.xy[1])); + let bestY = Number.POSITIVE_INFINITY; + let hasPreviousX = false; + for (let start = 0; start < pts.length;) { + let end = start + 1; + const x = pts[start].xy[0]; + while (end < pts.length && pts[end].xy[0] === x) end++; + const groupMinY = pts[start].xy[1]; + if (!hasPreviousX || bestY > groupMinY) { + for (let i = start; i < end && pts[i].xy[1] === groupMinY; i++) out.add(pts[i].key); + } + bestY = Math.min(bestY, groupMinY); + hasPreviousX = true; + start = end; } return out; } diff --git a/pm-stack/aggregator/test/aa.test.ts b/pm-stack/aggregator/test/aa.test.ts index 37ecb9a0f2..e402598607 100644 --- a/pm-stack/aggregator/test/aa.test.ts +++ b/pm-stack/aggregator/test/aa.test.ts @@ -339,7 +339,9 @@ describe("explorer points (charts + perspectives)", () => { // cost frontier, off the time frontier, beaten by nobody on all // three axes at once (A costs more). The reference design's exact // semantics, in data form. - function sectionWith(rows: { family: string; score: number; cost: number; time: number }[]): AaSection { + type FixtureRow = { family: string; score: number | null; cost: number | null; time: number | null }; + + function sectionWith(rows: FixtureRow[]): AaSection { const rankings: AaRanking[] = rows.map((r, i) => ({ rowIndex: i, testRunKey: `k${i}`, @@ -434,6 +436,103 @@ describe("explorer points (charts + perspectives)", () => { expect(ghost.frontierCostTime).toBe(false); expect(ghost.beatenBy).toEqual([]); }); + + test("Pareto sweep preserves equal-coordinate duplicates and same-axis domination", () => { + const pts = aaPoints( + sectionWith([ + { family: "higher-y", score: 50, cost: 1, time: 5 }, + { family: "same-x-min", score: 50, cost: 1, time: 3 }, + { family: "duplicate", score: 50, cost: 1, time: 3 }, + { family: "same-y-later-x", score: 50, cost: 2, time: 3 }, + { family: "dominated", score: 50, cost: 2, time: 4 }, + { family: "lower-y", score: 50, cost: 3, time: 2 }, + ]), + ); + expect(pts.filter((p) => p.frontierCostTime).map((p) => p.family)).toEqual([ + "same-x-min", + "duplicate", + "lower-y", + ]); + }); + + test("AA and CAI Pareto flags match the all-pairs dominance oracle under generated inputs", () => { + // Independent model: a point is dominated iff another point is no + // worse on either axis and strictly better on at least one axis. + type Pair = readonly [number | null, number | null]; + const nonDominated = (pairs: Pair[], i: number): boolean => { + const [x, y] = pairs[i]; + if (x === null || y === null) return false; + return !pairs.some( + ([a, b]) => a !== null && b !== null && a <= x && b <= y && (a < x || b < y), + ); + }; + + let state = 0x50415245; // fixed seed; a failure names its generated case + const draw = (limit: number): number => { + state = (Math.imul(state, 1_664_525) + 1_013_904_223) >>> 0; + return Math.floor((state / 2 ** 32) * limit); + }; + // A small domain makes ties common. Non-finite numbers exercise the + // pure projection APIs directly (feed decoding normalizes them to null). + const values = [null, -0, 0, 1, 2, 3, 5, 8, NaN, -Infinity, Infinity]; + const value = () => values[draw(values.length)]; + const negated = (n: number | null) => (n === null ? null : -n); + + for (let sample = 0; sample < 2_048; sample++) { + const rows: FixtureRow[] = Array.from({ length: draw(24) }, (_, i) => ({ + family: `row-${i}`, + score: value(), + cost: value(), + time: value(), + })); + for (let i = rows.length - 1; i > 0; i--) { + const j = draw(i + 1); + [rows[i], rows[j]] = [rows[j], rows[i]]; + } + + const costScore = rows.map((r): Pair => [r.cost, negated(r.score)]); + const timeScore = rows.map((r): Pair => [r.time, negated(r.score)]); + const costTime = rows.map((r): Pair => [r.cost, r.time]); + const expected = rows.map((r, i) => ({ + family: r.family, + frontierCost: nonDominated(costScore, i), + frontierTime: nonDominated(timeScore, i), + frontierCostTime: nonDominated(costTime, i), + })); + + const aa = sectionWith(rows); + // Exercise AA's joined cost/time fallback as well as direct metrics. + for (let i = 0; i < rows.length; i++) { + if (i % 2 === 0) aa.rankings[i].costTask = null; + if (i % 3 === 0) aa.rankings[i].timeTaskSec = null; + } + + const cai = rows.map((r): AaCaiRow => ({ + modelName: r.family, + family: r.family, + creator: "Test", + effort: null, + version: "v1.5", + score: r.score, + cost: r.cost, + wallSec: r.time, + tokensPerTask: null, + harness: null, + deepSwe: null, + terminalBench: null, + sweAtlas: null, + snapshotDate: null, + sourceUrl: null, + })); + + for (const [source, points] of [["AA", aaPoints(aa)], ["CAI", aaCaiPoints(cai)]] as const) { + const flags = points.map(({ family, frontierCost, frontierTime, frontierCostTime }) => ({ + family, frontierCost, frontierTime, frontierCostTime, + })); + expect(flags, `${source} generated case ${sample}`).toEqual(expected); + } + } + }); }); describe("fleet catalog (Model Explorer)", () => {