@dikolab/kbdb 0.1.6 → 0.2.1

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 (199) hide show
  1. package/README.md +30 -9
  2. package/dist/cli.cjs +181 -50
  3. package/dist/cli.cjs.map +3 -3
  4. package/dist/cli.mjs +181 -50
  5. package/dist/cli.mjs.map +3 -3
  6. package/dist/kbdb-worker.cjs +283 -29
  7. package/dist/kbdb-worker.cjs.map +4 -4
  8. package/dist/mod.cjs +13 -2
  9. package/dist/mod.cjs.map +2 -2
  10. package/dist/mod.d.ts +25 -0
  11. package/dist/mod.mjs +13 -2
  12. package/dist/mod.mjs.map +2 -2
  13. package/dist/src/cli.d.ts +8 -0
  14. package/dist/src/shared/cli/functions/format-output.function.d.ts +7 -7
  15. package/dist/src/shared/cli/functions/run-search.function.d.ts +25 -16
  16. package/dist/src/shared/cli/index.d.ts +1 -1
  17. package/dist/src/shared/cli/typings/cli-options.interface.d.ts +2 -0
  18. package/dist/src/shared/cli/typings/search-display.model.d.ts +25 -7
  19. package/dist/src/shared/mcp/functions/handle-recall.function.d.ts +29 -0
  20. package/dist/src/shared/mcp/typings/mcp-client.interface.d.ts +30 -11
  21. package/dist/src/shared/version/constants/version.constant.d.ts +1 -1
  22. package/dist/src/shared/wasm-codec/functions/decode-recall-result.function.d.ts +28 -0
  23. package/dist/src/shared/wasm-codec/functions/decode-scored-results.function.d.ts +14 -7
  24. package/dist/src/shared/wasm-codec/functions/encode-recall-params.function.d.ts +21 -0
  25. package/dist/src/shared/wasm-codec/functions/encode-search-params.function.d.ts +1 -1
  26. package/dist/src/shared/wasm-codec/index.d.ts +5 -2
  27. package/dist/src/shared/worker-client/classes/worker-client.class.d.ts +13 -3
  28. package/dist/src/shared/worker-client/index.d.ts +2 -1
  29. package/dist/src/shared/worker-client/typings/recall-result.model.d.ts +94 -0
  30. package/dist/src/shared/worker-client/typings/search-params.model.d.ts +2 -0
  31. package/dist/src/shared/worker-client/typings/search-result.model.d.ts +23 -7
  32. package/dist/src/shared/worker-daemon/classes/worker-daemon.class.d.ts +1 -0
  33. package/dist/src/worker.d.ts +7 -0
  34. package/dist/wasm/fs-database/kbdb_fs_database.d.ts +18 -0
  35. package/dist/wasm/fs-database/kbdb_fs_database.js +30 -0
  36. package/dist/wasm/fs-database/kbdb_fs_database_bg.wasm +0 -0
  37. package/dist/wasm/fs-database/kbdb_fs_database_bg.wasm.d.ts +2 -0
  38. package/dist/wasm/kb-worker/kbdb_worker.d.ts +38 -0
  39. package/dist/wasm/kb-worker/kbdb_worker.js +56 -0
  40. package/dist/wasm/kb-worker/kbdb_worker_bg.wasm +0 -0
  41. package/dist/wasm/kb-worker/kbdb_worker_bg.wasm.d.ts +3 -0
  42. package/dist/worker.mjs +283 -29
  43. package/dist/worker.mjs.map +4 -4
  44. package/docs/details/README.md +26 -0
  45. package/docs/details/cli.md +608 -0
  46. package/docs/details/library-api.md +406 -0
  47. package/docs/details/mcp-server.md +430 -0
  48. package/docs/details/search-and-ranking.md +457 -0
  49. package/docs/details/storage.md +529 -0
  50. package/docs/goals/agents.md +751 -0
  51. package/docs/goals/architecture.svg +198 -0
  52. package/docs/goals/cli.md +2308 -0
  53. package/docs/goals/content-composer.md +609 -0
  54. package/docs/goals/content-parser.md +279 -0
  55. package/docs/goals/database.md +1452 -0
  56. package/docs/goals/document.md +368 -0
  57. package/docs/goals/mcp.md +1467 -0
  58. package/docs/goals/overview.md +124 -0
  59. package/docs/goals/query-parser.md +271 -0
  60. package/docs/goals/query-result.md +693 -0
  61. package/docs/goals/skills.md +738 -0
  62. package/docs/goals/worker-client.md +944 -0
  63. package/docs/goals/worker-daemon.md +1447 -0
  64. package/docs/modules/overview.md +298 -0
  65. package/docs/modules/rust/fs-database/functions/document.md +48 -0
  66. package/docs/modules/rust/fs-database/functions/integrity.md +68 -0
  67. package/docs/modules/rust/fs-database/functions/io.md +87 -0
  68. package/docs/modules/rust/fs-database/functions/module.md +22 -0
  69. package/docs/modules/rust/fs-database/functions/query-exec.md +44 -0
  70. package/docs/modules/rust/fs-database/functions/section.md +59 -0
  71. package/docs/modules/rust/fs-database/indexes/btree.md +35 -0
  72. package/docs/modules/rust/fs-database/indexes/corpus.md +61 -0
  73. package/docs/modules/rust/fs-database/indexes/inverted.md +60 -0
  74. package/docs/modules/rust/fs-database/indexes/module.md +64 -0
  75. package/docs/modules/rust/fs-database/indexes/positional.md +39 -0
  76. package/docs/modules/rust/fs-database/module.md +149 -0
  77. package/docs/modules/rust/fs-database/types/catalog.md +53 -0
  78. package/docs/modules/rust/fs-database/types/module.md +12 -0
  79. package/docs/modules/rust/fs-database/types/section.md +101 -0
  80. package/docs/modules/rust/kb-worker/functions/compose.md +57 -0
  81. package/docs/modules/rust/kb-worker/functions/module.md +20 -0
  82. package/docs/modules/rust/kb-worker/functions/retrieve.md +194 -0
  83. package/docs/modules/rust/kb-worker/functions/search.md +173 -0
  84. package/docs/modules/rust/kb-worker/functions/write.md +172 -0
  85. package/docs/modules/rust/kb-worker/module.md +171 -0
  86. package/docs/modules/rust/kb-worker/types/cache.md +90 -0
  87. package/docs/modules/rust/kb-worker/types/context.md +26 -0
  88. package/docs/modules/rust/kb-worker/types/module.md +16 -0
  89. package/docs/modules/rust/kb-worker/types/status.md +29 -0
  90. package/docs/modules/rust/kb-worker/types/token.md +50 -0
  91. package/docs/modules/rust/overview.md +495 -0
  92. package/docs/modules/rust/query-parser/functions/extract.md +56 -0
  93. package/docs/modules/rust/query-parser/functions/module.md +16 -0
  94. package/docs/modules/rust/query-parser/functions/parse.md +33 -0
  95. package/docs/modules/rust/query-parser/functions/pipeline.md +103 -0
  96. package/docs/modules/rust/query-parser/functions/rank.md +85 -0
  97. package/docs/modules/rust/query-parser/module.md +136 -0
  98. package/docs/modules/rust/query-parser/types/cache.md +39 -0
  99. package/docs/modules/rust/query-parser/types/keyphrase.md +60 -0
  100. package/docs/modules/rust/query-parser/types/module.md +17 -0
  101. package/docs/modules/rust/query-parser/types/token.md +42 -0
  102. package/docs/modules/rust/shared/content-composer/functions.md +114 -0
  103. package/docs/modules/rust/shared/content-composer/module.md +147 -0
  104. package/docs/modules/rust/shared/content-composer/types.md +93 -0
  105. package/docs/modules/rust/shared/content-parser/functions.md +72 -0
  106. package/docs/modules/rust/shared/content-parser/module.md +99 -0
  107. package/docs/modules/rust/shared/content-parser/types.md +71 -0
  108. package/docs/modules/rust/shared/corpus/module.md +24 -0
  109. package/docs/modules/rust/shared/corpus/types.md +141 -0
  110. package/docs/modules/rust/shared/document/module.md +25 -0
  111. package/docs/modules/rust/shared/document/types.md +154 -0
  112. package/docs/modules/rust/shared/encode/module.md +22 -0
  113. package/docs/modules/rust/shared/encode/traits.md +304 -0
  114. package/docs/modules/rust/shared/error/module.md +28 -0
  115. package/docs/modules/rust/shared/error/types.md +302 -0
  116. package/docs/modules/rust/shared/kbid/module.md +24 -0
  117. package/docs/modules/rust/shared/kbid/types.md +147 -0
  118. package/docs/modules/rust/shared/memory/functions.md +209 -0
  119. package/docs/modules/rust/shared/memory/module.md +24 -0
  120. package/docs/modules/rust/shared/module.md +168 -0
  121. package/docs/modules/rust/shared/query/module.md +27 -0
  122. package/docs/modules/rust/shared/query/types.md +236 -0
  123. package/docs/modules/rust/shared/section/module.md +25 -0
  124. package/docs/modules/rust/shared/section/types.md +294 -0
  125. package/docs/modules/rust/shared/term/module.md +25 -0
  126. package/docs/modules/rust/shared/term/types.md +139 -0
  127. package/docs/modules/typescript/cli.md +129 -0
  128. package/docs/modules/typescript/kbdb-worker.md +135 -0
  129. package/docs/modules/typescript/overview.md +378 -0
  130. package/docs/modules/typescript/shared/cli/constants.md +23 -0
  131. package/docs/modules/typescript/shared/cli/functions.md +630 -0
  132. package/docs/modules/typescript/shared/cli/module.md +125 -0
  133. package/docs/modules/typescript/shared/cli/typings.md +172 -0
  134. package/docs/modules/typescript/shared/hash/functions.md +20 -0
  135. package/docs/modules/typescript/shared/hash/module.md +21 -0
  136. package/docs/modules/typescript/shared/mcp/classes.md +179 -0
  137. package/docs/modules/typescript/shared/mcp/functions.md +386 -0
  138. package/docs/modules/typescript/shared/mcp/module.md +160 -0
  139. package/docs/modules/typescript/shared/mcp/typings.md +522 -0
  140. package/docs/modules/typescript/shared/module.md +94 -0
  141. package/docs/modules/typescript/shared/platform/functions.md +42 -0
  142. package/docs/modules/typescript/shared/platform/module.md +25 -0
  143. package/docs/modules/typescript/shared/runtime/functions.md +26 -0
  144. package/docs/modules/typescript/shared/runtime/module.md +27 -0
  145. package/docs/modules/typescript/shared/runtime/typings.md +13 -0
  146. package/docs/modules/typescript/shared/version/module.md +27 -0
  147. package/docs/modules/typescript/shared/wasm-codec/functions.md +391 -0
  148. package/docs/modules/typescript/shared/wasm-codec/module.md +98 -0
  149. package/docs/modules/typescript/shared/worker-client/classes.md +264 -0
  150. package/docs/modules/typescript/shared/worker-client/functions.md +175 -0
  151. package/docs/modules/typescript/shared/worker-client/module.md +84 -0
  152. package/docs/modules/typescript/shared/worker-client/typings.md +508 -0
  153. package/docs/modules/typescript/shared/worker-daemon/classes.md +239 -0
  154. package/docs/modules/typescript/shared/worker-daemon/constants.md +37 -0
  155. package/docs/modules/typescript/shared/worker-daemon/functions.md +221 -0
  156. package/docs/modules/typescript/shared/worker-daemon/module.md +112 -0
  157. package/docs/modules/typescript/shared/worker-daemon/typings.md +126 -0
  158. package/docs/overview.md +188 -0
  159. package/docs/release-notes/0.1.0.md +189 -0
  160. package/docs/release-notes/0.1.1.md +46 -0
  161. package/docs/release-notes/0.1.2.md +38 -0
  162. package/docs/release-notes/0.1.3.md +42 -0
  163. package/docs/release-notes/0.2.0.md +147 -0
  164. package/docs/release-notes/README.md +9 -0
  165. package/mod.ts +26 -0
  166. package/package.json +8 -2
  167. package/src/cli.ts +9 -0
  168. package/src/shared/cli/functions/dispatch-command.function.ts +3 -3
  169. package/src/shared/cli/functions/format-output.function.ts +41 -34
  170. package/src/shared/cli/functions/parse-args.function.ts +7 -0
  171. package/src/shared/cli/functions/run-search.function.ts +53 -27
  172. package/src/shared/cli/index.ts +4 -1
  173. package/src/shared/cli/typings/cli-options.interface.ts +2 -0
  174. package/src/shared/cli/typings/search-display.model.ts +26 -7
  175. package/src/shared/mcp/functions/create-tool-definitions.function.ts +64 -4
  176. package/src/shared/mcp/functions/handle-recall.function.ts +66 -0
  177. package/src/shared/mcp/functions/handle-tool-call.function.ts +11 -1
  178. package/src/shared/mcp/typings/mcp-client.interface.ts +33 -15
  179. package/src/shared/version/constants/version.constant.ts +1 -1
  180. package/src/shared/wasm-codec/functions/decode-recall-result.function.ts +267 -0
  181. package/src/shared/wasm-codec/functions/decode-scored-results.function.ts +81 -27
  182. package/src/shared/wasm-codec/functions/encode-recall-params.function.ts +68 -0
  183. package/src/shared/wasm-codec/functions/encode-search-params.function.ts +4 -1
  184. package/src/shared/wasm-codec/index.ts +11 -3
  185. package/src/shared/worker-client/classes/worker-client.class.ts +24 -4
  186. package/src/shared/worker-client/index.ts +11 -1
  187. package/src/shared/worker-client/typings/recall-result.model.ts +98 -0
  188. package/src/shared/worker-client/typings/search-params.model.ts +2 -0
  189. package/src/shared/worker-client/typings/search-result.model.ts +24 -7
  190. package/src/shared/worker-daemon/classes/worker-daemon.class.ts +25 -4
  191. package/src/wasm/fs-database/kbdb_fs_database.d.ts +18 -0
  192. package/src/wasm/fs-database/kbdb_fs_database.js +30 -0
  193. package/src/wasm/fs-database/kbdb_fs_database_bg.wasm +0 -0
  194. package/src/wasm/fs-database/kbdb_fs_database_bg.wasm.d.ts +2 -0
  195. package/src/wasm/kb-worker/kbdb_worker.d.ts +38 -0
  196. package/src/wasm/kb-worker/kbdb_worker.js +56 -0
  197. package/src/wasm/kb-worker/kbdb_worker_bg.wasm +0 -0
  198. package/src/wasm/kb-worker/kbdb_worker_bg.wasm.d.ts +3 -0
  199. package/src/worker.ts +8 -0
@@ -0,0 +1,264 @@
1
+ # worker-client/classes -- WorkerClient
2
+
3
+ *IPC client class. Sends JSON-RPC 2.0 requests to the
4
+ worker daemon over a Unix socket or Windows named pipe
5
+ and returns typed results.*
6
+
7
+ ## Source
8
+
9
+ `src/shared/worker-client/classes/`
10
+
11
+ ## Classes
12
+
13
+ ### `WorkerClient`
14
+
15
+ **File:** `worker-client.class.ts`
16
+
17
+ Holds the IPC connection and exposes every daemon
18
+ operation as a typed async method. Instantiated
19
+ directly via the constructor -- not created through a
20
+ factory function.
21
+
22
+ All methods send a JSON-RPC 2.0 request over the IPC
23
+ connection and await the response. The `ctx` parameter
24
+ is automatically injected into every request.
25
+
26
+ ```ts
27
+ class WorkerClient {
28
+ constructor(
29
+ socketPath: string,
30
+ contextId: string,
31
+ );
32
+
33
+ // Lifecycle
34
+ connect(): Promise<void>;
35
+ disconnect(): void;
36
+
37
+ // Status
38
+ status(): Promise<StatusResult>;
39
+ dbStatus(): Promise<StatusResult>;
40
+
41
+ // Search
42
+ search(params: SearchParams):
43
+ Promise<PagedSearchResult>;
44
+ recall(params: RecallParams):
45
+ Promise<RecallResult | RecallResult[]>;
46
+
47
+ // Sections
48
+ readSections(kbids: string[]):
49
+ Promise<Section[]>;
50
+ addSection(params: AddSectionParams):
51
+ Promise<AddSectionResult>;
52
+ updateSection(
53
+ kbid: string,
54
+ params: AddSectionParams,
55
+ ): Promise<string>;
56
+ removeSection(kbid: string):
57
+ Promise<RemoveSectionResult>;
58
+
59
+ // Documents
60
+ groupSections(
61
+ title: string,
62
+ kbids: string[],
63
+ ): Promise<string>;
64
+ removeGrouping(docid: string): Promise<void>;
65
+ retrieveDocument(docid: string): Promise<unknown>;
66
+
67
+ // Content composition
68
+ content(ids: string[]): Promise<ContentResult>;
69
+
70
+ // Maintenance
71
+ integrityCheck(): Promise<IntegrityCheckResult>;
72
+ gc(): Promise<GcResult>;
73
+ rebuildIndexes(): Promise<RebuildResult>;
74
+
75
+ // Typed listing
76
+ listByType(sectionType: string): Promise<Section[]>;
77
+ listDocumentsByType(docType: string): Promise<unknown[]>;
78
+
79
+ // Cache
80
+ invalidate(kbid: string): Promise<void>;
81
+ invalidateAll(): Promise<void>;
82
+
83
+ // Version and migration
84
+ checkVersion(targetPath: string):
85
+ Promise<VersionStatus>;
86
+ migrateDatabase(targetPath: string):
87
+ Promise<MigrateResult>;
88
+
89
+ // Directory initialization
90
+ initDirectory(targetPath: string): Promise<void>;
91
+ }
92
+ ```
93
+
94
+ ### Constructor
95
+
96
+ | Parameter | Type | Description |
97
+ |--------------|----------|-------------------------------|
98
+ | `socketPath` | `string` | Absolute IPC socket path or |
99
+ | | | Windows named pipe path |
100
+ | `contextId` | `string` | 16-character context ID sent |
101
+ | | | as `ctx` in every request |
102
+
103
+ ### Lifecycle
104
+
105
+ #### `connect()`
106
+
107
+ Establishes the IPC connection to the worker daemon.
108
+ Must be called before any other method.
109
+
110
+ #### `disconnect()`
111
+
112
+ Destroys the IPC socket and clears the connection
113
+ state. The daemon continues running. After
114
+ disconnecting, all method calls throw until
115
+ `connect()` is called again.
116
+
117
+ ### Status Methods
118
+
119
+ #### `status()`
120
+
121
+ Returns the daemon's current initialization state and
122
+ cache statistics.
123
+
124
+ #### `dbStatus()`
125
+
126
+ Returns database metadata from the daemon's cached
127
+ state, including document count, section count, index
128
+ size, and cache statistics.
129
+
130
+ ### Search
131
+
132
+ #### `search(params)`
133
+
134
+ Executes a ranked search. Returns a `PagedSearchResult`
135
+ containing items sorted by descending relevance score
136
+ along with pagination metadata.
137
+
138
+ #### `recall(params)`
139
+
140
+ Progressive context expansion. Returns section
141
+ content, metadata, and optionally parent document
142
+ manifests, sibling sections, and forward/back
143
+ references depending on the `depth` parameter.
144
+ Accepts single `kbid` or batch `kbids`.
145
+
146
+ ```ts
147
+ recall(params: RecallParams):
148
+ Promise<RecallResult | RecallResult[]>;
149
+ ```
150
+
151
+ ### Section Methods
152
+
153
+ #### `addSection(params)`
154
+
155
+ Adds a new section. Returns `AddSectionResult` with
156
+ the assigned `kbid`.
157
+
158
+ #### `updateSection(kbid, params)`
159
+
160
+ Replaces a section's content and metadata. Returns the
161
+ knowledge-base identifier of the updated section.
162
+
163
+ #### `removeSection(kbid)`
164
+
165
+ Removes a section and its index entries.
166
+
167
+ #### `readSections(kbids)`
168
+
169
+ Retrieves one or more sections by their identifiers.
170
+
171
+ ### Document Methods
172
+
173
+ #### `groupSections(title, kbids)`
174
+
175
+ Groups sections under a named document. Returns the
176
+ assigned document identifier (`docid`).
177
+
178
+ #### `removeGrouping(docid)`
179
+
180
+ Removes the document grouping record. The sections
181
+ themselves are not deleted.
182
+
183
+ #### `retrieveDocument(docid)`
184
+
185
+ Returns a document record including its sections.
186
+
187
+ ### Typed Listing Methods
188
+
189
+ #### `listByType(sectionType)`
190
+
191
+ Returns all sections whose `sectionType` field
192
+ matches the given value. Used to retrieve all
193
+ sections of a particular semantic type (e.g.
194
+ `'skill'`) without a search query.
195
+
196
+ #### `listDocumentsByType(docType)`
197
+
198
+ Returns all document manifests whose `type` field
199
+ matches the given value. Used to retrieve all
200
+ documents of a particular kind (e.g. `'agent'`).
201
+
202
+ ### Content Composition
203
+
204
+ #### `content(ids)`
205
+
206
+ Composes a rendered Markdown document from one or more
207
+ kbid or docid identifiers. IDs are auto-detected
208
+ (try kbid first, then docid).
209
+
210
+ ### Maintenance Methods
211
+
212
+ #### `integrityCheck()`
213
+
214
+ Runs a full integrity check on the database.
215
+
216
+ #### `gc()`
217
+
218
+ Runs garbage collection to remove orphaned sections.
219
+
220
+ #### `rebuildIndexes()`
221
+
222
+ Rebuilds all indexes from section files and document
223
+ manifests on disk.
224
+
225
+ ### Cache Methods
226
+
227
+ #### `invalidate(kbid)`
228
+
229
+ Invalidates the cache entry for a single section.
230
+
231
+ #### `invalidateAll()`
232
+
233
+ Clears all entries from the daemon's in-memory cache.
234
+
235
+ ### Version and Migration Methods
236
+
237
+ #### `checkVersion(targetPath)`
238
+
239
+ Checks the format version of the `.kbdb` database at
240
+ the given path against the version required by the
241
+ current WASM engine. Read-only -- makes no changes.
242
+
243
+ #### `migrateDatabase(targetPath)`
244
+
245
+ Runs all pending database migrations for the `.kbdb`
246
+ directory at the given path.
247
+
248
+ ### Directory Initialization
249
+
250
+ #### `initDirectory(targetPath)`
251
+
252
+ Initialises a new `.kbdb` database directory by
253
+ sending an `initDirectory` request to the worker
254
+ daemon.
255
+
256
+ ## Testing
257
+
258
+ ```
259
+ src/shared/worker-client/
260
+ ├── worker-client.spec.ts
261
+ └── tests/
262
+ ├── client-lifecycle.spec.ts
263
+ └── e2e-worker-client.spec.ts
264
+ ```
@@ -0,0 +1,175 @@
1
+ # worker-client/functions -- Factory and Utilities
2
+
3
+ *Factory function that returns a connected
4
+ WorkerClient, plus internal utilities for path
5
+ resolution, context ID computation, daemon discovery,
6
+ spawning, and process checking.*
7
+
8
+ ## Source
9
+
10
+ `src/shared/worker-client/functions/`
11
+
12
+ ## Functions
13
+
14
+ ### `createWorkerClient`
15
+
16
+ **File:** `create-worker-client.function.ts`
17
+
18
+ Factory function. Returns a connected `WorkerClient`
19
+ instance ready for use.
20
+
21
+ ```ts
22
+ async function createWorkerClient(
23
+ options?: WorkerClientOptions,
24
+ ): Promise<WorkerClient>;
25
+ ```
26
+
27
+ **Resolution sequence:**
28
+
29
+ 1. Resolve context path -- use `options.contextPath`
30
+ directly if provided; otherwise call
31
+ `resolveDbPath(options.dbPath)` which appends
32
+ `/.kbdb` to `dbPath` (or `cwd()`).
33
+ 2. Compute the context ID via `computeContextId()`.
34
+ 3. Discover a running daemon via `discoverDaemon()`.
35
+ 4. If no daemon is running and `autoSpawn !== false`,
36
+ spawn one via `spawnDaemon()` then poll with
37
+ `waitForDaemon()`.
38
+ 5. Connect to the IPC socket and return the connected
39
+ `WorkerClient`.
40
+
41
+ Throws if the daemon fails to start or the IPC
42
+ connection cannot be established.
43
+
44
+ ### `resolveDbPath`
45
+
46
+ **File:** `resolve-db-path.function.ts`
47
+
48
+ Resolves the absolute path to the `.kbdb` database
49
+ directory.
50
+
51
+ ```ts
52
+ function resolveDbPath(
53
+ targetDir?: string,
54
+ ): string;
55
+ ```
56
+
57
+ Resolves `targetDir` (or `process.cwd()`) to an
58
+ absolute path and appends `/.kbdb`. Throws if the
59
+ `.kbdb` directory does not exist at the resolved path.
60
+
61
+ @throws {Error} if no `.kbdb` directory is found.
62
+
63
+ ### `computeContextId`
64
+
65
+ **File:** `compute-context-id.function.ts`
66
+
67
+ Computes the context ID from a database directory's
68
+ absolute path.
69
+
70
+ ```ts
71
+ async function computeContextId(
72
+ absolutePath: string,
73
+ ): Promise<string>;
74
+ ```
75
+
76
+ Produces the SHA-256 hash of the UTF-8 encoded path
77
+ string, then truncates the hex digest to 16 lowercase
78
+ characters.
79
+
80
+ ```
81
+ SHA-256("/home/user/project/.kbdb")
82
+ → "a3f1...64 hex chars..."
83
+ → "a3f1b2c4d5e6f789" (first 16 chars)
84
+ ```
85
+
86
+ ### `discoverDaemon`
87
+
88
+ **File:** `discover-daemon.function.ts`
89
+
90
+ Looks for a running daemon matching the given context
91
+ ID. Returns synchronously.
92
+
93
+ ```ts
94
+ function discoverDaemon(
95
+ contextId: string,
96
+ ): DaemonInfo | null;
97
+ ```
98
+
99
+ 1. Look for `kbdb-{contextId}.pid` in the OS temp
100
+ directory.
101
+ 2. If the PID file exists, read the process ID.
102
+ 3. Call `isProcessAlive(pid)` from `shared/platform/`
103
+ to verify the process is running.
104
+ 4. If alive, return `{ pid, socketPath }`.
105
+ 5. If the PID file is missing or the process is dead,
106
+ removes the stale PID file and returns `null`.
107
+
108
+ ### `spawnDaemon`
109
+
110
+ **File:** `spawn-daemon.function.ts`
111
+
112
+ Spawns a new worker daemon as a detached child process
113
+ and immediately unrefs the child so it does not keep
114
+ the parent process alive.
115
+
116
+ ```ts
117
+ function spawnDaemon(
118
+ contextPath: string,
119
+ runtime: Runtime,
120
+ workerScript?: string,
121
+ ): void;
122
+ ```
123
+
124
+ | Parameter | Description |
125
+ |----------------|--------------------------------------|
126
+ | `contextPath` | Absolute path to the `.kbdb` dir |
127
+ | `runtime` | `'deno'` or `'node'` |
128
+ | `workerScript` | Absolute path to the daemon script |
129
+
130
+ Throws if `workerScript` is not provided. The worker
131
+ script path must be resolved at the entry point by
132
+ `resolveWorkerScript` from the `dir-resolver` module.
133
+
134
+ **Deno:** spawns `deno run --allow-all <script> <path>`
135
+
136
+ **Node.js:** spawns `<execPath> <script> <path>`
137
+
138
+ After calling `spawnDaemon`, the caller should use
139
+ `waitForDaemon` to poll for the PID file before
140
+ attempting to connect.
141
+
142
+ ### `waitForDaemon`
143
+
144
+ **File:** `wait-for-daemon.function.ts`
145
+
146
+ Waits for the worker daemon's PID file to appear on
147
+ disk after spawning.
148
+
149
+ ```ts
150
+ async function waitForDaemon(
151
+ contextId: string,
152
+ timeoutMs?: number,
153
+ ): Promise<boolean>;
154
+ ```
155
+
156
+ Polls every 100 ms until the PID file at
157
+ `<tmpdir>/kbdb-<contextId>.pid` exists or the timeout
158
+ elapses. Returns `true` when the file appeared before
159
+ the timeout, `false` otherwise.
160
+
161
+ `timeoutMs` defaults to `10 000` ms.
162
+
163
+ ## Testing
164
+
165
+ Each function has a `.spec.ts` at the module root:
166
+
167
+ ```
168
+ src/shared/worker-client/
169
+ ├── create-worker-client.spec.ts
170
+ ├── resolve-db-path.spec.ts
171
+ ├── compute-context-id.spec.ts
172
+ ├── discover-daemon.spec.ts
173
+ ├── spawn-daemon.spec.ts
174
+ └── wait-for-daemon.spec.ts
175
+ ```
@@ -0,0 +1,84 @@
1
+ # worker-client -- IPC Client Module
2
+
3
+ *Sole IPC client for the worker daemon. Resolves the
4
+ database path, computes a context ID, discovers or
5
+ spawns the daemon, connects via IPC socket, and
6
+ provides a typed async API for all operations.*
7
+
8
+ ## Source
9
+
10
+ `src/shared/worker-client/`
11
+
12
+ ## Responsibility
13
+
14
+ The worker-client module is the only TypeScript module
15
+ that communicates with the worker daemon. It:
16
+
17
+ - Resolves the `.kbdb` database directory path.
18
+ - Computes the context ID (SHA-256 of the absolute
19
+ path, truncated to 16 hex characters).
20
+ - Discovers a running daemon or spawns a new one.
21
+ - Connects to the daemon's Unix socket or Windows
22
+ named pipe.
23
+ - Exposes every daemon IPC method as a typed async
24
+ function (JSON-RPC 2.0 over IPC).
25
+ - Handles reconnection when the connection drops.
26
+
27
+ The module is **not** exported from the library API
28
+ (`mod.ts`). Only `src/cli.ts` imports it.
29
+
30
+ ```
31
+ src/cli.ts ──→ worker-client ──→ src/worker.ts
32
+ ```
33
+
34
+ See [Worker Client](../../../../goals/worker-client.md)
35
+ for the full behavioral specification.
36
+
37
+ ## Directory Layout
38
+
39
+ ```
40
+ src/shared/worker-client/
41
+ ├── index.ts
42
+ ├── classes/
43
+ │ └── worker-client.class.ts
44
+ ├── functions/
45
+ │ ├── compute-context-id.function.ts
46
+ │ ├── create-worker-client.function.ts
47
+ │ ├── discover-daemon.function.ts
48
+ │ ├── resolve-db-path.function.ts
49
+ │ ├── spawn-daemon.function.ts
50
+ │ └── wait-for-daemon.function.ts
51
+ ├── typings/
52
+ │ ├── add-section-result.model.ts
53
+ │ ├── content-result.model.ts
54
+ │ ├── daemon-info.model.ts
55
+ │ ├── gc-result.model.ts
56
+ │ ├── integrity-check-result.model.ts
57
+ │ ├── migrate-result.model.ts
58
+ │ ├── rebuild-result.model.ts
59
+ │ ├── remove-section-result.model.ts
60
+ │ ├── search-params.model.ts
61
+ │ ├── search-result.model.ts
62
+ │ ├── section.model.ts
63
+ │ ├── status-result.model.ts
64
+ │ ├── version-status.model.ts
65
+ │ ├── worker-client-error.interface.ts
66
+ │ └── worker-client-options.interface.ts
67
+ └── *.spec.ts
68
+ ```
69
+
70
+ ## Exports
71
+
72
+ - **[classes.md](classes.md)** -- `WorkerClient`.
73
+ - **[functions.md](functions.md)** --
74
+ `createWorkerClient`, `resolveDbPath`,
75
+ `computeContextId`, `discoverDaemon`,
76
+ `spawnDaemon`, `waitForDaemon`.
77
+ - **[typings.md](typings.md)** --
78
+ `WorkerClientOptions`, `SearchParams`,
79
+ `SearchResult`, `Section`, `AddSectionParams`,
80
+ `AddSectionResult`, `RemoveSectionResult`,
81
+ `ContentResult`, `StatusResult`, `DaemonInfo`,
82
+ `VersionCompatibility`, `VersionStatus`,
83
+ `MigrateResult`, `IntegrityCheckResult`,
84
+ `GcResult`, `RebuildResult`, `WorkerClientError`.