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

feat(accounts): opt-in ranked-priority account selection strategy - #11

Merged
yjuyjuy merged 4 commits into
masterfrom
fm/priority-account-strategy
Aug 13, 2026
Merged

yjuyjuy merged 4 commits into
masterfrom
fm/priority-account-strategy

Conversation

@yjuyjuy

@yjuyjuy yjuyjuy commented Aug 12, 2026

Copy link
Copy Markdown
Owner

Adds a selectable same-provider account-selection strategy alongside the existing pace-aware balancing, implementing the captain-approved ranked-list behavior: always prefer the highest-priority live account, fall back down the list when it is capped, and return to it when its window resets.

Decision

Priority is OPT-IN (pace-aware balancing remains the shipped fleet default), and ranking is keyed on STABLE IDENTITY (account email), not the positional claude-1/claude-2 label, so relabeling accounts never silently reorders priority.

What this PR delivers

  • Config (jcode-config-types, Anthropic scope, defaults unchanged):
    • account_selection_strategy: pace (default, existing consume-first balancing), priority (new, opt-in), or exhaustion-only (no proactive rebalancing).
    • account_priority: ranked list, most-preferred first, matched to accounts by email (then a stable label), never the positional numbering.
    • Env overrides JCODE_ACCOUNT_SELECTION_STRATEGY and JCODE_ACCOUNT_PRIORITY, plus documentation in the default config file.
  • Selection (usage/pace.rs select_priority_target + AccountUsageProbe::priority_decision): walks the ranked order, prefers the highest-priority live account, and uses asymmetric hysteresis - eager to fall back off a capped primary, reluctant to return until a higher-priority account is comfortably below its cap - so a primary hovering at its cap never ping-pongs. Return-on-reset falls out for free: once a primary window resets it is live and below cap, so it wins the next evaluation. AccountPace gains an email field for stable-identity matching.
  • Wiring (provider/startup.rs): maybe_pace_balance is now reached through a maybe_rebalance dispatcher that branches on the strategy. The new maybe_priority_select applies switches through the same apply_account_override path the pace balancer and exhaustion failover already use (never a new token path) and emits priority-specific notices explaining the switch.

Scope note

The design ADR also scoped a poll-driven periodic re-evaluation (return-on-reset / fall-back-on-cap firing continuously). That is split into a follow-up because the running agent has no periodic usage-refresh timer today (only on-demand /usage); the priority strategy fires at exactly the same cadence as the existing pace strategy (provider construction + explicit switch), and adding a periodic timer improves both strategies equally.

Also fixed

Registered the pre-existing JCODE_PACE_AWARE_ACCOUNT_BALANCING env key in CONFIG_ENV_KEYS. It was read in env_overrides.rs but never registered, leaving config_env_fingerprint_tracks_every_apply_env_override_var failing on the base.

Validation

cargo build, cargo clippy, and cargo test clean for the touched crates (jcode-base, jcode-config-types) and the full jcode binary builds clean. 11 new priority-selection unit tests cover stay-on-highest, cap fallback, return-on-reset, asymmetric no-flap hysteresis, email-keying over positional labels, exhausted failover, all-exhausted, cooldown (and that an exhausted failover ignores the cooldown), and unlisted-account last-resort. The only failing tests on this machine are 6 pre-existing auth::cursor tests that require the sqlite3 CLI, which is absent here.

Adds a selectable same-provider account-selection strategy alongside the
existing pace-aware balancing, implementing the captain's ranked-list behavior:
always prefer the highest-priority live account, fall back down the list when it
is capped, and return to it when its window resets.

Config (jcode-config-types, all Anthropic scope, defaults unchanged):
- account_selection_strategy: pace (default, existing consume-first balancing),
  priority (new, opt-in), or exhaustion-only (no proactive rebalancing).
- account_priority: ranked list, most-preferred first, matched to accounts by
  STABLE identity - login email (AnthropicAccount.email) then a stable label -
  never the positional claude-1/claude-2 numbering, so relabeling accounts never
  silently reorders priority.
- Env overrides JCODE_ACCOUNT_SELECTION_STRATEGY and JCODE_ACCOUNT_PRIORITY, and
  documentation in the default config file, mirroring the existing knobs.

Selection (usage/pace.rs select_priority_target + AccountUsageProbe::
priority_decision): walks the ranked order, prefers the highest-priority live
account, and uses asymmetric hysteresis - eager to fall back off a capped
primary, reluctant to return until a higher-priority account is comfortably
below its cap - so a primary hovering at its cap never ping-pongs. Return on
reset falls out for free: once a primary's window resets it is live and below
cap, so it wins the next evaluation. AccountPace gains an email field so the
selector can key on stable identity.

Wiring (provider/startup.rs): maybe_pace_balance is now reached through a
maybe_rebalance dispatcher that branches on account_selection_strategy. The new
maybe_priority_select applies switches through the same apply_account_override
path the pace balancer and exhaustion failover already use (never a new token
path) and emits priority-specific startup notices explaining the switch.

Tests: 11 new priority-selection unit tests covering stay-on-highest, cap
fallback, return-on-reset, asymmetric no-flap hysteresis, email-keying over
positional labels, exhausted failover, all-exhausted, cooldown (and that an
exhausted failover ignores the cooldown), and unlisted-account last-resort.
cargo test -p jcode-base usage::pace = all pass; jcode-base builds clean.
…collapse if

Register JCODE_ACCOUNT_SELECTION_STRATEGY and JCODE_ACCOUNT_PRIORITY (added by
this branch) in CONFIG_ENV_KEYS so the config env fingerprint tracks them, as
the config_env_fingerprint_tracks_every_apply_env_override_var test requires.

Also register the pre-existing JCODE_PACE_AWARE_ACCOUNT_BALANCING key, which was
read in env_overrides.rs since the pace-balancing change but never added to
CONFIG_ENV_KEYS, leaving that fingerprint test failing on the base. Registering
it here makes the suite green.

Collapse the nested if in account_matches_priority_entry to satisfy clippy's
collapsible_if lint.
@yjuyjuy
yjuyjuy merged commit 4d7953e into master Aug 13, 2026
19 of 21 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