Files
lemonade/test/cpp/test_launch_command.cpp
Jeremy FowersandClaude Opus 5.5 0c8169f5bf refactor(server): run backend servers through ServerProcess and NativeProcess (#3705)
* refactor(server): run backend servers through ServerProcess and NativeProcess

WrappedServer now owns one ServerProcess while a model is loaded, in place
of a raw process handle and a separately recorded launch command. Each
backend describes what to run in a ServerCommand and hands it to
start_server(), which starts the process, polls its ready endpoint at the
address the process reports, and starts the watchdog. Unload, the
watchdog and a failed or cancelled load all end the server through
stop_server() or ServerProcess::stop().

NativeProcess is the only ServerProcess so far and starts the binary
through ProcessManager exactly as each backend did before.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* refactor(server): tighten ServerProcess ownership and error paths

- ServerProcess stops its server on destruction; start_server stops a
  replaced process outside the lock
- OpenMOSS voice design runs through NativeProcess
- FLM and ORT skip troubleshooting output when a load was cancelled
- /metrics scrapes the backend at the host it reports

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* refactor(server): reuse URL helpers and trim leftovers from the ServerProcess move

- /metrics parses backend URLs with utils::parse_target_url; base URLs
  bracket IPv6 hosts
- OpenMOSS voice design connects to the host its process reports and calls
  stop_server() directly
- vLLM skips the CWSR hint when a load was cancelled
- Drop the redundant stop in start_server, unused process_manager.h
  includes, and comments that restate the code

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* refactor(server): give WrappedServer a default unload() and drop per-backend leftovers

unload() now defaults to stop_server(), and ~WrappedServer() stops the
server, so backends whose unload() and destructor only did that drop them.
The overrides that remain do real cleanup: vLLM's shim directory, OpenMoss's
request lock, Cloud's state and whisper.cpp's temp directory.

The ready log moves into wait_for_ready() with the server's base URL,
replacing the per-backend "Model loaded on port" and "Server is ready" lines.

RyzenAI reads its model path from model_info in load() instead of storing it
through set_model_path(), and drops is_loaded_ and a model_name_ that shadowed
the base member. Router destroys a server after unload(), so member resets in
unload() (Moonshine, TheNoise, vLLM, OpenMoss, FLM) are removed.

Comments in server_process.h, wrapped_server.cpp and test_launch_command.cpp
now state specific contracts instead of restating names.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* docs: describe starting backends through start_server() and the default unload()

adding-a-backend.md gains a "Starting the server process" section with a
ServerCommand/start_server() example and the per-backend settings, and says
unload() is optional. The OpenMOSS notes in backends-reference.md now name
start_server(), the local NativeProcess designer and server_command() in place
of the removed stop_speech_process() and spawn(). AGENTS.md drops unload()
from the methods every backend implements.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* docs: leave AGENTS.md unchanged

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
2026-10-08 20:21:38 +00:00

143 lines
5.4 KiB
C++

// Checks that WrappedServer::get_process_info(), which /health reports, takes the
// PID and launch command from the same ServerProcess: a start publishes both, a
// restart replaces the previous command, and a stop or failed start clears both.
// An integration test against /health cannot reach these states: a restart only
// happens on a watchdog reset, and Router::get_all_loaded_models() drops dead
// backends before it builds the response.
#include "lemon/wrapped_server.h"
#include <httplib.h>
#include <cstdio>
#include <memory>
#include <stdexcept>
#include <string>
#include <thread>
#include <vector>
namespace {
int failures = 0;
void check(const std::string& what, bool ok) {
std::printf("%s %s\n", ok ? "PASS" : "FAIL", what.c_str());
if (!ok) ++failures;
}
} // namespace
namespace lemon {
// A server that reports itself running under `pid` without spawning anything,
// so the test controls liveness and never signals a real process.
class FakeProcess : public ServerProcess {
public:
explicit FakeProcess(int pid) : ServerProcess(ProcessOutput{}), pid_(pid) {}
~FakeProcess() override { handle_ = {nullptr, 0}; }
std::string start(const ServerCommand& command) override {
command_line_ = {command.program};
command_line_.insert(command_line_.end(), command.args.begin(), command.args.end());
handle_.pid = pid_;
return "127.0.0.1";
}
void stop() override { handle_ = {nullptr, 0}; }
bool running() const override { return handle_.pid != 0; }
private:
int pid_;
};
// Minimal WrappedServer that runs whatever ServerProcess the test hands it.
// start_server() and stop_server() are protected, so the stub republishes them.
class StubWrappedServer : public WrappedServer {
public:
StubWrappedServer() : WrappedServer("stub", "error", nullptr, nullptr) {}
void load(const std::string&, const ModelInfo&, const RecipeOptions&, bool) override {}
using WrappedServer::start_server;
using WrappedServer::stop_server;
};
} // namespace lemon
int main() {
httplib::Server health;
health.Get("/health", [](const httplib::Request&, httplib::Response& res) {
res.set_content("{}", "application/json");
});
const int port = health.bind_to_any_port("127.0.0.1");
if (port <= 0) {
std::printf("FAIL could not bind the ready endpoint\n");
return 1;
}
std::thread health_thread([&health]() { health.listen_after_bind(); });
health.wait_until_ready();
lemon::ServerCommand command;
command.program = "llama-server.exe";
command.port = port;
{
lemon::StubWrappedServer server;
check("a server that never started reports no command",
server.get_process_info().launch_command.empty());
command.args = {"-m", "model.gguf", "--ctx-size", "8192"};
server.start_server(std::make_unique<lemon::FakeProcess>(101), command, 5);
const lemon::WrappedServer::ProcessInfo first = server.get_process_info();
check("executable lands at index 0",
!first.launch_command.empty() && first.launch_command[0] == "llama-server.exe");
check("arguments follow the executable in order",
first.launch_command == std::vector<std::string>({"llama-server.exe", "-m",
"model.gguf", "--ctx-size",
"8192"}));
check("the PID is the started process's", first.pid == 101);
check("the standalone accessors agree with the snapshot",
server.get_launch_command() == first.launch_command &&
server.get_process_id() == first.pid && server.get_backend_port() == port);
// What a watchdog reset does: same object, second process.
server.stop_server();
command.args = {"-m", "model.gguf", "--port", "8082"};
server.start_server(std::make_unique<lemon::FakeProcess>(202), command, 5);
const lemon::WrappedServer::ProcessInfo second = server.get_process_info();
check("a restart replaces the previous command instead of appending",
second.launch_command == std::vector<std::string>({"llama-server.exe", "-m",
"model.gguf", "--port",
"8082"}));
check("a restart reports the new PID", second.pid == 202);
server.stop_server();
const lemon::WrappedServer::ProcessInfo stopped = server.get_process_info();
check("stopping erases pid and command in the same snapshot",
stopped.pid == 0 && stopped.launch_command.empty());
bool threw = false;
try {
server.start_server(std::make_unique<lemon::FakeProcess>(0), command, 5);
} catch (const std::runtime_error&) {
threw = true;
}
const lemon::WrappedServer::ProcessInfo failed = server.get_process_info();
check("a server that exits before it is ready fails the start", threw);
check("a failed start leaves no pid or command behind",
failed.pid == 0 && failed.launch_command.empty());
}
health.stop();
health_thread.join();
if (failures == 0) {
std::printf("\nAll launch command checks passed.\n");
return 0;
}
std::printf("\n%d launch command check(s) failed.\n", failures);
return 1;
}