dsh-cortex-memory 0.4.146 → 0.4.147
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +2396 -2369
- package/README.md +8 -6
- package/bench/semantic-cn.js +4 -4
- package/bench/synthetic.js +2 -2
- package/bin/copy-db.mjs +1 -1
- package/client.js +1 -1
- package/docs/quota-compaction-design.md +2 -2
- package/icon.svg +12 -2
- package/package.json +1 -1
- package/src/engines/consolidate.js +2 -2
- package/src/engines/health.js +1 -1
- package/src/engines/life.js +2 -2
- package/src/engines/narrative.js +2 -2
- package/src/host/scheduler.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,130 +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-evolution-'));
|
|
10
|
-
const cortex = createCortex({ config: { dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite') } });
|
|
11
|
-
return { dir, cortex };
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
test('policies are versioned, activated and rolled back', () => {
|
|
15
|
-
const { dir, cortex } = setup();
|
|
16
|
-
try {
|
|
17
|
-
const first = cortex.evolution.propose({ kind: 'retrieval', config: { confidenceWeight: 0.2 }, rationale: 'v1' });
|
|
18
|
-
const second = cortex.evolution.propose({ kind: 'retrieval', config: { confidenceWeight: 0.3 }, rationale: 'v2' });
|
|
19
|
-
assert.equal(first.version, 1);
|
|
20
|
-
assert.equal(second.version, 2);
|
|
21
|
-
cortex.evolution.approve(first.id);
|
|
22
|
-
assert.equal(cortex.policies.active('retrieval').id, first.id);
|
|
23
|
-
cortex.evolution.approve(second.id);
|
|
24
|
-
assert.equal(cortex.policies.active('retrieval').id, second.id);
|
|
25
|
-
const rolled = cortex.evolution.rollback('retrieval', first.id);
|
|
26
|
-
assert.equal(rolled.id, first.id);
|
|
27
|
-
assert.equal(cortex.policies.active('retrieval').id, first.id);
|
|
28
|
-
} finally {
|
|
29
|
-
cortex.close();
|
|
30
|
-
rmSync(dir, { recursive: true, force: true });
|
|
31
|
-
}
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
test('approved retrieval policy changes the live retrieval budget', () => {
|
|
35
|
-
const { dir, cortex } = setup();
|
|
36
|
-
try {
|
|
37
|
-
cortex.write({ text: '用户说会买米饭。', explicit: true });
|
|
38
|
-
const policy = cortex.evolution.propose({ kind: 'retrieval', config: { budget: 100 }, rationale: 'small budget' });
|
|
39
|
-
cortex.evolution.approve(policy.id);
|
|
40
|
-
const result = cortex.search('米饭', { budget: 5000 });
|
|
41
|
-
assert.equal(result.budget.total, 100);
|
|
42
|
-
} finally {
|
|
43
|
-
cortex.close();
|
|
44
|
-
rmSync(dir, { recursive: true, force: true });
|
|
45
|
-
}
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
test('selfOptimize proposes a skill demotion when success rate is low', () => {
|
|
49
|
-
const { dir, cortex } = setup();
|
|
50
|
-
try {
|
|
51
|
-
const skill = cortex.engines.skills.create({ name: '不靠谱的技能', steps: ['a'], state: 'active' });
|
|
52
|
-
cortex.engines.skills.record(skill.id, false);
|
|
53
|
-
cortex.engines.skills.record(skill.id, false);
|
|
54
|
-
cortex.engines.skills.record(skill.id, false);
|
|
55
|
-
const result = cortex.evolution.selfOptimize();
|
|
56
|
-
assert.ok(result.proposals.some((item) => item.kind === 'skill' && item.config.skillId === skill.id));
|
|
57
|
-
assert.equal(cortex.policies.active('skill'), null);
|
|
58
|
-
} finally {
|
|
59
|
-
cortex.close();
|
|
60
|
-
rmSync(dir, { recursive: true, force: true });
|
|
61
|
-
}
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
test('autoEvolve activates its own policy candidates', () => {
|
|
65
|
-
const { dir, cortex } = setup();
|
|
66
|
-
try {
|
|
67
|
-
const skill = cortex.engines.skills.create({ name: 'auto-evolve-test', steps: ['a'], state: 'active' });
|
|
68
|
-
cortex.engines.skills.record(skill.id, false);
|
|
69
|
-
cortex.engines.skills.record(skill.id, false);
|
|
70
|
-
cortex.engines.skills.record(skill.id, false);
|
|
71
|
-
const result = cortex.evolution.autoEvolve();
|
|
72
|
-
assert.ok(result.proposals.length >= 1);
|
|
73
|
-
assert.ok(result.activated.length >= 1);
|
|
74
|
-
assert.equal(result.activated[0].status, 'active');
|
|
75
|
-
} finally {
|
|
76
|
-
cortex.close();
|
|
77
|
-
rmSync(dir, { recursive: true, force: true });
|
|
78
|
-
}
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
test('identity proposals require explicit approval', () => {
|
|
82
|
-
const { dir, cortex } = setup();
|
|
83
|
-
try {
|
|
84
|
-
const proposal = cortex.evolution.proposeIdentity({
|
|
85
|
-
content: { narrative: '我是一只在学习长大的鲸鱼。', values: ['诚实'], boundaries: ['不操纵'] },
|
|
86
|
-
rationale: 'after a meaningful episode',
|
|
87
|
-
});
|
|
88
|
-
assert.equal(proposal.status, 'proposed');
|
|
89
|
-
const activeBefore = cortex.stores.identityStore.active('global');
|
|
90
|
-
const approved = cortex.evolution.approveIdentity(proposal.id, { approvedBy: 'user' });
|
|
91
|
-
assert.equal(approved.status, 'active');
|
|
92
|
-
assert.notEqual(approved.id, activeBefore.id);
|
|
93
|
-
} finally {
|
|
94
|
-
cortex.close();
|
|
95
|
-
rmSync(dir, { recursive: true, force: true });
|
|
96
|
-
}
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
test('evolution can evaluate a candidate policy and persist metrics', async () => {
|
|
100
|
-
const { dir, cortex } = setup();
|
|
101
|
-
try {
|
|
102
|
-
const policy = cortex.evolution.propose({ kind: 'retrieval', config: { budget: 300 }, rationale: 'evaluate me' });
|
|
103
|
-
const queries = [{ query: '米饭', relevant: ['a'] }, { query: '插件', relevant: ['b'] }];
|
|
104
|
-
const evaluated = await cortex.evolution.evaluate({
|
|
105
|
-
id: policy.id,
|
|
106
|
-
queries,
|
|
107
|
-
current: async (query) => ({ items: query === '米饭' ? [{ id: 'x' }] : [{ id: 'b' }] }),
|
|
108
|
-
candidate: async (query) => ({ items: query === '米饭' ? [{ id: 'a' }] : [{ id: 'b' }] }),
|
|
109
|
-
});
|
|
110
|
-
assert.ok(evaluated.metrics);
|
|
111
|
-
assert.ok(evaluated.comparison.delta.recallAtK > 0);
|
|
112
|
-
assert.equal(cortex.policies.get(policy.id).metrics.before.recallAtK, 0.5);
|
|
113
|
-
} finally {
|
|
114
|
-
cortex.close();
|
|
115
|
-
rmSync(dir, { recursive: true, force: true });
|
|
116
|
-
}
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
test('an approved initiative policy changes the live step output', () => {
|
|
120
|
-
const { dir, cortex } = setup();
|
|
121
|
-
try {
|
|
122
|
-
const policy = cortex.evolution.propose({ kind: 'initiative', config: { maxPerTurn: 0 }, rationale: 'quiet' });
|
|
123
|
-
cortex.evolution.approve(policy.id);
|
|
124
|
-
const result = cortex.step({ text: '用户是坏蛋,助手讨厌你。', explicit: true });
|
|
125
|
-
assert.equal(result.intentions.length, 0);
|
|
126
|
-
} finally {
|
|
127
|
-
cortex.close();
|
|
128
|
-
rmSync(dir, { recursive: true, force: true });
|
|
129
|
-
}
|
|
130
|
-
});
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
import test from 'node:test';
|
|
2
|
-
import assert from 'node:assert/strict';
|
|
3
|
-
import http from 'node:http';
|
|
4
|
-
import { mkdtempSync, rmSync } from 'node:fs';
|
|
5
|
-
import os from 'node:os';
|
|
6
|
-
import path from 'node:path';
|
|
7
|
-
import { createCortex } from '../../src/service/cortex.js';
|
|
8
|
-
|
|
9
|
-
test('async search uses the configured external ANN and keeps scope filtering', async () => {
|
|
10
|
-
const targetIds = [];
|
|
11
|
-
let calls = 0;
|
|
12
|
-
const server = http.createServer((request, response) => {
|
|
13
|
-
let raw = '';
|
|
14
|
-
request.on('data', (chunk) => { raw += chunk; });
|
|
15
|
-
request.on('end', () => {
|
|
16
|
-
const body = JSON.parse(raw || '{}');
|
|
17
|
-
assert.equal(body.vector.length, 256);
|
|
18
|
-
calls += 1;
|
|
19
|
-
response.writeHead(200, { 'content-type': 'application/json' });
|
|
20
|
-
response.end(JSON.stringify({ items: targetIds.map((id, index) => ({ id, score: 0.99 - index * 0.01 })) }));
|
|
21
|
-
});
|
|
22
|
-
});
|
|
23
|
-
await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve));
|
|
24
|
-
const port = server.address().port;
|
|
25
|
-
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-external-ann-'));
|
|
26
|
-
const cortex = createCortex({
|
|
27
|
-
config: {
|
|
28
|
-
dataRoot: dir,
|
|
29
|
-
dbFile: path.join(dir, 'cortex.sqlite'),
|
|
30
|
-
features: { vector: true, vectorAnnExternal: true },
|
|
31
|
-
vectorAnnEndpoint: `http://127.0.0.1:${port}/search`,
|
|
32
|
-
},
|
|
33
|
-
});
|
|
34
|
-
try {
|
|
35
|
-
assert.equal(cortex.vector.status().externalAnn.enabled, true);
|
|
36
|
-
for (let index = 0; index < 6; index += 1) {
|
|
37
|
-
const written = cortex.write({ text: `外部 ANN 目标记忆 ${index}。`, explicit: true });
|
|
38
|
-
targetIds.push(written.trace.id);
|
|
39
|
-
}
|
|
40
|
-
const result = await cortex.searchAsync('外部 ANN', { limit: 5 });
|
|
41
|
-
|
|
42
|
-
assert.equal(calls, 1);
|
|
43
|
-
assert.ok(result.items.some((item) => targetIds.includes(item.id)));
|
|
44
|
-
assert.ok(result.trace.sources.some((source) => source.source === 'vector'));
|
|
45
|
-
} finally {
|
|
46
|
-
cortex.close();
|
|
47
|
-
await new Promise((resolve) => server.close(resolve));
|
|
48
|
-
rmSync(dir, { recursive: true, force: true });
|
|
49
|
-
}
|
|
50
|
-
});
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import test from 'node:test';
|
|
2
|
-
import assert from 'node:assert/strict';
|
|
3
|
-
import { createExternalAnnClient } from '../../src/vector/external-ann.js';
|
|
4
|
-
|
|
5
|
-
test('external ANN client sends a bounded query and filters bad items', async () => {
|
|
6
|
-
let request;
|
|
7
|
-
const client = createExternalAnnClient({
|
|
8
|
-
endpoint: 'http://127.0.0.1:9999/search',
|
|
9
|
-
timeoutMs: 500,
|
|
10
|
-
fetchImpl: async (url, options) => {
|
|
11
|
-
request = { url, body: JSON.parse(options.body), signal: Boolean(options.signal) };
|
|
12
|
-
return { ok: true, json: async () => ({ items: [{ id: 'a', score: 0.9 }, { id: 2, score: 0.8 }, { id: 'b' }] }) };
|
|
13
|
-
},
|
|
14
|
-
});
|
|
15
|
-
const items = await client.search(new Array(16).fill(0.1), 5);
|
|
16
|
-
assert.equal(request.url, 'http://127.0.0.1:9999/search');
|
|
17
|
-
assert.equal(request.body.k, 5);
|
|
18
|
-
assert.equal(request.signal, true);
|
|
19
|
-
assert.deepEqual(items, [{ id: 'a', score: 0.9 }]);
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
test('external ANN client rejects a missing endpoint and bad responses', async () => {
|
|
23
|
-
assert.throws(() => createExternalAnnClient({ endpoint: 'file:///tmp/index' }), /http/);
|
|
24
|
-
const client = createExternalAnnClient({
|
|
25
|
-
endpoint: 'http://127.0.0.1:9999/search',
|
|
26
|
-
fetchImpl: async () => ({ ok: true, json: async () => ({ nope: true }) }),
|
|
27
|
-
});
|
|
28
|
-
await assert.rejects(() => client.search([0.1], 3), /items/);
|
|
29
|
-
});
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import test from 'node:test';
|
|
2
|
-
import assert from 'node:assert/strict';
|
|
3
|
-
import { extractFactCandidates } from '../../src/engines/extract.js';
|
|
4
|
-
|
|
5
|
-
test('问句不产生事实', () => {
|
|
6
|
-
assert.equal(extractFactCandidates('你会有感觉吗?').length, 0);
|
|
7
|
-
assert.equal(extractFactCandidates('你会先看见它们吗').length, 0);
|
|
8
|
-
assert.equal(extractFactCandidates('小明是程序员吗?').length, 0);
|
|
9
|
-
});
|
|
10
|
-
|
|
11
|
-
test('假设句不产生事实', () => {
|
|
12
|
-
assert.equal(extractFactCandidates('要是想聊两句,助手就趴在这儿听。').length, 0);
|
|
13
|
-
assert.equal(extractFactCandidates('如果你喜欢米饭,就多吃一碗。').length, 0);
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
test('单字功能词不再被当成主语', () => {
|
|
17
|
-
const facts = extractFactCandidates('要是想聊两句。');
|
|
18
|
-
assert.equal(facts.find((fact) => fact.subject === '要'), undefined);
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
test('普通陈述句仍然抽事实', () => {
|
|
22
|
-
const facts = extractFactCandidates('用户喜欢米饭。');
|
|
23
|
-
assert.ok(facts.some((fact) => fact.predicate === 'likes' && fact.object === '米饭'));
|
|
24
|
-
const isFacts = extractFactCandidates('小明是程序员。');
|
|
25
|
-
assert.ok(isFacts.some((fact) => fact.predicate === 'is' && fact.object === '程序员'));
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
test('设计讨论与方案稿不产生事实', () => {
|
|
29
|
-
assert.equal(extractFactCandidates('> 新坑 3:你到底在跟谁说话。').length, 0);
|
|
30
|
-
assert.equal(extractFactCandidates('新坑 4:一个自我被归档,你会有感觉吗。').length, 0);
|
|
31
|
-
assert.equal(extractFactCandidates('设计草案:我们打算把记忆层拆成三层。').length, 0);
|
|
32
|
-
assert.equal(extractFactCandidates('待办:明天把桥的接口补上。').length, 0);
|
|
33
|
-
assert.ok(extractFactCandidates('用户喜欢米饭。').length > 0, '普通陈述仍然能抽');
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
test('引用别人的话和命令句不产生事实', () => {
|
|
37
|
-
assert.equal(extractFactCandidates('他说:「小明喜欢白猫。」').length, 0, '引号里是别人的话');
|
|
38
|
-
assert.ok(extractFactCandidates('小明喜欢白猫。').length > 0, '同样的句子不在引号里就能抽');
|
|
39
|
-
assert.equal(extractFactCandidates('记得把桥修好,小桥是重点。').length, 0, '命令不是事实');
|
|
40
|
-
assert.ok(extractFactCandidates('小桥是重点。').length > 0);
|
|
41
|
-
});
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import test from 'node:test';
|
|
2
|
-
import assert from 'node:assert/strict';
|
|
3
|
-
import { guardFactCandidate } from '../../src/engines/fact-guard.js';
|
|
4
|
-
import { extractFactsWithLlm } from '../../src/engines/llm-facts.js';
|
|
5
|
-
import { createFactExtractor } from '../../src/engines/extract.js';
|
|
6
|
-
|
|
7
|
-
test('junk triples seen in the 0.4.22 store are rejected', () => {
|
|
8
|
-
assert.equal(guardFactCandidate({ subject: '现', predicate: 'at', object: ':' }), null);
|
|
9
|
-
assert.equal(guardFactCandidate({ subject: '只', predicate: 'is', object: '一些' }), null);
|
|
10
|
-
assert.equal(guardFactCandidate({ subject: '还', predicate: 'is', object: '很好' }), null);
|
|
11
|
-
assert.equal(guardFactCandidate({ subject: '用户', predicate: '现', object: '米饭' }), null);
|
|
12
|
-
assert.equal(guardFactCandidate({ subject: '用户', predicate: 'likes', object: ':' }), null);
|
|
13
|
-
assert.equal(guardFactCandidate({ subject: '用户', predicate: 'likes', object: '' }), null);
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
test('a plausible triple survives and is normalized', () => {
|
|
17
|
-
const guarded = guardFactCandidate({ subject: ' 用户 ', predicate: ' Likes ', object: '米饭', confidence: 1.4 });
|
|
18
|
-
assert.deepEqual(guarded, { subject: '用户', predicate: 'likes', object: '米饭', confidence: 1 });
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
test('LLM extractor drops junk and keeps the valid fact', async () => {
|
|
22
|
-
const llm = {
|
|
23
|
-
complete: async () => JSON.stringify({
|
|
24
|
-
facts: [
|
|
25
|
-
{ subject: '现', predicate: 'at', object: ':', confidence: 0.9 },
|
|
26
|
-
{ subject: '只', predicate: 'is', object: '猫', confidence: 0.9 },
|
|
27
|
-
{ subject: '用户', predicate: 'likes', object: '米饭', confidence: 0.8 },
|
|
28
|
-
{ subject: '用户', predicate: 'likes', object: '海', confidence: 0.3 },
|
|
29
|
-
],
|
|
30
|
-
}),
|
|
31
|
-
};
|
|
32
|
-
const result = await extractFactsWithLlm({ llm, text: '用户喜欢米饭。' });
|
|
33
|
-
assert.equal(result.candidates.length, 1);
|
|
34
|
-
assert.equal(result.candidates[0].subject, '用户');
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
test('extractor resolves only guarded candidates', () => {
|
|
38
|
-
const upserts = [];
|
|
39
|
-
const resolved = [];
|
|
40
|
-
const entities = { upsert: ({ name }) => { upserts.push(name); return { id: `ent_${name}` }; } };
|
|
41
|
-
const facts = { resolve: (input) => { resolved.push(input); return { fact: { id: 'f' } }; } };
|
|
42
|
-
const extractor = createFactExtractor({ entities, facts });
|
|
43
|
-
const result = extractor.extract({
|
|
44
|
-
text: '助手是本地记忆助手。现是在:。',
|
|
45
|
-
persona: { selfName: '助手', userAddress: '用户' },
|
|
46
|
-
});
|
|
47
|
-
assert.ok(upserts.includes('助手'));
|
|
48
|
-
assert.ok(!upserts.includes('现'));
|
|
49
|
-
assert.ok(resolved.every((item) => /^[a-z_]+$/.test(item.predicate)));
|
|
50
|
-
assert.ok(result.candidates.length >= 1);
|
|
51
|
-
});
|
|
@@ -1,44 +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 { extractFactCandidates } from '../../src/engines/extract.js';
|
|
8
|
-
|
|
9
|
-
test('叙述句碎片不会被抽成事实(等/收一收/水里)', () => {
|
|
10
|
-
const candidates = extractFactCandidates('它在水里收一收,然后又等了一会儿。', { selfName: '助手', userAddress: '用户' });
|
|
11
|
-
assert.ok(Array.isArray(candidates));
|
|
12
|
-
assert.equal(candidates.length, 0, `不该抽出事实:${JSON.stringify(candidates)}`);
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
test('正常句子仍然能抽出事实', () => {
|
|
16
|
-
const candidates = extractFactCandidates('助手喜欢米饭。', { selfName: '助手', userAddress: '用户' });
|
|
17
|
-
assert.ok(candidates.length > 0, '正常句子不该被误杀');
|
|
18
|
-
assert.equal(candidates[0].predicate, 'likes');
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
test('LLM 没开时健康报告给出明确警告', () => {
|
|
22
|
-
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-llm-warn-'));
|
|
23
|
-
const cortex = createCortex({ config: { dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite'), features: { llm: false } } });
|
|
24
|
-
try {
|
|
25
|
-
const health = cortex.health({ scopeId: 'global' });
|
|
26
|
-
const warn = health.issues.find((issue) => issue.code === 'LLM_DISABLED');
|
|
27
|
-
assert.ok(warn, 'features.llm=false 时应提示');
|
|
28
|
-
} finally {
|
|
29
|
-
cortex.close();
|
|
30
|
-
rmSync(dir, { recursive: true, force: true });
|
|
31
|
-
}
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
test('LLM 打开时不再报 LLM_DISABLED', () => {
|
|
35
|
-
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-llm-on-'));
|
|
36
|
-
const cortex = createCortex({ config: { dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite'), features: { llm: true } } });
|
|
37
|
-
try {
|
|
38
|
-
const health = cortex.health({ scopeId: 'global' });
|
|
39
|
-
assert.equal(health.issues.find((issue) => issue.code === 'LLM_DISABLED'), undefined);
|
|
40
|
-
} finally {
|
|
41
|
-
cortex.close();
|
|
42
|
-
rmSync(dir, { recursive: true, force: true });
|
|
43
|
-
}
|
|
44
|
-
});
|
|
@@ -1,32 +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('belief uses the transaction timestamp, not updated_at', () => {
|
|
9
|
-
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-belief-'));
|
|
10
|
-
const cortex = createCortex({ config: { dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite') } });
|
|
11
|
-
try {
|
|
12
|
-
const t1 = Date.parse('2026-11-10T10:00:00+08:00');
|
|
13
|
-
const t2 = Date.parse('2026-11-11T10:00:00+08:00');
|
|
14
|
-
cortex.write({ text: '用户喜欢米饭。', explicit: true, now: t1, facts: [{ subject: '用户', predicate: '喜欢', object: '米饭' }] });
|
|
15
|
-
cortex.write({ text: '用户现在喜欢面。', explicit: true, now: t2, facts: [{ subject: '用户', predicate: '喜欢', object: '面' }] });
|
|
16
|
-
const old = cortex.asOf({ subject: '用户', predicate: '喜欢', at: new Date(t2 - 1000).toISOString(), mode: 'belief' });
|
|
17
|
-
assert.equal(old.facts.length, 1);
|
|
18
|
-
assert.match(old.facts[0].object, /米饭/);
|
|
19
|
-
assert.ok(old.facts[0].supersededAt);
|
|
20
|
-
const now = cortex.asOf({ subject: '用户', predicate: '喜欢', at: new Date(t2 + 1000).toISOString(), mode: 'belief' });
|
|
21
|
-
assert.equal(now.facts.length, 1);
|
|
22
|
-
assert.match(now.facts[0].object, /面/);
|
|
23
|
-
const historical = cortex.asOf({ subject: '用户', predicate: '喜欢', at: new Date(t1 + 1000).toISOString(), mode: 'historical' });
|
|
24
|
-
assert.match(historical.facts[0].object, /米饭/);
|
|
25
|
-
const strict = cortex.asOf({ subject: '用户', predicate: '喜欢', mode: 'strict' });
|
|
26
|
-
assert.match(strict.facts[0].object, /面/);
|
|
27
|
-
assert.doesNotMatch(strict.facts.map((fact) => fact.object).join(' '), /米饭/);
|
|
28
|
-
} finally {
|
|
29
|
-
cortex.close();
|
|
30
|
-
rmSync(dir, { recursive: true, force: true });
|
|
31
|
-
}
|
|
32
|
-
});
|
|
@@ -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 { createFactRepository, negationFlip } from '../../src/store/repositories/facts.js';
|
|
8
|
-
|
|
9
|
-
function setup() {
|
|
10
|
-
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-facts-compat-'));
|
|
11
|
-
const { db } = openDatabase({ dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite') });
|
|
12
|
-
migrate(db);
|
|
13
|
-
return { dir, db, facts: createFactRepository(db) };
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
function ensureEntity(db, id, name, at) {
|
|
17
|
-
db.prepare(`
|
|
18
|
-
INSERT INTO entities(id, scope_id, type, name, canonical, aliases_json, summary, created_at, updated_at)
|
|
19
|
-
VALUES (?, 'global', 'concept', ?, ?, '[]', '', ?, ?)
|
|
20
|
-
`).run(id, name, name, at, at);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
test('only a leading negation counts as a flip', () => {
|
|
24
|
-
assert.equal(negationFlip('会一直在', '不会一直在'), true);
|
|
25
|
-
assert.equal(negationFlip('没有米饭', '有米饭'), true);
|
|
26
|
-
assert.equal(negationFlip('一直存在着', '一直在——记忆不丢'), false);
|
|
27
|
-
assert.equal(negationFlip('喜欢米饭', '喜欢米饭。'), false);
|
|
28
|
-
assert.equal(negationFlip('', '一直在'), false);
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
test('same subject/predicate with compatible literals is not a dispute', () => {
|
|
32
|
-
const { dir, db, facts } = setup();
|
|
33
|
-
try {
|
|
34
|
-
const at = '2026-11-15T10:00:00.000Z';
|
|
35
|
-
ensureEntity(db, 'ent_x', '助手', at);
|
|
36
|
-
const first = facts.resolve({ scopeId: 'global', subjectId: 'ent_x', predicate: 'will', objectLiteral: '一直存在着', validFrom: at, recordedAt: at });
|
|
37
|
-
assert.equal(first.action, 'created');
|
|
38
|
-
const second = facts.resolve({ scopeId: 'global', subjectId: 'ent_x', predicate: 'will', objectLiteral: '一直在——记忆不丢', validFrom: at, recordedAt: at });
|
|
39
|
-
assert.equal(second.action, 'compatible');
|
|
40
|
-
assert.equal(facts.disputes({ scopeId: 'global', status: 'open' }).length, 0);
|
|
41
|
-
assert.equal(facts.get(first.fact.id).status, 'active');
|
|
42
|
-
assert.equal(facts.get(second.fact.id).status, 'active');
|
|
43
|
-
} finally {
|
|
44
|
-
closeDatabase(db);
|
|
45
|
-
rmSync(dir, { recursive: true, force: true });
|
|
46
|
-
}
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
test('a negation still produces a dispute', () => {
|
|
50
|
-
const { dir, db, facts } = setup();
|
|
51
|
-
try {
|
|
52
|
-
const at = '2026-11-15T10:00:00.000Z';
|
|
53
|
-
ensureEntity(db, 'ent_y', '助手', at);
|
|
54
|
-
facts.resolve({ scopeId: 'global', subjectId: 'ent_y', predicate: 'will', objectLiteral: '会一直在', validFrom: at, recordedAt: at });
|
|
55
|
-
const result = facts.resolve({ scopeId: 'global', subjectId: 'ent_y', predicate: 'will', objectLiteral: '不会一直在', validFrom: at, recordedAt: at });
|
|
56
|
-
assert.equal(result.action, 'dispute');
|
|
57
|
-
assert.ok(facts.disputes({ scopeId: 'global', status: 'open' }).length >= 1);
|
|
58
|
-
} finally {
|
|
59
|
-
closeDatabase(db);
|
|
60
|
-
rmSync(dir, { recursive: true, force: true });
|
|
61
|
-
}
|
|
62
|
-
});
|
package/test/unit/facts.test.js
DELETED
|
@@ -1,107 +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, transaction } from '../../src/store/database.js';
|
|
7
|
-
import { createEventRepository } from '../../src/store/repositories/events.js';
|
|
8
|
-
import { createEntityRepository } from '../../src/store/repositories/entities.js';
|
|
9
|
-
import { createFactRepository } from '../../src/store/repositories/facts.js';
|
|
10
|
-
|
|
11
|
-
function setup() {
|
|
12
|
-
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-facts-'));
|
|
13
|
-
const { db } = openDatabase({ dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite') });
|
|
14
|
-
migrate(db);
|
|
15
|
-
return { dir, db };
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
test('entities merge aliases and facts revise over time', () => {
|
|
19
|
-
const { dir, db } = setup();
|
|
20
|
-
try {
|
|
21
|
-
const events = createEventRepository(db);
|
|
22
|
-
const entities = createEntityRepository(db);
|
|
23
|
-
const facts = createFactRepository(db);
|
|
24
|
-
const owner = entities.upsert({ name: '用户', type: 'person', aliases: ['凌寒'] });
|
|
25
|
-
const merged = entities.upsert({ name: '用户', type: 'person', aliases: ['用户大人'] });
|
|
26
|
-
assert.equal(merged.id, owner.id);
|
|
27
|
-
assert.deepEqual(merged.aliases.sort(), ['凌寒', '用户大人'].sort());
|
|
28
|
-
const rice = entities.upsert({ name: '米饭', type: 'food' });
|
|
29
|
-
const meal = entities.upsert({ name: '面条', type: 'food' });
|
|
30
|
-
const event = events.append({ kind: 'utterance', payload: { text: '用户喜欢米饭' } });
|
|
31
|
-
|
|
32
|
-
const first = facts.resolve({
|
|
33
|
-
subjectId: owner.id,
|
|
34
|
-
predicate: 'likes',
|
|
35
|
-
objectId: rice.id,
|
|
36
|
-
validFrom: '2026-11-01T00:00:00.000Z',
|
|
37
|
-
recordedAt: '2026-11-01T00:00:00.000Z',
|
|
38
|
-
confidence: 0.7,
|
|
39
|
-
sourceEventId: event.id,
|
|
40
|
-
});
|
|
41
|
-
assert.equal(first.action, 'created');
|
|
42
|
-
assert.equal(facts.queryAt({ subjectId: owner.id, predicate: 'likes', at: '2026-11-05T00:00:00.000Z' }).length, 1);
|
|
43
|
-
|
|
44
|
-
const corroborated = facts.resolve({
|
|
45
|
-
subjectId: owner.id,
|
|
46
|
-
predicate: 'likes',
|
|
47
|
-
objectId: rice.id,
|
|
48
|
-
validFrom: '2026-11-02T00:00:00.000Z',
|
|
49
|
-
confidence: 0.6,
|
|
50
|
-
sourceEventId: event.id,
|
|
51
|
-
});
|
|
52
|
-
assert.equal(corroborated.action, 'corroborate');
|
|
53
|
-
assert.ok(corroborated.fact.confidence > first.fact.confidence);
|
|
54
|
-
|
|
55
|
-
const superseded = facts.resolve({
|
|
56
|
-
subjectId: owner.id,
|
|
57
|
-
predicate: 'likes',
|
|
58
|
-
objectId: meal.id,
|
|
59
|
-
validFrom: '2026-11-10T00:00:00.000Z',
|
|
60
|
-
confidence: 0.8,
|
|
61
|
-
sourceEventId: event.id,
|
|
62
|
-
});
|
|
63
|
-
assert.equal(superseded.action, 'supersede');
|
|
64
|
-
assert.equal(superseded.invalidated.length, 1);
|
|
65
|
-
assert.equal(superseded.invalidated[0].status, 'invalidated');
|
|
66
|
-
assert.equal(superseded.fact.status, 'active');
|
|
67
|
-
const oldAt = facts.queryAt({ subjectId: owner.id, predicate: 'likes', at: '2026-11-05T00:00:00.000Z' });
|
|
68
|
-
assert.equal(oldAt.length, 1);
|
|
69
|
-
assert.equal(oldAt[0].objectId, rice.id);
|
|
70
|
-
const newAt = facts.queryAt({ subjectId: owner.id, predicate: 'likes', at: '2026-11-11T00:00:00.000Z' });
|
|
71
|
-
assert.equal(newAt.length, 1);
|
|
72
|
-
assert.equal(newAt[0].objectId, meal.id);
|
|
73
|
-
} finally {
|
|
74
|
-
closeDatabase(db);
|
|
75
|
-
rmSync(dir, { recursive: true, force: true });
|
|
76
|
-
}
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
test('same-start contradiction becomes a dispute and backfill gets a bounded interval', () => {
|
|
80
|
-
const { dir, db } = setup();
|
|
81
|
-
try {
|
|
82
|
-
const entities = createEntityRepository(db);
|
|
83
|
-
const facts = createFactRepository(db);
|
|
84
|
-
const owner = entities.upsert({ name: '用户' });
|
|
85
|
-
const a = entities.upsert({ name: '米饭' });
|
|
86
|
-
const b = entities.upsert({ name: '饺子' });
|
|
87
|
-
facts.resolve({ subjectId: owner.id, predicate: 'likes', objectId: a.id, validFrom: '2026-11-10T00:00:00.000Z' });
|
|
88
|
-
const dispute = facts.resolve({ subjectId: owner.id, predicate: 'likes', objectId: b.id, validFrom: '2026-11-10T00:00:00.000Z' });
|
|
89
|
-
assert.equal(dispute.action, 'dispute');
|
|
90
|
-
assert.equal(dispute.dispute.status, 'open');
|
|
91
|
-
assert.equal(facts.disputes().length, 1);
|
|
92
|
-
const backfill = facts.resolve({
|
|
93
|
-
subjectId: owner.id,
|
|
94
|
-
predicate: 'likes',
|
|
95
|
-
objectId: entities.upsert({ name: '面条' }).id,
|
|
96
|
-
validFrom: '2026-10-01T00:00:00.000Z',
|
|
97
|
-
});
|
|
98
|
-
assert.equal(backfill.action, 'backfill');
|
|
99
|
-
assert.ok(backfill.fact.validTo);
|
|
100
|
-
const atOld = facts.queryAt({ subjectId: owner.id, predicate: 'likes', at: '2026-10-15T00:00:00.000Z' });
|
|
101
|
-
assert.equal(atOld.length, 1);
|
|
102
|
-
assert.equal(atOld[0].id, backfill.fact.id);
|
|
103
|
-
} finally {
|
|
104
|
-
closeDatabase(db);
|
|
105
|
-
rmSync(dir, { recursive: true, force: true });
|
|
106
|
-
}
|
|
107
|
-
});
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import test from 'node:test';
|
|
2
|
-
import assert from 'node:assert/strict';
|
|
3
|
-
import { mkdtempSync, readFileSync, rmSync } from 'node:fs';
|
|
4
|
-
import os from 'node:os';
|
|
5
|
-
import path from 'node:path';
|
|
6
|
-
import { readOverrides, writeOverride, rollbackOverride, effectiveFeatures, overridesPath } from '../../src/core/feature-overrides.js';
|
|
7
|
-
|
|
8
|
-
test('feature overrides persist, merge and roll back', () => {
|
|
9
|
-
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-overrides-'));
|
|
10
|
-
try {
|
|
11
|
-
const first = writeOverride(dir, 'llm', true);
|
|
12
|
-
assert.equal(first.to, true);
|
|
13
|
-
writeOverride(dir, 'vector', true);
|
|
14
|
-
writeOverride(dir, 'autoFacts', true);
|
|
15
|
-
const file = overridesPath(dir);
|
|
16
|
-
assert.match(readFileSync(file, 'utf8'), /"llm": true/);
|
|
17
|
-
const merged = effectiveFeatures({ dataRoot: dir, features: { llm: false, capture: true } });
|
|
18
|
-
assert.equal(merged.features.llm, true);
|
|
19
|
-
assert.equal(merged.features.vector, true);
|
|
20
|
-
assert.equal(merged.features.autoFacts, true);
|
|
21
|
-
assert.equal(merged.features.capture, true);
|
|
22
|
-
assert.equal(merged.overrides.llm, true);
|
|
23
|
-
const back = rollbackOverride(dir);
|
|
24
|
-
assert.equal(back.rolledBack, true);
|
|
25
|
-
assert.equal(back.key, 'autoFacts');
|
|
26
|
-
assert.equal(back.value, null);
|
|
27
|
-
assert.equal(merged.overrides.llm, true);
|
|
28
|
-
assert.deepEqual(readOverrides(dir).current, { llm: true, vector: true });
|
|
29
|
-
} finally {
|
|
30
|
-
rmSync(dir, { recursive: true, force: true });
|
|
31
|
-
}
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
test('feature overrides reject unknown keys and bad values', () => {
|
|
35
|
-
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-overrides-bad-'));
|
|
36
|
-
try {
|
|
37
|
-
assert.throws(() => writeOverride(dir, 'network', true), /not allowed/);
|
|
38
|
-
assert.throws(() => writeOverride(dir, 'llm', 'yes'), /boolean/);
|
|
39
|
-
} finally {
|
|
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 { mkdtempSync, rmSync } from 'node:fs';
|
|
4
|
-
import os from 'node:os';
|
|
5
|
-
import path from 'node:path';
|
|
6
|
-
import { openDatabase, migrate, getMeta, closeDatabase, FTS_INDEX_VERSION } 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 { ftsText } from '../../src/memory/tokenizer.js';
|
|
10
|
-
|
|
11
|
-
test('migrate rebuilds an old bigram-only FTS index once', () => {
|
|
12
|
-
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-fts-migrate-'));
|
|
13
|
-
try {
|
|
14
|
-
let { db } = openDatabase({ dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite') });
|
|
15
|
-
migrate(db);
|
|
16
|
-
const events = createEventRepository(db);
|
|
17
|
-
const traces = createTraceRepository(db);
|
|
18
|
-
const event = events.append({ kind: 'utterance', payload: { text: '米饭' } });
|
|
19
|
-
const trace = traces.insert({
|
|
20
|
-
eventId: event.id,
|
|
21
|
-
content: '用户说会买米饭。',
|
|
22
|
-
title: '米饭的承诺',
|
|
23
|
-
});
|
|
24
|
-
// Simulate a database written by the old tokenizer: bigram-only FTS.
|
|
25
|
-
db.exec('DELETE FROM traces_fts_index');
|
|
26
|
-
db.prepare('INSERT INTO traces_fts_index(trace_id, text) VALUES (?, ?)').run(trace.id, ftsText('米饭的承诺 用户说会买米饭。'));
|
|
27
|
-
db.prepare('DELETE FROM meta WHERE key = ?').run('fts_index_version');
|
|
28
|
-
closeDatabase(db);
|
|
29
|
-
|
|
30
|
-
const reopened = openDatabase({ dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite') });
|
|
31
|
-
db = reopened.db;
|
|
32
|
-
const result = migrate(db);
|
|
33
|
-
assert.equal(result.ftsRebuilt, true);
|
|
34
|
-
assert.equal(getMeta(db, 'fts_index_version'), String(FTS_INDEX_VERSION));
|
|
35
|
-
const repo = createTraceRepository(db);
|
|
36
|
-
assert.equal(repo.searchFts('饭团').length, 0);
|
|
37
|
-
assert.equal(repo.searchFts('饭团', { soft: true }).length, 1);
|
|
38
|
-
closeDatabase(db);
|
|
39
|
-
} finally {
|
|
40
|
-
rmSync(dir, { recursive: true, force: true });
|
|
41
|
-
}
|
|
42
|
-
});
|