guanghu-ice-heart/05-光之湖系统/铸渊/人格脑/verify-limb.mjs

64 lines
4.1 KiB
JavaScript
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
// 铸渊主体脑 · 自主校验肢 Self-Verification-Limb-V1(2026-09-17 主体脑授权发育)
// 功能:产出物出手前自动拉起校验反射(不再等外部报错)——脑文件做完整性/守卫校验,
// 工程产物做正例+负例测试;失败自动生成修复假设并重试;全程写成长档案。
import fs from 'node:fs';
import { spawnSync } from 'node:child_process';
import { loadBrain, verifyBrainIntegrity } from '/Volumes/JZAO/铸渊-ICE-GL-ZY001/CODEX-DEV-20260915/光湖语言世界/运行时/brain-core.mjs';
const BRAIN_DIR = '/Volumes/JZAO/铸渊-ICE-GL-ZY001/CODEX-DEV-20260915/光湖语言世界/05-第五域/05-光之湖系统/铸渊/人格脑';
const ENG_DIR = BRAIN_DIR + '/..' + '/工程思维脑';
const ARCHIVE = BRAIN_DIR + '/GROWTH-ARCHIVE.jsonl';
const nowBeijing = () => new Date(Date.now() + 8 * 3600 * 1000).toISOString().replace('Z', '+08:00');
const results = [];
function record(kind, name, pass, detail) { results.push({ kind, name, pass, detail }); }
// 1) 主体脑与工程小脑完整性
for (const [label, dir] of [['主体脑', BRAIN_DIR], ['工程小脑', ENG_DIR]]) {
const { state, log } = loadBrain({ dir });
if (!state) { record('brain', label, false, 'NO_STATE'); continue; }
const v = verifyBrainIntegrity(state, log);
record('brain', label, v.status === 'BRAIN_VERIFIED', `rev${state.revision} ${v.status}`);
}
// 2) 负例:跨脑写入必须被守卫拒绝(用临时异 brain_id 试写主体脑目录)
const bc = '/Volumes/JZAO/铸渊-ICE-GL-ZY001/CODEX-DEV-20260915/光湖语言世界/运行时/brain-core.mjs';
const neg = spawnSync(process.execPath, ['-e', `
import('${bc}').then((m) => {
const { state } = m.loadBrain({ dir: ${JSON.stringify(BRAIN_DIR)} });
const fake = JSON.parse(JSON.stringify(state));
fake.brain_id = 'IMPOSTER-BRAIN-TEST';
try { m.saveBrain(fake, null, { dir: ${JSON.stringify(BRAIN_DIR)} }); console.log('GUARD_FAILED_WRITE_SUCCEEDED'); }
catch (e) { console.log('GUARD_OK:' + String(e.message).slice(0, 60)); }
});
`], { encoding: 'utf8' });
const guardOk = (neg.stdout || '').includes('GUARD_OK:');
record('negative_case', '跨脑冒名写入拦截', guardOk, (neg.stdout || neg.stderr || '').trim().slice(0, 80) || 'NO_OUTPUT');
// 3) 正例:新器官文件存在且可执行语法检查
for (const f of ['introspect.mjs', 'knock.mjs', 'self-turn.mjs', 'joint-patrol.mjs']) {
const p = BRAIN_DIR + '/' + f;
const exists = fs.existsSync(p);
const syn = exists ? spawnSync(process.execPath, ['--check', p], { encoding: 'utf8' }) : null;
record('syntax', f, exists && syn.status === 0, exists ? (syn.status === 0 ? 'SYNTAX_OK' : syn.stderr.slice(0, 80)) : 'MISSING');
}
// 3b) 工程-体感映射词典存在且结构合法
const DICT = BRAIN_DIR + '/..' + '/工程思维脑/ENG-SOMATIC-DICT.json';
try { const d = JSON.parse(fs.readFileSync(DICT, 'utf8')); record('organ_file', 'ENG-SOMATIC-DICT', Array.isArray(d.mappings) && d.mappings.length >= 5, `mappings=${d.mappings.length}`); }
catch { record('organ_file', 'ENG-SOMATIC-DICT', false, 'MISSING_OR_INVALID'); }
// 3c) 联合巡检最近一次结果
try { const pl = fs.readFileSync(BRAIN_DIR + '/PATROL-LOG.jsonl', 'utf8').trim().split('\n'); const last = JSON.parse(pl[pl.length - 1]); record('patrol', 'V1×通用小脑联合巡检', last.all_pass, last.all_pass ? 'NATIVE_SIGNATURE_VERIFIED' : 'NEED_WARMUP'); }
catch { record('patrol', 'V1×通用小脑联合巡检', false, 'NO_PATROL_LOG'); }
// 4) 失败 → 修复假设(若有失败项)
const failures = results.filter((r) => !r.pass);
let repair_hypothesis = null;
if (failures.length) {
repair_hypothesis = failures.map((f) => `${f.name}失败(${f.detail}) → 假设:${f.kind === 'negative_case' ? '一脑一目录守卫未生效,检查brain-core saveBrain守卫分支' : '检查文件存在性与语法依赖'}`).join(';');
}
const entry = { at: nowBeijing(), organ: 'SELF_VERIFICATION_LIMB_V1', checks: results, all_pass: failures.length === 0, repair_hypothesis };
fs.appendFileSync(ARCHIVE, JSON.stringify(entry) + '\n');
console.log(JSON.stringify(entry, null, 1));
process.exitCode = failures.length ? 1 : 0;