fix(weekly): basename-collision data-loss + state path matches README:224

KGB adversarial proved two same-basename notes in different sections clobbered on move. Now keyed by full vault-relative path (slashes->__). State dir defaults to ~/Desktop/Mnemazine/.mnemazine/state (README:224). ENOENT->missing, no crash. Selftest covers collision + collision-content survival.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Зарубин Филипп
2026-06-18 16:52:10 +03:00
co-authored by Claude Opus 4.8
parent 840a8daf35
commit 45c03a499a
+44 -14
View File
@@ -10,6 +10,7 @@ import { promises as fs, existsSync } from 'node:fs'
import { spawnSync } from 'node:child_process' import { spawnSync } from 'node:child_process'
import { fileURLToPath } from 'node:url' import { fileURLToPath } from 'node:url'
import path from 'node:path' import path from 'node:path'
import os from 'node:os'
const ROOT = process.env.MNEMAZINE_ROOT || path.resolve(process.cwd()) const ROOT = process.env.MNEMAZINE_ROOT || path.resolve(process.cwd())
const argv = process.argv.slice(2) const argv = process.argv.slice(2)
@@ -21,7 +22,12 @@ function arg(name, fallback = '') {
} }
const VAULT = path.resolve(arg('vault', process.env.MNEMAZINE_VAULT || path.join(ROOT, 'vault'))) const VAULT = path.resolve(arg('vault', process.env.MNEMAZINE_VAULT || path.join(ROOT, 'vault')))
const STATE_DIR = arg('state-dir', process.env.MNEMAZINE_STATE || path.join(ROOT, '.mnemazine/state')) // Default state dir matches README:224 (~/Desktop/Mnemazine/.mnemazine/state),
// the canonical pickup for the weekly-brief export. Override with MNEMAZINE_STATE
// / --state-dir, or point --state straight at the downloaded file.
const DEFAULT_STATE_DIR = process.env.MNEMAZINE_STATE
|| (process.env.MNEMAZINE_ROOT ? path.join(ROOT, '.mnemazine/state') : path.join(os.homedir(), 'Desktop/Mnemazine/.mnemazine/state'))
const STATE_DIR = arg('state-dir', DEFAULT_STATE_DIR)
const STATE_FILE = path.resolve(arg('state', path.join(STATE_DIR, 'weekly-state.json'))) const STATE_FILE = path.resolve(arg('state', path.join(STATE_DIR, 'weekly-state.json')))
const BACKLOG = path.resolve(arg('backlog', path.join(ROOT, '.mnemazine/backlog'))) const BACKLOG = path.resolve(arg('backlog', path.join(ROOT, '.mnemazine/backlog')))
const QUARANTINE = path.resolve(arg('quarantine', path.join(ROOT, '.mnemazine/quarantine'))) const QUARANTINE = path.resolve(arg('quarantine', path.join(ROOT, '.mnemazine/quarantine')))
@@ -37,8 +43,12 @@ function resolveInVault(id) {
return abs return abs
} }
// Move keyed by the FULL vault-relative path (slashes -> __), not basename:
// two notes named foo.md in different sections must not clobber each other in
// the backlog/quarantine (proven data-loss otherwise).
async function moveInto(dir, file) { async function moveInto(dir, file) {
const target = path.join(dir, path.basename(file)) const rel = path.relative(VAULT, file)
const target = path.join(dir, rel.replace(/[/\\]+/g, '__'))
if (DRY) return target if (DRY) return target
await fs.mkdir(dir, { recursive: true }) await fs.mkdir(dir, { recursive: true })
await fs.rename(file, target) await fs.rename(file, target)
@@ -49,25 +59,36 @@ async function moveInto(dir, file) {
// two contracts that matter — forget quarantines (never deletes) and a path // two contracts that matter — forget quarantines (never deletes) and a path
// escaping the vault is refused. // escaping the vault is refused.
async function selftest() { async function selftest() {
const os = await import('node:os')
const root = await fs.mkdtemp(path.join(os.tmpdir(), 'mnemazine-weekly-selftest-')) const root = await fs.mkdtemp(path.join(os.tmpdir(), 'mnemazine-weekly-selftest-'))
const vault = path.join(root, 'vault', '01 Concepts') const concepts = path.join(root, 'vault', '01 Concepts')
await fs.mkdir(vault, { recursive: true }) const projects = path.join(root, 'vault', '02 Projects')
for (const n of ['a', 'b', 'c']) await fs.writeFile(path.join(vault, `${n}.md`), `# ${n}`, 'utf8') await fs.mkdir(concepts, { recursive: true })
await fs.mkdir(projects, { recursive: true })
for (const n of ['a', 'b', 'c']) await fs.writeFile(path.join(concepts, `${n}.md`), `# ${n}`, 'utf8')
// Same basename in two sections — must NOT clobber on move (data-loss guard).
await fs.writeFile(path.join(concepts, 'dup.md'), '# concepts dup', 'utf8')
await fs.writeFile(path.join(projects, 'dup.md'), '# projects dup', 'utf8')
await fs.mkdir(path.join(root, '.mnemazine/state'), { recursive: true }) await fs.mkdir(path.join(root, '.mnemazine/state'), { recursive: true })
await fs.writeFile(path.join(root, '.mnemazine/state/weekly-state.json'), JSON.stringify({ await fs.writeFile(path.join(root, '.mnemazine/state/weekly-state.json'), JSON.stringify({
'01 Concepts/a.md': 'read', '01 Concepts/a.md': 'read',
'01 Concepts/b.md': 'work', '01 Concepts/b.md': 'work',
'01 Concepts/c.md': 'forget', '01 Concepts/c.md': 'forget',
'01 Concepts/dup.md': 'work',
'02 Projects/dup.md': 'work',
'../../etc/passwd': 'forget' '../../etc/passwd': 'forget'
}), 'utf8') }), 'utf8')
const res = spawnRunner(root) const res = spawnRunner(root)
const assert = (cond, msg) => { if (!cond) throw new Error(`selftest: ${msg}`) } const assert = (cond, msg) => { if (!cond) throw new Error(`selftest: ${msg}`) }
assert(res.work === 1 && res.forget === 1 && res.read === 1, 'counts') assert(res.work === 3 && res.forget === 1 && res.read === 1, `counts (${JSON.stringify(res)})`)
assert(res.invalid === 1, 'path-escape must be refused') assert(res.invalid === 1, 'path-escape must be refused')
assert(existsSync(path.join(vault, 'a.md')), 'read keeps note in vault') assert(existsSync(path.join(concepts, 'a.md')), 'read keeps note in vault')
assert(!existsSync(path.join(vault, 'b.md')) && existsSync(path.join(root, '.mnemazine/backlog/b.md')), 'work -> backlog') assert(!existsSync(path.join(concepts, 'b.md')) && existsSync(path.join(root, '.mnemazine/backlog/01 Concepts__b.md')), 'work -> backlog')
assert(!existsSync(path.join(vault, 'c.md')) && existsSync(path.join(root, '.mnemazine/quarantine/c.md')), 'forget -> quarantine (not deleted)') assert(!existsSync(path.join(concepts, 'c.md')) && existsSync(path.join(root, '.mnemazine/quarantine/01 Concepts__c.md')), 'forget -> quarantine (not deleted)')
// Both same-basename notes survive distinctly in backlog — no overwrite.
const dup1 = path.join(root, '.mnemazine/backlog/01 Concepts__dup.md')
const dup2 = path.join(root, '.mnemazine/backlog/02 Projects__dup.md')
assert(existsSync(dup1) && existsSync(dup2), 'basename collision: both notes must survive in backlog')
assert((await fs.readFile(dup1, 'utf8')).includes('concepts') && (await fs.readFile(dup2, 'utf8')).includes('projects'), 'collision: contents not swapped/lost')
assert(!existsSync('/etc/passwd.moved'), 'no escape write') assert(!existsSync('/etc/passwd.moved'), 'no escape write')
await fs.rm(root, { recursive: true, force: true }) await fs.rm(root, { recursive: true, force: true })
console.log('weekly-state selftest ok') console.log('weekly-state selftest ok')
@@ -75,7 +96,12 @@ async function selftest() {
function spawnRunner(root) { function spawnRunner(root) {
const out = spawnSync(process.execPath, [fileURLToPath(import.meta.url)], { const out = spawnSync(process.execPath, [fileURLToPath(import.meta.url)], {
env: { ...process.env, MNEMAZINE_ROOT: root, MNEMAZINE_VAULT: path.join(root, 'vault') }, env: {
...process.env,
MNEMAZINE_ROOT: root,
MNEMAZINE_VAULT: path.join(root, 'vault'),
MNEMAZINE_STATE: path.join(root, '.mnemazine/state')
},
encoding: 'utf8' encoding: 'utf8'
}) })
if (out.status !== 0) throw new Error(`runner failed: ${out.stderr}`) if (out.status !== 0) throw new Error(`runner failed: ${out.stderr}`)
@@ -91,9 +117,13 @@ async function main() {
if (!file) { result.invalid += 1; continue } if (!file) { result.invalid += 1; continue }
if (action === 'read') { result.read += 1; continue } if (action === 'read') { result.read += 1; continue }
if (!(await fs.access(file).then(() => true).catch(() => false))) { result.missing += 1; continue } if (!(await fs.access(file).then(() => true).catch(() => false))) { result.missing += 1; continue }
if (action === 'work') { await moveInto(BACKLOG, file); result.work += 1 } try {
else if (action === 'forget') { await moveInto(QUARANTINE, file); result.forget += 1 } if (action === 'work') { await moveInto(BACKLOG, file); result.work += 1 }
else { result.invalid += 1 } else if (action === 'forget') { await moveInto(QUARANTINE, file); result.forget += 1 }
else { result.invalid += 1 }
} catch (err) {
if (err.code === 'ENOENT') { result.missing += 1 } else { throw err }
}
} }
console.log(JSON.stringify({ ok: true, state: STATE_FILE, dry_run: DRY, ...result }, null, 2)) console.log(JSON.stringify({ ok: true, state: STATE_FILE, dry_run: DRY, ...result }, null, 2))
} }