feat(synthesize): honest draft notes + idempotent fingerprints; G4 LLM atomize + G5 hybrid verify
C2 status:draft/verified:false. U2/U3 content-fingerprint naming + skip-existing. G4: --deep atomizes one cluster into focused atoms via shared codex bridge (default off -> local template). G5: verification_status verified/assumed/unknown — local structural gate by default, --deep adds HEAD+codex web cross-check. 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
dc909f4bc5
commit
cc751dd6eb
@@ -0,0 +1,52 @@
|
||||
#!/usr/bin/env node
|
||||
// Shared Codex bridge for Mnemazine node scripts.
|
||||
// Reuses the headless `codex exec` pattern already proven in
|
||||
// scripts/kb-enrich-codex.sh / kb-recheck-codex.sh — one schema-constrained
|
||||
// call, JSON captured via `-o`, prompt on stdin. No new LLM client.
|
||||
// ponytail: codex-only by design (Phil's local cascade). Swap the binary via
|
||||
// MNEMAZINE_CODEX_BIN; wire another provider here only if codex stops being it.
|
||||
import { spawnSync } from 'node:child_process'
|
||||
import { existsSync, promises as fs } from 'node:fs'
|
||||
import os from 'node:os'
|
||||
import path from 'node:path'
|
||||
import crypto from 'node:crypto'
|
||||
|
||||
const CODEX = process.env.MNEMAZINE_CODEX_BIN || '/Applications/Codex.app/Contents/Resources/codex'
|
||||
const TIMEOUT_MS = Number(process.env.MNEMAZINE_CODEX_TIMEOUT_MS || '300000')
|
||||
|
||||
export function codexAvailable() {
|
||||
return existsSync(CODEX)
|
||||
}
|
||||
|
||||
// Run one schema-constrained codex call. Returns the parsed JSON object or
|
||||
// throws. `cwd` is the codex working dir (defaults to a throwaway temp dir so
|
||||
// codex never touches the repo). Caller owns the schema shape.
|
||||
export async function codexJson(prompt, schema, options = {}) {
|
||||
if (!codexAvailable()) throw new Error(`codex binary not found: ${CODEX}`)
|
||||
const cwd = options.cwd || (await fs.mkdtemp(path.join(os.tmpdir(), 'mnemazine-codex-')))
|
||||
const stamp = crypto.randomBytes(6).toString('hex')
|
||||
const schemaFile = path.join(os.tmpdir(), `mnemazine-schema-${stamp}.json`)
|
||||
const outFile = path.join(os.tmpdir(), `mnemazine-codex-${stamp}.json`)
|
||||
const promptFile = path.join(os.tmpdir(), `mnemazine-prompt-${stamp}.md`)
|
||||
await fs.writeFile(schemaFile, JSON.stringify(schema), 'utf8')
|
||||
await fs.writeFile(promptFile, prompt, 'utf8')
|
||||
try {
|
||||
const res = spawnSync(CODEX, [
|
||||
'exec',
|
||||
'-C', cwd,
|
||||
'--skip-git-repo-check',
|
||||
'--dangerously-bypass-approvals-and-sandbox',
|
||||
'--output-schema', schemaFile,
|
||||
'-o', outFile,
|
||||
'-'
|
||||
], { input: await fs.readFile(promptFile, 'utf8'), encoding: 'utf8', timeout: options.timeoutMs || TIMEOUT_MS })
|
||||
if (res.status !== 0) {
|
||||
throw new Error(`codex exec failed (status ${res.status}): ${String(res.stderr || '').slice(-400)}`)
|
||||
}
|
||||
const raw = await fs.readFile(outFile, 'utf8').catch(() => '')
|
||||
if (!raw.trim()) throw new Error('codex returned empty output')
|
||||
return JSON.parse(raw)
|
||||
} finally {
|
||||
for (const f of [schemaFile, outFile, promptFile]) await fs.rm(f, { force: true }).catch(() => {})
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,9 @@
|
||||
#!/usr/bin/env node
|
||||
import { promises as fs } from 'node:fs'
|
||||
import path from 'node:path'
|
||||
import crypto from 'node:crypto'
|
||||
import { codexAvailable, codexJson } from './mnemazine-codex.mjs'
|
||||
import { verifyLocal, verifyDeep } from './mnemazine-verify.mjs'
|
||||
|
||||
const ROOT = process.env.MNEMAZINE_ROOT || path.resolve(process.cwd())
|
||||
const argv = process.argv.slice(2)
|
||||
@@ -15,6 +18,11 @@ const VAULT = path.resolve(arg('vault', process.env.MNEMAZINE_VAULT || path.join
|
||||
const EXTRACTS = path.resolve(arg('extracts', process.env.MNEMAZINE_EXTRACTS || path.join(ROOT, '.mnemazine/cache/extracted')))
|
||||
const SESSION = arg('session', new Date().toISOString().slice(0, 10))
|
||||
const MIN_CLUSTER_CHARS = Number(arg('min-cluster-chars', '80'))
|
||||
// --deep / --atomize: LLM-split one cluster into many focused atoms (README:50).
|
||||
// Default off — the conservative path (release demo, run.mjs) stays local-only
|
||||
// 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 MAX_ATOMS = Number(arg('max-atoms', process.env.MNEMAZINE_MAX_ATOMS || '20'))
|
||||
|
||||
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' },
|
||||
@@ -120,6 +128,16 @@ function uniq(values) {
|
||||
return [...new Set(values.filter(Boolean))]
|
||||
}
|
||||
|
||||
// Content fingerprint = stable hash of a cluster's sorted source refs. Same
|
||||
// inputs -> same fingerprint -> same filename, so re-runs are idempotent and
|
||||
// exact-duplicate clusters are not rewritten (see write loop skip).
|
||||
// ponytail: exact-dup only via source-ref hash; near-duplicate (paraphrase)
|
||||
// dedup needs embeddings — wire fastembed/Ollama here if dup clusters appear.
|
||||
function fingerprint(cluster) {
|
||||
const refs = cluster.records.map(record => String(record.source_ref || '')).sort()
|
||||
return crypto.createHash('sha1').update(refs.join(' | ||||