Files
UnbiasedStrata/frontend/tests/engine_test.cpp
T
Robert KarlandClaude Fable 5.1 4c25aa6f6a Front end: llama.cpp's server files with the Strata engine behind them (design C), with fake-engine tests
frontend/strata_context.cpp replaces tools/server/server-context.cpp. The other seven server files
(server-http, -queue, -task, -common, -chat, -schema, -stream) compile unmodified from the submodule into
the static library strata_llama_server; frontend/ui_stub/ supplies the empty web-UI asset table that
server-http.cpp references. The context runs one slot: the engine is a child process spoken to over its
line protocol (docs/BACKENDS.md), a vocab-only llama_model provides tokenizing, the chat template and the
reasoning and tool-call parsers, and the server's own task and result types carry the OpenAI JSON, SSE
framing and client-disconnect cancel. Routes kept: /health, /v1/models, /v1/chat/completions, /tokenize,
/detokenize. The strata engine's 8-token reserve is kept out of max_new. A supervisor thread ends the
process with exit code 1 when the engine dies, for a unit with Restart=on-failure.

frontend/engine.hpp is the child adapter: fork+exec, READY/INFO, GEN/GENI, T/PP/RESUME/DONE/ERR/FATAL,
STOP with drain to DONE, non-blocking reap. quit() and the supervisor's reap_exit() share the pid through
an atomic that quit() takes over by exchange, and only a positive local copy ever reaches kill(). The
first draft kept a plain pid_t and the supervisor set it to -1 on reaping, so a SIGTERM during shutdown
raced into kill(-1, SIGKILL), which killed every process of the user; it did so twice on 2026-10-17.

Build: STRATA_FRONTEND (default on) gates the llama.cpp libraries and the front end, not CUDA, so they
build on a machine without a GPU. Makefile: unbiased-strata joins `make build`; `make frontend` builds it
alone without CUDA into build-frontend/; `make test` builds engine_test there and runs it and
frontend/tests/e2e.sh against a fake engine (frontend/tests/fake-engine.py). The nvcc probes run only
when there is an nvcc and only the build and configure goals demand one. UPSTREAM.md records all of it.

Verified on macOS arm64 without CUDA: `make frontend` and `make test` pass. engine_test, 7 cases: READY,
a request, STOP mid-request with drain and a clean next request, a 100k-token burst the caller abandons,
ERR leaves the adapter usable, the engine dying mid-request is diagnosed, a DONE with the wrong prompt
size is visible. e2e.sh, 12 checks: /health, /v1/models, a non-streaming chat with content, finish and
usage, a stream whose deltas join to the content and end with finish_reason and [DONE], a client that
disconnects mid-stream (the server logs the cancel and the next request completes), an oversize prompt
refused with 400, a tokenize/detokenize round trip. Zero new patch lines to llama.cpp, so the step-4
gate of PLAN.md section 5 passes and design C stands. Not yet run against a real engine.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-17 21:31:05 -07:00

163 lines
6.1 KiB
C++

// UnbiasedStrata: the engine adapter against the fake engine (frontend/tests/fake-engine.py). No GPU, no model.
//
// c++ -std=c++20 -I frontend frontend/tests/engine_test.cpp -o engine_test && ./engine_test frontend/tests/fake-engine.py
//
// Cases, each the shape of a bug UnbiasedStrata 1 met:
// 1. READY parsed: context and the stop capability
// 2. a request: tokens come back, DONE carries the counts, the adapter is idle afterwards
// 3. cancel: STOP mid-request, the rest is drained to DONE finish=cancel, the next request is clean
// 4. backpressure: a 100k-token burst the caller abandons does not fill the pipe for ever (drain to DONE)
// 5. ERR: a refused request leaves the adapter usable
// 6. the engine dies mid-request: EOF is reported, reap_exit names the exit code
// 7. a DONE whose prompt size does not match is visible to the caller (the context refuses such a reply)
#include "engine.hpp"
#include <cstdio>
#include <cstdlib>
#include <string>
#include <vector>
using strata_frontend::Engine;
using strata_frontend::EngineLine;
static int failures = 0;
#define CHECK(cond) do { if (!(cond)) { std::fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); ++failures; } } while (0)
struct Run {
std::vector<int32_t> tokens;
EngineLine done;
bool got_done = false, got_err = false, got_eof = false;
std::string err;
};
// read until DONE/ERR/EOF; `cancel_after` tokens -> send STOP; `abandon_after` tokens -> stop reading tokens into
// the vector (the caller is "gone") but keep draining
static Run run(Engine & e, int cancel_after = -1, int timeout_ms = 5000) {
Run r;
EngineLine line;
const int64_t deadline = Engine::now_ms() + timeout_ms;
while (Engine::now_ms() < deadline) {
if (!e.next_line(line, 50)) continue;
switch (line.kind) {
case EngineLine::TOKEN:
r.tokens.push_back(line.token);
if (cancel_after >= 0 && (int) r.tokens.size() == cancel_after) e.stop();
break;
case EngineLine::DONE: r.done = line; r.got_done = true; return r;
case EngineLine::ERR: r.err = line.message; r.got_err = true; return r;
case EngineLine::EOF_: r.got_eof = true; return r;
default: break;
}
}
std::fprintf(stderr, "FAIL: timed out waiting for DONE\n");
++failures;
return r;
}
static bool start(Engine & e, const std::string & script, const std::vector<std::pair<std::string, std::string>> & env) {
for (auto & kv : env) ::setenv(kv.first.c_str(), kv.second.c_str(), 1);
std::string err;
const bool ok = e.start(script, {script}, err) && e.wait_ready(err, 10);
for (auto & kv : env) ::unsetenv(kv.first.c_str());
if (!ok) std::fprintf(stderr, "start: %s\n", err.c_str());
return ok;
}
int main(int argc, char ** argv) {
if (argc < 2) { std::fprintf(stderr, "usage: engine_test <fake-engine.py>\n"); return 2; }
const std::string script = argv[1];
// 1 + 2: READY, one request echoing the prompt
{
Engine e;
CHECK(start(e, script, {{"FAKE_CONTEXT", "2048"}}));
CHECK(e.ready());
CHECK(e.max_context() == 2048);
CHECK(e.can_stop());
CHECK(e.info().count("engine") && e.info().at("engine") == "fake");
CHECK(e.send_request("GEN 10 temperature=0.6", {5, 6, 7}));
CHECK(e.busy());
Run r = run(e);
CHECK(r.got_done);
CHECK((r.tokens == std::vector<int32_t>{5, 6, 7}));
CHECK(r.done.produced == 3 && r.done.prompt_n == 3 && r.done.finish == "stop");
CHECK(!e.busy());
// max_new caps it
CHECK(e.send_request("GEN 2", {1, 2, 3, 4}));
r = run(e);
CHECK(r.got_done && r.tokens.size() == 2 && r.done.finish == "length");
e.quit();
CHECK(!e.alive());
}
// 3: cancel mid-request, then a clean next request
{
Engine e;
CHECK(start(e, script, {{"FAKE_N_TOKENS", "1000"}, {"FAKE_DELAY_MS", "2"}}));
CHECK(e.send_request("GEN 1000", {1}));
Run r = run(e, /* cancel_after */ 5);
CHECK(r.got_done);
CHECK(r.done.finish == "cancel");
CHECK(r.tokens.size() < 1000);
CHECK(e.send_request("GEN 3", {9}));
r = run(e);
CHECK(r.got_done && r.done.finish == "length" && r.tokens.size() == 3);
e.quit();
}
// 4: a burst the caller abandons; the pipe would hold ~64 KB, the burst is ~700 KB
{
Engine e;
CHECK(start(e, script, {{"FAKE_N_TOKENS", "100000"}}));
CHECK(e.send_request("GEN 100000", {1}));
Run r = run(e, /* cancel_after */ 1, /* timeout */ 30000);
CHECK(r.got_done);
// FAKE_N_TOKENS still applies, so the reply is one token of id 100 cut by max_new; what matters is
// that nothing of the abandoned burst leaks into it
CHECK(e.send_request("GEN 1", {2}));
r = run(e);
CHECK(r.got_done && r.tokens.size() == 1 && r.done.finish == "length");
e.quit();
}
// 5: ERR leaves the adapter usable
{
Engine e;
CHECK(start(e, script, {{"FAKE_ERR", "prompt too long"}}));
CHECK(e.send_request("GEN 1", {1}));
Run r = run(e);
CHECK(r.got_err && r.err == "prompt too long");
CHECK(!e.busy());
CHECK(e.alive());
e.quit();
}
// 6: the engine dies mid-request
{
Engine e;
CHECK(start(e, script, {{"FAKE_N_TOKENS", "50"}, {"FAKE_DIE_AFTER", "3"}}));
CHECK(e.send_request("GEN 50", {1}));
Run r = run(e);
CHECK(r.got_eof);
CHECK(r.tokens.size() == 3);
std::string why;
for (int i = 0; i < 50 && !e.reap_exit(why); ++i) ::usleep(10 * 1000);
CHECK(why == "engine exited with code 3");
CHECK(!e.alive());
}
// 7: prompt size mismatch is visible
{
Engine e;
CHECK(start(e, script, {{"FAKE_PROMPT_N", "99"}}));
CHECK(e.send_request("GEN 5", {1, 2}));
Run r = run(e);
CHECK(r.got_done && r.done.prompt_n == 99);
e.quit();
}
if (failures) { std::fprintf(stderr, "%d failure(s)\n", failures); return 1; }
std::printf("engine_test: 7 cases passed\n");
return 0;
}