Repository navigation
Description
Problem / Background
ml-explore/mlx-lm#2040 (server_v2) added two small server features that orchestrators and OpenAI SDKs use. /health turns into 503 stalled when the engine has work but stops making progress, so a liveness probe can restart a hung server. GET /v1/models/{id} returns a single model card. The same PR also handles two prompt and output details. These are unverified in mlxcel and must be confirmed before any change. This is low priority and must not break llama-server b10621 compatibility (CLAUDE.md principle 4).
Implement everything in one PR, run the verification block once at the end, and keep pushes to a minimum.
Current Behavior
src/server/routes/health.rs:14-28documents that b10621's/healthhas exactly two answers:503with theLoading modelenvelope, and200 {"status": "ok"}.health_check(:68-97) implements only those.route_inventory(src/server/app.rs:303,:430) registers/v1/modelsand/modelswithlist_models(src/server/routes/models.rs:130). There is no per-id route.
Proposed Solution
1. Stall detection on /health, opt-in
Upstream reference: https://github.com/ml-explore/mlx-lm/blob/main/mlx_lm/server_v2/api/app.py (is_stalled, health) and https://github.com/ml-explore/mlx-lm/blob/main/mlx_lm/server_v2/engine/core.py (seconds_since_step).
- Add an mlxcel-native flag,
--stall-timeout <seconds>(envMLXCEL_STALL_TIMEOUT), default0(disabled). With it disabled,/healthstays byte-identical to b10621. - Add
last_step_unix_ms: AtomicU64toBatchObservability(src/server/batch/observability.rs). The scheduler writes it after every prefill or decode step, and stores0when it has no active or queued sequence (idle). - When the timeout is enabled, the server is loaded,
last_step_unix_ms != 0, andnow - last_step > timeout,/healthanswers503with the b10621 error envelope:llama_error_response(SERVICE_UNAVAILABLE, "unavailable_error", "Engine stalled"). The first transition into the stalled state is logged aterrorlevel, once. - Acceptance: with
--stall-timeout 1and a stub scheduler that holds a sequence without stepping,/healthreturns 503 after 1 s and 200 again once a step lands. With the flag unset, the answer is always the current one. - Test: a
health.rstest driving the timestamp directly. It fails today because the flag and field do not exist.
2. GET /v1/models/{id}
Upstream reference: https://github.com/ml-explore/mlx-lm/blob/main/mlx_lm/server_v2/api/openai/route.py (model).
- Register
/v1/models/{*id}and/models/{*id}with a wildcard segment, because ids contain/. The handler returns the entry fromlist_models'dataarray whoseidequals the path id, matching the primary model and the embedding, reranker and decision side models alike. - An unknown id returns
404with the OpenAI envelope{"error": {"message": "The model <id> does not exist.", "type": "invalid_request_error", "param": "model", "code": "model_not_found"}}. - Factor the entry list out of
list_modelsso both handlers share it. - Acceptance:
curl localhost:PORT/v1/models/mlx-community/Qwen3-4B-4bitreturns that card, and an unknown id returns 404. - Test: a
models_tests.rscase with a slash-containing id. It fails today with 404 for the known id.
3. Chat template fallbacks: confirm first
Upstream reference: https://github.com/ml-explore/mlx-lm/blob/main/mlx_lm/server_v2/chat/rendering.py (_shows_system_turns, _system_as_user, _takes_user_after_tool, _user_into_tool).
Upstream probes the template once. If it does not render a system message after the first message, later system messages become user text joined with a blank line. If it refuses a user message right after a tool result (Mistral templates), the user text is appended to the tool result. Confirm whether /v1/chat/completions in mlxcel fails or drops text on these two message orders with a Mistral template (for example mlx-community/Mistral-Small-3.2-24B-Instruct-2506-4bit). /v1/messages already folds mid-conversation system turns (fold_system_messages, src/server/anthropic_translator.rs:361). If the chat route fails, apply the same two fallbacks in the shared render path and add a test per case. If it does not, record that in the PR.
4. Newline after a reasoning or tool block: confirm first
The audit reported that upstream drops the newline that immediately follows a closed reasoning block or tool block, so visible content does not start with \n. The upstream code for this was not located during the audit; start with https://github.com/ml-explore/mlx-lm/blob/main/mlx_lm/server_v2/engine/generations.py and https://github.com/ml-explore/mlx-lm/blob/main/mlx_lm/server_v2/api/openai/outputs.py. Confirm both the upstream behaviour and whether mlxcel's content begins with \n after </think> on a Qwen3 checkpoint. Change it only if both are confirmed, and record the finding either way.
Scope
In scope: src/server/routes/health.rs, src/server/batch/observability.rs, the scheduler step sites that update it, src/server/routes/models.rs, src/server/app.rs, the CLI and config for the new flag, and docs/llama-server-compat.md (document the opt-in extension).
Out of scope: changing /health defaults, and /slots or /metrics payloads.
Acceptance Criteria
- Items 1 and 2 meet their acceptance lines with failing-first tests. Items 3 and 4 are resolved and recorded in the PR.
-
/healthwithout--stall-timeoutis unchanged. The existingready_body_is_exactly_status_okandloading_response_matches_b10621_envelopetests still pass. - Integrated into the running server, including the router front if it serves
/v1/models(src/server/router_front.rs).
Verification
cargo test --profile test-fast routes::health
cargo test --profile test-fast models_tests
make verify-test
cargo clippy --workspace --all-targets -- -D warnings
cargo fmt --check
Activity
fix(server): time the stall probe on a monotonic clock
feat(server): detect stalled engines and support model-card lookup (#…