Repository navigation
fix(usage): show Kiro credits under API-key auth - #25434
Conversation
UX Review (Opus 5.5, fork) — ✅ PASSUX-level review of UX-Verdict: PASS Nothing to check. The frontend change only deletes the API-key "unavailable" state and its strings. It adds no control, and API-key accounts now use the existing credit display. [UX-REVIEWED] 6f9db4d Description read[DESCRIPTION-READ] 99dbfd2c505eedd95a0f4f16a7f54cfe958ed2bb69fe714b7779bf1701e1572a That sha256 names everything this verdict read: the PR description, plus the 3 evidence file(s) the workflow collected from it and handed this review. It covers that evidence because the media strip replaces every attachment URL with the same placeholder, so an edit swapping one attachment for another would leave the description's captured text identical while what this review judged changed. Recompute it with |
Design Review (Opus 5.5, fork) — ✅ PASSDesign-level review of Design-Verdict: PASS Nothing blocks: the fix is aimed at the real cause, and every reader of the removed Notes
[DESIGN-REVIEWED] 6f9db4d Description read[DESCRIPTION-READ] 4a6e54e1d71ad9b7182f6ba26f6c0790e657a7f7d697999331abb6500e0e2169 That sha256 names everything this verdict read: the PR description, plus the 1 evidence file(s) the workflow collected from it and handed this review. It covers that evidence because the media strip replaces every attachment URL with the same placeholder, so an edit swapping one attachment for another would leave the description's captured text identical while what this review judged changed. Recompute it with |
First Principles Review (Opus 5.5, fork) — ✅ PASSPremise-level review of First-Principles-Verdict: PASS Nothing blocks. This is a fix for #17443, and the test that was deleted is reversed openly, backed by a live API-key reading and a 403 on the API call. What this change shipsInventory (7 items) — 7 justifiedIntent: let a user who signs in with an API key see their real Kiro credit balance in the account widget. FIX.
[FIRST-PRINCIPLES-REVIEWED] 6f9db4d Description read[DESCRIPTION-READ] abc6bc67cd7cc3ff54b582fb41596ea0c9d8db55ff43db5cbc82ee8be2bc93b3 That sha256 names the PR description this verdict read. Recompute it with |
GPT 6.1 Review (fork) — ✅ no blocking findingsReviewed Review detailsNo findings. |
Opus 5.5 Review (fork) — ✅ no blocking findingsReviewed |
6cea6a2 to
f1bb565
Compare
|
Thanks for this. The backend change reads well: an The branch currently conflicts with
One non-blocking design note for the rebase: once kiro-cli retires V2, an API-key account has no source at all, and with the I will take another look once the branch is rebased and readiness has run on the new head. |
c231dc8 to
384a105
Compare
|
Thanks Bolin, all addressed. Rebased onto 1. Dropped the floor hunk. The Confirmed the bundled CLI lists v2, as you asked. So the probe pins v2 and the scrape runs for API-key accounts on the bundled CLI. Verified end-to-end on a live API-key account: 2. Added the no-V2 test. On the non-blocking note: agreed. Once V2 is retired, an API-key account has no local |
384a105 to
af72408
Compare
af72408 to
76f16de
Compare
76f16de to
5354bc0
Compare
|
|
Both addressed, and the fix landed at 1. The whoami shape. On my account 2. The fix. Rather than keep a terminal marker, the scrape is now PUBLISHED for the key-only case. On the API-key path ( The test you asked for is Frontend Lint: agreed, that red is |
|
The red Frontend Lint & Type Check is a main-side failure, not from this PR: the only eslint error is |
841f5d2 to
2102033
Compare
2102033 to
6d71e98
Compare
|
Rebased: main moved since this head went green and now carries #25429, which fixes the |
bolichen97
left a comment
There was a problem hiding this comment.
Thanks for the quick turnaround. The range diff from 5354bc0 to 6d71e98 is the rebase onto main plus the fix for the earlier point: on the API-key path an unproven whoami pair no longer drops the reading, and test_api_key_auth_with_no_identity_publishes_the_scrape pins the key-only {"account_type": "ApiKey"} shape. That part reads well.
One blocking point remains, and it matches the Opus lane. skip_usage_api is decided from the whoami at the top of the refresh, but the relaxed rule is applied to the before / after pair around the scrape. _whoami_agreement compares only email, start_url and the profile ARN, so any pair with none of those is unproven, not only an API-key pair. If the sign-in changes after the top whoami to an ExternalIdP sign-in ({"accountType":"ExternalIdP","email":""}), a lapsed type-only sign-in, or a signed-out profile, the pair is still unproven and the scrape is published as a live reading for that account. That breaks the _account_type_only rule that an ExternalIdP sign-in never shows a balance.
Suggested fix: allow unproven to publish only when both before and after are themselves the normalized apikey account type, and discard otherwise as on the non-API-key path. A test with an ApiKey top whoami and an ExternalIdP (or empty) after whoami, asserting nothing is published, would pin it.
Rebase-wise the branch is clean on main. CI is still running on this head.
| # unpublishable here; on the API-key path `unproven` falls through | ||
| # and publishes. | ||
| discard = ( | ||
| agreement == _WHOAMI_DIFFERENT if skip_usage_api else agreement != _WHOAMI_SAME |
There was a problem hiding this comment.
unproven publishes for any identity-less sign-in, not only an API key
skip_usage_api comes from the top-of-refresh whoami, but this check judges the before / after pair taken around the scrape. _whoami_agreement never looks at account_type, so a switch to ExternalIdP, a type-only lapsed sign-in, or a signed-out profile also yields unproven, and the scrape is then published for that account.
discard = (
agreement == _WHOAMI_DIFFERENT if skip_usage_api else agreement != _WHOAMI_SAME
)Suggestion: on the API-key path, treat unproven as publishable only when both snapshots are API-key sign-ins:
def _is_api_key(ident):
t = ident.get("account_type")
return isinstance(t, str) and re.sub(r"[^a-z0-9]", "", t.lower()) == "apikey"
if skip_usage_api and agreement == _WHOAMI_UNPROVEN:
discard = not (_is_api_key(before) and _is_api_key(after))
else:
discard = agreement != _WHOAMI_SAMEPlease add a test with an ApiKey top whoami and an ExternalIdP or empty after whoami that asserts nothing is published.
There was a problem hiding this comment.
Fixed in 6f9db4d. The API-key carve-out now requires the whoami on both sides of the scrape to be an API-key sign-in, so an ExternalIdP, lapsed type-only, or signed-out after discards instead of publishing. test_api_key_switch_to_identity_less_sign_in_publishes_nothing pins it for ExternalIdP, BuilderId and an empty whoami, and test_api_key_auth_with_no_identity_publishes_the_scrape still pins the key-only shape publishing. Spec updated to match.
6d71e98 to
6f9db4d
Compare
|
bolichen97
left a comment
There was a problem hiding this comment.
Thanks for the quick turnaround. The earlier blocking point is fixed at 6f9db4d: an unproven whoami pair around the /usage scrape now publishes only when both the before and after reads are themselves API-key sign-ins, so a switch to ExternalIdP, a lapsed type-only sign-in, or a signed-out profile discards the reading as it does on the non-API-key path. A different pair still always discards.
What I checked at this head:
- Range-diff against the previous head: only sessions.py, the usage test file and the one spec sentence changed; the other files are identical to the revision I read in full.
- Ran test/test_session_usage.py at this head: 76 passed. The new switch test fails for the ExternalIdP and type-only cases against the previous sessions.py and passes with this one.
_WHOAMI_UNPROVENis now a runtime import, which the new comparison needs.- AI review lanes on this head report no blocking findings.
This resolves my earlier change request. I will approve once the remaining backend test shards finish and the readiness check passes on this head.
bolichen97
left a comment
There was a problem hiding this comment.
Approving at 6f9db4d.
The earlier blocking point is fixed: an unproven whoami pair around the /usage scrape now publishes only when both the before and after reads are API-key sign-ins, so a switch to ExternalIdP, a lapsed type-only sign-in, or a signed-out profile discards the reading as it does on the non-API-key path. A different pair still always discards.
What I checked at this head:
- Range-diff against the previous head: only sessions.py, the usage test file and one spec sentence changed. The other 21 files are identical to the revision I read in full, so every hunk of the 24 files has been read.
- Ran test/test_session_usage.py at this head: 76 passed. The new switch test fails for the ExternalIdP and type-only cases against the previous sessions.py and passes with this one.
- Readiness is passed on this head, all checks are green, every AI review lane reports no blocking findings, and there is no merge conflict.
This replaces my earlier request for changes.
Problem / Motivation
Goal: Show real Kiro credit usage in the account widget for API-key sign-ins, the same as it does for SSO.
When you sign in to the Kiro backend with an API key, the account widget said "Account details unavailable / Credit usage isn't available for API key authentication" and never showed a number.
/usagein the Kiro CLI returns the real balance for the same API-key account, so the data is reachable; the gateway just refused to fetch it.Why it matters
Every API-key user sees a permanent "unavailable" message instead of their credits. It reads as a broken widget, and it hides the one number (balance, plan, reset date) the panel exists to show. The data was always obtainable.
Not a goal
GetUsageLimitsAPI work with an API key. It cannot: an API-key account has no SSO/OIDC bearer token, so that call returns 403. This PR routes around it, it does not fix it.What changed (motivation → approach → change)
The usage refresh ran
kiro-cli whoami, sawaccountType=ApiKey, and published anavailable: false, reason: api_key_authmarker before trying anything. The premise was that such accounts have no usable credential, so neither the API nor the scrape could help.That premise was half right. The API path genuinely cannot work (no bearer token). But the
/usagetext scrape does work for API-key accounts: kiro-cli answers/usagelocally from its own authenticated client, so the credit table prints under API-key auth exactly as it does for SSO. The gateway already pins that scrape to the v2 engine (where/usageruns as a command, not a model prompt) and already parses its output.So the fix, in
src/kiro_crew/dashboard/handlers/sessions.py, replaces the fast-fail with askip_usage_apiflag: an API-key account skips only the always-403 API call and falls through to the existing/usagescrape, which supplies the real number. The scrape's own engine selection (choosing--agent-engine v2, where/usageruns as a local command) is handled by the existing probe inusage_scrape.pyand is untouched here — this PR only lets API-key accounts reach that scrape. The SSO / Builder-ID path is unchanged: the existingGetUsageLimitsAPI call simply moves into theelsebranch of the new flag, so a non-API-key account runs exactly the code it ran before. With the backend no longer emittingapi_key_auth, the frontend's'api-key'usage state became unreachable, so this PR removes it rather than leave dead code: the sentinel and its render branches inwebsite/src/shell/topbar/kiroUsageReadout.tsxandwebsite/src/components/KiroAccountModal.tsx, the mapping inwebsite/src/api/kiroUsage.tsand the doc comment inwebsite/src/api/client.ts, and its two now-dead strings from every catalog underwebsite/src/i18n/locales/. Frontend tests underwebsite/src/test/drop the removed assertions, and the dev screenshot scriptwebsite/scripts/capture-credit-pill-states.mjsdrops its now-deadapi-keyscenario. An API-key account now resolves to an ordinary credit reading. The two owning specs are updated to match the new behaviour:docs/system-specs/modules/learn-cron-dashboard.mddescribes the API-key carve-out (API call skipped, scrape runs, anunprovenagreement publishes only when the whoami on both sides of the scrape is itself an API-key sign-in, and any other case discards) and drops its old "never forapi-key" clause, anddocs/system-specs/modules/kas-auth.mdreplaces the ApiKey "must fail-fast on usage/credit APIs" lesson with the skip-API-use-scrape behaviour.Backwards compatibility
Compatible: no field is rejected, renamed or removed from any wire payload; the
reasonstringapi_key_authsimply stops being produced.Removes nothing: the only removal is the frontend
'api-key'usage sentinel, which is internal UI state with no external consumer. No gateway payload shape, config key, or public function changes. Thereasonfield still exists and still carriessignin_required/account_unproven.Tests
TestApiKeyAuthUsesScrape(renamed fromTestApiKeyAuthFailFast) asserts anApiKeywhoami skips the API (fetch_usage_limitsnot called) yet runs the scrape and publishes the parsed credits, across every respelling of the enum.test_api_key_auth_with_no_identity_publishes_the_scrapecovers the no-identity API-key shape (whoami returns only{account_type: ApiKey}, no email): the scrape'sunprovenwhoami agreement must still PUBLISH, since the scrape is kiro-cli's own/usagefor the signed-in key and cannot be another account's. A genuine mid-scrape account switch (different) still discards. Frontend tests drop the removed'api-key'assertions and keep the no-handoff split on the remaining account-only state.Proof: PROVEN test/test_session_usage.py::TestApiKeyAuthUsesScrape::test_api_key_auth_skips_api_but_scrapes, test/test_session_usage.py::TestApiKeyAuthUsesScrape::test_api_key_auth_with_no_identity_publishes_the_scrape, test/test_session_usage.py::TestApiKeyAuthUsesScrape::test_account_type_comparison_tolerates_respelling[apikey], test/test_session_usage.py::TestApiKeyAuthUsesScrape::test_account_type_comparison_tolerates_respelling[APIKEY], test/test_session_usage.py::TestApiKeyAuthUsesScrape::test_account_type_comparison_tolerates_respelling[ ApiKey ] (head 6f9db4d)
Manual verification
Confirmed in the real UI: built the frontend, ran the gateway, and the Kiro Account widget shows a live balance for an API-key sign-in (
KIRO CREDIT POOLING, ~903 / 4777, ~19%, resets 2026-11-01) where it previously said "Credit usage isn't available for API key authentication". Also ran the real_fetch_usage_bgfrom this branch against the live account: before the fix it published{available: false}; after, a full reading matching the CLI's own/usageand the GetUsageLimits-backed IDE meter. The value refreshes on the existing 10-minute cache timer, not just once. The raw API key is not a valid RTS bearer (confirmed 403), which is why the API path is skipped rather than fixed. SSO / Builder-ID accounts were regression-checked: the existing usage suites (API-first path, refresh timer, scrape fallback, account-unproven, signin-required) pass unchanged.Screenshots / video
Why no screenshot: the frontend diff only removes the unreachable
'api-key'state; a reachable API-key account now renders through the ordinary credit-reading path, which existingKiroAccountModaltests already cover.Sibling sweep
Siblings: none -- the fast-fail that hid usage existed at exactly one site (the
account_type == "apikey"branch in_fetch_usage_bg), and the frontend'api-key'sentinel it fed had one producer (kiroUsage.ts) and the render branches removed here; no other auth type short-circuits the usage fetch.Pattern harvest
Not generalizable: a single correct-looking guard built on one unchecked premise ("API-key auth has no usable credential source") that was half true -- the API path cannot work, but the
/usagescrape can. The defect is the specific premise, not a repeatable code shape a linter could catch; the guard against it is testing a fast-fail's assumption against each fallback path it skips, which is a review habit rather than an automatable rule.Closes #17443