[PR #625] [CLOSED] serve: stable tool order on the DeepSeek paths too (a reordered tool list re-rendered the prompt head, so the engine re-read the conversation) #631

Closed
opened 2026-10-29 21:27:40 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/mw00/project-maya/pull/625
Author: @SeriousRising
Created: 10/28/2026
Status: ❌ Closed

Base: main ← Head: serve-ds4-tool-order


📝 Commits (1)

  • 9fc017b serve: stable tool order on the DeepSeek paths too (a reordered tool list re-rendered the prompt head, so the engine re-read the conversation)

📊 Changes

3 files changed (+51 additions, -9 deletions)

View changed files

📝 serve/frontend.py (+5 -3)
📝 serve/test_ds41_conformance.py (+16 -6)
📝 serve/test_server.py (+30 -0)

📄 Description

Problem. On the DeepSeek paths a request whose tools list arrives in a different order renders the tool block, at the prompt's head, in that new order. The engine's prefix match then breaks at the system prompt and it re-reads the whole conversation. Agent clients do reorder tools (an MCP hub's server order, plugin tools registered late). On 2x CMP 170HX that is ~33-38 s for a 60K-token conversation (an estimate: 60K tokens at the 1700-1850 tok/s we measured for 16K-32K fresh prompts; platform-specific).

Cause (v1.1.35):

  • serve/frontend.py:491 (openai_to_messages, DeepSeek branch): tools = tools_from_openai_format(declared) if declared else None, with no stable_tool_order, unlike the legacy path at :534.
  • serve/frontend.py:601-602 (anthropic_to_messages): if not deepseek: tools = stable_tool_order(tools), so DeepSeek is excluded explicitly.

Fix.

  • stable_tool_order on the DeepSeek OpenAI and Anthropic paths too. tools_from_openai_format returns flat dicts keyed by the namespace-qualified encoding name (ns::name), so namespaced tools with equal names stay distinct. It is the trade the legacy path already makes: a reordered turn renders bit-identical to the first-seen order, schemas untouched.
  • Test isolation: stable_tool_order's memory (_TOOL_ORDERS) is process-global and the DeepSeek converters use it now, so the DeepSeek conformance goldens (serve/test_ds41_conformance.py) clear it in setUp and no longer depend on test order.

Output: a DeepSeek prompt whose tools arrive in a new order now renders them in the first-seen order (the same set, the same schemas). Unchanged for a request whose order does not change, and for the legacy path.

Tests. serve/test_server.py StableToolOrder gains test_deepseek_paths_are_stabilized_too (OpenAI and Anthropic DeepSeek converters, the same set in three orders) and test_deepseek_namespaced_tools_key_on_the_encoding_name; both fail on v1.1.35's frontend.py and pass with the fix. Not measured end to end: we did not replay a rotated-tool session through the server. This is a prefix-reuse fix, not a speed claim.

Checks (as .github/workflows/tests.yml): tools/check_hip_compat.py OK; server tests 465 OK (12 skipped; v1.1.35 runs 463 - the two new DeepSeek tool-order tests); installer and tools tests 584 OK (31 skipped).

🤖 Generated with Claude Code


🔄 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/mw00/project-maya/pull/625 **Author:** [@SeriousRising](https://github.com/SeriousRising) **Created:** 10/28/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `serve-ds4-tool-order` --- ### 📝 Commits (1) - [`9fc017b`](https://github.com/mw00/project-maya/commit/9fc017b17c9c17ceb3527347ff12dcb5ced85c1c) serve: stable tool order on the DeepSeek paths too (a reordered tool list re-rendered the prompt head, so the engine re-read the conversation) ### 📊 Changes **3 files changed** (+51 additions, -9 deletions) <details> <summary>View changed files</summary> 📝 `serve/frontend.py` (+5 -3) 📝 `serve/test_ds41_conformance.py` (+16 -6) 📝 `serve/test_server.py` (+30 -0) </details> ### 📄 Description **Problem.** On the DeepSeek paths a request whose `tools` list arrives in a different order renders the tool block, at the prompt's head, in that new order. The engine's prefix match then breaks at the system prompt and it re-reads the whole conversation. Agent clients do reorder tools (an MCP hub's server order, plugin tools registered late). On 2x CMP 170HX that is ~33-38 s for a 60K-token conversation (an estimate: 60K tokens at the 1700-1850 tok/s we measured for 16K-32K fresh prompts; platform-specific). **Cause** (v1.1.35): - `serve/frontend.py:491` (`openai_to_messages`, DeepSeek branch): `tools = tools_from_openai_format(declared) if declared else None`, with no `stable_tool_order`, unlike the legacy path at `:534`. - `serve/frontend.py:601-602` (`anthropic_to_messages`): `if not deepseek: tools = stable_tool_order(tools)`, so DeepSeek is excluded explicitly. **Fix.** - `stable_tool_order` on the DeepSeek OpenAI and Anthropic paths too. `tools_from_openai_format` returns flat dicts keyed by the namespace-qualified encoding name (`ns::name`), so namespaced tools with equal names stay distinct. It is the trade the legacy path already makes: a reordered turn renders bit-identical to the first-seen order, schemas untouched. - Test isolation: `stable_tool_order`'s memory (`_TOOL_ORDERS`) is process-global and the DeepSeek converters use it now, so the DeepSeek conformance goldens (`serve/test_ds41_conformance.py`) clear it in `setUp` and no longer depend on test order. **Output:** a DeepSeek prompt whose tools arrive in a new order now renders them in the first-seen order (the same set, the same schemas). Unchanged for a request whose order does not change, and for the legacy path. **Tests.** `serve/test_server.py` `StableToolOrder` gains `test_deepseek_paths_are_stabilized_too` (OpenAI and Anthropic DeepSeek converters, the same set in three orders) and `test_deepseek_namespaced_tools_key_on_the_encoding_name`; both fail on v1.1.35's `frontend.py` and pass with the fix. Not measured end to end: we did not replay a rotated-tool session through the server. This is a prefix-reuse fix, not a speed claim. **Checks** (as `.github/workflows/tests.yml`): `tools/check_hip_compat.py` OK; server tests 465 OK (12 skipped; v1.1.35 runs 463 - the two new DeepSeek tool-order tests); installer and tools tests 584 OK (31 skipped). 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-29 21:27:40 +07:00
Sign in to join this conversation.
No labels
pull-request
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
mw00/project-maya#631
No description provided.