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

Unify ScheduleWakeup #706 fix + pace-aware account balancing - #7

Merged
yjuyjuy merged 4 commits into
masterfrom
fm/jcode-schedulewakeup-plus-pace
Aug 6, 2026
Merged

yjuyjuy merged 4 commits into
masterfrom
fm/jcode-schedulewakeup-plus-pace

Conversation

@yjuyjuy

@yjuyjuy yjuyjuy commented Aug 6, 2026

Copy link
Copy Markdown
Owner

Merges origin/pace-base (#5 pace-aware multi-account balancing) into origin/master (1jehuang#706 ScheduleWakeup OAuth schema fix + #4 Opus-Pro-OAuth) so a single fork build carries BOTH features.

Conflict resolutions (the 2 expected)

  • crates/jcode-tui/src/tui/ui_header.rs: both sides were formatting-only variants of the identical active-account-label logic. Kept master's rustfmt form; the active-account suffix display is preserved.
  • scripts/code_size_budget.json: took the max of each divergent entry (config-types/src/lib.rs -> 1504, desktop2/src/editor.rs -> 1373) so neither feature trips the size budget.

Extra fix

  • Added a scoped #[expect(clippy::too_many_arguments)] on start_processing_message (master's side raised it to 8 args via client_terminal_env), matching the existing handle_client attribute in the same module.

Verification (Linux, cargo build --release)

  • Build: clean.
  • 706 regression test oauth_forwards_schedule_backend_schema_under_scheduled_wakeup_name (jcode-provider-anthropic): PASS.
  • Pace suite (cargo test -p jcode-base pace): 43 passed, 0 failed.
  • Note: pre-existing clippy --all-targets dead-code lints on Linux also exist on origin/master (macOS-cfg helpers); not introduced by this merge.

1jehuang and others added 4 commits August 1, 2026 19:54
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]>
…ewakeup-plus-pace

# Conflicts:
#	crates/jcode-tui/src/tui/ui_header.rs
#	scripts/code_size_budget.json
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.
@yjuyjuy
yjuyjuy merged commit 63a5542 into master Aug 6, 2026
5 of 11 checks passed
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