fix: Multi-account rotation bugs, performance improvements, and enhanced test coverage#5
Merged
Conversation
…ced test coverage ## Bug Fixes - Fix true round-robin rotation: accounts now rotate on every request, not just on rate-limit - Add independent per-family cursors (cursorByFamily) to prevent cross-family interference - Remove incorrect markSwitched() call after rate-limiting that pointed back to blocked account - Fix cursor indexing into filtered arrays for consistent round-robin behavior ## Performance Improvements - Convert sync I/O to async + add memory caching in lib/prompts/codex.ts - Add debounced saveToDisk() with saveToDiskDebounced() and flushPendingSave() methods - Parallelize hydrateEmails() with Promise.all instead of sequential loop - Remove redundant loadAccounts() call, add hasRefreshToken() method - Refactor handleErrorResponse() to read response body once (safeReadBody) ## Error Handling Improvements - Add TokenFailureReason type with detailed error info (http_error, invalid_response, network_error, missing_refresh, unknown) - Add logging to 8 previously swallowed catch blocks across index.ts, lib/auth/auth.ts, lib/auth/server.ts - Increase OAuth poll timeout from 60s to 5 minutes ## Race Condition Fix - Add withStorageLock() mutex pattern in lib/storage.ts for concurrent write safety ## New Test Coverage (+81 tests) - test/cli.test.ts: 17 tests for CLI module with mocked readline - test/model-map.test.ts: 20 tests for model mapping functions - test/storage-async.test.ts: 23 tests for storage async operations - test/rotation-integration.test.ts: 21 tests for multi-account rotation - Updated test/accounts.test.ts with 3 new rotation tests ## Verification - Build: Clean (tsc) - Tests: 331 passed (was 250, +32% coverage) - Lint: Clean (eslint)
ndycode
added a commit
that referenced
this pull request
Apr 6, 2026
…improvements fix: Multi-account rotation bugs, performance improvements, and enhanced test coverage
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 join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
This PR contains comprehensive fixes for multi-account rotation bugs, significant performance improvements, enhanced error handling, and expanded test coverage.
Bug Fixes
cursorByFamilyto prevent cross-family interference when switching accountsPerformance Improvements
lib/prompts/codex.tswith in-memory cachingsaveToDiskDebounced()andflushPendingSave()methods to reduce disk writeshydrateEmails()from sequential loop toPromise.allloadAccounts()call, addedhasRefreshToken()methodhandleErrorResponse()to read response body once withsafeReadBody()Error Handling Improvements
TokenFailureReasontype with reasons:http_error,invalid_response,network_error,missing_refresh,unknownRace Condition Fix
withStorageLock()pattern inlib/storage.tsfor concurrent write safetyNew Test Coverage (+81 tests)
test/cli.test.tstest/model-map.test.tstest/storage-async.test.tstest/rotation-integration.test.tstest/accounts.test.tsVerification
Files Changed
index.ts- Debounced saves, parallel hydration, improved error handlinglib/accounts.ts- Per-family cursors, true round-robin, debounced save methodslib/auth/auth.ts- Detailed token failure reasonslib/auth/server.ts- Extended timeout, improved logginglib/prompts/codex.ts- Async I/O with memory cachinglib/request/fetch-helpers.ts- Single body read optimizationlib/storage.ts- Mutex for concurrent writeslib/types.ts- TokenFailureReason type