Skip to content

feat(budget): self-calibrating cap — learn from observed limits (audit D4/F17) - #460

Merged
ProtocolWarden merged 1 commit into
mainfrom
fix/self-calibrating-budget-cap
Jul 14, 2026
Merged

feat(budget): self-calibrating cap — learn from observed limits (audit D4/F17)#460
ProtocolWarden merged 1 commit into
mainfrom
fix/self-calibrating-budget-cap

Conversation

@ProtocolWarden

Copy link
Copy Markdown
Owner

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), the on_cooldown hook 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_status cap precedence is now: explicit OC_CLAUDE_BUDGET_CAP_WEIGHTED env override → learned median (≥2 samples, robust to a single anomalous event) → 42M cold-start seed.

Changes: usage_store gains record_budget_cap_sample + learned_budget_cap; usage_budget gains _resolve_cap/_learned_cap (lazy store import, best-effort); loop_bridge.on_cooldown captures the sample.

Tests: 9 new (median/min-samples, recency guard, learned-vs-env precedence, on_cooldown records for session_5h but not model_weekly). 38 pass; ruff + ty clean.

🤖 Generated with Claude Code

…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>
@ProtocolWarden
ProtocolWarden merged commit fc61108 into main Jul 14, 2026
25 checks passed
@ProtocolWarden
ProtocolWarden deleted the fix/self-calibrating-budget-cap branch July 14, 2026 01:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant