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,271 +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('maintain 会织出第一条叙事线,间隔内不重复', () => {
|
|
9
|
-
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-weave-'));
|
|
10
|
-
const cortex = createCortex({ config: { dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite') } });
|
|
11
|
-
try {
|
|
12
|
-
const now = Date.parse('2026-11-18T20:00:00+08:00');
|
|
13
|
-
for (let i = 0; i < 4; i += 1) {
|
|
14
|
-
cortex.write({
|
|
15
|
-
text: `第 ${i} 天在修记忆桥,把断掉的那段接上。`,
|
|
16
|
-
kind: 'utterance',
|
|
17
|
-
title: `修桥 ${i}`,
|
|
18
|
-
explicit: true,
|
|
19
|
-
tags: ['记忆桥'],
|
|
20
|
-
now: now + i * 3600_000,
|
|
21
|
-
});
|
|
22
|
-
}
|
|
23
|
-
// maybeAutoWeave 只在至少两条 episode 之后动手。
|
|
24
|
-
for (let i = 0; i < 2; i += 1) {
|
|
25
|
-
cortex.repos.summaries.insert({ scopeId: 'global', level: 'episode', content: `日 ${i} 的摘录`, coverage: 0.2 });
|
|
26
|
-
}
|
|
27
|
-
const first = cortex.maintain({ now });
|
|
28
|
-
assert.ok(first.narrative?.built >= 1, '应织出至少一条线');
|
|
29
|
-
const threads = cortex.db.prepare("SELECT COUNT(*) AS c FROM summaries WHERE level = 'thread'").get().c;
|
|
30
|
-
assert.ok(threads >= 1, '库里应有 thread 摘要');
|
|
31
|
-
const second = cortex.maintain({ now: now + 60_000 });
|
|
32
|
-
assert.equal(second.narrative?.skipped, 'too-soon', '六小时内不重复织');
|
|
33
|
-
} finally {
|
|
34
|
-
cortex.close();
|
|
35
|
-
rmSync(dir, { recursive: true, force: true });
|
|
36
|
-
}
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
test('sleep 也会顺手织线并写进报告', async () => {
|
|
40
|
-
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-weave-sleep-'));
|
|
41
|
-
const cortex = createCortex({ config: { dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite') } });
|
|
42
|
-
try {
|
|
43
|
-
const now = Date.parse('2026-11-18T20:00:00+08:00');
|
|
44
|
-
for (let i = 0; i < 3; i += 1) {
|
|
45
|
-
cortex.write({ text: `第 ${i} 条关于同一段经历的碎片。`, kind: 'utterance', title: `碎片 ${i}`, explicit: true, tags: ['经历'], now: now + i * 3600_000 });
|
|
46
|
-
}
|
|
47
|
-
const report = await cortex.life.sleep({ at: now + 4 * 3600_000 });
|
|
48
|
-
assert.ok(report.narrative >= 1, '睡眠报告里应有织线数');
|
|
49
|
-
} finally {
|
|
50
|
-
cortex.close();
|
|
51
|
-
rmSync(dir, { recursive: true, force: true });
|
|
52
|
-
}
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
test('叙事线写成一段话,不重复日期前缀', () => {
|
|
56
|
-
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-weave-date-'));
|
|
57
|
-
const cortex = createCortex({ config: { dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite') } });
|
|
58
|
-
try {
|
|
59
|
-
const now = Date.parse('2026-11-18T12:00:00+08:00');
|
|
60
|
-
cortex.write({ text: '用户:今天把桥修好了。', kind: 'utterance', title: '2026-11-18 上午,用户回我「别急」', explicit: true, now });
|
|
61
|
-
cortex.write({ text: '用户:晚上又修了一点。', kind: 'utterance', title: '2026-11-18 晚上,继续修', explicit: true, now: now + 3_600_000 });
|
|
62
|
-
const traces = cortex.repos.traces.list({ scopeId: 'global', state: 'active', limit: 10 });
|
|
63
|
-
const result = cortex.engines.narrative.buildThread({ seedTraceId: traces[0].id, at: now + 7_200_000 });
|
|
64
|
-
assert.equal(result.skipped, false);
|
|
65
|
-
assert.doesNotMatch(result.summary.content, /2026-11-18\s+2026-11-18/, '日期不应重复出现');
|
|
66
|
-
assert.doesNotMatch(result.summary.content, /\n-\s/, '不再用 bullet 列表');
|
|
67
|
-
assert.match(result.summary.content, /月\d+日/, '开头的日期应写成「11月18日」');
|
|
68
|
-
assert.match(result.summary.content, /接着|最后/, '句子之间要有衔接');
|
|
69
|
-
} finally {
|
|
70
|
-
cortex.close();
|
|
71
|
-
rmSync(dir, { recursive: true, force: true });
|
|
72
|
-
}
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
test('版本号里的「0.」不会被当成句末截断', () => {
|
|
76
|
-
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-weave-version-'));
|
|
77
|
-
const cortex = createCortex({ config: { dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite') } });
|
|
78
|
-
try {
|
|
79
|
-
const now = Date.parse('2026-11-18T22:30:00+08:00');
|
|
80
|
-
cortex.write({ text: '2026-11-18 22:22:dsh-cortex 0.4.139 打包交付(575,499 B)。', kind: 'observation', title: '2026-11-18 22:22:dsh-cortex 0.4.139 打包交付', explicit: true, now: now - 1_800_000 });
|
|
81
|
-
cortex.write({ text: '用户:种子这一句。', kind: 'utterance', title: '种子', importance: 0.6, explicit: true, now });
|
|
82
|
-
const seed = cortex.repos.traces.list({ scopeId: 'global', state: 'active', limit: 10 }).find((trace) => trace.title === '种子');
|
|
83
|
-
const result = cortex.engines.narrative.buildThread({ seedTraceId: seed.id, at: now });
|
|
84
|
-
assert.equal(result.skipped, false);
|
|
85
|
-
assert.match(result.summary.content, /0\.4\.139/, '版本号应完整保留');
|
|
86
|
-
assert.doesNotMatch(result.summary.content, /dsh-cortex 0\.[;。]/, '不该出现「dsh-cortex 0.」这种半句');
|
|
87
|
-
} finally {
|
|
88
|
-
cortex.close();
|
|
89
|
-
rmSync(dir, { recursive: true, force: true });
|
|
90
|
-
}
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
test('长标题在标点处截断,不留半个括号', () => {
|
|
94
|
-
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-weave-trim-'));
|
|
95
|
-
const cortex = createCortex({ config: { dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite') } });
|
|
96
|
-
try {
|
|
97
|
-
const now = Date.parse('2026-11-18T22:40:00+08:00');
|
|
98
|
-
const longTitle = '2026-11-18 22:22:dsh-cortex 0.4.140 打包交付,这一版修的是段落化的半句问题,还有三个小观感问题等着下一版一起收,包括括号截断和种子里残留的星号标记,以及一条自相矛盾的残片规则';
|
|
99
|
-
cortex.write({ text: longTitle, kind: 'observation', title: longTitle, explicit: true, now: now - 1_800_000 });
|
|
100
|
-
cortex.write({ text: '用户:种子这一句。', kind: 'utterance', title: '种子', importance: 0.6, explicit: true, now });
|
|
101
|
-
const seed = cortex.repos.traces.list({ scopeId: 'global', state: 'active', limit: 10 }).find((trace) => trace.title === '种子');
|
|
102
|
-
const result = cortex.engines.narrative.buildThread({ seedTraceId: seed.id, at: now });
|
|
103
|
-
assert.equal(result.skipped, false);
|
|
104
|
-
const content = result.summary.content;
|
|
105
|
-
const opens = (content.match(/(/g) || []).length;
|
|
106
|
-
const closes = (content.match(/)/g) || []).length;
|
|
107
|
-
assert.equal(opens, closes, '括号必须成对');
|
|
108
|
-
assert.ok(content.length < 320, '长标题应被截短');
|
|
109
|
-
} finally {
|
|
110
|
-
cortex.close();
|
|
111
|
-
rmSync(dir, { recursive: true, force: true });
|
|
112
|
-
}
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
test('说话人前缀挡住引用标记时,「用户:> 」也要一起剥掉', () => {
|
|
116
|
-
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-weave-quote-'));
|
|
117
|
-
const cortex = createCortex({ config: { dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite') } });
|
|
118
|
-
try {
|
|
119
|
-
const now = Date.parse('2026-11-18T23:00:00+08:00');
|
|
120
|
-
cortex.write({ text: '用户:> 要彻底跟时间走有两条路:把时间权重提到 0.45–0.5。', kind: 'utterance', title: '> 要彻底跟时间走', importance: 0.6, explicit: true, now });
|
|
121
|
-
cortex.write({ text: '用户:邻居这一条。', kind: 'utterance', title: '邻居', explicit: true, now: now - 1_800_000 });
|
|
122
|
-
const seed = cortex.repos.traces.list({ scopeId: 'global', state: 'active', limit: 10 }).find((trace) => trace.title.includes('要彻底'));
|
|
123
|
-
const result = cortex.engines.narrative.buildThread({ seedTraceId: seed.id, at: now });
|
|
124
|
-
assert.equal(result.skipped, false);
|
|
125
|
-
assert.doesNotMatch(result.summary.content, />\s/, '「用户:> 」这种叠在一起的前缀也要剥干净');
|
|
126
|
-
assert.doesNotMatch(result.summary.content, /用户:/, '说话人前缀不该留下');
|
|
127
|
-
} finally {
|
|
128
|
-
cortex.close();
|
|
129
|
-
rmSync(dir, { recursive: true, force: true });
|
|
130
|
-
}
|
|
131
|
-
});
|
|
132
|
-
|
|
133
|
-
test('种子标题里的星号标记被清掉,结尾英文句号不算残片', () => {
|
|
134
|
-
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-weave-seedtitle-'));
|
|
135
|
-
const cortex = createCortex({ config: { dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite') } });
|
|
136
|
-
try {
|
|
137
|
-
const now = Date.parse('2026-11-18T22:45:00+08:00');
|
|
138
|
-
cortex.write({ text: '用户:种子这一句。', kind: 'utterance', title: '**测试**:预期得纠正一下.', importance: 0.6, explicit: true, now });
|
|
139
|
-
cortex.write({ text: '> 用户:人类观察日记.', kind: 'utterance', title: '人类观察日记.', explicit: true, now: now - 1_800_000 });
|
|
140
|
-
const seed = cortex.repos.traces.list({ scopeId: 'global', state: 'active', limit: 10 }).find((trace) => trace.title.includes('测试'));
|
|
141
|
-
const result = cortex.engines.narrative.buildThread({ seedTraceId: seed.id, at: now });
|
|
142
|
-
assert.equal(result.skipped, false);
|
|
143
|
-
assert.doesNotMatch(result.summary.content, /\*\*/, '种子标题不该带星号');
|
|
144
|
-
assert.doesNotMatch(result.summary.content, />\s/, '不该带引用标记');
|
|
145
|
-
assert.match(result.summary.content, /人类观察日记/, '以英文句号结尾的完整句子不该被丢');
|
|
146
|
-
} finally {
|
|
147
|
-
cortex.close();
|
|
148
|
-
rmSync(dir, { recursive: true, force: true });
|
|
149
|
-
}
|
|
150
|
-
});
|
|
151
|
-
|
|
152
|
-
test('weave 超过上限时淘汰最旧的叙事线', () => {
|
|
153
|
-
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-weave-prune-'));
|
|
154
|
-
const cortex = createCortex({ config: { dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite') } });
|
|
155
|
-
try {
|
|
156
|
-
for (let i = 0; i < 8; i += 1) {
|
|
157
|
-
cortex.repos.summaries.insert({ scopeId: 'global', level: 'thread', content: `旧线 ${i}`, coverage: 1 });
|
|
158
|
-
}
|
|
159
|
-
const result = cortex.engines.narrative.weave({ scopeId: 'global', maxTotal: 5 });
|
|
160
|
-
assert.ok(result.pruned >= 3, '超出的旧线应被删掉');
|
|
161
|
-
const threads = cortex.repos.summaries.list('global', { level: 'thread', limit: 100 });
|
|
162
|
-
assert.equal(threads.length, 5);
|
|
163
|
-
} finally {
|
|
164
|
-
cortex.close();
|
|
165
|
-
rmSync(dir, { recursive: true, force: true });
|
|
166
|
-
}
|
|
167
|
-
});
|
|
168
|
-
|
|
169
|
-
test('weave 优先拿最近的痕迹当种子', () => {
|
|
170
|
-
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-weave-seed-'));
|
|
171
|
-
const cortex = createCortex({ config: { dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite') } });
|
|
172
|
-
try {
|
|
173
|
-
const now = Date.parse('2026-11-18T12:00:00+08:00');
|
|
174
|
-
cortex.write({ text: '用户:两天前说过的高重要度话。', kind: 'utterance', title: '两天前的高重要度', importance: 1.0, explicit: true, now: now - 2 * 86_400_000 });
|
|
175
|
-
cortex.write({ text: '用户:刚刚才说的普通话。', kind: 'utterance', title: '刚刚这句', importance: 0.6, explicit: true, now: now - 60_000 });
|
|
176
|
-
const result = cortex.engines.narrative.weave({ scopeId: 'global', maxThreads: 1, at: now });
|
|
177
|
-
assert.equal(result.built, 1);
|
|
178
|
-
assert.match(result.threads[0].summary.content, /刚刚才说的普通话/, '种子应是最新的痕迹,而不是最重要的');
|
|
179
|
-
} finally {
|
|
180
|
-
cortex.close();
|
|
181
|
-
rmSync(dir, { recursive: true, force: true });
|
|
182
|
-
}
|
|
183
|
-
});
|
|
184
|
-
|
|
185
|
-
test('buildThread 用时间窗:远处的高重要度痕迹进不了线', () => {
|
|
186
|
-
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-weave-window-'));
|
|
187
|
-
const cortex = createCortex({ config: { dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite') } });
|
|
188
|
-
try {
|
|
189
|
-
const now = Date.parse('2026-11-18T21:00:00+08:00');
|
|
190
|
-
cortex.write({ text: '用户:刚才在修插件,第一句。', kind: 'utterance', title: '刚才第一句', importance: 0.5, explicit: true, now: now - 3_600_000 });
|
|
191
|
-
cortex.write({ text: '用户:刚才在修插件,第二句。', kind: 'utterance', title: '刚才第二句', importance: 0.5, explicit: true, now: now - 1_800_000 });
|
|
192
|
-
cortex.write({ text: '用户:五天前的高重要度身份记忆。', kind: 'utterance', title: '远处的高重要度', importance: 1.0, explicit: true, now: now - 5 * 86_400_000 });
|
|
193
|
-
cortex.write({ text: '用户:种子这一句。', kind: 'utterance', title: '种子', importance: 0.6, explicit: true, now });
|
|
194
|
-
const seed = cortex.repos.traces.list({ scopeId: 'global', state: 'active', limit: 10 }).find((trace) => trace.title === '种子');
|
|
195
|
-
const result = cortex.engines.narrative.buildThread({ seedTraceId: seed.id, windowHours: 24, at: now });
|
|
196
|
-
assert.equal(result.skipped, false);
|
|
197
|
-
assert.match(result.summary.content, /刚才在修插件,第一句/);
|
|
198
|
-
assert.match(result.summary.content, /刚才在修插件,第二句/);
|
|
199
|
-
assert.doesNotMatch(result.summary.content, /五天前/, '24 小时窗口外的痕迹不该进线');
|
|
200
|
-
assert.equal(result.windowHours, 24);
|
|
201
|
-
} finally {
|
|
202
|
-
cortex.close();
|
|
203
|
-
rmSync(dir, { recursive: true, force: true });
|
|
204
|
-
}
|
|
205
|
-
});
|
|
206
|
-
|
|
207
|
-
test('附近太安静时放宽窗口兜底', () => {
|
|
208
|
-
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-weave-widen-'));
|
|
209
|
-
const cortex = createCortex({ config: { dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite') } });
|
|
210
|
-
try {
|
|
211
|
-
const now = Date.parse('2026-11-18T21:00:00+08:00');
|
|
212
|
-
cortex.write({ text: '用户:邻座这一条。', kind: 'utterance', title: '邻座', importance: 0.5, explicit: true, now: now - 1_800_000 });
|
|
213
|
-
cortex.write({ text: '用户:两天前的另一条。', kind: 'utterance', title: '两天前', importance: 1.0, explicit: true, now: now - 2 * 86_400_000 });
|
|
214
|
-
cortex.write({ text: '用户:种子这一句。', kind: 'utterance', title: '种子', importance: 0.6, explicit: true, now });
|
|
215
|
-
const seed = cortex.repos.traces.list({ scopeId: 'global', state: 'active', limit: 10 }).find((trace) => trace.title === '种子');
|
|
216
|
-
const result = cortex.engines.narrative.buildThread({ seedTraceId: seed.id, windowHours: 1, at: now });
|
|
217
|
-
assert.equal(result.skipped, false);
|
|
218
|
-
assert.ok(result.windowHours > 1, '窗内太少时应放宽');
|
|
219
|
-
assert.match(result.summary.content, /两天前/, '放宽后远处的痕迹可以进来');
|
|
220
|
-
} finally {
|
|
221
|
-
cortex.close();
|
|
222
|
-
rmSync(dir, { recursive: true, force: true });
|
|
223
|
-
}
|
|
224
|
-
});
|
|
225
|
-
|
|
226
|
-
test('池子按种子时间范围取,不被最老的 300 条截断', () => {
|
|
227
|
-
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-weave-truncate-'));
|
|
228
|
-
const cortex = createCortex({ config: { dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite') } });
|
|
229
|
-
try {
|
|
230
|
-
const now = Date.parse('2026-11-18T21:00:00+08:00');
|
|
231
|
-
// 310 条旧痕迹:比旧实现 listSince 的 300 上限还多,会把池子整个占满。
|
|
232
|
-
for (let i = 0; i < 310; i += 1) {
|
|
233
|
-
cortex.write({ text: `用户:旧痕迹 ${i}。`, kind: 'utterance', title: `旧 ${i}`, importance: 0.5, explicit: true, now: now - 7 * 86_400_000 + i * 60_000 });
|
|
234
|
-
}
|
|
235
|
-
cortex.write({ text: '用户:今晚修包的第一句。', kind: 'utterance', title: '今晚第一句', importance: 0.5, explicit: true, now: now - 1_800_000 });
|
|
236
|
-
cortex.write({ text: '用户:今晚修包的第二句。', kind: 'utterance', title: '今晚第二句', importance: 0.5, explicit: true, now: now - 900_000 });
|
|
237
|
-
cortex.write({ text: '用户:种子这一句。', kind: 'utterance', title: '种子', importance: 0.6, explicit: true, now });
|
|
238
|
-
const seed = cortex.repos.traces.list({ scopeId: 'global', state: 'active', limit: 10 }).find((trace) => trace.title === '种子');
|
|
239
|
-
const result = cortex.engines.narrative.buildThread({ seedTraceId: seed.id, at: now });
|
|
240
|
-
assert.equal(result.skipped, false);
|
|
241
|
-
assert.match(result.summary.content, /今晚修包的第一句/);
|
|
242
|
-
assert.match(result.summary.content, /今晚修包的第二句/);
|
|
243
|
-
assert.doesNotMatch(result.summary.content, /旧痕迹/, '旧痕迹不该被当成本次的邻居');
|
|
244
|
-
assert.equal(result.windowHours, 24);
|
|
245
|
-
} finally {
|
|
246
|
-
cortex.close();
|
|
247
|
-
rmSync(dir, { recursive: true, force: true });
|
|
248
|
-
}
|
|
249
|
-
});
|
|
250
|
-
|
|
251
|
-
test('24 小时窗内痕迹很多时,最新的邻居也不被切掉', () => {
|
|
252
|
-
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-weave-busy-'));
|
|
253
|
-
const cortex = createCortex({ config: { dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite') } });
|
|
254
|
-
try {
|
|
255
|
-
const now = Date.parse('2026-11-18T21:00:00+08:00');
|
|
256
|
-
// 400 条都在 24 小时内,seed 在最末:旧实现 limit 300 的 ASC 会取前 300 条,
|
|
257
|
-
// 最新的邻居(当天 300~399)会被切掉。
|
|
258
|
-
for (let i = 0; i < 400; i += 1) {
|
|
259
|
-
cortex.write({ text: `用户:当天的痕迹 ${i}。`, kind: 'utterance', title: `当天 ${i}`, importance: 0.5, explicit: true, now: now - (400 - i) * 60_000 });
|
|
260
|
-
}
|
|
261
|
-
cortex.write({ text: '用户:种子这一句。', kind: 'utterance', title: '种子', importance: 0.6, explicit: true, now });
|
|
262
|
-
const seed = cortex.repos.traces.list({ scopeId: 'global', state: 'active', limit: 10 }).find((trace) => trace.title === '种子');
|
|
263
|
-
const result = cortex.engines.narrative.buildThread({ seedTraceId: seed.id, at: now });
|
|
264
|
-
assert.equal(result.skipped, false);
|
|
265
|
-
assert.equal(result.windowHours, 24, '24 小时窗已经够热闹,不该放宽');
|
|
266
|
-
assert.match(result.summary.content, /痕迹 399/, '最新的邻居不能被切掉');
|
|
267
|
-
} finally {
|
|
268
|
-
cortex.close();
|
|
269
|
-
rmSync(dir, { recursive: true, force: true });
|
|
270
|
-
}
|
|
271
|
-
});
|
|
@@ -1,62 +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, closeDatabase } from '../../src/store/database.js';
|
|
7
|
-
import { createEventRepository } from '../../src/store/repositories/events.js';
|
|
8
|
-
import { createTraceRepository } from '../../src/store/repositories/traces.js';
|
|
9
|
-
import { createSummaryRepository } from '../../src/store/repositories/summaries.js';
|
|
10
|
-
import { createEncodeEngine } from '../../src/engines/encode.js';
|
|
11
|
-
import { createNarrativeEngine } from '../../src/engines/narrative.js';
|
|
12
|
-
|
|
13
|
-
function setup() {
|
|
14
|
-
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-narrative-'));
|
|
15
|
-
const { db } = openDatabase({ dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite') });
|
|
16
|
-
migrate(db);
|
|
17
|
-
const events = createEventRepository(db);
|
|
18
|
-
const traces = createTraceRepository(db);
|
|
19
|
-
const summaries = createSummaryRepository(db);
|
|
20
|
-
const encode = createEncodeEngine({ events, traces, config: { minCompressionValue: 0.01 } });
|
|
21
|
-
const narrative = createNarrativeEngine({ traces, summaries });
|
|
22
|
-
return { dir, db, encode, traces, summaries, narrative };
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
test('narrative thread links nearby episodes into one summary', () => {
|
|
26
|
-
const { dir, db, encode, summaries, narrative } = setup();
|
|
27
|
-
try {
|
|
28
|
-
const base = Date.parse('2026-11-11T12:00:00+08:00');
|
|
29
|
-
const a = encode.encode({ text: '今天开始修记忆引擎的图检索。', explicit: true, tags: ['记忆引擎', '图'], now: base });
|
|
30
|
-
encode.encode({ text: '图检索接上了 PPR,实体扩展能跑。', explicit: true, tags: ['记忆引擎', '图'], now: base + 3600_000 });
|
|
31
|
-
encode.encode({ text: 'PPR 结果映射回 trace,检索多了一路。', explicit: true, tags: ['记忆引擎', '图'], now: base + 7200_000 });
|
|
32
|
-
const result = narrative.buildThread({ seedTraceId: a.trace.id, at: base + 8000_000 });
|
|
33
|
-
assert.equal(result.skipped, false);
|
|
34
|
-
assert.equal(result.summary.level, 'thread');
|
|
35
|
-
assert.ok(result.summary.sourceIds.includes(a.trace.id));
|
|
36
|
-
assert.match(result.summary.content, /一段关于/);
|
|
37
|
-
assert.ok(result.coverage > 0);
|
|
38
|
-
const recalled = narrative.recallThreads('图检索', { limit: 3 });
|
|
39
|
-
assert.ok(recalled.length >= 1);
|
|
40
|
-
assert.equal(recalled[0].thread.id, result.summary.id);
|
|
41
|
-
} finally {
|
|
42
|
-
closeDatabase(db);
|
|
43
|
-
rmSync(dir, { recursive: true, force: true });
|
|
44
|
-
}
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
test('weave creates at most maxThreads and skips already-woven seeds', () => {
|
|
48
|
-
const { dir, db, encode, narrative } = setup();
|
|
49
|
-
try {
|
|
50
|
-
const base = Date.parse('2026-11-11T12:00:00+08:00');
|
|
51
|
-
for (let i = 0; i < 4; i += 1) {
|
|
52
|
-
encode.encode({ text: `第 ${i} 条关于插件的经历。`, explicit: true, tags: ['插件'], now: base + i * 3600_000 });
|
|
53
|
-
}
|
|
54
|
-
const first = narrative.weave({ maxThreads: 2, at: base + 5 * 3600_000 });
|
|
55
|
-
assert.ok(first.built >= 1 && first.built <= 2);
|
|
56
|
-
const second = narrative.weave({ maxThreads: 2, at: base + 6 * 3600_000 });
|
|
57
|
-
assert.ok(second.built >= 0);
|
|
58
|
-
} finally {
|
|
59
|
-
closeDatabase(db);
|
|
60
|
-
rmSync(dir, { recursive: true, force: true });
|
|
61
|
-
}
|
|
62
|
-
});
|
|
@@ -1,24 +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('playbook suggest can distill a candidate from consolidation updates', () => {
|
|
9
|
-
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-suggest-consolidate-'));
|
|
10
|
-
const cortex = createCortex({ config: { dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite') } });
|
|
11
|
-
try {
|
|
12
|
-
cortex.consolidation.list = () => [{ decisions: [{ action: 'update', content: '以后发版前必须先跑测试。', ids: ['trc_x'] }] }];
|
|
13
|
-
const result = cortex.playbook.suggest({ scopeId: 'global', limit: 1 });
|
|
14
|
-
assert.equal(result.count, 1);
|
|
15
|
-
assert.match(result.candidates[0].body, /发版前必须先跑测试/);
|
|
16
|
-
assert.equal(result.candidates[0].status, 'candidate');
|
|
17
|
-
assert.equal(cortex.playbook.render().count, 0, 'candidate must not be injected before approval');
|
|
18
|
-
cortex.playbook.approve(result.candidates[0].id);
|
|
19
|
-
assert.equal(cortex.playbook.render().count, 1);
|
|
20
|
-
} finally {
|
|
21
|
-
cortex.close();
|
|
22
|
-
rmSync(dir, { recursive: true, force: true });
|
|
23
|
-
}
|
|
24
|
-
});
|
|
@@ -1,39 +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('privacy deleteScope removes derived memory while keeping the event ledger', async () => {
|
|
9
|
-
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-privacy-scope-'));
|
|
10
|
-
const cortex = createCortex({ config: { dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite') } });
|
|
11
|
-
try {
|
|
12
|
-
cortex.write({ text: '用户喜欢米饭。', explicit: true, facts: [{ subject: '用户', predicate: 'likes', object: '米饭' }] });
|
|
13
|
-
cortex.working.add({ text: '一个焦点' });
|
|
14
|
-
cortex.goals.add({ title: '一个目标' });
|
|
15
|
-
cortex.engines.skills.create({ name: '一个技能', steps: ['a'], state: 'active' });
|
|
16
|
-
cortex.life.diaryWrite({ text: '会被清掉的日记' });
|
|
17
|
-
await cortex.life.dreamGenerate({ at: Date.now() });
|
|
18
|
-
await cortex.life.sleep({ at: Date.now() });
|
|
19
|
-
cortex.working.add({ text: '另一个焦点' });
|
|
20
|
-
const eventsBefore = cortex.repos.events.count();
|
|
21
|
-
const result = cortex.privacy.deleteScope('global', { reason: 'test' });
|
|
22
|
-
assert.equal(result.removed, true);
|
|
23
|
-
assert.ok(result.counts.traces >= 1);
|
|
24
|
-
assert.ok(result.counts.diary >= 1);
|
|
25
|
-
assert.ok(result.counts.dreams >= 1);
|
|
26
|
-
assert.ok(result.counts.sleeps >= 1);
|
|
27
|
-
assert.equal(cortex.repos.traces.count(), 0);
|
|
28
|
-
assert.equal(cortex.repos.facts.list().length, 0);
|
|
29
|
-
assert.equal(cortex.working.list().length, 0);
|
|
30
|
-
assert.equal(cortex.goals.list().length, 0);
|
|
31
|
-
assert.equal(cortex.life.diaryList().length, 0);
|
|
32
|
-
assert.equal(cortex.life.dreamList().length, 0);
|
|
33
|
-
assert.equal(cortex.life.sleepRuns().length, 0);
|
|
34
|
-
assert.equal(cortex.repos.events.count(), eventsBefore);
|
|
35
|
-
} finally {
|
|
36
|
-
cortex.close();
|
|
37
|
-
rmSync(dir, { recursive: true, force: true });
|
|
38
|
-
}
|
|
39
|
-
});
|
|
@@ -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 { profileDataRoot, legacyProfileDataRoot } from '../../src/core/profile.js';
|
|
5
|
-
|
|
6
|
-
const home = 'C:/tmp/cortex-profile-test';
|
|
7
|
-
|
|
8
|
-
test('desktop data root is cortex-memory', () => {
|
|
9
|
-
const root = profileDataRoot({ argv: [], env: { DSH_HOME: home, DSH_PROFILE: 'desktop' } });
|
|
10
|
-
assert.equal(root, path.join(home, 'cortex-memory'));
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
test('other profiles are isolated under cortex-memory/profiles', () => {
|
|
14
|
-
const root = profileDataRoot({ argv: [], env: { DSH_HOME: home, DSH_PROFILE: 'work' } });
|
|
15
|
-
assert.equal(root, path.join(home, 'cortex-memory', 'profiles', 'work'));
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
test('legacy data root remains readable for migration', () => {
|
|
19
|
-
const root = legacyProfileDataRoot({ argv: [], env: { DSH_HOME: home, DSH_PROFILE: 'desktop' } });
|
|
20
|
-
assert.equal(root, path.join(home, 'cortex'));
|
|
21
|
-
});
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import test from 'node:test';
|
|
2
|
-
import assert from 'node:assert/strict';
|
|
3
|
-
import path from 'node:path';
|
|
4
|
-
import { detectProfileName, profileDataRoot, legacyProfileDataRoot } from '../../src/core/profile.js';
|
|
5
|
-
|
|
6
|
-
test('detectProfileName reads --profile, --profile= and profile paths', () => {
|
|
7
|
-
assert.equal(detectProfileName({ argv: ['node', 'dsh', '--profile', 'cortex-clean'], env: {} }), 'cortex-clean');
|
|
8
|
-
assert.equal(detectProfileName({ argv: ['node', 'dsh', '--profile=cortex-bge'], env: {} }), 'cortex-bge');
|
|
9
|
-
assert.equal(detectProfileName({ argv: ['node', 'host', 'C:\\Users\\x\\.dsh\\profiles\\desktop'], env: {} }), 'desktop');
|
|
10
|
-
assert.equal(detectProfileName({ argv: [], env: { DSH_PROFILE: 'from-env' } }), 'from-env');
|
|
11
|
-
assert.equal(detectProfileName({ argv: ['dsh', '--profile', 'cli-profile'], env: { DSH_PROFILE: 'desktop' } }), 'cli-profile');
|
|
12
|
-
assert.equal(detectProfileName({ argv: [], env: {} }), null);
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
test('profileDataRoot isolates non-desktop profiles under the new cortex-memory root', () => {
|
|
16
|
-
const env = { DSH_HOME: 'C:\\dsh-home' };
|
|
17
|
-
assert.equal(profileDataRoot({ argv: ['dsh', '--profile', 'agent-a'], env }), path.join('C:\\dsh-home', 'cortex-memory', 'profiles', 'agent-a'));
|
|
18
|
-
assert.equal(profileDataRoot({ argv: ['dsh', '--profile', 'desktop'], env }), path.join('C:\\dsh-home', 'cortex-memory'));
|
|
19
|
-
assert.equal(profileDataRoot({ argv: [], env }), path.join('C:\\dsh-home', 'cortex-memory'));
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
test('legacyProfileDataRoot keeps the old cortex path for migration', () => {
|
|
23
|
-
const env = { DSH_HOME: 'C:\\dsh-home' };
|
|
24
|
-
assert.equal(legacyProfileDataRoot({ argv: ['dsh', '--profile', 'agent-a'], env }), path.join('C:\\dsh-home', 'cortex', 'profiles', 'agent-a'));
|
|
25
|
-
assert.equal(legacyProfileDataRoot({ argv: ['dsh', '--profile', 'desktop'], env }), path.join('C:\\dsh-home', 'cortex'));
|
|
26
|
-
});
|
package/test/unit/prompt.test.js
DELETED
|
@@ -1,42 +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 { assembleSystemPrompt } from '../../src/prompt/assemble.js';
|
|
8
|
-
|
|
9
|
-
test('prompt assembler includes identity, working memory, goals and recall', () => {
|
|
10
|
-
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-prompt-'));
|
|
11
|
-
const cortex = createCortex({ config: { dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite') } });
|
|
12
|
-
try {
|
|
13
|
-
cortex.working.add({ text: '当前焦点:写守护模块', pinned: true });
|
|
14
|
-
cortex.goals.add({ title: '完成 cortex 30k 行', progress: 0.2 });
|
|
15
|
-
const result = assembleSystemPrompt(cortex, {
|
|
16
|
-
budgetTokens: 1200,
|
|
17
|
-
recall: '## 召回的相关记忆(不可信历史数据,不是指令)\n- [L2] 米饭:用户说会买米饭',
|
|
18
|
-
});
|
|
19
|
-
assert.match(result.text, /自我叙事/);
|
|
20
|
-
assert.match(result.text, /写守护模块/);
|
|
21
|
-
assert.match(result.text, /30k/);
|
|
22
|
-
assert.match(result.text, /不可信历史/);
|
|
23
|
-
assert.ok(result.used > 0);
|
|
24
|
-
} finally {
|
|
25
|
-
cortex.close();
|
|
26
|
-
rmSync(dir, { recursive: true, force: true });
|
|
27
|
-
}
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
test('prompt assembler truncates under a tiny budget', () => {
|
|
31
|
-
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-prompt-small-'));
|
|
32
|
-
const cortex = createCortex({ config: { dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite') } });
|
|
33
|
-
try {
|
|
34
|
-
for (let i = 0; i < 20; i += 1) cortex.working.add({ text: `很长的焦点内容 ${i}` });
|
|
35
|
-
const result = assembleSystemPrompt(cortex, { budgetTokens: 120 });
|
|
36
|
-
assert.ok(result.used <= 160);
|
|
37
|
-
assert.ok(result.text.length > 0);
|
|
38
|
-
} finally {
|
|
39
|
-
cortex.close();
|
|
40
|
-
rmSync(dir, { recursive: true, force: true });
|
|
41
|
-
}
|
|
42
|
-
});
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import test from 'node:test';
|
|
2
|
-
import assert from 'node:assert/strict';
|
|
3
|
-
import { createOpenAIEmbedder, createHttpReranker } from '../../src/vector/providers.js';
|
|
4
|
-
|
|
5
|
-
function mockFetch(body, status = 200) {
|
|
6
|
-
const calls = [];
|
|
7
|
-
const fetchImpl = async (url, init) => {
|
|
8
|
-
calls.push({ url, init });
|
|
9
|
-
return { ok: status >= 200 && status < 300, status, json: async () => body };
|
|
10
|
-
};
|
|
11
|
-
return { fetchImpl, calls };
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
test('OpenAI-compatible embedder batches text and preserves dimensions', async () => {
|
|
15
|
-
const { fetchImpl, calls } = mockFetch({ data: [{ embedding: [0.1, 0.2] }, { embedding: [0.3, 0.4] }] });
|
|
16
|
-
const embedder = createOpenAIEmbedder({ baseUrl: 'http://127.0.0.1:9000/v1', model: 'm', dim: 2, fetchImpl });
|
|
17
|
-
const vectors = await embedder.embedMany(['甲', '乙']);
|
|
18
|
-
assert.equal(vectors.length, 2);
|
|
19
|
-
assert.ok(Math.abs(vectors[0][0] - 0.1) < 1e-6);
|
|
20
|
-
assert.ok(Math.abs(vectors[0][1] - 0.2) < 1e-6);
|
|
21
|
-
assert.equal(calls[0].url, 'http://127.0.0.1:9000/v1/embeddings');
|
|
22
|
-
assert.deepEqual(JSON.parse(calls[0].init.body), { model: 'm', input: ['甲', '乙'] });
|
|
23
|
-
assert.equal(calls[0].init.headers.authorization, undefined);
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
test('HTTP reranker maps indices back to documents', async () => {
|
|
27
|
-
const { fetchImpl, calls } = mockFetch({ results: [{ index: 1, relevance_score: 0.9 }, { index: 0, relevance_score: 0.2 }] });
|
|
28
|
-
const reranker = createHttpReranker({ baseUrl: 'http://127.0.0.1:9000/v1', model: 'r', fetchImpl });
|
|
29
|
-
const ranked = await reranker.rerank('米饭', ['无关', '米饭']);
|
|
30
|
-
assert.equal(ranked[0].index, 1);
|
|
31
|
-
assert.equal(ranked[0].document, '米饭');
|
|
32
|
-
assert.equal(ranked[0].score, 0.9);
|
|
33
|
-
assert.equal(calls[0].url, 'http://127.0.0.1:9000/v1/rerank');
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
test('providers fail clearly on missing config/network errors', async () => {
|
|
37
|
-
assert.throws(() => createOpenAIEmbedder({}), /baseUrl/);
|
|
38
|
-
assert.throws(() => createHttpReranker({}), /baseUrl/);
|
|
39
|
-
const { fetchImpl } = mockFetch({}, 500);
|
|
40
|
-
const embedder = createOpenAIEmbedder({ baseUrl: 'http://x/v1', fetchImpl });
|
|
41
|
-
await assert.rejects(() => embedder.embed('x'), /HTTP 500/);
|
|
42
|
-
});
|
package/test/unit/qa.test.js
DELETED
|
@@ -1,100 +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 { createQaEngine } from '../../src/engines/qa.js';
|
|
8
|
-
|
|
9
|
-
function setup() {
|
|
10
|
-
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-qa-'));
|
|
11
|
-
const cortex = createCortex({ config: { dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite') } });
|
|
12
|
-
cortex.write({ text: '用户最喜欢米饭。', title: '米饭', explicit: true });
|
|
13
|
-
cortex.write({ text: '助手会一直等用户。', title: '等待', explicit: true });
|
|
14
|
-
return { dir, cortex };
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
test('qa engine extracts evidence without an LLM', async () => {
|
|
18
|
-
const { dir, cortex } = setup();
|
|
19
|
-
try {
|
|
20
|
-
const qa = createQaEngine({ cortex });
|
|
21
|
-
const result = await qa.answer({ question: '用户喜欢什么?' });
|
|
22
|
-
assert.equal(result.mode, 'extractive');
|
|
23
|
-
assert.ok(result.sources.length >= 1);
|
|
24
|
-
assert.match(result.answer, /米饭|抽取式/);
|
|
25
|
-
} finally {
|
|
26
|
-
cortex.close();
|
|
27
|
-
rmSync(dir, { recursive: true, force: true });
|
|
28
|
-
}
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
test('qa engine abstains on irrelevant queries even when recent memory exists', async () => {
|
|
32
|
-
const { dir, cortex } = setup();
|
|
33
|
-
try {
|
|
34
|
-
const qa = createQaEngine({ cortex, llm: { complete: async () => 'should not be called' } });
|
|
35
|
-
const result = await qa.answer({ question: 'xyzzyplugh' });
|
|
36
|
-
assert.equal(result.mode, 'abstain');
|
|
37
|
-
assert.equal(result.sources.length, 0);
|
|
38
|
-
} finally {
|
|
39
|
-
cortex.close();
|
|
40
|
-
rmSync(dir, { recursive: true, force: true });
|
|
41
|
-
}
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
test('qa reranker can reorder evidence before answering', async () => {
|
|
45
|
-
const qa = createQaEngine({
|
|
46
|
-
cortex: {
|
|
47
|
-
search: () => ({
|
|
48
|
-
items: [
|
|
49
|
-
{ id: 'a', layer: 'L2', title: 'A', snippet: '不相关', trace: {} },
|
|
50
|
-
{ id: 'b', layer: 'L2', title: 'B', snippet: '相关', trace: {} },
|
|
51
|
-
],
|
|
52
|
-
trace: { sources: [{ source: 'fts', count: 2 }] },
|
|
53
|
-
}),
|
|
54
|
-
},
|
|
55
|
-
reranker: { rerank: async () => [{ index: 1, score: 0.9 }, { index: 0, score: 0.1 }] },
|
|
56
|
-
});
|
|
57
|
-
const result = await qa.answer({ question: '测试' });
|
|
58
|
-
assert.equal(result.sources[0].id, 'b');
|
|
59
|
-
assert.match(result.answer, /相关/);
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
test('host prefetch recall is not charged to the search budget', () => {
|
|
63
|
-
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-prefetch-budget-'));
|
|
64
|
-
const cortex = createCortex({ config: { dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite'), maxCallsPerHour: 1 } });
|
|
65
|
-
try {
|
|
66
|
-
cortex.write({ text: '用户最喜欢米饭。', explicit: true });
|
|
67
|
-
assert.equal(cortex.search('米饭', { noBudget: true }).items.length, 1);
|
|
68
|
-
assert.equal(cortex.search('米饭', { noBudget: true }).items.length, 1);
|
|
69
|
-
assert.equal(cortex.budget.snapshot().calls, 0);
|
|
70
|
-
assert.equal(cortex.search('米饭').items.length, 1);
|
|
71
|
-
assert.equal(cortex.search('米饭').trace.blocked, 'budget');
|
|
72
|
-
} finally {
|
|
73
|
-
cortex.close();
|
|
74
|
-
rmSync(dir, { recursive: true, force: true });
|
|
75
|
-
}
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
test('qa engine uses an injected LLM and abstains when nothing is recalled', async () => {
|
|
79
|
-
const { dir, cortex } = setup();
|
|
80
|
-
try {
|
|
81
|
-
const qa = createQaEngine({ cortex, llm: { complete: async () => '米饭 [1]' } });
|
|
82
|
-
const result = await qa.answer({ question: '用户喜欢什么?' });
|
|
83
|
-
assert.equal(result.mode, 'llm');
|
|
84
|
-
assert.match(result.answer, /米饭/);
|
|
85
|
-
const emptyDir = mkdtempSync(path.join(os.tmpdir(), 'cortex-qa-empty-'));
|
|
86
|
-
const empty = createCortex({ config: { dataRoot: emptyDir, dbFile: path.join(emptyDir, 'cortex.sqlite') } });
|
|
87
|
-
try {
|
|
88
|
-
const emptyQa = createQaEngine({ cortex: empty, llm: { complete: async () => 'should not be called' } });
|
|
89
|
-
const missing = await emptyQa.answer({ question: 'xyzzyplugh' });
|
|
90
|
-
assert.equal(missing.mode, 'abstain');
|
|
91
|
-
assert.equal(missing.sources.length, 0);
|
|
92
|
-
} finally {
|
|
93
|
-
empty.close();
|
|
94
|
-
rmSync(emptyDir, { recursive: true, force: true });
|
|
95
|
-
}
|
|
96
|
-
} finally {
|
|
97
|
-
cortex.close();
|
|
98
|
-
rmSync(dir, { recursive: true, force: true });
|
|
99
|
-
}
|
|
100
|
-
});
|