@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,124 +0,0 @@
1
- # Goals Overview
2
-
3
- *Design goals and behavioral specifications for every
4
- major subsystem in @dikolab/kbdb.*
5
-
6
- ## Purpose
7
-
8
- The goal documents describe the **intended behavior**
9
- of each subsystem -- what it should do, how it should
10
- respond, and what constraints it operates under. They
11
- serve as implementation specifications, not
12
- descriptions of current functionality.
13
-
14
- ## AI-Agent-First Design Principle
15
-
16
- kbdb exists to provide useful context to AI agents.
17
- Every interface decision is evaluated by a single
18
- criterion: can an AI agent, seeing this response
19
- for the first time, make a relevance judgment and
20
- answer the user's question without additional
21
- round trips?
22
-
23
- This principle drives three concrete rules:
24
-
25
- 1. **Search results are self-describing.** Every
26
- result includes a snippet, heading, section type,
27
- and source document identifiers -- enough for the
28
- agent to judge relevance from the result list
29
- alone, without fetching each section individually.
30
-
31
- 2. **Progressive drill-down over batch fetching.**
32
- The `recall` tool lets the agent expand context
33
- incrementally -- first the section, then its
34
- parent document, then siblings and references --
35
- rather than forcing a choice between "fetch
36
- nothing" and "fetch everything."
37
-
38
- 3. **Rank order is the signal.** Internal scoring
39
- details (BM25F coefficients, raw term
40
- frequencies) are implementation concerns, not
41
- agent-facing data. Results are ordered by
42
- relevance; the agent reads them in order.
43
-
44
- ## Architecture
45
-
46
- The system is organized in five layers. Data flows
47
- downward from user commands or agent requests through
48
- the worker client and daemon into the WASM engine,
49
- which performs heavy processing and manages the file
50
- database.
51
-
52
- ![System Architecture](architecture.svg)
53
-
54
- ## Goal Documents
55
-
56
- ### WASM Engine
57
-
58
- - **[Database](database.md)** -- Filesystem storage
59
- engine (`fs-database.wasm`). The `.kbdb` directory
60
- structure, section and document CRUD operations,
61
- index management, and storage design.
62
- - **[Query Parser](query-parser.md)** -- Query
63
- processing and tokenization (`query-parser.wasm`).
64
- Standalone WASM module for tokenization, query
65
- parsing, keyword extraction, and ranking.
66
- - **[Document](document.md)** -- Text content format
67
- specification. Markdown body text with embedding
68
- markers and cross-reference link markers.
69
- - **[Content Parser](content-parser.md)** -- Marker
70
- extraction engine (`kbdb-shared` rlib). Scans
71
- section content for `$(type: value)` markers and
72
- returns structured reference lists.
73
- - **[Content Composer](content-composer.md)** --
74
- Content composition engine (`kbdb-shared` rlib).
75
- Resolves sections and markers, produces rendered
76
- Markdown documents from kbid and docid identifiers.
77
- - **[Query Result](query-result.md)** -- Ranking and
78
- result ordering specification. BM25F scoring with
79
- per-field term frequencies, proximity and zone
80
- boosts, score combination, result modes, and
81
- pre-computed indexing data.
82
-
83
- ### TypeScript Layer
84
-
85
- - **[CLI](cli.md)** -- The `kbdb` command and its two
86
- run modes. CLI mode commands (`learn`, `unlearn`,
87
- `search`, `content`, `check`, `gc`, `rebuild`),
88
- MCP mode dispatch, output formats, and the
89
- pass-through execution model.
90
- - **[MCP](mcp.md)** -- MCP server mode (`kbdb mcp`).
91
- Invocation, database path configuration, stdio
92
- transport, MCP tools (search, recall, retrieve,
93
- content, learn, unlearn, and more), resources,
94
- agent wiring examples, and relationship to CLI
95
- mode.
96
- - **[Worker Daemon](worker-daemon.md)** -- Cache
97
- daemon (`kb-worker.wasm` + `src/worker.ts`).
98
- The detached TypeScript daemon, WASM module loading,
99
- caching, IPC protocol, lifecycle, and configuration.
100
- - **[Worker Client](worker-client.md)** -- Shared
101
- TypeScript client module (`src/shared/worker-client/`).
102
- Daemon discovery, spawning, IPC connection, typed
103
- API, and the sole entry point used by `src/cli.ts`.
104
-
105
- ## Implementation Guide
106
-
107
- The modules documentation describes **how** the goals
108
- above are implemented -- crate and module structure,
109
- file layout, build pipeline, and coding conventions
110
- for both the Rust engine and the TypeScript layer.
111
-
112
- - **[Modules Overview](../modules/overview.md)** --
113
- Full build pipeline. How Rust compiles to WASM,
114
- lands in the TypeScript source tree, and gets
115
- bundled into `dist/`. Start here.
116
- - **[Rust/WASM Modules](../modules/rust/overview.md)**
117
- -- Cargo workspace, 5 crates, module namespaces,
118
- WASM exports/imports, size optimization, and
119
- testing.
120
- - **[TypeScript Modules](../modules/typescript/overview.md)**
121
- -- 2 entry points (`src/cli.ts`,
122
- `src/worker.ts`), 8 shared sub-modules, file
123
- conventions, dual-runtime constraints, and esbuild
124
- bundling.
@@ -1,373 +0,0 @@
1
- # Query Parser -- Query Processing and Tokenization
2
-
3
- *A shared WASM module that provides tokenization, query
4
- parsing, and term normalization for the knowledge base
5
- system.*
6
-
7
- ## Goal
8
-
9
- The query parser module (`query-parser.wasm`) provides the core text
10
- processing capabilities shared across the knowledge base
11
- system. It handles:
12
-
13
- - Tokenizing input text into normalized terms.
14
- - Parsing search queries into structured query plans.
15
- - Normalizing and stemming terms for consistent indexing
16
- and matching.
17
- - Maintaining a query-scoped cache for intermediate
18
- results during query processing.
19
-
20
- The query parser is a pure computation module with no file I/O
21
- and no host dependencies beyond shared memory. It is
22
- imported by `kb-worker.wasm`, which orchestrates query
23
- processing and content indexing by combining the
24
- query parser's text analysis with `fs-database.wasm`'s
25
- storage operations.
26
-
27
- ## WASM Module: query-parser.wasm
28
-
29
- ### Dependency Position
30
-
31
- ```
32
- query-parser.wasm fs-database.wasm
33
- (standalone) (standalone)
34
- ↑ ↑
35
- └───────┬───────────┘
36
- |
37
- kb-worker.wasm
38
- (imports both, orchestrates)
39
- ```
40
-
41
- `query-parser.wasm` is a standalone module. It imports
42
- nothing from other WASM modules and depends only on
43
- host-provided memory. `kb-worker.wasm` imports its
44
- exports to drive tokenization, query parsing, and
45
- ranking.
46
-
47
- ### Host Imports
48
-
49
- | Import | Description |
50
- |----------|-------------------------------------|
51
- | `memory` | Shared `WebAssembly.Memory` for passing strings and buffers between modules |
52
-
53
- No file I/O, no network, no timers. The module is pure
54
- computation.
55
-
56
- ### Exports
57
-
58
- Functions exported by `query-parser.wasm` for use by other
59
- WASM modules and the TypeScript host:
60
-
61
- | Export | Description |
62
- |------------------|------------------------------------|
63
- | `tokenize` | Tokenize input text into an array of normalized tokens |
64
- | `parse_query` | Parse a query string into a structured query plan |
65
- | `normalize_term` | Normalize a single term (case fold, stem) |
66
- | `stem` | Stem a single term to its root form |
67
- | `alloc` / `free` | Memory management for shared buffer allocation |
68
-
69
- All functions operate on the shared
70
- `WebAssembly.Memory`. String arguments and return values
71
- are passed as `(pointer, length)` pairs into the shared
72
- memory buffer.
73
-
74
- ## Tokenization Pipeline
75
-
76
- The query parser provides a multi-stage tokenization
77
- pipeline used for both indexing (when content is added
78
- via `kbdb learn`) and query processing (when
79
- searching). Using the same pipeline for both ensures
80
- that indexed terms match query terms consistently.
81
-
82
- The pipeline processes **text and code content**.
83
- Image sections (`image/*` type) have their binary
84
- content skipped, but their `title` and `description`
85
- attributes are tokenized through this pipeline.
86
-
87
- Code sections (`text/*` and `application/json`) are
88
- processed through the same pipeline as text sections.
89
- The identifier splitting stage (stage 4) handles
90
- camelCase and snake_case decomposition common in
91
- source code.
92
-
93
- During indexing, the pipeline runs **per field** for
94
- each section: title content produces heading-field
95
- terms, body text and description produce body-field
96
- terms, and code content produces code-field terms.
97
- Per-field term frequencies are stored separately in
98
- the inverted index, enabling BM25F field weights to
99
- be tuned at query time without rebuilding indexes.
100
-
101
- Stages:
102
-
103
- 1. **Marker stripping** -- Remove all six marker
104
- types from the input text before tokenization:
105
- `$(image:)`, `$(code:)`, `$(text:)`,
106
- `$(document:)`, `$(link:)`, and `$(doc-link:)`.
107
- These are structural references, not searchable
108
- content. The `strip_content_markers()` function is
109
- imported from `kbdb-shared` (see
110
- [Content Parser](content-parser.md) and
111
- [Document](document.md)).
112
- 2. **Unicode normalization** (NFC).
113
- 3. **Word boundary detection** (Unicode Segmentation).
114
- 4. **Identifier splitting** (camelCase, snake_case
115
- decomposition).
116
- 5. **Case folding** (lowercase).
117
- 6. **Stop word removal** (configurable list).
118
- 7. **Synonym expansion** (bidirectional synonym map --
119
- inserts synonym tokens alongside originals).
120
- 8. **Stemming** (Snowball / Porter2 -- stores both
121
- stemmed and original forms).
122
- 9. **N-gram extraction** (bigrams, trigrams for phrase
123
- indexing and multi-word term matching).
124
-
125
- `kb-worker.wasm` calls the tokenization pipeline for
126
- both indexing (when content is added via `kbdb learn`)
127
- and search (when a query is submitted). It feeds the
128
- tokenized output to `fs-database.wasm` for index
129
- storage or index lookups as needed.
130
-
131
- ### Synonym Expansion (Stage 7)
132
-
133
- Bridges the lexical gap between user vocabulary and
134
- indexed content. Without synonym expansion, a search
135
- for `"login credentials"` returns zero results when
136
- the indexed document uses "signs in" and "identity"
137
- instead -- different root words with the same meaning.
138
-
139
- **Placement rationale:** Applied after stop-word
140
- removal (stage 6) and before stemming (stage 8). This
141
- ensures synonyms are expanded on lowercased,
142
- stop-word-filtered tokens, and both the original and
143
- synonym tokens are then stemmed consistently.
144
-
145
- **Built-in synonym map:** A small, curated
146
- `HashMap<String, Vec<String>>` of bidirectional
147
- synonym pairs. Bidirectional means if `login` maps to
148
- `sign-in`, then `sign-in` also maps to `login`.
149
-
150
- **Example map entries:**
151
-
152
- | Term | Synonyms |
153
- |--------------|------------------------------------|
154
- | login | sign-in, sign_in, signin |
155
- | auth | authentication, authenticate |
156
- | remove | delete, erase |
157
- | create | add, insert |
158
- | update | modify, change, edit |
159
- | error | exception, fault, failure |
160
- | config | configuration, settings |
161
- | credential | password, identity |
162
- | user | account, member |
163
- | deploy | release, publish, ship |
164
- | search | query, find, lookup |
165
- | doc | document, documentation |
166
- | api | endpoint, interface |
167
- | db | database |
168
- | env | environment |
169
- | repo | repository |
170
- | param | parameter, argument |
171
- | dir | directory, folder |
172
- | msg | message |
173
- | info | information |
174
-
175
- **Expansion behaviour:** For each token in the stream,
176
- look up the synonym map. If found, insert the synonym
177
- tokens at the same position as the original. Both the
178
- original and its synonyms proceed to the stemming
179
- stage. This preserves the original token's position
180
- for proximity/zone scoring while adding synonym
181
- coverage.
182
-
183
- **Applied symmetrically:** The same synonym expansion
184
- applies during both `learn` (indexing) and `search`
185
- (querying). If a document uses "login" and the user
186
- searches "sign-in", the expansion during indexing
187
- produced "sign-in" tokens from "login", and the
188
- expansion during querying produced "login" tokens
189
- from "sign-in".
190
-
191
- **Not user-configurable in v1.** The built-in map is
192
- hardcoded. Future versions may allow user-defined
193
- synonym maps via a `synonyms.toml` configuration file
194
- in the `.kbdb` directory.
195
-
196
- **Implementation status:** Synonym expansion is
197
- implemented in the Rust pipeline
198
- (`shared/src/pipeline/synonyms.rs`). The
199
- built-in map covers common technical synonyms.
200
- The `query-guidance` default skill complements this
201
- by instructing AI agents to rephrase queries with
202
- alternative terms before searching.
203
-
204
- ## Query Parsing
205
-
206
- Search queries are parsed into a structured query plan
207
- -- a data retrieval strategy that describes which terms
208
- to look up, how to combine them, and what to exclude.
209
- `kb-worker.wasm` feeds this plan to `fs-database.wasm`
210
- to execute index lookups.
211
-
212
- Parsing stages:
213
-
214
- 1. **Tokenize** -- Run the query text through the
215
- tokenization pipeline.
216
- 2. **Parse syntax** -- Recognize query operators: exact
217
- phrases (`"..."`), boolean AND/OR, and term exclusion
218
- (`-term`).
219
- 3. **Build query plan** -- Produce a structured plan
220
- containing: a list of terms to match, phrase groups,
221
- exclusions, and match mode.
222
-
223
- The query plan is the query parser's output --
224
- `kb-worker.wasm` receives it and passes it to
225
- `fs-database.wasm` for index lookups, then calls the
226
- query parser's ranking functions to score and order
227
- the raw results.
228
-
229
- ### Relaxed Query Mode
230
-
231
- When a multi-term AND query returns zero results,
232
- `kb-worker.wasm` retries the query in **relaxed
233
- mode** -- OR semantics where any term matches, not
234
- all required.
235
-
236
- The query parser generates both the primary (AND)
237
- plan and a relaxed (OR) plan from the same parsed
238
- query. `kb-worker.wasm` executes the AND plan first.
239
- If the AND plan produces zero matches, it falls back
240
- to the OR plan. No new WASM export is needed --
241
- `qp_parse_query` returns a `QueryPlan` that supports
242
- mode switching via a `match_mode` field
243
- (`All` | `Any`).
244
-
245
- Relaxed results are flagged with `relaxed: true` in
246
- the `PagedSearchResult` envelope (see
247
- [Query Result -- Relaxed-Match
248
- Fallback](query-result.md)). The fallback is
249
- transparent to the caller.
250
-
251
- Single-term queries are unaffected (AND and OR are
252
- equivalent for one term). Phrase queries and
253
- exclusions are preserved in both modes -- only the
254
- inter-term combination changes.
255
-
256
- ## Query Cache
257
-
258
- During query processing, the query parser maintains a
259
- short-lived cache scoped to a single query's lifetime.
260
- This cache holds:
261
-
262
- - **Tokenized terms** -- Avoids re-tokenizing duplicate
263
- terms within the same query.
264
- - **Stemmed forms** -- Avoids re-stemming the same root
265
- across query terms.
266
- - **Parsed sub-expressions** -- Reuses parsed structures
267
- for repeated patterns.
268
-
269
- The cache exists only for the duration of one query
270
- processing cycle. It is allocated when query processing
271
- begins and discarded immediately after the query plan is
272
- returned. It is not persisted, not shared across
273
- queries, and not visible to other modules.
274
-
275
- This prevents redundant computation when a query
276
- contains repeated terms or when the same term appears in
277
- different syntactic positions.
278
-
279
- ## Keyword Extraction
280
-
281
- When text content is added to the knowledge base, the
282
- query parser extracts searchable terms from the raw
283
- text. `kb-worker.wasm` calls the query parser's
284
- tokenization exports and then applies the statistical
285
- extraction pipeline to produce terms and weights,
286
- which it passes to `fs-database.wasm` for index
287
- storage.
288
-
289
- Code sections (`text/*` and `application/json`) are
290
- processed through the keyword extraction pipeline.
291
- Their content is tokenized and weighted the same way
292
- as text sections -- the identifier splitting stage
293
- handles code-specific patterns (camelCase,
294
- snake_case).
295
-
296
- Image sections (`image/*` type) have their binary
297
- content skipped by the keyword extraction pipeline --
298
- base64-encoded data produces no searchable terms.
299
- However, their `title` (required) and `description`
300
- (optional) attributes are tokenized and indexed
301
- through the same per-field pipeline, making image
302
- sections searchable via their metadata. See
303
- [Query Result](query-result.md) for field weight
304
- details.
305
-
306
- ### Statistical Keyword Extraction
307
-
308
- Extract candidate keywords and keyphrases using
309
- statistical methods that do not require training data or
310
- external models:
311
-
312
- - **RAKE** (Rapid Automatic Keyword Extraction) --
313
- Splits on stop words, scores candidates by word
314
- degree / frequency ratio.
315
- - **N-gram scoring** -- Bigrams and trigrams scored by
316
- co-occurrence frequency.
317
-
318
- The extraction method is unsupervised and implemented
319
- in pure Rust without ML dependencies.
320
-
321
- ### Term Weighting
322
-
323
- - Compute **per-field term frequency** (TF) -- term
324
- frequencies are computed separately for each BM25F
325
- field: heading (from title), body (from body text +
326
- description), and code (from code content).
327
- - Compute corpus-wide **term rarity** (IDF -- how
328
- uncommon a term is across all stored sections).
329
- - Combine frequency and rarity into per-field weights
330
- that reflect each term's discriminative value for
331
- search (TF-IDF).
332
-
333
- `kb-worker.wasm` passes the per-field term
334
- frequencies to `fs-database.wasm` for storage in the
335
- inverted index as separate `heading_tf`, `body_tf`,
336
- and `code_tf` values per posting entry. It also
337
- passes per-field token counts for storage in the
338
- section header. These pre-computed values are
339
- retrieved during search and fed to the query parser's
340
- ranking functions. See [Database](database.md) for
341
- the storage format.
342
-
343
- ## Ranking
344
-
345
- The query parser provides ranking functions that
346
- `kb-worker.wasm` calls after retrieving raw index
347
- results from `fs-database.wasm` to order results by
348
- relevance. See [Query Result](query-result.md) for
349
- the full ranking specification including scoring
350
- formulas, field weight mapping, and pre-computed data.
351
-
352
- - **BM25F** (Field-Weighted Okapi BM25) -- Primary
353
- ranking function. Probabilistic scoring with
354
- per-field term frequencies and field weights
355
- (heading 2.0x, body 1.0x, code 1.5x). Field
356
- weights are applied at query time using the stored
357
- per-field TFs, making them tunable without index
358
- rebuilds.
359
- - **Proximity scoring** -- Supplementary signal. Boosts
360
- sections where query terms appear close together.
361
- - **Zone scoring** -- Structural weighting. Terms in
362
- the first ~20% of a section (heading zone) weighted
363
- higher than mid-body text. Uses the pre-computed
364
- `token_count` to determine the zone boundary.
365
- - **Freshness weighting** -- Optional boost based on
366
- insertion timestamps.
367
-
368
- ## Rust Crates
369
-
370
- | Crate | Role |
371
- |------------------------|-------------------------------|
372
- | `unicode-segmentation` | Word boundary detection |
373
- | `rust-stemmers` | Snowball / Porter2 stemmer |