Skip to content

fix(usage): read credit usage from Crew sign-in without kiro-cli - #21732

Closed
CrysisDeu wants to merge 1 commit into
kirodotdev:mainfrom
CrysisDeu:fix/usage-without-kiro-cli
Closed

CrysisDeu wants to merge 1 commit into
kirodotdev:mainfrom
CrysisDeu:fix/usage-without-kiro-cli

Conversation

@CrysisDeu

Copy link
Copy Markdown
Contributor

Summary

With KAS on Crew's own sign-in and no kiro-cli installed, /api/sessions/usage returned 503 kiro_prerequisite_required and the credit pill went blank, even though Crew already holds a working OIDC token.

  • kiro_usage_api.fetch_usage_limits_with_credential(token, profile_arn): GetUsageLimits with one caller-supplied credential. Same never-raise / AUTH_* contract as fetch_usage_limits; uses the stored ARN, ListAvailableProfiles only for the display name (or the ARN when none is stored).
  • _fetch_usage_bg tries _refresh_from_vault() first: when the vault holds a usable identity, the token is resolved and refreshed through KasAuthProvider(allow_env_api_key=False) and the API is called directly. No kiro-cli is resolved or spawned.
  • The kiro-cli readiness gate on GET /api/sessions/usage and POST /refresh is skipped when the vault owns usage. That gate only exists to stop a signed-out kiro-cli spawn from opening a browser login.
  • The vault is authoritative once it holds an identity: failures report unavailable (signin_required when auth-class) and never fall back to kiro-cli, whose login may be a different account.
  • Identity invariant mirrors the whoami bracketing: the vault identity fingerprint is read before the credential and after the API answers, and the reading is published only when they match. A stale reading is preserved only when the cache was published for the same vault fingerprint (tracked beside the cache, never served).
  • kiro_crew.auth imports stay deferred to call time (boot-path / cryptography gates).

Without a vault identity, behavior is unchanged.

Testing

  • New test/test_usage_crew_vault.py (13 tests): vault publish without kiro-cli, fall-through when no vault identity, identity change mid-refresh, auth-rejected and token-refresh failures report signin_required without falling back, same-identity stale preservation, no preservation of a kiro-cli reading, gate bypass and gate retention, and the single-credential client (ARN payload, 401, transport error, empty token).
  • Locally: test_usage_crew_vault.py, test_session_usage.py, test_kiro_usage_api.py, test_kiro_spawn_readiness_gate.py, test_kas_login_api.py, test_approval_chain_no_cryptography.py pass; isort, flake8, mypy (touched files), black baseline gate and sync-io gate pass.
  • Verified on a host with kiro-cli removed: before this change the gateway logged the 503 refusal at startup.

When Crew's sign-in vault holds a usable Kiro identity, /api/sessions/usage now
reads GetUsageLimits directly with the vault's OIDC access token (resolved and
refreshed through KasAuthProvider) instead of requiring kiro-cli. The kiro-cli
readiness gate is skipped on that path, since it exists only to stop a
signed-out kiro-cli spawn from opening a browser login; no kiro-cli is spawned.

The vault is authoritative once it holds an identity: a failure reports
unavailable (signin_required when auth-class) rather than falling back to
kiro-cli's possibly different account. A reading is published only when the
vault identity fingerprint read before the credential matches the one read
after the API answers, and a stale reading is kept only for the same vault
identity. Without a vault identity the existing kiro-cli path is unchanged.
@CrysisDeu
CrysisDeu requested review from a team and KiroCrewCaptain as code owners October 18, 2026 03:17
@CrysisDeu
CrysisDeu requested a review from iamwhatever October 18, 2026 03:17
@github-actions github-actions Bot added fork Pull request from a fork (external contributor) readiness: checking Automated validation is still running labels Oct 18, 2026
@github-actions

Copy link
Copy Markdown
Contributor

UX Review (Opus 5.5, fork) — ⏭️ skipped

Revision 2efb9adaf85d607a90de5f34d4dc8f96d8900c57 touches no user-facing surface (no changes under website/ or committed screenshots), so the UX review was skipped. Advisory — does not block merge.

@github-actions

Copy link
Copy Markdown
Contributor

First Principles Review (Opus 5.5, fork) — 🟡 CONCERNS

Premise-level review of 2efb9adaf85d607a90de5f34d4dc8f96d8900c57 via the fork AI-review pipeline — why this exists and whether the shipped surface is the smallest honest version. Updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

First-Principles-Verdict: CONCERNS

fetch_usage_limits_with_credential copies the GetUsageLimits request/response body of fetch_usage_limits, which gives the same 401/JSON/CREDIT mapping a second copy to keep in sync.

Not justified as shipped

  • Item 1 — duplicate of src/kiro_crew/dashboard/handlers/kiro_usage_api.py:1057-1108: the new function repeats the existing loop body line for line. That is the payload build, _post, the 401-only AUTH_REJECTED rule, r.json(), _map_response, _account_name and _profile_arn. I counted 1 existing copy. Only the candidate selection and ownership proof differ.
    Subtraction: extract that per-token body from fetch_usage_limits into one helper and call it from both. That removes about 30 duplicated lines, and the two copies' error classification can no longer diverge.
    Clears when: both paths share one GetUsageLimits request/mapping body.
  • Item 4 — undeclared: when the vault holds an identity and kiro-cli is also installed, the pill now shows the vault account instead of kiro-cli's. The description says "Without a vault identity, behavior is unchanged" but never says the source switches for these dual-sign-in hosts.

What this change ships

Inventory (5 items) — 3 justified

Intent: show the credit usage pill for a Crew-sign-in user with no kiro-cli installed. This is a FIX.

  1. Usage is fetched with the Crew vault token through a new single-credential API client — duplicate of kiro_usage_api.py:1057-1108
  2. The credit pill populates with kiro-cli absent instead of 503 — justified
  3. The kiro-cli readiness gate is skipped on GET/POST usage when the vault owns usage — justified
  4. The vault account's usage replaces kiro-cli's when both are signed in, with no fallback — undeclared
  5. A stale reading is kept only for the same vault fingerprint, and a reading is discarded if the identity changes mid-refresh — justified

[FIRST-PRINCIPLES-REVIEWED] 2efb9ad

Description read

[DESCRIPTION-READ] 04256be5c17ec0b6a1c5a2f9bedeffdc10961a9fbda5ec4605ca4d8b43b3a179

That sha256 names the PR description this verdict read. Recompute it with .github/scripts/pr-description-capture.sh; if it differs, the description changed after this verdict was formed and any finding drawn from the description is unproven.

@github-actions

Copy link
Copy Markdown
Contributor

Design Review (Opus 5.5, fork) — 🟡 CONCERNS

Design-level review of 2efb9adaf85d607a90de5f34d4dc8f96d8900c57 via the fork AI-review pipeline — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

Design-Verdict: CONCERNS

The vault takes over usage for every backend, not only the one that uses it, so a kiro-cli user with a leftover Crew sign-in sees another account's credits.

Watch

  • Vault ownership isn't tied to the active harness. _refresh_from_vault and the gate bypass key only on _vault_owns_usage() (vault_holds_identity). Two things outside this PR use the vault the same way: the KAS spawn (acp/harness/kas.py:326) and the doctor row, which is gated on backend in ACP_BACKENDS_HOST_AUTH_CALLBACK (doctor_checks/agents.py:441). This PR has no such gate. So when the operator runs acp_backend=kiro (kiro-cli owns every session) and still has a usable vault identity from an earlier Crew sign-in:

    • the credit pill shows the vault account's credits;
    • a vault failure publishes signin_required and "never fall[s] back to kiro-cli", so a working kiro-cli login reads as signed out.

    This breaks the very invariant the PR cites ("kiro-cli … may be a different account"), just in the opposite direction. The description scopes the fix to "KAS on Crew's own sign-in". The code doesn't.
    Clears when: _vault_owns_usage also requires the configured agent.acp_backend to be in ACP_BACKENDS_HOST_AUTH_CALLBACK, and there is a test where a vault identity plus the kiro backend still takes the kiro-cli path and keeps the gate.

[DESIGN-REVIEWED] 2efb9ad

Description read

[DESCRIPTION-READ] 8ee86f4da43547dc0d65d1ffeecef7f497ae886485e5938e35e9fd3160620fb3

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 .github/scripts/pr-description-capture.sh over the same inputs; if it differs, the description or that evidence changed after this verdict was formed and any finding drawn from either is unproven.

@github-actions

Copy link
Copy Markdown
Contributor

Opus 5.5 Review (fork) — ✅ no blocking findings

Reviewed 2efb9adaf85d607a90de5f34d4dc8f96d8900c57 via the fork AI-review pipeline; updated in place on each push.

Review details

Usage pill can show the Crew sign-in vault's account instead of the account the selected backend runs on.

FINDING — src/kiro_crew/dashboard/handlers/sessions.py:1155 — if await _refresh_from_vault(): runs on every backend, and _vault_owns_usage checks only return await vault_holds_identity_off_loop(). It never asks which harness is selected. Example: agent.acp_backend is the default kiro-cli backend, kiro-cli is signed in as account A, and a usable Crew vault identity for account B is left over from KAS sign-in. GET /api/sessions/usage → _fetch_usage_bg → _refresh_from_vault → fetch_usage_limits_with_credential(B's token) publishes B's credits, plan and profile name, while every turn runs on and bills to A. The whoami-bracketed kiro-cli path never runs. The vault is only consulted for a backend in ACP_BACKENDS_HOST_AUTH_CALLBACK, as at doctor_checks/agents.py:441 and acp/harness/kas.py:326. → Fix: in _vault_owns_usage, check that the configured backend is in ACP_BACKENDS_HOST_AUTH_CALLBACK before probing the vault. That also limits the readiness-gate bypass to KAS.

[OPUS-REVIEWED] 2efb9ad

@github-actions

Copy link
Copy Markdown
Contributor

GPT 6.1 Review (fork) — 🔴 changes requested (blocking)

Reviewed 2efb9adaf85d607a90de5f34d4dc8f96d8900c57 via the fork AI-review pipeline; updated in place on each push.

1 of 1 blocking finding(s) are security-class and were withheld from adjudication, so the blocking verdict stands.

BLOCKING -- src/kiro_crew/dashboard/handlers/sessions.py:1481 -- Concurrent vault logout bypasses the CLI readiness guard
if not await _vault_owns_usage():
With kiro-cli signed out and the scrape unparked, Crew logout after the GET/POST vault probe -> readiness is skipped -> _refresh_from_vault() returns False -> _fetch_usage_bg() falls through to kiro-cli chat, opening browser login instead of refusing.
Anchor: residual/guard-removal
Fix: Carry the vault-only decision into the refresh; if that identity disappears, publish unavailable without falling back to kiro-cli.

[BLOCK-MERGE] 2efb9ad
[GPT-REVIEWED] 2efb9ad

Adjudication (Opus 5.5) — is blocking on each finding proportionate?

I flagged the one fenced finding (F1). It still blocks the merge; the flag only gives a repository writer a ready-made rationale if they choose to override it. There were no adjudicable findings to rule on, so the main footer is empty.

F1 (fenced): FLAG. The race is real, but the consequence is a single visible browser login that fixes itself on the next poll. No credential is exposed and no governance ceiling is bypassed.

Conditions (the changed lines come from the patch, since this checkout is the base):

  • kiro-cli is installed and signed out, and the /usage scrape is not parked (sessions.py:1160 in the base).
  • A refresh is actually due. The GET handler only starts one when the cache is older than 600s (sessions.py:1366, _USAGE_REFRESH_SECS).
  • The Crew logout lands between the handler's vault probe (patch line 228, or 251 for the POST) and the background task's own probe (patch line 157). The task is started right after the probe (sessions.py:1375), and the background probe is its first await (patch line 214), so the window is milliseconds.
  • A second, also small window: a NotAuthenticated from current() (patch line 169) also falls through to kiro-cli.

Recovery: On the next poll the vault is empty, so _vault_owns_usage() is False and reject_if_kiro_unverified blocks again (patch lines 228–231, sessions.py:1362). The result is at most one browser login tab per lost race, never the repeated browser opening every 30 seconds that the gate exists to stop.

[ADJUDICATION] 2efb9ad total=0 uphold=0 downgrade=0
[GPT-ADJUDICATED] 2efb9ad

[ADJUDICATION-FENCED] 2efb9ad fenced=1 flagged=1
FLAG F1 src/kiro_crew/dashboard/handlers/sessions.py:1481 -- Needs a Crew logout within milliseconds of a refresh that runs at most every 10 minutes, on a host where kiro-cli is installed but signed out and the scrape is not parked; the result is one visible browser login, and the next poll is blocked again by the restored readiness gate.
[GPT-ADJUDICATED-FENCED] 2efb9ad

🏷️ Fenced finding(s) machine-flagged as likely edge case

The security fence keeps these findings blocking regardless of adjudication; the only clearance path is a human override recorded by a repository writer, who must independently verify a rationale before recording it — it is machine-authored, and a wrong override on a security-class finding ships exactly the class the fence exists to stop. (This lane's comment deliberately carries no override command.)

  • F1 src/kiro_crew/dashboard/handlers/sessions.py:1481 — Needs a Crew logout within milliseconds of a refresh that runs at most every 10 minutes, on a host where kiro-cli is installed but signed out and the scrape is not parked; the result is one visible browser login, and the next poll is blocked again by the restored readiness gate.

@github-actions github-actions Bot added readiness: action required A blocking check or review needs attention merge conflict Branch has merge conflicts with its base — author must resolve before merge and removed readiness: checking Automated validation is still running labels Oct 18, 2026
@CrysisDeu

Copy link
Copy Markdown
Contributor Author

Superseded by #21956: same change, reopened as a same-repo branch and rebuilt on current main with this PR's review findings addressed (harness-scoped vault ownership, no kiro-cli fallback after a vault verdict, one shared GetUsageLimits helper).

@CrysisDeu CrysisDeu closed this Oct 18, 2026
@github-actions github-actions Bot removed the readiness: action required A blocking check or review needs attention label Oct 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fork Pull request from a fork (external contributor) merge conflict Branch has merge conflicts with its base — author must resolve before merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant