@jjh32456/longlive 0.0.0-stage → 1.8.12
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 +205 -0
- package/LICENSE +21 -0
- package/QUICKSTART.md +363 -0
- package/README.en.md +55 -0
- package/README.md +225 -2
- package/SPEC.md +211 -0
- package/build-info.json +1555 -0
- package/cordis.patch.yml +177 -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 +807 -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 +46 -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 +160 -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-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/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 +146 -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 +824 -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 +912 -0
- package/src/handoff/index.js +944 -0
- package/src/handoff/model.js +1010 -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 +657 -0
- package/src/identity/index.js +337 -0
- package/src/identity/inject-position.js +131 -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 +124 -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/audit-records.js +222 -0
- package/src/memory/auto-cards.js +441 -0
- package/src/memory/capture-host.js +228 -0
- package/src/memory/capture.js +457 -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/conclusion-distill.js +332 -0
- package/src/memory/conclusion-drafts.js +239 -0
- package/src/memory/conflicts.js +147 -0
- package/src/memory/consolidate.js +815 -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 +410 -0
- package/src/memory/forget.js +512 -0
- package/src/memory/frames-activation.js +247 -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 +362 -0
- package/src/memory/l2-gate.js +169 -0
- package/src/memory/lifecycle.js +107 -0
- package/src/memory/lock.js +121 -0
- package/src/memory/maintenance.js +785 -0
- package/src/memory/manifest.js +245 -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.js +963 -0
- package/src/memory/provenance.js +119 -0
- package/src/memory/recall-v2.js +206 -0
- package/src/memory/recall.js +256 -0
- package/src/memory/reconcile.js +265 -0
- package/src/memory/records.js +433 -0
- package/src/memory/reinforce.js +145 -0
- package/src/memory/retention.js +128 -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/version-chain.js +264 -0
- package/src/memory/wal.js +137 -0
- package/src/memory/working-frame.js +516 -0
- package/src/metrics/classify.mjs +189 -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-fallback.mjs +235 -0
- package/src/recall/archive-search.mjs +425 -0
- package/src/recall/cards.mjs +202 -0
- package/src/recall/edges.js +379 -0
- package/src/recall/embedding.mjs +698 -0
- package/src/recall/entities.mjs +112 -0
- package/src/recall/explain.mjs +433 -0
- package/src/recall/force-policy.mjs +138 -0
- package/src/recall/index.js +1036 -0
- package/src/recall/inject-order.mjs +277 -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 +765 -0
- package/src/recall/shared-visibility.js +302 -0
- package/src/recall/time-normalizer.mjs +196 -0
- package/src/retention/config.js +106 -0
- package/src/retention/index.js +643 -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 +3146 -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,245 @@
|
|
|
1
|
+
// Card manifest: one small JSON file that says what the card store looked like
|
|
2
|
+
// when it was last written.
|
|
3
|
+
//
|
|
4
|
+
// B3 asks for `<memoryRoot>/.longlive/manifest.json` with, per card,
|
|
5
|
+
// `{path, sha256, written_at, source}`, plus a verify function a startup check
|
|
6
|
+
// can call. The manifest is a *snapshot*, not an authority: a card whose bytes
|
|
7
|
+
// no longer match its recorded sha256 is reported as `changed`, never rewritten.
|
|
8
|
+
//
|
|
9
|
+
// Why this shape:
|
|
10
|
+
// * `path` is POSIX-relative to memoryRoot, so the file is portable and does
|
|
11
|
+
// not leak an absolute Windows path;
|
|
12
|
+
// * `sha256` is over the UTF-8 bytes, exactly like `sha256File`;
|
|
13
|
+
// * `written_at`/`source` come from the B3 provenance extras when present;
|
|
14
|
+
// for legacy cards the file mtime is the only honest fallback;
|
|
15
|
+
// * cards that could not be parsed are still tracked (sha over the raw text)
|
|
16
|
+
// so an edit to a broken card is visible, not invisible.
|
|
17
|
+
//
|
|
18
|
+
// The manifest itself is never written by `verifyManifest`; rebuilding is an
|
|
19
|
+
// explicit `refreshManifest` call. That keeps verification read-only.
|
|
20
|
+
|
|
21
|
+
import fs from 'node:fs';
|
|
22
|
+
import path from 'node:path';
|
|
23
|
+
|
|
24
|
+
import { memoryError, relativePath, resolveInside } from './paths.js';
|
|
25
|
+
import { parseFrontmatter } from './frontmatter.js';
|
|
26
|
+
import {
|
|
27
|
+
LONGLIVE_DIR, listMarkdownFiles, readJsonFile, sha256File, sha256Text, writeFileAtomic,
|
|
28
|
+
} from './state.js';
|
|
29
|
+
import { readProvenance } from './provenance.js';
|
|
30
|
+
|
|
31
|
+
export const MANIFEST_FILE = 'manifest.json';
|
|
32
|
+
export const MANIFEST_VERSION = 1;
|
|
33
|
+
|
|
34
|
+
/** Directories scanned for cards. Kept here so cards.js and tests share one list. */
|
|
35
|
+
export const CARD_DIRS = Object.freeze(['topics', 'journal', 'procedures']);
|
|
36
|
+
|
|
37
|
+
/** `<memoryRoot>/.longlive/manifest.json`. */
|
|
38
|
+
export function manifestPath(root) {
|
|
39
|
+
return path.join(root, LONGLIVE_DIR, MANIFEST_FILE);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function clean(value) {
|
|
43
|
+
return typeof value === 'string' && value.trim() !== '' ? value.trim() : null;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function fileIso(abs) {
|
|
47
|
+
try {
|
|
48
|
+
return fs.statSync(abs).mtime.toISOString();
|
|
49
|
+
} catch {
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* One manifest row for an existing card.
|
|
56
|
+
*
|
|
57
|
+
* A card with an unterminated frontmatter block still gets a row: `sha256` is
|
|
58
|
+
* read over the raw bytes and `source` is null. Hiding it would let an edit to
|
|
59
|
+
* a broken card go unnoticed.
|
|
60
|
+
*/
|
|
61
|
+
export function manifestEntry(root, abs, { now = new Date() } = {}) {
|
|
62
|
+
const rel = relativePath(root, abs);
|
|
63
|
+
const text = fs.readFileSync(abs, 'utf8');
|
|
64
|
+
let extras = {};
|
|
65
|
+
try {
|
|
66
|
+
extras = parseFrontmatter(text).extras;
|
|
67
|
+
} catch {
|
|
68
|
+
// A broken card is still tracked by its bytes; provenance is unavailable.
|
|
69
|
+
extras = {};
|
|
70
|
+
}
|
|
71
|
+
const provenance = readProvenance(extras);
|
|
72
|
+
const source = provenance.session === null && provenance.turn === null
|
|
73
|
+
? null
|
|
74
|
+
: { session: provenance.session, turn: provenance.turn };
|
|
75
|
+
const writtenAt = provenance.written_at ?? fileIso(abs) ?? now.toISOString();
|
|
76
|
+
return {
|
|
77
|
+
path: rel,
|
|
78
|
+
sha256: sha256Text(text),
|
|
79
|
+
written_at: writtenAt,
|
|
80
|
+
source,
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function byPath(left, right) {
|
|
85
|
+
if (left.path < right.path) return -1;
|
|
86
|
+
if (left.path > right.path) return 1;
|
|
87
|
+
return 0;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** Build the manifest object without touching the disk. */
|
|
91
|
+
export function buildManifest(root, { now = new Date() } = {}) {
|
|
92
|
+
const cards = [];
|
|
93
|
+
const warnings = [];
|
|
94
|
+
for (const dir of CARD_DIRS) {
|
|
95
|
+
for (const abs of listMarkdownFiles(root, dir)) {
|
|
96
|
+
try {
|
|
97
|
+
cards.push(manifestEntry(root, abs, { now }));
|
|
98
|
+
} catch (error) {
|
|
99
|
+
warnings.push({ path: relativePath(root, abs), message: error?.message ?? String(error) });
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
cards.sort(byPath);
|
|
104
|
+
return {
|
|
105
|
+
version: MANIFEST_VERSION,
|
|
106
|
+
generated_at: now.toISOString(),
|
|
107
|
+
cards,
|
|
108
|
+
...(warnings.length > 0 ? { warnings } : {}),
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** Write a manifest object atomically and prove the bytes read back. */
|
|
113
|
+
export function writeManifest(root, manifest, { now = new Date() } = {}) {
|
|
114
|
+
if (manifest === null || typeof manifest !== 'object' || Array.isArray(manifest)) {
|
|
115
|
+
throw memoryError('E_BAD_ARGUMENT', 'writeManifest 需要 manifest 对象');
|
|
116
|
+
}
|
|
117
|
+
const text = `${JSON.stringify(manifest, null, 2)}\n`;
|
|
118
|
+
const written = writeFileAtomic(root, manifestPath(root), text, { backup: false, now });
|
|
119
|
+
return { ...written, manifest };
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** Rebuild the manifest from every card on disk and write it. */
|
|
123
|
+
export function refreshManifest(root, { now = new Date() } = {}) {
|
|
124
|
+
const manifest = buildManifest(root, { now });
|
|
125
|
+
return writeManifest(root, manifest, { now });
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Read `.longlive/manifest.json`. A missing file returns null; malformed JSON
|
|
130
|
+
* throws `E_MANIFEST_CORRUPT` because pretending an unreadable manifest is an
|
|
131
|
+
* empty one would make the next verification silently pass.
|
|
132
|
+
*/
|
|
133
|
+
export function readManifest(root) {
|
|
134
|
+
const file = manifestPath(root);
|
|
135
|
+
const raw = readJsonFile(file, 'E_MANIFEST_CORRUPT', file);
|
|
136
|
+
if (raw == null) return null;
|
|
137
|
+
if (raw === null || typeof raw !== 'object' || Array.isArray(raw)) {
|
|
138
|
+
throw memoryError('E_MANIFEST_CORRUPT', `manifest 必须是对象:${file}`);
|
|
139
|
+
}
|
|
140
|
+
if (!Array.isArray(raw.cards)) {
|
|
141
|
+
throw memoryError('E_MANIFEST_CORRUPT', `manifest.cards 必须是数组:${file}`);
|
|
142
|
+
}
|
|
143
|
+
return {
|
|
144
|
+
version: Number.isInteger(raw.version) ? raw.version : MANIFEST_VERSION,
|
|
145
|
+
generated_at: typeof raw.generated_at === 'string' ? raw.generated_at : null,
|
|
146
|
+
cards: raw.cards,
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function diskCards(root) {
|
|
151
|
+
const found = new Map();
|
|
152
|
+
for (const dir of CARD_DIRS) {
|
|
153
|
+
for (const abs of listMarkdownFiles(root, dir)) {
|
|
154
|
+
found.set(relativePath(root, abs), abs);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
return found;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Compare the manifest in memory with the cards on disk.
|
|
162
|
+
*
|
|
163
|
+
* Returns `{ok, checked, changed, missing, untracked, invalid, manifestPath}`.
|
|
164
|
+
* `changed` carries both hashes, because "the card changed" is only actionable
|
|
165
|
+
* when the caller can see what it was expected to be.
|
|
166
|
+
*
|
|
167
|
+
* Read-only: this function never writes the manifest.
|
|
168
|
+
*/
|
|
169
|
+
export function verifyManifest(root, { manifest = undefined } = {}) {
|
|
170
|
+
const file = manifestPath(root);
|
|
171
|
+
const base = {
|
|
172
|
+
ok: false,
|
|
173
|
+
checked: 0,
|
|
174
|
+
changed: [],
|
|
175
|
+
missing: [],
|
|
176
|
+
untracked: [],
|
|
177
|
+
invalid: [],
|
|
178
|
+
manifestPath: file,
|
|
179
|
+
};
|
|
180
|
+
let current;
|
|
181
|
+
try {
|
|
182
|
+
current = manifest === undefined ? readManifest(root) : manifest;
|
|
183
|
+
} catch (error) {
|
|
184
|
+
return { ...base, code: error?.code ?? 'E_MANIFEST_CORRUPT', message: error?.message ?? String(error) };
|
|
185
|
+
}
|
|
186
|
+
if (current == null) {
|
|
187
|
+
// A missing manifest cannot prove anything. Every card on disk is untracked
|
|
188
|
+
// rather than "fine": that is the honest answer for a startup check.
|
|
189
|
+
const untracked = [...diskCards(root).keys()].sort();
|
|
190
|
+
return {
|
|
191
|
+
...base,
|
|
192
|
+
code: 'E_MANIFEST_MISSING',
|
|
193
|
+
message: `manifest 不存在:${file} — 先跑一次 refreshManifest 建立基线`,
|
|
194
|
+
untracked,
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const rows = Array.isArray(current.cards) ? current.cards : [];
|
|
199
|
+
const tracked = new Set();
|
|
200
|
+
for (const [index, row] of rows.entries()) {
|
|
201
|
+
if (row === null || typeof row !== 'object' || Array.isArray(row)) {
|
|
202
|
+
base.invalid.push({ index, reason: 'row is not an object' });
|
|
203
|
+
continue;
|
|
204
|
+
}
|
|
205
|
+
const rel = typeof row.path === 'string' ? row.path.trim() : '';
|
|
206
|
+
const expected = typeof row.sha256 === 'string' ? row.sha256.trim().toLowerCase() : '';
|
|
207
|
+
if (rel === '' || expected === '') {
|
|
208
|
+
base.invalid.push({ index, path: rel || null, reason: 'path/sha256 missing' });
|
|
209
|
+
continue;
|
|
210
|
+
}
|
|
211
|
+
if (tracked.has(rel)) {
|
|
212
|
+
base.invalid.push({ index, path: rel, reason: 'duplicate manifest row' });
|
|
213
|
+
continue;
|
|
214
|
+
}
|
|
215
|
+
tracked.add(rel);
|
|
216
|
+
let abs;
|
|
217
|
+
try {
|
|
218
|
+
abs = resolveInside(root, rel);
|
|
219
|
+
} catch (error) {
|
|
220
|
+
base.invalid.push({ index, path: rel, reason: error?.message ?? String(error) });
|
|
221
|
+
continue;
|
|
222
|
+
}
|
|
223
|
+
if (!fs.existsSync(abs)) {
|
|
224
|
+
base.missing.push({ path: rel, expected_sha256: expected });
|
|
225
|
+
continue;
|
|
226
|
+
}
|
|
227
|
+
base.checked += 1;
|
|
228
|
+
const actual = sha256File(abs);
|
|
229
|
+
if (actual !== expected) {
|
|
230
|
+
base.changed.push({ path: rel, expected_sha256: expected, actual_sha256: actual });
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
for (const rel of diskCards(root).keys()) {
|
|
235
|
+
if (!tracked.has(rel)) base.untracked.push(rel);
|
|
236
|
+
}
|
|
237
|
+
base.untracked.sort();
|
|
238
|
+
base.ok = base.changed.length === 0
|
|
239
|
+
&& base.missing.length === 0
|
|
240
|
+
&& base.untracked.length === 0
|
|
241
|
+
&& base.invalid.length === 0
|
|
242
|
+
&& base.code === undefined;
|
|
243
|
+
base.consistent = base.ok;
|
|
244
|
+
return base;
|
|
245
|
+
}
|