mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-03 19:03:30 +07:00
[PR #4686] [Other] make remaining streamed tool argument parsing linear #5136
Labels
No labels
needs-format
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Niko1221/Strata#5136
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/Niko1221/Strata/pull/4686
Author: @InB4DevOps
Created: 10/30/2026
Status: 🔄 Open
Base:
main← Head:perf/python-stream-tool-buffer📝 Commits (1)
a235f9a[Other] make streamed tool argument parsing linear📊 Changes
3 files changed (+117 additions, -9 deletions)
View changed files
📝
serve/frontend.py(+61 -8)📝
serve/test_native_tool_parser.py(+53 -0)📝
serve/test_parser_call_scan.py(+3 -1)📄 Description
Problem and result
A streamed tool call with a large string argument still keeps its whole body in
OutputParser.buf. Every small model delta appends to that growing Python string even after_scan()has emitted the text to the client. The work therefore grows quadratically with the argument size.On this DGX Spark, with four-character deltas pinned to one Cortex-X925 core, the median parser time changed as follows:
mainThe patched times grow approximately in proportion to input size. The emitted event counts and final parsed arguments matched in every measured run. These are synthetic Python parser timings, not end-to-end inference throughput.
Related discussion
This is a focused follow-up to #1557, incorporated as
fc27296d. That change avoids structural walks before a possible closing tag arrives, but its description explicitly leaves repeated tool-body string copying and linear scaling unresolved.The broader branch in #1017 also described a
_CallBoundaryparser rewrite. That branch closed after repository history was force-pushed and its parser rewrite is not in currentmain. This PR keeps the current parser and addresses only its remaining growing-buffer cost.No new issue or scope agreement: this is a focused performance fix with no new option or default change.
Approach
Move already-scanned call text out of the active string in chunks. Small incoming deltas then append to a bounded
bufinstead of copying the whole argument. Restore the chunks only when a structural walk or the final call parse needs the complete body.Closing-tag searches remember how much text they checked, including recovery mode and
</function>detection, so a literal closing tag in a string value does not cause a full walk on every later delta.cleancounts compacted chunks as held text. Final parsing, recovery behavior, streamed JSON, call IDs, and public events remain unchanged.The change is limited to the Python server parser and its regression test.
Validation
Baseline:
870aedd9e1ebe1c91f94da93b59af17729f6c5c0(mainat the start of this work). Candidate:d6a3b39f.Environment: NVIDIA DGX Spark, Linux aarch64, Ubuntu 24.04.5, kernel
7.0.0-1019-nvidia, Strata's CPython 3.12.15. The primary benchmark was pinned withtaskset -c 5to a Cortex-X925 performance core (maximum 3.9 GHz). Each table entry is the median of five runs; baseline and candidate order alternated. The harness validated the final argument and event count after every run.Passed:
/home/admin/.local/share/strata/terminal/0.1.54/python/bin/python3.12 -m unittest discover -s serve -p 'test_*.py'— 1,257 tests passed; 38 skipped; 373.942 s./home/admin/.local/share/strata/terminal/0.1.54/python/bin/python3.12 /tmp/opencode/check_stream_tool_parser_parity.py— 120 randomized large streamed calls matched the baseline after every feed and atfinish(). Inputs included literal</parameter>,</function>, and</tool_call>strings, incomplete calls, random chunk widths, and both recovery settings.taskset -c 5 /home/admin/.local/share/strata/terminal/0.1.54/python/bin/python3.12 /tmp/opencode/bench_stream_tool_parser_dgx_spark.py— five alternating samples at 100/200/400/800 KB produced the table above; all final arguments and event counts matched.git diff --check origin/main...HEAD— no errors.Failed:
python3could not importregexin HTTP/server tests (ModuleNotFoundError). There were no parser assertion failures. Re-running the targeted tests and then the completeservesuite with Strata's bundled Python passed as reported above.Not run:
Limits
The measurable benefit is for streamed tool calls with large string-valued arguments, such as generated file contents. Normal replies, small calls, non-streamed calls, model inference, network transfer, and the tool's own execution are not made faster.
The complete call is still retained and materialized once for final validation, so retained memory remains linear in argument size. Inputs containing many literal structural closing tags can still require additional structural walks.
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.