@dikolab/kbdb 0.4.3 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (224) hide show
  1. package/CLA.md +68 -0
  2. package/LICENSE +676 -12
  3. package/LICENSING.md +58 -0
  4. package/README.md +50 -27
  5. package/README.md.bak +320 -0
  6. package/dist/README.md +320 -0
  7. package/dist/{chunk-BJXEVALU.mjs → chunk-QLJ33C74.mjs} +44 -44
  8. package/dist/chunk-QLJ33C74.mjs.map +7 -0
  9. package/dist/cli.cjs +550 -232
  10. package/dist/cli.cjs.map +4 -4
  11. package/dist/cli.d.ts +0 -8
  12. package/dist/cli.mjs +483 -154
  13. package/dist/cli.mjs.map +4 -4
  14. package/dist/kbdb-worker.cjs +36 -5
  15. package/dist/kbdb-worker.cjs.map +3 -3
  16. package/dist/mod.cjs +89 -30
  17. package/dist/mod.cjs.map +4 -4
  18. package/dist/mod.mjs +88 -30
  19. package/dist/mod.mjs.map +4 -4
  20. package/dist/src/cli.d.ts +0 -8
  21. package/dist/src/shared/cli/constants/defaults.constant.d.ts +1 -1
  22. package/dist/src/shared/cli/constants/recfile-command-field-map.constant.d.ts +8 -0
  23. package/dist/src/shared/cli/constants/recfile-field-maps.constant.d.ts +21 -0
  24. package/dist/src/shared/cli/functions/compute-directory-level.function.d.ts +14 -0
  25. package/dist/src/shared/cli/functions/dispatch-command.function.d.ts +2 -28
  26. package/dist/src/shared/cli/functions/format-command-output.function.d.ts +14 -0
  27. package/dist/src/shared/cli/functions/format-db-not-found.function.d.ts +20 -0
  28. package/dist/src/shared/cli/functions/format-output.function.d.ts +1 -0
  29. package/dist/src/shared/cli/functions/format-recfile-output.function.d.ts +11 -0
  30. package/dist/src/shared/cli/functions/render-recfile-value.function.d.ts +11 -0
  31. package/dist/src/shared/cli/functions/resolve-db-dir.function.d.ts +24 -0
  32. package/dist/src/shared/cli/functions/resolve-recfile-key.function.d.ts +9 -0
  33. package/dist/src/shared/cli/functions/run-db-path.function.d.ts +10 -0
  34. package/dist/src/shared/cli/functions/run-export.function.d.ts +8 -4
  35. package/dist/src/shared/cli/functions/run-import.function.d.ts +4 -0
  36. package/dist/src/shared/cli/functions/run-learn.function.d.ts +12 -0
  37. package/dist/src/shared/cli/index.d.ts +4 -1
  38. package/dist/src/shared/cli/typings/cli-options.interface.d.ts +9 -2
  39. package/dist/src/shared/cli/typings/db-resolution.interface.d.ts +9 -0
  40. package/dist/src/shared/cli/typings/recfile-field-map.type.d.ts +10 -0
  41. package/dist/src/shared/version/constants/version.constant.d.ts +1 -1
  42. package/dist/src/shared/wasm-codec/functions/decode-option-u8.function.d.ts +9 -0
  43. package/dist/src/shared/wasm-codec/functions/decode-section-records.function.d.ts +4 -0
  44. package/dist/src/shared/wasm-codec/functions/encode-add-section-params.function.d.ts +2 -45
  45. package/dist/src/shared/wasm-codec/functions/encode-update-section-params.function.d.ts +10 -1
  46. package/dist/src/shared/wasm-codec/index.d.ts +1 -1
  47. package/dist/src/shared/wasm-codec/typings/add-section-input.model.d.ts +6 -0
  48. package/dist/src/shared/wasm-codec/typings/section-record-trailing.model.d.ts +5 -0
  49. package/dist/src/shared/wasm-codec/typings/section-record.model.d.ts +5 -0
  50. package/dist/src/shared/worker-client/functions/build-deno-permissions.function.d.ts +11 -0
  51. package/dist/src/shared/worker-client/functions/read-crash-reason.function.d.ts +9 -0
  52. package/dist/src/shared/worker-client/functions/resolve-spawn-script.function.d.ts +6 -0
  53. package/dist/src/shared/worker-client/functions/spawn-daemon.function.d.ts +1 -1
  54. package/dist/src/shared/worker-client/typings/section.model.d.ts +5 -0
  55. package/dist/src/shared/worker-client/typings/worker-client-options.interface.d.ts +0 -6
  56. package/dist/src/shared/worker-daemon/functions/count-files.function.d.ts +12 -0
  57. package/dist/src/shared/worker-daemon/functions/handle-import.function.d.ts +2 -0
  58. package/dist/src/shared/worker-daemon/functions/handle-read-ops.function.d.ts +8 -0
  59. package/dist/src/shared/worker-daemon/functions/validate-daemon-path.function.d.ts +15 -0
  60. package/dist/src/worker.d.ts +0 -7
  61. package/dist/wasm/default-embedding/kbdb_default_embedding_bg.wasm +0 -0
  62. package/dist/wasm/default-embedding/package.json +1 -1
  63. package/dist/wasm/fs-database/kbdb_fs_database_bg.wasm +0 -0
  64. package/dist/wasm/fs-database/package.json +1 -1
  65. package/dist/wasm/fs-migration/kbdb_fs_migration_bg.wasm +0 -0
  66. package/dist/wasm/fs-migration/package.json +1 -1
  67. package/dist/wasm/kb-worker/kbdb_worker_bg.wasm +0 -0
  68. package/dist/wasm/kb-worker/package.json +1 -1
  69. package/dist/wasm/query-parser/kbdb_query_parser_bg.wasm +0 -0
  70. package/dist/wasm/query-parser/package.json +1 -1
  71. package/dist/worker.d.ts +0 -7
  72. package/dist/worker.mjs +37 -6
  73. package/dist/worker.mjs.map +3 -3
  74. package/package.json +13 -12
  75. package/dist/chunk-BJXEVALU.mjs.map +0 -7
  76. package/dist/src/shared/cli/functions/resolve-raw-db-path.function.d.ts +0 -10
  77. package/docs/details/README.md +0 -30
  78. package/docs/details/agent-tooling.md +0 -132
  79. package/docs/details/cli.md +0 -777
  80. package/docs/details/knowledge-base.md +0 -180
  81. package/docs/details/library-api.md +0 -495
  82. package/docs/details/mcp-server.md +0 -482
  83. package/docs/details/search-and-ranking.md +0 -575
  84. package/docs/details/storage.md +0 -531
  85. package/docs/goals/agents.md +0 -751
  86. package/docs/goals/architecture.svg +0 -198
  87. package/docs/goals/auto-capture.md +0 -378
  88. package/docs/goals/benchmarking.md +0 -296
  89. package/docs/goals/cli.md +0 -2654
  90. package/docs/goals/concurrency.md +0 -259
  91. package/docs/goals/content-composer.md +0 -609
  92. package/docs/goals/content-parser.md +0 -279
  93. package/docs/goals/database.md +0 -1679
  94. package/docs/goals/document.md +0 -368
  95. package/docs/goals/hybrid-search.md +0 -465
  96. package/docs/goals/mcp.md +0 -1541
  97. package/docs/goals/overview.md +0 -124
  98. package/docs/goals/query-parser.md +0 -373
  99. package/docs/goals/query-result.md +0 -860
  100. package/docs/goals/semantic-dedup.md +0 -233
  101. package/docs/goals/skills.md +0 -810
  102. package/docs/goals/sync.md +0 -217
  103. package/docs/goals/worker-client.md +0 -1005
  104. package/docs/goals/worker-daemon.md +0 -1547
  105. package/docs/modules/overview.md +0 -319
  106. package/docs/modules/rust/embedding/module.md +0 -91
  107. package/docs/modules/rust/fs-database/functions/document.md +0 -48
  108. package/docs/modules/rust/fs-database/functions/integrity.md +0 -79
  109. package/docs/modules/rust/fs-database/functions/io.md +0 -87
  110. package/docs/modules/rust/fs-database/functions/module.md +0 -22
  111. package/docs/modules/rust/fs-database/functions/query-exec.md +0 -44
  112. package/docs/modules/rust/fs-database/functions/section.md +0 -76
  113. package/docs/modules/rust/fs-database/indexes/btree.md +0 -35
  114. package/docs/modules/rust/fs-database/indexes/corpus.md +0 -61
  115. package/docs/modules/rust/fs-database/indexes/inverted.md +0 -60
  116. package/docs/modules/rust/fs-database/indexes/module.md +0 -64
  117. package/docs/modules/rust/fs-database/indexes/positional.md +0 -39
  118. package/docs/modules/rust/fs-database/indexes/vectors.md +0 -110
  119. package/docs/modules/rust/fs-database/module.md +0 -150
  120. package/docs/modules/rust/fs-database/types/catalog.md +0 -53
  121. package/docs/modules/rust/fs-database/types/module.md +0 -12
  122. package/docs/modules/rust/fs-database/types/section.md +0 -127
  123. package/docs/modules/rust/kb-worker/functions/compose.md +0 -57
  124. package/docs/modules/rust/kb-worker/functions/module.md +0 -20
  125. package/docs/modules/rust/kb-worker/functions/retrieve.md +0 -194
  126. package/docs/modules/rust/kb-worker/functions/search.md +0 -173
  127. package/docs/modules/rust/kb-worker/functions/write.md +0 -172
  128. package/docs/modules/rust/kb-worker/module.md +0 -171
  129. package/docs/modules/rust/kb-worker/types/cache.md +0 -90
  130. package/docs/modules/rust/kb-worker/types/context.md +0 -26
  131. package/docs/modules/rust/kb-worker/types/module.md +0 -16
  132. package/docs/modules/rust/kb-worker/types/status.md +0 -29
  133. package/docs/modules/rust/kb-worker/types/token.md +0 -50
  134. package/docs/modules/rust/overview.md +0 -531
  135. package/docs/modules/rust/query-parser/functions/extract.md +0 -56
  136. package/docs/modules/rust/query-parser/functions/module.md +0 -18
  137. package/docs/modules/rust/query-parser/functions/parse.md +0 -33
  138. package/docs/modules/rust/query-parser/functions/pipeline.md +0 -9
  139. package/docs/modules/rust/query-parser/functions/rank.md +0 -85
  140. package/docs/modules/rust/query-parser/module.md +0 -131
  141. package/docs/modules/rust/query-parser/types/cache.md +0 -39
  142. package/docs/modules/rust/query-parser/types/keyphrase.md +0 -71
  143. package/docs/modules/rust/query-parser/types/module.md +0 -18
  144. package/docs/modules/rust/query-parser/types/token.md +0 -46
  145. package/docs/modules/rust/shared/content-composer/functions.md +0 -114
  146. package/docs/modules/rust/shared/content-composer/module.md +0 -147
  147. package/docs/modules/rust/shared/content-composer/types.md +0 -93
  148. package/docs/modules/rust/shared/content-parser/functions.md +0 -72
  149. package/docs/modules/rust/shared/content-parser/module.md +0 -99
  150. package/docs/modules/rust/shared/content-parser/types.md +0 -71
  151. package/docs/modules/rust/shared/corpus/module.md +0 -24
  152. package/docs/modules/rust/shared/corpus/types.md +0 -141
  153. package/docs/modules/rust/shared/document/module.md +0 -25
  154. package/docs/modules/rust/shared/document/types.md +0 -154
  155. package/docs/modules/rust/shared/encode/module.md +0 -22
  156. package/docs/modules/rust/shared/encode/traits.md +0 -304
  157. package/docs/modules/rust/shared/error/module.md +0 -28
  158. package/docs/modules/rust/shared/error/types.md +0 -302
  159. package/docs/modules/rust/shared/kbid/module.md +0 -24
  160. package/docs/modules/rust/shared/kbid/types.md +0 -147
  161. package/docs/modules/rust/shared/memory/functions.md +0 -209
  162. package/docs/modules/rust/shared/memory/module.md +0 -24
  163. package/docs/modules/rust/shared/module.md +0 -196
  164. package/docs/modules/rust/shared/pipeline/functions.md +0 -141
  165. package/docs/modules/rust/shared/pipeline/module.md +0 -62
  166. package/docs/modules/rust/shared/pipeline/types.md +0 -43
  167. package/docs/modules/rust/shared/query/module.md +0 -27
  168. package/docs/modules/rust/shared/query/types.md +0 -236
  169. package/docs/modules/rust/shared/section/module.md +0 -25
  170. package/docs/modules/rust/shared/section/types.md +0 -294
  171. package/docs/modules/rust/shared/term/module.md +0 -25
  172. package/docs/modules/rust/shared/term/types.md +0 -139
  173. package/docs/modules/typescript/cli.md +0 -131
  174. package/docs/modules/typescript/kbdb-worker.md +0 -150
  175. package/docs/modules/typescript/overview.md +0 -400
  176. package/docs/modules/typescript/shared/auto-capture/module.md +0 -73
  177. package/docs/modules/typescript/shared/cli/constants.md +0 -23
  178. package/docs/modules/typescript/shared/cli/functions.md +0 -809
  179. package/docs/modules/typescript/shared/cli/module.md +0 -138
  180. package/docs/modules/typescript/shared/cli/typings.md +0 -197
  181. package/docs/modules/typescript/shared/embedding/functions.md +0 -200
  182. package/docs/modules/typescript/shared/embedding/module.md +0 -77
  183. package/docs/modules/typescript/shared/embedding/typings.md +0 -124
  184. package/docs/modules/typescript/shared/hash/functions.md +0 -20
  185. package/docs/modules/typescript/shared/hash/module.md +0 -21
  186. package/docs/modules/typescript/shared/log/constants.md +0 -82
  187. package/docs/modules/typescript/shared/log/functions.md +0 -131
  188. package/docs/modules/typescript/shared/log/module.md +0 -153
  189. package/docs/modules/typescript/shared/log/typings.md +0 -82
  190. package/docs/modules/typescript/shared/mcp/classes.md +0 -179
  191. package/docs/modules/typescript/shared/mcp/functions.md +0 -386
  192. package/docs/modules/typescript/shared/mcp/module.md +0 -160
  193. package/docs/modules/typescript/shared/mcp/typings.md +0 -529
  194. package/docs/modules/typescript/shared/module.md +0 -110
  195. package/docs/modules/typescript/shared/platform/functions.md +0 -42
  196. package/docs/modules/typescript/shared/platform/module.md +0 -25
  197. package/docs/modules/typescript/shared/runtime/functions.md +0 -26
  198. package/docs/modules/typescript/shared/runtime/module.md +0 -27
  199. package/docs/modules/typescript/shared/runtime/typings.md +0 -13
  200. package/docs/modules/typescript/shared/version/module.md +0 -27
  201. package/docs/modules/typescript/shared/wasm-codec/functions.md +0 -391
  202. package/docs/modules/typescript/shared/wasm-codec/module.md +0 -98
  203. package/docs/modules/typescript/shared/worker-client/classes.md +0 -264
  204. package/docs/modules/typescript/shared/worker-client/functions.md +0 -175
  205. package/docs/modules/typescript/shared/worker-client/module.md +0 -92
  206. package/docs/modules/typescript/shared/worker-client/typings.md +0 -511
  207. package/docs/modules/typescript/shared/worker-daemon/classes.md +0 -239
  208. package/docs/modules/typescript/shared/worker-daemon/constants.md +0 -37
  209. package/docs/modules/typescript/shared/worker-daemon/functions.md +0 -234
  210. package/docs/modules/typescript/shared/worker-daemon/module.md +0 -118
  211. package/docs/modules/typescript/shared/worker-daemon/typings.md +0 -132
  212. package/docs/overview.md +0 -191
  213. package/docs/release-notes/0.1.0.md +0 -189
  214. package/docs/release-notes/0.1.1.md +0 -46
  215. package/docs/release-notes/0.1.2.md +0 -38
  216. package/docs/release-notes/0.1.3.md +0 -42
  217. package/docs/release-notes/0.2.0.md +0 -147
  218. package/docs/release-notes/0.3.0.md +0 -89
  219. package/docs/release-notes/0.3.1.md +0 -121
  220. package/docs/release-notes/0.3.2.md +0 -64
  221. package/docs/release-notes/0.4.0.md +0 -80
  222. package/docs/release-notes/0.4.1.md +0 -149
  223. package/docs/release-notes/0.4.3.md +0 -60
  224. package/docs/release-notes/README.md +0 -13
@@ -1,147 +0,0 @@
1
- # types::kbid -- Knowledge Base Section Identifier
2
-
3
- *Newtype wrapper around the 26-character,
4
- content-addressed section identifier used throughout
5
- the system.*
6
-
7
- ## File
8
-
9
- `engine/crates/shared/src/kbid/kbid.rs`
10
-
11
- ## Overview
12
-
13
- Every section in the knowledge base is identified by
14
- a **kb-id** -- a deterministic, collision-resistant,
15
- URL-safe string derived from the SHA-256 hash of the
16
- section's content. The `KbId` type encodes these
17
- invariants at the type level so that raw strings
18
- cannot be passed where a validated identifier is
19
- expected.
20
-
21
- See [Database -- Sections](../../../../../goals/database.md)
22
- for the behavioral specification of content-
23
- addressable storage.
24
-
25
- ## Type Definition
26
-
27
- ```rust
28
- #[derive(Clone, Debug, PartialEq, Eq, Hash)]
29
- pub struct KbId(String);
30
- ```
31
-
32
- The inner `String` is private. Construction goes
33
- through `KbId::new` or `KbId::from_content`, both of
34
- which validate or derive the identifier. This
35
- prevents accidental construction from arbitrary
36
- strings.
37
-
38
- ## Construction
39
-
40
- ### `KbId::from_content`
41
-
42
- ```rust
43
- impl KbId {
44
- pub fn from_content(content: &[u8]) -> Self;
45
- }
46
- ```
47
-
48
- Derives the kb-id from raw content bytes:
49
-
50
- 1. Compute SHA-256 of `content`.
51
- 2. Truncate the digest to 16 bytes (128 bits).
52
- 3. Encode with base32 (Crockford variant, lowercase).
53
- 4. Result: 26 alphanumeric characters.
54
-
55
- For text and code sections, `content` is the raw
56
- UTF-8 bytes. For image sections, `content` is the
57
- base64-encoded string bytes (not raw binary), so
58
- identical base64 input always produces the same
59
- kb-id.
60
-
61
- This function is infallible -- any byte slice
62
- produces a valid kb-id.
63
-
64
- ### `KbId::new`
65
-
66
- ```rust
67
- impl KbId {
68
- pub fn new(s: &str) -> Result<Self, KbIdError>;
69
- }
70
- ```
71
-
72
- Validates an existing identifier string:
73
-
74
- - Must be exactly 26 characters.
75
- - Must contain only lowercase alphanumeric characters
76
- (`[a-z0-9]`).
77
-
78
- Returns `KbIdError::InvalidLength` or
79
- `KbIdError::InvalidCharacter` on failure.
80
-
81
- ## Methods
82
-
83
- ```rust
84
- impl KbId {
85
- pub fn as_str(&self) -> &str;
86
-
87
- pub fn shard_prefix(&self) -> &str;
88
- }
89
- ```
90
-
91
- ### `as_str`
92
-
93
- Returns the inner string slice. Used when passing
94
- the kb-id to I/O functions, serializers, or display.
95
-
96
- ### `shard_prefix`
97
-
98
- Returns the first two characters of the kb-id. Used
99
- by `fs-database` to compute the hash-sharded
100
- directory path: `sections/{prefix}/{kbid}.sec`.
101
-
102
- ```rust
103
- let id = KbId::from_content(b"hello");
104
- assert_eq!(id.shard_prefix().len(), 2);
105
- ```
106
-
107
- ## Trait Implementations
108
-
109
- | Trait | Purpose |
110
- |--------------|----------------------------------|
111
- | `Clone` | Cache entries need owned copies |
112
- | `Debug` | Diagnostic output |
113
- | `PartialEq` / `Eq` | Identity comparison |
114
- | `Hash` | HashMap/HashSet keys (cache, index lookups) |
115
- | `Display` | Formats as the raw 26-char string |
116
- | `Encode` | Binary serialization for shared memory (see [encode](../encode/traits.md)) |
117
-
118
- `Ord` / `PartialOrd` are **not** derived. Kb-ids
119
- have no meaningful ordering -- they are content
120
- hashes. If sorting is needed (e.g. deterministic
121
- output), use `as_str()` for lexicographic comparison
122
- explicitly.
123
-
124
- ## Error Type
125
-
126
- ```rust
127
- #[derive(Debug)]
128
- pub enum KbIdError {
129
- InvalidLength(usize),
130
- InvalidCharacter(char, usize),
131
- }
132
- ```
133
-
134
- ## Testing
135
-
136
- Unit tests:
137
-
138
- - `from_content` produces 26-char lowercase
139
- alphanumeric output for various inputs.
140
- - Identical content always produces the same kb-id.
141
- - Different content produces different kb-ids.
142
- - `shard_prefix` returns the first two characters.
143
- - `new` accepts valid 26-char strings.
144
- - `new` rejects strings that are too short, too long,
145
- or contain uppercase/special characters.
146
- - `Encode` round-trip: encode then decode produces
147
- an equal `KbId`.
@@ -1,209 +0,0 @@
1
- # functions::alloc -- Allocator and Read/Write Helpers
2
-
3
- *Allocator exports and shared-memory read/write
4
- helpers for passing data between the TypeScript host
5
- and WASM modules.*
6
-
7
- ## File
8
-
9
- `engine/crates/shared/src/memory/alloc.rs`
10
-
11
- ## Overview
12
-
13
- WASM modules and the TypeScript host share a single
14
- `WebAssembly.Memory` buffer. All data exchange --
15
- strings, byte slices, structured records -- passes
16
- through this buffer using `(pointer, length)` pairs.
17
- This module provides:
18
-
19
- 1. **Allocator exports** (`kbdb_alloc`, `kbdb_free`)
20
- that the TypeScript host calls to manage buffers.
21
- 2. **Read helpers** that safely reconstruct Rust types
22
- from raw memory pointers.
23
- 3. **Write helpers** that serialize Rust types into
24
- the shared buffer and return pointer/length pairs.
25
-
26
- Every WASM module re-exports `kbdb_alloc` and
27
- `kbdb_free` from this module so the host can allocate
28
- memory in any module's address space.
29
-
30
- ## Allocator Exports
31
-
32
- ```rust
33
- use std::alloc::{alloc, dealloc, Layout};
34
-
35
- #[no_mangle]
36
- pub extern "C" fn kbdb_alloc(size: u32) -> *mut u8 {
37
- let layout = Layout::from_size_align(
38
- size as usize, 1,
39
- ).unwrap();
40
- unsafe { alloc(layout) }
41
- }
42
-
43
- #[no_mangle]
44
- pub extern "C" fn kbdb_free(
45
- ptr: *mut u8, size: u32,
46
- ) {
47
- let layout = Layout::from_size_align(
48
- size as usize, 1,
49
- ).unwrap();
50
- unsafe { dealloc(ptr, layout) }
51
- }
52
- ```
53
-
54
- ### Usage from TypeScript
55
-
56
- The TypeScript daemon uses this protocol to pass
57
- data into WASM functions:
58
-
59
- ```
60
- 1. host calls kbdb_alloc(size) → ptr
61
- 2. host writes bytes into memory at ptr
62
- 3. host calls wasm_function(ptr, size)
63
- 4. wasm reads from ptr
64
- 5. host calls kbdb_free(ptr, size)
65
- ```
66
-
67
- For return values:
68
-
69
- ```
70
- 1. wasm calls kbdb_alloc(size) internally → ptr
71
- 2. wasm writes result into ptr
72
- 3. wasm returns (ptr, size) to host
73
- 4. host reads bytes from memory at ptr
74
- 5. host calls kbdb_free(ptr, size)
75
- ```
76
-
77
- ### Alignment
78
-
79
- Alignment is `1` (byte-aligned). The data exchanged
80
- is always byte streams (UTF-8 strings, binary
81
- encoded structs). No multi-byte alignment is needed
82
- at the allocation level -- the `Encode` trait handles
83
- field alignment internally.
84
-
85
- ### Null and Zero
86
-
87
- `kbdb_alloc(0)` returns a non-null, dangling pointer
88
- (per Rust's allocator contract). Callers must not
89
- dereference zero-length allocations. `kbdb_free` with
90
- size `0` is a no-op.
91
-
92
- ## Read Helpers
93
-
94
- Safe functions that reconstruct Rust types from raw
95
- shared-memory pointers.
96
-
97
- ```rust
98
- pub unsafe fn read_str<'a>(
99
- ptr: *const u8, len: u32,
100
- ) -> &'a str;
101
-
102
- pub unsafe fn read_bytes<'a>(
103
- ptr: *const u8, len: u32,
104
- ) -> &'a [u8];
105
- ```
106
-
107
- ### `read_str`
108
-
109
- Interprets the byte range `[ptr, ptr + len)` as
110
- UTF-8. Panics if the bytes are not valid UTF-8.
111
-
112
- This is used for string arguments from the host
113
- (file paths, query strings, MIME types). The host
114
- always writes valid UTF-8 -- a panic here indicates
115
- a host bug.
116
-
117
- ### `read_bytes`
118
-
119
- Returns the byte range `[ptr, ptr + len)` as a
120
- slice. No UTF-8 validation. Used for binary data
121
- (section content, encoded structs).
122
-
123
- ## Write Helpers
124
-
125
- Functions that serialize Rust types into the shared
126
- buffer and return `(pointer, length)` pairs for the
127
- caller to pass back to the host or another module.
128
-
129
- ```rust
130
- pub fn write_str(s: &str) -> (*const u8, u32);
131
-
132
- pub fn write_bytes(data: &[u8]) -> (*const u8, u32);
133
-
134
- pub fn write_encoded<T: Encode>(
135
- value: &T,
136
- ) -> (*const u8, u32);
137
- ```
138
-
139
- ### `write_str`
140
-
141
- Allocates `s.len()` bytes via `kbdb_alloc`, copies
142
- the UTF-8 bytes, and returns the pointer and length.
143
- The caller (or host) is responsible for freeing the
144
- allocation.
145
-
146
- ### `write_bytes`
147
-
148
- Same as `write_str` but for arbitrary byte slices.
149
-
150
- ### `write_encoded`
151
-
152
- Encodes `value` using the `Encode` trait (see
153
- [encode](../encode/traits.md)), allocates a
154
- buffer for the result, copies the encoded bytes, and
155
- returns the pointer and length. Convenience wrapper
156
- for returning structured data from WASM exports.
157
-
158
- ## Return Value Convention
159
-
160
- WASM functions can only return scalar values. For
161
- functions that return variable-length data, the
162
- convention is a **two-slot return buffer**:
163
-
164
- ```rust
165
- pub fn set_return(ptr: *const u8, len: u32);
166
- pub fn get_return_ptr() -> *const u8;
167
- pub fn get_return_len() -> u32;
168
- ```
169
-
170
- A pair of module-global slots holds the most recent
171
- return value. The WASM export writes `(ptr, len)`
172
- into the slots via `set_return`, returns a status
173
- code (`0` for success, negative for error), and the
174
- host reads the actual data via `get_return_ptr` and
175
- `get_return_len`.
176
-
177
- This avoids multi-value returns, which have uneven
178
- support across WASM runtimes.
179
-
180
- ## Safety
181
-
182
- All read functions are `unsafe` because they
183
- dereference raw pointers. The safety contract:
184
-
185
- - `ptr` must point into the shared
186
- `WebAssembly.Memory` buffer.
187
- - `ptr + len` must not exceed the buffer's current
188
- size.
189
- - The memory region must not be freed or
190
- reallocated while the returned reference is alive.
191
-
192
- The write functions are safe -- they allocate fresh
193
- buffers and copy data in.
194
-
195
- ## Testing
196
-
197
- Unit tests (native Rust, not WASM):
198
-
199
- - `kbdb_alloc` + `kbdb_free` round-trip without
200
- panic for various sizes (1, 256, 65536).
201
- - `kbdb_alloc(0)` returns non-null.
202
- - `write_str` + `read_str` round-trip preserves
203
- content for ASCII, Unicode, and empty strings.
204
- - `write_bytes` + `read_bytes` round-trip preserves
205
- arbitrary byte sequences.
206
- - `write_encoded` + `Encode::decode` round-trip for
207
- sample types.
208
- - Return value slots: `set_return` → `get_return_ptr`
209
- / `get_return_len` retrieves the same values.
@@ -1,24 +0,0 @@
1
- # memory -- Memory Management
2
-
3
- *Allocator exports and shared-memory read/write
4
- helpers for passing data between the TypeScript host
5
- and WASM modules.*
6
-
7
- ## Source
8
-
9
- `engine/crates/shared/src/memory/`
10
-
11
- ## Responsibility
12
-
13
- Provides the shared-memory allocation protocol used
14
- by all four WASM modules. Every WASM export that
15
- exchanges variable-length data (strings, byte
16
- slices, encoded structs) goes through this module's
17
- allocator and read/write helpers.
18
-
19
- ## Exports
20
-
21
- - **[functions.md](functions.md)** -- `kbdb_alloc`,
22
- `kbdb_free`, `read_str`, `read_bytes`, `write_str`,
23
- `write_bytes`, `write_encoded`, `set_return`,
24
- `get_return_ptr`, `get_return_len`.
@@ -1,196 +0,0 @@
1
- # shared -- Shared Types and Utilities
2
-
3
- *Rust crate that provides types, memory helpers, error
4
- types, the tokenization pipeline, and serialization
5
- utilities shared across all four WASM modules.*
6
-
7
- ## Crate
8
-
9
- | Field | Value |
10
- |-------------|------------------------------------|
11
- | Name | `kbdb-shared` |
12
- | Path | `engine/crates/shared/` |
13
- | Crate type | `rlib` (library only, not WASM) |
14
- | Depends on | nothing (leaf crate) |
15
-
16
- `kbdb-shared` is a pure Rust library. It is **not**
17
- compiled to WASM on its own -- it is compiled into
18
- each of the four WASM modules that depend on it
19
- (`kbdb-fs-database`, `kbdb-query-parser`,
20
- `kbdb-default-embedding`, `kbdb-worker`).
21
-
22
- ## Workspace Position
23
-
24
- ```
25
- engine/
26
- ├── Cargo.toml # workspace root
27
- └── crates/
28
- ├── shared/ # this crate (leaf)
29
- ├── fs-database/ # depends on shared
30
- ├── query-parser/ # depends on shared
31
- ├── default-embedding/ # depends on shared
32
- └── kb-worker/ # depends on shared
33
- ```
34
-
35
- ## Directory Layout
36
-
37
- ```
38
- engine/crates/shared/
39
- ├── Cargo.toml
40
- └── src/
41
- ├── lib.rs # crate root, re-exports
42
- ├── memory/
43
- │ ├── mod.rs # memory module facade
44
- │ └── alloc.rs # kbdb_alloc, kbdb_free
45
- ├── encode/
46
- │ ├── mod.rs # encode module facade
47
- │ └── encode.rs # Encode trait + impls
48
- ├── kbid/
49
- │ ├── mod.rs # kbid module facade
50
- │ └── kbid.rs # KbId newtype
51
- ├── term/
52
- │ ├── mod.rs # term module facade
53
- │ └── term.rs # Term newtype
54
- ├── section/
55
- │ ├── mod.rs # section module facade
56
- │ └── section.rs # SectionType, records
57
- ├── document/
58
- │ ├── mod.rs # document module facade
59
- │ └── document.rs # DocumentManifest, records
60
- ├── query/
61
- │ ├── mod.rs # query module facade
62
- │ └── query.rs # QueryPlan, RawMatch
63
- ├── corpus/
64
- │ ├── mod.rs # corpus module facade
65
- │ └── corpus.rs # CorpusStats
66
- ├── content-parser/
67
- │ ├── mod.rs # content-parser facade
68
- │ ├── types.rs # ContentMarkerType,
69
- │ │ # ExtractedContentMarker
70
- │ ├── extract.rs # extract_content_markers()
71
- │ └── strip.rs # strip_content_markers()
72
- ├── content-composer/
73
- │ ├── mod.rs # content-composer facade
74
- │ ├── types.rs # ComposeRequest, etc.
75
- │ ├── compose.rs # compose_content()
76
- │ └── render.rs # marker rendering
77
- ├── pipeline/
78
- │ ├── mod.rs # pipeline orchestrator
79
- │ ├── markers.rs # stage 1: strip markers
80
- │ ├── unicode.rs # stage 2: NFC normalize
81
- │ ├── segment.rs # stage 3: word boundaries
82
- │ ├── identifiers.rs # stage 4: camelCase split
83
- │ ├── case.rs # stage 5: case folding
84
- │ ├── stopwords.rs # stage 6: stop words
85
- │ ├── synonyms.rs # stage 7: synonym expand
86
- │ ├── stem.rs # stage 8: Snowball stemmer
87
- │ ├── ngram.rs # stage 9: n-gram extract
88
- │ └── token_result.rs # TokenResult struct
89
- └── error/
90
- ├── mod.rs # error module facade
91
- └── error.rs # FsError, IndexError, etc.
92
- ```
93
-
94
- ## Modules
95
-
96
- Each sub-module has a single responsibility. Files
97
- within each module mirror their Rust source
98
- counterparts: `module.md` = `mod.rs`,
99
- `types.md` = type definitions,
100
- `functions.md` = function definitions,
101
- `traits.md` = trait definitions.
102
-
103
- ### [memory/](memory/module.md) -- Memory Management
104
-
105
- - [functions.md](memory/functions.md) --
106
- `kbdb_alloc`, `kbdb_free`, read/write helpers,
107
- return value convention.
108
-
109
- ### [encode/](encode/module.md) -- Binary Encoding
110
-
111
- - [traits.md](encode/traits.md) -- `Encode` trait,
112
- wire format, per-type implementations.
113
-
114
- ### [kbid/](kbid/module.md) -- Content-Addressed ID
115
-
116
- - [types.md](kbid/types.md) -- `KbId` newtype,
117
- `from_content`, `shard_prefix`, `KbIdError`.
118
-
119
- ### [term/](term/module.md) -- Search Term
120
-
121
- - [types.md](term/types.md) -- `Term` newtype,
122
- `Ord` for B+ tree keys, `TermError`.
123
-
124
- ### [section/](section/module.md) -- Section Types
125
-
126
- - [types.md](section/types.md) -- `SectionType`,
127
- `MimeType`, `SectionRecord`, CRUD params/results.
128
-
129
- ### [document/](document/module.md) -- Document Records
130
-
131
- - [types.md](document/types.md) --
132
- `DocumentManifest`, `DocumentSection`,
133
- `DocumentRecord`.
134
-
135
- ### [query/](query/module.md) -- Query Plan
136
-
137
- - [types.md](query/types.md) -- `QueryPlan`,
138
- `RawMatch`, `MatchMode`.
139
-
140
- ### [corpus/](corpus/module.md) -- Corpus Statistics
141
-
142
- - [types.md](corpus/types.md) -- `CorpusStats`.
143
-
144
- ### [content-parser/](content-parser/module.md) -- Content Parser
145
-
146
- - [types.md](content-parser/types.md) --
147
- `ContentMarkerType` enum,
148
- `ExtractedContentMarker` struct.
149
- - [functions.md](content-parser/functions.md) --
150
- `extract_content_markers()`,
151
- `strip_content_markers()`.
152
-
153
- ### [content-composer/](content-composer/module.md) -- Content Composer
154
-
155
- - [types.md](content-composer/types.md) --
156
- `ComposeRequest`, `ComposeResult`,
157
- `RenderedSection`, `ResolvedId`, `SectionData`,
158
- `DocumentData`.
159
- - [functions.md](content-composer/functions.md) --
160
- `compose_content()`.
161
-
162
- ### [pipeline/](pipeline/module.md) -- Tokenization Pipeline
163
-
164
- - [functions.md](pipeline/functions.md) --
165
- `tokenize()`, `bigrams()`, `trigrams()`. 9-stage
166
- pipeline: marker stripping, NFC, segmentation,
167
- identifier splitting, case folding, stop words,
168
- synonym expansion, stemming, n-grams.
169
- - [types.md](pipeline/types.md) -- `TokenResult`,
170
- `TokenStream`, `Ngram`, `NgramType`, `StemResult`.
171
-
172
- Consumers: `kbdb-query-parser` (re-exports via WASM
173
- FFI), `kbdb-default-embedding` (TF-IDF vectors).
174
-
175
- ### [error/](error/module.md) -- Error Types
176
-
177
- - [types.md](error/types.md) -- `FsError`,
178
- `IndexError`, `ParseError`, `WorkerError`.
179
-
180
- ## Dependencies
181
-
182
- ```toml
183
- [dependencies]
184
- wasm-bindgen = "0.2"
185
- unicode-segmentation = "1"
186
- unicode-normalization = "0.1"
187
- rust-stemmers = "1"
188
- ```
189
-
190
- ## Testing
191
-
192
- ```sh
193
- cd engine && cargo test -p kbdb-shared
194
- ```
195
-
196
- See each module doc for detailed test descriptions.
@@ -1,141 +0,0 @@
1
- # pipeline -- Tokenization Pipeline Functions
2
-
3
- *9-stage pipeline that converts raw text into
4
- normalized, stemmed tokens with synonym expansion.
5
- Used for both indexing and query processing.*
6
-
7
- ## Source
8
-
9
- `engine/crates/shared/src/pipeline/`
10
-
11
- ## Entry Point
12
-
13
- ```rust
14
- pub fn tokenize(input: &str) -> TokenStream;
15
- ```
16
-
17
- Runs all nine stages in sequence. Each stage is a
18
- pure function.
19
-
20
- ## Stages
21
-
22
- ### 1. Content Marker Stripping
23
-
24
- ```rust
25
- use kbdb_shared::content_parser::strip_content_markers;
26
- ```
27
-
28
- Removes all six content marker types from the input
29
- text before tokenization:
30
- `$(image:)`, `$(code:)`, `$(text:)`, `$(document:)`,
31
- `$(link:)`, `$(doc-link:)`.
32
-
33
- Delegates to `strip_content_markers()` from the
34
- sibling `content_parser` module (see
35
- [Content Parser](../content-parser/module.md)).
36
- These are structural references, not searchable
37
- content.
38
-
39
- ### 2. Unicode Normalization (`unicode.rs`)
40
-
41
- ```rust
42
- pub fn normalize_nfc(input: &str) -> String;
43
- ```
44
-
45
- NFC normalization for consistent codepoints.
46
-
47
- ### 3. Word Boundary Detection (`segment.rs`)
48
-
49
- ```rust
50
- pub fn segment_words(input: &str) -> Vec<&str>;
51
- ```
52
-
53
- Unicode Segmentation (UAX #29).
54
-
55
- ### 4. Identifier Splitting (`identifiers.rs`)
56
-
57
- ```rust
58
- pub fn split_identifiers(word: &str) -> Vec<String>;
59
- ```
60
-
61
- - `camelCase` → `["camel", "case"]`
62
- - `snake_case` → `["snake", "case"]`
63
- - `PascalCase` → `["pascal", "case"]`
64
- - `HTMLParser` → `["html", "parser"]`
65
-
66
- ### 5. Case Folding (`case.rs`)
67
-
68
- ```rust
69
- pub fn fold_case(word: &str) -> String;
70
- ```
71
-
72
- Unicode case folding (lowercase).
73
-
74
- ### 6. Stop Word Removal (`stopwords.rs`)
75
-
76
- ```rust
77
- pub fn is_stop_word(word: &str) -> bool;
78
- pub fn remove_stop_words(
79
- words: &[String],
80
- ) -> Vec<String>;
81
- ```
82
-
83
- Filters common function words. Single-character
84
- tokens also removed.
85
-
86
- ### 7. Synonym Expansion (`synonyms.rs`)
87
-
88
- ```rust
89
- pub fn expand_synonyms(
90
- word: &str,
91
- map: &SynonymMap,
92
- ) -> Vec<String>;
93
- ```
94
-
95
- Looks up the lowercased word in the built-in synonym
96
- map. Returns a vector containing the original word
97
- plus any synonyms. When no synonyms exist, returns a
98
- single-element vector containing only the original.
99
-
100
- The synonym map is a bidirectional
101
- `HashMap<String, Vec<String>>` built at compile time.
102
- Expansion is applied after stop-word removal and
103
- before stemming, so both original and synonym tokens
104
- are stemmed consistently.
105
-
106
- The map is small (~30-50 entries) and focused on
107
- common IT/development vocabulary mismatches:
108
- login/sign-in, auth/authentication, remove/delete,
109
- config/configuration, credential/password, etc.
110
-
111
- ### 8. Stemming (`stem.rs`)
112
-
113
- ```rust
114
- pub fn stem_word(word: &str) -> StemResult;
115
- ```
116
-
117
- Snowball / Porter2. Preserves both original and
118
- stemmed forms.
119
-
120
- ### 9. N-gram Extraction (`ngram.rs`)
121
-
122
- ```rust
123
- pub fn extract_ngrams(
124
- tokens: &[Token],
125
- ) -> Vec<Ngram>;
126
- ```
127
-
128
- Bigrams and trigrams for phrase indexing.
129
-
130
- ## Relaxed Query Mode
131
-
132
- The tokenization pipeline is used for both the
133
- primary AND-mode query plan and the relaxed OR-mode
134
- fallback plan. `qp_parse_query` produces a
135
- `QueryPlan` with a `match_mode` field (`All` | `Any`).
136
- When the AND plan returns zero matches,
137
- `kb-worker.wasm` retries with the OR plan using the
138
- same tokenized terms. No additional pipeline stage or
139
- WASM export is needed. See
140
- [Query Parser -- Relaxed Query
141
- Mode](../../../../goals/query-parser.md).