Files
UnbiasedStrata/frontend/engine_backpressure_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

72 lines
2.6 KiB
C++

#include "engine.hpp"
#include <cassert>
#include <condition_variable>
#include <deque>
#include <stdexcept>
#include <thread>
// Enough output to fill a Linux pipe, with the same independent stdin reader
// and per-token STOP check as the backend. This requires no model or CUDA.
int fake_backend() {
std::setvbuf(stdout, nullptr, _IONBF, 0);
std::mutex mutex;
std::condition_variable ready;
std::deque<std::string> commands;
std::atomic<bool> stop{false};
auto reader = std::thread([&] {
std::string input;
char byte;
while (::read(0, &byte, 1) == 1) {
if (byte != '\n') { input += byte; continue; }
if (input == "STOP") { stop.store(true); input.clear(); continue; }
const bool quit = input == "QUIT";
{ std::lock_guard<std::mutex> lock(mutex); commands.push_back(input); }
input.clear(); ready.notify_one();
if (quit) return;
}
});
std::puts("READY");
for (;;) {
std::unique_lock<std::mutex> lock(mutex);
ready.wait(lock, [&] { return !commands.empty(); });
auto command = commands.front(); commands.pop_front(); lock.unlock();
if (command == "QUIT") break;
stop.store(false);
const int token = command.find("99") != std::string::npos ? 99 : 11;
int count = 0;
while (count < 100000) {
std::printf("T %d\n", token); ++count;
if (stop.load()) break;
}
std::printf("DONE %d 1 1 1 stop 0 0\n", count);
}
reader.join();
return 0;
}
int main(int argc, char** argv) {
if (argc > 1) return fake_backend();
strata_frontend::Engine engine;
std::string error;
assert(engine.start({argv[0], "--fake-backend"}, error, argv[0]));
assert(engine.wait_ready(error));
auto failed = engine.generate("GEN 100000 ", {11}, [](int32_t) -> bool {
throw std::runtime_error("partial tool JSON formatter failure");
});
assert(!failed.ok);
int count = 0;
auto next = engine.generate("GEN 100000 ", {99}, [&](int32_t token) {
assert(token == 99); ++count; return true;
});
assert(next.ok && count == 100000);
auto cancel = engine.generate("GEN 100000 ", {11}, [](int32_t) { return false; });
assert(cancel.ok);
count = 0;
next = engine.generate("GEN 100000 ", {99}, [&](int32_t token) {
assert(token == 99); ++count; return true;
});
assert(next.ok && count == 100000);
engine.quit();
std::puts("PASS: large responses cannot fill an abandoned pipe or leak into the next request");
}