Skip to content

List databases in the tray and open the app on the one picked (#1536) - #2039

Merged
mstomar125 merged 3 commits into
mainfrom
issue-1536-tray-db-menu
Aug 12, 2026
Merged

mstomar125 merged 3 commits into
mainfrom
issue-1536-tray-db-menu

Conversation

@mstomar125

Copy link
Copy Markdown
Contributor

Summary

The tray offered a single "Open NodeSpace" that always landed on whatever database the app last had — even though the daemon has served several since ADR-053. It now carries a Databases submenu listing every registered database, marked with the two facts that matter (open, synced), and picking one opens the app on it.

Scope here is the tray slice only; multi-window (#2033) and the docs pass (#2034) were split out.

The constraint that shaped it

The registry can't be handed to the tray at startup: tray::run owns the main thread with a !Send tao loop, and it's already running before the gRPC side builds the DatabaseManager (main.rs:178 seeds the tray; the manager appears at :281, inside the spawned task).

⚠️ The 2026-07-10 design comment on this issue asserts Arc<DatabaseManager> is "already in scope where tray::run is called." It isn't — that shaped the wrong plan, so noting it here.

So the tray is told instead, over the same EventLoopProxy the Status label already uses (TrayEvent::DatabasesChanged). A snapshot arriving before the tray finishes initializing is held and applied once it does, so the two starting in either order is fine.

Testability

Labelling is a plain function over a RegistrySnapshot, so the rules live somewhere tests can reach without a display: open/synced markers, and a missing file being listed-but-unselectable rather than silently dropped (a vanished database should say so, not disappear). 4 Rust tests cover the labels, the missing case, id/order preservation, and the empty registry.

Launch path

The daemon sets NODESPACE_INITIAL_DATABASE on the process it spawns, and the app treats it as the highest-precedence choice for that launch only — a plain "Open NodeSpace" leaves it unset so the remembered selection still wins. An id naming a database that's no longer registered is ignored rather than leaving the app with no selection. Precedence is now requested ?? remembered ?? default ?? first, each validated against the registered list.

Two honest limits

  • A database added or removed later doesn't refresh the submenu until the daemon restarts — only the initial snapshot is pushed so far. Wiring the five registry-mutation handlers is a small follow-on.
  • Picking a database while the app is already running doesn't re-point it — the initial database is read at startup, and switching an open window needs a focus signal that doesn't exist yet.

Verification

Frontend 201 files / 4573 tests; cargo test -p nodespace-daemon + clippy --all-targets clean; lint + svelte-check clean. I confirmed the launch-precedence test fails against the pre-change code (the second precedence test is a guard — it passes either way, by design).

Unblocks #1535's remaining tenant/database picker work.

Part of #1536.

🤖 Generated with Claude Code

mstomar125 and others added 3 commits August 12, 2026 08:09
The tray offered a single "Open NodeSpace" that always landed on whatever
database the app last had, even though the daemon has served several since
ADR-053. It now carries a Databases submenu listing every registered database,
marked with the two facts that matter — whether it is open, and whether it syncs
to a cloud tenant — and picking one opens the app on it.

The registry cannot be handed to the tray at startup: the tray loop owns the
main thread and is running before the gRPC side builds the DatabaseManager. So
the tray is told instead, over the same event proxy the Status label already
uses. A snapshot that arrives before the tray finishes initializing is held and
applied once it does, so the two starting in either order is fine.

Labelling is a plain function over a registry snapshot, so the rules — open,
synced, and a missing file being listed-but-unselectable rather than silently
dropped — are decided somewhere tests can reach without a display.

For the launch itself the daemon sets NODESPACE_INITIAL_DATABASE on the process
it spawns, and the app treats it as the highest-precedence choice for that
launch only; a plain "Open NodeSpace" leaves it unset so the remembered
selection still wins. An id naming a database that is no longer registered is
ignored rather than leaving the app with no selection.

Two honest limits: a database added or removed later does not refresh the
submenu until the daemon restarts, since only the initial snapshot is pushed so
far; and picking a database while the app is already running does not re-point
it, because the initial database is read at startup and switching an open window
needs a focus signal that does not exist yet.

Multi-window and the docs pass that were bundled here are split out separately.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Review caught that the label's "open" marker is wrong more often than right. The
submenu is rendered once at daemon boot, and the idle reaper closes databases
during normal use, so the marker froze at whatever was open at startup. The
issue asks the menu to indicate state, so a confidently-stale marker is worse
than none: the label now carries the facts the registry actually keeps — which
database is the default, and which sync — and a test pins the deliberate absence
of the open marker so restoring it forces a decision about live refresh.

Also from review:

- The doc block above open_ui was left attached to the function inserted beneath
  it, so rebuild_databases_menu described spawning the UI and open_ui had no doc
  at all. Moved back, and extended to state what open_ui actually does when the
  app is already running.
- A failed removal during a rebuild orphaned menu items: the drain emptied the
  tracking list regardless, leaving items visible but unclickable and duplicated
  on the next rebuild. Removal failures are now logged and the list cleared
  unconditionally, and enablement is computed on the way out so a partial
  rebuild cannot leave reachable items behind a greyed-out parent.
- The launch-time database is answered once per process. It described a launch,
  but was re-read on every call, so switching database in-app and reloading the
  webview would have snapped back to the tray's choice.
- The desktop side spells the env var out because it cannot depend on the daemon
  crate; it now names the constant to keep the two in step.

Test hygiene, which this session has otherwise been spent fixing: the two new
tests installed a persistent mock implementation and left a remembered id in
localStorage, and the file's beforeEach used clearAllMocks (calls only) with no
storage reset — so both leaked into every later test in the file. One later test
was passing only because the leftover id happened to match. Reset the mock and
clear storage at the source.

Refreshing the menu live, restoring the open marker, and making a pick do
something when the app is already running are filed as a follow-up.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
The previous round "fixed" a latent concern by consuming the launch-time
database id on first read. Review found that made the common case wrong: a
second load() runs on every launch, so the second read got nothing, resolved to
the remembered database, and overwrote the tray's pick — nondeterministically,
depending on which load finished last. Picking a database in the tray would
have silently opened a different one.

The concern it addressed was latent — nothing reloads the webview today — so
the cure was worse than the disease. The read answers consistently again, and
the reason is written down next to it, along with where to consume it (at apply
time in the store) if a reload path ever appears.

Kept from that round: load() now re-checks that no selection was made while it
was suspended before assigning, so concurrent loads cannot overwrite each other
or hydrate the settings node twice. A test pins that two concurrent loads land
on the tray's pick.

Also from re-review: rebuild_databases_menu never fails, so it no longer returns
a Result that both call sites were pointlessly matching on; the label doc no
longer calls registry-persisted facts "stable" when the default is user-mutable
too; and two test fixtures named after status markers are renamed so the
assertion reads unambiguously.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@mstomar125

Copy link
Copy Markdown
Contributor Author

APPROVE (self-review, mstomar125)

Three rounds, and the second one is the instructive part.

Pass 1 confirmed the lifecycle work — RegistrySnapshot crossing into the !Send loop is sound, the pre-Init buffer is an Option that's replaced rather than accumulated so a failed initialize_tray can't grow it, .take() makes double-application impossible, muda MenuIds come from a never-reused counter so the click-routing chain can't collide, and set_enabled is honored because muda disables AppKit's autoenable. It found three majors: a doc block left attached to the wrong function, a rebuild path that orphaned menu items, and — the important one — the open marker freezing at daemon boot. spawn_idle_reaper closes idle databases minutes into normal use, so a marker pushed once would be confidently wrong most of the time, which is worse than absent when "indicate state" is the feature's whole point. The label now carries only what the registry persists (default · synced), with a test pinning the deliberate absence so restoring open forces the live-refresh decision. Live refresh, the open marker, and re-pointing a running app are #2040.

Pass 2 caught that one of my fixes made things worse. Consuming the launch-id on first read — meant to stop a webview reload re-applying it — broke the common case: a second load() runs on every launch via the daemon-reconnect listener, so the second read got nothing, resolved to the remembered database, and overwrote the tray's pick, nondeterministically depending on which load finished last. The reviewer demonstrated it with an interleaving table. Picking a database in the tray would have silently opened a different one. Reverted — the concern it addressed is latent (nothing reloads the webview today), so the cure was worse than the disease, and the reasoning is now written next to the code along with where to consume it if a reload path appears.

Kept from that round: load() re-checks that no selection was made while it was suspended before assigning, so concurrent loads can't overwrite each other or hydrate the settings node twice.

On the new concurrency test, honestly: with the read stable again it passes without the guard too, since both loads see the same value. It pins the outcome — re-introducing a consume-once read fails there rather than silently in the product — and the test says that rather than implying it proves the guard.

One AC note: the issue says "indicate active/synced". The daemon's only literal "active" notion is the compute scheduler's, which is as volatile as open, so default is a deliberate proxy under the staleness constraint — ticking that box on a different fact than it names, which #2040 closes properly.

cargo test -p nodespace-daemon 126 passed · clippy --all-targets clean · frontend 201 files / 4574 tests on a cold cache · lint + svelte-check clean.

@mstomar125
mstomar125 merged commit 6b8da87 into main Aug 12, 2026
@mstomar125
mstomar125 deleted the issue-1536-tray-db-menu branch August 12, 2026 06:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant