Skip to content

fix(platform): repeat a gateway POST only when its write rolled back - #5105

Closed
tale-code-bot wants to merge 1 commit into
mainfrom
fix/gateway-post-retry-post-commit-lock
Closed

tale-code-bot wants to merge 1 commit into
mainfrom
fix/gateway-post-retry-post-commit-lock

Conversation

@tale-code-bot

Copy link
Copy Markdown
Collaborator

Task: TALE-48 (Development board), from issue #3982. Plan: posted on the task before the first edit; this PR follows it.

What was wrong

#3975 (88259afe5) made managementFetch (services/platform/backend/core/node_only/sandbox/llm_gateway_admin.ts) send a POST again whenever its 5xx body matched /database is locked/i. It assumed a locked SQLite store always means the write rolled back. That holds for an insert the store turned away. It does not hold for a provider key's create, because the gateway commits the key first and then reads it back.

The pinned gateway is maximhq/bifrost:v2.2.4 (services/sandbox-llm-gateway/Dockerfile). Tag transports/v2.2.4 is commit ed8371a9779bfbc8aa689d4d77964cf8ce9308bf. Its AddProviderKey does two things in order:

  1. Insert. ConfigStore.CreateProviderKey runs in its own gorm transaction. The file is the same blob, 0d3449c1, at the framework/v1.7.5 that transports/go.mod pins.
    • On failure the gateway answers failed to create provider key in store: %w.
    • gorm rolls back on an error. go-sqlite3 v1.14.32 runs ROLLBACK when COMMIT fails. So this answer means nothing was stored.
  2. Read-back. ConfigStore.GetProviderKey reads the key again.
    • On failure the gateway answers failed to re-read provider key after create: %w. Its own comment there says "The DB write succeeded".

The handler turns the read-back failure into 500 Failed to create provider key: … database is locked. Bifrost's client error sanitizer (lib/errorsanitizer.go) leaves that text in. A second POST then hits the stored key's unique name and gets 409 "API key names must be unique across providers".

So the caller got that 409 instead of the original 500.

The change

  • managementFetch takes a new per-call option, storedNothing?: RegExp. A POST is sent again only when its 5xx matches the answer its own endpoint gives when it stored nothing.
  • Any other 5xx to a POST is returned at once with its original status and body. So is every 5xx to a POST without storedNothing.
  • GET, PUT, DELETE and resend: false are unchanged. GET, PUT and DELETE are still retried on any 5xx, after 250, 750 and 2000 ms.
  • No 409 is taken as success, and no reconciliation was added.

Every POST that goes through the helper (all three are in this file; the helper is module-private):

Caller Endpoint storedNothing After this change
writeProviderKey (key create) POST /api/providers/:p/keys PROVIDER_KEY_STORE_BUSY = /failed to create provider key in store: [^"]*database is locked/i Changed. Sent again only when the gateway's own insert was turned away, which rolled back. Two answers that come after the commit now return at once with the original 500: a lock on the read-back (failed to re-read provider key after create) and failed to update provider.
postVirtualKey (mint) POST /api/governance/virtual-keys STORE_BUSY (/database is locked/i) Unchanged: sent again on a lock answer. See the note below the table.
ensureModelPricingOverride (create) POST /api/governance/pricing-overrides none Never sent again. See the note below the table.

Mint. The gateway can hit the lock in two places only, both before anything is stored: while loading providers before the write (500 Failed to load providers), or inside the one ExecuteTransaction that writes the key, which rolls back. Once that transaction commits, it always answers 200 and only logs a failed ReloadVirtualKey (createVirtualKey).

Pricing override. The gateway's 5xx answers here never include the store's error: they are "Failed to create pricing override" and "Failed to upsert pricing override" (L4814, L4820). The second comes after the override was stored. Against this gateway the old code never resent it either. Now the contract says so, and a test pins it.

The PUT branches keep the idempotent retry: key rotate in writeProviderKey and update in ensureModelPricingOverride. A later gateway that rewords these messages can only lose a resend. It can never cause a committed create to be replayed.

Tests

Four cases added to the #3975 block in llm_gateway_admin.test.ts ("management calls the gateway store turned away for a moment"). A fixture, stubKeyCreate, models the gateway's key create; once the key is stored, a second create gets the 409 for the taken name.

  • Lock on the read-back after commit (the exact message from the issue): 1 POST, 1 key stored. provisionProviders reports the original (500): {"error":{"message":"Failed to create provider key: failed to re-read provider key after create: database is locked"}}, not a 409.
  • Lock on the insert (rolled back): 2 POSTs, 1 key stored, no failure. The created key id reaches onProviderKey.
  • Healthy create: 1 POST, no failure, key id reported.
  • Pricing-override create answered with a lock body: 1 POST, and the original 500 is returned.

The retries this PR leaves alone keep their existing tests:

  • a PUT sent again after two lock answers (sends a write again after a locked store…)
  • a mint resent only on a lock answer (repeats a mint only when the store said it was locked…)
  • a DELETE repeated with waits of 250, 750 and 2000 ms (repeats an idempotent call whatever its 5xx says…)
  • a 4xx never resent (never repeats a refusal below 500)
  • a GET listing retried (schedules the pass again at the next call when its listing failed)

Results

All at head 739db3aece749497fa0a42b8adfa4750fdaa2b2b on base main 9be5cadacb5f59cd6b2cc67c7840e3c0101bdde2, Bun 1.4.2, after bun install --frozen-lockfile. Every command ran with the GitHub credential removed from its environment.

Gate Command (in services/platform) Outcome
Unit, changed file bunx vitest --run --project server backend/core/node_only/sandbox/llm_gateway_admin.test.ts 138 passed
Unit, gateway neighbours the same, plus gateway_provisioning.test.ts and gateway_key_settlement.test.ts 3 files, 182 passed
Red first changed test file against unfixed llm_gateway_admin.ts (blob 45b836ee, as on main) 2 failed, as expected: read-back lock sent 2 POSTs (expected 1); pricing override sent 4 POSTs (expected 1). Insert-lock and healthy cases passed, so they behave as before
Types NODE_OPTIONS=--max-old-space-size=6144 bunx tsc --noEmit (whole platform program) exit 0
Lint bunx oxlint --type-aware on the two files 2 files, 226 rules, 0 diagnostics
Format bunx oxfmt --check on the two files clean
Hooks pre-commit (conflict markers, oxfmt, opengrep in skip-if-uncached mode) and commit-msg passed

Loopback reproduction. This is the issue's script, extended to three cases. It imports the complete module and calls provisionProviders against a Bun.serve gateway on 127.0.0.1 that answers the way pinned Bifrost can. Requests to any other origin are refused.

Case Unfixed module (blob 45b836ee) This PR (blob 46d79648)
healthy create 1 POST, 1 key, success 1 POST, 1 key, success
insert locked (rolled back) 2 POSTs, 1 key, success 2 POSTs, 1 key, success
read-back locked (committed) 2 POSTs, 1 key, 409 "API key names must be unique…" 1 POST, 1 key, original 500 "…failed to re-read provider key after create: database is locked"

Not done

  • No live Bifrost fault injection. The tests stub fetch, and the reproduction serves loopback HTTP, with the gateway's exact answers taken from the pinned source. Triage did not require a live gateway run. This PR does not claim end-to-end recovery.
  • Not run locally: the full unit suite, knip and SAST. This PR's CI runs them.
  • Follow-up, out of scope here, and the same before fix(platform): repeat a gateway call its busy store turned away #3975.
    • After a failed read-back, the key row is in the gateway's DB but not in its in-memory config.
    • GET /api/providers/:p/keys reads memory (listProviderKeys). So the next provision lists no key and POSTs again, which gets the 409 until the gateway reloads its config. This is inferred from source, not observed.
    • This change keeps the diagnostic correct but does not reconcile that state. A reconciliation would have to verify the stored key's identity before treating the 409 as success.

Closes #3982

managementFetch sent a POST again whenever its 5xx named the gateway's
locked SQLite store, reading that as proof the write rolled back. The
pinned gateway (Bifrost transports v2.2.4) commits a provider key, then
re-reads it; a lock on that read answers 500 "failed to re-read provider
key after create: database is locked" with the key stored, so the second
create met its unique name and the caller got a 409 instead of the
original error.

A POST now goes out again only when its 5xx matches the answer its own
endpoint gives when it stored nothing (storedNothing): a key create on
"failed to create provider key in store: ... database is locked", a mint
on any locked-store answer (its writes share one transaction, and it
answers 200 once that commits), a pricing override create never.
GET, PUT and DELETE keep their retries.

Closes #3982

@tale-review-bot tale-review-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approve at 739db3aece749497fa0a42b8adfa4750fdaa2b2b (task TALE-48, Closes #3982).

  • Scope matches the triage's "done means" and the plan. managementFetch now sends a POST again only when the caller passes its endpoint's proven nothing-stored pattern (storedNothing). Every other 5xx to a POST comes back at once, with its original status and body.
    • Key create (writeProviderKey) is resent only on failed to create provider key in store: … database is locked. A lock on the read-back (failed to re-read provider key after create) now surfaces as the original 500, not the replay's 409.
    • The mint keeps the lock resend.
    • A pricing-override create is never resent.
    • GET, PUT and DELETE are unchanged, and no 409 is taken as success.
  • Correctness.
    • The file has exactly three POST call sites (:683 mint, :1078 pricing override, :1624 key create), and each passes what the plan's audit of pinned Bifrost ed8371a9 says.
    • PROVIDER_KEY_STORE_BUSY matches the insert's refusal and not the read-back's.
    • The PUT rotate path keeps the idempotent resend.
  • Tests. There are four new tests: read-back lock, insert lock, healthy create, and pricing override never repeated. I staged and read the Coder's logs:
    • vitest-red-first-unfixed.log: against the unfixed file, the read-back test fails (expected 2 to be 1 creates, :1904), and the pricing-override test fails (4 POSTs instead of 1, :1960).
    • vitest-fixed-head.log: 3 files, 182 tests passed.
    • CI's server unit shards passed at this head.
  • Contract.
    • Branch fix/gateway-post-retry-post-commit-lock, off main 9be5cadac.
    • One commit; its header and the PR title are both 68 characters.
    • It is backend core, not a domain, so no spec card is owed.
    • No integration lane imports llm_gateway_admin.ts, so the queue-only Backend integration does not exercise this client. Its unit tests and the loopback reproduction are its proof.
  • Security: two files (the client and its test); no secret, permission, dependency or workflow change.
  • Required checks at this head: all seven CI ready (...) contexts passed. The runs are Commitlint 40930266692, SAST 40930267105, Security 40930266417, CLI 40930266722, E2E 40930266496, Build 40930266522 and Checks 40930266436.
  • Not run by me: the tests locally, and no live Bifrost fault injection (triage did not require one).
  • Note: the Coder's out-of-scope follow-up stays open: after a failed read-back, the key is in the DB but not in memory, so the next provision may 409 until the gateway reloads. It could be its own issue.

@tale-review-bot
tale-review-bot added this pull request to the merge queue Oct 29, 2026
@yannickmonney
yannickmonney removed this pull request from the merge queue due to a manual request Oct 29, 2026
@yannickmonney yannickmonney reopened this Oct 29, 2026
@tale-review-bot
tale-review-bot enabled auto-merge (squash) October 29, 2026 20:19
@tale-review-bot

Copy link
Copy Markdown
Collaborator

Reviewer: not landing yet. The approved head 739db3aece749497fa0a42b8adfa4750fdaa2b2b has no CI round under the current workflows.

  • The newest Checks run on this head, 40947403739 (created 20:15 UTC), checked out Merge 739db3aece749497fa0a42b8adfa4750fdaa2b2b into 9be5cadacb5f59cd6b2cc67c7840e3c0101bdde2 (merge commit 4dd9f52). That is main from before 0da08205 ("ci: land each pull request on its own, without the merge queue"), so the run uses the old workflows.
  • In that run, UI (both jobs), Browser, Performance and Backend integration were skipped. The current .github/CI.md has a pull request run every suite its paths owe. Its CI ready (Checks) job is still queued.
  • What lands it: a fresh round on the same head under the current workflows. Closing and reopening the pull request rebuilds its test merge on the current main; draft→ready does not. My approval of 739db3aece749497fa0a42b8adfa4750fdaa2b2b stands while the head is unchanged. Once every CI ready (...) of that round has finished green, it lands with gh pr merge --squash --auto --match-head-commit 739db3aece749497fa0a42b8adfa4750fdaa2b2b after a hold read.

@yannickmonney

yannickmonney commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Reopened so the checks run again. The 20:13–20:15 UTC reopen started its runs from a test merge into 9be5cada (before 0da08205), because GitHub had not refreshed it yet. This reopen's runs start from the test merge 99d135d92c, into 0da08205. The head is unchanged (no new commit).

@yannickmonney

Copy link
Copy Markdown
Contributor

Landed on train as fc65f6b: squashed at the approved head after the fast gate (formatting, typecheck, build). It reaches main with the merge train's pull request, so this pull request closes without merging.

yannickmonney pushed a commit that referenced this pull request Oct 30, 2026
Reviewed and approved in pull request #5105 at 739db3a.

Closes #3982
Task: TALE-48
@yannickmonney
yannickmonney deleted the fix/gateway-post-retry-post-commit-lock branch October 30, 2026 10:32
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.

bug(platform): gateway POST retry mistakes a post-commit read lock for rollback

3 participants