[PR #1852] [CLOSED] serve: tokenise the prompt from the last shared prefix, not from the start #3719

Closed
opened 2026-11-07 15:30:16 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/1852
Author: @gputier
Created: 10/10/2026
Status: ❌ Closed

Base: main ← Head: pr/prompt-encoder-0.1.41


📝 Commits (8)

  • c73b389 serve: the prompt is tokenised from the last shared prefix, not from the start
  • 4a1bab0 serve: #567's server tests take their reference from a full encode_prompt
  • 62ec02d tools: llama.cpp's qwen35 vectors are checked with the qwen35 vocabulary
  • e13bb1f serve, tools: #567's tests and ByteTokenizer without repeated code
  • 6f6ebc1 serve: #567's kept prompts, one per conversation, none refused for the context
  • da7fa64 serve: keep one prompt per engine conversation cache slot
  • 401cc50 serve: count tokens without storing the prompt in the encoder
  • 9b92530 serve: size the kept prompts from --conversation-cache-slots on a lazy start too

📊 Changes

6 files changed (+776 additions, -14 deletions)

View changed files

📝 SECURITY.md (+3 -1)
📝 serve/server.py (+55 -10)
➕ serve/test_prompt_encoder.py (+162 -0)
📝 serve/test_server.py (+138 -0)
📝 tools/strata_tokenizer.py (+120 -1)
📝 tools/test_strata_tokenizer.py (+298 -2)

📄 Description

This reopens #567 on the new main, as Niko1221 suggested after the history rewrite. It supersedes #567 and closes nothing.

Every request re-encoded the whole conversation before the engine saw it. PromptEncoder keeps the last prompts with their ids and resume points, and encodes only the text that follows the longest shared prefix. The ids are the ones a full encode gives, token for token.

Changes against #567

Rebased on v0.1.42 (61b3fb5d). #931's literals (control-token text inside a message stays text) go through the incremental encoder: the shared prefix ends where the two prompts' plain spans first differ. blange48 merged #567 with #931 the same way on port-567, and stays credited as co-author. f346dc6b's piece cache is kept, and the incremental encoder sits on top of it. Upstream's tools/test_strata_tokenizer.py tests are all kept, and ours are added.

Two fixes of #567's own behaviour:

  • Every rendered prompt starts with the template's header, a special-token boundary, so a prompt from another conversation counted as extending the kept one and replaced it. A subagent's request cost the main conversation its reuse on the next turn (8,276 characters reused, then 236). Now a prompt replaces an entry only when it reuses more than half of that entry's text. Otherwise it is a new entry, and the server keeps one per slot of the engine's conversation cache (--conversation-cache-slots, 4 without it). Token counts (/v1/messages/count_tokens) read the kept prompts without storing one.
  • A prompt refused because it does not fit the context is no longer kept by the encoder. Other refusals keep it, since the client resends the same prompt.

SECURITY.md now says that the server keeps the text and ids of its last few prompts in memory, and that no endpoint shows them.

Numbers and tests

Measured on v0.1.41 with llama.cpp's qwen35 vocabulary, Service.encode_prompt per turn, 12 measured turns per pass, in a 10-CPU Linux container on an Apple M5 Pro:

  • 125K-token agent conversation resent with one more ~500-token turn: 42.5 ms per turn on v0.1.41, 11.0 ms with this PR (3 passes, spread between passes 0.5 ms). Final branch, one pass: 11.7 ms.
  • 89K-token prompt resent identically: 29.5 ms on v0.1.41, 6.6 ms with this PR. Final branch: 6.6 ms.
  • Ids identical to v0.1.41 on all 84 compared turns, and on every turn of the final-branch run.
  • On a slower CPU the absolute times are higher: sergqwer measured 221 ms to 5 ms at 125K on #567, before the piece cache.

python -m pytest serve tools/test_strata_tokenizer.py: 598 passed, 18 skipped, 0 failed (v0.1.41 alone: 584 passed, 11 skipped). The 7 extra skips are the qwen35-vocabulary tests, which need STRATA_TOKENIZER or llama.cpp's vocab file. Fuzz: 2,400 random plain/literal-span cases, incremental against full, and 2,400 random message prompts through Service.encode_prompt with control-token and think text quoted. All equal.

Five commits on v0.1.41:

  • c319b113 serve: the prompt is tokenised from the last shared prefix, not from the start (co-author blange48)
  • b98660e9 serve: #567's server tests take their reference from a full encode_prompt
  • 854564d5 tools: llama.cpp's qwen35 vectors are checked with the qwen35 vocabulary
  • 8941f1f4 serve, tools: #567's tests and ByteTokenizer without repeated code
  • 96e6139b serve: #567's kept prompts, one per conversation, none refused for the context

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Niko1221/Strata/pull/1852 **Author:** [@gputier](https://github.com/gputier) **Created:** 10/10/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `pr/prompt-encoder-0.1.41` --- ### 📝 Commits (8) - [`c73b389`](https://github.com/Niko1221/Strata/commit/c73b389b31b877de2d963584eadb1b598067930d) serve: the prompt is tokenised from the last shared prefix, not from the start - [`4a1bab0`](https://github.com/Niko1221/Strata/commit/4a1bab0c05673efbd4e18ad44ff34835a6949be4) serve: #567's server tests take their reference from a full encode_prompt - [`62ec02d`](https://github.com/Niko1221/Strata/commit/62ec02d31f89c720593e054ad0c89beac647798c) tools: llama.cpp's qwen35 vectors are checked with the qwen35 vocabulary - [`e13bb1f`](https://github.com/Niko1221/Strata/commit/e13bb1f128325a8229d75ce323392f239bba9deb) serve, tools: #567's tests and ByteTokenizer without repeated code - [`6f6ebc1`](https://github.com/Niko1221/Strata/commit/6f6ebc156ef7fac31c78939a3fae009bf922b832) serve: #567's kept prompts, one per conversation, none refused for the context - [`da7fa64`](https://github.com/Niko1221/Strata/commit/da7fa640c5f7e798f90cd1edd12ee6fe441d80c4) serve: keep one prompt per engine conversation cache slot - [`401cc50`](https://github.com/Niko1221/Strata/commit/401cc508410b626a98cd53edb22edd13d4bf373c) serve: count tokens without storing the prompt in the encoder - [`9b92530`](https://github.com/Niko1221/Strata/commit/9b9253061e708b4caec485e984561321ea4e1a0a) serve: size the kept prompts from --conversation-cache-slots on a lazy start too ### 📊 Changes **6 files changed** (+776 additions, -14 deletions) <details> <summary>View changed files</summary> 📝 `SECURITY.md` (+3 -1) 📝 `serve/server.py` (+55 -10) ➕ `serve/test_prompt_encoder.py` (+162 -0) 📝 `serve/test_server.py` (+138 -0) 📝 `tools/strata_tokenizer.py` (+120 -1) 📝 `tools/test_strata_tokenizer.py` (+298 -2) </details> ### 📄 Description This reopens #567 on the new main, as Niko1221 suggested after the history rewrite. It supersedes #567 and closes nothing. Every request re-encoded the whole conversation before the engine saw it. `PromptEncoder` keeps the last prompts with their ids and resume points, and encodes only the text that follows the longest shared prefix. The ids are the ones a full encode gives, token for token. ## Changes against #567 Rebased on v0.1.42 (61b3fb5d). #931's literals (control-token text inside a message stays text) go through the incremental encoder: the shared prefix ends where the two prompts' plain spans first differ. blange48 merged #567 with #931 the same way on port-567, and stays credited as co-author. f346dc6b's piece cache is kept, and the incremental encoder sits on top of it. Upstream's `tools/test_strata_tokenizer.py` tests are all kept, and ours are added. Two fixes of #567's own behaviour: - Every rendered prompt starts with the template's header, a special-token boundary, so a prompt from another conversation counted as extending the kept one and replaced it. A subagent's request cost the main conversation its reuse on the next turn (8,276 characters reused, then 236). Now a prompt replaces an entry only when it reuses more than half of that entry's text. Otherwise it is a new entry, and the server keeps one per slot of the engine's conversation cache (`--conversation-cache-slots`, 4 without it). Token counts (`/v1/messages/count_tokens`) read the kept prompts without storing one. - A prompt refused because it does not fit the context is no longer kept by the encoder. Other refusals keep it, since the client resends the same prompt. SECURITY.md now says that the server keeps the text and ids of its last few prompts in memory, and that no endpoint shows them. ## Numbers and tests Measured on v0.1.41 with llama.cpp's qwen35 vocabulary, `Service.encode_prompt` per turn, 12 measured turns per pass, in a 10-CPU Linux container on an Apple M5 Pro: - 125K-token agent conversation resent with one more ~500-token turn: 42.5 ms per turn on v0.1.41, 11.0 ms with this PR (3 passes, spread between passes 0.5 ms). Final branch, one pass: 11.7 ms. - 89K-token prompt resent identically: 29.5 ms on v0.1.41, 6.6 ms with this PR. Final branch: 6.6 ms. - Ids identical to v0.1.41 on all 84 compared turns, and on every turn of the final-branch run. - On a slower CPU the absolute times are higher: sergqwer measured 221 ms to 5 ms at 125K on #567, before the piece cache. `python -m pytest serve tools/test_strata_tokenizer.py`: 598 passed, 18 skipped, 0 failed (v0.1.41 alone: 584 passed, 11 skipped). The 7 extra skips are the qwen35-vocabulary tests, which need `STRATA_TOKENIZER` or llama.cpp's vocab file. Fuzz: 2,400 random plain/literal-span cases, incremental against full, and 2,400 random message prompts through `Service.encode_prompt` with control-token and think text quoted. All equal. Five commits on v0.1.41: - c319b113 serve: the prompt is tokenised from the last shared prefix, not from the start (co-author blange48) - b98660e9 serve: #567's server tests take their reference from a full encode_prompt - 854564d5 tools: llama.cpp's qwen35 vectors are checked with the qwen35 vocabulary - 8941f1f4 serve, tools: #567's tests and ByteTokenizer without repeated code - 96e6139b serve: #567's kept prompts, one per conversation, none refused for the context --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-07 15:30:16 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Niko1221/Strata#3719
No description provided.