Skip to content

fix: Multi-account rotation bugs, performance improvements, and enhanced test coverage#5

Merged
ndycode merged 1 commit into
mainfrom
fix/rotation-bugs-and-performance-improvements
Jan 23, 2026
Merged

fix: Multi-account rotation bugs, performance improvements, and enhanced test coverage#5
ndycode merged 1 commit into
mainfrom
fix/rotation-bugs-and-performance-improvements

Conversation

@ndycode
Copy link
Copy Markdown
Owner

@ndycode ndycode commented Jan 23, 2026

Summary

This PR contains comprehensive fixes for multi-account rotation bugs, significant performance improvements, enhanced error handling, and expanded test coverage.

Bug Fixes

  • True round-robin rotation: Accounts now rotate on every request, not just on rate-limit failover
  • Independent per-family cursors: Added cursorByFamily to prevent cross-family interference when switching accounts
  • Fixed markSwitched() bug: Removed incorrect call after rate-limiting that pointed back to blocked account
  • Fixed cursor indexing: Cursor now indexes into accounts array directly for consistent round-robin behavior

Performance Improvements

  • Async I/O + memory caching: Converted sync I/O to async in lib/prompts/codex.ts with in-memory caching
  • Debounced saves: Added saveToDiskDebounced() and flushPendingSave() methods to reduce disk writes
  • Parallel hydration: Changed hydrateEmails() from sequential loop to Promise.all
  • Removed redundant calls: Eliminated duplicate loadAccounts() call, added hasRefreshToken() method
  • Single body read: Refactored handleErrorResponse() to read response body once with safeReadBody()

Error Handling Improvements

  • Detailed token failure info: Added TokenFailureReason type with reasons: http_error, invalid_response, network_error, missing_refresh, unknown
  • Logging for catch blocks: Added logging to 8 previously swallowed catch blocks
  • Extended OAuth timeout: Increased poll timeout from 60s to 5 minutes

Race Condition Fix

  • Storage mutex: Added withStorageLock() pattern in lib/storage.ts for concurrent write safety

New Test Coverage (+81 tests)

Test File Tests Coverage
test/cli.test.ts 17 CLI module with mocked readline
test/model-map.test.ts 20 Model mapping functions
test/storage-async.test.ts 23 Storage async operations
test/rotation-integration.test.ts 21 Multi-account rotation scenarios
test/accounts.test.ts +3 New rotation tests

Verification

  • ✅ Build: Clean (tsc)
  • ✅ Tests: 331 passed (was 250, +32% coverage)
  • ✅ Lint: Clean (eslint)

Files Changed

  • index.ts - Debounced saves, parallel hydration, improved error handling
  • lib/accounts.ts - Per-family cursors, true round-robin, debounced save methods
  • lib/auth/auth.ts - Detailed token failure reasons
  • lib/auth/server.ts - Extended timeout, improved logging
  • lib/prompts/codex.ts - Async I/O with memory caching
  • lib/request/fetch-helpers.ts - Single body read optimization
  • lib/storage.ts - Mutex for concurrent writes
  • lib/types.ts - TokenFailureReason type
  • 4 new test files + updates to existing tests

…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 ndycode merged commit 7c27d64 into main Jan 23, 2026
ndycode added a commit that referenced this pull request Apr 6, 2026
…improvements

fix: Multi-account rotation bugs, performance improvements, and enhanced test coverage
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant