// 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] -- // // 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 #include #include #include #include #include #include #include #include namespace { std::function 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 -m [llama-server 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 engine_args; std::vector 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 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 engine_died{false}; std::atomic 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; }