Run frontier MoE models on hardware you already own β pure C, zero deps, experts streamed from disk. Tiny engine, immense model. π¦
C
feat(serve): continue a trailing assistant turn automatically, default on
eni707cc4792361
diff --git a/c/openai_server.py b/c/openai_server.pyindex 2ff47220..0b544f57 100644--- a/c/openai_server.py+++ b/c/openai_server.py@@ -2330,8 +2330,18 @@ def render_chat_dsv41(messages, enable_thinking=False, reasoning_effort=None, to# llama.cpp needs no switch for this because it runs the checkpoint's jinja at request time,# so `add_generation_prompt=False` costs it nothing. This gateway renders by hand, on purpose# and for speed (tests/test_glm53_chat_template.py says why), and the bill for that choice is-# exactly here: one template flag, eight open-turn shapes to derive. That is the honest reason-# this ships for one family behind a switch instead of for all of them by default.+# exactly here: one template flag, one open-turn shape to derive per renderer. Each string+# renderer derives its own, pinned byte-for-byte against the checkpoint's template;+# CONTINUATION_FAMILIES is the set that has done so. Kimi K3 is the exception -- its prompt is+# framed engine-side (render_chat_kimi hands a K3CHAT1 record to kimi_k3.c, which assembles the+# XTML tokens), so its open turn is a change in the C path, not here, and is left to that engine.++# Families whose renderer implements the add_generation_prompt=False (open-turn) branch. A+# trailing assistant turn on a family NOT in this set falls through to the ordinary render+# (the cue is appended, exactly as before this existed) rather than erroring -- continuation is+# on by default, and a family without its open-turn shape yet must not start rejecting requests+# nobody opted into. Each renderer adds itself here in the same commit that derives its shape.+CONTINUATION_FAMILIES = {"glm53"}def resolve_generation_prompt(messages, body):@@ -2340,33 +2350,31 @@ def resolve_generation_prompt(messages, body):Returns True for the ordinary case (append the cue) and False for a continuation, which isthe template's `add_generation_prompt=False`.- COLI_CONTINUE_ASSISTANT=1 turns continuation on for the whole server, the way- COLI_THINK and COLI_TOOL_SALVAGE turn on their behaviours. It is deliberately NOT a- request field: a client would have to know colibri specifically to send one, and the- clients that most want this -- anything pointed at an OpenAI- or Anthropic-compatible- URL -- send a message list and nothing else. A trailing assistant turn already says- "continue me"; a second way to say it in the body would only be reachable by hand.-- Off is the default so that no existing deployment changes under anyone. Read it as a- migration switch rather than a mode: what it disables is not a behaviour anybody wants,- only the one that is there today.-- While off, both protocol paths keep their existing behaviour so clients sending a- trailing assistant turn today do not start getting errors from a release they did not- opt into.+ Continuation is ON by default. A message list ending in a non-empty assistant turn already+ says "continue me" -- the same contract as Anthropic's API -- and no OpenAI-compatible+ client sends a trailing assistant turn by accident. It is deliberately NOT a request field:+ a client would have to know colibri specifically to send one, and the clients that most+ want this -- anything pointed at an OpenAI- or Anthropic-compatible URL -- send a message+ list and nothing else.++ COLI_CONTINUE_ASSISTANT=0 is the off-switch, for a deployment that wants the old behaviour+ (fold the trailing turn into a completed one and append a fresh cue). It is the only value+ that turns this off; anything else, including unset, leaves it on.++ A family whose renderer has no open-turn shape yet (ARCH not in CONTINUATION_FAMILIES --+ Kimi K3, whose turn is framed engine-side) falls through to the ordinary render rather than+ erroring: continuation defaults on, so an unimplemented family must not start rejecting+ trailing-assistant requests that worked before."""- continuing = os.environ.get("COLI_CONTINUE_ASSISTANT", "0") == "1"+ continuing = os.environ.get("COLI_CONTINUE_ASSISTANT", "1") != "0"last = messages[-1] if isinstance(messages, list) and messages else Noneif not (isinstance(last, dict) and last.get("role") == "assistant"):return Truewhere = f"messages.{len(messages) - 1}"if not continuing:return True- if ARCH != "glm53":- raise APIError(400, f"COLI_CONTINUE_ASSISTANT is on, but continuing an assistant "- f"turn is implemented for the glm53 engine only, not {ARCH!r}: every "- f"family needs its own open-turn shape derived from its own template.",- "messages", "unsupported_parameter")+ if ARCH not in CONTINUATION_FAMILIES:+ return True # open-turn shape not derived for this family yet -- render as beforeif body.get("tools") or body.get("functions"):raise APIError(400, "A continued assistant turn cannot be combined with `tools`: ""the tool-call parsers read an assistant turn from its start, and a "@@ -2407,12 +2415,12 @@ def render_chat_for_arch(messages, enable_thinking=False, reasoning_effort=None,tool_choice=None, audio_out=None, add_generation_prompt=True):"""Render a chat request with the active engine's native prompt contract.- `add_generation_prompt=False` (a continued assistant turn) is implemented for glm53 only.- resolve_generation_prompt() refuses it for every other family upstream; the assert- here is the backstop, because silently appending a cue to a continuation is the exact- failure this change exists to remove.+ `add_generation_prompt=False` (a continued assistant turn) is implemented for the families+ in CONTINUATION_FAMILIES. resolve_generation_prompt() passes any other family through with+ the cue appended, so it never reaches here with the flag False; this stays as the backstop,+ because silently appending a cue to a continuation is the exact failure this exists to remove."""- if not add_generation_prompt and ARCH != "glm53":+ if not add_generation_prompt and ARCH not in CONTINUATION_FAMILIES:raise APIError(400, f"Continuing an assistant turn is not implemented for {ARCH!r}.","messages", "unsupported_parameter")if ARCH == "inkling":diff --git a/c/tests/test_openai_server.py b/c/tests/test_openai_server.pyindex 51428bbe..ae4f58f7 100644--- a/c/tests/test_openai_server.py+++ b/c/tests/test_openai_server.py@@ -2090,8 +2090,16 @@ class TrailingAssistantTurnTest(unittest.TestCase):self.assertFalse(prompt.endswith("<|assistant|><think>"))self.assertEqual(prompt.count("<|assistant|>"), 1)- def test_switch_off_changes_nothing(self):- """The default deployment must behave exactly as it did before this existed."""+ def test_default_is_on(self):+ """Continuation is the default now: unset (the ordinary deployment) continues a+ trailing assistant turn. Only COLI_CONTINUE_ASSISTANT=0 turns it off."""+ with patch.dict(os.environ, {}, clear=False), patch("openai_server.ARCH", "glm53"):+ os.environ.pop("COLI_CONTINUE_ASSISTANT", None)+ self.assertFalse(resolve_generation_prompt(self.OPEN_TURN, {}))++ def test_switch_off_restores_old_behaviour(self):+ """COLI_CONTINUE_ASSISTANT=0 is the off-switch: a deployment that sets it behaves+ exactly as the gateway did before continuation existed -- append a fresh cue."""with self.off(), patch("openai_server.ARCH", "glm53"):self.assertTrue(resolve_generation_prompt(self.OPEN_TURN, {}))self.assertEqual(render_chat_glm53(self.OPEN_TURN, enable_thinking=True),@@ -2135,16 +2143,17 @@ class TrailingAssistantTurnTest(unittest.TestCase):with self.assertRaises(APIError):resolve_generation_prompt(calls, {})- def test_rejects_families_without_an_open_turn_shape(self):- """Every family needs its own, derived from its own template. Until then, say so- instead of appending a cue to a turn the client meant to be continued."""+ def test_unimplemented_family_passes_through(self):+ """Continuation is on by default, so a family whose renderer has no open-turn shape+ yet must render as before -- append the cue -- not reject a request nobody opted into.+ Each family leaves this list in the commit that derives its shape; Kimi K3 (framed+ engine-side, so its open turn is a change in kimi_k3.c) is the standing case."""for arch in ("glm", "deepseek_v4", "kimi", "qwen38", "olmoe", "inkling", "qwen36"):with self.on(), patch("openai_server.ARCH", arch):- with self.assertRaises(APIError):- resolve_generation_prompt(self.OPEN_TURN, {})+ self.assertTrue(resolve_generation_prompt(self.OPEN_TURN, {}))def test_splitter_starts_in_content_mode_on_a_continued_turn(self):- """Measured glm53 int4, CPU: content '' with reasoning_chars 10 and 109,+ """Measured on glm53 int4, CPU: content '' with reasoning_chars 10 and 109,clean stop, and a byte-correct open turn on the wire. The model was fine; thesplitter was primed from enable_thinking alone, so it waited for a </think> theprompt had already passed and filed the whole answer as reasoning.diff --git a/docs/ENVIRONMENT.md b/docs/ENVIRONMENT.mdindex befe5a03..8f0f17d4 100644--- a/docs/ENVIRONMENT.md+++ b/docs/ENVIRONMENT.md@@ -465,7 +465,7 @@ These are read by the Python programs (not the `glm` engine), so they don't appe| `COLI_DEBUG` | `0` (off) | Tee the engine transaction to stderr, by level. **`1`** = decoded model output stream only (byte-by-byte, on both the tool-call and plain paths). **`2`** = both sides β the fully-rendered prompt the engine received *and* the output, bracketed and correlated by request id, so stderr reads as the whole conversation. Invaluable for seeing what the model received vs. emitted during an OpenCode session. || `COLI_TOOL_SALVAGE` | `0` (off) | Opt-in de-mangler: reconstruct a malformed int4 tool call by mapping its lone payload onto the tool's primary parameter. Never rewrites well-formed output; recommended for int4 deployments. || `COLI_THINK` | `0` (off) | Make thinking the default when the client sends *neither* `reasoning_effort` nor `enable_thinking`. Any explicit client value still wins. |-| `COLI_CONTINUE_ASSISTANT` | `0` (off) | On the OpenAI- and Anthropic-compatible chat endpoints, continue a trailing `assistant` message instead of opening a new turn β the checkpoint's own template with `add_generation_prompt=False`. `glm53` only; refused with `tools`, and the turn must carry text not ending in whitespace. Off preserves each endpoint's existing behavior. Unrelated to `COLI_PREFILL_CHUNK`, which is the compute phase. |+| `COLI_CONTINUE_ASSISTANT` | `1` (on) | On the OpenAI- and Anthropic-compatible chat endpoints, continue a trailing `assistant` message instead of opening a new turn β the checkpoint's own template with `add_generation_prompt=False`. On by default: a message list ending in a non-empty `assistant` turn continues, the same contract as Anthropic's API. Set `0` to restore the old behavior (append a fresh generation cue). Refused with `tools`/`tool_calls`, and the turn must carry text not ending in whitespace. A family whose renderer has no open-turn shape yet (Kimi K3, framed engine-side) falls through to the old behavior. Unrelated to `COLI_PREFILL_CHUNK`, which is the compute phase. || `COLI_MODEL` | unset | Default model directory (fallback for `--model`). || `COLI_MODEL_ID` | `glm-5.2-colibri` | Model id reported by the API. || `COLI_API_KEY` | unset | Required bearer token for the server. |diff --git a/docs/api.md b/docs/api.mdindex fa7298c7..845c6ba9 100644--- a/docs/api.md+++ b/docs/api.md@@ -28,18 +28,20 @@ completion requests support JSON responses, SSE streaming, usage counts,custom `stop` sequences. Stop sequences are removed from the response and endgeneration early in both JSON and streaming modes.-The server-side switch `COLI_CONTINUE_ASSISTANT=1` makes a trailing `assistant`-message *continue* that turn instead of starting a new one: the prompt ends-inside it, which is the official template rendered with-`add_generation_prompt=False`. There is no request field for this β a trailing-assistant turn already says "continue me", and a body extension would only be-reachable by hand-written JSON rather than from the clients that want it.-Implemented for the `glm53` engine only (every family needs an open-turn shape-derived from its own template) and refused together with `tools`, because the-tool-call parsers read an assistant turn from its start. The continuation must-carry text and must not end with whitespace: the template strips trailing-whitespace, so the model would resume from different bytes than the ones sent.-Off by default, and off means exactly today's behaviour.+A trailing `assistant` message *continues* that turn instead of starting a new+one: the prompt ends inside it, which is the official template rendered with+`add_generation_prompt=False`. This is on by default β a message list ending in a+non-empty `assistant` turn continues, the same contract as Anthropic's API β and+there is no request field for it, because a trailing assistant turn already says+"continue me" and a body extension would only be reachable by hand-written JSON+rather than from the clients that want it. The server-side switch+`COLI_CONTINUE_ASSISTANT=0` restores the old behaviour (fold the turn into a+completed one and append a fresh cue). Continuation is refused together with+`tools`/`tool_calls`, because the tool-call parsers read an assistant turn from+its start, and the turn must carry text not ending in whitespace: the template+strips trailing whitespace, so the model would resume from different bytes than+the ones sent. A family whose renderer has no open-turn shape yet β Kimi K3,+whose turn is framed engine-side β falls through to the old behaviour.A continuation resumes from the exact bytes you send, which makes the splitpoint part of the prompt. Splitting mid-word puts the model at a token boundary@@ -163,9 +165,13 @@ Not supported, and refused explicitly rather than ignored: `stop_sequences`,features that have not been wired to this protocol are likewise rejected withan explicit error.-A trailing `assistant` message keeps this endpoint's existing behavior while-`COLI_CONTINUE_ASSISTANT` is off. With the switch on, both the Anthropic- and-OpenAI-compatible endpoints continue that turn on `glm53`.+A trailing `assistant` message continues that turn by default on both the+Anthropic- and OpenAI-compatible endpoints (`COLI_CONTINUE_ASSISTANT=0` restores+the old behavior, where this endpoint appended a fresh cue). Note this changes+what an existing Anthropic client sees on `/v1/messages`: a trailing assistant+turn now continues rather than starting fresh β which is the real Anthropic+contract β and the off-switch is the escape hatch for anyone relying on the old+behavior.> The prefill warning below applies here too, and applies *hardest* to Claude Code:> its system prompt and tool catalog are large, and on a disk-streaming CPU path