Preserve assistant commentary and tool calls in one Qwen turn

This commit is contained in:
Pavlo Ratushnyi
2026-10-04 03:00:26 +02:00
parent a60ebe58de
commit a3770eda2c
4 changed files with 100 additions and 2 deletions
@@ -0,0 +1,40 @@
{
"provenance": "Pinned engine/strata/serve/chat_template.jinja rendered with Jinja2 3.1.6; sanitized fixture",
"request": {
"model": "strata-local",
"reasoning": {
"effort": "none"
},
"input": [
{
"role": "user",
"content": "Weather in Oslo?"
},
{
"type": "reasoning",
"content": [
{
"type": "reasoning_text",
"text": "Use the lookup."
}
]
},
{
"role": "assistant",
"content": "Checking. \n"
},
{
"type": "function_call",
"call_id": "call_fixture",
"name": "lookup",
"arguments": "{\"city\":\"Oslo\"}"
},
{
"type": "function_call_output",
"call_id": "call_fixture",
"output": "3°C"
}
]
},
"rendered": "<|im_start|>user\nWeather in Oslo?<|im_end|>\n<|im_start|>assistant\n<think>\nUse the lookup.\n</think>\n\nChecking.\n\n<tool_call>\n<function=lookup>\n<parameter=city>\nOslo\n</parameter>\n</function>\n</tool_call><|im_end|>\n<|im_start|>user\n<tool_response>\n3°C\n</tool_response><|im_end|>\n<|im_start|>assistant\n<think>\n\n</think>\n\n"
}
+36 -2
View File
@@ -885,7 +885,20 @@ fn render_request_prompt(request: &CreateRequest) -> Result<String, String> {
{
owned.push((Role::Assistant, String::new(), Some(thought)));
}
owned.push((role, content, reasoning.take()));
if role == Role::Assistant
&& let Some((Role::Assistant, previous, thought)) = owned.last_mut()
{
if !previous.is_empty() && !content.is_empty() {
previous.push('\n');
}
previous.push_str(&content);
if let Some(next) = reasoning.take() {
let current = thought.get_or_insert_with(String::new);
current.push_str(&next);
}
} else {
owned.push((role, content, reasoning.take()));
}
}
"function_call" => {
if !outputs.is_empty() {
@@ -985,7 +998,18 @@ fn render_request_prompt(request: &CreateRequest) -> Result<String, String> {
.map(|(_, value)| format!("<tool_response>\n{value}\n</tool_response>"))
.collect::<Vec<_>>()
.join("\n");
owned.push((Role::Assistant, call_text, reasoning.take()));
if let Some((Role::Assistant, content, thought)) = owned.last_mut() {
content.truncate(content.trim_end().len());
if !content.is_empty() {
content.push_str("\n\n");
}
content.push_str(&call_text);
if let Some(next) = reasoning.take() {
thought.get_or_insert_with(String::new).push_str(&next);
}
} else {
owned.push((Role::Assistant, call_text, reasoning.take()));
}
owned.push((Role::User, output_text, None));
calls.clear();
outputs.clear();
@@ -2205,6 +2229,16 @@ mod tests {
fs::remove_dir_all(root).unwrap();
}
#[test]
fn commentary_reasoning_and_tools_match_pinned_template_as_one_assistant_turn() {
let golden: Value = serde_json::from_str(include_str!(
"../../benchmarks/fixtures/responses/commentary-tool-prompt.json"
))
.unwrap();
let request: CreateRequest = serde_json::from_value(golden["request"].clone()).unwrap();
assert_eq!(render_request_prompt(&request).unwrap(), golden["rendered"]);
}
#[test]
fn console_late_approval_instructions_retain_authority_and_content() {
let request = make_request(json!({"instructions":"Initial instructions", "input":[
+6
View File
@@ -163,3 +163,9 @@ Approval/review requests can append developer messages after user input. The
native adapter stably coalesces these instruction messages into the leading
Qwen system section, preserving their content and order. It does not demote
approval instructions to user messages or bypass the console's review step.
Stateless replay keeps adjacent assistant commentary and function calls in one
assistant turn, with their reasoning content attached. The commentary-to-call
separator matches the pinned Qwen template, including trimming commentary's
trailing whitespace. The sanitized `commentary-tool-prompt.json` fixture checks
this against an independently rendered upstream template, byte for byte.
+18
View File
@@ -445,3 +445,21 @@ soak still remain; hosted Actions has not run.
namespaced item identity, tool text-part replay, and late approval
instructions. A fresh install into a temporary launcher copy matches the
installed integration byte-for-byte; a second install is idempotent.
- The fresh console run after those fixes had no transport/protocol errors and
produced a game that passed 23/23 independent browser checks (all eight win
lines, locked terminal boards, restart, draw, occupied-cell handling, mobile
clicking/layout, and no page errors). However, the agent stopped while
preparing its own browser verification; its MCP task TEST-9 remained
incomplete. Exit code zero and `turn.completed` are not sufficient evidence
of task completion. No assistance or continuation prompt was supplied.
- That incomplete run took 452.02 s. The console reported 21,622 output tokens,
including 9,225 reasoning tokens. The engine recorded 24 requests including
approval reviews, 23,180 generated tokens and weighted decode 102.26 tok/s
(per-request median 103.75; p10–p90 70.0–129.75). These are diagnostic numbers
from an incomplete task, not a paired throughput improvement.
- Further prompt inspection found that commentary and a subsequent tool call
from the same response were replayed as separate assistant turns. They now
remain one turn, with reasoning retained and the pinned Qwen template's exact
spacing. A sanitized golden fixture was generated using the pinned local
template with Jinja2 3.1.6; native rendering matches it byte-for-byte. A fresh
independent run is needed to qualify this revision.