Skip to content

fix(gateway): judge a rewritten model by the name it sends; tell Codex and Gemini conversations apart - #215

Merged
fylorn merged 1 commit into
mainfrom
fix/effective-model-and-session
Sep 26, 2026
Merged

fylorn merged 1 commit into
mainfrom
fix/effective-model-and-session

Conversation

@fylorn

@fylorn fylorn commented Sep 26, 2026

Copy link
Copy Markdown
Contributor

Two gateway bugs

1. A routing rule that rewrites the model never worked against an upstream with a model list

Model admission (admit), the serving filter (models::serving) and cheapest-group pricing all used the model the client wrote, before any rule rewrote it. So a rule such as claude-* → set model glm-5, to zhipu was rejected with No upstream serves model claude-… (or the candidate was skipped as "does not offer this model") whenever the target upstream publishes /v1/models. It only worked with upstreams that have no list.

This matters because Lite tells users to write exactly this rule: Claude Desktop only accepts Claude-like model names, so any non-Claude upstream needs it; the Antigravity CLI and DeepSeek Harness takeover notes suggest it too.

Fix:

  • Engine::models_asked lists the model each candidate will actually be asked for: phase-two (provider_would_be) rewrites over phase-one rewrites over the request's own model. Engine::phase_two_with takes a rule table so the dry run can evaluate drafts.
  • Admission now runs after the rule decision (still returns an error without a traffic row, as before) and passes if any candidate's asked model is admitted. When the model was rewritten, the new codes gw.model.no_upstream_rewritten / gw.model.not_allowed_rewritten name both models.
  • serving, unit_prices and the routing dry run take the per-candidate models.

2. Codex and Gemini requests had no session fingerprint

session::fingerprint only read system + messages. Responses bodies (instructions + input) and Gemini bodies (systemInstruction + contents) got None, so:

  • a load-balance group with session affinity (the default) sent every Codex request to its first member;
  • Codex/Gemini requests were never grouped into sessions in traffic.

The fingerprint now reads all four formats and prefers the request's own prompt_cache_key (Codex sends its conversation id there). Text alone would merge Codex conversations started in the same repository, whose opening instructions and environment context are identical.

Tests

  • tw-gateway/tests/models.rs: a phase-one rename against a listed upstream; a phase-two rename for one member of a group; a rename to a model nobody serves names both models.
  • tw-control/tests/models.rs: the dry run judges a candidate by the rewritten model.
  • session.rs: Responses and Gemini conversations keep their fingerprint as they grow; prompt_cache_key separates conversations with identical openings.

cargo fmt --check, cargo clippy --workspace --all-targets -D warnings and cargo test --workspace pass locally. No control-protocol change; tw-dialect/tw-guard/tw-breaker are untouched.

🤖 Generated with Claude Code

…x and Gemini conversations apart

Model admission, skipping candidates that cannot serve a request, and
cheapest-group pricing all looked at the model the client wrote, before any
routing rule rewrote it. A rule that renames claude-* to glm-* (which Claude
Desktop needs for any non-Claude upstream) was therefore rejected whenever the
target upstream publishes a model list: "No upstream serves model ...".

- Admission now runs after the rule decision and checks the model each
  candidate will actually be asked for, including per-upstream (phase-two)
  rewrites. New codes gw.model.no_upstream_rewritten and
  gw.model.not_allowed_rewritten name both the requested and the rewritten
  model.
- The serving filter, cheapest pricing and the routing dry run use the same
  per-candidate models (Engine::models_asked, Engine::phase_two_with).

Session fingerprints only read Anthropic/Chat bodies (system + messages), so
every Codex (Responses) and Gemini request had none: session-affine groups
pinned all of them to the first member, and traffic never grouped them into
sessions. The fingerprint now reads instructions/input and
systemInstruction/contents, and prefers the request's own prompt_cache_key
(Codex sends one per conversation), since Codex conversations in the same
repository start with identical text.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@fylorn
fylorn merged commit 1fa985d into main Sep 26, 2026
4 checks passed
@fylorn
fylorn deleted the fix/effective-model-and-session branch September 26, 2026 15:10
@fylorn fylorn mentioned this pull request Sep 26, 2026
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