Skip to content

feat(bin): read live ChatGPT quota and show both pools on the bearings board - #3

Merged
TastyTom13 merged 7 commits into
mainfrom
fm/fm-gpt-usage-visibility
Aug 31, 2026
Merged

TastyTom13 merged 7 commits into
mainfrom
fm/fm-gpt-usage-visibility

Conversation

@TastyTom13

Copy link
Copy Markdown
Owner

Intent

Make the ChatGPT subscription's usage visible live, the way the Claude pool already is, and put it where the owner can see it. Three parts were asked for.

PART 1 - INVESTIGATE how usage/limits can be read for this account: (a) does the Codex backend return rate-limit headers or a usage payload on responses; (b) is there a usage endpoint the stored OAuth can call (research OpenAI's documented surfaces, do not guess); (c) failing both, a local tally counting our own Pi requests and tokens per window from Pi's session logs. Report which of a/b/c is real with evidence.

Findings, all probed empirically on 2026-08-31 and recorded in docs/verification/gpt-quota.md:

  • (a) IS REAL and is what shipped. POST https://chatgpt.com/backend-api/codex/responses attaches 14 x-codex-* rate-limit headers to the response, carrying used-percent, window length in minutes, and reset instant for a primary and a secondary window.
  • (b) IS NOT REAL. GET /backend-api/codex/usage, /me, /accounts/check/v4-2023-04-27 and /codex/rate_limits each returned HTTP 403 with an HTML body, while the same stored OAuth credential returned HTTP 200 from /backend-api/wham/tasks in the same run - so the 403s are those routes refusing the client, not a bad credential. There is no usage endpoint this credential can call.
  • (c) was deliberately NOT implemented. It would measure this home's own spend rather than the account's remaining allowance, which is a different question, and (a) answers the asked question directly. The reader reserves estimate: true for a future source of that kind.

PART 2 - BUILD the reader as a small script producing the same shape quota-axi prints for Claude (percent remaining, window, reset time) from the best available source, clearly marking estimates as estimates.

bin/fm-gpt-quota.sh does this. Key deliberate decisions a reviewer reading only the diff would not know:

  • The probe sends a supported model with NO input field. The backend validates the model, attaches the rate-limit headers, and rejects with HTTP 400 missing_required_parameter BEFORE invoking any model. This was chosen specifically so the reading costs zero tokens; two successive probes both read used-percent 6, verified.
  • A request with an unknown or absent model returns 400 with ZERO x-codex-* headers (model validation runs first), and GET on the same path returns 405 with zero headers. Both were probed. That is why the probe model is named, is configurable via FM_GPT_QUOTA_MODEL, and why the reader reports no_headers naming the model rather than failing opaquely.
  • The access token is passed to curl through a config on stdin, never argv, so it cannot be read out of the process table. A test asserts this.
  • Every failure path (missing credential, expired token, unreachable backend, header-less response) exits 0 with status=unavailable and a concrete reason plus remedy, rather than failing the caller. This is intentional: a board build or digest must keep working with the pool marked unreadable.
  • An expired token is named from the stored expiry BEFORE any request is sent, so a known-dead credential does not spend a probe.
  • A window the account does not have arrives as 0 minutes with an empty reset; it is dropped rather than rendered as a 100%-free window, because inventing headroom is worse than showing one window.
  • The headline figure is the TIGHTEST window, since that is all the account can actually spend.
  • quota-axi 0.1.34 was verified to report "Codex quota unavailable" on this home, which is the gap this closes; the new reader does not replace or wrap quota-axi for any other provider.

PART 3 - SURFACE it: wire the reading into the bearings fleet board as a small always-visible usage gauge section, extending .agents/skills/bearings/assets/board-template.html and bin/fm-bearings-board.sh's payload contract, keeping fm-bearings-board.v1 compatibility or versioning it properly, showing BOTH pools: Claude from quota-axi and GPT from the new reader.

  • fm-bearings-board.v1 was KEPT, not versioned up. The new pools field is additive and optional: a payload that omits it stays valid and the board simply renders no gauge. That was the deliberate compatibility choice rather than minting a v2.
  • bin/fm-quota-pools.sh is a new small script that maps the two unrelated reader schemas (quota-axi for Claude, fm-gpt-quota.sh for ChatGPT) onto that one board shape. It exists so the bearings composer's per-invocation work stays "run this, splice the array in", matching fm-bearings-board.sh's existing principle that the agent never authors board data mechanics at invocation time.
  • An unreadable pool stays in the array with its reason and renders as "unreadable" with that reason on the board, never as an empty or full bar and never omitted - a wrong or vanishing gauge is worse than an absent one.
  • A pool whose source says estimate: true is labelled an estimate on the gauge, so a measured-spend number can never be read as the account's remaining allowance.
  • The bearings SKILL.md tells the composer to fill pools from bin/fm-quota-pools.sh and to omit the field entirely if that command fails.

VERIFICATION DONE: 21 new colocated tests (tests/fm-gpt-quota.test.sh, tests/fm-quota-pools.test.sh, plus gauge-render and payload-validation cases in the two existing bearings board suites), all passing, all driving real executables with fakes and no network. bin/fm-lint.sh and bin/fm-doc-audience-check.sh clean. A real end-to-end board was built from live readings (Claude 70% left, ChatGPT 94% left) and opened in Chrome to confirm the gauge renders correctly.

KNOWN PRE-EXISTING FAILURE, NOT INTRODUCED BY THIS CHANGE: tests/fm-bearings-board.test.sh's test_registration_cannot_consume_before_any_origin_binding fails on this machine with "cannot claim source: lavish-...". This was proved pre-existing by extracting the repo at HEAD with git archive and reproducing the identical failure there. It was deliberately left alone.

CONSTRAINTS: firstmate's shared tracked material ships through this pipeline. Push the branch to the fork remote https://github.com/TastyTom13/firstmate and open the PR there with base main; this machine cannot push to kunchenguid/firstmate. .agents/skills/firstmate-coding-guidelines/SKILL.md governs the repo style (one sentence per line in tracked Markdown, plain dash never an em dash, no agent co-author, shellcheck-clean bin scripts, colocated tests named .test.sh, tests must exercise behavior through an executable interface and never assert implementation source bytes, and a maintainer-verification record under docs/verification/ must be registered in docs/documentation-audiences.json).

FOLLOW-UP CONTEXT FOR THIS RUN: an earlier run on this same branch already completed review (clean, zero findings after three fix rounds), test, document and lint, and its four fix commits are now on the branch: c17b580 validates quota headers separately and guards empty reader output, 04cf7b0 rejects out-of-range pool percents and zero-padded windows, be5b0d4 names refused credentials and hides the empty fuel gauge, 13b8453 lists the new quota scripts in the bin toolbelt inventory. That run failed only at push, because it had cached the pre-swap push URL; the machine's GitHub account can push to https://github.com/TastyTom13/firstmate.git (verified with a dry-run push) but is denied on kunchenguid/firstmate. Nothing about the code was in question.

What Changed

  • Adds bin/fm-gpt-quota.sh, which reads the ChatGPT subscription's remaining allowance from the x-codex-* rate-limit headers the Codex backend attaches to a zero-token probe request (a named, FM_GPT_QUOTA_MODEL-configurable model with no input field, rejected with HTTP 400 before any model runs). It reports the tightest window as the headline, drops windows the account does not have, passes the access token to curl via stdin config rather than argv, and exits 0 with status=unavailable plus a reason and remedy on every failure path, including a stored expiry that is checked before a probe is spent. Evidence for the probe behaviour and for the 403-refusing /backend-api/codex/usage, /me, /accounts/check/v4-2023-04-27 and /codex/rate_limits routes is recorded in the new docs/verification/gpt-quota.md, registered in docs/documentation-audiences.json.
  • Adds bin/fm-quota-pools.sh, which maps the two unrelated reader schemas (quota-axi for Claude, fm-gpt-quota.sh for ChatGPT) onto one board pool shape, keeps an unreadable pool in the array with its reason, and marks any source reporting estimate: true as an estimate.
  • Extends bin/fm-bearings-board.sh and the board template with an optional additive pools field on the existing fm-bearings-board.v1 contract, rendering a usage gauge per pool (unreadable pools show their reason, estimates are labelled, an empty gauge is hidden); payloads omitting pools stay valid. The bearings SKILL.md directs the composer to fill pools from bin/fm-quota-pools.sh and omit the field if it fails. Covered by new tests/fm-gpt-quota.test.sh and tests/fm-quota-pools.test.sh plus gauge-render and payload-validation cases added to the two existing bearings board suites.

Risk Assessment

✅ Low: The change is additive and fail-open by design: the new readers exit 0 with a reported reason on every failure path traced, the pools board field is optional so an omitting payload stays a valid fm-bearings-board.v1, and the only defect found is an inaccurate diagnostic string on one uncommon HTTP path.

Testing

Ran the four focused suites covering the change (fm-gpt-quota, fm-quota-pools, fm-bearings-board-render, plus the new pools payload-validation case from fm-bearings-board) and all pass. Went beyond unit level: read the live ChatGPT subscription through bin/fm-gpt-quota.sh (94% remaining on the 30-day window, sourced from real x-codex-* response headers), mapped it with bin/fm-quota-pools.sh alongside a live Claude reading, built a real bearings board from that payload, and captured browser screenshots showing both fuel gauges rendering with percentage, window and reset date. Two further screenshots cover the unreadable-pool state (renders "unreadable" with its reason, not an empty bar) and the pools-omitted state (gauge row absent with no leftover gap, confirming the .bb-fuel[hidden] fix in a way the DOM-level test cannot). Two test failures surfaced - test_registration_cannot_consume_before_any_origin_binding in tests/fm-bearings-board.test.sh and the reconcile-claim case in tests/fm-procevent.test.sh - and both reproduce identically on a clean extraction of base commit 6e738d5, so they are pre-existing machine-environment failures unrelated to this change (the fm-procevent test edits in range come from upstream commit 4c04113, not from this feature). All temporary homes and payloads were removed and the worktree is clean.

Evidence: Live CLI transcript of both quota readers

Source: Live CLI transcript of both quota readers

$ bin/fm-gpt-quota.sh quota[1]{provider,scope,percentRemaining,window,resetsAt,source,estimate}: openai-codex,primary,94,"30 day","2026-09-30T21:37:52Z",response_headers,false $ bin/fm-quota-pools.sh | jq . [ {"provider":"claude","label":"Claude","percent_remaining":66,"window":"session","resets_at":"2026-09-01T02:19:59.743550+00:00","estimate":false,"note":null}, {"provider":"openai-codex","label":"ChatGPT","percent_remaining":94,"window":"30 day","resets_at":"2026-09-30T21:37:52Z","estimate":false,"note":null} ]

$ bin/fm-gpt-quota.sh
bin: bin/fm-gpt-quota.sh
description: Report the ChatGPT (Codex) subscription quota window for this home
generatedAt: "2026-08-31T22:37:54Z"
quota[1]{provider,scope,percentRemaining,window,resetsAt,source,estimate}:
  openai-codex,primary,94,"30 day","2026-09-30T21:37:52Z",response_headers,false

$ bin/fm-quota-pools.sh | jq .
[
  {
    "provider": "claude",
    "label": "Claude",
    "percent_remaining": 66,
    "window": "session",
    "resets_at": "2026-09-01T02:19:59.743550+00:00",
    "estimate": false,
    "note": null
  },
  {
    "provider": "openai-codex",
    "label": "ChatGPT",
    "percent_remaining": 94,
    "window": "30 day",
    "resets_at": "2026-09-30T21:37:52Z",
    "estimate": false,
    "note": null
  }
]

Pipeline

Updates from git push no-mistakes

✅ **intent** - passed

✅ No issues found.

✅ **Rebase** - passed

✅ No issues found.

⚠️ **Review** - 1 info
  • ℹ️ bin/fm-gpt-quota.sh:229 - A non-auth backend failure is reported with a remedy that names the wrong cause. Concrete path: the Codex backend (or a proxy in front of it) answers HTTP 502 with an HTML body and zero x-codex-* headers. curl exits 0, so the unreachable branch at line 197 is skipped; both append_window calls drop their window, so windows stays []; http_code is 502, so the 401/403 case at line 223 does not match; execution falls through to line 229 and emits detail "the Codex backend answered without usable rate-limit headers" with remedy "check that gpt-5.5 is still accepted for this account". bin/fm-quota-pools.sh:123 copies that detail into the pool note, and the board gauge shows it verbatim, so the captain is told to check the probe model when the backend was simply erroring. The status line is already captured in http_code at line 204 and then discarded on this path. Smallest honest remedy: include the observed HTTP code in the no_headers detail, and for a 5xx code say the backend errored rather than pointing at the model. This corrects an existing label using state the script already has; it adds no new state, request, or subsystem. Same class as the 401/403 mislabel already fixed in review round 3, but for the remaining codes.
✅ **Test** - passed

✅ No issues found.

  • bash tests/fm-gpt-quota.test.sh (16 cases, all pass)
  • bash tests/fm-quota-pools.test.sh (7 cases, all pass)
  • bash tests/fm-bearings-board-render.test.sh (all pass, including the 5 new gauge-render cases)
  • test_pools_are_optional_and_validated_when_present from tests/fm-bearings-board.test.sh, run in isolation because the suite aborts earlier at the pre-existing failure (passes)
  • bash tests/fm-procevent.test.sh (fails at reconcile never claimed the registered source)
  • Pre-existing-failure proof: git archive 6e738d5 | tar -x -C /tmp/fmbase then re-ran test_registration_cannot_consume_before_any_origin_binding and tests/fm-procevent.test.sh at base - identical failures
  • Live end-to-end read: bin/fm-gpt-quota.sh and bin/fm-gpt-quota.sh --json against the real Codex backend (status known, source response_headers, 94% remaining)
  • Live end-to-end read: bin/fm-quota-pools.sh producing both pools from quota-axi and the new reader
  • Manual board build: bin/fm-bearings-board.sh build <payload with live pools> in a temp FM_HOME, then screenshotted the rendered page in Chrome
  • Manual board build for the unreadable-ChatGPT-pool state and for a payload omitting pools entirely, each screenshotted
✅ **Document** - passed

✅ No issues found.

✅ **Lint** - passed

✅ No issues found.

✅ **Push** - passed

✅ No issues found.

kunchenguid and others added 7 commits September 1, 2026 00:32
* fix(bin): keep typed Lavish comments when an element is also annotated

read preferred element text over prompt, so an annotate-and-comment
item dropped the captain's words. Surface prompt as its own field.

Co-authored-by: Cursor <[email protected]>

* no-mistakes(review): Filter non-comment prompts from Lavish reader output

* no-mistakes(document): Clarify Lavish comment presentation contract

* no-mistakes(ci): Fixed Lavish reader comment provenance: non-choice prompts are now emitted even when identical to element text. Added observable regression coverage for identical selector+comment input while retaining pure annotation/message coverage. Reader cases, bash syntax, and diff checks pass. Full fm-procevent suite stops earlier at unrelated “reconcile never claimed” setup failure

* no-mistakes(ci): Fixed duplicate pure-annotation prompts by emitting `prompt:` only when it differs from captured element text. Updated behavioral coverage for selector+comment, pure annotation, and pure message cases. Focused reader regressions, syntax checks, and diff checks pass. Full suite remains blocked by the pre-existing “reconcile never claimed the registered source” failure

* fix(bin): always emit Lavish comments and use real annotation fixtures

Stop inferring comment provenance from prompt==text. Real pure
annotations have no prompt, so always-emit does not duplicate.

Co-authored-by: Cursor <[email protected]>

---------

Co-authored-by: Cursor <[email protected]>
…n the board

quota-axi reports "Codex quota unavailable" on a home whose only OpenAI
credential is an OAuth ChatGPT account with no codex CLI, because there is no
local usage file for it to read. The account's own quota is not missing though:
the Codex responses endpoint attaches it to every response as x-codex-* headers.

bin/fm-gpt-quota.sh reads it from there with a request that names a supported
model and carries no input, so the backend validates the model, attaches the
headers, and rejects the call before invoking anything. The reading is the
provider's own enforced figure and costs no tokens. Every failure - missing
credential, expired token, unreachable backend, a response without the headers -
is reported with its concrete reason instead of guessed at, and the token
reaches the request through a curl config on stdin rather than argv.

bin/fm-quota-pools.sh maps that reader and quota-axi onto one board-shaped
array, and the bearings board renders it as an always-visible fuel gauge: one
bar per subscription with what is left, the window, and when it resets. A pool
that cannot be read shows as unreadable with its reason rather than as an empty
bar, and a source that measures our own spend rather than the account's
allowance is labelled an estimate on the gauge.

The board payload keeps fm-bearings-board.v1: `pools` is additive and optional,
so a payload without it stays valid and simply renders no gauge.

docs/verification/gpt-quota.md records the probed vendor behaviour this rests
on, including the two ways the probe loses its headers.
@TastyTom13
TastyTom13 merged commit 0c08e70 into main Aug 31, 2026
13 checks passed
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.

2 participants