[PR #1624] [CLOSED] frontend: the tools in a canonical order #3568

Closed
opened 2026-11-07 14:42:23 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/1624
Author: @XinacS
Created: 10/8/2026
Status: ❌ Closed

Base: main ← Head: f-tool-order


📝 Commits (2)

  • 156b6f3 frontend: _tool_list returns the tools sorted by name - clients send them in the order their MCP servers finished registering, and the template renders the tool list before the system prompt, so one swap moves the shared prefix back thousands of tokens and the conversation is read again
  • 132ddb0 frontend: "system_reminders": "tail" moves a client's system-reminder block out of a past turn onto the last user turn

📊 Changes

3 files changed (+103 additions, -8 deletions)

View changed files

📝 serve/frontend.py (+47 -5)
📝 serve/server.py (+8 -3)
📝 serve/test_server.py (+48 -0)

📄 Description

_tool_list returns the tools sorted by name. Clients send them in the order their MCP servers finished registering, and the chat template renders the tool list before the system prompt, so one swap in that list moves the shared prefix back thousands of tokens and the whole conversation is read again. Sorted by name, the rendered tool region is identical on every request.

Measured before (opencode with the swift-iq3_xxs multi config: parallel 3, 262144 context, 16 conversation slots): a request of 190283 tokens reused 81694 and read 108589, with the shared prefix ending at token 3977. The text at the cut was the tool list, with rbash_remote-bash and read swapped between the two requests - a full conversation re-read caused by nothing the user did.

opencode has the same finding and closed it as not planned: https://github.com/anomalyco/opencode/issues/23571 (MCP tool ordering changes after restart and breaks prompt caching). Sorting on the serving side fixes it for every client, not just opencode.

Measured after (same setup): warm requests reuse 98.4% of prompt tokens, 7 turns in a row at 98.2-99.9% (42990 = 42946 reused, 38643 = 38397 reused; cold conversation starts excluded). No 3977-prefix cut recurred.

Tests: ToolOrder in serve/test_server.py - both request shapes (OpenAI {"type":"function","function":{...}} and Anthropic {"name":..,"input_schema":..}) come out sorted whatever order they arrive in. Full suite: 264 tests OK.


🔄 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/1624 **Author:** [@XinacS](https://github.com/XinacS) **Created:** 10/8/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `f-tool-order` --- ### 📝 Commits (2) - [`156b6f3`](https://github.com/Niko1221/Strata/commit/156b6f3bcf65196350dbe4eb114a82fb44554623) frontend: _tool_list returns the tools sorted by name - clients send them in the order their MCP servers finished registering, and the template renders the tool list before the system prompt, so one swap moves the shared prefix back thousands of tokens and the conversation is read again - [`132ddb0`](https://github.com/Niko1221/Strata/commit/132ddb00f979b9dd09796baee8ad34198d16a39e) frontend: "system_reminders": "tail" moves a client's system-reminder block out of a past turn onto the last user turn ### 📊 Changes **3 files changed** (+103 additions, -8 deletions) <details> <summary>View changed files</summary> 📝 `serve/frontend.py` (+47 -5) 📝 `serve/server.py` (+8 -3) 📝 `serve/test_server.py` (+48 -0) </details> ### 📄 Description `_tool_list` returns the tools sorted by name. Clients send them in the order their MCP servers finished registering, and the chat template renders the tool list before the system prompt, so one swap in that list moves the shared prefix back thousands of tokens and the whole conversation is read again. Sorted by name, the rendered tool region is identical on every request. **Measured before** (opencode with the `swift-iq3_xxs` multi config: parallel 3, 262144 context, 16 conversation slots): a request of 190283 tokens reused 81694 and read 108589, with the shared prefix ending at token 3977. The text at the cut was the tool list, with `rbash_remote-bash` and `read` swapped between the two requests - a full conversation re-read caused by nothing the user did. opencode has the same finding and closed it as not planned: https://github.com/anomalyco/opencode/issues/23571 (MCP tool ordering changes after restart and breaks prompt caching). Sorting on the serving side fixes it for every client, not just opencode. **Measured after (same setup):** warm requests reuse 98.4% of prompt tokens, 7 turns in a row at 98.2-99.9% (42990 = 42946 reused, 38643 = 38397 reused; cold conversation starts excluded). No 3977-prefix cut recurred. **Tests:** `ToolOrder` in serve/test_server.py - both request shapes (OpenAI `{"type":"function","function":{...}}` and Anthropic `{"name":..,"input_schema":..}`) come out sorted whatever order they arrive in. Full suite: 264 tests OK. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-07 14:42:23 +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#3568
No description provided.