Repository navigation
feat(daemon): start GAIA's Lemonade Server on demand - #4193
Conversation
A new user should install with npx, optionally run gaia init, and start GAIA from the TUI, web UI, desktop app or Claude Code. Scopes GAIA's embedded Lemonade replacing a system-wide install, start on demand from every entry point, the removal of the system-Lemonade code, and the follow-on PRs for npx install, TUI /init and the Claude Code plugin.
gaia opens the terminal UI, gaia web the web app and gaia app the desktop app; gaia chat --ui and the top-level --ui/--cli flags are removed as legacy. Adds this as its own PR in the sequence.
Records the decisions: the GAIA daemon starts and stops embedded Lemonade, GAIA never falls back to another Lemonade on the default port and reports the Lemonade app when it holds memory or the NPU, and a second PR removes gaia chat and every command that is neither useful nor exercised by CI, collapsing init profiles to gaia, npu and sd.
gaia init used to install Lemonade system-wide (MSI, PPA or .pkg, with an admin prompt on macOS), upgrade or uninstall existing installs, and try to start whatever it found. It now downloads GAIA's own Lemonade Server into ~/.gaia/lemonade (checksum-verified, no admin rights) and starts it on a private port. Re-running init restarts a stopped server, replaces an older one, and recovers one that stopped answering. LEMONADE_BASE_URL still names another server; init then only checks that it answers and meets the profile's minimum version. --remote now requires it. gaia init --check reports the embedded server's real state and targets it explicitly, so it can no longer call a profile ready because some other Lemonade answered on the default port. Removes --skip-lemonade (nothing used it), init's system install, upgrade and auto-start paths, and catch-all handlers in the steps touched.
5b9a02c to
9d9e245
Compare
Request changesThe daemon now owns GAIA's Lemonade Server — starting it at daemon startup and on demand, and stopping only a server it started itself. The ownership rules are careful and well tested; what needs another pass is the timeouts, which don't line up with the new, slower work. Two things to fix:
Neither is hard to fix, and the rest of the design is solid. Real-world evidenceNo 🔍 Technical details🟡 Important1. Daemon shutdown can now outrun the stop command's grace window (
On escalation the daemon is killed before Simplest fix is to give the stop command room for the work the daemon now does: Alternatively, bound the teardown inside 2.
Whatever number you pick, it should be derived from — and stay above — the Python-side ceiling; a comment naming that relationship would keep the two from drifting apart again. 🟢 Minor3. Stale comment on the ensure timeout ( 4. Misleading log when the server this daemon started has died ( 5. Strengths
|
…aged gaia init --check could call a user-chosen Lemonade Server ready that gaia init then refused as too old, so the TUI skipped setup and failed later. Both now share one minimum-version rule, and a server that reports no readable version is logged rather than silently passed. A shell that sourced GAIA's own credentials file set LEMONADE_BASE_URL to GAIA's server, which turned off init's management of it and kept a dead port and key after a restart. The file now also exports GAIA_LEMONADE_EMBEDDED=1, and GAIA follows its live state instead of those values. Also: --check reports a failure to list profile models instead of a traceback, the Linux troubleshooting entry about the Lemonade PPA is replaced with the embedded download's failure modes, and stale references to removed init helpers are fixed.
After a reboot, GAIA's own Lemonade Server was stopped and every entry point reported "Lemonade not running" until the user re-ran gaia init. The daemon now starts it when it comes up and whenever a GAIA command finds it stopped: LemonadeManager.ensure_ready() and gaia init --check ask the daemon through a new POST /daemon/v1/lemonade/ensure (host API 1.2). The daemon stops only a server it started itself, so one started by gaia init, by hand, or by another daemon keeps running. It never kills a server that is slow to answer (it may be loading a model) or replaces one running a different version; it reports those and points at gaia init. With LEMONADE_BASE_URL set it leaves Lemonade alone. It never installs anything.
9d9e245 to
6904ba4
Compare
…aits `gaia daemon stop` waited 10 s, but the daemon's shutdown now also stops the Lemonade Server it started (up to 20 s) and could wait up to 60 s behind a start still in flight, so a normal stop printed "did not exit; terminating" and killed the daemon before its custody store closed. Shutdown now waits at most 5 s for an in-flight start and leaves that server running, and the stop command waits 45 s. The TUI's setup check allowed 90 s while the Python side could spend over two minutes starting the daemon and the server after a reboot, so a successful start read as "could not be checked". The ensure timeout drops to 80 s (it never stops a stalled server) and the TUI allows 150 s, with a comment tying the two together. Also: a server this daemon started that has since exited is forgotten instead of logged as "pid None" on every stop, and --check's help and docs say it may start the daemon.
Unit tests block the network, so on a developer machine with GAIA set up the real ~/.gaia Lemonade Server looked stopped, and every test that called LemonadeManager.ensure_ready() asked a real daemon to start it: tests failed on start-lock timeouts and left daemons running. CI never saw it because nothing is installed there. An autouse fixture now stubs the start; tests of the start itself opt back in with @pytest.mark.embedded_start.
…ade-onboarding # Conflicts: # tests/unit/test_init_command.py
Windows CI failed to reach Lemonade on a port GAIA's own server had used: test_sd's `gaia init` now starts that server on a shared self-hosted runner, the job ended without stopping it, and its state file stayed in the runner's home. Every later client on that runner, in any job, followed the record to a dead port instead of the system Lemonade the job had started. The resolver now ignores a record whose process no longer exists (a standard-library check, cheap enough for every lookup). In CI, the scripts that start the system Lemonade name it through LEMONADE_BASE_URL for the rest of the job, and test_sd stops GAIA's server when it ends.
…-owns-embedded-lemonade
…sion Each version unpacks into its own directory, so the old binary on disk does not count as installed and init downloads the new one before starting it. The existing test mocked the install check and could not show that.
…-owns-embedded-lemonade
…r test A client launched by an earlier test keeps a fake server handle, so when the collector finalizes it mid-test its __del__ runs the port cleanup against that test's patched listener list and trips the "kills nothing" assertion. Collect before each test and keep the fake-launched clients alive.
…-owns-embedded-lemonade
On Windows the system temp dir lives under the user's home, so the "outside" target was inside home, nothing escaped, and the 403 never fired. Use a private home with the outside dir beside it.
…-owns-embedded-lemonade
Same Windows trap as the home-escape test: tmp_path sits inside the user's profile, so the outside dir was readable on home's account and nothing escaped.
…-owns-embedded-lemonade
The message formats the path with %r, which doubles every backslash on Windows, so a substring check against sys.executable could never match there.
…-owns-embedded-lemonade
Each version unpacks into its own dist dir, so is_installed() is false for a new version even with the old binary on disk.
…-owns-embedded-lemonade
A Windows checkout converted install.sh to CRLF, and dash then rejected `case ... in\r` as a syntax error, failing the Windows parse test and breaking the script for anyone running it from such a checkout.
…-owns-embedded-lemonade
On Windows the models and Hugging Face caches resolve from LOCALAPPDATA/HF_HOME before the home dir, so the purge-models test got the runner's real cache path instead of the fake home's. Clear both in the fixture.
…-owns-embedded-lemonade
…-owns-embedded-lemonade
…-owns-embedded-lemonade
The expected substrings use "/" but Windows prints "\", so the dry-run test could never pass there.
…-owns-embedded-lemonade
On a first run, callers that start at the same moment (Agent UI boot plus the first chat, or several sidecars) each saw an idle Lemonade server and sent their own `/load` — up to two parallel multi-GB downloads and two cold loads of the same model. Now the first caller loads the model and the rest wait for it, then pick up the result; if that load fails, each waiter retries and reports its own error instead of hanging. The existing concurrency test couldn't catch this because its mock returned "loaded" on the second status read; it now blocks in `load_model` and keeps the server idle until the load finishes, so it fails on `main` (2 loads) and passes here. <details> <summary>🔍 Technical details</summary> - `LemonadeManager._preload_in_flight` (a `threading.Event`) is set before `_try_preload_with_ctx` releases the lock and cleared in its `finally` after re-acquiring. `ensure_ready` waits on it, with the lock released, at the top of its locked block. - Overlap with open PRs: amd#4176 renames `DEFAULT_MODEL_NAME` → `model` inside `_try_preload_with_ctx` and amd#4193 adds a daemon start call after the "Initializing Lemonade" log in `ensure_ready`. Neither changes the lock handling, so they shouldn't conflict beyond possible adjacent-line rebases. </details> ## Test plan - [x] `pytest tests/unit/test_lemonade_manager_preload.py -k concurrent`: fails on `origin/main` (`load_model` called 2 times), passes with the fix (5/5 repeated runs) - [x] `pytest tests/unit/test_lemonade_manager_preload.py`: 23 passed, including the new `test_failed_preload_releases_waiting_callers` - [x] Neighbouring `LemonadeManager` suites (`test_chat_preflight`, `test_hardware_selection`, `test_llm`, `test_multi_device_wiring`, `test_npu_device_support`, `test_base_agent_lemonade_api_key`, `test_init_verify_ctx_display`, `chat/ui/test_server`, `chat/ui/test_chat_helpers`): 333 passed - [x] black / isort / pylint clean on the changed files Fixes amd#4212 --------- Co-authored-by: Kalin Ovtcharov <[email protected]> Co-authored-by: Kalin Ovtcharov <[email protected]>
amd#4309) **Stacked on amd#4193 (which is stacked on amd#4192).** Until those merge, this diff includes their commits; review only the last commit here. On a PC set up by `gaia init`, GAIA's own Lemonade Server runs on a private port, but about 20 places still looked for Lemonade on the old default port and found nothing. The Agent UI's status and onboarding screens, the OpenAI-compatible API server's health check, the builder agent and the email agent all reported the model server as missing. Now every lookup goes through one resolver: `LEMONADE_BASE_URL` if set, otherwise GAIA's own running server, otherwise the old default. With no GAIA server recorded, nothing changes. The email agent had a second problem on the same path: its readiness check called GAIA's server without the API key, got a 401, and reported the server's version as unknown. It now sends the key, as does the API server's health check. ## Test plan - [x] `pytest tests/unit` (14,472 passed; the one failure needs a live transcription server) plus the email and flagship agents' own tests with those packages on the path - [x] New tests: the Agent UI's system and onboarding lookups follow GAIA's recorded server, and a configured URL still wins. The API server and email agent health probes send that server's key. The flagship's health report names the URL the client would really have used. - [x] With only GAIA's own Lemonade running (scratch `GAIA_HOME`, no `LEMONADE_BASE_URL`): `gaia api start`, then `curl localhost:8611/health` reports the model backend ready at GAIA's server (output below) - [ ] On a PC with only GAIA's own Lemonade (no system install): the Agent UI's system status shows the model server as running, and the email agent's `GET /v1/email/init` reports the server's version <details> <summary>🔍 Technical details</summary> - Removing the fallback to the default port, which the self-hosted CI runners still rely on, is a separate change. - Several email-agent messages still say `lemonade-server serve`, and two tests assert them. They are left for a repo-wide sweep of that command's mentions. - Two tests that assumed the default port (flagship `test_stdio`, email `test_model_select`) now pin an empty `GAIA_HOME`, so they no longer depend on whether the developer's machine has GAIA set up. </details> <details> <summary>API server health with only GAIA's own Lemonade running (Apple Silicon)</summary> ``` $ gaia lemonade embedded status Running: yes on http://localhost:52461/api/v1 (pid 9670) $ gaia api start --port 8611 & $ curl -s localhost:8611/health {"status":"ok","service":"gaia-api","components":{"api":{"status":"ready"},"llm":{"status":"ready","backend":"lemonade","model":"Gemma-4-E4B-it-GGUF","url":"http://localhost:52461/api/v1"},"rag":{"status":"not_configured"}}} HTTP 200 ``` </details> --------- Co-authored-by: Kalin Ovtcharov <[email protected]> Co-authored-by: Kalin Ovtcharov <[email protected]>
Stacked on #4192. Land after #4301, which stops unit tests from spawning real daemons. With this PR a daemon starts GAIA's Lemonade Server in the real
~/.gaia, so a test-spawned one would start and stop the developer's server. Until that merges, this diff includes its commit; review only the last commit here.After a reboot, GAIA's own Lemonade Server is stopped, and every entry point said "Lemonade not running" until you re-ran
gaia init. Now the daemon starts it when the daemon comes up, and again whenever a GAIA command finds it stopped. Agents, the TUI's first-launch check (gaia init --check) and the Agent UI just work after a restart.The daemon is careful about what it stops.
gaia daemon stopstops the server only if that daemon started it, so one started bygaia init, by hand, or by another daemon (a second one used for testing, say) keeps running. It never kills a server that is slow to answer (it may be loading a large model) and never replaces one running a different version: it reports both and points atgaia init. WithLEMONADE_BASE_URLset it leaves Lemonade alone, and it never installs anything.A daemon from before this change doesn't have the new endpoint. Instead of a bare 404, clients get "restart it with
gaia daemon restart" (host API bumped to 1.2).Test plan
pytest tests/unit/test_daemon_lemonade.py tests/unit/test_daemon_clock_wiring.py tests/unit/test_init_command.py tests/unit/test_daemon_agents_routes.py;go test ./internal/gaiainit/ ./internal/daemon/GAIA_HOME,GAIA_DAEMON_HOMEandGAIA_CONFIG_DIRin a scratch dir and the server stopped:gaia init --checkhas a new daemon start it and prints READY (1.4 s);gaia daemon stopthen stops itgaia lemonade embedded start, thengaia daemon start/gaia daemon stop: the server keeps runningLemonadeManager.ensure_ready()gets it started through the daemon, loads Gemma and returns Truegaia-tuiwithout runninggaia init; it should go straight to chatTerminal output: the reboot case on Apple Silicon (GAIA set up, no daemon or server running)