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>
226 lines
7.4 KiB
C++
226 lines
7.4 KiB
C++
// UnbiasedStrata: the front end's program. Mirrors llama.cpp's tools/server/server.cpp without router mode, MCP,
|
|
// server-side tools, the web UI and model downloads.
|
|
//
|
|
// unbiased-strata --engine build/bin/strata-poly -m model.gguf [llama-server options] -- <engine options>
|
|
//
|
|
// Options before `--` are llama-server's (common_params_parse): -m/--model gives the GGUF the vocabulary and chat
|
|
// template are read from, --host/--port/--api-key/--threads-http the listener, --temp/--top-k/... the sampling
|
|
// defaults, --reasoning-format/--chat-template-kwargs/--jinja the chat handling. Options after `--` go to the engine
|
|
// as they are; `--serve` is added. The engine's READY line gives the context size.
|
|
|
|
#include "strata_context.hpp"
|
|
|
|
#include "arg.h"
|
|
#include "common.h"
|
|
#include "llama.h"
|
|
#include "log.h"
|
|
#include "server-common.h"
|
|
#include "server-stream.h"
|
|
|
|
#include <atomic>
|
|
#include <clocale>
|
|
#include <csignal>
|
|
#include <cstdio>
|
|
#include <cstring>
|
|
#include <string>
|
|
#include <thread>
|
|
#include <vector>
|
|
|
|
#include <unistd.h>
|
|
|
|
namespace {
|
|
|
|
std::function<void(int)> shutdown_handler;
|
|
std::atomic_flag is_terminating = ATOMIC_FLAG_INIT;
|
|
|
|
void signal_handler(int signal) {
|
|
if (is_terminating.test_and_set()) {
|
|
std::fprintf(stderr, "Received second interrupt, terminating immediately.\n");
|
|
std::exit(1);
|
|
}
|
|
if (shutdown_handler) {
|
|
shutdown_handler(signal);
|
|
}
|
|
}
|
|
|
|
void usage() {
|
|
std::fprintf(stderr,
|
|
"usage: unbiased-strata --engine <strata|strata-poly|path> -m <model.gguf> [llama-server options] -- <engine options>\n"
|
|
"\n"
|
|
" --engine PATH the engine binary; a bare name is looked up beside this program\n"
|
|
" -m, --model FILE the GGUF the vocabulary and chat template are read from (no tensors are loaded)\n"
|
|
" --host, --port, --api-key, --threads-http, --temp, --top-k, --top-p, --min-p, --reasoning-format,\n"
|
|
" --chat-template-kwargs, --alias ... llama-server options (see llama-server --help)\n"
|
|
" -- everything after it is passed to the engine; --serve is added\n"
|
|
"\n"
|
|
"example (Qwen3.6-35B-A3B from its GGUF):\n"
|
|
" unbiased-strata --engine strata-poly -m Qwen3.6-35B-A3B-UD-Q3_K_M.gguf --port 8080 \\\n"
|
|
" -- --model Qwen3.6-35B-A3B-UD-Q3_K_M.gguf --max-context 32768\n");
|
|
}
|
|
|
|
std::string self_dir() {
|
|
char buf[4096];
|
|
const ssize_t n = ::readlink("/proc/self/exe", buf, sizeof buf - 1);
|
|
if (n <= 0) {
|
|
return ".";
|
|
}
|
|
buf[n] = 0;
|
|
std::string p(buf);
|
|
const size_t slash = p.find_last_of('/');
|
|
return slash == std::string::npos ? "." : p.substr(0, slash);
|
|
}
|
|
|
|
bool file_exists(const std::string & p) {
|
|
return ::access(p.c_str(), X_OK) == 0;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
int main(int argc, char ** argv) {
|
|
std::setlocale(LC_NUMERIC, "C");
|
|
#ifndef _WIN32
|
|
signal(SIGPIPE, SIG_IGN);
|
|
#endif
|
|
|
|
// split: ours (--engine), llama-server's, and after `--` the engine's
|
|
std::string engine_exe;
|
|
std::vector<std::string> engine_args;
|
|
std::vector<char *> llama_argv;
|
|
llama_argv.push_back(argv[0]);
|
|
for (int i = 1; i < argc; ++i) {
|
|
const std::string a = argv[i];
|
|
if (a == "--") {
|
|
for (++i; i < argc; ++i) {
|
|
engine_args.push_back(argv[i]);
|
|
}
|
|
break;
|
|
}
|
|
if (a == "--engine") {
|
|
if (i + 1 >= argc) { usage(); return 2; }
|
|
engine_exe = argv[++i];
|
|
continue;
|
|
}
|
|
if (a.rfind("--engine=", 0) == 0) {
|
|
engine_exe = a.substr(9);
|
|
continue;
|
|
}
|
|
llama_argv.push_back(argv[i]);
|
|
}
|
|
if (engine_exe.empty()) {
|
|
usage();
|
|
return 2;
|
|
}
|
|
if (engine_exe.find('/') == std::string::npos) {
|
|
const std::string beside = self_dir() + "/" + engine_exe;
|
|
if (file_exists(beside)) {
|
|
engine_exe = beside;
|
|
}
|
|
}
|
|
if (!file_exists(engine_exe)) {
|
|
std::fprintf(stderr, "unbiased-strata: engine not found or not executable: %s\n", engine_exe.c_str());
|
|
return 2;
|
|
}
|
|
|
|
common_params params;
|
|
common_init();
|
|
server_stream_session_manager_start();
|
|
|
|
if (!common_params_parse((int) llama_argv.size(), llama_argv.data(), params, LLAMA_EXAMPLE_SERVER)) {
|
|
return 1;
|
|
}
|
|
if (params.model.path.empty()) {
|
|
std::fprintf(stderr, "unbiased-strata: -m <model.gguf> is required (the vocabulary and chat template come from it)\n");
|
|
return 2;
|
|
}
|
|
if (!params.mmproj.path.empty()) {
|
|
std::fprintf(stderr, "unbiased-strata: --mmproj (images) is not in this build yet; see PLAN.md step 8\n");
|
|
return 2;
|
|
}
|
|
auto model_name = params.model.get_name();
|
|
if (params.model_alias.empty() && !model_name.empty()) {
|
|
params.model_alias.insert(model_name);
|
|
}
|
|
|
|
llama_backend_init();
|
|
llama_numa_init(params.numa);
|
|
|
|
strata_context ctx_server;
|
|
server_http_context ctx_http;
|
|
if (!ctx_http.init(params)) {
|
|
SRV_ERR("%s", "failed to initialize HTTP server\n");
|
|
return 1;
|
|
}
|
|
|
|
// routes first (handlers are not called until is_ready), then the engine (forked before any thread exists),
|
|
// then the HTTP thread, which answers /health with 503 while the engine loads
|
|
ctx_server.init_routes(ctx_http);
|
|
|
|
std::string err;
|
|
if (!ctx_server.start_engine(params, engine_exe, engine_args, err)) {
|
|
SRV_ERR("could not start the engine: %s\n", err.c_str());
|
|
return 1;
|
|
}
|
|
|
|
auto clean_up = [&]() {
|
|
SRV_INF("%s", "cleaning up before exit...\n");
|
|
server_stream_session_manager_stop();
|
|
ctx_http.stop();
|
|
ctx_server.terminate();
|
|
llama_backend_free();
|
|
};
|
|
|
|
if (!ctx_http.start()) {
|
|
clean_up();
|
|
SRV_ERR("%s", "exiting due to HTTP server error\n");
|
|
return 1;
|
|
}
|
|
|
|
if (!ctx_server.load(params, err)) {
|
|
SRV_ERR("%s\n", err.c_str());
|
|
clean_up();
|
|
if (ctx_http.thread.joinable()) {
|
|
ctx_http.thread.join();
|
|
}
|
|
return 1;
|
|
}
|
|
ctx_http.is_ready.store(true);
|
|
SRV_INF("listening on %s, context %lld tokens\n", ctx_http.listening_address.c_str(), ctx_server.n_ctx());
|
|
|
|
shutdown_handler = [&](int) {
|
|
ctx_server.terminate();
|
|
};
|
|
struct sigaction sigint_action;
|
|
sigint_action.sa_handler = signal_handler;
|
|
sigemptyset(&sigint_action.sa_mask);
|
|
sigint_action.sa_flags = 0;
|
|
sigaction(SIGINT, &sigint_action, NULL);
|
|
sigaction(SIGTERM, &sigint_action, NULL);
|
|
|
|
// the engine's exit ends this process too, with a non-zero code, so a unit with Restart=on-failure brings both
|
|
// back. A replacement engine is not forked from this multithreaded process (UnbiasedStrata 1 main.cpp:873).
|
|
std::atomic<bool> engine_died{false};
|
|
std::atomic<bool> stop_supervisor{false};
|
|
std::thread supervisor([&]() {
|
|
std::string why;
|
|
while (!stop_supervisor) {
|
|
if (ctx_server.engine_exited(why)) {
|
|
SRV_ERR("%s; shutting down so the service manager can restart\n", why.c_str());
|
|
engine_died = true;
|
|
ctx_server.terminate();
|
|
return;
|
|
}
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
|
}
|
|
});
|
|
|
|
ctx_server.start_loop(); // blocks until terminate()
|
|
|
|
stop_supervisor = true;
|
|
supervisor.join();
|
|
clean_up();
|
|
if (ctx_http.thread.joinable()) {
|
|
ctx_http.thread.join();
|
|
}
|
|
return engine_died ? 1 : 0;
|
|
}
|