@agent-wiki/mcp 0.17.9

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 (217) hide show
  1. package/.claude-plugin/plugin.json +7 -0
  2. package/README.md +171 -0
  3. package/architecture.svg +58 -0
  4. package/dist/atlassian.d.ts +63 -0
  5. package/dist/atlassian.d.ts.map +1 -0
  6. package/dist/atlassian.js +596 -0
  7. package/dist/atlassian.js.map +1 -0
  8. package/dist/cli.d.ts +11 -0
  9. package/dist/cli.d.ts.map +1 -0
  10. package/dist/cli.js +391 -0
  11. package/dist/cli.js.map +1 -0
  12. package/dist/cobol/__tests__/call-boundary-lineage.test.d.ts +2 -0
  13. package/dist/cobol/__tests__/call-boundary-lineage.test.d.ts.map +1 -0
  14. package/dist/cobol/__tests__/call-boundary-lineage.test.js +505 -0
  15. package/dist/cobol/__tests__/call-boundary-lineage.test.js.map +1 -0
  16. package/dist/cobol/__tests__/code-analysis.test.d.ts +2 -0
  17. package/dist/cobol/__tests__/code-analysis.test.d.ts.map +1 -0
  18. package/dist/cobol/__tests__/code-analysis.test.js +465 -0
  19. package/dist/cobol/__tests__/code-analysis.test.js.map +1 -0
  20. package/dist/cobol/__tests__/db2-table-lineage.test.d.ts +2 -0
  21. package/dist/cobol/__tests__/db2-table-lineage.test.d.ts.map +1 -0
  22. package/dist/cobol/__tests__/db2-table-lineage.test.js +182 -0
  23. package/dist/cobol/__tests__/db2-table-lineage.test.js.map +1 -0
  24. package/dist/cobol/__tests__/evidence-mapping.test.d.ts +2 -0
  25. package/dist/cobol/__tests__/evidence-mapping.test.d.ts.map +1 -0
  26. package/dist/cobol/__tests__/evidence-mapping.test.js +49 -0
  27. package/dist/cobol/__tests__/evidence-mapping.test.js.map +1 -0
  28. package/dist/cobol/__tests__/extractors.test.d.ts +2 -0
  29. package/dist/cobol/__tests__/extractors.test.d.ts.map +1 -0
  30. package/dist/cobol/__tests__/extractors.test.js +189 -0
  31. package/dist/cobol/__tests__/extractors.test.js.map +1 -0
  32. package/dist/cobol/__tests__/field-lineage.test.d.ts +2 -0
  33. package/dist/cobol/__tests__/field-lineage.test.d.ts.map +1 -0
  34. package/dist/cobol/__tests__/field-lineage.test.js +329 -0
  35. package/dist/cobol/__tests__/field-lineage.test.js.map +1 -0
  36. package/dist/cobol/__tests__/graph.test.d.ts +2 -0
  37. package/dist/cobol/__tests__/graph.test.d.ts.map +1 -0
  38. package/dist/cobol/__tests__/graph.test.js +456 -0
  39. package/dist/cobol/__tests__/graph.test.js.map +1 -0
  40. package/dist/cobol/__tests__/integration.test.d.ts +2 -0
  41. package/dist/cobol/__tests__/integration.test.d.ts.map +1 -0
  42. package/dist/cobol/__tests__/integration.test.js +971 -0
  43. package/dist/cobol/__tests__/integration.test.js.map +1 -0
  44. package/dist/cobol/__tests__/lexer.test.d.ts +2 -0
  45. package/dist/cobol/__tests__/lexer.test.d.ts.map +1 -0
  46. package/dist/cobol/__tests__/lexer.test.js +111 -0
  47. package/dist/cobol/__tests__/lexer.test.js.map +1 -0
  48. package/dist/cobol/__tests__/parser.test.d.ts +2 -0
  49. package/dist/cobol/__tests__/parser.test.d.ts.map +1 -0
  50. package/dist/cobol/__tests__/parser.test.js +180 -0
  51. package/dist/cobol/__tests__/parser.test.js.map +1 -0
  52. package/dist/cobol/__tests__/variable-tracer.test.d.ts +2 -0
  53. package/dist/cobol/__tests__/variable-tracer.test.d.ts.map +1 -0
  54. package/dist/cobol/__tests__/variable-tracer.test.js +256 -0
  55. package/dist/cobol/__tests__/variable-tracer.test.js.map +1 -0
  56. package/dist/cobol/__tests__/wiki-gen.test.d.ts +2 -0
  57. package/dist/cobol/__tests__/wiki-gen.test.d.ts.map +1 -0
  58. package/dist/cobol/__tests__/wiki-gen.test.js +76 -0
  59. package/dist/cobol/__tests__/wiki-gen.test.js.map +1 -0
  60. package/dist/cobol/call-boundary-lineage.d.ts +49 -0
  61. package/dist/cobol/call-boundary-lineage.d.ts.map +1 -0
  62. package/dist/cobol/call-boundary-lineage.js +159 -0
  63. package/dist/cobol/call-boundary-lineage.js.map +1 -0
  64. package/dist/cobol/db2-table-lineage.d.ts +45 -0
  65. package/dist/cobol/db2-table-lineage.d.ts.map +1 -0
  66. package/dist/cobol/db2-table-lineage.js +127 -0
  67. package/dist/cobol/db2-table-lineage.js.map +1 -0
  68. package/dist/cobol/evidence-mapping.d.ts +31 -0
  69. package/dist/cobol/evidence-mapping.d.ts.map +1 -0
  70. package/dist/cobol/evidence-mapping.js +27 -0
  71. package/dist/cobol/evidence-mapping.js.map +1 -0
  72. package/dist/cobol/extractors.d.ts +88 -0
  73. package/dist/cobol/extractors.d.ts.map +1 -0
  74. package/dist/cobol/extractors.js +297 -0
  75. package/dist/cobol/extractors.js.map +1 -0
  76. package/dist/cobol/field-lineage.d.ts +101 -0
  77. package/dist/cobol/field-lineage.d.ts.map +1 -0
  78. package/dist/cobol/field-lineage.js +603 -0
  79. package/dist/cobol/field-lineage.js.map +1 -0
  80. package/dist/cobol/graph.d.ts +182 -0
  81. package/dist/cobol/graph.d.ts.map +1 -0
  82. package/dist/cobol/graph.js +453 -0
  83. package/dist/cobol/graph.js.map +1 -0
  84. package/dist/cobol/lexer.d.ts +15 -0
  85. package/dist/cobol/lexer.d.ts.map +1 -0
  86. package/dist/cobol/lexer.js +251 -0
  87. package/dist/cobol/lexer.js.map +1 -0
  88. package/dist/cobol/parser.d.ts +13 -0
  89. package/dist/cobol/parser.d.ts.map +1 -0
  90. package/dist/cobol/parser.js +584 -0
  91. package/dist/cobol/parser.js.map +1 -0
  92. package/dist/cobol/plugin.d.ts +23 -0
  93. package/dist/cobol/plugin.d.ts.map +1 -0
  94. package/dist/cobol/plugin.js +507 -0
  95. package/dist/cobol/plugin.js.map +1 -0
  96. package/dist/cobol/types.d.ts +73 -0
  97. package/dist/cobol/types.d.ts.map +1 -0
  98. package/dist/cobol/types.js +9 -0
  99. package/dist/cobol/types.js.map +1 -0
  100. package/dist/cobol/variable-tracer.d.ts +31 -0
  101. package/dist/cobol/variable-tracer.d.ts.map +1 -0
  102. package/dist/cobol/variable-tracer.js +361 -0
  103. package/dist/cobol/variable-tracer.js.map +1 -0
  104. package/dist/cobol/wiki-gen.d.ts +18 -0
  105. package/dist/cobol/wiki-gen.d.ts.map +1 -0
  106. package/dist/cobol/wiki-gen.js +222 -0
  107. package/dist/cobol/wiki-gen.js.map +1 -0
  108. package/dist/code-analysis.d.ts +160 -0
  109. package/dist/code-analysis.d.ts.map +1 -0
  110. package/dist/code-analysis.js +53 -0
  111. package/dist/code-analysis.js.map +1 -0
  112. package/dist/evidence-search.d.ts +43 -0
  113. package/dist/evidence-search.d.ts.map +1 -0
  114. package/dist/evidence-search.js +112 -0
  115. package/dist/evidence-search.js.map +1 -0
  116. package/dist/evidence-search.test.d.ts +2 -0
  117. package/dist/evidence-search.test.d.ts.map +1 -0
  118. package/dist/evidence-search.test.js +98 -0
  119. package/dist/evidence-search.test.js.map +1 -0
  120. package/dist/evidence.d.ts +32 -0
  121. package/dist/evidence.d.ts.map +1 -0
  122. package/dist/evidence.js +16 -0
  123. package/dist/evidence.js.map +1 -0
  124. package/dist/evidence.test.d.ts +2 -0
  125. package/dist/evidence.test.d.ts.map +1 -0
  126. package/dist/evidence.test.js +51 -0
  127. package/dist/evidence.test.js.map +1 -0
  128. package/dist/extraction.d.ts +40 -0
  129. package/dist/extraction.d.ts.map +1 -0
  130. package/dist/extraction.js +283 -0
  131. package/dist/extraction.js.map +1 -0
  132. package/dist/extraction.test.d.ts +5 -0
  133. package/dist/extraction.test.d.ts.map +1 -0
  134. package/dist/extraction.test.js +137 -0
  135. package/dist/extraction.test.js.map +1 -0
  136. package/dist/index.d.ts +38 -0
  137. package/dist/index.d.ts.map +1 -0
  138. package/dist/index.js +35 -0
  139. package/dist/index.js.map +1 -0
  140. package/dist/queue.d.ts +52 -0
  141. package/dist/queue.d.ts.map +1 -0
  142. package/dist/queue.js +139 -0
  143. package/dist/queue.js.map +1 -0
  144. package/dist/queue.test.d.ts +2 -0
  145. package/dist/queue.test.d.ts.map +1 -0
  146. package/dist/queue.test.js +161 -0
  147. package/dist/queue.test.js.map +1 -0
  148. package/dist/search.d.ts +163 -0
  149. package/dist/search.d.ts.map +1 -0
  150. package/dist/search.js +653 -0
  151. package/dist/search.js.map +1 -0
  152. package/dist/search.test.d.ts +6 -0
  153. package/dist/search.test.d.ts.map +1 -0
  154. package/dist/search.test.js +551 -0
  155. package/dist/search.test.js.map +1 -0
  156. package/dist/server.d.ts +33 -0
  157. package/dist/server.d.ts.map +1 -0
  158. package/dist/server.js +2530 -0
  159. package/dist/server.js.map +1 -0
  160. package/dist/server.test.d.ts +8 -0
  161. package/dist/server.test.d.ts.map +1 -0
  162. package/dist/server.test.js +2397 -0
  163. package/dist/server.test.js.map +1 -0
  164. package/dist/version.d.ts +2 -0
  165. package/dist/version.d.ts.map +1 -0
  166. package/dist/version.js +5 -0
  167. package/dist/version.js.map +1 -0
  168. package/dist/wiki.d.ts +496 -0
  169. package/dist/wiki.d.ts.map +1 -0
  170. package/dist/wiki.js +2677 -0
  171. package/dist/wiki.js.map +1 -0
  172. package/dist/wiki.test.d.ts +8 -0
  173. package/dist/wiki.test.d.ts.map +1 -0
  174. package/dist/wiki.test.js +1881 -0
  175. package/dist/wiki.test.js.map +1 -0
  176. package/docs/code-analysis-plugins.md +169 -0
  177. package/docs/configuration.md +135 -0
  178. package/docs/diagrams/cobol-analysis-flow.png +0 -0
  179. package/docs/diagrams/cobol-analysis-flow.svg +156 -0
  180. package/docs/evidence-envelope.md +176 -0
  181. package/docs/graph-1.gif +0 -0
  182. package/docs/legacy-code-knowledge-compiler-prd.md +511 -0
  183. package/docs/product-direction.md +120 -0
  184. package/docs/request-optimization.md +201 -0
  185. package/docs/search.md +266 -0
  186. package/docs/tools.md +269 -0
  187. package/graph-viewer/dist/cli.d.ts +3 -0
  188. package/graph-viewer/dist/cli.d.ts.map +1 -0
  189. package/graph-viewer/dist/cli.js +97 -0
  190. package/graph-viewer/dist/cli.js.map +1 -0
  191. package/graph-viewer/dist/graph.d.ts +14 -0
  192. package/graph-viewer/dist/graph.d.ts.map +1 -0
  193. package/graph-viewer/dist/graph.js +96 -0
  194. package/graph-viewer/dist/graph.js.map +1 -0
  195. package/graph-viewer/dist/index.d.ts +6 -0
  196. package/graph-viewer/dist/index.d.ts.map +1 -0
  197. package/graph-viewer/dist/index.js +4 -0
  198. package/graph-viewer/dist/index.js.map +1 -0
  199. package/graph-viewer/dist/parse.d.ts +10 -0
  200. package/graph-viewer/dist/parse.d.ts.map +1 -0
  201. package/graph-viewer/dist/parse.js +62 -0
  202. package/graph-viewer/dist/parse.js.map +1 -0
  203. package/graph-viewer/dist/server.d.ts +15 -0
  204. package/graph-viewer/dist/server.d.ts.map +1 -0
  205. package/graph-viewer/dist/server.js +168 -0
  206. package/graph-viewer/dist/server.js.map +1 -0
  207. package/graph-viewer/dist/types.d.ts +48 -0
  208. package/graph-viewer/dist/types.d.ts.map +1 -0
  209. package/graph-viewer/dist/types.js +2 -0
  210. package/graph-viewer/dist/types.js.map +1 -0
  211. package/graph-viewer/public/app.js +525 -0
  212. package/graph-viewer/public/index.html +68 -0
  213. package/graph-viewer/public/style.css +68 -0
  214. package/package.json +73 -0
  215. package/skills/aceclaw/SKILL.md +173 -0
  216. package/skills/agent-wiki/SKILL.md +184 -0
  217. package/skills/agent-wiki/references/tools-reference.md +368 -0
@@ -0,0 +1,201 @@
1
+ # Request Optimization
2
+
3
+ How to minimize LLM requests when digesting documents and querying the wiki.
4
+
5
+ ## Understanding the Constraints
6
+
7
+ ### The binding limit: raw_read truncation
8
+
9
+ `raw_read` truncates text content at **10,000 characters per document** — regardless of context window size. This cap is what determines how much of a document your agent actually sees.
10
+
11
+ | Language | 10K chars ≈ tokens | Docs per 200k context |
12
+ |----------|--------------------|----------------------:|
13
+ | English | ~2,500 | ~72 |
14
+ | Chinese | ~5,000 | ~36 |
15
+
16
+ **Documents shorter than 10K chars** (~6-7 pages) are fully read — quality is high.
17
+ **Documents longer than 10K chars** — only the first portion is read unless you paginate.
18
+
19
+ Check your corpus size before designing a workflow:
20
+
21
+ ```bash
22
+ find raw/ -type f | xargs wc -c | tail -1
23
+ # bytes ÷ 4 ≈ tokens (English)
24
+ # bytes ÷ 2 ≈ tokens (Chinese)
25
+ ```
26
+
27
+ ---
28
+
29
+ ## Pagination for Large Documents
30
+
31
+ When you specify a pagination parameter, `raw_read` bypasses the 10K truncation and returns the requested range in full, along with metadata for follow-up reads.
32
+
33
+ | Format | Param | Example | Response includes |
34
+ |--------|-------|---------|------------------|
35
+ | PDF | `pages` | `"1-10"`, `"3"`, `"1-3,7-10"` | `pagination.total_pages` |
36
+ | PPTX | `pages` | `"1-20"` | `pagination.total_slides` |
37
+ | XLSX | `sheet` | `"Revenue"` | `pagination.sheet_names`, `total_sheets` |
38
+ | DOCX / text | `offset` + `limit` | `offset: 0, limit: 200` | `pagination.total_lines`, `truncated`, `next_offset` |
39
+
40
+ **XLSX tip:** always read without `sheet` first — the response lists all available sheet names so you can then target each one individually.
41
+
42
+ **DOCX/text tip:** use `next_offset` from the response to paginate: if `truncated: true`, call again with `offset: <next_offset>`.
43
+
44
+ ---
45
+
46
+ ## Batch Digest Strategy
47
+
48
+ ### How many docs per request?
49
+
50
+ Because `raw_read` caps each document at 10K chars, batching is predictable:
51
+
52
+ ```
53
+ available context (e.g. 180k) ÷ tokens per doc = max docs per request
54
+ English: 180k ÷ 2,500 ≈ 72 docs
55
+ Chinese: 180k ÷ 5,000 ≈ 36 docs
56
+ ```
57
+
58
+ For a 10 MB corpus, you need roughly 15–25 requests minimum — context window size doesn't change this math.
59
+
60
+ ### Quality tradeoffs
61
+
62
+ | Approach | Requests | Per-doc depth | Cross-doc patterns |
63
+ |----------|:--------:|:-------------:|:------------------:|
64
+ | One doc at a time | N | High | Low |
65
+ | Batch by topic (10–15/group) | N/10 | Medium | High |
66
+ | All at once (50+) | 1 | Low / truncated | Medium |
67
+
68
+ The sweet spot for most use cases is **batching by topic**, 10–15 docs per request, and targeting synthesis pages as output.
69
+
70
+ ### Output strategy: synthesis over individual pages
71
+
72
+ When batching, write one synthesis page per batch rather than one page per document:
73
+
74
+ ```markdown
75
+ ---
76
+ title: Q1 Engineering Reports — Key Findings
77
+ type: synthesis
78
+ derived_from: [report-jan.md, report-feb.md, report-mar.md]
79
+ ---
80
+
81
+ # Cross-cutting insights from Q1 reports
82
+ ...
83
+ ```
84
+
85
+ Future queries read **1 page** instead of 15 individual reads — this is where the real request savings compound over time.
86
+
87
+ ---
88
+
89
+ ## Per-Tool Shortcuts (saves 1 request each)
90
+
91
+ These save single round trips in the most common patterns:
92
+
93
+ ### `wiki_read` — read multiple pages at once
94
+
95
+ Instead of 3 separate reads (or a verbose `batch` block):
96
+
97
+ ```json
98
+ { "tool": "wiki_read", "args": { "pages": ["concept-a.md", "concept-b.md", "concept-c.md"] } }
99
+ ```
100
+
101
+ Returns an array of results. Missing pages are marked `not_found: true` rather than throwing.
102
+
103
+ ### `wiki_write` — get the written content back
104
+
105
+ Set `return_content: true` to receive the final content inline. Eliminates a follow-up `wiki_read` in write-then-reference workflows:
106
+
107
+ ```json
108
+ { "tool": "wiki_write", "args": {
109
+ "page": "synthesis.md",
110
+ "content": "---\ntitle: Synthesis\n---\nContent...",
111
+ "return_content": true
112
+ }}
113
+ ```
114
+
115
+ Response includes `content` — no need to call `wiki_read` afterwards.
116
+
117
+ ### `wiki_search` — filter by type or tags inline
118
+
119
+ Instead of `wiki_search` + manual filtering or a separate `wiki_list`:
120
+
121
+ ```json
122
+ { "tool": "wiki_search", "args": { "query": "neural networks", "type": "concept", "tags": ["ml"] } }
123
+ ```
124
+
125
+ Filter is applied post-ranking — BM25 scores are unaffected. `tags` is an OR match (any tag satisfies).
126
+
127
+ ### `raw_coverage` — one call replaces multi-step coverage discovery
128
+
129
+ Instead of `raw_list` + `wiki_list`/`wiki_search` + client-side diff to figure out which sources are still uncompiled:
130
+
131
+ ```json
132
+ { "tool": "raw_coverage", "args": { "limit": 50, "sort": "newest" } }
133
+ ```
134
+
135
+ Returns `{ totalRaw, coveredRaw, uncoveredRaw, coverageRatio, uncovered[], truncated }` — everything the agent needs to pick what to compile next.
136
+
137
+ **Usage pattern — session-scoped, not per-turn.** Call `raw_coverage` at the **start** of a compilation session to plan, or at the **end** to confirm progress. Do **not** call it every turn as a "live dashboard" — the coverage set changes only when `raw_ingest` or `wiki_write` runs, so re-polling without those events just burns requests. Cache the result in the agent's working memory until you actually add or digest something.
138
+
139
+ ### `wiki_search` — `knowledge_gap` on miss → skip retry loop
140
+
141
+ When search returns 0 results, the response includes a ready-to-use `knowledge_gap`:
142
+
143
+ ```json
144
+ {
145
+ "results": [],
146
+ "count": 0,
147
+ "knowledge_gap": {
148
+ "query": "transformer attention",
149
+ "suggested_page": "concept-transformer-attention.md",
150
+ "suggested_title": "Transformer Attention",
151
+ "suggested_type": "concept",
152
+ "suggested_tags": ["transformer", "attention-mechanism"],
153
+ "hint": "No pages found. Use wiki_write to create ..."
154
+ }
155
+ }
156
+ ```
157
+
158
+ **Without `knowledge_gap`** (old pattern):
159
+ 1. `wiki_search` → 0 results
160
+ 2. Retry with synonyms × N
161
+ 3. `wiki_list` to see what exists
162
+ 4. Infer type/tags manually
163
+ 5. `wiki_write`
164
+
165
+ **With `knowledge_gap`** (new pattern):
166
+ 1. `wiki_search` → 0 results + `knowledge_gap`
167
+ 2. `wiki_write` with `suggested_page` / `suggested_type` / `suggested_tags`
168
+
169
+ Saves N retry searches + `wiki_list` + manual classify step. `knowledge_gap` is computed inside the existing `wiki_search` call — zero extra requests.
170
+
171
+ ---
172
+
173
+ ## Batch Tools
174
+
175
+ The `batch` tool collapses multiple operations into a single MCP request:
176
+
177
+ ```json
178
+ { "tool": "batch", "args": { "operations": [
179
+ { "tool": "wiki_read", "args": { "page": "concept-a.md" } },
180
+ { "tool": "wiki_read", "args": { "page": "concept-b.md" } },
181
+ { "tool": "wiki_write", "args": { "page": "synthesis-x.md", "content": "..." } }
182
+ ]}}
183
+ ```
184
+
185
+ | Workflow | Without | With shortcuts/batch | Savings |
186
+ |----------|:---:|:---:|:---:|
187
+ | Read 3 related pages | 3 | **1** (`wiki_read pages[]`) | 67% |
188
+ | Write then reference | 2 | **1** (`return_content: true`) | 50% |
189
+ | Search + filter by type | 2 | **1** (`type` param) | 50% |
190
+ | Search miss → create page | 3–5+ | **2** (`knowledge_gap`) | 60–80% |
191
+ | Discover uncompiled sources | 2–3 | **1** (`raw_coverage`) | 50–67% |
192
+ | Import 5 files | 5 | **1** | 80% |
193
+ | Digest 5 sources into wiki | 10 | **2** | 80% |
194
+ | Search + read top results | 6 | **1** | 83% |
195
+ | Ingest 50-file directory | 50+ | **1** | 98% |
196
+
197
+ **`wiki_search` + `wiki_read pages:[...]`** — Search first, then read the top pages in one follow-up call. This replaces the old dedicated `wiki_search_read` surface without losing batching efficiency.
198
+
199
+ **`knowledge_ingest` with `mode: "batch"`** — Scan a directory, import files, extract text with provenance (per-page PDF, per-sheet XLSX, per-slide PPTX), chunk, and pack into digest packs — all in one request.
200
+
201
+ **`knowledge_ingest` with `mode: "digest_write"`** — Write LLM-generated summaries back to wiki with structured provenance (`sources`, `sourcePacks`). Batch writes with one index rebuild at the end.
package/docs/search.md ADDED
@@ -0,0 +1,266 @@
1
+ # Search Architecture
2
+
3
+ How agent-wiki finds and ranks knowledge — from keyword matching to hybrid semantic search.
4
+
5
+ ## Overview
6
+
7
+ agent-wiki search is local, deterministic, and LLM-free. It ships with a BM25 lexical engine and an optional hybrid mode that blends BM25 with dense vector similarity for semantic recall.
8
+
9
+ | Mode | Dependency | Latency | Best for |
10
+ |------|-----------|---------|----------|
11
+ | **BM25 only** (default) | None | <10ms | Exact keywords, known terms, CJK text |
12
+ | **Hybrid BM25+Vector** | `@xenova/transformers` (~90 MB model) | 50-200ms | Semantic queries, synonym-heavy domains, cross-lingual concepts |
13
+
14
+ ---
15
+
16
+ ## BM25 Engine
17
+
18
+ ### Scoring Formula
19
+
20
+ Each wiki page is scored against the query using [Okapi BM25](https://en.wikipedia.org/wiki/Okapi_BM25) with parameters `k1=1.2, b=0.75`. Scoring runs independently across five fields with different weights:
21
+
22
+ | Field | Weight | Why |
23
+ |-------|:------:|-----|
24
+ | `title` | 4.0x | Strongest signal — a title match almost always means relevance |
25
+ | `tags` | 3.0x | Curated metadata, high precision |
26
+ | `slug` | 3.0x | File name captures topic even when title is verbose |
27
+ | `frontmatter` | 1.5x | Type, sources, dates — secondary metadata |
28
+ | `body` | 1.0x | Largest field but noisiest — baseline weight |
29
+
30
+ Final score = sum of per-field BM25 scores, weighted.
31
+
32
+ ### Query Pipeline
33
+
34
+ ```
35
+ User query
36
+ │
37
+ ├── 1. Normalize (lowercase, full-width→half-width, strip punctuation)
38
+ │
39
+ ├── 2. Tokenize (whitespace split for Latin; Intl.Segmenter + n-grams for CJK)
40
+ │
41
+ ├── 3. Expand synonyms (llm→"large language model", k8s→kubernetes, etc.)
42
+ │ ↳ Expanded terms scored at 0.4× weight to avoid diluting exact matches
43
+ │
44
+ ├── 4. Postings union (O(1) lookup per term → candidate document set)
45
+ │
46
+ ├── 5. Prefix matching (title, tags, slug only)
47
+ │ ↳ "deploy" matches "deployment"
48
+ │
49
+ ├── 6. Fuzzy matching (edit distance: 1 for 4-7 char terms, 2 for 8+)
50
+ │ ↳ "kuberntes" matches "kubernetes" (Latin only, not CJK)
51
+ │
52
+ ├── 7. Score each candidate across all 5 fields
53
+ │
54
+ ├── 8. Exact phrase boost (+8 title, +6 slug, +3 body)
55
+ │
56
+ └── 9. Sort by score, return top N
57
+ ```
58
+
59
+ ### Built-in Synonyms
60
+
61
+ The synonym table expands common abbreviations at query time:
62
+
63
+ | Term | Expands to |
64
+ |------|-----------|
65
+ | `llm` | large language model |
66
+ | `rag` | retrieval augmented generation |
67
+ | `k8s` | kubernetes |
68
+ | `ml` | machine learning |
69
+ | `dl` | deep learning |
70
+ | `cv` | computer vision |
71
+ | `nlp` | natural language processing |
72
+ | `ci` / `cd` | continuous integration / delivery |
73
+ | `auth` | authentication, authorization |
74
+ | `api` | interface, endpoint |
75
+ | `config` | configuration |
76
+ | `deps` | dependencies |
77
+ | `infra` | infrastructure |
78
+ | `repo` | repository |
79
+ | `docs` | documentation |
80
+
81
+ Expansion is bidirectional where listed. Expanded matches receive 0.4x weight to prevent synonym flooding.
82
+
83
+ ### CJK Support
84
+
85
+ For Chinese, Japanese, and Korean text:
86
+
87
+ 1. **`Intl.Segmenter`** (Node 16+) performs word-level segmentation for Chinese (`zh` locale)
88
+ 2. **2-gram + 3-gram fallback** captures sub-word patterns when segmenter is unavailable or for Japanese/Korean
89
+ 3. CJK tokens are **not fuzzy-matched** (edit distance = 0) — character-level changes are too destructive in logographic scripts
90
+
91
+ Mixed text (e.g. "GPT-4模型架构") is split into script runs and tokenized separately.
92
+
93
+ ### Inverted Index
94
+
95
+ The index is an in-memory postings map: `term → { docPath → field hit counts }`.
96
+
97
+ - **Lazy build**: constructed on first search, not on startup
98
+ - **Incremental invalidation**: `wiki_write` / `wiki_delete` marks the index stale; next search rebuilds it
99
+ - **Postings union**: only documents containing at least one query term are scored — the rest of the corpus is skipped entirely
100
+
101
+ For a wiki with 1000 pages, a typical search touches 10-50 documents, not 1000.
102
+
103
+ ---
104
+
105
+ ## Hybrid BM25+Vector Search
106
+
107
+ Hybrid mode re-ranks BM25 candidates using dense vector cosine similarity. This improves recall for semantic queries where the user's words don't match the wiki's terminology.
108
+
109
+ ### When to use hybrid
110
+
111
+ | Scenario | BM25 alone | Hybrid |
112
+ |----------|:---:|:---:|
113
+ | Exact keyword: "kubernetes pod scheduling" | Great | Great |
114
+ | Synonym: "container orchestration" → finds kubernetes page | Good (if synonym table covers it) | Better |
115
+ | Semantic: "how does auth work" → finds "OAuth 2.0 Flow" page | Weak | Strong |
116
+ | CJK keyword: "注意力机制" | Great | Great |
117
+ | Cross-concept: "tradeoffs between latency and throughput" | Weak | Better |
118
+
119
+ **Rule of thumb**: if your wiki uses consistent terminology and your queries use the same terms, BM25 alone is sufficient. If your queries are natural-language questions or your wiki has diverse terminology, enable hybrid.
120
+
121
+ ### Setup
122
+
123
+ 1. Install the embedding library:
124
+
125
+ ```bash
126
+ npm install @xenova/transformers
127
+ ```
128
+
129
+ 2. Enable in `.agent-wiki.yaml`:
130
+
131
+ ```yaml
132
+ search:
133
+ hybrid: true
134
+ ```
135
+
136
+ 3. Build the vector index (embeds all existing pages):
137
+
138
+ ```bash
139
+ agent-wiki call wiki_admin '{"action":"rebuild"}'
140
+ ```
141
+
142
+ The model (`Xenova/all-MiniLM-L6-v2`, ~90 MB) downloads automatically from HuggingFace Hub on first run and is cached locally.
143
+
144
+ After setup, every `wiki_write` automatically embeds the new/updated page — no manual steps needed.
145
+
146
+ ### How it works
147
+
148
+ ```
149
+ User query
150
+ │
151
+ ├── 1. BM25 search (same pipeline as above)
152
+ │ ↳ Fetch 3× requested limit as candidate pool
153
+ │
154
+ ├── 2. Embed query → 384-dim dense vector
155
+ │ ↳ Uses Xenova/all-MiniLM-L6-v2 sentence-transformer
156
+ │
157
+ ├── 3. For each candidate:
158
+ │ ├── Normalize BM25 score to [0, 1] (min-max within candidate set)
159
+ │ ├── Compute cosine similarity with page embedding
160
+ │ └── Final score = bm25_weight × norm_bm25 + vector_weight × cosine
161
+ │
162
+ └── 4. Re-sort by final score, return top N
163
+ ```
164
+
165
+ ### Configuration
166
+
167
+ | Key | Default | Description |
168
+ |-----|---------|-------------|
169
+ | `search.hybrid` | `false` | Enable hybrid mode |
170
+ | `search.model` | `Xenova/all-MiniLM-L6-v2` | Sentence-transformer model for embeddings |
171
+ | `search.bm25_weight` | `0.5` | Weight for BM25 score in final blend (0-1) |
172
+ | `search.vector_weight` | `0.5` | Weight for cosine similarity in final blend (0-1) |
173
+
174
+ ### Tuning weights
175
+
176
+ | Use case | `bm25_weight` | `vector_weight` | Rationale |
177
+ |----------|:---:|:---:|-----------|
178
+ | Keyword-heavy wiki (code, API docs) | 0.7 | 0.3 | Exact terms matter more than semantics |
179
+ | Natural-language wiki (meeting notes, reports) | 0.4 | 0.6 | Semantic similarity captures intent better |
180
+ | Balanced (default) | 0.5 | 0.5 | Good starting point for most wikis |
181
+ | Debugging search issues | 1.0 | 0.0 | Equivalent to pure BM25 — useful for comparison |
182
+
183
+ Weights must sum to 1.0 for consistent scoring. Adjust in `.agent-wiki.yaml` and re-run searches — no rebuild needed (weights only affect score blending, not the vector index).
184
+
185
+ ### Vector index persistence
186
+
187
+ - Embeddings are saved to `wiki/.search-vectors.json`
188
+ - Loaded on startup when `hybrid: true`
189
+ - Automatically invalidated if `search.model` changes
190
+ - `wiki_write` updates the single page's embedding incrementally
191
+ - `wiki_delete` removes the page's embedding
192
+ - `wiki_admin` with `action: "rebuild"` rebuilds all embeddings from scratch
193
+
194
+ ### Graceful degradation
195
+
196
+ Hybrid search never fails:
197
+
198
+ - If `@xenova/transformers` is not installed → falls back to pure BM25
199
+ - If the model download fails → falls back to pure BM25
200
+ - If a page has no embedding yet → that page is scored by BM25 only
201
+ - Error details are logged but not surfaced to the MCP client
202
+
203
+ ---
204
+
205
+ ## Search Result Format
206
+
207
+ Each result includes:
208
+
209
+ ```json
210
+ {
211
+ "path": "concept-transformer.md",
212
+ "score": 12.34,
213
+ "snippet": "...Attention is all you need. The Transformer architecture...",
214
+ "section": "## Architecture"
215
+ }
216
+ ```
217
+
218
+ | Field | Description |
219
+ |-------|-------------|
220
+ | `path` | Page path relative to `wiki/` |
221
+ | `score` | Final score (BM25 or hybrid blend). Higher = more relevant |
222
+ | `snippet` | ~150 char context window around the best matching term |
223
+ | `section` | The `## heading` containing the match (if any). Use with `wiki_read(section: ...)` for targeted reads |
224
+
225
+ ### `knowledge_gap` on zero results
226
+
227
+ When search returns 0 results, the response includes a ready-to-use suggestion:
228
+
229
+ ```json
230
+ {
231
+ "results": [],
232
+ "count": 0,
233
+ "knowledge_gap": {
234
+ "query": "transformer attention",
235
+ "suggested_page": "concept-transformer-attention.md",
236
+ "suggested_title": "Transformer Attention",
237
+ "suggested_type": "concept",
238
+ "suggested_tags": ["transformer", "attention-mechanism"],
239
+ "hint": "No pages found. Use wiki_write to create ..."
240
+ }
241
+ }
242
+ ```
243
+
244
+ This eliminates the retry-search-then-create loop — the agent can directly `wiki_write` the suggested page.
245
+
246
+ ---
247
+
248
+ ## Performance Characteristics
249
+
250
+ | Metric | BM25 | Hybrid |
251
+ |--------|------|--------|
252
+ | Index build | ~1ms per page | Same + ~50ms per page for embedding |
253
+ | Query latency | <10ms | 50-200ms (embedding computation) |
254
+ | Memory (1000 pages) | ~5 MB (inverted index) | ~5 MB + ~1.5 MB (384-dim vectors) |
255
+ | Disk (vector index) | 0 | ~1.5 MB for 1000 pages |
256
+ | Model download | 0 | ~90 MB (one-time, cached) |
257
+ | Accuracy (keyword queries) | High | High |
258
+ | Accuracy (semantic queries) | Medium | High |
259
+
260
+ ### Scaling
261
+
262
+ The BM25 index and vector store are both in-memory. For wikis up to ~10,000 pages this is fast and practical. Beyond that:
263
+
264
+ - BM25 inverted index scales linearly with corpus vocabulary size
265
+ - Vector index scales linearly with page count (384 floats per page)
266
+ - Query time scales with candidate set size, not corpus size (postings union)