Skip to content

Daemon database manager: registry, per-database services, and gRPC routing #1532

Closed

Description

@malibio

Overview

Implements the daemon core of ADR-053 (One Daemon, Multiple Local Databases — ../nodespace-docs/decisions/053-one-daemon-multiple-databases.md). The daemon currently opens exactly one database, resolved once at boot, with every service built as a per-process singleton. This issue makes the daemon serve N registered databases concurrently behind its existing single socket.

Problem Statement

  • resolve_db_path() (packages/daemon/src/lib.rs) reads only the NODESPACED_DB_PATH env var (hard-coded into the launchd plist / systemd unit by daemon_setup.rs) or the default path. The active_database_path field in ~/.nodespace/daemon.toml is written by the Settings UI but never read — "Open Database…" is a silent no-op.
  • build_services() (packages/daemon/src/main.rs) constructs one SqliteStore, one NodeService, and one ServiceBundle per process. No gRPC request or event carries a database identifier.

Proposed Solution

Per ADR-053 §1–3: a DatabaseManager holding a registry plus a map of open per-database service sets, gRPC metadata routing with default-database fallback, and a new DatabaseService RPC surface. Greenfield rules — delete the dead machinery, no compatibility shims.

Implementation Approach

Registry and lifecycle

  • Add [[databases]] entries to ~/.nodespace/daemon.toml: id (ULID), name, path, created_at, last_opened_at, plus a default_database id. First boot with no registry auto-registers ~/.nodespace/database/nodespace.db under a default (user-renamable) name.
  • DatabaseManager: RwLock<HashMap<DatabaseId, Arc<DatabaseServices>>> + registry. Databases open lazily on first request; local-only databases may idle-close. Handle registered-but-missing files by surfacing a "missing database" status — never auto-delete registry entries.
  • Refactor build_services() into build_database_services(db_path, shared) (per-database: SqliteStore, NodeService + seeding, NodeEmbeddingService, EmbeddingProcessor, agent event watcher, GraphContextAssembler, import handler) and build_shared_services() (process-global: embedding model Arc<EmbeddingService>, chat CompositeModelManager, PtySessionManager, settings, tray). sqlite-vec registration (ensure_sqlite_vec_registered) is already process-global and multi-database-safe — keep the OnceCell exactly as is.

Routing

  • Tonic interceptor reads x-ns-database-id metadata, resolves via DatabaseManager (lazy-open), injects Arc<DatabaseServices> as a request extension. Absent header → default database so existing clients keep working.
  • New database_service.proto: List, Create(name, path?), Register(path), Remove (unregister only, never deletes files), SetDefault, Rename, plus per-database status (open/closed/missing/sync-bound). Add to BaseServices so ADR-043's compile-time completeness extends it to the Pro daemon.
  • Every server-streamed event envelope gains database_id.

Deletions (greenfield)

  • Delete active_database_path from DaemonConfig and its UpdateDaemonConfig handling (packages/daemon/src/services/settings_service.rs).
  • Delete NODESPACED_DB_PATH injection from plist/systemd/Windows generation in packages/desktop-app/src-tauri/src/daemon_setup.rs; keep the env var only as a test override mapping to a synthetic single-entry registry.

Acceptance Criteria

  • Two databases can be created, registered, and served concurrently by one daemon process (integration test)
  • Requests without the metadata header route to the default database; CLI and app work unchanged against it
  • DatabaseService RPCs work end-to-end (list/create/register/remove/set-default/rename/status)
  • Event streams carry database_id and are filterable per database
  • active_database_path and the plist NODESPACED_DB_PATH injection are gone
  • Named Pipe (Windows) path gets the same routing coverage as UDS
  • Tests pass (bun run test:all)
  • Code passes bun run quality:fix

Technical Specifications

Reference Files

  • DB path resolution: packages/daemon/src/lib.rs — resolve_db_path()
  • Service assembly: packages/daemon/src/main.rs — build_services(), serve loops
  • Router factory: packages/daemon/src/router.rs — BaseServices (ADR-043)
  • Settings service: packages/daemon/src/services/settings_service.rs — DaemonConfig
  • Service files: packages/desktop-app/src-tauri/src/daemon_setup.rs — plist/systemd writers
  • Store init: packages/core/src/db/sqlite_store.rs — ensure_sqlite_vec_registered(), SqliteStore::new

Implementation Details

Related Issues

Non-Goals

Client UX (switcher, CLI flags), compute fairness, and sync lifecycle — follow-on issues.

Activity

  1. self-assigned this
    on Jul 7, 2026
  2. mstomar125 commented on Jul 7, 2026

    @mstomar125
    Contributor

    Picked up now (2026-07-07) as a compiling foundation scaffold (DatabaseService proto + registry/DatabaseManager types) → draft PR.

    The full implementation is XL / multi-day and is not landable today. It converts every service handler from a captured Arc<CoreNodeService> to per-request Arc<DatabaseServices> resolution (all 6 impls + both streaming RPCs + every daemon integration test), plus a new 7-RPC proto service, a persistent registry with lazy-open/idle-close/missing-file lifecycle, the build_services split, database_id event-envelope plumbing, and greenfield deletions that ripple into the desktop Tauri commands and test harnesses.

    This is the ADR-053 foundation that #1533, #1534, #1535, and #1537 all depend on — sequencing it first is why the others are parked.

  3. mstomar125 commented on Jul 9, 2026

    @mstomar125
    Contributor

    Progress update — daemon routing foundation landed (Stages 1–5)

    The daemon-side foundation of ADR-053 is now on main across four merged PRs. All of it is behavior-preserving for the current single-database product and touches no cross-repo contract (BaseServices / build_base_router / service constructors are unchanged), so nodespaced-pro is unaffected.

    Merged

    Acceptance criteria status

    • ✅ Requests without the header route to the default database; CLI/app work unchanged.
    • ✅ Named Pipe (Windows) serve loops get the same routing wiring as UDS.
    • ✅ test:all / test:e2e green on every PR; quality:fix clean.
    • 🟡 "Two databases served concurrently" — the daemon can register, lazily open, and route between databases (unit-tested), but there is no client-reachable path to register a second one yet (see below).
    • ⬜ DatabaseService RPCs (list/create/register/remove/set-default/rename/status) — not started.
    • ⬜ Event streams carry database_id — not started.
    • ⬜ Remove active_database_path + plist NODESPACED_DB_PATH injection — not started.

    Remaining work (gated)

    1. DatabaseService gRPC server — this is the reachable path that makes routing user-visible (until it exists, production always resolves to the single default, so routing is exercised only in tests). It adds a service to BaseServices, which under ADR-043 is a cross-repo change requiring nodespace-sync lock-step — to be scheduled with sign-off, not landed unilaterally.
    2. LocalAgentService routing — deferred deliberately: its model manager is process-global (pending a hoist-to-shared refactor) while only chat-turn processing is per-database; it will be routed together with that refactor.
    3. Event-envelope database_id, and removal of active_database_path / NODESPACED_DB_PATH.

    Keeping this issue In Progress; the foundation is complete and the next step is the cross-repo DatabaseService server.

  4. mstomar125 commented on Jul 9, 2026

    @mstomar125
    Contributor

    Progress — DatabaseService gRPC server (the reachable multi-database path)

    Building the piece that makes ADR-053 routing user-visible. Until now a client had no way to register a second database, so the Stage 1–5 routing was exercised only in tests; production always resolved to the single default.

    Core — PR #1592 (open, CI running):

    • New DatabaseServiceImpl wrapping the existing DatabaseManager: List/Create/Register/Remove/SetDefault/Rename over the already-generated proto.
    • Process-global (operates on the registry itself), so it is deliberately not routed by x-ns-database-id. Wired into BaseServices + build_base_router + all four serve loops, cloning the manager already opened for the default (no double-open).
    • Verified: fmt/clippy --all-targets clean; cargo test -p nodespace-daemon 51 lib (+3 new unit tests) + all integration suites green. New e2e over the real tonic transport (database_service_e2e.rs): the DbManagerLayer injects the manager over the wire → Create registers a 2nd database → a header-routed NodeService write lands in it (visible with the header, NotFound without — real isolation), the 2nd database flips to Open, and an unregistered header is rejected.

    Cross-repo (ADR-043) — sync prepared, pending core merge:
    Adding database to BaseServices is the intended compile-time contract. The nodespaced-pro change is ready in the working tree: bump the core pin, seed a DatabaseManager with the active database as default, provide DatabaseServiceImpl. Design decision: the Pro daemon installs no routing layer — its build_database_services bundle hides the raw NodeService handle the sync layer needs, and installing a layer over a manager it didn't build its bundle through would double-open the same SQLite file. So on Pro the service is a truthful registry view of the single active database (List reports it); serving multiple local databases under one cloud-sync session is an explicit deferral (undefined tenant mapping). Pro's data/sync path is byte-for-byte unchanged. It lands as a coordinated nodespace-sync PR once #1592 merges.

    Acceptance criteria after this lands: ✅ DatabaseService RPCs. Remaining: event-envelope database_id and removal of the single-database active_database_path/NODESPACED_DB_PATH assumption — next.

  5. mstomar125 commented on Jul 9, 2026

    @mstomar125
    Contributor

    ✅ DatabaseService server landed (core merged + sync lock-step PR up)

    • core ADR-053: DatabaseService gRPC server (local-database registry) #1592 — MERGED (b82c899c). Independent review confirmed the adapter correctness, the unregister-only remove (no data-destruction path), the real transport-level e2e, and endorsed the design (compile-required BaseServices field + process-global/not-routed). CI green.
    • sync Implement OrderedListNode (Auto-Numbered Lists) #294 — OPEN (the ADR-043 lock-step pair): nodespaced-pro bumps its core pin to b82c899c and provides DatabaseServiceImpl backed by a manager seeded with its active database. No DbManagerLayer on Pro (its data/sync path is unchanged; multi-DB-under-one-cloud-session deferred). Validated: builds against the merged core, 45 tests green, clippy + fmt clean.

    Acceptance criteria: ✅ default-header routing · ✅ Windows serve-loop wiring · ✅ DatabaseService RPCs · 🟡 concurrent serving (unit + transport e2e) · ⬜ event database_id · ⬜ remove active_database_path.

    Next (to close this epic): event-envelope database_id and migrating the Settings DB-path display to DatabaseService.List so the vestigial active_database_path / plist NODESPACED_DB_PATH single-database assumption can be removed.

  6. mstomar125 commented on Jul 9, 2026

    @mstomar125
    Contributor

    ✅ DatabaseService coordinated pair fully merged + Stage 6 (event database_id) up

    Acceptance criteria: ✅ default-header routing · ✅ Windows serve loops · ✅ DatabaseService RPCs · ✅ concurrent serving (unit + transport e2e) · 🟡 event database_id (#1593 in review) · ⬜ remove active_database_path + plist NODESPACED_DB_PATH injection (last criterion, in progress).

  7. mstomar125 commented on Jul 9, 2026

    @mstomar125
    Contributor

    ✅ Stage 6 merged — core #1593 (7fe5a896): WatchNodes events now carry the serving database's database_id. Additive + non-breaking (Pro inherits the proto field on its next pin bump; its single-DB streams carry an empty id, as documented).

    Only remaining criterion: remove active_database_path + the plist/systemd NODESPACED_DB_PATH injection (in progress — the daemon already defaults to the exact injected path, so removing the injection doesn't move the DB). Once that lands, all acceptance criteria are met and this epic closes.

  8. mstomar125 commented on Jul 9, 2026

    @mstomar125
    Contributor

    Stage 7 (last criterion) split into two — first half up as #1594

    #1594 (open): removes the redundant NODESPACED_DB_PATH injection from the launchd plist + systemd unit. Verified safe — the daemon's resolve_db_path() default is the identical path, so the DB location is unchanged.

    Remaining half — remove active_database_path: scoped after investigation. Findings:

    • active_database_path (in daemon.toml / the SettingsService daemon-config RPCs) is vestigial — the daemon picks its database via resolve_db_path() + the registry and never reads it.
    • Its select_new_database / reset_database_to_default tauri commands are therefore effectively no-ops (the daemon ignores the value they set), and their Settings→Database buttons are wired to those broken commands.
    • Its one working use is the read-only DB-path display (Settings→Database + Diagnostics).

    Plan for that half: drop active_database_path from the settings proto + daemon config; re-source the path display from DatabaseService.List (default entry) via a new tauri command (keeping the display working); delete or migrate the no-op select/reset commands + their buttons. This touches the settings proto + tauri command layer + the frontend (with bun run test), so it lands as its own focused change rather than being rushed — the multi-database capability (routing + DatabaseService + event database_id) is already fully merged; this is the config/UX cleanup tail. One UX call to confirm: under the registry model, "select database" should become "register + set default" — or the already-broken buttons are simply removed as dead code.

  9. mstomar125 commented on Jul 9, 2026

    @mstomar125
    Contributor

    Resolved — ADR-053 daemon core complete

    The daemon now serves N registered databases concurrently behind its single socket, with a full DatabaseService RPC surface, metadata-based routing (default-database fallback), and per-database event stamping. The Pro daemon (nodespaced-pro) provides DatabaseService in lock-step via ADR-043's compile-time completeness.

    Acceptance criteria

    Pro lock-step

    • nodespaced-pro seeds a single-active-database DatabaseManager and provides DatabaseServiceImpl (no routing layer — building the default bundle over a manager whose per-database services the Pro sync layer needs would double-open the same SQLite file; the second registered database is intentionally inert for now). (nodespace-sync Implement OrderedListNode (Auto-Numbered Lists) #294)

    PRs

    Deferred (non-goals per this issue / ADR-053)

    Client UX (database switcher, CLI flags), compute fairness, sync lifecycle for the second database, and local_agent per-database routing (bundled with a future model-manager hoist) are follow-on work, out of scope here.

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions