Skip to content

refactor(pairing): move device pairing store to shared SQLite state DB - #103160

Merged
steipete merged 3 commits into
mainfrom
claude/device-pairing-sqlite
Jul 10, 2026
Merged

steipete merged 3 commits into
mainfrom
claude/device-pairing-sqlite

Conversation

@steipete

@steipete steipete commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

What Problem This Solves

Device pairing state (paired devices with role tokens, pending pairing requests, bootstrap tokens) was the last auth-critical store still living in loose JSON files (devices/{paired,pending,bootstrap}.json). Per the database-first storage policy, OpenClaw-owned runtime state belongs in the shared SQLite state DB — the JSON files were migration debt with hand-rolled snapshot writes, and the schema already carried empty device_pairing_* / device_bootstrap_tokens tables (plus dead node_pairing_* tables) from an earlier reverted attempt.

Why This Change Was Made

  • Runtime store swap: src/infra/device-pairing.ts and src/infra/device-bootstrap.ts now load/persist through a new src/infra/device-pairing-store.ts row-mapping module backed by state/openclaw.sqlite (device_pairing_pending, device_pairing_paired, device_bootstrap_tokens). Persistence replaces table contents in one immediate transaction, preserving the exact snapshot semantics of the retired JSON files; WAL + busy_timeout keep concurrent gateway/CLI access safe. An explicit baseDir routes to that dir's DB, so all existing per-test temp-dir isolation keeps working.
  • Schema: added refreshed_at_ms (pending TTL keepalive) and approved_via / node_surface_json / pending_node_surface_json (paired) columns, additively for existing DBs. Removed the never-written node_pairing_* tables from the schema and dropped them from existing DBs (no shipped writer ever existed; provably empty).
  • Migration: new migrateLegacyDevicePairingStore imports legacy devices/paired.json records at gateway startup (existing SQLite rows win) and archives all three files with a .migrated suffix. It is ordered strictly before the node-surface fold, which writes onto the imported device records. Transient pending (5 min TTL) and bootstrap (10 min TTL) rows are dropped, not imported.
  • Doctor: the corrupt-JSON read diagnostic is replaced by a legacy-file check — doctor warns when devices/*.json still exists (migration has not run or the file is unreadable) and never treats it as empty.
  • Docs updated (docs/gateway/pairing.md, docs/channels/pairing.md, docs/nodes/index.md, docs/refactor/database-first.md).

User Impact

Transparent for upgrades: the first gateway start after the update imports existing paired devices (tokens, roles, scopes, node surfaces, approval provenance) into SQLite and archives the JSON files. Devices stay paired; nodes reconnect without re-approval. In-flight pending pairing requests and unredeemed setup codes at upgrade time expire (both are minutes-lived transients; devices re-request and setup codes can be reissued). openclaw doctor flags a store that could not be imported.

Evidence

  • Focused suites (device-pairing, device-pairing-prune, device-bootstrap, device-pairing-migration, node-pairing, node-pairing-migration, pairing-files, doctor-device-pairing, gateway nodes/authz/rate-limit/prune/control-ui-auth/probe-auth, openclaw-state-db): 5 Vitest shards, all passed.
  • New migration suite covers import + archive, existing-row precedence, corrupt-file fail-closed (files left in place), transient-only archive, and device-import-before-node-fold ordering.
  • tsgo core + core-test typechecks clean; import-cycle, deadcode, and database-first legacy-store guards pass; Kysely generated types regenerated via pnpm db:kysely:gen.
  • Live upgrade test on a production personal gateway (real paired devices + Telegram + nodes): results posted in PR comments before merge.

Device pairing, pending requests, and bootstrap tokens now live in the
device_pairing_* / device_bootstrap_tokens tables of state/openclaw.sqlite
instead of devices/{paired,pending,bootstrap}.json. Gateways import legacy
paired records once at startup (before the node-surface fold) and archive
the JSON files with a .migrated suffix; transient pending/bootstrap rows
are dropped. The unshipped node_pairing_* tables are removed from the
schema and dropped from existing DBs. Doctor now flags un-imported legacy
store files instead of corrupt-JSON reads.
@steipete
steipete requested a review from a team as a code owner July 9, 2026 23:47
@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation gateway Gateway runtime commands Command implementations plugin: file-transfer size: XL maintainer Maintainer-authored PR labels Jul 9, 2026
@steipete

Copy link
Copy Markdown
Contributor Author

Live upgrade test (production state copy, personal cloud gateway host, branch build at 91e400a):

  • Source state: the host's real ~/.openclaw pairing store — 127 paired devices (mixed operator/node roles, real tokens, 47 approved node surfaces from the earlier nodes/*.json fold) plus pending.json/bootstrap.json. Copied (with a consistent sqlite3 .backup of the live WAL-mode state DB) into an isolated OPENCLAW_STATE_DIR, gateway run from the branch build on a loopback test port with no channels configured, production gateway untouched.
  • First boot: device pairing store migrated to SQLite: imported 127 paired device(s), kept 0 existing record(s); all three devices/*.json archived as .migrated; node_pairing_pending/node_pairing_paired dropped from the copied production DB.
  • SQLite counts: device_pairing_paired = 127 (47 rows with node_surface_json, 53 with node role) — matches the JSON source exactly.
  • openclaw devices list --json over the gateway WS: 127 paired / 0 pending (identical to the pre-upgrade production CLI output). openclaw nodes status: Known 53 · Paired 53, surfaces intact.
  • Second boot: no re-migration (archives respected), gateway healthy, still 127 devices.
  • Test gateway, state copy, and build worktree removed after the run.

@steipete
steipete merged commit e6a8feb into main Jul 10, 2026
152 of 153 checks passed
@steipete
steipete deleted the claude/device-pairing-sqlite branch July 10, 2026 00:46
@steipete

Copy link
Copy Markdown
Contributor Author

Merged via squash.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Jul 10, 2026
openclaw#103160)

* refactor(pairing): move device pairing store to shared SQLite state DB

Device pairing, pending requests, and bootstrap tokens now live in the
device_pairing_* / device_bootstrap_tokens tables of state/openclaw.sqlite
instead of devices/{paired,pending,bootstrap}.json. Gateways import legacy
paired records once at startup (before the node-surface fold) and archive
the JSON files with a .migrated suffix; transient pending/bootstrap rows
are dropped. The unshipped node_pairing_* tables are removed from the
schema and dropped from existing DBs. Doctor now flags un-imported legacy
store files instead of corrupt-JSON reads.

* refactor(pairing): drop stale awaits now that store persistence is synchronous

* refactor(pairing): extract leaf record types to break store/domain module cycle
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

commands Command implementations docs Improvements or additions to documentation gateway Gateway runtime maintainer Maintainer-authored PR plugin: file-transfer size: XL

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant