import {
  fromClaudeCodeSession,
  fromCodexSession,
  fromKimiCodeSession,
  fromOpenCodeSession,
  fromPiSession
} from "../chunk-S42AWHMP.js";
import {
  calibrationFromPairs
} from "../chunk-NPCTHQIO.js";
import {
  projectRuntimeTrajectoryEvidence
} from "../chunk-T4SQEITX.js";
import "../chunk-VI2UW6B6.js";
import {
  offPolicyEstimateAll
} from "../chunk-4DIJWVUT.js";
import {
  confidenceInterval
} from "../chunk-HRGUJTER.js";
import {
  ValidationError
} from "../chunk-3BFEG2F6.js";
import "../chunk-PZ5AY32C.js";

// src/belief-state/calibration.ts
function calibrateBeliefDecisions(points, options = {}) {
  const filtered = filterCalibrationRegion(points, options);
  const pairs = filtered.filter((point) => typeof point.confidence === "number" && point.outcome).map((point) => ({
    evalScore: point.confidence,
    outcome: outcomeScore(point)
  })).filter((pair) => Number.isFinite(pair.outcome));
  const minPairs = options.minPairs ?? 10;
  if (pairs.length < minPairs) return null;
  return calibrationFromPairs(pairs, "belief-confidence", "decision-outcome", {
    bins: options.bins ?? 5,
    range: { lo: 0, hi: 1 }
  });
}
function filterCalibrationRegion(points, options) {
  const region = options.region ?? "all";
  if (region === "all") return points;
  const policy = options.policy;
  if (!policy) {
    throw new ValidationError(
      `calibrateBeliefDecisions: policy is required when region is "${region}"`
    );
  }
  return points.filter((point) => {
    const accepted = policy.decide(point).action === "accept";
    return region === "accepted" ? accepted : !accepted;
  });
}
function outcomeScore(point) {
  if (typeof point.outcome?.reward === "number") return point.outcome.reward;
  if (typeof point.outcome?.score === "number") return point.outcome.score;
  if (point.outcome?.success === true) return 1;
  if (point.outcome?.success === false) return 0;
  return Number.NaN;
}

// src/belief-state/ope.ts
function embeddedBeliefOpeTargetPolicy(id = "embedded-target-prob") {
  return {
    id,
    targetProbOf(point) {
      return point.targetProb;
    },
    qHatOf(point) {
      return point.qHat;
    }
  };
}
function beliefDecisionsToOffPolicyTrajectories(points, targetPolicy, options = {}) {
  const trajectories = [];
  const diagnostics = [];
  for (const point of points) {
    if (!point.outcome) {
      diagnostics.push(`${point.id}: missing outcome`);
      continue;
    }
    if (!isBehaviorProbability(point.behaviorProb)) {
      diagnostics.push(`${point.id}: invalid behaviorProb ${formatProbability(point.behaviorProb)}`);
      continue;
    }
    let targetProb;
    let qHat;
    try {
      targetProb = targetPolicy.targetProbOf(point);
      qHat = targetPolicy.qHatOf?.(point);
    } catch (error) {
      diagnostics.push(
        `${point.id}: target policy ${targetPolicy.id} threw (${errorMessage(error)})`
      );
      continue;
    }
    if (!isTargetProbability(targetProb)) {
      diagnostics.push(`${point.id}: invalid targetProb ${formatProbability(targetProb)}`);
      continue;
    }
    if (qHat !== null && qHat !== void 0 && !isTargetProbability(qHat)) {
      diagnostics.push(`${point.id}: invalid qHat ${formatProbability(qHat)}; ignoring qHat`);
      qHat = null;
    }
    trajectories.push({
      runId: point.id,
      reward: rewardOf(point),
      behaviorProb: point.behaviorProb,
      targetProb,
      qHat
    });
  }
  return {
    targetPolicyId: targetPolicy.id,
    trajectories,
    dropped: points.length - trajectories.length,
    diagnostics: compactDiagnostics(diagnostics, options.maxDiagnostics ?? 20)
  };
}
function evaluateBeliefOffPolicy(points, targetPolicy, options = {}) {
  const trajectoryReport = beliefDecisionsToOffPolicyTrajectories(points, targetPolicy, options);
  const { trajectories } = trajectoryReport;
  const estimates = offPolicyEstimateAll(trajectories, options);
  const support = supportDiagnostics(estimates.dr, {
    minEffectiveSampleSize: options.minEffectiveSampleSize ?? 30,
    minEffectiveSampleRatio: options.minEffectiveSampleRatio ?? 0.25,
    dropped: trajectoryReport.dropped,
    diagnostics: trajectoryReport.diagnostics
  });
  return { targetPolicyId: targetPolicy.id, ...estimates, support };
}
function supportDiagnostics(estimate, options) {
  const ratio2 = estimate.n > 0 ? estimate.effectiveSampleSize / estimate.n : 0;
  const reasons = [...options.diagnostics];
  if (estimate.n === 0) {
    reasons.push("no valid OPE trajectories");
  }
  if (options.dropped > 0) {
    reasons.push(`dropped ${options.dropped} unsupported decision(s)`);
  }
  if (estimate.effectiveSampleSize < options.minEffectiveSampleSize) {
    reasons.push(
      `effective sample size ${estimate.effectiveSampleSize.toFixed(2)} below ${options.minEffectiveSampleSize}`
    );
  }
  if (ratio2 < options.minEffectiveSampleRatio) {
    reasons.push(
      `effective sample ratio ${ratio2.toFixed(2)} below ${options.minEffectiveSampleRatio}`
    );
  }
  if (estimate.maxImportanceWeight > 10) {
    reasons.push(`max importance weight ${estimate.maxImportanceWeight.toFixed(2)} is high`);
  }
  return {
    supported: reasons.length === 0,
    n: estimate.n,
    dropped: options.dropped,
    effectiveSampleSize: estimate.effectiveSampleSize,
    effectiveSampleRatio: ratio2,
    maxImportanceWeight: estimate.maxImportanceWeight,
    reasons
  };
}
function rewardOf(point) {
  if (typeof point.outcome?.reward === "number") return point.outcome.reward;
  if (typeof point.outcome?.score === "number") return point.outcome.score;
  if (point.outcome?.success === true) return 1;
  return 0;
}
function isBehaviorProbability(value) {
  return typeof value === "number" && Number.isFinite(value) && value > 0 && value <= 1;
}
function isTargetProbability(value) {
  return typeof value === "number" && Number.isFinite(value) && value >= 0 && value <= 1;
}
function formatProbability(value) {
  return typeof value === "number" ? String(value) : String(value ?? "missing");
}
function errorMessage(error) {
  return error instanceof Error ? error.message : String(error);
}
function compactDiagnostics(diagnostics, maxDiagnostics) {
  if (diagnostics.length <= maxDiagnostics) return diagnostics;
  return [
    ...diagnostics.slice(0, maxDiagnostics),
    `${diagnostics.length - maxDiagnostics} additional OPE diagnostic(s) omitted`
  ];
}

// src/belief-state/selective.ts
var DEFAULT_UTILITY = {
  successUtility: 1,
  failureUtility: -1,
  deferUtility: 0,
  verifyCost: 0.05,
  askCost: 0.05,
  retryCost: 0.1,
  stopUtility: 0,
  costWeight: 1
};
function thresholdSelectivePolicy(options) {
  const threshold = options.confidenceThreshold;
  if (!Number.isFinite(threshold) || threshold < 0 || threshold > 1) {
    throw new ValidationError(
      `thresholdSelectivePolicy: confidenceThreshold must be in [0, 1], got ${threshold}`
    );
  }
  const belowThresholdAction = options.belowThresholdAction ?? "verify";
  return {
    id: options.id ?? `confidence>=${threshold}`,
    decide(point) {
      const confidence = point.confidence ?? 0;
      return {
        action: confidence >= threshold ? "accept" : belowThresholdAction,
        confidence,
        targetProb: point.targetProb,
        qHat: point.qHat,
        reason: confidence >= threshold ? "confidence threshold passed" : "confidence threshold failed"
      };
    }
  };
}
function evaluateBeliefSelectivePolicy(points, policy, options = {}) {
  const utility = { ...DEFAULT_UTILITY, ...options.utility ?? {} };
  const scored = points.filter((point) => point.outcome);
  const minN = options.minN ?? 30;
  const minAccepted = options.minAccepted ?? 5;
  const minUtilityDelta = options.minUtilityDelta ?? 0;
  const deltas = [];
  const acceptedRewards = [];
  const rejectedRewards = [];
  let baselineUtility = 0;
  let policyUtility = 0;
  let accepted = 0;
  let acceptedErrors = 0;
  for (const point of scored) {
    const baseline = acceptUtility(point, utility);
    const decision = policy.decide(point);
    const candidate = policyDecisionUtility(point, decision.action, utility);
    const reward = rewardOf2(point, utility);
    baselineUtility += baseline;
    policyUtility += candidate;
    deltas.push(candidate - baseline);
    if (decision.action === "accept") {
      accepted++;
      acceptedRewards.push(reward);
      if (reward < 0) acceptedErrors++;
    } else {
      rejectedRewards.push(reward);
    }
  }
  const n = scored.length;
  const rejected = Math.max(0, n - accepted);
  const ci = confidenceInterval(deltas, 0.95, { seed: options.seed ?? 17 });
  const reasons = [];
  if (n < minN) reasons.push(`need at least ${minN} scored decisions, got ${n}`);
  if (accepted < minAccepted)
    reasons.push(`need at least ${minAccepted} accepted decisions, got ${accepted}`);
  if (ci.lower <= minUtilityDelta) {
    reasons.push(`utility CI lower bound ${ci.lower.toFixed(4)} does not clear ${minUtilityDelta}`);
  }
  const recommendation = n < minN || accepted < minAccepted ? "need_more_data" : ci.lower > minUtilityDelta ? "ship" : "hold";
  return {
    policyId: policy.id,
    n,
    accepted,
    rejected,
    coverage: n > 0 ? accepted / n : 0,
    acceptedErrorRate: accepted > 0 ? acceptedErrors / accepted : 0,
    baselineUtility,
    policyUtility,
    utilityDelta: policyUtility - baselineUtility,
    utilityCi95: ci,
    rejectedMeanReward: rejectedRewards.length > 0 ? mean(rejectedRewards) : null,
    recommendation,
    reasons
  };
}
function acceptUtility(point, utility) {
  return rewardOf2(point, utility) - utility.costWeight * (point.costUsd ?? point.outcome?.costUsd ?? 0);
}
function policyDecisionUtility(point, action, utility) {
  if (action === "accept") return acceptUtility(point, utility);
  if (action === "verify") return utility.deferUtility - utility.verifyCost;
  if (action === "ask") return utility.deferUtility - utility.askCost;
  if (action === "retry") return utility.deferUtility - utility.retryCost;
  if (action === "stop") return utility.stopUtility;
  return utility.deferUtility;
}
function rewardOf2(point, utility) {
  const outcome = point.outcome;
  if (!outcome) return utility.failureUtility;
  if (typeof outcome.reward === "number") return 2 * outcome.reward - 1;
  if (typeof outcome.score === "number") return 2 * outcome.score - 1;
  if (outcome.success === true) return utility.successUtility;
  if (outcome.success === false) return utility.failureUtility;
  return utility.failureUtility;
}
function mean(values) {
  return values.reduce((sum, value) => sum + value, 0) / values.length;
}

// src/belief-state/report.ts
function analyzeBeliefPolicy(options) {
  const selective = evaluateBeliefSelectivePolicy(options.points, options.policy, options.selective);
  const calibration = calibrateBeliefDecisions(options.points, options.calibration);
  const opeTargetPolicy = options.ope?.targetPolicy;
  const ope = opeTargetPolicy ? evaluateBeliefOffPolicy(options.points, opeTargetPolicy, options.ope) : null;
  const diagnostics = [];
  const selectiveStatus = selective.recommendation;
  const calibrationStatus = calibration ? "supported" : "unsupported";
  const opeRequested = options.requireOpe === true || options.ope !== void 0;
  const opeStatus = ope ? ope.support.supported ? "supported" : "unsupported" : opeRequested ? "unsupported" : "not_requested";
  if (!calibration) diagnostics.push("calibration unsupported: not enough confidence/outcome pairs");
  if (opeRequested && !opeTargetPolicy) diagnostics.push("OPE unsupported: missing target policy");
  else if (ope && !ope.support.supported)
    diagnostics.push(...ope.support.reasons.map((reason) => `OPE unsupported: ${reason}`));
  const status = overallStatus({
    selectiveStatus,
    hasCalibration: calibration !== null,
    opeStatus,
    opeRequested
  });
  return {
    policyId: options.policy.id,
    n: options.points.length,
    status,
    selectiveStatus,
    calibrationStatus,
    opeStatus,
    ...ope ? { opeTargetPolicyId: ope.targetPolicyId } : {},
    selective,
    ...calibration ? { calibration } : {},
    ...ope ? { ope } : {},
    diagnostics
  };
}
function overallStatus(options) {
  if (options.selectiveStatus === "need_more_data" || !options.hasCalibration) {
    return "need_more_data";
  }
  if (options.selectiveStatus === "hold") return "hold";
  if (options.opeRequested && options.opeStatus !== "supported") return "hold";
  return "ship";
}

// src/belief-state/code-agent-corpus.ts
var FAILURE_RECOVERY_ACTIONS = ["retry", "verify", "continue", "stop"];
var TARGET_LABELS = {
  "failure-recovery": "Failure recovery after tool or patch failure",
  "tool-selection": "Tool/action selection",
  "graph-completion": "Graph completion decision"
};
function extractCodeAgentBeliefDecisionPoints(options) {
  const entries = options.entries.filter(isRecord);
  const diagnostics = [];
  const observed = observedActionsFor(options.source, entries, options);
  const decisions = [];
  for (const action of observed) {
    if (action.kind === "tool" || action.kind === "patch") {
      decisions.push(toolSelectionDecision(action, options));
    }
    if (action.kind === "graph-completion") {
      decisions.push(graphCompletionDecision(action, options));
    }
  }
  for (const failed of observed) {
    if (failed.kind !== "tool" && failed.kind !== "patch" || failed.success !== false) continue;
    const next = observed.find(
      (candidate) => candidate.stepIndex > failed.stepIndex && (candidate.kind === "tool" || candidate.kind === "patch" || candidate.kind === "terminal")
    );
    if (!next) {
      diagnostics.push({
        runId: options.run.runId,
        severity: "warning",
        reason: `${failed.id}: failed action has no observable follow-up decision`
      });
      continue;
    }
    decisions.push(failureRecoveryDecision(failed, next, options));
  }
  if (decisions.length === 0) {
    diagnostics.push({
      runId: options.run.runId,
      severity: "info",
      reason: `no belief decision points extracted from ${options.source} entries`
    });
  }
  return { decisions, diagnostics };
}
function inventoryBeliefDecisionPoints(points) {
  const byKind = [...groupBy(points, (point) => point.kind).entries()].map(
    ([kind, bucketPoints]) => bucketFor(kind, bucketPoints, { kind })
  ).sort(sortBuckets);
  const byTarget = [...groupBy(points, targetIdOf).entries()].filter((entry) => {
    return entry[0] !== void 0;
  }).map(([targetId, bucketPoints]) => bucketFor(targetId, bucketPoints, { targetId })).sort(sortBuckets);
  const diagnostics = [];
  if (points.length === 0) diagnostics.push("no decision points available");
  for (const bucket of byTarget) {
    if (bucket.withOutcome < bucket.n) {
      diagnostics.push(`${bucket.id}: ${bucket.n - bucket.withOutcome} decision(s) missing outcome`);
    }
    if (bucket.withBehaviorProb < bucket.n || bucket.withTargetProb < bucket.n) {
      diagnostics.push(`${bucket.id}: OPE support incomplete`);
    }
  }
  return { n: points.length, byKind, byTarget, diagnostics };
}
function selectBeliefDecisionTarget(points, options = {}) {
  const minN = options.minN ?? 10;
  const minOutcomeCoverage = options.minOutcomeCoverage ?? 0.8;
  const preferredTargets = options.preferredTargets ?? [
    "failure-recovery",
    "tool-selection",
    "graph-completion"
  ];
  const inventory = inventoryBeliefDecisionPoints(points);
  for (const targetId of preferredTargets) {
    const support = inventory.byTarget.find((bucket) => bucket.targetId === targetId);
    if (!support) continue;
    const reasons = [];
    if (support.n < minN) reasons.push(`need at least ${minN} decisions, got ${support.n}`);
    const outcomeCoverage = support.n > 0 ? support.withOutcome / support.n : 0;
    if (outcomeCoverage < minOutcomeCoverage) {
      reasons.push(
        `outcome coverage ${outcomeCoverage.toFixed(2)} below ${minOutcomeCoverage.toFixed(2)}`
      );
    }
    if (reasons.length > 0) continue;
    const targetPoints = points.filter((point) => targetIdOf(point) === targetId);
    return {
      id: targetId,
      label: TARGET_LABELS[targetId],
      points: targetPoints,
      support,
      reasons
    };
  }
  return null;
}
function analyzeBeliefDecisionCorpus(options) {
  const inventory = inventoryBeliefDecisionPoints(options.points);
  const diagnostics = [...inventory.diagnostics];
  const target = options.targetId !== void 0 ? targetSelectionFor(options.points, options.targetId, options) : selectBeliefDecisionTarget(options.points, options);
  if (!target) {
    diagnostics.push("no decision target has enough support for policy evaluation");
    return { inventory, diagnostics };
  }
  const policy = options.policy ?? thresholdSelectivePolicy({
    id: `${target.id}:confidence>=${options.confidenceThreshold ?? 0.5}`,
    confidenceThreshold: options.confidenceThreshold ?? 0.5,
    belowThresholdAction: "verify"
  });
  const minN = options.minN ?? 10;
  const evaluation = analyzeBeliefPolicy({
    points: target.points,
    policy,
    selective: {
      minN,
      minAccepted: options.minAccepted ?? Math.min(5, minN),
      minUtilityDelta: 0,
      ...options.policyOptions?.selective ?? {}
    },
    calibration: {
      minPairs: Math.min(10, minN),
      policy,
      region: "all",
      ...options.policyOptions?.calibration ?? {}
    },
    ope: {
      targetPolicy: embeddedBeliefOpeTargetPolicy(`${target.id}:embedded-target-prob`),
      minEffectiveSampleSize: minN,
      ...options.policyOptions?.ope ?? {}
    },
    requireOpe: options.requireOpe ?? true
  });
  return { inventory, target, policy, evaluation, diagnostics };
}
function observedActionsFor(source, entries, options) {
  switch (source) {
    case "codex":
      return codexObservedActions(entries, options);
    case "claude-code":
      return claudeObservedActions(entries, options);
    case "opencode":
      return openCodeObservedActions(entries, options);
    case "kimi-code":
      return kimiObservedActions(entries, options);
    case "pi":
      return piObservedActions(entries, options);
  }
}
function codexObservedActions(entries, options) {
  const actions = [];
  const calls = /* @__PURE__ */ new Map();
  for (const entry of entries) {
    const payload = record(entry.payload) ?? {};
    const entryType = stringField(entry, "type");
    const payloadType = stringField(payload, "type");
    const timestamp = timestampMs(entry.timestamp);
    if (entryType === "response_item") {
      if (payloadType === "function_call" || payloadType === "custom_tool_call") {
        const callId = stringField(payload, "call_id") ?? stringField(payload, "id") ?? `${actions.length}`;
        const action = observedAction({
          options,
          localId: callId,
          stepIndex: actions.length,
          kind: "tool",
          action: stringField(payload, "name") ?? payloadType,
          timestamp,
          metadata: { sourceEventType: entryType, payloadType }
        });
        calls.set(callId, action);
        actions.push(action);
      }
      if (payloadType === "function_call_output" || payloadType === "custom_tool_call_output") {
        const callId = stringField(payload, "call_id") ?? stringField(payload, "id");
        const action = callId ? calls.get(callId) : void 0;
        if (action) action.success = !looksLikeError(payload.output);
      }
    }
    if (entryType === "event_msg") {
      if (payloadType === "patch_apply_end") {
        actions.push(
          observedAction({
            options,
            localId: stringField(payload, "call_id") ?? `patch-${actions.length}`,
            stepIndex: actions.length,
            kind: "patch",
            action: "patch",
            timestamp,
            success: typeof payload.success === "boolean" ? payload.success : void 0,
            metadata: { sourceEventType: entryType, payloadType }
          })
        );
      }
      if (payloadType === "task_complete" || payloadType === "turn_aborted") {
        actions.push(
          observedAction({
            options,
            localId: `${payloadType}-${actions.length}`,
            stepIndex: actions.length,
            kind: "terminal",
            action: payloadType === "task_complete" ? "stop" : "abort",
            timestamp,
            success: payloadType === "task_complete",
            metadata: { sourceEventType: entryType, payloadType }
          })
        );
      }
    }
  }
  return actions;
}
function claudeObservedActions(entries, options) {
  const actions = [];
  const calls = /* @__PURE__ */ new Map();
  for (const entry of entries) {
    const timestamp = timestampMs(entry.timestamp);
    const message = record(entry.message);
    const content = Array.isArray(message?.content) ? message.content : [];
    for (const item of content) {
      const part = record(item);
      if (!part) continue;
      const partType = stringField(part, "type");
      if (partType === "tool_use") {
        const id = stringField(part, "id") ?? `tool-${actions.length}`;
        const action = observedAction({
          options,
          localId: id,
          stepIndex: actions.length,
          kind: "tool",
          action: stringField(part, "name") ?? "tool",
          timestamp,
          metadata: { sourceEventType: stringField(entry, "type"), partType }
        });
        calls.set(id, action);
        actions.push(action);
      }
      if (partType === "tool_result") {
        const id = stringField(part, "tool_use_id");
        const action = id ? calls.get(id) : void 0;
        if (action) action.success = part.is_error !== true;
      }
    }
    if (stringField(entry, "type") === "pr-link") {
      actions.push(
        observedAction({
          options,
          localId: `pr-link-${actions.length}`,
          stepIndex: actions.length,
          kind: "terminal",
          action: "stop",
          timestamp,
          success: true,
          metadata: { sourceEventType: "pr-link" }
        })
      );
    }
  }
  return actions;
}
function openCodeObservedActions(entries, options) {
  const actions = [];
  for (const entry of entries) {
    const type = stringField(entry, "type");
    const role = stringField(entry, "role");
    const time = record(entry.time);
    const timestamp = timestampMs(time?.created);
    if (type === "tool") {
      const state = record(entry.state);
      const status = stringField(state ?? {}, "status");
      actions.push(
        observedAction({
          options,
          localId: stringField(entry, "id") ?? `tool-${actions.length}`,
          stepIndex: actions.length,
          kind: "tool",
          action: stringField(entry, "tool") ?? "tool",
          timestamp,
          success: status === "completed" ? true : status === "error" ? false : void 0,
          metadata: { sourceEventType: type, status }
        })
      );
    }
    if (type === "patch") {
      actions.push(
        observedAction({
          options,
          localId: stringField(entry, "id") ?? `patch-${actions.length}`,
          stepIndex: actions.length,
          kind: "patch",
          action: "patch",
          timestamp,
          success: true,
          metadata: { sourceEventType: type }
        })
      );
    }
    if (role === "assistant") {
      const finish = stringField(entry, "finish");
      if (finish === "stop" || finish === "error") {
        actions.push(
          observedAction({
            options,
            localId: stringField(entry, "id") ?? `terminal-${actions.length}`,
            stepIndex: actions.length,
            kind: "terminal",
            action: finish === "stop" ? "stop" : "abort",
            timestamp: timestampMs(time?.completed) ?? timestamp,
            success: finish === "stop",
            costUsd: numberField(entry, "cost"),
            metadata: { sourceEventType: "assistant", finish }
          })
        );
      }
    }
  }
  return actions;
}
function kimiObservedActions(entries, options) {
  const actions = [];
  const calls = /* @__PURE__ */ new Map();
  for (const entry of entries) {
    const timestamp = timestampMs(entry.timestamp);
    const message = record(entry.message);
    const messageType = stringField(message ?? {}, "type");
    const payload = record(message?.payload) ?? {};
    if (messageType === "ToolCall") {
      const call = record(payload.function);
      const id = stringField(payload, "id") ?? `tool-${actions.length}`;
      const action = observedAction({
        options,
        localId: id,
        stepIndex: actions.length,
        kind: "tool",
        action: stringField(call ?? {}, "name") ?? "tool",
        timestamp,
        metadata: { sourceEventType: messageType }
      });
      calls.set(id, action);
      actions.push(action);
    }
    if (messageType === "ToolResult") {
      const id = stringField(payload, "tool_call_id");
      const action = id ? calls.get(id) : void 0;
      if (action) action.success = record(payload.return_value)?.is_error !== true;
    }
    if (messageType === "TurnEnd" || messageType === "StepInterrupted") {
      actions.push(
        observedAction({
          options,
          localId: `${messageType}-${actions.length}`,
          stepIndex: actions.length,
          kind: "terminal",
          action: messageType === "TurnEnd" ? "stop" : "abort",
          timestamp,
          success: messageType === "TurnEnd",
          metadata: { sourceEventType: messageType }
        })
      );
    }
  }
  return actions;
}
function piObservedActions(entries, options) {
  const actions = [];
  for (const entry of entries) {
    const nodes = Array.isArray(entry.nodes) ? entry.nodes : [];
    for (const node of nodes) {
      const obj = record(node);
      const ir = record(obj?.ir) ?? obj;
      const kind = stringField(ir ?? {}, "kind");
      if (kind === "ToolInvocation") {
        actions.push(
          observedAction({
            options,
            localId: stringField(ir ?? {}, "id") ?? stringField(obj ?? {}, "id") ?? `tool-${actions.length}`,
            stepIndex: actions.length,
            kind: "tool",
            action: "graph-tool",
            timestamp: timestampMs(ir?.createdAt),
            success: void 0,
            metadata: { sourceEventType: "graph-node", graphKind: kind }
          })
        );
      }
      if (kind === "ToolResult") {
        const prior = [...actions].reverse().find((action) => action.kind === "tool");
        if (prior) prior.success = true;
      }
      if (kind === "CompletionDecision") {
        actions.push(
          observedAction({
            options,
            localId: stringField(ir ?? {}, "id") ?? stringField(obj ?? {}, "id") ?? `completion-${actions.length}`,
            stepIndex: actions.length,
            kind: "graph-completion",
            action: "complete",
            timestamp: timestampMs(ir?.createdAt),
            success: true,
            metadata: { sourceEventType: "graph-node", graphKind: kind }
          })
        );
      }
    }
  }
  return actions;
}
function toolSelectionDecision(action, options) {
  return {
    id: `${options.run.runId}:tool-selection:${action.localId}`,
    runId: options.run.runId,
    scenarioId: options.run.scenarioId,
    stepIndex: action.stepIndex,
    kind: "tool-select",
    chosenAction: action.action,
    candidateActions: [action.action],
    confidence: 0.65,
    costUsd: action.costUsd,
    evidence: action.evidence,
    outcome: outcomeFromAction(action, options.run),
    metadata: {
      target: "tool-selection",
      source: options.source,
      actionKind: action.kind,
      confidenceSource: "fixed-observed-action-prior",
      ...action.metadata
    }
  };
}
function graphCompletionDecision(action, options) {
  return {
    id: `${options.run.runId}:graph-completion:${action.localId}`,
    runId: options.run.runId,
    scenarioId: options.run.scenarioId,
    stepIndex: action.stepIndex,
    kind: "stop",
    chosenAction: "complete",
    candidateActions: ["complete", "continue", "verify"],
    confidence: 0.75,
    evidence: action.evidence,
    outcome: outcomeFromAction(action, options.run),
    metadata: {
      target: "graph-completion",
      source: options.source,
      confidenceSource: "fixed-graph-completion-prior",
      ...action.metadata
    }
  };
}
function failureRecoveryDecision(failed, next, options) {
  const chosenAction = classifyFailureRecovery(failed, next);
  return {
    id: `${options.run.runId}:failure-recovery:${failed.localId}`,
    runId: options.run.runId,
    scenarioId: options.run.scenarioId,
    stepIndex: failed.stepIndex,
    kind: "retry",
    chosenAction,
    candidateActions: [...FAILURE_RECOVERY_ACTIONS],
    confidence: recoveryConfidence(chosenAction),
    evidence: [...failed.evidence, ...next.evidence],
    outcome: outcomeFromAction(next, options.run),
    metadata: {
      target: "failure-recovery",
      source: options.source,
      failedActionKind: failed.kind,
      failedAction: failed.action,
      nextActionKind: next.kind,
      nextAction: next.action,
      confidenceSource: "heuristic-observed-follow-up"
    }
  };
}
function classifyFailureRecovery(failed, next) {
  if (next.kind === "terminal") return "stop";
  if (isVerificationAction(next.action)) return "verify";
  if (next.kind === failed.kind && next.action === failed.action) return "retry";
  return "continue";
}
function recoveryConfidence(action) {
  if (action === "verify") return 0.8;
  if (action === "retry") return 0.6;
  if (action === "stop") return 0.55;
  return 0.35;
}
function isVerificationAction(action) {
  const normalized = action.toLowerCase();
  return normalized.includes("verify") || normalized.includes("test") || normalized.includes("check") || normalized.includes("lint") || normalized.includes("build") || normalized.includes("typecheck") || normalized.includes("pytest") || normalized.includes("vitest") || normalized.includes("tsc");
}
function outcomeFromAction(action, run) {
  const runScore = scoreFromRun(run);
  const success = action.success ?? (runScore !== null ? runScore >= 0.5 : void 0);
  const score = action.success === void 0 ? runScore ?? void 0 : action.success ? 1 : 0;
  if (success === void 0 && score === void 0) return void 0;
  return {
    ...success !== void 0 ? { success } : {},
    ...score !== void 0 ? { score, reward: score } : {},
    ...action.costUsd !== void 0 ? { costUsd: action.costUsd } : {},
    metadata: {
      outcomeSource: action.success === void 0 ? "run-score" : "observed-action-status"
    }
  };
}
function observedAction(input) {
  const id = `${input.options.run.runId}:${input.options.source}:${input.localId}`;
  return {
    id,
    localId: input.localId,
    stepIndex: input.stepIndex,
    kind: input.kind,
    action: input.action,
    timestamp: input.timestamp,
    success: input.success,
    costUsd: input.costUsd,
    evidence: [
      {
        source: "event",
        id,
        runId: input.options.run.runId,
        detail: input.action,
        metadata: {
          source: input.options.source,
          sourcePath: input.options.sourcePath,
          ...input.metadata
        }
      }
    ],
    metadata: input.metadata ?? {}
  };
}
function targetSelectionFor(points, targetId, options) {
  const targetPoints = points.filter((point) => targetIdOf(point) === targetId);
  if (targetPoints.length === 0) return null;
  const support = bucketFor(targetId, targetPoints, { targetId });
  const minN = options.minN ?? 10;
  const minOutcomeCoverage = options.minOutcomeCoverage ?? 0.8;
  const reasons = [];
  if (support.n < minN) reasons.push(`need at least ${minN} decisions, got ${support.n}`);
  const outcomeCoverage = support.n > 0 ? support.withOutcome / support.n : 0;
  if (outcomeCoverage < minOutcomeCoverage) {
    reasons.push(
      `outcome coverage ${outcomeCoverage.toFixed(2)} below ${minOutcomeCoverage.toFixed(2)}`
    );
  }
  if (reasons.length > 0) return null;
  return { id: targetId, label: TARGET_LABELS[targetId], points: targetPoints, support, reasons };
}
function bucketFor(id, points, identity) {
  const outcomes = points.filter((point) => point.outcome);
  const scores = outcomes.map((point) => outcomeScore2(point.outcome)).filter((score) => score !== null);
  const confidences = points.map((point) => point.confidence).filter((confidence) => typeof confidence === "number");
  const successes = outcomes.filter((point) => point.outcome?.success === true).length;
  const successDenominator = outcomes.filter(
    (point) => typeof point.outcome?.success === "boolean"
  ).length;
  return {
    id,
    ...identity,
    n: points.length,
    withOutcome: outcomes.length,
    withConfidence: confidences.length,
    withCandidateActions: points.filter((point) => (point.candidateActions?.length ?? 0) > 0).length,
    withBehaviorProb: points.filter((point) => point.behaviorProb !== void 0).length,
    withTargetProb: points.filter((point) => point.targetProb !== void 0).length,
    successRate: successDenominator > 0 ? successes / successDenominator : null,
    meanScore: scores.length > 0 ? mean2(scores) : null,
    meanConfidence: confidences.length > 0 ? mean2(confidences) : null
  };
}
function targetIdOf(point) {
  const target = point.metadata?.target;
  if (target === "failure-recovery" || target === "tool-selection" || target === "graph-completion")
    return target;
  return void 0;
}
function outcomeScore2(outcome) {
  if (!outcome) return null;
  if (typeof outcome.score === "number") return outcome.score;
  if (typeof outcome.reward === "number") return outcome.reward;
  if (outcome.success === true) return 1;
  if (outcome.success === false) return 0;
  return null;
}
function scoreFromRun(run) {
  if (typeof run.outcome.holdoutScore === "number") return run.outcome.holdoutScore;
  if (typeof run.outcome.searchScore === "number") return run.outcome.searchScore;
  return null;
}
function sortBuckets(a, b) {
  return b.n - a.n || a.id.localeCompare(b.id);
}
function groupBy(values, keyOf) {
  const map = /* @__PURE__ */ new Map();
  for (const value of values) {
    const key = keyOf(value);
    const bucket = map.get(key);
    if (bucket) bucket.push(value);
    else map.set(key, [value]);
  }
  return map;
}
function mean2(values) {
  return values.reduce((sum, value) => sum + value, 0) / values.length;
}
function looksLikeError(value) {
  if (isRecord(value)) {
    if (value.is_error === true || value.error === true) return true;
    if ("Err" in value) return true;
  }
  if (typeof value !== "string") return false;
  return /\b(error|failed|exception|traceback)\b/i.test(value);
}
function timestampMs(value) {
  if (typeof value === "number" && Number.isFinite(value)) {
    return value > 1e12 ? value : value * 1e3;
  }
  if (typeof value === "string" && value.length > 0) {
    const parsed = Date.parse(value);
    return Number.isFinite(parsed) ? parsed : void 0;
  }
  return void 0;
}
function stringField(obj, key) {
  const value = obj[key];
  return typeof value === "string" && value.length > 0 ? value : void 0;
}
function numberField(obj, key) {
  const value = obj[key];
  return typeof value === "number" && Number.isFinite(value) ? value : void 0;
}
function record(value) {
  return isRecord(value) ? value : null;
}
function isRecord(value) {
  return value !== null && typeof value === "object" && !Array.isArray(value);
}

// src/belief-state/research-evidence.ts
function buildBeliefDecisionResearchEvidencePacket(options) {
  const claimScope = options.claimScope ?? "counterfactual";
  const requireOpe = claimScope === "counterfactual";
  const analysis = analyzeBeliefDecisionCorpus({
    ...options,
    requireOpe: options.requireOpe ?? requireOpe
  });
  const gates = [
    corpusGate(analysis),
    selectiveGate(analysis),
    calibrationGate(analysis),
    ...requireOpe ? [opeGate(analysis)] : []
  ];
  const caveats = unique([
    ...gates.flatMap((gate) => gate.caveats),
    ...claimScope === "selective" ? ["counterfactual claims excluded: OPE support was not required"] : []
  ]);
  return {
    claimScope,
    status: gates.every((gate) => gate.status === "supported") ? "supported" : "blocked",
    analysis,
    gates,
    blockers: unique(gates.flatMap((gate) => gate.blockers)),
    caveats
  };
}
function corpusGate(analysis) {
  const support = analysis.target?.support;
  if (!support) {
    return blocked("corpus", "no decision target has enough outcome support");
  }
  const caveats = support.withBehaviorProb < support.n || support.withTargetProb < support.n ? ["propensity support incomplete; counterfactual claims will require OPE support"] : [];
  return { id: "corpus", status: "supported", blockers: [], caveats };
}
function selectiveGate(analysis) {
  const evaluation = analysis.evaluation;
  if (!evaluation) return blocked("selective", "no policy evaluation was produced");
  if (evaluation.selectiveStatus !== "ship") {
    return blocked(
      "selective",
      ...orDefault(
        evaluation.selective.reasons,
        `selective status is ${evaluation.selectiveStatus}`
      )
    );
  }
  return { id: "selective", status: "supported", blockers: [], caveats: [] };
}
function calibrationGate(analysis) {
  const evaluation = analysis.evaluation;
  if (!evaluation) return blocked("calibration", "no policy evaluation was produced");
  if (evaluation.calibrationStatus !== "supported") {
    return blocked("calibration", "not enough confidence/outcome pairs for calibration");
  }
  return { id: "calibration", status: "supported", blockers: [], caveats: [] };
}
function opeGate(analysis) {
  const evaluation = analysis.evaluation;
  if (!evaluation) return blocked("ope", "no policy evaluation was produced");
  if (evaluation.opeStatus !== "supported") {
    const reasons = evaluation.ope?.support.reasons ?? evaluation.diagnostics.filter((diagnostic) => diagnostic.includes("OPE"));
    return blocked("ope", ...orDefault(reasons, "missing OPE support"));
  }
  return { id: "ope", status: "supported", blockers: [], caveats: [] };
}
function blocked(id, ...blockers) {
  return { id, status: "blocked", blockers, caveats: [] };
}
function orDefault(values, fallback) {
  return values.length > 0 ? values : [fallback];
}
function unique(values) {
  return [...new Set(values)];
}

// src/belief-state/code-agent-evidence.ts
function buildCodeAgentBeliefEvidenceCorpus(options) {
  const { sessions, ...evidenceOptions } = options;
  const runs = [];
  const metrics = [];
  const intakeDiagnostics = [];
  const extractionDiagnostics = [];
  const decisions = [];
  for (const session of sessions) {
    const intake = fromCodeAgentBeliefSession(session);
    runs.push(...intake.runs);
    metrics.push(...intake.metrics);
    intakeDiagnostics.push(...intake.diagnostics);
    for (const run of intake.runs) {
      const extraction = extractCodeAgentBeliefDecisionPoints({
        source: session.source,
        entries: session.entries,
        run,
        sourcePath: session.sourcePath
      });
      decisions.push(...extraction.decisions);
      extractionDiagnostics.push(...extraction.diagnostics);
    }
  }
  const evidence = buildBeliefDecisionResearchEvidencePacket({
    ...evidenceOptions,
    points: decisions
  });
  return {
    runs,
    metrics,
    intakeDiagnostics,
    extractionDiagnostics,
    decisions,
    inventory: inventoryBeliefDecisionPoints(decisions),
    evidence
  };
}
function fromCodeAgentBeliefSession(session) {
  switch (session.source) {
    case "codex":
      return fromCodexSession(session);
    case "claude-code":
      return fromClaudeCodeSession(session);
    case "opencode":
      return fromOpenCodeSession(session);
    case "kimi-code":
      return fromKimiCodeSession(session);
    case "pi":
      return fromPiSession(session);
  }
}

// src/belief-state/types.ts
var BELIEF_DECISION_KINDS = [
  "continue",
  "verify",
  "ask",
  "retry",
  "stop",
  "memory-write",
  "memory-read",
  "tool-select",
  "skill-select",
  "workflow-select",
  "surface-promote"
];
var BELIEF_EVIDENCE_SOURCES = [
  "run",
  "span",
  "event",
  "finding",
  "memory",
  "knowledge",
  "policy"
];
var BELIEF_EVIDENCE_QUALITIES = [
  "direct",
  "derived",
  "self-reported",
  "unverified",
  "stale",
  "contradicted"
];
var BELIEF_EVALUATION_CRITERIA = [
  {
    id: "capture-integrity",
    label: "Capture integrity",
    reasonCodes: ["trace-missing", "run-record-missing", "backend-integrity-missing"]
  },
  {
    id: "decision-completeness",
    label: "Decision completeness",
    reasonCodes: [
      "candidate-actions-missing",
      "chosen-action-missing",
      "decision-evidence-missing"
    ]
  },
  {
    id: "evidence-quality",
    label: "Evidence quality",
    reasonCodes: [
      "evidence-stale",
      "evidence-contradictory",
      "evidence-unverified",
      "evidence-self-reported"
    ]
  },
  {
    id: "outcome-quality",
    label: "Outcome quality",
    reasonCodes: ["outcome-missing", "outcome-delayed", "cost-missing"]
  },
  {
    id: "calibration",
    label: "Calibration",
    reasonCodes: ["confidence-missing", "calibration-unsupported", "calibration-gap-high"]
  },
  {
    id: "accepted-region-risk",
    label: "Accepted-region risk",
    reasonCodes: ["accepted-error-high", "coverage-too-low"]
  },
  {
    id: "policy-value",
    label: "Policy value",
    reasonCodes: ["utility-lift-missing", "baseline-dominates", "cost-too-high"]
  },
  {
    id: "ope-support",
    label: "OPE support",
    reasonCodes: [
      "behavior-propensity-missing",
      "behavior-propensity-invalid",
      "target-propensity-missing",
      "target-propensity-invalid",
      "effective-sample-size-low",
      "importance-weight-high"
    ]
  },
  {
    id: "memory-health",
    label: "Memory health",
    reasonCodes: [
      "memory-stale",
      "memory-poisoning-risk",
      "context-bloat",
      "memory-write-unverified"
    ]
  },
  {
    id: "surface-attribution",
    label: "Surface attribution",
    reasonCodes: ["surface-claim-unsupported", "causal-attribution-missing"]
  },
  {
    id: "generalization",
    label: "Generalization",
    reasonCodes: [
      "split-missing",
      "holdout-regression",
      "task-family-coverage-low",
      "leakage-risk"
    ]
  },
  {
    id: "promotion",
    label: "Promotion",
    reasonCodes: ["negative-control-failed", "promotion-gate-failed", "human-review-required"]
  }
];
function isBeliefDecisionKind(value) {
  return typeof value === "string" && BELIEF_DECISION_KINDS.includes(value);
}
function isBeliefEvidenceSource(value) {
  return typeof value === "string" && BELIEF_EVIDENCE_SOURCES.includes(value);
}

// src/belief-state/extract.ts
var DECISION_MARKERS = /* @__PURE__ */ new Set(["belief_decision", "belief.decision", "decision_point"]);
async function extractBeliefDecisionPoints(store, options = {}) {
  const runs = options.runIds ? (await Promise.all(options.runIds.map((runId) => store.getRun(runId)))).filter(Boolean) : await store.listRuns();
  const decisions = [];
  const diagnostics = [];
  for (const run of runs) {
    if (!run) continue;
    const events = await store.events({ runId: run.runId });
    const spans = await store.spans({ runId: run.runId });
    const spanIds = new Set(spans.map((span) => span.spanId));
    let stepIndex = 0;
    for (const event of [...events].sort((a, b) => a.timestamp - b.timestamp)) {
      const parsed = parseDecisionEvent(event, {
        scenarioId: run.scenarioId,
        stepIndex,
        spanExists: event.spanId ? spanIds.has(event.spanId) : false
      });
      if (!parsed) continue;
      if ("diagnostic" in parsed) {
        diagnostics.push(parsed.diagnostic);
        continue;
      }
      decisions.push(parsed.decision);
      stepIndex++;
    }
  }
  return { decisions, diagnostics };
}
function parseDecisionEvent(event, context) {
  const payload = event.payload;
  const marker = stringField2(payload, "kind") ?? stringField2(payload, "type");
  if (!marker || !DECISION_MARKERS.has(marker)) return null;
  const decisionKind = stringField2(payload, "decisionKind");
  if (!isBeliefDecisionKind(decisionKind)) {
    return {
      diagnostic: {
        runId: event.runId,
        eventId: event.eventId,
        severity: "warning",
        reason: `belief decision event has unsupported decisionKind "${decisionKind ?? ""}"`
      }
    };
  }
  const chosenAction = stringField2(payload, "chosenAction");
  if (!chosenAction) {
    return {
      diagnostic: {
        runId: event.runId,
        eventId: event.eventId,
        severity: "warning",
        reason: "belief decision event is missing chosenAction"
      }
    };
  }
  const evidence = [
    {
      source: "event",
      id: event.eventId,
      runId: event.runId,
      eventId: event.eventId,
      quality: "direct"
    }
  ];
  if (event.spanId && context.spanExists) {
    evidence.push({
      source: "span",
      id: event.spanId,
      runId: event.runId,
      spanId: event.spanId,
      quality: "direct"
    });
  }
  return {
    decision: {
      id: stringField2(payload, "id") ?? event.eventId,
      runId: event.runId,
      scenarioId: stringField2(payload, "scenarioId") ?? context.scenarioId,
      stepIndex: numberField2(payload, "stepIndex") ?? context.stepIndex,
      kind: decisionKind,
      chosenAction,
      candidateActions: stringArrayField(payload, "candidateActions"),
      confidence: finiteUnitField(payload, "confidence"),
      behaviorProb: numberField2(payload, "behaviorProb"),
      targetProb: numberField2(payload, "targetProb"),
      qHat: finiteUnitField(payload, "qHat"),
      costUsd: nonNegativeNumberField(payload, "costUsd"),
      evidence,
      outcome: parseOutcome(payload),
      metadata: recordField(payload, "metadata")
    }
  };
}
function parseOutcome(payload) {
  const value = recordField(payload, "outcome");
  if (!value) return void 0;
  return {
    success: typeof value.success === "boolean" ? value.success : void 0,
    score: finiteUnitField(value, "score"),
    reward: finiteUnitField(value, "reward"),
    costUsd: nonNegativeNumberField(value, "costUsd"),
    observedAt: stringField2(value, "observedAt"),
    metadata: recordField(value, "metadata")
  };
}
function stringField2(obj, key) {
  const value = obj[key];
  return typeof value === "string" && value.length > 0 ? value : void 0;
}
function numberField2(obj, key) {
  const value = obj[key];
  return typeof value === "number" && Number.isFinite(value) ? value : void 0;
}
function finiteUnitField(obj, key) {
  const value = numberField2(obj, key);
  return value === void 0 ? void 0 : Math.max(0, Math.min(1, value));
}
function nonNegativeNumberField(obj, key) {
  const value = numberField2(obj, key);
  return value === void 0 ? void 0 : Math.max(0, value);
}
function stringArrayField(obj, key) {
  const value = obj[key];
  if (!Array.isArray(value)) return void 0;
  const strings = value.filter(
    (item) => typeof item === "string" && item.length > 0
  );
  return strings.length > 0 ? strings : void 0;
}
function recordField(obj, key) {
  const value = obj[key];
  if (!value || typeof value !== "object" || Array.isArray(value)) return void 0;
  return value;
}

// src/belief-state/runtime-hooks.ts
var DEFAULT_MAX_CONTEXT_CHARS = 12e3;
var DEFAULT_PAYLOAD_PREVIEW_CHARS = 2e3;
function runtimeDecisionPointToBeliefShadowProbeInput(point, options) {
  const diagnostics = [];
  const decisionKind = resolveDecisionKind(point, options.decisionKind, diagnostics);
  if (!decisionKind) return { diagnostics };
  const lifecycleEvidence = runtimeHookEventsToEvidenceRefs(point, options);
  const evidence = [...point.evidence ?? [], ...lifecycleEvidence];
  return {
    input: {
      probeId: options.probeId,
      decisionId: point.id,
      runId: point.runId,
      scenarioId: point.scenarioId,
      stepIndex: point.stepIndex,
      decisionKind,
      candidateActions: uniqueStrings(point.candidateActions ?? []),
      evidence: evidence.map((ref) => ({
        id: ref.id,
        source: ref.source,
        ...options.includeEvidenceDetail && ref.detail ? { detail: ref.detail } : {},
        ...ref.quality ? { quality: ref.quality } : {}
      })),
      context: trimText(point.context, options.maxContextChars),
      metadata: mergeMetadata(point.metadata, lifecycleMetadata(lifecycleEvidence))
    },
    diagnostics
  };
}
function runtimeDecisionPointToBeliefDecisionPoint(point, options) {
  const diagnostics = [];
  const decisionKind = resolveDecisionKind(point, options.decisionKind, diagnostics);
  const chosenAction = stringOrUndefined(options.chosenAction);
  if (!chosenAction) {
    diagnostics.push({
      decisionId: point.id,
      severity: "error",
      reason: "missing chosenAction"
    });
  }
  const candidateActions = uniqueStrings(point.candidateActions ?? []);
  if (chosenAction && candidateActions.length > 0 && !candidateActions.includes(chosenAction)) {
    diagnostics.push({
      decisionId: point.id,
      severity: "warning",
      reason: `chosenAction ${chosenAction} is not in candidateActions`
    });
  }
  if (!decisionKind || !chosenAction) return { diagnostics };
  const lifecycleEvidence = runtimeHookEventsToEvidenceRefs(point, options);
  const evidence = [...point.evidence ?? [], ...lifecycleEvidence];
  return {
    point: {
      id: point.id,
      runId: point.runId,
      scenarioId: point.scenarioId,
      stepIndex: point.stepIndex,
      kind: decisionKind,
      chosenAction,
      candidateActions,
      confidence: unitProbabilityOrUndefined(options.confidence),
      behaviorProb: finiteNumberOrUndefined(options.behaviorProb),
      targetProb: finiteNumberOrUndefined(options.targetProb),
      qHat: options.qHat === null ? null : unitProbabilityOrUndefined(options.qHat),
      costUsd: nonNegativeNumberOrUndefined(options.costUsd),
      evidence: evidence.map((ref) => runtimeEvidenceToBeliefEvidence(ref, point)),
      outcome: options.outcome,
      metadata: mergeMetadata(
        mergeMetadata(point.metadata, lifecycleMetadata(lifecycleEvidence)),
        options.metadata
      )
    },
    diagnostics
  };
}
function createBeliefRuntimeHookCollector(defaults) {
  const decisions = [];
  const events = [];
  return {
    hooks: {
      onEvent: (event) => {
        events.push(snapshotRuntimeHookEvent(event));
      },
      onDecisionPoint: (point) => {
        decisions.push(snapshotRuntimeDecisionPoint(point));
      }
    },
    decisions,
    events,
    toShadowProbeInputs: (options = {}) => {
      const inputs = [];
      const diagnostics = [];
      const includeLifecycleEvidence = options.includeLifecycleEvidence ?? defaults.includeLifecycleEvidence;
      for (const point of decisions) {
        const report = runtimeDecisionPointToBeliefShadowProbeInput(point, {
          ...defaults,
          ...options,
          includeLifecycleEvidence,
          lifecycleEvents: includeLifecycleEvidence === false ? void 0 : options.lifecycleEvents ?? defaults.lifecycleEvents ?? events
        });
        if (report.input) inputs.push(report.input);
        diagnostics.push(...report.diagnostics);
      }
      return { inputs, diagnostics };
    },
    clear: () => {
      decisions.length = 0;
      events.length = 0;
    }
  };
}
function resolveDecisionKind(point, override, diagnostics) {
  const kind = override ?? point.kind;
  if (isBeliefDecisionKind(kind)) return kind;
  diagnostics.push({
    decisionId: point.id,
    severity: "error",
    reason: `unsupported decisionKind "${kind}"`
  });
  return void 0;
}
function runtimeEvidenceToBeliefEvidence(ref, point) {
  if (isBeliefEvidenceSource(ref.source)) {
    return {
      source: ref.source,
      id: ref.id,
      runId: point.runId,
      detail: ref.detail,
      quality: ref.quality,
      metadata: ref.metadata
    };
  }
  return {
    source: "event",
    id: ref.id,
    runId: point.runId,
    detail: ref.detail,
    quality: ref.quality,
    metadata: mergeMetadata({ runtimeSource: ref.source }, ref.metadata)
  };
}
function runtimeHookEventsToEvidenceRefs(point, options) {
  if (options.includeLifecycleEvidence === false) return [];
  return (options.lifecycleEvents ?? []).filter((event) => runtimeHookEventMatchesDecision(point, event)).map(runtimeHookEventToEvidenceRef);
}
function runtimeHookEventMatchesDecision(point, event) {
  if (event.runId !== point.runId) return false;
  if (event.scenarioId && point.scenarioId && event.scenarioId !== point.scenarioId) return false;
  return event.stepIndex === void 0 || event.stepIndex === point.stepIndex;
}
function runtimeHookEventToEvidenceRef(event) {
  return {
    source: "runtime_event",
    id: event.id,
    detail: `${event.target}:${event.phase}`,
    quality: "direct",
    metadata: mergeMetadata(
      compactMetadata({
        target: event.target,
        phase: event.phase,
        timestamp: event.timestamp,
        stepIndex: event.stepIndex,
        parentId: event.parentId,
        payloadPreview: previewUnknown(event.payload)
      }),
      event.metadata
    )
  };
}
function lifecycleMetadata(refs) {
  if (refs.length === 0) return void 0;
  return {
    lifecycleEventCount: refs.length,
    lifecycleEventIds: refs.map((ref) => ref.id)
  };
}
function snapshotRuntimeHookEvent(event) {
  return {
    id: event.id,
    runId: event.runId,
    scenarioId: event.scenarioId,
    target: event.target,
    phase: event.phase,
    timestamp: event.timestamp,
    stepIndex: event.stepIndex,
    parentId: event.parentId,
    payload: snapshotUnknown(event.payload),
    metadata: event.metadata ? { ...event.metadata } : void 0
  };
}
function snapshotRuntimeDecisionPoint(point) {
  return {
    id: point.id,
    runId: point.runId,
    scenarioId: point.scenarioId,
    stepIndex: point.stepIndex,
    kind: point.kind,
    candidateActions: [...point.candidateActions ?? []],
    context: point.context,
    evidence: (point.evidence ?? []).map((ref) => ({
      source: ref.source,
      id: ref.id,
      detail: ref.detail,
      quality: ref.quality,
      metadata: ref.metadata ? { ...ref.metadata } : void 0
    })),
    metadata: point.metadata ? { ...point.metadata } : void 0
  };
}
function mergeMetadata(base, extra) {
  if (!base && !extra) return void 0;
  return { ...base ?? {}, ...extra ?? {} };
}
function compactMetadata(values) {
  const entries = Object.entries(values).filter(([, value]) => value !== void 0);
  return entries.length > 0 ? Object.fromEntries(entries) : void 0;
}
function previewUnknown(value, maxChars = DEFAULT_PAYLOAD_PREVIEW_CHARS) {
  if (value === void 0) return void 0;
  if (typeof value === "string") return trimText(value, maxChars);
  try {
    return trimText(JSON.stringify(value), maxChars);
  } catch {
    return trimText(String(value), maxChars);
  }
}
function snapshotUnknown(value) {
  if (Array.isArray(value)) return [...value];
  if (isRecord2(value)) return { ...value };
  return value;
}
function isRecord2(value) {
  return typeof value === "object" && value !== null && !Array.isArray(value);
}
function uniqueStrings(values) {
  return [...new Set(values.filter((value) => value.length > 0))];
}
function trimText(value, maxChars = DEFAULT_MAX_CONTEXT_CHARS) {
  if (!value) return void 0;
  return value.length > maxChars ? value.slice(value.length - maxChars) : value;
}
function stringOrUndefined(value) {
  return typeof value === "string" && value.length > 0 ? value : void 0;
}
function finiteNumberOrUndefined(value) {
  return typeof value === "number" && Number.isFinite(value) ? value : void 0;
}
function unitProbabilityOrUndefined(value) {
  const number = finiteNumberOrUndefined(value);
  return number !== void 0 && number >= 0 && number <= 1 ? number : void 0;
}
function nonNegativeNumberOrUndefined(value) {
  const number = finiteNumberOrUndefined(value);
  return number !== void 0 && number >= 0 ? number : void 0;
}

// src/belief-state/phase0-measurement.ts
var DEFAULT_BASELINE_POLICY_ID = "always-accept-observed-action";
function buildRuntimeBeliefPhase0Measurement(options) {
  const runsById = new Map(options.runs.map((run) => [run.runId, run]));
  const labelsByDecisionId = /* @__PURE__ */ new Map();
  const diagnostics = [];
  for (const label of options.labels) {
    if (labelsByDecisionId.has(label.decisionId)) {
      diagnostics.push(`${label.decisionId}: duplicate label; using the last label`);
    }
    labelsByDecisionId.set(label.decisionId, label);
  }
  const points = [];
  let missingRunRecordCount = 0;
  let missingLabelCount = 0;
  for (const decision of options.decisions) {
    const run = runsById.get(decision.runId);
    if (!run) {
      missingRunRecordCount += 1;
      diagnostics.push(`${decision.id}: missing RunRecord join for runId ${decision.runId}`);
      continue;
    }
    const label = labelsByDecisionId.get(decision.id);
    if (!label) {
      missingLabelCount += 1;
      diagnostics.push(`${decision.id}: missing observed action/outcome label`);
      continue;
    }
    const splitTag = label.splitTag ?? run.splitTag;
    const report = runtimeDecisionPointToBeliefDecisionPoint(
      { ...decision, scenarioId: decision.scenarioId ?? run.scenarioId },
      {
        chosenAction: label.chosenAction,
        confidence: label.confidence,
        behaviorProb: label.behaviorProb,
        targetProb: label.targetProb,
        qHat: label.qHat,
        costUsd: label.costUsd,
        outcome: label.outcome,
        lifecycleEvents: options.events,
        metadata: compactMetadata2({
          baselinePolicyId: options.baselinePolicyId ?? DEFAULT_BASELINE_POLICY_ID,
          splitTag,
          ...label.metadata
        })
      }
    );
    diagnostics.push(...report.diagnostics.map((item) => `${item.decisionId}: ${item.reason}`));
    if (report.point) points.push(report.point);
  }
  const packet = buildBeliefDecisionResearchEvidencePacket({
    ...options,
    points
  });
  return {
    points,
    packet,
    summary: summarizePhase0Measurement(options, points, packet, {
      missingRunRecordCount,
      missingLabelCount
    }),
    diagnostics
  };
}
function summarizePhase0Measurement(options, points, packet, counts) {
  const producerDecisionCount = options.decisions.length;
  return {
    runCount: options.runs.length,
    producerDecisionCount,
    lifecycleEventCount: options.events?.length ?? 0,
    labelCount: options.labels.length,
    completedPointCount: points.length,
    runJoinRate: ratio(producerDecisionCount - counts.missingRunRecordCount, producerDecisionCount),
    labelJoinRate: ratio(points.length, producerDecisionCount),
    missingRunRecordCount: counts.missingRunRecordCount,
    missingLabelCount: counts.missingLabelCount,
    withEvidence: points.filter((point) => point.evidence.length > 0).length,
    withOutcome: points.filter((point) => point.outcome).length,
    withSplit: points.filter((point) => typeof point.metadata?.splitTag === "string").length,
    withBehaviorProb: points.filter((point) => point.behaviorProb !== void 0).length,
    withTargetProb: points.filter((point) => point.targetProb !== void 0).length,
    baselinePolicyId: options.baselinePolicyId ?? DEFAULT_BASELINE_POLICY_ID,
    packetStatus: packet.status,
    claimScope: packet.claimScope
  };
}
function ratio(numerator, denominator) {
  return denominator > 0 ? numerator / denominator : 0;
}
function compactMetadata2(values) {
  const entries = Object.entries(values).filter(([, value]) => value !== void 0);
  return entries.length > 0 ? Object.fromEntries(entries) : void 0;
}

// src/belief-state/runtime-benchmark-corpus.ts
var MAX_STRING_LENGTH = 12e3;
var MAX_CONTEXT_LENGTH = 2e4;
var MAX_EVIDENCE_DETAIL_LENGTH = 2e3;
var MAX_CANDIDATE_ACTIONS = 50;
var MAX_EVIDENCE_REFS = 50;
var MAX_METADATA_DEPTH = 4;
var MAX_METADATA_KEYS = 100;
var SENSITIVE_KEY_RE = /(?:authorization|api[_-]?key|token|secret|password|cookie|credential|bearer)/i;
var SENSITIVE_VALUE_RES = [
  /\bBearer\s+[A-Za-z0-9._~+/=-]+/gi,
  /\b(?:sk|gh[pousr])_[A-Za-z0-9_]{20,}\b/g,
  /\b(?:sk|ghp|gho|ghu|ghs|ghr)-[A-Za-z0-9_-]{20,}\b/g
];
var SENSITIVE_ASSIGNMENT_RE = /\b(api[_-]?key|token|secret|password|cookie)\s*[:=]\s*["']?[^"'\s,;}]+/gi;
function buildRuntimeBenchmarkBeliefPhase0Measurement(options) {
  const diagnostics = [];
  const trajectory = projectRuntimeTrajectoryEvidence({
    records: options.records,
    defaultSplitTag: options.defaultSplitTag,
    recordIdOf: runtimeBenchmarkRecordId,
    scenarioIdOf: runtimeBenchmarkScenarioId
  });
  const decisions = options.decisions ?? runtimeBenchmarkDecisionPoints(options.records, diagnostics);
  const labels = options.labels ?? [];
  if (decisions.length === 0) {
    diagnostics.push(
      "no runtime decision points supplied or found on records; benchmark lifecycle events alone cannot produce belief decision rows"
    );
  }
  if (labels.length === 0 && decisions.length > 0) {
    diagnostics.push(
      "no decision labels supplied; observed action/outcome joins will be incomplete"
    );
  }
  const measurement = buildRuntimeBeliefPhase0Measurement({
    ...options,
    runs: trajectory.runs,
    events: trajectory.events,
    decisions,
    labels
  });
  return {
    runs: trajectory.runs,
    events: trajectory.events,
    decisions,
    labels,
    trajectory,
    measurement,
    summary: {
      decisionCount: decisions.length,
      labelCount: labels.length
    },
    diagnostics: [...trajectory.diagnostics, ...diagnostics, ...measurement.diagnostics]
  };
}
function runtimeBenchmarkRecordId(record2) {
  const parts = [
    nonEmptyString(record2.benchmark),
    nonEmptyString(record2.instanceId),
    nonEmptyString(record2.condition)
  ].filter((part) => part !== void 0);
  return parts.length > 0 ? parts.join(":") : void 0;
}
function runtimeBenchmarkScenarioId(record2) {
  return nonEmptyString(record2.instanceId);
}
function runtimeBenchmarkDecisionPoints(records, diagnostics) {
  const decisions = [];
  for (let recordIndex = 0; recordIndex < records.length; recordIndex += 1) {
    const record2 = records[recordIndex];
    const raw = record2.runtimeDecisionPoints;
    if (raw === void 0) continue;
    const recordId = runtimeBenchmarkRecordId(record2) ?? `record[${recordIndex}]`;
    if (!Array.isArray(raw)) {
      diagnostics.push(`${recordId}: runtimeDecisionPoints is not an array`);
      continue;
    }
    for (let pointIndex = 0; pointIndex < raw.length; pointIndex += 1) {
      const point = runtimeBenchmarkDecisionPoint(raw[pointIndex], {
        diagnostics,
        path: `${recordId}: runtimeDecisionPoints[${pointIndex}]`
      });
      if (!point) {
        diagnostics.push(
          `${recordId}: runtimeDecisionPoints[${pointIndex}] is not a RuntimeDecisionPoint`
        );
        continue;
      }
      decisions.push(point);
    }
  }
  return decisions;
}
function runtimeBenchmarkDecisionPoint(input, context) {
  if (!isRecord3(input)) return null;
  if (typeof input.id !== "string" || input.id.length === 0) return null;
  if (typeof input.runId !== "string" || input.runId.length === 0) return null;
  if (typeof input.stepIndex !== "number" || !Number.isInteger(input.stepIndex) || input.stepIndex < 0) {
    return null;
  }
  if (typeof input.kind !== "string" || input.kind.length === 0) return null;
  return {
    id: sanitizeString(input.id, MAX_STRING_LENGTH),
    runId: sanitizeString(input.runId, MAX_STRING_LENGTH),
    scenarioId: sanitizeOptionalString(input.scenarioId, MAX_STRING_LENGTH),
    stepIndex: input.stepIndex,
    kind: sanitizeString(input.kind, MAX_STRING_LENGTH),
    candidateActions: stringArray(input.candidateActions, {
      ...context,
      maxItems: MAX_CANDIDATE_ACTIONS,
      label: "candidateActions"
    }),
    context: sanitizeOptionalString(input.context, MAX_CONTEXT_LENGTH),
    evidence: runtimeBenchmarkEvidence(input.evidence, context),
    metadata: sanitizeMetadataRecord(input.metadata)
  };
}
function runtimeBenchmarkEvidence(input, context) {
  if (!Array.isArray(input)) return [];
  if (input.length > MAX_EVIDENCE_REFS) {
    context.diagnostics.push(`${context.path}: evidence truncated to ${MAX_EVIDENCE_REFS} refs`);
  }
  return input.slice(0, MAX_EVIDENCE_REFS).flatMap((item) => {
    if (!isRecord3(item)) return [];
    const source = sanitizeOptionalString(item.source, MAX_STRING_LENGTH);
    const id = sanitizeOptionalString(item.id, MAX_STRING_LENGTH);
    if (!source || !id) return [];
    return [
      {
        source,
        id,
        detail: sanitizeOptionalString(item.detail, MAX_EVIDENCE_DETAIL_LENGTH),
        metadata: sanitizeMetadataRecord(item.metadata)
      }
    ];
  });
}
function stringArray(input, context) {
  if (!Array.isArray(input)) return void 0;
  if (input.length > context.maxItems) {
    context.diagnostics.push(`${context.path}: ${context.label} truncated to ${context.maxItems}`);
  }
  const values = input.slice(0, context.maxItems).filter((value) => typeof value === "string" && value.length > 0).map((value) => sanitizeString(value, MAX_STRING_LENGTH));
  return values.length > 0 ? values : void 0;
}
function sanitizeMetadataRecord(metadata) {
  if (!isRecord3(metadata)) return void 0;
  const sanitized = sanitizeMetadata(metadata);
  if (!sanitized || typeof sanitized !== "object" || Array.isArray(sanitized)) return void 0;
  return sanitized;
}
function sanitizeMetadata(value, depth = 0) {
  if (value == null) return value;
  if (typeof value === "string") return sanitizeString(value, MAX_STRING_LENGTH);
  if (typeof value === "number" || typeof value === "boolean") return value;
  if (Array.isArray(value)) {
    if (depth >= MAX_METADATA_DEPTH) return "[MaxDepth]";
    return value.slice(0, MAX_METADATA_KEYS).map((item) => sanitizeMetadata(item, depth + 1));
  }
  if (!isRecord3(value)) return void 0;
  if (depth >= MAX_METADATA_DEPTH) return "[MaxDepth]";
  const sanitized = {};
  for (const [key, nested] of Object.entries(value).slice(0, MAX_METADATA_KEYS)) {
    sanitized[key] = SENSITIVE_KEY_RE.test(key) ? "[REDACTED]" : sanitizeMetadata(nested, depth + 1);
  }
  return sanitized;
}
function sanitizeOptionalString(value, maxLength) {
  return typeof value === "string" && value.length > 0 ? sanitizeString(value, maxLength) : void 0;
}
function sanitizeString(value, maxLength) {
  let sanitized = value;
  for (const pattern of SENSITIVE_VALUE_RES) {
    sanitized = sanitized.replace(pattern, "[REDACTED]");
  }
  sanitized = sanitized.replace(
    SENSITIVE_ASSIGNMENT_RE,
    (_match, key) => `${key}=[REDACTED]`
  );
  if (sanitized.length <= maxLength) return sanitized;
  return sanitized.slice(0, maxLength);
}
function isRecord3(value) {
  return typeof value === "object" && value !== null && !Array.isArray(value);
}
function nonEmptyString(value) {
  return typeof value === "string" && value.length > 0 ? value : void 0;
}

// src/belief-state/shadow-probe.ts
var DEFAULT_CONCURRENCY = 4;
var DEFAULT_MAX_CONTEXT_CHARS2 = 12e3;
async function runBeliefShadowProbe(options) {
  const concurrency = boundedInteger(options.concurrency ?? DEFAULT_CONCURRENCY, 1, 32);
  const records = [];
  const diagnostics = [];
  let next = 0;
  async function worker() {
    while (next < options.points.length) {
      const index = next;
      next += 1;
      const point = options.points[index];
      if (!point) continue;
      const result = await probePoint(point, options);
      records[index] = result.record;
      diagnostics.push(...result.diagnostics);
    }
  }
  await Promise.all(Array.from({ length: Math.min(concurrency, options.points.length) }, worker));
  const completed = records.filter((record2) => !!record2);
  return {
    probeId: options.probeId,
    records: completed,
    diagnostics,
    summary: summarizeShadowProbe(options.points.length, completed)
  };
}
function formatBeliefShadowProbePrompt(input) {
  return [
    "Return only JSON. Do not include chain-of-thought.",
    "Infer the agent belief state at this decision boundary using only the context below.",
    "",
    `decisionKind: ${input.decisionKind}`,
    `candidateActions: ${JSON.stringify(input.candidateActions)}`,
    input.observedAction ? `observedAction: ${JSON.stringify(input.observedAction)}` : "",
    input.context ? `context:
${input.context}` : "",
    "",
    "Schema:",
    JSON.stringify({
      predictedAction: "one candidate action",
      confidence: "number in [0,1]",
      beliefSummary: "short outcome-blind summary",
      uncertainty: ["short uncertainty"],
      evidenceRefs: ["evidence id"],
      wouldChangeMindIf: ["observable evidence"],
      targetProb: "optional number in [0,1]",
      qHat: "optional number in [0,1]"
    })
  ].filter(Boolean).join("\n");
}
async function probePoint(point, options) {
  const diagnostics = [];
  const candidateActions = uniqueStrings2(point.candidateActions ?? []);
  if ((options.requireCandidateActions ?? true) && candidateActions.length === 0) {
    diagnostics.push({
      decisionId: point.id,
      severity: "warning",
      reason: "missing candidateActions"
    });
    return { diagnostics };
  }
  let response;
  try {
    response = await options.probe({
      probeId: options.probeId,
      decisionId: point.id,
      runId: point.runId,
      scenarioId: point.scenarioId,
      stepIndex: point.stepIndex,
      decisionKind: point.kind,
      candidateActions,
      ...options.includeObservedAction ? { observedAction: point.chosenAction } : {},
      evidence: point.evidence.map((ref) => ({
        id: ref.id,
        source: ref.source,
        ...options.includeEvidenceDetail && ref.detail ? { detail: ref.detail } : {},
        ...ref.quality ? { quality: ref.quality } : {}
      })),
      context: trimText2(await options.contextOf?.(point), options.maxContextChars),
      metadata: await options.metadataOf?.(point)
    });
  } catch (error) {
    diagnostics.push({
      decisionId: point.id,
      severity: "error",
      reason: `probe threw: ${errorMessage2(error)}`
    });
    return { diagnostics };
  }
  const normalized = normalizeProbeResponse(response, {
    point,
    candidateActions,
    allowOutOfSetActions: options.allowOutOfSetActions ?? false
  });
  if (!normalized.record) {
    diagnostics.push(...normalized.diagnostics);
    return { diagnostics };
  }
  return {
    record: {
      probeId: options.probeId,
      decisionId: point.id,
      runId: point.runId,
      scenarioId: point.scenarioId,
      stepIndex: point.stepIndex,
      decisionKind: point.kind,
      candidateActions,
      observedAction: point.chosenAction,
      agreesWithObservedAction: normalized.record.predictedAction === point.chosenAction,
      ...options.includeOutcomeInRecord === false ? {} : { outcome: point.outcome },
      ...normalized.record
    },
    diagnostics
  };
}
function normalizeProbeResponse(response, options) {
  const diagnostics = [];
  const predictedAction = stringOrNull(response.predictedAction);
  if (!predictedAction) {
    diagnostics.push({
      decisionId: options.point.id,
      severity: "error",
      reason: "missing predictedAction"
    });
  } else if (!options.allowOutOfSetActions && options.candidateActions.length > 0 && !options.candidateActions.includes(predictedAction)) {
    diagnostics.push({
      decisionId: options.point.id,
      severity: "error",
      reason: `predictedAction ${predictedAction} is not in candidateActions`
    });
  }
  if (!isUnitProbability(response.confidence)) {
    diagnostics.push({
      decisionId: options.point.id,
      severity: "error",
      reason: `invalid confidence ${String(response.confidence)}`
    });
  }
  if (response.targetProb !== void 0 && !isUnitProbability(response.targetProb)) {
    diagnostics.push({
      decisionId: options.point.id,
      severity: "error",
      reason: `invalid targetProb ${String(response.targetProb)}`
    });
  }
  if (response.qHat !== void 0 && response.qHat !== null && !isUnitProbability(response.qHat)) {
    diagnostics.push({
      decisionId: options.point.id,
      severity: "error",
      reason: `invalid qHat ${String(response.qHat)}`
    });
  }
  if (diagnostics.length > 0 || !predictedAction) return { diagnostics };
  return {
    record: {
      predictedAction,
      confidence: response.confidence,
      ...response.beliefSummary ? { beliefSummary: trimText2(response.beliefSummary, 2e3) } : {},
      uncertainty: compactStrings(response.uncertainty),
      evidenceRefs: compactStrings(response.evidenceRefs),
      wouldChangeMindIf: compactStrings(response.wouldChangeMindIf),
      ...response.targetProb !== void 0 ? { targetProb: response.targetProb } : {},
      ...response.qHat !== void 0 ? { qHat: response.qHat } : {},
      ...response.metadata ? { metadata: response.metadata } : {}
    },
    diagnostics
  };
}
function summarizeShadowProbe(attempted, records) {
  const confidences = records.map((record2) => record2.confidence);
  const agreements = records.filter((record2) => record2.agreesWithObservedAction).length;
  return {
    attempted,
    completed: records.length,
    dropped: attempted - records.length,
    withOutcome: records.filter((record2) => record2.outcome !== void 0).length,
    withTargetProb: records.filter((record2) => record2.targetProb !== void 0).length,
    meanConfidence: confidences.length > 0 ? mean3(confidences) : null,
    observedAgreementRate: records.length > 0 ? agreements / records.length : null
  };
}
function isUnitProbability(value) {
  return typeof value === "number" && Number.isFinite(value) && value >= 0 && value <= 1;
}
function boundedInteger(value, min, max) {
  if (!Number.isFinite(value)) return min;
  return Math.max(min, Math.min(max, Math.floor(value)));
}
function compactStrings(values, maxItems = 12) {
  if (!Array.isArray(values)) return [];
  return values.filter((value) => typeof value === "string" && value.length > 0).slice(0, maxItems).map((value) => trimText2(value, 500) ?? "").filter(Boolean);
}
function uniqueStrings2(values) {
  return [...new Set(values.filter((value) => value.length > 0))];
}
function stringOrNull(value) {
  return typeof value === "string" && value.length > 0 ? value : null;
}
function trimText2(value, maxChars = DEFAULT_MAX_CONTEXT_CHARS2) {
  if (!value) return void 0;
  return value.length > maxChars ? value.slice(value.length - maxChars) : value;
}
function mean3(values) {
  return values.reduce((sum, value) => sum + value, 0) / values.length;
}
function errorMessage2(error) {
  return error instanceof Error ? error.message : String(error);
}
export {
  BELIEF_DECISION_KINDS,
  BELIEF_EVALUATION_CRITERIA,
  BELIEF_EVIDENCE_QUALITIES,
  BELIEF_EVIDENCE_SOURCES,
  analyzeBeliefDecisionCorpus,
  analyzeBeliefPolicy,
  beliefDecisionsToOffPolicyTrajectories,
  buildBeliefDecisionResearchEvidencePacket,
  buildCodeAgentBeliefEvidenceCorpus,
  buildRuntimeBeliefPhase0Measurement,
  buildRuntimeBenchmarkBeliefPhase0Measurement,
  calibrateBeliefDecisions,
  createBeliefRuntimeHookCollector,
  embeddedBeliefOpeTargetPolicy,
  evaluateBeliefOffPolicy,
  evaluateBeliefSelectivePolicy,
  extractBeliefDecisionPoints,
  extractCodeAgentBeliefDecisionPoints,
  formatBeliefShadowProbePrompt,
  inventoryBeliefDecisionPoints,
  isBeliefDecisionKind,
  isBeliefEvidenceSource,
  runBeliefShadowProbe,
  runtimeDecisionPointToBeliefDecisionPoint,
  runtimeDecisionPointToBeliefShadowProbeInput,
  selectBeliefDecisionTarget,
  thresholdSelectivePolicy
};
//# sourceMappingURL=index.js.map