8.1 KiB
076 — Move db-sync auth fields from frontend.worker.state/*db-sync-config to frontend.worker.state/*state
Goal: Move all db-sync auth-related runtime fields out of frontend.worker.state/*db-sync-config and store them in frontend.worker.state/*state.
Goal: Keep *db-sync-config focused on transport/runtime config (ws-url, http-base, and non-auth flags), and keep auth/token lifecycle in one place (*state).
Goal: Align auth flow behavior across web app, desktop app, logseq-cli, and db-worker-node.
Architecture: Worker sync modules (frontend.worker.sync.util, frontend.worker.sync.auth, related sync helpers) should read auth state from worker-state/*state only after migration.
Architecture: :thread-api/sync-app-state becomes the canonical write path for auth fields; :thread-api/set-db-sync-config remains for non-auth sync config.
Related:
src/main/frontend/worker/state.cljssrc/main/frontend/worker/db_core.cljssrc/main/frontend/worker/sync/util.cljssrc/main/frontend/worker/sync/auth.cljssrc/main/frontend/persist_db/browser.cljssrc/main/frontend/handler/events/ui.cljssrc/main/frontend/components/repo.cljssrc/main/frontend/handler/events/rtc.cljssrc/main/frontend/handler/db_based/sync.cljssrc/main/logseq/cli/command/sync.cljssrc/main/frontend/worker/db_worker_node.cljs
Current implementation snapshot
Web app
- Worker init (
persist_db/browser.cljs) currently sends:thread-api/set-db-sync-configwith::ws-url:http-base:oauth-domain:oauth-client-id
- Auth tokens (
:auth/id-token,:auth/access-token,:auth/refresh-token) are synced through:thread-api/sync-app-state.
Desktop app
- Desktop renderer uses remote db-worker-node runtime (
persist_db/remote.cljs+ electron IPC:db-worker-runtime). - It shares the same frontend sync call sites (
events/ui.cljs,components/repo.cljs) that currently push oauth fields through:thread-api/set-db-sync-config. - It also uses
:thread-api/sync-app-stateto sync token state before starting sync flows.
logseq-cli
logseq.cli.command.synccurrently builds worker sync config with:ws-url,:http-base, and:auth-token.- CLI sends
:thread-api/set-db-sync-configbefore sync operations. - CLI currently uses
:thread-api/get-db-sync-configto preserve existing worker:auth-tokenfor non-authenticated actions (sync stop,sync status) when runtime config has no token. - CLI does not currently treat
:thread-api/sync-app-stateas the primary auth write path.
db-worker-node
frontend.worker.db-worker-node/non-repo-methodsalready allows both::thread-api/set-db-sync-config:thread-api/get-db-sync-config:thread-api/sync-app-state
- db-worker-node currently persists whatever auth fields are sent in
set-db-sync-configbecause worker stores the full map in*db-sync-config.
Auth field inventory to migrate
Auth-related fields currently coupled to *db-sync-config:
:auth-token:oauth-token-url:oauth-domain:oauth-client-id
Non-auth fields that should remain in *db-sync-config:
:ws-url:http-base:enabled?- non-auth feature toggles/config flags
Target state contract
After migration:
*db-sync-configcontains only non-auth sync config.*statecontains all sync auth fields, including oauth metadata used for worker-side token refresh.
Proposed worker auth keys in *state:
:auth/id-token:auth/access-token:auth/refresh-token:auth/oauth-token-url:auth/oauth-domain:auth/oauth-client-id
Locked decisions
:thread-api/get-db-sync-configmust return non-auth fields only.- Implementation must follow phase order in this document (Phase 1 → Phase 5) without reordering.
Implementation plan
Phase 1 — Worker state model + compatibility bridge
- Extend
frontend.worker.state/*statedefaults with oauth auth metadata keys. - Add worker helpers to normalize/split auth fields from sync config payloads.
- Update
frontend.worker.sync.util/auth-tokenandfrontend.worker.sync.auth/<refresh-id&access-tokento read auth fields from*state. - Keep a temporary compatibility bridge in
:thread-api/set-db-sync-config:- If auth keys are present in incoming config, move/merge them into
*state. - Keep only non-auth keys in
*db-sync-config.
- If auth keys are present in incoming config, move/merge them into
- Enforce
:thread-api/get-db-sync-configcontract to return non-auth fields only from the beginning of migration. - Add/update worker tests for auth resolution and oauth token URL/client ID lookup from
*state.
Phase 2 — Web + desktop producer updates
- Update web/desktop
set-db-sync-configcall sites to stop sending auth-related fields:src/main/frontend/persist_db/browser.cljssrc/main/frontend/handler/events/ui.cljssrc/main/frontend/components/repo.cljs
- Ensure oauth metadata is sent through
:thread-api/sync-app-statepayloads (or equivalent state sync path) so worker auth refresh can still resolve oauth endpoints. - Update state-sync payload builders:
src/main/frontend/handler/events/rtc.cljssrc/main/frontend/handler/db_based/sync.cljs
- Add/update frontend tests to assert new payload boundaries (
sync-app-statecarries auth metadata;set-db-sync-configcarries non-auth config only).
Phase 3 — CLI migration to sync-app-state auth writes
- Remove
:auth-tokenfrom CLI worker sync config payload (sync-configfunction inlogseq.cli.command.sync). - Replace CLI token-preservation logic that depends on
get-db-sync-configwith explicit auth-state sync:- Resolve auth via
cli-auth/resolve-auth!/resolve-auth-token!. - Send auth into worker via
:thread-api/sync-app-stateas:auth/*fields.
- Resolve auth via
- Keep
sync config set|get|unsetas non-auth config only (ws-url,http-base). - Update CLI tests in
src/test/logseq/cli/command/sync_test.cljsto validate new invoke sequence (sync-app-state+set-db-sync-confignon-auth payload).
Phase 4 — db-worker-node API contract alignment
- Keep method allowlist behavior unchanged (
sync-app-stateremains non-repo method). - Update db-worker-node tests that currently assert
auth-tokeninsideget-db-sync-config:- move assertions to
sync-app-statebehavior and sync runtime behavior.
- move assertions to
- Ensure repo-mismatch behavior remains unchanged (no auth contract leakage into repo-bound checks).
Phase 5 — Remove temporary compatibility paths
- Remove fallback reads from
*db-sync-configfor auth fields once all producers are migrated. - Keep
get-db-sync-configstrict: return non-auth fields only. - Keep final state model strict:
- auth only from
*state - transport config only from
*db-sync-config
- auth only from
Testing plan
Targeted tests
bb dev:test -v frontend.worker.node-sync-testbb dev:test -v frontend.worker.sync.crypt-testbb dev:test -v frontend.worker.db-sync-testbb dev:test -v frontend.worker.db-worker-node-testbb dev:test -v frontend.handler.db-based.sync-testbb dev:test -v frontend.components.repo-testbb dev:test -v logseq.cli.command.sync-test
Final verification
bb dev:lint-and-test
Edge cases and migration risks
- Running daemon with stale in-memory auth state while CLI switches between commands.
- Mixed-version compatibility (new CLI with old worker, old CLI with new worker) during rollout.
- Worker token refresh requiring oauth metadata after migration; missing oauth fields must produce clear diagnostics.
- Avoid confusion with unrelated db-worker HTTP auth token (
persist_db.node/persist_db.remotetransport auth); this plan only changes db-sync cloud auth state inside worker sync logic.
Acceptance criteria
- Worker sync auth logic no longer depends on auth fields inside
*db-sync-config. - All auth-related db-sync fields are sourced from
*state. set-db-sync-configpayloads from web/desktop/CLI carry only non-auth sync config.- CLI sync flows remain functional (start/status/stop/upload/download/remote-graphs/ensure-keys/grant-access).
- db-worker-node tests and CLI tests pass with updated contracts.
- No regression in desktop/web db-sync startup and key-management flows.