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.
- package/CHANGELOG.md +2423 -2369
- package/README.md +8 -8
- package/bench/governance.mjs +265 -0
- package/bench/migration-bench.mjs +1 -1
- package/bench/semantic-cn.js +4 -4
- package/bench/synthetic.js +3 -3
- package/bin/copy-db.mjs +1 -1
- package/bin/repair.mjs +7 -7
- package/client.js +1 -1
- package/docs/install-windows.md +2 -2
- package/docs/models.md +1 -1
- package/docs/quota-compaction-design.md +3 -3
- package/icon.svg +12 -2
- package/index.js +5 -6
- package/package.json +7 -8
- package/src/adapters/shiori.js +1 -1
- package/src/engines/compression.js +8 -3
- package/src/engines/consolidate.js +1 -1
- package/src/engines/encode.js +3 -3
- package/src/engines/extract.js +1 -1
- package/src/engines/health.js +1 -1
- package/src/engines/life.js +2 -2
- package/src/engines/narrative.js +4 -8
- package/src/engines/retrieve.js +41 -4
- package/src/host/scheduler.js +5 -4
- package/src/memory/tokenizer.js +1 -1
- package/src/self/alias.js +4 -5
- package/src/self/memory-policy.js +59 -0
- package/src/service/cortex.js +20 -2
- package/src/store/repositories/facts.js +1 -1
- package/src/store/repositories/traces.js +1 -1
- 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 -79
- package/test/integration/edge-cases.test.js +0 -1089
- package/test/integration/exchange.test.js +0 -326
- package/test/integration/life-loop.test.js +0 -189
- 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/alias.test.js +0 -87
- 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/auto-reflect.test.js +0 -141
- 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/capture-filter.test.js +0 -296
- package/test/unit/cli-default-root.test.js +0 -21
- package/test/unit/client-contract.test.js +0 -32
- package/test/unit/compaction-preview.test.js +0 -91
- package/test/unit/compression.test.js +0 -76
- package/test/unit/consolidate-dedupe.test.js +0 -174
- package/test/unit/consolidate-errors.test.js +0 -63
- package/test/unit/consolidate-gate.test.js +0 -26
- package/test/unit/consolidate-preview.test.js +0 -168
- package/test/unit/consolidate-protection.test.js +0 -156
- package/test/unit/consolidate-reason.test.js +0 -206
- package/test/unit/consolidate-retry.test.js +0 -91
- package/test/unit/consolidate-selection.test.js +0 -103
- package/test/unit/core.test.js +0 -77
- package/test/unit/coverage-rewrite.test.js +0 -38
- package/test/unit/dedupe-episodes.test.js +0 -34
- package/test/unit/dedupe-threshold.test.js +0 -14
- 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 -62
- 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/extract-question.test.js +0 -41
- package/test/unit/fact-guard.test.js +0 -51
- package/test/unit/fact-noise-llm.test.js +0 -44
- package/test/unit/facts-belief.test.js +0 -32
- package/test/unit/facts-compat.test.js +0 -62
- 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 -124
- package/test/unit/guard-runtime.test.js +0 -40
- package/test/unit/guardian.test.js +0 -91
- package/test/unit/health-consolidation.test.js +0 -52
- package/test/unit/health-fts-scope.test.js +0 -33
- package/test/unit/health-privacy.test.js +0 -102
- package/test/unit/health-summaries.test.js +0 -24
- package/test/unit/health-text-maintain.test.js +0 -33
- package/test/unit/health-today.test.js +0 -45
- 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/inhibition.test.js +0 -90
- 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/label-entities-skip.test.js +0 -18
- 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/maintain-dispute.test.js +0 -62
- package/test/unit/maintain-duration.test.js +0 -194
- 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/mood-saturation.test.js +0 -33
- package/test/unit/narrative-weave.test.js +0 -271
- 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/reflect-backfill.test.js +0 -158
- package/test/unit/reflect-coverage.test.js +0 -35
- package/test/unit/reflect-overview.test.js +0 -22
- package/test/unit/reflect-phrasing.test.js +0 -75
- package/test/unit/reflect-sampling.test.js +0 -80
- package/test/unit/reflect-source-budget.test.js +0 -24
- package/test/unit/relationship-dynamics.test.js +0 -29
- 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/revive-on-read.test.js +0 -84
- 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-autoconsolidate.test.js +0 -95
- package/test/unit/scheduler-autodedupe.test.js +0 -52
- package/test/unit/scheduler-autosleep.test.js +0 -50
- package/test/unit/scheduler-cadence.test.js +0 -33
- package/test/unit/scheduler-deep.test.js +0 -62
- package/test/unit/scheduler-tick-gate.test.js +0 -38
- package/test/unit/scheduler.test.js +0 -137
- 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/spacing.test.js +0 -14
- package/test/unit/store.test.js +0 -111
- package/test/unit/summon-left-slot.test.js +0 -51
- package/test/unit/summon-rank.test.js +0 -51
- package/test/unit/temporal-graph.test.js +0 -44
- package/test/unit/tiers-gist.test.js +0 -140
- 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-coverage.test.js +0 -34
- package/test/unit/vector-override.test.js +0 -37
- package/test/unit/vector-typed-array.test.js +0 -47
- package/test/unit/vector.test.js +0 -68
- package/test/unit/working.test.js +0 -99
|
@@ -1,156 +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(run) {
|
|
9
|
-
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-protect-'));
|
|
10
|
-
const cortex = createCortex({ config: { dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite'), features: { llm: true } }, llm: run.llm });
|
|
11
|
-
return { cortex, dir };
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
test('保护线按分位:最高的那批不能归档,低的可以', async () => {
|
|
15
|
-
const now = Date.now();
|
|
16
|
-
let ids = [];
|
|
17
|
-
const { cortex, dir } = withCortex({
|
|
18
|
-
llm: {
|
|
19
|
-
available: () => true,
|
|
20
|
-
complete: async () => JSON.stringify([
|
|
21
|
-
{ action: 'archive', ids: [ids[0]], reason: '最高重要度' },
|
|
22
|
-
{ action: 'archive', ids: [ids[3]], reason: '最低重要度' },
|
|
23
|
-
]),
|
|
24
|
-
},
|
|
25
|
-
});
|
|
26
|
-
try {
|
|
27
|
-
const specs = [1.0, 0.95, 0.8, 0.6];
|
|
28
|
-
specs.forEach((importance, index) => cortex.write({
|
|
29
|
-
text: `用户:重要度 ${importance} 的样本 ${index}`,
|
|
30
|
-
kind: 'utterance',
|
|
31
|
-
title: `样本 ${index}`,
|
|
32
|
-
importance,
|
|
33
|
-
explicit: true,
|
|
34
|
-
now,
|
|
35
|
-
}));
|
|
36
|
-
// write() 返回事件而不是痕迹,按重要度从高到低取实际 id。
|
|
37
|
-
ids = cortex.repos.traces.list({ scopeId: 'global', state: 'active', limit: 10 })
|
|
38
|
-
.sort((a, b) => Number(b.importance) - Number(a.importance))
|
|
39
|
-
.map((trace) => trace.id);
|
|
40
|
-
const result = await cortex.consolidation.run({ scopeId: 'global', force: true });
|
|
41
|
-
assert.equal(result.applied.length, 1, '只有低重要度那条能被归档');
|
|
42
|
-
assert.match(String(result.run.error), /rejected 1/);
|
|
43
|
-
const archived = cortex.repos.traces.get(ids[3]);
|
|
44
|
-
assert.equal(archived.state, 'archived');
|
|
45
|
-
const top = cortex.repos.traces.get(ids[0]);
|
|
46
|
-
assert.equal(top.state, 'active', '最高重要度必须仍活跃');
|
|
47
|
-
} finally {
|
|
48
|
-
cortex.close();
|
|
49
|
-
rmSync(dir, { recursive: true, force: true });
|
|
50
|
-
}
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
test('提示词里写的是本次保护线,而不是写死的 0.7', async () => {
|
|
54
|
-
let seen = '';
|
|
55
|
-
const now = Date.now();
|
|
56
|
-
const { cortex, dir } = withCortex({
|
|
57
|
-
llm: {
|
|
58
|
-
available: () => true,
|
|
59
|
-
complete: async (options) => {
|
|
60
|
-
seen = `${options?.system ?? ''}\n${options?.prompt ?? ''}`;
|
|
61
|
-
return JSON.stringify([{ action: 'keep', ids: [] }]);
|
|
62
|
-
},
|
|
63
|
-
},
|
|
64
|
-
});
|
|
65
|
-
try {
|
|
66
|
-
for (let i = 0; i < 6; i += 1) {
|
|
67
|
-
cortex.write({ text: `用户:样本 ${i}`, kind: 'utterance', title: `样本 ${i}`, importance: 0.7 + i * 0.05, explicit: true, now });
|
|
68
|
-
}
|
|
69
|
-
await cortex.consolidation.run({ scopeId: 'global', force: true });
|
|
70
|
-
assert.doesNotMatch(seen, /importance>=0\.7 /, '不应再写死 0.7');
|
|
71
|
-
assert.match(seen, /本次保护线 importance>=0\.9/);
|
|
72
|
-
assert.match(seen, /受保护/);
|
|
73
|
-
} finally {
|
|
74
|
-
cortex.close();
|
|
75
|
-
rmSync(dir, { recursive: true, force: true });
|
|
76
|
-
}
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
test('有完全重复的候选时,提示词会把重复组直接交给模型', async () => {
|
|
80
|
-
let seen = '';
|
|
81
|
-
const now = Date.now();
|
|
82
|
-
const { cortex, dir } = withCortex({
|
|
83
|
-
llm: {
|
|
84
|
-
available: () => true,
|
|
85
|
-
complete: async (options) => {
|
|
86
|
-
seen = String(options?.prompt ?? '');
|
|
87
|
-
return JSON.stringify([{ action: 'keep', ids: [] }]);
|
|
88
|
-
},
|
|
89
|
-
},
|
|
90
|
-
});
|
|
91
|
-
try {
|
|
92
|
-
for (let i = 0; i < 2; i += 1) {
|
|
93
|
-
const text = i === 0
|
|
94
|
-
? '用户:今天在图书馆看了超时空辉夜姬第三集,很好笑。'
|
|
95
|
-
: '用户:今天在图书馆看了超时空辉夜姬第三集,特别好笑。';
|
|
96
|
-
cortex.write({ text, kind: 'utterance', title: `重复 ${i}`, importance: 0.8, explicit: true, now });
|
|
97
|
-
}
|
|
98
|
-
await cortex.consolidation.run({ scopeId: 'global', force: true });
|
|
99
|
-
assert.match(seen, /已检测到这些重复组/, '应把重复组说出来');
|
|
100
|
-
assert.match(seen, /merge/, '并告诉它用 merge');
|
|
101
|
-
} finally {
|
|
102
|
-
cortex.close();
|
|
103
|
-
rmSync(dir, { recursive: true, force: true });
|
|
104
|
-
}
|
|
105
|
-
});
|
|
106
|
-
|
|
107
|
-
test('一组里两条受保护时,不把这组交给模型(merge 必被拒)', async () => {
|
|
108
|
-
let seen = '';
|
|
109
|
-
const now = Date.now();
|
|
110
|
-
const { cortex, dir } = withCortex({
|
|
111
|
-
llm: { available: () => true, complete: async (options) => { seen = String(options?.prompt ?? ''); return JSON.stringify([{ action: 'keep', ids: [] }]); } },
|
|
112
|
-
});
|
|
113
|
-
try {
|
|
114
|
-
const texts = [
|
|
115
|
-
'用户:今天在图书馆看了超时空辉夜姬第三集,很好笑。',
|
|
116
|
-
'用户:今天在图书馆看了超时空辉夜姬第三集,特别好笑。',
|
|
117
|
-
'用户:今天在图书馆看了超时空辉夜姬第三集,非常好笑。',
|
|
118
|
-
];
|
|
119
|
-
const importances = [0.95, 0.95, 0.6];
|
|
120
|
-
texts.forEach((text, i) => cortex.write({ text, kind: 'utterance', title: `重复 ${i}`, importance: importances[i], explicit: true, now }));
|
|
121
|
-
const result = await cortex.consolidation.run({ scopeId: 'global', force: true });
|
|
122
|
-
assert.doesNotMatch(seen, /已检测到这些重复组/, '两条受保护的组不该交出去');
|
|
123
|
-
const audit = cortex.db.prepare("SELECT detail_json FROM audit WHERE action = 'consolidation.run' ORDER BY at DESC LIMIT 1").get();
|
|
124
|
-
assert.equal(JSON.parse(audit.detail_json).groups, 0);
|
|
125
|
-
assert.equal(JSON.parse(audit.detail_json).groupsDropped, 1, '被丢掉的组要有数');
|
|
126
|
-
assert.ok(result.run);
|
|
127
|
-
} finally {
|
|
128
|
-
cortex.close();
|
|
129
|
-
rmSync(dir, { recursive: true, force: true });
|
|
130
|
-
}
|
|
131
|
-
});
|
|
132
|
-
|
|
133
|
-
test('一组里只有一条受保护时,照常交给模型', async () => {
|
|
134
|
-
const now = Date.now();
|
|
135
|
-
let seen = '';
|
|
136
|
-
const { cortex, dir } = withCortex({
|
|
137
|
-
llm: {
|
|
138
|
-
available: () => true,
|
|
139
|
-
complete: async (options) => {
|
|
140
|
-
seen = String(options?.prompt ?? '');
|
|
141
|
-
return JSON.stringify([{ action: 'keep', ids: [] }]);
|
|
142
|
-
},
|
|
143
|
-
},
|
|
144
|
-
});
|
|
145
|
-
try {
|
|
146
|
-
cortex.write({ text: '用户:今天在图书馆看了超时空辉夜姬第三集,很好笑。', kind: 'utterance', title: '重复 0', importance: 0.95, explicit: true, now });
|
|
147
|
-
cortex.write({ text: '用户:今天在图书馆看了超时空辉夜姬第三集,特别好笑。', kind: 'utterance', title: '重复 1', importance: 0.6, explicit: true, now });
|
|
148
|
-
await cortex.consolidation.run({ scopeId: 'global', force: true });
|
|
149
|
-
const audit = cortex.db.prepare("SELECT detail_json FROM audit WHERE action = 'consolidation.run' ORDER BY at DESC LIMIT 1").get();
|
|
150
|
-
assert.equal(JSON.parse(audit.detail_json).groups, 1);
|
|
151
|
-
assert.match(seen, /mustBeTarget/, '受保护的那条要作为结构约束出现');
|
|
152
|
-
} finally {
|
|
153
|
-
cortex.close();
|
|
154
|
-
rmSync(dir, { recursive: true, force: true });
|
|
155
|
-
}
|
|
156
|
-
});
|
|
@@ -1,206 +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 fakeLlm(reason) {
|
|
9
|
-
return {
|
|
10
|
-
available: () => true,
|
|
11
|
-
complete: async () => null,
|
|
12
|
-
lastFailure: () => reason,
|
|
13
|
-
};
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
test('空回复要带 adapter 给出的原因', async () => {
|
|
17
|
-
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-reason-'));
|
|
18
|
-
const cortex = createCortex({ config: { dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite'), features: { llm: true } }, llm: fakeLlm('empty-text:length') });
|
|
19
|
-
try {
|
|
20
|
-
const now = Date.now();
|
|
21
|
-
for (let i = 0; i < 3; i += 1) cortex.write({ text: `用户:短 ${i}`, kind: 'utterance', title: `短 ${i}`, explicit: true, now });
|
|
22
|
-
const result = await cortex.consolidation.run({ scopeId: 'global', force: true });
|
|
23
|
-
assert.equal(result.run.error, 'llm-empty:empty-text:length');
|
|
24
|
-
} finally {
|
|
25
|
-
cortex.close();
|
|
26
|
-
rmSync(dir, { recursive: true, force: true });
|
|
27
|
-
}
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
test('重试也失败时标成 llm-empty-retry,而不是和一次就空混在一起', async () => {
|
|
31
|
-
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-reason-retry-'));
|
|
32
|
-
const cortex = createCortex({ config: { dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite'), features: { llm: true } }, llm: fakeLlm('finish-error') });
|
|
33
|
-
try {
|
|
34
|
-
const now = Date.now();
|
|
35
|
-
for (let i = 0; i < 16; i += 1) {
|
|
36
|
-
cortex.write({ text: `用户:长 ${i} ${'细节'.repeat(200)}`, kind: 'utterance', title: `长 ${i}`, explicit: true, now });
|
|
37
|
-
}
|
|
38
|
-
const result = await cortex.consolidation.run({ scopeId: 'global', force: true });
|
|
39
|
-
assert.equal(result.run.error, 'llm-empty-retry:finish-error');
|
|
40
|
-
} finally {
|
|
41
|
-
cortex.close();
|
|
42
|
-
rmSync(dir, { recursive: true, force: true });
|
|
43
|
-
}
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
test('health 按原因分桶统计空回复', async () => {
|
|
47
|
-
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-reason-health-'));
|
|
48
|
-
const cortex = createCortex({ config: { dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite'), features: { llm: true } }, llm: fakeLlm('exception') });
|
|
49
|
-
try {
|
|
50
|
-
const now = Date.now();
|
|
51
|
-
for (let i = 0; i < 3; i += 1) cortex.write({ text: `用户:样本 ${i}`, kind: 'utterance', title: `样本 ${i}`, explicit: true, now });
|
|
52
|
-
for (let i = 0; i < 5; i += 1) await cortex.consolidation.run({ scopeId: 'global', force: true });
|
|
53
|
-
const health = cortex.health({ scopeId: 'global' });
|
|
54
|
-
const warn = health.issues.find((issue) => issue.code === 'LLM_EMPTY');
|
|
55
|
-
assert.ok(warn, '应报 LLM_EMPTY');
|
|
56
|
-
assert.equal(warn.detail?.count, 5);
|
|
57
|
-
assert.equal(warn.detail?.buckets?.['llm-empty:exception'], 5);
|
|
58
|
-
} finally {
|
|
59
|
-
cortex.close();
|
|
60
|
-
rmSync(dir, { recursive: true, force: true });
|
|
61
|
-
}
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
test('adapter 抛异常时记 exception-outer,而不是裸 llm-empty', async () => {
|
|
65
|
-
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-reason-throw-'));
|
|
66
|
-
const cortex = createCortex({
|
|
67
|
-
config: { dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite'), features: { llm: true } },
|
|
68
|
-
llm: { available: () => true, complete: async () => { throw new Error('boom'); } },
|
|
69
|
-
});
|
|
70
|
-
try {
|
|
71
|
-
const now = Date.now();
|
|
72
|
-
cortex.write({ text: '用户:样本。', kind: 'utterance', title: '样本', explicit: true, now });
|
|
73
|
-
const result = await cortex.consolidation.run({ scopeId: 'global', force: true });
|
|
74
|
-
assert.equal(result.run.error, 'llm-empty:exception-outer');
|
|
75
|
-
} finally {
|
|
76
|
-
cortex.close();
|
|
77
|
-
rmSync(dir, { recursive: true, force: true });
|
|
78
|
-
}
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
test('一次调用的原因不会被另一次调用的失败覆盖', async () => {
|
|
82
|
-
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-reason-scope-'));
|
|
83
|
-
const cortex = createCortex({
|
|
84
|
-
config: { dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite'), features: { llm: true } },
|
|
85
|
-
llm: {
|
|
86
|
-
available: () => true,
|
|
87
|
-
// 模拟 adapter:先回调本次调用的 onReason,再把实例级的 lastFailure 改掉。
|
|
88
|
-
complete: async (options) => {
|
|
89
|
-
options?.onReason?.('empty-text:max-tokens');
|
|
90
|
-
return null;
|
|
91
|
-
},
|
|
92
|
-
lastFailure: () => 'no-selection',
|
|
93
|
-
},
|
|
94
|
-
});
|
|
95
|
-
try {
|
|
96
|
-
const now = Date.now();
|
|
97
|
-
cortex.write({ text: '用户:样本。', kind: 'utterance', title: '样本', explicit: true, now });
|
|
98
|
-
const result = await cortex.consolidation.run({ scopeId: 'global', force: true });
|
|
99
|
-
assert.equal(result.run.error, 'llm-empty:empty-text:max-tokens');
|
|
100
|
-
} finally {
|
|
101
|
-
cortex.close();
|
|
102
|
-
rmSync(dir, { recursive: true, force: true });
|
|
103
|
-
}
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
test('巩固记账会记下是谁触发的,方便解释「一分钟两条」', async () => {
|
|
107
|
-
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-caller-'));
|
|
108
|
-
const cortex = createCortex({ config: { dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite') } });
|
|
109
|
-
try {
|
|
110
|
-
const now = Date.now();
|
|
111
|
-
cortex.write({ text: '用户:样本。', kind: 'utterance', title: '样本', explicit: true, now });
|
|
112
|
-
await cortex.consolidation.run({ scopeId: 'global', force: true, caller: 'auto-trace-delta' });
|
|
113
|
-
const row = cortex.db.prepare("SELECT detail_json FROM audit WHERE action = 'consolidation.run' ORDER BY at DESC LIMIT 1").get();
|
|
114
|
-
assert.ok(row, '应有一条巩固审计');
|
|
115
|
-
const detail = JSON.parse(row.detail_json);
|
|
116
|
-
assert.equal(detail.caller, 'auto-trace-delta');
|
|
117
|
-
assert.equal(detail.error, 'no-llm-adapter');
|
|
118
|
-
} finally {
|
|
119
|
-
cortex.close();
|
|
120
|
-
rmSync(dir, { recursive: true, force: true });
|
|
121
|
-
}
|
|
122
|
-
});
|
|
123
|
-
|
|
124
|
-
test('整批都在保护线上时,仍允许 update(提示词排除 archive/merge)', async () => {
|
|
125
|
-
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-all-protected-'));
|
|
126
|
-
let seen = '';
|
|
127
|
-
const cortex = createCortex({
|
|
128
|
-
config: { dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite'), features: { llm: true } },
|
|
129
|
-
llm: {
|
|
130
|
-
available: () => true,
|
|
131
|
-
complete: async (options) => {
|
|
132
|
-
seen = String(options?.prompt ?? '');
|
|
133
|
-
const id = cortex.repos.traces.list({ scopeId: 'global', state: 'active', limit: 1 })[0].id;
|
|
134
|
-
return JSON.stringify([{ action: 'update', ids: [id], content: '更正后的内容' }]);
|
|
135
|
-
},
|
|
136
|
-
},
|
|
137
|
-
});
|
|
138
|
-
try {
|
|
139
|
-
const now = Date.now();
|
|
140
|
-
for (let i = 0; i < 3; i += 1) cortex.write({ text: `用户:受保护样本 ${i}`, kind: 'utterance', title: `保护 ${i}`, importance: 0.95, explicit: true, now });
|
|
141
|
-
const result = await cortex.consolidation.run({ scopeId: 'global', force: true });
|
|
142
|
-
assert.match(seen, /本批全部在保护线上/, '提示词要说明只能 keep/update/conflict');
|
|
143
|
-
assert.equal(result.applied.length, 1, '受保护批次里的 update 应该能落地');
|
|
144
|
-
assert.equal(result.applied[0].action, 'update');
|
|
145
|
-
} finally {
|
|
146
|
-
cortex.close();
|
|
147
|
-
rmSync(dir, { recursive: true, force: true });
|
|
148
|
-
}
|
|
149
|
-
});
|
|
150
|
-
|
|
151
|
-
test('被拒时错误里带上原因分布', async () => {
|
|
152
|
-
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-reject-note-'));
|
|
153
|
-
let ids = [];
|
|
154
|
-
const cortex = createCortex({
|
|
155
|
-
config: { dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite'), features: { llm: true } },
|
|
156
|
-
llm: { available: () => true, complete: async () => JSON.stringify([{ action: 'archive', ids: [ids[0]] }]) },
|
|
157
|
-
});
|
|
158
|
-
try {
|
|
159
|
-
const now = Date.now();
|
|
160
|
-
for (let i = 0; i < 3; i += 1) cortex.write({ text: `用户:样本 ${i}`, kind: 'utterance', title: `样本 ${i}`, importance: i === 2 ? 0.6 : 0.95, explicit: true, now });
|
|
161
|
-
ids = cortex.repos.traces.list({ scopeId: 'global', state: 'active', limit: 10 }).map((trace) => trace.id);
|
|
162
|
-
const result = await cortex.consolidation.run({ scopeId: 'global', force: true, force: true });
|
|
163
|
-
assert.match(String(result.run.error), /rejected 1(protected×1)/);
|
|
164
|
-
} finally {
|
|
165
|
-
cortex.close();
|
|
166
|
-
rmSync(dir, { recursive: true, force: true });
|
|
167
|
-
}
|
|
168
|
-
});
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
test('invalid-json 分支也带 groups 记账', async () => {
|
|
172
|
-
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-invalid-json-'));
|
|
173
|
-
const cortex = createCortex({
|
|
174
|
-
config: { dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite'), features: { llm: true } },
|
|
175
|
-
llm: { available: () => true, complete: async () => '这不是 JSON' },
|
|
176
|
-
});
|
|
177
|
-
try {
|
|
178
|
-
const now = Date.now();
|
|
179
|
-
for (let i = 0; i < 3; i += 1) cortex.write({ text: `用户:样本 ${i}`, kind: 'utterance', title: `样本 ${i}`, importance: 0.7, explicit: true, now });
|
|
180
|
-
const result = await cortex.consolidation.run({ scopeId: 'global', force: true });
|
|
181
|
-
assert.equal(result.run.error, 'invalid-json');
|
|
182
|
-
const audit = cortex.db.prepare("SELECT detail_json FROM audit WHERE action = 'consolidation.run' ORDER BY at DESC LIMIT 1").get();
|
|
183
|
-
const detail = JSON.parse(audit.detail_json);
|
|
184
|
-
assert.equal(typeof detail.groups, 'number');
|
|
185
|
-
assert.equal(typeof detail.groupsDropped, 'number');
|
|
186
|
-
} finally {
|
|
187
|
-
cortex.close();
|
|
188
|
-
rmSync(dir, { recursive: true, force: true });
|
|
189
|
-
}
|
|
190
|
-
});
|
|
191
|
-
|
|
192
|
-
test('dedupe 同一批决策重跑会被 unchanged 跳过', async () => {
|
|
193
|
-
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-dedupe-unchanged-'));
|
|
194
|
-
const cortex = createCortex({ config: { dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite') } });
|
|
195
|
-
try {
|
|
196
|
-
const now = Date.now();
|
|
197
|
-
for (let i = 0; i < 3; i += 1) cortex.write({ text: `用户:完全不同的样本 ${i} ${['鲸','潮','灯'][i]}`, kind: 'utterance', title: `样本 ${i}`, importance: 0.6, explicit: true, now });
|
|
198
|
-
const first = await cortex.consolidation.dedupe({ scopeId: 'global', dryRun: false });
|
|
199
|
-
assert.equal(first.decisions.length, 0);
|
|
200
|
-
const second = await cortex.consolidation.dedupe({ scopeId: 'global', dryRun: false });
|
|
201
|
-
assert.equal(second.skipped, 'unchanged');
|
|
202
|
-
} finally {
|
|
203
|
-
cortex.close();
|
|
204
|
-
rmSync(dir, { recursive: true, force: true });
|
|
205
|
-
}
|
|
206
|
-
});
|
|
@@ -1,91 +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
|
-
test('提示词过长时先失败,会自动用精简候选重试一次并打上 -retry', async () => {
|
|
9
|
-
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-consolidate-retry-'));
|
|
10
|
-
let calls = 0;
|
|
11
|
-
const prompts = [];
|
|
12
|
-
const cortex = createCortex({
|
|
13
|
-
config: { dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite'), features: { llm: true } },
|
|
14
|
-
llm: {
|
|
15
|
-
available: () => true,
|
|
16
|
-
complete: async ({ prompt }) => {
|
|
17
|
-
calls += 1;
|
|
18
|
-
prompts.push(String(prompt).length);
|
|
19
|
-
if (calls === 1) return null;
|
|
20
|
-
const id = cortex.repos.traces.list({ scopeId: 'global', state: 'active', limit: 1 })[0].id;
|
|
21
|
-
return JSON.stringify([{ action: 'keep', ids: [id], reason: 'retry-ok' }]);
|
|
22
|
-
},
|
|
23
|
-
},
|
|
24
|
-
});
|
|
25
|
-
try {
|
|
26
|
-
const now = Date.now();
|
|
27
|
-
for (let i = 0; i < 16; i += 1) {
|
|
28
|
-
cortex.write({ text: `用户:第 ${i} 条很长的日志,${'细节'.repeat(200)}`, kind: 'utterance', title: `长样本 ${i}`, explicit: true, now });
|
|
29
|
-
}
|
|
30
|
-
const result = await cortex.consolidation.run({ scopeId: 'global', force: true });
|
|
31
|
-
assert.equal(calls, 2, '应该调用两次:一次原始、一次精简');
|
|
32
|
-
assert.ok(prompts[1] < prompts[0], '重试的提示词必须更短');
|
|
33
|
-
assert.equal(result.run.status, 'applied');
|
|
34
|
-
assert.match(String(result.run.model), /-retry$/, '成功的重试要打标记');
|
|
35
|
-
} finally {
|
|
36
|
-
cortex.close();
|
|
37
|
-
rmSync(dir, { recursive: true, force: true });
|
|
38
|
-
}
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
test('短提示词失败时不重试,直接记 llm-empty', async () => {
|
|
42
|
-
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-consolidate-noretry-'));
|
|
43
|
-
let calls = 0;
|
|
44
|
-
const cortex = createCortex({
|
|
45
|
-
config: { dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite'), features: { llm: true } },
|
|
46
|
-
llm: { available: () => true, complete: async () => { calls += 1; return null; } },
|
|
47
|
-
});
|
|
48
|
-
try {
|
|
49
|
-
const now = Date.now();
|
|
50
|
-
for (let i = 0; i < 3; i += 1) {
|
|
51
|
-
cortex.write({ text: `用户:短 ${i}`, kind: 'utterance', title: `短 ${i}`, explicit: true, now });
|
|
52
|
-
}
|
|
53
|
-
const result = await cortex.consolidation.run({ scopeId: 'global', force: true });
|
|
54
|
-
assert.equal(calls, 1, '短输入不该重试');
|
|
55
|
-
assert.equal(result.run.error, 'llm-empty');
|
|
56
|
-
} finally {
|
|
57
|
-
cortex.close();
|
|
58
|
-
rmSync(dir, { recursive: true, force: true });
|
|
59
|
-
}
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
test('大候选先用精简预算一次成功,并标 -bounded', async () => {
|
|
63
|
-
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-consolidate-bounded-'));
|
|
64
|
-
let calls = 0;
|
|
65
|
-
let maxTokensSeen = 0;
|
|
66
|
-
const cortex = createCortex({
|
|
67
|
-
config: { dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite'), features: { llm: true } },
|
|
68
|
-
llm: {
|
|
69
|
-
available: () => true,
|
|
70
|
-
complete: async (options) => {
|
|
71
|
-
calls += 1;
|
|
72
|
-
maxTokensSeen = options?.maxTokens ?? 0;
|
|
73
|
-
const id = cortex.repos.traces.list({ scopeId: 'global', state: 'active', limit: 1 })[0].id;
|
|
74
|
-
return JSON.stringify([{ action: 'keep', ids: [id] }]);
|
|
75
|
-
},
|
|
76
|
-
},
|
|
77
|
-
});
|
|
78
|
-
try {
|
|
79
|
-
const now = Date.now();
|
|
80
|
-
for (let i = 0; i < 40; i += 1) {
|
|
81
|
-
cortex.write({ text: `用户:大样本 ${i} ${'细节'.repeat(150)}`, kind: 'utterance', title: `大 ${i}`, explicit: true, now });
|
|
82
|
-
}
|
|
83
|
-
const result = await cortex.consolidation.run({ scopeId: 'global', force: true });
|
|
84
|
-
assert.equal(calls, 1, '大候选应该一次成功,不必先失败再重试');
|
|
85
|
-
assert.match(String(result.run.model), /-bounded$/);
|
|
86
|
-
assert.ok(maxTokensSeen >= 2000, `bounded 尝试的输出预算应足够(实际 ${maxTokensSeen})`);
|
|
87
|
-
} finally {
|
|
88
|
-
cortex.close();
|
|
89
|
-
rmSync(dir, { recursive: true, force: true });
|
|
90
|
-
}
|
|
91
|
-
});
|
|
@@ -1,103 +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 setup(complete) {
|
|
9
|
-
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-selection-'));
|
|
10
|
-
const cortex = createCortex({
|
|
11
|
-
config: { dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite'), features: { llm: true } },
|
|
12
|
-
llm: { available: () => true, complete },
|
|
13
|
-
});
|
|
14
|
-
return { dir, cortex };
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
test('候选优先挑低重要度痕迹,高重要度沉底', async () => {
|
|
18
|
-
const { dir, cortex } = setup(async () => JSON.stringify([{ action: 'keep', ids: [] }]));
|
|
19
|
-
try {
|
|
20
|
-
const now = Date.now();
|
|
21
|
-
cortex.write({ text: '用户:高重要度身份记忆。', kind: 'utterance', title: '高', importance: 0.95, explicit: true, now });
|
|
22
|
-
cortex.write({ text: '用户:低重要度碎片甲。', kind: 'utterance', title: '低甲', importance: 0.3, explicit: true, now: now + 1000 });
|
|
23
|
-
cortex.write({ text: '用户:中低重要度碎片乙。', kind: 'utterance', title: '低乙', importance: 0.5, explicit: true, now: now + 2000 });
|
|
24
|
-
const result = await cortex.consolidation.run({ scopeId: 'global', force: true });
|
|
25
|
-
const input = result.run.input;
|
|
26
|
-
assert.equal(input[0].title, '低甲', '最低重要度先问');
|
|
27
|
-
assert.equal(input.at(-1).title, '高', '高重要度沉到最后');
|
|
28
|
-
} finally {
|
|
29
|
-
cortex.close();
|
|
30
|
-
rmSync(dir, { recursive: true, force: true });
|
|
31
|
-
}
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
test('已被摘要覆盖的痕迹排在未覆盖之后', async () => {
|
|
35
|
-
const { dir, cortex } = setup(async () => JSON.stringify([{ action: 'keep', ids: [] }]));
|
|
36
|
-
try {
|
|
37
|
-
const now = Date.now();
|
|
38
|
-
cortex.write({ text: '用户:已经被总结过的一条。', kind: 'utterance', title: '已覆盖', importance: 0.3, explicit: true, now });
|
|
39
|
-
cortex.write({ text: '用户:还没被总结的一条。', kind: 'utterance', title: '未覆盖', importance: 0.3, explicit: true, now: now + 2000 });
|
|
40
|
-
const list = cortex.repos.traces.list({ scopeId: 'global', state: 'active', limit: 10 });
|
|
41
|
-
const covered = list.find((trace) => trace.title === '已覆盖');
|
|
42
|
-
cortex.repos.summaries.insert({ scopeId: 'global', level: 'episode', content: '一条旧摘要', sourceIds: [covered.id], coverage: 1 });
|
|
43
|
-
const result = await cortex.consolidation.run({ scopeId: 'global', force: true });
|
|
44
|
-
assert.equal(result.run.input[0].title, '未覆盖');
|
|
45
|
-
} finally {
|
|
46
|
-
cortex.close();
|
|
47
|
-
rmSync(dir, { recursive: true, force: true });
|
|
48
|
-
}
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
test('输入完全相同的第二次运行不再调用模型,也不写新行', async () => {
|
|
52
|
-
let calls = 0;
|
|
53
|
-
let traceId = null;
|
|
54
|
-
const { dir, cortex } = setup(async () => {
|
|
55
|
-
calls += 1;
|
|
56
|
-
return JSON.stringify([{ action: 'keep', ids: traceId ? [traceId] : [] }]);
|
|
57
|
-
});
|
|
58
|
-
try {
|
|
59
|
-
const now = Date.now();
|
|
60
|
-
cortex.write({ text: '用户:同一批里的一条。', kind: 'utterance', explicit: true, now });
|
|
61
|
-
traceId = cortex.repos.traces.list({ scopeId: 'global', state: 'active', limit: 1 })[0].id;
|
|
62
|
-
const first = await cortex.consolidation.run({ scopeId: 'global' });
|
|
63
|
-
assert.equal(calls, 1);
|
|
64
|
-
assert.equal(first.run.status, 'applied');
|
|
65
|
-
const second = await cortex.consolidation.run({ scopeId: 'global' });
|
|
66
|
-
assert.equal(second.skipped, 'unchanged', '同样的输入应直接复用上一次结果');
|
|
67
|
-
assert.equal(calls, 1, '不该再问一次模型');
|
|
68
|
-
assert.equal(cortex.consolidation.list({ scopeId: 'global', limit: 10 }).length, 1, '不该多写一行');
|
|
69
|
-
} finally {
|
|
70
|
-
cortex.close();
|
|
71
|
-
rmSync(dir, { recursive: true, force: true });
|
|
72
|
-
}
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
test('rejected 的同一批输入仍允许重试', async () => {
|
|
76
|
-
let calls = 0;
|
|
77
|
-
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-selection-retry-'));
|
|
78
|
-
const cortex = createCortex({
|
|
79
|
-
config: { dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite'), features: { llm: true } },
|
|
80
|
-
llm: {
|
|
81
|
-
available: () => true,
|
|
82
|
-
// 第一次返回没有 ids 的决策(会被判 rejected),第二次正常 keep。
|
|
83
|
-
complete: async () => {
|
|
84
|
-
calls += 1;
|
|
85
|
-
return calls === 1
|
|
86
|
-
? JSON.stringify([{ action: 'archive' }, { action: 'keep' }])
|
|
87
|
-
: JSON.stringify([{ action: 'keep', ids: [] }]);
|
|
88
|
-
},
|
|
89
|
-
},
|
|
90
|
-
});
|
|
91
|
-
try {
|
|
92
|
-
const now = Date.now();
|
|
93
|
-
cortex.write({ text: '用户:会被坏模型回答的一条。', kind: 'utterance', explicit: true, now });
|
|
94
|
-
const first = await cortex.consolidation.run({ scopeId: 'global' });
|
|
95
|
-
assert.equal(first.run.status, 'rejected');
|
|
96
|
-
const second = await cortex.consolidation.run({ scopeId: 'global' });
|
|
97
|
-
assert.equal(calls, 2, 'rejected 说明模型坏了,应允许重试');
|
|
98
|
-
assert.notEqual(second.skipped, 'unchanged');
|
|
99
|
-
} finally {
|
|
100
|
-
cortex.close();
|
|
101
|
-
rmSync(dir, { recursive: true, force: true });
|
|
102
|
-
}
|
|
103
|
-
});
|
package/test/unit/core.test.js
DELETED
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
import test from 'node:test';
|
|
2
|
-
import assert from 'node:assert/strict';
|
|
3
|
-
import { makeId, hashText, chainHash } from '../../src/core/ids.js';
|
|
4
|
-
import { normalizeConfig } from '../../src/core/config.js';
|
|
5
|
-
import { parseTimeWindow, daysBetween } from '../../src/core/time.js';
|
|
6
|
-
import { stableStringify } from '../../src/core/json.js';
|
|
7
|
-
import { tokenList, tokens, ftsText, buildMatchQuery, jaccard, estimateTokens } from '../../src/memory/tokenizer.js';
|
|
8
|
-
|
|
9
|
-
test('makeId is prefixed and lexically ordered by time', () => {
|
|
10
|
-
const a = makeId('trc', 1000);
|
|
11
|
-
const b = makeId('trc', 2000);
|
|
12
|
-
assert.match(a, /^trc_/);
|
|
13
|
-
assert.ok(a < b);
|
|
14
|
-
assert.notEqual(a, b);
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
test('hashText and chainHash are stable', () => {
|
|
18
|
-
assert.equal(hashText('米饭'), hashText('米饭'));
|
|
19
|
-
assert.notEqual(hashText('米饭'), hashText('面条'));
|
|
20
|
-
assert.equal(chainHash(null, 'x'), chainHash(null, 'x'));
|
|
21
|
-
assert.notEqual(chainHash(null, 'x'), chainHash('a', 'x'));
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
test('stableStringify sorts object keys and rejects cycles', () => {
|
|
25
|
-
assert.equal(stableStringify({ b: 1, a: 2 }), '{"a":2,"b":1}');
|
|
26
|
-
const cycle = {};
|
|
27
|
-
cycle.self = cycle;
|
|
28
|
-
assert.throws(() => stableStringify(cycle));
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
test('tokenizer builds CJK bigrams and latin terms', () => {
|
|
32
|
-
const list = tokenList('用户说米饭很好吃 DSH-plugin 2026');
|
|
33
|
-
assert.ok(list.includes('米饭'));
|
|
34
|
-
assert.ok(list.includes('用户'));
|
|
35
|
-
assert.ok(list.includes('dsh'));
|
|
36
|
-
assert.ok(list.includes('plugin'));
|
|
37
|
-
assert.ok(tokens('米饭').has('米饭'));
|
|
38
|
-
assert.equal(ftsText('米饭'), '米饭');
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
test('FTS match query quotes tokens and supports AND', () => {
|
|
42
|
-
assert.equal(buildMatchQuery('米饭 用户'), '"米饭" OR "用户"');
|
|
43
|
-
assert.equal(buildMatchQuery('米饭 用户', { mode: 'and' }), '"米饭" AND "用户"');
|
|
44
|
-
assert.equal(buildMatchQuery(''), '');
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
test('jaccard and estimateTokens are bounded', () => {
|
|
48
|
-
assert.equal(jaccard('米饭 用户', '米饭 用户'), 1);
|
|
49
|
-
assert.ok(jaccard('米饭 用户', '插件 安装') < 0.2);
|
|
50
|
-
assert.ok(estimateTokens('') === 0);
|
|
51
|
-
assert.ok(estimateTokens('米饭') >= 1);
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
test('config normalizes paths and clamps numbers', () => {
|
|
55
|
-
const config = normalizeConfig({
|
|
56
|
-
dataRoot: 'relative-data',
|
|
57
|
-
dbFile: ':memory:',
|
|
58
|
-
contextBudget: 999999,
|
|
59
|
-
recallTimeoutMs: 1,
|
|
60
|
-
dsr: { decay: -2, archiveRetrievability: 2 },
|
|
61
|
-
});
|
|
62
|
-
assert.ok(config.dataRoot.endsWith('relative-data'));
|
|
63
|
-
assert.equal(config.dbFile, ':memory:');
|
|
64
|
-
assert.equal(config.contextBudget, 20000);
|
|
65
|
-
assert.equal(config.recallTimeoutMs, 100);
|
|
66
|
-
assert.equal(config.dsr.decay, -1);
|
|
67
|
-
assert.equal(config.dsr.archiveRetrievability, 0.9);
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
test('time helpers parse windows and age', () => {
|
|
71
|
-
const now = Date.parse('2026-11-11T00:00:00.000Z');
|
|
72
|
-
const today = parseTimeWindow('今天做了什么', now);
|
|
73
|
-
assert.ok(today.from < today.to);
|
|
74
|
-
const ago = parseTimeWindow('3天前的事', now);
|
|
75
|
-
assert.ok(ago.from < ago.to);
|
|
76
|
-
assert.ok(daysBetween('2026-11-01T00:00:00.000Z', now) >= 9);
|
|
77
|
-
});
|
|
@@ -1,38 +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 { openDatabase, migrate } from '../../src/store/database.js';
|
|
7
|
-
import { recomputeEpisodeCoverage } from '../../src/engines/summary-coverage.js';
|
|
8
|
-
|
|
9
|
-
test('重算 v1 摘要的 coverage,按重要度加权,并跳过已 v2 的', () => {
|
|
10
|
-
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-coverage-rewrite-'));
|
|
11
|
-
const { db } = openDatabase({ dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite') });
|
|
12
|
-
migrate(db);
|
|
13
|
-
try {
|
|
14
|
-
const now = '2026-11-15T10:00:00.000Z';
|
|
15
|
-
db.prepare("INSERT INTO events(id, seq, at, kind, scope_id, payload_json, hash) VALUES ('evt_1', 1, ?, 'observation', 'global', '{}', 'h0')").run(now);
|
|
16
|
-
const insert = db.prepare(`INSERT INTO traces(id, scope_id, kind, state, title, content, event_id, hash, recorded_at, updated_at, created_at, importance)
|
|
17
|
-
VALUES (?, 'global', 'observation', 'active', ?, ?, 'evt_1', ?, ?, ?, ?, ?)`);
|
|
18
|
-
// 窗口内:2 条重要(0.9)+ 2 条琐碎(0.1);摘要只覆盖了 2 条重要
|
|
19
|
-
insert.run('t1', '重要一', '正文', 'h1', '2026-11-15T09:00:00.000Z', now, now, 0.9);
|
|
20
|
-
insert.run('t2', '重要二', '正文', 'h2', '2026-11-15T09:30:00.000Z', now, now, 0.9);
|
|
21
|
-
insert.run('t3', '琐碎一', '正文', 'h3', '2026-11-15T08:00:00.000Z', now, now, 0.1);
|
|
22
|
-
insert.run('t4', '琐碎二', '正文', 'h4', '2026-11-15T07:00:00.000Z', now, now, 0.1);
|
|
23
|
-
|
|
24
|
-
db.prepare(`INSERT INTO summaries(id, scope_id, level, period_start, period_end, content, source_ids_json, coverage, model, created_at)
|
|
25
|
-
VALUES ('s_v1', 'global', 'episode', '2026-11-15T06:00:00.000Z', '2026-11-15T10:00:00.000Z', '旧摘要', '["t1","t2"]', 0.05, 'extractive-v1', ?)`).run(now);
|
|
26
|
-
db.prepare(`INSERT INTO summaries(id, scope_id, level, period_start, period_end, content, source_ids_json, coverage, model, created_at)
|
|
27
|
-
VALUES ('s_v2', 'global', 'episode', '2026-11-15T06:00:00.000Z', '2026-11-15T10:00:00.000Z', '新摘要', '["t1"]', 0.8, 'extractive-v2-weighted', ?)`).run(now);
|
|
28
|
-
|
|
29
|
-
const result = recomputeEpisodeCoverage({ db, scopeId: 'global' });
|
|
30
|
-
assert.equal(result.candidates, 1, '只应候选 v1 摘要');
|
|
31
|
-
assert.equal(result.updates.length, 1);
|
|
32
|
-
// covered = 0.9+0.9 = 1.8;total = 1.8+0.1+0.1 = 2.0 → 0.9
|
|
33
|
-
assert.equal(result.updates[0].to, 0.9);
|
|
34
|
-
} finally {
|
|
35
|
-
db.close();
|
|
36
|
-
rmSync(dir, { recursive: true, force: true });
|
|
37
|
-
}
|
|
38
|
-
});
|