Skip to content

feat(core): report Command Code quota to Oh My Pi usage surfaces - #120

Merged
pierreraby merged 3 commits into
patlux:mainfrom
aarisr:feat/omp-usage-provider
Sep 27, 2026
Merged

pierreraby merged 3 commits into
patlux:mainfrom
aarisr:feat/omp-usage-provider

Conversation

@aarisr

@aarisr aarisr commented Sep 26, 2026

Copy link
Copy Markdown
Contributor

Problem

createProviderConfig registered name, baseUrl, apiKey, api, streamSimple, headers, oauth and models — and no usage field. Oh My Pi resolves usage providers from that field, so it had none for commandcode: usageProviderFor("commandcode") stayed undefined, the stored credentials were treated as "a provider with no usage concept", and the quota this provider already fetches for /commandcode-quota was unreachable from OMP's usage surfaces.

With OMP 18.2.6 on the same account, omp usage --provider commandcode was the only way to see the credentials at all, and none of them reported anything:

Usage

Commandcode — 4 accounts
  ○ OAuth account — no usage data
  ○ OAuth account — no usage data
  ○ OAuth account — no usage data
  ○ API key — no usage data

Change

pi.registerProvider("commandcode", …) now carries usage: UsageProvider (src/usage.ts), whose fetchUsage reuses fetchCommandCodeQuota from src/quota.ts — the same four alpha endpoints and the same credential resolution /commandcode-quota uses — instead of adding a second HTTP path.

Command Code UsageReport
credits.windowLimits.fiveHour limit 5h / "5 Hour", window 5h (5 h)
credits.windowLimits.weekly limit 7d / "Weekly", window 7d (7 d)
credits.remainingCredits + summary.totalCost limit credits / "Credits", unit usd, used = billing-period spend, limit = remaining + spend, remaining = remaining
subscription.planId / status metadata.planType / metadata.planStatus
account.login metadata.accountId, limits' scope.accountId

Rules the mapping follows:

  • Sections the API did not report stay absent. quota.unavailable becomes the report's notes, and those sections produce no limits: with billing/credits down there is no window row and no credit row; with usage/summary down the credit row carries remaining and no limit/used, because the pool is remaining + spend and the spend is unknown. Nothing is zero-filled, which is what the provider README requires.
  • No report rather than a fabricated one. A rejected key, an unreachable endpoint or an unrecognized schema returns null.
  • Fractions are published explicitly, derived from the reported used/limit and clamped like the built-in providers do, because OMP's session and ACP /usage text renderer reads amount.usedFraction without resolving the pair. Without them a window renders as 0.00 used with an empty bar. They are set only where both inputs are known.
  • metadata.orgId is deliberately unset: OMP reads it as an org gate and would list every org-less stored credential as an account without usage data.

pi has no usage field in ProviderConfig and ignores the extra key, so the config type is widened locally (CommandCodeProviderConfig) instead of shimming the host.

Evidence

npm run test:usage — 10 pass. The tests drive the real fetchCommandCodeQuota with a mocked fetchImpl, so HTTP payload → credential → report is covered together:

✔ maps the full quota snapshot onto the host report
✔ reports unavailable sections instead of fabricating zero usage
✔ keeps the credit pool out of the report until the spend is known
✔ returns no report when every section is unavailable
✔ returns no report when Command Code rejects the credential
✔ uses the OAuth bearer, and a configured key for placeholder credentials
✔ fetches nothing without a usable credential
✔ omits reset times the API reports as absent
✔ publishes the fractions text renderers read directly
✔ maps a report for an account without an organization
ℹ tests 10 · pass 10 · fail 0

The unavailable-endpoint case is the one that fails without the mapping honouring quota.unavailable: it would emit a credits row of zeros ($0.00 / $0.00 · 0.0% used), which is indistinguishable downstream from a real measurement.

End to end, live API, with this checkout installed as the OMP plugin:

Session /usage dashboard over a real account (omp 18.2.6) — the Command Code column is new:

| [!] Google Antigravity                   [i] Commandcode                                                             |
|   Claude & GP… ██████████   0%  1d8h     Credits      ██████████░░░░░░  40%                                          |
|   Gemini    7d █████░░░░░  54% 3d13h     5 Hour       ░░░░░░░░░░░░░░░░ 100%                                          |
|   Gemini    5h ░░░░░░░░░░ 100% 4h54m     Weekly       ░░░░░░░░░░░░░░░░ 100%                                          |

The dashboard grid percentage is the free fraction — freePct = Math.max(0, Math.round((1 - window.fraction) * 100)) (@oh-my-pi/pi-tui src/overlays/usage-dashboard.ts:424) — so 100% on the windows means untouched (0.00 of 3.00 used), not exhausted. The detail view says it in words:

Commandcode
[*] 5 Hour
  aarisr
  ░░░░░░░░░░░░░░░░░░░░░░░░                                                                                100% free
[*] Weekly
  aarisr
  ░░░░░░░░░░░░░░░░░░░░░░░░                                                                                100% free
[*] Credits (default)
  aarisr
  ██████████████▒░░░░░░░░░                                                                                39.8% free

The host recorded the same fetch itself: 9 rows in OMP's usage_history for provider = 'commandcode', a 5h/7d/credits triple per credential (two OAuth, one API key), with credits.used_fraction = 0.6019436072 — the live $6.02 / $10.00:

commandcode  oauth    5h       5 Hour    used_fraction 0.0
commandcode  oauth    7d       Weekly    used_fraction 0.0
commandcode  oauth    credits  Credits   used_fraction 0.6019436072
commandcode  api_key  5h       5 Hour    used_fraction 0.0
commandcode  api_key  7d       Weekly    used_fraction 0.0
commandcode  api_key  credits  Credits   used_fraction 0.6019436072
commandcode  oauth    5h       5 Hour    used_fraction 0.0
commandcode  oauth    7d       Weekly    used_fraction 0.0
commandcode  oauth    credits  Credits   used_fraction 0.6019436072

ACP /usage text (omp acp) with the same plugin — one of these runs also caught a transient upstream failure of billing/subscriptions and usage/summary, which is what the notes rule is for:

Commandcode
- 5 Hour
  aarisr: 0.00 unknown used (100.0% left)
  [░░░░░░░░░░░░░░░░░░░░░░░░] 0%
- Weekly
  aarisr: 0.00 unknown used (100.0% left)
  [░░░░░░░░░░░░░░░░░░░░░░░░] 0%
- Credits
  aarisr: 6.02 usd used
  [························]

and, when those endpoints failed, Plan unavailable from Command Code / Usage summary unavailable from Command Code above the same window rows, with the credit row reduced to what remained known.

Scope note

omp usage itself does not load plugin extensions in 18.2.6 or 18.3.2: src/cli/usage-cli.ts builds new ModelRegistry(authStorage) without extension providers, so an extension-only usage provider is never consulted there. This change is what OMP's documented usage contract asks for, it is what makes every extension-aware surface (session /usage dashboard, ACP text, usage history, credential ranking) report the quota today, and it needs no further change here once that command loads extensions. The CLI gap is reported to OMP separately.

Checks

  • npm run typecheck — pass
  • npm run format:check — pass
  • npm run test:usage — 10 pass
  • npm run test:unit — pass
  • OMP_COMPAT_REQUIRED=1 node tests/test-omp-compat.mjs (real omp 18.2.6) — PASS
  • OMP_COMPAT_REQUIRED=1 node tests/test-omp-compat.mjs needs OMP_AUTH_BROKER_URL unset locally: with a broker URL in the environment the isolated test HOME has no broker token and OMP exits with MissingApiKeyError before the extension loads. Same on main.
  • PI_LOCAL_REQUIRED=1 node tests/test-pi-local.mjs — fails locally with No models matching "commandcode" against the installed pi 0.79.0, identically on an unmodified main checkout; the CI job installs @earendil-works/pi-coding-agent@latest.
  • node --test tests/test-release.mjs — 3 failures in the git-release-guard tarball fixtures (object is not iterable), identical on an unmodified main checkout and unrelated to this change (local npm/Node combination).

@aarisr
aarisr requested a review from patlux as a code owner September 26, 2026 19:01
The registered provider config carried models, oauth and streamSimple but no
`usage` field, so Oh My Pi had no usage provider for this provider: its
accounts were culled from the usage surfaces and the quota the provider
already fetches stayed unreachable there.

Register an OMP `UsageProvider` built from the existing quota layer.
`fetchUsage` calls `fetchCommandCodeQuota` with the host's fetch and the
credential OMP hands over — an already-resolved API key or an OAuth bearer,
both through the placeholder-aware helper `/commandcode-quota` uses — and maps
the snapshot onto OMP's report: the 5-hour and weekly usage windows, credits
remaining and used against the billing-period pool, and the plan in metadata.
The used/remaining fractions are published explicitly because OMP's session
and ACP text renderer reads them without resolving `used`/`limit`.

Sections the API does not report stay out of the limits and are listed in the
report's notes instead, so an unreachable endpoint is never displayed as zero
usage; a failed fetch returns no report at all.

pi has no `usage` field in `ProviderConfig` and ignores it.

@pierreraby pierreraby left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified locally from this branch (dba622e): npm run typecheck clean, test:usage 10/10, test:quota (23) and test:quota-command (3) pass, format:check clean. Also ran the host tests against the real binaries with a mock API: test-pi-local.mjs PASS on pi 0.87.1, test-omp-compat.mjs PASS on omp 17.2.7 (CI covers 18.x). Trial merge into current main is clean, and npm pack --dry-run confirms src/usage.ts ships in the tarball (files covers src/).

Spot-checked the mapping against the sources: the resetAt seconds→ms round-trip is consistent (src/quota.ts:57 normalizes to seconds, src/usage.ts:165 converts back with * 1000); credentials go through the same pickCommandCodeApiKey helper as /commandcode-quota (src/usage.ts:243, src/quota-command.ts:44); null on failure and no zero-filled limits hold (src/usage.ts:247,257). On the installed pi 0.87.1, ProviderConfig (dist/core/extensions/types.d.ts:1161) has no usage field and the runner forwards the config object unvalidated, so the extra field is ignored as the PR says.

Three non-blocking nits:

  1. src/usage.ts:48 — the OMP types are mirrored structurally, so a future OMP schema change would break silently with only the mirrored tests catching it. Worth a shape test or a pinned host-version reference at some point, not a merge blocker.
  2. src/usage.ts:252 — context?.fetch while UsageFetchContext (src/usage.ts:94-96) declares fetch required. Either makes the field optional or requires the context.
  3. tests/test-usage.ts — hermetic and thorough through the real quota fetch; a raw-seconds resetAt fixture (<1e12) would pin the normalizeResetAt branch through this path too.

LGTM — happy to merge once the branch is updated and CI is green again.

@pierreraby
pierreraby merged commit 9716d87 into patlux:main Sep 27, 2026
15 checks passed
@pierreraby

Copy link
Copy Markdown
Collaborator

@aarisr — quick follow-up on the scope note: I verified on OMP 18.3.4 that omp usage still doesn't consult extension usage providers (empty reports, account listed under accountsWithoutUsage), while the session /usage surfaces do pick it up. So the provider side of #120 is doing its job; the CLI gap is upstream.

If you haven't filed it on can1357/oh-my-pi yet, I'm happy to open the issue with the 18.3.4 repro — just say the word. A link here would help track it either way.

@karaaslanz

karaaslanz commented Sep 27, 2026 via email

Copy link
Copy Markdown
Collaborator

@pierreraby

Copy link
Copy Markdown
Collaborator

Upstream issue opened as requested: can1357/oh-my-pi#13579 — with a minimal network-free repro extension (gist linked in the issue) confirming never consults extension usage providers on 18.3.4, whether loaded via -e or installed as a plugin.

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.

3 participants