Skip to content

fix(knowledge): retry ingest runs that failed only on transient provider errors - #5682

Open
kojiwakayama wants to merge 8 commits into
mainfrom
fix/knowledge-ingest-retry-and-source
Open

kojiwakayama wants to merge 8 commits into
mainfrom
fix/knowledge-ingest-retry-and-source

Conversation

@kojiwakayama

@kojiwakayama kojiwakayama commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

Problem

A knowledge ingest run that failed on a transient provider error ended on its first attempt. When the embedding provider returned a 503 (overloaded), the run response never set retryable, so the control plane did not start another attempt.

Cause

executeKnowledgeIngestRun in src/server/handlers/request/project-run-execute.handler.ts returned { success: false, error } for every failure. Neither the outer catch nor the upload path set retryable. The embedding layers below (the runtime bridge and the embedding facade) do not wrap errors, so the ProviderError from provider-http.ts reached the handler with its own retryable classification intact. The handler dropped it.

While writing a handler test for a gateway policy refusal, I found a second problem on the vendor-neutral gateway routes. normalizeNeutralGatewayRefusal in src/provider/veryfront-cloud/shared.ts clones an error response to check it for a Veryfront refusal. Cloning moves the original response onto a new body stream. The sealed host response keeps its own body reference to the old stream, which the clone has locked. When the error was not a refusal, the original was passed through, and provider error classification failed with "ReadableStream is locked". It then fell back to status rules, which treat an OpenAI-format insufficient_quota 429 as a retryable rate limit.

Change

  • isRetryableProviderFailure walks the error's cause chain (maximum depth 8, own data properties only). It returns the nearest ProviderError's own retryable === true. Any failure that is not a provider error stays terminal. Project code runs in the same realm, so the instance checks use the captured Error constructor and Function.prototype[Symbol.hasInstance], not the live global Error or a replaced Symbol.hasInstance. If inspecting the error throws, the failure stays terminal and the original error is rethrown unchanged.
  • The outer catch, which covers index_existing_canonical mode and any thrown failure, adds retryable: true only in that case.
  • In upload mode, indexing is wrapped so it counts the sources that failed on a transient provider error. The run is retryable only when at least one source failed and every failed source failed that way.
  • Error messages are unchanged. The doc comment on ProjectRunExecuteResponse.retryable now covers knowledge ingest.
  • normalizeNeutralGatewayRefusal no longer clones the response. It tees the upstream body with the captured ReadableStream.prototype.tee, reads a bounded prefix of one branch, and passes the other branch through on the truncated, unparseable and non-refusal exits. Refusals are rewritten as before, and the unused branch is cancelled. Every response it builds is sealed with bindHostResponseAccessors (now exported from outbound-fetch.ts), like the transport's own responses, so the classifier reads the upstream status, headers and body even after project code replaces the Response.prototype getters. Cloning also failed on Bun: after a clone, a body getter that had already been read returned an empty stream, so every upstream error body arrived empty.

Retrying is safe. A retried upload run finds its already-published document through the ingest-source lookup and republishes it against the current version. Binary companions reconcile an existing identical asset. Canonical mode re-reads the same admitted version and checks its checksum.

Tests

src/server/handlers/request/project-run-execute.handler.test.ts:

  • "fails existing canonical indexing visibly on …" (index_existing_canonical) adds provider_overloaded (embeddings return 503) and provider_policy_refused (a 503 carrying eu_inference_policy). Every case asserts payload.retryable: true only for the overload, undefined for authority, 401, policy refusal and 409.
  • "fails default knowledge indexing visibly when …" (upload mode) adds a 503 case, plus two 503 cases in which the mocked provider call replaces the global Error with another function, or defines Symbol.hasInstance on Error to return false. It asserts retryable: true for every 503 case and undefined otherwise. It also asserts that the generated document records resource: "uploads/topic.txt" and never the runtime's download directory.
  • New: "retries a multi-source knowledge ingest only when every failed source was transient". It uses two uploads. Alpha always gets a 503. Beta gets a 503 (run is retryable) or a 401 (run stays terminal).

src/provider/veryfront-cloud/provider.test.ts:

  • New: "keeps an upstream error body readable through its stream on a neutral route". It sends an insufficient_quota 429 through createVeryfrontCloudFetch on the OpenAI neutral route and reads it through response.body.
  • New: "keeps a neutral-route error response's own accessors after project code replaces Response.prototype". For a passed-through insufficient_quota 429 and a rewritten credit refusal, it replaces the body, headers, ok, status and statusText getters on Response.prototype and asserts the response still reports the upstream values.

Results, all run locally through deno task test:file with the named tests temporarily focused:

  • On the unchanged handler, both new 503 retryable assertions failed with - undefined + true. Their "status 503" message assertions and the other five cases passed.
  • On the unchanged shared.ts, the new neutral-route test failed with TypeError: ReadableStream is locked or disturbed.
  • With both fixes, all 10 focused handler cases passed: canonical ×5, upload ×3, multi-source ×2.
  • With the upload rule temporarily changed from retryableIndexFailures === failedCount to > 0, the multi-source 401 case failed. I restored the rule before committing.
  • On the previous shared.ts, the new prototype test failed: it read [200, true, "text/plain", "rate limited"] from the replaced getters instead of [429, false, "application/json", <upstream body>].
  • On the previous isRetryableProviderFailure, both new Error cases failed with - undefined + true on retryable.
  • Bun: four neutral-route tests in provider.test.ts failed in CI with an empty body. A Bun script that calls createVeryfrontCloudFetch against the same stub responses returned empty bodies with the clone-based shared.ts and the full upstream bodies with the tee-based one.
  • With all fixes: the full provider.test.ts passed (4 tests, 75 steps), the full project-run-execute.handler.test.ts passed (19 tests, 602 steps), and outbound-fetch.test.ts passed.
  • deno fmt --check, deno lint and deno check passed on the five changed files, and the anti-slop audit passed.

Review round

Changes after review:

  • Nothing tested upload mode with more than one source and a mix of transient and permanent failures. I added the multi-source test above, and it fails if the "every failed source" rule is loosened.
  • No handler-level test showed that a gateway policy refusal sent as a 503 stays terminal. I added the provider_policy_refused case. While writing it, I found that error bodies on the neutral route were unreadable after the refusal check, which is the shared.ts fix above. The case sends the refusal in the neutral envelope ({ error: { code, message } }), which the gateway normalizer rewrites before classification. On its own, this case does not fail against the unchanged handler, because that handler never set retryable. It guards against a later handler change that marks every 503 retryable.

Not verified

  • I did not run the full unit and integration suites, the Bun suite through its runner (it needs the npm build), or repo-wide lint and custom checks. CI runs them on this branch.
  • When project code replaces the global Error with a value that is not callable, the retry check no longer throws, but the ingest still fails earlier, in existing code outside this change that uses instanceof Error. This change does not address that.
  • I did not test that the control plane requeues a retryable: true knowledge-ingest response as a second attempt.
  • I did not run a live end-to-end retry against a real provider 503. I also did not reproduce the locked-body problem against the live gateway; the evidence is the stub test transport. From reading the code, the production host transport seals responses the same way (bindHostResponseAccessors in outbound-fetch.ts).
  • Other callers that read neutral-route error bodies now get readable bodies. Only the provider test file above was run for that change.

…ures

The knowledge ingest task turned every failure into a terminal result, so a
transient embedding failure such as a provider 503 ended the run on its first
attempt even though the control plane would retry it.

Report retryable when the failure is a provider error the provider classified
as transient: in the canonical index mode and any thrown failure, from the
error's cause chain; in the upload mode, only when every failed source failed
on such an error while indexing. Policy refusals, 4xx rejections, version
conflicts and missing inference authority stay terminal.

Tests also pin that a remote upload ingest records the project upload path,
not the runtime's download copy, as the generated document's resource.
…usal check

Checking a neutral-route error for a Veryfront refusal cloned the response.
Cloning moves the original onto a new body stream, so the body reference a
sealed host response keeps is left locked. Provider error classification then
could not read the body and fell back to status rules, so a hard quota error
was treated as a retryable rate limit. Upstream errors now pass through on the
live stream.
…ecisions

A run whose sources failed on both a transient 503 and a permanent 401 stays
terminal, and a gateway policy refusal sent as a 503 is never retried.
@github-actions

github-actions Bot commented Dec 4, 2026

Copy link
Copy Markdown

📦 Client bundle boundary

Entrypoint Modules Source size Server leaks
src/index.client.ts 66 371 KiB ✅ 0
src/react/components/Head.tsx 5 74 KiB ✅ 0
src/react/router/index.tsx 5 74 KiB ✅ 0
src/react/context/index.tsx 5 74 KiB ✅ 0
src/react/fonts/index.ts 6 81 KiB ✅ 0
src/react/components/ui/index.ts 111 720 KiB ✅ 0
src/react/components/ui/adapter/contract.ts 1 18 KiB ✅ 0
src/react/components/ui/icons/index.ts 3 17 KiB ✅ 0
src/chat/index.ts 305 2055 KiB ✅ 0
src/chat/ag-ui.ts 61 464 KiB ✅ 0
src/chat/protocol.ts 1 11 KiB ✅ 0
src/chat/types.ts 46 248 KiB ✅ 0
src/chat/message-prep.ts 68 519 KiB ✅ 0
src/markdown/index.ts 6 30 KiB ✅ 0
src/mdx/index.ts 2 2 KiB ✅ 0
src/agent/identity-contracts.ts 1 3 KiB ✅ 0
src/schemas/index.ts 43 222 KiB ✅ 0
src/platform/env.ts 5 55 KiB ✅ 0
src/workflow/claude-code/react/index.ts 9 91 KiB ✅ 0
src/events/index.ts 37 631 KiB ✅ 0
src/events/ag-ui/public.ts 36 624 KiB ✅ 0
src/run-events/index.ts 54 323 KiB ✅ 0

A server module in a client graph aborts hydration in the browser. New leaks fail CI; known leaks are tracked in scripts/lint/client-bundle-baseline.json to burn down.

@kojiwakayama
kojiwakayama marked this pull request as ready for review December 4, 2026 21:29
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 4, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-12-04T22:46:17.449497Z 3d75faa Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@github-actions

github-actions Bot commented Dec 4, 2026

Copy link
Copy Markdown

@codex review

@github-actions

github-actions Bot commented Dec 4, 2026

Copy link
Copy Markdown

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e78e9b06c7

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/provider/veryfront-cloud/shared.ts Resolved
@codecov

codecov Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 84.72222% with 11 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/provider/veryfront-cloud/shared.ts 85.00% 2 Missing and 4 partials ⚠️
...er/handlers/request/project-run-execute.handler.ts 83.87% 4 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

…instead of cloning

The refusal check now tees the upstream body and reads a bounded prefix of
one branch, then returns a response sealed with the host accessor binding,
so its status, headers and body stay readable after project code replaces
the Response.prototype getters. Cloning left Bun with an empty body on a
response whose body getter had already been read.
…trinsics

The retry decision no longer reads the live global Error or a replaceable
Symbol.hasInstance, reads retryable as an own data property, and treats any
failure while inspecting the error as terminal instead of replacing it.
@github-actions

github-actions Bot commented Dec 4, 2026

Copy link
Copy Markdown

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Swish!

Reviewed commit: dc6f5f0ed3

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@github-actions

github-actions Bot commented Dec 4, 2026

Copy link
Copy Markdown

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Hooray!

Reviewed commit: 19961275b3

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@github-actions

github-actions Bot commented Dec 4, 2026

Copy link
Copy Markdown

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 3d75faab73

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

return await indexCanonical(indexInput);
} catch (error) {
if (isRetryableProviderFailure(error)) retryableIndexFailures++;
throw error;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Avoid the live Error constructor in ingest catches

When project code sets globalThis.Error to a non-callable value, or installs a throwing Symbol.hasInstance, while the embedding request is in flight, this wrapper counts the transient provider failure and rethrows it, but ingestResolvedSources then evaluates the live error instanceof Error at cli/commands/knowledge/command.ts:1162 and throws a TypeError; the outer errorMessage at src/server/handlers/request/project-run-execute.handler.ts:1127-1129 repeats the same unsafe check. Consequently, the handler never returns the newly intended retryable: true response. This missed downstream catch is fresh evidence beyond the corrected constructor checks, so these message-extraction paths should also use captured intrinsics.

AGENTS.md reference: AGENTS.md:L26-L28

Useful? React with 👍 / 👎.

@sonarqubecloud

sonarqubecloud Bot commented Dec 4, 2026

Copy link
Copy Markdown

This branch has not been deployed

No deployments
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