Repository navigation
fix(ai): let a small broker pool serve while an account cools down - #3794
Merged
Merged
Conversation
The hand-out floor holds an account back while another account of its vendor can take the work, judged by the gateway's own view of the pool. A consumer applies rules the gateway cannot see: the platform's shared 60-second cooldown after a 429, and OpenAI's required vendor account id. When those drop the one account the gateway counted as servable, the floor-held account arrived as `available: false` like a spent quota, and a two-account pool refused all work for the hour before each refresh. Each token now carries `hold`: `quota` while its quota is spent (also when the floor holds it too), `refresh` while only the floor holds it, null while it is available. `available` keeps its meaning, so a consumer that reads nothing else still skips a floor-held account; one whose own rules leave it no available account may start work on a `refresh` hold, the latest `refresh_at` first, as the gateway would itself.
The credential-rotation lane's header credited `failure_code` to migration 0132, the task-repeat series; 0133_project_agent_runs_failure_code.sql adds it.
A free credential-rotation retry stamped `Math.max(streak, 1)`, so the first one after a clean run read "Auto-retry 1 of 3" on the task card and the next ordinary failure read "Auto-retry 1 of 3" again, while the docs say the attempt count does not advance. A free retry now stamps the attempts actually spent. With none spent it is 0, which the card shows as "Resumed after a token refresh" (en, de, fr) instead of a count; after counted failures it keeps the count it had reached. The task-automation docs say what the card shows.
The gateway holds an account back for its coming token refresh while another account of its vendor can take the work, judged without the platform's own rules. When the platform then drops that other account — the shared 60-second cooldown after a 429, or OpenAI's missing account_id — nothing was left, the start threw CREDENTIAL_BROKER_EXHAUSTED or CREDENTIAL_BROKER_EMPTY, and the immediate retries spent the whole budget in seconds. A two-account pool (A cooling, B inside its refresh floor) failed the task or automation, where before the floor B would have served. Accounts the broker marks `hold: "refresh"` now pass every other filter into a held tier: the durable pick serves one, the latest refresh first, only when no available account is out of its cooldown — never while one can serve, and never on a quota hold. A broker that names no `hold` (the gateway before this release) keeps today's behaviour, so the platform can ship first. A pool whose every account is cooling down now says when the first comes back. A task retry is kicked at once, so the card shows it queued, but its start job waits until then; an automation re-kick's start is held the same way, its time limit counted from the start. Both waits are capped at the cooldown. The refused start still counts as an attempt: a pool that other work keeps cooling must not hold a run in an endless wait, and the attempt after the wait can vend. A start's credential refusal is shown in its own words instead of its serialized payload.
An adversarial review of the first round found: - A 429 still cost two attempts when no other account was healthy: the failed turn counted, and so did its retry, which the cooling pool refused at once. A start refused this way now settles as `credential_cooldown`. When the refused run retried the task's own 429, the wait is free (`freeCooldownWaits`, and `retriedRateLimit` on an automation cursor). Every free wait needs a counted 429 directly behind it, so a pool that other work keeps cooling still cannot loop. The retry history now reads each run's vendor status and card stamp, over a window wide enough for that extra free row. - An automation re-kicked after such a refusal started its conversation over. It now resumes the conversation the refused attempt was to resume, with the reason that cut it (`resumeReason` on the cursor). - A task's comment restart and an automation's answered-ask resume ignored the refusal: they showed its serialized payload and retried into it at once. Both now use the refusal's own words (`runFailureMessage`) and carry its `retryAtMs`. - A free rotation after a progress reset showed "Resumed after a token refresh" where the cut run had shown "Auto-retry 1 of 3". A free rotation's retry now shows whatever the cut run showed, 0 only when that run showed no count, so the docs' "the count does not advance" holds. Docs (en, de, fr), the gateway README order, the manual reference, and a held-order test follow.
An automation agent step whose every start a cooling broker pool refused settled `credential_cooldown`, which the agent-to-run code mapping did not know, so a run whose budget such refusals spent reported `harness_error` through the public API for a turn that never started. Before the cooldown code existed the same run reported `start_failed`. `agentFailureCodeOf` now reports each retry-only agent code as the wire code it refines: `credential_cooldown` as `start_failed`, `credential_rotated` as `harness_error` (the code a rotated 401 always carried). The OpenAPI enum does not change, and a new agent code that is neither on the wire nor mapped fails to compile. A unit test locks the mapping, and two stepper tests check the final run code of an exhausted cooldown chain and of an exhausted rotation streak.
A free credential rotation's retry copied the attempt the cut run showed, even when that run had worked past the progress threshold and so refreshed the budget: a third auto-retry that worked twenty minutes before the refresh cut it resumed as "Auto-retry 3 of 3", and the next ordinary failure then dropped to "1 of 3". The automation lane already stamps 0 in that case. The task lane now stamps 0 too, so the card reads "Resumed after a token refresh", and the docs (en/de/fr) say when it does.
TASK-F47 provokes a free rotation after a run nothing retried and judges the "Resumed after a token refresh" caption in the task dialog and on the task page in en, de and fr, where the German caption wraps. ACCT-21 lists `available` and `hold` among the token fields. The platform reference records the rotation stamp after real progress and the run codes an exhausted cooldown or rotation chain reports, and hands the caption's layout to TASK-F47.
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.
Follow-up to #3742 (TALE-82), from the post-merge review of a970418. Each finding was re-checked on
mainbefore it was changed; the high one reproduced as described.What was wrong
account_id.available: falseand were never selection candidates.CREDENTIAL_BROKER_EXHAUSTED(orCREDENTIAL_BROKER_EMPTYin the OpenAI case), and the immediate retries spent the whole budget in seconds.credential-rotation.integration.tscreditedfailure_codeto migration 0132; it is 0133.What changed
Gateway (
services/ai-gateway). Every token now carrieshold:quotawhile its quota is spent (also when the floor holds it too);refreshwhile only the hand-out floor holds it;nullwhile it is available.availablekeeps its meaning, so a consumer that reads nothing else still skips a floor-held account. The README,.env.exampleand the manual reference say so.Platform broker resolution.
diagnoseTokenMappingputs an account withavailable: falseandhold: "refresh"into a separateheldAccountslist, as long as it passes every other filter (provider, account id, status, expiry, dedupe). The list is ordered by latestavailable_atfirst; for a refresh hold that is the gateway'srefresh_at, so the token with the most life comes first. A quota hold, an unknown hold, or noholdat all stays excluded as before.pickBrokerCandidate, extracted fromselectBrokerAccountso it can be unit-tested) never takes a cooling account. It serves a held account only when no available account is out of its cooldown, never while one can serve (even one already tried this run). Held accounts go in the order given.CREDENTIAL_BROKER_EMPTYonly when both lists are empty, and logs when it vends a held account.holdyet produces no held accounts, so behaviour is exactly as today. A unit test covers this.All accounts cooling down: retry at
retryAtMs. The cooldown refusal now carriesretryAtMs(read withcredentialRetryAtMs), and a start refused this way settles ascredential_cooldown.task.agent_retrywithstartAfterMs. The retry kicks the run at once, so the card shows it queued, while itstask.agent_turnjob is held in pg-boss until then.notBefore, and the host delays the start. The turn's time limit counts from the start, and the re-kick resumes the conversation the refused attempt was meant to resume.BROKER_RATE_LIMIT_COOLDOWN_MS), well inside the stranded-run sweeps' 4-minute window.freeCooldownWaits/retriedRateLimit).start_failure.ts,agent_retry.tsand the docs.runFailureMessage) instead of the serializedAppErrorpayload, following the repo's "words, never payload" rule.Free rotation caption. A free rotation's retry now shows what the run it replaces showed, so the count never advances or falls back. When that run showed no count, or worked at least fifteen minutes (which refreshed the budget, as the automation lane already stamps it), the stamp is
0and the card reads Resumed after a token refresh (en, de, fr).Run failure codes on the wire.
credential_cooldownandcredential_rotatedare retry-only agent codes. When an automation run's budget runs out on them,Run.failureCodereports the code the same failure carried before this work:start_failedfor a start a cooling pool refused,harness_errorfor a rotated 401. The OpenAPI enum does not change, and an agent code that is neither on the wire nor mapped fails to compile.Docs (en/de/fr):
holdfield, the fallback, and the wait;The platform and gateway manual references record the new behaviour.
TASK-F47judges the caption's layout in every locale, andACCT-21listsholdamong the token fields.How it was verified
lib/i18n/messages.test.ts;accounts,routesandrollbacktests;@tale/docsstructural suite.hold;holdfield, in the service and on the wire;retryAtMsflowing through the task start, the comment restart, the automation start and the answered-ask resume, to a delayed arm, kick and scheduled start, including the cap;max(…, 1)stamp.backend:integration, local pgserver, filtered run):checkBrokerAccountSelectionhas two new records: a real HTTP broker whose A cools down or lacksaccount_idvends B, and an all-cooling pool refuses with A's cooldown end.checkCooledStartRetrylane shows three things: the retry arm carries the wait, the history reads the wait after the run's own 429 as free, and the queued run'stask.agent_turnjob hasstart_afterat the cooldown end.checkCredentialRotationRetryandcheckAutoRetryAndKickPlan(the live 1-2-3 retry cascade) pass, as docheckOneLiveRunPerTask,checkQueuedRunRecoveryandcheckTaskRunStartFencefrom the first round.knowledge BM25 indexes verified at boot: pgserver has nopg_search, and it fails the same way onmain.oxlint --type-aware --type-checkon every changed file,lint:manual, and a local Opengrep scan (0 findings).main, with the same three load defects: a flicker on the Board toggle, a 0.013 page layout shift, and a 0.002 shift of the Repeat field. None is in the run strip, and no element is affected by the caption.harness_error, and the rotation stamp after real progress. Both are fixed in 128e27b and be109d7, with tests that fail without the fix.main.Not run locally: the whole platform suite, a workspace-wide
tsc, the fullbackend:integration, and a live vendor 429/401. CI runs the first two.