feat(governor): add budget/turn/termination cap calculator - #4374
Conversation
Add a pure evaluateGovernorCaps(usage, limits) to the engine's governor module, a sibling to rate-limit.ts. It evaluates three independent cumulative ceilings — budget/cost, turns, and elapsed session time (termination) — and combines them into one verdict drawn from GOVERNOR_LEDGER_EVENT_TYPES (allowed / denied / kill_switch), never a parallel vocabulary. Pure and side-effect-free: no IO, no clock read (elapsed time and usage are caller-supplied), no state, no enforcement. Every numeric input is normalized so a non-finite, negative, or fractional value can never yield a NaN verdict or a negative remaining value. Re-exported from the package entrypoint alongside rate-limit. The fail-closed enforcement chokepoint that composes this with rate-limit and the non-convergence detector is separate, maintainer-owned work. Closes JSONbored#4288
|
Superagent didn't find any vulnerabilities or security issues in this PR. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #4374 +/- ##
=======================================
Coverage 93.95% 93.95%
=======================================
Files 400 401 +1
Lines 36820 36829 +9
Branches 13452 13456 +4
=======================================
+ Hits 34595 34604 +9
Misses 1569 1569
Partials 656 656
🚀 New features to boost your workflow:
|
|
Tip 🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩 ✅ Gittensory review result - approve/merge recommendedReview updated: 2026-07-09 17:15:54 UTC
✅ Suggested Action - Approve/Merge
Review summary Blockers
Nits — 5 non-blocking
Linked issue satisfactionAddressed Review context
Contributor next steps
Signal definitions
🟩 Safe / merged · 🟦 Advisory · 🟨 Held for review · 🟥 Blocked / closed 💰 Earn for open-source contributions like this. Gittensor lets GitHub contributors earn for the work they already do — register to start earning →. Checked by Gittensory, a quiet PR intelligence layer for OSS maintainers.
|
Summary
Add a pure budget/turn/termination cap calculator to the engine's local Governor module —
evaluateGovernorCaps(usage, limits)inpackages/gittensory-engine/src/governor/budget-cap.ts, a sibling torate-limit.ts(not built on top of it).Where
rate-limit.tsmeasures a rolling-window request rate that resets, these caps are cumulative, monotonic counters across a whole run:Each dimension is evaluated independently, then combined into one verdict drawn from
GOVERNOR_LEDGER_EVENT_TYPES(packages/gittensory-engine/src/governor-ledger.ts) rather than a parallel vocabulary:kill_switchwhen the termination ceiling is reached (a hard stop, and the most severe),deniedwhen a budget or turn ceiling is reached,allowedwhen everything is clear.It mirrors
rate-limit.ts's discipline exactly:nowMs), no state, no scheduling, no enforcement.finiteNonNegativeInt), so no input can produce a NaN verdict or a negative remaining value; the turn dimension is floored to whole iterations while budget/elapsed keep fractional precision.rate-limit.The fail-closed enforcement chokepoint that composes this with rate-limit and the non-convergence detector is separate, maintainer-owned work (#2340) — this issue builds one of those three calculators in isolation.
Scope
packages/), noblockedPaths, no secrets/private termsValidation
npm run typechecknpm run test:coverage(full unsharded suite)test/unit/governor-budget-cap.test.ts: per-dimension under/at/over cap, termination precedence, non-finite/negative normalization, and fractional-turn flooringexceededcomparison, and every verdict branch) — 100% statements/branches/functions on the new fileSafety
Closes #4288