Skip to content

feat: Opt-in stalled /health and GET /v1/models/{id} #3633

Done

Description

@inureyes

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-28 documents that b10621's /health has exactly two answers: 503 with the Loading model envelope, and 200 {"status": "ok"}. health_check (:68-97) implements only those.
  • route_inventory (src/server/app.rs:303, :430) registers /v1/models and /models with list_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> (env MLXCEL_STALL_TIMEOUT), default 0 (disabled). With it disabled, /health stays byte-identical to b10621.
  • Add last_step_unix_ms: AtomicU64 to BatchObservability (src/server/batch/observability.rs). The scheduler writes it after every prefill or decode step, and stores 0 when it has no active or queued sequence (idle).
  • When the timeout is enabled, the server is loaded, last_step_unix_ms != 0, and now - last_step > timeout, /health answers 503 with the b10621 error envelope: llama_error_response(SERVICE_UNAVAILABLE, "unavailable_error", "Engine stalled"). The first transition into the stalled state is logged at error level, once.
  • Acceptance: with --stall-timeout 1 and a stub scheduler that holds a sequence without stepping, /health returns 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.rs test 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 from list_models' data array whose id equals the path id, matching the primary model and the embedding, reranker and decision side models alike.
  • An unknown id returns 404 with 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_models so both handlers share it.
  • Acceptance: curl localhost:PORT/v1/models/mlx-community/Qwen3-4B-4bit returns that card, and an unknown id returns 404.
  • Test: a models_tests.rs case 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.
  • /health without --stall-timeout is unchanged. The existing ready_body_is_exactly_status_ok and loading_response_matches_b10621_envelope tests 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

  1. added
    status:ready
    type:enhancement
    priority:low
    on Dec 1, 2026
  2. added
    status:done
    and removed
    status:ready
    on Dec 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority:lowstatus:donetype:enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions