Repository navigation
Description
Problem
LocalAgentService is built per-database but wired into the gRPC router — and treated by the desktop client — as a single daemon-global instance. Every registered database's engine, live-token stream, and in-flight turns become unreachable except on whichever database happened to be the boot-time default.
build_database_services (packages/daemon/src/services/assembly.rs:223-228), the per-database open path also used by lazy-open and idle-reopen (see #2043), constructs a brand-new LocalAgentServiceImpl for every database it opens — its own ChatInferenceEngine, its own token_tx broadcast channel, its own turn_tokens cancellation map, its own active_model_id, and its own start_event_watcher() reacting to that database's node_service. So the backend genuinely maintains N independent local-agent instances, one per open database.
But serve_headless/serve_grpc (packages/daemon/src/main.rs, BaseServices construction at lines 262, 332, 462, 551 — one per unix/windows × headless/tray build) only ever populate BaseServices.local_agent from bundle.local_agent — the bundle returned by open_default_database, i.e. the boot-time default database's instance — and build_base_router (packages/daemon/src/router.rs:65-71) registers exactly that one LocalAgentServiceServer. There is no per-request resolution on top of it.
Compare this to every other per-database service. NodeServiceImpl, ImportServiceImpl, EmbeddingsServiceImpl, and AgentSessionHandler all route through self.route(&request) / crate::db_routing::routed_database_services(request), which reads the x-ns-database-id metadata header and resolves the request to that specific database's service instance (packages/daemon/src/services/node_service.rs:143). LocalAgentServiceImpl's impl GrpcLocalAgentService (packages/daemon/src/services/local_agent_service.rs:1005) has no such adapter — subscribe_token_stream, cancel_turn, get_status, ensure_model_ready, list_models, load_model, unload_model, cancel_model_download, etc. all act directly on self/self.inner with zero header handling.
The desktop client codifies this as intentional rather than working around a gap: grpc_client.rs groups local_agent: LocalAgentServiceClient<Channel> under "Unrouted clients — registry-global … or daemon-global" (packages/desktop-app/src-tauri/src/services/grpc_client.rs:129-132), and local_agent_client() is commented "The local inference model is a daemon-global resource and is never routed by database" (:289-291). set_active_database() (:310-329) only rebuilds the routed node/import/embeddings/agent_session clients when the active database changes — its own doc comment says so explicitly: "settings/local_agent/database_service stay unrouted" (:307-308).
This directly contradicts ADR-053, which specifies "the desktop app stamps the active database id on every call" (../nodespace-docs/decisions/053-one-daemon-multiple-databases.md:40) as the client-side contract for every per-database service.
Failure scenario
- User loads a chat model.
LoadModel/EnsureModelReadygo over the unrouted client, so the engine is swapped into the default database'sLocalAgentServiceImplonly. - User switches the app to any other registered database via the ADR-053 live switcher.
- User sends an ai-chat message. The node write is correctly routed (via
NodeServiceImpl::route) to the other database, so that database's independentstart_event_watcher()picks it up — itsLocalAgentServiceImplstill holds aNoOpInferenceEngine, since the model was only ever loaded into the default instance. The turn fails immediately withInferenceError::NoModelLoadedand an error assistant message is appended. Chat never works on a non-default database. - Live token streaming is also broken independent of (1)-(3):
SubscribeTokenStream(local_agent_service.rs:1008) subscribes toself.inner.token_tx— the default instance's broadcast channel — while any turn running on another database broadcasts on that instance'stoken_tx. No client ever sees those tokens. CancelTurn(:1035) looks upnode_idin the default instance'sturn_tokensmap, so cancelling a turn running on another database silently no-ops.GetLocalStatus(:1048) reports the default instance's idle/busy state regardless of which database is actually mid-turn — so the UI can show Idle while another database is generating, or Busy when it's actually idle.SetDefaultDatabasechanging the daemon's runtime default does not fix this:BaseServiceswas constructed once at boot from the clone captured byopen_default_database, so the wrong-instance binding is fixed to whichever database was the default at process start, independent of later default changes.
Why it matters
Per ADR-053, one daemon serves N concurrently-open databases and the desktop app is meant to interact with whichever one is active. Local AI chat — model loading, sending a turn, streaming tokens, cancelling, and status — silently and completely stops working the moment a second database is registered and selected. There's no error surfaced to the user pointing at the real cause; it just looks like "the model isn't loaded" on the second database, because the wrong service instance is being asked.
Suggested fix
Either:
- Give
LocalAgentServiceImplthe sameroute()/routed_database_servicesadapter the other per-database services have, and register per-database resolution in the router the same way — reachable databases would then each dispatch to their own engine/turn state as already built; or - If a single daemon-global inference engine is actually the intended design (only one model loaded at a time, shared across databases), collapse the per-database construction: stop building a new
LocalAgentServiceImpl(and a new event watcher) per open database inbuild_database_services, and make the ai-chat event watcher itself dispatch by database — otherwise the N independent watchers, engines, and turn-state maps that already exist per database are pure dead weight that will silently diverge from whichever one instance is actually reachable.
The current state — per-database construction on the backend, single-instance wiring in the router, and explicit "daemon-global, never routed" framing on the client — is three different, mutually contradicted assumptions about the same feature.
Severity
Blocker. AI chat is completely non-functional on every database except the boot-time default, with no error message that points at the actual cause.
Activity
mstomar125 commentedon Aug 24, 2026
Baseline (worktree issue-2187-2191-2193-database-services-lifecycle, branched from origin/main @ d393ad1): frontend 227 test files / 4794 tests passed. Fixing #2187, #2191 and #2193 together in one PR — they are three manifestations of the same DatabaseServices lifecycle gap and two of them touch the same lines.
mstomar125 commentedon Aug 24, 2026
Resolved in PR #2267 (#2267).
All three issues were fixed together: they are three manifestations of one root cause — nothing owned which
DatabaseServicesinstance is authoritative for a database at a given moment.DatabaseServices::shutdown()is now the single, complete teardown (it also cancels in-flight turns) called on every path that retires a service set;evict_idle_databasesskips a database with a turn running; andLocalAgentServiceImplis split into a process-globalSharedLocalAgent(engine + model catalog) and a per-database half whose turn-state RPCs route byx-ns-database-idlike every other per-database service.Five regression tests, each verified to fail without its fix. Awaiting review — not self-merged.