[bug] Constant losing connection, rate limited, VERY VERY slow
Summary
Describe the bug in one paragraph. I had before the other codex-multi-auth plugin but it gave to much problems/headache. Now this plugin constant slows down tons of my spawned swarm of deep agents get tons of rate limited errors, or worse the 'Provider response headers timed out after 10000ms' (then the subagents DO NOT continue untill I press ok and it would go/proceed further)
The other plugin didn't had this problem, is there a way that i can tune/make it more agressive ? I mean it was slow but there I could spawn up to 20 deep agents at the same time though.
Latest verison of opencode, latest verison of this plugin.
What can I do/tweak?
OH EDIT I have 2 20x plans
Coding Plan
Summary
root cause is a default-off concurrency race in account selection plus an unlocked rate-limit marker (lib/accounts.ts:1158-1189, AUDIT-H3/D-07/D-09), which makes concurrent subagents pile onto the same freshly-rate-limited account — fix by defaulting routingMutex to "enabled", adding a mutex-gated markRateLimitedLocked, and defaulting pidOffsetEnabled on to spread parallel processes.
the 10000ms stall is not ours: it's opencode's openai provider headerTimeout — resolved purely by documentation telling users to raise it (our own fetchTimeoutMs is already 60_000).
backfill the missing concurrency regression tests and default-value assertions so the fix can't silently regress.
fill the documentation gap for swarm/high-concurrency tuning and pool sizing across docs/configuration.md, docs/reference/settings.md, and docs/troubleshooting.md.
Design Choices
Design Choice 1: should we flip the concurrency-safety defaults, or keep them opt-in and document them?
Options Considered:
- flip
routingMutexto"enabled"andpidOffsetEnabledtotrueby default, close the rate-limit-marker race, plus docs. - leave defaults untouched, only add documentation telling users to set
CODEX_AUTH_ROUTING_MUTEX=enabledetc.
Chosen Option: 1
Rationale: the race actively amplifies the reported symptom (concurrent selection of a freshly rate-limited account), the oracle already recommended default-enabled, and docs-only leaves the footgun armed for the next user. the audit oracle (docs/audits/MASTER_AUDIT.md, R4) recommended shipping routingMutex: "enabled" as default for a release cycle, but v2.2.1 shipped it opt-in and left legacy unchanged. the ticket is direct evidence that the default is causing real pain.
💡 User Tips
Regenerate the plan with different choices with @coderabbitai <feedback>.
Implementation Steps
Phase 1: make concurrent account selection safe by default
close the selection/rate-limit race so 20 concurrent subagents stop stampeding onto the same account. this is the core bug fix — everything else is tuning and docs on top.
Task 1: default the routing mutex on, and gate the rate-limit marker through it
stop concurrent requests from reading the same cursor and co-selecting an account that's already being rate-limited.
- flip the default in
lib/schemas.ts:80andlib/config.ts:242(DEFAULT_PLUGIN_CONFIG) from"legacy"to"enabled"; keepCODEX_AUTH_ROUTING_MUTEX=legacyas the escape hatch. - add a mutex-gated
markRateLimitedLockedvariant next to the existing*Lockedmethods inlib/accounts.ts(markRateLimitedWithReasonatlib/accounts.ts:1158-1189is currently unlocked in both modes — this is the real hole), routing it throughwithRoutingMutex(this.routingMutexMode, fn)likemarkSwitchedLocked/markAccountCoolingDownLocked(lib/accounts.ts:1083-1140). - in the 429 branch of
lib/runtime-rotation-proxy.ts(~:1171-1185), commit the rate-limit marker via the locked variant before the loopcontinue, so the account is visibly cooling-down to any concurrent request's nextchooseAccountcall. concurrency risk to verify: the marker mutation must be inside the same mutex domain aschooseAccount/cursor-commit (~:962-995) or the barrier buys nothing. - reentrancy: the mutex uses
AsyncLocalStorage(isRoutingMutexHeld) so nested locked calls run inline — confirm the new locked marker isn't called from inside an already-held selection section to avoid a silent double-lock no-op.
Task 2: reduce cross-process contention for parallel agents
each opencode subagent process gets its own process-wide mutex singleton, so the mutex only fixes intra-process races; multiple processes still need deterministic spreading.
- flip
pidOffsetEnableddefault totrueinlib/schemas.ts+lib/config.ts(currentlyfalse). this gives each parallel process a deterministic account-selection offset (seegetPidOffsetEnabled), reducing the "all processes pick account#0" collision the ticket describes. - keep
schedulingStrategydefault"hybrid"(spreads load); no change needed, but note in docs that"sequential"is the wrong choice for swarms since it drains one account and ignores affinity. - windows edge case to flag:
pidOffsetderives fromprocess.pid; confirm the offset math is stable for large win32 pids and doesn't modulo to a constant.
Task 3: regression tests for concurrent selection
the audit findings have no regression coverage for the default path; add it.
- extend
test/runtime-rotation-proxy.test.ts(already has "hands distinct accounts to concurrent enabled-mode requests without deadlock") with a case asserting that when account A returns 429, a concurrent in-flight request does not re-select A after the marker is committed — i.e. the AUDIT-H3 amplification is gone under the new default. - add a unit test in the accounts suite for
markRateLimitedLockedrunning under both"enabled"and"legacy"modes. - missing-test flag: there is currently no test asserting the default config value of
routingMutex/pidOffsetEnabled; add a schema/default assertion so a future refactor can't silently revert this fix.
🤖 Prompt for AI agents
implement phase 1: close the concurrent account-selection race so parallel
subagents stop stampeding onto the same rate-limited account. this is the core
bug fix.
first, flip the default in `lib/schemas.ts:80` and `lib/config.ts:242`
(`DEFAULT_PLUGIN_CONFIG`) for `routingMutex` from `"legacy"` to `"enabled"`,
keeping `CODEX_AUTH_ROUTING_MUTEX=legacy` available as an escape hatch. flip
`pidOffsetEnabled` default to `true` in `lib/schemas.ts` + `lib/config.ts`
(currently `false`) so each parallel process gets a deterministic
account-selection offset via `getPidOffsetEnabled`. leave `schedulingStrategy`
default as `"hybrid"` — no change needed there.
add a mutex-gated `markRateLimitedLocked` variant next to the existing `*Locked`
methods in `lib/accounts.ts`, routing it through
`withRoutingMutex(this.routingMutexMode, fn)` the same way
`markSwitchedLocked`/`markAccountCoolingDownLocked`
(`lib/accounts.ts:1083-1140`) do — `markRateLimitedWithReason`
(`lib/accounts.ts:1158-1189`) is currently unlocked in both modes and this is
the actual hole causing the amplification. in the 429 branch of
`lib/runtime-rotation-proxy.ts` (~`:1171-1185`), commit the rate-limit marker
via this new locked variant *before* the loop `continue`, so the account is
visibly cooling-down to any concurrent request's next `chooseAccount` call.
verify the marker mutation lands inside the same mutex domain as
`chooseAccount`/cursor-commit (~`:962-995`), otherwise the barrier does nothing.
also confirm the new locked marker isn't invoked from inside an already-held
selection section — the mutex uses `AsyncLocalStorage` (`isRoutingMutexHeld`) so
nested locked calls run inline, and a call from within an existing lock would be
a silent double-lock no-op.
for `pidOffsetEnabled`, double check the offset math derived from `process.pid`
is stable for large win32 pids and doesn't collapse to a constant via modulo.
finally, add regression coverage: extend `test/runtime-rotation-proxy.test.ts`
(which already has a "hands distinct accounts to concurrent enabled-mode
requests without deadlock" case) with a test asserting that once account A
returns 429 and the marker is committed, a concurrent in-flight request does not
re-select A. add a unit test in the accounts suite for `markRateLimitedLocked`
under both `"enabled"` and `"legacy"` modes. add a schema/default assertion test
confirming the default values of `routingMutex` and `pidOffsetEnabled`, since no
such test exists today and this fix could otherwise silently regress.
Phase 2: surface tuning knobs and the client-side timeout fix
give users the levers to run swarms and, critically, fix the 10000ms stall which is opencode-side, not ours.
Task 1: document high-concurrency / swarm tuning
fill the doc gap the exploration found (docs/troubleshooting.md, docs/configuration.md, docs/reference/settings.md have zero coverage for concurrent-agent usage).
- add a "running many parallel agents / swarms" section to
docs/configuration.mdand mirror the knobs indocs/reference/settings.md: recommendschedulingStrategy=hybrid,routingMutex=enabled(now default),pidOffsetEnabled=true(now default, previously undocumented), and a rough pool-sizing heuristic (roughly one healthy account per few concurrent agents; more accounts = less 429 contention). - document tuning
fetchTimeoutMs(default60_000) andstreamStallTimeoutMs(default45_000) for slow/loaded upstreams, andretryAllAccountsMaxRetriesfor rotation aggressiveness.
Task 2: add the "Provider response headers timed out after 10000ms" troubleshooting entry
this is the highest-leverage user-facing fix — the error is opencode's provider headerTimeout, not this plugin.
- add a troubleshooting entry in
docs/troubleshooting.mdexplaining the string is emitted by opencode's openai provider (defaultheaderTimeout: 10000), not by codex-multi-auth (whose ownfetchTimeoutMsis60_000), and instruct users to raise the providerheaderTimeout/headerstimeout in their opencode config (per the opencode issue in the web results,60000resolves it). - add a companion entry for "cascading 429s under many parallel agents" pointing at the phase-1 defaults and the pool-sizing guidance, so users landing from the ticket symptom get a direct answer.
🤖 Prompt for AI agents
implement phase 2: surface the tuning knobs for high-concurrency swarms and
document the client-side fix for the `10000ms` header timeout stall.
add a "running many parallel agents / swarms" section to `docs/configuration.md`
and mirror the same knobs in `docs/reference/settings.md`. recommend
`schedulingStrategy=hybrid`, `routingMutex=enabled` (now the default per phase
1), and `pidOffsetEnabled=true` (now the default per phase 1, previously
undocumented). include a rough pool-sizing heuristic — roughly one healthy
account per few concurrent agents, since more accounts means less 429
contention. also document tuning `fetchTimeoutMs` (default `60_000`) and
`streamStallTimeoutMs` (default `45_000`) for slow or loaded upstreams, and
`retryAllAccountsMaxRetries` for controlling rotation aggressiveness.
add a troubleshooting entry in `docs/troubleshooting.md` for "Provider response
headers timed out after 10000ms": explain that this string is emitted by
opencode's openai provider (default `headerTimeout: 10000`), not by
codex-multi-auth (whose own `fetchTimeoutMs` defaults to `60_000`), and instruct
users to raise the provider `headerTimeout`/`headers` timeout in their opencode
config — per the referenced opencode issue, setting it to `60000` resolves it.
add a companion troubleshooting entry for "cascading 429s under many parallel
agents" that points at the phase-1 default changes (`routingMutex`,
`pidOffsetEnabled`) and the pool-sizing guidance, so users landing on this
symptom get a direct, actionable answer.
Research
requests flow client → lib/local-bridge.ts (thin async node http + hono proxy, no concurrency cap) → lib/runtime-rotation-proxy.ts::handleRequestInner (account selection + upstream fetch + rotation-on-failure loop). account state lives in lib/accounts.ts; selection scoring/health/token-bucket in lib/rotation.ts; per-request retry budget via state.maxRuntimeAccountAttempts in lib/runtime/rotation-proxy-state.ts. concurrency coordination is lib/routing-mutex.ts (withRoutingMutex), config surface is lib/schemas.ts + lib/config.ts (getters + DEFAULT_PLUGIN_CONFIG). docs live under docs/configuration.md, docs/reference/settings.md, docs/troubleshooting.md.
the two root causes here:
- concurrency race, off by default.
routingMutexdefaults to"legacy"(lib/config.ts:242,lib/schemas.ts:80), sowithRoutingMutexruns inline with zero locking (lib/routing-mutex.ts:186-207). worse,markRateLimitedWithReason(lib/accounts.ts:1158-1189) is never mutex-gated in any mode. under load, n concurrent requests read the same cursor/active-index and all pick the same account, then all 429 on it before any cooldown marker is visible — this is exactly AUDIT-H3 / D-07 / D-09 (docs/audits/evidence/dim-D-routing.md), still confirmed-open, only mitigated behind an opt-in flag the oracle recommended shipping default-enabled. - the 10000ms timeout is not ours. the plugin's own upstream timeout is
fetchTimeoutMsdefault60_000(lib/config.ts::getFetchTimeoutMs) and the literal string does not exist anywhere in this repo. it comes from opencode's openai providerheaderTimeout(see the opencode issue in web results) — purely client-side config the user must raise.
🚀 Next Steps
🤖 All AI agent prompts combined
Task: 1
implement phase 1: close the concurrent account-selection race so parallel
subagents stop stampeding onto the same rate-limited account. this is the core
bug fix.
first, flip the default in `lib/schemas.ts:80` and `lib/config.ts:242`
(`DEFAULT_PLUGIN_CONFIG`) for `routingMutex` from `"legacy"` to `"enabled"`,
keeping `CODEX_AUTH_ROUTING_MUTEX=legacy` available as an escape hatch. flip
`pidOffsetEnabled` default to `true` in `lib/schemas.ts` + `lib/config.ts`
(currently `false`) so each parallel process gets a deterministic
account-selection offset via `getPidOffsetEnabled`. leave `schedulingStrategy`
default as `"hybrid"` — no change needed there.
add a mutex-gated `markRateLimitedLocked` variant next to the existing `*Locked`
methods in `lib/accounts.ts`, routing it through
`withRoutingMutex(this.routingMutexMode, fn)` the same way
`markSwitchedLocked`/`markAccountCoolingDownLocked`
(`lib/accounts.ts:1083-1140`) do — `markRateLimitedWithReason`
(`lib/accounts.ts:1158-1189`) is currently unlocked in both modes and this is
the actual hole causing the amplification. in the 429 branch of
`lib/runtime-rotation-proxy.ts` (~`:1171-1185`), commit the rate-limit marker
via this new locked variant *before* the loop `continue`, so the account is
visibly cooling-down to any concurrent request's next `chooseAccount` call.
verify the marker mutation lands inside the same mutex domain as
`chooseAccount`/cursor-commit (~`:962-995`), otherwise the barrier does nothing.
also confirm the new locked marker isn't invoked from inside an already-held
selection section — the mutex uses `AsyncLocalStorage` (`isRoutingMutexHeld`) so
nested locked calls run inline, and a call from within an existing lock would be
a silent double-lock no-op.
for `pidOffsetEnabled`, double check the offset math derived from `process.pid`
is stable for large win32 pids and doesn't collapse to a constant via modulo.
finally, add regression coverage: extend `test/runtime-rotation-proxy.test.ts`
(which already has a "hands distinct accounts to concurrent enabled-mode
requests without deadlock" case) with a test asserting that once account A
returns 429 and the marker is committed, a concurrent in-flight request does not
re-select A. add a unit test in the accounts suite for `markRateLimitedLocked`
under both `"enabled"` and `"legacy"` modes. add a schema/default assertion test
confirming the default values of `routingMutex` and `pidOffsetEnabled`, since no
such test exists today and this fix could otherwise silently regress.
===============================================================================
Task: 2
implement phase 2: surface the tuning knobs for high-concurrency swarms and
document the client-side fix for the `10000ms` header timeout stall.
add a "running many parallel agents / swarms" section to `docs/configuration.md`
and mirror the same knobs in `docs/reference/settings.md`. recommend
`schedulingStrategy=hybrid`, `routingMutex=enabled` (now the default per phase
1), and `pidOffsetEnabled=true` (now the default per phase 1, previously
undocumented). include a rough pool-sizing heuristic — roughly one healthy
account per few concurrent agents, since more accounts means less 429
contention. also document tuning `fetchTimeoutMs` (default `60_000`) and
`streamStallTimeoutMs` (default `45_000`) for slow or loaded upstreams, and
`retryAllAccountsMaxRetries` for controlling rotation aggressiveness.
add a troubleshooting entry in `docs/troubleshooting.md` for "Provider response
headers timed out after 10000ms": explain that this string is emitted by
opencode's openai provider (default `headerTimeout: 10000`), not by
codex-multi-auth (whose own `fetchTimeoutMs` defaults to `60_000`), and instruct
users to raise the provider `headerTimeout`/`headers` timeout in their opencode
config — per the referenced opencode issue, setting it to `60000` resolves it.
add a companion troubleshooting entry for "cascading 429s under many parallel
agents" that points at the phase-1 default changes (`routingMutex`,
`pidOffsetEnabled`) and the pool-sizing guidance, so users landing on this
symptom get a direct, actionable answer.
💡 Iterate on the plan with:
`@coderabbitai` <feedback>
Example Feedback
- `@coderabbitai` You can skip phase 3. Add a simple unit test case for phase 2.
- `@coderabbitai` For design choice 1 go ahead with option 3 and replan.
💬 Have feedback or questions? Drop into our discord!
i think this is more related issue on the backend server itself. i get an error sometimes earlier too. let me fix this
i think this is more related issue on the backend server itself. i get an error sometimes earlier too. let me fix this
Cannot wait for the update thanks !