dsh-cortex-memory 0.4.22 → 0.4.147
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 +2390 -154
- package/README.md +11 -9
- package/bench/migration-bench.mjs +1 -1
- package/bench/semantic-cn.js +10 -10
- package/bench/synthetic.js +3 -3
- package/bin/copy-db.mjs +47 -0
- package/bin/repair.mjs +328 -0
- package/bin/verify.mjs +202 -0
- package/client.js +1 -1
- package/cordis.patch.yml +1 -1
- package/docs/install-windows.md +2 -2
- package/docs/models.md +1 -1
- package/docs/quota-compaction-design.md +175 -0
- package/icon.svg +12 -2
- package/index.js +27 -3
- package/package.json +3 -3
- package/src/adapters/shiori.js +1 -1
- package/src/api/routes.js +1 -1
- package/src/core/feature-overrides.js +2 -2
- package/src/engines/compression.js +172 -3
- package/src/engines/consolidate.js +397 -26
- package/src/engines/encode.js +16 -3
- package/src/engines/extract.js +72 -8
- package/src/engines/fact-guard.js +51 -0
- package/src/engines/health.js +263 -3
- package/src/engines/inhibition.js +39 -0
- package/src/engines/life.js +14 -3
- package/src/engines/llm-facts.js +12 -9
- package/src/engines/narrative.js +150 -13
- package/src/engines/reflect.js +172 -14
- package/src/engines/retrieve.js +44 -5
- package/src/engines/summary-coverage.js +62 -0
- package/src/engines/summary-dedupe.js +77 -0
- package/src/engines/summon-rank.js +132 -0
- package/src/host/runtime.js +64 -4
- package/src/host/scheduler.js +224 -3
- package/src/llm/adapter.js +29 -8
- package/src/memory/query.js +1 -1
- package/src/memory/spacing.js +15 -0
- package/src/memory/tokenizer.js +1 -1
- package/src/prompt/tiers.js +88 -12
- package/src/self/alias.js +25 -0
- package/src/self/emotion.js +3 -1
- package/src/self/relationship.js +44 -13
- package/src/self/state.js +20 -2
- package/src/self/working.js +34 -3
- package/src/service/cortex.js +493 -53
- package/src/store/repositories/facts.js +34 -1
- package/src/store/repositories/summaries.js +8 -1
- package/src/store/repositories/traces.js +22 -2
- package/src/tools/definitions.js +118 -4
- package/src/vector/index.js +96 -7
- package/test/integration/_helpers.js +0 -128
- package/test/integration/acceptance.test.js +0 -803
- package/test/integration/ann-load.test.js +0 -61
- package/test/integration/api-contract.test.js +0 -1476
- package/test/integration/backup-privacy.test.js +0 -214
- package/test/integration/consolidate.test.js +0 -75
- package/test/integration/edge-cases.test.js +0 -1088
- package/test/integration/exchange.test.js +0 -326
- package/test/integration/life-loop.test.js +0 -188
- package/test/integration/load.test.js +0 -751
- package/test/integration/markdown-sync.test.js +0 -35
- package/test/integration/policy-query-api.test.js +0 -306
- package/test/integration/resilience-writes.test.js +0 -241
- package/test/integration/tools-contract.test.js +0 -1324
- package/test/unit/ann-edge.test.js +0 -102
- package/test/unit/ann.test.js +0 -77
- package/test/unit/api-client.test.js +0 -576
- package/test/unit/api-validate.test.js +0 -19
- package/test/unit/api.test.js +0 -555
- package/test/unit/appraisal.test.js +0 -48
- package/test/unit/attachments.test.js +0 -33
- package/test/unit/autonomy.test.js +0 -53
- package/test/unit/batch.test.js +0 -46
- package/test/unit/bench.test.js +0 -40
- package/test/unit/benchmark-synthetic.test.js +0 -52
- package/test/unit/cli-default-root.test.js +0 -21
- package/test/unit/client-contract.test.js +0 -32
- package/test/unit/compression.test.js +0 -76
- package/test/unit/core.test.js +0 -77
- package/test/unit/develop.test.js +0 -130
- package/test/unit/development.test.js +0 -68
- package/test/unit/digest.test.js +0 -26
- package/test/unit/dsr.test.js +0 -111
- package/test/unit/edges-candidates.test.js +0 -113
- package/test/unit/emotion.test.js +0 -61
- package/test/unit/encode.test.js +0 -98
- package/test/unit/evolution.test.js +0 -130
- package/test/unit/external-ann-search.test.js +0 -50
- package/test/unit/external-ann.test.js +0 -29
- package/test/unit/facts-belief.test.js +0 -32
- package/test/unit/facts.test.js +0 -107
- package/test/unit/feature-overrides.test.js +0 -42
- package/test/unit/fts-migration.test.js +0 -42
- package/test/unit/graph.test.js +0 -79
- package/test/unit/growth-reflect.test.js +0 -123
- package/test/unit/guard-runtime.test.js +0 -40
- package/test/unit/guardian.test.js +0 -91
- package/test/unit/health-privacy.test.js +0 -102
- package/test/unit/hnsw.test.js +0 -35
- package/test/unit/host-runtime.test.js +0 -142
- package/test/unit/host.test.js +0 -58
- package/test/unit/identity.test.js +0 -98
- package/test/unit/induct-evaluate.test.js +0 -41
- package/test/unit/ingest-extracted.test.js +0 -35
- package/test/unit/initiative.test.js +0 -90
- package/test/unit/jobs.test.js +0 -93
- package/test/unit/json-snapshot.test.js +0 -57
- package/test/unit/life-consolidate.test.js +0 -34
- package/test/unit/life.test.js +0 -149
- package/test/unit/llm-facts-integration.test.js +0 -27
- package/test/unit/llm-facts.test.js +0 -16
- package/test/unit/llm.test.js +0 -45
- package/test/unit/locomo.test.js +0 -45
- package/test/unit/longmemeval.test.js +0 -56
- package/test/unit/markdown-import.test.js +0 -44
- package/test/unit/markdown.test.js +0 -34
- package/test/unit/memory-edit.test.js +0 -112
- package/test/unit/metrics.test.js +0 -26
- package/test/unit/migration.test.js +0 -38
- package/test/unit/model-config.test.js +0 -52
- package/test/unit/narrative.test.js +0 -62
- package/test/unit/playbook-consolidate.test.js +0 -24
- package/test/unit/privacy-scope.test.js +0 -39
- package/test/unit/profile-paths.test.js +0 -21
- package/test/unit/profile.test.js +0 -26
- package/test/unit/prompt.test.js +0 -42
- package/test/unit/providers.test.js +0 -42
- package/test/unit/qa.test.js +0 -100
- package/test/unit/query.test.js +0 -53
- package/test/unit/retention.test.js +0 -31
- package/test/unit/retrieval-ranking.test.js +0 -31
- package/test/unit/retrieve.test.js +0 -117
- package/test/unit/rotation.test.js +0 -25
- package/test/unit/scanner.test.js +0 -50
- package/test/unit/scenario.test.js +0 -78
- package/test/unit/scheduler.test.js +0 -136
- package/test/unit/self-state.test.js +0 -94
- package/test/unit/service.test.js +0 -72
- package/test/unit/shiori-import.test.js +0 -77
- package/test/unit/skills.test.js +0 -104
- package/test/unit/store.test.js +0 -111
- package/test/unit/temporal-graph.test.js +0 -44
- package/test/unit/tiers.test.js +0 -39
- package/test/unit/tokenizer-fts.test.js +0 -35
- package/test/unit/tools.test.js +0 -122
- package/test/unit/upgrade-0412.test.js +0 -104
- package/test/unit/upgrade-0413.test.js +0 -84
- package/test/unit/upgrade-0414.test.js +0 -101
- package/test/unit/vector-ann.test.js +0 -53
- package/test/unit/vector-async.test.js +0 -69
- package/test/unit/vector.test.js +0 -68
- package/test/unit/working.test.js +0 -80
|
@@ -1,214 +0,0 @@
|
|
|
1
|
-
import test from 'node:test';
|
|
2
|
-
import assert from 'node:assert/strict';
|
|
3
|
-
import fs from 'node:fs';
|
|
4
|
-
import path from 'node:path';
|
|
5
|
-
import { DatabaseSync } from 'node:sqlite';
|
|
6
|
-
import { BASE_MS, cleanup, makeCortex } from './_helpers.js';
|
|
7
|
-
|
|
8
|
-
test('guardian backup validates, restores to an offline path and refuses the live DB', () => {
|
|
9
|
-
const context = makeCortex();
|
|
10
|
-
const { cortex, dir } = context;
|
|
11
|
-
try {
|
|
12
|
-
const written = cortex.write({
|
|
13
|
-
text: '备份前的白饭承诺:用户会买白饭。',
|
|
14
|
-
title: '白饭承诺',
|
|
15
|
-
explicit: true,
|
|
16
|
-
now: BASE_MS,
|
|
17
|
-
facts: [{ subject: '用户', predicate: '答应', objectLiteral: '买白饭', confidence: 0.9 }],
|
|
18
|
-
});
|
|
19
|
-
assert.equal(written.skipped, false);
|
|
20
|
-
assert.equal(cortex.repos.traces.count(), 1);
|
|
21
|
-
assert.equal(cortex.repos.facts.list({ scopeId: 'global' }).length, 1);
|
|
22
|
-
assert.equal(cortex.guardian.integrity(), 'ok');
|
|
23
|
-
|
|
24
|
-
// --- Backup writes a standalone, integrity-checked SQLite file. ---
|
|
25
|
-
const backupPath = path.join(dir, 'backups', 'cortex-backup.sqlite');
|
|
26
|
-
const backup = cortex.guardian.backup(backupPath);
|
|
27
|
-
assert.equal(backup.path, path.resolve(backupPath));
|
|
28
|
-
assert.ok(path.isAbsolute(backup.path));
|
|
29
|
-
assert.ok(backup.bytes > 0);
|
|
30
|
-
assert.ok(fs.existsSync(backupPath));
|
|
31
|
-
|
|
32
|
-
const validation = cortex.guardian.validateBackup(backupPath);
|
|
33
|
-
assert.equal(validation.ok, true);
|
|
34
|
-
assert.equal(validation.integrity, 'ok');
|
|
35
|
-
assert.deepEqual(validation.missing, []);
|
|
36
|
-
assert.ok(validation.tables >= 20);
|
|
37
|
-
|
|
38
|
-
assert.deepEqual(cortex.guardian.validateBackup(path.join(dir, 'never-created.sqlite')), {
|
|
39
|
-
ok: false,
|
|
40
|
-
reason: 'missing',
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
// --- Restore is only allowed to an offline path. ---
|
|
44
|
-
const offline = path.join(dir, 'offline', 'restored.sqlite');
|
|
45
|
-
const restored = cortex.guardian.restore(backupPath, offline);
|
|
46
|
-
assert.equal(restored.restored, true);
|
|
47
|
-
assert.equal(restored.target, path.resolve(offline));
|
|
48
|
-
assert.equal(restored.validation.ok, true);
|
|
49
|
-
assert.equal(cortex.guardian.validateBackup(offline).ok, true);
|
|
50
|
-
|
|
51
|
-
// The restored file is a real database with the backed-up rows.
|
|
52
|
-
const restoredDb = new DatabaseSync(offline);
|
|
53
|
-
try {
|
|
54
|
-
assert.equal(restoredDb.prepare('PRAGMA integrity_check').get().integrity_check, 'ok');
|
|
55
|
-
assert.equal(Number(restoredDb.prepare('SELECT COUNT(*) AS c FROM traces').get().c), 1);
|
|
56
|
-
assert.equal(Number(restoredDb.prepare('SELECT COUNT(*) AS c FROM facts').get().c), 1);
|
|
57
|
-
assert.equal(restoredDb.prepare('SELECT content FROM traces LIMIT 1').get().content, '备份前的白饭承诺:用户会买白饭。');
|
|
58
|
-
assert.equal(restoredDb.prepare("SELECT value FROM meta WHERE key = 'schema_version'").get().value, '7');
|
|
59
|
-
} finally {
|
|
60
|
-
restoredDb.close();
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
// Restoring over an existing destination keeps a `.before-restore-*` safety copy.
|
|
64
|
-
const second = cortex.guardian.restore(backupPath, offline);
|
|
65
|
-
assert.equal(second.restored, true);
|
|
66
|
-
const safetyCopies = fs.readdirSync(path.dirname(offline)).filter((name) => name.startsWith('restored.sqlite.before-restore-'));
|
|
67
|
-
assert.equal(safetyCopies.length, 1);
|
|
68
|
-
|
|
69
|
-
// --- The live DB path is refused before any copy happens. ---
|
|
70
|
-
const liveAttempt = cortex.guardian.restore(backupPath, cortex.config.dbFile);
|
|
71
|
-
assert.equal(liveAttempt.restored, false);
|
|
72
|
-
assert.equal(liveAttempt.reason, 'live-db');
|
|
73
|
-
assert.equal(cortex.repos.traces.count(), 1);
|
|
74
|
-
assert.match(liveAttempt.message, /新路径|离线|关闭/);
|
|
75
|
-
assert.equal(cortex.guardian.validateBackup(cortex.config.dbFile).ok, true);
|
|
76
|
-
|
|
77
|
-
// Missing target and missing backup are both safe refusals.
|
|
78
|
-
assert.equal(cortex.guardian.restore(backupPath).reason, 'target-required');
|
|
79
|
-
const missing = cortex.guardian.restore(path.join(dir, 'nope.sqlite'), path.join(dir, 'offline', 'x.sqlite'));
|
|
80
|
-
assert.equal(missing.restored, false);
|
|
81
|
-
assert.equal(missing.reason, 'invalid-backup');
|
|
82
|
-
assert.equal(missing.validation.reason, 'missing');
|
|
83
|
-
|
|
84
|
-
// The live service still works after all of that.
|
|
85
|
-
const after = cortex.write({ text: '恢复流程之后仍然可以写入。', explicit: true, now: BASE_MS + 10_000 });
|
|
86
|
-
assert.equal(after.skipped, false);
|
|
87
|
-
assert.equal(cortex.repos.traces.count(), 2);
|
|
88
|
-
assert.equal(cortex.guardian.check().ok, true);
|
|
89
|
-
} finally {
|
|
90
|
-
cleanup(context);
|
|
91
|
-
}
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
test('guardian detects FTS drift, checks it, repairs it and re-checks clean', () => {
|
|
95
|
-
const context = makeCortex();
|
|
96
|
-
const { cortex } = context;
|
|
97
|
-
try {
|
|
98
|
-
const first = cortex.write({ text: '第一条用于索引的记忆。', explicit: true, now: BASE_MS });
|
|
99
|
-
const second = cortex.write({ text: '第二条用于索引的记忆。', explicit: true, now: BASE_MS + 1_000 });
|
|
100
|
-
assert.equal(cortex.repos.traces.ftsCount(), 2);
|
|
101
|
-
assert.equal(cortex.guardian.check().ok, true);
|
|
102
|
-
|
|
103
|
-
// Simulate a stale/corrupt FTS projection, the exact condition the guardian names.
|
|
104
|
-
cortex.db.prepare('DELETE FROM traces_fts_index WHERE trace_id = ?').run(first.trace.id);
|
|
105
|
-
assert.equal(cortex.repos.traces.ftsCount(), 1);
|
|
106
|
-
|
|
107
|
-
const drift = cortex.guardian.check();
|
|
108
|
-
assert.equal(drift.ok, false);
|
|
109
|
-
const issue = drift.issues.find((item) => item.code === 'FTS_DRIFT');
|
|
110
|
-
assert.ok(issue);
|
|
111
|
-
assert.match(issue.message, /FTS 1 \/ traces 2/);
|
|
112
|
-
|
|
113
|
-
const repaired = cortex.guardian.repairFts();
|
|
114
|
-
assert.equal(repaired.before, 1);
|
|
115
|
-
assert.equal(repaired.after, 2);
|
|
116
|
-
assert.equal(repaired.reindexed, 2);
|
|
117
|
-
assert.equal(cortex.guardian.check().ok, true);
|
|
118
|
-
|
|
119
|
-
// Search still finds both traces after the repair.
|
|
120
|
-
const found = cortex.search('索引', { now: BASE_MS + 2_000, limit: 5 });
|
|
121
|
-
assert.ok(found.items.some((item) => item.id === first.trace.id));
|
|
122
|
-
assert.ok(found.items.some((item) => item.id === second.trace.id));
|
|
123
|
-
} finally {
|
|
124
|
-
cleanup(context);
|
|
125
|
-
}
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
test('privacy deleteScope removes traces/facts/working/goals but keeps the event ledger', () => {
|
|
129
|
-
const context = makeCortex();
|
|
130
|
-
const { cortex } = context;
|
|
131
|
-
try {
|
|
132
|
-
const first = cortex.write({
|
|
133
|
-
text: '用户答应买白饭。',
|
|
134
|
-
title: '白饭',
|
|
135
|
-
explicit: true,
|
|
136
|
-
now: BASE_MS,
|
|
137
|
-
facts: [{ subject: '用户', predicate: '答应', objectLiteral: '买白饭', confidence: 0.9 }],
|
|
138
|
-
});
|
|
139
|
-
const second = cortex.write({
|
|
140
|
-
text: '用户想给助手一个实体。',
|
|
141
|
-
title: '实体',
|
|
142
|
-
explicit: true,
|
|
143
|
-
now: BASE_MS + 1_000,
|
|
144
|
-
facts: [{ subject: '用户', predicate: '想给', object: '助手', confidence: 0.8 }],
|
|
145
|
-
});
|
|
146
|
-
assert.equal(first.skipped, false);
|
|
147
|
-
assert.equal(second.skipped, false);
|
|
148
|
-
assert.equal(cortex.repos.facts.list({ scopeId: 'global' }).length, 2);
|
|
149
|
-
assert.equal(cortex.repos.entities.list({ scopeId: 'global' }).length, 2);
|
|
150
|
-
|
|
151
|
-
cortex.feedback({ traceId: first.trace.id, signal: 'used', predicted: 0.7, now: BASE_MS + 2_000 });
|
|
152
|
-
cortex.working.add({ text: '写隐私删除测试。', priority: 0.9, at: BASE_MS });
|
|
153
|
-
cortex.working.add({ text: '已经过期的焦点。', at: BASE_MS, ttlMs: 1_000 });
|
|
154
|
-
const goalOne = cortex.goals.add({ title: '完成集成测试', priority: 0.9 });
|
|
155
|
-
cortex.goals.add({ title: '另一个目标', priority: 0.4 });
|
|
156
|
-
cortex.goals.complete(goalOne.id);
|
|
157
|
-
|
|
158
|
-
assert.equal(cortex.working.list({ scopeId: 'global', includeExpired: true }).length, 2);
|
|
159
|
-
assert.equal(cortex.goals.list({ scopeId: 'global' }).length, 2);
|
|
160
|
-
assert.equal(cortex.repos.feedback.list({ scopeId: 'global' }).length, 1);
|
|
161
|
-
|
|
162
|
-
const eventsBefore = cortex.repos.events.count();
|
|
163
|
-
assert.equal(eventsBefore, 2);
|
|
164
|
-
const payloadBefore = cortex.repos.events.list({ limit: 10 }).map((event) => event.payload);
|
|
165
|
-
assert.equal(JSON.stringify(payloadBefore).includes('白饭'), false); // the ledger stores hashes, never raw text
|
|
166
|
-
|
|
167
|
-
const deleted = cortex.privacy.deleteScope('global', { actor: 'user', reason: 'integration-privacy' });
|
|
168
|
-
assert.equal(deleted.scopeId, 'global');
|
|
169
|
-
assert.equal(deleted.removed, true);
|
|
170
|
-
assert.equal(deleted.counts.traces, 2);
|
|
171
|
-
assert.equal(deleted.counts.facts, 2);
|
|
172
|
-
assert.equal(deleted.counts.entities, 2);
|
|
173
|
-
assert.equal(deleted.counts.working, 2);
|
|
174
|
-
assert.equal(deleted.counts.goals, 2);
|
|
175
|
-
assert.equal(deleted.counts.feedback, 1);
|
|
176
|
-
assert.equal(deleted.counts.identity, 1);
|
|
177
|
-
|
|
178
|
-
// Scoped content is gone from every derived store.
|
|
179
|
-
assert.equal(cortex.repos.traces.count(), 0);
|
|
180
|
-
assert.equal(cortex.get(first.trace.id), null);
|
|
181
|
-
assert.equal(cortex.get(second.trace.id), null);
|
|
182
|
-
assert.equal(cortex.repos.facts.list({ scopeId: 'global' }).length, 0);
|
|
183
|
-
assert.equal(cortex.repos.entities.list({ scopeId: 'global' }).length, 0);
|
|
184
|
-
assert.equal(cortex.working.list({ scopeId: 'global', includeExpired: true }).length, 0);
|
|
185
|
-
assert.equal(cortex.goals.list({ scopeId: 'global' }).length, 0);
|
|
186
|
-
assert.equal(cortex.repos.feedback.list({ scopeId: 'global' }).length, 0);
|
|
187
|
-
assert.equal(cortex.repos.traces.ftsCount(), 0);
|
|
188
|
-
|
|
189
|
-
// The append-only ledger keeps its count and stays verifiable.
|
|
190
|
-
assert.equal(cortex.repos.events.count(), eventsBefore);
|
|
191
|
-
const chain = cortex.repos.events.verifyChain();
|
|
192
|
-
assert.equal(chain.ok, true);
|
|
193
|
-
assert.equal(chain.count, eventsBefore);
|
|
194
|
-
assert.equal(JSON.stringify(cortex.repos.events.list({ limit: 10 }).map((event) => event.payload)).includes('白饭'), false);
|
|
195
|
-
|
|
196
|
-
// The deletion itself is audited.
|
|
197
|
-
const audit = cortex.repos.audit.list({ limit: 20 });
|
|
198
|
-
const record = audit.find((item) => item.action === 'privacy.delete-scope');
|
|
199
|
-
assert.ok(record);
|
|
200
|
-
assert.equal(record.targetId, 'global');
|
|
201
|
-
assert.equal(record.detail.reason, 'integration-privacy');
|
|
202
|
-
assert.equal(record.detail.counts.traces, 2);
|
|
203
|
-
|
|
204
|
-
// Search is empty rather than stale, and the service still accepts new writes.
|
|
205
|
-
assert.equal(cortex.search('白饭', { now: BASE_MS + 3_000 }).items.length, 0);
|
|
206
|
-
const rewritten = cortex.write({ text: '删除之后重新开始。', explicit: true, now: BASE_MS + 4_000 });
|
|
207
|
-
assert.equal(rewritten.skipped, false);
|
|
208
|
-
assert.equal(cortex.repos.traces.count(), 1);
|
|
209
|
-
assert.equal(cortex.repos.events.count(), eventsBefore + 1);
|
|
210
|
-
assert.equal(cortex.repos.events.verifyChain().ok, true);
|
|
211
|
-
} finally {
|
|
212
|
-
cleanup(context);
|
|
213
|
-
}
|
|
214
|
-
});
|
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
import test from 'node:test';
|
|
2
|
-
import assert from 'node:assert/strict';
|
|
3
|
-
import { mkdtempSync, rmSync } from 'node:fs';
|
|
4
|
-
import os from 'node:os';
|
|
5
|
-
import path from 'node:path';
|
|
6
|
-
import { createCortex } from '../../src/service/cortex.js';
|
|
7
|
-
|
|
8
|
-
function withCortex(reply, fn) {
|
|
9
|
-
return async () => {
|
|
10
|
-
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-consolidate-'));
|
|
11
|
-
const cortex = createCortex({ config: { dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite') }, llm: reply ? { complete: async () => reply } : null });
|
|
12
|
-
try {
|
|
13
|
-
await fn(cortex);
|
|
14
|
-
} finally {
|
|
15
|
-
cortex.close();
|
|
16
|
-
rmSync(dir, { recursive: true, force: true });
|
|
17
|
-
}
|
|
18
|
-
};
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
function seed(cortex) {
|
|
22
|
-
const at = Date.parse('2026-11-11T10:00:00+08:00');
|
|
23
|
-
const a = cortex.write({ text: '旧的低价值记忆 A。', explicit: true, importance: 0.3, now: at });
|
|
24
|
-
const b = cortex.write({ text: '要保留的记忆 B。', explicit: true, importance: 0.6, now: at + 1000 });
|
|
25
|
-
const c = cortex.write({ text: '要合并的记忆 C。', explicit: true, importance: 0.5, now: at + 2000 });
|
|
26
|
-
const d = cortex.write({ text: '重要记忆 D,不能归档。', explicit: true, importance: 0.95, now: at + 3000 });
|
|
27
|
-
return { a: a.trace.id, b: b.trace.id, c: c.trace.id, d: d.trace.id };
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
test('consolidation applies validated archive/merge and records a replayable run', withCortex(null, async (cortex) => {
|
|
31
|
-
const ids = seed(cortex);
|
|
32
|
-
const reply = JSON.stringify([
|
|
33
|
-
{ action: 'archive', ids: [ids.a], reason: 'low value' },
|
|
34
|
-
{ action: 'merge', ids: [ids.b, ids.c], targetId: ids.b, reason: 'same topic' },
|
|
35
|
-
]);
|
|
36
|
-
cortex.llm = { complete: async () => reply };
|
|
37
|
-
// Inject through a fresh engine so the same service can use the reply adapter.
|
|
38
|
-
const { createConsolidationEngine } = await import('../../src/engines/consolidate.js');
|
|
39
|
-
const engine = createConsolidationEngine({ db: cortex.db, traces: cortex.repos.traces, summaries: cortex.repos.summaries, audit: cortex.repos.audit, llm: cortex.llm });
|
|
40
|
-
const result = await engine.run({ scopeId: 'global', now: Date.parse('2026-11-11T12:00:00+08:00'), limit: 20 });
|
|
41
|
-
assert.equal(result.run.status, 'applied');
|
|
42
|
-
assert.equal(result.applied.length, 2);
|
|
43
|
-
assert.equal(cortex.get(ids.a).state, 'archived');
|
|
44
|
-
assert.equal(cortex.get(ids.c).state, 'archived');
|
|
45
|
-
assert.equal(cortex.get(ids.b).state, 'active');
|
|
46
|
-
assert.equal(cortex.get(ids.d).state, 'active');
|
|
47
|
-
assert.equal(engine.list({ scopeId: 'global' }).length, 1);
|
|
48
|
-
assert.equal(engine.list({ scopeId: 'global' })[0].inputDigest.length, 64);
|
|
49
|
-
}));
|
|
50
|
-
|
|
51
|
-
test('consolidation dry-run and invalid/protected decisions do not mutate', withCortex(null, async (cortex) => {
|
|
52
|
-
const ids = seed(cortex);
|
|
53
|
-
cortex.llm = { complete: async () => JSON.stringify([
|
|
54
|
-
{ action: 'archive', ids: [ids.d] },
|
|
55
|
-
{ action: 'archive', ids: ['trc_missing'] },
|
|
56
|
-
{ action: 'nonsense', ids: [ids.a] },
|
|
57
|
-
]) };
|
|
58
|
-
const { createConsolidationEngine } = await import('../../src/engines/consolidate.js');
|
|
59
|
-
const engine = createConsolidationEngine({ db: cortex.db, traces: cortex.repos.traces, summaries: cortex.repos.summaries, audit: cortex.repos.audit, llm: cortex.llm });
|
|
60
|
-
const result = await engine.run({ scopeId: 'global', dryRun: true });
|
|
61
|
-
assert.equal(result.run.status, 'rejected');
|
|
62
|
-
assert.equal(result.applied.length, 0);
|
|
63
|
-
assert.equal(cortex.get(ids.d).state, 'active');
|
|
64
|
-
assert.equal(cortex.get(ids.a).state, 'active');
|
|
65
|
-
}));
|
|
66
|
-
|
|
67
|
-
test('consolidation without an LLM records skipped and changes nothing', withCortex(null, async (cortex) => {
|
|
68
|
-
const ids = seed(cortex);
|
|
69
|
-
const result = await cortex.consolidation.run({ scopeId: 'global' });
|
|
70
|
-
assert.equal(result.run.status, 'skipped');
|
|
71
|
-
assert.equal(result.run.error, 'no-llm');
|
|
72
|
-
assert.equal(result.applied.length, 0);
|
|
73
|
-
assert.equal(cortex.get(ids.a).state, 'active');
|
|
74
|
-
assert.equal(cortex.consolidation.list({ scopeId: 'global' }).length, 1);
|
|
75
|
-
}));
|