Skip to content

run_serve: refuse over-context prompts with CONTEXT_EXCEEDED instead of silently truncating - #2104

Merged
JustVugg merged 3 commits into
JustVugg:devfrom
Varshith-Kali:fix/run-serve-context-exceeded-2097
Oct 24, 2026
Merged

JustVugg merged 3 commits into
JustVugg:devfrom
Varshith-Kali:fix/run-serve-context-exceeded-2097

Conversation

@Varshith-Kali

Copy link
Copy Markdown
Contributor

Fixes #2097

What was wrong

The legacy run_serve path (coli chat and the \x02PROMPT pre-mux API mode) encoded prompts with a hard tok_encode cap and no overflow signal. An over-long prompt was silently cut to the cap and the engine generated a normal-looking answer from the truncated text -- the exact #401 field failure the mux path already refuses loudly. The interactive arm additionally clipped rendered turns at the 64 KB buf and, when the conversation filled the context, silently wiped history (len=0) and restarted from a clamped prompt.

What the fix does

Mirrors the mux_submit pattern in both run_serve arms: encode into a maxctx-1 buffer purely to detect overflow (a full buffer means the prompt does not fit) and refuse instead of generating from cut text.

  • \x02PROMPT arm: over-context prompts are refused rather than truncated.
  • Interactive arm: refuses on token overflow, on a clipped 64 KB render (detected via the snprintf return value), and when the conversation history no longer fits -- the old silent history wipe is gone; the stderr message points at \x02RESET.

The legacy protocol has no ERROR frame, so a refusal is an empty answer plus a STAT line carrying CONTEXT_EXCEEDED as a seventh token (coli's STAT regex matches the first fields and ignores the rest) and a stderr line naming the cause and the remedy (raise CTX, shorten the prompt). This matches how every sibling engine and the mux path refuse. docs/serve_protocol.md documents the new refusal contract. coli_serve_budget() was checked and doesn't fit here -- it computes a generation budget from a known prompt size, not overflow detection.

Verification

  • make -C c colibri builds clean under the repo's flags (-Wall -Wextra), no new warnings.
  • Code-path review of both arms: refusal paths free tmp/raw, restore g_temp/g_nuc, leave hist/len/KV untouched (a refused turn corrupts no state), and the hist+len memcpy stays within bounds on the accepted path (len+k <= maxctx-9-g_draft).
  • Standalone check of the snprintf truncation-detection idiom (fits / overflows / exact-fit cases) behaves as coded.
  • No runtime test: serving needs model weights, which aren't available here. The boundary behavior (a prompt of exactly maxctx-2 tokens is accepted with near-zero generation room, same as mux_submit) mirrors the mux path.

@JustVugg
JustVugg changed the base branch from main to dev October 22, 2026 06:51
@JustVugg

Copy link
Copy Markdown
Owner

Thanks for this. The API-mode half is right: a \x02PROMPT that does not fit is now refused instead of answered from cut text.

The interactive half changes what coli chat does once a conversation fills the context. Before, the engine reset the history and answered the new turn. With this change every following turn gets an empty answer, and the reason goes only to the engine's stderr, which coli chat writes to a temporary log and does not show. The user would see empty answers until they type :reset, without being told to.

Either of these would work:

  • keep the refusal, and have coli chat read the CONTEXT_EXCEEDED token on the STAT line and print the reason with the :reset hint (the gateway could then answer the API mode's refusal with a 400, as it does for the mux path);
  • or keep the old reset in the interactive mode, with a line coli chat shows, and refuse only in the API mode.

A test would help too: the tiny oracle model (tools/make_glm_oracle.py) with a small CTX and a prompt longer than it is enough to show the refusal in both modes.

It stays out of today's batch (#2284) for that reason only.

@Varshith-Kali

Copy link
Copy Markdown
Contributor Author

Went with option (a): the refusal stays in both arms, and coli chat now reads the CONTEXT_EXCEEDED token on the STAT line and prints the reason with the hint that fixes it, instead of an empty answer.

What changed (pushed as 918a979):

  • The engine appends the refusal cause to the STAT line, next to the CONTEXT_EXCEEDED token: PROMPT_TOO_LONG, HISTORY_FULL, or RENDER_CLIPPED. Existing STAT parsers match the leading fields and ignore the rest, so the frame stays compatible.
  • coli chat's stream_turn picks up the token, and the chat loop prints e.g. [context is full: the conversation history no longer fits the context; type :reset to clear it and continue]. An over-long turn says to shorten the prompt or raise CTX (noting :reset only clears history), and a clipped render says to paste shorter chunks. An older engine that sends no cause token still gets the generic message.
  • docs/serve_protocol.md documents the cause token.

On the gateway half: I checked, and no change was needed. openai_server.py launches the engine with SERVE_BATCH=1, so it only ever speaks the mux protocol; the legacy \x02PROMPT arm has no gateway consumer in the repo, and the mux CONTEXT_EXCEEDED ERROR already maps to the 400 via _engine_error.

Test evidence, with the tiny oracle from c/tools/make_glm_oracle.py at CTX=64:

  • c/tests/test_run_serve_context_exceeded.py (new, 4 tests, all passing): drives the real engine with SERVE=1 and asserts the refusal STAT in the interactive arm (STAT 0 0.00 0.0 ... 63 0 CONTEXT_EXCEEDED PROMPT_TOO_LONG for a 120-token prompt), in the \x02PROMPT API arm, the HISTORY_FULL cause once a second fitting turn overflows the conversation, and that a fitting prompt is still answered normally.
  • c/tests/test_chat_context_exceeded.py (new, 4 tests, all passing): runs the real cmd_chat against a fake refusing engine and asserts the user-facing message for each cause spelling, plus the fallback for an engine without the cause token.
  • The engine rebuilds cleanly (make -C c colibri, no warnings) and the existing test_chat_engine_stderr suite still passes.

@JustVugg

Copy link
Copy Markdown
Owner

Thanks, this does what was asked: coli chat now says why the turn was refused and what to type. I merged it locally onto dev with the other PRs of the next batch: it builds with no warnings, test_chat_context_exceeded.py passes, and test_run_serve_context_exceeded.py passes too once COLI_ORACLE_FIXTURE points at the glm_tiny that tools/make_glm_oracle.py writes.

One thing before it goes in: no workflow sets COLI_ORACLE_FIXTURE, so in CI those four engine-side tests are always skipped, and a regression in run_serve would pass. Could you run them in the Linux job of .github/workflows/ci.yml, after the step that generates the fixture? For example in the "Oracle allowance and failure regressions" step:

cd c && python3 tests/test_glm_oracle.py && COLI_ORACLE_FIXTURE=$PWD/glm_tiny python3 -m unittest tests.test_run_serve_context_exceeded

With that it goes into the next batch.

@bherald

bherald commented Oct 23, 2026

Copy link
Copy Markdown
Contributor

I used Codex to put together a small CI change for this PR. The Linux job already generates glm_tiny, so this reuses that fixture to run the four serving-context tests after the existing oracle checks.

Commit 049edad1 changes only .github/workflows/ci.yml:

cd c
python3 tests/test_glm_oracle.py
COLI_ORACLE_FIXTURE="$PWD/glm_tiny" python3 -m unittest -v tests.test_run_serve_context_exceeded

The local CPU run passed all 17 oracle checks and all four serving-context tests, with none skipped. Running the serving tests without the fixture variable skipped all four.

The generated fixture was 2.46 MB. The checks used CPU-only PyTorch 2.14.1, Transformers 5.20.2 and Safetensors 0.8.0, with model downloads disabled. GitHub Actions has not run for this commit yet.

You're welcome to cherry-pick it into this PR.

@Varshith-Kali

Copy link
Copy Markdown
Contributor Author

Done: the Linux efficiency job now runs the four run_serve context-refusal tests right after the oracle checks, with COLI_ORACLE_FIXTURE pointed at the generated glm_tiny (commit 402e8d1). Verified the step parses as valid YAML and the test module collects all four (they skip cleanly without the fixture, as expected on a box with no built engine). The workflow run on the new head should exercise them for real.

@Varshith-Kali

Copy link
Copy Markdown
Contributor Author

Thanks for putting this together, and for the local verification numbers. Applied the equivalent change to this PR (commit 402e8d1), crediting your patch bherald/colibri@049edad1 in the commit message. Only tweak is a slightly shorter comment line in the step.

…of silently truncating

The legacy run_serve path (coli chat and the \x02PROMPT pre-mux API mode)
encoded prompts with a hard tok_encode cap and no overflow signal, so an
over-long prompt was silently cut and answered from the truncated text --
the exact JustVugg#401 field failure the mux path already refuses loudly.

Mirror the mux_submit pattern in both run_serve arms: encode into a
maxctx-1 buffer purely to detect overflow (a full buffer means the prompt
does not fit) and refuse. The legacy protocol has no ERROR frame, so a
refusal is an empty answer plus a STAT line carrying CONTEXT_EXCEEDED as a
seventh token (coli's STAT regex matches the first fields and ignores the
rest) and a stderr line naming the cause.

The interactive arm additionally detects a clipped 64KB render via the
snprintf return value and refuses rather than answering from clipped text;
a conversation history that no longer fits is refused with a hint to
\x02RESET instead of being silently wiped and restarted.

Fixes JustVugg#2097

Signed-off-by: Varshith Puli <pulivarshit@gmail.com>
Follow-up to the JustVugg#2097 refusal: the interactive arm refused over-context
turns with an empty answer and the reason only on the engine's stderr,
which coli chat drains to a temp log and never shows, so users saw empty
answers with no hint to :reset.

Keep the refusal in both arms (option a), and make it visible:

- The engine now appends the refusal cause to the STAT line
  (PROMPT_TOO_LONG, HISTORY_FULL, RENDER_CLIPPED), next to the
  CONTEXT_EXCEEDED token the previous commit added. Existing STAT parsers
  match the leading fields and ignore the rest, so the frame stays
  compatible.
- coli chat's stream_turn reads the token and the chat loop prints the
  reason with the hint that fixes it: :reset for a full history, a
  shorter prompt or larger CTX for an over-long turn, smaller chunks for
  a clipped render.

Tests: c/tests/test_run_serve_context_exceeded.py drives the real engine
(SERVE=1) against the tiny GLM oracle at CTX=64 and asserts the refusal
STAT in the interactive arm, the \x02PROMPT API arm, the HISTORY_FULL
cause on a filled conversation, and that a fitting prompt is still
answered. c/tests/test_chat_context_exceeded.py runs the real cmd_chat
against a fake refusing engine and asserts the user-facing message for
each cause spelling (and for an older engine with no cause token).

No gateway change: openai_server.py only speaks the mux protocol
(SERVE_BATCH=1); the legacy \x02PROMPT arm has no gateway consumer, and
the mux CONTEXT_EXCEEDED ERROR already maps to HTTP 400.

Signed-off-by: Varshith Puli <pulivarshit@gmail.com>
…ny fixture

The Linux efficiency job generates glm_tiny but never set
COLI_ORACLE_FIXTURE, so the four run_serve context-refusal tests always
skipped in CI and a regression there would pass silently. Run them after
the oracle checks, reusing the same fixture, as requested in review.

Based on bherald's patch (bherald/colibri@049edad1), applied with a
small comment tweak.
@Varshith-Kali
Varshith-Kali force-pushed the fix/run-serve-context-exceeded-2097 branch from 402e8d1 to 5227865 Compare October 23, 2026 22:35
@Varshith-Kali

Copy link
Copy Markdown
Contributor Author

Heads up: rebased onto current dev to clear a conflict (dev added the MTP draft-gate step right after the oracle step in the meantime). The serving-context test step is preserved after the oracle checks, ahead of the new MTP step. CI is running on the new head 5227865.

@JustVugg
JustVugg merged commit c3dc99b into JustVugg:dev Oct 24, 2026
61 of 116 checks passed
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.

[Bug]: legacy run_serve path (coli chat / \x02PROMPT) silently truncates prompts that exceed CTX instead of refusing with CONTEXT_EXCEEDED

3 participants