Skip to content

fix(claude): keep the credits record whole and name the shared pause - #125

Merged
bernardopg merged 1 commit into
mainfrom
fix/claude-prepaid-review
Nov 11, 2026
Merged

bernardopg merged 1 commit into
mainfrom
fix/claude-prepaid-review

Conversation

@bernardopg

Copy link
Copy Markdown
Owner

Summary

Follow-up to #124, addressing the three findings of the automated review there:

  • Interrupted writes — since fix(claude): stop asking for refused usage credits on every refresh #124 a failed credits read rewrites claude-prepaid-cache.json, in place. The record is now built first and replaced with atomic_write (temp file + mv -fT), so a refresh killed mid-write keeps the last record and balance; leftover claude-prepaid-cache.json.?????? files are swept under the account lock, as the transcript cache already was.
  • 2.4.0 records — a {cached_at, data} record names no organization, and (.org // $org) == $org took it as the current one. After an organization switch inside its 10 minute TTL the previous organization's balance could be shown. Now only .org == $org is honoured: such a record is re-checked once and never shown.
  • Retry time — a credits 429 started the shared backoff but left USAGE_RETRY_AT / USAGE_RETRY_REASON empty, and so did a run serving a fresh usage snapshot while a backoff held. note_usage_backoff now fills them whenever the pause holds (not for rejected credentials), as docs/architecture.md already describes; use_usage_fallback uses the same helper.

CHANGELOG.md Unreleased bullet for #123 updated. No UI string changed.

Related issues

Refs #123, #124.

Type of change

  • New provider
  • Bug fix
  • Enhancement
  • Documentation / i18n
  • CI / tooling

Checklist

  • plugin.json .version and CHANGELOG.md are consistent (no release in this PR)
  • New/changed provider scripts are executable and emit valid JSON
  • i18n/en.json keys have translations in every locale
  • Every commit is signed off (git commit -s) under the DCO
  • CI is green

Tests (tests/test-limit-resets.sh): a 2.4.0 record holding another balance is re-checked and not shown on a 403, then replaced by an organization-scoped read; a failing rename leaves the record byte-identical and the balance shown, with no temp file left; a credits 429 reports USAGE_RETRY_AT equal to the stored deadline and rate_limited, and the next run serving the fresh snapshot still reports it. Each test fails with its fix reverted. Local: Claude suites, check-metadata, check-changelog --base origin/main, check-dco, shellcheck, git diff --check, statement coverage 100.00% (2400/2400) with kcov built from the CI-pinned commit.

Review follow-up to #124:

- The credits record is replaced with a rename (atomic_write), so a
  refresh killed mid-write keeps the last record and its balance;
  leftover temporary files are swept under the account lock. Failed
  reads rewrite the record since #124, which made an in-place write
  riskier than before.
- A record written by 2.4.0 ({cached_at, data}) names no organization.
  It was taken as the current one, so after a switch inside its TTL the
  previous organization's balance could be shown. Such a record is now
  re-checked once and never shown.
- A credits 429 starts the shared usage backoff but left
  USAGE_RETRY_AT/USAGE_RETRY_REASON empty, as did a run that served a
  fresh usage snapshot while a backoff held. The card now names the next
  try whenever the pause holds, as docs/architecture.md describes.

Refs #123

Signed-off-by: Bernardo Pinto Gomes <[email protected]>
@github-actions github-actions Bot added documentation Improvements or additions to documentation area:providers Provider adapters (providers/) area:ci CI, workflows, tooling labels Nov 11, 2026
@qodo-code-review

Copy link
Copy Markdown

PR Summary by Qodo

Preserve Claude credits records and report shared backoff

🐞 Bug fix 🧪 Tests 📝 Documentation 🕐 20-40 Minutes

Grey Divider

AI Description

• Replace credits records atomically so interrupted refreshes preserve the last balance.
• Re-check legacy credits records whose organization is unknown instead of showing a potentially
 wrong balance.
• Report the shared retry deadline after credits rate limits, including when cached usage is served.
Diagram

graph TD
    R["Claude refresh"] --> L["Account lock"] --> O{"Scoped record?"} -->|stale or unknown| F["Credits request"] --> W["Atomic credits record"] --> C["Card output"]
    O -->|fresh| C
    F -->|429| B["Shared backoff"] --> N["Retry metadata"] --> C
    L -->|backoff active| N
Loading
High-Level Assessment

Reusing the existing atomic-write helper and shared backoff state is the smallest coherent fix. Retaining organization-less legacy records cannot safely distinguish balances after an organization switch, while duplicating retry logic would risk inconsistent card output.

Files changed (3) +59 / -20

Bug fix (1) +23 / -12
get-claude-usageProtect credits records and expose shared retry state +23/-12

Protect credits records and expose shared retry state

• Sweeps abandoned credits temporary files under the account lock and writes new records through the existing atomic-write helper. Rejects records without the current organization and centralizes retry metadata so credits 429s and cached usage served during backoff report the pause.

providers/get-claude-usage

Tests (1) +35 / -7
test-limit-resets.shCover legacy credits, failed replacement, and retry reporting +35/-7

Cover legacy credits, failed replacement, and retry reporting

• Checks that an organization-less legacy balance is not shown and is re-fetched. Simulates a failed rename to verify the previous record and balance survive, then asserts that a credits 429 and a subsequent cached-usage run report the stored retry deadline.

tests/test-limit-resets.sh

Documentation (1) +1 / -1
CHANGELOG.mdDocument credits-record and retry fixes +1/-1

Document credits-record and retry fixes

• Expands the Unreleased Claude fix entry to cover atomic record replacement, re-checking organization-less 2.4.0 records, and showing the next try during a shared pause.

CHANGELOG.md

@qodo-code-review

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (0) 📘 Rule violations (0) 📎 Requirement gaps (0)

Grey Divider

Great, no issues found!

Qodo reviewed your code and found no material issues that require review

Grey Divider

Tip of the day
💡 Did you know, you can import rules and skills only from the repositories your team follows

More tips ↗ | Customize Qodo ↗ | Qodo docs ↗

Grey Divider

Qodo Logo

@bernardopg
bernardopg enabled auto-merge November 11, 2026 18:33
@bernardopg
bernardopg merged commit 43e581f into main Nov 11, 2026
20 checks passed
@bernardopg
bernardopg deleted the fix/claude-prepaid-review branch November 11, 2026 18:35
@bernardopg bernardopg mentioned this pull request Nov 11, 2026
4 of 5 tasks
bernardopg added a commit that referenced this pull request Nov 11, 2026
Claude Team accounts no longer ask for refused usage credits on every
refresh (#123, #124, #125): a refusal is remembered for a day per
organization, a credits 429 pauses the usage endpoint too, and the card
names the next try.

Signed-off-by: Bernardo Pinto Gomes <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:ci CI, workflows, tooling area:providers Provider adapters (providers/) documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant