This repository was archived by the owner on Sep 30, 2026. It is now read-only.
Repository navigation
feat(accounts): poll-driven account re-evaluation between turns - #12
Merged
Merged
Conversation
Account selection previously fired only at provider construction and explicit provider switch, so a priority strategy could not return to a reset primary or fall back off a capped one, and pace balancing could not keep rebalancing, until the process restarted or the user switched manually. Add a poll-driven re-evaluation that runs at each turn boundary: - New Provider::reselect_account (default no-op) on the provider trait; the MultiProvider impl runs the same strategy dispatch as startup (pace / priority / exhaustion-only) and invalidates cached credentials on a switch so the next request uses the new account. JcodeProvider delegates to its inner MultiProvider. - MultiProvider::reevaluate_account_selection coarsely time-gates the expensive per-account usage probe (default 60s, overridable via JCODE_ACCOUNT_REEVAL_INTERVAL_SECS, 0 disables), so calling it every turn adds no per-turn latency. - The agent turn loop calls self.provider.reselect_account() once at the top of each turn. It is a no-op for providers without multi-account support. This benefits both the pace and priority strategies equally: return-on-reset and fall-back-on-cap now take effect continuously at poll cadence, not just at startup. Also register the pre-existing JCODE_PACE_AWARE_ACCOUNT_BALANCING env key in CONFIG_ENV_KEYS, which was read in env_overrides.rs but never registered, leaving config_env_fingerprint_tracks_every_apply_env_override_var failing on the base. Tests: 4 new unit tests for the interval parse (unset/malformed default, 0 disables, valid honored). jcode-base, jcode-provider-core, jcode-app-core, and the full jcode binary build + clippy clean; jcode-base tests pass except 6 pre-existing auth::cursor tests that require the absent sqlite3 CLI.
# Conflicts: # scripts/swallowed_error_budget.json
# Conflicts: # crates/jcode-base/src/config.rs # crates/jcode-base/src/config/default_file.rs
# Conflicts: # crates/jcode-base/src/provider/mod.rs # scripts/code_size_budget.json # scripts/swallowed_error_budget.json
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.
Account selection previously fired only at provider construction and explicit provider switch, so the new priority strategy could not actually return to a reset primary or fall back off a capped account, and pace balancing could not keep rebalancing, until the process restarted or the user switched manually. This PR adds the poll-driven re-evaluation that makes those behaviors take effect continuously.
What this adds
Provider::reselect_account(new trait method, default no-op): re-evaluates same-provider account selection against current usage and switches the active account if the configured strategy warrants it, invalidating cached credentials so the next request uses the new account. TheMultiProviderimpl runs the same strategy dispatch as startup (pace / priority / exhaustion-only);JcodeProviderdelegates to its innerMultiProvider. It is a no-op for providers without multi-account support.MultiProvider::reevaluate_account_selection: coarsely time-gates the expensive per-account usage probe (default 60s, overridable viaJCODE_ACCOUNT_REEVAL_INTERVAL_SECS;0disables poll-driven re-evaluation), so it is cheap to call every turn.self.provider.reselect_account()once at the top of each turn. A turn boundary is the natural cadence: the provider is already in hand, only active sessions re-evaluate (no wall-clock churn across idle sessions), and the cooldown keeps the probe from running more than once per interval.This benefits both the
paceandprioritystrategies equally.Why a turn-boundary hook (not a wall-clock server timer)
The Anthropic runtime caches its OAuth access token per provider instance until near-expiry, so changing the account override alone does not re-route requests - the switch must call
invalidate_credentials()on the live provider instance. The turn loop already holds that live provider, so a turn-boundary hook reaches it cleanly, re-evaluates against fresh usage only for sessions that are actually working, and is trivially cooldown-gated.Relationship to the priority-strategy PR
This is the poll-driven half that was intentionally split out of the priority-strategy PR (#11). The two are orthogonal and can land in either order; this branch is based on the same
masterbase and does not depend on #11.Validation
cargo build,cargo clippy, andcargo testclean for jcode-base, jcode-provider-core, jcode-app-core, and the fulljcodebinary. 4 new unit tests cover the interval parse (unset/malformed fall back to the 60s default,0disables, a valid value is honored). Also registered the pre-existingJCODE_PACE_AWARE_ACCOUNT_BALANCINGenv key inCONFIG_ENV_KEYS(read inenv_overrides.rsbut never registered), which had leftconfig_env_fingerprint_tracks_every_apply_env_override_varfailing on the base. The only failing tests on this machine are 6 pre-existingauth::cursortests that require thesqlite3CLI, which is absent here.