@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,139 @@
|
|
|
1
|
+
// src/memory/migrate.js — 1.8.0 / v2.8 §16.6(A1 分片):records.jsonl → claims.jsonl 迁移
|
|
2
|
+
//
|
|
3
|
+
// 规则(写死):
|
|
4
|
+
// * 旧记录 sourceTier 缺失 → host(v2.8 迁移口径);subject 缺失 → {entity: record.id};
|
|
5
|
+
// evidence 空;state 缺失 → active;隐式 evidence=[];legacy:true + audit.migratedFrom。
|
|
6
|
+
// * 迁移不增不减:records 行数 === 迁移后 claims 行数(同一批旧数据)。
|
|
7
|
+
// * 幂等:同 id 的 claim 已存在 → skip(重复 apply 条数不变)。
|
|
8
|
+
// * 不物理删除:apply 前把 records.jsonl 备份到 `.longlive/backups/`(sha256 记录)。
|
|
9
|
+
// * 默认 dry-run;--apply 才写。
|
|
10
|
+
|
|
11
|
+
import crypto from 'node:crypto';
|
|
12
|
+
import fs from 'node:fs';
|
|
13
|
+
import path from 'node:path';
|
|
14
|
+
|
|
15
|
+
import { claimFromLegacyRecord, claimsPathOf, readClaimsForHost } from './governance.js';
|
|
16
|
+
|
|
17
|
+
export function migrateBackupPath(memoryRoot, now = () => new Date()) {
|
|
18
|
+
const stamp = (now instanceof Date ? now : new Date()).toISOString().replace(/[:.]/g, '-');
|
|
19
|
+
return path.join(path.resolve(String(memoryRoot)), '.longlive', 'backups', `records-${stamp}.jsonl.bak`);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function sha256Buf(buf) {
|
|
23
|
+
return crypto.createHash('sha256').update(buf).digest('hex');
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Migrate legacy records.jsonl into claims.jsonl.
|
|
28
|
+
* @param {object} options
|
|
29
|
+
* @param {string} options.memoryRoot
|
|
30
|
+
* @param {boolean} [options.apply] default false (dry-run)
|
|
31
|
+
* @param {() => Date} [options.now]
|
|
32
|
+
* @returns {object} receipt
|
|
33
|
+
*/
|
|
34
|
+
export function migrateRecordsToClaims({ memoryRoot, apply = false, now = () => new Date() } = {}) {
|
|
35
|
+
const root = path.resolve(String(memoryRoot));
|
|
36
|
+
const recordsFile = path.join(root, '.longlive', 'records.jsonl');
|
|
37
|
+
const claimsFile = claimsPathOf(root);
|
|
38
|
+
const receipt = {
|
|
39
|
+
schema: 'migrate-records-v180/v1',
|
|
40
|
+
memoryRoot: root,
|
|
41
|
+
mode: apply ? 'apply' : 'dry-run',
|
|
42
|
+
recordsFile,
|
|
43
|
+
claimsFile,
|
|
44
|
+
recordsPresent: false,
|
|
45
|
+
recordsTotal: 0,
|
|
46
|
+
mapped: 0,
|
|
47
|
+
duplicates: 0,
|
|
48
|
+
unmappable: [],
|
|
49
|
+
skippedExisting: 0,
|
|
50
|
+
appended: 0,
|
|
51
|
+
recordsSha256: null,
|
|
52
|
+
backup: null,
|
|
53
|
+
beforeClaims: 0,
|
|
54
|
+
afterClaims: 0,
|
|
55
|
+
countMatched: false,
|
|
56
|
+
code: null,
|
|
57
|
+
exitCode: 0,
|
|
58
|
+
};
|
|
59
|
+
let text = '';
|
|
60
|
+
try {
|
|
61
|
+
text = fs.readFileSync(recordsFile, 'utf8');
|
|
62
|
+
receipt.recordsPresent = true;
|
|
63
|
+
} catch {
|
|
64
|
+
text = '';
|
|
65
|
+
}
|
|
66
|
+
const rows = [];
|
|
67
|
+
for (const line of text.split('\n')) {
|
|
68
|
+
if (line.trim() === '') continue;
|
|
69
|
+
try {
|
|
70
|
+
rows.push(JSON.parse(line));
|
|
71
|
+
} catch {
|
|
72
|
+
receipt.unmappable.push({ reason: 'E_JSON', line: line.slice(0, 120) });
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
receipt.recordsTotal = rows.length;
|
|
76
|
+
receipt.recordsSha256 = receipt.recordsPresent ? sha256Buf(Buffer.from(text, 'utf8')) : null;
|
|
77
|
+
// §16.6 / P0-2:没有可迁移记录不是成功 no-op。空/缺 records 必须显式
|
|
78
|
+
// E_NOTHING_TO_MIGRATE(CLI exit 2),countMatched 不得为 true(假绿)。
|
|
79
|
+
if (!receipt.recordsPresent || receipt.recordsTotal === 0) {
|
|
80
|
+
receipt.code = 'E_NOTHING_TO_MIGRATE';
|
|
81
|
+
receipt.exitCode = 2;
|
|
82
|
+
receipt.countMatched = false;
|
|
83
|
+
return receipt;
|
|
84
|
+
}
|
|
85
|
+
const existing = readClaimsForHost({ memoryRoot: root }).items;
|
|
86
|
+
const existingIds = new Set(existing.map((claim) => claim.id));
|
|
87
|
+
receipt.beforeClaims = existing.length;
|
|
88
|
+
const claims = [];
|
|
89
|
+
const seen = new Set();
|
|
90
|
+
for (const row of rows) {
|
|
91
|
+
try {
|
|
92
|
+
const claim = claimFromLegacyRecord(row);
|
|
93
|
+
if (seen.has(claim.id)) {
|
|
94
|
+
receipt.duplicates += 1;
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
seen.add(claim.id);
|
|
98
|
+
claims.push(claim);
|
|
99
|
+
receipt.mapped += 1;
|
|
100
|
+
} catch (error) {
|
|
101
|
+
receipt.unmappable.push({ reason: error?.code ?? 'E_MAP', message: String(error?.message ?? error) });
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
for (const claim of claims) {
|
|
105
|
+
if (existingIds.has(claim.id)) receipt.skippedExisting += 1;
|
|
106
|
+
}
|
|
107
|
+
if (apply && receipt.recordsPresent) {
|
|
108
|
+
fs.mkdirSync(path.dirname(migrateBackupPath(root, now())), { recursive: true });
|
|
109
|
+
const backupFile = migrateBackupPath(root, now());
|
|
110
|
+
// 备份 + 落盘都要 fsync(迁移期间崩溃不产生半行)。
|
|
111
|
+
fs.copyFileSync(recordsFile, backupFile);
|
|
112
|
+
try {
|
|
113
|
+
const bfd = fs.openSync(backupFile, 'r');
|
|
114
|
+
fs.fsyncSync(bfd);
|
|
115
|
+
fs.closeSync(bfd);
|
|
116
|
+
} catch { /* best effort */ }
|
|
117
|
+
receipt.backup = { path: backupFile, sha256: receipt.recordsSha256 };
|
|
118
|
+
fs.mkdirSync(path.dirname(claimsFile), { recursive: true });
|
|
119
|
+
const fd = fs.openSync(claimsFile, 'a');
|
|
120
|
+
try {
|
|
121
|
+
for (const claim of claims) {
|
|
122
|
+
if (existingIds.has(claim.id)) continue;
|
|
123
|
+
fs.writeSync(fd, `${JSON.stringify(claim)}\n`, null, 'utf8');
|
|
124
|
+
fs.fsyncSync(fd);
|
|
125
|
+
existingIds.add(claim.id);
|
|
126
|
+
receipt.appended += 1;
|
|
127
|
+
}
|
|
128
|
+
} finally {
|
|
129
|
+
fs.closeSync(fd);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
const after = readClaimsForHost({ memoryRoot: root }).items;
|
|
133
|
+
receipt.afterClaims = after.length;
|
|
134
|
+
// count consistency: every legacy record has exactly one claim (new ones or pre-existing)
|
|
135
|
+
const legacyClaimIds = new Set(claims.map((claim) => claim.id));
|
|
136
|
+
const presentLegacy = after.filter((claim) => legacyClaimIds.has(claim.id)).length;
|
|
137
|
+
receipt.countMatched = presentLegacy === claims.length;
|
|
138
|
+
return receipt;
|
|
139
|
+
}
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
// Memory-layer path guard.
|
|
2
|
+
//
|
|
3
|
+
// One resolver for every memory tool, so `memoryRoot` and the older `memRoot`
|
|
4
|
+
// key can never point at two different stores. Resolution order:
|
|
5
|
+
//
|
|
6
|
+
// 1. config.memoryRoot
|
|
7
|
+
// 2. config.memRoot (backward-compatible alias)
|
|
8
|
+
// 3. $DSH_LONGLIVE_MEMORY_ROOT
|
|
9
|
+
// 4. $DSH_MEM (the pre-0.7.3 name, still honoured)
|
|
10
|
+
// 5. <DSH home>/longlive-memory (derived default; created on demand)
|
|
11
|
+
//
|
|
12
|
+
// A derived default is created (mkdir -p); an explicitly configured path is
|
|
13
|
+
// NOT: pointing `memoryRoot` at a typo must fail loudly, not silently grow a
|
|
14
|
+
// new empty store next to the intended one. `strictMemoryRoot: true` (or
|
|
15
|
+
// $DSH_LONGLIVE_STRICT_MEMORY_ROOT=1) removes step 5 and restores the old
|
|
16
|
+
// "unset root is an error" behaviour.
|
|
17
|
+
//
|
|
18
|
+
// All other paths (cards, reports, backups) go through `resolveInside`, so a
|
|
19
|
+
// `..` traversal or an absolute path on another drive is rejected with
|
|
20
|
+
// `E_PATH_OUTSIDE_ROOT` before any read or write happens.
|
|
21
|
+
|
|
22
|
+
import fs from 'node:fs';
|
|
23
|
+
import path from 'node:path';
|
|
24
|
+
import { dshHome } from '../home.js';
|
|
25
|
+
|
|
26
|
+
export const PATH_CODES = Object.freeze({
|
|
27
|
+
ROOT_UNSET: 'E_MEMORY_ROOT_UNSET',
|
|
28
|
+
ROOT_MISSING: 'E_MEMORY_ROOT_MISSING',
|
|
29
|
+
ROOT_NOT_DIR: 'E_MEMORY_ROOT_NOT_DIR',
|
|
30
|
+
PATH_OUTSIDE_ROOT: 'E_PATH_OUTSIDE_ROOT',
|
|
31
|
+
BAD_ARGUMENT: 'E_BAD_ARGUMENT',
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
/** Preferred environment variable; the one QUICKSTART/ERRORS document. */
|
|
35
|
+
export const MEMORY_ROOT_ENV = 'DSH_LONGLIVE_MEMORY_ROOT';
|
|
36
|
+
/** Pre-0.7.3 name. Kept so an existing install does not lose its store. */
|
|
37
|
+
export const LEGACY_MEMORY_ROOT_ENV = 'DSH_MEM';
|
|
38
|
+
/** Set to 1/true to disable the derived default. */
|
|
39
|
+
export const STRICT_MEMORY_ROOT_ENV = 'DSH_LONGLIVE_STRICT_MEMORY_ROOT';
|
|
40
|
+
/** Directory created under the DSH home when nothing is configured. */
|
|
41
|
+
export const DEFAULT_MEMORY_DIRNAME = 'longlive-memory';
|
|
42
|
+
|
|
43
|
+
function nonEmpty(value) {
|
|
44
|
+
return typeof value === 'string' && value.trim() !== '' ? value.trim() : '';
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function truthy(value) {
|
|
48
|
+
const text = nonEmpty(value).toLowerCase();
|
|
49
|
+
return text === '1' || text === 'true' || text === 'yes' || text === 'on';
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Describe where a memory root would come from, without touching the disk.
|
|
54
|
+
* Shared with `src/tools.js` so `session_wake` / `lease_*` follow exactly the
|
|
55
|
+
* same chain as `memory_*` (the 0.7.2 split sent one family to a fixed drive
|
|
56
|
+
* and the other to E_MEMORY_ROOT_UNSET on the very same machine).
|
|
57
|
+
*/
|
|
58
|
+
export function memoryRootSetting(config = {}, env = process.env) {
|
|
59
|
+
const fromConfig = nonEmpty(config?.memoryRoot);
|
|
60
|
+
const fromAlias = nonEmpty(config?.memRoot);
|
|
61
|
+
const fromEnv = nonEmpty(env?.[MEMORY_ROOT_ENV]);
|
|
62
|
+
const fromLegacy = nonEmpty(env?.[LEGACY_MEMORY_ROOT_ENV]);
|
|
63
|
+
const explicit = fromConfig || fromAlias || fromEnv || fromLegacy;
|
|
64
|
+
const source = fromConfig ? 'config.memoryRoot'
|
|
65
|
+
: fromAlias ? 'config.memRoot'
|
|
66
|
+
: fromEnv ? MEMORY_ROOT_ENV
|
|
67
|
+
: fromLegacy ? LEGACY_MEMORY_ROOT_ENV
|
|
68
|
+
: 'default';
|
|
69
|
+
const strict = config?.strictMemoryRoot === true || truthy(env?.[STRICT_MEMORY_ROOT_ENV]);
|
|
70
|
+
return { explicit, source, strict };
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** `<DSH home>/longlive-memory`, honouring an explicit `config.home` (tests). */
|
|
74
|
+
export function defaultMemoryRoot(config = {}, env = process.env) {
|
|
75
|
+
const configuredHome = nonEmpty(config?.home);
|
|
76
|
+
const home = configuredHome ? path.resolve(configuredHome) : dshHome(env);
|
|
77
|
+
return path.resolve(home, DEFAULT_MEMORY_DIRNAME);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Resolve the configured memory root.
|
|
82
|
+
*
|
|
83
|
+
* Returns the absolute path of an existing directory. Throws a coded error
|
|
84
|
+
* (`E_MEMORY_ROOT_UNSET` / `E_MEMORY_ROOT_MISSING` / `E_MEMORY_ROOT_NOT_DIR`)
|
|
85
|
+
* whose message names the next action, never a bare ENOENT.
|
|
86
|
+
*/
|
|
87
|
+
export function memoryRootFromConfig(config = {}, env = process.env) {
|
|
88
|
+
const setting = memoryRootSetting(config, env);
|
|
89
|
+
let root;
|
|
90
|
+
if (setting.explicit) {
|
|
91
|
+
root = path.resolve(setting.explicit);
|
|
92
|
+
} else {
|
|
93
|
+
if (setting.strict) {
|
|
94
|
+
throw memoryError(
|
|
95
|
+
PATH_CODES.ROOT_UNSET,
|
|
96
|
+
'memoryRoot 未配置(strictMemoryRoot 严格模式已开启)— 设置插件配置 memoryRoot、' +
|
|
97
|
+
`${MEMORY_ROOT_ENV}(旧名 ${LEGACY_MEMORY_ROOT_ENV}),或关闭严格模式改用默认目录 ` +
|
|
98
|
+
`<DSH home>/${DEFAULT_MEMORY_DIRNAME}`,
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
root = defaultMemoryRoot(config, env);
|
|
102
|
+
try {
|
|
103
|
+
fs.mkdirSync(root, { recursive: true });
|
|
104
|
+
} catch (error) {
|
|
105
|
+
throw memoryError(
|
|
106
|
+
PATH_CODES.ROOT_MISSING,
|
|
107
|
+
`无法创建默认记忆根目录:${root} — 检查 $DSH_HOME 或用户目录的写权限,` +
|
|
108
|
+
`或显式设置 memoryRoot / ${MEMORY_ROOT_ENV}(旧名 ${LEGACY_MEMORY_ROOT_ENV})`,
|
|
109
|
+
error,
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
let stat = null;
|
|
114
|
+
try {
|
|
115
|
+
stat = fs.statSync(root);
|
|
116
|
+
} catch (error) {
|
|
117
|
+
throw memoryError(
|
|
118
|
+
PATH_CODES.ROOT_MISSING,
|
|
119
|
+
`记忆根目录不存在:${root} — 创建该目录(mkdir -p),或把 memoryRoot / ` +
|
|
120
|
+
`${MEMORY_ROOT_ENV}(旧名 ${LEGACY_MEMORY_ROOT_ENV})指向一个已存在的目录;` +
|
|
121
|
+
'插件不会替你创建显式配置的路径',
|
|
122
|
+
error,
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
if (!stat.isDirectory()) {
|
|
126
|
+
throw memoryError(
|
|
127
|
+
PATH_CODES.ROOT_NOT_DIR,
|
|
128
|
+
`memoryRoot 不是目录:${root} — 指向一个目录(不要指向文件),或删掉同名文件后重试`,
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
return root;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** Build an Error with a stable `.code` the tools and tests can branch on. */
|
|
135
|
+
export function memoryError(code, message, cause) {
|
|
136
|
+
const error = new Error(message);
|
|
137
|
+
error.code = code;
|
|
138
|
+
if (cause !== undefined) error.cause = cause;
|
|
139
|
+
return error;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** True when `target` is `root` itself or lives under it (no `..` escape). */
|
|
143
|
+
export function isInsideRoot(root, target) {
|
|
144
|
+
const rootAbs = path.resolve(root);
|
|
145
|
+
const targetAbs = path.resolve(target);
|
|
146
|
+
const rel = path.relative(rootAbs, targetAbs);
|
|
147
|
+
if (rel === '') return true;
|
|
148
|
+
if (path.isAbsolute(rel)) return false;
|
|
149
|
+
return rel !== '..' && !rel.startsWith(`..${path.sep}`);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Resolve a path through the nearest existing ancestor, so a target that does
|
|
154
|
+
* not exist yet can still be checked against junctions and symlinks already on
|
|
155
|
+
* disk. Returns the real path with the missing tail re-appended.
|
|
156
|
+
*/
|
|
157
|
+
export function realPathProbe(target) {
|
|
158
|
+
const abs = path.resolve(target);
|
|
159
|
+
let existing = abs;
|
|
160
|
+
let tail = [];
|
|
161
|
+
for (;;) {
|
|
162
|
+
try {
|
|
163
|
+
const real = fs.realpathSync.native
|
|
164
|
+
? fs.realpathSync.native(existing)
|
|
165
|
+
: fs.realpathSync(existing);
|
|
166
|
+
return tail.length === 0 ? real : path.join(real, ...tail.reverse());
|
|
167
|
+
} catch (error) {
|
|
168
|
+
if (error?.code !== 'ENOENT' && error?.code !== 'ENOTDIR') throw error;
|
|
169
|
+
const parent = path.dirname(existing);
|
|
170
|
+
if (parent === existing) return abs;
|
|
171
|
+
tail.push(path.basename(existing));
|
|
172
|
+
existing = parent;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Resolve `target` (relative to the root, or absolute) and prove it stays
|
|
179
|
+
* inside `root` both lexically and after resolving every existing junction or
|
|
180
|
+
* symlink on the way. Throws `E_PATH_OUTSIDE_ROOT` for `..`, another drive, an
|
|
181
|
+
* absolute path elsewhere, or a real path that escapes the root; never returns
|
|
182
|
+
* a path the caller may not touch.
|
|
183
|
+
*
|
|
184
|
+
* The lexical path is returned unchanged (callers keep their stable names),
|
|
185
|
+
* but the real-path check runs before any read or write.
|
|
186
|
+
*/
|
|
187
|
+
export function resolveInside(root, target) {
|
|
188
|
+
if (typeof target !== 'string' || target.trim() === '' || target.includes('\0')) {
|
|
189
|
+
throw memoryError(PATH_CODES.BAD_ARGUMENT, `拒绝空路径或非法路径:${JSON.stringify(target)}`);
|
|
190
|
+
}
|
|
191
|
+
const rootAbs = path.resolve(root);
|
|
192
|
+
const targetAbs = path.isAbsolute(target)
|
|
193
|
+
? path.resolve(target)
|
|
194
|
+
: path.resolve(rootAbs, target);
|
|
195
|
+
if (!isInsideRoot(rootAbs, targetAbs)) {
|
|
196
|
+
throw memoryError(
|
|
197
|
+
PATH_CODES.PATH_OUTSIDE_ROOT,
|
|
198
|
+
`拒绝越界路径:${targetAbs} 不在记忆根 ${rootAbs} 内 (E_PATH_OUTSIDE_ROOT)`,
|
|
199
|
+
);
|
|
200
|
+
}
|
|
201
|
+
let realRoot;
|
|
202
|
+
try {
|
|
203
|
+
realRoot = fs.realpathSync.native ? fs.realpathSync.native(rootAbs) : fs.realpathSync(rootAbs);
|
|
204
|
+
} catch (error) {
|
|
205
|
+
throw memoryError(
|
|
206
|
+
PATH_CODES.ROOT_MISSING,
|
|
207
|
+
`无法解析记忆根目录的真实路径:${rootAbs} — 目录不存在或不可访问`,
|
|
208
|
+
error,
|
|
209
|
+
);
|
|
210
|
+
}
|
|
211
|
+
const realTarget = realPathProbe(targetAbs);
|
|
212
|
+
if (!isInsideRoot(realRoot, realTarget)) {
|
|
213
|
+
throw memoryError(
|
|
214
|
+
PATH_CODES.PATH_OUTSIDE_ROOT,
|
|
215
|
+
`拒绝越界路径:${targetAbs} 的真实路径 ${realTarget} 不在记忆根 ${rootAbs} 的真实路径 ${realRoot} 内 (E_PATH_OUTSIDE_ROOT)`,
|
|
216
|
+
);
|
|
217
|
+
}
|
|
218
|
+
return targetAbs;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/** Assert an already-resolved path is inside the root; returns it unchanged. */
|
|
222
|
+
export function assertInsideRoot(root, target) {
|
|
223
|
+
return resolveInside(root, target);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/** POSIX-style path relative to the root, for reports and index rows. */
|
|
227
|
+
export function relativePath(root, target) {
|
|
228
|
+
return path.relative(path.resolve(root), path.resolve(target)).split(path.sep).join('/');
|
|
229
|
+
}
|