feat: Claude usage threshold gate#52
Merged
Merged
Conversation
Adds a per-user threshold gate over the existing Anthropic OAuth usage poller
(agent/src/usage-poller.ts → hub/src/usage/store.ts) so remo-code pauses NEW
dispatches when 5-hour or 7-day utilization crosses a user-configured cap.
Architecture review:
.planning/phases/claude-usage-thresholds/00-architecture-review.md
Schema (idempotent, back-compat):
- users.claude_session_threshold_pct INT NULL (1-100, NULL = OFF)
- users.claude_week_threshold_pct INT NULL (1-100, NULL = OFF)
- scheduled_task_runs.status += 'skipped_quota' (CHECK constraint rebuilt)
Gate sites (all 4 dispatch entry points per review §3):
1. hub/src/scheduler/dispatcher.ts — fireTask() + queue-promotion path
2. hub/src/sessions/routing.ts — pickSessionTarget returns kind:'quota_blocked'
3. hub/src/error-capture/dispatcher.ts — dispatchPendingError head
4. hub/src/ws/client.ts — send_message handler (returns send_refused)
Refusal shape (uniform across HTTP + WS):
{ error: 'quota_threshold_reached', reason: 'session_threshold'|'week_threshold',
utilization_pct, threshold_pct, resets_at }
Decision precedence: session_threshold wins ties (soonest reset). Opus carve-out
counts toward week_threshold. NULL thresholds OR null snapshot = fail-open.
API (plain Hono, JWT-authed):
- GET /api/account/claude-thresholds → { session_pct, week_pct }
- PUT /api/account/claude-thresholds body strict-validated, both nullable
- GET /api/account/usage → { usage, thresholds, paused, reason, resets_at }
UI: Settings → Profile → new ClaudeUsageCard. Two per-cap toggles + 50-95 sliders.
Inline 'Paused' banner when current snapshot trips a saved threshold. Suggested
default value is 90 but column stays NULL until the user clicks Save.
In-flight runs are NOT killed — only NEW dispatch is refused. Waiter promotion
in session-queue re-evaluates the gate and drops with skipped_quota if still
over.
Tests:
- hub/test/threshold.test.ts (10 cases): back-compat null, fail-open, session-over,
week-over, both-over precedence, boundary >=, opus carve-out, single-cap modes.
- Full suite: 256 pass / 0 fail / 56 skip (e2e gated by REMO_E2E_DB_URL).
Out of scope (deferred per review §1/§4):
- ccusage fallback (the OAuth endpoint is authoritative; no new dep)
- Per-agent usage endpoint (usage is per-user / per-subscription)
- Token-counts hover (not in OAuth payload)
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.
Summary
Adds a per-user Anthropic usage threshold gate over the existing OAuth usage poller. When 5h or 7d utilization crosses a user-configured percentage, remo-code pauses NEW dispatches at all 4 entry points (scheduler, pickSessionTarget, error-capture, manual chat) until the window resets. In-flight runs are NOT killed.
Architecture:
.planning/phases/claude-usage-thresholds/00-architecture-review.mdKey amendments from review (all applied):
/api/oauth/usagepoller; ccusage deferred to future opt-in PRscheduled_task_runs.status = 'skipped_quota'Schema
Refusal shape (HTTP 503 + WS
send_refused){ "error": "quota_threshold_reached", "reason": "session_threshold", "utilization_pct": 92, "threshold_pct": 90, "resets_at": "2026-05-26T05:00:00Z" }Tests
hub/test/threshold.test.ts— 10 cases (back-compat NULL, fail-open, session-over, week-over, both-over precedence, boundary>=, opus carve-out, single-cap modes)bun run build:webcleanTest plan
zewfc6g9dw3c4h88z2jd2o4g{session_pct: 90, week_pct: null}persistssend_refusedstatus='skipped_quota'🤖 Generated with Claude Code