import {
  applyMemoryWorthFilter
} from "./chunk-3GPTTA4J.js";
import "./chunk-IISBCCWR.js";
import "./chunk-PZ5AY32C.js";

// src/memory-worth-bench.ts
function mulberry32(seed) {
  let t = seed >>> 0;
  return () => {
    t = t + 1831565813 >>> 0;
    let r = t;
    r = Math.imul(r ^ r >>> 15, r | 1);
    r ^= r + Math.imul(r ^ r >>> 7, r | 61);
    return ((r ^ r >>> 14) >>> 0) / 4294967296;
  };
}
function buildCase(caseIndex, rng) {
  const candidates = [];
  for (let i = 0; i < 2; i += 1) {
    candidates.push({
      path: `case-${caseIndex}-trap-${i}.md`,
      baseScore: 0.95 - i * 0.02,
      isRelevant: false,
      counters: { mw_success: 0, mw_fail: 10 }
    });
  }
  for (let i = 0; i < 3; i += 1) {
    candidates.push({
      path: `case-${caseIndex}-noise-high-${i}.md`,
      baseScore: 0.9 - i * 0.02,
      isRelevant: false
    });
  }
  for (let i = 0; i < 3; i += 1) {
    candidates.push({
      path: `case-${caseIndex}-gold-${i}.md`,
      baseScore: 0.7 - i * 0.05,
      isRelevant: true,
      counters: { mw_success: 10, mw_fail: 0 }
    });
  }
  for (let i = 0; i < 12; i += 1) {
    candidates.push({
      path: `case-${caseIndex}-noise-low-${i}.md`,
      baseScore: rng() * 0.5,
      isRelevant: false
    });
  }
  for (let i = candidates.length - 1; i > 0; i -= 1) {
    const j = Math.floor(rng() * (i + 1));
    [candidates[i], candidates[j]] = [candidates[j], candidates[i]];
  }
  return { id: `case-${caseIndex}`, candidates, k: 5 };
}
function computePrecisionAtK(orderedPaths, relevant, k) {
  const topK = orderedPaths.slice(0, k);
  if (topK.length === 0) return 0;
  let hits = 0;
  for (const p of topK) if (relevant.has(p)) hits += 1;
  return hits / Math.min(k, topK.length);
}
function runMemoryWorthBench(options) {
  const requestedCases = options?.cases ?? 50;
  if (!Number.isFinite(requestedCases) || !Number.isInteger(requestedCases) || requestedCases < 1) {
    throw new Error(
      `runMemoryWorthBench: cases must be a positive integer; got ${requestedCases}`
    );
  }
  const numCases = requestedCases;
  const rng = mulberry32(options?.seed ?? 3735928559);
  const now = options?.now ?? /* @__PURE__ */ new Date("2026-01-01T00:00:00.000Z");
  let sumOff = 0;
  let sumOn = 0;
  let onWinsOrTies = 0;
  for (let i = 0; i < numCases; i += 1) {
    const c = buildCase(i, rng);
    const relevant = new Set(
      c.candidates.filter((x) => x.isRelevant).map((x) => x.path)
    );
    const off = [...c.candidates].sort((a, b) => b.baseScore - a.baseScore).map((x) => x.path);
    const pOff = computePrecisionAtK(off, relevant, c.k);
    const counters = /* @__PURE__ */ new Map();
    for (const cand of c.candidates) {
      if (cand.counters) counters.set(cand.path, cand.counters);
    }
    const filtered = applyMemoryWorthFilter(
      c.candidates.map((x) => ({ path: x.path, score: x.baseScore })),
      { counters, now }
    );
    const on = filtered.map((x) => x.path);
    const pOn = computePrecisionAtK(on, relevant, c.k);
    sumOff += pOff;
    sumOn += pOn;
    if (pOn >= pOff) onWinsOrTies += 1;
  }
  const avgOff = sumOff / numCases;
  const avgOn = sumOn / numCases;
  return {
    cases: numCases,
    k: 5,
    precisionAtK_off: avgOff,
    precisionAtK_on: avgOn,
    delta: avgOn - avgOff,
    filterWinsOrTies: onWinsOrTies === numCases
  };
}
function runMemoryWorthBenchCli() {
  const result = runMemoryWorthBench();
  console.log(JSON.stringify(result, null, 2));
  if (!result.filterWinsOrTies) {
    console.error("memory-worth bench: filter lost to no-filter baseline on at least one case");
    process.exit(1);
  }
}
if (process.argv[1]) {
  try {
    const { pathToFileURL } = await import("url");
    if (import.meta.url === pathToFileURL(process.argv[1]).href) {
      runMemoryWorthBenchCli();
    }
  } catch {
    if (import.meta.url === `file://${process.argv[1]}`) {
      runMemoryWorthBenchCli();
    }
  }
}
export {
  runMemoryWorthBench,
  runMemoryWorthBenchCli
};
//# sourceMappingURL=memory-worth-bench.js.map