Repository navigation
fix(gateway): the completion gateway resolves and walks its chain as generateText and streamText do - #341
Conversation
…generateText and streamText do createCompletionGateway built its hop list as the caller's chain or the policy chain, unresolved, so the checks the walkers make never applied to a GMI turn's fallback hops: a model in routerParams.excludedModelIds was sent, an entry lacking an explicitly required capability was tried, the policy chain's entry for the first model was retried, every refill ran, and the policy chain's Claude legs ran after a refusal. The gateway now resolves the chain once with resolveFallbackChain, keeps the walk's state on each resolution, and gates every fallback hop with gateFallbackEntry. resolve() takes the error the previous hop failed with; the GMI passes it, so a refusal passes over the Claude legs and a standing leg that failed on availability is owed the next ranked model. A hop that cannot start counts as an availability failure. Passed-over entries are reported through onHopSkipped, which the GMI writes to its reasoning trace. Closes #139.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Reviewer's GuideThe completion gateway now resolves and walks fallback chains with the same filtering and stateful semantics as generateText and streamText, including refusal-aware gating, availability-driven refills, and skipped-hop diagnostics; GMI passes failure context through and records those skips, with regression tests covering the affected scenarios. Sequence diagram for stateful completion fallback walkingsequenceDiagram
participant GMI
participant Gateway as CompletionGateway
participant Resolver as FallbackWalk
participant Provider
GMI->>Gateway: resolve(route)
Gateway->>Resolver: resolveFallbackChain(fallbacks, requirements)
Resolver-->>Gateway: resolved chain and INITIAL_FALLBACK_WALK
Gateway->>Resolver: gateFallbackEntry(walk, entry)
Gateway->>Provider: start hop
Provider-->>Gateway: completion or failure
Gateway-->>GMI: resolution with walk state
GMI->>Gateway: resolve(route, failedResolution, failure)
Gateway->>Resolver: advanceFallbackWalk(walk, walkRole, failure)
Gateway->>Resolver: gateFallbackEntry(updatedWalk, nextEntry)
alt entry is passed over
Gateway-->>GMI: onHopSkipped(provider, model, reason)
else entry can run
Gateway->>Provider: start fallback hop
end
File-Level Changes
Assessment against linked issues
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
|
Warning Review limit reached
This review includes 4 billable files and costs up to $1.00.
Reviews can continue after your included limit without a manual trigger. An admin must approve usage-based billing. Or wait 14 minutes for your next included review. View limit details
Comment |
PR Summary by QodoAlign completion gateway fallback walks with generateText and streamText
AI Description
Diagram
High-Level Assessment
Files changed (4)
|
Code Review by Qodo
1. Refusals reach Claude for some agents
|
| throw new GMIError(`LLM provider error: ${outcome.error.message}`, GMIErrorCode.LLM_PROVIDER_ERROR, { turnId, providerId: failedHop.providerId, hop: failedHop.hop }); | ||
| } | ||
| const next = await gateway.resolve(route, failedHop); | ||
| const next = await gateway.resolve(route, failedHop, outcome.error); |
There was a problem hiding this comment.
1. Refusals reach claude for some agents 🐞 Bug ≡ Correctness
gatewayFor() wraps resolve() for agents with an onFallback listener but forwards only route and after, dropping the newly passed outcome.error. When such an agent's model fails with content_filter, advanceFallbackWalk() receives undefined and never records the refusal, so policy-chain Claude hops remain eligible.
Agent Prompt
## Issue description
The agent-level fallback-listener wrapper discards the failure argument passed by GMI, preventing the gateway from recognizing content refusals and skipping policy-chain Claude hops.
## Fix Focus Areas
- src/api/gmi.ts[228-242]
## Recommended Fix
Accept the third `failure` argument in the wrapper's `resolve` function and forward it to `base.resolve` after `after`. Add a test that exercises a GMI agent with an `onFallback` listener and a content-refusal fallback.
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: fcf9710d10
ℹ️ 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".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| throw new GMIError(`LLM provider error: ${outcome.error.message}`, GMIErrorCode.LLM_PROVIDER_ERROR, { turnId, providerId: failedHop.providerId, hop: failedHop.hop }); | ||
| } | ||
| const next = await gateway.resolve(route, failedHop); | ||
| const next = await gateway.resolve(route, failedHop, outcome.error); |
There was a problem hiding this comment.
Forward the failure through the agent gateway wrapper
When an agent() is configured with onFallback, gatewayFor() in src/api/gmi.ts wraps resolve as (route, after) => base.resolve(..., after) and silently discards this new third argument. Consequently, a content-filter refusal is treated as an availability failure: refusalSeen remains false, refills may be incorrectly owed, and policy-default Claude legs are attempted instead of skipped. Forward failure from the wrapper; the added GMI test injects the base gateway directly and therefore does not exercise this observable public-agent path.
AGENTS.md reference: AGENTS.md:L53-L53
Useful? React with 👍 / 👎.
## [0.13.67](v0.13.66...v0.13.67) (2026-10-12) ### fix * **agency:** the post-approval check reads the approved input, and output guards receive tool-call chunks (#340) ([a81d09d](a81d09d)) * **api:** send the routing keys of customModelParams with the planning call (#329) ([7271cc3](7271cc3)) * **gateway:** the completion gateway resolves and walks its chain as generateText and streamText do (#341) ([b4c8280](b4c8280))
…he walkers' tier, and its skips wait for a walk (#346) Folds of the cold read of #341 (one P1, five P2s, each verified against the code): - **P1.** `agent({ runtime: 'gmi' })` wraps the completion gateway when the agent sets `onFallback` (`gatewayFor` in `src/api/gmi.ts`), and the wrapper passed only the route and the failed hop to `resolve()`. The error was dropped, so a refusal was walked as an outage: the policy chain's Claude legs ran after it, and a refusing standing leg owed a refill. The wrapper forwards it. - The gateway's chain read its tier from `route.policyTier` alone; it now uses `resolvePolicyTier` as the walkers do (route params, the call's own tier, the host policy's, the router's). - The entries the chain's one-time resolution passes over are held on the first hop and reported when a walk starts, so a turn the first model answers traces nothing (the walkers log their skips only when they walk). - `onFallback` receives the error the previous hop failed with, instead of a placeholder. - Tests: the `gmi()` refusal case with `onFallback` set (`gmi-runtime.session.test.ts`); in `completionGateway.test.ts`, the refill test on a tagged chain (it fails on the parent: a standing leg that cannot start owes a refill, one that refuses does not), expectations computed from the walk's rule rather than from the live ladder's length, the host-policy tier, and `onFallback`'s error.
The completion gateway built its hop list as the caller's chain or the policy chain, unresolved, so the checks generateText's and streamText's walks make never applied to a GMI turn's fallback hops (#139): a model in
routerParams.excludedModelIdswas sent, an entry lacking an explicitly required capability was tried, the policy chain's entry for the first model was retried, every refill ran, and the policy chain's Claude legs ran after a refusal.resolve()resolves the chain once withresolveFallbackChain(the route's excluded models and explicit capabilities, each entry checked as it will be sent; the policy chain's entry for the first model dropped; standing legs and refills marked).CompletionResolutioncarries the walk's state; every fallback hop passesgateFallbackEntry(a refill only while a standing leg is owed one, no policy-chain Claude leg after a refusal).resolve(route, after, failure)takes the error the previous hop failed with, and the GMI passes it. A hop that cannot start counts as an availability failure.onHopSkipped, which the GMI writes to its reasoning trace.Tests:
completionGateway.test.ts(the #139 case with a provider default and aprovider:-prefixed id, the failed first model's ladder entry, refusals vs. outages over the real catalog's mature ladder, a standing leg that cannot start owing a refill) andGMI.completion-gateway.test.ts(a GMI turn whose first model refuses passes over a policy-chain Claude leg to the next leg).Closes #139.
Summary by Sourcery
Align completion fallback resolution and walking with generateText and streamText so each hop respects routing constraints and failure-aware fallback behavior.
Bug Fixes:
Enhancements:
Tests: