@dikolab/kbdb 0.1.5 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (193) 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.mjs +13 -2
  11. package/dist/mod.mjs.map +2 -2
  12. package/dist/src/shared/cli/functions/format-output.function.d.ts +7 -7
  13. package/dist/src/shared/cli/functions/run-search.function.d.ts +25 -16
  14. package/dist/src/shared/cli/index.d.ts +1 -1
  15. package/dist/src/shared/cli/typings/cli-options.interface.d.ts +2 -0
  16. package/dist/src/shared/cli/typings/search-display.model.d.ts +25 -7
  17. package/dist/src/shared/mcp/functions/handle-recall.function.d.ts +29 -0
  18. package/dist/src/shared/mcp/typings/mcp-client.interface.d.ts +30 -11
  19. package/dist/src/shared/version/constants/version.constant.d.ts +1 -1
  20. package/dist/src/shared/wasm-codec/functions/decode-recall-result.function.d.ts +28 -0
  21. package/dist/src/shared/wasm-codec/functions/decode-scored-results.function.d.ts +14 -7
  22. package/dist/src/shared/wasm-codec/functions/encode-recall-params.function.d.ts +21 -0
  23. package/dist/src/shared/wasm-codec/functions/encode-search-params.function.d.ts +1 -1
  24. package/dist/src/shared/wasm-codec/index.d.ts +5 -2
  25. package/dist/src/shared/worker-client/classes/worker-client.class.d.ts +13 -3
  26. package/dist/src/shared/worker-client/index.d.ts +2 -1
  27. package/dist/src/shared/worker-client/typings/recall-result.model.d.ts +94 -0
  28. package/dist/src/shared/worker-client/typings/search-params.model.d.ts +2 -0
  29. package/dist/src/shared/worker-client/typings/search-result.model.d.ts +23 -7
  30. package/dist/src/shared/worker-daemon/classes/worker-daemon.class.d.ts +1 -0
  31. package/dist/wasm/fs-database/kbdb_fs_database.d.ts +18 -0
  32. package/dist/wasm/fs-database/kbdb_fs_database.js +30 -0
  33. package/dist/wasm/fs-database/kbdb_fs_database_bg.wasm +0 -0
  34. package/dist/wasm/fs-database/kbdb_fs_database_bg.wasm.d.ts +2 -0
  35. package/dist/wasm/kb-worker/kbdb_worker.d.ts +38 -0
  36. package/dist/wasm/kb-worker/kbdb_worker.js +56 -0
  37. package/dist/wasm/kb-worker/kbdb_worker_bg.wasm +0 -0
  38. package/dist/wasm/kb-worker/kbdb_worker_bg.wasm.d.ts +3 -0
  39. package/dist/worker.mjs +283 -29
  40. package/dist/worker.mjs.map +4 -4
  41. package/docs/details/README.md +26 -0
  42. package/docs/details/cli.md +608 -0
  43. package/docs/details/library-api.md +406 -0
  44. package/docs/details/mcp-server.md +430 -0
  45. package/docs/details/search-and-ranking.md +457 -0
  46. package/docs/details/storage.md +529 -0
  47. package/docs/goals/agents.md +751 -0
  48. package/docs/goals/architecture.svg +198 -0
  49. package/docs/goals/cli.md +2308 -0
  50. package/docs/goals/content-composer.md +609 -0
  51. package/docs/goals/content-parser.md +279 -0
  52. package/docs/goals/database.md +1452 -0
  53. package/docs/goals/document.md +368 -0
  54. package/docs/goals/mcp.md +1467 -0
  55. package/docs/goals/overview.md +124 -0
  56. package/docs/goals/query-parser.md +271 -0
  57. package/docs/goals/query-result.md +693 -0
  58. package/docs/goals/skills.md +738 -0
  59. package/docs/goals/worker-client.md +944 -0
  60. package/docs/goals/worker-daemon.md +1447 -0
  61. package/docs/modules/overview.md +298 -0
  62. package/docs/modules/rust/fs-database/functions/document.md +48 -0
  63. package/docs/modules/rust/fs-database/functions/integrity.md +68 -0
  64. package/docs/modules/rust/fs-database/functions/io.md +87 -0
  65. package/docs/modules/rust/fs-database/functions/module.md +22 -0
  66. package/docs/modules/rust/fs-database/functions/query-exec.md +44 -0
  67. package/docs/modules/rust/fs-database/functions/section.md +59 -0
  68. package/docs/modules/rust/fs-database/indexes/btree.md +35 -0
  69. package/docs/modules/rust/fs-database/indexes/corpus.md +61 -0
  70. package/docs/modules/rust/fs-database/indexes/inverted.md +60 -0
  71. package/docs/modules/rust/fs-database/indexes/module.md +64 -0
  72. package/docs/modules/rust/fs-database/indexes/positional.md +39 -0
  73. package/docs/modules/rust/fs-database/module.md +149 -0
  74. package/docs/modules/rust/fs-database/types/catalog.md +53 -0
  75. package/docs/modules/rust/fs-database/types/module.md +12 -0
  76. package/docs/modules/rust/fs-database/types/section.md +101 -0
  77. package/docs/modules/rust/kb-worker/functions/compose.md +57 -0
  78. package/docs/modules/rust/kb-worker/functions/module.md +20 -0
  79. package/docs/modules/rust/kb-worker/functions/retrieve.md +194 -0
  80. package/docs/modules/rust/kb-worker/functions/search.md +173 -0
  81. package/docs/modules/rust/kb-worker/functions/write.md +172 -0
  82. package/docs/modules/rust/kb-worker/module.md +171 -0
  83. package/docs/modules/rust/kb-worker/types/cache.md +90 -0
  84. package/docs/modules/rust/kb-worker/types/context.md +26 -0
  85. package/docs/modules/rust/kb-worker/types/module.md +16 -0
  86. package/docs/modules/rust/kb-worker/types/status.md +29 -0
  87. package/docs/modules/rust/kb-worker/types/token.md +50 -0
  88. package/docs/modules/rust/overview.md +495 -0
  89. package/docs/modules/rust/query-parser/functions/extract.md +56 -0
  90. package/docs/modules/rust/query-parser/functions/module.md +16 -0
  91. package/docs/modules/rust/query-parser/functions/parse.md +33 -0
  92. package/docs/modules/rust/query-parser/functions/pipeline.md +103 -0
  93. package/docs/modules/rust/query-parser/functions/rank.md +85 -0
  94. package/docs/modules/rust/query-parser/module.md +136 -0
  95. package/docs/modules/rust/query-parser/types/cache.md +39 -0
  96. package/docs/modules/rust/query-parser/types/keyphrase.md +60 -0
  97. package/docs/modules/rust/query-parser/types/module.md +17 -0
  98. package/docs/modules/rust/query-parser/types/token.md +42 -0
  99. package/docs/modules/rust/shared/content-composer/functions.md +114 -0
  100. package/docs/modules/rust/shared/content-composer/module.md +147 -0
  101. package/docs/modules/rust/shared/content-composer/types.md +93 -0
  102. package/docs/modules/rust/shared/content-parser/functions.md +72 -0
  103. package/docs/modules/rust/shared/content-parser/module.md +99 -0
  104. package/docs/modules/rust/shared/content-parser/types.md +71 -0
  105. package/docs/modules/rust/shared/corpus/module.md +24 -0
  106. package/docs/modules/rust/shared/corpus/types.md +141 -0
  107. package/docs/modules/rust/shared/document/module.md +25 -0
  108. package/docs/modules/rust/shared/document/types.md +154 -0
  109. package/docs/modules/rust/shared/encode/module.md +22 -0
  110. package/docs/modules/rust/shared/encode/traits.md +304 -0
  111. package/docs/modules/rust/shared/error/module.md +28 -0
  112. package/docs/modules/rust/shared/error/types.md +302 -0
  113. package/docs/modules/rust/shared/kbid/module.md +24 -0
  114. package/docs/modules/rust/shared/kbid/types.md +147 -0
  115. package/docs/modules/rust/shared/memory/functions.md +209 -0
  116. package/docs/modules/rust/shared/memory/module.md +24 -0
  117. package/docs/modules/rust/shared/module.md +168 -0
  118. package/docs/modules/rust/shared/query/module.md +27 -0
  119. package/docs/modules/rust/shared/query/types.md +236 -0
  120. package/docs/modules/rust/shared/section/module.md +25 -0
  121. package/docs/modules/rust/shared/section/types.md +294 -0
  122. package/docs/modules/rust/shared/term/module.md +25 -0
  123. package/docs/modules/rust/shared/term/types.md +139 -0
  124. package/docs/modules/typescript/cli.md +129 -0
  125. package/docs/modules/typescript/kbdb-worker.md +135 -0
  126. package/docs/modules/typescript/overview.md +378 -0
  127. package/docs/modules/typescript/shared/cli/constants.md +23 -0
  128. package/docs/modules/typescript/shared/cli/functions.md +630 -0
  129. package/docs/modules/typescript/shared/cli/module.md +125 -0
  130. package/docs/modules/typescript/shared/cli/typings.md +172 -0
  131. package/docs/modules/typescript/shared/hash/functions.md +20 -0
  132. package/docs/modules/typescript/shared/hash/module.md +21 -0
  133. package/docs/modules/typescript/shared/mcp/classes.md +179 -0
  134. package/docs/modules/typescript/shared/mcp/functions.md +386 -0
  135. package/docs/modules/typescript/shared/mcp/module.md +160 -0
  136. package/docs/modules/typescript/shared/mcp/typings.md +522 -0
  137. package/docs/modules/typescript/shared/module.md +94 -0
  138. package/docs/modules/typescript/shared/platform/functions.md +42 -0
  139. package/docs/modules/typescript/shared/platform/module.md +25 -0
  140. package/docs/modules/typescript/shared/runtime/functions.md +26 -0
  141. package/docs/modules/typescript/shared/runtime/module.md +27 -0
  142. package/docs/modules/typescript/shared/runtime/typings.md +13 -0
  143. package/docs/modules/typescript/shared/version/module.md +27 -0
  144. package/docs/modules/typescript/shared/wasm-codec/functions.md +391 -0
  145. package/docs/modules/typescript/shared/wasm-codec/module.md +98 -0
  146. package/docs/modules/typescript/shared/worker-client/classes.md +264 -0
  147. package/docs/modules/typescript/shared/worker-client/functions.md +175 -0
  148. package/docs/modules/typescript/shared/worker-client/module.md +84 -0
  149. package/docs/modules/typescript/shared/worker-client/typings.md +508 -0
  150. package/docs/modules/typescript/shared/worker-daemon/classes.md +239 -0
  151. package/docs/modules/typescript/shared/worker-daemon/constants.md +37 -0
  152. package/docs/modules/typescript/shared/worker-daemon/functions.md +221 -0
  153. package/docs/modules/typescript/shared/worker-daemon/module.md +112 -0
  154. package/docs/modules/typescript/shared/worker-daemon/typings.md +126 -0
  155. package/docs/overview.md +188 -0
  156. package/docs/release-notes/0.1.0.md +189 -0
  157. package/docs/release-notes/0.1.1.md +46 -0
  158. package/docs/release-notes/0.1.2.md +38 -0
  159. package/docs/release-notes/0.1.3.md +42 -0
  160. package/docs/release-notes/0.2.0.md +147 -0
  161. package/docs/release-notes/README.md +9 -0
  162. package/package.json +8 -2
  163. package/src/shared/cli/functions/dispatch-command.function.ts +3 -3
  164. package/src/shared/cli/functions/format-output.function.ts +41 -34
  165. package/src/shared/cli/functions/parse-args.function.ts +7 -0
  166. package/src/shared/cli/functions/run-search.function.ts +53 -27
  167. package/src/shared/cli/index.ts +4 -1
  168. package/src/shared/cli/typings/cli-options.interface.ts +2 -0
  169. package/src/shared/cli/typings/search-display.model.ts +26 -7
  170. package/src/shared/mcp/functions/create-tool-definitions.function.ts +64 -4
  171. package/src/shared/mcp/functions/handle-recall.function.ts +66 -0
  172. package/src/shared/mcp/functions/handle-tool-call.function.ts +11 -1
  173. package/src/shared/mcp/typings/mcp-client.interface.ts +33 -15
  174. package/src/shared/version/constants/version.constant.ts +1 -1
  175. package/src/shared/wasm-codec/functions/decode-recall-result.function.ts +267 -0
  176. package/src/shared/wasm-codec/functions/decode-scored-results.function.ts +81 -27
  177. package/src/shared/wasm-codec/functions/encode-recall-params.function.ts +68 -0
  178. package/src/shared/wasm-codec/functions/encode-search-params.function.ts +4 -1
  179. package/src/shared/wasm-codec/index.ts +11 -3
  180. package/src/shared/worker-client/classes/worker-client.class.ts +24 -4
  181. package/src/shared/worker-client/index.ts +11 -1
  182. package/src/shared/worker-client/typings/recall-result.model.ts +98 -0
  183. package/src/shared/worker-client/typings/search-params.model.ts +2 -0
  184. package/src/shared/worker-client/typings/search-result.model.ts +24 -7
  185. package/src/shared/worker-daemon/classes/worker-daemon.class.ts +25 -4
  186. package/src/wasm/fs-database/kbdb_fs_database.d.ts +18 -0
  187. package/src/wasm/fs-database/kbdb_fs_database.js +30 -0
  188. package/src/wasm/fs-database/kbdb_fs_database_bg.wasm +0 -0
  189. package/src/wasm/fs-database/kbdb_fs_database_bg.wasm.d.ts +2 -0
  190. package/src/wasm/kb-worker/kbdb_worker.d.ts +38 -0
  191. package/src/wasm/kb-worker/kbdb_worker.js +56 -0
  192. package/src/wasm/kb-worker/kbdb_worker_bg.wasm +0 -0
  193. package/src/wasm/kb-worker/kbdb_worker_bg.wasm.d.ts +3 -0
@@ -0,0 +1,90 @@
1
+ # types::cache -- LRU Cache with TTL
2
+
3
+ *In-memory cache between IPC request handling and
4
+ disk I/O. Configuration from `worker.toml`.*
5
+
6
+ ## Source
7
+
8
+ `engine/crates/kb-worker/src/cache/`
9
+
10
+ ## Type Definitions
11
+
12
+ ### `CacheConfig`
13
+
14
+ ```rust
15
+ pub struct CacheConfig {
16
+ pub item_limit: u32,
17
+ pub index_limit: u32,
18
+ pub item_ttl_ms: u64,
19
+ }
20
+ ```
21
+
22
+ Defaults: `item_limit = 1000`,
23
+ `index_limit = 100`, `item_ttl_ms = 60000`.
24
+
25
+ ### `CacheEntry<V>`
26
+
27
+ ```rust
28
+ pub struct CacheEntry<V> {
29
+ pub value: V,
30
+ pub created_at: u64,
31
+ pub last_accessed: u64,
32
+ pub ttl_ms: u64,
33
+ }
34
+
35
+ impl<V> CacheEntry<V> {
36
+ pub fn is_expired(&self, now: u64) -> bool;
37
+ }
38
+ ```
39
+
40
+ ### `LruCache<K, V>`
41
+
42
+ ```rust
43
+ pub struct LruCache<K, V> { /* doubly-linked list + hashmap */ }
44
+
45
+ impl<K: Hash + Eq, V> LruCache<K, V> {
46
+ pub fn new(capacity: u32) -> Self;
47
+ pub fn get(&mut self, key: &K) -> Option<&V>;
48
+ pub fn put(&mut self, key: K, value: V);
49
+ pub fn remove(&mut self, key: &K) -> bool;
50
+ pub fn clear(&mut self);
51
+ pub fn len(&self) -> u32;
52
+ pub fn stats(&self) -> CacheStats;
53
+ }
54
+
55
+ pub struct CacheStats {
56
+ pub items_cached: u32,
57
+ pub hits: u64,
58
+ pub misses: u64,
59
+ pub hit_rate: f32,
60
+ }
61
+ ```
62
+
63
+ O(1) access and O(1) eviction.
64
+
65
+ ### `Cache`
66
+
67
+ ```rust
68
+ pub struct Cache {
69
+ content: LruCache<KbId, SectionRecord>,
70
+ indexes: LruCache<Term, PostingList>,
71
+ manifests: LruCache<String, DocumentManifest>,
72
+ config: CacheConfig,
73
+ }
74
+
75
+ impl Cache {
76
+ pub fn new(config: CacheConfig) -> Self;
77
+ pub fn get_section(&mut self, kbid: &KbId)
78
+ -> Option<&SectionRecord>;
79
+ pub fn put_section(
80
+ &mut self, kbid: KbId, record: SectionRecord,
81
+ );
82
+ pub fn invalidate(&mut self, keys: &[KbId])
83
+ -> u32;
84
+ pub fn invalidate_all(&mut self) -> u32;
85
+ pub fn stats(&self) -> CacheStats;
86
+ }
87
+ ```
88
+
89
+ Separate LRU instances for content objects, index
90
+ segments, and manifests, each with its own capacity.
@@ -0,0 +1,26 @@
1
+ # types::context -- Database Context
2
+
3
+ *The `Context` struct initialized at daemon startup.*
4
+
5
+ ## Source
6
+
7
+ `engine/crates/kb-worker/src/context.rs`
8
+
9
+ ## Type Definition
10
+
11
+ ```rust
12
+ pub struct Context {
13
+ pub ctx_id: String,
14
+ pub target_dir: String,
15
+ pub catalog: Catalog,
16
+ }
17
+
18
+ pub fn init_context(
19
+ target_dir: &str,
20
+ ) -> Result<Context, WorkerError>;
21
+ ```
22
+
23
+ `init_context` calls `db_init` from
24
+ `kbdb-fs-database` to validate the database
25
+ directory and load the catalog. The context is held
26
+ for the daemon's lifetime.
@@ -0,0 +1,16 @@
1
+ # kb-worker/types -- Type Definitions
2
+
3
+ *Structs defined in the kb-worker crate for context,
4
+ caching, result tokens, and status reporting.*
5
+
6
+ ## Modules
7
+
8
+ - **[context](context.md)** -- `Context` struct.
9
+ Database context with catalog data.
10
+ - **[cache](cache.md)** -- `Cache`, `CacheConfig`,
11
+ `CacheEntry`, `CacheStats`, `LruCache`. In-memory
12
+ LRU cache with TTL.
13
+ - **[token](token.md)** -- `TokenStore`, `TokenEntry`,
14
+ `TokenStatus`. Async result token management.
15
+ - **[status](status.md)** -- `StatusResult`. Combined
16
+ database and cache statistics.
@@ -0,0 +1,29 @@
1
+ # types::status -- Status Reporting
2
+
3
+ *Combined database and cache statistics returned by
4
+ `worker_status`.*
5
+
6
+ ## Source
7
+
8
+ `engine/crates/kb-worker/src/status.rs`
9
+
10
+ ## Type Definition
11
+
12
+ ```rust
13
+ pub struct StatusResult {
14
+ pub target_dir: String,
15
+ pub document_count: u32,
16
+ pub section_count: u32,
17
+ pub index_size_bytes: u64,
18
+ pub last_modified: String,
19
+ pub cache_stats: CacheStats,
20
+ }
21
+
22
+ pub fn get_status(
23
+ ctx: &Context,
24
+ cache: &Cache,
25
+ ) -> Result<StatusResult, WorkerError>;
26
+ ```
27
+
28
+ Combines catalog data from `fs-database.wasm` with
29
+ cache statistics from the local cache.
@@ -0,0 +1,50 @@
1
+ # types::token -- Result Token Management
2
+
3
+ *Async result tokens for the submit → poll → fetch
4
+ query flow.*
5
+
6
+ ## Source
7
+
8
+ `engine/crates/kb-worker/src/token.rs`
9
+
10
+ ## Type Definitions
11
+
12
+ ```rust
13
+ pub struct TokenStore {
14
+ tokens: HashMap<String, TokenEntry>,
15
+ }
16
+
17
+ pub struct TokenEntry {
18
+ pub status: TokenStatus,
19
+ pub data: Option<Vec<u8>>,
20
+ pub created_at: u64,
21
+ }
22
+
23
+ pub enum TokenStatus {
24
+ Busy,
25
+ Success,
26
+ Fail(String),
27
+ }
28
+ ```
29
+
30
+ ## Methods
31
+
32
+ ```rust
33
+ impl TokenStore {
34
+ pub fn create(&mut self) -> String;
35
+ pub fn complete(
36
+ &mut self, token: &str, data: Vec<u8>,
37
+ );
38
+ pub fn fail(
39
+ &mut self, token: &str, reason: String,
40
+ );
41
+ pub fn status(&self, token: &str)
42
+ -> Option<&TokenStatus>;
43
+ pub fn take_data(&mut self, token: &str)
44
+ -> Option<Vec<u8>>;
45
+ }
46
+ ```
47
+
48
+ Tokens are random 16-character hex strings. Tied to
49
+ cached data -- when the cached entry is evicted, the
50
+ token becomes invalid and returns `null` on fetch.
@@ -0,0 +1,495 @@
1
+ # Rust Engine -- Overview
2
+
3
+ *Architecture, crate structure, and module layout
4
+ for the Rust/WASM engine.*
5
+
6
+ ## Compilation Model
7
+
8
+ Rust compiles by **crate** -- each crate is an
9
+ independent compilation unit with its own
10
+ `Cargo.toml`. Within a crate, code is organized into
11
+ **modules** (namespaces) that compile together as one
12
+ unit.
13
+
14
+ ```
15
+ crate = compilation unit (has Cargo.toml)
16
+ module = namespace (has mod.rs)
17
+ ```
18
+
19
+ A crate can only use code from:
20
+ 1. Its own modules (`crate::`)
21
+ 2. Dependencies declared in its `Cargo.toml`
22
+
23
+ There are no path-based imports across crates --
24
+ unlike TypeScript/ESM where any file can import any
25
+ other file. Code sharing requires a crate dependency.
26
+
27
+ ## Workspace
28
+
29
+ All Rust code lives in `engine/`. The workspace
30
+ contains four crates under `engine/crates/`:
31
+
32
+ ```
33
+ engine/
34
+ ├── Cargo.toml ← workspace root
35
+ ├── Cargo.lock ← shared lock file
36
+ ├── .cargo/
37
+ │ └── config.toml ← default: wasm32
38
+ └── crates/
39
+ ├── shared/ ← rlib (library)
40
+ ├── fs-database/ ← cdylib (→ .wasm)
41
+ ├── query-parser/ ← cdylib (→ .wasm)
42
+ └── kb-worker/ ← cdylib (→ .wasm)
43
+ ```
44
+
45
+ ## Crate Types
46
+
47
+ ### `rlib` -- Rust Library
48
+
49
+ Produces no output artifact. Compiled **into** each
50
+ crate that depends on it. The compiler includes only
51
+ the code each consumer actually uses (dead code
52
+ elimination).
53
+
54
+ - **`kbdb-shared`** is the only rlib crate.
55
+
56
+ ### `cdylib` -- WebAssembly Module
57
+
58
+ Produces a standalone `.wasm` file. Each cdylib crate
59
+ is an entry point that compiles to a self-contained
60
+ WASM binary.
61
+
62
+ - **`kbdb-fs-database`** → `fs-database.wasm`
63
+ - **`kbdb-query-parser`** → `query-parser.wasm`
64
+ - **`kbdb-worker`** → `kb-worker.wasm`
65
+
66
+ ## Crate Dependency Graph
67
+
68
+ ```
69
+ kbdb-shared (rlib)
70
+
71
+ ├──→ kbdb-fs-database (cdylib)
72
+ │ produces: fs-database.wasm
73
+
74
+ ├──→ kbdb-query-parser (cdylib)
75
+ │ produces: query-parser.wasm
76
+
77
+ └──→ kbdb-worker (cdylib)
78
+ produces: kb-worker.wasm
79
+ also depends on:
80
+ kbdb-fs-database (Cargo)
81
+ kbdb-query-parser (Cargo)
82
+ ```
83
+
84
+ ### Cargo vs WASM Dependencies
85
+
86
+ `kbdb-worker` depends on the other two crates at
87
+ **two levels**:
88
+
89
+ 1. **Cargo dependency** -- `kbdb-worker` lists
90
+ `kbdb-fs-database` and `kbdb-query-parser` in its
91
+ `Cargo.toml`. This gives it compile-time access to
92
+ their type definitions and WASM import signatures.
93
+
94
+ 2. **WASM runtime imports** -- at runtime,
95
+ `kb-worker.wasm` imports functions from
96
+ `fs-database.wasm` and `query-parser.wasm` through
97
+ the shared `WebAssembly.Memory` interface.
98
+
99
+ The standalone modules (`fs-database`, `query-parser`)
100
+ have **no dependency** on each other or on `kb-worker`.
101
+
102
+ ## Build Order
103
+
104
+ ```
105
+ 1. kbdb-shared (no deps, builds first)
106
+ 2. kbdb-fs-database (depends on shared)
107
+ kbdb-query-parser (depends on shared)
108
+ ↑ these two build in parallel
109
+ 3. kbdb-worker (depends on all three)
110
+ ```
111
+
112
+ ## WASM Output
113
+
114
+ ```
115
+ dist/wasm/
116
+ ├── fs-database/
117
+ │ ├── fs-database.wasm
118
+ │ └── fs-database.js ← JS glue
119
+ ├── query-parser/
120
+ │ ├── query-parser.wasm
121
+ │ └── query-parser.js
122
+ └── kb-worker/
123
+ ├── kb-worker.wasm
124
+ └── kb-worker.js
125
+ ```
126
+
127
+ All three modules share one `WebAssembly.Memory`
128
+ instance at runtime. Data crosses module boundaries
129
+ via `(pointer, length)` pairs encoded with the
130
+ `Encode` trait from `kbdb-shared`.
131
+
132
+ ## WASM Size Optimization
133
+
134
+ Each `.wasm` file is optimized for size:
135
+
136
+ ```toml
137
+ # engine/Cargo.toml [profile.release]
138
+ opt-level = "z" # optimize for size
139
+ lto = true # link-time optimization
140
+ codegen-units = 1 # better optimization
141
+ strip = true # remove debug symbols
142
+ panic = "abort" # no unwinding (~10KB saved)
143
+ ```
144
+
145
+ The `shared` rlib is dead-code-eliminated per
146
+ consumer -- each `.wasm` only includes the shared
147
+ code it actually references.
148
+
149
+ ---
150
+
151
+ ## Crate Layouts
152
+
153
+ ### kbdb-shared (rlib)
154
+
155
+ Shared types, traits, memory helpers, and error
156
+ types. Not compiled to WASM on its own -- compiled
157
+ into each consumer.
158
+
159
+ Namespaces are first-level directories inside `src/`.
160
+ Each namespace owns one concern.
161
+
162
+ ```
163
+ engine/crates/shared/
164
+ ├── Cargo.toml
165
+ └── src/
166
+ ├── lib.rs ← pub mod declarations
167
+
168
+ ├── memory/ ← memory management
169
+ │ ├── mod.rs
170
+ │ └── alloc.rs
171
+
172
+ ├── encode/ ← binary serialization
173
+ │ ├── mod.rs
174
+ │ └── encode.rs
175
+
176
+ ├── kbid/ ← content-addressed ID
177
+ │ ├── mod.rs
178
+ │ └── kbid.rs
179
+
180
+ ├── term/ ← search term newtype
181
+ │ ├── mod.rs
182
+ │ └── term.rs
183
+
184
+ ├── section/ ← section types & records
185
+ │ ├── mod.rs
186
+ │ └── section.rs
187
+
188
+ ├── document/ ← document manifests
189
+ │ ├── mod.rs
190
+ │ └── document.rs
191
+
192
+ ├── query/ ← query plan & matches
193
+ │ ├── mod.rs
194
+ │ └── query.rs
195
+
196
+ ├── corpus/ ← corpus statistics
197
+ │ ├── mod.rs
198
+ │ └── corpus.rs
199
+
200
+ ├── content-parser/ ← content marker extraction
201
+ │ ├── mod.rs
202
+ │ ├── types.rs
203
+ │ ├── extract.rs
204
+ │ └── strip.rs
205
+
206
+ ├── content-composer/ ← content composition
207
+ │ ├── mod.rs
208
+ │ ├── types.rs
209
+ │ ├── compose.rs
210
+ │ └── render.rs
211
+
212
+ └── error/ ← error hierarchy
213
+ ├── mod.rs
214
+ └── error.rs
215
+ ```
216
+
217
+ Detailed docs: [shared/module.md](shared/module.md)
218
+
219
+ ### kbdb-fs-database (cdylib → .wasm)
220
+
221
+ Filesystem storage engine. Section CRUD, document
222
+ manifests, B+ tree indexes, content-addressable
223
+ storage.
224
+
225
+ ```
226
+ engine/crates/fs-database/
227
+ ├── Cargo.toml
228
+ └── src/
229
+ ├── lib.rs ← WASM exports
230
+ ├── io.rs ← host import wrappers
231
+ ├── mem.rs ← shared-memory helpers
232
+ ├── catalog.rs ← catalog.toml
233
+
234
+ ├── section/ ← section CRUD
235
+ │ ├── mod.rs
236
+ │ ├── hash.rs
237
+ │ ├── parse.rs
238
+ │ └── shard.rs
239
+
240
+ ├── document.rs ← document manifests
241
+
242
+ ├── index/ ← index subsystem
243
+ │ ├── mod.rs
244
+ │ ├── btree.rs
245
+ │ ├── inverted.rs
246
+ │ ├── positional.rs
247
+ │ └── corpus.rs
248
+
249
+ ├── query_exec.rs ← query plan execution
250
+ ├── integrity.rs ← checksum, GC
251
+ └── atomic.rs ← temp-file + rename
252
+ ```
253
+
254
+ Detailed docs:
255
+ [fs-database/module.md](fs-database/module.md)
256
+
257
+ ### kbdb-query-parser (cdylib → .wasm)
258
+
259
+ Tokenization pipeline, query parsing, keyword
260
+ extraction, and ranking.
261
+
262
+ ```
263
+ engine/crates/query-parser/
264
+ ├── Cargo.toml
265
+ └── src/
266
+ ├── lib.rs ← WASM exports
267
+ ├── mem.rs ← shared-memory helpers
268
+
269
+ ├── pipeline/ ← 8-stage tokenizer
270
+ │ ├── mod.rs
271
+ │ ├── markers.rs
272
+ │ ├── unicode.rs
273
+ │ ├── segment.rs
274
+ │ ├── identifiers.rs
275
+ │ ├── case.rs
276
+ │ ├── stopwords.rs
277
+ │ ├── stem.rs
278
+ │ └── ngram.rs
279
+
280
+ ├── parse.rs ← query → QueryPlan
281
+
282
+ ├── extract/ ← keyword extraction
283
+ │ ├── mod.rs
284
+ │ ├── rake.rs
285
+ │ └── tfidf.rs
286
+
287
+ ├── rank/ ← scoring functions
288
+ │ ├── mod.rs
289
+ │ ├── bm25f.rs
290
+ │ ├── proximity.rs
291
+ │ ├── zone.rs
292
+ │ └── freshness.rs
293
+
294
+ └── cache.rs ← query-scoped cache
295
+ ```
296
+
297
+ Detailed docs:
298
+ [query-parser/module.md](query-parser/module.md)
299
+
300
+ ### kbdb-worker (cdylib → .wasm)
301
+
302
+ Orchestration layer. Imports the other two WASM
303
+ modules, manages LRU cache and async result tokens.
304
+
305
+ ```
306
+ engine/crates/kb-worker/
307
+ ├── Cargo.toml
308
+ └── src/
309
+ ├── lib.rs ← WASM exports
310
+ ├── mem.rs ← shared-memory helpers
311
+ ├── context.rs ← database context
312
+
313
+ ├── orchestrate/ ← operation flows
314
+ │ ├── mod.rs
315
+ │ ├── search.rs
316
+ │ ├── write.rs
317
+ │ └── retrieve.rs
318
+
319
+ ├── cache/ ← LRU cache
320
+ │ ├── mod.rs
321
+ │ ├── lru.rs
322
+ │ ├── entry.rs
323
+ │ └── config.rs
324
+
325
+ ├── token.rs ← async result tokens
326
+ └── status.rs ← status reporting
327
+ ```
328
+
329
+ Detailed docs:
330
+ [kb-worker/module.md](kb-worker/module.md)
331
+
332
+ ---
333
+
334
+ ## Module Conventions
335
+
336
+ ### File Roles
337
+
338
+ | File | Purpose |
339
+ |---------------|----------------------------|
340
+ | `lib.rs` | Crate root. `pub mod` declarations, WASM exports |
341
+ | `mod.rs` | Module entry. Declares sub-modules, re-exports public API |
342
+ | `*.rs` | Implementation. Types, functions, or traits |
343
+
344
+ ### Naming
345
+
346
+ | Rust convention | File name |
347
+ |------------------|----------------------|
348
+ | Module | `snake_case/` |
349
+ | Source file | `snake_case.rs` |
350
+ | Struct / Enum | `PascalCase` |
351
+ | Function | `snake_case` |
352
+ | Trait | `PascalCase` |
353
+ | Constant | `SCREAMING_SNAKE` |
354
+
355
+ ### Visibility
356
+
357
+ | Modifier | Scope |
358
+ |----------------|---------------------------|
359
+ | (none) | Private to current module |
360
+ | `pub(super)` | Parent module only |
361
+ | `pub(crate)` | Anywhere in the crate |
362
+ | `pub` | External consumers |
363
+
364
+ ### `mod.rs` Pattern
365
+
366
+ Every directory module has a `mod.rs` that declares
367
+ its children and re-exports the public API:
368
+
369
+ ```rust
370
+ // corpus/mod.rs
371
+ mod types;
372
+ mod functions;
373
+
374
+ pub use types::CorpusStats;
375
+ pub use functions::get_corpus_stats;
376
+ ```
377
+
378
+ This is analogous to TypeScript's `index.ts` with
379
+ re-exports -- but in Rust, if a file is not declared
380
+ with `mod`, the compiler does not see it even if it
381
+ exists on disk.
382
+
383
+ ### Shared Code Reuse
384
+
385
+ Crates import shared namespaces via Cargo dependency:
386
+
387
+ ```toml
388
+ # fs-database/Cargo.toml
389
+ [dependencies]
390
+ kbdb-shared = { path = "../shared" }
391
+ ```
392
+
393
+ ```rust
394
+ // fs-database/src/lib.rs
395
+ use kbdb_shared::corpus::CorpusStats;
396
+ use kbdb_shared::encode::Encode;
397
+ use kbdb_shared::kbid::KbId;
398
+ ```
399
+
400
+ Only the imported code ends up in the final `.wasm`
401
+ -- unused shared modules are stripped by dead code
402
+ elimination.
403
+
404
+ ---
405
+
406
+ ## Testing Structure
407
+
408
+ ### Unit Tests (TDD)
409
+
410
+ Inline `#[cfg(test)] mod tests` block at the bottom
411
+ of each `.rs` file. Written before the implementation
412
+ (spec-first).
413
+
414
+ ### BDD Module Tests
415
+
416
+ `tests/` subdirectory inside a module for cross-unit
417
+ scenarios:
418
+
419
+ ```
420
+ section/
421
+ ├── mod.rs
422
+ ├── hash.rs
423
+ ├── parse.rs
424
+ └── tests/
425
+ ├── mod.rs
426
+ └── hash_and_shard.rs
427
+ ```
428
+
429
+ ### Crate-Level Integration Tests
430
+
431
+ `engine/tests/` directory for each crate's WASM API
432
+ tests using `#[wasm_bindgen_test]`.
433
+
434
+ ### Running Tests
435
+
436
+ ```sh
437
+ cd engine && cargo test # all
438
+ cd engine && cargo test -p kbdb-shared # one crate
439
+ cd engine && wasm-pack test --node # WASM tests
440
+ ```
441
+
442
+ ---
443
+
444
+ ## Doc Directory Structure
445
+
446
+ Documentation mirrors the source layout:
447
+
448
+ ```
449
+ docs/modules/rust/
450
+ ├── overview.md ← this file
451
+
452
+ ├── shared/ ← kbdb-shared crate
453
+ │ ├── module.md ← crate overview
454
+ │ ├── memory/
455
+ │ │ ├── module.md ← namespace overview
456
+ │ │ └── functions.md ← function docs
457
+ │ ├── encode/
458
+ │ │ ├── module.md
459
+ │ │ └── traits.md
460
+ │ ├── kbid/
461
+ │ │ ├── module.md
462
+ │ │ └── types.md
463
+ │ └── ... ← one dir per namespace
464
+
465
+ ├── fs-database/ ← kbdb-fs-database crate
466
+ │ ├── module.md ← crate overview
467
+ │ ├── types/
468
+ │ │ ├── module.md
469
+ │ │ ├── catalog.md
470
+ │ │ └── section.md
471
+ │ ├── functions/
472
+ │ │ ├── module.md
473
+ │ │ ├── io.md
474
+ │ │ └── ...
475
+ │ └── indexes/
476
+ │ ├── module.md
477
+ │ └── ...
478
+
479
+ ├── query-parser/ ← kbdb-query-parser crate
480
+ │ ├── module.md
481
+ │ ├── types/
482
+ │ ├── functions/
483
+ │ └── ...
484
+
485
+ └── kb-worker/ ← kbdb-worker crate
486
+ ├── module.md
487
+ ├── types/
488
+ ├── functions/
489
+ └── ...
490
+ ```
491
+
492
+ `module.md` = `mod.rs` (namespace entry point).
493
+ Category files (`types.md`, `functions.md`,
494
+ `traits.md`) document the implementations within
495
+ each namespace.