// src/graph-edge-reinforcement.ts
var DEFAULT_REINFORCE_DELTA = 0.05;
var CONFIDENCE_CEILING = 1;
var DEFAULT_DECAY_WINDOW_MS = 90 * 24 * 60 * 60 * 1e3;
var DEFAULT_DECAY_FLOOR = 0.1;
var DEFAULT_DECAY_PER_WINDOW = 0.1;
function readEdgeConfidence(edge) {
  const raw = edge.confidence;
  if (raw === void 0 || raw === null || !Number.isFinite(raw)) {
    return CONFIDENCE_CEILING;
  }
  if (raw < 0) return 0;
  if (raw > CONFIDENCE_CEILING) return CONFIDENCE_CEILING;
  return raw;
}
function readLastReinforcedAt(edge) {
  return edge.lastReinforcedAt ?? edge.ts;
}
function reinforceEdge(edge, now, delta = DEFAULT_REINFORCE_DELTA) {
  const current = readEdgeConfidence(edge);
  const safeDelta = Number.isFinite(delta) && delta >= 0 ? delta : 0;
  const next = Math.min(CONFIDENCE_CEILING, Math.max(0, current + safeDelta));
  return {
    ...edge,
    confidence: next,
    lastReinforcedAt: now
  };
}
function decayEdgeConfidence(edge, now, opts = {}) {
  const windowMs = opts.windowMs ?? DEFAULT_DECAY_WINDOW_MS;
  const perWindow = opts.perWindow ?? DEFAULT_DECAY_PER_WINDOW;
  const floor = opts.floor ?? DEFAULT_DECAY_FLOOR;
  if (!(windowMs > 0) || !Number.isFinite(perWindow) || perWindow < 0 || !Number.isFinite(floor)) {
    return { ...edge, confidence: readEdgeConfidence(edge) };
  }
  const safeFloor = Math.min(CONFIDENCE_CEILING, Math.max(0, floor));
  const nowMs = Date.parse(now);
  const refMs = Date.parse(readLastReinforcedAt(edge));
  if (!Number.isFinite(nowMs) || !Number.isFinite(refMs)) {
    return { ...edge, confidence: readEdgeConfidence(edge) };
  }
  const age = nowMs - refMs;
  const current = readEdgeConfidence(edge);
  if (age <= windowMs) {
    return { ...edge, confidence: current };
  }
  const windowsPast = Math.ceil((age - windowMs) / windowMs);
  const decayed = current - perWindow * windowsPast;
  const lowerBound = Math.min(safeFloor, current);
  const next = Math.max(lowerBound, Math.min(current, decayed));
  const newRefMs = refMs + windowsPast * windowMs;
  const newRef = new Date(newRefMs).toISOString();
  return { ...edge, confidence: next, lastReinforcedAt: newRef };
}

export {
  DEFAULT_REINFORCE_DELTA,
  CONFIDENCE_CEILING,
  DEFAULT_DECAY_WINDOW_MS,
  DEFAULT_DECAY_FLOOR,
  DEFAULT_DECAY_PER_WINDOW,
  readEdgeConfidence,
  readLastReinforcedAt,
  reinforceEdge,
  decayEdgeConfidence
};
//# sourceMappingURL=chunk-2LSZVONP.js.map