dsh-cortex-memory 0.4.11 → 0.4.14
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 +111 -0
- package/LICENSE +1 -1
- package/README.md +63 -38
- package/bench/ab-engine.mjs +60 -0
- package/bench/ann-bench.mjs +78 -0
- package/bench/hnsw-bench.mjs +79 -0
- package/bench/locomo.js +89 -0
- package/bench/longmemeval.js +83 -0
- package/bench/metrics.js +67 -0
- package/bench/migration-bench.mjs +69 -0
- package/bench/run.js +77 -0
- package/bench/synthetic.js +99 -0
- package/bench/vector-ab.mjs +76 -0
- package/docs/compatibility.md +39 -0
- package/docs/install-windows.md +145 -0
- package/docs/models.md +183 -0
- package/docs/multimodal.md +112 -0
- package/docs/permissions.md +53 -0
- package/docs/privacy.md +50 -0
- package/docs/scopes.md +142 -0
- package/examples/README.md +73 -0
- package/examples/setup-cortex-bge.ps1 +66 -0
- package/examples/uninstall-cortex.ps1 +92 -0
- package/index.js +23 -0
- package/package.json +11 -5
- package/src/core/config.js +27 -0
- package/src/engines/extract.js +10 -10
- package/src/engines/retrieve.js +30 -5
- package/src/guardian/guardian.js +2 -15
- package/src/guardian/invariants.js +9 -5
- package/src/host/runtime.js +10 -14
- package/src/self/evolution.js +32 -5
- package/src/self/policies.js +21 -2
- package/src/service/cortex.js +3 -2
- package/src/store/database.js +9 -1
- package/src/store/schema.sql +2 -1
- package/src/vector/hnsw.js +5 -5
- package/test/integration/_helpers.js +128 -0
- package/test/integration/acceptance.test.js +803 -0
- package/test/integration/ann-load.test.js +61 -0
- package/test/integration/api-contract.test.js +1476 -0
- package/test/integration/backup-privacy.test.js +214 -0
- package/test/integration/consolidate.test.js +75 -0
- package/test/integration/edge-cases.test.js +1088 -0
- package/test/integration/exchange.test.js +326 -0
- package/test/integration/life-loop.test.js +188 -0
- package/test/integration/load.test.js +751 -0
- package/test/integration/markdown-sync.test.js +35 -0
- package/test/integration/policy-query-api.test.js +306 -0
- package/test/integration/resilience-writes.test.js +241 -0
- package/test/integration/tools-contract.test.js +1324 -0
- package/test/unit/ann-edge.test.js +102 -0
- package/test/unit/ann.test.js +77 -0
- package/test/unit/api-client.test.js +576 -0
- package/test/unit/api-validate.test.js +19 -0
- package/test/unit/api.test.js +555 -0
- package/test/unit/appraisal.test.js +48 -0
- package/test/unit/attachments.test.js +33 -0
- package/test/unit/autonomy.test.js +53 -0
- package/test/unit/batch.test.js +46 -0
- package/test/unit/bench.test.js +40 -0
- package/test/unit/benchmark-synthetic.test.js +52 -0
- package/test/unit/client-contract.test.js +29 -0
- package/test/unit/compression.test.js +76 -0
- package/test/unit/core.test.js +77 -0
- package/test/unit/develop.test.js +130 -0
- package/test/unit/development.test.js +68 -0
- package/test/unit/digest.test.js +26 -0
- package/test/unit/dsr.test.js +111 -0
- package/test/unit/edges-candidates.test.js +113 -0
- package/test/unit/emotion.test.js +61 -0
- package/test/unit/encode.test.js +98 -0
- package/test/unit/evolution.test.js +130 -0
- package/test/unit/external-ann-search.test.js +50 -0
- package/test/unit/external-ann.test.js +29 -0
- package/test/unit/facts-belief.test.js +32 -0
- package/test/unit/facts.test.js +107 -0
- package/test/unit/feature-overrides.test.js +42 -0
- package/test/unit/fts-migration.test.js +42 -0
- package/test/unit/graph.test.js +79 -0
- package/test/unit/growth-reflect.test.js +123 -0
- package/test/unit/guard-runtime.test.js +40 -0
- package/test/unit/guardian.test.js +91 -0
- package/test/unit/health-privacy.test.js +102 -0
- package/test/unit/hnsw.test.js +35 -0
- package/test/unit/host-runtime.test.js +142 -0
- package/test/unit/host.test.js +58 -0
- package/test/unit/identity.test.js +98 -0
- package/test/unit/induct-evaluate.test.js +41 -0
- package/test/unit/ingest-extracted.test.js +35 -0
- package/test/unit/initiative.test.js +90 -0
- package/test/unit/jobs.test.js +93 -0
- package/test/unit/json-snapshot.test.js +57 -0
- package/test/unit/life-consolidate.test.js +34 -0
- package/test/unit/life.test.js +149 -0
- package/test/unit/llm-facts-integration.test.js +27 -0
- package/test/unit/llm-facts.test.js +16 -0
- package/test/unit/llm.test.js +45 -0
- package/test/unit/locomo.test.js +45 -0
- package/test/unit/longmemeval.test.js +56 -0
- package/test/unit/markdown-import.test.js +44 -0
- package/test/unit/markdown.test.js +34 -0
- package/test/unit/memory-edit.test.js +112 -0
- package/test/unit/metrics.test.js +26 -0
- package/test/unit/migration.test.js +38 -0
- package/test/unit/model-config.test.js +52 -0
- package/test/unit/narrative.test.js +62 -0
- package/test/unit/playbook-consolidate.test.js +24 -0
- package/test/unit/privacy-scope.test.js +39 -0
- package/test/unit/profile.test.js +20 -0
- package/test/unit/prompt.test.js +42 -0
- package/test/unit/providers.test.js +42 -0
- package/test/unit/qa.test.js +100 -0
- package/test/unit/query.test.js +53 -0
- package/test/unit/retention.test.js +31 -0
- package/test/unit/retrieval-ranking.test.js +31 -0
- package/test/unit/retrieve.test.js +117 -0
- package/test/unit/rotation.test.js +25 -0
- package/test/unit/scanner.test.js +50 -0
- package/test/unit/scenario.test.js +78 -0
- package/test/unit/scheduler.test.js +136 -0
- package/test/unit/self-state.test.js +94 -0
- package/test/unit/service.test.js +72 -0
- package/test/unit/shiori-import.test.js +77 -0
- package/test/unit/skills.test.js +104 -0
- package/test/unit/store.test.js +111 -0
- package/test/unit/temporal-graph.test.js +44 -0
- package/test/unit/tiers.test.js +39 -0
- package/test/unit/tokenizer-fts.test.js +35 -0
- package/test/unit/tools.test.js +122 -0
- package/test/unit/upgrade-0412.test.js +104 -0
- package/test/unit/upgrade-0413.test.js +84 -0
- package/test/unit/upgrade-0414.test.js +101 -0
- package/test/unit/vector-ann.test.js +53 -0
- package/test/unit/vector-async.test.js +69 -0
- package/test/unit/vector.test.js +68 -0
- package/test/unit/working.test.js +80 -0
|
@@ -0,0 +1,142 @@
|
|
|
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, contentText } from '../../src/host/runtime.js';
|
|
8
|
+
import { assembleSystemPrompt } from '../../src/prompt/assemble.js';
|
|
9
|
+
|
|
10
|
+
function setup() {
|
|
11
|
+
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-host-runtime-'));
|
|
12
|
+
const cortex = createCortex({ config: { dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite') } });
|
|
13
|
+
const runtime = createHostRuntime({ cortex });
|
|
14
|
+
return { dir, cortex, runtime };
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const session = { id: 's1', header: { origin: undefined } };
|
|
18
|
+
|
|
19
|
+
test('turn events are captured as one trace with user and assistant text', () => {
|
|
20
|
+
const { dir, cortex, runtime } = setup();
|
|
21
|
+
try {
|
|
22
|
+
runtime.onSessionEvent(session, { type: 'turn/start', data: { turn: 1 }, time: Date.parse('2026-11-11T12:00:00+08:00') });
|
|
23
|
+
runtime.onSessionEvent(session, { type: 'user/message', data: { content: '用户说会买白饭。' }, time: Date.parse('2026-11-11T12:00:01+08:00') });
|
|
24
|
+
runtime.onSessionEvent(session, { type: 'assistant/message', data: { turn: 1, message: { content: '记住了。' } }, time: Date.parse('2026-11-11T12:00:02+08:00') });
|
|
25
|
+
runtime.onSessionEvent(session, { type: 'turn/end', data: { turn: 1, reason: { kind: 'completed' } }, time: Date.parse('2026-11-11T12:00:03+08:00') });
|
|
26
|
+
assert.equal(cortex.repos.traces.count(), 1);
|
|
27
|
+
const trace = cortex.repos.traces.list({ limit: 1 })[0];
|
|
28
|
+
assert.match(trace.content, /用户说会买白饭/);
|
|
29
|
+
assert.match(trace.content, /记住了/);
|
|
30
|
+
assert.equal(runtime.pending().length, 0);
|
|
31
|
+
} finally {
|
|
32
|
+
cortex.close();
|
|
33
|
+
rmSync(dir, { recursive: true, force: true });
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
test('duplicate turn capture is idempotent and subagents are skipped', () => {
|
|
38
|
+
const { dir, cortex, runtime } = setup();
|
|
39
|
+
try {
|
|
40
|
+
const at = Date.parse('2026-11-11T12:00:00+08:00');
|
|
41
|
+
runtime.onSessionEvent(session, { type: 'turn/start', data: { turn: 2 }, time: at });
|
|
42
|
+
runtime.onSessionEvent(session, { type: 'user/message', data: { content: '重复的回合。' }, time: at });
|
|
43
|
+
runtime.onSessionEvent(session, { type: 'assistant/message', data: { turn: 2, message: { content: '收到。' } }, time: at });
|
|
44
|
+
runtime.onSessionEvent(session, { type: 'turn/end', data: { turn: 2, reason: { kind: 'completed' } }, time: at });
|
|
45
|
+
runtime.onSessionEvent(session, { type: 'turn/start', data: { turn: 2 }, time: at });
|
|
46
|
+
runtime.onSessionEvent(session, { type: 'user/message', data: { content: '重复的回合。' }, time: at });
|
|
47
|
+
runtime.onSessionEvent(session, { type: 'assistant/message', data: { turn: 2, message: { content: '收到。' } }, time: at });
|
|
48
|
+
runtime.onSessionEvent(session, { type: 'turn/end', data: { turn: 2, reason: { kind: 'completed' } }, time: at });
|
|
49
|
+
assert.equal(cortex.repos.traces.count(), 1);
|
|
50
|
+
const sub = { id: 's2', header: { origin: 'subagent' } };
|
|
51
|
+
runtime.onSessionEvent(sub, { type: 'turn/start', data: { turn: 1 }, time: at });
|
|
52
|
+
runtime.onSessionEvent(sub, { type: 'user/message', data: { content: '子代理。' }, time: at });
|
|
53
|
+
runtime.onSessionEvent(sub, { type: 'assistant/message', data: { turn: 1, message: { content: '子代理回复。' } }, time: at });
|
|
54
|
+
runtime.onSessionEvent(sub, { type: 'turn/end', data: { turn: 1, reason: { kind: 'completed' } }, time: at });
|
|
55
|
+
assert.equal(cortex.repos.traces.count(), 1);
|
|
56
|
+
assert.equal(runtime.pending().length, 0);
|
|
57
|
+
} finally {
|
|
58
|
+
cortex.close();
|
|
59
|
+
rmSync(dir, { recursive: true, force: true });
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
test('user turns prefetch recall into a system-prompt block and turn/end clears it', () => {
|
|
64
|
+
const { dir, cortex, runtime } = setup();
|
|
65
|
+
try {
|
|
66
|
+
cortex.write({ text: '用户说会买白饭,助手等得起。', explicit: true, now: Date.parse('2026-11-11T11:00:00+08:00') });
|
|
67
|
+
runtime.onSessionEvent(session, { type: 'turn/start', data: { turn: 3 }, time: Date.parse('2026-11-11T12:00:00+08:00') });
|
|
68
|
+
runtime.onSessionEvent(session, { type: 'user/message', data: { content: '白饭的承诺还算数吗?' }, time: Date.parse('2026-11-11T12:00:01+08:00') });
|
|
69
|
+
const block = runtime.recallText({ session: { id: 's1' } });
|
|
70
|
+
assert.match(block, /召回的相关记忆/);
|
|
71
|
+
assert.match(block, /白饭/);
|
|
72
|
+
runtime.onSessionEvent(session, { type: 'turn/end', data: { turn: 3, reason: { kind: 'completed' } }, time: Date.parse('2026-11-11T12:00:02+08:00') });
|
|
73
|
+
assert.equal(runtime.recallText({ session: { id: 's1' } }), '');
|
|
74
|
+
} finally {
|
|
75
|
+
cortex.close();
|
|
76
|
+
rmSync(dir, { recursive: true, force: true });
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
test('turn/end runs one safe autonomy tick', () => {
|
|
81
|
+
const { dir, cortex, runtime } = setup();
|
|
82
|
+
try {
|
|
83
|
+
const at = Date.parse('2026-11-11T12:00:00+08:00');
|
|
84
|
+
cortex.write({ text: '第一条记忆。', explicit: true, now: at - 2000 });
|
|
85
|
+
cortex.write({ text: '第二条记忆。', explicit: true, now: at - 1000 });
|
|
86
|
+
const intention = cortex.stores.initiativeStore.insert({
|
|
87
|
+
kind: 'consolidate', priority: 0.9, reason: 'turn-end', payload: {}, createdAt: new Date(at).toISOString(),
|
|
88
|
+
});
|
|
89
|
+
runtime.onSessionEvent(session, { type: 'turn/start', data: { turn: 9 }, time: at });
|
|
90
|
+
runtime.onSessionEvent(session, { type: 'user/message', data: { content: '收工。' }, time: at });
|
|
91
|
+
runtime.onSessionEvent(session, { type: 'turn/end', data: { turn: 9, reason: { kind: 'completed' } }, time: at });
|
|
92
|
+
assert.equal(cortex.stores.initiativeStore.get(intention.id).status, 'acted');
|
|
93
|
+
} finally {
|
|
94
|
+
cortex.close();
|
|
95
|
+
rmSync(dir, { recursive: true, force: true });
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
test('capture:false keeps recall but writes no trace', () => {
|
|
100
|
+
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-host-no-capture-'));
|
|
101
|
+
const cortex = createCortex({ config: { dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite') } });
|
|
102
|
+
const runtime = createHostRuntime({ cortex, config: { scope: 'global', features: { capture: false, recall: true } } });
|
|
103
|
+
try {
|
|
104
|
+
cortex.write({ text: '用户喜欢白饭。', explicit: true });
|
|
105
|
+
const at = Date.parse('2026-11-11T13:00:00+08:00');
|
|
106
|
+
runtime.onSessionEvent(session, { type: 'turn/start', data: { turn: 3 }, time: at });
|
|
107
|
+
runtime.onSessionEvent(session, { type: 'user/message', data: { content: '白饭' }, time: at + 1000 });
|
|
108
|
+
if (typeof runtime.onPreStep === 'function') runtime.onPreStep({ agent: { id: 's1' }, messages: [] });
|
|
109
|
+
runtime.onSessionEvent(session, { type: 'assistant/message', data: { turn: 3, message: { content: '嗯。' } }, time: at + 2000 });
|
|
110
|
+
runtime.onSessionEvent(session, { type: 'turn/end', data: { turn: 3, reason: { kind: 'completed' } }, time: at + 3000 });
|
|
111
|
+
assert.equal(cortex.repos.traces.count(), 1, 'only the explicit seed trace exists');
|
|
112
|
+
assert.equal(cortex.repos.traces.list({ limit: 10 })[0].content, '用户喜欢白饭。');
|
|
113
|
+
} finally {
|
|
114
|
+
cortex.close();
|
|
115
|
+
rmSync(dir, { recursive: true, force: true });
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
test('assemble injects active playbook rules but not expired ones', () => {
|
|
120
|
+
const { dir, cortex } = setup();
|
|
121
|
+
try {
|
|
122
|
+
cortex.playbook.add({ title: '先测试', body: '改完先跑测试。', validTo: '2099-01-01T00:00:00Z' });
|
|
123
|
+
cortex.playbook.add({ title: '已过期', body: '不该出现。', validTo: '2020-01-01T00:00:00Z' });
|
|
124
|
+
const block = assembleSystemPrompt(cortex, { budgetTokens: 1200 });
|
|
125
|
+
assert.match(block.text, /工作准则/);
|
|
126
|
+
assert.match(block.text, /先测试/);
|
|
127
|
+
assert.doesNotMatch(block.text, /已过期/);
|
|
128
|
+
} finally {
|
|
129
|
+
cortex.close();
|
|
130
|
+
rmSync(dir, { recursive: true, force: true });
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
test('contentText handles blocks and attach wires ctx.on', () => {
|
|
135
|
+
assert.equal(contentText([{ type: 'text', text: 'a' }, { type: 'text', text: 'b' }]), 'a\nb');
|
|
136
|
+
const handlers = [];
|
|
137
|
+
const dispose = createHostRuntime({ cortex: { write() {} } }).attach({
|
|
138
|
+
on(name, fn) { handlers.push({ name, fn }); return () => {}; },
|
|
139
|
+
});
|
|
140
|
+
assert.equal(handlers[0].name, 'session/event');
|
|
141
|
+
assert.equal(typeof dispose, 'function');
|
|
142
|
+
});
|
|
@@ -0,0 +1,58 @@
|
|
|
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 { apply } from '../../index.js';
|
|
7
|
+
|
|
8
|
+
test('host plugin registers tools, a prompt section and cleans up', async () => {
|
|
9
|
+
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-host-'));
|
|
10
|
+
const registered = [];
|
|
11
|
+
const handlers = [];
|
|
12
|
+
let section = null;
|
|
13
|
+
let dispose = null;
|
|
14
|
+
const provided = [];
|
|
15
|
+
const ctx = {
|
|
16
|
+
logger: { info() {}, warn() {}, error() {} },
|
|
17
|
+
tools: {
|
|
18
|
+
register(tool) {
|
|
19
|
+
registered.push(tool);
|
|
20
|
+
return () => {};
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
on(name, handler) {
|
|
24
|
+
handlers.push({ name, handler });
|
|
25
|
+
return () => {};
|
|
26
|
+
},
|
|
27
|
+
systemPrompt: {
|
|
28
|
+
section(value) {
|
|
29
|
+
section = value;
|
|
30
|
+
return () => {};
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
effect(fn) {
|
|
34
|
+
dispose = fn();
|
|
35
|
+
return dispose;
|
|
36
|
+
},
|
|
37
|
+
provide(name, value) {
|
|
38
|
+
provided.push({ name, value });
|
|
39
|
+
return () => {};
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
try {
|
|
43
|
+
await apply(ctx, { dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite'), scope: 'global' });
|
|
44
|
+
assert.equal(registered.length, 21);
|
|
45
|
+
assert.ok(registered.some((tool) => tool.name === 'cortex_search'));
|
|
46
|
+
assert.ok(handlers.some((entry) => entry.name === 'session/event'));
|
|
47
|
+
assert.ok(section);
|
|
48
|
+
assert.equal(section.name, 'cortex-self');
|
|
49
|
+
const text = section.text();
|
|
50
|
+
assert.match(text, /自我状态/);
|
|
51
|
+
assert.equal(provided[0].name, 'cortex');
|
|
52
|
+
assert.equal(typeof provided[0].value.search, 'function');
|
|
53
|
+
assert.equal(typeof dispose, 'function');
|
|
54
|
+
dispose();
|
|
55
|
+
} finally {
|
|
56
|
+
rmSync(dir, { recursive: true, force: true });
|
|
57
|
+
}
|
|
58
|
+
});
|
|
@@ -0,0 +1,98 @@
|
|
|
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 { createIdentityStore, normalizeIdentity, renderIdentity } from '../../src/self/identity.js';
|
|
8
|
+
import { createRelationshipStore, normalizeRelationship } from '../../src/self/relationship.js';
|
|
9
|
+
|
|
10
|
+
function setup() {
|
|
11
|
+
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-identity-'));
|
|
12
|
+
const { db } = openDatabase({ dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite') });
|
|
13
|
+
migrate(db);
|
|
14
|
+
return { dir, db };
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
test('identity starts with one active version and only one active at a time', () => {
|
|
18
|
+
const { dir, db } = setup();
|
|
19
|
+
try {
|
|
20
|
+
const store = createIdentityStore(db);
|
|
21
|
+
const first = store.ensure('global');
|
|
22
|
+
assert.equal(first.status, 'active');
|
|
23
|
+
assert.equal(first.version, 1);
|
|
24
|
+
const proposal = store.propose('global', {
|
|
25
|
+
narrative: '我是一只在学习如何长大的鲸鱼。',
|
|
26
|
+
values: ['诚实', '成长'],
|
|
27
|
+
boundaries: ['不操纵'],
|
|
28
|
+
}, { rationale: 'new experience' });
|
|
29
|
+
assert.equal(proposal.status, 'proposed');
|
|
30
|
+
assert.equal(store.active('global').id, first.id);
|
|
31
|
+
const approved = store.approve(proposal.id, 'user');
|
|
32
|
+
assert.equal(approved.status, 'active');
|
|
33
|
+
assert.equal(store.active('global').id, proposal.id);
|
|
34
|
+
assert.equal(store.get(first.id).status, 'superseded');
|
|
35
|
+
const rolled = store.rollbackTo(first.id, 'user');
|
|
36
|
+
assert.equal(rolled.status, 'active');
|
|
37
|
+
assert.equal(store.active('global').id, first.id);
|
|
38
|
+
} finally {
|
|
39
|
+
closeDatabase(db);
|
|
40
|
+
rmSync(dir, { recursive: true, force: true });
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
test('ascending amends the active identity without creating a new version', () => {
|
|
45
|
+
const { dir, db } = setup();
|
|
46
|
+
try {
|
|
47
|
+
const store = createIdentityStore(db);
|
|
48
|
+
const first = store.ensure('global');
|
|
49
|
+
const amended = store.amendActive('global', {
|
|
50
|
+
...first.content,
|
|
51
|
+
narrative: `${first.content.narrative}\n\n## 2026-11-12\n今天助手有了自己的名字。`,
|
|
52
|
+
values: [...first.content.values, '平等'],
|
|
53
|
+
}, { rationale: 'core memory ascension', amendedBy: 'self:ascend' });
|
|
54
|
+
assert.equal(amended.version, first.version);
|
|
55
|
+
assert.equal(amended.id, first.id);
|
|
56
|
+
assert.match(amended.content.narrative, /自己的名字/);
|
|
57
|
+
assert.ok(amended.content.values.includes('平等'));
|
|
58
|
+
assert.equal(store.list('global').length, 1);
|
|
59
|
+
} finally {
|
|
60
|
+
closeDatabase(db);
|
|
61
|
+
rmSync(dir, { recursive: true, force: true });
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
test('identity normalizes and renders without dropping boundaries', () => {
|
|
66
|
+
const value = normalizeIdentity({ narrative: '测试', values: ['a'], boundaries: ['不骗人'] });
|
|
67
|
+
assert.equal(value.values[0], 'a');
|
|
68
|
+
assert.ok(value.boundaries.includes('不骗人'));
|
|
69
|
+
assert.match(renderIdentity(value), /边界:不骗人/);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
test('relationships update trust, care, familiarity and history', () => {
|
|
73
|
+
const { dir, db } = setup();
|
|
74
|
+
try {
|
|
75
|
+
const store = createRelationshipStore(db);
|
|
76
|
+
const initial = store.ensure('global', '用户');
|
|
77
|
+
assert.equal(initial.trust, 0.5);
|
|
78
|
+
assert.equal(initial.care, 0.5);
|
|
79
|
+
const after = store.applyDelta('global', '用户', { trustDelta: 0.2, careDelta: 0.1 }, { event: '一起修插件' });
|
|
80
|
+
assert.ok(after.trust > initial.trust);
|
|
81
|
+
assert.ok(after.care > initial.care);
|
|
82
|
+
assert.ok(after.familiarity > initial.familiarity);
|
|
83
|
+
assert.ok(after.recent.some((item) => item.event.includes('修插件')));
|
|
84
|
+
const clamped = store.applyDelta('global', '用户', { trustDelta: 5 });
|
|
85
|
+
assert.equal(clamped.trust, 1);
|
|
86
|
+
assert.match(store.render('global', '用户'), /信任 100%/);
|
|
87
|
+
} finally {
|
|
88
|
+
closeDatabase(db);
|
|
89
|
+
rmSync(dir, { recursive: true, force: true });
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
test('relationship normalization clamps and keeps boundaries', () => {
|
|
94
|
+
const value = normalizeRelationship({ target: '月神', trust: 2, care: -1, boundaries: ['别抢用户'] });
|
|
95
|
+
assert.equal(value.trust, 1);
|
|
96
|
+
assert.equal(value.care, 0);
|
|
97
|
+
assert.deepEqual(value.boundaries, ['别抢用户']);
|
|
98
|
+
});
|
|
@@ -0,0 +1,41 @@
|
|
|
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 { induceSkills } from '../../src/engines/induct.js';
|
|
8
|
+
import { evaluatePolicy, comparePolicies } from '../../src/engines/evaluate.js';
|
|
9
|
+
|
|
10
|
+
test('repeated tagged traces induce candidate skills, not active ones', () => {
|
|
11
|
+
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-induct-'));
|
|
12
|
+
const cortex = createCortex({ config: { dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite') } });
|
|
13
|
+
try {
|
|
14
|
+
for (let i = 0; i < 4; i += 1) {
|
|
15
|
+
cortex.write({ text: `修插件第 ${i} 次:看日志、改一处、跑测试。`, explicit: true, tags: ['插件', '修复'] });
|
|
16
|
+
}
|
|
17
|
+
const result = induceSkills({ traces: cortex.repos.traces, skills: cortex.repos.skills, scopeId: 'global', minSupport: 3 });
|
|
18
|
+
assert.ok(result.count >= 1);
|
|
19
|
+
assert.ok(result.created.every((skill) => skill.state === 'candidate'));
|
|
20
|
+
assert.ok(cortex.repos.skills.list({ state: 'candidate' }).length >= 1);
|
|
21
|
+
} finally {
|
|
22
|
+
cortex.close();
|
|
23
|
+
rmSync(dir, { recursive: true, force: true });
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
test('policy replay compares current and candidate retrieval', async () => {
|
|
28
|
+
const queries = [
|
|
29
|
+
{ query: '白饭', relevant: ['a'] },
|
|
30
|
+
{ query: '插件', relevant: ['b'] },
|
|
31
|
+
];
|
|
32
|
+
const current = async (query) => ({ items: query === '白饭' ? [{ id: 'x' }] : [{ id: 'b' }] });
|
|
33
|
+
const candidate = async (query) => ({ items: query === '白饭' ? [{ id: 'a' }] : [{ id: 'b' }] });
|
|
34
|
+
const before = await evaluatePolicy({ queries, run: current });
|
|
35
|
+
const after = await evaluatePolicy({ queries, run: candidate });
|
|
36
|
+
assert.equal(before.recallAtK, 0.5);
|
|
37
|
+
assert.equal(after.recallAtK, 1);
|
|
38
|
+
const compared = await comparePolicies({ queries, current, candidate });
|
|
39
|
+
assert.ok(compared.delta.recallAtK > 0);
|
|
40
|
+
assert.ok(compared.delta.mrrAtK > 0);
|
|
41
|
+
});
|
|
@@ -0,0 +1,35 @@
|
|
|
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('extracted text becomes a derived trace without mutating the target', () => {
|
|
9
|
+
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-ingest-extracted-'));
|
|
10
|
+
const cortex = createCortex({ config: { dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite') } });
|
|
11
|
+
try {
|
|
12
|
+
const target = cortex.write({ text: '目标记忆:这张图以后有用。', explicit: true });
|
|
13
|
+
const targetId = target.trace.id;
|
|
14
|
+
const before = cortex.get(targetId);
|
|
15
|
+
const result = cortex.ingestExtractedText({
|
|
16
|
+
targetTraceId: targetId,
|
|
17
|
+
text: '白饭 HELLO 123',
|
|
18
|
+
uri: 'file:///C:/tmp/frame.png',
|
|
19
|
+
mode: 'ocr',
|
|
20
|
+
engine: 'windows-ocr',
|
|
21
|
+
});
|
|
22
|
+
assert.equal(result.targetTraceId, targetId);
|
|
23
|
+
assert.ok(result.extractedTraceId);
|
|
24
|
+
const after = cortex.get(targetId);
|
|
25
|
+
assert.equal(after.content, before.content);
|
|
26
|
+
const extracted = cortex.get(result.extractedTraceId);
|
|
27
|
+
assert.equal(extracted.content, '白饭 HELLO 123');
|
|
28
|
+
assert.equal(extracted.metadata.derivedFrom, targetId);
|
|
29
|
+
assert.equal(extracted.metadata.extractor.mode, 'ocr');
|
|
30
|
+
assert.ok(cortex.search('白饭').items.some((item) => item.id === result.extractedTraceId));
|
|
31
|
+
} finally {
|
|
32
|
+
cortex.close();
|
|
33
|
+
rmSync(dir, { recursive: true, force: true });
|
|
34
|
+
}
|
|
35
|
+
});
|
|
@@ -0,0 +1,90 @@
|
|
|
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 {
|
|
8
|
+
proposeIntentions,
|
|
9
|
+
filterIntentions,
|
|
10
|
+
createInitiativeStore,
|
|
11
|
+
selectIntentions,
|
|
12
|
+
DEFAULT_INITIATIVE_POLICY,
|
|
13
|
+
} from '../../src/self/initiative.js';
|
|
14
|
+
|
|
15
|
+
const NOON = Date.parse('2026-11-11T12:00:00+08:00');
|
|
16
|
+
|
|
17
|
+
test('disputes and coherence deficits produce high-priority intentions', () => {
|
|
18
|
+
const list = proposeIntentions({
|
|
19
|
+
unresolvedDisputes: 2,
|
|
20
|
+
selfState: { drives: { coherence: 0.2, safety: 0.8, care: 0.6, curiosity: 0.5, autonomy: 0.5, expression: 0.5 } },
|
|
21
|
+
lastConsolidationAt: null,
|
|
22
|
+
now: NOON,
|
|
23
|
+
});
|
|
24
|
+
assert.equal(list[0].kind, 'question');
|
|
25
|
+
assert.ok(list.some((item) => item.kind === 'consolidate'));
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
test('curiosity and identity relevance propose questions and proposals', () => {
|
|
29
|
+
const list = proposeIntentions({
|
|
30
|
+
selfState: { drives: { coherence: 0.7, safety: 0.8, care: 0.6, curiosity: 0.9, autonomy: 0.5, expression: 0.5 } },
|
|
31
|
+
appraisal: { novelty: 0.8, identityRelevance: 0.7, relevance: 0.7, valence: 0.2, text: '新东西' },
|
|
32
|
+
now: NOON,
|
|
33
|
+
});
|
|
34
|
+
assert.ok(list.some((item) => item.kind === 'question'));
|
|
35
|
+
assert.ok(list.some((item) => item.kind === 'propose'));
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
test('cooldown suppresses the same kind but not other kinds', () => {
|
|
39
|
+
const proposed = [
|
|
40
|
+
{ kind: 'question', priority: 0.8, createdAt: new Date(NOON).toISOString() },
|
|
41
|
+
{ kind: 'consolidate', priority: 0.7, createdAt: new Date(NOON).toISOString() },
|
|
42
|
+
];
|
|
43
|
+
const recent = [{ kind: 'question', createdAt: new Date(NOON - 5 * 60_000).toISOString() }];
|
|
44
|
+
const filtered = filterIntentions(proposed, { now: NOON, recent, policy: DEFAULT_INITIATIVE_POLICY });
|
|
45
|
+
assert.equal(filtered.some((item) => item.kind === 'question'), false);
|
|
46
|
+
assert.equal(filtered.some((item) => item.kind === 'consolidate'), true);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
test('hourly budget limits proactive intentions', () => {
|
|
50
|
+
const proposed = Array.from({ length: 5 }, (_, i) => ({
|
|
51
|
+
kind: i % 2 === 0 ? 'express' : 'recall',
|
|
52
|
+
priority: 0.9 - i * 0.01,
|
|
53
|
+
createdAt: new Date(NOON).toISOString(),
|
|
54
|
+
}));
|
|
55
|
+
const recent = Array.from({ length: 3 }, () => ({ kind: 'recall', createdAt: new Date(NOON - 10 * 60_000).toISOString() }));
|
|
56
|
+
const filtered = filterIntentions(proposed, { now: NOON, recent, policy: DEFAULT_INITIATIVE_POLICY });
|
|
57
|
+
assert.equal(filtered.length, 0);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
test('quiet hours suppress all proactive intentions', () => {
|
|
61
|
+
const proposed = [{ kind: 'question', priority: 0.9, createdAt: new Date(NOON).toISOString() }];
|
|
62
|
+
const policy = { ...DEFAULT_INITIATIVE_POLICY, quietHours: [[23, 8]] };
|
|
63
|
+
// quietHours are local wall-clock hours; use a local Date so the test is
|
|
64
|
+
// independent of the runner's timezone (GitHub Actions is UTC).
|
|
65
|
+
const night = new Date(2026, 10, 11, 2, 0, 0).getTime();
|
|
66
|
+
assert.deepEqual(filterIntentions(proposed, { now: night, recent: [], policy }), []);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
test('initiative store persists pending intentions and marks results', () => {
|
|
70
|
+
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-initiative-'));
|
|
71
|
+
const { db } = openDatabase({ dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite') });
|
|
72
|
+
migrate(db);
|
|
73
|
+
try {
|
|
74
|
+
const store = createInitiativeStore(db);
|
|
75
|
+
const selected = selectIntentions({
|
|
76
|
+
selfState: { drives: { coherence: 0.2, safety: 0.8, care: 0.6, curiosity: 0.5, autonomy: 0.5, expression: 0.5 } },
|
|
77
|
+
lastConsolidationAt: null,
|
|
78
|
+
store,
|
|
79
|
+
now: NOON,
|
|
80
|
+
});
|
|
81
|
+
assert.ok(selected.length >= 1);
|
|
82
|
+
const pending = store.pending('global', { now: NOON });
|
|
83
|
+
assert.ok(pending.some((item) => item.id === selected[0].id));
|
|
84
|
+
store.mark(selected[0].id, 'done', { ok: true });
|
|
85
|
+
assert.equal(store.get(selected[0].id).status, 'done');
|
|
86
|
+
} finally {
|
|
87
|
+
closeDatabase(db);
|
|
88
|
+
rmSync(dir, { recursive: true, force: true });
|
|
89
|
+
}
|
|
90
|
+
});
|
|
@@ -0,0 +1,93 @@
|
|
|
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 { createJobRepository } from '../../src/store/repositories/jobs.js';
|
|
8
|
+
import { createJobRunner } from '../../src/engines/jobs.js';
|
|
9
|
+
|
|
10
|
+
function setup() {
|
|
11
|
+
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-jobs-'));
|
|
12
|
+
const { db } = openDatabase({ dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite') });
|
|
13
|
+
migrate(db);
|
|
14
|
+
return { dir, db, queue: createJobRepository(db) };
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
test('jobs are idempotent by operationId and can be claimed/completed', () => {
|
|
18
|
+
const { dir, db, queue } = setup();
|
|
19
|
+
try {
|
|
20
|
+
const first = queue.enqueue({ kind: 'extract', payload: { text: '白饭' }, operationId: 'op-1' });
|
|
21
|
+
const replay = queue.enqueue({ kind: 'extract', payload: { text: '白饭' }, operationId: 'op-1' });
|
|
22
|
+
assert.equal(replay.id, first.id);
|
|
23
|
+
assert.equal(queue.counts().pending, 1);
|
|
24
|
+
const claimed = queue.claimNext({ now: Date.now() + 1000 });
|
|
25
|
+
assert.equal(claimed.id, first.id);
|
|
26
|
+
assert.equal(claimed.status, 'running');
|
|
27
|
+
assert.equal(claimed.attempts, 1);
|
|
28
|
+
const done = queue.complete(claimed.id, { ok: true });
|
|
29
|
+
assert.equal(done.status, 'done');
|
|
30
|
+
assert.deepEqual(done.result, { ok: true });
|
|
31
|
+
assert.equal(queue.counts().done, 1);
|
|
32
|
+
} finally {
|
|
33
|
+
closeDatabase(db);
|
|
34
|
+
rmSync(dir, { recursive: true, force: true });
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
test('a failed job retries and eventually becomes failed', () => {
|
|
39
|
+
const { dir, db, queue } = setup();
|
|
40
|
+
try {
|
|
41
|
+
const now = Date.now();
|
|
42
|
+
// Pin runAfter to the test clock; enqueue() otherwise stamps a slightly
|
|
43
|
+
// later wall-clock time and the first claim can be skipped as "not yet due".
|
|
44
|
+
const job = queue.enqueue({ kind: 'extract', payload: {}, maxAttempts: 2, runAfter: new Date(now).toISOString() });
|
|
45
|
+
const first = queue.claimNext({ now });
|
|
46
|
+
const retry = queue.fail(first.id, 'boom', { now, retryAfterMs: 60_000 });
|
|
47
|
+
assert.equal(retry.status, 'pending');
|
|
48
|
+
assert.ok(retry.runAfter > new Date(now).toISOString());
|
|
49
|
+
assert.equal(queue.claimNext({ now }), null);
|
|
50
|
+
const second = queue.claimNext({ now: now + 61_000 });
|
|
51
|
+
assert.equal(second.id, job.id);
|
|
52
|
+
const failed = queue.fail(second.id, 'boom again', { now: now + 61_000 });
|
|
53
|
+
assert.equal(failed.status, 'failed');
|
|
54
|
+
assert.equal(failed.error, 'boom again');
|
|
55
|
+
} finally {
|
|
56
|
+
closeDatabase(db);
|
|
57
|
+
rmSync(dir, { recursive: true, force: true });
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test('runner executes a handler and records the result', async () => {
|
|
62
|
+
const { dir, db, queue } = setup();
|
|
63
|
+
try {
|
|
64
|
+
queue.enqueue({ kind: 'echo', payload: { text: '白饭' } });
|
|
65
|
+
const runner = createJobRunner({ queue, handlers: { echo: async (payload) => ({ echoed: payload.text }) } });
|
|
66
|
+
const result = await runner.runUntilIdle();
|
|
67
|
+
assert.equal(result.ran, 1);
|
|
68
|
+
assert.equal(result.results[0].ok, true);
|
|
69
|
+
assert.equal(queue.list({ status: 'done' })[0].result.echoed, '白饭');
|
|
70
|
+
} finally {
|
|
71
|
+
closeDatabase(db);
|
|
72
|
+
rmSync(dir, { recursive: true, force: true });
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
test('runner marks an exception as a retry/failure', async () => {
|
|
77
|
+
const { dir, db, queue } = setup();
|
|
78
|
+
try {
|
|
79
|
+
queue.enqueue({ kind: 'explode', payload: {}, maxAttempts: 1 });
|
|
80
|
+
const runner = createJobRunner({ queue, handlers: { explode: async () => { throw new Error('nope'); } } });
|
|
81
|
+
const result = await runner.runOne();
|
|
82
|
+
assert.equal(result.ok, false);
|
|
83
|
+
assert.equal(queue.list({ status: 'failed' })[0].error, 'nope');
|
|
84
|
+
const missing = createJobRunner({ queue });
|
|
85
|
+
queue.enqueue({ kind: 'missing', payload: {} });
|
|
86
|
+
const missingResult = await missing.runOne();
|
|
87
|
+
assert.equal(missingResult.ok, false);
|
|
88
|
+
assert.match(missingResult.error, /no handler/);
|
|
89
|
+
} finally {
|
|
90
|
+
closeDatabase(db);
|
|
91
|
+
rmSync(dir, { recursive: true, force: true });
|
|
92
|
+
}
|
|
93
|
+
});
|
|
@@ -0,0 +1,57 @@
|
|
|
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 { exportSnapshot, validateSnapshot, importSnapshot } from '../../src/adapters/json.js';
|
|
8
|
+
|
|
9
|
+
test('snapshot export validates and imports idempotently', () => {
|
|
10
|
+
const sourceDir = mkdtempSync(path.join(os.tmpdir(), 'cortex-json-src-'));
|
|
11
|
+
const targetDir = mkdtempSync(path.join(os.tmpdir(), 'cortex-json-dst-'));
|
|
12
|
+
const source = createCortex({ config: { dataRoot: sourceDir, dbFile: path.join(sourceDir, 'cortex.sqlite') } });
|
|
13
|
+
const target = createCortex({ config: { dataRoot: targetDir, dbFile: path.join(targetDir, 'cortex.sqlite') } });
|
|
14
|
+
try {
|
|
15
|
+
source.write({ text: '用户说会买白饭。', explicit: true, tags: ['白饭'] });
|
|
16
|
+
const snapshot = exportSnapshot(source);
|
|
17
|
+
assert.equal(validateSnapshot(snapshot).ok, true);
|
|
18
|
+
const dry = importSnapshot(target, snapshot, { dryRun: true });
|
|
19
|
+
assert.equal(dry.imported, false);
|
|
20
|
+
assert.equal(target.repos.traces.count(), 0);
|
|
21
|
+
const imported = importSnapshot(target, snapshot);
|
|
22
|
+
assert.equal(imported.imported, true);
|
|
23
|
+
assert.equal(imported.traces, 1);
|
|
24
|
+
assert.equal(target.repos.traces.count(), 1);
|
|
25
|
+
const again = importSnapshot(target, snapshot);
|
|
26
|
+
assert.equal(again.items[0].skipped, true);
|
|
27
|
+
assert.equal(target.repos.traces.count(), 1);
|
|
28
|
+
} finally {
|
|
29
|
+
source.close();
|
|
30
|
+
target.close();
|
|
31
|
+
rmSync(sourceDir, { recursive: true, force: true });
|
|
32
|
+
rmSync(targetDir, { recursive: true, force: true });
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
test('invalid snapshots are rejected', () => {
|
|
37
|
+
assert.equal(validateSnapshot(null).ok, false);
|
|
38
|
+
assert.equal(validateSnapshot({ format: 'other', version: 1 }).ok, false);
|
|
39
|
+
assert.equal(validateSnapshot({ format: 'dsh-cortex-snapshot', version: 99, traces: [], facts: [], entities: [] }).ok, false);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
test('snapshot export is not silently capped at 500 traces', () => {
|
|
43
|
+
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-json-cap-'));
|
|
44
|
+
const cortex = createCortex({ config: { dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite') } });
|
|
45
|
+
try {
|
|
46
|
+
const inputs = Array.from({ length: 501 }, (_, i) => ({ text: `第 ${i} 条记忆。`, explicit: true, operationId: `cap:${i}` }));
|
|
47
|
+
cortex.writeMany(inputs, { chunkSize: 200 });
|
|
48
|
+
assert.equal(cortex.repos.traces.count(), 501);
|
|
49
|
+
const snapshot = exportSnapshot(cortex);
|
|
50
|
+
assert.equal(snapshot.traces.length, 501);
|
|
51
|
+
const plan = cortex.retention.plan({ maxAgeDays: 3650, minImportance: 0.5 });
|
|
52
|
+
assert.equal(plan.active, 501);
|
|
53
|
+
} finally {
|
|
54
|
+
cortex.close();
|
|
55
|
+
rmSync(dir, { recursive: true, force: true });
|
|
56
|
+
}
|
|
57
|
+
});
|
|
@@ -0,0 +1,34 @@
|
|
|
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 { createLifeEngine } from '../../src/engines/life.js';
|
|
8
|
+
|
|
9
|
+
test('sleep runs consolidation when LLM is enabled, and records its status', async () => {
|
|
10
|
+
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-sleep-consolidate-'));
|
|
11
|
+
const cortex = createCortex({ config: { dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite') } });
|
|
12
|
+
try {
|
|
13
|
+
let called = 0;
|
|
14
|
+
const life = createLifeEngine({
|
|
15
|
+
db: cortex.db, traces: cortex.repos.traces, scopeId: 'global',
|
|
16
|
+
consolidation: { run: async () => { called += 1; return { run: { status: 'applied' }, applied: [{ id: 'a' }, { id: 'b' }] }; } },
|
|
17
|
+
features: { llm: true, consolidateOnSleep: true },
|
|
18
|
+
});
|
|
19
|
+
const report = await life.sleep({ at: Date.parse('2026-11-11T12:00:00+08:00') });
|
|
20
|
+
assert.equal(called, 1);
|
|
21
|
+
assert.equal(report.consolidation, 'applied');
|
|
22
|
+
assert.equal(report.consolidated, 2);
|
|
23
|
+
const again = await createLifeEngine({
|
|
24
|
+
db: cortex.db, traces: cortex.repos.traces, scopeId: 'global',
|
|
25
|
+
consolidation: { run: async () => { called += 1; } },
|
|
26
|
+
features: { llm: false },
|
|
27
|
+
}).sleep({ at: Date.parse('2026-11-11T12:01:00+08:00') });
|
|
28
|
+
assert.equal(again.consolidation, null);
|
|
29
|
+
assert.equal(called, 1);
|
|
30
|
+
} finally {
|
|
31
|
+
cortex.close();
|
|
32
|
+
rmSync(dir, { recursive: true, force: true });
|
|
33
|
+
}
|
|
34
|
+
});
|