UNPKG

pi-lens

Version:

Real-time code feedback for pi — LSP, linters, formatters, type-checking, structural analysis & booboo

85 lines (82 loc) • 2.64 kB
import { createRequire as __pilensCreateRequire } from "node:module"; const require = __pilensCreateRequire(import.meta.url); import { incrementDegradationCount } from "./chunk-N3YQJI6O.js"; import { logLatency } from "./chunk-5THXD6X5.js"; // dist/clients/bounded-telemetry.js var MAX_TRACKED_TURN_CAPS = 64; var turnCounts = /* @__PURE__ */ new Map(); var latestCountedTurnIndex; var retiredThroughTurnIndex = Number.NEGATIVE_INFINITY; function admitBounded(phase, identity, options = {}) { try { let isRisingEdge = true; if (options.ledgerKind !== void 0) { isRisingEdge = incrementDegradationCount({ kind: options.ledgerKind, subject: identity, reason: options.reason ?? phase }); } if (options.risingEdgePer !== void 0 && !isRisingEdge) return false; return admitAgainstTurnCap(phase, options.capPerTurn); } catch { return true; } } function emitBounded(phase, identity, payload, options = {}) { try { if (!admitBounded(phase, identity, options)) return false; logLatency({ ...payload, type: payload.type ?? "phase", phase, // `filePath` is what existing latency.log queries filter on, so a site // may keep a coarser one; `metadata.identity` below always carries the // full discriminating identity. Aggregation that loses WHICH subject // is stuck is the failure AGENTS.md names. filePath: payload.filePath ?? identity, metadata: { ...payload.metadata, identity } }); return true; } catch { return false; } } function admitAgainstTurnCap(phase, cap) { if (cap === void 0) return true; if (cap.turnIndex <= retiredThroughTurnIndex) return false; let counts = turnCounts.get(cap.turnIndex); if (!counts) { if (turnCounts.size >= MAX_TRACKED_TURN_CAPS) { const oldestTrackedTurn = Math.min(...turnCounts.keys()); if (cap.turnIndex < oldestTrackedTurn) return false; turnCounts.delete(oldestTrackedTurn); retiredThroughTurnIndex = Math.max(retiredThroughTurnIndex, oldestTrackedTurn); } counts = /* @__PURE__ */ new Map(); turnCounts.set(cap.turnIndex, counts); } latestCountedTurnIndex = Math.max(latestCountedTurnIndex ?? cap.turnIndex, cap.turnIndex); const used = counts.get(phase) ?? 0; if (used >= cap.limit) return false; counts.set(phase, used + 1); return true; } function resetBoundedTelemetry() { turnCounts = /* @__PURE__ */ new Map(); latestCountedTurnIndex = void 0; retiredThroughTurnIndex = Number.NEGATIVE_INFINITY; } export { emitBounded, resetBoundedTelemetry };