@jjh32456/dsh-longlive 1.6.7 → 1.8.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 +70 -1508
- package/LICENSE +1 -1
- package/QUICKSTART.md +34 -13
- package/README.en.md +18 -7
- package/README.md +34 -9
- package/SPEC.md +5 -5
- package/cordis.patch.yml +23 -9
- package/docs/ANSWER-GUARD.md +92 -0
- package/docs/ARCHITECTURE.md +4 -3
- package/docs/AUTO-RECALL-WIRING.md +1 -1
- package/docs/AUTO-RECALL.md +15 -9
- package/docs/CONFIG.md +168 -35
- package/docs/ERRORS.md +17 -5
- package/docs/FAQ.md +7 -7
- package/docs/FEATURES.md +96 -28
- package/docs/MIGRATION-1.6.0.md +2 -2
- package/docs/OPERATIONS.md +6 -6
- package/docs/PANEL-INTEGRATION.md +6 -0
- package/docs/PRIVACY.md +88 -0
- package/docs/QUICKSTART.md +5 -5
- package/docs/README.en.md +4 -2
- package/docs/RESIDENT-WATCHER.md +1 -1
- package/docs/SESSION-WATCH.md +6 -6
- package/docs/TROUBLESHOOTING.md +4 -4
- package/package.json +47 -117
- package/runbook.md +2 -2
- package/scripts/backfill-auto-cards.mjs +98 -0
- package/scripts/install-surgery-task.ps1 +5 -2
- package/scripts/lib/session-watch-fixture.mjs +1 -1
- package/scripts/longlive-doctor.mjs +6 -0
- package/scripts/migrate-180.mjs +163 -0
- package/scripts/recall-index-build.mjs +128 -0
- package/scripts/session-watch-drill.mjs +1 -1
- package/scripts/surgery-run.cmd +1 -1
- package/scripts/unattended-surgery.ps1 +5 -2
- package/scripts/verify-card-anchors.mjs +138 -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 +760 -0
- package/src/activity/model.js +351 -0
- package/src/activity/store.js +644 -0
- package/src/activity/thinking.js +176 -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 +235 -0
- package/src/adapters/registry.js +315 -0
- package/src/answer/audit.js +193 -0
- package/src/answer/evidence.js +30 -0
- package/src/answer/guard.js +1370 -0
- package/src/answer/index.js +1067 -0
- package/src/answer/query.js +111 -0
- package/src/answer/second-hop.mjs +101 -0
- package/src/auto-truncate.js +1 -1
- package/src/config.js +171 -47
- package/src/deps.js +6 -4
- package/src/features.js +182 -6
- package/src/heartbeat.js +156 -23
- package/src/identity/handover.js +52 -1
- package/src/identity/index.js +13 -5
- package/src/identity/inject-position.js +2 -2
- package/src/index.js +97 -7
- package/src/lifecycle/gap-clocks.js +328 -0
- package/src/lifecycle/index.js +269 -0
- package/src/live.js +22 -2
- package/src/memory/activity-capture.js +229 -0
- package/src/memory/anchors.js +194 -0
- package/src/memory/audit-records.js +222 -0
- package/src/memory/auto-cards.js +401 -0
- package/src/memory/capture.js +442 -0
- package/src/memory/card-anchors.js +203 -0
- package/src/memory/conflicts.js +106 -0
- package/src/memory/decay.js +311 -0
- package/src/memory/distill.js +471 -0
- package/src/memory/event-index.js +917 -0
- package/src/memory/evidence-chain.js +295 -0
- package/src/memory/evidence.js +145 -0
- package/src/memory/filter-runner.js +1 -1
- package/src/memory/filter.js +5 -1
- package/src/memory/forget-v2.js +410 -0
- package/src/memory/forget.js +71 -0
- package/src/memory/frames-activation.js +247 -0
- package/src/memory/fts-query.js +174 -0
- package/src/memory/governance.js +604 -0
- package/src/memory/index.js +1 -0
- package/src/memory/l2-gate.js +169 -0
- package/src/memory/lock.js +121 -0
- package/src/memory/migrate.js +139 -0
- package/src/memory/recall-v2.js +223 -0
- package/src/memory/recall.js +65 -2
- package/src/memory/records.js +425 -0
- package/src/memory/retention.js +128 -0
- package/src/memory/review.js +124 -0
- package/src/memory/trust.js +88 -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 +5 -3
- package/src/metrics/index.mjs +21 -1
- package/src/metrics/prestep-latency.mjs +300 -0
- package/src/metrics/store.mjs +25 -0
- package/src/policy-profile.js +18 -3
- package/src/recall/anaphora.mjs +87 -0
- package/src/recall/cards.mjs +7 -0
- package/src/recall/embedding.mjs +8 -8
- package/src/recall/entities.mjs +112 -0
- package/src/recall/index.js +235 -19
- package/src/recall/inject-order.mjs +10 -8
- package/src/recall/privacy.mjs +105 -16
- package/src/recall/recall-core.mjs +116 -10
- package/src/recall/shared-visibility.js +302 -0
- package/src/recall/time-normalizer.mjs +196 -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 +176 -25
- package/src/router/background/agent-runner.mjs +1 -1
- package/src/router/background/anchor.mjs +1 -1
- package/src/router/background/audit.mjs +2 -2
- package/src/router/background/budget-ledger.mjs +2 -2
- package/src/router/background/host-sim.mjs +1 -1
- package/src/router/background/memory.mjs +4 -4
- package/src/router/background/queue.mjs +1 -1
- package/src/router/background/retry.mjs +1 -1
- package/src/router/background/router.mjs +1 -1
- package/src/router/background/stream.mjs +1 -1
- package/src/router/background/tool-policy.mjs +1 -1
- package/src/router/design-headings.json +5 -5
- package/src/router/face/config.mjs +1 -1
- package/src/router/face/identity.mjs +3 -3
- package/src/router/judge.mjs +8 -7
- package/src/router/policies/generic.json +21 -4
- package/src/router/route.js +109 -22
- package/src/router/router.mjs +10 -6
- package/src/router/routing-rules.json +260 -445
- package/src/router/run-sim.mjs +8 -8
- package/src/router/runtime.js +23 -12
- package/src/router/service/board.mjs +95 -15
- package/src/router/service/config.mjs +5 -5
- package/src/router/service/context.mjs +1 -1
- package/src/router/service/hint.mjs +14 -0
- package/src/router/service/queue.mjs +1 -1
- package/src/router/service/service.mjs +27 -11
- package/src/router/shadow.mjs +4 -3
- package/src/surgery-recovery.js +2 -2
- package/src/tool-output.js +2 -2
- package/src/tools.js +39 -7
- package/src/truncate-visible.js +2 -2
- package/src/ui/actions.js +1312 -0
- package/src/ui/panel-api.js +367 -0
- package/src/ui/panel-auth.js +251 -0
- package/src/ui/panel-integrity.js +243 -0
- package/src/ui/routes-readonly.js +695 -0
- package/src/ui/routes.js +3 -0
- package/docs/DEV.md +0 -279
- package/docs/PRESENCE-BUS-WIRING.md +0 -491
- package/docs/PRESENCE-BUS.md +0 -694
- package/docs/SURGERY.md +0 -186
- package/scripts/mark-core-cards.mjs +0 -289
- package/scripts/pack.mjs +0 -392
- package/scripts/router-proposals.mjs +0 -386
- package/src/router/fixtures/M1.json +0 -16
- package/src/router/fixtures/M2.json +0 -16
- package/src/router/fixtures/M3.json +0 -20
- package/src/router/fixtures/M4.json +0 -20
- package/src/router/fixtures/M5.json +0 -45
- package/src/router/fixtures/M6.json +0 -53
- package/src/router/fixtures/M7.json +0 -46
- package/src/router/fixtures/S1.json +0 -20
- package/src/router/fixtures/S2.json +0 -23
- package/src/router/fixtures/S3.json +0 -20
- package/src/router/fixtures/S4.json +0 -34
- package/src/router/fixtures/S5.json +0 -43
- package/src/router/fixtures/S6.json +0 -47
- package/src/router/fixtures/T1.json +0 -23
- package/src/router/fixtures/T2.json +0 -16
- package/src/router/fixtures/T3.json +0 -19
- package/src/router/fixtures/T4.json +0 -16
- package/src/router/fixtures/X1.json +0 -17
- package/src/router/fixtures/X2.json +0 -20
- package/src/router/gen-design-headings.mjs +0 -107
- package/src/router/policies/owner-rulings-2026-11-03.json +0 -62
- package/test/auto-recall.test.mjs +0 -474
- package/test/auto-truncate.test.mjs +0 -931
- package/test/autobind.test.mjs +0 -385
- package/test/bus.test.mjs +0 -1572
- package/test/cli-guard.test.mjs +0 -198
- package/test/conclusion.test.mjs +0 -408
- package/test/config-schema.test.mjs +0 -204
- package/test/d6-output.test.mjs +0 -194
- package/test/d6.test.mjs +0 -395
- package/test/deps.test.mjs +0 -342
- package/test/doctor-setup.test.mjs +0 -297
- package/test/embedding-recall.test.mjs +0 -347
- package/test/feature-flags.test.mjs +0 -568
- package/test/filter-shadow.test.mjs +0 -170
- package/test/fixtures/baseline-0.8.0-registration.json +0 -556
- package/test/fixtures/d6-header-tools.json +0 -1
- package/test/fixtures/lock-child.mjs +0 -67
- package/test/fixtures/pre-step-container.mjs +0 -100
- package/test/fixtures/privacy-memory/topics/pii-unmarked.md +0 -9
- package/test/fixtures/privacy-memory/topics/secret-local-only.md +0 -11
- package/test/fixtures/profile-session-longlive-config.json +0 -20
- package/test/fixtures/proposal-cas-child.mjs +0 -66
- package/test/host-engine-status.test.mjs +0 -307
- package/test/hot-truncate.test.mjs +0 -1349
- package/test/identity-card-marker.test.mjs +0 -307
- package/test/identity-router/run.mjs +0 -721
- package/test/integration-pre-step.test.mjs +0 -553
- package/test/live.test.mjs +0 -1020
- package/test/maintenance-schedule.test.mjs +0 -538
- package/test/memory.test.mjs +0 -2828
- package/test/metrics.test.mjs +0 -478
- package/test/orchestrator.test.mjs +0 -266
- package/test/p1_probe.mjs +0 -97
- package/test/packaging.test.mjs +0 -182
- package/test/portability.test.mjs +0 -343
- package/test/rc4-regressions.test.mjs +0 -562
- package/test/rc5-regressions.test.mjs +0 -197
- package/test/rc6-chinese.test.mjs +0 -394
- package/test/router-1100/apply-off.test.mjs +0 -175
- package/test/router-1100/budget-reconcile.test.mjs +0 -215
- package/test/router-1100/config-context.test.mjs +0 -211
- package/test/router-1100/helpers.mjs +0 -129
- package/test/router-1100/hint-envelope.test.mjs +0 -150
- package/test/router-1100/lock-crossproc.test.mjs +0 -206
- package/test/router-1100/proposal-cas-crossproc.test.mjs +0 -184
- package/test/router-1100/router-apply.test.mjs +0 -372
- package/test/router-1100/router-fixb.test.mjs +0 -602
- package/test/router-1100/router-proposals.test.mjs +0 -405
- package/test/router-1100/router-queue-lease-wal.test.mjs +0 -355
- package/test/router-1100/router-security-cr.test.mjs +0 -333
- package/test/router-1100/service-lifecycle.test.mjs +0 -220
- package/test/router-1100/tools-contract.test.mjs +0 -164
- package/test/router-1100/turn-reflect-tick.test.mjs +0 -89
- package/test/router-1100/v6-contract.test.mjs +0 -158
- package/test/router-1100/v6b-contract.test.mjs +0 -293
- package/test/router-1100/v6c-contract.test.mjs +0 -249
- package/test/router-1100/wal-rotation.test.mjs +0 -375
- package/test/router-1100/wp1-config.test.mjs +0 -170
- package/test/router-1100/wp1-default-off.test.mjs +0 -77
- package/test/router-1100/wp1-model-status.test.mjs +0 -90
- package/test/router-1100/wp1-status.test.mjs +0 -220
- package/test/router-1100/wp1-tools.test.mjs +0 -146
- package/test/router-1100/wp5-cli.test.mjs +0 -240
- package/test/router-1100/wp5-model-auto.test.mjs +0 -418
- package/test/router-1100/wp6-merge.test.mjs +0 -386
- package/test/router-1100/wp6-wake.test.mjs +0 -502
- package/test/router-background/audit.test.mjs +0 -37
- package/test/router-background/budget-ledger.test.mjs +0 -330
- package/test/router-background/cross-process.test.mjs +0 -222
- package/test/router-background/fixtures/cross-child.mjs +0 -85
- package/test/router-background/fixtures/real-shapes.mjs +0 -239
- package/test/router-background/router.test.mjs +0 -1044
- package/test/router-background/stream-retry.test.mjs +0 -358
- package/test/router-background/tool-agent.test.mjs +0 -384
- package/test/router-face/bridge.test.mjs +0 -169
- package/test/router-face/face.test.mjs +0 -316
- package/test/router-failclosed.test.mjs +0 -466
- package/test/router-identity.test.mjs +0 -384
- package/test/router-l1-l2.contract.test.mjs +0 -1075
- package/test/router-memory-tools.test.mjs +0 -262
- package/test/router-policy.test.mjs +0 -225
- package/test/router-runtime.test.mjs +0 -438
- package/test/router-shadow.test.mjs +0 -287
- package/test/router.test.mjs +0 -903
- package/test/run-rc.mjs +0 -299
- package/test/run.mjs +0 -2469
- package/test/safety-defaults.test.mjs +0 -179
- package/test/seeded-load.test.mjs +0 -436
- package/test/session-original.test.mjs +0 -338
- package/test/surgery-recovery.test.mjs +0 -936
- package/test/t7_drive.mjs +0 -151
- package/test/tool-output.test.mjs +0 -393
- package/test/tool-schema.test.mjs +0 -177
- package/test/tools_contract.test.mjs +0 -716
- package/test/truncate-visible.test.mjs +0 -1150
- package/test/ui-flags.test.mjs +0 -477
- package/test/v4-hardening.test.mjs +0 -427
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
// src/memory/conflicts.js — 1.8.0 / v2.8 §15.1-A2/A4:冲突检测唯一实现(避免两套真相)
|
|
2
|
+
//
|
|
3
|
+
// 目标:Claim(治理层)与 records(v2.6.4 遗留)共用同一套互斥判定,
|
|
4
|
+
// 以 cards.js 的极性词表为唯一词源;输出 conflicts[]{idA,idB,subject,why,source}。
|
|
5
|
+
// - subject 归一:entity(+attribute)小写、去空白;同 subject 才可能互斥;
|
|
6
|
+
// - 互斥:正反极性(cards.isNegativeConclusion 取反)或同 attribute 不同 value;
|
|
7
|
+
// - supersedes/supersededBy 链内不判冲突;非 active/verified 状态不参与;
|
|
8
|
+
// - 有 claims 时以 claims 为准(旧 records 是迁移来源),避免同一事实重复计数。
|
|
9
|
+
|
|
10
|
+
import crypto from 'node:crypto';
|
|
11
|
+
import fs from 'node:fs';
|
|
12
|
+
import path from 'node:path';
|
|
13
|
+
|
|
14
|
+
import { readClaims } from './governance.js';
|
|
15
|
+
import { detectRecordConflicts, listRecords } from './records.js';
|
|
16
|
+
|
|
17
|
+
export function conflictKeyOf(claimA, claimB) {
|
|
18
|
+
return [String(claimA), String(claimB)].sort().join('|');
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function conflictIdOf(claimA, claimB) {
|
|
22
|
+
const key = conflictKeyOf(claimA, claimB);
|
|
23
|
+
return `cfl_${crypto.createHash('sha1').update(key, 'utf8').digest('hex').slice(0, 12)}`;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function conflictStatePathOf(memoryRoot) {
|
|
27
|
+
return path.join(path.resolve(String(memoryRoot)), '.longlive', 'conflicts', 'state.json');
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Persistent arbitration state (CAS version source for conflict.arbitrate). */
|
|
31
|
+
export function readConflictState({ memoryRoot }) {
|
|
32
|
+
try {
|
|
33
|
+
const value = JSON.parse(fs.readFileSync(conflictStatePathOf(memoryRoot), 'utf8'));
|
|
34
|
+
return {
|
|
35
|
+
version: Number.isInteger(value?.version) ? value.version : 0,
|
|
36
|
+
items: value?.items && typeof value.items === 'object' ? value.items : {},
|
|
37
|
+
};
|
|
38
|
+
} catch {
|
|
39
|
+
return { version: 0, items: {} };
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function resolutionState(item) {
|
|
44
|
+
if (!item || typeof item.resolution !== 'string') return 'open';
|
|
45
|
+
if (item.resolution === 'disputed') return 'disputed';
|
|
46
|
+
if (item.resolution === 'both') return 'both';
|
|
47
|
+
return 'resolved';
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export const CONFLICT_SCHEMA = 'conflicts/v1';
|
|
51
|
+
|
|
52
|
+
function normalizeSubjectPart(value) {
|
|
53
|
+
return String(value ?? '').toLowerCase().replace(/[\s\u3000]+/g, '');
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Detect conflicts via the SINGLE implementation in records.js (EA-03: one source of truth).
|
|
58
|
+
* Entries may be claims (subject object) or legacy records (subject string).
|
|
59
|
+
*/
|
|
60
|
+
export function detectConflicts(entries) {
|
|
61
|
+
const list = Array.isArray(entries) ? entries : [];
|
|
62
|
+
const { conflicts, byId } = detectRecordConflicts(list);
|
|
63
|
+
return {
|
|
64
|
+
conflicts: conflicts.map((conflict) => ({ ...conflict, source: (list.find((entry) => entry.id === conflict.idA) ?? {}).source ?? null })),
|
|
65
|
+
byId,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Unified view: claims preferred; legacy records only when no claims exist.
|
|
71
|
+
* Every entry gets conflict/conflict_with flags computed here (one source of truth).
|
|
72
|
+
*/
|
|
73
|
+
export function unifiedConflicts({ memoryRoot }) {
|
|
74
|
+
const claims = readClaims({ memoryRoot, actor: { kind: 'host' } }).items.map((claim) => ({ ...claim, source: 'claim' }));
|
|
75
|
+
let pool = claims;
|
|
76
|
+
if (pool.length === 0) {
|
|
77
|
+
pool = listRecords({ memoryRoot }).items.map((record) => ({
|
|
78
|
+
...record,
|
|
79
|
+
source: 'record',
|
|
80
|
+
subject: record.subject ?? null,
|
|
81
|
+
}));
|
|
82
|
+
}
|
|
83
|
+
const { conflicts: rawConflicts, byId } = detectConflicts(pool);
|
|
84
|
+
const conflictState = readConflictState({ memoryRoot });
|
|
85
|
+
const conflicts = rawConflicts.map((conflict) => {
|
|
86
|
+
const key = conflictKeyOf(conflict.idA, conflict.idB);
|
|
87
|
+
const item = conflictState.items[key] ?? null;
|
|
88
|
+
return {
|
|
89
|
+
...conflict,
|
|
90
|
+
conflictId: conflictIdOf(conflict.idA, conflict.idB),
|
|
91
|
+
state: resolutionState(item),
|
|
92
|
+
version: item && Number.isInteger(item.version) ? item.version : 0,
|
|
93
|
+
};
|
|
94
|
+
});
|
|
95
|
+
const marked = pool.map((entry) => {
|
|
96
|
+
const ids = byId.get(entry.id) ?? [];
|
|
97
|
+
return { ...entry, conflict: ids.length > 0, conflict_with: ids };
|
|
98
|
+
});
|
|
99
|
+
return {
|
|
100
|
+
schema: CONFLICT_SCHEMA,
|
|
101
|
+
source: claims.length > 0 ? 'claim' : 'record',
|
|
102
|
+
entries: marked,
|
|
103
|
+
conflicts,
|
|
104
|
+
total: conflicts.length,
|
|
105
|
+
};
|
|
106
|
+
}
|
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
// src/memory/decay.js — M2-D:衰减提案 / 合并(merge)/ 提升(promote)
|
|
2
|
+
//
|
|
3
|
+
// 设计依据 MEMORY-RECALL-V2-PROPOSAL §4.4(触发:时间衰减/重复合并/consolidation/manual)
|
|
4
|
+
// 与 §4.5(merge/distill/promote,无删除):
|
|
5
|
+
// * 衰减只提案、不直接删:同主题事件 90 天未被任何卡/记录引用 → 可合并为 distill
|
|
6
|
+
// 摘要(真正的蒸馏由 distill.js 执行;本模块只出 plan);
|
|
7
|
+
// * merge:重复 digest/同内容 → 保留 1 条主记录 + 其余登记 duplicateOf;旧卡
|
|
8
|
+
// 移到 `archive/merged/`(先备份、rename,不删除);保护卡/重要事件拒绝合并;
|
|
9
|
+
// * promote:新事实成立 → append 新版本(claimVersion+1、supersedes 链、CAS
|
|
10
|
+
// expectedVersion),旧版本永久保留;rollback 只切 active 指针。
|
|
11
|
+
//
|
|
12
|
+
// 与 forget.js/retention.js 的接口保持最小:只 import 保护判定与既有记录写入器,
|
|
13
|
+
// 不重写它们。
|
|
14
|
+
|
|
15
|
+
import fs from 'node:fs';
|
|
16
|
+
import path from 'node:path';
|
|
17
|
+
|
|
18
|
+
import { appendAuditRecord } from './audit-records.js';
|
|
19
|
+
import { appendRecordLine } from './capture.js';
|
|
20
|
+
import { memoryError, relativePath, resolveInside } from './paths.js';
|
|
21
|
+
import { isProtectedPath } from './retention.js';
|
|
22
|
+
import { appendAudit, backupInto, ensureDir, sha256Text, writeFileAtomic } from './state.js';
|
|
23
|
+
|
|
24
|
+
export const DECAY_SCHEMA = 'decay/v1';
|
|
25
|
+
export const PROMOTE_SCHEMA = 'promote/v1';
|
|
26
|
+
export const MERGE_SCHEMA = 'merge/v1';
|
|
27
|
+
export const VERSIONS_DIR = '.longlive/versions';
|
|
28
|
+
export const MERGE_ARCHIVE_DIR = '.longlive/archive/merged';
|
|
29
|
+
export const DECAY_POLICY = Object.freeze({
|
|
30
|
+
unreferencedDays: 90,
|
|
31
|
+
strict: true,
|
|
32
|
+
dailyCap: 50,
|
|
33
|
+
mergeNoiseOnly: true,
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
const DAY_MS = 24 * 60 * 60 * 1000;
|
|
37
|
+
const ISO_DAY_RE = /^\d{4}-\d{2}-\d{2}$/;
|
|
38
|
+
|
|
39
|
+
export function dayOf(value) {
|
|
40
|
+
if (typeof value === 'string' && ISO_DAY_RE.test(value.slice(0, 10))) return value.slice(0, 10);
|
|
41
|
+
const date = value instanceof Date ? value : new Date(value);
|
|
42
|
+
return Number.isNaN(date.getTime()) ? null : date.toISOString().slice(0, 10);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function daysSince(day, now = new Date()) {
|
|
46
|
+
const left = dayOf(day);
|
|
47
|
+
const right = dayOf(now);
|
|
48
|
+
if (left === null || right === null) return null;
|
|
49
|
+
return Math.floor((Date.parse(`${right}T00:00:00Z`) - Date.parse(`${left}T00:00:00Z`)) / DAY_MS);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function norm(value) {
|
|
53
|
+
return String(value ?? '')
|
|
54
|
+
.normalize('NFKC')
|
|
55
|
+
.replace(/[\s\u3000]+/g, ' ')
|
|
56
|
+
.replace(/[,。;:!?、,.!?;:…—–\-_/\\()()[\]{}"'"'`]+/g, '')
|
|
57
|
+
.trim()
|
|
58
|
+
.toLowerCase();
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function contentKeyOf(record) {
|
|
62
|
+
const raw = record?.content ?? record?.text ?? record?.gist ?? '';
|
|
63
|
+
return sha256Text(norm(raw));
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Pure decay score for one assertion/card: older + weaker => more decayed. */
|
|
67
|
+
export function decayScore(record, now = new Date()) {
|
|
68
|
+
const day = record?.lastUsed ?? record?.last_used ?? record?.createdAt ?? null;
|
|
69
|
+
const ageDays = day === null ? null : daysSince(day, now);
|
|
70
|
+
const strength = Number.isFinite(record?.strength) ? Number(record.strength) : 3;
|
|
71
|
+
const useCount = Number.isInteger(record?.useCount) ? record.useCount : 0;
|
|
72
|
+
return { ageDays, strength, useCount, decayed: ageDays !== null && ageDays > DECAY_POLICY.unreferencedDays && strength <= 3 && useCount === 0 };
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Time-decay proposals (read-only): same-topic records unreferenced for >90 days
|
|
77
|
+
* become distill candidates. Never deletes, never writes.
|
|
78
|
+
*/
|
|
79
|
+
export function proposeDecay({ records = [], cards = [], now = new Date(), days = DECAY_POLICY.unreferencedDays, cap = DECAY_POLICY.dailyCap } = {}) {
|
|
80
|
+
const topics = new Map();
|
|
81
|
+
const immune = [];
|
|
82
|
+
for (const record of records) {
|
|
83
|
+
const topic = norm(record?.subject?.entity ?? record?.subject ?? record?.topic ?? record?.kind ?? 'unknown');
|
|
84
|
+
const references = Array.isArray(record?.referencedBy) ? record.referencedBy : [];
|
|
85
|
+
const day = record?.lastUsed ?? record?.last_used ?? null;
|
|
86
|
+
const ageDays = day === null ? null : daysSince(day, now);
|
|
87
|
+
if (Number(record?.salience) >= 5 || record?.state === 'locked' || references.length > 0 || ageDays === null || ageDays <= days) {
|
|
88
|
+
immune.push({ id: record?.id ?? null, reason: ageDays === null ? 'no-date' : ageDays <= days ? `age=${ageDays}<=${days}` : 'protected-or-referenced' });
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
const bucket = topics.get(topic) ?? { topic, ids: [], ageDays };
|
|
92
|
+
bucket.ids.push(record.id);
|
|
93
|
+
bucket.ageDays = Math.max(bucket.ageDays, ageDays);
|
|
94
|
+
topics.set(topic, bucket);
|
|
95
|
+
}
|
|
96
|
+
for (const card of cards) {
|
|
97
|
+
if (isProtectedPath({ memoryRoot: card.memoryRoot ?? '', relPath: card.rel ?? '' })) {
|
|
98
|
+
immune.push({ id: card.rel ?? null, reason: 'protected-card' });
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
const topic = norm(card.fields?.tags?.[0] ?? card.title ?? 'unknown');
|
|
102
|
+
const ageDays = daysSince(card.fields?.last_used ?? null, now);
|
|
103
|
+
if (ageDays === null || ageDays <= days) continue;
|
|
104
|
+
const bucket = topics.get(topic) ?? { topic, ids: [], ageDays };
|
|
105
|
+
bucket.ids.push(card.rel);
|
|
106
|
+
bucket.ageDays = Math.max(bucket.ageDays, ageDays);
|
|
107
|
+
topics.set(topic, bucket);
|
|
108
|
+
}
|
|
109
|
+
const proposals = [...topics.values()]
|
|
110
|
+
.filter((bucket) => bucket.ids.length > 0)
|
|
111
|
+
.sort((left, right) => right.ageDays - left.ageDays)
|
|
112
|
+
.slice(0, cap)
|
|
113
|
+
.map((bucket) => ({
|
|
114
|
+
action: 'distill',
|
|
115
|
+
topic: bucket.topic,
|
|
116
|
+
sourceIds: [...bucket.ids].sort(),
|
|
117
|
+
ageDays: bucket.ageDays,
|
|
118
|
+
reason: `同主题 ${bucket.ids.length} 条事件 ${bucket.ageDays} 天未被引用(>${days})→ 可合成为 distill 摘要(原文保留)`,
|
|
119
|
+
}));
|
|
120
|
+
return { schema: DECAY_SCHEMA, policy: { ...DECAY_POLICY, days, cap }, proposals, immune, executed: false };
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** Duplicate groups by normalized content: keep first, mark the rest duplicateOf. */
|
|
124
|
+
export function mergePlan({ records = [] } = {}) {
|
|
125
|
+
const groups = new Map();
|
|
126
|
+
for (const record of records) {
|
|
127
|
+
if (!record || record.state === 'superseded' || record.state === 'archived') continue;
|
|
128
|
+
const key = contentKeyOf(record);
|
|
129
|
+
if (key === sha256Text('')) continue;
|
|
130
|
+
const bucket = groups.get(key) ?? [];
|
|
131
|
+
bucket.push(record);
|
|
132
|
+
groups.set(key, bucket);
|
|
133
|
+
}
|
|
134
|
+
const plans = [];
|
|
135
|
+
for (const [digest, bucket] of groups) {
|
|
136
|
+
if (bucket.length < 2) continue;
|
|
137
|
+
const [keep, ...duplicates] = bucket;
|
|
138
|
+
plans.push({
|
|
139
|
+
action: 'merge',
|
|
140
|
+
digest,
|
|
141
|
+
keep: {
|
|
142
|
+
id: keep.id ?? null,
|
|
143
|
+
rel: keep.rel ?? null,
|
|
144
|
+
content: keep.content ?? null,
|
|
145
|
+
anchors: Array.isArray(keep.anchors) ? keep.anchors : [],
|
|
146
|
+
sourceEventIds: Array.isArray(keep.sourceEventIds) ? keep.sourceEventIds : [],
|
|
147
|
+
},
|
|
148
|
+
duplicates: duplicates.map((record) => ({
|
|
149
|
+
id: record.id ?? null,
|
|
150
|
+
rel: record.rel ?? null,
|
|
151
|
+
anchors: Array.isArray(record.anchors) ? record.anchors : [],
|
|
152
|
+
sourceEventIds: Array.isArray(record.sourceEventIds) ? record.sourceEventIds : [],
|
|
153
|
+
})),
|
|
154
|
+
mergeId: `merge:${digest.slice(0, 16)}`,
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
return { schema: MERGE_SCHEMA, plans, policy: { archiveMerged: MERGE_ARCHIVE_DIR, noDelete: true } };
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function auditRow({ root, action, recordId, extra, now }) {
|
|
161
|
+
try {
|
|
162
|
+
appendAuditRecord({
|
|
163
|
+
memoryRoot: root,
|
|
164
|
+
action,
|
|
165
|
+
recordId,
|
|
166
|
+
readerTier: 'host',
|
|
167
|
+
questionId: 'unknown',
|
|
168
|
+
expectedTurns: null,
|
|
169
|
+
answerText: null,
|
|
170
|
+
at: now.toISOString(),
|
|
171
|
+
extra,
|
|
172
|
+
});
|
|
173
|
+
} catch {
|
|
174
|
+
/* best effort: audit failure must not abort an already-recorded move */
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Apply a merge plan. Records: append one merge row (no delete). Cards: back up +
|
|
180
|
+
* rename duplicates into `archive/merged/`; protected cards are refused.
|
|
181
|
+
*/
|
|
182
|
+
export function applyMerge({ root, plan, now = new Date(), by = 'decay:v1' } = {}) {
|
|
183
|
+
const memoryRoot = String(root ?? '');
|
|
184
|
+
if (memoryRoot === '') throw memoryError('E_MEMORY_ROOT', 'applyMerge requires root');
|
|
185
|
+
if (!plan || plan.action !== 'merge') throw memoryError('E_MERGE_PLAN', 'merge plan 必须是 mergePlan 的产物');
|
|
186
|
+
const duplicates = Array.isArray(plan.duplicates) ? plan.duplicates : [];
|
|
187
|
+
const moved = [];
|
|
188
|
+
for (const duplicate of duplicates) {
|
|
189
|
+
if (typeof duplicate.rel === 'string' && duplicate.rel !== '') {
|
|
190
|
+
if (isProtectedPath({ memoryRoot, relPath: duplicate.rel })) {
|
|
191
|
+
throw memoryError('E_PROTECTED', `保护卡拒绝合并/归档:${duplicate.rel}(误杀率必须 0)`);
|
|
192
|
+
}
|
|
193
|
+
const source = resolveInside(memoryRoot, duplicate.rel);
|
|
194
|
+
if (!fs.existsSync(source)) continue;
|
|
195
|
+
ensureDir(memoryRoot, MERGE_ARCHIVE_DIR);
|
|
196
|
+
const target = resolveInside(memoryRoot, path.join(MERGE_ARCHIVE_DIR, path.basename(duplicate.rel)));
|
|
197
|
+
if (fs.existsSync(target)) continue;
|
|
198
|
+
const backup = backupInto(memoryRoot, source, path.join('.longlive', 'backups'), { now });
|
|
199
|
+
fs.renameSync(source, target);
|
|
200
|
+
moved.push({ rel: duplicate.rel, archived: relativePath(memoryRoot, target), backup: backup.rel, sha256: backup.sha256 });
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
const mergeRow = {
|
|
204
|
+
id: plan.mergeId,
|
|
205
|
+
kind: 'merge',
|
|
206
|
+
state: 'merged',
|
|
207
|
+
content: `duplicate merge: keep=${plan.keep?.id ?? plan.keep?.rel ?? 'unknown'} duplicates=${duplicates.length}`,
|
|
208
|
+
duplicateOf: plan.keep?.id ?? plan.keep?.rel ?? null,
|
|
209
|
+
supersedes: duplicates.map((duplicate) => duplicate.id ?? duplicate.rel).filter(Boolean),
|
|
210
|
+
anchors: [
|
|
211
|
+
...(Array.isArray(plan.keep?.anchors) ? plan.keep.anchors : []),
|
|
212
|
+
...duplicates.flatMap((duplicate) => (Array.isArray(duplicate.anchors) ? duplicate.anchors : [])),
|
|
213
|
+
],
|
|
214
|
+
sourceEventIds: [
|
|
215
|
+
...(Array.isArray(plan.keep?.sourceEventIds) ? plan.keep.sourceEventIds : []),
|
|
216
|
+
...duplicates.flatMap((duplicate) => (Array.isArray(duplicate.sourceEventIds) ? duplicate.sourceEventIds : [])),
|
|
217
|
+
],
|
|
218
|
+
backup: moved.map((item) => ({ rel: item.rel, path: item.backup, sha256: item.sha256 })),
|
|
219
|
+
archived: moved.map((item) => ({ rel: item.rel, archived: item.archived })),
|
|
220
|
+
createdAt: now.toISOString(),
|
|
221
|
+
audit: { by, reason: 'duplicate-digest-merge', moved: moved.length },
|
|
222
|
+
};
|
|
223
|
+
const written = appendRecordLine({ memoryRoot, record: mergeRow });
|
|
224
|
+
appendAudit(memoryRoot, { action: 'memory_merge', object: mergeRow.id, result: `duplicates=${duplicates.length} moved=${moved.length}`, now });
|
|
225
|
+
auditRow({
|
|
226
|
+
root: memoryRoot,
|
|
227
|
+
action: 'merge',
|
|
228
|
+
recordId: mergeRow.id,
|
|
229
|
+
extra: {
|
|
230
|
+
keep: mergeRow.duplicateOf,
|
|
231
|
+
keepAnchors: Array.isArray(plan.keep?.anchors) ? plan.keep.anchors : [],
|
|
232
|
+
duplicates: duplicates.length,
|
|
233
|
+
duplicateAnchors: duplicates.flatMap((duplicate) => (Array.isArray(duplicate.anchors) ? duplicate.anchors : [])),
|
|
234
|
+
moved,
|
|
235
|
+
backup: mergeRow.backup,
|
|
236
|
+
archived: mergeRow.archived,
|
|
237
|
+
anchors: mergeRow.anchors,
|
|
238
|
+
},
|
|
239
|
+
now,
|
|
240
|
+
});
|
|
241
|
+
return { ok: true, schema: MERGE_SCHEMA, mergeId: mergeRow.id, keep: mergeRow.duplicateOf, moved, record: mergeRow, written };
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
function promoteDirOf(root, id) {
|
|
245
|
+
return path.join(path.resolve(String(root)), VERSIONS_DIR, 'records', `${sha256Text(String(id)).slice(0, 12)}.jsonl`);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
export function promoteHistory({ root, id } = {}) {
|
|
249
|
+
const file = promoteDirOf(root, id);
|
|
250
|
+
let raw;
|
|
251
|
+
try {
|
|
252
|
+
raw = fs.readFileSync(file, 'utf8');
|
|
253
|
+
} catch (error) {
|
|
254
|
+
if (error?.code === 'ENOENT') return { id, file, version: null, versions: [] };
|
|
255
|
+
throw error;
|
|
256
|
+
}
|
|
257
|
+
const versions = raw.split(/\r?\n/).filter((line) => line.trim() !== '').map((line) => JSON.parse(line));
|
|
258
|
+
return { id, file, version: versions.at(-1)?.claimVersion ?? null, versions };
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Promote a new assertion: append claimVersion+1 with supersedes chain (CAS on
|
|
263
|
+
* expectedVersion). Old versions stay on disk; rollback only moves the pointer.
|
|
264
|
+
*/
|
|
265
|
+
export function promoteRecord({ root, record, supersedes = null, expectedVersion = null, by = 'decay:v1', reason = '', now = new Date() } = {}) {
|
|
266
|
+
const memoryRoot = String(root ?? '');
|
|
267
|
+
if (memoryRoot === '') throw memoryError('E_MEMORY_ROOT', 'promoteRecord requires root');
|
|
268
|
+
if (!record || typeof record.id !== 'string' || record.id === '') throw memoryError('E_PROMOTE_RECORD', 'promote 需要 record.id');
|
|
269
|
+
const history = promoteHistory({ root: memoryRoot, id: record.id });
|
|
270
|
+
const applied = history.versions.at(-1) ?? null;
|
|
271
|
+
const currentVersion = applied?.claimVersion ?? 0;
|
|
272
|
+
if (expectedVersion !== null && expectedVersion !== currentVersion) {
|
|
273
|
+
throw memoryError('E_CAS_VERSION', `promote CAS 失败:期望 v${expectedVersion},当前 v${currentVersion}`);
|
|
274
|
+
}
|
|
275
|
+
const version = currentVersion + 1;
|
|
276
|
+
const row = {
|
|
277
|
+
...record,
|
|
278
|
+
schema: PROMOTE_SCHEMA,
|
|
279
|
+
id: record.id,
|
|
280
|
+
claimVersion: version,
|
|
281
|
+
prevVersion: applied ? `${record.id}@v${currentVersion}` : null,
|
|
282
|
+
supersedes: supersedes ? [supersedes] : (record.supersedes ?? []),
|
|
283
|
+
state: record.state ?? 'active',
|
|
284
|
+
audit: { by, reason, createdAt: now.toISOString() },
|
|
285
|
+
updatedAt: now.toISOString(),
|
|
286
|
+
};
|
|
287
|
+
const file = promoteDirOf(memoryRoot, record.id);
|
|
288
|
+
ensureDir(memoryRoot, path.dirname(file));
|
|
289
|
+
fs.appendFileSync(file, `${JSON.stringify(row)}\n`, 'utf8');
|
|
290
|
+
appendAudit(memoryRoot, { action: 'memory_promote', object: `${record.id}@v${version}`, result: `supersedes=${row.supersedes.join(',') || '-'} by=${by}`, now });
|
|
291
|
+
auditRow({ root: memoryRoot, action: 'promote', recordId: record.id, extra: { claimVersion: version, supersedes: row.supersedes, prevVersion: row.prevVersion }, now });
|
|
292
|
+
return { ok: true, schema: PROMOTE_SCHEMA, id: record.id, claimVersion: version, prevVersion: row.prevVersion, supersedes: row.supersedes, file, record: row };
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
export function promoteRollback({ root, id, toVersion, by = 'admin', reason = '', now = new Date() } = {}) {
|
|
296
|
+
const memoryRoot = String(root ?? '');
|
|
297
|
+
const history = promoteHistory({ root: memoryRoot, id });
|
|
298
|
+
const target = history.versions.find((row) => row.claimVersion === toVersion);
|
|
299
|
+
if (!target) throw memoryError('E_CHAIN_NO_VERSION', `promote 链 ${id} 没有 v${toVersion}`);
|
|
300
|
+
const file = history.file;
|
|
301
|
+
const pointer = path.join(path.resolve(memoryRoot), VERSIONS_DIR, 'records', 'index.json');
|
|
302
|
+
let index = { schema: 'promote-index/v1', entries: {} };
|
|
303
|
+
try {
|
|
304
|
+
index = JSON.parse(fs.readFileSync(pointer, 'utf8'));
|
|
305
|
+
} catch { /* first write */ }
|
|
306
|
+
index.entries[id] = { file: path.basename(file), activeVersion: toVersion, updatedAt: now.toISOString() };
|
|
307
|
+
writeFileAtomic(memoryRoot, pointer, `${JSON.stringify(index, null, 2)}\n`, { backup: false, now });
|
|
308
|
+
appendAudit(memoryRoot, { action: 'memory_promote_rollback', object: `${id}->v${toVersion}`, result: `by=${by} reason=${reason || '-'}`, now });
|
|
309
|
+
auditRow({ root: memoryRoot, action: 'promote_rollback', recordId: id, extra: { toVersion, by, reason }, now });
|
|
310
|
+
return { ok: true, id, activeVersion: toVersion, file };
|
|
311
|
+
}
|