This repository was archived by the owner on Sep 30, 2026. It is now read-only.
Repository navigation
feat(usage): pace-aware multi-account balancing and priming - #5
Merged
Merged
Conversation
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
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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):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).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.Priming (
should_prime): opens an account's never-used 5-hour window (the0% / 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 onecomplete_simplerequest, and always restores the original account.claude-swap design choice
The task referenced the captain's
claude-swapfork (Pythoncswap) 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:AccountUsageProbe, so the pace math is small pure functions over existing in-process data.pace.py's modulo-based cycle-start derivation and 24h post-reset suppression,paced_selector.py/autoswitch.py'sconsume-firstlanding 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'sbeststrategy).Hard constraint honored
No credential is reset, rotated, or refreshed by this path beyond the existing override/invalidate that the manual
/account switchand exhaustion failover already use. Priming sends only a single minimal inference request.Verification
Verified against real
jcode usage --jsonoutput on the captain's live fleet.pace_tests.rsincludes areal_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:will_last_to_resetprojection, 5h vs weekly period handling);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.shreports two pre-existing failures unrelated to this change:cargo fmtand the oversized-file ratchet both flagcrates/jcode-tui/src/tui/ui_header.rsandcrates/jcode-app-core/src/agent/turn_streaming_mpsc.rs, which this PR does not touch (their committed ratchet baselines are stale relative to currentmaster). 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 intentionaljcode-config-types/src/lib.rsconfig-field addition.