Repository navigation
fix: preserve streamed tool call arguments #3762
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
diegosouzapw
merged 7 commits into
cheaper-inference:release/v3.8.24
from
Mffff4:fix/tool-call-integrity
Jun 13, 2026
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ec78fe3
fix(publish): clean opencode-plugin node_modules after tsup build to …
diegosouzapw 420d62b
deps: bump esbuild from 0.28.0 to 0.28.1 (#3746)
dependabot[bot] 0843c8a
fix: preserve streamed tool call arguments
d009d90
docs: translate TOOL_CALL_INTEGRITY.md to English
Mffff4 ce2a0da
fix: address code review comments (jq filter and O(N) overlap detection)
Mffff4 d80d5ab
Merge remote-tracking branch 'origin/release/v3.8.24' into pr-3762
diegosouzapw af01b4c
fix(sse): make tool-call argument dedup snapshot-only to stop silent …
Mffff4 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| # Tool Call Integrity Fix | ||
|
|
||
| ## Проблема | ||
|
|
||
| В OmniRoute v3.8.21 при streaming tool calls аргументы функций могли | ||
| искажаться в клиенте дублированием или повторной вставкой фрагментов, например | ||
| `find` превращался в `fifnd`, а `grep` — в `grreep`. Симптом проявлялся только | ||
| на machine JSON полях tool calls (`function.arguments` / `partial_json`) и был | ||
| независим от провайдера, потому что повреждение происходило в общем | ||
| OmniRoute SSE/translation pipeline после upstream-ответа. | ||
|
|
||
| ## Корневая причина | ||
|
|
||
| Tool-call argument chunks обрабатывались как обычный человекочитаемый текст в | ||
| нескольких общих слоях: | ||
|
|
||
| - `src/lib/sseTextTransform.ts` рекурсивно передавал строковые поля | ||
| `arguments` и `partial_json` в текстовый processor. | ||
| - `src/lib/streamingPiiTransform.ts` буферизовал эти поля через rolling-window | ||
| PII sanitizer. Для machine JSON deltas это недопустимо: chunk может быть | ||
| дельтой, snapshot или overlap-fragment, а sanitizer не знает семантику | ||
| tool-call JSON. | ||
| - `open-sse/transformer/responsesTransformer.ts`, | ||
| `open-sse/translator/response/openai-to-claude.ts`, | ||
| `open-sse/translator/response/openai-responses.ts` и | ||
| `open-sse/handlers/sseParser.ts` накапливали аргументы простым `+=`, поэтому | ||
| повторный snapshot или overlapping delta добавлялся второй раз. | ||
|
|
||
| Обычный чат не ломался, потому что текстовые `content` deltas допускают | ||
| санитизацию и буферизацию. Tool calls ломались, потому что `arguments` — это | ||
| машинный JSON-контракт, который должен проходить byte-preserving до клиента. | ||
|
|
||
| ## Исправление | ||
|
|
||
| В core source добавлена явная защита tool-call JSON: | ||
|
|
||
| 1. `src/lib/sseTextTransform.ts` пропускает `toolArgs` и `partialJson` без | ||
| текстового processor. | ||
| 2. `src/lib/streamingPiiTransform.ts` возвращает `toolArgs` и `partialJson` | ||
| как есть, без rolling-window buffering. | ||
| 3. Общие stream assemblers используют `appendToolCallArgumentDelta()` вместо | ||
| слепого `+=`, чтобы повторные snapshots и overlapping chunks добавлялись | ||
| ровно один раз. | ||
| 4. Responses/OpenAI/Claude translation paths эмитят клиенту только новый suffix | ||
| tool arguments, а не повторный snapshot. | ||
|
|
||
| ## Как предотвратить регрессию | ||
|
|
||
| - `tool_calls.function.name`, `tool_calls.function.arguments`, Responses | ||
| `function_call.arguments` и Claude `input_json_delta.partial_json` никогда не | ||
| должны проходить через text/PII/compression/dedup transforms. | ||
| - Любой transform для SSE должен различать human text (`content`, `reasoning`) | ||
| и machine JSON (`arguments`, `partial_json`). | ||
| - Regression tests находятся в: | ||
| - `tests/unit/sseTextTransform.test.ts` | ||
| - `tests/unit/streamingPiiTransform.test.ts` | ||
| - `tests/unit/sse-parser.test.ts` | ||
| - `tests/unit/responses-transformer.test.ts` | ||
| - `tests/unit/translator-resp-openai-responses.test.ts` | ||
| - E2E smoke script: `tests/e2e-tool-calls.sh`. | ||
|
|
||
| ## Конфигурация | ||
|
|
||
| Для coding-сессий можно дополнительно отключить risky text transforms: | ||
|
|
||
| ```env | ||
| PII_RESPONSE_SANITIZATION=false | ||
| COMPRESSION_LEVEL=off | ||
| RTK_ENABLED=false | ||
| CAVEMAN_ENABLED=false | ||
| ``` | ||
|
|
||
| Основной фикс не зависит от этих env-переменных: machine tool-call JSON | ||
| защищён в core pipeline и не должен изменяться даже при включённой PII | ||
| response sanitization. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| export function appendToolCallArgumentDelta(current: unknown, incoming: unknown): string { | ||
| const existing = typeof current === "string" ? current : ""; | ||
| const next = typeof incoming === "string" ? incoming : ""; | ||
|
|
||
| if (!existing || !next) return existing + next; | ||
| if (existing.endsWith(next)) return existing; | ||
| if (next.startsWith(existing)) return next; | ||
|
|
||
| const maxOverlap = Math.min(existing.length, next.length); | ||
| for (let size = maxOverlap; size > 0; size -= 1) { | ||
| if (existing.endsWith(next.slice(0, size))) { | ||
| return existing + next.slice(size); | ||
| } | ||
| } | ||
|
|
||
| return existing + next; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.