feat(budget): self-calibrating cap — learn from observed limits (audit D4/F17) - #460
Merged
Merged
Conversation
…t D4/F17) Retire the 42M magic constant. When an account-wide claude limit trips (session_5h/global_weekly), the on_cooldown hook snapshots the estimator's trailing-window weighted usage as an observed cap sample (same units the estimator uses, so bias cancels). budget_status cap precedence: env override > learned median (>=2 samples) > 42M seed. Best-effort, one sample per episode. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Retires the 42M magic constant in the budget guard (audit finding F17, operator decision D4).
Problem: the cap was fit to a single observed limit hit — fragile, since plan-tier or weight changes silently invalidate it.
Fix: learn the cap from reality. When an account-wide claude limit trips (
session_5h/global_weekly), theon_cooldownhook snapshots the estimator's current trailing-window weighted usage — an observed sample of the real cap, measured in the same weighted-token units the estimator uses, so systematic estimator bias cancels out. Samples are stored in the usage store (best-effort, one per limit episode via a 1h recency guard; last 8 kept).budget_statuscap precedence is now: explicitOC_CLAUDE_BUDGET_CAP_WEIGHTEDenv override → learned median (≥2 samples, robust to a single anomalous event) → 42M cold-start seed.Changes:
usage_storegainsrecord_budget_cap_sample+learned_budget_cap;usage_budgetgains_resolve_cap/_learned_cap(lazy store import, best-effort);loop_bridge.on_cooldowncaptures the sample.Tests: 9 new (median/min-samples, recency guard, learned-vs-env precedence,
on_cooldownrecords forsession_5hbut notmodel_weekly). 38 pass; ruff + ty clean.🤖 Generated with Claude Code