@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,61 @@
1
+ # indexes::corpus -- Corpus Statistics
2
+
3
+ *Corpus-wide term statistics for IDF computation.
4
+ Stored in `indexes/corpus.dat`.*
5
+
6
+ ## Source
7
+
8
+ `engine/crates/fs-database/src/index/corpus.rs`
9
+
10
+ ## Type Definition
11
+
12
+ ```rust
13
+ pub struct CorpusStats {
14
+ pub total_sections: u32,
15
+ pub total_terms: u32,
16
+ pub term_doc_freq: HashMap<Term, u32>,
17
+ pub avg_section_length: f32,
18
+ pub avg_heading_length: f32,
19
+ pub avg_body_length: f32,
20
+ pub avg_code_length: f32,
21
+ }
22
+ ```
23
+
24
+ This is the **full** corpus stats struct with the
25
+ per-term document frequency map. The lightweight
26
+ `CorpusStats` in `kbdb-shared` (see
27
+ [shared/corpus](../../shared/corpus/types.md))
28
+ contains only the scalar fields that cross module
29
+ boundaries.
30
+
31
+ Per-field average lengths enable accurate BM25F
32
+ per-field length normalization. Each field's
33
+ average is computed from the corresponding
34
+ per-field token counts stored in section headers.
35
+
36
+ ## Methods
37
+
38
+ ```rust
39
+ impl CorpusStats {
40
+ pub fn idf(&self, term: &Term) -> f32;
41
+ pub fn update_add(
42
+ &mut self,
43
+ terms: &[Term],
44
+ field_token_counts: &FieldTokenCounts,
45
+ );
46
+ pub fn update_remove(
47
+ &mut self,
48
+ terms: &[Term],
49
+ field_token_counts: &FieldTokenCounts,
50
+ );
51
+ }
52
+ ```
53
+
54
+ `idf` computes inverse document frequency:
55
+ `ln((N - df(t) + 0.5) / (df(t) + 0.5) + 1)`.
56
+
57
+ `update_add` increments document frequency counters
58
+ for each term and updates per-field average length
59
+ accumulators using the section's field token counts.
60
+ `update_remove` decrements them and adjusts the
61
+ per-field averages accordingly.
@@ -0,0 +1,60 @@
1
+ # indexes::inverted -- Inverted Index
2
+
3
+ *Maps normalized terms to lists of
4
+ (kb-id, heading_tf, body_tf, code_tf) posting
5
+ entries. Stored in `indexes/terms.idx`.*
6
+
7
+ ## Source
8
+
9
+ `engine/crates/fs-database/src/index/inverted.rs`
10
+
11
+ ## Type Definitions
12
+
13
+ ```rust
14
+ pub struct InvertedIndex {
15
+ tree: BPlusTree<Term, PostingList>,
16
+ }
17
+
18
+ pub struct PostingList {
19
+ pub entries: Vec<Posting>,
20
+ }
21
+
22
+ pub struct Posting {
23
+ pub kbid: KbId,
24
+ pub heading_tf: f32,
25
+ pub body_tf: f32,
26
+ pub code_tf: f32,
27
+ }
28
+ ```
29
+
30
+ Each `Posting` stores per-field term frequencies
31
+ instead of a single aggregate weight. Storing
32
+ per-field TFs separately enables query-time field
33
+ weight tuning (heading 2.0x, body 1.0x, code 1.5x)
34
+ without rebuilding indexes. See
35
+ [FieldTf](../../shared/query/types.md) for the
36
+ shared struct used across module boundaries.
37
+
38
+ ## Methods
39
+
40
+ ```rust
41
+ impl InvertedIndex {
42
+ pub fn lookup_term(&self, term: &Term)
43
+ -> Option<&PostingList>;
44
+
45
+ pub fn add_terms(
46
+ &mut self,
47
+ kbid: &KbId,
48
+ terms: &[(Term, FieldTf)],
49
+ );
50
+
51
+ pub fn remove_section(&mut self, kbid: &KbId);
52
+ }
53
+ ```
54
+
55
+ `add_terms` inserts or updates per-field TF
56
+ entries for a section. Each `FieldTf` value
57
+ contains the heading, body, and code term
58
+ frequencies computed during per-field
59
+ tokenization. `remove_section` removes all
60
+ postings for a given kb-id across all terms.
@@ -0,0 +1,64 @@
1
+ # fs-database/indexes -- B+ Tree Index Subsystem
2
+
3
+ *Binary index files in `indexes/`. Optimized for read
4
+ speed -- O(log n) term lookups.*
5
+
6
+ ## Source
7
+
8
+ `engine/crates/fs-database/src/index/`
9
+
10
+ ## Modules
11
+
12
+ - **[btree](btree.md)** -- Generic `BPlusTree<K, V>`
13
+ implementation. On-disk ordered key-value storage
14
+ with configurable page size.
15
+ - **[inverted](inverted.md)** -- `InvertedIndex`.
16
+ Maps normalized terms to posting lists (kb-id +
17
+ weight). Stored in `indexes/terms.idx`.
18
+ - **[positional](positional.md)** -- `PositionalIndex`.
19
+ Maps (term, kb-id) pairs to position lists for
20
+ phrase query support. Stored in
21
+ `indexes/positions.idx`.
22
+ - **[corpus](corpus.md)** -- `CorpusStats`. Corpus-wide
23
+ term statistics for IDF computation. Stored in
24
+ `indexes/corpus.dat`.
25
+ - **[references](references.md)** -- `ReferenceIndex`.
26
+ Bidirectional graph of content marker references
27
+ between sections. Stored in `indexes/references.idx`.
28
+
29
+ ---
30
+
31
+ ### Reference Graph Index (`references.idx`)
32
+
33
+ Bidirectional index mapping content marker
34
+ references between sections. Built at section
35
+ insertion time by parsing content markers via
36
+ `extract_content_markers()` from `kbdb-shared`.
37
+
38
+ **Index entry format:**
39
+
40
+ | Field | Type | Description |
41
+ |-------------------|--------|-----------------------|
42
+ | `source_kbid` | KbId | Section with marker |
43
+ | `target_id` | String | Referenced kbid/docid |
44
+ | `marker_type` | u8 | ContentMarkerType enum value |
45
+ | `context_snippet` | String | ~150 chars around marker |
46
+ | `direction` | u8 | 0 = forward, 1 = back |
47
+
48
+ **Operations:**
49
+
50
+ - **Build**: On section add/update, extract markers,
51
+ generate context snippets (~150 chars of the
52
+ surrounding sentence/paragraph), write one forward
53
+ entry per marker plus one back-reference entry at
54
+ each target.
55
+ - **Remove**: On section delete, remove all forward
56
+ entries where `source_kbid` matches and all back
57
+ entries where `target_id` matches the deleted
58
+ section's kbid.
59
+ - **Lookup**: Given a kbid, return all forward
60
+ references (markers in this section's content) and
61
+ all back-references (markers in other sections
62
+ pointing to this section). O(1) via B+ tree index.
63
+ - **Rebuild**: Regenerated from section content
64
+ alongside other index files during `rebuild`.
@@ -0,0 +1,39 @@
1
+ # indexes::positional -- Positional Index
2
+
3
+ *Maps (term, kb-id) pairs to position lists for
4
+ phrase query support. Stored in
5
+ `indexes/positions.idx`.*
6
+
7
+ ## Source
8
+
9
+ `engine/crates/fs-database/src/index/positional.rs`
10
+
11
+ ## Type Definition
12
+
13
+ ```rust
14
+ pub struct PositionalIndex {
15
+ tree: BPlusTree<(Term, KbId), Vec<u32>>,
16
+ }
17
+ ```
18
+
19
+ ## Methods
20
+
21
+ ```rust
22
+ impl PositionalIndex {
23
+ pub fn positions(
24
+ &self, term: &Term, kbid: &KbId,
25
+ ) -> Option<&[u32]>;
26
+
27
+ pub fn add_positions(
28
+ &mut self,
29
+ kbid: &KbId,
30
+ term_positions: &[(Term, Vec<u32>)],
31
+ );
32
+
33
+ pub fn remove_section(&mut self, kbid: &KbId);
34
+ }
35
+ ```
36
+
37
+ Position values are zero-based token offsets. Used
38
+ for phrase matching (verifying adjacent term
39
+ positions) and proximity scoring.
@@ -0,0 +1,149 @@
1
+ # fs-database -- Filesystem Storage Engine
2
+
3
+ *Rust crate that manages the `.kbdb` directory:
4
+ section CRUD, document manifests, B+ tree indexes,
5
+ and content-addressable storage. Compiles to
6
+ `fs-database.wasm`.*
7
+
8
+ ## Crate
9
+
10
+ | Field | Value |
11
+ |-------------|------------------------------------|
12
+ | Name | `kbdb-fs-database` |
13
+ | Path | `engine/crates/fs-database/` |
14
+ | Crate type | `cdylib` (WASM) + `rlib` (tests) |
15
+ | WASM output | `dist/wasm/fs-database.wasm` |
16
+ | Depends on | `kbdb-shared` |
17
+
18
+ `kbdb-fs-database` is a **standalone** WASM module. It
19
+ does not import `kbdb-query-parser` or `kbdb-worker` at
20
+ the WASM level. `kb-worker.wasm` imports this module's
21
+ exports and orchestrates it alongside
22
+ `query-parser.wasm`.
23
+
24
+ See [Database](../../../goals/database.md) for the
25
+ full behavioral specification.
26
+
27
+ ## Workspace Position
28
+
29
+ ```
30
+ engine/
31
+ ├── Cargo.toml # workspace root
32
+ └── crates/
33
+ ├── shared/ # kbdb-shared
34
+ ├── fs-database/ # this crate
35
+ ├── query-parser/ # kbdb-query-parser
36
+ └── kb-worker/ # kbdb-worker
37
+ ```
38
+
39
+ ## Directory Layout
40
+
41
+ ```
42
+ engine/crates/fs-database/
43
+ ├── Cargo.toml
44
+ └── src/
45
+ ├── lib.rs # crate root, WASM exports
46
+ ├── io.rs # host import wrappers
47
+ ├── mem.rs # shared-memory helpers
48
+ ├── catalog.rs # catalog.toml read/write
49
+ ├── section/
50
+ │ ├── mod.rs # section CRUD facade
51
+ │ ├── hash.rs # SHA-256 hashing, kb-id
52
+ │ ├── parse.rs # TOML frontmatter parser
53
+ │ └── shard.rs # hash-sharded path logic
54
+ ├── document.rs # document manifest CRUD
55
+ ├── index/
56
+ │ ├── mod.rs # index facade
57
+ │ ├── btree.rs # B+ tree implementation
58
+ │ ├── inverted.rs # term → kb-id mappings
59
+ │ ├── positional.rs # term position data
60
+ │ └── corpus.rs # corpus-wide statistics
61
+ ├── query_exec.rs # execute query plans
62
+ ├── integrity.rs # checksum verify, GC
63
+ └── atomic.rs # temp-file + rename
64
+ ```
65
+
66
+ ## Modules
67
+
68
+ ### [types/](types/module.md) -- Structs
69
+
70
+ - **[catalog](types/catalog.md)** -- `Catalog` struct.
71
+ Database table of contents.
72
+ - **[section](types/section.md)** -- `SectionHeader`,
73
+ frontmatter parse/serialize, shard path resolution.
74
+
75
+ ### [functions/](functions/module.md) -- Operations
76
+
77
+ - **[io](functions/io.md)** -- Host import wrappers,
78
+ return conventions, shared memory protocol.
79
+ - **[section](functions/section.md)** -- Section CRUD
80
+ (`read`, `add`, `update`, `remove`), content
81
+ hashing, atomic writes.
82
+ - **[document](functions/document.md)** -- Document
83
+ manifest CRUD, `update_section_ref`.
84
+ - **[query-exec](functions/query-exec.md)** -- Execute
85
+ query plans against indexes, return raw matches.
86
+ - **[integrity](functions/integrity.md)** -- Integrity
87
+ checks, garbage collection, index rebuild.
88
+
89
+ ### [indexes/](indexes/module.md) -- Index Subsystem
90
+
91
+ - **[btree](indexes/btree.md)** -- Generic B+ tree
92
+ for binary index files.
93
+ - **[inverted](indexes/inverted.md)** -- Term →
94
+ posting list mappings (`terms.idx`).
95
+ - **[positional](indexes/positional.md)** -- Term
96
+ position data for phrase queries
97
+ (`positions.idx`).
98
+ - **[corpus](indexes/corpus.md)** -- Corpus-wide
99
+ statistics for IDF (`corpus.dat`).
100
+
101
+ ## WASM Exports
102
+
103
+ | Export | Description |
104
+ |------------------------|----------------------------|
105
+ | `db_init` | Open database, read catalog |
106
+ | `db_read_section` | Read section by kb-id |
107
+ | `db_read_sections` | Read multiple sections |
108
+ | `db_add_section` | Create section file |
109
+ | `db_update_section` | Replace section content |
110
+ | `db_remove_section` | Delete section file |
111
+ | `db_read_document` | Read document manifest |
112
+ | `db_write_document` | Create/update document |
113
+ | `db_remove_document` | Delete document manifest |
114
+ | `db_update_section_ref`| Replace kb-id in manifests |
115
+ | `db_index_add` | Add terms to inverted index |
116
+ | `db_index_remove` | Remove section from indexes |
117
+ | `db_index_rebuild` | Full index rebuild |
118
+ | `db_execute_plan` | Run query plan against indexes |
119
+ | `db_corpus_stats` | Return corpus statistics |
120
+ | `db_status` | Return database metadata |
121
+ | `db_check_integrity` | Run integrity checks |
122
+ | `db_garbage_collect` | Remove orphaned sections |
123
+ | `alloc` / `free` | Memory management (from `kbdb-shared`) |
124
+
125
+ ## Dependencies
126
+
127
+ ```toml
128
+ [dependencies]
129
+ kbdb-shared = { path = "../shared" }
130
+ sha2 = "0.10"
131
+ data-encoding = "2"
132
+ toml = { version = "0.8", default-features = false }
133
+ ```
134
+
135
+ ## Build
136
+
137
+ ```sh
138
+ cd engine
139
+ wasm-pack build crates/fs-database \
140
+ --target no-modules \
141
+ --out-dir ../../../dist/wasm/fs-database
142
+ ```
143
+
144
+ ## Testing
145
+
146
+ ```sh
147
+ cd engine && cargo test -p kbdb-fs-database
148
+ cd engine && wasm-pack test --node crates/fs-database
149
+ ```
@@ -0,0 +1,53 @@
1
+ # types::catalog -- Database Catalog
2
+
3
+ *The `Catalog` struct representing `catalog.toml` --
4
+ the database's table of contents.*
5
+
6
+ ## Source
7
+
8
+ `engine/crates/fs-database/src/catalog.rs`
9
+
10
+ ## Type Definition
11
+
12
+ ```rust
13
+ pub struct Catalog {
14
+ pub version: u32,
15
+ pub created_at: String,
16
+ pub last_modified: String,
17
+ pub section_count: u32,
18
+ pub document_count: u32,
19
+ pub checksum: String,
20
+ }
21
+ ```
22
+
23
+ | Field | Description |
24
+ |------------------|---------------------------------|
25
+ | `version` | Database format version |
26
+ | `created_at` | ISO 8601 creation timestamp |
27
+ | `last_modified` | ISO 8601 last write timestamp |
28
+ | `section_count` | Total sections in the database |
29
+ | `document_count` | Total documents in the database |
30
+ | `checksum` | SHA-256 integrity checksum |
31
+
32
+ The catalog is the first file `fs-database.wasm`
33
+ reads when opening a database. It is updated after
34
+ every write operation using atomic temp-file + rename.
35
+
36
+ ## Functions
37
+
38
+ ```rust
39
+ pub fn read_catalog() -> Result<Catalog, FsError>;
40
+
41
+ pub fn write_catalog(cat: &Catalog)
42
+ -> Result<(), FsError>;
43
+
44
+ pub fn increment_sections(delta: i32)
45
+ -> Result<Catalog, FsError>;
46
+
47
+ pub fn increment_documents(delta: i32)
48
+ -> Result<Catalog, FsError>;
49
+ ```
50
+
51
+ `increment_sections` and `increment_documents` read
52
+ the catalog, adjust the count, update
53
+ `last_modified`, and write back atomically.
@@ -0,0 +1,12 @@
1
+ # fs-database/types -- Type Definitions
2
+
3
+ *Structs defined in the fs-database crate for catalog
4
+ metadata and section file parsing.*
5
+
6
+ ## Modules
7
+
8
+ - **[catalog](catalog.md)** -- `Catalog` struct.
9
+ Database table of contents from `catalog.toml`.
10
+ - **[section](section.md)** -- `SectionHeader` struct
11
+ and `IntegrityReport`. TOML frontmatter
12
+ parse/serialize, hash-sharded path resolution.
@@ -0,0 +1,101 @@
1
+ # types::section -- Section Header and Parsing
2
+
3
+ *The `SectionHeader` struct and `IntegrityReport`
4
+ struct, plus TOML frontmatter parse/serialize and
5
+ hash-sharded path resolution.*
6
+
7
+ ## Source
8
+
9
+ `engine/crates/fs-database/src/section/`
10
+
11
+ ## Type Definitions
12
+
13
+ ### `SectionHeader`
14
+
15
+ ```rust
16
+ pub struct SectionHeader {
17
+ pub kbid: KbId,
18
+ pub docids: Vec<String>,
19
+ pub section_type: SectionType,
20
+ pub title: Option<String>,
21
+ pub description: Option<String>,
22
+ pub size: u32,
23
+ pub created_at: String,
24
+ pub checksum: String,
25
+ pub token_count: u32,
26
+ pub heading_token_count: u32,
27
+ pub body_token_count: u32,
28
+ pub code_token_count: u32,
29
+ }
30
+ ```
31
+
32
+ TOML frontmatter fields from a section file. Parsed
33
+ by `parse_section_file`, serialized by
34
+ `serialize_section_file`.
35
+
36
+ `docids` lists the document IDs this section belongs
37
+ to (may be empty, one, or many -- many-to-many
38
+ relationship). `title` is required for image sections
39
+ and optional for text/code sections. `description` is
40
+ always optional. Token counts are computed by
41
+ `query-parser.wasm` during per-field tokenization
42
+ and stored in the header for query-time retrieval.
43
+ See [FieldTokenCounts](../../shared/query/types.md)
44
+ for the shared struct.
45
+
46
+ ### `IntegrityReport`
47
+
48
+ ```rust
49
+ pub struct IntegrityReport {
50
+ pub checksum_failures: Vec<KbId>,
51
+ pub orphaned_sections: Vec<KbId>,
52
+ pub orphaned_documents: Vec<String>,
53
+ pub broken_refs: Vec<(KbId, String, KbId)>,
54
+ pub type_mismatches: Vec<(KbId, String, KbId)>,
55
+ pub circular_refs: Vec<(KbId, Vec<KbId>)>,
56
+ }
57
+ ```
58
+
59
+ Returned by `check_integrity`. `broken_refs`
60
+ entries are `(source_kbid, marker_type, target_kbid)`
61
+ covering all six marker types (`text`, `code`,
62
+ `image`, `document`, `link`, `doc-link`).
63
+ `type_mismatches` are `(source_kbid, marker_type,
64
+ target_kbid)` where the target section type does
65
+ not match the marker type. `circular_refs` are
66
+ `(start_kbid, cycle_path)` for reference chains
67
+ that form loops.
68
+
69
+ ## Functions
70
+
71
+ ### Frontmatter Parsing (`section/parse.rs`)
72
+
73
+ ```rust
74
+ pub fn parse_section_file(data: &[u8])
75
+ -> Result<(SectionHeader, &[u8]), ParseError>;
76
+
77
+ pub fn serialize_section_file(
78
+ header: &SectionHeader,
79
+ body: &[u8],
80
+ ) -> Vec<u8>;
81
+ ```
82
+
83
+ Splits on `---\n` separator. Header is TOML; body
84
+ is raw bytes stored verbatim.
85
+
86
+ ### Content Hashing (`section/hash.rs`)
87
+
88
+ ```rust
89
+ pub fn compute_kbid(content: &[u8]) -> KbId;
90
+ ```
91
+
92
+ SHA-256 → truncate to 16 bytes → base32 (Crockford)
93
+ → 26-character kb-id.
94
+
95
+ ### Path Resolution (`section/shard.rs`)
96
+
97
+ ```rust
98
+ pub fn section_path(kbid: &KbId) -> String;
99
+ ```
100
+
101
+ Returns `sections/{first_two_chars}/{kbid}.sec`.
@@ -0,0 +1,57 @@
1
+ # functions::compose -- Content Composition Flow
2
+
3
+ *Orchestrates content composition: resolve identifiers,
4
+ fetch content, compose rendered Markdown.*
5
+
6
+ ## Source
7
+
8
+ `engine/crates/kb-worker/src/orchestrate/compose.rs`
9
+
10
+ ## Exported WASM Function
11
+
12
+ ### `worker_compose`
13
+
14
+ ```rust
15
+ pub fn worker_compose(
16
+ ids_ptr: *const u8,
17
+ ids_len: u32,
18
+ ) -> u64;
19
+ ```
20
+
21
+ Composes a rendered Markdown document from one or
22
+ more kbid or docid identifiers. Returns a result
23
+ token (same async pattern as `worker_query`).
24
+
25
+ ## Compose Flow
26
+
27
+ ```
28
+ 1. Receive identifiers from IPC `compose` method
29
+
30
+ 2. Resolve identifiers via fs-database
31
+ (try kbid first, then docid)
32
+
33
+ 3. Expand documents to section kbids, deduplicate
34
+
35
+ 4. Fetch section/document content from fs-database
36
+
37
+ 5. Extract content markers recursively using
38
+ extract_content_markers() from content-parser
39
+
40
+ 6. Build ComposeRequest with all resolved data
41
+
42
+ 7. Call compose_content() from content-composer
43
+
44
+ 8. Return ComposeResult to daemon
45
+ ```
46
+
47
+ The caller retrieves the composed document via
48
+ `worker_get_result` with type `"content"`.
49
+
50
+ Individual sections fetched during composition are
51
+ cached. The composed result itself is NOT cached --
52
+ it is computed fresh on each request because
53
+ composition depends on the full set of input
54
+ identifiers.
55
+
56
+ See [Content Composer](../../shared/content-composer/module.md)
57
+ for the full composition specification.
@@ -0,0 +1,20 @@
1
+ # kb-worker/functions -- Orchestration Functions
2
+
3
+ *Functions that coordinate calls between
4
+ query-parser.wasm and fs-database.wasm to implement
5
+ full database operations.*
6
+
7
+ ## Modules
8
+
9
+ - **[search](search.md)** -- Search orchestration.
10
+ Parse query → build plan → execute → rank →
11
+ return results.
12
+ - **[write](write.md)** -- Write orchestration.
13
+ Add/update/remove text, code, and image sections.
14
+ - **[retrieve](retrieve.md)** -- Read orchestration.
15
+ Retrieve sections and documents, with cache.
16
+ Progressive context expansion (`worker_recall`).
17
+ Status reporting.
18
+ - **[compose](compose.md)** -- Content composition.
19
+ Resolve identifiers, fetch content, compose
20
+ rendered Markdown via content-composer.