@kedem/okdb 2.4.0 → 2.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (234) hide show
  1. package/CHANGELOG.md +160 -0
  2. package/algorithm-worker-thread.js +1 -1
  3. package/bin/okdb.js +1 -1
  4. package/docs/auth-and-sync.md +2 -1
  5. package/docs/auth-permissions.md +31 -5
  6. package/docs/change-log.md +6 -6
  7. package/docs/licensing.md +6 -1
  8. package/docs/migration.md +60 -1
  9. package/docs/plugins.md +1 -1
  10. package/docs/processors.md +1 -1
  11. package/docs/subscriptions.md +6 -4
  12. package/docs/views.md +4 -0
  13. package/okdb-functions-sandbox-worker.js +1 -1
  14. package/okdb-http-worker-child.js +1 -1
  15. package/okdb-views-bootstrap-worker.js +1 -1
  16. package/okdb.js +1 -1
  17. package/package.json +1 -1
  18. package/public/admin-app.js +1 -1
  19. package/public/components/schema-edit-modal.ok.js +1 -0
  20. package/public/core/db/db-section.ok.html +1 -0
  21. package/public/core/db/modals/add-item-modal.ok.js +1 -0
  22. package/public/core/db/modals/create-fts-modal.ok.js +1 -0
  23. package/public/core/db/modals/create-index-modal.ok.js +1 -0
  24. package/public/core/db/parts/db-overview.ok.js +1 -0
  25. package/public/core/db/parts/edit-row.ok.js +1 -0
  26. package/public/core/db/parts/type-changelog.ok.js +1 -0
  27. package/public/core/db/parts/type-data-tab.ok.js +1 -0
  28. package/public/core/db/parts/type-list.ok.js +1 -0
  29. package/public/core/db/parts/type-manage-tab.ok.js +1 -0
  30. package/public/core/db/parts/type-schema-tab.ok.js +1 -0
  31. package/public/core/db/parts/type-viewer.ok.js +1 -0
  32. package/public/core/db/type-ops.js +1 -0
  33. package/public/core/docs/docs-page.ok.html +1 -0
  34. package/public/core/license/license-panel.ok.js +1 -0
  35. package/public/core/system/parts/system-log.ok.js +1 -0
  36. package/public/core/system/parts/system-processing-panel.ok.js +1 -0
  37. package/public/core/system/parts/system-storage-panel.ok.js +1 -0
  38. package/public/core/system/system-events-page.ok.js +1 -0
  39. package/public/core/system/system-logs-page.ok.js +1 -0
  40. package/public/core/system/system-overview-page.ok.html +1 -0
  41. package/public/core/system/system-runtime-page.ok.js +1 -0
  42. package/public/core/system/system-section.ok.html +1 -0
  43. package/public/features/api/api-panel.ok.js +1 -0
  44. package/public/features/api/index.js +1 -0
  45. package/public/features/auth/auth-panel.ok.js +1 -0
  46. package/public/features/auth/index.js +1 -0
  47. package/public/features/files/files-section.ok.html +1 -0
  48. package/public/features/files/index.js +1 -0
  49. package/public/features/files/parts/files-file-list.ok.js +1 -0
  50. package/public/features/files/parts/files-path-browser.ok.js +1 -0
  51. package/public/features/files/parts/files-preview.ok.js +1 -0
  52. package/public/features/files/parts/files-upload-panel.ok.js +1 -0
  53. package/public/features/functions/exports.js +1 -0
  54. package/public/features/functions/functions-section.ok.html +1 -0
  55. package/public/features/functions/functions-store.js +1 -0
  56. package/public/features/functions/index.js +1 -0
  57. package/public/features/functions/modals/function-modal.ok.js +1 -0
  58. package/public/features/functions/parts/function-detail.ok.js +1 -0
  59. package/public/features/functions/parts/function-logs.ok.js +1 -0
  60. package/public/features/functions/parts/function-runner.ok.js +1 -0
  61. package/public/features/pipelines/engine-ui-utils.js +1 -0
  62. package/public/features/pipelines/exports.js +1 -0
  63. package/public/features/pipelines/index.js +1 -0
  64. package/public/features/pipelines/modals/create-pipeline-modal.ok.js +1 -0
  65. package/public/features/pipelines/modals/edit-pipeline-modal.ok.js +1 -0
  66. package/public/features/pipelines/modals/engine-modal.ok.js +1 -0
  67. package/public/features/pipelines/parts/embed-create-panel.ok.js +1 -0
  68. package/public/features/pipelines/parts/embed-embedder-panel.ok.js +1 -0
  69. package/public/features/pipelines/parts/embed-indexer-panel.ok.js +1 -0
  70. package/public/features/pipelines/parts/embed-search-panel.ok.js +1 -0
  71. package/public/features/pipelines/parts/embed-worker-panel.ok.js +1 -0
  72. package/public/features/pipelines/parts/embeddings-engine-panel.ok.js +1 -0
  73. package/public/features/pipelines/parts/engine-declaration-editor.ok.js +1 -0
  74. package/public/features/pipelines/parts/engines-overview.ok.js +1 -0
  75. package/public/features/pipelines/parts/generic-engine-panel.ok.js +1 -0
  76. package/public/features/pipelines/parts/pipeline-create-panel.ok.js +1 -0
  77. package/public/features/pipelines/parts/pipeline-flow-strip.ok.js +1 -0
  78. package/public/features/pipelines/parts/type-pipelines-tab.ok.js +1 -0
  79. package/public/features/pipelines/pipelines-section.ok.html +1 -0
  80. package/public/features/queue/index.js +1 -0
  81. package/public/features/queue/modals/create-bucket-modal.ok.js +1 -0
  82. package/public/features/queue/modals/edit-bucket-modal.ok.js +1 -0
  83. package/public/features/queue/modals/edit-job-modal.ok.js +1 -0
  84. package/public/features/queue/modals/enqueue-job-modal.ok.js +1 -0
  85. package/public/features/queue/modals/job-type-modal.ok.js +1 -0
  86. package/public/features/queue/parts/job-log-panel.ok.js +1 -0
  87. package/public/features/queue/parts/queue-buckets.ok.js +1 -0
  88. package/public/features/queue/parts/queue-job-types.ok.js +1 -0
  89. package/public/features/queue/parts/queue-jobs.ok.js +1 -0
  90. package/public/features/queue/parts/queue-types.ok.js +1 -0
  91. package/public/features/queue/parts/queue-workers.ok.js +1 -0
  92. package/public/features/queue/queue-section.ok.html +1 -0
  93. package/public/features/sync/index.js +1 -0
  94. package/public/features/sync/sync-panel.ok.js +1 -0
  95. package/public/features/views/index.js +1 -0
  96. package/public/features/views/modals/create-view-modal.ok.js +1 -0
  97. package/public/features/views/parts/views-list.ok.js +1 -0
  98. package/public/features/views/parts/views-overview.ok.js +1 -0
  99. package/public/features/views/views-section.ok.html +1 -0
  100. package/public/features/views/views-store.js +1 -0
  101. package/public/index.html +9 -3
  102. package/public/modules/ok.js +1 -1
  103. package/public/shell/admin-shell.ok.html +3 -0
  104. package/public/shell/core-manifest.js +1 -0
  105. package/public/shell/shell-piece.ok.js +1 -0
  106. package/public/shell/shell-section-host.ok.js +1 -0
  107. package/public/shell/shell-section.ok.js +1 -0
  108. package/public/shell/shell-store.js +1 -0
  109. package/public/stores/auth-store.js +1 -1
  110. package/public/stores/okdb-store.js +1 -1
  111. package/public/stores/storage-store.js +1 -0
  112. package/public/styles.css +1 -1
  113. package/public/vendor/okjs/ok.esm.min.js +1 -1
  114. package/public/vendor/okjs/toolkit/components/ok-chart.ok.js +1 -1
  115. package/public/vendor/okjs/toolkit/components/ok-doc-viewer.ok.js +1 -1
  116. package/public/vendor/okjs/toolkit/components/ok-marked.ok.js +1 -1
  117. package/public/vendor/okjs/toolkit/components/ok-monaco.ok.js +1 -1
  118. package/types/okdb.d.ts +20 -0
  119. package/public/components/admin-route-host.ok.js +0 -1
  120. package/public/components/field.html +0 -158
  121. package/public/layouts/_default.ok.html +0 -3
  122. package/public/sections/api/index.ok.html +0 -1
  123. package/public/sections/auth/index.ok.html +0 -1
  124. package/public/sections/auth/modals/perm-info-popover.ok.js +0 -1
  125. package/public/sections/db/index.ok.html +0 -1
  126. package/public/sections/db/modals/add-item-modal.ok.js +0 -1
  127. package/public/sections/db/modals/create-fts-modal.ok.js +0 -1
  128. package/public/sections/db/modals/create-index-modal.ok.js +0 -1
  129. package/public/sections/db/modals/create-pipeline-modal.ok.js +0 -1
  130. package/public/sections/db/modals/schema-edit-modal.ok.js +0 -1
  131. package/public/sections/db/parts/db-overview.ok.js +0 -1
  132. package/public/sections/db/parts/edit-row.ok.js +0 -1
  133. package/public/sections/db/parts/type-changelog.ok.js +0 -1
  134. package/public/sections/db/parts/type-list.ok.js +0 -1
  135. package/public/sections/db/parts/type-pipelines-tab.ok.js +0 -1
  136. package/public/sections/db/parts/type-schema-tab.ok.js +0 -1
  137. package/public/sections/db/parts/type-viewer.ok.js +0 -1
  138. package/public/sections/db/parts/type-violations-tab.ok.js +0 -1
  139. package/public/sections/docs/index.ok.html +0 -1
  140. package/public/sections/embeddings/index.ok.html +0 -1
  141. package/public/sections/embeddings/modals/edit-pipeline-modal.ok.js +0 -1
  142. package/public/sections/embeddings/parts/embed-create-panel.ok.js +0 -1
  143. package/public/sections/embeddings/parts/embed-embedder-panel.ok.js +0 -1
  144. package/public/sections/embeddings/parts/embed-engine-declaration-editor.ok.js +0 -1
  145. package/public/sections/embeddings/parts/embed-engine-list.ok.js +0 -1
  146. package/public/sections/embeddings/parts/embed-indexer-panel.ok.js +0 -1
  147. package/public/sections/embeddings/parts/embed-lifecycle-bar.ok.js +0 -1
  148. package/public/sections/embeddings/parts/embed-overview.ok.js +0 -1
  149. package/public/sections/embeddings/parts/embed-search-panel.ok.js +0 -1
  150. package/public/sections/embeddings/parts/embed-worker-panel.ok.js +0 -1
  151. package/public/sections/embeddings/parts/embeddings-engine-panel.ok.js +0 -1
  152. package/public/sections/embeddings/parts/engines-overview.ok.js +0 -1
  153. package/public/sections/embeddings/parts/pipeline-create-panel.ok.js +0 -1
  154. package/public/sections/embeddings/parts/pipeline-flow-strip.ok.js +0 -1
  155. package/public/sections/engines/engine-ui-utils.js +0 -1
  156. package/public/sections/engines/index.ok.html +0 -1
  157. package/public/sections/engines/modals/engine-modal.ok.js +0 -1
  158. package/public/sections/engines/parts/engine-declaration-editor.ok.js +0 -1
  159. package/public/sections/engines/parts/generic-engine-panel.ok.js +0 -1
  160. package/public/sections/files/index.ok.html +0 -1
  161. package/public/sections/files/parts/files-collection-list.ok.js +0 -1
  162. package/public/sections/files/parts/files-collection-panel.ok.js +0 -1
  163. package/public/sections/files/parts/files-file-list.ok.js +0 -1
  164. package/public/sections/files/parts/files-path-browser.ok.js +0 -1
  165. package/public/sections/files/parts/files-preview.ok.js +0 -1
  166. package/public/sections/files/parts/files-upload-panel.ok.js +0 -1
  167. package/public/sections/functions/index.ok.html +0 -1
  168. package/public/sections/functions/modals/function-modal.ok.js +0 -1
  169. package/public/sections/functions/parts/function-detail.ok.js +0 -1
  170. package/public/sections/functions/parts/function-logs.ok.js +0 -1
  171. package/public/sections/functions/parts/function-runner.ok.js +0 -1
  172. package/public/sections/license/index.ok.html +0 -1
  173. package/public/sections/license/parts/license-panel.ok.js +0 -1
  174. package/public/sections/queue/index.ok.html +0 -1
  175. package/public/sections/queue/modals/create-bucket-modal.ok.js +0 -1
  176. package/public/sections/queue/modals/edit-bucket-modal.ok.js +0 -1
  177. package/public/sections/queue/modals/edit-job-modal.ok.js +0 -1
  178. package/public/sections/queue/modals/enqueue-job-modal.ok.js +0 -1
  179. package/public/sections/queue/modals/job-type-modal.ok.js +0 -1
  180. package/public/sections/queue/parts/job-log-panel.ok.js +0 -1
  181. package/public/sections/queue/parts/ok-job-form.ok.js +0 -1
  182. package/public/sections/queue/parts/queue-buckets.ok.js +0 -1
  183. package/public/sections/queue/parts/queue-job-types.ok.js +0 -1
  184. package/public/sections/queue/parts/queue-jobs.ok.js +0 -1
  185. package/public/sections/queue/parts/queue-types.ok.js +0 -1
  186. package/public/sections/queue/parts/queue-workers.ok.js +0 -1
  187. package/public/sections/sync/index.ok.html +0 -5
  188. package/public/sections/sync/parts/sync-node-detail.ok.js +0 -1
  189. package/public/sections/system/index.ok.html +0 -1
  190. package/public/sections/system/parts/system-api-panel.ok.js +0 -1
  191. package/public/sections/system/parts/system-auth-panel.ok.js +0 -1
  192. package/public/sections/system/parts/system-log.ok.js +0 -1
  193. package/public/sections/system/parts/system-processing-panel.ok.js +0 -1
  194. package/public/sections/system/parts/system-status.ok.js +0 -1
  195. package/public/sections/system/parts/system-storage-panel.ok.js +0 -1
  196. package/public/sections/system/parts/system-sync-panel.ok.js +0 -1
  197. package/public/sections/views/index.ok.html +0 -1
  198. package/public/sections/views/modals/create-view-modal.ok.js +0 -1
  199. package/public/sections/views/parts/views-list.ok.js +0 -1
  200. package/public/sections/views/parts/views-overview.ok.js +0 -1
  201. package/public/stores/functions-store.js +0 -1
  202. /package/public/{sections → core}/db/parts/tm-timeline.ok.js +0 -0
  203. /package/public/{util → core/db}/sift-schema.js +0 -0
  204. /package/public/{view-models → core/db}/type-query-vm.js +0 -0
  205. /package/public/{sections → core}/system/modals/data-ops-modal.ok.js +0 -0
  206. /package/public/{sections → core}/system/parts/system-locks-panel.ok.js +0 -0
  207. /package/public/{sections → core}/system/parts/system-log-history.ok.js +0 -0
  208. /package/public/{sections → core}/system/parts/system-process-panel.ok.js +0 -0
  209. /package/public/{sections → core}/system/parts/system-runtime-overview.ok.js +0 -0
  210. /package/public/{sections → features}/api/parts/api-endpoint.ok.js +0 -0
  211. /package/public/{sections → features}/auth/modals/create-token-modal.ok.js +0 -0
  212. /package/public/{sections → features}/auth/modals/edit-token-modal.ok.js +0 -0
  213. /package/public/{sections → features}/auth/parts/token-detail.ok.js +0 -0
  214. /package/public/{sections → features}/auth/parts/token-list.ok.js +0 -0
  215. /package/public/{sections → features}/files/parts/files-blob-status.ok.js +0 -0
  216. /package/public/{sections → features}/files/parts/files-overview.ok.js +0 -0
  217. /package/public/{sections → features}/functions/parts/functions-list.ok.js +0 -0
  218. /package/public/{sections/engines → features/pipelines}/engine-panel-shared-style.js +0 -0
  219. /package/public/{sections/embeddings → features/pipelines}/modals/register-model-modal.ok.js +0 -0
  220. /package/public/{sections/embeddings → features/pipelines}/modals/replace-embedder-modal.ok.js +0 -0
  221. /package/public/{sections/embeddings → features/pipelines}/parts/embed-model-catalog.ok.js +0 -0
  222. /package/public/{sections/engines → features/pipelines}/parts/engine-lifecycle-actions.ok.js +0 -0
  223. /package/public/{sections/engines → features/pipelines}/parts/engine-panel-shell.ok.js +0 -0
  224. /package/public/{sections/engines → features/pipelines}/parts/engine-resource-card.ok.js +0 -0
  225. /package/public/{sections/engines → features/pipelines}/parts/engine-row-item.ok.js +0 -0
  226. /package/public/{sections/embeddings → features/pipelines}/parts/pipeline-try-search.ok.js +0 -0
  227. /package/public/{sections → features}/queue/parts/code-panel.ok.js +0 -0
  228. /package/public/{sections → features}/sync/modals/connect-peer-modal.ok.js +0 -0
  229. /package/public/{sections → features}/sync/parts/sync-retention.ok.js +0 -0
  230. /package/public/{sections → features}/sync/parts/sync-topology.ok.js +0 -0
  231. /package/public/{sections → features}/views/parts/view-detail.ok.js +0 -0
  232. /package/public/{sections/db → shell}/modals/create-env-modal.ok.js +0 -0
  233. /package/public/{sections/db → shell}/modals/demo-env-modal.ok.js +0 -0
  234. /package/public/{sections/db → shell}/modals/remove-env-modal.ok.js +0 -0
@@ -146,7 +146,8 @@ Pull-based, last-write-wins replication over HTTP; no coordinator, no consensus.
146
146
  ```
147
147
 
148
148
  Each node has a persistent `nodeId` (`__identity`) and an Ed25519 keypair (`__nodePrivateKey` /
149
- `__nodePublicKey`), generated on first open and never replicated. Peers are recorded in the
149
+ `__nodePublicKey`), generated on first use (the first sync operation that needs it — a join, a
150
+ signed request, the node's own `~sync_nodes` record) and never replicated. Peers are recorded in the
150
151
  `~sync_nodes` type in `~system` — with their address and **public key** — which itself replicates,
151
152
  so the mesh forms from one join.
152
153
 
@@ -40,9 +40,14 @@ These are global only (per-env grants cannot hold them).
40
40
  | Namespace | Operations | What it covers |
41
41
  | --------- | ----------------------- | ----------------------------------------------------------------------- |
42
42
  | `auth` | `read`, `write` | API tokens and users |
43
- | `system` | `read`, `write` | Server info, logs, events, processor status, backups, engines |
43
+ | `system` | `read`, `write` | Server info, logs, events, processor status, backups, engines, licence |
44
44
  | `sync` | `read`, `write`, `peer` | Cluster replication — `peer` is for machine-to-machine only (see below) |
45
45
 
46
+ The `/api/env/:env/engines…` routes are `system:*` (so a grant must be global — see above) but
47
+ still declare `scope: 'env'`: a token needs `system:write` to manage engines anywhere, and
48
+ additionally `protected:write` to manage them in a `~`-prefixed env such as `~system` (see
49
+ [The `protected` gate](#the-protected-gate)). `ttl` routes follow the same pattern.
50
+
46
51
  ### Gate
47
52
 
48
53
  | Namespace | Operations | What it covers |
@@ -86,10 +91,13 @@ Shape of a token with per-env grants:
86
91
  The token can list tokens globally, read records in `production`, and do anything data-related in `staging`.
87
92
 
88
93
  :::note
89
- Per-env grants are only consulted on routes that resolve their env from the path — records,
90
- types, indexes, TTL, views, subscriptions and env admin. The files, queue, FTS and functions
91
- routes currently check **global** `permissions` only, so a per-env `files:read` /
92
- `queue:work` / `functions:run` / `search:query` grant does not open them — grant those globally.
94
+ Per-env grants are only consulted on routes that resolve their env from the path (the route
95
+ declares `access.scope: 'env'` or `{ env: 'params.env' }`). That covers every env-scoped
96
+ feature — records, types/indexes/TTL/FTS/vector, views, subscriptions, env admin, files, queue,
97
+ functions, pipelines and time machine — so e.g. a per-env `files:read` or `queue:work` grant
98
+ works exactly like a per-env `data:read` grant. `tests/auth-routes-have-permissions.test.js`
99
+ fails the build for any `:env` route whose permission is missing that declaration, so this list
100
+ stays accurate.
93
101
  :::
94
102
 
95
103
  ## The `protected` gate
@@ -110,6 +118,24 @@ is only applied on routes that resolve their env from the path (see the note abo
110
118
 
111
119
  `sync:peer` gates the cluster replication endpoints. A node holding this token can receive replicated data for any namespace. **Do not grant it to human users.**
112
120
 
121
+ ## Time machine, licence, and setup permissions
122
+
123
+ - **Time machine** (`/api/env/:env/time-machine…`) is env-scoped (`scope: 'env'`), so per-env
124
+ grants and the protected `~` env gate both apply:
125
+ - Status/config reads (`GET …/time-machine`, `…/types`, `…/types/:type`, the deprecated
126
+ `…/status`) need `schema:read` — the same permission as index/FTS status.
127
+ - Document history, point-in-time state, and diffs (`…/:type/:key/history`, `…/:type/:key/at/:clock`,
128
+ `…/changes`) are document contents, so they need `data:read`.
129
+ - Enabling/disabling/dropping/flushing tracking (`…/types/:type/enable|disable|drop|flush`,
130
+ `…/enable-all`, `…/disable-all`, incl. the deprecated per-env `…/enable`/`…/disable`) needs
131
+ `schema:write` — the same permission as registering or dropping an index.
132
+ - **Licence** (`/admin/license…`) is a `system:*` namespace, so it is global only:
133
+ `GET /admin/license/status` needs `system:read`; `POST /admin/license`,
134
+ `POST /admin/license/:id/activate` and `DELETE /admin/license/:id` need `system:write`.
135
+ - **Setup join** (`POST /admin/setup/join`) needs `sync:write` — joining starts replicating this
136
+ node's data to the remote, so it is cluster configuration like `PUT /api/sync/links`. First-run
137
+ setup in open/bootstrap mode bypasses the permission check (there is no token yet).
138
+
113
139
  ## Cookbook: common token recipes
114
140
 
115
141
  ### Read-only browser client
@@ -171,13 +171,13 @@ Async (`single`/`fanout`) handlers receive change-log records:
171
171
 
172
172
  ### originMode
173
173
 
174
- `origin` is the id of the OKDB instance that made the write (every process has its own, even on the same path).
174
+ `origin` is the identity of the store that made the write — every process on the same path shares it, so `'remote'` means writes synced in from other stores. A cloned store counts its history from before the copy as its own. The filter applies to inline and async processors alike (live drains, log replay, and the snapshot bootstrap); a drain chunk that is filtered out entirely still advances the cursor.
175
175
 
176
- | Value | Fires when |
177
- | ---------- | ----------------------------------------------------------------- |
178
- | `'self'` | **Default.** Only writes made by this OKDB instance |
179
- | `'remote'` | Only writes made by other instances (other processes, sync peers) |
180
- | `'all'` | Any write |
176
+ | Value | Fires when |
177
+ | ---------- | --------------------------------------------- |
178
+ | `'self'` | **Default.** Only writes made by this store |
179
+ | `'remote'` | Only writes made by other stores (sync peers) |
180
+ | `'all'` | Any write |
181
181
 
182
182
  This is useful for reacting differently to local vs. replicated changes — e.g., sending a notification only when your own node creates a record, not when it receives one from a peer.
183
183
 
package/docs/licensing.md CHANGED
@@ -180,13 +180,18 @@ A missing or invalid file is a warning naming the error code — never the file'
180
180
  ## Node identity, reset, and moving a database
181
181
 
182
182
  The activation is bound to the node identity created when the database was first initialized.
183
- Two consequences:
183
+ Consequences:
184
184
 
185
185
  - **`okdb reset` preserves your license.** The reset command deliberately carries the node
186
186
  identity and all stored licenses into the fresh database, so activation survives.
187
187
  - **Deleting the data directory by hand does not.** A new directory means a new node identity;
188
188
  previously activated licenses show `wrong_node` and need a re-activation from your vendor
189
189
  (add the license again, send the new PIN).
190
+ - **A clone is a new node.** `OKDB.clone()`, `OKDB.reidentify()` and
191
+ `okdb restore --new-identity` give the copy its own identity: its licence records come along,
192
+ but a node-bound activation shows `wrong_node` there until the copy is activated with its own
193
+ PIN. The source keeps its activation. See
194
+ [Cloning a store](migration.md#cloning-a-store-new-identity).
190
195
 
191
196
  The same applies to copying a license between machines: the blob can be added anywhere, but
192
197
  each node needs its own activation.
package/docs/migration.md CHANGED
@@ -5,6 +5,7 @@ OKDB's migration feature handles export/import of all types and records as JSON
5
5
  | You want | Use |
6
6
  | ----------------------------------------- | --------------------------------------------------------------------------------- |
7
7
  | a restorable copy of the whole store | [`migrate.backup(dir)`](#hot-backup-and-restore) / `okdb backup` + `okdb restore` |
8
+ | a copy that is a store of its own | [`OKDB.clone(src, dst)`](#cloning-a-store-new-identity) / `OKDB.reidentify(dir)` |
8
9
  | portable, version-independent data | [`migrate.exportToFile()`](#streaming-export-ndjson) / `okdb export` |
9
10
  | a small in-memory snapshot (tests, demos) | `migrate.export()` / `exportJSON()` |
10
11
 
@@ -196,7 +197,9 @@ overwrites. The result directory **is a store**: open OKDB on it (or on a copy o
196
197
  `okdb restore <backup> --path <target>` copies it into a new/empty target. Things to know:
197
198
 
198
199
  - The restored store has the **same node identity** as the source. Do not run it as a sync
199
- peer alongside the original — restore replaces a node, it does not clone one.
200
+ peer alongside the original — restore replaces a node, it does not clone one. To bring the
201
+ copy up as a **new** node instead, use `okdb restore <backup> --path <target> --new-identity`
202
+ (or `OKDB.reidentify(target)`) — see [Cloning a store](#cloning-a-store-new-identity).
200
203
  - An env registered with an absolute path _outside_ the store (legacy / hand-edited records
201
204
  only; `createEnvironment` always places envs under the root) is copied to its default
202
205
  placement and flagged `outOfTree` in the manifest; while its original path still exists,
@@ -216,6 +219,62 @@ new OKDB('/data/okdb', { backup: { root: '/mnt/backups/okdb' } });
216
219
 
217
220
  ---
218
221
 
222
+ ## Cloning a store (new identity)
223
+
224
+ Every store has a node identity (`db.id`), created on its first open. A byte copy keeps it, so
225
+ two copies of one store behave as one node: they join the same bus group (and receive each
226
+ other's change and compaction signals), share the sync node id, keypair and token secret, and
227
+ match the same licence activation. Use `OKDB.clone()` whenever a copy must be a store of its
228
+ own — test templates, a staging copy, a backup brought up next to the original.
229
+
230
+ ```javascript
231
+ await OKDB.clone('/data/template', '/tmp/test-42'); // → { path, files, bytes, ms }
232
+ const db = new OKDB('/tmp/test-42');
233
+ await db.open(); // the first open gives the copy its own identity
234
+ db.id; // new UUID
235
+ db.formerIds; // [ '<template id>' ]
236
+ ```
237
+
238
+ - **The source must be closed** by every process. A copy of a running store is not consistent
239
+ across its LMDB files, so `clone()` refuses with `STORE_IN_USE` when a process has the store
240
+ open (detected on Linux from the LMDB file locks; on other platforms this is your
241
+ responsibility). For a store that must stay up, take a [hot backup](#hot-backup-and-restore)
242
+ and re-identify it: `await db.migrate.backup(dir); OKDB.reidentify(dir)`.
243
+ - `dstPath` must not exist or be empty (`CLONE_DEST_NOT_EMPTY`), and must not overlap the source.
244
+ - **Everything is copied** — every env, `~fts`, time-machine history, `~vectors`, `~blobs`, views,
245
+ indexes and processor cursors — **except** LMDB `lock.mdb` files, the disposable `~processes`
246
+ env, upload staging (`~blobs/tmp`) and compaction leftovers. The copy is fast (a file copy,
247
+ reflinked where the filesystem supports it: a small store clones in a few milliseconds) and
248
+ opens with every index, view and search index ready — nothing is rebuilt or reprocessed.
249
+ - Envs are expected to live under the store directory (the default). If an env was registered at
250
+ an absolute path outside it and that directory exists, the copy's first open refuses with
251
+ `REIDENTIFY_OUT_OF_TREE_ENV` instead of opening the source's files.
252
+
253
+ **`OKDB.reidentify(path)`** marks a closed copy you made yourself (`cp -r`, a restored backup) to
254
+ take a new identity on its next open — the same step `clone()` ends with. It only writes a small
255
+ marker file (`okdb-reidentify.json`) at the store root; the store's **first open** applies it,
256
+ exactly once, even when several processes open the copy at the same time. Marking is idempotent,
257
+ and a marked copy that is copied again before it is opened still gives each copy its own identity.
258
+ A store that has already been opened under its new identity is an ordinary store: copying it
259
+ again needs another `clone()` / `reidentify()`.
260
+
261
+ **What the first open changes** — and what it keeps:
262
+
263
+ | Area | On the copy |
264
+ | ---------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
265
+ | Identity | a new `db.id`; the old one is kept in `db.formerIds`. The bus group (`okdb-<id>`) follows the id, so the copy and the source never hear each other. |
266
+ | Secrets | a new token secret (sessions signed by the source stop validating, unless you set `auth.token.secret` yourself) and a new sync keypair (minted on first sync use). Stored users and API tokens are data and are copied. |
267
+ | Sync | not a member of anything: no `~sync_nodes` / `~sync_links` rows, no peer cursors. They are removed from the copy's changelog too, so nothing it later sends can delete them in the source's cluster. `db.sync.join(...)` makes it a new node, which pulls the topology back. |
268
+ | Licences | records are kept. A node-bound (standard) activation shows `wrong_node` on the copy; activate it again with the copy's own PIN. Open and internal licences, and `OKDB_LICENSE_FILE` installs of them, keep working. |
269
+ | Processes | leases, want-markers, the migration lock and process-keyed control rows are dropped — they belonged to the source's processes. |
270
+ | Changelog | kept as-is, origins included. Changes stamped with a former identity up to the point the copy was made count as the copy's own (`originMode: 'self'`); a former identity's later changes — the source's, synced in — are remote. |
271
+ | Data and derived | untouched: documents, indexes, views, FTS, time machine, vectors, files, queue jobs and every processor cursor. |
272
+
273
+ Queue jobs that were claimed when the source closed are recovered by the normal claim-timeout
274
+ path, as after a restart.
275
+
276
+ ---
277
+
219
278
  ## Logical backup workflow
220
279
 
221
280
  A version-independent alternative to the hot backup (slower; re-derives indexes on import):
package/docs/plugins.md CHANGED
@@ -75,7 +75,7 @@ Plugin name must not conflict with existing OKDB properties (`env`, `put`, `get`
75
75
  | `instance.start(okdb)` | async | **yes** | Start servers, timers, workers |
76
76
  | `instance.stop(okdb)` | async | **yes** | Stop everything started in `start()` |
77
77
 
78
- `start`/`stop` are optional. Plugins start in registration order at the end of `open()` and stop in reverse order during `close()`. If any plugin's `start()` throws, all already-started plugins are stopped in reverse order before the error propagates.
78
+ `start`/`stop` are optional. Plugins start in registration order at the end of `open()` and stop in reverse order during `close()`. If any plugin's `start()` throws, all already-started plugins are stopped in reverse order (the throwing plugin's `stop()` is not called), then `open()` releases everything else it had opened and rethrows the error; the instance is left stopped, so `close()` is a no-op.
79
79
 
80
80
  ---
81
81
 
@@ -82,7 +82,7 @@ Both are called as `fn(ctx, changes, info)`. A bare closure (`handler: async (ch
82
82
 
83
83
  `changes` are change-log records (`{ type, key, action, clock, origin, ... }`). With the default `hydrateValues: true`, `put` records also carry `.value` (the current document); otherwise read the document via `ctx.env.get(c.type, c.key)`.
84
84
 
85
- `originMode` filters by who wrote the change (compared to the change's `origin`, the writing OKDB instance id): `self` (default — this instance's writes only), `remote` (other instances/processes), or `all`. Async processors with `remote`/`all` also wake on cross-process writes via the bus.
85
+ `originMode` filters by who wrote the change (compared to the change's `origin`, the writing store's identity — shared by every process on the same path): `self` (default — this store's writes, including a cloned store's history from before the copy), `remote` (writes synced in from other stores), or `all`. It applies on every delivery path — inline, async live drains, log replay and the snapshot bootstrap (which judges each doc by its latest change). A drain chunk whose changes are all filtered out is not delivered, and the cursor still moves past it. A `quantum` module receives the mode as `payload.originMode` and applies it itself with `filterByOrigin` (`src/runtime/okdb-processor-origin.js`), as the built-in quanta do. Async processors with `remote`/`all` also wake on cross-process writes via the bus.
86
86
 
87
87
  **`rebuild` (snapshot-bootstrap processors).** With `bootstrap: 'snapshot'` you may pass `rebuild: async () => { ... }` — a feature-owned **full rebuild** (scan the actual docs, then reposition the cursor via `setCursor` + `markSnapshotComplete`). It runs whenever the processor bootstraps with an **un-positioned cursor**: after a cursor reset (the admin ⟲ "full reprocess"), or when a build handoff was interrupted by a crash. Without it, the framework's only option is replaying the changelog from 0 — which is **not** a rebuild: sync-GC prunes the changelog (its horizon ignores processor cursors), so replay-from-0 can silently yield a partial derived store. FTS registers one (re-scans all docs of the type into every index, clearing first).
88
88
 
@@ -230,10 +230,12 @@ Subscription state lives in a dedicated environment named `~sub`, opened `sync:f
230
230
  `onDelete: cascade`, and a reverse index on `[dataEnv, type, key]` for O(interested
231
231
  sessions) routing.
232
232
 
233
- Routing is lazy: a process routes changes only while it holds at least one open subscription
234
- connection (signals are only ever delivered to connections on the process that holds them).
235
- An embedded instance that never serves subscriptions does no per-change subscription work,
236
- and no change-feed is started on its behalf.
233
+ Everything is lazy. The `~sub` store and the signal router are built on a process's first
234
+ subscription use (its first SSE connection or control call), never at `open()` — a store that
235
+ never had a subscriber has no `~sub` env at all. Routing is lazy too: a process routes changes
236
+ only while it holds at least one open subscription connection (signals are only ever delivered
237
+ to connections on the process that holds them). An embedded instance that never serves
238
+ subscriptions does no per-change subscription work, and no change-feed is started on its behalf.
237
239
 
238
240
  The `~sub` _env_ name starts with `~` so its writes stay out of the data change-feed, while
239
241
  the non-`~` type names keep FK cascade and TTL working. `sync:false` keeps machine-bound
package/docs/views.md CHANGED
@@ -694,6 +694,10 @@ See [pipelines.md](./pipelines.md) for materializer documentation.
694
694
 
695
695
  View definitions are stored in the `~views` system type within their environment and sync to connected peers like any other document. On startup and after each sync reconcile pass, OKDB activates any view definitions that arrived from remote peers but are not yet live locally.
696
696
 
697
+ A view counts every document of its source types, whoever wrote it: documents synced in from a peer are maintained live exactly like local writes — by sync and async views, `$ref` sections and live map `$ref`s alike — so a view's live result always equals what a rebuild of the same data gives. Up to okdb 2.4.0 the sync-mode handlers (sync views, and every view's `$ref` / live map `$ref` sections) skipped remote-origin writes. View storage version 7 repairs that once, on first open: on a store that has or had a sync peer (a peer in `~sync_nodes`, a peer sync cursor, a sync link, or a cloned / re-identified store) those views rebuild; everywhere else — pure async views, and every view on a store that never synced — the version is restamped without a rebuild, since nothing can be missing. One info line (`views storage v7: …`) logs the decision and its reason.
698
+
699
+ The one case the upgrade cannot see: writes stamped with an explicit foreign `origin` (`put(..., { origin })`) on a store that never synced. If a sync-mode view was fed such writes before the upgrade, repair it with `await env.views.rebuild(name)`.
700
+
697
701
  ---
698
702
 
699
703
  ## Error codes