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

feat(usage): pace-aware multi-account balancing and priming - #5

Merged
yjuyjuy merged 1 commit into
pace-basefrom
pace-aware-account-switching
Aug 6, 2026
Merged

yjuyjuy merged 1 commit into
pace-basefrom
pace-aware-account-switching

Conversation

@yjuyjuy

@yjuyjuy yjuyjuy commented Aug 6, 2026 •

Copy link
Copy Markdown
Owner

Closes #6

Summary

Adds pace-aware Claude-account balancing and priming to jcode's multi-account handling. Previously jcode switched off an Anthropic account only once it was fully exhausted (both 5h and 7d windows at ~99%), then picked the peer with the most headroom. That is exhaustion-only: it ignores pace (how fast a window burns relative to how much window time remains), so one account's perishable weekly quota could expire unused while another was maxed.

Three features, all opt-out via [provider].pace_aware_account_balancing (default on):

  1. PACE calculation (compute_window_pace): burn-rate pace per window (5h and weekly). A window is "ahead of pace" when usage exceeds the fraction of the cycle elapsed. Weekly pace is suppressed for 24h after a reset (expected is near zero then, so any usage falsely reads as far ahead).

  2. Same-pace balancing (select_balanced_target): instead of draining one account then moving on, proactively consume the soonest-resetting weekly window first (use-it-or-lose-it), keeping accounts at roughly equal pace. Hysteresis is a reset-time margin (two accounts resetting near the same instant never ping-pong) plus a cooldown between proactive switches. A 7d-heavy peer is deprioritized, not excluded, so 5h relief still wins when it is the only option. Failover on an exhausted active account keeps the old most-headroom behavior.

  3. Priming (should_prime): opens an account's never-used 5-hour window (the 0% / 0% with no reset timestamp case) with a single minimal request, so its clock is already counting down when the fleet needs it. "Worth it" trigger: fires only when the active account is under real pressure - ahead of pace on some window, or its 5h window within a margin of the threshold - otherwise priming just burns quota for no benefit. Priming temporarily activates the peer, sends one complete_simple request, and always restores the original account.

claude-swap design choice

The task referenced the captain's claude-swap fork (Python cswap) as a mirror. I built the logic natively in Rust rather than vendoring or shelling out to claude-swap, and mirror its algorithms rather than its process:

  • jcode already exposes per-account 5h/7d ratios and reset times through AccountUsageProbe, so the pace math is small pure functions over existing in-process data.
  • A Python subprocess would need its own separate credential store and cross-runtime plumbing - fragile, with a second source of truth for account state, for no functional gain in a Rust binary.
  • The Rust module deliberately follows claude-swap's shapes: pace.py's modulo-based cycle-start derivation and 24h post-reset suppression, paced_selector.py / autoswitch.py's consume-first landing rule (5h-below-threshold gate, soonest-weekly-reset ordering, 7d-heavy deprioritization), the hysteresis + cooldown anti-flip-flop, and the priming concept. The one deliberate divergence: consume-first is reset-ordered, so its hysteresis is a reset-time gap, not claude-swap's headroom-margin gap (that margin belongs to claude-swap's best strategy).

Hard constraint honored

No credential is reset, rotated, or refreshed by this path beyond the existing override/invalidate that the manual /account switch and exhaustion failover already use. Priming sends only a single minimal inference request.

Verification

Verified against real jcode usage --json output on the captain's live fleet. pace_tests.rs includes a real_fleet_* scenario reproducing the captured live account data exactly (claude-2 active 13%/65%, claude-1 0%/86% with unopened 5h, claude-3 0%/0% never opened, claude-4 token-expired). 22 pace tests cover:

  • the pace math (ahead/behind pace, post-reset suppression, will_last_to_reset projection, 5h vs weekly period handling);
  • the balancing decision (consume-soonest, stay-when-already-soonest, cooldown block, reset-hysteresis block, failover, all-exhausted, 7d-heavy deprioritization, no-candidates);
  • the priming trigger (under pressure, fleet-slack, already-open, exhausted/errored, ahead-of-pace).

Test coverage declaration

Not built strictly test-first, but the pace math and balancing/priming decisions - the entire behavioral core - are pure functions with full unit coverage (22 tests, all green). The live wiring in MultiProvider::auto_select_multi_account_for_provider (override switch + priming request) is not covered by an end-to-end test because it drives real OAuth accounts and inference requests; it reuses the exact override/invalidate path the existing exhaustion failover already uses. Gap: no automated end-to-end test of the live switch/prime side effects.

CI note for the reviewer

scripts/check_guardrails.sh reports two pre-existing failures unrelated to this change: cargo fmt and the oversized-file ratchet both flag crates/jcode-tui/src/tui/ui_header.rs and crates/jcode-app-core/src/agent/turn_streaming_mpsc.rs, which this PR does not touch (their committed ratchet baselines are stale relative to current master). This PR's own additions pass every gate: the swallowed-error, oversized-test, and warning ratchets are all green, and the code-size baseline is bumped only for the intentional jcode-config-types/src/lib.rs config-field addition.

jcode's multi-account policy was exhaustion-only: it switched off an
Anthropic account only once both its 5h and 7d windows hit ~99%, then
picked the peer with the most headroom. That ignores pace - how fast a
window burns relative to how much window time remains - so one account's
perishable weekly quota could expire unused while another was maxed.

Add a native Rust pace module (crates/jcode-base/src/usage/pace.rs)
mirroring the reference fork claude-swap's logic:

1. compute_window_pace: burn-rate pace per window (5h and weekly). A
   window is "ahead of pace" when usage exceeds the fraction of the cycle
   elapsed. Weekly pace is suppressed for 24h after a reset (matches
   claude-swap SUPPRESS_AFTER_RESET_S) since expected is near zero then.

2. select_balanced_target: a same-pace balancing decision. Instead of
   draining one account then moving on, it proactively consumes the
   soonest-resetting weekly window first (use-it-or-lose-it), keeping
   accounts at roughly equal pace. Hysteresis is a reset-time margin
   (two accounts resetting near the same instant never ping-pong) plus a
   cooldown between proactive switches. A 7d-heavy peer is deprioritized,
   not excluded, so 5h relief still wins when it is the only option.
   Failover on an exhausted active account keeps the old most-headroom
   behavior.

3. should_prime: whether to open an account's never-used 5-hour window
   (the 0%/0%-with-no-reset case) with a minimal request, so its clock is
   already counting down when the fleet needs it. Only fires when the
   active account is under real pressure (ahead of pace, or its 5h window
   near threshold) - otherwise priming just burns quota for no benefit.

Wire it into MultiProvider::auto_select_multi_account_for_provider: when
the active account is not yet exhausted and pace balancing is enabled, it
evaluates the balancing decision (switching the active-account override
exactly as the exhaustion failover does) and otherwise considers priming.
Priming temporarily activates the peer, sends ONE minimal inference
request via complete_simple, and always restores the original account.

Design choice: build natively in Rust rather than vendor claude-swap's
Python. jcode already exposes per-account 5h/7d ratios and reset times
through AccountUsageProbe, so the pace math is small pure functions on
existing data. A Python subprocess would need its own separate credential
store and cross-runtime plumbing - fragile for no gain. We mirror
claude-swap's algorithms (consume-first, per-window pace, hysteresis +
cooldown, priming), not its process.

Gated by [provider].pace_aware_account_balancing (default on, opt-out via
config or JCODE_PACE_AWARE_ACCOUNT_BALANCING); off leaves the
exhaustion-only behavior unchanged. No credential is ever reset, rotated,
or refreshed by this path beyond the existing override/invalidate the
manual /account switch already uses.

Verified against real `jcode usage --json` output; pace_tests.rs includes
a real-fleet scenario reproducing the captured live account data. 22 pace
tests cover the pace math, the balancing decision (including flip-flop
prevention), and the priming trigger.
@yjuyjuy
yjuyjuy merged commit a10f856 into pace-base Aug 6, 2026
1 of 2 checks passed
yjuyjuy added a commit that referenced this pull request Aug 6, 2026
)

* feat(tui): show active account label in header model line

* feat(usage): pace-aware multi-account balancing and priming (#5)

jcode's multi-account policy was exhaustion-only: it switched off an
Anthropic account only once both its 5h and 7d windows hit ~99%, then
picked the peer with the most headroom. That ignores pace - how fast a
window burns relative to how much window time remains - so one account's
perishable weekly quota could expire unused while another was maxed.

Add a native Rust pace module (crates/jcode-base/src/usage/pace.rs)
mirroring the reference fork claude-swap's logic:

1. compute_window_pace: burn-rate pace per window (5h and weekly). A
   window is "ahead of pace" when usage exceeds the fraction of the cycle
   elapsed. Weekly pace is suppressed for 24h after a reset (matches
   claude-swap SUPPRESS_AFTER_RESET_S) since expected is near zero then.

2. select_balanced_target: a same-pace balancing decision. Instead of
   draining one account then moving on, it proactively consumes the
   soonest-resetting weekly window first (use-it-or-lose-it), keeping
   accounts at roughly equal pace. Hysteresis is a reset-time margin
   (two accounts resetting near the same instant never ping-pong) plus a
   cooldown between proactive switches. A 7d-heavy peer is deprioritized,
   not excluded, so 5h relief still wins when it is the only option.
   Failover on an exhausted active account keeps the old most-headroom
   behavior.

3. should_prime: whether to open an account's never-used 5-hour window
   (the 0%/0%-with-no-reset case) with a minimal request, so its clock is
   already counting down when the fleet needs it. Only fires when the
   active account is under real pressure (ahead of pace, or its 5h window
   near threshold) - otherwise priming just burns quota for no benefit.

Wire it into MultiProvider::auto_select_multi_account_for_provider: when
the active account is not yet exhausted and pace balancing is enabled, it
evaluates the balancing decision (switching the active-account override
exactly as the exhaustion failover does) and otherwise considers priming.
Priming temporarily activates the peer, sends ONE minimal inference
request via complete_simple, and always restores the original account.

Design choice: build natively in Rust rather than vendor claude-swap's
Python. jcode already exposes per-account 5h/7d ratios and reset times
through AccountUsageProbe, so the pace math is small pure functions on
existing data. A Python subprocess would need its own separate credential
store and cross-runtime plumbing - fragile for no gain. We mirror
claude-swap's algorithms (consume-first, per-window pace, hysteresis +
cooldown, priming), not its process.

Gated by [provider].pace_aware_account_balancing (default on, opt-out via
config or JCODE_PACE_AWARE_ACCOUNT_BALANCING); off leaves the
exhaustion-only behavior unchanged. No credential is ever reset, rotated,
or refreshed by this path beyond the existing override/invalidate the
manual /account switch already uses.

Verified against real `jcode usage --json` output; pace_tests.rs includes
a real-fleet scenario reproducing the captured live account data. 22 pace
tests cover the pace math, the balancing decision (including flip-flop
prevention), and the priming trigger.

Co-authored-by: jeremy <[email protected]>

* Add too_many_arguments expect on start_processing_message

The merge of the ScheduleWakeup/opus-oauth branch (which added the
client_terminal_env parameter, bringing the arg count to 8) trips
clippy::too_many_arguments. Silence it with a scoped #[expect], matching
the existing handle_client attribute in the same module.

---------

Co-authored-by: jeremy <[email protected]>
Co-authored-by: firstmate crewmate <firstmate@localhost>
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.

2 participants