feat(supervisor): gate quota on the real claude.ai usage % (OAuth usage endpoint) #243

Merged
chaim merged 1 commits from worktree-supervisor-usage-endpoint into main 2026-06-13 10:19:46 +00:00
Owner

רקע

חיים רצה לראות את אחוז-השימוש האמיתי (ה"29%" שמופיע ב-Claude Code/claude.ai). מחקר בתיעוד אנטרופיק + בדיקה חיה מצא endpoint לא-מתועד GET https://api.anthropic.com/api/oauth/usage שמחזיר בדיוק את אותם אחוזים (5-שעות / שבועי / שבועי-Opus) + זמני-איפוס.

מה ה-PR עושה (שכבת לוגיקה/נתון בלבד — host-side)

  • subscription_usage() — קורא token מ-~/.claude/.credentials.json ו-GET ל-/api/oauth/usage עם UA claude-code/* (חובה, אחרת 429). מחזיר {five_hour, seven_day, seven_day_opus, ...} או None.
  • quota_available() — עכשיו מעדיף את ה-endpoint: הדריינר מתחדש רק כשחלונות 5-שעות + שבועי + שבועי-Opus כולם <100% (המחלץ רץ Opus). מדויק יותר מבדיקת-ה-probe ורואה כל מגבלה כמו ה-UI. fallback לבדיקת claude -p הזעירה אם ה-endpoint לא זמין (הוא לא-רשמי, עלול להשתנות).
  • status מדפיס עכשיו את האחוזים החיים + זמני-איפוס.

אימות חי

  • subscription_usage()five_hour.utilization=37% (תאם ל-UI). quota_available()True. הסקריפט עובר ast.parse.

מה לא נכלל (שער-עיצוב)

הצגת האחוז על דף /operations היא שינוי-UI ויזואלי → חייב לעבור קודם דרך שער Claude Design (web-ui/AGENTS.md). נדחה לשלב נפרד דרך השער. ה-PR הזה הוא רק התשתית (host-side), בלי deploy.

Invariants

  • G1 — החלטת-החידוש נגזרת ממצב-המכסה האמיתי.
  • אין מסלול-בקרה מקביל.

🤖 Generated with Claude Code

## רקע חיים רצה לראות את אחוז-השימוש האמיתי (ה"29%" שמופיע ב-Claude Code/claude.ai). מחקר בתיעוד אנטרופיק + בדיקה חיה מצא endpoint **לא-מתועד** `GET https://api.anthropic.com/api/oauth/usage` שמחזיר בדיוק את אותם אחוזים (5-שעות / שבועי / שבועי-Opus) + זמני-איפוס. ## מה ה-PR עושה (שכבת לוגיקה/נתון בלבד — host-side) - **`subscription_usage()`** — קורא token מ-`~/.claude/.credentials.json` ו-GET ל-`/api/oauth/usage` עם UA `claude-code/*` (חובה, אחרת 429). מחזיר `{five_hour, seven_day, seven_day_opus, ...}` או `None`. - **`quota_available()`** — עכשיו מעדיף את ה-endpoint: הדריינר מתחדש רק כשחלונות 5-שעות + שבועי + שבועי-Opus **כולם <100%** (המחלץ רץ Opus). מדויק יותר מבדיקת-ה-probe ורואה כל מגבלה כמו ה-UI. **fallback** לבדיקת `claude -p` הזעירה אם ה-endpoint לא זמין (הוא לא-רשמי, עלול להשתנות). - **`status`** מדפיס עכשיו את האחוזים החיים + זמני-איפוס. ## אימות חי - `subscription_usage()` → `five_hour.utilization=37%` (תאם ל-UI). `quota_available()` → `True`. הסקריפט עובר `ast.parse`. ## מה *לא* נכלל (שער-עיצוב) הצגת האחוז על דף `/operations` היא **שינוי-UI ויזואלי** → חייב לעבור קודם דרך שער Claude Design (`web-ui/AGENTS.md`). נדחה לשלב נפרד דרך השער. ה-PR הזה הוא רק התשתית (host-side), בלי deploy. ## Invariants - **G1** — החלטת-החידוש נגזרת ממצב-המכסה האמיתי. - אין מסלול-בקרה מקביל. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
chaim added 1 commit 2026-06-13 10:19:41 +00:00
feat(supervisor): read real claude.ai usage % from OAuth endpoint for quota gating
All checks were successful
G12 Leak-Guard / leak-guard (pull_request) Successful in 6s
9e46db3c48
The supervisor's quota check used a tiny `claude -p` probe to decide whether the
claude.ai subscription had room. That works but is indirect (an Opus-adjacent
round trip) and only answers yes/no. Anthropic exposes the actual utilization —
the same 5-hour / weekly / weekly-Opus percentages the Claude Code status bar
shows — via the (undocumented) GET /api/oauth/usage endpoint.

- subscription_usage(): reads the OAuth token from ~/.claude/.credentials.json
  and GETs /api/oauth/usage with the required `claude-code/*` User-Agent (without
  it the request hits an aggressively rate-limited bucket and 429s). Returns the
  parsed {five_hour, seven_day, seven_day_opus, ...} or None on any failure.
- quota_available(): now prefers the endpoint — a drain run resumes only when the
  5-hour, weekly, AND weekly-Opus windows are all <100% (the extractor runs Opus).
  More precise than the probe and sees every limit the way the UI does. Falls
  back to the `claude -p` probe when the endpoint is unreachable (it's
  undocumented and may change).
- `status` subcommand now prints the live percentages + reset times.

Note: this is the data/logic layer only. Surfacing the % on the /operations
page is a visual UI change and must go through the Claude Design gate first
(web-ui/AGENTS.md) — deferred.

Invariants: G1 (resume decision driven by the authoritative usage state).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
chaim merged commit d093319ffd into main 2026-06-13 10:19:46 +00:00
chaim deleted branch worktree-supervisor-usage-endpoint 2026-06-13 10:19:47 +00:00
Sign in to join this conversation.
No Reviewers
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: ezer-mishpati/legal-ai#243