Skip to content

Create the DB file on database create; error on wrong-database reads instead of serving the active DB - #1681

Merged
mstomar125 merged 1 commit into
NodeSpaceAI:mainfrom
mstomar125:issue-1675-db-create-search
Jul 16, 2026
Merged

mstomar125 merged 1 commit into
NodeSpaceAI:mainfrom
mstomar125:issue-1675-db-create-search

Conversation

@mstomar125

Copy link
Copy Markdown
Contributor

Summary

Two round-1 daemon findings on the multi-database (ADR-053) path:

  1. database create never created the DB file. DatabaseManager::create() registered the database in the registry but never created/opened the SQLite file, so a freshly-"created" database 500'd on first use. create() now creates and opens the file before registering, and rolls the registry entry back if the open fails. register() now errors when the target file is absent instead of silently registering a phantom.

  2. A wrong---database read silently served the active DB. When a request carried an x-ns-database-id header naming a database the daemon has no DatabaseManager for, routing fell through to the default services and returned the active database's data — a silent wrong-answer. routed_database_services() now returns UNIMPLEMENTED for a named-DB request when no DatabaseManager is present, while a header-less request keeps the default-fallback behavior. Verified safe for normal flows: the CLI and desktop app only stamp the header on an explicit database-switcher selection, never on the default single-DB path.

Testing

  • bun run test:all + cargo build --bin nodespaced + test:e2e (full pre-push gate) green.
  • Unit coverage: create-then-open rollback, register-absent-file error, routed-services header cases.

Closes #1675
Closes #1676

…ad instead of serving the active DB

database create only registered an entry in databases.toml — the file
was never created (a lazy-open follow-on that never shipped), so a new
database reported status=missing and the switcher wedged.

- DatabaseManager::create now creates + opens the file and its schema
  before registering, caches the open handle, and rolls back (closes the
  handle, deletes the db/-wal/-shm files) if registration fails; a fresh
  database reports Open. insert_entry rolls back its in-memory mutation
  when the TOML save fails; register now errors on an absent file.
- Database routing: the shared routed_database_services contract rejects
  a request that names a database (x-ns-database-id) on a daemon with no
  routing middleware installed with UNIMPLEMENTED, instead of silently
  serving the active database — a wrong-database read the caller can't
  detect. Header-less requests keep the default-database fallback, so the
  single-database Pro daemon and direct-construction tests are unchanged.
  This is what made semantic `search --database` (and any header-carrying
  request) silently return the active DB's results on the Pro daemon.

Full multi-database routing on the Pro daemon additionally needs the
routing layer installed there (tracked separately).

Closes #1675
Closes #1676

Co-Authored-By: Claude Fable 5 <[email protected]>
@mstomar125

Copy link
Copy Markdown
Contributor Author

APPROVE (adversarial re-review)

Verified the one intended behavior change is isolated and no normal flow breaks:

  • All four Server::builder() sites in packages/daemon/src/main.rs install DbManagerLayer, so the core daemon always has a manager → the new UNIMPLEMENTED branch is unreachable in-core.
  • Both client interceptors stamp the x-ns-database-id header only on explicit selection (packages/cli/src/lib.rs, packages/desktop-app/src-tauri/src/services/grpc_client.rs insert nothing when the id is None), so the default single-DB path is unaffected.
  • create() creates+opens before registering and rolls the registry entry back atomically on save failure; failed_create_rolls_back_and_leaves_the_daemon_serviceable proves the daemon stays serviceable. e2e/CLI/database_service tests were strengthened, not weakened.

Two low-severity nits accepted as best-effort on exceptional paths (documented behavior, match existing register/get_or_open patterns): an orphan -wal/-shm window if a registry-save fails while a model is loaded, and a concurrent same-path create TOCTOU.

The Pro-build validation item (external Pro daemon may omit DbManagerLayer; confirm the desktop switcher passes None for the default DB rather than its concrete id) is tracked in nodespace-sync#327 — a live check for Wave 5, not a blocker here.

Merging.

@mstomar125
mstomar125 merged commit 63fc83b into NodeSpaceAI:main Jul 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant