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

feat(accounts): poll-driven account re-evaluation between turns - #12

Merged
yjuyjuy merged 7 commits into
masterfrom
fm/account-selection-periodic-reeval
Aug 14, 2026
Merged

yjuyjuy merged 7 commits into
masterfrom
fm/account-selection-periodic-reeval

Conversation

@yjuyjuy

@yjuyjuy yjuyjuy commented Aug 12, 2026

Copy link
Copy Markdown
Owner

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. The MultiProvider impl runs the same strategy dispatch as startup (pace / priority / exhaustion-only); JcodeProvider delegates to its inner MultiProvider. 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 via JCODE_ACCOUNT_REEVAL_INTERVAL_SECS; 0 disables poll-driven re-evaluation), so it is cheap to call every turn.
  • Turn-loop hook: the agent turn loop calls 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 pace and priority strategies 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 master base and does not depend on #11.

Validation

cargo build, cargo clippy, and cargo test clean for jcode-base, jcode-provider-core, jcode-app-core, and the full jcode binary. 4 new unit tests cover the interval parse (unset/malformed fall back to the 60s default, 0 disables, a valid value is honored). Also registered the pre-existing JCODE_PACE_AWARE_ACCOUNT_BALANCING env key in CONFIG_ENV_KEYS (read in env_overrides.rs but never registered), which had left config_env_fingerprint_tracks_every_apply_env_override_var failing on the base. The only failing tests on this machine are 6 pre-existing auth::cursor tests that require the sqlite3 CLI, which is absent here.

1jehuang and others added 7 commits August 12, 2026 12:03
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
@yjuyjuy
yjuyjuy merged commit f7502ee into master Aug 14, 2026
10 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