Skip to content

What each upstream serves, and routing that respects it - #31

Merged
fylorn merged 2 commits into
mainfrom
feat/upstream-models
Sep 17, 2026
Merged

fylorn merged 2 commits into
mainfrom
feat/upstream-models

Conversation

@fylorn

@fylorn fylorn commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor

Why

Two failures with one cause: the model catalog knew which models existed, but not reliably which upstream served them.

  • A config reload emptied the discovered models. The catalog was rebuilt from the hand-written models lists alone. Everything discovered from /v1/models vanished and requests for those models were refused ("the client is not allowed to use …") until the daily refresh. A newly added upstream also waited up to a day to be asked for its models, which is exactly the flow the new "create upstream" dialog relies on.
  • Routing ignored the catalog. When a group put an upstream first that did not offer the requested model, that upstream answered 404, which does not trigger failover. The request failed with an upstream that could serve it right beside.

Both are reproduced by integration tests written before the fix (tw-gateway/tests/models.rs).

What

Model directory (tw-gateway/src/models.rs)

  • Answers. Each upstream's last answer is kept across reloads: listed, no list with the reason, or failed with the reason, plus when it was checked. An answer stays valid while the address, credential account, protocol and proxy are unchanged.
  • Catalog. The catalog is derived from the directory and the current config whenever either changes. Disabling an upstream or narrowing its scope applies at once.
  • Schedule.
    • New and changed upstreams are asked straight away in the background.
    • Everything else is re-asked daily, and a failure retries after an hour.
    • Checks compare wall-clock time every ten minutes, so a laptop that slept still refreshes.
    • Upstreams are asked concurrently.

Routing

  • Filtering. Candidates are filtered before a group orders them, so cheapest and url-test choose among upstreams that can serve the request. Skipped:
    • disabled upstreams;
    • models outside models_only;
    • models missing from a known list.
  • Unknown lists. An upstream without a model list is kept, because not knowing is not the same as not offering.
  • Errors. When nothing is left, the error names each upstream and why. It is a 500 if every candidate is disabled, since that is configuration, and a 400 otherwise.
  • Admission. It now says "no upstream offers this model" when that is the reason.
  • Dry run. The dry run reports the same skips (skipped), and outcome: unavailable when no candidate remains.

Config

  • disabled keeps an upstream's settings but takes it out of routing and out of /v1/models.
  • models_only limits which of an upstream's models are used, by ID or glob. An empty list is refused and points to disabled.
  • billing: free records an exact $0 with no price source, and sorts first for cheapest.
  • Provider fields are reordered, so a newly written upstream reads name, connection, models, billing, security. A misplaced doc comment (the proxy note sat on models) is fixed.

Control plane

  • GET /providers/{name}/models returns source (discovered / manual / none), checked_at_ms, error, and for each model: whether it is in scope, its context window, and its price and price source under the upstream's price sheet.
  • POST /providers/{name}/models/refresh asks the upstream now and returns the same view.
  • ProviderView gains model_source, model_count (in scope, 0 when disabled), models_only and disabled. ProviderInput accepts models_only and disabled.
  • Speed-test and replay quotes share tw_gateway::quote, one wording per billing mode, and carry billing instead of subscription. An upstream with unknown billing no longer gets a per-token price. A subscription reads "counts against the subscription quota" rather than "free".
  • The diagnostics bundle shows each upstream's state (disabled, circuit open) and its model list.
  • The Chinese README's cost line now matches the English one from Price sheets in config.yaml, a daily default table, and a price source on every request #30.
  • The workspace version goes to 0.2.0. The desktop app pins tw-api and bundles twcore by tag, and Run the inference speed test the way each upstream expects #28 to What each upstream serves, and routing that respects it #31 change the control API it compiles against. The tag follows this merge.

Tests

  • tw-gateway/tests/models.rs, 5 tests. Two reproduce the bugs above:

    • discovered models survive a reload, and a new upstream is listed promptly;
    • a group skips an upstream without the model.

    Three cover the new behaviour:

    • disabled upstreams, including every candidate disabled;
    • models_only in listing and routing;
    • the error when no candidate serves the model.
  • tw-control/tests/models.rs, 4 tests:

    • the models view before and after a refresh, with scope, context window and price;
    • a refused refresh falling back to the manual list, with the reason;
    • disabled and models_only written through the resource API, and an empty scope refused;
    • the dry run's skips and unavailable.
  • Unit tests:

    • the directory keeps answers across unrelated edits, drops stale answers, and schedules retries;
    • the catalog treats unknown and not-offered differently;
    • one quote per billing mode, and totals;
    • free recorded as $0;
    • empty models_only validation.
  • Gates: fmt, clippy -D warnings and cargo test --workspace (1,246 tests) pass.

  • scripts/smoke.sh: 48 passed, 0 failed, 1 over target and not a regression (memory 35 MB against a 30 MB target).

🤖 Generated with Claude Code

Two failures shared one cause: the model catalog knew which models existed,
but not reliably which upstream served them.

- A config reload rebuilt the catalog from the hand-written `models` lists
  alone. Every model discovered from an upstream's /v1/models vanished,
  so requests for them were refused, until the daily refresh. A newly
  added upstream also waited up to a day to be asked for its models.
- Routing ignored the catalog. When a group put an upstream first that did
  not offer the requested model, that upstream answered 404, which does
  not trigger failover, and the request failed with an upstream that
  could serve it right beside.

The gateway now keeps a model directory beside the catalog:

- Each upstream's last answer is kept across reloads: listed, no list
  (and why), or failed (and why), with the time it was checked. An answer
  stays valid while the address, credential account, protocol and proxy
  stay the same.
- The catalog is derived from the directory and the current config on
  every change, so disabling an upstream or narrowing its scope applies
  at once. New and changed upstreams are asked straight away in the
  background. Everything else is re-asked daily, and a failure retries
  after an hour. Checks compare wall-clock time, so a laptop that slept
  still refreshes.
- Candidates are filtered before a group orders them. Disabled upstreams,
  models outside `models_only`, and models missing from a known list are
  skipped. An upstream without a list is kept, since not knowing is not
  the same as not offering. When nothing is left, the error names each
  upstream and why. The dry run reports the same skips, with an
  `unavailable` outcome.
- Admission says "no upstream offers this model" when that is the reason,
  rather than blaming the client's allow list.

Config:
- `disabled` keeps an upstream's settings but takes it out of routing and
  out of /v1/models.
- `models_only` limits which of an upstream's models are used, by ID or
  glob. An empty list is refused in favour of `disabled`.
- `billing: free` records an exact $0, and sorts first for `cheapest`.

Control plane:
- `GET /providers/{name}/models` lists an upstream's models with source,
  check time, error, scope, context window and price.
  `POST /providers/{name}/models/refresh` asks the upstream now.
- `ProviderView` reports `model_source`, `model_count`, `models_only` and
  `disabled`. `ProviderInput` accepts the last two.
- Speed-test and replay quotes share one quote per billing mode, and
  carry `billing` in place of `subscription`. A quote for an upstream
  with unknown billing no longer reads as a per-token price.
- The diagnostics bundle shows each upstream's state and model list.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown

Thanks for the PR. One thing before review: this targets main, and it should target dev.

main is a release-only line — branch-protected, linear history, and every commit on it is a tagged release snapshot. Routine work lands on dev and reaches main with the next release. GitHub pre-filled main here because it is the default branch, so this is our defaults being unhelpful rather than anything you did wrong.

No need to close and reopen: click Edit next to the PR title and change the base branch to dev. The commits and the discussion carry over.

See CONTRIBUTING.md and CONTRIBUTING.md for the branch contract.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@fylorn
fylorn merged commit d214cfa into main Sep 17, 2026
1 check passed
@fylorn
fylorn deleted the feat/upstream-models branch September 17, 2026 05:01
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