dsh-cortex-memory 0.4.146 → 0.4.148

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.
Files changed (184) hide show
  1. package/CHANGELOG.md +2423 -2369
  2. package/README.md +8 -8
  3. package/bench/governance.mjs +265 -0
  4. package/bench/migration-bench.mjs +1 -1
  5. package/bench/semantic-cn.js +4 -4
  6. package/bench/synthetic.js +3 -3
  7. package/bin/copy-db.mjs +1 -1
  8. package/bin/repair.mjs +7 -7
  9. package/client.js +1 -1
  10. package/docs/install-windows.md +2 -2
  11. package/docs/models.md +1 -1
  12. package/docs/quota-compaction-design.md +3 -3
  13. package/icon.svg +12 -2
  14. package/index.js +5 -6
  15. package/package.json +7 -8
  16. package/src/adapters/shiori.js +1 -1
  17. package/src/engines/compression.js +8 -3
  18. package/src/engines/consolidate.js +1 -1
  19. package/src/engines/encode.js +3 -3
  20. package/src/engines/extract.js +1 -1
  21. package/src/engines/health.js +1 -1
  22. package/src/engines/life.js +2 -2
  23. package/src/engines/narrative.js +4 -8
  24. package/src/engines/retrieve.js +41 -4
  25. package/src/host/scheduler.js +5 -4
  26. package/src/memory/tokenizer.js +1 -1
  27. package/src/self/alias.js +4 -5
  28. package/src/self/memory-policy.js +59 -0
  29. package/src/service/cortex.js +20 -2
  30. package/src/store/repositories/facts.js +1 -1
  31. package/src/store/repositories/traces.js +1 -1
  32. package/test/integration/_helpers.js +0 -128
  33. package/test/integration/acceptance.test.js +0 -803
  34. package/test/integration/ann-load.test.js +0 -61
  35. package/test/integration/api-contract.test.js +0 -1476
  36. package/test/integration/backup-privacy.test.js +0 -214
  37. package/test/integration/consolidate.test.js +0 -79
  38. package/test/integration/edge-cases.test.js +0 -1089
  39. package/test/integration/exchange.test.js +0 -326
  40. package/test/integration/life-loop.test.js +0 -189
  41. package/test/integration/load.test.js +0 -751
  42. package/test/integration/markdown-sync.test.js +0 -35
  43. package/test/integration/policy-query-api.test.js +0 -306
  44. package/test/integration/resilience-writes.test.js +0 -241
  45. package/test/integration/tools-contract.test.js +0 -1324
  46. package/test/unit/alias.test.js +0 -87
  47. package/test/unit/ann-edge.test.js +0 -102
  48. package/test/unit/ann.test.js +0 -77
  49. package/test/unit/api-client.test.js +0 -576
  50. package/test/unit/api-validate.test.js +0 -19
  51. package/test/unit/api.test.js +0 -555
  52. package/test/unit/appraisal.test.js +0 -48
  53. package/test/unit/attachments.test.js +0 -33
  54. package/test/unit/auto-reflect.test.js +0 -141
  55. package/test/unit/autonomy.test.js +0 -53
  56. package/test/unit/batch.test.js +0 -46
  57. package/test/unit/bench.test.js +0 -40
  58. package/test/unit/benchmark-synthetic.test.js +0 -52
  59. package/test/unit/capture-filter.test.js +0 -296
  60. package/test/unit/cli-default-root.test.js +0 -21
  61. package/test/unit/client-contract.test.js +0 -32
  62. package/test/unit/compaction-preview.test.js +0 -91
  63. package/test/unit/compression.test.js +0 -76
  64. package/test/unit/consolidate-dedupe.test.js +0 -174
  65. package/test/unit/consolidate-errors.test.js +0 -63
  66. package/test/unit/consolidate-gate.test.js +0 -26
  67. package/test/unit/consolidate-preview.test.js +0 -168
  68. package/test/unit/consolidate-protection.test.js +0 -156
  69. package/test/unit/consolidate-reason.test.js +0 -206
  70. package/test/unit/consolidate-retry.test.js +0 -91
  71. package/test/unit/consolidate-selection.test.js +0 -103
  72. package/test/unit/core.test.js +0 -77
  73. package/test/unit/coverage-rewrite.test.js +0 -38
  74. package/test/unit/dedupe-episodes.test.js +0 -34
  75. package/test/unit/dedupe-threshold.test.js +0 -14
  76. package/test/unit/develop.test.js +0 -130
  77. package/test/unit/development.test.js +0 -68
  78. package/test/unit/digest.test.js +0 -26
  79. package/test/unit/dsr.test.js +0 -111
  80. package/test/unit/edges-candidates.test.js +0 -113
  81. package/test/unit/emotion.test.js +0 -62
  82. package/test/unit/encode.test.js +0 -98
  83. package/test/unit/evolution.test.js +0 -130
  84. package/test/unit/external-ann-search.test.js +0 -50
  85. package/test/unit/external-ann.test.js +0 -29
  86. package/test/unit/extract-question.test.js +0 -41
  87. package/test/unit/fact-guard.test.js +0 -51
  88. package/test/unit/fact-noise-llm.test.js +0 -44
  89. package/test/unit/facts-belief.test.js +0 -32
  90. package/test/unit/facts-compat.test.js +0 -62
  91. package/test/unit/facts.test.js +0 -107
  92. package/test/unit/feature-overrides.test.js +0 -42
  93. package/test/unit/fts-migration.test.js +0 -42
  94. package/test/unit/graph.test.js +0 -79
  95. package/test/unit/growth-reflect.test.js +0 -124
  96. package/test/unit/guard-runtime.test.js +0 -40
  97. package/test/unit/guardian.test.js +0 -91
  98. package/test/unit/health-consolidation.test.js +0 -52
  99. package/test/unit/health-fts-scope.test.js +0 -33
  100. package/test/unit/health-privacy.test.js +0 -102
  101. package/test/unit/health-summaries.test.js +0 -24
  102. package/test/unit/health-text-maintain.test.js +0 -33
  103. package/test/unit/health-today.test.js +0 -45
  104. package/test/unit/hnsw.test.js +0 -35
  105. package/test/unit/host-runtime.test.js +0 -142
  106. package/test/unit/host.test.js +0 -58
  107. package/test/unit/identity.test.js +0 -98
  108. package/test/unit/induct-evaluate.test.js +0 -41
  109. package/test/unit/ingest-extracted.test.js +0 -35
  110. package/test/unit/inhibition.test.js +0 -90
  111. package/test/unit/initiative.test.js +0 -90
  112. package/test/unit/jobs.test.js +0 -93
  113. package/test/unit/json-snapshot.test.js +0 -57
  114. package/test/unit/label-entities-skip.test.js +0 -18
  115. package/test/unit/life-consolidate.test.js +0 -34
  116. package/test/unit/life.test.js +0 -149
  117. package/test/unit/llm-facts-integration.test.js +0 -27
  118. package/test/unit/llm-facts.test.js +0 -16
  119. package/test/unit/llm.test.js +0 -45
  120. package/test/unit/locomo.test.js +0 -45
  121. package/test/unit/longmemeval.test.js +0 -56
  122. package/test/unit/maintain-dispute.test.js +0 -62
  123. package/test/unit/maintain-duration.test.js +0 -194
  124. package/test/unit/markdown-import.test.js +0 -44
  125. package/test/unit/markdown.test.js +0 -34
  126. package/test/unit/memory-edit.test.js +0 -112
  127. package/test/unit/metrics.test.js +0 -26
  128. package/test/unit/migration.test.js +0 -38
  129. package/test/unit/model-config.test.js +0 -52
  130. package/test/unit/mood-saturation.test.js +0 -33
  131. package/test/unit/narrative-weave.test.js +0 -271
  132. package/test/unit/narrative.test.js +0 -62
  133. package/test/unit/playbook-consolidate.test.js +0 -24
  134. package/test/unit/privacy-scope.test.js +0 -39
  135. package/test/unit/profile-paths.test.js +0 -21
  136. package/test/unit/profile.test.js +0 -26
  137. package/test/unit/prompt.test.js +0 -42
  138. package/test/unit/providers.test.js +0 -42
  139. package/test/unit/qa.test.js +0 -100
  140. package/test/unit/query.test.js +0 -53
  141. package/test/unit/reflect-backfill.test.js +0 -158
  142. package/test/unit/reflect-coverage.test.js +0 -35
  143. package/test/unit/reflect-overview.test.js +0 -22
  144. package/test/unit/reflect-phrasing.test.js +0 -75
  145. package/test/unit/reflect-sampling.test.js +0 -80
  146. package/test/unit/reflect-source-budget.test.js +0 -24
  147. package/test/unit/relationship-dynamics.test.js +0 -29
  148. package/test/unit/retention.test.js +0 -31
  149. package/test/unit/retrieval-ranking.test.js +0 -31
  150. package/test/unit/retrieve.test.js +0 -117
  151. package/test/unit/revive-on-read.test.js +0 -84
  152. package/test/unit/rotation.test.js +0 -25
  153. package/test/unit/scanner.test.js +0 -50
  154. package/test/unit/scenario.test.js +0 -78
  155. package/test/unit/scheduler-autoconsolidate.test.js +0 -95
  156. package/test/unit/scheduler-autodedupe.test.js +0 -52
  157. package/test/unit/scheduler-autosleep.test.js +0 -50
  158. package/test/unit/scheduler-cadence.test.js +0 -33
  159. package/test/unit/scheduler-deep.test.js +0 -62
  160. package/test/unit/scheduler-tick-gate.test.js +0 -38
  161. package/test/unit/scheduler.test.js +0 -137
  162. package/test/unit/self-state.test.js +0 -94
  163. package/test/unit/service.test.js +0 -72
  164. package/test/unit/shiori-import.test.js +0 -77
  165. package/test/unit/skills.test.js +0 -104
  166. package/test/unit/spacing.test.js +0 -14
  167. package/test/unit/store.test.js +0 -111
  168. package/test/unit/summon-left-slot.test.js +0 -51
  169. package/test/unit/summon-rank.test.js +0 -51
  170. package/test/unit/temporal-graph.test.js +0 -44
  171. package/test/unit/tiers-gist.test.js +0 -140
  172. package/test/unit/tiers.test.js +0 -39
  173. package/test/unit/tokenizer-fts.test.js +0 -35
  174. package/test/unit/tools.test.js +0 -122
  175. package/test/unit/upgrade-0412.test.js +0 -104
  176. package/test/unit/upgrade-0413.test.js +0 -84
  177. package/test/unit/upgrade-0414.test.js +0 -101
  178. package/test/unit/vector-ann.test.js +0 -53
  179. package/test/unit/vector-async.test.js +0 -69
  180. package/test/unit/vector-coverage.test.js +0 -34
  181. package/test/unit/vector-override.test.js +0 -37
  182. package/test/unit/vector-typed-array.test.js +0 -47
  183. package/test/unit/vector.test.js +0 -68
  184. package/test/unit/working.test.js +0 -99
@@ -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,79 +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
- // 预演现在明确标成 preview,而不是把「有决策被拒」误报成 rejected:
62
- // 预演不应用任何东西,也不该被当成一次真实的巩固。
63
- assert.equal(result.run.status, 'preview');
64
- assert.match(String(result.run.error), /dry-run/);
65
- assert.equal(result.applied.length, 0);
66
- assert.equal(cortex.get(ids.d).state, 'active');
67
- assert.equal(cortex.get(ids.a).state, 'active');
68
- }));
69
-
70
- test('consolidation without an LLM records skipped and changes nothing', withCortex(null, async (cortex) => {
71
- const ids = seed(cortex);
72
- const result = await cortex.consolidation.run({ scopeId: 'global' });
73
- assert.equal(result.run.status, 'skipped');
74
- // 0.4.97 起「没有 adapter」和「adapter 回了空」分开标了,旧断言没跟上。
75
- assert.equal(result.run.error, 'no-llm-adapter');
76
- assert.equal(result.applied.length, 0);
77
- assert.equal(cortex.get(ids.a).state, 'active');
78
- assert.equal(cortex.consolidation.list({ scopeId: 'global' }).length, 1);
79
- }));