refactor(runtime): extract rotation-proxy closure state (phase 2) - #548
refactor(runtime): extract rotation-proxy closure state (phase 2)#548ndycode wants to merge 1 commit into
Conversation
Phase 2 of the §4.1.3 rotation-proxy decomposition (follows the phase-1 carve, PR #532). The closure state inside startRuntimeRotationProxy is now an explicit RotationProxyState container created once at startup and passed to plain module-level functions; function bodies are unchanged apart from closure-variable -> state-field references. Module map: - lib/runtime/rotation-proxy-state.ts (116 lines): RotationProxyState container (status, session-affinity store, thread-goal fallbacks, rotation stickiness, active AccountManager) plus the deduped recoverStaleRuntimeState reload. - lib/runtime/rotation-account-selection.ts (257 lines): chooseAccount and the shared linear-scan fallback. - lib/runtime/rotation-token-refresh.ts (141 lines): ensureFreshAccessToken, the per-manager refresh-commit dedupe WeakMap, and applyMonotonicAuthCooldown. - lib/runtime/rotation-storage-meta.ts (206 lines): the content-hash storage-meta cache (readStorageMetaFromDisk and friends). lib/runtime-rotation-proxy.ts: 2,099 -> 1,505 lines. Every previously exported symbol is still exported from lib/runtime-rotation-proxy.ts via re-exports, so lib/index.ts, scripts/codex.js, scripts/codex-app-router.js, and all test imports are untouched. The HTTP plumbing helpers (request parsing/forwarding, writeJson and friends) deliberately stay in runtime-rotation-proxy.ts: moving them would relocate the entire module rather than break up the closure state. https://claude.ai/code/session_01XNtnkLbBiXZxfQQYLMpucB
|
Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits. |
|
Warning Review limit reached
More reviews will be available in 9 minutes and 26 seconds. Learn how PR review limits work. Your organization has run out of usage credits. Purchase more in the billing tab. ⌛ How to resolve this issue?After more reviews become available, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available. Please see our Fair Usage Limits Policy for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: Organization UI Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (5)
✨ Finishing Touches🧪 Generate unit tests (beta)
✨ Simplify code
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
The codex-manager and rotation-proxy decompositions are now fully delivered (monolith 3,810 -> 690; proxy 2,498 -> 1,505). Remaining deferred work narrows to the giant-suite mock-factory migrations. https://claude.ai/code/session_01XNtnkLbBiXZxfQQYLMpucB
…hase2 refactor(runtime): extract rotation-proxy closure state (phase 2)
Summary
Phase 2 of the
runtime-rotation-proxy.tsdecomposition — audit roadmap §4.1.3 (docs/audits/AUDIT_2026-06-10.md, PR #522): the closure-entangled state that phase 1 deliberately deferred moves into explicit state-object modules. The proxy shrinks 2,099 → 1,505 lines (2,498 → 1,505 across both phases). Zero behavior change.Extractions (all new modules; none imports
runtime-rotation-proxy.tsback)lib/runtime/rotation-proxy-state.tsRotationProxyStatecontainer (status object, session-affinity store, thread-goal fallbacks, rotation stickiness, activeAccountManager, stale-reload dedupe fields) +createRotationProxyState+recoverStaleRuntimeStatelib/runtime/rotation-account-selection.tschooseAccount+chooseLinearScanFallback, verbatimlib/runtime/rotation-token-refresh.tsensureFreshAccessToken, the refresh-commit dedupeWeakMap,applyMonotonicAuthCooldown,DEFAULT_AUTH_FAILURE_COOLDOWN_MSlib/runtime/rotation-storage-meta.tsStorageMeta, content-hash cache,readStorageMetaFromDisk,readPinnedAccountIndexFromDisk,resetPinCacheForTesting,maybeInvalidateAffinityFromDisk, verbatimThe request handler (
handleRequest/handleRequestInner, containing the rotation loop) is lifted out of thestartRuntimeRotationProxyclosure into module-level functions taking the state object; every closure variable became a state field.git diff -wshows only state-field substitutions, import/re-export wiring, and the lifted function headers. All previously-exported symbols remain exported fromlib/runtime-rotation-proxy.ts; non-facade cross-module exports are/** @internal */. The 3 call sites (lib/index.ts,scripts/codex.js,scripts/codex-app-router.js) and every test file are untouched.Deliberately not moved: the HTTP plumbing (request-context builders,
writeJson/writePoolExhausted, header/auth helpers,persistRuntimeActiveAccount, server lifecycle). The rotation loop depends on ~15 such helpers; moving them would relocate essentially the whole module rather than de-entangle closure state.Validation
npm run typecheck; eslint on all 5 touched files--max-warnings=0npx madge --circular: no cycle involves any rotation module (the 27 pre-existing storage/accounts cycles are unchanged)::1environment failuresgit stash push -uagainst base4d5dd51: identical counts and identical failing test names on both sidesRisk / Rollback
Mechanical closure-state extraction (riskier than phase 1 by nature, hence the dedicated PR); revert the single phase-2 commit.
https://claude.ai/code/session_01XNtnkLbBiXZxfQQYLMpucB
Generated by Claude Code
note: greptile review for oc-chatgpt-multi-auth. cite files like
lib/foo.ts:123. confirm regression tests + windows concurrency/token redaction coverage.Greptile Summary
phase 2 of the
runtime-rotation-proxy.tsdecomposition: extracts closure-entangled state into four new modules (rotation-proxy-state,rotation-account-selection,rotation-storage-meta,rotation-token-refresh) and liftshandleRequest/handleRequestInnerout of thestartRuntimeRotationProxyclosure into module-level functions that take an explicitRotationProxyStateobject. all closure variable accesses becomestate.fieldreads with no semantic change.RotationProxyStatecontainer (rotation-proxy-state.ts): holds all mutable per-instance fields;recoverStaleRuntimeStatecorrectly deduplicates concurrent stale-reload attempts via a promise ref and a 1 s wall-clock guard;close()retains the correctstate.activeAccountManagerreference semantics.rotation-account-selection.ts,rotation-storage-meta.ts,rotation-token-refresh.ts):chooseAccount,readStorageMetaFromDisk(windows mtime-collision-safe, path-keyed cache), andensureFreshAccessToken(WeakMap commit dedup) are moved unchanged; all previously-exported symbols are re-exported throughruntime-rotation-proxy.ts.Confidence Score: 4/5
mechanical closure extraction with no behavior change; re-export surface is intact and the routing-mutex concurrency contract is fully preserved across all four new modules.
all four new modules are verbatim lifts or direct closure-to-state-field substitutions; the stale-reload dedup, token-refresh WeakMap, and windows-safe mtime cache each retain exactly their original semantics. the only gap is that none of the four new modules have a dedicated unit test file — recoverStaleRuntimeState, ensureFreshAccessToken, and the commitRefreshedAuthOnce dedup path are covered only through the integration proxy suite, leaving individual state paths (e.g. the 1s reload dedup boundary, the commit-once collision) untested in isolation.
lib/runtime/rotation-proxy-state.ts and lib/runtime/rotation-token-refresh.ts — both carry observable stateful paths (reload dedup, refresh commit dedup) with no dedicated test file.
Important Files Changed
Prompt To Fix All With AI
Reviews (1): Last reviewed commit: "refactor(runtime): extract rotation-prox..." | Re-trigger Greptile