Repository navigation
fix(router): preserve policy authorization across fallback redirects (carry #6380) #6396
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c7173f5
fix(router): constrain policy fallback redirects
luvs01 964f76a
fix(router): keep policy fallback candidates that cannot redirect
devin-ai-integration[bot] 399b4fc
fix(router): keep policy fallback usable when redirect probes fail or…
luvs01 13a9bcc
Merge dev and preserve request-owned policy fallback authorization
luvs01 8176e72
test(routing): declare policy-fallback hop candidates in request-owne…
devin-ai-integration[bot] 99658c9
Merge latest dev and preserve policy fallback failure logging and ide…
luvs01 2c34217
Merge authored policy-scope fixture corrections while retaining verif…
luvs01 2acbf20
Merge current upstream dev before submitting focused change 674
luvs01 05bc47d
test(responses): align source guards with policy request scope extrac…
luvs01 5cb0701
fix(responses): capture policy scope only for an accepted shadow target
lidge-jun 10644c2
Merge remote-tracking branch 'origin/dev' into codex/rel-l3-6380-poli…
lidge-jun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| import { formatErrorResponse } from "../../bridge"; | ||
| import type { RouteResult } from "../../router"; | ||
| import type { RouteDecisionTraceV1 } from "../../routing/trace"; | ||
| import { resolveOpenAiVirtualModel } from "../../providers/openai-virtual-models"; | ||
|
|
||
| /** Request-owned authorization, separate from the diagnostic trace and each physical attempt. */ | ||
| export interface PolicyRequestScope { | ||
| decision?: RouteDecisionTraceV1; | ||
| eligibility?: ReadonlySet<string>; | ||
| readonly triedDestinations: Set<string>; | ||
| preparedDestination?: string; | ||
| } | ||
|
|
||
| export function policyDestinationKey(provider: string, model: string): string { | ||
| return `${provider}\u0000${model}`; | ||
| } | ||
|
|
||
| export function policyWireDestination(route: Pick<RouteResult, "providerName" | "modelId">): string { | ||
| const model = resolveOpenAiVirtualModel(route.providerName, route.modelId)?.wireModelId ?? route.modelId; | ||
| return policyDestinationKey(route.providerName, model); | ||
| } | ||
|
|
||
| export class PolicyCandidateUnavailableError extends Error { | ||
| constructor() { | ||
| super("The candidate cannot serve this request within its original routing policy."); | ||
| this.name = "PolicyCandidateUnavailableError"; | ||
| } | ||
| } | ||
|
|
||
| const localRefusals = new WeakSet<Response>(); | ||
|
|
||
| export function policyCandidateRefusalResponse(error: unknown): Response | undefined { | ||
| if (!(error instanceof PolicyCandidateUnavailableError)) return undefined; | ||
| const response = formatErrorResponse(409, "policy_candidate_unavailable", error.message); | ||
| localRefusals.add(response); | ||
| return response; | ||
| } | ||
|
|
||
| /** Upstream text or a matching error code cannot authorize another policy attempt. */ | ||
| export function isPolicyCandidateRefusal(response: Response): boolean { | ||
| return localRefusals.has(response); | ||
| } | ||
|
|
||
| export function capturePolicyRequestRoute(scope: PolicyRequestScope, route: RouteResult): RouteResult { | ||
| if (!scope.eligibility && route.policyEligibility && route.routeDecision?.routeKind === "policy") { | ||
| scope.eligibility = new Set(route.policyEligibility); | ||
| scope.decision = route.routeDecision; | ||
| } | ||
| if (!scope.eligibility) return route; | ||
| if (!scope.eligibility.has(policyDestinationKey(route.providerName, route.modelId))) { | ||
| throw new PolicyCandidateUnavailableError(); | ||
| } | ||
| // Preparation and normalization both project from RouteResult into the log. Keep the original | ||
| // authorization and decision on every allowed replacement, including recovery's second pass. | ||
| route.routeKind = "policy"; | ||
| route.policyEligibility = scope.eligibility; | ||
| route.routeDecision = scope.decision; | ||
| return route; | ||
| } | ||
|
|
||
| /** Only the admitted selector's existing virtual-model rewrite may change its final route id. */ | ||
| export function assertPolicyNormalizedRoute( | ||
| scope: PolicyRequestScope, | ||
| admitted: Pick<RouteResult, "providerName" | "modelId">, | ||
| route: RouteResult, | ||
| ): void { | ||
| if (!scope.eligibility) return; | ||
| if (route.providerName !== admitted.providerName || (route.modelId !== admitted.modelId | ||
| && route.modelId !== resolveOpenAiVirtualModel(admitted.providerName, admitted.modelId)?.wireModelId)) { | ||
| throw new PolicyCandidateUnavailableError(); | ||
| } | ||
| } | ||
|
|
||
| /** Called once preparation has settled the wire, before dispatch; account retries keep their own budget. */ | ||
| export function recordPolicyPreparedDestination(scope: PolicyRequestScope, provider: string, model: string): void { | ||
| if (!scope.eligibility) return; | ||
| const destination = policyDestinationKey(provider, model); | ||
| if (scope.triedDestinations.has(destination)) throw new PolicyCandidateUnavailableError(); | ||
| scope.preparedDestination = destination; | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
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.
On the normal
/v1/responsespath,handleResponsesCoreowns a translator budget, andfinalizeOwnedTranslatorBudgetreplaces every body-bearing response with a newResponse. Because this marker is keyed only by the original object,isPolicyCandidateRefusalreturns false after the core call. If policy scope locally rejects a subagent/recovery destination or a duplicate candidate after an upstream failure, fallback therefore returns the synthetic 409 or overwrites the last real failure instead of skipping to another eligible candidate. Propagate the marker through response wrappers or use internal metadata that survives wrapping, and cover the defaultrunCorepath rather than only the test stub.Useful? React with 👍 / 👎.