Files
UnbiasedStrata/frontend/chat_parser_test.cpp
T
Robert KarlandClaude Fable 5.1 46c46ecfc1 Front end: image input through the model's vision encoder; engine child supervision; tests
Images: --mmproj PATH loads the Qwen3.8 Flash-Next projector through llama.cpp's mtmd
library and encodes image_url parts on the CPU (frontend/vision.hpp). The embeddings go to
the engine as a GENI request, its existing vision path, with the prompt's <|image_pad|>
runs standing in for each image. --vision-tokens caps the tokens per image. GET /v1/models
lists "image" under input_modalities when the encoder is loaded. Checked on an RTX 4090:
screenshots, photos, several images in one message, and pi's file attachments and
clipboard paste.

Engine process: the engine is started and watched from frontend/engine.hpp; when it exits
or its stall watchdog fires, the front end exits too, for a supervisor to restart. Stall
reports add the current stage and thread wait channels. Reasoning streams; content and
tool calls are sent once the whole message is parsed, so partial JSON never changes
arguments already sent.

Tests: make test-frontend (child exit, protocol, backpressure; no model needed) and
make test-chat-parser MODEL=<gguf> (template and tool-call parsing against the real GGUF).

The image path and the supervision were written by a Codex session; this commit finishes,
documents and checks them.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-08 15:54:53 -07:00

73 lines
3.9 KiB
C++

// Exercise request preparation and final tool parsing with the actual GGUF.
// No inference engine or CUDA context is created.
#define main unused_serving_main
#include "main.cpp"
#undef main
#include <cassert>
#include <sstream>
int strata_engine_main(int, char**) { return 99; }
int main(int argc, char** argv) {
if (argc != 2) return 2;
llama_log_set(quiet_log, nullptr);
llama_backend_init();
Server server;
server.opt.ctx = 196608;
auto params = llama_model_default_params(); params.vocab_only = true;
server.model = llama_model_load_from_file(argv[1], params);
assert(server.model);
server.vocab = llama_model_get_vocab(server.model);
server.tmpls = common_chat_templates_init(server.model, "");
assert(common_chat_templates_get_caps(server.tmpls.get()).at("supports_parallel_tool_calls"));
auto request = common_json::parse_no_throw(R"({
"messages":[{"role":"user","content":"Check the files and the date."}],
"tools":[{"type":"function","function":{"name":"bash","parameters":{
"type":"object","properties":{"command":{"type":"string"}},"required":["command"]
}}}]
})");
const std::string output = "Reasoning.\n</think>\n\n"
"<tool_call>\n<function=bash>\n<parameter=command>\nls\n</parameter>\n</function>\n</tool_call>\n"
"<tool_call>\n<function=bash>\n<parameter=command>\ndate\n</parameter>\n</function>\n</tool_call>";
Prepared default_request, explicit_request;
std::string error;
assert(server.prepare(request, default_request, error));
auto message = common_chat_parse(output, false, default_request.parser);
assert(message.tool_calls.size() == 2);
assert(message.tool_calls[0].arguments == "{\"command\":\"ls\"}");
assert(message.tool_calls[1].arguments == "{\"command\":\"date\"}");
request["parallel_tool_calls"] = true;
assert(server.prepare(request, explicit_request, error));
assert(explicit_request.ids == default_request.ids);
assert(common_chat_parse(output, false, explicit_request.parser).tool_calls.size() == 2);
request["parallel_tool_calls"] = false;
assert(server.prepare(request, explicit_request, error));
assert(explicit_request.ids == default_request.ids);
assert(common_chat_parse(output, false, explicit_request.parser).tool_calls.size() == 1);
// Arguments in any order: the model writes optional parameters before required ones, and upstream's
// required-first rule dropped such calls without an error (patches/llama.cpp-qwen3-coder-any-argument-order.patch).
auto read_request = common_json::parse_no_throw(R"({
"messages":[{"role":"user","content":"Read the file."}],
"tools":[{"type":"function","function":{"name":"read","parameters":{
"type":"object","required":["path"],"properties":{"path":{"type":"string"},
"offset":{"type":"number"},"limit":{"type":"number"}}
}}}]
})");
Prepared read_prepared;
assert(server.prepare(read_request, read_prepared, error));
for (const char* order : {"limit offset path", "offset path limit", "path limit offset", "path"}) {
std::string text = "Reasoning.\n</think>\n\n<tool_call>\n<function=read>\n";
std::istringstream names(order);
for (std::string name; names >> name;)
text += "<parameter=" + name + ">\n" + (name == "path" ? "/tmp/a.txt" : "40") + "\n</parameter>\n";
text += "</function>\n</tool_call>";
auto parsed = common_chat_parse(text, false, read_prepared.parser);
assert(parsed.tool_calls.size() == 1);
assert(parsed.tool_calls[0].name == "read");
assert(parsed.tool_calls[0].arguments.find("\"path\":\"/tmp/a.txt\"") != std::string::npos);
}
llama_model_free(server.model);
std::puts("PASS: omitted parallel_tool_calls uses the model capability; explicit false remains honored; "
"tool arguments parse in any order");
}