Skip to content

Preserve exact tool-call text during prompt replay#151

Open
howard0su wants to merge 3 commits into
Luce-Org:mainfrom
howard0su:rax
Open

Preserve exact tool-call text during prompt replay#151
howard0su wants to merge 3 commits into
Luce-Org:mainfrom
howard0su:rax

Conversation

@howard0su
Copy link
Copy Markdown
Contributor

The OpenAI-compatible dflash server was parsing assistant tool-call output into structured JSON and then rebuilding those turns through the chat template on later requests. That preserved the semantics of the tool call, but not the exact text the model originally emitted. Small formatting differences in the rebuilt assistant turn can change tokenization, which makes prefix and KV reuse less stable for tool-using conversations.

Fix this by keeping a small Python-side tool-memory store in the server path and using it during prompt reconstruction. The server now remembers the original assistant text for generated tool-call turns, keyed by tool-call IDs. When a later request sends those same tool calls back as structured history, the prompt tokenizer looks up the IDs and re-injects the original assistant text verbatim instead of re-rendering canonicalized tool-call objects. If a lookup is missing or inconsistent, the existing structured rendering path still applies.

This change stays intentionally simple and Python-first. It adds a focused ToolMemory helper plus regression coverage for chat-completions and responses round trips, without introducing a native radix-tree backend before profiling shows the server-side store is a real bottleneck.

The OpenAI-compatible dflash server was parsing assistant tool-call output into structured JSON and then rebuilding those turns through the chat template on later requests. That preserved the semantics of the tool call, but not the exact text the model originally emitted. Small formatting differences in the rebuilt assistant turn can change tokenization, which makes prefix and KV reuse less stable for tool-using conversations.

Fix this by keeping a small Python-side tool-memory store in the server path and using it during prompt reconstruction. The server now remembers the original assistant text for generated tool-call turns, keyed by tool-call IDs. When a later request sends those same tool calls back as structured history, the prompt tokenizer looks up the IDs and re-injects the original assistant text verbatim instead of re-rendering canonicalized tool-call objects. If a lookup is missing or inconsistent, the existing structured rendering path still applies.

This change stays intentionally simple and Python-first. It adds a focused ToolMemory helper plus regression coverage for chat-completions and responses round trips, without introducing a native radix-tree backend before profiling shows the server-side store is a real bottleneck.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copy link
Copy Markdown

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 issues found across 4 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="dflash/scripts/server.py">

<violation number="1" location="dflash/scripts/server.py:675">
P1: Startup crash: `prefill_cache_bytes` is undefined here, and `PrefixCache.init_full_cache()` does not accept `budget_bytes`, so enabling prefill will raise before the server starts.</violation>

<violation number="2" location="dflash/scripts/server.py:1255">
P1: Streaming tool-call replay can lose preceding reasoning text, so the remembered raw assistant turn is not exact.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

Comment thread dflash/scripts/server.py Outdated
Comment thread dflash/scripts/server.py Outdated
@howard0su howard0su marked this pull request as draft May 11, 2026 11:57
howard0su and others added 2 commits May 11, 2026 23:35
Preserve exact streamed assistant text for tool-call turns by tracking raw decoded output in the streaming state machines. This keeps preceding reasoning text in remembered tool-call replay and avoids reconstruction loss.

Add a regression test covering streamed reasoning before a tool call and verify replay uses the exact original raw assistant turn.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@howard0su howard0su marked this pull request as ready for review May 11, 2026 15:38
Copy link
Copy Markdown

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 4 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="dflash/scripts/server.py">

<violation number="1" location="dflash/scripts/server.py:676">
P2: Process-wide ToolMemory can replay raw assistant text from unrelated requests when tool-call IDs are reused, causing cross-request data mixing/information disclosure.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

Comment thread dflash/scripts/server.py
@@ -41,6 +41,7 @@
compress_text_via_daemon, _drain_until_sentinel,
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: Process-wide ToolMemory can replay raw assistant text from unrelated requests when tool-call IDs are reused, causing cross-request data mixing/information disclosure.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At dflash/scripts/server.py, line 676:

<comment>Process-wide ToolMemory can replay raw assistant text from unrelated requests when tool-call IDs are reused, causing cross-request data mixing/information disclosure.</comment>

<file context>
@@ -672,6 +673,21 @@ def _resolve_kv_k_type():
     )
     if prefill_cfg is not None and prefill_cache_slots > 0:
         prefix_cache.init_full_cache(prefill_cache_slots)
+    tool_memory = ToolMemory(
+        max_entries=int(os.environ.get("DFLASH_TOOL_MEMORY_MAX_ENTRIES", "50000")),
+        max_bytes=int(os.environ.get("DFLASH_TOOL_MEMORY_MAX_BYTES", str(64 * 1024 * 1024))),
</file context>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant