From 48af0c5b4c4468e9a068a3ceb3008041515f7ce9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=97=D0=B0=D1=80=D1=83=D0=B1=D0=B8=D0=BD=20=D0=A4=D0=B8?= =?UTF-8?q?=D0=BB=D0=B8=D0=BF=D0=BF?= <289370046+zarubinphil@users.noreply.github.com> Date: Fri, 19 Jun 2026 18:40:38 +0300 Subject: [PATCH] feat(search): fail-closed cost cap on swarm size MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Hard-limit the number of LLM agents a deep search spawns via MNEMAZINE_SEARCH_MAX_SHARDS / --max-shards (default 12). Local mode stays uncapped (0-token). Dropped shards are logged to stderr AND surfaced in the report header — no silent truncation. Closes the missing circuit-breaker called out by the agent-systems playbook. Co-Authored-By: Claude Opus 4.8 (1M context) --- scripts/mnemazine-kb-search.mjs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/scripts/mnemazine-kb-search.mjs b/scripts/mnemazine-kb-search.mjs index 01f903c..bc86109 100644 --- a/scripts/mnemazine-kb-search.mjs +++ b/scripts/mnemazine-kb-search.mjs @@ -31,6 +31,7 @@ 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 // --- helpers ----------------------------------------------------------------- @@ -197,6 +198,7 @@ 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.droppedNotes) L.push(`> ⚠️ Лимит стоимости: ${meta.droppedNotes} заметок-кандидатов не разобраны (--max-shards). Подними лимит для полного охвата.`, '') L.push('## Главное', '', report.summary || '—', '') if (report.themes?.length) { L.push('## По под-темам', '') @@ -231,8 +233,16 @@ async function run(topic, vault, outDir) { scored.sort((a, b) => b.score - a.score) const candidates = scored.slice(0, MAX_NOTES) // FAN-OUT - const shards = [] + let shards = [] for (let i = 0; i < candidates.length; i += SHARD_SIZE) shards.push(candidates.slice(i, i + SHARD_SIZE)) + // 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) + 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)) } @@ -241,7 +251,7 @@ async function run(topic, vault, outDir) { findings.sort((a, b) => (b.relevance || 0) - (a.relevance || 0)) // 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) }) + const md = renderReport(topic, report, { stamp, scanned: files.length, candidates: candidates.length, findings: findings.length, deep: DEEP && llmAvailable(PROVIDER), droppedNotes }) 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') @@ -259,6 +269,9 @@ async function selftest() { 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') }