Skip to content

server: every chat request records where each message starts in the prompt, at exact token boundaries (#3617 step 0) - #3679

Merged
ashhart merged 5 commits into
mainfrom
lead/message-starts-0
Nov 17, 2026
Merged

ashhart merged 5 commits into
mainfrom
lead/message-starts-0

Conversation

@ashhart

@ashhart ashhart commented Nov 17, 2026

Copy link
Copy Markdown
Owner

Step 0 of #3617: every chat request now records where each message starts in the rendered prompt, as exact token offsets, beside the chunk plan. Nothing reads these starts yet, so every reply is token-identical to main. The next steps use them to keep prompt states at message starts on each family.

What a start is. The token offset where a message's own part of the prompt begins. The list is sorted; it may leave a message out, but every start it gives is exact:

  • A message gets a start when a template loop iteration over it, or one that binds it (a name, or a macro argument), writes output. The start is the byte where that iteration began writing.
  • Output captured into a set block, a macro body or a scoped block carries its mark with it, through ~ and + joins, to where the string is finally written. A mark whose text is never written is dropped.
  • A start is kept only at a proven token boundary: the prefix up to it must tokenize to the same tokens as the start of the whole prompt. Anything else is left out rather than guessed.

Cost. About one more encode of the text, and constant time per message lookup. A test renders 400 messages through Qwen3.8's template and fails if the time is more than twice the plain render.

Unchanged. Request.chunks, every family's prompt cuts and the prompt cache store.

Tests (test-server-cpu): template marks through captures, macros and joins (message_marks_test.zig); token-boundary proofs with real tokenizer.json files (message_starts_proof_test.zig); the prepared chunk plan compared with main's; and the cost bound. The proof and cost tests fail on the first version of this change and pass here.

Checks: test-server-cpu (only the known slide_forget_test fails, as on main), test-golden 0 unexpected differences, test-server-security 28 passed, test-template-limits-host, test-lane-strand-host and test-lane-api-host (only the known originals_test failures). Files stay at 600 lines or fewer; lean findings are level with main.

  • Synthena

@ashhart

ashhart commented Nov 17, 2026

Copy link
Copy Markdown
Owner Author

/atc exchange 0bb53970548e4699e60dc285551abc3d1b5465c81a62895a44a54eef6571120f

…k plan (#3617 step 0)

The chat template's render records where each request message's own output begins: an iteration of a loop over
the message, or one that sets it to a name or passes it to a macro (GLM-5.3 writes a run of tool results in the
first one's iteration), counts only if it writes top-level output (eval.zig Marks; template.renderMarked).
prompt.renderStarts maps those bytes to tokens, keeping a start only where the piece before it encodes to the
request's own ids (message_starts.zig), and prepare returns them as Rendered.message_starts when the prompt cache
or a prefill step can use them. They travel as api.Request.message_starts to lanes.Stream.message_starts.

Nothing reads them yet: Request.chunks, every family's prefill cuts and the cache store's starts are unchanged, so
every reply is token-identical. Raw completions, image prompts, video-replaced text and segment or native
renderers (Kimi, DeepSeek) carry no starts.

Tests: every message of Qwen3.8's and GLM-5.3's own templates, and of Gemma 4, Llama 3 and Nemotron turn formats
(system, user, assistant, two tool turns, all closer than min_chunk), starts at one whose segment holds that
message's text alone; a request's chunk plan is pinned to main's; a raw completion has no starts.
… text (#3617 step 0)

The template's marks for a message now follow captured output (set blocks,
macro bodies, scoped blocks, ~ and + joins) to where that exact string is
written, and are dropped when the string is changed or never written. A
binding made inside a capture no longer marks the iteration, and keyword
macro arguments bind as positional ones do. Message lookup is a hash map.

A byte start is kept only where the tokens before it spell the text before
it exactly, token by token, and the text on each side re-encodes alone to
exactly its ids; a fused unknown or a normalized byte drops it. The cost is
one walk of the ids and one more encode of the text in total.

The checker's chunk test now passes an empty fields object, which
chat.prepare requires.

Request.chunks, family cuts and the prompt cache store are unchanged.
@ashhart
ashhart force-pushed the lead/message-starts-0 branch from 6ce768a to 7b5f8c4 Compare November 17, 2026 18:05
@ashhart
ashhart merged commit d83a06c into main Nov 17, 2026
2 of 11 checks passed
@ashhart
ashhart deleted the lead/message-starts-0 branch November 17, 2026 18:05
ashhart added a commit that referenced this pull request Nov 17, 2026
ashhart added a commit that referenced this pull request Nov 17, 2026
ashhart added a commit that referenced this pull request Nov 17, 2026
Seven duplicate entries go (a PR's own line beside its lines-file line), five outside authors are thanked, #1503 gets the line its close note promised, and eleven lines now match their PRs' code and receipts (#3129, #3194, #3258, #2696, #2921, #3365, #3345, #3384, #3679, #3682, #3699, #3393).
mrv777 added a commit to mrv777/TensorFold that referenced this pull request Nov 24, 2026
…ind TF_QWEN27_MESSAGE_STARTS (ashhart#3617)

With TF_QWEN27_MESSAGE_STARTS=1 a chat request plans prompt states at up to four of its latest message
starts (the exact token offsets ashhart#3679 records), and the rewind state moves back to the last user message's
start, role header included. A later request that edits a tool result or replaces a message in that history
resumes from that message's start instead of re-reading the whole prompt.

The starts only take room the cache can already promise beside the request's own endpoint: they never
displace another kept state, and under pressure the reservation trims them before the pass. Unset or 0
leaves every plan as on main. Raw prompts carry no starts and are unchanged.
mrv777 added a commit to mrv777/TensorFold that referenced this pull request Nov 25, 2026
…ind TF_QWEN27_MESSAGE_STARTS (ashhart#3617)

With TF_QWEN27_MESSAGE_STARTS=1 a chat request plans prompt states at up to four of its latest message
starts (the exact token offsets ashhart#3679 records), and the rewind state moves back to the last user message's
start, role header included. A later request that edits a tool result or replaces a message in that history
resumes from that message's start instead of re-reading the whole prompt.

The starts only take room the cache can already promise beside the request's own endpoint: they never
displace another kept state, and under pressure the reservation trims them before the pass. Unset or 0
leaves every plan as on main. Raw prompts carry no starts and are unchanged.
ashhart pushed a commit to mrv777/TensorFold that referenced this pull request Nov 25, 2026
…ind TF_QWEN27_MESSAGE_STARTS (ashhart#3617)

With TF_QWEN27_MESSAGE_STARTS=1 a chat request plans prompt states at up to four of its latest message
starts (the exact token offsets ashhart#3679 records), and the rewind state moves back to the last user message's
start, role header included. A later request that edits a tool result or replaces a message in that history
resumes from that message's start instead of re-reading the whole prompt.

The starts only take room the cache can already promise beside the request's own endpoint: they never
displace another kept state, and under pressure the reservation trims them before the pass. Unset or 0
leaves every plan as on main. Raw prompts carry no starts and are unchanged.
ashhart pushed a commit that referenced this pull request Nov 25, 2026
…ind TF_QWEN27_MESSAGE_STARTS (#3617)

With TF_QWEN27_MESSAGE_STARTS=1 a chat request plans prompt states at up to four of its latest message
starts (the exact token offsets #3679 records), and the rewind state moves back to the last user message's
start, role header included. A later request that edits a tool result or replaces a message in that history
resumes from that message's start instead of re-reading the whole prompt.

The starts only take room the cache can already promise beside the request's own endpoint: they never
displace another kept state, and under pressure the reservation trims them before the pass. Unset or 0
leaves every plan as on main. Raw prompts carry no starts and are unchanged.
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.

1 participant