Skip to content

Commit 037ee39

Browse files
committed
Ignore tool calls emitted inside thinking
Refs #167. DSML tool calls are executable only after the assistant closes the thinking stanza. The live decoder now skips tool-marker detection while thinking is open, and the final parser searches for executable DSML only after the last </think> when thinking mode is active. This prevents a DSML block inside reasoning from being duplicated as both thinking text and a structured tool call. We were not able to prove from traces that the model actually made this exact mistake in the reported case, because the trace gives text rather than the original sampled-token/KV state. It is nevertheless possible, and it matches the reported shape of the bug. We did see evidence that the model can discuss tool usage and DSML syntax inside the thinking stanza, so this is a conservative protocol fix: it does not force the end of thinking, and it does not execute tool calls while still inside thinking. It remains unclear what the model would do if it truly mis-emits a tool call before </think>: it might recover by closing thinking later, or it might not. A follow-up commit should address the likely root source by changing sampler behavior itself instead of relying only on protocol filtering.
1 parent 011aa67 commit 037ee39

2 files changed

Lines changed: 158 additions & 52 deletions

File tree

‎ds4_server.c‎

Lines changed: 156 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -4246,6 +4246,17 @@ static const char *skip_ascii_ws(const char *p) {
42464246
return p;
42474247
}
42484248

4249+
static const char *find_last_substr(const char *s, const char *needle) {
4250+
if (!s || !needle || !needle[0]) return NULL;
4251+
const char *last = NULL;
4252+
const char *p = s;
4253+
while ((p = strstr(p, needle)) != NULL) {
4254+
last = p;
4255+
p++;
4256+
}
4257+
return last;
4258+
}
4259+
42494260
/* The prompt renderer escapes DSML text so a tool argument can safely contain
42504261
* shell operators or closing tags. The generated-DSML parser must undo exactly
42514262
* those entities before it turns parameters back into JSON; otherwise
@@ -4401,29 +4412,49 @@ static void split_reasoning_content(const char *text, size_t n, char **content_o
44014412
free(s);
44024413
}
44034414

4404-
static bool parse_generated_message(const char *text, char **content_out,
4405-
char **reasoning_out, tool_calls *calls) {
4406-
const char *start = strstr(text, "\n\n" DS4_TOOL_CALLS_START);
4415+
static bool parse_generated_message_ex(const char *text, bool require_thinking_closed,
4416+
char **content_out, char **reasoning_out,
4417+
tool_calls *calls) {
4418+
text = text ? text : "";
4419+
const char *tool_search = text;
4420+
4421+
/* When thinking mode is enabled the model is expected to close
4422+
* </think> before it enters the executable assistant surface. DSML inside
4423+
* reasoning is just model text: it may be a mistaken attempt, a quotation,
4424+
* or an explanation of the protocol. Treating it as a real tool call
4425+
* duplicates it into both reasoning and structured tool_calls, and can make
4426+
* clients execute something the assistant had not actually emitted as its
4427+
* post-thinking action. */
4428+
if (require_thinking_closed) {
4429+
const char *think_end = find_last_substr(text, "</think>");
4430+
if (!think_end) {
4431+
split_reasoning_content(text, strlen(text), content_out, reasoning_out);
4432+
return true;
4433+
}
4434+
tool_search = think_end + 8;
4435+
}
4436+
4437+
const char *start = strstr(tool_search, "\n\n" DS4_TOOL_CALLS_START);
44074438
int style = 0; /* 0: DSML, 1: plain XML, 2: DSML with the first vertical bar omitted. */
4408-
if (!start) start = strstr(text, DS4_TOOL_CALLS_START);
4439+
if (!start) start = strstr(tool_search, DS4_TOOL_CALLS_START);
44094440
if (!start) {
4410-
start = strstr(text, "\n\n" DS4_TOOL_CALLS_START_SHORT);
4441+
start = strstr(tool_search, "\n\n" DS4_TOOL_CALLS_START_SHORT);
44114442
style = start ? 2 : style;
44124443
}
44134444
if (!start) {
4414-
start = strstr(text, DS4_TOOL_CALLS_START_SHORT);
4445+
start = strstr(tool_search, DS4_TOOL_CALLS_START_SHORT);
44154446
style = start ? 2 : style;
44164447
}
44174448
if (!start) {
4418-
start = strstr(text, "\n\n<tool_calls>");
4449+
start = strstr(tool_search, "\n\n<tool_calls>");
44194450
style = start ? 1 : style;
44204451
}
44214452
if (!start) {
4422-
start = strstr(text, "<tool_calls>");
4453+
start = strstr(tool_search, "<tool_calls>");
44234454
style = start ? 1 : style;
44244455
}
44254456
if (!start) {
4426-
split_reasoning_content(text, text ? strlen(text) : 0, content_out, reasoning_out);
4457+
split_reasoning_content(text, strlen(text), content_out, reasoning_out);
44274458
return true;
44284459
}
44294460

@@ -4571,6 +4602,7 @@ static const char *tool_parse_failure_recovery_finish(const char *finish) {
45714602
static bool parse_generated_message_for_response(const char *text,
45724603
bool has_tools,
45734604
bool saw_tool_start,
4605+
bool require_thinking_closed,
45744606
const char **finish_io,
45754607
char *err,
45764608
size_t errlen,
@@ -4580,8 +4612,10 @@ static bool parse_generated_message_for_response(const char *text,
45804612
bool *recovered_out) {
45814613
if (recovered_out) *recovered_out = false;
45824614

4583-
bool parsed_ok = parse_generated_message(text ? text : "", content_out,
4584-
reasoning_out, calls);
4615+
bool parsed_ok = parse_generated_message_ex(text ? text : "",
4616+
require_thinking_closed,
4617+
content_out, reasoning_out,
4618+
calls);
45854619
if (parsed_ok) return true;
45864620

45874621
free(*content_out);
@@ -10829,6 +10863,9 @@ static void generate_job(server *s, job *j) {
1082910863
double last_decode_log_t = decode_t0;
1083010864
int last_decode_log_completion = 0;
1083110865
thinking_state thinking = thinking_state_from_prompt(&j->req);
10866+
const bool thinking_gates_tool_markers = ds4_think_mode_enabled(j->req.think_mode);
10867+
bool tool_scan_waiting_for_think_close =
10868+
thinking_gates_tool_markers && thinking.inside;
1083210869
dsml_decode_tracker dsml_tracker;
1083310870
dsml_decode_tracker_init(&dsml_tracker);
1083410871

@@ -10966,36 +11003,52 @@ static void generate_job(server *s, job *j) {
1096611003
free(piece);
1096711004

1096811005
if (j->req.kind == REQ_CHAT && j->req.has_tools) {
10969-
const char *tool_scan = text.ptr ? text.ptr + tool_scan_from : "";
10970-
bool orphan_end = false;
10971-
bool old_start = saw_tool_start;
10972-
bool old_end = saw_tool_end;
10973-
observe_tool_markers(tool_scan, &saw_tool_start, &saw_tool_end, &orphan_end);
10974-
if (orphan_end && !saw_orphan_tool_end) {
10975-
saw_orphan_tool_end = true;
10976-
server_log(DS4_LOG_WARNING,
10977-
"ds4-server: chat ctx=%s%s%s ignored orphan tool-call end marker after %d generated tokens",
10978-
ctx_span,
10979-
req_flags[0] ? " " : "",
10980-
req_flags,
10981-
completion);
10982-
trace_event(s, trace_id,
10983-
"ignored orphan tool-call end marker after %d generated tokens",
10984-
completion);
10985-
}
10986-
if (saw_tool_start && !old_start) {
10987-
trace_event(s, trace_id, "entered tool-call block after %d generated tokens", completion);
10988-
}
10989-
if (saw_tool_end && !old_end) {
10990-
trace_event(s, trace_id, "closed tool-call block after %d generated tokens", completion);
10991-
}
10992-
const size_t marker_hold = 80;
10993-
tool_scan_from = text.len > marker_hold ? text.len - marker_hold : 0;
10994-
if (s->trace && completion >= next_tool_progress) {
10995-
trace_event(s, trace_id,
10996-
"progress gen=%d dsml_start=%d dsml_end=%d",
10997-
completion, saw_tool_start ? 1 : 0, saw_tool_end ? 1 : 0);
10998-
next_tool_progress += 128;
11006+
if (thinking_gates_tool_markers && thinking.inside) {
11007+
/* A DSML block inside reasoning is not executable. This is
11008+
* the live guard: do not let a quoted or mistaken marker in
11009+
* <think> stop decoding as a real tool call. */
11010+
tool_scan_waiting_for_think_close = true;
11011+
tool_scan_from = text.len;
11012+
} else {
11013+
if (tool_scan_waiting_for_think_close) {
11014+
const char *think_end = find_last_substr(text.ptr, "</think>");
11015+
tool_scan_from = think_end ? (size_t)((think_end + 8) - text.ptr) : text.len;
11016+
if (tool_scan_from > text.len) tool_scan_from = text.len;
11017+
tool_scan_waiting_for_think_close = false;
11018+
}
11019+
if (tool_scan_from > text.len) tool_scan_from = text.len;
11020+
const char *tool_scan = text.ptr ? text.ptr + tool_scan_from : "";
11021+
bool orphan_end = false;
11022+
bool old_start = saw_tool_start;
11023+
bool old_end = saw_tool_end;
11024+
observe_tool_markers(tool_scan, &saw_tool_start, &saw_tool_end, &orphan_end);
11025+
if (orphan_end && !saw_orphan_tool_end) {
11026+
saw_orphan_tool_end = true;
11027+
server_log(DS4_LOG_WARNING,
11028+
"ds4-server: chat ctx=%s%s%s ignored orphan tool-call end marker after %d generated tokens",
11029+
ctx_span,
11030+
req_flags[0] ? " " : "",
11031+
req_flags,
11032+
completion);
11033+
trace_event(s, trace_id,
11034+
"ignored orphan tool-call end marker after %d generated tokens",
11035+
completion);
11036+
}
11037+
if (saw_tool_start && !old_start) {
11038+
trace_event(s, trace_id, "entered tool-call block after %d generated tokens", completion);
11039+
}
11040+
if (saw_tool_end && !old_end) {
11041+
trace_event(s, trace_id, "closed tool-call block after %d generated tokens", completion);
11042+
}
11043+
const size_t marker_hold = 80;
11044+
size_t hold_from = text.len > marker_hold ? text.len - marker_hold : 0;
11045+
if (hold_from > tool_scan_from) tool_scan_from = hold_from;
11046+
if (s->trace && completion >= next_tool_progress) {
11047+
trace_event(s, trace_id,
11048+
"progress gen=%d dsml_start=%d dsml_end=%d",
11049+
completion, saw_tool_start ? 1 : 0, saw_tool_end ? 1 : 0);
11050+
next_tool_progress += 128;
11051+
}
1099911052
}
1100011053
}
1100111054

@@ -11074,6 +11127,7 @@ static void generate_job(server *s, job *j) {
1107411127
text.ptr ? text.ptr : "",
1107511128
j->req.has_tools,
1107611129
saw_tool_start,
11130+
ds4_think_mode_enabled(j->req.think_mode),
1107711131
&final_finish,
1107811132
err,
1107911133
sizeof(err),
@@ -12569,8 +12623,8 @@ static void test_anthropic_tool_stream_sends_live_tool_use(void) {
1256912623
char *parsed_content = NULL;
1257012624
char *parsed_reasoning = NULL;
1257112625
tool_calls calls = {0};
12572-
TEST_ASSERT(parse_generated_message(raw_complete, &parsed_content,
12573-
&parsed_reasoning, &calls));
12626+
TEST_ASSERT(parse_generated_message_ex(raw_complete, false, &parsed_content,
12627+
&parsed_reasoning, &calls));
1257412628
TEST_ASSERT(calls.len == 1);
1257512629
apply_anthropic_stream_tool_ids(&calls, &st);
1257612630
TEST_ASSERT(calls.v[0].id != NULL);
@@ -12906,7 +12960,7 @@ static void test_openai_tool_stream_sends_partial_arguments(void) {
1290612960
char *parsed_content = NULL;
1290712961
char *parsed_reasoning = NULL;
1290812962
tool_calls calls = {0};
12909-
TEST_ASSERT(parse_generated_message(raw_complete, &parsed_content, &parsed_reasoning, &calls));
12963+
TEST_ASSERT(parse_generated_message_ex(raw_complete, false, &parsed_content, &parsed_reasoning, &calls));
1291012964
TEST_ASSERT(calls.len == 1);
1291112965
apply_openai_stream_tool_ids(&calls, &st);
1291212966
TEST_ASSERT(calls.v[0].id != NULL);
@@ -13452,7 +13506,7 @@ static void test_parse_short_dsml_and_canonical_suffix(void) {
1345213506
char *content = NULL;
1345313507
char *reasoning = NULL;
1345413508
tool_calls calls = {0};
13455-
TEST_ASSERT(parse_generated_message(generated, &content, &reasoning, &calls));
13509+
TEST_ASSERT(parse_generated_message_ex(generated, false, &content, &reasoning, &calls));
1345613510
TEST_ASSERT(reasoning && !strcmp(reasoning, "need a tool"));
1345713511
TEST_ASSERT(content && content[0] == '\0');
1345813512
TEST_ASSERT(calls.len == 1);
@@ -13492,7 +13546,7 @@ static void test_dsml_parser_recovers_loose_nested_parameters(void) {
1349213546
char *content = NULL;
1349313547
char *reasoning = NULL;
1349413548
tool_calls calls = {0};
13495-
TEST_ASSERT(parse_generated_message(generated, &content, &reasoning, &calls));
13549+
TEST_ASSERT(parse_generated_message_ex(generated, false, &content, &reasoning, &calls));
1349613550
TEST_ASSERT(content && !strcmp(content, "review done"));
1349713551
TEST_ASSERT(calls.len == 1);
1349813552
TEST_ASSERT(calls.v[0].name && !strcmp(calls.v[0].name, "edit"));
@@ -13523,6 +13577,7 @@ static void test_tool_parse_failure_returns_recoverable_finish(void) {
1352313577
TEST_ASSERT(!parse_generated_message_for_response(generated,
1352413578
true,
1352513579
true,
13580+
false,
1352613581
&finish,
1352713582
err,
1352813583
sizeof(err),
@@ -13542,6 +13597,55 @@ static void test_tool_parse_failure_returns_recoverable_finish(void) {
1354213597
tool_calls_free(&calls);
1354313598
}
1354413599

13600+
static void test_thinking_dsml_is_not_executable_before_think_close(void) {
13601+
const char *generated =
13602+
"<think>I might mention a malformed or tentative tool call here:\n\n"
13603+
DS4_TOOL_CALLS_START "\n"
13604+
DS4_INVOKE_START " name=\"bash\">\n"
13605+
DS4_PARAM_START " name=\"command\" string=\"true\">true" DS4_PARAM_END "\n"
13606+
DS4_INVOKE_END "\n"
13607+
DS4_TOOL_CALLS_END
13608+
"\nBut it is still reasoning, not an assistant action.</think>Final answer.";
13609+
13610+
char *content = NULL;
13611+
char *reasoning = NULL;
13612+
tool_calls calls = {0};
13613+
TEST_ASSERT(parse_generated_message_ex(generated, true,
13614+
&content, &reasoning, &calls));
13615+
TEST_ASSERT(calls.len == 0);
13616+
TEST_ASSERT(reasoning && strstr(reasoning, DS4_TOOL_CALLS_START) != NULL);
13617+
TEST_ASSERT(content && !strcmp(content, "Final answer."));
13618+
13619+
free(content);
13620+
free(reasoning);
13621+
tool_calls_free(&calls);
13622+
}
13623+
13624+
static void test_thinking_dsml_after_think_close_is_executable(void) {
13625+
const char *generated =
13626+
"<think>need a shell check</think>\n\n"
13627+
DS4_TOOL_CALLS_START "\n"
13628+
DS4_INVOKE_START " name=\"bash\">\n"
13629+
DS4_PARAM_START " name=\"command\" string=\"true\">pwd" DS4_PARAM_END "\n"
13630+
DS4_INVOKE_END "\n"
13631+
DS4_TOOL_CALLS_END;
13632+
13633+
char *content = NULL;
13634+
char *reasoning = NULL;
13635+
tool_calls calls = {0};
13636+
TEST_ASSERT(parse_generated_message_ex(generated, true,
13637+
&content, &reasoning, &calls));
13638+
TEST_ASSERT(calls.len == 1);
13639+
TEST_ASSERT(reasoning && !strcmp(reasoning, "need a shell check"));
13640+
TEST_ASSERT(content && content[0] == '\0');
13641+
TEST_ASSERT(calls.v[0].name && !strcmp(calls.v[0].name, "bash"));
13642+
TEST_ASSERT(strstr(calls.v[0].arguments, "\"command\": \"pwd\"") != NULL);
13643+
13644+
free(content);
13645+
free(reasoning);
13646+
tool_calls_free(&calls);
13647+
}
13648+
1354513649
static void test_tool_checkpoint_suffix_is_future_prompt_canonical(void) {
1354613650
tool_schema_orders orders = make_bash_order();
1354713651
const char *tool_schemas =
@@ -13567,7 +13671,7 @@ static void test_tool_checkpoint_suffix_is_future_prompt_canonical(void) {
1356713671
char *content = NULL;
1356813672
char *reasoning = NULL;
1356913673
tool_calls calls = {0};
13570-
TEST_ASSERT(parse_generated_message(generated, &content, &reasoning, &calls));
13674+
TEST_ASSERT(parse_generated_message_ex(generated, false, &content, &reasoning, &calls));
1357113675
TEST_ASSERT(calls.len == 1);
1357213676
TEST_ASSERT(strstr(calls.v[0].arguments, "cd /tmp && git diff 2>/dev/null") != NULL);
1357313677
TEST_ASSERT(strstr(calls.v[0].arguments, "&amp;&amp;") == NULL);
@@ -13646,7 +13750,7 @@ static void test_tool_checkpoint_minifies_json_parameters(void) {
1364613750
char *content = NULL;
1364713751
char *reasoning = NULL;
1364813752
tool_calls calls = {0};
13649-
TEST_ASSERT(parse_generated_message(generated, &content, &reasoning, &calls));
13753+
TEST_ASSERT(parse_generated_message_ex(generated, false, &content, &reasoning, &calls));
1365013754
TEST_ASSERT(calls.len == 1);
1365113755

1365213756
request r;
@@ -13703,7 +13807,7 @@ static void test_tool_memory_replays_sampled_dsml(void) {
1370313807
char *content = NULL;
1370413808
char *reasoning = NULL;
1370513809
tool_calls sampled = {0};
13706-
TEST_ASSERT(parse_generated_message(generated, &content, &reasoning, &sampled));
13810+
TEST_ASSERT(parse_generated_message_ex(generated, false, &content, &reasoning, &sampled));
1370713811
TEST_ASSERT(sampled.len == 1);
1370813812

1370913813
server s;
@@ -14313,7 +14417,7 @@ static void test_tool_separator_whitespace_is_not_content(void) {
1431314417
char *content = NULL;
1431414418
char *reasoning = NULL;
1431514419
tool_calls calls = {0};
14316-
TEST_ASSERT(parse_generated_message(generated, &content, &reasoning, &calls));
14420+
TEST_ASSERT(parse_generated_message_ex(generated, false, &content, &reasoning, &calls));
1431714421
TEST_ASSERT(reasoning && !strcmp(reasoning, "need a tool"));
1431814422
TEST_ASSERT(content && !strcmp(content, "I will inspect the files."));
1431914423
TEST_ASSERT(calls.len == 1);
@@ -15400,6 +15504,8 @@ static void ds4_server_unit_tests_run(void) {
1540015504
test_parse_short_dsml_and_canonical_suffix();
1540115505
test_dsml_parser_recovers_loose_nested_parameters();
1540215506
test_tool_parse_failure_returns_recoverable_finish();
15507+
test_thinking_dsml_is_not_executable_before_think_close();
15508+
test_thinking_dsml_after_think_close_is_executable();
1540315509
test_tool_checkpoint_suffix_is_future_prompt_canonical();
1540415510
test_tool_checkpoint_minifies_json_parameters();
1540515511
test_tool_memory_replays_sampled_dsml();

‎tests/ds4_test.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -606,8 +606,8 @@ static void test_tool_call_quality_one(bool quality) {
606606
char *content = NULL;
607607
char *reasoning = NULL;
608608
tool_calls calls = {0};
609-
bool parsed = parse_generated_message(text.ptr ? text.ptr : "",
610-
&content, &reasoning, &calls);
609+
bool parsed = parse_generated_message_ex(text.ptr ? text.ptr : "",
610+
false, &content, &reasoning, &calls);
611611
TEST_ASSERT(decode_ok);
612612
TEST_ASSERT(parsed);
613613
TEST_ASSERT(calls.len > 0);

0 commit comments

Comments
 (0)