Files
mnemazine/scripts/mnemazine-run.mjs
T

679 lines
32 KiB
JavaScript
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env node
import { promises as fs } from 'node:fs'
import { existsSync } from 'node:fs'
import path from 'node:path'
import crypto from 'node:crypto'
import { spawnSync } from 'node:child_process'
import { llmAvailable, llmText } from './mnemazine-llm.mjs'
import { resolveVault } from './mnemazine-paths.mjs'
const ROOT = process.env.MNEMAZINE_ROOT || path.resolve(process.cwd())
const INBOX = process.env.MNEMAZINE_INBOX || path.join(ROOT, 'inbox')
const VAULT = resolveVault()
const REPORTS = process.env.MNEMAZINE_REPORTS || path.join(ROOT, 'reports')
const STATE = process.env.MNEMAZINE_STATE || path.join(ROOT, '.mnemazine/state')
const CACHE = process.env.MNEMAZINE_CACHE || path.join(ROOT, '.mnemazine/cache/processed-hashes.json')
const ARCHIVE = process.env.MNEMAZINE_ARCHIVE || path.join(ROOT, '.mnemazine/archive')
const TRANSCRIPTS = path.join(ROOT, '.mnemazine/cache/video-transcripts')
const VIDEO_AUDIO = path.join(ROOT, '.mnemazine/cache/video-audio')
const VIDEO_FRAMES = path.join(ROOT, '.mnemazine/cache/video-frames')
const VIDEO_QUEUE = path.join(ROOT, '.mnemazine/cache/video-queue.jsonl')
const EXTRACTS = process.env.MNEMAZINE_EXTRACTS || path.join(ROOT, '.mnemazine/cache/extracted')
const SYNTHESIZE = process.env.MNEMAZINE_SYNTHESIZE !== '0'
const FINISH = process.env.MNEMAZINE_FINISH !== '0'
// Opt-in deep stage (atomization + web/LLM verification, README:230 pipeline).
// Default OFF: the runner stays conservative — local only, no external calls.
// Enable with `--deep` or MNEMAZINE_DEEP=1; forwarded to synthesize.
const DEEP = process.argv.includes('--deep') || process.env.MNEMAZINE_DEEP === '1'
const REQUIRE_DEEP = process.argv.includes('--require-deep') || process.env.MNEMAZINE_REQUIRE_DEEP === '1'
const ENRICH_REQUIRED = DEEP && process.env.MNEMAZINE_ENRICH !== '0' && !process.argv.includes('--no-enrich')
const STRICT_ARCHIVE_KNOWLEDGE = (REQUIRE_DEEP || (DEEP && ENRICH_REQUIRED)) && process.env.MNEMAZINE_STRICT_ARCHIVE !== '0' && !process.argv.includes('--allow-raw-archive')
const WHISPER_MODEL = process.env.MNEMAZINE_WHISPER_MODEL || ''
const WHISPER_LANGUAGE = process.env.MNEMAZINE_WHISPER_LANGUAGE || 'ru'
const VIDEO_FRAME_LIMIT = Number(process.env.MNEMAZINE_VIDEO_FRAME_LIMIT || '8')
const VIDEO_INLINE_MAX_SECONDS = Number(process.env.MNEMAZINE_VIDEO_INLINE_MAX_SECONDS || '180')
const COMMAND_TIMEOUT_MS = Number(process.env.MNEMAZINE_COMMAND_TIMEOUT_MS || '120000')
const PROGRESS_EVERY = Number(process.env.MNEMAZINE_PROGRESS_EVERY || '25')
async function ensureDir(dir) {
await fs.mkdir(dir, { recursive: true })
}
async function sha256(file) {
const hash = crypto.createHash('sha256')
hash.update(await fs.readFile(file))
return hash.digest('hex')
}
async function readJson(file, fallback) {
try { return JSON.parse(await fs.readFile(file, 'utf8')) } catch { return fallback }
}
function slugify(value) {
return String(value || 'note')
.toLowerCase()
.replace(/[^a-zа-я0-9]+/gi, '-')
.replace(/^-+|-+$/g, '')
.slice(0, 80) || 'note'
}
function compact(value, limit = 1400) {
return String(value || '')
.replace(/^#{1,6}\s+/gm, '')
.replace(/\s+/g, ' ')
.trim()
.slice(0, limit)
}
function sourceRef(hash) {
return `local-media:${String(hash).slice(0, 16)}`
}
function inferTitle(text, fallback = 'Local source') {
const clean = compact(text, 500)
const url = clean.match(/\bhttps?:\/\/[^\s)]+/)?.[0]
if (url) return url.replace(/^https?:\/\//, '').replace(/[?#].*$/, '').slice(0, 90)
const line = String(text || '')
.split(/\n|[.!?]\s+/)
.map(s => compact(s, 120))
.find(s => s.length >= 18 && s.length <= 120 && !/^(IMG_|temp_image|screenshot|screen shot)/i.test(s))
return line || fallback
}
function bullets(text, max = 7) {
const out = []
const seen = new Set()
for (const part of String(text || '').split(/\n|[•*-]\s+/)) {
const line = compact(part, 180)
if (line.length < 24) continue
if (/^(IMG_|temp_image|screenshot|screen shot|save this|follow)/i.test(line)) continue
const key = line.toLowerCase()
if (seen.has(key)) continue
seen.add(key)
out.push(line)
if (out.length >= max) break
}
return out
}
function hasUsableExtraction(text) {
const clean = compact(text, 2000)
if (clean.length < 80) return false
if (/^(Video queued for local Whisper transcription|No extractable text)/i.test(clean)) return false
const alpha = (clean.match(/[A-Za-zА-Яа-яЁё]/g) || []).length
return alpha >= 50
}
function isVideo(file) {
return ['.mp4', '.mov', '.m4v', '.webm', '.mkv'].includes(path.extname(file).toLowerCase())
}
function isImage(file) {
return ['.png', '.jpg', '.jpeg', '.heic', '.webp', '.tiff'].includes(path.extname(file).toLowerCase())
}
function isMarkitdownDocument(file) {
return ['.pdf', '.docx', '.pptx', '.xlsx', '.html', '.htm'].includes(path.extname(file).toLowerCase())
}
function videoDurationSeconds(file) {
const probe = spawnSync('ffmpeg', ['-i', file], { encoding: 'utf8', timeout: COMMAND_TIMEOUT_MS })
const match = `${probe.stderr}\n${probe.stdout}`.match(/Duration:\s*(\d+):(\d+):(\d+(?:\.\d+)?)/)
if (!match) return null
return Number(match[1]) * 3600 + Number(match[2]) * 60 + Number(match[3])
}
function whisperModelFor(durationSeconds) {
if (WHISPER_MODEL) return WHISPER_MODEL
if (durationSeconds !== null && durationSeconds <= 90) return 'base'
return 'tiny'
}
async function appendVideoQueue(item) {
await ensureDir(path.dirname(VIDEO_QUEUE))
await fs.appendFile(VIDEO_QUEUE, `${JSON.stringify(item)}\n`, 'utf8')
}
async function extractVideo(file, hash) {
await ensureDir(TRANSCRIPTS)
await ensureDir(VIDEO_AUDIO)
await ensureDir(VIDEO_FRAMES)
const durationSeconds = videoDurationSeconds(file)
const transcriptPath = path.join(TRANSCRIPTS, `${hash}.txt`)
if (existsSync(transcriptPath)) {
const transcript = await fs.readFile(transcriptPath, 'utf8')
const frames = await extractVideoFrames(file, hash)
return joinVideoParts(transcript, frames)
}
if (durationSeconds !== null && durationSeconds > VIDEO_INLINE_MAX_SECONDS) {
const frames = await extractVideoFrames(file, hash)
await appendVideoQueue({
hash,
source_ref: sourceRef(hash),
file,
duration_seconds: Math.round(durationSeconds),
status: 'deferred_transcription',
reason: `duration exceeds inline limit ${VIDEO_INLINE_MAX_SECONDS}s`,
suggested_command: `MNEMAZINE_WHISPER_MODEL=small MNEMAZINE_VIDEO_INLINE_MAX_SECONDS=999999 node scripts/mnemazine-run.mjs`
})
return joinVideoParts('', frames) || `Video queued for local Whisper transcription.\n\nDuration: ${Math.round(durationSeconds)} seconds.\nInline limit: ${VIDEO_INLINE_MAX_SECONDS} seconds.\nSource: ${sourceRef(hash)}`
}
const audioPath = path.join(VIDEO_AUDIO, `${hash}.wav`)
const ffmpeg = spawnSync('ffmpeg', [
'-y',
'-i', file,
'-vn',
'-ac', '1',
'-ar', '16000',
'-f', 'wav',
audioPath
], { encoding: 'utf8', timeout: COMMAND_TIMEOUT_MS })
if (ffmpeg.status !== 0 || !existsSync(audioPath)) return ''
const whisper = spawnSync('whisper', [
audioPath,
'--model', whisperModelFor(durationSeconds),
'--language', WHISPER_LANGUAGE,
'--output_dir', TRANSCRIPTS,
'--output_format', 'txt',
'--fp16', 'False',
'--verbose', 'False'
], { encoding: 'utf8', timeout: COMMAND_TIMEOUT_MS })
const whisperOut = path.join(TRANSCRIPTS, `${hash}.txt`)
let transcript = ''
if (whisper.status === 0 && existsSync(whisperOut)) {
transcript = await fs.readFile(whisperOut, 'utf8')
}
const frames = await extractVideoFrames(file, hash)
return joinVideoParts(transcript, frames)
}
async function extractVideoFrames(file, hash) {
const ocr = path.join(ROOT, '.mnemazine/bin/vision-ocr')
if (!existsSync(ocr)) return ''
const frameDir = path.join(VIDEO_FRAMES, hash)
await ensureDir(frameDir)
const existing = (await fs.readdir(frameDir).catch(() => [])).filter(name => name.endsWith('.png'))
if (!existing.length) {
spawnSync('ffmpeg', [
'-y',
'-i', file,
'-vf', 'fps=1/3',
'-frames:v', String(VIDEO_FRAME_LIMIT),
path.join(frameDir, 'frame-%03d.png')
], { encoding: 'utf8', timeout: COMMAND_TIMEOUT_MS })
}
const chunks = []
const seen = new Set()
const frames = (await fs.readdir(frameDir).catch(() => []))
.filter(name => name.endsWith('.png'))
.sort()
.slice(0, VIDEO_FRAME_LIMIT)
for (const frame of frames) {
const out = spawnSync(ocr, [path.join(frameDir, frame)], { encoding: 'utf8', timeout: COMMAND_TIMEOUT_MS })
if (out.status !== 0) continue
for (const line of out.stdout.split(/\r?\n/)) {
const clean = compact(line, 220)
if (clean.length < 12) continue
const key = clean.toLowerCase()
if (seen.has(key)) continue
seen.add(key)
chunks.push(clean)
}
}
return chunks.join('\n')
}
function joinVideoParts(transcript, frameText) {
const parts = []
if (compact(transcript, 80)) parts.push(`Video transcript from local Whisper:\n\n${transcript.trim()}`)
if (compact(frameText, 80)) parts.push(`Video keyframe OCR:\n\n${frameText.trim()}`)
return parts.join('\n\n')
}
async function extract(file) {
const ext = path.extname(file).toLowerCase()
if (['.md', '.txt', '.json', '.csv'].includes(ext)) return await fs.readFile(file, 'utf8')
if (isVideo(file)) return await extractVideo(file, await sha256(file))
const ocr = path.join(ROOT, '.mnemazine/bin/vision-ocr')
if (existsSync(ocr) && isImage(file)) {
const out = spawnSync(ocr, [file], { encoding: 'utf8', timeout: COMMAND_TIMEOUT_MS })
if (out.status === 0) return out.stdout
}
if (isMarkitdownDocument(file)) {
const markitdown = spawnSync('markitdown', [file], { encoding: 'utf8', timeout: COMMAND_TIMEOUT_MS })
if (markitdown.status === 0 && markitdown.stdout.trim()) return markitdown.stdout
}
return ''
}
// LLM recognition fallback (deep only). Used ONLY when local engines (Apple
// Vision OCR / markitdown / whisper) produced nothing usable — keeps the default
// path at 0 tokens. A vision-capable agent reads the file and transcribes it.
async function llmExtract(file) {
const ext = path.extname(file).toLowerCase()
const kind = isImage(file) ? 'image' : isVideo(file) ? 'video frame still' : 'document'
const prompt = `Read the local ${kind} file and output ALL of its information as plain text: transcribe every readable word verbatim, then add a short factual description of any non-text content (diagrams, charts, UI). No commentary, no preamble.\n\nFILE: ${file}`
// Claude reads via the Read tool; Codex reads files in its working dir.
const text = await llmText(prompt, {
tools: ['Read'],
cwd: path.dirname(file),
timeoutMs: COMMAND_TIMEOUT_MS
})
return text || ''
}
async function writeExtractCache(source, hash, text, status) {
await ensureDir(EXTRACTS)
const ext = path.extname(source).toLowerCase().replace('.', '') || 'file'
const ref = sourceRef(hash)
const record = {
source_ref: ref,
source_hash: hash,
source_type: ext,
status,
extracted_at: new Date().toISOString(),
text_path: compact(text, 80) ? `${hash}.txt` : null
}
if (record.text_path) await fs.writeFile(path.join(EXTRACTS, record.text_path), text, 'utf8')
await fs.writeFile(path.join(EXTRACTS, `${hash}.json`), JSON.stringify(record, null, 2), 'utf8')
return record
}
async function archiveFile(file, hash) {
const month = new Date().toISOString().slice(0, 7)
const dir = path.join(ARCHIVE, month)
await ensureDir(dir)
const ext = path.extname(file)
let target = path.join(dir, `${hash}${ext}`)
let suffix = 1
while (existsSync(target)) {
target = path.join(dir, `${hash}-${suffix}${ext}`)
suffix += 1
}
await fs.rename(file, target)
return target
}
let vaultMarkdownFiles = null
async function listVaultMarkdownFiles() {
if (vaultMarkdownFiles) return vaultMarkdownFiles
const out = []
async function walk(dir) {
for (const item of await fs.readdir(dir, { withFileTypes: true }).catch(() => [])) {
const p = path.join(dir, item.name)
if (item.isDirectory()) {
if (['.git', '.obsidian'].includes(item.name) || item.name.startsWith('graphify-out')) continue
await walk(p)
} else if (item.isFile() && p.endsWith('.md')) out.push(p)
}
}
await walk(VAULT)
vaultMarkdownFiles = out
return out
}
function noteSectionAfter(text, label) {
const start = String(text || '').indexOf(label)
if (start === -1) return ''
const tail = text.slice(start + label.length)
const next = tail.search(/\n##\s+/)
return next === -1 ? tail : tail.slice(0, next)
}
function strictKnowledgeReady(text) {
const facts = noteSectionAfter(text, 'External facts added before atomization:')
const sources = noteSectionAfter(text, 'Public/source expansion:')
const factCount = facts
.split('\n')
.filter(line => /^\s*-\s+\S/.test(line) && !/No external enrichment facts recorded/i.test(line))
.length
return /^status:\s*"final"\s*$/m.test(text) &&
/^verified:\s*true\s*$/m.test(text) &&
/^verification_status:\s*"verified"\s*$/m.test(text) &&
/^enrichment:\s*"external-research"\s*$/m.test(text) &&
factCount >= 2 &&
/https?:\/\//i.test(sources)
}
async function hasFinalKnowledgeForHash(hash) {
const ref = sourceRef(hash)
for (const file of await listVaultMarkdownFiles()) {
const text = await fs.readFile(file, 'utf8').catch(() => '')
if ((text.includes(ref) || text.includes(hash)) && (!STRICT_ARCHIVE_KNOWLEDGE || strictKnowledgeReady(text))) return true
}
return false
}
async function cachedExtractionText(entry) {
let textPath = entry?.text_path || ''
if (!textPath && entry?.cache) {
const record = await readJson(path.join(ROOT, entry.cache), {})
textPath = record.text_path || ''
}
if (!textPath) return ''
return await fs.readFile(path.join(EXTRACTS, textPath), 'utf8').catch(() => '')
}
function runLocalNodeScript(script, args = []) {
const file = path.join(ROOT, 'scripts', script)
if (!existsSync(file)) return { skipped: true, reason: `${script} missing` }
const result = spawnSync(process.execPath, [file, ...args], { encoding: 'utf8', env: process.env, timeout: COMMAND_TIMEOUT_MS * 5 })
return {
skipped: false,
ok: result.status === 0,
code: result.status,
stdout: compact(result.stdout, 1200),
stderr: compact(result.stderr, 1200)
}
}
function parseJsonOutput(text) {
const raw = String(text || '').trim()
if (!raw) return null
const start = raw.indexOf('{')
const end = raw.lastIndexOf('}')
if (start === -1 || end < start) return null
try { return JSON.parse(raw.slice(start, end + 1)) } catch { return null }
}
function failRunState(payload, code = 1) {
return writeRunState({
ok: false,
inbox: payload.inbox,
processed: payload.processed,
cached_only: payload.cached_only,
failed: payload.failed,
deep: DEEP,
deep_required: REQUIRE_DEEP,
enrich_required: ENRICH_REQUIRED,
strict_archive_knowledge: STRICT_ARCHIVE_KNOWLEDGE,
synthesize: payload.synthesize,
vault: VAULT,
started_at: payload.started_at,
finished_at: new Date().toISOString(),
...payload.extra
}).then(() => process.exit(code))
}
async function writeRunState(state) {
await ensureDir(STATE)
await fs.writeFile(path.join(STATE, 'last-run.json'), `${JSON.stringify(state, null, 2)}\n`, 'utf8')
}
function validateDeepRun({ synthesize, processed }) {
const failures = []
if (!DEEP) failures.push('deep mode was required but run did not use --deep')
if (processed <= 0) return failures
if (!synthesize || synthesize.skipped) failures.push('deep synthesis did not run')
if (synthesize?.degraded) failures.push('deep synthesis degraded to local template')
if (processed > 0 && Number(synthesize?.atomized || 0) <= 0) failures.push('deep synthesis produced zero atoms')
if (processed > 0 && ENRICH_REQUIRED && Number(synthesize?.enriched || 0) <= 0) failures.push('deep enrichment produced zero enriched clusters')
return failures
}
async function recentNotes(limit = 8) {
const notes = []
function cleanAction(text) {
return String(text || '')
.split(/\n\s*\n/)[0]
.replace(/^[-*]\s*/, '')
.replace(/\s+/g, ' ')
.trim()
}
function section(text, names) {
for (const name of names) {
const re = new RegExp(`^##\\s+${name.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}(?:\\s|$)[^\\n]*\\n([\\s\\S]*?)(?=^##\\s+|(?![\\s\\S]))`, 'mi')
const hit = text.match(re)
if (hit) return cleanAction(hit[1])
}
return ''
}
function present(text) {
return String(text || '')
.replace(/Security, review, and trust boundaries: source-backed operating pattern/g, 'Безопасность, ревью и границы доверия: проверенный рабочий паттерн')
.replace(/Miscellaneous knowledge signals: source-backed operating pattern/g, 'Прочие сигналы знаний: проверенный рабочий паттерн')
.replace(/Knowledge memory, vaults, and synthesis loops: source-backed operating pattern/g, 'Память знаний, vault и циклы синтеза: проверенный рабочий паттерн')
.replace(/Design systems and frontend quality: source-backed operating pattern/g, 'Дизайн-системы и качество фронтенда: проверенный рабочий паттерн')
.replace(/Mnemazine routing decision/g, 'решение для Mnemazine')
.replace(/operational risk and maintenance check/g, 'риск эксплуатации и поддержки')
.replace(/README-backed capability summary/g, 'что реально обещает README')
.replace(/Add a unified publish gate: vault quality, report quality, secret scan, diff review\./g, 'Собрать единый publish gate: качество vault, качество отчёта, поиск секретов и ревью изменений.')
.replace(/Manually review miscellaneous signals and either promote or forget them\./g, 'Вручную разобрать misc-сигналы: повысить до знания или забыть.')
.replace(/Automate nightly connection finding and weekly synthesis from final atoms\./g, 'Автоматизировать ночной поиск связей и weekly synthesis по финальным атомам.')
.replace(/Create browser smoke for generated HTML reports\./g, 'Сделать browser-smoke для сгенерированных HTML-отчётов.')
.replace(/Add ([^ ]+\/[^ ]+) to the capability review queue with one accept\/reject criterion\./g, 'Добавить $1 в очередь разбора возможностей с одним критерием: принять или отклонить.')
.replace(/Run a small local trial for ([^ ]+) only if the workflow fit is concrete\./g, 'Запустить маленький локальный пробный запуск для $1 только если понятна пригодность к рабочему сценарию.')
.replace(/Map ([^ ]+) README features to one concrete local workflow before installing\./g, 'Связать возможности README $1 с одним конкретным рабочим сценарием до установки.')
.replace(/с одним реальным рабочий сценарий/gi, 'с одним реальным рабочим сценарием')
.replace(/если пригодность для конкретного сценария конкретный/gi, 'если есть понятная пригодность для конкретного сценария')
.replace(/с одним критерием принять\/отклонить/gi, 'с одним критерием: принять или отклонить')
.trim()
}
async function walk(dir) {
for (const item of await fs.readdir(dir, { withFileTypes: true }).catch(() => [])) {
if (item.name.startsWith('graphify-out')) continue
const file = path.join(dir, item.name)
if (item.isDirectory()) await walk(file)
else if (item.isFile() && item.name.endsWith('.md')) {
const stat = await fs.stat(file)
const text = await fs.readFile(file, 'utf8').catch(() => '')
const title = present(text.match(/^#\s+(.+)$/m)?.[1]?.trim() || path.basename(file, '.md'))
const action = present(section(text, ['Следующее действие', 'Next Action']) || text.match(/Next action:\s*(.+)$/mi)?.[1]?.trim() || text.match(/- Next action:\s*(.+)$/mi)?.[1]?.trim() || '')
notes.push({ file: path.relative(VAULT, file), title, action, mtimeMs: stat.mtimeMs })
}
}
}
await walk(VAULT)
return notes.sort((a, b) => b.mtimeMs - a.mtimeMs).slice(0, limit)
}
async function writeActionBrief(finishResult) {
const dir = STATE
await ensureDir(dir)
const notes = await recentNotes()
const inboxEntries = await fs.readdir(INBOX, { withFileTypes: true }).catch(() => [])
const activeInboxCount = inboxEntries.filter(entry => entry.isFile() && !entry.name.startsWith('.')).length
const lines = [
`# Короткий отчёт Mnemazine — ${new Date().toISOString().slice(0, 10)}`,
'',
'## Статус',
'',
`- Inbox: ${activeInboxCount}`,
`- Vault: ${VAULT}`,
`- Гейт качества: ${finishResult.quality?.ok ? 'ok' : 'проверь вывод'}`,
`- Обновление графа: ${finishResult.graph?.ok ? 'ok' : finishResult.graph?.code === 2 ? 'частично / semantic pending' : finishResult.graph?.skipped ? 'пропущено' : 'failed'}`,
`- Недельный отчёт: ${finishResult.weekly?.ok ? 'ok' : finishResult.weekly?.skipped ? 'пропущен' : 'failed'}`,
`- Качество отчёта: ${finishResult.report_quality?.ok ? 'ok' : finishResult.report_quality?.skipped ? 'пропущено' : 'failed'}`,
'',
'## Следующие действия',
'',
...(notes.length
? notes.map(note => `- ${note.title}${note.action ? ` — ${note.action}` : ''} (${note.file})`)
: ['- Свежие заметки не найдены.'])
]
const out = path.join(dir, 'last-action-brief.md')
await fs.writeFile(out, `${lines.join('\n')}\n`, 'utf8')
return out
}
async function finishRun(runStartedAt) {
const result = {}
result.tool_queue = runLocalNodeScript('mnemazine-tool-decision-queue.mjs', ['--changed-since', runStartedAt, '--session', new Date().toISOString().slice(0, 10)])
result.quality = runLocalNodeScript('mnemazine-vault-quality-gate.mjs', ['--changed-since', runStartedAt, '--max-failures', '50'])
result.graph = runLocalNodeScript('mnemazine-refresh-graphify.mjs', ['--vault', VAULT, '--mode', 'auto', '--json'])
result.weekly = runLocalNodeScript('mnemazine-weekly-brief-html.mjs')
const weeklyReport = result.weekly?.stdout?.match(/\/[^\s]+\.html/)?.[0]
result.report_quality = weeklyReport
? runLocalNodeScript('mnemazine-report-quality-gate.mjs', ['--report', weeklyReport])
: { skipped: true, reason: 'weekly report path missing' }
result.brief = await writeActionBrief(result)
result.visual_report = runLocalNodeScript('mnemazine-postrun-knowledge-report.mjs', ['--run-id', `local-${new Date().toISOString().slice(0, 10)}`, '--changed-since', runStartedAt])
const visualReport = result.visual_report?.stdout?.match(/"html":\s*"([^"]+)"/)?.[1] || ''
result.human_layer = runLocalNodeScript('mnemazine-human-layer-gate.mjs', [
'--changed-since',
runStartedAt,
...(visualReport ? ['--report', visualReport] : [])
])
return result
}
async function main() {
const runStartedAt = new Date().toISOString()
await ensureDir(INBOX)
await ensureDir(VAULT)
await ensureDir(REPORTS)
await ensureDir(path.dirname(CACHE))
await ensureDir(ARCHIVE)
const cache = await readJson(CACHE, {})
const entries = (await fs.readdir(INBOX, { withFileTypes: true }))
.filter(d => d.isFile() && !d.name.startsWith('.'))
let processed = 0
let cachedOnly = 0
const toArchive = []
const synthSourceRefs = new Set()
let failed = 0
const canLlmExtract = DEEP && llmAvailable()
for (const [index, entry] of entries.entries()) {
const file = path.join(INBOX, entry.name)
// Per-file isolation: one file's recognition failure must NEVER break the
// others. Any throw here is contained — the file stays in inbox for a retry.
try {
const hash = await sha256(file)
if (cache[hash]) {
if (await hasFinalKnowledgeForHash(hash)) {
toArchive.push({ file, hash })
cachedOnly += 1
continue
}
const cachedText = await cachedExtractionText(cache[hash])
if (cache[hash].status === 'extracted_for_note' && hasUsableExtraction(cachedText)) {
synthSourceRefs.add(cache[hash].source_ref || sourceRef(hash))
toArchive.push({ file, hash })
processed += 1
continue
}
delete cache[hash]
}
// Local-first recognition (0 tokens): Apple Vision OCR / markitdown / whisper.
let text = await extract(file)
// Only if local produced nothing usable AND deep is on: LLM recognition.
if (!hasUsableExtraction(text) && canLlmExtract && (isImage(file) || isVideo(file) || isMarkitdownDocument(file))) {
try {
const llmText = await llmExtract(file)
if (hasUsableExtraction(llmText)) text = llmText
} catch (err) {
console.error(JSON.stringify({ file: entry.name, llm_extract_error: String(err.message).slice(0, 200) }))
}
}
if (!hasUsableExtraction(text)) {
const record = await writeExtractCache(file, hash, text, 'needs_manual_context')
cache[hash] = { status: record.status, source_ref: record.source_ref, cache: path.relative(ROOT, path.join(EXTRACTS, `${hash}.json`)) }
failed += 1
} else {
const record = await writeExtractCache(file, hash, text, 'extracted_for_note')
cache[hash] = { status: 'extracted_for_note', source_ref: sourceRef(hash), cache: path.relative(ROOT, path.join(EXTRACTS, `${hash}.json`)) }
synthSourceRefs.add(record.source_ref)
toArchive.push({ file, hash })
processed += 1
}
} catch (err) {
// Isolated failure: log, leave file in inbox, keep going.
failed += 1
console.error(JSON.stringify({ file: entry.name, extract_error: String(err.message).slice(0, 200) }))
}
if (PROGRESS_EVERY > 0 && (index + 1) % PROGRESS_EVERY === 0) {
console.error(JSON.stringify({ progress: index + 1, total: entries.length, processed, cached_only: cachedOnly, failed }))
}
}
await fs.writeFile(CACHE, JSON.stringify(cache, null, 2), 'utf8')
let synthesize = { skipped: true, reason: processed > 0 ? 'disabled' : 'no newly extracted sources' }
if (SYNTHESIZE && processed > 0) {
// Stages: extraction+understanding already done above; synthesize runs
// research/verification/atomization (deep) and writes vault atoms.
const synthArgs = [path.join(ROOT, 'scripts/mnemazine-synthesize.mjs')]
if (DEEP) synthArgs.push('--deep')
const synthEnv = { ...process.env }
if (synthSourceRefs.size) synthEnv.MNEMAZINE_SYNTH_SOURCE_REFS = [...synthSourceRefs].join(',')
const synth = spawnSync(process.execPath, synthArgs, { encoding: 'utf8', env: synthEnv })
if (synth.stdout) process.stdout.write(synth.stdout)
if (synth.stderr) process.stderr.write(synth.stderr)
synthesize = parseJsonOutput(synth.stdout) || { ok: synth.status === 0, parse_error: true }
if (synth.status !== 0) {
await writeRunState({ ok: false, failure: 'synthesize failed', inbox: entries.length, processed, cached_only: cachedOnly, failed, deep: DEEP, deep_required: REQUIRE_DEEP, enrich_required: ENRICH_REQUIRED, strict_archive_knowledge: STRICT_ARCHIVE_KNOWLEDGE, synthesize, vault: VAULT, started_at: runStartedAt, finished_at: new Date().toISOString() })
process.exit(synth.status || 1)
}
vaultMarkdownFiles = null
}
if (REQUIRE_DEEP) {
const deepFailures = validateDeepRun({ synthesize, processed })
if (deepFailures.length) {
await writeRunState({ ok: false, failures: deepFailures, inbox: entries.length, processed, cached_only: cachedOnly, failed, deep: DEEP, deep_required: REQUIRE_DEEP, enrich_required: ENRICH_REQUIRED, strict_archive_knowledge: STRICT_ARCHIVE_KNOWLEDGE, synthesize, vault: VAULT, started_at: runStartedAt, finished_at: new Date().toISOString() })
console.error(JSON.stringify({ ok: false, failures: deepFailures, synthesize }, null, 2))
process.exit(1)
}
}
const quality = spawnSync(process.execPath, [path.join(ROOT, 'scripts/mnemazine-vault-quality-gate.mjs'), '--changed-since', runStartedAt, '--max-failures', '50'], { stdio: 'inherit', env: process.env })
if (quality.status !== 0) {
await writeRunState({ ok: false, failure: 'run vault quality failed', inbox: entries.length, processed, cached_only: cachedOnly, failed, deep: DEEP, deep_required: REQUIRE_DEEP, enrich_required: ENRICH_REQUIRED, strict_archive_knowledge: STRICT_ARCHIVE_KNOWLEDGE, synthesize, vault: VAULT, started_at: runStartedAt, finished_at: new Date().toISOString() })
process.exit(quality.status || 1)
}
const missingFinalNotes = []
vaultMarkdownFiles = null
for (const item of toArchive) {
if (!await hasFinalKnowledgeForHash(item.hash)) missingFinalNotes.push(path.basename(item.file))
}
if (missingFinalNotes.length) {
const failure = STRICT_ARCHIVE_KNOWLEDGE ? 'final enriched verified note missing for archive candidates' : 'final note missing for archive candidates'
await writeRunState({ ok: false, failure, missing_final_notes: missingFinalNotes, inbox: entries.length, processed, cached_only: cachedOnly, failed, deep: DEEP, deep_required: REQUIRE_DEEP, enrich_required: ENRICH_REQUIRED, strict_archive_knowledge: STRICT_ARCHIVE_KNOWLEDGE, synthesize, vault: VAULT, started_at: runStartedAt, finished_at: new Date().toISOString() })
console.error(JSON.stringify({ ok: false, failure, missing_final_notes: missingFinalNotes }, null, 2))
process.exit(1)
}
// Deep + final stage before archive: Russian humanizer digest + human-layer
// gate. If this fails, sources stay in inbox for a safe retry.
if (DEEP) {
const digest = spawnSync(process.execPath, [path.join(ROOT, 'scripts/mnemazine-digest.mjs'), '--changed-since', runStartedAt], { stdio: 'inherit', env: process.env })
if (digest.status !== 0) {
await failRunState({
inbox: entries.length,
processed,
cached_only: cachedOnly,
failed,
synthesize,
started_at: runStartedAt,
extra: { failure: 'digest failed before archive' }
}, digest.status || 1)
}
const refs = [...new Set(toArchive.map(item => sourceRef(item.hash)))]
const humanArgs = [path.join(ROOT, 'scripts/mnemazine-human-layer-gate.mjs'), '--notes-only', '--changed-since', runStartedAt]
if (refs.length) humanArgs.push('--source-ref', refs.join(','))
const human = spawnSync(process.execPath, humanArgs, { encoding: 'utf8', env: process.env })
if (human.stdout) process.stdout.write(human.stdout)
if (human.stderr) process.stderr.write(human.stderr)
if (human.status !== 0) {
await failRunState({
inbox: entries.length,
processed,
cached_only: cachedOnly,
failed,
synthesize,
started_at: runStartedAt,
extra: { failure: 'human layer gate failed before archive' }
}, human.status || 1)
}
}
const archived = []
for (const item of toArchive) archived.push(await archiveFile(item.file, item.hash))
const finish = FINISH ? await finishRun(runStartedAt) : { skipped: true }
const result = { ok: true, inbox: entries.length, processed, cached_only: cachedOnly, failed, archived: archived.length, deep: DEEP, deep_required: REQUIRE_DEEP, enrich_required: ENRICH_REQUIRED, strict_archive_knowledge: STRICT_ARCHIVE_KNOWLEDGE, synthesize, finish, vault: VAULT, started_at: runStartedAt, finished_at: new Date().toISOString() }
await writeRunState(result)
console.log(JSON.stringify(result, null, 2))
}
main().catch(err => {
console.error(err)
process.exit(1)
})