import {
  readUtilityLearningSnapshot
} from "./chunk-PYXS46O7.js";
import {
  clamp01
} from "./chunk-TBBDFYXW.js";

// src/utility-runtime.ts
var RANKING_MULTIPLIER_LIMIT = 0.12;
var PROMOTION_THRESHOLD_DELTA_LIMIT = 0.07;
var PROMOTION_THRESHOLD_WEIGHT_FACTOR = 0.2;
function roundRuntimeValue(value) {
  return Math.round(value * 1e3) / 1e3;
}
function clampSigned(value, limit) {
  if (!Number.isFinite(value) || limit <= 0) return 0;
  return Math.max(-limit, Math.min(limit, value));
}
function learnedWeightFor(snapshot, target, decision) {
  const entry = snapshot.weights.find((weight) => weight.target === target && weight.decision === decision);
  return entry?.learnedWeight ?? 0;
}
async function loadUtilityRuntimeValues(options) {
  if (!options.memoryUtilityLearningEnabled || !options.promotionByOutcomeEnabled) return null;
  const snapshot = await readUtilityLearningSnapshot(options.memoryDir, options.utilityTelemetryDir);
  if (!snapshot) return null;
  return {
    rankingBoostMultiplier: roundRuntimeValue(
      1 + clampSigned(learnedWeightFor(snapshot, "ranking", "boost"), RANKING_MULTIPLIER_LIMIT)
    ),
    rankingSuppressMultiplier: roundRuntimeValue(
      1 + clampSigned(learnedWeightFor(snapshot, "ranking", "suppress"), RANKING_MULTIPLIER_LIMIT)
    ),
    promoteThresholdDelta: roundRuntimeValue(
      clampSigned(
        learnedWeightFor(snapshot, "promotion", "promote") * -PROMOTION_THRESHOLD_WEIGHT_FACTOR,
        PROMOTION_THRESHOLD_DELTA_LIMIT
      )
    ),
    demoteThresholdDelta: roundRuntimeValue(
      clampSigned(
        learnedWeightFor(snapshot, "promotion", "demote") * PROMOTION_THRESHOLD_WEIGHT_FACTOR,
        PROMOTION_THRESHOLD_DELTA_LIMIT
      )
    ),
    snapshotUpdatedAt: snapshot.updatedAt
  };
}
function applyUtilityRankingRuntimeDelta(delta, runtime, mode) {
  if (!runtime || !Number.isFinite(delta) || delta === 0) return delta;
  const multiplier = mode === "boost" ? runtime.rankingBoostMultiplier : runtime.rankingSuppressMultiplier;
  return roundRuntimeValue(delta * multiplier);
}
function applyUtilityPromotionRuntimePolicy(policy, runtime) {
  if (!runtime) return policy;
  return {
    ...policy,
    demotionValueThreshold: roundRuntimeValue(clamp01(policy.demotionValueThreshold + runtime.demoteThresholdDelta)),
    promotionValueThreshold: roundRuntimeValue(clamp01(policy.promotionValueThreshold + runtime.promoteThresholdDelta))
  };
}

export {
  loadUtilityRuntimeValues,
  applyUtilityRankingRuntimeDelta,
  applyUtilityPromotionRuntimePolicy
};
//# sourceMappingURL=chunk-FSFEQI74.js.map