@dikolab/kbdb 0.5.0 → 0.6.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 (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 -25
  5. package/README.md.bak +322 -0
  6. package/dist/README.md +322 -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/package.json +1 -1
  66. package/dist/wasm/kb-worker/kbdb_worker_bg.wasm +0 -0
  67. package/dist/wasm/kb-worker/package.json +1 -1
  68. package/dist/wasm/query-parser/kbdb_query_parser_bg.wasm +0 -0
  69. package/dist/wasm/query-parser/package.json +1 -1
  70. package/dist/worker.d.ts +0 -7
  71. package/dist/worker.mjs +37 -6
  72. package/dist/worker.mjs.map +3 -3
  73. package/package.json +11 -10
  74. package/dist/chunk-BJXEVALU.mjs.map +0 -7
  75. package/dist/src/shared/cli/functions/resolve-raw-db-path.function.d.ts +0 -10
  76. package/docs/details/README.md +0 -30
  77. package/docs/details/agent-tooling.md +0 -132
  78. package/docs/details/cli.md +0 -777
  79. package/docs/details/knowledge-base.md +0 -180
  80. package/docs/details/library-api.md +0 -495
  81. package/docs/details/mcp-server.md +0 -482
  82. package/docs/details/search-and-ranking.md +0 -575
  83. package/docs/details/storage.md +0 -531
  84. package/docs/goals/agents.md +0 -751
  85. package/docs/goals/architecture.svg +0 -198
  86. package/docs/goals/auto-capture.md +0 -378
  87. package/docs/goals/benchmarking.md +0 -296
  88. package/docs/goals/cli.md +0 -2654
  89. package/docs/goals/concurrency.md +0 -259
  90. package/docs/goals/content-composer.md +0 -609
  91. package/docs/goals/content-parser.md +0 -279
  92. package/docs/goals/database.md +0 -1679
  93. package/docs/goals/document.md +0 -368
  94. package/docs/goals/hybrid-search.md +0 -465
  95. package/docs/goals/mcp.md +0 -1541
  96. package/docs/goals/overview.md +0 -124
  97. package/docs/goals/query-parser.md +0 -373
  98. package/docs/goals/query-result.md +0 -860
  99. package/docs/goals/semantic-dedup.md +0 -233
  100. package/docs/goals/skills.md +0 -810
  101. package/docs/goals/sync.md +0 -217
  102. package/docs/goals/worker-client.md +0 -1005
  103. package/docs/goals/worker-daemon.md +0 -1547
  104. package/docs/modules/overview.md +0 -319
  105. package/docs/modules/rust/embedding/module.md +0 -91
  106. package/docs/modules/rust/fs-database/functions/document.md +0 -48
  107. package/docs/modules/rust/fs-database/functions/integrity.md +0 -79
  108. package/docs/modules/rust/fs-database/functions/io.md +0 -87
  109. package/docs/modules/rust/fs-database/functions/module.md +0 -22
  110. package/docs/modules/rust/fs-database/functions/query-exec.md +0 -44
  111. package/docs/modules/rust/fs-database/functions/section.md +0 -76
  112. package/docs/modules/rust/fs-database/indexes/btree.md +0 -35
  113. package/docs/modules/rust/fs-database/indexes/corpus.md +0 -61
  114. package/docs/modules/rust/fs-database/indexes/inverted.md +0 -60
  115. package/docs/modules/rust/fs-database/indexes/module.md +0 -64
  116. package/docs/modules/rust/fs-database/indexes/positional.md +0 -39
  117. package/docs/modules/rust/fs-database/indexes/vectors.md +0 -110
  118. package/docs/modules/rust/fs-database/module.md +0 -150
  119. package/docs/modules/rust/fs-database/types/catalog.md +0 -53
  120. package/docs/modules/rust/fs-database/types/module.md +0 -12
  121. package/docs/modules/rust/fs-database/types/section.md +0 -127
  122. package/docs/modules/rust/kb-worker/functions/compose.md +0 -57
  123. package/docs/modules/rust/kb-worker/functions/module.md +0 -20
  124. package/docs/modules/rust/kb-worker/functions/retrieve.md +0 -194
  125. package/docs/modules/rust/kb-worker/functions/search.md +0 -173
  126. package/docs/modules/rust/kb-worker/functions/write.md +0 -172
  127. package/docs/modules/rust/kb-worker/module.md +0 -171
  128. package/docs/modules/rust/kb-worker/types/cache.md +0 -90
  129. package/docs/modules/rust/kb-worker/types/context.md +0 -26
  130. package/docs/modules/rust/kb-worker/types/module.md +0 -16
  131. package/docs/modules/rust/kb-worker/types/status.md +0 -29
  132. package/docs/modules/rust/kb-worker/types/token.md +0 -50
  133. package/docs/modules/rust/overview.md +0 -531
  134. package/docs/modules/rust/query-parser/functions/extract.md +0 -56
  135. package/docs/modules/rust/query-parser/functions/module.md +0 -18
  136. package/docs/modules/rust/query-parser/functions/parse.md +0 -33
  137. package/docs/modules/rust/query-parser/functions/pipeline.md +0 -9
  138. package/docs/modules/rust/query-parser/functions/rank.md +0 -85
  139. package/docs/modules/rust/query-parser/module.md +0 -131
  140. package/docs/modules/rust/query-parser/types/cache.md +0 -39
  141. package/docs/modules/rust/query-parser/types/keyphrase.md +0 -71
  142. package/docs/modules/rust/query-parser/types/module.md +0 -18
  143. package/docs/modules/rust/query-parser/types/token.md +0 -46
  144. package/docs/modules/rust/shared/content-composer/functions.md +0 -114
  145. package/docs/modules/rust/shared/content-composer/module.md +0 -147
  146. package/docs/modules/rust/shared/content-composer/types.md +0 -93
  147. package/docs/modules/rust/shared/content-parser/functions.md +0 -72
  148. package/docs/modules/rust/shared/content-parser/module.md +0 -99
  149. package/docs/modules/rust/shared/content-parser/types.md +0 -71
  150. package/docs/modules/rust/shared/corpus/module.md +0 -24
  151. package/docs/modules/rust/shared/corpus/types.md +0 -141
  152. package/docs/modules/rust/shared/document/module.md +0 -25
  153. package/docs/modules/rust/shared/document/types.md +0 -154
  154. package/docs/modules/rust/shared/encode/module.md +0 -22
  155. package/docs/modules/rust/shared/encode/traits.md +0 -304
  156. package/docs/modules/rust/shared/error/module.md +0 -28
  157. package/docs/modules/rust/shared/error/types.md +0 -302
  158. package/docs/modules/rust/shared/kbid/module.md +0 -24
  159. package/docs/modules/rust/shared/kbid/types.md +0 -147
  160. package/docs/modules/rust/shared/memory/functions.md +0 -209
  161. package/docs/modules/rust/shared/memory/module.md +0 -24
  162. package/docs/modules/rust/shared/module.md +0 -196
  163. package/docs/modules/rust/shared/pipeline/functions.md +0 -141
  164. package/docs/modules/rust/shared/pipeline/module.md +0 -62
  165. package/docs/modules/rust/shared/pipeline/types.md +0 -43
  166. package/docs/modules/rust/shared/query/module.md +0 -27
  167. package/docs/modules/rust/shared/query/types.md +0 -236
  168. package/docs/modules/rust/shared/section/module.md +0 -25
  169. package/docs/modules/rust/shared/section/types.md +0 -294
  170. package/docs/modules/rust/shared/term/module.md +0 -25
  171. package/docs/modules/rust/shared/term/types.md +0 -139
  172. package/docs/modules/typescript/cli.md +0 -131
  173. package/docs/modules/typescript/kbdb-worker.md +0 -150
  174. package/docs/modules/typescript/overview.md +0 -400
  175. package/docs/modules/typescript/shared/auto-capture/module.md +0 -73
  176. package/docs/modules/typescript/shared/cli/constants.md +0 -23
  177. package/docs/modules/typescript/shared/cli/functions.md +0 -809
  178. package/docs/modules/typescript/shared/cli/module.md +0 -138
  179. package/docs/modules/typescript/shared/cli/typings.md +0 -197
  180. package/docs/modules/typescript/shared/embedding/functions.md +0 -200
  181. package/docs/modules/typescript/shared/embedding/module.md +0 -77
  182. package/docs/modules/typescript/shared/embedding/typings.md +0 -124
  183. package/docs/modules/typescript/shared/hash/functions.md +0 -20
  184. package/docs/modules/typescript/shared/hash/module.md +0 -21
  185. package/docs/modules/typescript/shared/log/constants.md +0 -82
  186. package/docs/modules/typescript/shared/log/functions.md +0 -131
  187. package/docs/modules/typescript/shared/log/module.md +0 -153
  188. package/docs/modules/typescript/shared/log/typings.md +0 -82
  189. package/docs/modules/typescript/shared/mcp/classes.md +0 -179
  190. package/docs/modules/typescript/shared/mcp/functions.md +0 -386
  191. package/docs/modules/typescript/shared/mcp/module.md +0 -160
  192. package/docs/modules/typescript/shared/mcp/typings.md +0 -529
  193. package/docs/modules/typescript/shared/module.md +0 -110
  194. package/docs/modules/typescript/shared/platform/functions.md +0 -42
  195. package/docs/modules/typescript/shared/platform/module.md +0 -25
  196. package/docs/modules/typescript/shared/runtime/functions.md +0 -26
  197. package/docs/modules/typescript/shared/runtime/module.md +0 -27
  198. package/docs/modules/typescript/shared/runtime/typings.md +0 -13
  199. package/docs/modules/typescript/shared/version/module.md +0 -27
  200. package/docs/modules/typescript/shared/wasm-codec/functions.md +0 -391
  201. package/docs/modules/typescript/shared/wasm-codec/module.md +0 -98
  202. package/docs/modules/typescript/shared/worker-client/classes.md +0 -264
  203. package/docs/modules/typescript/shared/worker-client/functions.md +0 -175
  204. package/docs/modules/typescript/shared/worker-client/module.md +0 -92
  205. package/docs/modules/typescript/shared/worker-client/typings.md +0 -511
  206. package/docs/modules/typescript/shared/worker-daemon/classes.md +0 -239
  207. package/docs/modules/typescript/shared/worker-daemon/constants.md +0 -37
  208. package/docs/modules/typescript/shared/worker-daemon/functions.md +0 -234
  209. package/docs/modules/typescript/shared/worker-daemon/module.md +0 -118
  210. package/docs/modules/typescript/shared/worker-daemon/typings.md +0 -132
  211. package/docs/overview.md +0 -191
  212. package/docs/release-notes/0.1.0.md +0 -189
  213. package/docs/release-notes/0.1.1.md +0 -46
  214. package/docs/release-notes/0.1.2.md +0 -38
  215. package/docs/release-notes/0.1.3.md +0 -42
  216. package/docs/release-notes/0.2.0.md +0 -147
  217. package/docs/release-notes/0.3.0.md +0 -89
  218. package/docs/release-notes/0.3.1.md +0 -121
  219. package/docs/release-notes/0.3.2.md +0 -64
  220. package/docs/release-notes/0.4.0.md +0 -80
  221. package/docs/release-notes/0.4.1.md +0 -149
  222. package/docs/release-notes/0.4.3.md +0 -60
  223. package/docs/release-notes/0.5.0.md +0 -41
  224. package/docs/release-notes/README.md +0 -16
@@ -1,304 +0,0 @@
1
- # traits::encode -- Binary Encoding Trait
2
-
3
- *Compact binary serialization trait for structured
4
- data passed through shared `WebAssembly.Memory`
5
- between WASM modules.*
6
-
7
- ## File
8
-
9
- `engine/crates/shared/src/encode/encode.rs`
10
-
11
- ## Overview
12
-
13
- When `kb-worker.wasm` passes a `QueryPlan` to
14
- `fs-database.wasm`, or receives a `Vec<RawMatch>`
15
- back, the data crosses a module boundary through
16
- shared memory. JSON would work but adds parsing
17
- overhead on every call. The `Encode` trait provides
18
- a minimal, zero-copy-friendly binary format that
19
- both sides compile from the same type definitions --
20
- no schema negotiation, no version headers, no
21
- self-description.
22
-
23
- This is **not** a general-purpose serializer. It
24
- handles only the specific data shapes defined in
25
- `kbdb-shared`'s types. If a type changes, both the
26
- encoder and decoder are recompiled from the same
27
- source, so wire compatibility is guaranteed by the
28
- build system.
29
-
30
- ## Trait Definition
31
-
32
- ```rust
33
- pub trait Encode: Sized {
34
- fn encode(&self, buf: &mut Vec<u8>);
35
-
36
- fn decode(buf: &[u8])
37
- -> Result<Self, DecodeError>;
38
-
39
- fn encoded_size(&self) -> usize;
40
- }
41
- ```
42
-
43
- ### `encode`
44
-
45
- Appends the binary representation of `self` to
46
- `buf`. The caller pre-allocates or grows the buffer.
47
- Encoding is deterministic -- the same value always
48
- produces the same bytes.
49
-
50
- ### `decode`
51
-
52
- Reads a value from the beginning of `buf`. Returns
53
- the decoded value and advances past the consumed
54
- bytes (via the return value's size). Returns
55
- `DecodeError` if the buffer is too short or contains
56
- invalid data.
57
-
58
- ### `encoded_size`
59
-
60
- Returns the number of bytes `encode` will append.
61
- Used to pre-allocate buffers for write helpers in
62
- [memory/functions](../memory/functions.md).
63
-
64
- ## Wire Format
65
-
66
- ### Scalars
67
-
68
- | Rust type | Wire format | Bytes |
69
- |-----------|---------------------|-------|
70
- | `u8` | Raw byte | 1 |
71
- | `u16` | Little-endian | 2 |
72
- | `u32` | Little-endian | 4 |
73
- | `u64` | Little-endian | 8 |
74
- | `i32` | Little-endian | 4 |
75
- | `f32` | Little-endian IEEE 754 | 4 |
76
- | `bool` | `0x00` / `0x01` | 1 |
77
-
78
- ### Strings
79
-
80
- ```
81
- [len: u32] [utf8_bytes: len]
82
- ```
83
-
84
- Length-prefixed. `len` is the byte count (not
85
- character count). Empty strings encode as
86
- `[0x00 0x00 0x00 0x00]` (4 bytes, zero length).
87
-
88
- ### `Vec<T>`
89
-
90
- ```
91
- [count: u32] [item_0] [item_1] ... [item_n]
92
- ```
93
-
94
- Count-prefixed. Each item is encoded using `T`'s
95
- `Encode` implementation. Empty vecs encode as
96
- `[0x00 0x00 0x00 0x00]`.
97
-
98
- ### `Option<T>`
99
-
100
- ```
101
- [tag: u8] [value: T]?
102
- ```
103
-
104
- - `0x00` = `None` (no value bytes follow).
105
- - `0x01` = `Some` (value bytes follow).
106
-
107
- ### Tuples `(A, B)`
108
-
109
- ```
110
- [a: A] [b: B]
111
- ```
112
-
113
- Concatenated, no separator. Both sides know the
114
- types from the schema.
115
-
116
- ### Enums
117
-
118
- ```
119
- [variant: u8] [fields...]
120
- ```
121
-
122
- One-byte discriminant followed by the variant's
123
- fields (if any). Variant numbering follows
124
- declaration order (`0, 1, 2, ...`).
125
-
126
- ## Implementations
127
-
128
- ### `KbId`
129
-
130
- ```
131
- [len: u32] [utf8_bytes: 26]
132
- ```
133
-
134
- Always 30 bytes (4-byte length prefix + 26-byte
135
- identifier). The length prefix is always `26` but
136
- included for consistency with the string format.
137
-
138
- ### `Term`
139
-
140
- ```
141
- [len: u32] [utf8_bytes: len]
142
- ```
143
-
144
- Standard string encoding. Variable length.
145
-
146
- ### `SectionType`
147
-
148
- ```
149
- [variant: u8] [mime_str...]?
150
- ```
151
-
152
- - `0x00` = `Text` (no additional data).
153
- - `0x01` = `Code` + MIME string encoding.
154
- - `0x02` = `Image` + MIME string encoding.
155
-
156
- ### `QueryPlan`
157
-
158
- ```
159
- [terms: Vec<Term>]
160
- [phrases: Vec<Vec<Term>>]
161
- [exclusions: Vec<Term>]
162
- [mode: u8]
163
- ```
164
-
165
- `mode`: `0x00` = `And`, `0x01` = `Or`.
166
-
167
- ### `FieldTf`
168
-
169
- ```
170
- [heading: f32]
171
- [body: f32]
172
- [code: f32]
173
- ```
174
-
175
- Fixed 12 bytes.
176
-
177
- ### `FieldTokenCounts`
178
-
179
- ```
180
- [heading: u32]
181
- [body: u32]
182
- [code: u32]
183
- ```
184
-
185
- Fixed 12 bytes.
186
-
187
- ### `RawMatch`
188
-
189
- ```
190
- [kbid: KbId]
191
- [field_tfs: Vec<(Term, FieldTf)>]
192
- [positions: Vec<(Term, Vec<u32>)>]
193
- [token_count: u32]
194
- [field_token_counts: FieldTokenCounts]
195
- ```
196
-
197
- ### `SectionRecord`
198
-
199
- ```
200
- [kbid: KbId]
201
- [docids: Vec<String>]
202
- [section_type: SectionType]
203
- [title: Option<String>]
204
- [description: Option<String>]
205
- [content: Vec<u8>] -- as [len: u32] [bytes]
206
- [size: u32]
207
- [created_at: String]
208
- [checksum: String]
209
- [token_count: u32]
210
- [heading_token_count: u32]
211
- [body_token_count: u32]
212
- [code_token_count: u32]
213
- ```
214
-
215
- ### `DocumentManifest`
216
-
217
- ```
218
- [docid: String]
219
- [title: String]
220
- [created_at: String]
221
- [sections: Vec<DocumentSection>]
222
- ```
223
-
224
- ### `DocumentSection`
225
-
226
- ```
227
- [kbid: KbId]
228
- [position: u32]
229
- ```
230
-
231
- ### `CorpusStats`
232
-
233
- ```
234
- [total_sections: u32]
235
- [total_terms: u32]
236
- [avg_section_length: f32]
237
- [avg_heading_length: f32]
238
- [avg_body_length: f32]
239
- [avg_code_length: f32]
240
- ```
241
-
242
- Fixed 24 bytes.
243
-
244
- ## Error Type
245
-
246
- ```rust
247
- #[derive(Debug)]
248
- pub enum DecodeError {
249
- BufferTooShort {
250
- expected: usize,
251
- available: usize,
252
- },
253
- InvalidUtf8(usize),
254
- InvalidVariant(u8),
255
- }
256
- ```
257
-
258
- | Variant | Cause |
259
- |------------------|--------------------------------|
260
- | `BufferTooShort` | Not enough bytes to read the next field |
261
- | `InvalidUtf8` | String bytes are not valid UTF-8 at the given offset |
262
- | `InvalidVariant` | Enum discriminant is out of range |
263
-
264
- ## Design Decisions
265
-
266
- **Why not serde + bincode?** The `serde` ecosystem
267
- adds significant binary size to WASM modules.
268
- `bincode` pulls in `serde` as a dependency. The data
269
- shapes here are small and known at compile time --
270
- a hand-rolled trait with ~200 lines of impl is
271
- lighter than the dependency tree.
272
-
273
- **Why not JSON?** JSON parsing on every cross-module
274
- call adds CPU overhead. The inverted index may
275
- return thousands of `RawMatch` entries per query.
276
- Binary encoding avoids string escaping, number
277
- parsing, and allocation overhead.
278
-
279
- **Why length-prefixed?** Simplicity. No delimiters
280
- to escape, no backtracking. The decoder reads the
281
- length, allocates, reads the payload, advances. Each
282
- field is independent.
283
-
284
- **Why little-endian?** WASM is little-endian. Native
285
- byte order avoids byte-swapping.
286
-
287
- ## Testing
288
-
289
- Unit tests:
290
-
291
- - Round-trip for every `Encode` implementation:
292
- encode then decode produces an equal value.
293
- - `encoded_size` matches actual `encode` output
294
- length for all types.
295
- - `DecodeError::BufferTooShort` when buffer is
296
- truncated mid-field.
297
- - `DecodeError::InvalidVariant` when enum
298
- discriminant is out of range.
299
- - `DecodeError::InvalidUtf8` when string bytes are
300
- not valid UTF-8.
301
- - Edge cases: empty `Vec`, `None` options, zero-
302
- length strings, maximum `u32` values.
303
- - Large payloads: `Vec<RawMatch>` with 1000 entries
304
- encodes and decodes correctly.
@@ -1,28 +0,0 @@
1
- # error -- Error Types
2
-
3
- *Hierarchical error enums shared across all three
4
- WASM modules, mapping to JSON-RPC 2.0 error codes at
5
- the IPC boundary.*
6
-
7
- ## Source
8
-
9
- `engine/crates/shared/src/error/`
10
-
11
- ## Responsibility
12
-
13
- Defines the unified error hierarchy so that errors
14
- originating in `fs-database` or `query-parser`
15
- propagate through `kb-worker` to the TypeScript
16
- daemon with consistent `From` conversions and
17
- JSON-RPC 2.0 code mapping.
18
-
19
- See [Worker Daemon -- Error Responses](
20
- ../../../../goals/worker-daemon.md) for the JSON-RPC
21
- 2.0 error codes at the IPC boundary.
22
-
23
- ## Exports
24
-
25
- - **[types.md](types.md)** -- `FsError`,
26
- `IndexError`, `ParseError`, `WorkerError`. `From`
27
- conversions, `Display`/`Error` implementations,
28
- JSON-RPC 2.0 code mapping.
@@ -1,302 +0,0 @@
1
- # types::error -- Error Types
2
-
3
- *Hierarchical error enums shared across all three
4
- WASM modules, mapping to JSON-RPC 2.0 error codes at
5
- the IPC boundary.*
6
-
7
- ## File
8
-
9
- `engine/crates/shared/src/error/error.rs`
10
-
11
- ## Overview
12
-
13
- Errors originate in the standalone modules
14
- (`fs-database`, `query-parser`) and propagate upward
15
- through `kb-worker` to the TypeScript daemon, which
16
- translates them into JSON-RPC 2.0 error responses.
17
- Defining all error types in `kbdb-shared` ensures a
18
- single hierarchy with consistent `From`
19
- implementations across crate boundaries.
20
-
21
- See [Worker Daemon -- Error Responses](
22
- ../../../../../goals/worker-daemon.md) for the JSON-RPC
23
- 2.0 error codes at the IPC boundary.
24
-
25
- ## Error Hierarchy
26
-
27
- ```
28
- WorkerError (kb-worker.wasm -> TypeScript)
29
- ├── Fs(FsError) (from fs-database.wasm)
30
- ├── Index(IndexError) (from fs-database.wasm)
31
- ├── Parse(ParseError) (from query-parser.wasm)
32
- ├── CacheFull
33
- ├── InvalidToken(String)
34
- └── ContextMismatch { expected, actual }
35
- ```
36
-
37
- `WorkerError` is the top-level error returned by
38
- `kb-worker.wasm` exports. It wraps lower-level
39
- errors from the standalone modules.
40
-
41
- ## Type Definitions
42
-
43
- ### `FsError`
44
-
45
- ```rust
46
- #[derive(Debug)]
47
- pub enum FsError {
48
- NotFound(String),
49
- PermissionDenied(String),
50
- IoError(String),
51
- ParseError(String),
52
- ChecksumMismatch {
53
- expected: String,
54
- actual: String,
55
- },
56
- }
57
- ```
58
-
59
- Errors from `fs-database.wasm` file operations.
60
-
61
- | Variant | Cause |
62
- |--------------------|--------------------------------|
63
- | `NotFound` | Section, document, or file path does not exist |
64
- | `PermissionDenied` | Host I/O import returned permission error |
65
- | `IoError` | Host I/O import returned generic error |
66
- | `ParseError` | TOML header or catalog parsing failed |
67
- | `ChecksumMismatch` | Section file content does not match its stored checksum |
68
-
69
- `NotFound` carries the path or kb-id that was
70
- requested. `ChecksumMismatch` carries both the
71
- expected and actual checksum strings for diagnostics.
72
-
73
- ### `IndexError`
74
-
75
- ```rust
76
- #[derive(Debug)]
77
- pub enum IndexError {
78
- Corrupted(String),
79
- KeyNotFound(String),
80
- }
81
- ```
82
-
83
- Errors from `fs-database.wasm` index operations.
84
-
85
- | Variant | Cause |
86
- |--------------|-----------------------------------|
87
- | `Corrupted` | B+ tree structure is invalid, page size mismatch, or binary data is unreadable |
88
- | `KeyNotFound`| Term not found in the inverted index (not necessarily an error -- a query for a term with no matches) |
89
-
90
- `Corrupted` indicates the index files need a
91
- rebuild. The daemon can recover by calling
92
- `worker_rebuild_indexes`.
93
-
94
- ### `ParseError`
95
-
96
- ```rust
97
- #[derive(Debug)]
98
- pub enum ParseError {
99
- InvalidSyntax(String),
100
- EmptyQuery,
101
- UnmatchedQuote(usize),
102
- }
103
- ```
104
-
105
- Errors from `query-parser.wasm` query parsing.
106
-
107
- | Variant | Cause |
108
- |------------------|--------------------------------|
109
- | `InvalidSyntax` | Unrecognized query operator or malformed expression |
110
- | `EmptyQuery` | Query string is empty or contains only stop words |
111
- | `UnmatchedQuote` | Opening `"` without a closing `"` -- carries the character position |
112
-
113
- ### `WorkerError`
114
-
115
- ```rust
116
- #[derive(Debug)]
117
- pub enum WorkerError {
118
- Fs(FsError),
119
- Index(IndexError),
120
- Parse(ParseError),
121
- CacheFull,
122
- InvalidToken(String),
123
- ContextMismatch {
124
- expected: String,
125
- actual: String,
126
- },
127
- }
128
- ```
129
-
130
- Top-level error returned by `kb-worker.wasm` exports.
131
-
132
- | Variant | Cause |
133
- |------------------|--------------------------------|
134
- | `Fs(FsError)` | Storage operation failed |
135
- | `Index(IndexError)` | Index operation failed |
136
- | `Parse(ParseError)` | Query parsing failed |
137
- | `CacheFull` | Cache limit reached and eviction failed (should not happen with LRU, but defensive) |
138
- | `InvalidToken` | Result token does not exist or has expired |
139
- | `ContextMismatch`| Request's `ctx` param does not match the daemon's context |
140
-
141
- ## `From` Implementations
142
-
143
- ```rust
144
- impl From<FsError> for WorkerError {
145
- fn from(e: FsError) -> Self {
146
- WorkerError::Fs(e)
147
- }
148
- }
149
-
150
- impl From<IndexError> for WorkerError {
151
- fn from(e: IndexError) -> Self {
152
- WorkerError::Index(e)
153
- }
154
- }
155
-
156
- impl From<ParseError> for WorkerError {
157
- fn from(e: ParseError) -> Self {
158
- WorkerError::Parse(e)
159
- }
160
- }
161
- ```
162
-
163
- These conversions allow the `?` operator to propagate
164
- errors naturally across module boundaries:
165
-
166
- ```rust
167
- fn search(...) -> Result<SearchResult, WorkerError> {
168
- let plan = parse_query(query)?; // ParseError -> WorkerError
169
- let raw = execute_plan(&plan)?; // IndexError -> WorkerError
170
- let section = read_section(id)?; // FsError -> WorkerError
171
- // ...
172
- }
173
- ```
174
-
175
- ## JSON-RPC 2.0 Mapping
176
-
177
- The TypeScript daemon maps `WorkerError` variants to
178
- JSON-RPC 2.0 error codes:
179
-
180
- | WorkerError variant | JSON-RPC code | JSON-RPC message |
181
- |---------------------|---------------|----------------------|
182
- | `Fs(NotFound)` | `-32000` | `"Not found"` |
183
- | `Fs(PermissionDenied)` | `-32000` | `"Permission denied"`|
184
- | `Fs(IoError)` | `-32000` | `"I/O error"` |
185
- | `Fs(ParseError)` | `-32000` | `"Parse error"` |
186
- | `Fs(ChecksumMismatch)` | `-32000` | `"Checksum mismatch"`|
187
- | `Index(Corrupted)` | `-32000` | `"Index corrupted"` |
188
- | `Index(KeyNotFound)`| `-32000` | `"Key not found"` |
189
- | `Parse(InvalidSyntax)` | `-32000` | `"Invalid query syntax"` |
190
- | `Parse(EmptyQuery)` | `-32000` | `"Empty query"` |
191
- | `Parse(UnmatchedQuote)` | `-32000` | `"Unmatched quote"` |
192
- | `CacheFull` | `-32000` | `"Cache full"` |
193
- | `InvalidToken` | `-32002` | `"Invalid token"` |
194
- | `ContextMismatch` | `-32001` | `"Context mismatch"` |
195
-
196
- Codes `-32001` (context mismatch) and `-32002`
197
- (invalid token) are application-specific codes
198
- defined in the [Worker Daemon](
199
- ../../../../../goals/worker-daemon.md) specification.
200
- All other errors use the generic application error
201
- code `-32000`.
202
-
203
- ## Error Relay -- WASM to Host
204
-
205
- WASM errors must reach the TypeScript process's
206
- stderr before the WASM instance is cleaned up. The
207
- relay sequence:
208
-
209
- 1. A WASM export (`worker_init`, `worker_search`,
210
- etc.) encounters an error and returns a
211
- `WorkerError` via the shared memory return
212
- convention (`set_return(ptr, len)` with the
213
- `Encode`-serialized error).
214
- 2. The TypeScript daemon **reads the error from
215
- shared memory immediately** -- before any cleanup,
216
- cache purge, or WASM teardown.
217
- 3. The daemon deserializes the `WorkerError`, maps it
218
- to the appropriate JSON-RPC error code and
219
- human-readable message.
220
- 4. **For IPC-connected errors:** The daemon sends the
221
- JSON-RPC error response to the client over IPC,
222
- then resumes normal operation (no teardown).
223
- 5. **For fatal startup errors** (precondition failures
224
- during `worker_init`): The daemon writes the error
225
- message to **stderr**, then proceeds with shutdown
226
- (purge cache -> remove PID file -> remove socket
227
- -> exit). The WASM modules remain instantiated
228
- until the error is fully read and relayed.
229
-
230
- **Key invariant:** the TypeScript host always reads
231
- and relays the WASM error before the WASM instance
232
- is purged. No error is lost to premature cleanup.
233
-
234
- ## Daemon-Level Error Codes
235
-
236
- Beyond the WASM-origin error codes (`-32000` through
237
- `-32002`), the daemon defines additional error codes
238
- for startup and client-side failures:
239
-
240
- | Code | Meaning | Origin | Relay path |
241
- |----------|--------------------------------------|--------|------------|
242
- | `-32001` | Context mismatch | WASM (`WorkerError::ContextMismatch`) | IPC response |
243
- | `-32002` | Invalid token | WASM (`WorkerError::InvalidToken`) | IPC response |
244
- | `-32003` | Daemon unavailable | Worker client (spawn failure) | `WorkerClientError` thrown to caller |
245
- | `-32004` | Database directory not found | WASM `worker_init` or TS precondition | stderr -> `WorkerClientError.daemonStderr` |
246
- | `-32005` | Database directory not accessible | WASM `worker_init` or TS precondition | stderr -> `WorkerClientError.daemonStderr` |
247
- | `-32006` | Database version too new | WASM `worker_init` | stderr -> `WorkerClientError.daemonStderr` |
248
- | `-32007` | Database version too old (needs migration) | WASM `worker_init` | stderr -> `WorkerClientError.daemonStderr` |
249
-
250
- Codes `-32004` through `-32007` are startup errors.
251
- They are never returned over IPC because the daemon
252
- exits before creating the socket. The daemon writes
253
- the error to stderr; the worker client captures
254
- stderr during the spawn window and includes it in
255
- `WorkerClientError.daemonStderr`. This relay ensures
256
- the user sees the specific failure reason even though
257
- the daemon runs as a separate detached process.
258
-
259
- ## `Display` and `Error` Implementations
260
-
261
- All error types implement `std::fmt::Display` and
262
- `std::error::Error`:
263
-
264
- ```rust
265
- impl fmt::Display for FsError {
266
- fn fmt(&self, f: &mut fmt::Formatter<'_>)
267
- -> fmt::Result
268
- {
269
- match self {
270
- Self::NotFound(p) =>
271
- write!(f, "not found: {p}"),
272
- Self::PermissionDenied(p) =>
273
- write!(f, "permission denied: {p}"),
274
- Self::IoError(msg) =>
275
- write!(f, "I/O error: {msg}"),
276
- Self::ParseError(msg) =>
277
- write!(f, "parse error: {msg}"),
278
- Self::ChecksumMismatch { expected, actual } =>
279
- write!(
280
- f,
281
- "checksum mismatch: expected {expected}, got {actual}",
282
- ),
283
- }
284
- }
285
- }
286
- ```
287
-
288
- `WorkerError::source()` returns the wrapped inner
289
- error for `Fs`, `Index`, and `Parse` variants,
290
- enabling error chain inspection.
291
-
292
- ## Testing
293
-
294
- Unit tests:
295
-
296
- - `From` conversions: each error type converts to
297
- the correct `WorkerError` variant.
298
- - `Display` formatting: each variant produces a
299
- human-readable message.
300
- - `source()` chain: `WorkerError::Fs(e).source()`
301
- returns `Some(&e)`.
302
- - All variants are `Debug`-printable without panic.
@@ -1,24 +0,0 @@
1
- # kbid -- Content-Addressed Identifier
2
-
3
- *Content-addressed 26-character section identifier
4
- derived from SHA-256 hashing.*
5
-
6
- ## Source
7
-
8
- `engine/crates/shared/src/kbid/`
9
-
10
- ## Responsibility
11
-
12
- Defines the `KbId` newtype and its construction
13
- methods. Every section in the knowledge base is
14
- identified by a deterministic, collision-resistant,
15
- URL-safe string derived from the section's content.
16
-
17
- See [Database -- Sections](../../../../goals/database.md)
18
- for the behavioral specification of content-
19
- addressable storage.
20
-
21
- ## Exports
22
-
23
- - **[types.md](types.md)** -- `KbId` newtype,
24
- `from_content`, `new`, `shard_prefix`, `KbIdError`.