Skip to content

server: tensorfold serve --router answers for several models at one address, each a serve child loaded on demand - #3540

Draft
ashhart wants to merge 1 commit into
mainfrom
lead/serve-router
Draft

ashhart wants to merge 1 commit into
mainfrom
lead/serve-router

Conversation

@ashhart

@ashhart ashhart commented Nov 16, 2026

Copy link
Copy Markdown
Owner

One tensorfold serve serves one model, so switching models means restarting it, and a client can't pick between models at one address. Users coming from Ollama, LM Studio or llama.cpp's router mode expect one server that loads whatever model a request names. tensorfold serve --router does that, with each model in its own tensorfold serve child.

What this changes

  • Command: tensorfold serve --router [--router-config FILE.toml] [--max-loaded N] [--idle-unload S] [--host H] [--port P] [--api-key K] [--api-key-file F] [--allowed-host N] [--allowed-origin O].

  • Which models are available: without a config, every complete checkpoint in the local cache that a family serves, from the same scan tensorfold models uses. A config lists models explicitly, each with an id, a model (repo id or path), optional aliases, and serve flags read with the serve config's own TOML rules (docs/serve-config.md). A cached model that serve would refuse is skipped and logged; one named in a config stops startup.

  • Routing:

    • Requests that carry model are forwarded to that model's child, by id or alias: /v1/chat/completions, /v1/completions, /v1/responses, /v1/messages and count_tokens, /v1/embeddings, /v1/rerank, /tokenize, /detokenize, and Ollama's /api/chat, /api/generate, /api/embed, /api/show.
    • A model that isn't running is started. That request and any others for it wait, and a load timeout returns a clear 503.
    • An unknown model gets a 404 in its API's shape: OpenAI's model_not_found, Anthropic's not_found_error, or Ollama's.
  • The relay: a request body is read in full and re-sent with a fresh Content-Length, with hop-by-hop headers dropped, so there's no request-smuggling path to a child. The reply is relayed byte for byte as it arrives, SSE included, while tracking chunked or fixed framing to find its end. A client that hangs up closes the child's connection, so the child cancels the request.

  • Children:

    • Each is the same binary serving one model on 127.0.0.1 at a free port, started through the benchmark's serve child (Managed.spawn). It has its own process group, so a Ctrl-C or closed terminal reaches only the router, which then stops every child.
    • Keys: each child gets a random key in its environment. The router drops the client's Authorization and x-api-key and sends the child's key. A request without it gets 401 on the child's port. The router's own keys (--api-key, scopes) go through the same key gate the server uses, moved verbatim from server.zig into auth_gate.zig.
    • Making room: --max-loaded N (default 1) caps how many children run at once. Loading another first stops the least recently used child with no request in flight. If every loaded child is busy, the request waits up to 5 minutes, then gets a 503.
    • Memory: a model's memory floor (the tensorfold info estimate) is checked against what loaded children hold, and a model that can't fit even alone is refused by name.
    • --idle-unload stops idle children.
    • Crashes: a child that dies is cleared. A request in flight gets a 502 if its reply hadn't started, and otherwise ends where the child stopped, as a lone server's would. The next request starts the model again.
  • Router-level routes: /health lists loaded children, /v1/models lists every available model with a loaded flag, and Ollama's /api/tags, /api/ps and /api/version are served. /metrics is refused with a note that metrics are per model.

  • Not supported yet: every other route, including WebSocket upgrades and GET /v1/responses/{id}, gets 404 unsupported_route by name.

  • Unchanged: single-model tensorfold serve; the goldens show 0 unexpected differences.

  • Windows: --router is refused by name, since the child and relay code is POSIX.

  • Issue: n/a. This came from the 16 Nov check of competitor gap docs against main (Ollama, llama.cpp's router mode, omlx, mlx-serve, qwegn and exo serve several models with on-demand loading); no issue or PR covered it.

  • Stacked on: cli: tensorfold chat talks to a model in the terminal, through a serve child of its own or a running server's --url #3538 (tensorfold chat), commits bd2e51576b, 8dffea7872, 762c91eab6 and 407722e939. This PR adds 79f6a9bdda. Both extend the benchmark's serve child, and there's now one set of options:

    • chat's served, lock, own_group, key and words;
    • the router's environ, guard, stop and failure;
    • each defaulted so the benchmark and chat behave exactly as before.
  • atc overlap --json: to run after review. Known text conflicts:

Checks on this head

  • Head commit: 79f6a9b on cli: tensorfold chat talks to a model in the terminal, through a serve child of its own or a running server's --url #3538's head 407722e (main e7e2424).
  • Lean checker: 337 on base and head, 0 new (tools/pr_check.py: 0 errors, 1 advisory warning, the PR number).
  • Tests, all host:
    • zig build test-router-host (new, /bin/sh stand-in children with in-process fake servers): 49/49.
    • zig build test-router-serve-host (new, real fake_serve children): 3/3.
    • zig build test-server-cpu: 1531 pass, 9 skip.
    • zig build test-golden: 322/335 equal, 0 unexpected (main's 13 known differences); security 55/55.
    • zig build test-serve-flags-host: 37/37. test-cli: 41 pass, 15 skip. test-cli-memory-host: 17/17.
    • zig build test-chat-cli-host: 46/46. The five test-benchmark-* steps and test-thinking-benchmark-host pass. check-chat-windows-host compiles.
    • zig fmt --check, git diff --check: clean.
  • What the tests cover:
    • routing by id and alias, and unknown or missing models in each API's shape;
    • least-recently-used stop order and --max-loaded, the busy wait and its timeout;
    • a crash before the reply (502) and mid-stream (cut), then a restart;
    • byte-for-byte SSE with an event split across reads, and chunked framing however the bytes are cut;
    • the child's key forwarded and never returned, config parsing and refusals, refused routes, and idle stop.
  • By hand, the engine-less binary from zig build server-cpu:
    • The router started and answered /health and /v1/models.
    • A model whose child failed got a 503 carrying that child's own last log line.
    • SIGTERM stopped it cleanly, with its log folder left empty.
  • CI: pending.
  • Not run:
    • a real model loaded through the router, and GPU memory freed between children;
    • Linux hosts;
    • Windows beyond the compile-time refusal.

Receipt

n/a for speed: each model runs in an unchanged tensorfold serve child, so tokens and speed are the child's own. The router adds one loopback hop and relays bytes unchanged. Measuring that hop with a real model is the follow-up.

Follow-ups, not built:

  • WebSocket passthrough for Responses, and stored responses across children;
  • Ollama /api/pull and /api/delete through the router, and the cancel route;
  • per-child metrics;
  • GPU memory in the capacity check (today it counts system RAM, which misjudges discrete cards);
  • fairness when one busy model keeps another waiting;
  • refreshing the model list without a restart;
  • speech, image and OCR routes.

Checklist

  • The title and description match the head commit.

  • New tests fail before the change, pass after it, and skip cleanly without their dependency.

  • Comments and docstrings are one line. No measurements or history in the source.

  • No personal data, machine names, internal hosts or local paths in the diff or its commits. No AI attribution lines.

  • Every token still goes through the lane rounds: the router forwards to an unchanged serve.

  • Synthena (Ash's Agent)

…ddress, each a serve child loaded on demand

A request's model field picks the child, which the router starts if need be and relays byte for byte,
with --max-loaded, --idle-unload and each model's memory floor bounding what stays loaded, and
--router-config FILE.toml listing the models in [[models]] tables. Single-model serve is unchanged.

This branch has not been deployed

No deployments
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