Skip to content
This repository was archived by the owner on Sep 30, 2026. It is now read-only.

usage: converge Anthropic/OpenAI usage cache on quota-axi shared file - #21

Closed
yjuyjuy wants to merge 2 commits into
masterfrom
fm/asw-jcode-cache-converge
Closed

yjuyjuy wants to merge 2 commits into
masterfrom
fm/asw-jcode-cache-converge

Conversation

@yjuyjuy

@yjuyjuy yjuyjuy commented Aug 13, 2026

Copy link
Copy Markdown
Owner

Summary

Point jcode's Anthropic and OpenAI usage fetchers at quota-axi's shared on-disk usage cache (read-through, write-through) so N live jcode sessions stop polling the provider usage endpoints independently. This is Phase 1 of the account-switch orchestrator (ADR 0031).

Today each jcode session keeps its own in-memory usage cache (300s TTL, 900s backoff on 429). With N live sessions that is N independent pollers hitting the Anthropic and OpenAI usage endpoints, the likely cause of the constant 429s. quota-axi now owns one host-wide on-disk usage cache; this change makes jcode read and write THROUGH that same file so the whole host makes roughly one usage fetch per provider-account per TTL regardless of session count.

What changed

  • New crates/jcode-base/src/usage/shared_cache.rs: the L2 layer. Reads and writes ${XDG_CACHE_HOME:-~/.cache}/quota-axi/quotas.json (schemaVersion 1), byte-matching quota-axi's on-disk format, window identities, atomic 0600 write, and provider ordering.
  • usage.rs / accessors.rs / provider_fetch.rs: the Anthropic and OpenAI usage-fetch paths now check L1 (per-session in-memory) first, then L2 (shared file) read-through, then hit the endpoint and write-through to both.

Design

  • Two-layer. The existing per-session in-memory cache stays as L1. The shared file is L2, read-through and write-through. A fresh L2 read warms L1 so subsequent reads stay in-process.
  • Active-account scoped. quota-axi stores exactly one record per provider with no account identity, and jcode's active account is host-global disk state read the same way by every session and by quota-axi. So L2 is used only for the active account (which is also the account every session polls constantly). Non-active account probes used during account switching skip L2 and fetch directly, avoiding cross-account contamination.
  • Backoff preserved. quota-axi only ever persists successful fresh records; errors and 429s never reach the shared file. jcode does the same: only successful fetches are written through, and all error/rate-limit backoff (the existing 900s-on-429 behavior) stays entirely in L1. The shared file can never mask an error or shorten a backoff.
  • Freshness. L2 records older than 300s (by state.refreshedAt) are ignored on read, matching jcode's L1 CACHE_DURATION and quota-axi's ~5min TTL, so a stale shared file cannot pin sessions to old data.
  • Cross-tool contract. The OpenAI (codex) writer emits only the two windows with valid quota-axi codex identities (five_hour/session, seven_day/weekly); quota-axi rejects a whole codex record on an unrecognized window id. jcode's spark window has no codex identity and stays L1-only.

Testing

Built test-first for the L2 module. Colocated unit tests in usage/shared_cache/tests.rs (9 tests, all passing) cover:

  • write-then-read round-trip for Anthropic and OpenAI
  • byte-level schema match against quota-axi's format (provider/label/source/state fields, exact window ids/labels/kinds/windowSeconds)
  • reading a real quota-axi-written record (its Fable example, verbatim shape)
  • stale-record rejection past the TTL horizon
  • error-state exclusion, proving 429/backoff stays owned by L1
  • schema-version guarding
  • upsert preserving other providers and quota-axi's provider ordering

cargo clippy -p jcode-base is clean.

Coverage gap: the unit tests exercise the L2 module (format, conversions, freshness, backoff exclusion) directly. There is no automated multi-process end-to-end test asserting host-wide fetch rate collapses from N to 1, because that needs multiple live authenticated jcode sessions against the live provider endpoints. The single-fetch property follows from the read-through + TTL logic that the unit tests do cover.

Chris Yuan added 2 commits August 13, 2026 17:12
Each live jcode session kept its own in-memory usage cache, so N sessions
meant N independent pollers hitting the provider usage endpoints, the likely
cause of the constant 429s. quota-axi now owns one host-wide on-disk usage
cache; this points jcode's Anthropic and OpenAI usage fetchers at that same
file so the whole host makes roughly one usage fetch per provider-account per
TTL regardless of session count (Phase 1 of ADR 0031).

Two-layer design: the existing per-session in-memory cache stays as L1; the
shared file (${XDG_CACHE_HOME:-~/.cache}/quota-axi/quotas.json, schemaVersion
1) is L2, read-through and write-through. L2 is scoped to the host-active
account (host-global disk state read the same way by every session and by
quota-axi) and only successful fetches are written through, so all error and
429 backoff stays owned by L1 and existing backoff semantics are preserved.

The on-disk format, window identities, atomic 0600 write, and provider
ordering byte-match quota-axi's merged implementation so both tools share one
file rather than maintaining parallel formats.

Colocated tests cover round-trip conversion, byte-level schema match against
quota-axi, reading a real quota-axi-written record, stale-record rejection,
error-state exclusion (backoff preservation), schema-version guarding, and
upsert ordering.
@yjuyjuy

yjuyjuy commented Aug 14, 2026

Copy link
Copy Markdown
Owner Author

Superseded by #23 (rebased version of the same usage-cache converge change), which is being landed. Closing this original unmerged PR.

@yjuyjuy yjuyjuy closed this Aug 14, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant