@dikolab/kbdb 0.4.3 → 0.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 (224) hide show
  1. package/CLA.md +68 -0
  2. package/LICENSE +676 -12
  3. package/LICENSING.md +58 -0
  4. package/README.md +50 -27
  5. package/README.md.bak +320 -0
  6. package/dist/README.md +320 -0
  7. package/dist/{chunk-BJXEVALU.mjs → chunk-QLJ33C74.mjs} +44 -44
  8. package/dist/chunk-QLJ33C74.mjs.map +7 -0
  9. package/dist/cli.cjs +550 -232
  10. package/dist/cli.cjs.map +4 -4
  11. package/dist/cli.d.ts +0 -8
  12. package/dist/cli.mjs +483 -154
  13. package/dist/cli.mjs.map +4 -4
  14. package/dist/kbdb-worker.cjs +36 -5
  15. package/dist/kbdb-worker.cjs.map +3 -3
  16. package/dist/mod.cjs +89 -30
  17. package/dist/mod.cjs.map +4 -4
  18. package/dist/mod.mjs +88 -30
  19. package/dist/mod.mjs.map +4 -4
  20. package/dist/src/cli.d.ts +0 -8
  21. package/dist/src/shared/cli/constants/defaults.constant.d.ts +1 -1
  22. package/dist/src/shared/cli/constants/recfile-command-field-map.constant.d.ts +8 -0
  23. package/dist/src/shared/cli/constants/recfile-field-maps.constant.d.ts +21 -0
  24. package/dist/src/shared/cli/functions/compute-directory-level.function.d.ts +14 -0
  25. package/dist/src/shared/cli/functions/dispatch-command.function.d.ts +2 -28
  26. package/dist/src/shared/cli/functions/format-command-output.function.d.ts +14 -0
  27. package/dist/src/shared/cli/functions/format-db-not-found.function.d.ts +20 -0
  28. package/dist/src/shared/cli/functions/format-output.function.d.ts +1 -0
  29. package/dist/src/shared/cli/functions/format-recfile-output.function.d.ts +11 -0
  30. package/dist/src/shared/cli/functions/render-recfile-value.function.d.ts +11 -0
  31. package/dist/src/shared/cli/functions/resolve-db-dir.function.d.ts +24 -0
  32. package/dist/src/shared/cli/functions/resolve-recfile-key.function.d.ts +9 -0
  33. package/dist/src/shared/cli/functions/run-db-path.function.d.ts +10 -0
  34. package/dist/src/shared/cli/functions/run-export.function.d.ts +8 -4
  35. package/dist/src/shared/cli/functions/run-import.function.d.ts +4 -0
  36. package/dist/src/shared/cli/functions/run-learn.function.d.ts +12 -0
  37. package/dist/src/shared/cli/index.d.ts +4 -1
  38. package/dist/src/shared/cli/typings/cli-options.interface.d.ts +9 -2
  39. package/dist/src/shared/cli/typings/db-resolution.interface.d.ts +9 -0
  40. package/dist/src/shared/cli/typings/recfile-field-map.type.d.ts +10 -0
  41. package/dist/src/shared/version/constants/version.constant.d.ts +1 -1
  42. package/dist/src/shared/wasm-codec/functions/decode-option-u8.function.d.ts +9 -0
  43. package/dist/src/shared/wasm-codec/functions/decode-section-records.function.d.ts +4 -0
  44. package/dist/src/shared/wasm-codec/functions/encode-add-section-params.function.d.ts +2 -45
  45. package/dist/src/shared/wasm-codec/functions/encode-update-section-params.function.d.ts +10 -1
  46. package/dist/src/shared/wasm-codec/index.d.ts +1 -1
  47. package/dist/src/shared/wasm-codec/typings/add-section-input.model.d.ts +6 -0
  48. package/dist/src/shared/wasm-codec/typings/section-record-trailing.model.d.ts +5 -0
  49. package/dist/src/shared/wasm-codec/typings/section-record.model.d.ts +5 -0
  50. package/dist/src/shared/worker-client/functions/build-deno-permissions.function.d.ts +11 -0
  51. package/dist/src/shared/worker-client/functions/read-crash-reason.function.d.ts +9 -0
  52. package/dist/src/shared/worker-client/functions/resolve-spawn-script.function.d.ts +6 -0
  53. package/dist/src/shared/worker-client/functions/spawn-daemon.function.d.ts +1 -1
  54. package/dist/src/shared/worker-client/typings/section.model.d.ts +5 -0
  55. package/dist/src/shared/worker-client/typings/worker-client-options.interface.d.ts +0 -6
  56. package/dist/src/shared/worker-daemon/functions/count-files.function.d.ts +12 -0
  57. package/dist/src/shared/worker-daemon/functions/handle-import.function.d.ts +2 -0
  58. package/dist/src/shared/worker-daemon/functions/handle-read-ops.function.d.ts +8 -0
  59. package/dist/src/shared/worker-daemon/functions/validate-daemon-path.function.d.ts +15 -0
  60. package/dist/src/worker.d.ts +0 -7
  61. package/dist/wasm/default-embedding/kbdb_default_embedding_bg.wasm +0 -0
  62. package/dist/wasm/default-embedding/package.json +1 -1
  63. package/dist/wasm/fs-database/kbdb_fs_database_bg.wasm +0 -0
  64. package/dist/wasm/fs-database/package.json +1 -1
  65. package/dist/wasm/fs-migration/kbdb_fs_migration_bg.wasm +0 -0
  66. package/dist/wasm/fs-migration/package.json +1 -1
  67. package/dist/wasm/kb-worker/kbdb_worker_bg.wasm +0 -0
  68. package/dist/wasm/kb-worker/package.json +1 -1
  69. package/dist/wasm/query-parser/kbdb_query_parser_bg.wasm +0 -0
  70. package/dist/wasm/query-parser/package.json +1 -1
  71. package/dist/worker.d.ts +0 -7
  72. package/dist/worker.mjs +37 -6
  73. package/dist/worker.mjs.map +3 -3
  74. package/package.json +13 -12
  75. package/dist/chunk-BJXEVALU.mjs.map +0 -7
  76. package/dist/src/shared/cli/functions/resolve-raw-db-path.function.d.ts +0 -10
  77. package/docs/details/README.md +0 -30
  78. package/docs/details/agent-tooling.md +0 -132
  79. package/docs/details/cli.md +0 -777
  80. package/docs/details/knowledge-base.md +0 -180
  81. package/docs/details/library-api.md +0 -495
  82. package/docs/details/mcp-server.md +0 -482
  83. package/docs/details/search-and-ranking.md +0 -575
  84. package/docs/details/storage.md +0 -531
  85. package/docs/goals/agents.md +0 -751
  86. package/docs/goals/architecture.svg +0 -198
  87. package/docs/goals/auto-capture.md +0 -378
  88. package/docs/goals/benchmarking.md +0 -296
  89. package/docs/goals/cli.md +0 -2654
  90. package/docs/goals/concurrency.md +0 -259
  91. package/docs/goals/content-composer.md +0 -609
  92. package/docs/goals/content-parser.md +0 -279
  93. package/docs/goals/database.md +0 -1679
  94. package/docs/goals/document.md +0 -368
  95. package/docs/goals/hybrid-search.md +0 -465
  96. package/docs/goals/mcp.md +0 -1541
  97. package/docs/goals/overview.md +0 -124
  98. package/docs/goals/query-parser.md +0 -373
  99. package/docs/goals/query-result.md +0 -860
  100. package/docs/goals/semantic-dedup.md +0 -233
  101. package/docs/goals/skills.md +0 -810
  102. package/docs/goals/sync.md +0 -217
  103. package/docs/goals/worker-client.md +0 -1005
  104. package/docs/goals/worker-daemon.md +0 -1547
  105. package/docs/modules/overview.md +0 -319
  106. package/docs/modules/rust/embedding/module.md +0 -91
  107. package/docs/modules/rust/fs-database/functions/document.md +0 -48
  108. package/docs/modules/rust/fs-database/functions/integrity.md +0 -79
  109. package/docs/modules/rust/fs-database/functions/io.md +0 -87
  110. package/docs/modules/rust/fs-database/functions/module.md +0 -22
  111. package/docs/modules/rust/fs-database/functions/query-exec.md +0 -44
  112. package/docs/modules/rust/fs-database/functions/section.md +0 -76
  113. package/docs/modules/rust/fs-database/indexes/btree.md +0 -35
  114. package/docs/modules/rust/fs-database/indexes/corpus.md +0 -61
  115. package/docs/modules/rust/fs-database/indexes/inverted.md +0 -60
  116. package/docs/modules/rust/fs-database/indexes/module.md +0 -64
  117. package/docs/modules/rust/fs-database/indexes/positional.md +0 -39
  118. package/docs/modules/rust/fs-database/indexes/vectors.md +0 -110
  119. package/docs/modules/rust/fs-database/module.md +0 -150
  120. package/docs/modules/rust/fs-database/types/catalog.md +0 -53
  121. package/docs/modules/rust/fs-database/types/module.md +0 -12
  122. package/docs/modules/rust/fs-database/types/section.md +0 -127
  123. package/docs/modules/rust/kb-worker/functions/compose.md +0 -57
  124. package/docs/modules/rust/kb-worker/functions/module.md +0 -20
  125. package/docs/modules/rust/kb-worker/functions/retrieve.md +0 -194
  126. package/docs/modules/rust/kb-worker/functions/search.md +0 -173
  127. package/docs/modules/rust/kb-worker/functions/write.md +0 -172
  128. package/docs/modules/rust/kb-worker/module.md +0 -171
  129. package/docs/modules/rust/kb-worker/types/cache.md +0 -90
  130. package/docs/modules/rust/kb-worker/types/context.md +0 -26
  131. package/docs/modules/rust/kb-worker/types/module.md +0 -16
  132. package/docs/modules/rust/kb-worker/types/status.md +0 -29
  133. package/docs/modules/rust/kb-worker/types/token.md +0 -50
  134. package/docs/modules/rust/overview.md +0 -531
  135. package/docs/modules/rust/query-parser/functions/extract.md +0 -56
  136. package/docs/modules/rust/query-parser/functions/module.md +0 -18
  137. package/docs/modules/rust/query-parser/functions/parse.md +0 -33
  138. package/docs/modules/rust/query-parser/functions/pipeline.md +0 -9
  139. package/docs/modules/rust/query-parser/functions/rank.md +0 -85
  140. package/docs/modules/rust/query-parser/module.md +0 -131
  141. package/docs/modules/rust/query-parser/types/cache.md +0 -39
  142. package/docs/modules/rust/query-parser/types/keyphrase.md +0 -71
  143. package/docs/modules/rust/query-parser/types/module.md +0 -18
  144. package/docs/modules/rust/query-parser/types/token.md +0 -46
  145. package/docs/modules/rust/shared/content-composer/functions.md +0 -114
  146. package/docs/modules/rust/shared/content-composer/module.md +0 -147
  147. package/docs/modules/rust/shared/content-composer/types.md +0 -93
  148. package/docs/modules/rust/shared/content-parser/functions.md +0 -72
  149. package/docs/modules/rust/shared/content-parser/module.md +0 -99
  150. package/docs/modules/rust/shared/content-parser/types.md +0 -71
  151. package/docs/modules/rust/shared/corpus/module.md +0 -24
  152. package/docs/modules/rust/shared/corpus/types.md +0 -141
  153. package/docs/modules/rust/shared/document/module.md +0 -25
  154. package/docs/modules/rust/shared/document/types.md +0 -154
  155. package/docs/modules/rust/shared/encode/module.md +0 -22
  156. package/docs/modules/rust/shared/encode/traits.md +0 -304
  157. package/docs/modules/rust/shared/error/module.md +0 -28
  158. package/docs/modules/rust/shared/error/types.md +0 -302
  159. package/docs/modules/rust/shared/kbid/module.md +0 -24
  160. package/docs/modules/rust/shared/kbid/types.md +0 -147
  161. package/docs/modules/rust/shared/memory/functions.md +0 -209
  162. package/docs/modules/rust/shared/memory/module.md +0 -24
  163. package/docs/modules/rust/shared/module.md +0 -196
  164. package/docs/modules/rust/shared/pipeline/functions.md +0 -141
  165. package/docs/modules/rust/shared/pipeline/module.md +0 -62
  166. package/docs/modules/rust/shared/pipeline/types.md +0 -43
  167. package/docs/modules/rust/shared/query/module.md +0 -27
  168. package/docs/modules/rust/shared/query/types.md +0 -236
  169. package/docs/modules/rust/shared/section/module.md +0 -25
  170. package/docs/modules/rust/shared/section/types.md +0 -294
  171. package/docs/modules/rust/shared/term/module.md +0 -25
  172. package/docs/modules/rust/shared/term/types.md +0 -139
  173. package/docs/modules/typescript/cli.md +0 -131
  174. package/docs/modules/typescript/kbdb-worker.md +0 -150
  175. package/docs/modules/typescript/overview.md +0 -400
  176. package/docs/modules/typescript/shared/auto-capture/module.md +0 -73
  177. package/docs/modules/typescript/shared/cli/constants.md +0 -23
  178. package/docs/modules/typescript/shared/cli/functions.md +0 -809
  179. package/docs/modules/typescript/shared/cli/module.md +0 -138
  180. package/docs/modules/typescript/shared/cli/typings.md +0 -197
  181. package/docs/modules/typescript/shared/embedding/functions.md +0 -200
  182. package/docs/modules/typescript/shared/embedding/module.md +0 -77
  183. package/docs/modules/typescript/shared/embedding/typings.md +0 -124
  184. package/docs/modules/typescript/shared/hash/functions.md +0 -20
  185. package/docs/modules/typescript/shared/hash/module.md +0 -21
  186. package/docs/modules/typescript/shared/log/constants.md +0 -82
  187. package/docs/modules/typescript/shared/log/functions.md +0 -131
  188. package/docs/modules/typescript/shared/log/module.md +0 -153
  189. package/docs/modules/typescript/shared/log/typings.md +0 -82
  190. package/docs/modules/typescript/shared/mcp/classes.md +0 -179
  191. package/docs/modules/typescript/shared/mcp/functions.md +0 -386
  192. package/docs/modules/typescript/shared/mcp/module.md +0 -160
  193. package/docs/modules/typescript/shared/mcp/typings.md +0 -529
  194. package/docs/modules/typescript/shared/module.md +0 -110
  195. package/docs/modules/typescript/shared/platform/functions.md +0 -42
  196. package/docs/modules/typescript/shared/platform/module.md +0 -25
  197. package/docs/modules/typescript/shared/runtime/functions.md +0 -26
  198. package/docs/modules/typescript/shared/runtime/module.md +0 -27
  199. package/docs/modules/typescript/shared/runtime/typings.md +0 -13
  200. package/docs/modules/typescript/shared/version/module.md +0 -27
  201. package/docs/modules/typescript/shared/wasm-codec/functions.md +0 -391
  202. package/docs/modules/typescript/shared/wasm-codec/module.md +0 -98
  203. package/docs/modules/typescript/shared/worker-client/classes.md +0 -264
  204. package/docs/modules/typescript/shared/worker-client/functions.md +0 -175
  205. package/docs/modules/typescript/shared/worker-client/module.md +0 -92
  206. package/docs/modules/typescript/shared/worker-client/typings.md +0 -511
  207. package/docs/modules/typescript/shared/worker-daemon/classes.md +0 -239
  208. package/docs/modules/typescript/shared/worker-daemon/constants.md +0 -37
  209. package/docs/modules/typescript/shared/worker-daemon/functions.md +0 -234
  210. package/docs/modules/typescript/shared/worker-daemon/module.md +0 -118
  211. package/docs/modules/typescript/shared/worker-daemon/typings.md +0 -132
  212. package/docs/overview.md +0 -191
  213. package/docs/release-notes/0.1.0.md +0 -189
  214. package/docs/release-notes/0.1.1.md +0 -46
  215. package/docs/release-notes/0.1.2.md +0 -38
  216. package/docs/release-notes/0.1.3.md +0 -42
  217. package/docs/release-notes/0.2.0.md +0 -147
  218. package/docs/release-notes/0.3.0.md +0 -89
  219. package/docs/release-notes/0.3.1.md +0 -121
  220. package/docs/release-notes/0.3.2.md +0 -64
  221. package/docs/release-notes/0.4.0.md +0 -80
  222. package/docs/release-notes/0.4.1.md +0 -149
  223. package/docs/release-notes/0.4.3.md +0 -60
  224. package/docs/release-notes/README.md +0 -13
@@ -1,239 +0,0 @@
1
- # worker-daemon/classes -- WorkerDaemon
2
-
3
- *Core daemon class that loads WASM modules, manages
4
- the IPC server, routes requests, and handles lifecycle
5
- (startup, idle tracking, graceful shutdown).*
6
-
7
- ## Source
8
-
9
- `src/shared/worker-daemon/classes/`
10
-
11
- ## Classes
12
-
13
- ### `WorkerDaemon`
14
-
15
- **File:** `worker-daemon.class.ts`
16
-
17
- The main daemon class. Orchestrates WASM module
18
- loading, IPC server creation, request routing, idle
19
- timeout tracking, and graceful shutdown.
20
-
21
- #### Constructor
22
-
23
- ```ts
24
- class WorkerDaemon {
25
- constructor(
26
- ctx: string,
27
- targetDir: string,
28
- config: DaemonConfig,
29
- wasmDir?: string,
30
- );
31
- }
32
- ```
33
-
34
- | Parameter | Type | Description |
35
- |-------------|----------------|-------------------------|
36
- | `ctx` | `string` | Context ID (16-hex) |
37
- | `targetDir` | `string` | Absolute path to the |
38
- | | | `.kbdb` directory |
39
- | `config` | `DaemonConfig` | Parsed config from |
40
- | | | `worker.toml` |
41
- | `wasmDir` | `string?` | Path to WASM build |
42
- | | | output directory |
43
-
44
- #### Properties
45
-
46
- | Property | Type | Description |
47
- |-------------------|----------------|------------------------|
48
- | `ctx` | `string` | Context ID |
49
- | `targetDir` | `string` | Database directory |
50
- | `config` | `DaemonConfig` | Daemon config |
51
- | `connectionCount` | `number` | Active IPC connections |
52
-
53
- #### Public Methods
54
-
55
- ##### `checkPreconditions`
56
-
57
- ```ts
58
- checkPreconditions(): void;
59
- ```
60
-
61
- Validates startup preconditions before WASM module
62
- loading. Checks that:
63
-
64
- 1. Database directory exists.
65
- 2. Directory is readable and writable.
66
- 3. `catalog.toml` reports a compatible version.
67
-
68
- Throws on any failure. `startDaemon` catches the
69
- error, prints to stderr, and exits with code 1.
70
-
71
- ##### `start`
72
-
73
- ```ts
74
- async start(): Promise<void>;
75
- ```
76
-
77
- Startup sequence:
78
-
79
- 1. Calls `loadWasmModules(config, wasmDir)` to
80
- compile and instantiate the WASM modules.
81
- 2. Initializes the worker context.
82
- 3. Writes the PID file (`kbdb-{ctx}.pid`) to the
83
- OS temporary directory.
84
- 4. Calls `createIpcServer(ipcPath, handler)` to
85
- bind the IPC socket.
86
- 5. Begins listening for connections and resets the
87
- idle timer.
88
-
89
- ##### `stop`
90
-
91
- ```ts
92
- stop(): void;
93
- ```
94
-
95
- Graceful shutdown sequence:
96
-
97
- 1. Clears the idle timer.
98
- 2. Stops accepting new connections.
99
- 3. Purges WASM module references.
100
- 4. Removes the PID file.
101
- 5. Removes the IPC socket file.
102
- 6. Calls `process.exit(0)`.
103
-
104
- ##### `handleRequest`
105
-
106
- ```ts
107
- async handleRequest(
108
- request: IpcRequest,
109
- ): Promise<IpcResponse>;
110
- ```
111
-
112
- Handles a single JSON-RPC 2.0 request. Validates
113
- context, resets the idle timer, routes to the
114
- appropriate handler, and returns a response.
115
- Requests whose `ctx` param does not match the
116
- daemon's context ID are rejected with error
117
- code `-32001`.
118
-
119
- ##### `incrementConnections` / `decrementConnections`
120
-
121
- ```ts
122
- incrementConnections(): void;
123
- decrementConnections(): void;
124
- ```
125
-
126
- Adjust the active connection count. Called by the
127
- IPC server on connect/disconnect events.
128
-
129
- ##### `injectModulesForTesting`
130
-
131
- ```ts
132
- injectModulesForTesting(modules: WasmModules): void;
133
- ```
134
-
135
- Injects pre-built WASM modules for unit testing.
136
- Must not be called in production code.
137
-
138
- #### Private Behavior
139
-
140
- ##### Idle Timeout Tracking
141
-
142
- The daemon maintains an internal idle timer. The
143
- timer resets on every:
144
-
145
- - Client connection.
146
- - Client disconnection.
147
- - IPC request received.
148
-
149
- When the timer reaches `config.timeoutMs` with no
150
- active connections, the daemon calls `stop()`.
151
-
152
- ##### Connection Counting
153
-
154
- Tracks the number of active client connections. The
155
- daemon stays alive regardless of the idle timer as
156
- long as at least one connection is open.
157
-
158
- ##### Startup Preconditions
159
-
160
- Before WASM modules are loaded, the daemon checks
161
- three preconditions. Failure causes `startDaemon` to
162
- print the error to stderr and exit with code 1:
163
-
164
- 1. Database directory exists.
165
- 2. Directory is readable and writable.
166
- 3. `catalog.toml` version is compatible.
167
-
168
- ##### Request Routing
169
-
170
- Routes incoming JSON-RPC 2.0 requests. Daemon-state
171
- methods are handled inline; all other methods are
172
- looked up in `WASM_METHOD_MAP`:
173
-
174
- | IPC Method | Handler / WASM Export |
175
- |----------------------|--------------------------------|
176
- | `status` | daemon state (busy/idle) |
177
- | `idle_at` | last idle Unix timestamp |
178
- | `config_{key}` | `DaemonConfig` property lookup |
179
- | `checkVersion` | catalog version check |
180
- | `search` | `worker_search` |
181
- | `recall` | `worker_recall` |
182
- | `addSection` | `worker_add_section` |
183
- | `updateSection` | `worker_update_section` |
184
- | `removeSection` | `worker_remove_section` |
185
- | `readSections` | `worker_read_sections` |
186
- | `groupSections` | `worker_group_sections` |
187
- | `removeGrouping` | `worker_remove_grouping` |
188
- | `retrieveDocument` | `worker_retrieve_doc` |
189
- | `content` | `worker_compose` |
190
- | `integrityCheck` | `worker_check_integrity` |
191
- | `gc` | `worker_garbage_collect` |
192
- | `rebuildIndexes` | `worker_rebuild_indexes` |
193
- | `initDirectory` | `worker_init_directory` |
194
- | `dbStatus` | `worker_status` |
195
- | `invalidate` | `worker_invalidate` |
196
- | `invalidateAll` | `worker_invalidate_all` |
197
- | `get_result_status` | `worker_result_status` |
198
- | `get_result` | `worker_get_result` |
199
- | `query` | `worker_query` |
200
- | `listByType` / `list_by_type` | `worker_list_by_type` |
201
- | `listDocumentsByType` | `worker_list_docs_by_type` |
202
-
203
- Snake_case aliases (`add_section`, `remove_section`,
204
- etc.) are also accepted and map to the same exports.
205
-
206
- ##### Context Validation
207
-
208
- Every IPC request includes a `ctx` param. The daemon
209
- validates it against its own context ID before
210
- processing. Mismatches are rejected with JSON-RPC
211
- error code `-32001` (context mismatch).
212
-
213
- ##### `handleListByType(params)`
214
-
215
- Handles `listByType` / `list_by_type` IPC requests.
216
- Receives `{ ctx, sectionType }` and delegates to
217
- `worker_list_by_type` in the WASM engine, which
218
- scans the sections directory and returns all records
219
- whose `type` field matches `sectionType`.
220
-
221
- Returns: array of section records filtered by type.
222
-
223
- ##### `handleListDocumentsByType(params)`
224
-
225
- Handles `listDocumentsByType` IPC requests.
226
- Receives `{ ctx, docType }` and delegates to
227
- `worker_list_docs_by_type` in the WASM engine,
228
- which scans the documents directory and returns all
229
- manifests whose `type` field matches `docType`.
230
-
231
- Returns: array of document manifests filtered by
232
- type.
233
-
234
- ## Testing
235
-
236
- ```
237
- src/shared/worker-daemon/
238
- └── worker-daemon.spec.ts
239
- ```
@@ -1,37 +0,0 @@
1
- # worker-daemon/constants -- Daemon Defaults
2
-
3
- *Default values for daemon configuration when
4
- `worker.toml` is missing or incomplete.*
5
-
6
- ## Source
7
-
8
- `src/shared/worker-daemon/constants/`
9
-
10
- ### `daemon-defaults`
11
-
12
- **File:** `daemon-defaults.constant.ts`
13
-
14
- ```ts
15
- const DEFAULT_ITEM_LIMIT = 1000;
16
- const DEFAULT_INDEX_LIMIT = 100;
17
- const DEFAULT_TIMEOUT_MS = 300_000;
18
- const DEFAULT_ITEM_TTL_MS = 60_000;
19
- ```
20
-
21
- | Constant | Value | Description |
22
- |-----------------------|-----------|----------------|
23
- | `DEFAULT_ITEM_LIMIT` | `1000` | Max content |
24
- | | | objects held |
25
- | | | in cache |
26
- | `DEFAULT_INDEX_LIMIT` | `100` | Max index |
27
- | | | segments held |
28
- | | | in cache |
29
- | `DEFAULT_TIMEOUT_MS` | `300000` | Daemon idle |
30
- | | | timeout in ms |
31
- | | | (5 minutes) |
32
- | `DEFAULT_ITEM_TTL_MS` | `60000` | Per-item cache |
33
- | | | TTL in ms |
34
- | | | (1 minute) |
35
-
36
- These constants are used by `readConfig()` to fill
37
- missing keys when parsing `worker.toml`.
@@ -1,234 +0,0 @@
1
- # worker-daemon/functions -- Daemon Functions
2
-
3
- *Worker entry point, daemon entry point, host I/O
4
- imports, WASM module loading, IPC server creation,
5
- and configuration reading.*
6
-
7
- ## Source
8
-
9
- `src/shared/worker-daemon/functions/`
10
-
11
- ## Functions
12
-
13
- ### `runWorker`
14
-
15
- **File:** `run-worker.function.ts`
16
-
17
- Worker daemon entry point extracted from
18
- `src/worker.ts`. Parses `argv` for the context path
19
- and the optional `--log-level <level>` argument,
20
- configures the log level, validates the context path,
21
- and delegates to `startDaemon`.
22
-
23
- Usage: `kbdb-worker <context-path> [--log-level <level>]`
24
-
25
- ```ts
26
- async function runWorker(
27
- argv: string[],
28
- deps: WorkerDeps,
29
- ): Promise<WorkerResult>;
30
- ```
31
-
32
- ```ts
33
- interface WorkerDeps {
34
- existsSync: (path: string) => boolean;
35
- startDaemon: (
36
- contextPath: string,
37
- wasmDir?: string,
38
- ) => Promise<void>;
39
- wasmDir?: string;
40
- }
41
-
42
- interface WorkerResult {
43
- error?: string;
44
- exitCode: number;
45
- logLevel?: LogLevel;
46
- }
47
- ```
48
-
49
- `LogLevel` is `'debug' | 'info' | 'warning' | 'error'`.
50
- The log level is resolved from the `--log-level` CLI
51
- argument, then the `KBDB_LOG_LEVEL` environment
52
- variable, then defaults to `'error'`.
53
-
54
- Returns a `WorkerResult` without touching process
55
- globals, making it fully testable.
56
-
57
- ### `startDaemon`
58
-
59
- **File:** `start-daemon.function.ts`
60
-
61
- Entry point for the daemon process. Called from
62
- `runWorker` after path validation.
63
-
64
- ```ts
65
- async function startDaemon(
66
- contextPath: string,
67
- wasmDir?: string,
68
- ): Promise<void>;
69
- ```
70
-
71
- 1. Calls `readConfig(contextPath)` to load
72
- `worker.toml`.
73
- 2. Computes the context ID from `contextPath`
74
- (SHA-256 prefix, 16 hex characters).
75
- 3. Creates a new `WorkerDaemon(ctx, contextPath,
76
- config, wasmDir)`.
77
- 4. Calls `daemon.checkPreconditions()`. Failures
78
- are written to stderr and a crash log, then
79
- `process.exit(1)` is called.
80
- 5. Calls `daemon.start()`.
81
- 6. Registers OS signal handlers (`SIGTERM`,
82
- `SIGINT`) that call `daemon.stop()`.
83
-
84
- ### `createHostImports`
85
-
86
- **File:** `create-host-imports.function.ts`
87
-
88
- Creates the host I/O import functions required by
89
- `kb-worker.wasm`. All functions access WASM linear
90
- memory through a deferred `MemoryRef` that is wired
91
- up after module instantiation.
92
-
93
- ```ts
94
- function createHostImports(
95
- contextPath: string,
96
- memoryRef?: MemoryRef,
97
- fs?: FsAdapter,
98
- allocator?: AllocatorRef,
99
- ): Record<string, WebAssembly.ImportValue>;
100
- ```
101
-
102
- | Parameter | Description |
103
- |---------------|------------------------------------|
104
- | `contextPath` | Absolute path to `.kbdb` directory |
105
- | `memoryRef` | Deferred WASM memory reference |
106
- | `fs` | Filesystem adapter (default: real |
107
- | | `node:fs` operations) |
108
- | `allocator` | Deferred WASM allocator reference |
109
-
110
- **Host imports provided:**
111
-
112
- | Import name | Description |
113
- |-------------------|---------------------------------|
114
- | `host_read_file` | Read a file into WASM memory |
115
- | `host_write_file` | Write WASM memory to a file |
116
- | `host_rename` | Rename a file |
117
- | `host_remove_file`| Delete a file |
118
- | `host_list_dir` | List directory entries |
119
- | `host_mkdir` | Create a directory (recursive) |
120
- | `host_file_exists`| Check if a file exists |
121
-
122
- All paths received from WASM are resolved relative
123
- to `contextPath`.
124
-
125
- **Supporting interfaces:**
126
-
127
- ```ts
128
- interface FsAdapter {
129
- readFileSync(path: string): Buffer;
130
- writeFileSync(path: string, data: Uint8Array): void;
131
- renameSync(oldPath: string, newPath: string): void;
132
- unlinkSync(path: string): void;
133
- readdirSync(path: string): string[];
134
- mkdirSync(path: string): void;
135
- existsSync(path: string): boolean;
136
- }
137
-
138
- interface MemoryRef {
139
- memory: WebAssembly.Memory | null;
140
- }
141
-
142
- interface AllocatorRef {
143
- alloc: ((size: number) => number) | null;
144
- }
145
- ```
146
-
147
- ### `loadWasmModules`
148
-
149
- **File:** `load-wasm-modules.function.ts`
150
-
151
- Compiles and instantiates the WASM modules used by
152
- the worker daemon.
153
-
154
- ```ts
155
- async function loadWasmModules(
156
- config: DaemonConfig,
157
- wasmDir?: string,
158
- ): Promise<WasmModules>;
159
- ```
160
-
161
- Loading sequence:
162
-
163
- 1. Builds `createHostImports` with a deferred
164
- `MemoryRef` and `AllocatorRef`.
165
- 2. Compiles and instantiates `kb-worker.wasm`,
166
- passing host imports.
167
- 3. Wires the exported `memory` and `kbdb_alloc`
168
- function back into the deferred refs.
169
- 4. Compiles and instantiates
170
- `default-embedding.wasm` -- no host imports
171
- needed (pure computation). The module is
172
- instantiated with an empty import object.
173
-
174
- Returns a `WasmModules` object containing the
175
- instantiated modules and their shared memory.
176
-
177
- ### `createIpcServer`
178
-
179
- **File:** `create-ipc-server.function.ts`
180
-
181
- Creates a local IPC server bound to the given socket
182
- path and returns the `net.Server` instance.
183
-
184
- ```ts
185
- function createIpcServer(
186
- ipcPath: string,
187
- handler: IpcHandler,
188
- ): net.Server;
189
- ```
190
-
191
- Platform behavior:
192
-
193
- - **Linux / macOS** -- Unix domain socket.
194
- - **Windows** -- Named pipe.
195
-
196
- Messages are newline-delimited JSON-RPC 2.0. Each
197
- message is a single line of JSON followed by `\n`.
198
-
199
- Incoming requests are parsed and forwarded to
200
- `handler`. Responses are serialized and written back
201
- to the connection.
202
-
203
- ### `readConfig`
204
-
205
- **File:** `read-config.function.ts`
206
-
207
- Reads daemon configuration from `worker.toml` in
208
- the `.kbdb` directory.
209
-
210
- ```ts
211
- function readConfig(
212
- contextPath: string,
213
- ): DaemonConfig;
214
- ```
215
-
216
- Reads `${contextPath}/worker.toml`. Missing keys
217
- are filled with defaults from
218
- [daemon-defaults.constant.ts](constants.md). If the
219
- file does not exist, returns a `DaemonConfig` with
220
- all defaults.
221
-
222
- ## Testing
223
-
224
- Each function has a `.spec.ts` at the module root:
225
-
226
- ```
227
- src/shared/worker-daemon/
228
- ├── run-worker.spec.ts
229
- ├── start-daemon.spec.ts
230
- ├── create-host-imports.spec.ts
231
- ├── load-wasm-modules.spec.ts
232
- ├── create-ipc-server.spec.ts
233
- └── read-config.spec.ts
234
- ```
@@ -1,118 +0,0 @@
1
- # worker-daemon -- Daemon Process Module
2
-
3
- *Background process that loads WASM modules once,
4
- listens on IPC, serves JSON-RPC 2.0 requests, manages
5
- cache + context + idle timeout. Spawned on demand by
6
- worker-client, shuts down on idle timeout or OS
7
- signal.*
8
-
9
- ## Source
10
-
11
- `src/shared/worker-daemon/`
12
-
13
- ## Responsibility
14
-
15
- The worker-daemon module implements a detached
16
- background process that:
17
-
18
- - Loads and compiles the four WASM modules once at
19
- startup (`query-parser.wasm`, `fs-database.wasm`,
20
- `default-embedding.wasm`, `kb-worker.wasm`).
21
- - Reads configuration from `worker.toml` in the
22
- context directory.
23
- - Listens on a local IPC socket (Unix domain socket
24
- or Windows named pipe) for JSON-RPC 2.0 requests.
25
- Configures accepted connections with `SO_KEEPALIVE`
26
- (30 s) and listens with backlog 256.
27
- - Maintains an in-memory LRU cache with TTL expiry
28
- for indexes, content objects, and manifests.
29
- - Tracks active connections and resets the idle timer
30
- on every connect, disconnect, or request.
31
- - Shuts down gracefully on idle timeout or OS signal.
32
-
33
- The daemon is **not** a persistent server. It is a
34
- short-lived process spawned by
35
- [worker-client](../worker-client/module.md) that
36
- exists only to amortize WASM compilation cost and
37
- share cached data across connections.
38
-
39
- See [Worker Daemon](../../../../goals/worker-daemon.md)
40
- for the full behavioral specification.
41
-
42
- ## Directory Layout
43
-
44
- ```
45
- src/shared/worker-daemon/
46
- ├── index.ts
47
- ├── classes/
48
- │ └── worker-daemon.class.ts
49
- ├── functions/
50
- │ ├── create-host-imports.function.ts
51
- │ ├── create-ipc-server.function.ts
52
- │ ├── load-wasm-modules.function.ts
53
- │ ├── read-config.function.ts
54
- │ ├── run-worker.function.ts
55
- │ └── start-daemon.function.ts
56
- ├── typings/
57
- │ ├── daemon-config.interface.ts
58
- │ ├── ipc-handler.interface.ts
59
- │ └── wasm-modules.interface.ts
60
- ├── constants/
61
- │ └── daemon-defaults.constant.ts
62
- └── *.spec.ts
63
- ```
64
-
65
- ## Exports
66
-
67
- - **[classes.md](classes.md)** -- `WorkerDaemon`.
68
- - **[functions.md](functions.md)** -- `startDaemon`,
69
- `runWorker`, `createHostImports`, `loadWasmModules`,
70
- `createIpcServer`, `readConfig`.
71
- - **[typings.md](typings.md)** -- `DaemonConfig`,
72
- `IpcHandler`, `IpcRequest`, `IpcResponse`,
73
- `IpcErrorDetail`, `WasmModules`.
74
- - **[constants.md](constants.md)** --
75
- `DEFAULT_ITEM_LIMIT`, `DEFAULT_INDEX_LIMIT`,
76
- `DEFAULT_TIMEOUT_MS`, `DEFAULT_ITEM_TTL_MS`.
77
-
78
- ## Startup Sequence
79
-
80
- ```
81
- src/worker.ts (entry point)
82
-
83
- ├─ startDaemon(contextPath)
84
- │ ├─ readConfig(contextPath)
85
- │ │ └─ reads worker.toml from .kbdb dir
86
- │ ├─ new WorkerDaemon(ctx, config)
87
- │ └─ daemon.start()
88
- │ ├─ loadWasmModules(config)
89
- │ │ ├─ allocate shared WebAssembly.Memory
90
- │ │ ├─ compile query-parser.wasm ─┐
91
- │ │ ├─ compile fs-database.wasm ──┤
92
- │ │ │ (parallel, standalone, │
93
- │ │ │ shared memory) │
94
- │ │ ├─ compile default-embedding ─┤
95
- │ │ │ (standalone, no host │
96
- │ │ │ imports, pure computation)│
97
- │ │ └─ compile kb-worker.wasm ────┘
98
- │ │ (imports both modules)
99
- │ ├─ init cache
100
- │ ├─ remove stale IPC socket
101
- │ ├─ await createIpcServer(ctx, handler)
102
- │ │ └─ kb-worker-{ctx}.sock (listening)
103
- │ ├─ write PID file
104
- │ │ └─ kb-worker-{ctx}.pid
105
- │ └─ start idle timer
106
- └─ daemon running
107
- ```
108
-
109
- ## Shutdown Sequence
110
-
111
- On idle timeout or OS signal (`SIGTERM`, `SIGINT`):
112
-
113
- 1. Stop accepting new connections.
114
- 2. Wait for in-flight requests to complete.
115
- 3. Purge all cached data.
116
- 4. Remove PID file from OS temporary directory.
117
- 5. Remove IPC socket file (Unix only).
118
- 6. Exit process.