@dikolab/kbdb 0.5.0 → 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (224) hide show
  1. package/CLA.md +68 -0
  2. package/LICENSE +676 -12
  3. package/LICENSING.md +58 -0
  4. package/README.md +50 -25
  5. package/README.md.bak +322 -0
  6. package/dist/README.md +322 -0
  7. package/dist/{chunk-BJXEVALU.mjs → chunk-QLJ33C74.mjs} +44 -44
  8. package/dist/chunk-QLJ33C74.mjs.map +7 -0
  9. package/dist/cli.cjs +550 -232
  10. package/dist/cli.cjs.map +4 -4
  11. package/dist/cli.d.ts +0 -8
  12. package/dist/cli.mjs +483 -154
  13. package/dist/cli.mjs.map +4 -4
  14. package/dist/kbdb-worker.cjs +36 -5
  15. package/dist/kbdb-worker.cjs.map +3 -3
  16. package/dist/mod.cjs +89 -30
  17. package/dist/mod.cjs.map +4 -4
  18. package/dist/mod.mjs +88 -30
  19. package/dist/mod.mjs.map +4 -4
  20. package/dist/src/cli.d.ts +0 -8
  21. package/dist/src/shared/cli/constants/defaults.constant.d.ts +1 -1
  22. package/dist/src/shared/cli/constants/recfile-command-field-map.constant.d.ts +8 -0
  23. package/dist/src/shared/cli/constants/recfile-field-maps.constant.d.ts +21 -0
  24. package/dist/src/shared/cli/functions/compute-directory-level.function.d.ts +14 -0
  25. package/dist/src/shared/cli/functions/dispatch-command.function.d.ts +2 -28
  26. package/dist/src/shared/cli/functions/format-command-output.function.d.ts +14 -0
  27. package/dist/src/shared/cli/functions/format-db-not-found.function.d.ts +20 -0
  28. package/dist/src/shared/cli/functions/format-output.function.d.ts +1 -0
  29. package/dist/src/shared/cli/functions/format-recfile-output.function.d.ts +11 -0
  30. package/dist/src/shared/cli/functions/render-recfile-value.function.d.ts +11 -0
  31. package/dist/src/shared/cli/functions/resolve-db-dir.function.d.ts +24 -0
  32. package/dist/src/shared/cli/functions/resolve-recfile-key.function.d.ts +9 -0
  33. package/dist/src/shared/cli/functions/run-db-path.function.d.ts +10 -0
  34. package/dist/src/shared/cli/functions/run-export.function.d.ts +8 -4
  35. package/dist/src/shared/cli/functions/run-import.function.d.ts +4 -0
  36. package/dist/src/shared/cli/functions/run-learn.function.d.ts +12 -0
  37. package/dist/src/shared/cli/index.d.ts +4 -1
  38. package/dist/src/shared/cli/typings/cli-options.interface.d.ts +9 -2
  39. package/dist/src/shared/cli/typings/db-resolution.interface.d.ts +9 -0
  40. package/dist/src/shared/cli/typings/recfile-field-map.type.d.ts +10 -0
  41. package/dist/src/shared/version/constants/version.constant.d.ts +1 -1
  42. package/dist/src/shared/wasm-codec/functions/decode-option-u8.function.d.ts +9 -0
  43. package/dist/src/shared/wasm-codec/functions/decode-section-records.function.d.ts +4 -0
  44. package/dist/src/shared/wasm-codec/functions/encode-add-section-params.function.d.ts +2 -45
  45. package/dist/src/shared/wasm-codec/functions/encode-update-section-params.function.d.ts +10 -1
  46. package/dist/src/shared/wasm-codec/index.d.ts +1 -1
  47. package/dist/src/shared/wasm-codec/typings/add-section-input.model.d.ts +6 -0
  48. package/dist/src/shared/wasm-codec/typings/section-record-trailing.model.d.ts +5 -0
  49. package/dist/src/shared/wasm-codec/typings/section-record.model.d.ts +5 -0
  50. package/dist/src/shared/worker-client/functions/build-deno-permissions.function.d.ts +11 -0
  51. package/dist/src/shared/worker-client/functions/read-crash-reason.function.d.ts +9 -0
  52. package/dist/src/shared/worker-client/functions/resolve-spawn-script.function.d.ts +6 -0
  53. package/dist/src/shared/worker-client/functions/spawn-daemon.function.d.ts +1 -1
  54. package/dist/src/shared/worker-client/typings/section.model.d.ts +5 -0
  55. package/dist/src/shared/worker-client/typings/worker-client-options.interface.d.ts +0 -6
  56. package/dist/src/shared/worker-daemon/functions/count-files.function.d.ts +12 -0
  57. package/dist/src/shared/worker-daemon/functions/handle-import.function.d.ts +2 -0
  58. package/dist/src/shared/worker-daemon/functions/handle-read-ops.function.d.ts +8 -0
  59. package/dist/src/shared/worker-daemon/functions/validate-daemon-path.function.d.ts +15 -0
  60. package/dist/src/worker.d.ts +0 -7
  61. package/dist/wasm/default-embedding/kbdb_default_embedding_bg.wasm +0 -0
  62. package/dist/wasm/default-embedding/package.json +1 -1
  63. package/dist/wasm/fs-database/kbdb_fs_database_bg.wasm +0 -0
  64. package/dist/wasm/fs-database/package.json +1 -1
  65. package/dist/wasm/fs-migration/package.json +1 -1
  66. package/dist/wasm/kb-worker/kbdb_worker_bg.wasm +0 -0
  67. package/dist/wasm/kb-worker/package.json +1 -1
  68. package/dist/wasm/query-parser/kbdb_query_parser_bg.wasm +0 -0
  69. package/dist/wasm/query-parser/package.json +1 -1
  70. package/dist/worker.d.ts +0 -7
  71. package/dist/worker.mjs +37 -6
  72. package/dist/worker.mjs.map +3 -3
  73. package/package.json +11 -10
  74. package/dist/chunk-BJXEVALU.mjs.map +0 -7
  75. package/dist/src/shared/cli/functions/resolve-raw-db-path.function.d.ts +0 -10
  76. package/docs/details/README.md +0 -30
  77. package/docs/details/agent-tooling.md +0 -132
  78. package/docs/details/cli.md +0 -777
  79. package/docs/details/knowledge-base.md +0 -180
  80. package/docs/details/library-api.md +0 -495
  81. package/docs/details/mcp-server.md +0 -482
  82. package/docs/details/search-and-ranking.md +0 -575
  83. package/docs/details/storage.md +0 -531
  84. package/docs/goals/agents.md +0 -751
  85. package/docs/goals/architecture.svg +0 -198
  86. package/docs/goals/auto-capture.md +0 -378
  87. package/docs/goals/benchmarking.md +0 -296
  88. package/docs/goals/cli.md +0 -2654
  89. package/docs/goals/concurrency.md +0 -259
  90. package/docs/goals/content-composer.md +0 -609
  91. package/docs/goals/content-parser.md +0 -279
  92. package/docs/goals/database.md +0 -1679
  93. package/docs/goals/document.md +0 -368
  94. package/docs/goals/hybrid-search.md +0 -465
  95. package/docs/goals/mcp.md +0 -1541
  96. package/docs/goals/overview.md +0 -124
  97. package/docs/goals/query-parser.md +0 -373
  98. package/docs/goals/query-result.md +0 -860
  99. package/docs/goals/semantic-dedup.md +0 -233
  100. package/docs/goals/skills.md +0 -810
  101. package/docs/goals/sync.md +0 -217
  102. package/docs/goals/worker-client.md +0 -1005
  103. package/docs/goals/worker-daemon.md +0 -1547
  104. package/docs/modules/overview.md +0 -319
  105. package/docs/modules/rust/embedding/module.md +0 -91
  106. package/docs/modules/rust/fs-database/functions/document.md +0 -48
  107. package/docs/modules/rust/fs-database/functions/integrity.md +0 -79
  108. package/docs/modules/rust/fs-database/functions/io.md +0 -87
  109. package/docs/modules/rust/fs-database/functions/module.md +0 -22
  110. package/docs/modules/rust/fs-database/functions/query-exec.md +0 -44
  111. package/docs/modules/rust/fs-database/functions/section.md +0 -76
  112. package/docs/modules/rust/fs-database/indexes/btree.md +0 -35
  113. package/docs/modules/rust/fs-database/indexes/corpus.md +0 -61
  114. package/docs/modules/rust/fs-database/indexes/inverted.md +0 -60
  115. package/docs/modules/rust/fs-database/indexes/module.md +0 -64
  116. package/docs/modules/rust/fs-database/indexes/positional.md +0 -39
  117. package/docs/modules/rust/fs-database/indexes/vectors.md +0 -110
  118. package/docs/modules/rust/fs-database/module.md +0 -150
  119. package/docs/modules/rust/fs-database/types/catalog.md +0 -53
  120. package/docs/modules/rust/fs-database/types/module.md +0 -12
  121. package/docs/modules/rust/fs-database/types/section.md +0 -127
  122. package/docs/modules/rust/kb-worker/functions/compose.md +0 -57
  123. package/docs/modules/rust/kb-worker/functions/module.md +0 -20
  124. package/docs/modules/rust/kb-worker/functions/retrieve.md +0 -194
  125. package/docs/modules/rust/kb-worker/functions/search.md +0 -173
  126. package/docs/modules/rust/kb-worker/functions/write.md +0 -172
  127. package/docs/modules/rust/kb-worker/module.md +0 -171
  128. package/docs/modules/rust/kb-worker/types/cache.md +0 -90
  129. package/docs/modules/rust/kb-worker/types/context.md +0 -26
  130. package/docs/modules/rust/kb-worker/types/module.md +0 -16
  131. package/docs/modules/rust/kb-worker/types/status.md +0 -29
  132. package/docs/modules/rust/kb-worker/types/token.md +0 -50
  133. package/docs/modules/rust/overview.md +0 -531
  134. package/docs/modules/rust/query-parser/functions/extract.md +0 -56
  135. package/docs/modules/rust/query-parser/functions/module.md +0 -18
  136. package/docs/modules/rust/query-parser/functions/parse.md +0 -33
  137. package/docs/modules/rust/query-parser/functions/pipeline.md +0 -9
  138. package/docs/modules/rust/query-parser/functions/rank.md +0 -85
  139. package/docs/modules/rust/query-parser/module.md +0 -131
  140. package/docs/modules/rust/query-parser/types/cache.md +0 -39
  141. package/docs/modules/rust/query-parser/types/keyphrase.md +0 -71
  142. package/docs/modules/rust/query-parser/types/module.md +0 -18
  143. package/docs/modules/rust/query-parser/types/token.md +0 -46
  144. package/docs/modules/rust/shared/content-composer/functions.md +0 -114
  145. package/docs/modules/rust/shared/content-composer/module.md +0 -147
  146. package/docs/modules/rust/shared/content-composer/types.md +0 -93
  147. package/docs/modules/rust/shared/content-parser/functions.md +0 -72
  148. package/docs/modules/rust/shared/content-parser/module.md +0 -99
  149. package/docs/modules/rust/shared/content-parser/types.md +0 -71
  150. package/docs/modules/rust/shared/corpus/module.md +0 -24
  151. package/docs/modules/rust/shared/corpus/types.md +0 -141
  152. package/docs/modules/rust/shared/document/module.md +0 -25
  153. package/docs/modules/rust/shared/document/types.md +0 -154
  154. package/docs/modules/rust/shared/encode/module.md +0 -22
  155. package/docs/modules/rust/shared/encode/traits.md +0 -304
  156. package/docs/modules/rust/shared/error/module.md +0 -28
  157. package/docs/modules/rust/shared/error/types.md +0 -302
  158. package/docs/modules/rust/shared/kbid/module.md +0 -24
  159. package/docs/modules/rust/shared/kbid/types.md +0 -147
  160. package/docs/modules/rust/shared/memory/functions.md +0 -209
  161. package/docs/modules/rust/shared/memory/module.md +0 -24
  162. package/docs/modules/rust/shared/module.md +0 -196
  163. package/docs/modules/rust/shared/pipeline/functions.md +0 -141
  164. package/docs/modules/rust/shared/pipeline/module.md +0 -62
  165. package/docs/modules/rust/shared/pipeline/types.md +0 -43
  166. package/docs/modules/rust/shared/query/module.md +0 -27
  167. package/docs/modules/rust/shared/query/types.md +0 -236
  168. package/docs/modules/rust/shared/section/module.md +0 -25
  169. package/docs/modules/rust/shared/section/types.md +0 -294
  170. package/docs/modules/rust/shared/term/module.md +0 -25
  171. package/docs/modules/rust/shared/term/types.md +0 -139
  172. package/docs/modules/typescript/cli.md +0 -131
  173. package/docs/modules/typescript/kbdb-worker.md +0 -150
  174. package/docs/modules/typescript/overview.md +0 -400
  175. package/docs/modules/typescript/shared/auto-capture/module.md +0 -73
  176. package/docs/modules/typescript/shared/cli/constants.md +0 -23
  177. package/docs/modules/typescript/shared/cli/functions.md +0 -809
  178. package/docs/modules/typescript/shared/cli/module.md +0 -138
  179. package/docs/modules/typescript/shared/cli/typings.md +0 -197
  180. package/docs/modules/typescript/shared/embedding/functions.md +0 -200
  181. package/docs/modules/typescript/shared/embedding/module.md +0 -77
  182. package/docs/modules/typescript/shared/embedding/typings.md +0 -124
  183. package/docs/modules/typescript/shared/hash/functions.md +0 -20
  184. package/docs/modules/typescript/shared/hash/module.md +0 -21
  185. package/docs/modules/typescript/shared/log/constants.md +0 -82
  186. package/docs/modules/typescript/shared/log/functions.md +0 -131
  187. package/docs/modules/typescript/shared/log/module.md +0 -153
  188. package/docs/modules/typescript/shared/log/typings.md +0 -82
  189. package/docs/modules/typescript/shared/mcp/classes.md +0 -179
  190. package/docs/modules/typescript/shared/mcp/functions.md +0 -386
  191. package/docs/modules/typescript/shared/mcp/module.md +0 -160
  192. package/docs/modules/typescript/shared/mcp/typings.md +0 -529
  193. package/docs/modules/typescript/shared/module.md +0 -110
  194. package/docs/modules/typescript/shared/platform/functions.md +0 -42
  195. package/docs/modules/typescript/shared/platform/module.md +0 -25
  196. package/docs/modules/typescript/shared/runtime/functions.md +0 -26
  197. package/docs/modules/typescript/shared/runtime/module.md +0 -27
  198. package/docs/modules/typescript/shared/runtime/typings.md +0 -13
  199. package/docs/modules/typescript/shared/version/module.md +0 -27
  200. package/docs/modules/typescript/shared/wasm-codec/functions.md +0 -391
  201. package/docs/modules/typescript/shared/wasm-codec/module.md +0 -98
  202. package/docs/modules/typescript/shared/worker-client/classes.md +0 -264
  203. package/docs/modules/typescript/shared/worker-client/functions.md +0 -175
  204. package/docs/modules/typescript/shared/worker-client/module.md +0 -92
  205. package/docs/modules/typescript/shared/worker-client/typings.md +0 -511
  206. package/docs/modules/typescript/shared/worker-daemon/classes.md +0 -239
  207. package/docs/modules/typescript/shared/worker-daemon/constants.md +0 -37
  208. package/docs/modules/typescript/shared/worker-daemon/functions.md +0 -234
  209. package/docs/modules/typescript/shared/worker-daemon/module.md +0 -118
  210. package/docs/modules/typescript/shared/worker-daemon/typings.md +0 -132
  211. package/docs/overview.md +0 -191
  212. package/docs/release-notes/0.1.0.md +0 -189
  213. package/docs/release-notes/0.1.1.md +0 -46
  214. package/docs/release-notes/0.1.2.md +0 -38
  215. package/docs/release-notes/0.1.3.md +0 -42
  216. package/docs/release-notes/0.2.0.md +0 -147
  217. package/docs/release-notes/0.3.0.md +0 -89
  218. package/docs/release-notes/0.3.1.md +0 -121
  219. package/docs/release-notes/0.3.2.md +0 -64
  220. package/docs/release-notes/0.4.0.md +0 -80
  221. package/docs/release-notes/0.4.1.md +0 -149
  222. package/docs/release-notes/0.4.3.md +0 -60
  223. package/docs/release-notes/0.5.0.md +0 -41
  224. package/docs/release-notes/README.md +0 -16
@@ -1,127 +0,0 @@
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
- pub source_key: Option<String>,
30
- pub source_path: Option<String>,
31
- pub source_mtime: Option<String>,
32
- pub updated_at: Option<String>,
33
- pub tags: Vec<String>,
34
- pub supersedes: Option<KbId>,
35
- }
36
- ```
37
-
38
- TOML frontmatter fields from a section file. Parsed
39
- by `parse_section_file`, serialized by
40
- `serialize_section_file`.
41
-
42
- `docids` lists the document IDs this section belongs
43
- to (may be empty, one, or many -- many-to-many
44
- relationship). `title` is required for image sections
45
- and optional for text/code sections. `description` is
46
- always optional. Token counts are computed by
47
- `query-parser.wasm` during per-field tokenization
48
- and stored in the header for query-time retrieval.
49
- See [FieldTokenCounts](../../shared/query/types.md)
50
- for the shared struct.
51
-
52
- `source_key` is a stable logical identity derived
53
- from `sha256("${source_path}\0${heading}")`,
54
- truncated to 16 hex chars. Separate from `kbid`
55
- (content hash) -- identifies *what the section is
56
- about* rather than *what it currently says*. `null`
57
- for stdin input. `source_path` is the original file
58
- path that was learned. `source_mtime` is the ISO 8601
59
- modified time of the source file at learn time.
60
- `updated_at` is the ISO 8601 timestamp of the last
61
- supersession (`null` on first create). `tags` are
62
- user-supplied strings for scoping and filtering.
63
- `supersedes` is the kbid of the section this one
64
- replaced (`null` on first create). See
65
- [Database -- Logical Identity](../../../goals/database.md).
66
-
67
- ### `IntegrityReport`
68
-
69
- ```rust
70
- pub struct IntegrityReport {
71
- pub checksum_failures: Vec<KbId>,
72
- pub orphaned_sections: Vec<KbId>,
73
- pub orphaned_documents: Vec<String>,
74
- pub broken_refs: Vec<(KbId, String, KbId)>,
75
- pub type_mismatches: Vec<(KbId, String, KbId)>,
76
- pub circular_refs: Vec<(KbId, Vec<KbId>)>,
77
- pub divergent_content: Vec<(String, Vec<KbId>)>,
78
- }
79
- ```
80
-
81
- Returned by `check_integrity`. `broken_refs`
82
- entries are `(source_kbid, marker_type, target_kbid)`
83
- covering all six marker types (`text`, `code`,
84
- `image`, `document`, `link`, `doc-link`).
85
- `type_mismatches` are `(source_kbid, marker_type,
86
- target_kbid)` where the target section type does
87
- not match the marker type. `circular_refs` are
88
- `(start_kbid, cycle_path)` for reference chains
89
- that form loops. `divergent_content` entries are
90
- `(source_key, conflicting_kbids)` -- sections
91
- sharing the same `source_key` that have different
92
- content, indicating contradictory versions that
93
- should be resolved via supersession.
94
-
95
- ## Functions
96
-
97
- ### Frontmatter Parsing (`section/parse.rs`)
98
-
99
- ```rust
100
- pub fn parse_section_file(data: &[u8])
101
- -> Result<(SectionHeader, &[u8]), ParseError>;
102
-
103
- pub fn serialize_section_file(
104
- header: &SectionHeader,
105
- body: &[u8],
106
- ) -> Vec<u8>;
107
- ```
108
-
109
- Splits on `---\n` separator. Header is TOML; body
110
- is raw bytes stored verbatim.
111
-
112
- ### Content Hashing (`section/hash.rs`)
113
-
114
- ```rust
115
- pub fn compute_kbid(content: &[u8]) -> KbId;
116
- ```
117
-
118
- SHA-256 → truncate to 16 bytes → base32 (Crockford)
119
- → 26-character kb-id.
120
-
121
- ### Path Resolution (`section/shard.rs`)
122
-
123
- ```rust
124
- pub fn section_path(kbid: &KbId) -> String;
125
- ```
126
-
127
- Returns `sections/{first_two_chars}/{kbid}.sec`.
@@ -1,57 +0,0 @@
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.
@@ -1,20 +0,0 @@
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.
@@ -1,194 +0,0 @@
1
- # functions::retrieve -- Read & Status Orchestration
2
-
3
- *Read sections and documents from cache or disk.
4
- Report database status.*
5
-
6
- ## Source
7
-
8
- `engine/crates/kb-worker/src/orchestrate/retrieve.rs`
9
-
10
- ## Exported WASM Functions
11
-
12
- ### `worker_read_sections`
13
-
14
- ```rust
15
- pub fn worker_read_sections(
16
- kbid_ptr: *const u8,
17
- kbid_len: u32,
18
- ) -> u64;
19
- ```
20
-
21
- Read one or more sections by KbId. Returns Encode'd
22
- `Vec<SectionRecord>`.
23
-
24
- ### `worker_retrieve_doc`
25
-
26
- ```rust
27
- pub fn worker_retrieve_doc(
28
- doc_id_ptr: *const u8,
29
- doc_id_len: u32,
30
- ) -> u64;
31
- ```
32
-
33
- Read a document manifest and all its sections.
34
- Returns Encode'd `DocumentRecord`.
35
-
36
- ### `worker_status`
37
-
38
- ```rust
39
- pub fn worker_status() -> u64;
40
- ```
41
-
42
- Returns Encode'd `StatusResult` (see
43
- [types/status](../types/status.md)).
44
-
45
- ### `worker_result_status`
46
-
47
- ```rust
48
- pub fn worker_result_status(
49
- token_ptr: *const u8,
50
- token_len: u32,
51
- ) -> u32;
52
- ```
53
-
54
- Poll an async token. Returns `0` = busy,
55
- `1` = success, `2` = fail.
56
-
57
- ### `worker_get_result`
58
-
59
- ```rust
60
- pub fn worker_get_result(
61
- token_ptr: *const u8,
62
- token_len: u32,
63
- ) -> u64;
64
- ```
65
-
66
- Fetch the result buffer for a completed token.
67
- Returns `(ptr, len)`. Consumes the token (one-shot).
68
-
69
- ### `worker_invalidate`
70
-
71
- ```rust
72
- pub fn worker_invalidate(
73
- keys_ptr: *const u8,
74
- keys_len: u32,
75
- ) -> u32;
76
- ```
77
-
78
- Evict specific cache entries. Returns count evicted.
79
-
80
- ### `worker_invalidate_all`
81
-
82
- ```rust
83
- pub fn worker_invalidate_all() -> u32;
84
- ```
85
-
86
- Evict entire cache. Returns count evicted.
87
-
88
- ## `worker_recall` -- Progressive Context Expansion
89
-
90
- ```rust
91
- pub fn worker_recall(
92
- params_ptr: *const u8,
93
- params_len: u32,
94
- ) -> u64;
95
- ```
96
-
97
- WASM export for the `recall` IPC method. Accepts
98
- a serialized `RecallParams` (kbid or kbids array,
99
- depth 0--3). Returns serialized `RecallResult`
100
- (single) or `RecallResult[]` (batch) via the shared
101
- memory return slot.
102
-
103
- ### Recall Flow
104
-
105
- ```
106
- 1. Receive kbid(s) + depth
107
- |
108
- 2. For each kbid: read section from cache
109
- (disk fallback)
110
- -> SectionRecord with heading, type, docids,
111
- content
112
- |
113
- 3. If depth == 0: return section data
114
- |
115
- 4. If depth >= 1: for each docid in section's
116
- docids, read document manifest -> list of all
117
- section kbids with headings and types.
118
- Query references.idx for back-references
119
- (entries where target_id == kbid and
120
- direction == back).
121
- |
122
- 5. If depth >= 2: read full content of sibling
123
- sections (other sections in the same documents).
124
- Query references.idx for forward references
125
- (entries where source_kbid == kbid and
126
- direction == forward).
127
- |
128
- 6. If depth >= 3: for each forward-referenced
129
- section, read its full content. Query
130
- references.idx for their back-references.
131
- |
132
- Return RecallResult(s) via shared memory
133
- ```
134
-
135
- **Cache interaction:** Recall is read-heavy and
136
- benefits from the LRU cache. Section reads at
137
- depth 0 hit the cache populated by recent searches.
138
- Sibling and reference reads at depth 2--3 may cause
139
- cache misses on first access.
140
-
141
- **Reference graph lookup:** Forward and back
142
- references are read from `references.idx` via
143
- `fs-database.wasm` index lookup functions. This is
144
- O(1) per kbid via the B+ tree index -- no content
145
- re-parsing needed.
146
-
147
- ---
148
-
149
- ## Retrieve Flow
150
-
151
- ```
152
- 1. Receive KbId
153
-
154
- 2. Check cache → hit? → return cached
155
- ↓ miss
156
- 3. db_read_section → SectionRecord from disk
157
-
158
- 4. Put into cache
159
-
160
- Return SectionRecord
161
- ```
162
-
163
- Document retrieval follows the same pattern but calls
164
- `db_read_document` and caches the manifest separately
165
- from individual sections.
166
-
167
- ## Maintenance Exports
168
-
169
- ### `worker_check_integrity`
170
-
171
- ```rust
172
- pub fn worker_check_integrity() -> u64;
173
- ```
174
-
175
- Delegates to `db_check_integrity`. Returns Encode'd
176
- `IntegrityReport`.
177
-
178
- ### `worker_garbage_collect`
179
-
180
- ```rust
181
- pub fn worker_garbage_collect() -> u64;
182
- ```
183
-
184
- Delegates to `db_garbage_collect`. Returns count of
185
- removed orphans.
186
-
187
- ### `worker_rebuild_indexes`
188
-
189
- ```rust
190
- pub fn worker_rebuild_indexes() -> u64;
191
- ```
192
-
193
- Delegates to `db_index_rebuild`. Invalidates entire
194
- cache afterward.
@@ -1,173 +0,0 @@
1
- # functions::search -- Search Orchestration
2
-
3
- *End-to-end search flow: query string in, ranked
4
- results out.*
5
-
6
- ## Source
7
-
8
- `engine/crates/kb-worker/src/orchestrate/search.rs`
9
-
10
- ## Exported WASM Functions
11
-
12
- ### `worker_search`
13
-
14
- ```rust
15
- pub fn worker_search(
16
- query_ptr: *const u8,
17
- query_len: u32,
18
- limit: u32,
19
- ) -> u64;
20
- ```
21
-
22
- Synchronous search. Returns `(ptr, len)` packed into
23
- `u64`. The result buffer contains Encode'd ranked
24
- matches.
25
-
26
- ### `worker_query`
27
-
28
- ```rust
29
- pub fn worker_query(
30
- query_ptr: *const u8,
31
- query_len: u32,
32
- limit: u32,
33
- ) -> u64;
34
- ```
35
-
36
- Async search. Returns a `(ptr, len)` token string.
37
- Poll with `worker_result_status`, fetch with
38
- `worker_get_result`.
39
-
40
- ## Search Flow (13 Steps)
41
-
42
- ```
43
- 1. Receive query string from TypeScript IPC
44
-
45
- 2. qp_tokenize → token stream
46
-
47
- 3. qp_parse_query → QueryPlan
48
-
49
- 4. db_execute_plan → Vec<RawMatch>
50
-
51
- 5. db_corpus_stats → CorpusStats
52
-
53
- 6. qp_score_bm25f → base scores
54
-
55
- 7. qp_score_proximity → proximity boost
56
-
57
- 8. qp_score_zone → zone/field boost
58
-
59
- 9. Sort by combined score descending
60
-
61
- 10. Apply offset: skip first `offset` entries
62
-
63
- 11. Truncate to `limit`
64
-
65
- 12. Shape results: for each scored section, read
66
- heading, type, and docids from the section
67
- header; generate KWIC snippet (~150 chars
68
- centered on the highest-scoring matched term
69
- position from the positional index)
70
-
71
- 13. Build paged envelope: wrap shaped items with
72
- `total` (pre-offset/limit count), `offset`,
73
- `limit`, `has_more`
74
-
75
- Return PagedSearchResult via shared memory
76
- ```
77
-
78
- ### Step Details
79
-
80
- 1. **Receive query** -- TypeScript writes the UTF-8
81
- query into shared memory and calls
82
- `worker_search(ptr, len, limit)`.
83
-
84
- 2. **Tokenize** -- Calls `qp_tokenize` from
85
- query-parser. Produces a `TokenStream` with
86
- normalized, stemmed tokens.
87
-
88
- 3. **Parse** -- Calls `qp_parse_query`. Interprets
89
- boolean operators, phrase markers, field
90
- qualifiers. Produces a `QueryPlan`.
91
-
92
- 4. **Execute plan** -- Calls `db_execute_plan` from
93
- fs-database. Walks the inverted index and
94
- positional index. Returns `Vec<RawMatch>` with
95
- per-field TFs (`field_tfs`), positions,
96
- `token_count`, and `field_token_counts` per
97
- matching section.
98
-
99
- 5. **Corpus stats** -- Calls `db_corpus_stats`. Gets
100
- document count, term frequencies for IDF
101
- computation, and per-field average lengths
102
- (`avg_heading_length`, `avg_body_length`,
103
- `avg_code_length`) for BM25F normalization.
104
-
105
- 6. **BM25F** -- Calls `qp_score_bm25f`. Computes
106
- per-field BM25 using `FieldTf` values from each
107
- `RawMatch` and per-field corpus averages. Field
108
- weights (heading 2.0x, body 1.0x, code 1.5x) are
109
- applied at query time. See
110
- [Query Result](../../../../goals/query-result.md)
111
- for the full scoring specification.
112
-
113
- 7. **Proximity** -- Calls `qp_score_proximity`.
114
- Boosts documents where query terms appear close
115
- together.
116
-
117
- 8. **Zone scoring** -- Calls `qp_score_zone`. Boosts
118
- matches in high-value zones (title, headings,
119
- first paragraph).
120
-
121
- 9. **Sort** -- Combines all scores, sorts descending.
122
-
123
- 10. **Apply offset** -- Skips the first `offset`
124
- entries from the sorted result list. Enables
125
- cursor-based pagination without re-executing the
126
- query.
127
-
128
- 11. **Truncate** -- Takes at most `limit` entries
129
- from the offset-adjusted list.
130
-
131
- 12. **Shape results** -- For each scored section,
132
- reads heading, type, and docids from the section
133
- header. Generates a KWIC snippet (~150 chars
134
- centered on the highest-scoring matched term
135
- position from the positional index). See
136
- [Snippet Generation](#snippet-generation) below.
137
-
138
- 13. **Build paged envelope** -- Wraps shaped items
139
- with `total` (pre-offset/limit count), `offset`,
140
- `limit`, and `has_more`. Encodes as
141
- `PagedSearchResult` via the `Encode` trait into
142
- shared memory.
143
-
144
- ---
145
-
146
- ### Snippet Generation
147
-
148
- Snippets are generated during step 12 (result
149
- shaping), using data already available from the
150
- search pipeline:
151
-
152
- - The positional index provides the token offset
153
- of each matched term in each section.
154
- - The section content is read from cache (or disk
155
- fallback).
156
-
157
- **Algorithm:**
158
-
159
- 1. For each matched section, find the position of
160
- the highest-scoring matched term (the term with
161
- the highest BM25F contribution).
162
- 2. Convert the token position to a byte offset in
163
- the section content.
164
- 3. Extract ~150 characters centered on that byte
165
- offset. If the offset is near the start or end,
166
- shift the window accordingly.
167
- 4. Trim to word boundaries and add `...` ellipsis
168
- at truncated boundaries.
169
- 5. If the highest-scoring match is in the heading
170
- (not body), use the first ~150 characters of
171
- the body content instead.
172
- 6. For image sections (`image/*`) or sections with
173
- no body content, the snippet is empty string.