Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 12
feat(core): add multi-account pool with automatic rate-limit failover#214
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base:main
Are you sure you want to change the base?
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
9061e2584dd3fb92cac6f902c964a7a40f34e297a00d8859a313f2ca3996fdab1aa21d88a595762ff7458641ed7b37270bFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| // CHANGE: add API-level account pool service with persistence and rate-limit monitoring | ||
| // WHY: enable automatic switching between registered accounts when one hits API rate limits | ||
| // QUOTE(ТЗ): "Сделать возможность регистрировать много аккаунтов codex, claude code и когда на одном лимиты закаончиваются он переходит на другой аккаунт" | ||
| // REF: issue-213 | ||
| // SOURCE: n/a | ||
| // FORMAT THEOREM: ∀op ∈ PoolOperation: op(state) → persist(nextState) ∧ consistent(nextState) | ||
| // PURITY: SHELL | ||
| // EFFECT: Effect<Result, ApiError> | ||
| // INVARIANT: pool state is persisted to disk after every mutation; in-memory state is source of truth | ||
| // COMPLEXITY: O(n) per operation where n = total accounts | ||
| import { defaultProjectsRoot } from "@effect-template/lib/usecases/path-helpers" | ||
| import type { | ||
| AccountPoolProvider, | ||
| AccountPoolState, | ||
| AccountEntry, | ||
| RateLimitEvent | ||
| } from "@effect-template/lib/core/account-pool-domain" | ||
| import { | ||
| addAccount, | ||
| removeAccount, | ||
| markRateLimited, | ||
| clearCooldown, | ||
| selectNextAvailable, | ||
| advanceActiveIndex, | ||
| listAccounts, | ||
| listAllAccounts, | ||
| poolSummary, | ||
| emptyPoolState | ||
| } from "@effect-template/lib/usecases/account-pool" | ||
| import { detectRateLimit } from "@effect-template/lib/usecases/rate-limit-detector" | ||
| import { promises as fs } from "node:fs" | ||
| import { join } from "node:path" | ||
| let poolState: AccountPoolState = emptyPoolState(new Date().toISOString()) | ||
| let initialized = false | ||
| const nowIso = (): string => new Date().toISOString() | ||
| const stateFilePath = (): string => | ||
| join(defaultProjectsRoot(process.cwd()), ".orch", "state", "account-pool.json") | ||
| const persistState = async (): Promise<void> => { | ||
| const filePath = stateFilePath() | ||
| await fs.mkdir(join(filePath, ".."), { recursive: true }) | ||
| await fs.writeFile(filePath, JSON.stringify(poolState, null, 2), "utf8") | ||
| } | ||
| const persistBestEffort = (): void => { | ||
| void persistState().catch(() => { | ||
| // best effort | ||
| }) | ||
| } | ||
| export const initializeAccountPool = async (): Promise<void> => { | ||
| if (initialized) { | ||
| return | ||
| } | ||
| const filePath = stateFilePath() | ||
| const exists = await fs.stat(filePath).then(() => true).catch(() => false) | ||
| if (exists) { | ||
| const raw = await fs.readFile(filePath, "utf8") | ||
| const parsed = JSON.parse(raw) as AccountPoolState | ||
| poolState = { | ||
| pools: parsed.pools ?? [], | ||
| updatedAt: parsed.updatedAt ?? nowIso() | ||
| } | ||
Comment on lines
+63
to
+68
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: #!/bin/bash# Verify unchecked parse+cast path in this service.
rg -n -C3 'JSON\.parse\(raw\)|as AccountPoolState|poolState =' packages/api/src/services/account-pool.tsRepository: ProverCoderAI/docker-git Length of output: 1424 🏁 Script executed: cat -n packages/api/src/services/account-pool.tsRepository: ProverCoderAI/docker-git Length of output: 5905 🏁 Script executed: find packages -name "*account-pool*" -type f | head -20Repository: ProverCoderAI/docker-git Length of output: 246 🏁 Script executed: cat -n packages/lib/src/core/account-pool-domain.tsRepository: ProverCoderAI/docker-git Length of output: 3886 🏁 Script executed: rg -n "decode|validator|parse|Decoder" packages/lib/src/core/account-pool-domain.ts packages/lib/src/usecases/account-pool.ts packages/api/src/services/account-pool.tsRepository: ProverCoderAI/docker-git Length of output: 337 🏁 Script executed: rg -n "Decoder|decoder|Validation|validation|decode" packages/lib/src --type ts | head -30Repository: ProverCoderAI/docker-git Length of output: 3638 🏁 Script executed: cat -n packages/lib/src/usecases/scrap-session-manifest.ts | head -70Repository: ProverCoderAI/docker-git Length of output: 2827 🏁 Script executed: rg -n "Schema\.|parseJson" packages/lib/src/core/ packages/api/src/Repository: ProverCoderAI/docker-git Length of output: 6553 🏁 Script executed: rg -n "AccountPoolState|AccountPoolStateSchema" packages/ --type tsRepository: ProverCoderAI/docker-git Length of output: 2372 🏁 Script executed: cat -n packages/api/src/api/schema.ts | grep -A20 "AccountPool"Repository: ProverCoderAI/docker-git Length of output: 1184 🏁 Script executed: rg -n "AccountPoolStateSchema" packages/ --type tsRepository: ProverCoderAI/docker-git Length of output: 50 🏁 Script executed: rg -n "fs.readFile|JSON.parse" packages/api/src packages/lib/src -A5 | head -50Repository: ProverCoderAI/docker-git Length of output: 4238 Validate persisted JSON instead of asserting Line 64 uses 🤖 Prompt for AI Agents | ||
| } | ||
| initialized = true | ||
| } | ||
| export const addPoolAccount = ( | ||
| provider: AccountPoolProvider, | ||
| label: string | ||
| ): AccountPoolState => { | ||
| const now = nowIso() | ||
| poolState = addAccount(poolState, provider, label, now) | ||
| persistBestEffort() | ||
| return poolState | ||
| } | ||
| export const removePoolAccount = ( | ||
| provider: AccountPoolProvider, | ||
| label: string | ||
| ): AccountPoolState => { | ||
| const now = nowIso() | ||
| poolState = removeAccount(poolState, provider, label, now) | ||
| persistBestEffort() | ||
| return poolState | ||
| } | ||
| export const markAccountRateLimited = ( | ||
| event: RateLimitEvent | ||
| ): AccountPoolState => { | ||
| const now = nowIso() | ||
| poolState = markRateLimited(poolState, event, now) | ||
| persistBestEffort() | ||
| return poolState | ||
| } | ||
| export const clearAccountCooldown = ( | ||
| provider: AccountPoolProvider, | ||
| label: string | ||
| ): AccountPoolState => { | ||
| const now = nowIso() | ||
| poolState = clearCooldown(poolState, provider, label, now) | ||
| persistBestEffort() | ||
| return poolState | ||
| } | ||
| export const selectNextPoolAccount = ( | ||
| provider: AccountPoolProvider | ||
| ): AccountEntry | undefined => { | ||
| const now = nowIso() | ||
| const account = selectNextAvailable(poolState, provider, now) | ||
| if (account !== undefined) { | ||
| poolState = advanceActiveIndex(poolState, provider, now) | ||
| persistBestEffort() | ||
| } | ||
| return account | ||
| } | ||
| export const listPoolAccounts = ( | ||
| provider: AccountPoolProvider | ||
| ): ReadonlyArray<AccountEntry> => | ||
| listAccounts(poolState, provider) | ||
| export const listAllPoolAccounts = (): ReadonlyArray<AccountEntry> => | ||
| listAllAccounts(poolState) | ||
| export const getPoolSummary = ( | ||
| provider: AccountPoolProvider | ||
| ): { | ||
| readonly total: number | ||
| readonly available: number | ||
| readonly coolingDown: number | ||
| readonly activeLabel: string | undefined | ||
| } => poolSummary(poolState, provider, nowIso()) | ||
| export const getPoolState = (): AccountPoolState => poolState | ||
| /** | ||
| * Check an agent output line for rate-limit signals. | ||
| * If a rate-limit is detected, marks the account as rate-limited | ||
| * and returns the event for the caller to act upon. | ||
| * | ||
| * @effect mutates poolState on detection | ||
| */ | ||
| export const checkLineForRateLimit = ( | ||
| provider: AccountPoolProvider, | ||
| label: string, | ||
| line: string | ||
| ): RateLimitEvent | undefined => { | ||
| const now = nowIso() | ||
| const event = detectRateLimit(provider, label, line, now) | ||
| if (event !== undefined) { | ||
| markAccountRateLimited(event) | ||
| } | ||
| return event | ||
| } | ||
Comment on lines
+12
to
+162
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: #!/bin/bash# Verify direct Promise/async IO usage and missing Effect composition in this file.
rg -n -C2 'async |Promise<|await |from "node:fs"|from "node:path"|Effect\.|Layer\.|Context\.Tag' packages/api/src/services/account-pool.tsRepository: ProverCoderAI/docker-git Length of output: 1102 Refactor to Effect + Layer pattern for filesystem and state management. This SHELL service directly imports 🤖 Prompt for AI Agents | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Initialization errors are silently swallowed without logging.
If
initializeAccountPool()fails, the error is caught and discarded. This can hide critical issues (corrupted state file, permission errors) that operators would want to know about. At minimum, log the failure before continuing.Proposed fix to log errors before swallowing
🤖 Prompt for AI Agents