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,141 +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() {
|
|
9
|
-
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-auto-reflect-'));
|
|
10
|
-
const cortex = createCortex({ config: { dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite') } });
|
|
11
|
-
return { dir, cortex };
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
function fill(cortex, count, now, prefix = '日常') {
|
|
15
|
-
for (let i = 0; i < count; i += 1) {
|
|
16
|
-
cortex.write({ text: `用户:${prefix}第 ${i} 条,记录一下天气和心情。`, kind: 'utterance', title: `${prefix} ${i}`, explicit: true, now });
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
test('材料够时自动写一条 episode,同一窗口内不再写第二条', () => {
|
|
21
|
-
const { dir, cortex } = setup();
|
|
22
|
-
try {
|
|
23
|
-
const now = Date.now();
|
|
24
|
-
fill(cortex, 60, now);
|
|
25
|
-
const first = cortex.maintain({ now });
|
|
26
|
-
assert.equal(first.reflection?.created, true, '材料够时应该生成摘要');
|
|
27
|
-
assert.equal(cortex.repos.summaries.list('global', { level: 'episode', limit: 5 }).length, 1);
|
|
28
|
-
|
|
29
|
-
const second = cortex.maintain({ now: now + 60_000 });
|
|
30
|
-
assert.equal(second.reflection?.skipped, 'covered', '同一 24h 窗口只写一条');
|
|
31
|
-
assert.equal(cortex.repos.summaries.list('global', { level: 'episode', limit: 5 }).length, 1);
|
|
32
|
-
} finally {
|
|
33
|
-
cortex.close();
|
|
34
|
-
rmSync(dir, { recursive: true, force: true });
|
|
35
|
-
}
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
test('材料不足以下界时不写,并报出下界', () => {
|
|
39
|
-
const { dir, cortex } = setup();
|
|
40
|
-
try {
|
|
41
|
-
const now = Date.now();
|
|
42
|
-
fill(cortex, 40, now, '稀疏');
|
|
43
|
-
const report = cortex.maintain({ now });
|
|
44
|
-
assert.equal(report.reflection?.skipped, 'few-traces');
|
|
45
|
-
assert.equal(report.reflection?.minMaterial, 50);
|
|
46
|
-
assert.equal(cortex.repos.summaries.list('global', { level: 'episode', limit: 5 }).length, 0);
|
|
47
|
-
} finally {
|
|
48
|
-
cortex.close();
|
|
49
|
-
rmSync(dir, { recursive: true, force: true });
|
|
50
|
-
}
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
test('真实形态:痕迹铺满 24 小时时,7 小时后仍判为已覆盖', () => {
|
|
54
|
-
const { dir, cortex } = setup();
|
|
55
|
-
try {
|
|
56
|
-
const now = Date.now();
|
|
57
|
-
// 铺满整整一天:periodStart 会落在 now-24h 附近,这正是线上真实数据的形状。
|
|
58
|
-
for (let i = 0; i < 60; i += 1) {
|
|
59
|
-
cortex.write({ text: `用户:铺满窗口的第 ${i} 条记录,间隔二十四分钟。`, kind: 'utterance', title: `铺满 ${i}`, explicit: true, now: now - i * 24 * 60_000 });
|
|
60
|
-
}
|
|
61
|
-
assert.equal(cortex.maintain({ now }).reflection?.created, true);
|
|
62
|
-
const later = cortex.maintain({ now: now + 7 * 3_600_000 });
|
|
63
|
-
assert.equal(later.reflection?.skipped, 'covered', '铺满窗口时也必须按写入时间去重');
|
|
64
|
-
assert.equal(cortex.repos.summaries.list('global', { level: 'episode', limit: 5 }).length, 1);
|
|
65
|
-
} finally {
|
|
66
|
-
cortex.close();
|
|
67
|
-
rmSync(dir, { recursive: true, force: true });
|
|
68
|
-
}
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
test('跨过 24 小时之后可以再写一条', () => {
|
|
72
|
-
const { dir, cortex } = setup();
|
|
73
|
-
try {
|
|
74
|
-
const now = Date.now();
|
|
75
|
-
fill(cortex, 60, now, '跨天');
|
|
76
|
-
assert.equal(cortex.maintain({ now }).reflection?.created, true);
|
|
77
|
-
fill(cortex, 60, now + 25 * 3_600_000, '第二天');
|
|
78
|
-
const nextDay = cortex.maintain({ now: now + 25 * 3_600_000 });
|
|
79
|
-
assert.equal(nextDay.reflection?.created, true, '第二天应该允许再写一条');
|
|
80
|
-
assert.equal(cortex.repos.summaries.list('global', { level: 'episode', limit: 5 }).length, 2);
|
|
81
|
-
} finally {
|
|
82
|
-
cortex.close();
|
|
83
|
-
rmSync(dir, { recursive: true, force: true });
|
|
84
|
-
}
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
test('窗口已覆盖但新增材料 ≥150 且距上次 ≥6h,可以再写一段不重叠的', () => {
|
|
88
|
-
const { dir, cortex } = setup();
|
|
89
|
-
try {
|
|
90
|
-
const now = Date.now();
|
|
91
|
-
fill(cortex, 60, now, '第一段');
|
|
92
|
-
assert.equal(cortex.maintain({ now }).reflection?.created, true);
|
|
93
|
-
const first = cortex.repos.summaries.list('global', { level: 'episode', limit: 5 })[0];
|
|
94
|
-
|
|
95
|
-
const later = now + 7 * 3_600_000;
|
|
96
|
-
fill(cortex, 160, later, '第二段');
|
|
97
|
-
const report = cortex.maintain({ now: later });
|
|
98
|
-
assert.equal(report.reflection?.created, true, '材料够就该写第二段');
|
|
99
|
-
const episodes = cortex.repos.summaries.list('global', { level: 'episode', limit: 5 });
|
|
100
|
-
assert.equal(episodes.length, 2);
|
|
101
|
-
const second = episodes[0];
|
|
102
|
-
assert.ok(Date.parse(second.periodStart) > Date.parse(first.periodEnd), '第二段必须接在第一段结束之后,不重叠');
|
|
103
|
-
assert.equal(second.model, 'extractive-v2-segment', '分段摘要要打上标记');
|
|
104
|
-
} finally {
|
|
105
|
-
cortex.close();
|
|
106
|
-
rmSync(dir, { recursive: true, force: true });
|
|
107
|
-
}
|
|
108
|
-
});
|
|
109
|
-
|
|
110
|
-
test('新增材料不够 150 时仍是 covered,不写第二段', () => {
|
|
111
|
-
const { dir, cortex } = setup();
|
|
112
|
-
try {
|
|
113
|
-
const now = Date.now();
|
|
114
|
-
fill(cortex, 60, now, '第一段');
|
|
115
|
-
assert.equal(cortex.maintain({ now }).reflection?.created, true);
|
|
116
|
-
const later = now + 7 * 3_600_000;
|
|
117
|
-
fill(cortex, 60, later, '第二段');
|
|
118
|
-
const report = cortex.maintain({ now: later });
|
|
119
|
-
assert.equal(report.reflection?.skipped, 'covered');
|
|
120
|
-
assert.equal(cortex.repos.summaries.list('global', { level: 'episode', limit: 5 }).length, 1);
|
|
121
|
-
} finally {
|
|
122
|
-
cortex.close();
|
|
123
|
-
rmSync(dir, { recursive: true, force: true });
|
|
124
|
-
}
|
|
125
|
-
});
|
|
126
|
-
|
|
127
|
-
test('材料极少时一条都不写', () => {
|
|
128
|
-
const { dir, cortex } = setup();
|
|
129
|
-
try {
|
|
130
|
-
const now = Date.now();
|
|
131
|
-
for (let i = 0; i < 3; i += 1) {
|
|
132
|
-
cortex.write({ text: `用户:只有第 ${i} 条。`, kind: 'utterance', title: `少量 ${i}`, explicit: true, now });
|
|
133
|
-
}
|
|
134
|
-
const report = cortex.maintain({ now });
|
|
135
|
-
assert.equal(report.reflection?.skipped, 'few-traces');
|
|
136
|
-
assert.equal(cortex.repos.summaries.list('global', { level: 'episode', limit: 5 }).length, 0);
|
|
137
|
-
} finally {
|
|
138
|
-
cortex.close();
|
|
139
|
-
rmSync(dir, { recursive: true, force: true });
|
|
140
|
-
}
|
|
141
|
-
});
|
|
@@ -1,53 +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() {
|
|
9
|
-
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-autonomy-'));
|
|
10
|
-
const cortex = createCortex({ config: { dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite') } });
|
|
11
|
-
return { dir, cortex };
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
test('autonomy tick executes consolidate and recall, leaves external proposals', () => {
|
|
15
|
-
const { dir, cortex } = setup();
|
|
16
|
-
try {
|
|
17
|
-
const now = Date.parse('2026-11-11T12:00:00+08:00');
|
|
18
|
-
cortex.write({ text: '第一条记忆。', explicit: true, now: now - 2000 });
|
|
19
|
-
cortex.write({ text: '第二条记忆。', explicit: true, now: now - 1000 });
|
|
20
|
-
const consolidate = cortex.stores.initiativeStore.insert({
|
|
21
|
-
kind: 'consolidate', priority: 0.9, reason: '整理', payload: {}, createdAt: new Date(now).toISOString(),
|
|
22
|
-
});
|
|
23
|
-
const recall = cortex.stores.initiativeStore.insert({
|
|
24
|
-
kind: 'recall', priority: 0.8, reason: '找记忆', payload: { topic: '记忆' }, createdAt: new Date(now).toISOString(),
|
|
25
|
-
});
|
|
26
|
-
const external = cortex.stores.initiativeStore.insert({
|
|
27
|
-
kind: 'external', priority: 0.7, reason: '发消息', payload: {}, createdAt: new Date(now).toISOString(),
|
|
28
|
-
});
|
|
29
|
-
const result = cortex.autonomy.tick({ at: now, max: 3 });
|
|
30
|
-
assert.equal(result.executed.length, 2);
|
|
31
|
-
assert.equal(result.proposals.length, 1);
|
|
32
|
-
assert.equal(result.proposals[0].requires, 'host');
|
|
33
|
-
assert.equal(cortex.stores.initiativeStore.get(consolidate.id).status, 'acted');
|
|
34
|
-
assert.equal(cortex.stores.initiativeStore.get(recall.id).status, 'acted');
|
|
35
|
-
assert.equal(cortex.stores.initiativeStore.get(external.id).status, 'pending');
|
|
36
|
-
} finally {
|
|
37
|
-
cortex.close();
|
|
38
|
-
rmSync(dir, { recursive: true, force: true });
|
|
39
|
-
}
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
test('autonomy status is readable', () => {
|
|
43
|
-
const { dir, cortex } = setup();
|
|
44
|
-
try {
|
|
45
|
-
cortex.stores.initiativeStore.insert({ kind: 'question', priority: 0.6, reason: '确认', payload: {}, createdAt: new Date().toISOString() });
|
|
46
|
-
const status = cortex.autonomy.status();
|
|
47
|
-
assert.equal(status.pending, 1);
|
|
48
|
-
assert.equal(status.byKind.question, 1);
|
|
49
|
-
} finally {
|
|
50
|
-
cortex.close();
|
|
51
|
-
rmSync(dir, { recursive: true, force: true });
|
|
52
|
-
}
|
|
53
|
-
});
|
package/test/unit/batch.test.js
DELETED
|
@@ -1,46 +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('writeMany rolls back the whole chunk and does not count rolled-back writes', () => {
|
|
9
|
-
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-batch-rollback-'));
|
|
10
|
-
const cortex = createCortex({ config: { dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite') } });
|
|
11
|
-
try {
|
|
12
|
-
cortex.write({ text: '原始记忆。', explicit: true, operationId: 'dup' });
|
|
13
|
-
const before = cortex.metrics().counters['writes.created'] ?? 0;
|
|
14
|
-
assert.throws(() => cortex.writeMany([
|
|
15
|
-
{ text: '冲突记忆。', explicit: true, operationId: 'dup' },
|
|
16
|
-
{ text: '同批另一条。', explicit: true, operationId: 'other' },
|
|
17
|
-
], { chunkSize: 10 }));
|
|
18
|
-
assert.equal(cortex.repos.traces.count(), 1);
|
|
19
|
-
assert.equal(cortex.metrics().counters['writes.created'] ?? 0, before);
|
|
20
|
-
} finally {
|
|
21
|
-
cortex.close();
|
|
22
|
-
rmSync(dir, { recursive: true, force: true });
|
|
23
|
-
}
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
test('writeMany commits a batch and is idempotent per operationId', () => {
|
|
27
|
-
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-batch-'));
|
|
28
|
-
const cortex = createCortex({ config: { dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite') } });
|
|
29
|
-
try {
|
|
30
|
-
const inputs = Array.from({ length: 25 }, (_, i) => ({
|
|
31
|
-
text: `批量记忆 ${i}:用户和助手在写模块。`,
|
|
32
|
-
explicit: true,
|
|
33
|
-
operationId: `batch:${i}`,
|
|
34
|
-
}));
|
|
35
|
-
const first = cortex.writeMany(inputs, { chunkSize: 10 });
|
|
36
|
-
assert.equal(first.length, 25);
|
|
37
|
-
assert.equal(first.filter((item) => item.trace).length, 25);
|
|
38
|
-
assert.equal(cortex.repos.traces.count(), 25);
|
|
39
|
-
const second = cortex.writeMany(inputs, { chunkSize: 10 });
|
|
40
|
-
assert.equal(second.filter((item) => item.skipped).length, 25);
|
|
41
|
-
assert.equal(cortex.repos.traces.count(), 25);
|
|
42
|
-
} finally {
|
|
43
|
-
cortex.close();
|
|
44
|
-
rmSync(dir, { recursive: true, force: true });
|
|
45
|
-
}
|
|
46
|
-
});
|
package/test/unit/bench.test.js
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import test from 'node:test';
|
|
2
|
-
import assert from 'node:assert/strict';
|
|
3
|
-
import { recallAtK, precisionAtK, reciprocalRank, ndcgAtK, percentile, evaluateQueries } from '../../bench/metrics.js';
|
|
4
|
-
import { generateSynthetic } from '../../bench/synthetic.js';
|
|
5
|
-
import { runBenchmark } from '../../bench/run.js';
|
|
6
|
-
|
|
7
|
-
test('ranking metrics are correct on a simple list', () => {
|
|
8
|
-
const ranked = ['a', 'b', 'c', 'd'];
|
|
9
|
-
const relevant = ['b', 'd'];
|
|
10
|
-
assert.equal(recallAtK(ranked, relevant, 2), 0.5);
|
|
11
|
-
assert.equal(precisionAtK(ranked, relevant, 2), 0.5);
|
|
12
|
-
assert.equal(reciprocalRank(ranked, relevant, 4), 0.5);
|
|
13
|
-
assert.ok(ndcgAtK(ranked, relevant, 4) > 0 && ndcgAtK(ranked, relevant, 4) <= 1);
|
|
14
|
-
assert.equal(percentile([1, 2, 3, 4], 50), 2);
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
test('synthetic workload is deterministic and has relevant ids', () => {
|
|
18
|
-
const a = generateSynthetic({ count: 50, queries: 10, seed: 7 });
|
|
19
|
-
const b = generateSynthetic({ count: 50, queries: 10, seed: 7 });
|
|
20
|
-
assert.deepEqual(a.traces.map((trace) => trace.id), b.traces.map((trace) => trace.id));
|
|
21
|
-
assert.ok(a.queries.every((query) => query.relevant.length >= 1));
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
test('evaluateQueries aggregates metrics', () => {
|
|
25
|
-
const result = evaluateQueries([
|
|
26
|
-
{ items: [{ id: 'a' }, { id: 'b' }], relevant: ['b'] },
|
|
27
|
-
{ items: [{ id: 'c' }], relevant: ['c'] },
|
|
28
|
-
], { k: 2 });
|
|
29
|
-
assert.equal(result.n, 2);
|
|
30
|
-
assert.equal(result.recallAtK, 1);
|
|
31
|
-
assert.equal(result.mrrAtK, (0.5 + 1) / 2);
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
test('small end-to-end benchmark returns throughput and recall', () => {
|
|
35
|
-
const result = runBenchmark({ count: 80, queries: 10, seed: 3, limit: 5 });
|
|
36
|
-
assert.equal(result.count, 80);
|
|
37
|
-
assert.ok(result.writeMs >= 0);
|
|
38
|
-
assert.ok(result.search.p50Ms >= 0);
|
|
39
|
-
assert.ok(result.search.recallAtK > 0);
|
|
40
|
-
});
|
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
import test from 'node:test';
|
|
2
|
-
import assert from 'node:assert/strict';
|
|
3
|
-
import { generateSynthetic } from '../../bench/synthetic.js';
|
|
4
|
-
import { evaluateQueries, recallAtK, hitAtK, precisionAtK, reciprocalRank, ndcgAtK } from '../../bench/metrics.js';
|
|
5
|
-
|
|
6
|
-
test('synthetic benchmark queries name one unique needle', () => {
|
|
7
|
-
const dataset = generateSynthetic({ count: 50, queries: 10, seed: 7 });
|
|
8
|
-
assert.equal(dataset.traces.length, 50);
|
|
9
|
-
assert.equal(dataset.queries.length, 10);
|
|
10
|
-
for (const query of dataset.queries) {
|
|
11
|
-
assert.equal(query.type, 'needle');
|
|
12
|
-
assert.equal(query.relevant.length, 1);
|
|
13
|
-
assert.match(query.targetId, /^trc_syn_\d+$/);
|
|
14
|
-
const index = Number(query.targetId.slice('trc_syn_'.length));
|
|
15
|
-
assert.ok(query.query.includes(`第 ${index} 条合成记忆`));
|
|
16
|
-
assert.ok(dataset.traces[index].text.includes(`第 ${index} 条合成记忆`));
|
|
17
|
-
}
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
test('synthetic benchmark is deterministic for a fixed seed', () => {
|
|
21
|
-
const a = generateSynthetic({ count: 30, queries: 5, seed: 11 });
|
|
22
|
-
const b = generateSynthetic({ count: 30, queries: 5, seed: 11 });
|
|
23
|
-
assert.deepEqual(a.queries, b.queries);
|
|
24
|
-
assert.deepEqual(a.traces, b.traces);
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
test('theme and paraphrase queries expose different retrieval tasks', () => {
|
|
28
|
-
const theme = generateSynthetic({ count: 80, queries: 12, seed: 3, task: 'theme' });
|
|
29
|
-
assert.equal(theme.task, 'theme');
|
|
30
|
-
assert.ok(theme.queries.every((query) => query.type === 'theme'));
|
|
31
|
-
assert.ok(theme.queries.every((query) => query.query === query.targetToken));
|
|
32
|
-
assert.ok(theme.queries.every((query) => query.relevant.length >= 2));
|
|
33
|
-
|
|
34
|
-
const paraphrase = generateSynthetic({ count: 80, queries: 12, seed: 3, task: 'paraphrase' });
|
|
35
|
-
assert.equal(paraphrase.task, 'paraphrase');
|
|
36
|
-
assert.ok(paraphrase.queries.every((query) => query.type === 'paraphrase'));
|
|
37
|
-
assert.ok(paraphrase.queries.every((query) => query.query !== query.targetToken));
|
|
38
|
-
assert.ok(paraphrase.queries.every((query) => query.relevant.length >= 2));
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
test('retrieval metrics score a perfect ranking as one', () => {
|
|
42
|
-
const ranked = ['a', 'b'];
|
|
43
|
-
assert.equal(recallAtK(ranked, ['a'], 10), 1);
|
|
44
|
-
assert.equal(hitAtK(ranked, ['a'], 10), 1);
|
|
45
|
-
assert.equal(precisionAtK(ranked, ['a'], 10), 0.5);
|
|
46
|
-
assert.equal(reciprocalRank(ranked, ['a'], 10), 1);
|
|
47
|
-
assert.equal(ndcgAtK(ranked, ['a'], 10), 1);
|
|
48
|
-
const metrics = evaluateQueries([{ items: [{ id: 'a' }], relevant: ['a'] }], { k: 10 });
|
|
49
|
-
assert.equal(metrics.recallAtK, 1);
|
|
50
|
-
assert.equal(metrics.hitAtK, 1);
|
|
51
|
-
assert.equal(metrics.mrrAtK, 1);
|
|
52
|
-
});
|
|
@@ -1,296 +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
|
-
import { createHostRuntime, stripInjected, deriveHostTitle, contentText } from '../../src/host/runtime.js';
|
|
8
|
-
import { isInjectedNoise } from '../../src/engines/encode.js';
|
|
9
|
-
|
|
10
|
-
function setup(config = {}) {
|
|
11
|
-
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-capture-filter-'));
|
|
12
|
-
const cortex = createCortex({ config: { dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite'), ...config } });
|
|
13
|
-
return { dir, cortex };
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
const RUNTIME_CONTEXT = [
|
|
17
|
-
'Time sampled while preparing turn 33, step 1: 2026-11-15T00:57:58+08:00[Asia/Shanghai]',
|
|
18
|
-
'Browser time zone for this request: Asia/Shanghai. Interpret otherwise-unqualified dates and times in this zone.',
|
|
19
|
-
'Elapsed since the preceding model-visible message: 27m 56s.',
|
|
20
|
-
'用户说想吃米饭。',
|
|
21
|
-
].join('\n');
|
|
22
|
-
|
|
23
|
-
test('stripInjected drops the runtime header and keeps the human line', () => {
|
|
24
|
-
const cleaned = stripInjected(RUNTIME_CONTEXT);
|
|
25
|
-
assert.equal(cleaned, '用户说想吃米饭。');
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
test('stripInjected removes system-reminder blocks and pure boilerplate becomes empty', () => {
|
|
29
|
-
const withReminder = '<system-reminder>\nThe available skill catalog changed.\n</system-reminder>\n帮咱看看记忆。';
|
|
30
|
-
assert.equal(stripInjected(withReminder), '帮咱看看记忆。');
|
|
31
|
-
const pure = '<system-reminder>\nCurrent runtime context. This snapshot supersedes earlier versions.\n</system-reminder>';
|
|
32
|
-
assert.equal(stripInjected(pure), '');
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
test('isInjectedNoise only fires on a strong head so discussing the marker is safe', () => {
|
|
36
|
-
assert.equal(isInjectedNoise('Time sampled while preparing turn 3'), true);
|
|
37
|
-
assert.equal(isInjectedNoise('Elapsed since the preceding step context: unavailable.'), true);
|
|
38
|
-
assert.equal(isInjectedNoise('用户问:为什么 "Time sampled" 会进库?'), false);
|
|
39
|
-
assert.equal(isInjectedNoise('记住 <system-reminder> 这种块不要入库'), false);
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
test('the "Elapsed since the preceding step context" line is stripped too', () => {
|
|
43
|
-
const text = 'Elapsed since the preceding step context: unavailable.\n今天想吃米饭。';
|
|
44
|
-
assert.equal(stripInjected(text), '今天想吃米饭。');
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
test('scheduler reminder batches are harness automation, not user speech', () => {
|
|
48
|
-
const reminder = '[SCHEDULE REMINDER BATCH]\nThis is a scheduled message from the user\nreminders_json: [{"schedule_id":"x"}]';
|
|
49
|
-
assert.equal(stripInjected(reminder), '');
|
|
50
|
-
assert.equal(isInjectedNoise(reminder), true);
|
|
51
|
-
assert.equal(isInjectedNoise('<goal_complete>\nObjective: "x"'), true);
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
test('titles fall back to the assistant line instead of "会话 N"', () => {
|
|
55
|
-
assert.equal(deriveHostTitle('', null, '助手'), null);
|
|
56
|
-
assert.equal(deriveHostTitle('助手:这一轮收工了,装的是 0.4.57', null, '助手'), '这一轮收工了,装的是 0.4.57');
|
|
57
|
-
assert.equal(deriveHostTitle('', 7, '助手'), '会话 7');
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
test('deriveHostTitle turns the first real line into a readable title', () => {
|
|
61
|
-
assert.equal(deriveHostTitle('用户说想吃米饭。\n第二行', 12), '用户说想吃米饭。');
|
|
62
|
-
assert.equal(deriveHostTitle('', 12), '会话 12');
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
test('a turn whose user text is only injected context is not captured', () => {
|
|
66
|
-
const { dir, cortex } = setup();
|
|
67
|
-
const runtime = createHostRuntime({ cortex });
|
|
68
|
-
const session = { id: 's-injected', header: {} };
|
|
69
|
-
try {
|
|
70
|
-
const at = Date.parse('2026-11-15T10:00:00+08:00');
|
|
71
|
-
runtime.onSessionEvent(session, { type: 'turn/start', data: { turn: 1 }, time: at });
|
|
72
|
-
runtime.onSessionEvent(session, { type: 'user/message', data: { content: '<system-reminder>Current runtime context.</system-reminder>' }, time: at });
|
|
73
|
-
runtime.onSessionEvent(session, { type: 'turn/end', data: { turn: 1, reason: { kind: 'completed' } }, time: at });
|
|
74
|
-
assert.equal(cortex.repos.traces.count(), 0);
|
|
75
|
-
} finally {
|
|
76
|
-
cortex.close();
|
|
77
|
-
rmSync(dir, { recursive: true, force: true });
|
|
78
|
-
}
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
test('host capture stores clean text with a readable title', () => {
|
|
82
|
-
const { dir, cortex } = setup();
|
|
83
|
-
const runtime = createHostRuntime({ cortex, config: { persona: { selfName: '助手' } } });
|
|
84
|
-
const session = { id: 's-clean', header: {} };
|
|
85
|
-
try {
|
|
86
|
-
const at = Date.parse('2026-11-15T10:00:00+08:00');
|
|
87
|
-
runtime.onSessionEvent(session, { type: 'turn/start', data: { turn: 7 }, time: at });
|
|
88
|
-
runtime.onSessionEvent(session, { type: 'user/message', data: { content: RUNTIME_CONTEXT }, time: at });
|
|
89
|
-
runtime.onSessionEvent(session, { type: 'assistant/message', data: { turn: 7, message: { content: '这就去煮。' } }, time: at });
|
|
90
|
-
runtime.onSessionEvent(session, { type: 'turn/end', data: { turn: 7, reason: { kind: 'completed' } }, time: at });
|
|
91
|
-
const trace = cortex.repos.traces.list({ limit: 1 })[0];
|
|
92
|
-
assert.doesNotMatch(trace.content, /Time sampled/);
|
|
93
|
-
assert.match(trace.content, /用户说想吃米饭/);
|
|
94
|
-
assert.equal(trace.title, '用户说想吃米饭。');
|
|
95
|
-
const health = cortex.health();
|
|
96
|
-
assert.equal(health.capture.total, 1);
|
|
97
|
-
assert.equal(health.capture.stripped, 1);
|
|
98
|
-
assert.equal(health.capture.clean, 0);
|
|
99
|
-
assert.equal(typeof health.vectors.coverage, 'number');
|
|
100
|
-
} finally {
|
|
101
|
-
cortex.close();
|
|
102
|
-
rmSync(dir, { recursive: true, force: true });
|
|
103
|
-
}
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
test('recall records access so DSR recency is not frozen', () => {
|
|
107
|
-
const { dir, cortex } = setup();
|
|
108
|
-
try {
|
|
109
|
-
cortex.write({ text: '用户在 2026-11-14 说,助手将来会走得很远。', kind: 'promise', title: '用户的话' });
|
|
110
|
-
const before = cortex.repos.traces.list({ limit: 1 })[0];
|
|
111
|
-
assert.equal(before.accessCount, 0);
|
|
112
|
-
const result = cortex.search('助手 很远');
|
|
113
|
-
assert.ok(result.items.length > 0);
|
|
114
|
-
const after = cortex.repos.traces.get(before.id);
|
|
115
|
-
assert.equal(after.accessCount, 1);
|
|
116
|
-
assert.ok(after.lastAccessAt);
|
|
117
|
-
cortex.search('助手 很远');
|
|
118
|
-
assert.equal(cortex.repos.traces.get(before.id).accessCount, 1, 'a second immediate recall must not inflate access');
|
|
119
|
-
} finally {
|
|
120
|
-
cortex.close();
|
|
121
|
-
rmSync(dir, { recursive: true, force: true });
|
|
122
|
-
}
|
|
123
|
-
});
|
|
124
|
-
|
|
125
|
-
test('async vector queue drains and backfills misses instead of silently dropping', async () => {
|
|
126
|
-
const embedder = {
|
|
127
|
-
kind: 'openai-embedding',
|
|
128
|
-
async: true,
|
|
129
|
-
model: 'fake-embed',
|
|
130
|
-
dim: 4,
|
|
131
|
-
embed: async () => [1, 0, 0, 0],
|
|
132
|
-
};
|
|
133
|
-
const { dir, cortex } = setup({ features: { vector: true }, embedder, vectorDim: 4, vectorModel: 'fake-embed' });
|
|
134
|
-
try {
|
|
135
|
-
const written = cortex.write({ text: '海上的月亮很亮。', kind: 'observation', title: '月' });
|
|
136
|
-
assert.ok(written.trace?.id);
|
|
137
|
-
let status = cortex.vector.status();
|
|
138
|
-
assert.equal(status.queue.pending, 1);
|
|
139
|
-
const drained = await cortex.vector.drainQueue({ max: 10 });
|
|
140
|
-
assert.equal(drained.ok, 1);
|
|
141
|
-
assert.equal(cortex.vector.status().count, 1);
|
|
142
|
-
|
|
143
|
-
cortex.vector.remove(written.trace.id);
|
|
144
|
-
assert.equal(cortex.vector.status().count, 0);
|
|
145
|
-
const queued = cortex.vector.backfillMissing({ limit: 10 });
|
|
146
|
-
assert.equal(queued.queued, 1);
|
|
147
|
-
await cortex.vector.drainQueue({ max: 10 });
|
|
148
|
-
assert.equal(cortex.vector.status().count, 1);
|
|
149
|
-
|
|
150
|
-
// vectorMaintain is what the heartbeat calls: it should backfill and drain
|
|
151
|
-
// in one pass, and honour the batch sizes it is given.
|
|
152
|
-
for (let i = 0; i < 4; i += 1) {
|
|
153
|
-
cortex.write({ text: `海边的第 ${i} 条记忆。`, kind: 'observation', title: `海${i}` });
|
|
154
|
-
}
|
|
155
|
-
const maintain = await cortex.vectorMaintain({ backfill: 10, drain: 10 });
|
|
156
|
-
assert.ok(maintain.ok >= 4, `expected at least 4 embeddings, got ${JSON.stringify(maintain)}`);
|
|
157
|
-
assert.equal(maintain.stoppedBy, 'done');
|
|
158
|
-
assert.ok(maintain.elapsedMs >= 0);
|
|
159
|
-
assert.equal(cortex.vector.status().count, 5);
|
|
160
|
-
} finally {
|
|
161
|
-
cortex.close();
|
|
162
|
-
rmSync(dir, { recursive: true, force: true });
|
|
163
|
-
}
|
|
164
|
-
});
|
|
165
|
-
|
|
166
|
-
test('maintain expires stale intentions and collapses duplicate summaries', () => {
|
|
167
|
-
const { dir, cortex } = setup();
|
|
168
|
-
try {
|
|
169
|
-
const at = new Date('2026-11-15T03:00:00.000Z').toISOString();
|
|
170
|
-
const past = new Date('2026-11-12T03:00:00.000Z').toISOString();
|
|
171
|
-
cortex.db.prepare(`
|
|
172
|
-
INSERT INTO intentions(id, scope_id, kind, priority, status, payload_json, reason, created_at, due_at, expires_at)
|
|
173
|
-
VALUES ('itn_test', 'global', 'question', 0.4, 'pending', '{}', 'stale', ?, ?, ?)
|
|
174
|
-
`).run(past, past, past);
|
|
175
|
-
for (const id of ['sum_a', 'sum_b']) {
|
|
176
|
-
cortex.db.prepare(`
|
|
177
|
-
INSERT INTO summaries(id, scope_id, level, period_start, period_end, content, source_ids_json, coverage, created_at)
|
|
178
|
-
VALUES (?, 'global', 'episode', ?, ?, '同一段摘要', '[]', 1, ?)
|
|
179
|
-
`).run(id, past, past, past);
|
|
180
|
-
}
|
|
181
|
-
const report = cortex.maintain({ now: Date.parse(at), scopeId: 'global' });
|
|
182
|
-
assert.equal(report.intentions, 1);
|
|
183
|
-
assert.equal(cortex.db.prepare("SELECT status FROM intentions WHERE id = 'itn_test'").get().status, 'expired');
|
|
184
|
-
assert.equal(report.summaries, 1);
|
|
185
|
-
assert.equal(cortex.db.prepare("SELECT COUNT(*) AS c FROM summaries WHERE content = '同一段摘要'").get().c, 1);
|
|
186
|
-
} finally {
|
|
187
|
-
cortex.close();
|
|
188
|
-
rmSync(dir, { recursive: true, force: true });
|
|
189
|
-
}
|
|
190
|
-
});
|
|
191
|
-
|
|
192
|
-
test('auto-continuation goal rounds are harness text, not user speech', () => {
|
|
193
|
-
const goalRound = [
|
|
194
|
-
'<goal_round>',
|
|
195
|
-
'Objective: "持续自主迭代。"',
|
|
196
|
-
'Round: 8/40',
|
|
197
|
-
'',
|
|
198
|
-
'Continue working toward the objective.',
|
|
199
|
-
'</goal_round>',
|
|
200
|
-
].join('\n');
|
|
201
|
-
assert.equal(stripInjected(goalRound), '');
|
|
202
|
-
assert.equal(stripInjected('Objective: "x"\nRound: 3/40\n\n继续。'), '');
|
|
203
|
-
assert.equal(isInjectedNoise(goalRound), true);
|
|
204
|
-
assert.equal(stripInjected('用户说这句话里的 Round: 8/40 不要存。'), '用户说这句话里的 Round: 8/40 不要存。');
|
|
205
|
-
});
|
|
206
|
-
|
|
207
|
-
test('a goal-round turn keeps only the assistant reply', () => {
|
|
208
|
-
const { dir, cortex } = setup();
|
|
209
|
-
const runtime = createHostRuntime({ cortex, config: { persona: { selfName: '助手' } } });
|
|
210
|
-
const session = { id: 's-goal-round', header: {} };
|
|
211
|
-
try {
|
|
212
|
-
const at = Date.parse('2026-11-15T12:00:00+08:00');
|
|
213
|
-
runtime.onSessionEvent(session, { type: 'turn/start', data: { turn: 3 }, time: at });
|
|
214
|
-
runtime.onSessionEvent(session, {
|
|
215
|
-
type: 'user/message',
|
|
216
|
-
data: { content: '<goal_round>\nObjective: "x"\nRound: 3/40\n\n继续。\n</goal_round>' },
|
|
217
|
-
time: at,
|
|
218
|
-
});
|
|
219
|
-
runtime.onSessionEvent(session, { type: 'assistant/message', data: { turn: 3, message: { content: '这一轮修了捕获过滤。' } }, time: at });
|
|
220
|
-
runtime.onSessionEvent(session, { type: 'turn/end', data: { turn: 3, reason: { kind: 'completed' } }, time: at });
|
|
221
|
-
const trace = cortex.repos.traces.list({ limit: 1 })[0];
|
|
222
|
-
assert.ok(trace, 'the assistant reply is still worth remembering');
|
|
223
|
-
assert.doesNotMatch(trace.content, /Objective/);
|
|
224
|
-
assert.match(trace.content, /这一轮修了捕获过滤/);
|
|
225
|
-
} finally {
|
|
226
|
-
cortex.close();
|
|
227
|
-
rmSync(dir, { recursive: true, force: true });
|
|
228
|
-
}
|
|
229
|
-
});
|
|
230
|
-
|
|
231
|
-
test('maintain archives goal-round traces stored by older versions', () => {
|
|
232
|
-
const { dir, cortex } = setup();
|
|
233
|
-
try {
|
|
234
|
-
const written = cortex.write({
|
|
235
|
-
text: '用户:<goal_round>\nObjective: "旧版本存进来的"\nRound: 2/40\n\n继续。',
|
|
236
|
-
explicit: true,
|
|
237
|
-
});
|
|
238
|
-
assert.ok(written.trace?.id);
|
|
239
|
-
const report = cortex.maintain({ now: Date.parse('2026-11-15T12:00:00+08:00'), scopeId: 'global' });
|
|
240
|
-
assert.equal(report.goalRounds, 1);
|
|
241
|
-
assert.equal(cortex.repos.traces.get(written.trace.id).state, 'archived');
|
|
242
|
-
} finally {
|
|
243
|
-
cortex.close();
|
|
244
|
-
rmSync(dir, { recursive: true, force: true });
|
|
245
|
-
}
|
|
246
|
-
});
|
|
247
|
-
|
|
248
|
-
test('maintain drops stale unpinned working items but keeps pinned ones', () => {
|
|
249
|
-
const { dir, cortex } = setup();
|
|
250
|
-
try {
|
|
251
|
-
const now = Date.parse('2026-11-15T12:00:00+08:00');
|
|
252
|
-
const old = new Date(now - 30 * 86_400_000).toISOString();
|
|
253
|
-
cortex.db.prepare("INSERT INTO working_items(id, scope_id, kind, text, priority, pinned, created_at, updated_at) VALUES ('wrk_old','global','todo','很久没动过的事','0.5',0,?,?)").run(old, old);
|
|
254
|
-
cortex.db.prepare("INSERT INTO working_items(id, scope_id, kind, text, priority, pinned, created_at, updated_at) VALUES ('wrk_pin','global','todo','置顶不动的事','1',1,?,?)").run(old, old);
|
|
255
|
-
const report = cortex.maintain({ now, scopeId: 'global' });
|
|
256
|
-
assert.equal(report.working, 1);
|
|
257
|
-
assert.equal(cortex.db.prepare("SELECT COUNT(*) AS c FROM working_items WHERE id = 'wrk_old'").get().c, 0);
|
|
258
|
-
assert.equal(cortex.db.prepare("SELECT COUNT(*) AS c FROM working_items WHERE id = 'wrk_pin'").get().c, 1);
|
|
259
|
-
const health = cortex.health();
|
|
260
|
-
assert.ok(health.maintain, 'health should surface the last maintenance pass');
|
|
261
|
-
assert.equal(health.maintain.working, 1);
|
|
262
|
-
} finally {
|
|
263
|
-
cortex.close();
|
|
264
|
-
rmSync(dir, { recursive: true, force: true });
|
|
265
|
-
}
|
|
266
|
-
});
|
|
267
|
-
|
|
268
|
-
test('capture report buckets host turns by day', () => {
|
|
269
|
-
const { dir, cortex } = setup();
|
|
270
|
-
const runtime = createHostRuntime({ cortex });
|
|
271
|
-
const session = { id: 's-days', header: {} };
|
|
272
|
-
try {
|
|
273
|
-
const days = [Date.parse('2026-11-14T10:00:00+08:00'), Date.parse('2026-11-15T10:00:00+08:00')];
|
|
274
|
-
days.forEach((at, index) => {
|
|
275
|
-
const turn = index + 1;
|
|
276
|
-
runtime.onSessionEvent(session, { type: 'turn/start', data: { turn }, time: at });
|
|
277
|
-
runtime.onSessionEvent(session, { type: 'user/message', data: { content: `Time sampled while preparing turn ${turn}\nElapsed since the preceding model-visible message: 1m\n用户第 ${turn} 天说想吃米饭。` }, time: at });
|
|
278
|
-
runtime.onSessionEvent(session, { type: 'turn/end', data: { turn, reason: { kind: 'completed' } }, time: at });
|
|
279
|
-
});
|
|
280
|
-
const health = cortex.health();
|
|
281
|
-
assert.equal(health.capture.days.length, 2);
|
|
282
|
-
assert.equal(health.capture.days[0].day, '2026-11-15');
|
|
283
|
-
assert.equal(health.capture.days[1].day, '2026-11-14');
|
|
284
|
-
assert.equal(health.capture.days[0].stripped, 1);
|
|
285
|
-
} finally {
|
|
286
|
-
cortex.close();
|
|
287
|
-
rmSync(dir, { recursive: true, force: true });
|
|
288
|
-
}
|
|
289
|
-
});
|
|
290
|
-
|
|
291
|
-
test('contentText keeps text blocks only', () => {
|
|
292
|
-
const value = contentText([{ type: 'text', text: 'a' }, { type: 'image' }, 'b']);
|
|
293
|
-
assert.match(value, /a/);
|
|
294
|
-
assert.match(value, /b/);
|
|
295
|
-
assert.doesNotMatch(value, /\[object/);
|
|
296
|
-
});
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import test from 'node:test';
|
|
2
|
-
import assert from 'node:assert/strict';
|
|
3
|
-
import path from 'node:path';
|
|
4
|
-
import { resolveDataRoot } from '../../bin/cortex.mjs';
|
|
5
|
-
|
|
6
|
-
const home = 'C:/tmp/cortex-cli-home';
|
|
7
|
-
|
|
8
|
-
test('CLI defaults to the cortex-memory root, not legacy cortex', () => {
|
|
9
|
-
const root = resolveDataRoot({}, { DSH_HOME: home, DSH_PROFILE: 'desktop' });
|
|
10
|
-
assert.equal(root, path.resolve(path.join(home, 'cortex-memory')));
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
test('CLI --data overrides the profile root', () => {
|
|
14
|
-
const root = resolveDataRoot({ data: 'C:/tmp/explicit-cortex' }, { DSH_HOME: home, DSH_PROFILE: 'desktop' });
|
|
15
|
-
assert.equal(root, path.resolve('C:/tmp/explicit-cortex'));
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
test('CLI CORTEX_HOME overrides the profile root', () => {
|
|
19
|
-
const root = resolveDataRoot({}, { DSH_HOME: home, CORTEX_HOME: 'C:/tmp/cortex-env' });
|
|
20
|
-
assert.equal(root, path.resolve('C:/tmp/cortex-env'));
|
|
21
|
-
});
|