import {
  log
} from "./chunk-2ODBA7MQ.js";

// src/extraction-judge-telemetry.ts
import path from "path";
import { appendFile, mkdir, readFile } from "fs/promises";
var EXTRACTION_JUDGE_VERDICT_CATEGORY = "EXTRACTION_JUDGE_VERDICT";
function judgeTelemetryPath(memoryDir) {
  return path.join(
    memoryDir,
    "state",
    "observation-ledger",
    "extraction-judge-verdicts.jsonl"
  );
}
async function recordJudgeVerdict(event, options) {
  if (!options.enabled) return;
  const filePath = judgeTelemetryPath(options.memoryDir);
  try {
    await mkdir(path.dirname(filePath), { recursive: true });
    await appendFile(filePath, `${JSON.stringify(event)}
`, "utf-8");
  } catch (err) {
    log.debug(
      `extraction-judge-telemetry: append failed (non-fatal): ${err instanceof Error ? err.message : String(err)}`
    );
  }
}
async function readJudgeVerdictStats(memoryDir, opts = {}) {
  const filePath = judgeTelemetryPath(memoryDir);
  let raw;
  try {
    raw = await readFile(filePath, "utf-8");
  } catch (err) {
    const code = err.code;
    if (code === "ENOENT") {
      return {
        total: 0,
        accept: 0,
        reject: 0,
        defer: 0,
        deferCapTriggered: 0,
        meanElapsedMs: 0,
        deferRate: 0,
        malformed: 0
      };
    }
    throw err;
  }
  let total = 0;
  let accept = 0;
  let reject = 0;
  let defer = 0;
  let deferCapTriggered = 0;
  let elapsedSum = 0;
  let malformed = 0;
  let firstTs;
  let lastTs;
  for (const line of raw.split("\n")) {
    if (!line.trim()) continue;
    let parsed;
    try {
      parsed = JSON.parse(line);
    } catch {
      malformed += 1;
      continue;
    }
    if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) {
      malformed += 1;
      continue;
    }
    const p = parsed;
    if (p.category !== EXTRACTION_JUDGE_VERDICT_CATEGORY) {
      malformed += 1;
      continue;
    }
    const ts = typeof p.ts === "string" ? p.ts : null;
    if (ts === null) {
      malformed += 1;
      continue;
    }
    const tsMs = Date.parse(ts);
    if (!Number.isFinite(tsMs)) {
      malformed += 1;
      continue;
    }
    if (typeof opts.sinceMs === "number" && tsMs < opts.sinceMs) continue;
    if (typeof opts.untilMs === "number" && tsMs >= opts.untilMs) continue;
    const kind = p.verdictKind;
    if (kind === "accept") accept += 1;
    else if (kind === "reject") reject += 1;
    else if (kind === "defer") defer += 1;
    else {
      malformed += 1;
      continue;
    }
    if (p.deferCapTriggered === true) deferCapTriggered += 1;
    if (typeof p.elapsedMs === "number" && Number.isFinite(p.elapsedMs)) {
      elapsedSum += p.elapsedMs;
    }
    total += 1;
    if (firstTs === void 0 || ts < firstTs) firstTs = ts;
    if (lastTs === void 0 || ts > lastTs) lastTs = ts;
  }
  return {
    total,
    accept,
    reject,
    defer,
    deferCapTriggered,
    meanElapsedMs: total > 0 ? elapsedSum / total : 0,
    deferRate: total > 0 ? defer / total : 0,
    firstTs,
    lastTs,
    malformed
  };
}

export {
  EXTRACTION_JUDGE_VERDICT_CATEGORY,
  judgeTelemetryPath,
  recordJudgeVerdict,
  readJudgeVerdictStats
};
//# sourceMappingURL=chunk-AJU4PJGY.js.map