@dikolab/kbdb 0.5.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (224) hide show
  1. package/CLA.md +68 -0
  2. package/LICENSE +676 -12
  3. package/LICENSING.md +58 -0
  4. package/README.md +47 -24
  5. package/README.md.bak +320 -0
  6. package/dist/README.md +320 -0
  7. package/dist/{chunk-BJXEVALU.mjs → chunk-QLJ33C74.mjs} +44 -44
  8. package/dist/chunk-QLJ33C74.mjs.map +7 -0
  9. package/dist/cli.cjs +550 -232
  10. package/dist/cli.cjs.map +4 -4
  11. package/dist/cli.d.ts +0 -8
  12. package/dist/cli.mjs +483 -154
  13. package/dist/cli.mjs.map +4 -4
  14. package/dist/kbdb-worker.cjs +36 -5
  15. package/dist/kbdb-worker.cjs.map +3 -3
  16. package/dist/mod.cjs +89 -30
  17. package/dist/mod.cjs.map +4 -4
  18. package/dist/mod.mjs +88 -30
  19. package/dist/mod.mjs.map +4 -4
  20. package/dist/src/cli.d.ts +0 -8
  21. package/dist/src/shared/cli/constants/defaults.constant.d.ts +1 -1
  22. package/dist/src/shared/cli/constants/recfile-command-field-map.constant.d.ts +8 -0
  23. package/dist/src/shared/cli/constants/recfile-field-maps.constant.d.ts +21 -0
  24. package/dist/src/shared/cli/functions/compute-directory-level.function.d.ts +14 -0
  25. package/dist/src/shared/cli/functions/dispatch-command.function.d.ts +2 -28
  26. package/dist/src/shared/cli/functions/format-command-output.function.d.ts +14 -0
  27. package/dist/src/shared/cli/functions/format-db-not-found.function.d.ts +20 -0
  28. package/dist/src/shared/cli/functions/format-output.function.d.ts +1 -0
  29. package/dist/src/shared/cli/functions/format-recfile-output.function.d.ts +11 -0
  30. package/dist/src/shared/cli/functions/render-recfile-value.function.d.ts +11 -0
  31. package/dist/src/shared/cli/functions/resolve-db-dir.function.d.ts +24 -0
  32. package/dist/src/shared/cli/functions/resolve-recfile-key.function.d.ts +9 -0
  33. package/dist/src/shared/cli/functions/run-db-path.function.d.ts +10 -0
  34. package/dist/src/shared/cli/functions/run-export.function.d.ts +8 -4
  35. package/dist/src/shared/cli/functions/run-import.function.d.ts +4 -0
  36. package/dist/src/shared/cli/functions/run-learn.function.d.ts +12 -0
  37. package/dist/src/shared/cli/index.d.ts +4 -1
  38. package/dist/src/shared/cli/typings/cli-options.interface.d.ts +9 -2
  39. package/dist/src/shared/cli/typings/db-resolution.interface.d.ts +9 -0
  40. package/dist/src/shared/cli/typings/recfile-field-map.type.d.ts +10 -0
  41. package/dist/src/shared/version/constants/version.constant.d.ts +1 -1
  42. package/dist/src/shared/wasm-codec/functions/decode-option-u8.function.d.ts +9 -0
  43. package/dist/src/shared/wasm-codec/functions/decode-section-records.function.d.ts +4 -0
  44. package/dist/src/shared/wasm-codec/functions/encode-add-section-params.function.d.ts +2 -45
  45. package/dist/src/shared/wasm-codec/functions/encode-update-section-params.function.d.ts +10 -1
  46. package/dist/src/shared/wasm-codec/index.d.ts +1 -1
  47. package/dist/src/shared/wasm-codec/typings/add-section-input.model.d.ts +6 -0
  48. package/dist/src/shared/wasm-codec/typings/section-record-trailing.model.d.ts +5 -0
  49. package/dist/src/shared/wasm-codec/typings/section-record.model.d.ts +5 -0
  50. package/dist/src/shared/worker-client/functions/build-deno-permissions.function.d.ts +11 -0
  51. package/dist/src/shared/worker-client/functions/read-crash-reason.function.d.ts +9 -0
  52. package/dist/src/shared/worker-client/functions/resolve-spawn-script.function.d.ts +6 -0
  53. package/dist/src/shared/worker-client/functions/spawn-daemon.function.d.ts +1 -1
  54. package/dist/src/shared/worker-client/typings/section.model.d.ts +5 -0
  55. package/dist/src/shared/worker-client/typings/worker-client-options.interface.d.ts +0 -6
  56. package/dist/src/shared/worker-daemon/functions/count-files.function.d.ts +12 -0
  57. package/dist/src/shared/worker-daemon/functions/handle-import.function.d.ts +2 -0
  58. package/dist/src/shared/worker-daemon/functions/handle-read-ops.function.d.ts +8 -0
  59. package/dist/src/shared/worker-daemon/functions/validate-daemon-path.function.d.ts +15 -0
  60. package/dist/src/worker.d.ts +0 -7
  61. package/dist/wasm/default-embedding/kbdb_default_embedding_bg.wasm +0 -0
  62. package/dist/wasm/default-embedding/package.json +1 -1
  63. package/dist/wasm/fs-database/kbdb_fs_database_bg.wasm +0 -0
  64. package/dist/wasm/fs-database/package.json +1 -1
  65. package/dist/wasm/fs-migration/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 +10 -9
  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,25 +0,0 @@
1
- # term -- Normalized Search Term
2
-
3
- *Newtype wrapper for normalized, stemmed search terms
4
- used as keys in inverted indexes and query plans.*
5
-
6
- ## Source
7
-
8
- `engine/crates/shared/src/term/`
9
-
10
- ## Responsibility
11
-
12
- Defines the `Term` newtype with ordering support for
13
- B+ tree keys. Terms are the atomic unit of the
14
- inverted index -- every index lookup, ranking
15
- computation, and query plan operates on `Term`
16
- values.
17
-
18
- See [Query Parser -- Tokenization Pipeline](
19
- ../../../../goals/query-parser.md) for the pipeline
20
- that produces terms.
21
-
22
- ## Exports
23
-
24
- - **[types.md](types.md)** -- `Term` newtype,
25
- `from_stemmed`, `TermError`.
@@ -1,139 +0,0 @@
1
- # types::term -- Normalized Search Term
2
-
3
- *Newtype wrapper around a normalized, stemmed term
4
- used as the key in inverted indexes and query plans.*
5
-
6
- ## File
7
-
8
- `engine/crates/shared/src/term/term.rs`
9
-
10
- ## Overview
11
-
12
- A `Term` represents a single searchable token after
13
- the full tokenization pipeline has run: Unicode
14
- normalization, case folding, stemming, and stop word
15
- removal. Terms are the atomic unit of the inverted
16
- index -- every index lookup, every ranking
17
- computation, and every query plan operates on `Term`
18
- values.
19
-
20
- See [Query Parser -- Tokenization Pipeline](
21
- ../../../../../goals/query-parser.md) for the pipeline
22
- that produces terms.
23
-
24
- ## Type Definition
25
-
26
- ```rust
27
- #[derive(Clone, Debug, PartialEq, Eq,
28
- Hash, Ord, PartialOrd)]
29
- pub struct Term(String);
30
- ```
31
-
32
- The inner `String` is private. Construction goes
33
- through `Term::new`, which validates the invariants.
34
-
35
- ## Invariants
36
-
37
- A valid `Term`:
38
-
39
- - Is non-empty.
40
- - Contains only lowercase characters (post-case-fold).
41
- - Contains no whitespace.
42
- - Is not a stop word (stop words are filtered before
43
- `Term` construction).
44
- - May be a stemmed form (e.g. `"comput"` from
45
- `"computing"`).
46
-
47
- These invariants are enforced at construction time
48
- by the tokenization pipeline. Code that receives a
49
- `Term` can rely on them without re-checking.
50
-
51
- ## Construction
52
-
53
- ```rust
54
- impl Term {
55
- pub fn new(s: &str) -> Result<Self, TermError>;
56
-
57
- pub fn from_stemmed(s: String) -> Self;
58
- }
59
- ```
60
-
61
- ### `Term::new`
62
-
63
- Validates the input string against the term
64
- invariants. Returns `TermError::Empty` for empty
65
- strings, `TermError::ContainsWhitespace` if
66
- whitespace is found.
67
-
68
- This is the safe constructor for terms created
69
- outside the pipeline (e.g. from deserialized index
70
- data).
71
-
72
- ### `Term::from_stemmed`
73
-
74
- Unchecked constructor for terms produced by the
75
- stemming stage. The pipeline guarantees the
76
- invariants, so validation is skipped. This is a
77
- performance path -- only the tokenization pipeline
78
- should call it.
79
-
80
- ## Methods
81
-
82
- ```rust
83
- impl Term {
84
- pub fn as_str(&self) -> &str;
85
-
86
- pub fn len(&self) -> usize;
87
- }
88
- ```
89
-
90
- ### `as_str`
91
-
92
- Returns the inner string slice. Used for index
93
- lookups, display, and serialization.
94
-
95
- ### `len`
96
-
97
- Returns the byte length. Used by TF-IDF weighting
98
- and B+ tree key sizing.
99
-
100
- ## Trait Implementations
101
-
102
- | Trait | Purpose |
103
- |--------------|----------------------------------|
104
- | `Clone` | Terms are stored in multiple structures (index, cache, results) |
105
- | `Debug` | Diagnostic output |
106
- | `PartialEq` / `Eq` | Term matching in index lookups |
107
- | `Hash` | HashMap keys (inverted index, query cache) |
108
- | `Ord` / `PartialOrd` | B+ tree key ordering in `fs-database` |
109
- | `Display` | Formats as the raw term string |
110
- | `Encode` | Binary serialization for shared memory (see [encode](../encode/traits.md)) |
111
-
112
- `Ord` **is** derived for `Term` (unlike `KbId`)
113
- because the B+ tree index requires ordered keys for
114
- range queries and efficient lookups.
115
-
116
- ## Error Type
117
-
118
- ```rust
119
- #[derive(Debug)]
120
- pub enum TermError {
121
- Empty,
122
- ContainsWhitespace,
123
- }
124
- ```
125
-
126
- ## Testing
127
-
128
- Unit tests:
129
-
130
- - `new` accepts lowercase, non-empty, no-whitespace
131
- strings.
132
- - `new` rejects empty strings and strings with
133
- spaces, tabs, or newlines.
134
- - `from_stemmed` produces a valid term without
135
- validation overhead.
136
- - Ordering: terms sort lexicographically
137
- (`"apple" < "banana"`).
138
- - `Encode` round-trip: encode then decode produces
139
- an equal `Term`.
@@ -1,131 +0,0 @@
1
- # src/cli.ts -- CLI and MCP Executable
2
-
3
- *Thin entry point that dispatches to CLI mode or MCP
4
- server mode based on the first argument.*
5
-
6
- ## File
7
-
8
- `src/cli.ts`
9
-
10
- ## Built To
11
-
12
- | Format | Output | Notes |
13
- |--------|------------------|----------------|
14
- | ESM | `dist/cli.mjs` | shebang, npm bin |
15
- | CJS | `dist/cli.cjs` | shebang |
16
- | `.d.ts`| `dist/cli.d.ts` | |
17
-
18
- The npm `bin` entry `kbdb` points to `dist/cli.mjs`.
19
-
20
- ## Run Modes
21
-
22
- ```
23
- kbdb mcp [--db <path>] → MCP server mode
24
- kbdb <command> [options] → CLI mode
25
- kbdb --non-interactive <command> → non-interactive
26
- ```
27
-
28
- The executable inspects its first argument. If `mcp`,
29
- it starts as a long-lived MCP server. Otherwise, it
30
- runs a single CLI command and exits.
31
-
32
- ## Execution Flow
33
-
34
- ### CLI Mode
35
-
36
- ```
37
- src/cli.ts
38
-
39
- ├─ import detectRuntime from shared/runtime/
40
- ├─ import getArgs from shared/runtime/
41
- ├─ import parseArgs from shared/cli/
42
- ├─ import createWorkerClient from
43
- │ shared/worker-client/
44
-
45
- ├─ detect runtime (Deno / Node.js)
46
- ├─ argv = getArgs()
47
- ├─ options = parseArgs(argv)
48
-
49
- ├─ if options.init:
50
- │ └─ ensureDb(options.dbPath)
51
-
52
- ├─ if options.command ≠ 'mcp':
53
- │ ├─ client = await createWorkerClient(options)
54
- │ ├─ if options.migrate:
55
- │ │ └─ run database migration
56
- │ ├─ match command:
57
- │ │ ├─ learn → runLearn(client, options)
58
- │ │ ├─ unlearn → runUnlearn(client, options)
59
- │ │ ├─ search → runSearch(client, options)
60
- │ │ ├─ content → runContent(client, options)
61
- │ │ ├─ check → runCheck(client, options)
62
- │ │ ├─ gc → runGc(client, options)
63
- │ │ └─ rebuild → runRebuild(client, options)
64
- │ ├─ formatOutput(result, options.format)
65
- │ ├─ print to stdout
66
- │ └─ client.disconnect()
67
-
68
- └─ exit
69
- ```
70
-
71
- ### MCP Mode
72
-
73
- ```
74
- src/cli.ts
75
-
76
- ├─ argv[0] === 'mcp'
77
- ├─ resolve --db flag or KBDB_DB_DIR env or cwd
78
- ├─ resolve --non-interactive or KBDB_NON_INTERACTIVE
79
- ├─ client = await createWorkerClient({ targetDir })
80
- ├─ server = createMcpServer(client)
81
- ├─ server.listen() ← stdio, blocks until closed
82
- └─ client.disconnect()
83
- ```
84
-
85
- ## Imports
86
-
87
- ```ts
88
- import {
89
- detectRuntime,
90
- } from './src/shared/runtime/functions/detect-runtime.function.ts';
91
- import {
92
- getArgs,
93
- } from './src/shared/runtime/functions/get-args.function.ts';
94
- import {
95
- parseArgs,
96
- } from './src/shared/cli/functions/parse-args.function.ts';
97
- import {
98
- createWorkerClient,
99
- } from './src/shared/worker-client/functions/create-worker-client.function.ts';
100
- import {
101
- createMcpServer,
102
- } from './src/shared/mcp/functions/create-mcp-server.function.ts';
103
- ```
104
-
105
- ## No Business Logic
106
-
107
- This file contains **only** runtime detection, arg
108
- parsing dispatch, and mode selection. All logic lives
109
- in the shared sub-modules:
110
-
111
- | Concern | Module |
112
- |-------------------|---------------------------|
113
- | Arg parsing | `shared/cli/` |
114
- | CLI commands | `shared/cli/` |
115
- | Output formatting | `shared/cli/` |
116
- | MCP server | `shared/mcp/` |
117
- | IPC to daemon | `shared/worker-client/` |
118
- | Runtime detection | `shared/runtime/` |
119
-
120
- ## Dual Runtime
121
-
122
- Works on both Deno and Node.js. Runtime-specific
123
- code is handled by `shared/runtime/` and
124
- `shared/platform/`.
125
-
126
- - **Deno**: `deno run --allow-all src/cli.ts learn ./docs`
127
- - **Node**: `node dist/cli.mjs learn ./docs`
128
- - **npm bin**: `kbdb learn ./docs`
129
-
130
- See [CLI](../../goals/cli.md) for the full behavioral
131
- specification.
@@ -1,150 +0,0 @@
1
- # src/worker.ts -- Worker Daemon Executable
2
-
3
- *Thin entry point for the detached daemon process that
4
- loads WASM modules and serves IPC requests.*
5
-
6
- ## File
7
-
8
- `src/worker.ts`
9
-
10
- ## Built To
11
-
12
- | Format | Output | Notes |
13
- |--------|--------------------------|---------|
14
- | ESM | `dist/worker.mjs` | shebang |
15
- | CJS | `dist/worker.cjs` | shebang |
16
- | `.d.ts`| `dist/worker.d.ts` | |
17
-
18
- Not registered as an npm `bin` -- this executable is
19
- not invoked directly by users. It is spawned as a
20
- detached child process by `shared/worker-client/`.
21
-
22
- ## How It Is Spawned
23
-
24
- The worker-client module spawns this executable when
25
- no running daemon is found for the target database:
26
-
27
- **Node.js:**
28
-
29
- ```ts
30
- spawn(
31
- 'node',
32
- ['kbdb-worker.js', contextPath, '--log-level', logLevel],
33
- { detached: true, stdio: 'ignore' },
34
- ).unref();
35
- ```
36
-
37
- **Deno:**
38
-
39
- ```ts
40
- new Deno.Command('deno', {
41
- args: [
42
- 'run', '--allow-all',
43
- 'src/worker.ts', contextPath,
44
- '--log-level', logLevel,
45
- ],
46
- stdin: 'null',
47
- stdout: 'null',
48
- stderr: 'piped',
49
- }).spawn().unref();
50
- ```
51
-
52
- The first command-line argument is the **context path**
53
- -- the absolute path to the `.kbdb` database directory.
54
- The optional `--log-level <level>` argument controls
55
- daemon log verbosity. It is forwarded from the
56
- `KBDB_LOG_LEVEL` environment variable by the spawner.
57
-
58
- The `KBDB_NON_INTERACTIVE` environment variable is
59
- inherited by the daemon through normal process
60
- environment inheritance (no explicit forwarding
61
- needed).
62
-
63
- ## Execution Flow
64
-
65
- ```
66
- src/worker.ts
67
-
68
- ├─ import startDaemon from shared/worker-daemon/
69
-
70
- ├─ read contextPath from argv[0]
71
- ├─ validate contextPath exists
72
- ├─ parse --log-level from argv (if present)
73
- ├─ configure log level
74
- │ (CLI arg → KBDB_LOG_LEVEL env → ERROR default)
75
-
76
- └─ await startDaemon(contextPath)
77
-
78
- ├─ readConfig(contextPath)
79
- │ └─ parse .kbdb/worker.toml
80
-
81
- ├─ loadWasmModules(config)
82
- │ ├─ allocate shared WebAssembly.Memory
83
- │ ├─ compile query-parser.wasm (standalone)
84
- │ ├─ compile fs-database.wasm (standalone)
85
- │ ├─ compile default-embedding.wasm
86
- │ │ (standalone, no host imports)
87
- │ └─ compile kb-worker.wasm
88
- │ (imports query-parser + fs-database)
89
-
90
- ├─ write PID file
91
- │ └─ kb-worker-{ctx}.pid in tmpdir
92
-
93
- ├─ createIpcServer(ctx, handler)
94
- │ └─ Unix socket or named pipe
95
-
96
- ├─ listen for connections
97
- │ └─ JSON-RPC 2.0, newline-delimited
98
-
99
- └─ on idle timeout or signal:
100
- ├─ stop accepting connections
101
- ├─ wait for in-flight requests
102
- ├─ purge cache
103
- ├─ remove PID file
104
- ├─ remove IPC socket
105
- └─ exit
106
- ```
107
-
108
- ## Imports
109
-
110
- ```ts
111
- import {
112
- startDaemon,
113
- } from './src/shared/worker-daemon/functions/start-daemon.function.ts';
114
- ```
115
-
116
- ## No Business Logic
117
-
118
- This file contains **only** argument reading and a
119
- single function call. All daemon logic lives in
120
- `shared/worker-daemon/`:
121
-
122
- | Concern | Module |
123
- |--------------------|-----------------------------|
124
- | WASM loading | `shared/worker-daemon/` |
125
- | IPC server | `shared/worker-daemon/` |
126
- | Config reading | `shared/worker-daemon/` |
127
- | Cache management | WASM (`kb-worker.wasm`) |
128
- | Query orchestration| WASM (`kb-worker.wasm`) |
129
- | Index operations | WASM (`fs-database.wasm`) |
130
- | Tokenization | WASM (`query-parser.wasm`) |
131
- | TF-IDF embedding | WASM (`default-embedding.wasm`) |
132
- | Platform helpers | `shared/platform/` |
133
-
134
- ## Lifecycle
135
-
136
- 1. Spawned by worker-client as detached child process
137
- 2. Loads and compiles 4 WASM modules (once)
138
- 3. Serves IPC requests from any connected client
139
- 4. Shuts down after idle timeout (`timeout_ms`) or
140
- OS signal (SIGTERM, SIGINT)
141
- 5. Cleans up PID file and IPC socket on exit
142
-
143
- Multiple daemons can run simultaneously on the same
144
- machine, each serving a different database directory.
145
- Each daemon is identified by its **context ID**
146
- (SHA-256 of the database path, truncated to 16 hex
147
- chars).
148
-
149
- See [Worker Daemon](../../goals/worker-daemon.md) for
150
- the full behavioral specification.