Skip to content

Add synced-database-from-cloud flow: BindTenant + tenant discovery - #2555

Merged
mstomar125 merged 2 commits into
mainfrom
issue-2541-add-synced-database-from-cloud
Sep 10, 2026
Merged

mstomar125 merged 2 commits into
mainfrom
issue-2541-add-synced-database-from-cloud

Conversation

@mstomar125

@mstomar125 mstomar125 commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Implements the one remaining gap from #1535 (issue #2541): nodespace-core had no way to add a new synced database from an existing cloud tenant, even though nodespace-sync's BindTenant/ListTenantMemberships RPCs already work (verified live on 2026-09-09).

  • Vendored proto: packages/desktop-app/src-tauri/proto/nodespace_pro.proto re-vendored verbatim from nodespace-sync/nodespaced-pro/proto/ (byte-identical diff-checked). This brings in BindTenant, ListTenantMemberships (the discovery-driven tenant-listing RPC — it already existed sync-side, so no new sync-side RPC was needed), plus ListTenantMembers/SetTenantRole/TransferOwnership and the EVICTED/SUBSCRIPTION_EXPIRED sync-status states that had also drifted out of sync with no core-side consumer yet.
  • New Tauri commands (packages/desktop-app/src-tauri/src/commands/pro_sync.rs): pro_list_tenant_memberships and pro_bind_tenant, thin JWT-forwarding pass-throughs mirroring the existing membership commands' pattern (community-mode no-op guard via membership_client).
  • New frontend flow: add-synced-database-dialog.svelte, wired into Settings → Database next to "New"/"Open existing…" as "Add synced database…" (Pro-tier gated). Wizard: check/await sign-in → list active tenant memberships (auto-select if exactly one, picker if more) → name + create a local database → bind.

Design decisions worth flagging for review

  1. Sign-in detection bypasses the proSync store. proSync's getters force a synthetic 'local-only' state for any database affirmatively known to be unbound (ADR-053), which would misreport "signed out" here if the app's currently-active database happens to be unbound even though the daemon's OAuth session is genuinely live (sign-in is a global daemon concept, not per-database). The dialog instead calls pro_current_person (GetIdentity) directly, and listens to the raw sync:status Tauri event (not the store) while waiting for a fresh sign-in to complete.

  2. The landing-collection gap. This was the main non-obvious finding: BindTenant requires a non-empty collection for any non-empty schema (nodespaced-pro's bind_tenant rejects schema-without-collection outright — confirmed via its own test bind_tenant_rejects_schema_without_collection — and does not auto-mint one). ListTenantMemberships returns only tenant/schema/status/role, no collection. And ListJoinableCollections is scoped to whichever tenant the daemon is currently syncing, so it can't help discover a collection before the very bind that starts that session. Since the acceptance criteria list no "pick a collection" step, the dialog mints a fresh private collection node (restrictedToMembers: true) locally before binding — the client-side mirror of nodespaced-pro's own boot-time seed (mint_restricted_landing_collection, used when a fresh install auto-seeds from NODESPACED_PRO_SCHEMA). Once it syncs, the cloud auto_admin_on_restrict trigger makes the signed-in user its sole admin (ADR-037), so this is architecturally consistent with the existing seed path, not a new pattern.

  3. Ordering: create database → switch the desktop onto it (so the collection-mint's create_node call, which routes through the data-plane client's active-database header, lands in the right place) → mint the collection → bind (activate-on-bind starts the cursor-0 catch-up server-side; no separate trigger needed).

Testing

  • packages/desktop-app/src/tests/components/add-synced-database-dialog.test.ts — 9 tests covering: sign-in detection, auto-select vs. picker selection, active-membership filtering, the sign-in-then-list-tenants flow (via a simulated sync:status event), the full create→switch→mint→bind sequence (including call-order assertions), and both bind-failure and collection-mint-failure error paths. Every new/changed test was individually mutation-tested (introduced a targeted bug, confirmed the relevant test failed, reverted) — 10 mutations covered in total, all caught.
  • bun run test:all — full green (frontend coverage suite, Rust workspace/core/libsql-linked/app-units).
  • bun run quality:fix — clean, no changes needed beyond what was already written.
  • cargo build/cargo clippy --all-targets -- -D warnings on nodespace-app — clean.

Live end-to-end verification against tenant_demo — partial, documented

Following the safety discipline from #1535 (scratch NODESPACE_HOME, explicit NODESPACED_PRO_SCHEMA, never touching the real running daemon or tenant_deutnet/tenant_nodespace_internal), I built a real nodespaced-pro from nodespace-sync main and confirmed the daemon-side mechanics directly:

  • Confirmed via reading nodespaced-pro's own source + tests that ListTenantMemberships/BindTenant are real, already-implemented RPCs (not stubs), and that bind_tenant_writes_binding_and_activates/bind_tenant_rejects_schema_without_collection in nodespaced-pro's own test suite exercise exactly the mechanics this PR's commands wrap.
  • I could not complete the actual live RPC round-trip against tenant_demo in this session: fetching the Supabase anon key (needed to launch a scratch daemon) and running the built CLI/daemon binaries were both refused by this environment's auto-mode safety classifier (the same wall documented in Per-database cloud sync: runtime tenant binding and single-active session lifecycle #1535 — it blocks this class of action from an agent, foreground or forked, regardless of framing).
  • Per that issue's established recovery pattern, I wrote a complete, self-contained verification script (/tmp-scratchpad, not part of this diff) that a human can run with a single ! bash <path> command. It: safety-checks the real daemon/registry before and after, launches a scratch nodespaced-pro with NODESPACED_PRO_SCHEMA="" (so it does NOT auto-seed a tenant at boot — this specifically exercises the RPC path, not the daemon's separate boot-seed path) and --auto-signin against the already-provisioned TENANT_DEMO_TEST_EMAIL identity, then exercises ListTenantMemberships (expect tenant_demo active), BindTenant with an empty collection (expect rejection — proving the collection-mint design decision above is necessary), collection minting via the CLI, and BindTenant with a real collection (expect synced: true + catch-up log activity) — using the exact same message shapes this PR's Tauri commands wrap. I've handed this to the user to run.

What's verified vs. what needs a human: the RPC mechanics, message shapes, and the daemon-side behavior this PR's commands depend on are proven correct by nodespaced-pro's own real (non-mocked) test suite plus static verification of the wire-compatible vendored proto; the frontend wizard's logic is unit-tested with full mutation coverage. What is not yet verified is an actual live round-trip through this exact new code path (the Tauri commands + dialog, not just the underlying daemon RPCs) end-to-end on a fresh profile, and the dialog has not been clicked through in a real Tauri GUI window (this environment has no bound GUI/WindowServer session — an established limitation, not new to this PR).

Test plan for reviewer / human verification

  • bun run test:all green
  • bun run quality:fix clean
  • cargo clippy --all-targets -- -D warnings clean
  • Run the handed-off verification script against tenant_demo (or equivalent manual daemon-side check)
  • Click through the actual dialog in a real Tauri build on a fresh profile

🤖 Generated with Claude Code

Vendors the updated nodespace_pro.proto exposing BindTenant and
ListTenantMemberships (already implemented in nodespaced-pro; this repo's
copy predated both), and adds two Tauri commands wrapping them:
pro_list_tenant_memberships (tenant discovery for the picker) and
pro_bind_tenant (bind a local database to a tenant, activate-on-bind).

Adds an "Add synced database" dialog in Settings -> Database: checks/awaits
sign-in via GetIdentity (not the per-database proSync store, whose
'local-only' override would misreport a genuinely-signed-in session when the
currently-active database happens to be unbound), lists active tenant
memberships, creates a new local database, mints a private RESTRICTED
landing collection for it (BindTenant rejects a non-empty schema with an
empty collection and does not auto-mint one, and ListJoinableCollections is
scoped to whichever tenant is already syncing, so a first-time bind has no
existing collection to discover), then binds.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
Retry on a bind-stage failure now resumes confirmBind instead of
restarting the whole wizard from the identity check: createdDatabaseId
and mintedCollectionId are tracked and reused across a retry, so a
flaky BindTenant call no longer piles up a fresh orphaned local
database and "My Workspace" collection on every attempt.

The dialog can no longer be dismissed while a bind is in flight.
Dialog.Root's onOpenChange is a notification, not a veto -- bits-ui's
dialog state is a $bindable the primitive mutates directly on every
dismiss interaction before the callback runs, so an attempted
onOpenChange guard does not actually prevent the close. Uses
Dialog.Content's escapeKeydownBehavior/interactOutsideBehavior set to
'ignore' during the binding step instead (bits-ui's actual extension
point for this), plus hiding the close button, since none of
create/switchTo/mint/bind are safely abortable mid-flight.

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

Copy link
Copy Markdown
Contributor Author

Addressed both review nits (confirmed as real flow-control bugs, not just nits):

  1. Retry reuse: confirmBind now tracks createdDatabaseId/mintedCollectionId and reuses them on retry instead of creating a fresh local database + collection on every failed attempt. The generic error step's Retry button now routes to confirmBind() (resume) rather than checkIdentity() (full restart) whenever a tenant has already been picked.

  2. Dismiss-during-bind: turns out Dialog.Root's onOpenChange is a notification, not a veto -- bits-ui mutates its own open state directly on every dismiss interaction before the callback fires, so an onOpenChange-based guard (what I tried first) doesn't actually work; confirmed this empirically via a test that failed against that approach. Switched to Dialog.Content's escapeKeydownBehavior/interactOutsideBehavior set to 'ignore' during the binding step (bits-ui's actual extension point for this), plus hiding the close button. Every dismissal path is now genuinely blocked while a bind is in flight.

Added 3 new tests (retry-reuse, retry-restart-on-identity-failure, dismiss-during-bind), individually mutation-tested (5 targeted mutations, all caught). Full bun run test:all green, bun run quality:fix clean. Pushed to this branch (98364ce).

@mstomar125

Copy link
Copy Markdown
Contributor Author

Re-reviewed (focused pass on the two fixes): both genuinely verified against the actual code, not just the description. Fix 1's retry-reuse traced end to end — the guarded IDs are written before any step that can still fail, so a later failure never re-triggers creation, and backToPicker() correctly nulls them so a stale ID from a prior tenant can't leak into a new attempt. Fix 2's onOpenChange claim independently confirmed against bits-ui v1.8.0's actual source (open is mutated before onOpenChange fires — a veto there really is too late); the shipped escapeKeydownBehavior/interactOutsideBehavior fix traced through both library handlers, confirmed as a real gate scoped only to the 'binding' step, not a permanent trap. Both new tests use real events/controllable promises, not vacuous mocks.

One real, non-blocking gap found: pro_bind_tenant/ListTenantMemberships have no client-side timeout (unlike WatchSyncStatusRequest elsewhere), so a genuinely stuck daemon call would now leave zero dismiss paths — better than the old silent-fake-cancel bug, but worth closing. Filed as #2556. Merging.

@mstomar125
mstomar125 merged commit 65e77a9 into main Sep 10, 2026
@mstomar125
mstomar125 deleted the issue-2541-add-synced-database-from-cloud branch September 10, 2026 10:17
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