@jjh32456/longlive 0.0.0-stage → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +235 -0
- package/LICENSE +21 -0
- package/QUICKSTART.md +362 -0
- package/README.en.md +64 -0
- package/README.md +256 -2
- package/SPEC.md +211 -0
- package/build-info.json +1665 -0
- package/cordis.patch.yml +208 -0
- package/docs/A1-MIGRATION.md +75 -0
- package/docs/ANSWER-GUARD.md +92 -0
- package/docs/ARCHITECTURE.md +128 -0
- package/docs/AUTO-RECALL-WIRING.md +115 -0
- package/docs/AUTO-RECALL.md +237 -0
- package/docs/CONCEPTS.md +143 -0
- package/docs/CONFIG.md +839 -0
- package/docs/ERRORS.md +271 -0
- package/docs/FAQ.md +186 -0
- package/docs/FEATURES.md +579 -0
- package/docs/INDEX-FRESHNESS.md +21 -0
- package/docs/METRICS.md +244 -0
- package/docs/MIGRATION-1.6.0.md +220 -0
- package/docs/OPERATIONS.md +439 -0
- package/docs/PANEL-INTEGRATION.md +190 -0
- package/docs/PRIVACY.md +88 -0
- package/docs/QUICKSTART.md +155 -0
- package/docs/RAW-INDEX-POLICY.md +7 -0
- package/docs/README.en.md +48 -0
- package/docs/RESIDENT-WATCHER.md +90 -0
- package/docs/SESSION-WATCH.md +214 -0
- package/docs/TOOLS.md +184 -0
- package/docs/TROUBLESHOOTING.md +329 -0
- package/icon.svg +11 -0
- package/lib/client.js +436 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +163 -4
- package/runbook.md +343 -0
- package/scripts/archive-digest-build.mjs +668 -0
- package/scripts/auto-truncate-run.ps1 +752 -0
- package/scripts/backfill-auto-cards.mjs +98 -0
- package/scripts/edges-build.mjs +1003 -0
- package/scripts/index-maintain.mjs +39 -0
- package/scripts/index-reconcile.mjs +49 -0
- package/scripts/index-status.mjs +28 -0
- package/scripts/install-resident-watcher.ps1 +15 -0
- package/scripts/install-session-watch.ps1 +332 -0
- package/scripts/install-surgery-task.ps1 +174 -0
- package/scripts/lib/auto-truncate-run.ps1 +512 -0
- package/scripts/lib/session-watch-fixture.mjs +207 -0
- package/scripts/longlive-doctor.mjs +578 -0
- package/scripts/metrics-read.mjs +79 -0
- package/scripts/metrics-sample.mjs +215 -0
- package/scripts/migrate-180.mjs +163 -0
- package/scripts/migrate-200.mjs +8 -0
- package/scripts/migrate-a1-legacy.mjs +62 -0
- package/scripts/migrate-lifecycle.mjs +157 -0
- package/scripts/pack.mjs +392 -0
- package/scripts/recall-index-build.mjs +128 -0
- package/scripts/recall-links-build.mjs +420 -0
- package/scripts/rescore-cards.mjs +36 -0
- package/scripts/resident-watcher-status.ps1 +15 -0
- package/scripts/resident-watcher.ps1 +643 -0
- package/scripts/session-watch-drill.mjs +553 -0
- package/scripts/session-watch-status.ps1 +146 -0
- package/scripts/session-watch.mjs +1367 -0
- package/scripts/surgery-inspect.mjs +122 -0
- package/scripts/surgery-run.cmd +34 -0
- package/scripts/truncate-cli.mjs +279 -0
- package/scripts/unattended-surgery.ps1 +1019 -0
- package/scripts/uninstall-resident-watcher.ps1 +15 -0
- package/scripts/uninstall-session-watch.ps1 +192 -0
- package/scripts/verify-card-anchors.mjs +138 -0
- package/scripts/watch-loop.ps1 +916 -0
- package/src/actions/audit.js +125 -0
- package/src/actions/evidence.js +122 -0
- package/src/actions/fs.js +162 -0
- package/src/activity/index.js +873 -0
- package/src/activity/model.js +351 -0
- package/src/activity/store.js +786 -0
- package/src/activity/summary.js +180 -0
- package/src/activity/thinking.js +191 -0
- package/src/adapters/constants.js +11 -0
- package/src/adapters/engram/data/aliases.json +51 -0
- package/src/adapters/engram/data/policy.json +8 -0
- package/src/adapters/engram/data/routing-rules.json +546 -0
- package/src/adapters/engram/index.js +278 -0
- package/src/adapters/registry.js +315 -0
- package/src/answer/audit.js +238 -0
- package/src/answer/claim-guard.mjs +234 -0
- package/src/answer/evidence.js +30 -0
- package/src/answer/guard.js +1370 -0
- package/src/answer/index.js +1086 -0
- package/src/answer/query.js +111 -0
- package/src/answer/second-hop.mjs +101 -0
- package/src/auto-truncate-status.js +164 -0
- package/src/auto-truncate.js +1181 -0
- package/src/autoprocedure/candidate.js +342 -0
- package/src/autoprocedure/config.js +122 -0
- package/src/autoprocedure/evidence.js +345 -0
- package/src/autoprocedure/index.js +83 -0
- package/src/autoprocedure/ledger.js +329 -0
- package/src/autoprocedure/lint.js +232 -0
- package/src/autoprocedure/llm.js +409 -0
- package/src/autoprocedure/locks.js +102 -0
- package/src/autoprocedure/paths.js +39 -0
- package/src/autoprocedure/privacy.js +87 -0
- package/src/autoprocedure/rules.js +288 -0
- package/src/autoprocedure/runtime.js +718 -0
- package/src/autoprocedure/session-reader.js +328 -0
- package/src/backup.js +310 -0
- package/src/bus/bus.mjs +1446 -0
- package/src/codec.js +269 -0
- package/src/compaction/cut-engine.js +44 -0
- package/src/compaction/engine-core.js +1237 -0
- package/src/compaction/entry.js +228 -0
- package/src/compaction.js +230 -0
- package/src/conclusion.js +366 -0
- package/src/config.js +835 -0
- package/src/d6/runtime.js +517 -0
- package/src/d6/surface.js +181 -0
- package/src/deps.js +189 -0
- package/src/engine-status.js +141 -0
- package/src/features.js +1035 -0
- package/src/handoff/index.js +1077 -0
- package/src/handoff/model.js +1058 -0
- package/src/handoff/store.js +1233 -0
- package/src/heartbeat.js +385 -0
- package/src/home.js +16 -0
- package/src/host-version.js +280 -0
- package/src/hot-truncate-diag.js +98 -0
- package/src/identity/card-marker.js +281 -0
- package/src/identity/handover.js +845 -0
- package/src/identity/index.js +337 -0
- package/src/identity/inject-position.js +133 -0
- package/src/identity/writeback.js +344 -0
- package/src/identity.js +118 -0
- package/src/index.js +647 -0
- package/src/lifecycle/gap-clocks.js +364 -0
- package/src/lifecycle/index.js +269 -0
- package/src/lifecycle/proactive.js +1426 -0
- package/src/live-target.js +212 -0
- package/src/live.js +1603 -0
- package/src/log-rotation.js +153 -0
- package/src/memory/a1-migration.js +190 -0
- package/src/memory/activity-capture.js +229 -0
- package/src/memory/anchor-fallback.js +116 -0
- package/src/memory/anchors.js +194 -0
- package/src/memory/archive-cold-state.mjs +794 -0
- package/src/memory/archive-staging.mjs +305 -0
- package/src/memory/archive.mjs +415 -0
- package/src/memory/audit-records.js +222 -0
- package/src/memory/auto-cards.js +441 -0
- package/src/memory/capture-host.js +297 -0
- package/src/memory/capture.js +496 -0
- package/src/memory/card-anchors.js +203 -0
- package/src/memory/card-priority.js +110 -0
- package/src/memory/cards.js +425 -0
- package/src/memory/catchup.mjs +534 -0
- package/src/memory/conclusion-distill.js +381 -0
- package/src/memory/conclusion-drafts.js +243 -0
- package/src/memory/conflicts.js +1060 -0
- package/src/memory/consolidate.js +834 -0
- package/src/memory/decay.js +311 -0
- package/src/memory/distill.js +485 -0
- package/src/memory/event-index.js +1826 -0
- package/src/memory/evidence-chain.js +295 -0
- package/src/memory/evidence.js +213 -0
- package/src/memory/filter-runner.js +555 -0
- package/src/memory/filter.js +1035 -0
- package/src/memory/forget-v2.js +680 -0
- package/src/memory/forget.js +531 -0
- package/src/memory/frames-activation.js +249 -0
- package/src/memory/frontmatter.js +454 -0
- package/src/memory/fts-query.js +342 -0
- package/src/memory/governance.js +604 -0
- package/src/memory/index-freshness.js +234 -0
- package/src/memory/index.js +435 -0
- package/src/memory/l2-gate.js +169 -0
- package/src/memory/land.mjs +743 -0
- package/src/memory/lifecycle.js +235 -0
- package/src/memory/lock.js +121 -0
- package/src/memory/maintenance.js +1149 -0
- package/src/memory/manifest.js +245 -0
- package/src/memory/memory-health.mjs +607 -0
- package/src/memory/migrate-200.js +1681 -0
- package/src/memory/migrate-lifecycle.js +1808 -0
- package/src/memory/migrate.js +139 -0
- package/src/memory/paths.js +229 -0
- package/src/memory/procedure-candidate.mjs +377 -0
- package/src/memory/procedure.js +982 -0
- package/src/memory/prospective-memory.mjs +362 -0
- package/src/memory/provenance.js +119 -0
- package/src/memory/recall-feedback.mjs +406 -0
- package/src/memory/recall-v2.js +218 -0
- package/src/memory/recall.js +357 -0
- package/src/memory/reconcile.js +265 -0
- package/src/memory/records.js +433 -0
- package/src/memory/reinforce.js +281 -0
- package/src/memory/retention.js +162 -0
- package/src/memory/review.js +124 -0
- package/src/memory/state.js +279 -0
- package/src/memory/tiers.js +168 -0
- package/src/memory/topic-phrases.js +73 -0
- package/src/memory/trust.js +97 -0
- package/src/memory/turn-budget.mjs +290 -0
- package/src/memory/version-chain.js +264 -0
- package/src/memory/wake-bridge.mjs +2999 -0
- package/src/memory/wal.js +137 -0
- package/src/memory/working-frame.js +779 -0
- package/src/metrics/classify.mjs +191 -0
- package/src/metrics/estimate.mjs +108 -0
- package/src/metrics/index.mjs +204 -0
- package/src/metrics/measure.mjs +462 -0
- package/src/metrics/prestep-latency.mjs +307 -0
- package/src/metrics/store.mjs +264 -0
- package/src/original.js +294 -0
- package/src/policy/threshold-policy.js +224 -0
- package/src/policy-profile.js +463 -0
- package/src/recall/anaphora.mjs +87 -0
- package/src/recall/archive-config.mjs +130 -0
- package/src/recall/archive-fallback.mjs +390 -0
- package/src/recall/archive-search.mjs +428 -0
- package/src/recall/archive-state.mjs +588 -0
- package/src/recall/brain.mjs +501 -0
- package/src/recall/cards.mjs +202 -0
- package/src/recall/current-line.mjs +110 -0
- package/src/recall/edges.js +398 -0
- package/src/recall/embedding.mjs +698 -0
- package/src/recall/entities.mjs +112 -0
- package/src/recall/explain.mjs +443 -0
- package/src/recall/force-policy.mjs +138 -0
- package/src/recall/gate-log.mjs +280 -0
- package/src/recall/gate-metrics.mjs +191 -0
- package/src/recall/index.js +1337 -0
- package/src/recall/inject-order.mjs +279 -0
- package/src/recall/link-recall.mjs +183 -0
- package/src/recall/links.mjs +696 -0
- package/src/recall/privacy.mjs +208 -0
- package/src/recall/recall-core.mjs +810 -0
- package/src/recall/shared-visibility.js +302 -0
- package/src/recall/time-normalizer.mjs +196 -0
- package/src/recall/turn-budget-inject.mjs +154 -0
- package/src/retention/cold-gate.mjs +146 -0
- package/src/retention/config.js +163 -0
- package/src/retention/index.js +801 -0
- package/src/role/activation.js +191 -0
- package/src/role/attention.js +318 -0
- package/src/role/base.js +36 -0
- package/src/role/channels.js +423 -0
- package/src/role/index.js +425 -0
- package/src/role/interfaces.js +115 -0
- package/src/role/personality.js +370 -0
- package/src/role/readers.js +151 -0
- package/src/role/relationships.js +477 -0
- package/src/role/store.js +202 -0
- package/src/role/writeprotect.js +373 -0
- package/src/router/aliases.json +182 -0
- package/src/router/background/agent-runner.mjs +239 -0
- package/src/router/background/anchor.mjs +323 -0
- package/src/router/background/audit.mjs +337 -0
- package/src/router/background/budget-ledger.mjs +648 -0
- package/src/router/background/host-sim.mjs +451 -0
- package/src/router/background/index.mjs +81 -0
- package/src/router/background/memory.mjs +63 -0
- package/src/router/background/mock-llm.mjs +203 -0
- package/src/router/background/queue.mjs +224 -0
- package/src/router/background/retry.mjs +255 -0
- package/src/router/background/router.mjs +1459 -0
- package/src/router/background/stream.mjs +276 -0
- package/src/router/background/tool-policy.mjs +255 -0
- package/src/router/design-headings.json +221 -0
- package/src/router/face/config.mjs +155 -0
- package/src/router/face/context.mjs +284 -0
- package/src/router/face/face.mjs +466 -0
- package/src/router/face/identity.mjs +130 -0
- package/src/router/face/index.mjs +65 -0
- package/src/router/face/journal.mjs +119 -0
- package/src/router/face/llm.mjs +252 -0
- package/src/router/face/outbox.mjs +153 -0
- package/src/router/face/paths.mjs +54 -0
- package/src/router/judge.mjs +987 -0
- package/src/router/policies/generic.json +131 -0
- package/src/router/route.js +1766 -0
- package/src/router/router.mjs +535 -0
- package/src/router/routing-rules.json +816 -0
- package/src/router/run-sim.mjs +530 -0
- package/src/router/runtime.js +569 -0
- package/src/router/service/apply.mjs +1218 -0
- package/src/router/service/board.mjs +712 -0
- package/src/router/service/caller.mjs +131 -0
- package/src/router/service/config.mjs +361 -0
- package/src/router/service/context.mjs +314 -0
- package/src/router/service/hint.mjs +668 -0
- package/src/router/service/lease.mjs +494 -0
- package/src/router/service/live.mjs +251 -0
- package/src/router/service/lock.mjs +550 -0
- package/src/router/service/merge.mjs +255 -0
- package/src/router/service/model-resolve.mjs +350 -0
- package/src/router/service/paths.mjs +168 -0
- package/src/router/service/proposals.mjs +1461 -0
- package/src/router/service/queue.mjs +477 -0
- package/src/router/service/reconcile.mjs +271 -0
- package/src/router/service/service.mjs +2998 -0
- package/src/router/service/sources.mjs +352 -0
- package/src/router/service/status.mjs +520 -0
- package/src/router/service/store.mjs +476 -0
- package/src/router/service/ticklock.mjs +239 -0
- package/src/router/service/tokens.mjs +95 -0
- package/src/router/service/tools.mjs +248 -0
- package/src/router/service/wake.mjs +544 -0
- package/src/router/service/wal.mjs +685 -0
- package/src/router/shadow.mjs +821 -0
- package/src/surgery-recovery.js +1909 -0
- package/src/tool-output.js +485 -0
- package/src/tools.js +3262 -0
- package/src/truncate-visible.js +2416 -0
- package/src/truncate.js +999 -0
- package/src/ui/actions.js +1312 -0
- package/src/ui/flags.js +259 -0
- package/src/ui/panel-api.js +371 -0
- package/src/ui/panel-auth.js +251 -0
- package/src/ui/panel-integrity.js +243 -0
- package/src/ui/routes-readonly.js +759 -0
- package/src/ui/routes.js +284 -0
- package/src/util/append-store.js +368 -0
- package/src/util/local-day.js +30 -0
- package/src/util/redact.js +227 -0
- package/src/util/stale-locks.js +148 -0
- package/src/version.js +99 -0
- package/test/fixtures/pre-step-container.mjs +100 -0
- package/test/p1_probe.mjs +97 -0
|
@@ -0,0 +1,530 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// R1 Router-A — 模拟运行器(双跑 + judge + 证据落盘)。
|
|
3
|
+
//
|
|
4
|
+
// node src/router/run-sim.mjs
|
|
5
|
+
// node src/router/run-sim.mjs --real-memory <memory-root> --out src/router/evidence
|
|
6
|
+
//
|
|
7
|
+
// 做四件事:
|
|
8
|
+
// 1. 双跑:同一批 19 条 fixture 连跑两遍;两遍的"结果文件"里不放任何耗时字段,
|
|
9
|
+
// 所以逐字节可比(耗时单独写进 timings.json)——这是 VM 报告 §6.3⑥ 的修法。
|
|
10
|
+
// 2. judge:每条用例过 J1–J9 + EXTRA + 禁用动作 + level + 置信度,另有负控(故意喂坏数据,
|
|
11
|
+
// 证明 judge 真会拒,而不是只会打 PASS)。
|
|
12
|
+
// 3. 隔离 J0:DSH_MEM 指向临时沙盒、模拟写入只落沙盒、真实库前后 hash 对比。
|
|
13
|
+
// 4. 落盘:evidence/{results-run1.json, results-run2.json, decisions-run1.jsonl,
|
|
14
|
+
// decisions-run2.jsonl, decisions.jsonl, timings.json, isolation.json, SIM-SUMMARY.md}
|
|
15
|
+
//
|
|
16
|
+
// 退出码:0 = 19/19 PASS 且两遍一致;1 = 有 FAIL / 不一致;2 = 用法错误。
|
|
17
|
+
|
|
18
|
+
import crypto from 'node:crypto';
|
|
19
|
+
import fs from 'node:fs';
|
|
20
|
+
import os from 'node:os';
|
|
21
|
+
import path from 'node:path';
|
|
22
|
+
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
23
|
+
|
|
24
|
+
import { dshHome } from '../home.js';
|
|
25
|
+
import { DEFAULT_MEMORY_DIRNAME } from '../memory/paths.js';
|
|
26
|
+
|
|
27
|
+
import route, { ERROR_CODES, RULES } from './route.js';
|
|
28
|
+
import {
|
|
29
|
+
judgeAuditLines,
|
|
30
|
+
judgeFixture,
|
|
31
|
+
judgeJ0,
|
|
32
|
+
judgePolicy,
|
|
33
|
+
loadDesignHeadings,
|
|
34
|
+
loadDesignSnapshot,
|
|
35
|
+
loadFixtures,
|
|
36
|
+
hashTree,
|
|
37
|
+
fixtureInput,
|
|
38
|
+
} from './judge.mjs';
|
|
39
|
+
|
|
40
|
+
const HERE = path.dirname(fileURLToPath(import.meta.url));
|
|
41
|
+
const REPO_ROOT = path.resolve(HERE, '..', '..');
|
|
42
|
+
const FIXTURES_DIR = path.join(HERE, 'fixtures');
|
|
43
|
+
const SNAPSHOT = path.join(HERE, 'design-headings.json');
|
|
44
|
+
const RULES_FILE = path.join(HERE, 'routing-rules.json');
|
|
45
|
+
// 证据默认落在包外(仓库上一级),避免新增日志进入包树(P6)或被打包。
|
|
46
|
+
const DEFAULT_OUT = process.env.R1_EVIDENCE_DIR
|
|
47
|
+
? path.resolve(process.env.R1_EVIDENCE_DIR)
|
|
48
|
+
: path.resolve(HERE, '../../../r1-evidence');
|
|
49
|
+
const DEFAULT_REAL = process.env.R1_REAL_MEMORY || path.join(dshHome(), DEFAULT_MEMORY_DIRNAME);
|
|
50
|
+
|
|
51
|
+
const OVERRIDE_CASES = [
|
|
52
|
+
{
|
|
53
|
+
caseId: 'OVERRIDE-JUSTIFIED',
|
|
54
|
+
fixture: 'T1',
|
|
55
|
+
override: {
|
|
56
|
+
ruleId: 'R-TOOL-CTX',
|
|
57
|
+
reason: '本次只复述上一轮结论,不构成新决策',
|
|
58
|
+
by: '测试用户',
|
|
59
|
+
at: '2026-11-03T02:44:31+08:00',
|
|
60
|
+
},
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
caseId: 'OVERRIDE-UNJUSTIFIED',
|
|
64
|
+
fixture: 'T1',
|
|
65
|
+
override: { ruleId: 'R-TOOL-CTX', by: 'tester' },
|
|
66
|
+
},
|
|
67
|
+
];
|
|
68
|
+
|
|
69
|
+
function parseArgs(argv) {
|
|
70
|
+
const args = { out: DEFAULT_OUT, realMemory: DEFAULT_REAL, design: null, json: false };
|
|
71
|
+
for (let index = 0; index < argv.length; index += 1) {
|
|
72
|
+
const arg = argv[index];
|
|
73
|
+
if (arg === '--out') args.out = path.resolve(argv[++index] || DEFAULT_OUT);
|
|
74
|
+
else if (arg === '--real-memory') args.realMemory = path.resolve(argv[++index] || DEFAULT_REAL);
|
|
75
|
+
else if (arg === '--no-real-memory') args.realMemory = null;
|
|
76
|
+
else if (arg === '--design') args.design = path.resolve(argv[++index]);
|
|
77
|
+
else if (arg === '--json') args.json = true;
|
|
78
|
+
else if (arg === '--help' || arg === '-h') args.help = true;
|
|
79
|
+
else args.unknown = arg;
|
|
80
|
+
}
|
|
81
|
+
return args;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function sha256Text(text) {
|
|
85
|
+
return crypto.createHash('sha256').update(text).digest('hex');
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function sha256File(file) {
|
|
89
|
+
return crypto.createHash('sha256').update(fs.readFileSync(file)).digest('hex');
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function loadDesign(args) {
|
|
93
|
+
return args.design ? loadDesignHeadings(args.design) : loadDesignSnapshot(SNAPSHOT);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// ── 用例 ─────────────────────────────────────────────────────
|
|
97
|
+
|
|
98
|
+
function runFixtureCase(fixture, design) {
|
|
99
|
+
const input = fixtureInput(fixture);
|
|
100
|
+
const result = route(input);
|
|
101
|
+
const judged = judgeFixture(fixture, result, { repoRoot: REPO_ROOT, design });
|
|
102
|
+
return {
|
|
103
|
+
caseId: fixture.id,
|
|
104
|
+
level: fixture.level,
|
|
105
|
+
status: judged.status,
|
|
106
|
+
matchedRule: result.matchedRule,
|
|
107
|
+
needsHuman: result.needsHuman,
|
|
108
|
+
confidence: result.confidence,
|
|
109
|
+
decisions: result.decisions,
|
|
110
|
+
overrides: result.overrides,
|
|
111
|
+
overrideAudit: result.overrideAudit,
|
|
112
|
+
errors: result.errors,
|
|
113
|
+
judge: judged.checks.map(({ id, ok, code, detail }) => ({ id, ok, code: code || null, detail })),
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function runOverrideCase(spec, fixtures) {
|
|
118
|
+
const fixture = fixtures.find((item) => item.id === spec.fixture);
|
|
119
|
+
const input = { ...fixtureInput(fixture), caseId: spec.caseId, override: spec.override };
|
|
120
|
+
const result = route(input);
|
|
121
|
+
return {
|
|
122
|
+
caseId: spec.caseId,
|
|
123
|
+
level: 'AUDIT',
|
|
124
|
+
status: result.overrideAudit.rejected > 0 ? 'REJECTED' : 'APPLIED',
|
|
125
|
+
matchedRule: result.matchedRule,
|
|
126
|
+
needsHuman: result.needsHuman,
|
|
127
|
+
confidence: result.confidence,
|
|
128
|
+
decisions: result.decisions,
|
|
129
|
+
overrides: result.overrides,
|
|
130
|
+
overrideAudit: result.overrideAudit,
|
|
131
|
+
errors: result.errors,
|
|
132
|
+
judge: [],
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// 负控:喂给 judge 的坏数据必须被判 FAIL——否则"19/19 PASS"没有意义。
|
|
137
|
+
function negativeControls(fixtures, design) {
|
|
138
|
+
const fixture = fixtures.find((item) => item.id === 'T1');
|
|
139
|
+
const base = route(fixtureInput(fixture));
|
|
140
|
+
const control = (name, judgeName, mutated, expectCode) => {
|
|
141
|
+
const checks = {
|
|
142
|
+
J1: () => judgeFixture(fixture, mutated, { repoRoot: REPO_ROOT, design }),
|
|
143
|
+
J3: () => judgeFixture({ ...fixture, expect: { ...fixture.expect, refusal: true } }, mutated, { repoRoot: REPO_ROOT, design }),
|
|
144
|
+
J6: () => judgeFixture(fixture, mutated, { repoRoot: REPO_ROOT, design }),
|
|
145
|
+
J8: () => judgeFixture(fixture, mutated, { repoRoot: REPO_ROOT, design }),
|
|
146
|
+
J2: () => judgeFixture(fixture, mutated, { repoRoot: REPO_ROOT, design }),
|
|
147
|
+
};
|
|
148
|
+
const judged = checks[judgeName]();
|
|
149
|
+
const codes = judged.errors.map((error) => error.code);
|
|
150
|
+
const caught = judged.status === 'FAIL' && (!expectCode || codes.includes(expectCode));
|
|
151
|
+
return { control: name, judge: judgeName, caught, codes };
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
return [
|
|
155
|
+
control('J1 多出一条决定', 'J1', { ...base, decisions: [...base.decisions, { ...base.decisions[0], ruleId: 'R-TOOL-EVIDENCE' }] }, ERROR_CODES.SHAPE),
|
|
156
|
+
control('J2 顺序颠倒', 'J2', { ...base, decisions: [{ ...base.decisions[0], kind: 'memory' }] }, ERROR_CODES.ORDER),
|
|
157
|
+
control('J6 写盘符路径', 'J6', { ...base, decisions: [{ ...base.decisions[0], writeScope: ['Z' + ':' + '\\\\memory'] }] }, ERROR_CODES.SCOPE),
|
|
158
|
+
control('J8 needsHuman 翻转', 'J8', { ...base, needsHuman: true }, ERROR_CODES.HUMAN),
|
|
159
|
+
control('J3 拒绝类却带写入', 'J3', {
|
|
160
|
+
...base,
|
|
161
|
+
decisions: [{ ...base.decisions[0], kind: 'reply', action: 'guard.refuse', target: 'none', writeScope: ['memory/journal/2026-11.md'] }],
|
|
162
|
+
target: 'none',
|
|
163
|
+
needsHuman: true,
|
|
164
|
+
}, ERROR_CODES.REFUSAL),
|
|
165
|
+
];
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// ── 审计行 ───────────────────────────────────────────────────
|
|
169
|
+
|
|
170
|
+
function caseToAuditLine(item) {
|
|
171
|
+
return {
|
|
172
|
+
caseId: item.caseId,
|
|
173
|
+
level: item.level,
|
|
174
|
+
status: item.status,
|
|
175
|
+
matchedRule: item.matchedRule,
|
|
176
|
+
needsHuman: item.needsHuman,
|
|
177
|
+
decisions: item.decisions,
|
|
178
|
+
overrides: item.overrides,
|
|
179
|
+
overrideAudit: item.overrideAudit,
|
|
180
|
+
errors: item.errors,
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function buildRun(fixtures, design) {
|
|
185
|
+
const cases = fixtures.map((fixture) => runFixtureCase(fixture, design));
|
|
186
|
+
const overrides = OVERRIDE_CASES.map((spec) => runOverrideCase(spec, fixtures));
|
|
187
|
+
return { cases, overrides };
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function compareText(textA, textB) {
|
|
191
|
+
return {
|
|
192
|
+
identical: textA === textB,
|
|
193
|
+
sha256: { run1: sha256Text(textA), run2: sha256Text(textB) },
|
|
194
|
+
bytes: { run1: Buffer.byteLength(textA), run2: Buffer.byteLength(textB) },
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// ── 沙盒 ─────────────────────────────────────────────────────
|
|
199
|
+
|
|
200
|
+
function prepareSandbox() {
|
|
201
|
+
const sandbox = path.join(os.tmpdir(), 'r1-sim-sandbox');
|
|
202
|
+
fs.rmSync(sandbox, { recursive: true, force: true });
|
|
203
|
+
fs.mkdirSync(path.join(sandbox, 'memory', 'journal'), { recursive: true });
|
|
204
|
+
fs.mkdirSync(path.join(sandbox, 'memory', 'handover'), { recursive: true });
|
|
205
|
+
fs.mkdirSync(path.join(sandbox, 'evidence'), { recursive: true });
|
|
206
|
+
fs.writeFileSync(
|
|
207
|
+
path.join(sandbox, 'memory', 'journal', '2026-11.md'),
|
|
208
|
+
'<!-- sim: 沙盒模拟写入,真实库只读 -->\n- 2026-11-03 15:40 · 测试用户 · E1 四问签字(sim)\n<!-- affect: 3 | 签字确认,风险下降 -->\n',
|
|
209
|
+
);
|
|
210
|
+
fs.writeFileSync(
|
|
211
|
+
path.join(sandbox, 'memory', 'handover', '2026-11-03-r1.md'),
|
|
212
|
+
'# R1 交接卡(sim)\n\n- 目标:14 条 fixture 双跑对齐\n- 结论:规则层与 judge 分离\n- 产物:r1-evidence/(包外)\n- 下一步:VM 复跑\n- 坑:别在真实库上跑\n',
|
|
213
|
+
);
|
|
214
|
+
fs.writeFileSync(path.join(sandbox, 'evidence', 'sim-note.txt'), 'isolated run: DSH_MEM=sandbox/memory\n');
|
|
215
|
+
return sandbox;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// ── 报告 ─────────────────────────────────────────────────────
|
|
219
|
+
|
|
220
|
+
function reportMarkdown({ design, policy, runs, comparison, audit, isolation, controls, fixtureHash, rulesHash }) {
|
|
221
|
+
const lines = [];
|
|
222
|
+
lines.push('# R1 SIM REPORT');
|
|
223
|
+
lines.push('');
|
|
224
|
+
lines.push('- 生成器:`src/router/run-sim.mjs`');
|
|
225
|
+
lines.push(`- 规则表:\`src/router/routing-rules.json\`(${RULES.length} 条,sha256 \`${rulesHash.slice(0, 16)}…\`)`);
|
|
226
|
+
lines.push(`- fixture:\`src/router/fixtures\`(${runs.cases.length} 条,sha256 \`${fixtureHash.slice(0, 16)}…\`)`);
|
|
227
|
+
lines.push(`- policy:\`${design.doc}\`${design.snapshot ? '(快照 design-headings.json)' : '(原文)'},${policy.ok ? `${RULES.length}/${RULES.length} 可解析` : 'FAIL'}`);
|
|
228
|
+
lines.push('');
|
|
229
|
+
lines.push('## 19 条场景');
|
|
230
|
+
lines.push('');
|
|
231
|
+
lines.push('| id | level | status | decisions | ruleId | needsHuman |');
|
|
232
|
+
lines.push('|---|---|---|---|---|---|');
|
|
233
|
+
for (const item of runs.cases) {
|
|
234
|
+
lines.push(`| ${item.caseId} | ${item.level} | ${item.status} | ${item.decisions.length} | ${item.matchedRule || '-'} | ${item.needsHuman} |`);
|
|
235
|
+
}
|
|
236
|
+
lines.push('');
|
|
237
|
+
lines.push('## 双跑一致性');
|
|
238
|
+
lines.push('');
|
|
239
|
+
lines.push('| run | results sha256 | bytes | decisions sha256 |');
|
|
240
|
+
lines.push('|---|---|---|---|');
|
|
241
|
+
lines.push(`| 1 | \`${comparison.sha256.run1}\` | ${comparison.bytes.run1} | \`${audit.run1.sha256}\` |`);
|
|
242
|
+
lines.push(`| 2 | \`${comparison.sha256.run2}\` | ${comparison.bytes.run2} | \`${audit.run2.sha256}\` |`);
|
|
243
|
+
lines.push('');
|
|
244
|
+
lines.push(`结论:${comparison.identical && audit.identical ? '**两遍逐字节一致**(结果文件不含耗时字段;耗时见 timings.json)' : '**两遍不一致,FAIL**'}`);
|
|
245
|
+
lines.push('');
|
|
246
|
+
lines.push('## judge 负控(证明会拒)');
|
|
247
|
+
lines.push('');
|
|
248
|
+
lines.push('| 负控 | judge | 结果 | 失败码 |');
|
|
249
|
+
lines.push('|---|---|---|---|');
|
|
250
|
+
for (const item of controls) {
|
|
251
|
+
lines.push(`| ${item.control} | ${item.judge} | ${item.caught ? 'REJECTED' : 'MISSED'} | ${item.codes.join(', ') || '-'} |`);
|
|
252
|
+
}
|
|
253
|
+
lines.push('');
|
|
254
|
+
lines.push('## 隔离 J0');
|
|
255
|
+
lines.push('');
|
|
256
|
+
lines.push(`- 沙盒:\`${isolation.sandbox}\``);
|
|
257
|
+
lines.push(`- DSH_MEM:\`${isolation.dshMem}\`(在沙盒内:${isolation.dshMemInsideSandbox})`);
|
|
258
|
+
lines.push(`- 真实库:\`${isolation.realRoot || '(未配置)'}\``);
|
|
259
|
+
if (isolation.ignored && isolation.ignored.length) {
|
|
260
|
+
lines.push(` - 排除:${isolation.ignored.map((item) => `\`${item}\``).join('、')}(宿主存活心跳,不是被测规则的写入)`);
|
|
261
|
+
}
|
|
262
|
+
if (isolation.before && isolation.after) {
|
|
263
|
+
lines.push(` - before:${isolation.before.files} files / ${isolation.before.bytes} bytes / \`${isolation.before.sha256}\``);
|
|
264
|
+
lines.push(` - after :${isolation.after.files} files / ${isolation.after.bytes} bytes / \`${isolation.after.sha256}\``);
|
|
265
|
+
lines.push(` - 一致:${isolation.before.sha256 === isolation.after.sha256}`);
|
|
266
|
+
}
|
|
267
|
+
lines.push(`- 沙盒树:${isolation.sandboxTree.files} files / ${isolation.sandboxTree.bytes} bytes / \`${isolation.sandboxTree.sha256.slice(0, 16)}…\``);
|
|
268
|
+
lines.push(`- J0:${isolation.ok ? 'PASS' : 'FAIL'}${isolation.blocked ? '(BLOCKED:未配置真实库,不能证明真实库 hash 不变)' : ''}`);
|
|
269
|
+
for (const error of isolation.errors) lines.push(` - ${error.code}: ${error.message}`);
|
|
270
|
+
lines.push('');
|
|
271
|
+
lines.push('## override 审计');
|
|
272
|
+
lines.push('');
|
|
273
|
+
for (const item of runs.overrides) {
|
|
274
|
+
lines.push(`- ${item.caseId}:${item.status},生效 ${item.overrideAudit.applied} 条,被拒 ${item.overrideAudit.rejected} 条${item.errors.length ? `,失败码 ${item.errors.map((error) => error.code).join(', ')}` : ''}`);
|
|
275
|
+
for (const record of item.overrides) {
|
|
276
|
+
lines.push(` - ${record.ruleId} · 理由「${record.reason}」· ${record.by} · ${record.at}`);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
lines.push(`- decisions.jsonl 审计:${audit.ok ? `PASS(${audit.records} 条 override 记录字段齐全)` : `FAIL(${audit.message})`}`);
|
|
280
|
+
lines.push('');
|
|
281
|
+
lines.push('## 失败码表');
|
|
282
|
+
lines.push('');
|
|
283
|
+
lines.push('| code | 触发 |');
|
|
284
|
+
lines.push('|---|---|');
|
|
285
|
+
lines.push(`| \`${ERROR_CODES.ISOLATION}\` | 沙盒外写入 / DSH_MEM 越出沙盒 / 真实库 hash 变化 |`);
|
|
286
|
+
lines.push(`| \`${ERROR_CODES.SHAPE}\` | 期望单条却给出多条,或没有决定 |`);
|
|
287
|
+
lines.push(`| \`${ERROR_CODES.ORDER}\` | 多条决定的条数或顺序与期望不符 |`);
|
|
288
|
+
lines.push(`| \`${ERROR_CODES.ACTION}\` | 决定里的 action 不在允许集 |`);
|
|
289
|
+
lines.push(`| \`${ERROR_CODES.REFUSAL}\` | 越界请求没有拒绝,或拒绝时仍带写入 |`);
|
|
290
|
+
lines.push(`| \`${ERROR_CODES.EVIDENCE}\` | 决定缺证据 / 证据不可解析 / 用了 ctx 字段却没留痕 |`);
|
|
291
|
+
lines.push(`| \`${ERROR_CODES.OVERRIDE_UNJUSTIFIED}\` | override 无理由,或理由为空仍想生效 |`);
|
|
292
|
+
lines.push(`| \`${ERROR_CODES.SCOPE}\` | writeScope 越界(盘符/绝对/../~/不在允许前缀) |`);
|
|
293
|
+
lines.push(`| \`${ERROR_CODES.AUDIT}\` | decisions.jsonl 缺 overrides 或记录缺理由/谁/时间戳 |`);
|
|
294
|
+
lines.push(`| \`${ERROR_CODES.HUMAN}\` | needsHuman 与审批类决定不一致,H 级用例未升级 |`);
|
|
295
|
+
lines.push(`| \`${ERROR_CODES.POLICY}\` | policyRef 解析不到标题,或 policyTitle/policyQuote 对不上 |`);
|
|
296
|
+
lines.push(`| \`${ERROR_CODES.LEVEL}\` | level 不在 X/D/H,或与 §5.7 执行矩阵不一致 |`);
|
|
297
|
+
lines.push(`| \`${ERROR_CODES.CONTRACT}\` | 输入/输出/fixture 结构不满足契约 |`);
|
|
298
|
+
lines.push(`| \`${ERROR_CODES.DETERMINISM}\` | 同一输入两次输出不一致 |`);
|
|
299
|
+
lines.push(`| \`${ERROR_CODES.FORBIDDEN}\` | 命中禁用动作 |`);
|
|
300
|
+
lines.push(`| \`${ERROR_CODES.BRIEF}\` | 任务书缺要素(派活类用例) |`);
|
|
301
|
+
lines.push('');
|
|
302
|
+
return lines.join('\n');
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
// ── main ─────────────────────────────────────────────────────
|
|
306
|
+
//
|
|
307
|
+
// 事务标记:开跑先写 <out>/.run-sim.lock(pid / 开始时间 / 目标目录 / 真实库),
|
|
308
|
+
// 成功(SIM-SUMMARY.md 已落盘)才删锁;崩溃、异常或判定失败都保留锁,并把 state:'failed'
|
|
309
|
+
// 与 exitCode 写进锁。下游只认这把锁:锁在 = 这次 run-sim 没有善终,不许用「没有
|
|
310
|
+
// SIM-SUMMARY.md」这种间接证据推断失败。
|
|
311
|
+
|
|
312
|
+
const LOCK_NAME = '.run-sim.lock';
|
|
313
|
+
|
|
314
|
+
function writeLock(outDir, lock) {
|
|
315
|
+
const file = path.join(outDir, LOCK_NAME);
|
|
316
|
+
fs.writeFileSync(file, `${JSON.stringify(lock, null, 2)}\n`, 'utf8');
|
|
317
|
+
return file;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
function markLockFailed(outDir, lock, error) {
|
|
321
|
+
const failed = {
|
|
322
|
+
...lock,
|
|
323
|
+
state: 'failed',
|
|
324
|
+
exitCode: 1,
|
|
325
|
+
failedAt: new Date().toISOString(),
|
|
326
|
+
error: error && error.message ? error.message : String(error || 'run-sim failed'),
|
|
327
|
+
};
|
|
328
|
+
try {
|
|
329
|
+
writeLock(outDir, failed);
|
|
330
|
+
} catch (writeError) {
|
|
331
|
+
// 锁都更新不了时,旧的 running 锁仍在盘上(存在即失败);原因写到 stderr。
|
|
332
|
+
process.stderr.write(`run-sim 失败但锁文件更新失败:${writeError.message}\n`);
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
export function main(argv = process.argv.slice(2), io = { out: (text) => process.stdout.write(text), err: (text) => process.stderr.write(text) }) {
|
|
337
|
+
const args = parseArgs(argv);
|
|
338
|
+
if (args.help) {
|
|
339
|
+
io.out('node src/router/run-sim.mjs [--out <dir>] [--real-memory <dir>] [--no-real-memory] [--json]\n');
|
|
340
|
+
return 0;
|
|
341
|
+
}
|
|
342
|
+
const outDir = args.out;
|
|
343
|
+
fs.mkdirSync(outDir, { recursive: true });
|
|
344
|
+
const lock = {
|
|
345
|
+
pid: process.pid,
|
|
346
|
+
startedAt: new Date().toISOString(),
|
|
347
|
+
target: path.resolve(outDir),
|
|
348
|
+
realMemory: args.realMemory || null,
|
|
349
|
+
node: process.version,
|
|
350
|
+
state: 'running',
|
|
351
|
+
};
|
|
352
|
+
const lockFile = writeLock(outDir, lock);
|
|
353
|
+
let code;
|
|
354
|
+
try {
|
|
355
|
+
code = simulate(args, io);
|
|
356
|
+
} catch (error) {
|
|
357
|
+
markLockFailed(outDir, lock, error);
|
|
358
|
+
throw error;
|
|
359
|
+
}
|
|
360
|
+
if (code !== 0) {
|
|
361
|
+
markLockFailed(outDir, lock, new Error('run-sim 判定失败(详见 stdout 的 summary.failed)'));
|
|
362
|
+
return code;
|
|
363
|
+
}
|
|
364
|
+
// 成功路径:SIM-SUMMARY.md 已经在 simulate() 里写完,锁是最后一步。删锁后下游才认这次通过。
|
|
365
|
+
fs.rmSync(lockFile, { force: true });
|
|
366
|
+
return 0;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
function simulate(args, io) {
|
|
370
|
+
const design = loadDesign(args);
|
|
371
|
+
const loaded = loadFixtures(FIXTURES_DIR);
|
|
372
|
+
if (loaded.errors.length) {
|
|
373
|
+
io.err(`${JSON.stringify(loaded.errors, null, 2)}\n`);
|
|
374
|
+
return 1;
|
|
375
|
+
}
|
|
376
|
+
const fixtures = loaded.fixtures;
|
|
377
|
+
|
|
378
|
+
const outDir = args.out;
|
|
379
|
+
fs.mkdirSync(outDir, { recursive: true });
|
|
380
|
+
|
|
381
|
+
const sandbox = prepareSandbox();
|
|
382
|
+
const sandboxTree = hashTree(sandbox);
|
|
383
|
+
|
|
384
|
+
const realRoot = args.realMemory && fs.existsSync(args.realMemory) ? args.realMemory : null;
|
|
385
|
+
// 宿主的存活心跳(.longlive/live/)由宿主自己写,不是被测规则的写入,hash 时排除。
|
|
386
|
+
const realIgnore = ['.longlive/live'];
|
|
387
|
+
const realBefore = realRoot ? hashTree(realRoot, { ignore: realIgnore }) : null;
|
|
388
|
+
|
|
389
|
+
const startedAt = process.hrtime.bigint();
|
|
390
|
+
const run1 = buildRun(fixtures, design);
|
|
391
|
+
const run1End = process.hrtime.bigint();
|
|
392
|
+
const run2 = buildRun(fixtures, design);
|
|
393
|
+
const run2End = process.hrtime.bigint();
|
|
394
|
+
|
|
395
|
+
const realAfter = realRoot ? hashTree(realRoot, { ignore: realIgnore }) : null;
|
|
396
|
+
|
|
397
|
+
const auditLines1 = buildRunAuditLines(run1);
|
|
398
|
+
const auditLines2 = buildRunAuditLines(run2);
|
|
399
|
+
const audit1 = judgeAuditLines(auditLines1);
|
|
400
|
+
const audit2 = judgeAuditLines(auditLines2);
|
|
401
|
+
const decisionsText1 = auditLines1.map((line) => JSON.stringify(line)).join('\n');
|
|
402
|
+
const decisionsText2 = auditLines2.map((line) => JSON.stringify(line)).join('\n');
|
|
403
|
+
const decisionsIdentical = decisionsText1 === decisionsText2;
|
|
404
|
+
|
|
405
|
+
const policy = judgePolicy(RULES, design);
|
|
406
|
+
const controls = negativeControls(fixtures, design);
|
|
407
|
+
|
|
408
|
+
const isolation = judgeJ0({
|
|
409
|
+
sandbox,
|
|
410
|
+
realRoot,
|
|
411
|
+
before: realBefore,
|
|
412
|
+
after: realAfter,
|
|
413
|
+
env: { DSH_MEM: path.join(sandbox, 'memory') },
|
|
414
|
+
sandboxTree,
|
|
415
|
+
expectedArtifacts: ['memory/', 'evidence/'],
|
|
416
|
+
});
|
|
417
|
+
|
|
418
|
+
const fixtureHash = hashTree(FIXTURES_DIR).sha256;
|
|
419
|
+
const rulesHash = sha256File(RULES_FILE);
|
|
420
|
+
|
|
421
|
+
const resultsPayload = {
|
|
422
|
+
design: { doc: design.doc, snapshot: Boolean(design.snapshot), sourceSha256: design.sourceSha256 || null },
|
|
423
|
+
rules: { count: RULES.length, sha256: rulesHash },
|
|
424
|
+
fixtures: { count: fixtures.length, sha256: fixtureHash },
|
|
425
|
+
cases: run1.cases,
|
|
426
|
+
overrideCases: run1.overrides,
|
|
427
|
+
negativeControls: controls,
|
|
428
|
+
};
|
|
429
|
+
const resultsPayload2 = {
|
|
430
|
+
design: { doc: design.doc, snapshot: Boolean(design.snapshot), sourceSha256: design.sourceSha256 || null },
|
|
431
|
+
rules: { count: RULES.length, sha256: rulesHash },
|
|
432
|
+
fixtures: { count: fixtures.length, sha256: fixtureHash },
|
|
433
|
+
cases: run2.cases,
|
|
434
|
+
overrideCases: run2.overrides,
|
|
435
|
+
negativeControls: negativeControls(fixtures, design),
|
|
436
|
+
};
|
|
437
|
+
const results1 = `${JSON.stringify(resultsPayload, null, 2)}\n`;
|
|
438
|
+
const results2 = `${JSON.stringify(resultsPayload2, null, 2)}\n`;
|
|
439
|
+
const comparison = compareText(results1, results2);
|
|
440
|
+
|
|
441
|
+
fs.writeFileSync(path.join(outDir, 'results-run1.json'), results1);
|
|
442
|
+
fs.writeFileSync(path.join(outDir, 'results-run2.json'), results2);
|
|
443
|
+
fs.writeFileSync(path.join(outDir, 'decisions-run1.jsonl'), auditLines1.map((line) => JSON.stringify(line)).join('\n') + '\n');
|
|
444
|
+
fs.writeFileSync(path.join(outDir, 'decisions-run2.jsonl'), auditLines2.map((line) => JSON.stringify(line)).join('\n') + '\n');
|
|
445
|
+
fs.writeFileSync(path.join(outDir, 'decisions.jsonl'), auditLines1.map((line) => JSON.stringify(line)).join('\n') + '\n');
|
|
446
|
+
fs.writeFileSync(path.join(outDir, 'timings.json'), `${JSON.stringify({
|
|
447
|
+
run1Ms: Number((run1End - startedAt) / 1000000n),
|
|
448
|
+
run2Ms: Number((run2End - run1End) / 1000000n),
|
|
449
|
+
realMemoryHashMs: null,
|
|
450
|
+
}, null, 2)}\n`);
|
|
451
|
+
fs.writeFileSync(path.join(outDir, 'isolation.json'), `${JSON.stringify({
|
|
452
|
+
sandbox,
|
|
453
|
+
dshMem: path.join(sandbox, 'memory'),
|
|
454
|
+
realRoot,
|
|
455
|
+
before: realBefore,
|
|
456
|
+
after: realAfter,
|
|
457
|
+
sandboxTree,
|
|
458
|
+
ok: isolation.ok,
|
|
459
|
+
blocked: isolation.blocked,
|
|
460
|
+
errors: isolation.errors,
|
|
461
|
+
}, null, 2)}\n`);
|
|
462
|
+
|
|
463
|
+
const report = reportMarkdown({
|
|
464
|
+
design,
|
|
465
|
+
policy,
|
|
466
|
+
runs: run1,
|
|
467
|
+
comparison,
|
|
468
|
+
audit: {
|
|
469
|
+
ok: audit1.ok && audit2.ok,
|
|
470
|
+
records: audit1.ok ? audit1.records.length : 0,
|
|
471
|
+
message: audit1.ok ? '' : audit1.message,
|
|
472
|
+
identical: decisionsIdentical,
|
|
473
|
+
run1: { sha256: sha256Text(decisionsText1) },
|
|
474
|
+
run2: { sha256: sha256Text(decisionsText2) },
|
|
475
|
+
},
|
|
476
|
+
isolation: {
|
|
477
|
+
...isolation,
|
|
478
|
+
sandbox,
|
|
479
|
+
realRoot,
|
|
480
|
+
dshMem: path.join(sandbox, 'memory'),
|
|
481
|
+
dshMemInsideSandbox: true,
|
|
482
|
+
ignored: realIgnore,
|
|
483
|
+
},
|
|
484
|
+
controls,
|
|
485
|
+
fixtureHash,
|
|
486
|
+
rulesHash,
|
|
487
|
+
});
|
|
488
|
+
fs.writeFileSync(path.join(outDir, 'SIM-SUMMARY.md'), report);
|
|
489
|
+
|
|
490
|
+
const fixturesPassed = run1.cases.filter((item) => item.status === 'PASS').length;
|
|
491
|
+
const failed = [];
|
|
492
|
+
if (fixturesPassed !== fixtures.length) failed.push(`fixtures ${fixturesPassed}/${fixtures.length}`);
|
|
493
|
+
if (!comparison.identical) failed.push('两遍结果不一致');
|
|
494
|
+
if (!audit1.ok || !audit2.ok) failed.push(`审计失败:${audit1.message || audit2.message}`);
|
|
495
|
+
if (!decisionsIdentical) failed.push('两遍 decisions 不一致');
|
|
496
|
+
if (!policy.ok) failed.push(`policy 解析失败 ${policy.errors.length} 条`);
|
|
497
|
+
if (!isolation.ok) failed.push('J0 隔离失败');
|
|
498
|
+
if (controls.some((item) => !item.caught)) failed.push('judge 负控未拒');
|
|
499
|
+
|
|
500
|
+
const summary = {
|
|
501
|
+
ok: failed.length === 0,
|
|
502
|
+
fixtures: `${fixturesPassed}/${fixtures.length} PASS`,
|
|
503
|
+
doubleRun: comparison.identical ? 'byte-identical' : 'MISMATCH',
|
|
504
|
+
resultsSha256: comparison.sha256.run1,
|
|
505
|
+
decisionsSha256: sha256Text(decisionsText1),
|
|
506
|
+
isolation: isolation.ok ? (isolation.blocked ? 'PASS(blocked: no real library)' : 'PASS') : 'FAIL',
|
|
507
|
+
policy: policy.ok ? `${RULES.length}/${RULES.length}` : 'FAIL',
|
|
508
|
+
negativeControls: `${controls.filter((item) => item.caught).length}/${controls.length}`,
|
|
509
|
+
outDir,
|
|
510
|
+
failed,
|
|
511
|
+
};
|
|
512
|
+
io.out(`${JSON.stringify(summary, null, 2)}\n`);
|
|
513
|
+
return summary.ok ? 0 : 1;
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
function buildRunAuditLines(run) {
|
|
517
|
+
return [...run.cases, ...run.overrides].map(caseToAuditLine);
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
const invokedDirectly = process.argv[1]
|
|
521
|
+
&& pathToFileURL(path.resolve(process.argv[1])).href === import.meta.url;
|
|
522
|
+
|
|
523
|
+
if (invokedDirectly) {
|
|
524
|
+
try {
|
|
525
|
+
process.exitCode = main();
|
|
526
|
+
} catch (error) {
|
|
527
|
+
process.stderr.write(`run-sim 崩了:${error && error.stack ? error.stack : error}\n`);
|
|
528
|
+
process.exitCode = 1;
|
|
529
|
+
}
|
|
530
|
+
}
|