This repository was archived by the owner on Sep 30, 2026. It is now read-only.
Repository navigation
feat(accounts): opt-in ranked-priority account selection strategy - #11
Merged
Merged
Conversation
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.
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.
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
account_selection_strategy:pace(default, existing consume-first balancing),priority(new, opt-in), orexhaustion-only(no proactive rebalancing).account_priority: ranked list, most-preferred first, matched to accounts by email (then a stable label), never the positional numbering.JCODE_ACCOUNT_SELECTION_STRATEGYandJCODE_ACCOUNT_PRIORITY, plus documentation in the default config file.usage/pace.rsselect_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.AccountPacegains anemailfield for stable-identity matching.provider/startup.rs):maybe_pace_balanceis now reached through amaybe_rebalancedispatcher that branches on the strategy. The newmaybe_priority_selectapplies switches through the sameapply_account_overridepath 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_BALANCINGenv key inCONFIG_ENV_KEYS. It was read inenv_overrides.rsbut never registered, leavingconfig_env_fingerprint_tracks_every_apply_env_override_varfailing on the base.Validation
cargo build,cargo clippy, andcargo testclean for the touched crates (jcode-base, jcode-config-types) and the fulljcodebinary 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-existingauth::cursortests that require thesqlite3CLI, which is absent here.