Skip to content

LocalAgentService is not database-routed while its engine/turn state is per-database — AI chat broken on every non-default database #2187

Closed

Description

@mstomar125

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

  1. User loads a chat model. LoadModel/EnsureModelReady go over the unrouted client, so the engine is swapped into the default database's LocalAgentServiceImpl only.
  2. User switches the app to any other registered database via the ADR-053 live switcher.
  3. User sends an ai-chat message. The node write is correctly routed (via NodeServiceImpl::route) to the other database, so that database's independent start_event_watcher() picks it up — its LocalAgentServiceImpl still holds a NoOpInferenceEngine, since the model was only ever loaded into the default instance. The turn fails immediately with InferenceError::NoModelLoaded and an error assistant message is appended. Chat never works on a non-default database.
  4. Live token streaming is also broken independent of (1)-(3): SubscribeTokenStream (local_agent_service.rs:1008) subscribes to self.inner.token_tx — the default instance's broadcast channel — while any turn running on another database broadcasts on that instance's token_tx. No client ever sees those tokens.
  5. CancelTurn (:1035) looks up node_id in the default instance's turn_tokens map, so cancelling a turn running on another database silently no-ops.
  6. 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.
  7. SetDefaultDatabase changing the daemon's runtime default does not fix this: BaseServices was constructed once at boot from the clone captured by open_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 LocalAgentServiceImpl the same route()/routed_database_services adapter 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 in build_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

  1. added
    bug
    on Aug 20, 2026
  2. self-assigned this
    on Aug 20, 2026
  3. mstomar125 commented on Aug 24, 2026

    @mstomar125
    ContributorAuthor

    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.

  4. mstomar125 commented on Aug 24, 2026

    @mstomar125
    ContributorAuthor

    Resolved in PR #2267 (#2267).

    All three issues were fixed together: they are three manifestations of one root cause — nothing owned which DatabaseServices instance 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_databases skips a database with a turn running; and LocalAgentServiceImpl is split into a process-global SharedLocalAgent (engine + model catalog) and a per-database half whose turn-state RPCs route by x-ns-database-id like every other per-database service.

    Five regression tests, each verified to fail without its fix. Awaiting review — not self-merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

backendbughigh-priority

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions