Skip to content

Refresh the tray database menu live, and restore the open marker - #2042

Merged
mstomar125 merged 1 commit into
mainfrom
issue-2040-tray-live-refresh
Aug 12, 2026
Merged

mstomar125 merged 1 commit into
mainfrom
issue-2040-tray-live-refresh

Conversation

@mstomar125

Copy link
Copy Markdown
Contributor

First half of #2040. Does not close it — see "Not addressed" below.

Problem

The Databases submenu had exactly one push, right after the default database opened, and nothing pushed again. So it showed the registry as it was at daemon boot: a database created, renamed or removed afterwards — or opened by a switch, or closed by the idle reaper — read wrong until the daemon restarted.

Change

DatabaseManager::subscribe_changes() publishes on every registry and open-set mutation (insert_entry, covering create/register; rename; close, covering remove and eviction; and get_or_open's real-open branch). spawn_tray_database_sync in the daemon forwards it to the tray for the life of the process, replacing the two identical one-shot calls (there are two tray serve_grpc variants).

Two decisions worth a look:

  • A counter rides the channel, not the snapshot. Building a RegistrySnapshot takes the same locks the mutation is holding, so publishing one from inside a mutation would invite a deadlock. Observers re-read list() after waking — which also means a burst of changes collapses into one refresh, since watch keeps only the latest value. Every notify_changed() fires after its locks are released, so a woken observer cannot deadlock against the mutation that woke it.
  • The open marker is back. Multi-database polish: tray database menu, multi-window, and docs reconciliation #1536 left it out on purpose: pushed once at boot it would be confidently wrong within minutes, because the idle reaper closes databases and in-app switching opens others. With the menu rebuilt on every change it tracks reality. Markers render in a fixed order (default · open · synced) — under live updates a reshuffling label reads as flicker rather than information — and missing still suppresses everything else.

Tests

  • signal fires on create / rename / remove, and on open / evict;
  • labels carry the open marker, in stable order, with missing still winning.

Both signal tests were confirmed to fail with notify_changed stubbed out.

Two notes on the test changes:

  • open_state_is_not_reflected_in_the_label was replaced rather than fixed — it pinned the old deliberate omission, which is exactly what this PR reverses.
  • My first version of the open/evict test was wrong, not the code: create() already opens the database, so the get_or_open that followed was a cache hit and correctly signalled nothing. It now evicts first, then reopens.

cargo test -p nodespace-daemon green · clippy --all-targets -- -D warnings clean.

Not addressed (issue stays open)

The other half — picking a database while the app is already running should switch it in place and focus the window, and there should be exactly one app instance regardless of launch path. That needs cross-process focus (NSRunningApplication::activate and platform equivalents) plus a single-instance plugin, neither of which can be exercised or verified without a desktop session. So these acceptance boxes remain unticked:

  • Picking a database while the app is running switches it in place and focuses the window
  • Exactly one app instance regardless of how it was launched

Also unrun here: bun run test:all / quality:fix — this change is Rust-only and the pre-push gate ran test:all plus the e2e suite, but the frontend legs of that acceptance box belong with the UI half.

Note packages/desktop-app/src-tauri/src/commands/nodes.rs carries pre-existing cargo fmt drift on main; I kept it out of this commit rather than mixing an unrelated reformat in.

Co-Authored-By: Claude Opus 5 [email protected]

The Databases submenu was built once, right after the default database opened, and
nothing pushed again — so it showed the registry exactly as it was at daemon boot.
A database created, renamed or removed afterwards, or opened by a switch, or closed
by the idle reaper, read wrong until the daemon restarted.

DatabaseManager now publishes a change signal on every registry and open-set
mutation, and the daemon forwards it to the tray for the life of the process. The
signal is a counter rather than the snapshot: building a snapshot takes the same
locks the mutation holds, so publishing one from inside a mutation would invite a
deadlock. Observers re-read after waking, which also collapses a burst of changes
into a single refresh, and every notify fires after its locks are released.

That makes the open marker safe to show again. It was deliberately left out of the
first slice because a marker pushed once at boot would be confidently wrong within
minutes — the idle reaper closes databases and in-app switching opens others. With
the menu rebuilt on every change it now tracks reality, so the label reads
"name — default · open · synced" in a fixed marker order (a reshuffling label would
read as flicker under live updates). Missing still suppresses every other marker.

The test asserting open state was absent now asserts it is present; it pinned the
old deliberate omission, not a property worth keeping.

Tests cover the signal on create/rename/remove and on open/evict, and were
confirmed to fail with notify_changed stubbed out.

Does not address the other half of the issue — picking a database while the app is
already running should switch it in place and focus the window, and there should be
exactly one app instance regardless of launch path. Both need cross-process focus
and a single-instance plugin, which cannot be exercised without a desktop session.

Co-Authored-By: Claude Opus 5 <[email protected]>
@mstomar125
mstomar125 merged commit daa69e5 into main Aug 12, 2026
@mstomar125
mstomar125 deleted the issue-2040-tray-live-refresh branch August 12, 2026 11:19
mstomar125 added a commit that referenced this pull request Aug 20, 2026
#2180)

* Focus and switch the running app when picking a database from the tray (closes #2040)

open_ui previously no-op'd (or, when the app was launched outside the
tray, silently spawned a second process) whenever it tried to check
whether a UI process was already alive — the daemon has no reliable
way to know that, especially for a process it never spawned itself.

Delegate dedup to the app instead of tracking it in the daemon:
tauri-plugin-single-instance now guards the desktop app, so open_ui
always spawns a fresh UI process and a genuinely-second launch detects
the running instance, forwards its argv, and exits immediately. The
requested database rides both the existing INITIAL_DATABASE_ENV (read
by a cold launch at startup) and a new `--database <id>` CLI arg (the
only thing visible to an already-running instance's relaunch handler,
since a spawned-then-immediately-exited process's environment is not).

The relaunch handler focuses the main window (unminimize + show +
set_focus, the cross-process equivalent of NSRunningApplication::
activate on every desktop platform Tauri targets) and, when a database
was requested, emits `tray:select-database` so the frontend switches
in place through the same databaseStore.switchTo path an in-app
switcher click already uses.

Live tray-submenu refresh (registry/open-set changes pushed to the
tray without a daemon restart, and the `open` marker) already landed
in #2042; this closes the second half of #2040.

* Address adversarial review findings on the tray relaunch/switch mechanism

- Validate the tray-supplied database id against the registered list
  before committing to a switch in databaseStore.switchTo, mirroring
  the registered() guard load() already applies to this exact input.
  Previously an id for a database removed (or otherwise not
  registered) between the tray click and the relaunch event arriving
  would clear every cache and reset the workspace before the daemon's
  per-request NOT_FOUND ever surfaced, with no rollback.

- Add a pull-based fallback for the tray:select-database event: Tauri
  does not buffer/replay an event emitted with zero current listeners,
  and the frontend's listener only exists once app-shell.svelte has
  mounted, so a relaunch landing in that narrow boot window would
  silently drop the switch (the window still focused correctly).
  handle_relaunch now also stashes the id in a process-wide slot that
  the frontend pulls once via a new take_pending_tray_database_selection
  command right after registering the listener; switchTo is idempotent
  against the value the live event also happened to deliver.

- Log unminimize/show/set_focus failures in handle_relaunch instead of
  swallowing them silently, matching the emit's existing warn-on-error.

- Soften the open_ui/plugin-registration doc comments: this plugin's
  macOS backend claims its lock with a connect-then-bind probe rather
  than one atomic OS primitive (Windows uses a named mutex, Linux a
  D-Bus name claim), so two processes launched in that scheduling gap
  can theoretically both survive. Not fixable in this codebase; now
  documented rather than overstated as an absolute guarantee.

- Add test coverage for handle_relaunch (generic over the Tauri
  runtime, like graceful_shutdown/handle_run_event, so it's testable
  against tauri::test::mock_app()): no-main-window early return, and
  the full stash/pull/consume cycle for the fallback.

Found by a fresh adversarial review agent against the real PR diff.
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