Files
mnemazine/scripts/mnemazine-kb-search.mjs
T
Зарубин ФилиппandClaude Opus 4.8 103efd029a feat(search): facet-diverse swarm + verify lenses + eval harness + install greeting
2A perspective-diversity for ~free: the planner call now returns 2-4 query
facets (real axes of the question) alongside terms. Facets steer 0-token recon
routing, give each shard a focus axis in its prompt, and seed synthesis themes
— diversity without extra agents.

2B opt-in verify (MNEMAZINE_SEARCH_VERIFY=1): one agent per top-K finding judges
3 lenses at once (grounded/relevant/actionable) against the note's own text and
drops ungrounded findings. Bounded (K calls), default off to keep tokens tight.

Eval harness (tests/search-eval.mjs, §5): Tier A deterministic recall/anti-noise
over an inline synthetic vault (0 tokens, wired into release-check); Tier B
opt-in LLM judge scoring the deep report for groundedness/relevance/coverage
(generator != evaluator). New `npm run search:eval`.

install.sh: bilingual (RU/EN) greeting from the author on first install —
thanks, GitHub star ask, invite to suggest improvements.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-19 18:52:30 +03:00

382 lines
20 KiB
JavaScript

#!/usr/bin/env node
// KB topic search — an orchestrator-led agent swarm over the live vault.
// Pipeline (mirrors the swarm playbook: recon -> fan-out -> fan-in):
// 1. RECON (0 tokens): keyword-score every note, keep the top candidates.
// --deep also asks the orchestrator to widen the query first.
// 2. FAN-OUT (swarm): shard candidates; a bounded pool of agents reads each
// shard and returns findings (note / source / insight / relevance).
// Workers write to a shared store (the result), never chat. One
// failing worker never blocks the swarm (inner+outer try/catch).
// 3. FAN-IN (synthesis): the orchestrator dedups + synthesizes findings into
// a Russian "Справка" report. Generator != evaluator — workers
// extract, the orchestrator judges and writes.
// node scripts/mnemazine-kb-search.mjs --topic "..." [--vault <p>] [--deep]
// Default is conservative (0 tokens, local extract). --deep / MNEMAZINE_DEEP=1
// engages the LLM swarm. Report written to reports/ as Markdown; path printed.
import { promises as fs } from 'node:fs'
import path from 'node:path'
import { resolveVault } from './mnemazine-paths.mjs'
import { llmAvailable, llmJson, fenceUntrusted } from './mnemazine-llm.mjs'
const argv = process.argv.slice(2)
const SELFTEST = argv.includes('--selftest')
function arg(name, fb = '') {
const hit = argv.find(a => a === `--${name}` || a.startsWith(`--${name}=`))
if (!hit) return fb
return hit.includes('=') ? hit.split('=').slice(1).join('=') : argv[argv.indexOf(hit) + 1] || fb
}
const DEEP = argv.includes('--deep') || process.env.MNEMAZINE_DEEP === '1'
const PROVIDER = arg('provider', process.env.MNEMAZINE_LLM || 'claude')
const CONCURRENCY = Number(arg('concurrency', process.env.MNEMAZINE_CONCURRENCY || '4'))
const MAX_NOTES = Number(arg('max-notes', '60')) // candidate cap fed to the swarm
const SHARD_SIZE = Number(arg('shard-size', '6')) // notes per agent
const MAX_SHARDS = Number(arg('max-shards', process.env.MNEMAZINE_SEARCH_MAX_SHARDS || '12')) // deep cost cap: hard limit on agents spawned
const EXCERPT = 2500 // chars of each note shown to an agent
// 2B perspective-diverse verify: opt-in (extra K agents). Default OFF to keep
// the token budget tight — enable when groundedness matters. One call per
// top-K finding, judging 3 lenses at once (grounded/relevant/actionable).
const VERIFY = argv.includes('--verify') || process.env.MNEMAZINE_SEARCH_VERIFY === '1'
const MAX_VERIFY = Number(arg('max-verify', '5')) // top-K findings to verify
// --- helpers -----------------------------------------------------------------
async function walk(dir, out = []) {
for (const e of await fs.readdir(dir, { withFileTypes: true }).catch(() => [])) {
if (e.name.startsWith('.')) continue
const full = path.join(dir, e.name)
if (e.isDirectory()) await walk(full, out)
else if (e.name.endsWith('.md')) out.push(full)
}
return out
}
const STOP = new Set(['the', 'and', 'для', 'что', 'как', 'это', 'про', 'или', 'был', 'are', 'with', 'из', 'на', 'по', 'не', 'от'])
const tokens = s => (String(s).toLowerCase().match(/[\p{L}\p{N}]{3,}/gu) || []).filter(t => !STOP.has(t))
function scoreNote(text, rel, terms) {
const hay = (rel + '\n' + text).toLowerCase()
let score = 0
for (const t of terms) {
let from = 0, n = 0
while ((from = hay.indexOf(t, from)) !== -1) { n++; from += t.length; if (n >= 8) break }
score += n
if (rel.toLowerCase().includes(t)) score += 4 // title/path hit weighs more
}
return score
}
// Pull a few keyword-centered snippets — the local (0-token) "finding".
function snippets(text, terms, max = 3) {
const out = []
const lower = text.toLowerCase()
for (const t of terms) {
const i = lower.indexOf(t)
if (i === -1) continue
const s = Math.max(0, i - 120), e = Math.min(text.length, i + 200)
const snip = text.slice(s, e).replace(/\s+/g, ' ').trim()
if (snip && !out.includes(snip)) out.push(snip)
if (out.length >= max) break
}
return out
}
// Bounded-concurrency pool — the swarm. One failing task never blocks the rest.
async function mapLimit(items, limit, fn) {
let i = 0
const workers = Array.from({ length: Math.max(1, Math.min(limit, items.length || 1)) }, async () => {
while (i < items.length) { const idx = i++; await fn(items[idx], idx) }
})
await Promise.all(workers)
}
const FINDINGS_SCHEMA = {
type: 'object', additionalProperties: false, required: ['findings'],
properties: {
findings: {
type: 'array',
items: {
type: 'object', additionalProperties: false,
required: ['note', 'insight', 'relevance'],
properties: {
note: { type: 'string' },
source: { type: 'string' },
insight: { type: 'string' },
relevance: { type: 'integer', minimum: 1, maximum: 5 }
}
}
}
}
}
const SYNTH_SCHEMA = {
type: 'object', additionalProperties: false,
required: ['summary', 'themes', 'gaps'],
properties: {
summary: { type: 'string' },
themes: {
type: 'array',
items: {
type: 'object', additionalProperties: false, required: ['title', 'points'],
properties: { title: { type: 'string' }, points: { type: 'array', items: { type: 'string' } } }
}
},
connections: { type: 'array', items: { type: 'string' } },
contradictions: { type: 'array', items: { type: 'string' } },
gaps: { type: 'array', items: { type: 'string' } },
next: { type: 'array', items: { type: 'string' } }
}
}
const VERDICT_SCHEMA = {
type: 'object', additionalProperties: false,
required: ['grounded', 'relevant', 'actionable'],
properties: {
grounded: { type: 'boolean' }, relevant: { type: 'boolean' },
actionable: { type: 'boolean' }, reason: { type: 'string' }
}
}
// --- orchestrator: plan the query (deep only) --------------------------------
// 2A: one call returns BOTH search terms AND 2-4 facets (the real axes of THIS
// query). Facets steer 0-token recon routing, shard-agent focus, and synthesis
// themes — perspective diversity for ~free (no extra agents). Local mode skips
// it and stays 0-token (facets:[]).
async function plan(topic) {
if (!DEEP || !llmAvailable(PROVIDER)) return { terms: tokens(topic), facets: [] }
try {
const out = await llmJson(
`Тема поиска по базе знаний: "${topic}".
1) terms: ключевые слова и синонимы (рус+англ) для полнотекстового поиска — плоский список.
2) facets: 2-4 РЕАЛЬНЫЕ оси/под-вопроса именно этой темы (не общие "плюсы/минусы"). Для каждой: label (короткое имя оси) и terms (свои ключевые слова оси).`,
{
type: 'object', additionalProperties: false, required: ['terms', 'facets'],
properties: {
terms: { type: 'array', items: { type: 'string' } },
facets: {
type: 'array',
items: {
type: 'object', additionalProperties: false, required: ['label', 'terms'],
properties: { label: { type: 'string' }, terms: { type: 'array', items: { type: 'string' } } }
}
}
}
},
{ provider: PROVIDER }
)
const terms = new Set(tokens(topic))
for (const t of out.terms || []) for (const w of tokens(t)) terms.add(w)
const facets = (out.facets || []).slice(0, 4)
.map(f => ({ label: String(f.label || '').trim(), terms: [...new Set((f.terms || []).flatMap(tokens))] }))
.filter(f => f.label && f.terms.length)
for (const f of facets) for (const w of f.terms) terms.add(w)
return { terms: [...terms], facets }
} catch (e) {
console.error(`[kb-search] query planning failed: ${e.message}; using raw terms`)
return { terms: tokens(topic), facets: [] }
}
}
// Assign a candidate note to its best-matching facet (max facet-term hits).
// 0-token. Returns facet label or 'Общее' when no facet wins.
function assignFacet(note, facets) {
if (!facets.length) return null
const hay = (note.rel + '\n' + note.text).toLowerCase()
let best = null, bestN = 0
for (const f of facets) {
let n = 0
for (const t of f.terms) if (hay.includes(t)) n++
if (n > bestN) { bestN = n; best = f.label }
}
return best || 'Общее'
}
// --- worker: extract findings from one shard ---------------------------------
async function extractShard(topic, shard, facet) {
if (DEEP && llmAvailable(PROVIDER)) {
const blob = shard.map(n =>
fenceUntrusted('NOTE', `### ${n.rel}\n${n.text.slice(0, EXCERPT)}`)).join('\n\n')
const focus = facet ? `\nФокус-ось этого шарда: «${facet}» — приоритет находкам по этой оси (но не пропускай иное важное по теме).` : ''
const prompt = `Ты агент-исследователь. Тема: "${topic}".${focus}
Из заметок ниже выбери ТОЛЬКО релевантное теме. Для каждой находки: note (путь заметки), source (URL/ссылка из заметки, если есть), insight (1-2 предложения по-русски — что важного для темы), relevance (1-5). Ничего не выдумывай сверх заметок.
${blob}`
try {
const out = await llmJson(prompt, FINDINGS_SCHEMA, { provider: PROVIDER, tools: [] })
return (out.findings || []).filter(f => f.insight && f.relevance >= 2)
} catch (e) {
console.error(`[kb-search] shard agent failed: ${e.message}; local fallback`)
}
}
// Local (0-token) fallback: snippet extraction.
const terms = tokens(topic)
return shard.flatMap(n => {
const snips = snippets(n.text, terms)
return snips.length ? [{ note: n.rel, source: n.url || '', insight: snips.join(' … '), relevance: Math.min(5, 2 + Math.floor(n.score / 4)) }] : []
})
}
// --- verify (2B): perspective-diverse check on the top-K findings ------------
// One agent per finding, judging 3 lenses at once (grounded/relevant/actionable)
// against the note's own text. Bounded (K calls). Drops ungrounded findings —
// the anti-hallucination gate (§6 claim->content). Opt-in via VERIFY.
async function verifyFindings(topic, findings, textByNote) {
if (!VERIFY || !DEEP || !llmAvailable(PROVIDER) || !findings.length) return { kept: findings, dropped: 0 }
const top = findings.slice(0, MAX_VERIFY)
const rest = findings.slice(MAX_VERIFY)
const verdicts = []
await mapLimit(top, CONCURRENCY, async (f, idx) => {
const note = textByNote.get(f.note) || ''
const prompt = `Тема: "${topic}". Находка из заметки «${f.note}»:
"${f.insight}"
Оцени по 3 осям относительно текста заметки ниже: grounded (находка реально опирается на текст, не выдумана), relevant (по теме), actionable (полезна на практике). Верни булевы + reason.
${fenceUntrusted('NOTE', note.slice(0, EXCERPT))}`
try { verdicts[idx] = await llmJson(prompt, VERDICT_SCHEMA, { provider: PROVIDER, tools: [] }) }
catch (e) { console.error(`[kb-search] verify failed for ${f.note}: ${e.message}; keeping finding`); verdicts[idx] = { grounded: true, relevant: true, actionable: true } }
})
const kept = top.filter((_, i) => verdicts[i]?.grounded !== false).concat(rest)
return { kept, dropped: top.length - (kept.length - rest.length) }
}
// --- orchestrator: synthesize findings into a report -------------------------
async function synthesize(topic, findings, facets = []) {
if (DEEP && llmAvailable(PROVIDER) && findings.length) {
const blob = findings.map(f => `- [${f.note}] (rel ${f.relevance}) ${f.insight}${f.source ? ` <${f.source}>` : ''}`).join('\n')
const axes = facets.length ? `\nСгруппируй по этим осям темы (themes), в этом порядке: ${facets.map(f => f.label).join(', ')}. Добавь иную ось только если находки не лезут ни в одну.` : ''
const prompt = `Ты оркестратор. Собери из находок агентов справку по теме "${topic}" на РУССКОМ, стиль humanizer — живо, ясно, по делу, без воды и канцелярита. Дедуплицируй, сгруппируй по под-темам (themes), отметь связи (connections), противоречия (contradictions), пробелы знаний (gaps) и следующие шаги (next). Не выдумывай сверх находок.${axes}
Находки:
${blob}`
try { return await llmJson(prompt, SYNTH_SCHEMA, { provider: PROVIDER }) }
catch (e) { console.error(`[kb-search] synthesis failed: ${e.message}; local assembly`) }
}
// Local assembly: group by note, no LLM.
const byNote = new Map()
for (const f of findings) {
if (!byNote.has(f.note)) byNote.set(f.note, [])
byNote.get(f.note).push(f.insight)
}
const themes = [...byNote.entries()]
.sort((a, b) => b[1].length - a[1].length)
.slice(0, 12)
.map(([note, points]) => ({ title: note, points: [...new Set(points)] }))
return {
summary: `Найдено ${findings.length} совпадений в ${byNote.size} заметках по теме «${topic}» (локальный поиск без LLM).`,
themes, connections: [], contradictions: [], gaps: [], next: []
}
}
// --- report rendering --------------------------------------------------------
function slugify(s) { return String(s).toLowerCase().replace(/[^\p{L}\p{N}]+/gu, '-').replace(/^-+|-+$/g, '').slice(0, 60) || 'topic' }
function renderReport(topic, report, meta) {
const L = []
L.push(`# Справка: ${topic}`, '')
L.push(`> Поиск по базе знаний · ${meta.stamp} · просмотрено ${meta.scanned} заметок, отобрано ${meta.candidates}, находок ${meta.findings} · режим: ${meta.deep ? 'рой агентов (deep)' : 'локальный'}`, '')
if (meta.facets?.length) L.push(`> Оси разбора: ${meta.facets.join(' · ')}`, '')
if (meta.droppedNotes) L.push(`> ⚠️ Лимит стоимости: ${meta.droppedNotes} заметок-кандидатов не разобраны (--max-shards). Подними лимит для полного охвата.`, '')
if (meta.droppedByVerify) L.push(`> 🔎 Verify: ${meta.droppedByVerify} находок отброшено как необоснованные.`, '')
L.push('## Главное', '', report.summary || '—', '')
if (report.themes?.length) {
L.push('## По под-темам', '')
for (const t of report.themes) {
L.push(`### ${t.title}`)
for (const p of t.points || []) L.push(`- ${p}`)
L.push('')
}
}
const block = (title, arr) => { if (arr?.length) { L.push(`## ${title}`, ''); for (const x of arr) L.push(`- ${x}`); L.push('') } }
block('Связи', report.connections)
block('Противоречия', report.contradictions)
block('Пробелы', report.gaps)
block('Дальше', report.next)
return L.join('\n')
}
// --- main --------------------------------------------------------------------
async function run(topic, vault, outDir) {
const stamp = new Date().toISOString()
const { terms, facets } = await plan(topic)
// RECON
const files = await walk(vault)
const scored = []
for (const f of files) {
const text = await fs.readFile(f, 'utf8').catch(() => '')
if (!text) continue
const rel = path.relative(vault, f)
const score = scoreNote(text, rel, terms)
if (score > 0) scored.push({ rel, text, score, url: text.match(/https?:\/\/\S+/)?.[0] || '' })
}
scored.sort((a, b) => b.score - a.score)
const candidates = scored.slice(0, MAX_NOTES)
// FAN-OUT — shard within facet groups so each shard carries one focus axis
// (2A). No facets (local / planning failed) → flat sharding as before.
let shards = [] // each: { notes:[...], facet }
if (facets.length) {
const groups = new Map()
for (const c of candidates) {
const f = assignFacet(c, facets)
if (!groups.has(f)) groups.set(f, [])
groups.get(f).push(c)
}
for (const [facet, notes] of groups)
for (let i = 0; i < notes.length; i += SHARD_SIZE) shards.push({ notes: notes.slice(i, i + SHARD_SIZE), facet })
} else {
for (let i = 0; i < candidates.length; i += SHARD_SIZE) shards.push({ notes: candidates.slice(i, i + SHARD_SIZE), facet: null })
}
// Cost cap (deep only): fail-closed on the number of agents spawned. Local
// mode is 0-token, so it is uncapped. Dropped shards are logged, never silent.
let droppedNotes = 0
if (DEEP && llmAvailable(PROVIDER) && shards.length > MAX_SHARDS) {
droppedNotes = shards.slice(MAX_SHARDS).reduce((n, s) => n + s.notes.length, 0)
console.error(`[kb-search] cost cap: ${shards.length} shards > MNEMAZINE_SEARCH_MAX_SHARDS=${MAX_SHARDS}; dropping ${shards.length - MAX_SHARDS} shards (${droppedNotes} notes). Raise --max-shards to cover more.`)
shards = shards.slice(0, MAX_SHARDS)
}
const findings = []
await mapLimit(shards, DEEP ? CONCURRENCY : 1, async shard => {
try { findings.push(...await extractShard(topic, shard.notes, shard.facet)) }
catch (e) { console.error(`[kb-search] shard failed: ${e.message}`) }
})
findings.sort((a, b) => (b.relevance || 0) - (a.relevance || 0))
// VERIFY (2B, opt-in): drop ungrounded top findings before synthesis.
const textByNote = new Map(candidates.map(c => [c.rel, c.text]))
const { kept, dropped: droppedByVerify } = await verifyFindings(topic, findings, textByNote)
// FAN-IN
const report = await synthesize(topic, kept, facets)
const md = renderReport(topic, report, { stamp, scanned: files.length, candidates: candidates.length, findings: kept.length, deep: DEEP && llmAvailable(PROVIDER), droppedNotes, droppedByVerify, facets: facets.map(f => f.label) })
await fs.mkdir(outDir, { recursive: true })
const out = path.join(outDir, `search-${slugify(topic)}-${stamp.slice(0, 10)}-${Date.now()}.md`)
await fs.writeFile(out, md, 'utf8')
return out
}
async function selftest() {
const assert = (c, m) => { if (!c) throw new Error(`selftest: ${m}`) }
const tmp = await fs.mkdtemp(path.join((await import('node:os')).tmpdir(), 'kbsearch-'))
const vault = path.join(tmp, 'vault'); await fs.mkdir(vault, { recursive: true })
await fs.writeFile(path.join(vault, 'harness.md'), '# Agent harness\nСреда важнее модели. Контекст это бюджет. https://example.com/x', 'utf8')
await fs.writeFile(path.join(vault, 'coffee.md'), '# Кофе\nЭспрессо и молоко.', 'utf8')
const out = await run('agent harness контекст', vault, path.join(tmp, 'reports'))
const md = await fs.readFile(out, 'utf8')
assert(md.includes('Справка:'), 'report has header')
assert(md.includes('harness.md'), 'relevant note surfaced')
assert(!md.includes('coffee.md'), 'irrelevant note excluded')
assert(!md.includes('Лимит стоимости'), 'no cost-cap warning when nothing dropped')
const capped = renderReport('x', { summary: 's', themes: [] }, { stamp: 'now', scanned: 1, candidates: 1, findings: 1, deep: true, droppedNotes: 18 })
assert(capped.includes('Лимит стоимости') && capped.includes('18'), 'cost-cap warning surfaced when shards dropped')
await fs.rm(tmp, { recursive: true, force: true })
console.log('selftest ok')
}
if (SELFTEST) {
selftest().catch(e => { console.error(e.message); process.exit(1) })
} else {
const topic = arg('topic') || argv.find(a => !a.startsWith('--'))
if (!topic) { console.error('Usage: mnemazine-kb-search.mjs --topic "<тема>" [--deep] [--vault <p>]'); process.exit(1) }
const vault = resolveVault({ cli: arg('vault') })
const outDir = path.resolve(arg('out', process.env.MNEMAZINE_REPORTS || path.join(process.cwd(), 'reports')))
run(topic, vault, outDir)
.then(p => console.log(p))
.catch(e => { console.error(`[kb-search] ${e.message}`); process.exit(1) })
}