feat(synthesize): B knowledge enrichment stage before atomize (--deep)
Web-capable LLM agent researches and EXPANDS captured material (kb-enrich philosophy: recon, primary sources, practitioner experience, anti-hallucination fact->URL) before atomization, so atoms are built from grown knowledge not just the raw capture. On within --deep; disable via --no-enrich / MNEMAZINE_ENRICH=0. Reports enriched count; degrades to raw atomize on failure. Stub-tested enrich->atom chain. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.8
parent
bc60800660
commit
4cae8f5fb2
@@ -23,6 +23,8 @@ const MIN_CLUSTER_CHARS = Number(arg('min-cluster-chars', '80'))
|
|||||||
// and never needs codex. Flag also honoured via MNEMAZINE_DEEP=1.
|
// and never needs codex. Flag also honoured via MNEMAZINE_DEEP=1.
|
||||||
const DEEP = argv.includes('--deep') || argv.includes('--atomize') || process.env.MNEMAZINE_DEEP === '1'
|
const DEEP = argv.includes('--deep') || argv.includes('--atomize') || process.env.MNEMAZINE_DEEP === '1'
|
||||||
const MAX_ATOMS = Number(arg('max-atoms', process.env.MNEMAZINE_MAX_ATOMS || '20'))
|
const MAX_ATOMS = Number(arg('max-atoms', process.env.MNEMAZINE_MAX_ATOMS || '20'))
|
||||||
|
// Enrichment is on within --deep unless explicitly disabled (it needs the network).
|
||||||
|
const ENRICH = DEEP && process.env.MNEMAZINE_ENRICH !== '0' && !argv.includes('--no-enrich')
|
||||||
|
|
||||||
const sourceHints = [
|
const sourceHints = [
|
||||||
{ re: /mcp|model context protocol|filesystem mcp|memory mcp|zapier/i, name: 'Model Context Protocol docs', url: 'https://modelcontextprotocol.io/docs/getting-started/intro' },
|
{ re: /mcp|model context protocol|filesystem mcp|memory mcp|zapier/i, name: 'Model Context Protocol docs', url: 'https://modelcontextprotocol.io/docs/getting-started/intro' },
|
||||||
@@ -338,8 +340,11 @@ const ATOM_SCHEMA = {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function atomPrompt(cluster, sources) {
|
function atomPrompt(cluster, sources, materialOverride) {
|
||||||
const text = cluster.records.map(r => compact(r.text, 4000)).join('\n---\n').slice(0, 24000)
|
// When enrichment ran, atomize the EXPANDED knowledge; else the raw capture.
|
||||||
|
const text = materialOverride
|
||||||
|
? String(materialOverride).slice(0, 28000)
|
||||||
|
: cluster.records.map(r => compact(r.text, 4000)).join('\n---\n').slice(0, 24000)
|
||||||
const urls = sources.map(s => s.url).join(', ') || 'none detected'
|
const urls = sources.map(s => s.url).join(', ') || 'none detected'
|
||||||
return `You are Mnemazine's atomization agent. Split the raw material below into FOCUSED, atomic knowledge notes — one idea per atom, up to ${MAX_ATOMS}. Do NOT merge unrelated ideas; do NOT invent facts not present in the material. Each atom: a precise title, a one-paragraph "what", a one-paragraph "why it matters", 2-5 concrete "how to use" bullets, one "next action", and the subset of source URLs that support it (from: ${urls}; [] if none).
|
return `You are Mnemazine's atomization agent. Split the raw material below into FOCUSED, atomic knowledge notes — one idea per atom, up to ${MAX_ATOMS}. Do NOT merge unrelated ideas; do NOT invent facts not present in the material. Each atom: a precise title, a one-paragraph "what", a one-paragraph "why it matters", 2-5 concrete "how to use" bullets, one "next action", and the subset of source URLs that support it (from: ${urls}; [] if none).
|
||||||
|
|
||||||
@@ -348,12 +353,49 @@ Return ONLY JSON matching the schema.
|
|||||||
${fenceUntrusted('MATERIAL', text)}`
|
${fenceUntrusted('MATERIAL', text)}`
|
||||||
}
|
}
|
||||||
|
|
||||||
async function atomizeCluster(cluster, sources) {
|
async function atomizeCluster(cluster, sources, materialOverride) {
|
||||||
const result = await llmJson(atomPrompt(cluster, sources), ATOM_SCHEMA)
|
const result = await llmJson(atomPrompt(cluster, sources, materialOverride), ATOM_SCHEMA)
|
||||||
const atoms = Array.isArray(result?.atoms) ? result.atoms : []
|
const atoms = Array.isArray(result?.atoms) ? result.atoms : []
|
||||||
return atoms.filter(a => a && a.title && a.what).slice(0, MAX_ATOMS)
|
return atoms.filter(a => a && a.title && a.what).slice(0, MAX_ATOMS)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// --- Enrichment (knowledge EXPANSION, README "research", G/B) ---
|
||||||
|
// A web-capable LLM agent researches the captured material and grows it "as much
|
||||||
|
// as truly needed": pulls primary sources, current facts/versions, practitioner
|
||||||
|
// experience — each added fact tied to a fetched URL (anti-hallucination). Output
|
||||||
|
// feeds atomize, so atoms are built from EXPANDED knowledge, not just the capture.
|
||||||
|
const ENRICH_SCHEMA = {
|
||||||
|
type: 'object',
|
||||||
|
additionalProperties: false,
|
||||||
|
required: ['enriched', 'sources'],
|
||||||
|
properties: {
|
||||||
|
enriched: { type: 'string' },
|
||||||
|
sources: { type: 'array', items: { type: 'string' } },
|
||||||
|
added_facts: { type: 'array', items: { type: 'string' } }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function enrichPrompt(text, sources) {
|
||||||
|
const urls = sources.map(s => s.url).join(', ') || 'none detected'
|
||||||
|
return `You are Mnemazine's research-enrich agent. The MATERIAL below is a SEED, not the final knowledge. Research it with available tools (web search, web fetch, and any configured MCP web tools) and EXPAND it as much as is genuinely useful — no padding. Pull: the primary source, current facts/numbers/versions, concrete examples, and real practitioner experience (issues, pros/cons, gotchas) with thread/issue URLs. Anti-hallucination: every added fact MUST trace to a fetched URL; if unconfirmed, say so, do not strengthen it. Keep it tight and factual.
|
||||||
|
|
||||||
|
Known source hints: ${urls}.
|
||||||
|
|
||||||
|
Produce: "enriched" = the expanded knowledge as clean prose (English ok; the human-readable Russian digest is a later stage), "sources" = all source URLs used, "added_facts" = short bullet list of what you added beyond the seed.
|
||||||
|
|
||||||
|
${fenceUntrusted('MATERIAL', text)}`
|
||||||
|
}
|
||||||
|
|
||||||
|
async function enrichCluster(cluster, sources) {
|
||||||
|
const text = cluster.records.map(r => compact(r.text, 6000)).join('\n---\n').slice(0, 24000)
|
||||||
|
const res = await llmJson(enrichPrompt(text, sources), ENRICH_SCHEMA, {
|
||||||
|
tools: ['WebSearch', 'WebFetch', 'mcp__firecrawl', 'mcp__tavily']
|
||||||
|
})
|
||||||
|
const enriched = typeof res?.enriched === 'string' ? res.enriched.trim() : ''
|
||||||
|
const addedSources = Array.isArray(res?.sources) ? res.sources.filter(Boolean) : []
|
||||||
|
return { enriched, addedSources }
|
||||||
|
}
|
||||||
|
|
||||||
function atomFingerprint(atom, clusterId = '') {
|
function atomFingerprint(atom, clusterId = '') {
|
||||||
// clusterId scopes the hash so identical titles in different clusters (common
|
// clusterId scopes the hash so identical titles in different clusters (common
|
||||||
// for sourceless low-confidence atoms) get distinct filenames, not silent skips.
|
// for sourceless low-confidence atoms) get distinct filenames, not silent skips.
|
||||||
@@ -432,6 +474,7 @@ for (const record of records) {
|
|||||||
let written = 0
|
let written = 0
|
||||||
let skipped = 0
|
let skipped = 0
|
||||||
let atomized = 0
|
let atomized = 0
|
||||||
|
let enriched_clusters = 0
|
||||||
const useAtomize = DEEP && llmAvailable()
|
const useAtomize = DEEP && llmAvailable()
|
||||||
if (DEEP && !llmAvailable()) {
|
if (DEEP && !llmAvailable()) {
|
||||||
console.error('[synthesize] --deep requested but LLM unavailable; falling back to local template synthesis')
|
console.error('[synthesize] --deep requested but LLM unavailable; falling back to local template synthesis')
|
||||||
@@ -446,7 +489,21 @@ for (const cluster of clusters.values()) {
|
|||||||
if (useAtomize) {
|
if (useAtomize) {
|
||||||
try {
|
try {
|
||||||
const sources = publicSources(part.records.map(r => r.text).join('\n\n'))
|
const sources = publicSources(part.records.map(r => r.text).join('\n\n'))
|
||||||
const atoms = await atomizeCluster(part, sources)
|
// Expand the knowledge first (research), then atomize the EXPANDED material.
|
||||||
|
let material
|
||||||
|
if (ENRICH) {
|
||||||
|
try {
|
||||||
|
const { enriched, addedSources } = await enrichCluster(part, sources)
|
||||||
|
if (enriched && enriched.length > 200) {
|
||||||
|
material = enriched
|
||||||
|
for (const u of addedSources) if (!sources.some(s => s.url === u)) sources.push({ name: hostOf(u) || 'Source', url: u })
|
||||||
|
enriched_clusters += 1
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
console.error(`[synthesize] enrich failed for cluster ${cluster.id}: ${err.message}; atomizing raw capture`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const atoms = await atomizeCluster(part, sources, material)
|
||||||
let wroteAtom = false
|
let wroteAtom = false
|
||||||
for (const atom of atoms) {
|
for (const atom of atoms) {
|
||||||
const out = path.join(VAULT, '01 Concepts', `synthesis-${slugify(atom.title)}-${atomFingerprint(atom, part.id)}.md`)
|
const out = path.join(VAULT, '01 Concepts', `synthesis-${slugify(atom.title)}-${atomFingerprint(atom, part.id)}.md`)
|
||||||
@@ -484,4 +541,4 @@ for (const cluster of clusters.values()) {
|
|||||||
// degraded: --deep was requested but the deep path could not run (codex absent),
|
// degraded: --deep was requested but the deep path could not run (codex absent),
|
||||||
// so the run silently fell back to local templates. Callers can detect this.
|
// so the run silently fell back to local templates. Callers can detect this.
|
||||||
const degraded = DEEP && !llmAvailable()
|
const degraded = DEEP && !llmAvailable()
|
||||||
console.log(JSON.stringify({ ok: true, degraded, records: records.length, clusters: clusters.size, written, atomized, skipped }, null, 2))
|
console.log(JSON.stringify({ ok: true, degraded, records: records.length, clusters: clusters.size, written, atomized, enriched: enriched_clusters, skipped }, null, 2))
|
||||||
|
|||||||
Reference in New Issue
Block a user