Repository navigation
opensecret: health-aware Auto model selection for Router V2 - #968
Conversation
Router V2 resolved an Auto alias to one public model before any health check, so a single-route model with no eligible provider left Auto Quick with nothing to do. Add a small compiled Auto model policy that runs after alias resolution and before any model-dependent work: paid Quick may move from DeepSeek V4.1 Flash to GLM 5.3 Flash and paid Powerful from GLM 5.3 to Kimi K3 only when every route of the preferred model is unavailable under that model's own failover policy. Free tiers keep their single target, explicit selections never change public model, and Router V1 is untouched. The decision consumes one health snapshot across all candidate routes, claims nothing, and feeds the chosen model into the existing same-model planner, first-send claim, tool-loop pinning, persistence, and usage. An alternate is skipped when the request needs more context than its window (Chat uses a text-only estimate with a byte bound before tokenizing, plus tool schemas and the caller's output limit), when it lacks vision the request needs, or, for Kimi on Chat, when tool-call ids are not in Kimi form. Before any provider send, a chosen Auto model that cannot hold the request's context or loses its routes earns exactly one further decision with that model excluded; nothing is ever replayed after a send starts. Router V2 also remembers the route a provider accepted per account, surface, and selector for one idle hour so warmed prompt caches are reused: the planner prefers the remembered same-model provider while eligible, and Auto retries the remembered alternate before the preferred model while it stays healthy. Responses now binds the user key, conversation ownership, the usage gate, and the caller's message against the tier's own model before the decision; its input handling is split so the budget check and both context builds use the final model. Co-Authored-By: Claude Fable 5.1 <[email protected]>
ddb3fea to
14f4646
Compare
|
Reviewed commit Verdict: fix one introduced P2 before merging. The reviewed design otherwise preserves explicit model choice, V1 behavior, entitlement checks, actual-model attribution, and no replay after a provider attempt. P2 — Preserve capacity errors when bounded Auto reselection has no alternate This is source-confirmed; it was not reproduced through the authenticated HTTP handler. auto_model.rs:219–228 records every excluded candidate as A free With no candidate left, the selector returns Suggested fix: carry the typed capacity cause and retry hint across the bounded second decision, or return the original capacity error if reselection cannot produce an eligible alternate. Keep genuine context overflow distinct. Preserve the Responses replay-safety downgrade when image helpers have already performed billed work. Regression coverage should include free single-candidate Auto and paid Auto with an incompatible alternate, across Chat and Responses. Assert the capacity result and retry hint, zero main provider sends, bounded reselection, and the helper-related replay-safety behavior. Other reviewed boundaries
Policy and coverage notes, separate from the P2
Validation independently executed at the reviewed head Through the component's pinned Nix shell with stateful hooks disabled:
Exact-head backend and SDK CI checks were also green. Ignored database/live-provider tests and encrypted fallback smoke were not executed in this review. The earlier healthy-path smoke described in the PR remains author-reported evidence. |
…ernate A bounded second Auto decision excluded the lost model as a context overflow even when the cause was a route that became unavailable after the first snapshot. With no other candidate (free Quick, or a paid tier whose alternate is incompatible) the selector reported no health rejection and the request ended as a 500 instead of the typed capacity result, losing the retry hint although nothing had been sent. Carry the exclusion cause: a lost route is excluded as unavailable with the capacity result's own retry hint, so the second decision reports it and keeps the 503 contract and Retry-After; a context overflow stays distinct. The Responses replay-safety downgrade after billed image helpers still applies. Tests cover free single-candidate Auto and paid Auto with an incompatible alternate on both surfaces, the hint fallback, the compatible alternate still being taken once, and the helper downgrade. Co-Authored-By: Claude Fable 5.1 <[email protected]>
|
Thanks for the review. The P2 is fixed in dbe7c6e (kept as a separate commit on top of the reviewed head so the delta is easy to see; happy to squash before merge). What changed
Regression coverage added
Not covered: the complete authenticated Responses sequence (helper execution, health change, reselection, persistence, tool continuation) still has no end-to-end test; that remains as stated in the PR body. PR prose: corrected the first-send-claim paragraph. Responses can only replan same-model providers or fail safely at its first-send claim; Chat can take its one bounded cross-model decision there because it has no model-dependent preparation to rebuild. Other notes: Flash stays on Validation at dbe7c6e in the pinned Nix shell with the stateful hooks disabled: |
|
Rereviewed The previous P2 is resolved. No new findings in the follow-up. Verified that:
Independently reran the pinned, hook-disabled local gates at this exact head: format clean; strict Clippy clean; 681 tests passed, 0 failed, 23 ignored; At the latest CI read, SDK integration and the Nix/application-package job were successful; the backend format/lint/test job was still running. Code-review concerns are cleared, subject to completion of required CI. This is not a deployment approval. The previously stated limits remain accurately scoped: no complete authenticated Responses helper/reselection/persistence test was run, near-window image-heavy Chat fallback remains unverified, and Flash standard circuit policy remains the separate #971 follow-up. These are not new findings in this fix. |
|
Three independent read-only reviews of |
Router V2 resolved an Auto alias to one public model before any health check, so an Auto Quick request whose only route was unavailable had nowhere to go. This adds the missing Auto model-selection stage: a small compiled policy that may choose another approved model of the same tier when every route of the preferred model is unavailable, decided before any model-dependent work and pinned for the whole response. Explicit selections never change public model and Router V1 is untouched.
Policy (
src/inference/auto_model.rs,auto-model-v1)auto:quickdeepseek-v4-1-flashglm-5-3-flashauto:powerfulglm-5-3kimi-k3auto:quickgpt-oss-120bauto:powerfulglm-5-3(denied by plan, unchanged)A model is unavailable only when all of its configured routes are open or probing under that model's own
FailoverPolicy, evaluated from one health snapshot across every candidate (GLM Flash keeps its capacity-only gates). Same-model provider failover therefore still happens before any model change. Every candidate passes the real plan check; an alias never grants access. A preferred model with no configured route at all is a server error, never something an alternate quietly masks.An alternate is skipped when the request needs more context than its window (Responses: user message + system prompt + inline instructions +
max_output_tokens; Chat: text-only token estimate including tool schemas and the caller'smax_tokensormax_completion_tokens, with a byte bound so tokenizing only happens near the limit), when it lacks vision the request needs, or, for Kimi on Chat, when assistant tool-call ids are not in Kimi form. Image parts are not counted because their provider token cost is model-specific. When no candidate is eligible the request gets the established typed capacity result with the earliest bounded recovery hint and zero upstream sends.One bounded second decision before any send
The Responses context builder keeps the system prompt regardless of truncation and keeps the conversation's first user turn whenever it fits, so an alternate admitted by the up-front estimate can still fail the context build; and a chosen model can lose its routes between the decision and the pin. In each of those cases the request decides exactly once more with that model excluded, before any provider send. The exclusion carries its cause: a model that lost its routes is excluded as unavailable with the capacity result's own retry hint, so when nothing else is eligible (free Quick has a single candidate; a paid alternate can be incompatible) the request still returns the 503 capacity contract with Retry-After, not a server error. Surface behavior at the late boundaries differs: Responses rebuilds its context for the new model without repeating image descriptions and pins; at its first-send claim it can only replan same-model providers or fail safely, it does not reselect models there. Chat re-prepares the route and can take its one bounded cross-model decision at the first-send claim as well, since it has no model-dependent preparation to rebuild. Any capacity result after billed image helper work is not replay-safe. A message too large for the tier's own model keeps its existing context error, checked before the decision. After the first send the logical response stays pinned; nothing is ever replayed.
Sticky routes (
src/inference/sticky_routes.rs)Router V2 remembers the route a provider accepted per account, inference surface, and selector, and forgets it one idle hour after the account's last accepted request (bounded to 50k entries per enclave; lookups do not extend or reorder entries). The same-model planner prefers the remembered provider over the weighted bucket while it is eligible (
RouteSelectionSource::Sticky,PlanDecision::RememberedRoute), and Auto retries the remembered alternate before the preferred model while it stays healthy. This keeps warmed provider prompt caches; a remembered route is a preference, never a permit. Consequences to be aware of: an account that stays active keeps a healthy alternate (or shifted provider) until it has been idle for an hour, and memory is enclave-local like the health state, so conversations that hop enclaves can see different decisions.Where the decision sits
body.modelis written by alias resolution and then once more from the decision; every model-dependent step reads it after that second write.preferred_model_id(),auto_model_reason()), and the pinned-route log includes selector, preferred, chosen, reason, provider, and policy version. Persistedresponses.model, response payloads, and usage attribution all use the executed model.Unchanged: first-send probe claims and same-model replanning, tool-loop pinning, no replay after any upstream failure, Responses disconnect/cancellation/storage behavior, transport, usage publication.
Known limitations and follow-ups
Three independent read-only model reviews (security, control-flow correctness, and product/operations lenses) plus the earlier review found no P0 or P1 at
dbe7c6e4. These are the items worth knowing; none changes the contract above.AppStatefixture. A regression in the once-only guards would not be caught by the suite.Validation
Static/unit, from
services/opensecret/in the pinned Nix shell with the dev-shell hooks disabled:cargo fmt --all -- --checkclean.cargo clippy --locked --all-targets --all-features -- -D warningsclean.cargo test --locked --all-features: 681 passed, 0 failed, 23 ignored (the ignored tests are the existing database and live-provider suites).New focused tests cover: candidate tables anchored to the Router V2 alias targets; primary, retained, fallback, and excluded-candidate decisions; context, vision, and Kimi tool-history incompatibility; free-plan gates and the unconfigured-preferred error; recovery hints only for health rejections; the bounded token estimate; per-model availability under each failover policy from one snapshot; remembered-provider planning; sticky memory expiry, eviction order, and per-surface keys; cache reads that do not disturb eviction order; and a mock-provider flow where the request that discovers a DeepSeek 503 is not replayed, the next Auto Quick request moves to GLM Flash before any send, the response model is canonicalized, an explicit DeepSeek request stays capacity-unavailable with zero sends, and the account retains GLM Flash while DeepSeek stays open. The DeepSeek-to-GLM Flash smaller-window case is tested for both the alternate skip and the existing truncation contract, an overflowing remembered alternate is shown to yield to the healthy preferred model, and a reselection with no eligible alternate (free single-candidate Auto, paid Auto with an incompatible alternate, on both surfaces) keeps the capacity result and retry hint with zero sends, including the Responses replay-safety downgrade after helper work.
Local encrypted full stack (this component's pinned local stack with a loopback stub for the router flag, deterministic Free and Pro fixture users, live Tinfoil and Continuum routes, healthy path only), run on the first revision of this branch: the TypeScript SDK streaming Chat and Responses tests passed on Router V2 for the Pro user with
auto:quickand for the Free user withauto:quick, and passed again with the flag off; the backend log showedrouting_mode=V2withreason=primarydecisions on both surfaces and both plans,routing_mode=Legacywith no decision when the flag is off, and the Rust SDK streaming test drove anauto:powerfulV2 decision on the Chat surface. Two live-suite assertions unrelated to routing fail with reasoning models (the Rust test's 10-token budget is consumed by GLM reasoning; the TypeScript workflow test expects the first output item to be a message while DeepSeek emits reasoning first).Not exercised here: induced provider failures against the real binary (the plain-HTTP Tinfoil transport is test-only, so outages exist only in the mock-provider tests), the bounded second decision end to end, API-key attribution end to end, image-helper requests through the full stack, and multi-enclave behavior. No flags, prices, PCRs, or deployments were changed; production rollout is a separate decision.
🤖 Generated with Claude Code