chat: a tool-call delimiter is not a turn terminator

A converted checkpoint can name a token end-of-generation that the chat format
itself writes as one of its call delimiters -- Qwen3.8-Flash-Next's
`</tool_call>` is a single added token, and a container that lists it in
generation_config's eos list stops every tool-calling turn the moment the call
closes. The damage is worse than a late stop: a second call is never written,
finish_reason says "stop" on a tool-calling turn, and a reply that quotes the
tag as that token is cut mid-sentence (seen in a coding session where the model
wrote about the template and the turn ended between two tokens).

The bridge already derives the format, so it knows what the model writes as a
call boundary. It now reports the end-of-generation ids whose spelling is one
of those markers, leaves them out of the reply parser's terminators, and the
engine demotes them in the decoder's stop predicate with a warning. The
template's real terminator (<|im_end|>) is untouched, and a model without
tools, or a container that names nothing extra, is unchanged.
This commit is contained in:
Victor Ionescu authored and StillDeadcode committed 2026-11-16 18:03:16 +01:00
1 parent b988378e70
commit 38c182a2ba
1 file changed
+33
+33
View File
@@ -1479,6 +1479,39 @@ TEST(the_chat_bridge_derives_the_whole_request_from_one_format) {
}
/* The declared format preserves HYV3's outer section and all of its argument markers. */
/* A container that names the tool-call close end-of-generation cannot serve a second call, or
* a reply that quotes the tag: the turn ends the moment the call does. The bridge finds the ids
* this format writes as its own delimiters and the engine drops them from the stop set, leaving
* the real turn terminator alone. */
TEST(a_call_delimiter_is_not_a_turn_terminator) {
VocabBuild vb;
vb.tokens = { "a", "<|im_end|>", "<tool_call>", "</tool_call>" };
vb.types = { RAD_TT_NORMAL, RAD_TT_CONTROL, RAD_TT_CONTROL, RAD_TT_CONTROL };
vb.eos = 1;
vb.eos_ids = { 3 }; /* the close, declared terminal by the container */
vb.chat_template = fixture("glm53");
auto v = std::make_shared<Vocab>();
REQUIRE(v->load(std::move(vb)) >= 0);
REQUIRE(v->is_eog(3));
server::ChatTemplateBridge br;
REQUIRE(br.init(*v) >= 0);
CHECK(br.supports_tools());
REQUIRE(br.eog_call_markers().size() == 1);
CHECK_EQ(br.eog_call_markers()[0], 3);
/* The terminator the template writes stays one; the engine demotes only what the bridge
* reports (core/engine.cpp). */
CHECK(v->is_eog(1));
/* A template with no tool calls has no call delimiters to report. */
std::shared_ptr<Vocab> plain_v = vocab_with("");
REQUIRE(plain_v != nullptr);
server::ChatTemplateBridge plain;
REQUIRE(plain.init(*plain_v) >= 0);
CHECK(!plain.supports_tools());
CHECK(plain.eog_call_markers().empty());
}
TEST(hyv3_plugin_format_runs_through_the_chat_template_bridge) {
std::shared_ptr<Vocab> v = vocab_with(fixture("hyv3"));
REQUIRE(v != nullptr);