diff --git a/docs/api-contract.md b/docs/api-contract.md index cde97280..9bca21f5 100644 --- a/docs/api-contract.md +++ b/docs/api-contract.md @@ -537,7 +537,11 @@ This local Qwen implementation does not provide OpenAI encrypted reasoning. ## Console request compatibility The native route accepts the console's `include`, `prompt_cache_key`, and -string-valued `client_metadata` fields. Only `reasoning.encrypted_content` is +string-valued `client_metadata` fields. It also accepts, validates and ignores the hosted-service hints a Codex +configuration may add: `service_tier` (`auto`, `default`, `flex`, `priority`, `scale`), `user`, `text.verbosity` (`low`, +`medium`, `high`; `text.format` may then be absent and means plain text), `reasoning.effort: minimal` (served as `low`, the template's +brief-thinking instruction) and `reasoning.summary: none`. Still refused: `concise`/`detailed` summaries, `max_tool_calls`, +other `include` values and unknown fields. Only `reasoning.encrypted_content` is accepted in `include`: it is an optional inclusion hint, and this runtime still returns **raw reasoning content, not encrypted content**. Metadata is not added to the model prompt or persisted by the runtime. The cache key is a routing hint; @@ -817,3 +821,16 @@ connection cleanup follow it. The rendered systemd unit retains its 45-second hard stop; the 20-second grace cap reserves time for worker cleanup. Standalone execution has no new overall exit deadline. For longer maintenance, explicitly drain and wait before sending the stop signal. + +## Runtime backend (text-level engine) + +With a `runtime` block in the configuration (`control-plane/config.runtime.example.json`) the same Responses, Chat and Messages +routes run in front of a `leleka-inference` process over the `leleka.runtime.v1` gRPC contract instead of a stdio engine child. +The engine contract is text in, text out, so the surface differs from the stdio engine in these ways (see +`docs/api-over-rust-engine.md`): the prompt is sent as the rendered template text after a round-trip check against the token ids +that were counted; text deltas are re-tokenized for the HTTP layer with a byte-equality check, `<|im_end|>` / `<|endoftext|>` +end the text and cancel the engine generation, `usage.output_tokens` is the engine's own count and `reasoning_tokens` an +approximation bounded by it; images, `reasoning_budget_tokens`, `strata_tune` and `experimental_speed_projection` are +refused with an error; a default `max_output_tokens` is clamped to `runtime.max_output_tokens` (4096, the runtime's cap) and a larger +explicit value is refused; `/unload` is `unsupported` because the runtime's lifecycle belongs to its operator. A terminal event +(`response.completed`, `response.incomplete` or `response.failed`) ends every stream, including when the engine dies. diff --git a/docs/api-over-rust-engine.md b/docs/api-over-rust-engine.md index e8cafcb2..83fd9ce4 100644 --- a/docs/api-over-rust-engine.md +++ b/docs/api-over-rust-engine.md @@ -109,4 +109,48 @@ Not expressible over the text contract, refused (fail closed, with an explicit m ## What the slice delivers and what it does not -See "Status" at the end of this page, written after the implementation. +### Delivered (branch `claude/api-over-rust-engine`) + +| piece | where | +| --- | --- | +| worker for a text-level engine, selected by the `runtime` config block; same `GenerationCommand` channel, `WorkerStatus` and `Control` as the stdio supervisor | `control-plane/src/backend_worker.rs`, `runtime_config.rs`, `main.rs` (`serve`) | +| `Backend` over the real gRPC contract that **attaches** to a loaded runtime (no Load/Unload RPC), re-reads epoch and handle before every generation | `crates/runtime-client` (`GrpcBackend`); the proto, conversions and validated client moved here from `desktop-core`, which re-exports them unchanged | +| in-process scripted engine implementing the same `Backend` trait (feature `scripted`) | `crates/backend-api/src/scripted.rs` | +| literal stop markers across deltas | `crates/serving-policy/src/stop.rs` | +| harmless Codex hints accepted (`service_tier`, `user`, `text.verbosity`, `reasoning.effort: minimal`, `summary: none`) | `control-plane/src/responses.rs` | +| a test-only fake `leleka-inference` over the real `RuntimeServer`, driven by the existing scripted `engine.sh` programs | `desktop-core/examples/fake_runtime.rs`, `scripts/test-api-over-runtime.py`, `fixture.runtime_stack()` in `scripts/smoke-worker-cancel.py` | + +### Tests (no GPU, no model; commands run from the repository root) + +| layer | command | what it covers | +| --- | --- | --- | +| API + worker over the scripted engine, real HTTP/SSE | `cargo test -p strata-control --test backend_worker_http` (27) | Responses JSON/SSE, Chat, tools (function and custom), `text.format` valid/invalid, finish reasons, stop literals, client disconnect, generation deadline, wedged engine, dying engine, native tool-call delta, refused features, output cap, sampling fixed point, token counting, Codex-shaped two-turn exchange with stateless replay, `tool_choice` / `parallel_tool_calls` | +| `GrpcBackend` over loopback HTTP/2 | `cargo test -p leleka-runtime-client` (11) | attach checks, no Load RPC, frame mapping, missing terminal, drop/cancel, stale epoch, unreachable runtime | +| whole chain: router -> worker -> `GrpcBackend` -> gRPC -> real `RuntimeServer` -> scripted engine | `cargo test -p strata-desktop-core --test api_over_runtime` (6) | streaming, finish reasons, tool call, stop marker, disconnect through gRPC, failure and engine death, runtime restart and re-attach, the 256-generation limit | +| existing HTTP smokes, unchanged assertions, engine moved behind the runtime | `python3 scripts/test-api-over-runtime.py target/debug/strata-next target/debug/examples/fake_runtime` (10 scripts) | replay driver and corrupted-stream rejection, custom tools, Chat, Messages, 216 tool-policy cases, input tokens, stored responses across a restart, keep-alive, drain, queue deadline | + +The ten scripts are the ones whose assertions do not depend on `strata-next` owning the engine process. Not moved: +`smoke-worker-cancel.py` (a wedged engine ends the stdio supervisor; the runtime backend re-attaches instead), +`smoke-signal-drain.py` (expects `QUIT` on the engine's stdin) and `smoke-generation-deadline.py` (the deadline is a +`runtime.generation_timeout_s` here, covered by `backend_worker_http`). + +### Not covered / known gaps + +* **No real engine ran.** Nothing here loads a model or touches CUDA. Tokenizer agreement between `strata-next`'s assets and the + engine's GGUF tokenizer is checked per request (prompt round trip) and by the input-token counter comparison (`the engine counted + N input tokens, the control plane M` on stderr), but never against the real vocabulary. +* **Output budget.** `RuntimeServer::generate` refuses `max_output_tokens > 4096`; the worker carries that cap + (`runtime.max_output_tokens`), clamps default budgets to it and refuses larger explicit ones. Codex's 32768 default is therefore + not reachable until the runtime's cap is raised (its value belongs to the engine owner). +* **256 generations per runtime epoch.** `RuntimeServer` (`MAX_IDS`) and the engine backend (`seen`) remember every accepted + request id and refuse new work after 256; `api_over_runtime::documents_the_256_generations_per_runtime_epoch_limit` records it. + An agent session exhausts that quickly; the runtime needs eviction or an epoch rollover before this stack can serve for hours. +* **Token ids.** `usage.output_tokens` is the engine's; `reasoning_tokens` is derived from re-tokenized text, approximate, and clamped + to the engine total. Reasoning-budget wrap-up, images, `strata_tune` and control vectors are refused (fail closed). +* **No prefix reuse and no prefill progress.** The runtime resets to position 0 per request (`original-serve.md`), and the text + contract has no progress events, so the stall watchdog is off by default; `: keep-alive` comments still flow. +* **Sampling precision** is refused at generation time (502), not at admission (400), for values that are not exact + thousandths/millionths. +* **Lifecycle** stays with the operator: `/unload` reports `unsupported` and `/load` is a no-op while attached; the runtime must + already be loaded (`leleka-cli load-mtp`) with the mode named in the `runtime` block. +* `leleka-daemon` is unchanged and still has its narrow Responses subset; retiring that route is a separate step. diff --git a/docs/delivery-gap-audit.md b/docs/delivery-gap-audit.md index 3377b7d3..8197a4b4 100644 --- a/docs/delivery-gap-audit.md +++ b/docs/delivery-gap-audit.md @@ -6,6 +6,12 @@ to this branch. The run recipe is [`rust-runtime-run.md`](rust-runtime-run.md). ## 0. Findings that decide the next work +> Update 2026-10-16, branch `claude/api-over-rust-engine`: finding 1 and section 7 task 1 are addressed in software by +> [`api-over-rust-engine.md`](api-over-rust-engine.md) (the full `strata-next` API in front of the runtime, tested with a scripted +> engine; no real engine run). Findings 2-5 and the delivery tasks are unchanged. The lock flakes of section 1 are most likely a test +> artefact (a `flock` kept alive past `drop` by a child another test forked meanwhile); the reopen in those tests now waits, and 12 of 12 +> parallel runs of the `strata-desktop-core` lib suite passed afterwards (the mechanism itself was not isolated). + 1. **Two serving stacks exist and the full API is wired to the wrong one.** `strata-next` (`control-plane/`) has the Responses / Chat / Messages surface, text.format, custom tools, reasoning, drain, history, MCP hub, but it drives an *engine process over the Original stdio wire protocol* (`READY`/`GEN `/`T `/`DONE`, `control-plane/src/wire.rs`). The Rust engine diff --git a/docs/rust-runtime-run.md b/docs/rust-runtime-run.md index 4d5782f0..8a416dd6 100644 --- a/docs/rust-runtime-run.md +++ b/docs/rust-runtime-run.md @@ -115,6 +115,46 @@ Notes taken from the code: * After an engine restart the model is **not** loaded again automatically; issue `load-mtp` again (new epoch). * `leleka-inference` and `leleka-daemon` shut down gracefully on SIGINT only (observed: the daemon exits 0 on SIGINT and with status 143 on SIGTERM); under systemd use `KillSignal=SIGINT`, as the templates do. +## The full API in front of the runtime: `strata-next` with a `runtime` block + +`leleka-daemon` serves only a narrow text subset of `/v1/responses`. For Codex and the rest of the native API (Responses with +stored responses, tools, `text.format`, reasoning; Chat Completions; Messages; `input_tokens`; drain; metrics) run `strata-next` +with a `runtime` block instead of an `engine` child. Design and limits: [`api-over-rust-engine.md`](api-over-rust-engine.md). + +```text +client / Codex --HTTP, Bearer STRATA api key--> strata-next 127.0.0.1:8081 +strata-next --gRPC, Bearer LELEKA_RUNTIME_TOKEN--> leleka-inference 127.0.0.1:18766 (the same process the daemon talks to) +``` + +Order (steps 1 and 3 of the commands above are unchanged; the daemon is optional and only needed for `leleka-cli`): + +```sh +# 1. engine, as above 2. (optional) leleka-daemon 3. explicit load: leleka-cli ... load-mtp qwen-iq3 qwen4exp-kar-ple-mtp-v1 +# 4. the tokenizer assets strata-next counts tokens with (same GGUF the engine loads; written once, offline): +strata-next model extract-tokenizer $A/model/Qwen3.8-Flash-Next-GSQ-RCO-IQ3_S-00001-of-00002.gguf --output $S/tokenizer-assets +# 5. the API +export LELEKA_RUNTIME_TOKEN=... # same value the engine was started with; 32..4096 printable ASCII bytes +strata-next serve --config control-plane/config.runtime.example.json # edit tokenizer_dir (= $S/tokenizer-assets/tokenizer) and api_key_file +strata-next wait-ready --url http://127.0.0.1:8081 --model qwen-iq3 --key-file /etc/leleka/api-key +``` + +`runtime.model` and `runtime.mode` name what the engine must have loaded (here the pinned IQ3_S artifact and the experimental +`qwen4exp-kar-ple-mtp-v1` mode). `strata-next` **only attaches**: it never sends Load or Unload, so an HTTP request cannot start a +55 GB load. If the runtime has nothing loaded, another artifact, or another mode, `/ready` stays 503 (worker `starting` or +`restarting`) and the reason is logged (`waiting for the engine: ...`); when the runtime restarts, readiness drops within +`runtime.health_interval_s` and returns by itself once the operator has loaded the model again. `/unload` answers `unsupported`. + +What a client can rely on, and what it cannot (verified with a scripted engine only; no model has run through this path): + +* `max_output_tokens` above `runtime.max_output_tokens` (default 4096, the runtime facade's own cap) is refused with a message; + an omitted value is clamped to that cap instead of the usual 32768. +* The runtime accepts 256 generations per epoch; after that every request fails with `generation_capacity` until the runtime + is restarted and loaded again (`api_over_runtime::documents_the_256_generations_per_runtime_epoch_limit`). +* Images, `reasoning_budget_tokens`, `strata_tune` and `experimental_speed_projection` are refused: the text contract cannot carry them. +* `usage.output_tokens` is the engine's count; `reasoning_tokens` is approximate. +* Credential handling: the runtime token comes from the environment variable named by `token_env`, is never printed, and the address + must be a loopback `ip:port`. + ## What was executed for this page (host-side, no model, no GPU context) Host: Ubuntu 26.04.1, kernel 7.0.0-38, driver 595.99.02, RTX 5090 idle apart from the bring-up job, which was left alone.