[PR #626] serve: the tool-call parser resumes its </tool_call> search; TCP_NODELAY on streamed replies #634

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

📋 Pull Request Information

Original PR: https://github.com/mw00/project-maya/pull/626
Author: @SeriousRising
Created: 10/28/2026
Status: 🔄 Open

Base: main ← Head: serve-parser-nodelay


📝 Commits (1)

  • 72aa0e6 serve: the tool-call parser resumes its </tool_call> search; TCP_NODELAY on streamed replies

📊 Changes

4 files changed (+74 additions, -1 deletions)

View changed files

📝 serve/frontend.py (+8 -1)
📝 serve/server.py (+7 -0)
📝 serve/test_server.py (+27 -0)
📝 serve/test_tools.py (+32 -0)

📄 Description

Two small serve changes on the streaming path; both leave the bytes sent unchanged.

Problem. While the model streams a long tool argument (an agent writing a file), the handler thread's CPU per token grows with the argument's length.

Cause. serve/frontend.py:1350 (v1.1.35), in call state: i = self.buf.find(CALL_END) searches from 0 on every piece, and buf holds the whole call so far: O(n²) over the call. #403 / #482 fixed what is emitted while a call streams; this is the search cost, which they did not touch.

Fix. After a miss, keep _call_resume = max(0, len(buf) - len(CALL_END) + 1) and search from there. That is sound because in call state buf only grows (the one temporary swap for stream_tools restores it whole). The offset is reset on entering call state.

Output: the same events. serve/test_tools.py CallEndRescan feeds a call whose argument contains </tool_cal, </tool_callx and trailing partial fragments at piece sizes 1 / 2 / 3 / 5 / 11 / 64, and requires the whole-feed result each time; test_search_resume_advances_with_the_body errors on v1.1.35's frontend.py and passes with the fix.

Measured (a 200K-char string argument in 20-char pieces through OutputParser(stream_tools=True), the same 8 events):

CPU before after
Ryzen 7 9800X3D (the same change on our v1.1.29-based tree) 229 ms (22.9 µs a piece) 56 ms (5.6 µs a piece)
Xeon E5-2697 v4 VM, v1.1.35 vs this branch, while an engine run used the CPU lane (best of 3, not a clean benchmark) 872 ms 492 ms

2. TCP_NODELAY on streamed replies

Problem. Streamed replies are small frames, often one token each. With Nagle on and the client's delayed ACK, a frame can wait for the ACK (classically ~40 ms) on a LAN hop, e.g. to a proxy in front of Maya.

Fix. Handler._sse() (serve/server.py:4276, the one entry for every streaming path) sets IPPROTO_TCP / TCP_NODELAY = 1; an OSError is ignored. The bytes are identical; only the packetization differs.

Test: serve/test_server.py NoDelayStreaming uses a real loopback socket and checks the option goes 0 -> 1 through _sse(); it fails on v1.1.35's server.py. Not measured: we did not timestamp frame arrivals with and without it; the ~40 ms is the textbook Nagle / delayed-ACK interaction, not our measurement.

No new settings.

Checks (as .github/workflows/tests.yml): tools/check_hip_compat.py OK; server tests 466 OK (12 skipped; v1.1.35 runs 463 - the new parser / NODELAY 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/626 **Author:** [@SeriousRising](https://github.com/SeriousRising) **Created:** 10/28/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `serve-parser-nodelay` --- ### 📝 Commits (1) - [`72aa0e6`](https://github.com/mw00/project-maya/commit/72aa0e61e6f2592574ae40e35db7a92dd8d0fc00) serve: the tool-call parser resumes its </tool_call> search; TCP_NODELAY on streamed replies ### 📊 Changes **4 files changed** (+74 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `serve/frontend.py` (+8 -1) 📝 `serve/server.py` (+7 -0) 📝 `serve/test_server.py` (+27 -0) 📝 `serve/test_tools.py` (+32 -0) </details> ### 📄 Description Two small serve changes on the streaming path; both leave the bytes sent unchanged. ### 1. The tool-call parser resumes its `</tool_call>` search **Problem.** While the model streams a long tool argument (an agent writing a file), the handler thread's CPU per token grows with the argument's length. **Cause.** `serve/frontend.py:1350` (v1.1.35), in call state: `i = self.buf.find(CALL_END)` searches from 0 on every piece, and `buf` holds the whole call so far: O(n²) over the call. #403 / #482 fixed what is *emitted* while a call streams; this is the search cost, which they did not touch. **Fix.** After a miss, keep `_call_resume = max(0, len(buf) - len(CALL_END) + 1)` and search from there. That is sound because in call state `buf` only grows (the one temporary swap for `stream_tools` restores it whole). The offset is reset on entering call state. **Output:** the same events. `serve/test_tools.py` `CallEndRescan` feeds a call whose argument contains `</tool_cal`, `</tool_callx` and trailing partial fragments at piece sizes 1 / 2 / 3 / 5 / 11 / 64, and requires the whole-feed result each time; `test_search_resume_advances_with_the_body` errors on v1.1.35's `frontend.py` and passes with the fix. **Measured** (a 200K-char string argument in 20-char pieces through `OutputParser(stream_tools=True)`, the same 8 events): | CPU | before | after | |---|---|---| | Ryzen 7 9800X3D (the same change on our v1.1.29-based tree) | 229 ms (22.9 µs a piece) | 56 ms (5.6 µs a piece) | | Xeon E5-2697 v4 VM, v1.1.35 vs this branch, while an engine run used the CPU lane (best of 3, not a clean benchmark) | 872 ms | 492 ms | ### 2. TCP_NODELAY on streamed replies **Problem.** Streamed replies are small frames, often one token each. With Nagle on and the client's delayed ACK, a frame can wait for the ACK (classically ~40 ms) on a LAN hop, e.g. to a proxy in front of Maya. **Fix.** `Handler._sse()` (`serve/server.py:4276`, the one entry for every streaming path) sets `IPPROTO_TCP / TCP_NODELAY = 1`; an `OSError` is ignored. The bytes are identical; only the packetization differs. **Test:** `serve/test_server.py` `NoDelayStreaming` uses a real loopback socket and checks the option goes 0 -> 1 through `_sse()`; it fails on v1.1.35's `server.py`. **Not measured:** we did not timestamp frame arrivals with and without it; the ~40 ms is the textbook Nagle / delayed-ACK interaction, not our measurement. No new settings. **Checks** (as `.github/workflows/tests.yml`): `tools/check_hip_compat.py` OK; server tests 466 OK (12 skipped; v1.1.35 runs 463 - the new parser / NODELAY 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>
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#634
No description provided.