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>
This commit is contained in:
Зарубин Филипп
2026-06-19 18:52:30 +03:00
co-authored by Claude Opus 4.8
parent 48af0c5b4c
commit 103efd029a
5 changed files with 269 additions and 20 deletions
+29
View File
@@ -67,3 +67,32 @@ echo "Root: $ROOT"
echo "Inbox: $INBOX"
echo "Vault: $VAULT"
echo "Open the vault folder in Obsidian."
cat <<'EOF'
────────────────────────────────────────────────────────────
Привет! Это Philipp — автор Мнемазины.
Спасибо, что ставите мою разработку. Я делал её, чтобы сырьё
превращалось в проверенное, готовое к работе знание — и рад,
что теперь она будет помогать и вам.
★ Если зашло — поставьте звезду на GitHub, мне это правда важно:
https://github.com/zarubinphil/Mnemazine
💡 Есть идея, как сделать лучше? Откройте issue или PR —
предлагайте улучшения, я читаю каждое.
— — —
Hi! This is Philipp — the author of Mnemazine.
Thank you for installing my project. I built it to turn raw
input into verified, ready-to-use knowledge — glad it will now
help you too.
★ If you like it, please star it on GitHub — it truly helps:
https://github.com/zarubinphil/Mnemazine
💡 Got an idea to make it better? Open an issue or PR —
improvements are always welcome, I read every one.
────────────────────────────────────────────────────────────
EOF
+1
View File
@@ -17,6 +17,7 @@
"ingest:site": "node scripts/mnemazine-ingest-site.mjs",
"synthesize": "node scripts/mnemazine-synthesize.mjs",
"search": "node scripts/mnemazine-kb-search.mjs",
"search:eval": "node tests/search-eval.mjs",
"weekly": "node scripts/mnemazine-weekly-brief-html.mjs",
"weekly:state": "node scripts/mnemazine-weekly-state.mjs",
"digest": "node scripts/mnemazine-digest.mjs",
+112 -20
View File
@@ -33,6 +33,11 @@ const MAX_NOTES = Number(arg('max-notes', '60')) // candidate cap fed to the
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 = []) {
@@ -122,30 +127,76 @@ const SYNTH_SCHEMA = {
}
}
// --- orchestrator: widen the query (deep only) -------------------------------
async function widen(topic) {
if (!DEEP || !llmAvailable(PROVIDER)) return tokens(topic)
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}". Дай ключевые слова и синонимы (рус+англ) для полнотекстового поиска по заметкам — только список терминов, без объяснений.`,
{ type: 'object', additionalProperties: false, required: ['terms'], properties: { terms: { type: 'array', items: { type: 'string' } } } },
`Тема поиска по базе знаний: "${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)
return [...terms]
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 widening failed: ${e.message}; using raw terms`)
return tokens(topic)
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) {
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 prompt = `Ты агент-исследователь. Тема: "${topic}".
const focus = facet ? `\nФокус-ось этого шарда: «${facet}» — приоритет находкам по этой оси (но не пропускай иное важное по теме).` : ''
const prompt = `Ты агент-исследователь. Тема: "${topic}".${focus}
Из заметок ниже выбери ТОЛЬКО релевантное теме. Для каждой находки: note (путь заметки), source (URL/ссылка из заметки, если есть), insight (1-2 предложения по-русски — что важного для темы), relevance (1-5). Ничего не выдумывай сверх заметок.
${blob}`
@@ -164,11 +215,35 @@ ${blob}`
})
}
// --- 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) {
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 prompt = `Ты оркестратор. Собери из находок агентов справку по теме "${topic}" на РУССКОМ, стиль humanizer — живо, ясно, по делу, без воды и канцелярита. Дедуплицируй, сгруппируй по под-темам (themes), отметь связи (connections), противоречия (contradictions), пробелы знаний (gaps) и следующие шаги (next). Не выдумывай сверх находок.
const axes = facets.length ? `\nСгруппируй по этим осям темы (themes), в этом порядке: ${facets.map(f => f.label).join(', ')}. Добавь иную ось только если находки не лезут ни в одну.` : ''
const prompt = `Ты оркестратор. Собери из находок агентов справку по теме "${topic}" на РУССКОМ, стиль humanizer — живо, ясно, по делу, без воды и канцелярита. Дедуплицируй, сгруппируй по под-темам (themes), отметь связи (connections), противоречия (contradictions), пробелы знаний (gaps) и следующие шаги (next). Не выдумывай сверх находок.${axes}
Находки:
${blob}`
@@ -198,7 +273,9 @@ 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('## По под-темам', '')
@@ -219,7 +296,7 @@ function renderReport(topic, report, meta) {
// --- main --------------------------------------------------------------------
async function run(topic, vault, outDir) {
const stamp = new Date().toISOString()
const terms = await widen(topic)
const { terms, facets } = await plan(topic)
// RECON
const files = await walk(vault)
const scored = []
@@ -232,26 +309,41 @@ async function run(topic, vault, outDir) {
}
scored.sort((a, b) => b.score - a.score)
const candidates = scored.slice(0, MAX_NOTES)
// FAN-OUT
let shards = []
for (let i = 0; i < candidates.length; i += SHARD_SIZE) shards.push(candidates.slice(i, i + SHARD_SIZE))
// 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.length, 0)
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)) }
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, findings)
const md = renderReport(topic, report, { stamp, scanned: files.length, candidates: candidates.length, findings: findings.length, deep: DEEP && llmAvailable(PROVIDER), droppedNotes })
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')
+9
View File
@@ -56,6 +56,7 @@ async function checkSyntax() {
'scripts/mnemazine-semantic-shards.mjs',
'scripts/mnemazine-semantic-batches.mjs',
'scripts/mnemazine-synthesize.mjs',
'scripts/mnemazine-kb-search.mjs',
'scripts/mnemazine-llm.mjs',
'scripts/mnemazine-codex.mjs',
'scripts/mnemazine-verify.mjs',
@@ -174,6 +175,13 @@ async function reportQualityGateSmoke() {
await must('report quality gate smoke:good', process.execPath, ['scripts/mnemazine-report-quality-gate.mjs', '--report', good])
}
async function searchEvalSmoke() {
// Tier A only (0 tokens): recall/anti-noise of the KB search skill. Tier B
// (LLM judge) is opt-in via `npm run search:eval -- --deep`, not in the gate.
await must('kb-search selftest', process.execPath, ['scripts/mnemazine-kb-search.mjs', '--selftest'])
await must('kb-search eval (Tier A)', process.execPath, ['tests/search-eval.mjs'])
}
async function repoMetadataCheck() {
const pkg = await readJson(path.join(ROOT, 'package.json'))
if (!pkg.description || !/[А-Яа-яЁё]/.test(pkg.description) || !/[A-Za-z]/.test(pkg.description)) {
@@ -211,6 +219,7 @@ async function main() {
['syntax', checkSyntax],
['demo-smoke', demoSmoke],
['quality-public', qualityAndPublicChecks],
['search-eval', searchEvalSmoke],
['repo-metadata', repoMetadataCheck]
]
const passed = []
+118
View File
@@ -0,0 +1,118 @@
#!/usr/bin/env node
// Eval harness for the KB search skill (§5: a skill without evals is not a
// skill). Two tiers, split by token cost:
// Tier A — deterministic, 0 tokens (runs in release-check, every time):
// representative queries over a synthetic fixture vault; assert
// recall (mustHit) and anti-noise (mustMiss) on the local path.
// Tier B — LLM judge, opt-in (`--deep`, pre-merge only): a separate evaluator
// agent scores the deep "Справка" for groundedness/relevance/coverage
// (generator != evaluator, §4). Bounded: tiny fixture, few queries.
// Fixtures are generated inline — reproducible, public-safe, no live-vault PII.
// node tests/search-eval.mjs # Tier A only (free)
// node tests/search-eval.mjs --deep # + Tier B judge (spends tokens)
import { promises as fs } from 'node:fs'
import os from 'node:os'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import { spawn } from 'node:child_process'
const HERE = path.dirname(fileURLToPath(import.meta.url))
const SCRIPT = path.join(HERE, '..', 'scripts', 'mnemazine-kb-search.mjs')
const DEEP = process.argv.includes('--deep')
// Synthetic corpus: 3 on-topic clusters + 2 noise notes.
const FIXTURES = {
'harness-vs-model.md': '# Harness важнее модели\nСреда и workflow дают до 90% выигрыша. Контекст это бюджет, не свалка. Чинить harness, не менять модель.',
'context-budget.md': '# Контекст как бюджет\n73% токенов уходят до промпта. CLAUDE.md короткий роутер до 200 строк. Субагенты изолируют контекст.',
'swarm-fan-out.md': '# Рой и fan-out\nOrchestrator-worker, recon до fan-out, read параллельно write изолировать. Рой ~15x токенов, оправдан при независимости.',
'autonomy-loops.md': '# Автономные петли\nCircuit breaker и cost cap, keep-or-revert, generator не равно evaluator, status-файлы.',
'coffee.md': '# Кофе\nЭспрессо, молоко, ростер, обжарка зерна.',
'family.md': '# Семья\nДочки, выходные, прогулки в парке, личное время.'
}
const CASES = [
{ topic: 'harness важнее модели', mustHit: ['harness-vs-model.md'], mustMiss: ['coffee.md', 'family.md'] },
{ topic: 'контекст токены бюджет', mustHit: ['context-budget.md'], mustMiss: ['coffee.md'] },
{ topic: 'рой агентов fan-out параллелизм', mustHit: ['swarm-fan-out.md'], mustMiss: ['family.md'] },
{ topic: 'автономные петли circuit breaker', mustHit: ['autonomy-loops.md'], mustMiss: ['coffee.md', 'family.md'] }
]
function runSearch(topic, vault, outDir, deep) {
return new Promise((resolve, reject) => {
const env = { ...process.env, MNEMAZINE_DEEP: deep ? '1' : '' }
const child = spawn('node', [SCRIPT, '--topic', topic, '--vault', vault, '--out', outDir], { env, stdio: ['ignore', 'pipe', 'inherit'] })
let out = ''
child.stdout.on('data', d => { out += d })
child.on('error', reject)
child.on('close', code => code === 0 ? resolve(out.trim()) : reject(new Error(`search exited ${code}`)))
})
}
async function main() {
const tmp = await fs.mkdtemp(path.join(os.tmpdir(), 'kbsearch-eval-'))
const vault = path.join(tmp, 'vault'), outDir = path.join(tmp, 'reports')
await fs.mkdir(vault, { recursive: true })
for (const [name, body] of Object.entries(FIXTURES)) await fs.writeFile(path.join(vault, name), body, 'utf8')
let pass = 0, fail = 0
const failures = []
// --- Tier A: deterministic recall/precision (0 tokens) ---
for (const c of CASES) {
const reportPath = await runSearch(c.topic, vault, outDir, false)
const md = await fs.readFile(reportPath, 'utf8')
for (const hit of c.mustHit) {
if (md.includes(hit)) pass++
else { fail++; failures.push(`[A] "${c.topic}" MISSED ${hit}`) }
}
for (const miss of c.mustMiss) {
if (!md.includes(miss)) pass++
else { fail++; failures.push(`[A] "${c.topic}" LEAKED noise ${miss}`) }
}
}
console.log(`Tier A (0-token): ${pass} pass, ${fail} fail`)
// --- Tier B: LLM judge on the deep report (opt-in) ---
if (DEEP) {
const { llmAvailable, llmJson, fenceUntrusted } = await import('../scripts/mnemazine-llm.mjs')
if (!llmAvailable()) {
console.log('Tier B skipped: no LLM available')
} else {
const corpus = Object.entries(FIXTURES).map(([n, b]) => `### ${n}\n${b}`).join('\n\n')
const RUBRIC = {
type: 'object', additionalProperties: false,
required: ['groundedness', 'relevance', 'coverage'],
properties: {
groundedness: { type: 'integer', minimum: 1, maximum: 5 },
relevance: { type: 'integer', minimum: 1, maximum: 5 },
coverage: { type: 'integer', minimum: 1, maximum: 5 },
notes: { type: 'string' }
}
}
const THRESHOLD = 3
for (const c of CASES.slice(0, 2)) { // bound token cost: 2 deep queries
const reportPath = await runSearch(c.topic, vault, outDir, true)
const md = await fs.readFile(reportPath, 'utf8')
const judge = await llmJson(
`Ты строгий судья качества справки по теме "${c.topic}". Оцени 1-5: groundedness (всё опирается на корпус, ничего не выдумано), relevance (по теме), coverage (покрыты ключевые моменты корпуса). Верни числа + notes.
КОРПУС:
${fenceUntrusted('CORPUS', corpus)}
СПРАВКА:
${fenceUntrusted('REPORT', md)}`,
RUBRIC, {}
)
const min = Math.min(judge.groundedness, judge.relevance, judge.coverage)
if (min >= THRESHOLD) { pass++; console.log(`Tier B "${c.topic}": g${judge.groundedness}/r${judge.relevance}/c${judge.coverage} ✓`) }
else { fail++; failures.push(`[B] "${c.topic}" below threshold: g${judge.groundedness}/r${judge.relevance}/c${judge.coverage} — ${judge.notes || ''}`) }
}
}
}
await fs.rm(tmp, { recursive: true, force: true })
if (failures.length) { console.error('\nFAILURES:\n' + failures.join('\n')); process.exit(1) }
console.log(`\neval ok — ${pass} checks passed`)
}
main().catch(e => { console.error(e.message); process.exit(1) })