@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,319 +0,0 @@
1
- # Modules Overview -- Build Pipeline
2
-
3
- *How the Rust engine compiles to WASM, lands in the
4
- TypeScript source tree, and gets bundled into the
5
- final `dist/` output.*
6
-
7
- ## Build Pipeline
8
-
9
- ```
10
- 1. Rust/WASM 2. TypeScript
11
- ───────────── ────────────────
12
-
13
- engine/crates/ src/shared/
14
- ├── shared (rlib) ├── worker-daemon/
15
- ├── fs-database (cdylib) │ ├── wasm/ ← 4 WASM modules
16
- ├── query-parser (cdylib) │ │ ├── fs-database.*
17
- ├── default-embedding (cdylib)│ │ ├── query-parser.*
18
- └── kb-worker (cdylib) │ │ ├── default-embedding.*
19
- │ │ │ └── kb-worker.*
20
- │ wasm-pack build │ └── functions/
21
- └─────────────────────┘ └── load-wasm-modules
22
- .function.ts
23
-
24
- │ esbuild + tsc
25
-
26
- dist/
27
- ├── cli.mjs
28
- ├── worker.mjs
29
- ├── mod.mjs
30
- └── ...
31
- ```
32
-
33
- ## Phase 1 -- Build Rust to WASM
34
-
35
- The Rust engine compiles to four standalone `.wasm`
36
- files. The `kbdb-shared` crate is an `rlib` -- it
37
- produces no artifact, compiled **into** each consumer
38
- via dead code elimination.
39
-
40
- ### Crates
41
-
42
- | Crate | Type | Output |
43
- |--------------------|----------|-----------------------|
44
- | `kbdb-shared` | `rlib` | none (compiled into consumers) |
45
- | `kbdb-fs-database` | `cdylib` | `fs-database.wasm` |
46
- | `kbdb-query-parser`| `cdylib` | `query-parser.wasm` |
47
- | `kbdb-default-embedding` | `cdylib` | `default-embedding.wasm` |
48
- | `kbdb-worker` | `cdylib` | `kb-worker.wasm` |
49
-
50
- ### Build Order
51
-
52
- ```
53
- 1. kbdb-shared (leaf, no deps)
54
- 2. kbdb-fs-database ┐ (all depend on shared,
55
- kbdb-query-parser │ built in parallel)
56
- kbdb-default-embedding ┘
57
- 3. kbdb-worker (depends on all above)
58
- ```
59
-
60
- ### Build Command
61
-
62
- ```sh
63
- npm run build:wasm
64
- ```
65
-
66
- This runs `wasm-pack build` for each cdylib crate.
67
- Output lands in the TypeScript source tree at
68
- `src/wasm/`:
69
-
70
- ```sh
71
- wasm-pack build crates/fs-database \
72
- --target no-modules \
73
- --out-dir ../../../src/wasm/fs-database
74
-
75
- wasm-pack build crates/query-parser \
76
- --target no-modules \
77
- --out-dir ../../../src/wasm/query-parser
78
-
79
- wasm-pack build crates/default-embedding \
80
- --target no-modules \
81
- --out-dir ../../../src/wasm/default-embedding
82
-
83
- wasm-pack build crates/kb-worker \
84
- --target no-modules \
85
- --out-dir ../../../src/wasm/kb-worker
86
- ```
87
-
88
- ### Size Optimization
89
-
90
- ```toml
91
- # engine/Cargo.toml [profile.release]
92
- opt-level = "z" # optimize for size
93
- lto = true # link-time optimization
94
- codegen-units = 1 # single codegen unit
95
- strip = true # remove debug symbols
96
- panic = "abort" # no unwind tables
97
- ```
98
-
99
- Each `.wasm` includes only the `kbdb-shared` code it
100
- actually references -- LLVM eliminates unused code
101
- per consumer.
102
-
103
- **Full Rust architecture:**
104
- [rust/overview.md](rust/overview.md)
105
-
106
- ---
107
-
108
- ## Phase 2 -- WASM in TypeScript Source
109
-
110
- The WASM build output lands inside the TypeScript
111
- source tree, where the `worker-daemon` module loads
112
- it at runtime:
113
-
114
- ```
115
- src/shared/worker-daemon/
116
- ├── wasm/ ← gitignored
117
- │ ├── fs-database/
118
- │ │ ├── fs-database.wasm
119
- │ │ ├── fs-database.js ← JS glue
120
- │ │ └── fs-database.d.ts ← type defs
121
- │ ├── query-parser/
122
- │ │ ├── query-parser.wasm
123
- │ │ ├── query-parser.js
124
- │ │ └── query-parser.d.ts
125
- │ ├── default-embedding/
126
- │ │ ├── default-embedding.wasm
127
- │ │ ├── default-embedding.js
128
- │ │ └── default-embedding.d.ts
129
- │ └── kb-worker/
130
- │ ├── kb-worker.wasm
131
- │ ├── kb-worker.js
132
- │ └── kb-worker.d.ts
133
-
134
- └── functions/
135
- └── load-wasm-modules.function.ts
136
- ```
137
-
138
- The `wasm/` directory is **gitignored** -- it is a
139
- build artifact, regenerated by `npm run build:wasm`.
140
- Never edit files inside it manually.
141
-
142
- ### How TypeScript Loads WASM
143
-
144
- The `loadWasmModules` function in
145
- `shared/worker-daemon/` performs the loading sequence:
146
-
147
- ```
148
- loadWasmModules(config)
149
-
150
- ├─ 1. Allocate shared WebAssembly.Memory
151
-
152
- ├─ 2. Compile + instantiate query-parser.wasm
153
- │ (standalone, shared memory only)
154
-
155
- ├─ 3. Compile + instantiate fs-database.wasm
156
- │ (standalone, shared memory + host I/O)
157
- │ ↑ steps 2 and 3 run in parallel
158
-
159
- ├─ 4. Compile + instantiate default-embedding.wasm
160
- │ (standalone, no host imports -- pure computation)
161
-
162
- └─ 5. Compile + instantiate kb-worker.wasm
163
- (imports query-parser + fs-database exports)
164
- ```
165
-
166
- All four WASM modules share one `WebAssembly.Memory`
167
- instance. Data crosses module boundaries via
168
- `(pointer, length)` pairs encoded with the `Encode`
169
- trait from `kbdb-shared`.
170
-
171
- Only the `worker-daemon` module imports WASM. No other
172
- TypeScript module directly accesses WASM files or the
173
- `WebAssembly` API.
174
-
175
- ---
176
-
177
- ## Phase 3 -- TypeScript Modules
178
-
179
- All TypeScript logic lives under `src/shared/` in
180
- 9 sub-modules. Two thin entry points at the project
181
- root dispatch into them.
182
-
183
- ### Entry Points
184
-
185
- | File | Purpose |
186
- |--------------------|---------------------------|
187
- | `src/cli.ts` | CLI + MCP executable |
188
- | `src/worker.ts` | Worker daemon executable |
189
- | `mod.ts` | Library API (npm / JSR) |
190
-
191
- ### Shared Modules
192
-
193
- | Module | Responsibility |
194
- |------------------|----------------------------|
195
- | `cli/` | CLI arg parsing, command handlers, output formatting |
196
- | `mcp/` | MCP server, tool + resource handlers |
197
- | `worker-client/` | IPC client -- discovers/spawns daemon, typed async API |
198
- | `worker-daemon/` | Daemon process -- loads WASM, IPC server, config |
199
- | `version/` | Package version constant |
200
- | `runtime/` | Deno / Node.js detection |
201
- | `platform/` | OS abstractions (tmpdir, PID, IPC paths) |
202
- | `hash/` | SHA-256 via Web Crypto API |
203
- | `log/` | Structured logging with configurable levels and ANSI formatting |
204
-
205
- ### Module Dependencies
206
-
207
- ```
208
- src/cli.ts
209
- ├── shared/cli/ ──→ shared/worker-client/
210
- │ └── shared/log/
211
- └── shared/mcp/ ──→ shared/worker-client/
212
- │ ├── shared/platform/
213
- │ ├── shared/hash/
214
- │ └── shared/log/
215
- └── shared/log/
216
-
217
- src/worker.ts
218
- └── shared/worker-daemon/
219
- ├── shared/platform/
220
- ├── shared/log/
221
- └── wasm/ (fs-database, query-parser,
222
- default-embedding, kb-worker)
223
-
224
- mod.ts
225
- └── shared/version/
226
-
227
- shared/log/ (no dependencies -- standalone)
228
- ```
229
-
230
- **Full TypeScript architecture:**
231
- [typescript/overview.md](typescript/overview.md)
232
-
233
- ---
234
-
235
- ## Phase 4 -- Build to dist/
236
-
237
- esbuild bundles the TypeScript entry points into
238
- `dist/` as ESM (with code-splitting) and CJS. `tsc`
239
- emits declaration files separately.
240
-
241
- ### Build Command
242
-
243
- ```sh
244
- npm run build # TypeScript only
245
- npm run build:all # WASM + TypeScript
246
- ```
247
-
248
- ### Build Steps
249
-
250
- ```
251
- 1. esbuild bundles:
252
- ├── src/cli.ts → dist/cli.mjs + dist/cli.cjs
253
- ├── src/worker.ts → dist/worker.mjs + dist/worker.cjs
254
- ├── mod.ts → dist/mod.mjs + dist/mod.cjs
255
- └── shared code → dist/chunk-*.mjs (code-split)
256
-
257
- 2. tsc --emitDeclarationOnly:
258
- └── *.d.ts files for all entry points
259
- ```
260
-
261
- ### Output
262
-
263
- ```
264
- dist/
265
- ├── cli.mjs ← CLI + MCP (ESM, shebang)
266
- ├── cli.cjs ← CLI + MCP (CJS, shebang)
267
- ├── worker.mjs ← daemon (ESM, shebang)
268
- ├── worker.cjs ← daemon (CJS, shebang)
269
- ├── mod.mjs ← library API (ESM)
270
- ├── mod.cjs ← library API (CJS)
271
- ├── mod.d.ts ← declarations
272
- ├── cli.d.ts
273
- ├── worker.d.ts
274
- ├── chunk-*.mjs ← shared code chunks
275
- └── src/
276
- └── shared/
277
- └── **/*.d.ts ← per-file declarations
278
- ```
279
-
280
- WASM files are **not** bundled into `dist/`. They ship
281
- as separate `.wasm` binaries in the npm package and
282
- are loaded at runtime by the daemon via
283
- `WebAssembly.compile()`.
284
-
285
- ---
286
-
287
- ## Full Build Sequence
288
-
289
- ```sh
290
- # 1. Build WASM (Rust → .wasm → src/wasm/)
291
- npm run build:wasm
292
-
293
- # 2. Build TypeScript (src/ → dist/)
294
- npm run build
295
-
296
- # Or both in one command:
297
- npm run build:all
298
- ```
299
-
300
- ### build:all Sequence
301
-
302
- ```
303
- build:all
304
-
305
- ├─ build:wasm
306
- │ ├─ cargo build (5 crates)
307
- │ └─ wasm-pack build (4 cdylib → wasm/)
308
-
309
- └─ build
310
- ├─ esbuild (2 entry points → dist/)
311
- └─ tsc --emitDeclarationOnly (→ dist/*.d.ts)
312
- ```
313
-
314
- ## Module Documentation Index
315
-
316
- | Layer | Overview | Modules |
317
- |------------|-----------------------------------|---------|
318
- | Rust/WASM | [rust/overview.md](rust/overview.md) | 5 crates, 9 shared namespaces |
319
- | TypeScript | [typescript/overview.md](typescript/overview.md) | 2 entry points, 9 shared modules |
@@ -1,91 +0,0 @@
1
- # default-embedding -- TF-IDF Vector Computation
2
-
3
- **Source:** `engine/crates/default-embedding/`
4
- **Crate type:** `cdylib` + `rlib`
5
- **Output:** `default-embedding.wasm`
6
-
7
- ## Responsibility
8
-
9
- Computes feature-hashed sublinear TF vectors for the
10
- default embedding provider. Reuses kbdb-shared's
11
- 9-stage tokenization pipeline (NFC normalization,
12
- Unicode segmentation, camelCase splitting, stop-word
13
- removal, synonym expansion, Snowball stemming) so
14
- that TF-IDF vectors are token-consistent with the
15
- lexical index.
16
-
17
- No host imports -- pure computation with no file I/O.
18
- The WASM module is instantiated without an import
19
- object.
20
-
21
- ## Algorithm
22
-
23
- 1. Tokenize text via `kbdb_shared::pipeline::tokenize`
24
- 2. Count term frequencies on stemmed tokens
25
- 3. Hash each stemmed token to a bucket using FNV-1a:
26
- `bucket = fnv1a(token) % 256`
27
- 4. Apply sign trick to reduce collision bias:
28
- `sign = if fnv1a(token + "_s") % 2 == 0 { 1 } else { -1 }`
29
- 5. Accumulate `sign * (1.0 + tf.ln())` per bucket
30
- (sublinear TF provides diminishing returns for
31
- repeated terms)
32
- 6. L2-normalize the vector
33
-
34
- Output: 256-dimensional `Float32Array`, suitable for
35
- cosine similarity via `cosineSimilarity()` in the
36
- TypeScript embedding module.
37
-
38
- ## Directory Layout
39
-
40
- ```
41
- engine/crates/default-embedding/
42
- ├── Cargo.toml
43
- └── src/
44
- ├── lib.rs ← WASM exports
45
- ├── mem.rs ← shared-memory helpers
46
- ├── fnv1a.rs ← FNV-1a hash function
47
- └── feature_hash.rs ← feature hashing + L2 norm
48
- ```
49
-
50
- ## WASM Exports
51
-
52
- | Export | Signature | Description |
53
- |-------------------------------|------------------------|-------------|
54
- | `embedding_tfidf` | `(ptr, len) -> u32` | Single text → 256×f32 vector |
55
- | `embedding_tfidf_batch` | `(ptr, len) -> u32` | Newline-delimited texts → count + count×256 f32s |
56
- | `embedding_dimensions` | `() -> u32` | Returns 256 |
57
- | `embedding_model_name` | `(ptr, len) -> u32` | Writes `tfidf-v1` to return slot |
58
-
59
- All exports use the standard return-slot protocol:
60
- result data is written via `mem::set_return(ptr, len)`
61
- and read by TypeScript via `get_return_ptr` /
62
- `get_return_len`.
63
-
64
- ## Dependencies
65
-
66
- ```toml
67
- [dependencies]
68
- kbdb-shared = { path = "../shared" }
69
- wasm-bindgen = "0.2"
70
- ```
71
-
72
- - **kbdb-shared** -- memory management (`kbdb_alloc`,
73
- `kbdb_free`, `set_return`, `read_str`) and
74
- tokenization pipeline (`pipeline::tokenize`,
75
- `pipeline::TokenResult`)
76
-
77
- ## Dependency Direction
78
-
79
- ```
80
- kbdb-shared (rlib)
81
- │ provides: memory, pipeline::tokenize
82
-
83
- └──→ kbdb-default-embedding (cdylib)
84
- uses: shared memory + shared tokenization
85
- consumed by: TypeScript TfidfEmbeddingProvider
86
- ```
87
-
88
- The embedding crate is a leaf in the dependency graph.
89
- No other Rust crate depends on it. It is loaded as a
90
- standalone WASM instance by the TypeScript worker
91
- daemon.
@@ -1,48 +0,0 @@
1
- # functions::document -- Document Manifest CRUD
2
-
3
- *Document manifest operations: read, write, remove,
4
- and section reference updates.*
5
-
6
- ## Source
7
-
8
- `engine/crates/fs-database/src/document.rs`
9
-
10
- ## Functions
11
-
12
- ```rust
13
- pub fn read_document(docid: &str)
14
- -> Result<DocumentManifest, FsError>;
15
-
16
- pub fn write_document(manifest: &DocumentManifest)
17
- -> Result<(), FsError>;
18
-
19
- pub fn remove_document(docid: &str)
20
- -> Result<u32, FsError>;
21
-
22
- pub fn update_section_ref(
23
- old_kbid: &KbId, new_kbid: &KbId,
24
- ) -> Result<u32, FsError>;
25
- ```
26
-
27
- ### `write_document`
28
-
29
- Serializes the manifest to TOML and writes to
30
- `documents/{docid}.toml` atomically. Appends the
31
- docid to each referenced section's `docids` array
32
- in its header. Updates catalog.
33
-
34
- ### `remove_document`
35
-
36
- Deletes the manifest file. Removes the docid from
37
- each section's `docids` array in its header.
38
- Sections themselves are not deleted -- only the
39
- grouping is removed. Returns the number of
40
- ungrouped sections.
41
-
42
- ### `update_section_ref`
43
-
44
- Scans all document manifests and replaces occurrences
45
- of `old_kbid` with `new_kbid`, preserving position.
46
- Called during section updates (content-addressable
47
- storage produces new kb-ids on content change).
48
- Returns the number of updated documents.
@@ -1,79 +0,0 @@
1
- # functions::integrity -- Integrity and Maintenance
2
-
3
- *Integrity checks, garbage collection, and full index
4
- rebuild.*
5
-
6
- ## Source
7
-
8
- `engine/crates/fs-database/src/integrity.rs`
9
-
10
- ## Functions
11
-
12
- ```rust
13
- pub fn check_integrity()
14
- -> Result<IntegrityReport, FsError>;
15
-
16
- pub fn garbage_collect()
17
- -> Result<u32, FsError>;
18
-
19
- pub fn rebuild_indexes()
20
- -> Result<(), FsError>;
21
- ```
22
-
23
- ### `check_integrity`
24
-
25
- - Verify section file checksums match content.
26
- - Detect orphaned sections (not referenced by any
27
- document).
28
- - Detect orphaned documents (referencing missing
29
- sections).
30
- - Find broken content marker references across all
31
- six marker types:
32
- - `$(image: {kbid})` -- referenced section must
33
- exist and have type `image/*`.
34
- - `$(code: {kbid})` -- referenced section must
35
- exist and have type `text/*` or
36
- `application/json`.
37
- - `$(text: {kbid})` -- referenced section must
38
- exist and have type `text/markdown`.
39
- - `$(document: {docid})` -- referenced document
40
- manifest must exist.
41
- - `$(link: {kbid})` -- referenced section must
42
- exist (any type).
43
- - `$(doc-link: {docid})` -- referenced document
44
- manifest must exist.
45
- - Detect circular references in `$(text:)` and
46
- `$(document:)` marker chains.
47
- - Detect **divergent content**: sections sharing the
48
- same `source_key` that have different content.
49
- Indicates contradictory versions that should be
50
- resolved via supersession. Reported as
51
- `divergent_content` entries in `IntegrityReport`.
52
-
53
- ### `garbage_collect`
54
-
55
- Remove section files not referenced by any document
56
- manifest. Returns the number of removed files.
57
-
58
- Also reports `stale_sources` -- an advisory count of
59
- sections whose `source_path` points to a file that no
60
- longer contains their heading text. Stale sections
61
- are not automatically removed (they may still be
62
- referenced by documents); the count is informational.
63
-
64
- ### `rebuild_indexes`
65
-
66
- Reconstruct all index files (`terms.idx`,
67
- `positions.idx`, `corpus.dat`) from sections and
68
- document manifests. Recovers from index corruption.
69
-
70
- Rebuild recomputes per-field TFs (heading, body,
71
- code) from section content via per-field
72
- tokenization, recomputes per-field token counts
73
- (`heading_token_count`, `body_token_count`,
74
- `code_token_count`) in section headers, and
75
- recalculates per-field corpus averages
76
- (`avg_heading_length`, `avg_body_length`,
77
- `avg_code_length`) in `corpus.dat`. Image sections
78
- have their title and description re-tokenized into
79
- heading and body fields respectively.
@@ -1,87 +0,0 @@
1
- # functions::io -- Host Import Wrappers
2
-
3
- *Raw extern FFI bindings for filesystem operations
4
- provided by the TypeScript host.*
5
-
6
- ## Source
7
-
8
- `engine/crates/fs-database/src/io.rs`
9
-
10
- ## Overview
11
-
12
- `fs-database.wasm` cannot access the filesystem
13
- directly. The TypeScript daemon provides file I/O
14
- functions as WASM imports when instantiating the
15
- module. All paths are relative to the `.kbdb` context
16
- directory -- the host resolves them to absolute paths
17
- and enforces sandbox boundaries.
18
-
19
- ## Host Imports
20
-
21
- ```rust
22
- #[link(wasm_import_module = "kbdb_host")]
23
- extern "C" {
24
- fn host_read_file(
25
- path_ptr: *const u8, path_len: u32,
26
- buf_ptr: *mut u8, buf_len: u32,
27
- ) -> i32;
28
-
29
- fn host_write_file(
30
- path_ptr: *const u8, path_len: u32,
31
- data_ptr: *const u8, data_len: u32,
32
- ) -> i32;
33
-
34
- fn host_remove_file(
35
- path_ptr: *const u8, path_len: u32,
36
- ) -> i32;
37
-
38
- fn host_list_dir(
39
- path_ptr: *const u8, path_len: u32,
40
- buf_ptr: *mut u8, buf_len: u32,
41
- ) -> i32;
42
-
43
- fn host_mkdir(
44
- path_ptr: *const u8, path_len: u32,
45
- ) -> i32;
46
-
47
- fn host_file_exists(
48
- path_ptr: *const u8, path_len: u32,
49
- ) -> i32;
50
-
51
- fn host_rename(
52
- from_ptr: *const u8, from_len: u32,
53
- to_ptr: *const u8, to_len: u32,
54
- ) -> i32;
55
- }
56
- ```
57
-
58
- ## Return Conventions
59
-
60
- - Positive integer = bytes written or entry count.
61
- - `0` = success (for void operations).
62
- - `-1` = file not found.
63
- - `-2` = permission denied.
64
- - `-3` = I/O error.
65
-
66
- The `io.rs` module wraps these raw imports in safe
67
- Rust functions that return `Result<T, IoError>`.
68
-
69
- ## Shared Memory
70
-
71
- All data exchange with the host uses the shared
72
- `WebAssembly.Memory` buffer. Strings and byte slices
73
- are passed as `(pointer, length)` pairs. The crate
74
- re-exports `alloc` and `free` from `kbdb-shared` for
75
- the host to manage buffer allocation.
76
-
77
- ## Atomic Writes (`atomic.rs`)
78
-
79
- ```rust
80
- pub fn atomic_write(
81
- path: &str, data: &[u8],
82
- ) -> Result<(), FsError>;
83
- ```
84
-
85
- Writes data to a temporary file in the same
86
- directory, then renames it into place via
87
- `host_rename`. Prevents partial writes on crash.
@@ -1,22 +0,0 @@
1
- # fs-database/functions -- Operations
2
-
3
- *Functions exported by the fs-database crate for
4
- section CRUD, document manifest management, query
5
- execution, and maintenance.*
6
-
7
- ## Modules
8
-
9
- - **[io](io.md)** -- Host import wrappers. Raw extern
10
- FFI bindings for filesystem operations, return
11
- conventions, shared memory protocol.
12
- - **[section](section.md)** -- Section CRUD operations.
13
- `read_section`, `add_section`, `update_section`,
14
- `remove_section`.
15
- - **[document](document.md)** -- Document manifest
16
- CRUD. `read_document`, `write_document`,
17
- `remove_document`, `update_section_ref`.
18
- - **[query-exec](query-exec.md)** -- Query plan
19
- execution against indexes. Returns raw matches with
20
- term frequencies and positional data.
21
- - **[integrity](integrity.md)** -- Integrity checks,
22
- garbage collection, full index rebuild.
@@ -1,44 +0,0 @@
1
- # functions::query-exec -- Query Plan Execution
2
-
3
- *Execute pre-parsed query plans against the indexes
4
- and return raw matches.*
5
-
6
- ## Source
7
-
8
- `engine/crates/fs-database/src/query_exec.rs`
9
-
10
- ## Function
11
-
12
- ```rust
13
- pub fn execute_plan(
14
- plan: &QueryPlan,
15
- inverted: &InvertedIndex,
16
- positional: &PositionalIndex,
17
- corpus: &CorpusStats,
18
- ) -> Vec<RawMatch>;
19
- ```
20
-
21
- Does not parse queries or rank results -- those are
22
- `query-parser.wasm` responsibilities orchestrated by
23
- `kb-worker.wasm`.
24
-
25
- ## Execution Steps
26
-
27
- 1. Look up each term in `plan.terms` in the inverted
28
- index.
29
- 2. Combine posting lists based on `plan.mode`:
30
- - `And` -- intersect all posting lists.
31
- - `Or` -- union all posting lists.
32
- 3. Remove sections matching any term in
33
- `plan.exclusions`.
34
- 4. For phrase queries (`plan.phrases`), verify
35
- adjacent positions in the positional index.
36
- 5. For each surviving section, collect per-field
37
- term frequencies (heading_tf, body_tf, code_tf
38
- from inverted index), positional data from the
39
- positional index, `token_count` and
40
- `field_token_counts` from the section header
41
- into a `RawMatch`.
42
-
43
- Returns raw matches. `kb-worker.wasm` passes these to
44
- `query-parser.wasm`'s ranking functions for scoring.