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,555 @@
|
|
|
1
|
+
import test from 'node:test';
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
3
|
+
import { mkdtempSync, rmSync, writeFileSync } from 'node:fs';
|
|
4
|
+
import crypto from 'node:crypto';
|
|
5
|
+
import { pathToFileURL } from 'node:url';
|
|
6
|
+
import os from 'node:os';
|
|
7
|
+
import path from 'node:path';
|
|
8
|
+
import { Readable } from 'node:stream';
|
|
9
|
+
import { createCortex } from '../../src/service/cortex.js';
|
|
10
|
+
import { createApiHandler, API_BASE } from '../../src/api/routes.js';
|
|
11
|
+
|
|
12
|
+
function setup() {
|
|
13
|
+
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-api-'));
|
|
14
|
+
const cortex = createCortex({ config: { dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite') } });
|
|
15
|
+
const handler = createApiHandler({ cortex });
|
|
16
|
+
return { dir, cortex, handler };
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function request({ method = 'GET', route = '/ping', body = null, headers = {} } = {}) {
|
|
20
|
+
const stream = Readable.from(body == null ? [] : [Buffer.from(JSON.stringify(body), 'utf8')]);
|
|
21
|
+
stream.method = method;
|
|
22
|
+
stream.url = `${API_BASE}${route}`;
|
|
23
|
+
stream.headers = {
|
|
24
|
+
host: '127.0.0.1:37990',
|
|
25
|
+
origin: 'http://127.0.0.1:37990',
|
|
26
|
+
'sec-fetch-site': 'same-origin',
|
|
27
|
+
...(body == null ? {} : { 'content-type': 'application/json', 'x-cortex': '1' }),
|
|
28
|
+
...headers,
|
|
29
|
+
};
|
|
30
|
+
stream.socket = { remoteAddress: '127.0.0.1' };
|
|
31
|
+
return stream;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function response() {
|
|
35
|
+
return {
|
|
36
|
+
statusCode: 0,
|
|
37
|
+
body: '',
|
|
38
|
+
writeHead(code) { this.statusCode = code; },
|
|
39
|
+
end(chunk) { this.body += chunk ? String(chunk) : ''; },
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
async function invoke(handler, options) {
|
|
44
|
+
const res = response();
|
|
45
|
+
await handler(request(options), res);
|
|
46
|
+
return { status: res.statusCode, json: JSON.parse(res.body || '{}') };
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
test('api serves health/self/search and writes through POST', async () => {
|
|
50
|
+
const { dir, cortex, handler } = setup();
|
|
51
|
+
try {
|
|
52
|
+
const ping = await invoke(handler);
|
|
53
|
+
assert.equal(ping.status, 200);
|
|
54
|
+
assert.equal(ping.json.ok, true);
|
|
55
|
+
const health = await invoke(handler, { route: '/health' });
|
|
56
|
+
assert.equal(health.status, 200);
|
|
57
|
+
assert.ok(health.json.data.score > 0);
|
|
58
|
+
const write = await invoke(handler, { method: 'POST', route: '/write', body: { text: '用户说会买白饭。', explicit: true } });
|
|
59
|
+
assert.equal(write.status, 200);
|
|
60
|
+
assert.equal(write.json.data.trace.content, '用户说会买白饭。');
|
|
61
|
+
const search = await invoke(handler, { route: '/search?q=白饭' });
|
|
62
|
+
assert.equal(search.status, 200);
|
|
63
|
+
assert.ok(search.json.data.items.length >= 1);
|
|
64
|
+
|
|
65
|
+
const withFacts = await invoke(handler, {
|
|
66
|
+
method: 'POST',
|
|
67
|
+
route: '/write',
|
|
68
|
+
body: {
|
|
69
|
+
text: 'API 事实写入:测试对象喜欢测试值。',
|
|
70
|
+
explicit: true,
|
|
71
|
+
facts: [{ subject: '测试对象', predicate: '喜欢', object: '测试值', confidence: 0.8 }],
|
|
72
|
+
},
|
|
73
|
+
});
|
|
74
|
+
assert.equal(withFacts.status, 200);
|
|
75
|
+
assert.equal(withFacts.json.data.facts.length, 1);
|
|
76
|
+
assert.equal(withFacts.json.data.edgeCandidates.length, 1);
|
|
77
|
+
const asOf = await invoke(handler, { route: '/as-of?subject=测试对象&predicate=喜欢&mode=historical' });
|
|
78
|
+
assert.equal(asOf.status, 200);
|
|
79
|
+
assert.equal(asOf.json.data.facts.length, 1);
|
|
80
|
+
assert.equal(asOf.json.data.facts[0].object, '测试值');
|
|
81
|
+
|
|
82
|
+
const withAttachment = await invoke(handler, {
|
|
83
|
+
method: 'POST',
|
|
84
|
+
route: '/write',
|
|
85
|
+
body: {
|
|
86
|
+
text: '带附件元数据的记忆。',
|
|
87
|
+
explicit: true,
|
|
88
|
+
attachments: [{ kind: 'image', uri: 'https://example.test/a.png', mime: 'image/png', bytes: 1234 }],
|
|
89
|
+
},
|
|
90
|
+
});
|
|
91
|
+
assert.equal(withAttachment.status, 200);
|
|
92
|
+
assert.equal(withAttachment.json.data.trace.metadata.attachments.length, 1);
|
|
93
|
+
assert.equal(withAttachment.json.data.trace.metadata.attachments[0].kind, 'image');
|
|
94
|
+
const attachmentSearch = await invoke(handler, { route: '/search?q=附件元数据' });
|
|
95
|
+
assert.equal(attachmentSearch.json.data.items[0].attachments, 1);
|
|
96
|
+
const self = await invoke(handler, { route: '/self' });
|
|
97
|
+
assert.equal(self.status, 200);
|
|
98
|
+
assert.ok(self.json.data.self);
|
|
99
|
+
} finally {
|
|
100
|
+
cortex.close();
|
|
101
|
+
rmSync(dir, { recursive: true, force: true });
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
test('api rejects cross-origin and missing POST header', async () => {
|
|
106
|
+
const { dir, cortex, handler } = setup();
|
|
107
|
+
try {
|
|
108
|
+
const cross = await invoke(handler, { headers: { origin: 'http://evil.example', 'sec-fetch-site': 'cross-site' } });
|
|
109
|
+
assert.equal(cross.status, 403);
|
|
110
|
+
const noHeader = await invoke(handler, { method: 'POST', route: '/write', body: { text: 'x' }, headers: { 'x-cortex': '' } });
|
|
111
|
+
assert.equal(noHeader.status, 403);
|
|
112
|
+
} finally {
|
|
113
|
+
cortex.close();
|
|
114
|
+
rmSync(dir, { recursive: true, force: true });
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
test('api timeline, trace and evolution endpoints work', async () => {
|
|
119
|
+
const { dir, cortex, handler } = setup();
|
|
120
|
+
try {
|
|
121
|
+
const write = await invoke(handler, { method: 'POST', route: '/write', body: { text: '第一次测试。', explicit: true } });
|
|
122
|
+
const id = write.json.data.trace.id;
|
|
123
|
+
const trace = await invoke(handler, { route: `/trace?id=${id}` });
|
|
124
|
+
assert.equal(trace.status, 200);
|
|
125
|
+
assert.equal(trace.json.data.id, id);
|
|
126
|
+
const wrongScope = await invoke(handler, { route: `/trace?id=${id}&scope=s2` });
|
|
127
|
+
assert.equal(wrongScope.status, 404);
|
|
128
|
+
const timeline = await invoke(handler, { route: `/timeline?id=${id}` });
|
|
129
|
+
assert.equal(timeline.status, 200);
|
|
130
|
+
assert.equal(timeline.json.data.anchor.id, id);
|
|
131
|
+
const status = await invoke(handler, { route: '/evolution' });
|
|
132
|
+
assert.equal(status.status, 200);
|
|
133
|
+
assert.ok(status.json.data.active);
|
|
134
|
+
} finally {
|
|
135
|
+
cortex.close();
|
|
136
|
+
rmSync(dir, { recursive: true, force: true });
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
test('api rejects invalid write/search bodies with 400', async () => {
|
|
141
|
+
const { dir, cortex, handler } = setup();
|
|
142
|
+
try {
|
|
143
|
+
const badWrite = await invoke(handler, { method: 'POST', route: '/write', body: { text: '' } });
|
|
144
|
+
assert.equal(badWrite.status, 400);
|
|
145
|
+
assert.match(badWrite.json.error, /text/);
|
|
146
|
+
const badSearch = await invoke(handler, { method: 'POST', route: '/search', body: { query: 'x', limit: 99 } });
|
|
147
|
+
assert.equal(badSearch.status, 400);
|
|
148
|
+
assert.match(badSearch.json.error, /limit/);
|
|
149
|
+
const badAttachment = await invoke(handler, {
|
|
150
|
+
method: 'POST',
|
|
151
|
+
route: '/write',
|
|
152
|
+
body: { text: '坏附件', explicit: true, attachments: [{ kind: 'image', uri: '' }] },
|
|
153
|
+
});
|
|
154
|
+
assert.equal(badAttachment.status, 400);
|
|
155
|
+
assert.match(badAttachment.json.error, /attachments/);
|
|
156
|
+
} finally {
|
|
157
|
+
cortex.close();
|
|
158
|
+
rmSync(dir, { recursive: true, force: true });
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
test('api read-only scopes reject writes but allow reads', async () => {
|
|
163
|
+
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-api-readonly-'));
|
|
164
|
+
const seed = createCortex({ config: { dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite') } });
|
|
165
|
+
const sharedWorking = seed.working.add({ scopeId: 'shared', text: '共享工作项', kind: 'focus' });
|
|
166
|
+
const sharedGoal = seed.goals.add({ scopeId: 'shared', title: '共享目标' });
|
|
167
|
+
const sharedRule = seed.playbook.add({ scopeId: 'shared', title: '共享准则', body: '只在共享 scope 生效的准则内容。' });
|
|
168
|
+
const sharedTrace = seed.write({ scopeId: 'shared', text: '共享 scope 的记忆不应被外部删除。', explicit: true });
|
|
169
|
+
seed.close();
|
|
170
|
+
const cortex = createCortex({
|
|
171
|
+
config: {
|
|
172
|
+
dataRoot: dir,
|
|
173
|
+
dbFile: path.join(dir, 'cortex.sqlite'),
|
|
174
|
+
readOnlyScopes: ['shared'],
|
|
175
|
+
scopeWriteGrants: [
|
|
176
|
+
'shared:agent-a',
|
|
177
|
+
{ grant: 'shared:agent-old', until: '2000-01-01T00:00:00.000Z' },
|
|
178
|
+
{ grant: 'shared:agent-new', until: '2999-01-01T00:00:00.000Z' },
|
|
179
|
+
],
|
|
180
|
+
},
|
|
181
|
+
});
|
|
182
|
+
const handler = createApiHandler({ cortex });
|
|
183
|
+
try {
|
|
184
|
+
const denied = await invoke(handler, {
|
|
185
|
+
method: 'POST',
|
|
186
|
+
route: '/write',
|
|
187
|
+
body: { text: '共享 scope 不应写入。' },
|
|
188
|
+
headers: { 'x-cortex-scope': 'shared' },
|
|
189
|
+
});
|
|
190
|
+
assert.equal(denied.status, 403);
|
|
191
|
+
assert.match(denied.json.error, /scope-read-only/);
|
|
192
|
+
const workingDenied = await invoke(handler, {
|
|
193
|
+
method: 'POST',
|
|
194
|
+
route: '/working/add',
|
|
195
|
+
body: { text: '只读工作项' },
|
|
196
|
+
headers: { 'x-cortex-scope': 'shared' },
|
|
197
|
+
});
|
|
198
|
+
assert.equal(workingDenied.status, 403);
|
|
199
|
+
const goalDenied = await invoke(handler, {
|
|
200
|
+
method: 'POST',
|
|
201
|
+
route: '/goals/add',
|
|
202
|
+
body: { title: '只读目标' },
|
|
203
|
+
headers: { 'x-cortex-scope': 'shared' },
|
|
204
|
+
});
|
|
205
|
+
assert.equal(goalDenied.status, 403);
|
|
206
|
+
const candidateDenied = await invoke(handler, {
|
|
207
|
+
method: 'POST',
|
|
208
|
+
route: '/graph/candidates/action',
|
|
209
|
+
body: { action: 'add', fromId: 'a', toId: 'b' },
|
|
210
|
+
headers: { 'x-cortex-scope': 'shared' },
|
|
211
|
+
});
|
|
212
|
+
assert.equal(candidateDenied.status, 403);
|
|
213
|
+
const workingUpdate = await invoke(handler, { method: 'POST', route: '/working/update', body: { id: sharedWorking.id, text: '改名' } });
|
|
214
|
+
assert.equal(workingUpdate.status, 403);
|
|
215
|
+
const goalComplete = await invoke(handler, { method: 'POST', route: '/goals/complete', body: { id: sharedGoal.id } });
|
|
216
|
+
assert.equal(goalComplete.status, 403);
|
|
217
|
+
const ruleRetire = await invoke(handler, { method: 'POST', route: '/playbook', body: { action: 'retire', id: sharedRule.id } });
|
|
218
|
+
assert.equal(ruleRetire.status, 403);
|
|
219
|
+
const emotionDenied = await invoke(handler, { method: 'POST', route: '/emotion/record', body: { text: '用户有点担心' }, headers: { 'x-cortex-scope': 'shared' } });
|
|
220
|
+
assert.equal(emotionDenied.status, 403);
|
|
221
|
+
const lifeDenied = await invoke(handler, { method: 'POST', route: '/life/diary', body: { text: '共享日记' }, headers: { 'x-cortex-scope': 'shared' } });
|
|
222
|
+
assert.equal(lifeDenied.status, 403);
|
|
223
|
+
const suggestDenied = await invoke(handler, { method: 'POST', route: '/playbook', body: { action: 'suggest' }, headers: { 'x-cortex-scope': 'shared' } });
|
|
224
|
+
assert.equal(suggestDenied.status, 403);
|
|
225
|
+
const compressDenied = await invoke(handler, { method: 'POST', route: '/compress', body: { limit: 1 }, headers: { 'x-cortex-scope': 'shared' } });
|
|
226
|
+
assert.equal(compressDenied.status, 403);
|
|
227
|
+
const forgetDenied = await invoke(handler, { method: 'POST', route: '/forget', body: { id: sharedTrace.trace.id, mode: 'archive' } });
|
|
228
|
+
assert.equal(forgetDenied.status, 403);
|
|
229
|
+
const guardSyncDenied = await invoke(handler, { method: 'POST', route: '/guard/sync', body: { dir: path.join(dir, 'vault') }, headers: { 'x-cortex-scope': 'shared' } });
|
|
230
|
+
assert.equal(guardSyncDenied.status, 403);
|
|
231
|
+
const guardSyncDry = await invoke(handler, { method: 'POST', route: '/guard/sync', body: { dir: path.join(dir, 'vault'), dryRun: true }, headers: { 'x-cortex-scope': 'shared' } });
|
|
232
|
+
assert.equal(guardSyncDry.status, 200);
|
|
233
|
+
const allowed = await invoke(handler, { method: 'POST', route: '/write', body: { text: '本 scope 可以写入。' } });
|
|
234
|
+
assert.equal(allowed.status, 200);
|
|
235
|
+
const granted = await invoke(handler, {
|
|
236
|
+
method: 'POST',
|
|
237
|
+
route: '/write',
|
|
238
|
+
body: { text: 'agent-a 有显式写授权。' },
|
|
239
|
+
headers: { 'x-cortex-scope': 'shared', 'x-cortex-agent': 'agent-a' },
|
|
240
|
+
});
|
|
241
|
+
assert.equal(granted.status, 200);
|
|
242
|
+
const revoked = await invoke(handler, { method: 'POST', route: '/security/grants', body: { action: 'revoke', grant: 'shared:agent-a' } });
|
|
243
|
+
assert.equal(revoked.status, 200);
|
|
244
|
+
const afterRevoke = await invoke(handler, {
|
|
245
|
+
method: 'POST',
|
|
246
|
+
route: '/write',
|
|
247
|
+
body: { text: '撤销后不应写入。' },
|
|
248
|
+
headers: { 'x-cortex-scope': 'shared', 'x-cortex-agent': 'agent-a' },
|
|
249
|
+
});
|
|
250
|
+
assert.equal(afterRevoke.status, 403);
|
|
251
|
+
const reAdded = await invoke(handler, { method: 'POST', route: '/security/grants', body: { action: 'add', grant: 'shared:agent-a' } });
|
|
252
|
+
assert.equal(reAdded.status, 200);
|
|
253
|
+
const afterAdd = await invoke(handler, {
|
|
254
|
+
method: 'POST',
|
|
255
|
+
route: '/write',
|
|
256
|
+
body: { text: '恢复授权后可以写入。' },
|
|
257
|
+
headers: { 'x-cortex-scope': 'shared', 'x-cortex-agent': 'agent-a' },
|
|
258
|
+
});
|
|
259
|
+
assert.equal(afterAdd.status, 200);
|
|
260
|
+
const deniedAgent = await invoke(handler, {
|
|
261
|
+
method: 'POST',
|
|
262
|
+
route: '/write',
|
|
263
|
+
body: { text: 'agent-b 没有授权。' },
|
|
264
|
+
headers: { 'x-cortex-scope': 'shared', 'x-cortex-agent': 'agent-b' },
|
|
265
|
+
});
|
|
266
|
+
assert.equal(deniedAgent.status, 403);
|
|
267
|
+
const expiredAgent = await invoke(handler, {
|
|
268
|
+
method: 'POST',
|
|
269
|
+
route: '/write',
|
|
270
|
+
body: { text: '过期授权不应写入。' },
|
|
271
|
+
headers: { 'x-cortex-scope': 'shared', 'x-cortex-agent': 'agent-old' },
|
|
272
|
+
});
|
|
273
|
+
assert.equal(expiredAgent.status, 403);
|
|
274
|
+
const futureAgent = await invoke(handler, {
|
|
275
|
+
method: 'POST',
|
|
276
|
+
route: '/write',
|
|
277
|
+
body: { text: '未来授权可以写入。' },
|
|
278
|
+
headers: { 'x-cortex-scope': 'shared', 'x-cortex-agent': 'agent-new' },
|
|
279
|
+
});
|
|
280
|
+
assert.equal(futureAgent.status, 200);
|
|
281
|
+
const read = await invoke(handler, { route: '/search?q=共享', headers: { 'x-cortex-scope': 'shared' } });
|
|
282
|
+
assert.equal(read.status, 200);
|
|
283
|
+
} finally {
|
|
284
|
+
cortex.close();
|
|
285
|
+
rmSync(dir, { recursive: true, force: true });
|
|
286
|
+
}
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
test('extractor API writes a derived trace and fails closed when unavailable', async () => {
|
|
290
|
+
const { dir, cortex } = setup();
|
|
291
|
+
const handler = createApiHandler({
|
|
292
|
+
cortex,
|
|
293
|
+
getExtractor: () => ({ extract: async ({ uri, mode }) => ({ text: '提取文本', uri, mode, engine: 'fake' }) }),
|
|
294
|
+
});
|
|
295
|
+
try {
|
|
296
|
+
const target = await invoke(handler, { method: 'POST', route: '/write', body: { text: '目标记忆。', explicit: true } });
|
|
297
|
+
const targetId = target.json.data.trace.id;
|
|
298
|
+
const result = await invoke(handler, {
|
|
299
|
+
method: 'POST',
|
|
300
|
+
route: '/attachments/extract',
|
|
301
|
+
body: { traceId: targetId, uri: 'file:///C:/tmp/a.png', mode: 'ocr' },
|
|
302
|
+
});
|
|
303
|
+
assert.equal(result.status, 200);
|
|
304
|
+
assert.equal(result.json.data.targetTraceId, targetId);
|
|
305
|
+
assert.ok(result.json.data.extractedTraceId);
|
|
306
|
+
const derived = cortex.get(result.json.data.extractedTraceId);
|
|
307
|
+
assert.equal(derived.content, '提取文本');
|
|
308
|
+
assert.equal(derived.metadata.derivedFrom, targetId);
|
|
309
|
+
|
|
310
|
+
const unavailable = createApiHandler({ cortex });
|
|
311
|
+
const denied = await invoke(unavailable, {
|
|
312
|
+
method: 'POST',
|
|
313
|
+
route: '/attachments/extract',
|
|
314
|
+
body: { traceId: targetId, uri: 'file:///C:/tmp/a.png', mode: 'ocr' },
|
|
315
|
+
});
|
|
316
|
+
assert.equal(denied.status, 503);
|
|
317
|
+
} finally {
|
|
318
|
+
cortex.close();
|
|
319
|
+
rmSync(dir, { recursive: true, force: true });
|
|
320
|
+
}
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
test('attachment metadata can be attached to an existing trace', async () => {
|
|
324
|
+
const { dir, cortex, handler } = setup();
|
|
325
|
+
try {
|
|
326
|
+
const written = await invoke(handler, { method: 'POST', route: '/write', body: { text: '已有记忆,稍后补附件。', explicit: true } });
|
|
327
|
+
const id = written.json.data.trace.id;
|
|
328
|
+
const attached = await invoke(handler, {
|
|
329
|
+
method: 'POST',
|
|
330
|
+
route: '/trace/attachments',
|
|
331
|
+
body: { id, attachments: [{ kind: 'image', uri: 'https://example.test/later.png', name: 'later.png' }] },
|
|
332
|
+
});
|
|
333
|
+
assert.equal(attached.status, 200);
|
|
334
|
+
assert.equal(attached.json.data.added, 1);
|
|
335
|
+
assert.equal(attached.json.data.trace.metadata.attachments.length, 1);
|
|
336
|
+
} finally {
|
|
337
|
+
cortex.close();
|
|
338
|
+
rmSync(dir, { recursive: true, force: true });
|
|
339
|
+
}
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
test('attachment reader is opt-in and only returns local file metadata', async () => {
|
|
343
|
+
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-api-attachment-'));
|
|
344
|
+
const file = path.join(dir, 'rice.txt');
|
|
345
|
+
writeFileSync(file, 'white rice', 'utf8');
|
|
346
|
+
const cortex = createCortex({ config: { dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite'), features: { attachmentReader: true } } });
|
|
347
|
+
const handler = createApiHandler({ cortex });
|
|
348
|
+
try {
|
|
349
|
+
const result = await invoke(handler, { method: 'POST', route: '/attachments/inspect', body: { uri: pathToFileURL(file).href } });
|
|
350
|
+
assert.equal(result.status, 200);
|
|
351
|
+
assert.equal(result.json.data.name, 'rice.txt');
|
|
352
|
+
assert.equal(result.json.data.bytes, 10);
|
|
353
|
+
assert.equal(result.json.data.sha256, crypto.createHash('sha256').update('white rice').digest('hex'));
|
|
354
|
+
assert.equal(result.json.data.content, undefined);
|
|
355
|
+
const remote = await invoke(handler, { method: 'POST', route: '/attachments/inspect', body: { uri: 'https://example.test/rice.txt' } });
|
|
356
|
+
assert.equal(remote.status, 400);
|
|
357
|
+
const disabled = createCortex({ config: { dataRoot: dir, dbFile: path.join(dir, 'disabled.sqlite') } });
|
|
358
|
+
const disabledHandler = createApiHandler({ cortex: disabled });
|
|
359
|
+
try {
|
|
360
|
+
const denied = await invoke(disabledHandler, { method: 'POST', route: '/attachments/inspect', body: { uri: pathToFileURL(file).href } });
|
|
361
|
+
assert.equal(denied.status, 403);
|
|
362
|
+
} finally { disabled.close(); }
|
|
363
|
+
} finally {
|
|
364
|
+
cortex.close();
|
|
365
|
+
rmSync(dir, { recursive: true, force: true });
|
|
366
|
+
}
|
|
367
|
+
});
|
|
368
|
+
|
|
369
|
+
test('api scope header/query isolates writes and working memory', async () => {
|
|
370
|
+
const { dir, cortex, handler } = setup();
|
|
371
|
+
try {
|
|
372
|
+
const wrote = await invoke(handler, { method: 'POST', route: '/write', body: { text: 'scope s2 的记忆。', explicit: true }, headers: { 'x-cortex-scope': 's2' } });
|
|
373
|
+
assert.equal(wrote.status, 200);
|
|
374
|
+
assert.equal(wrote.json.data.trace.scopeId, 's2');
|
|
375
|
+
const own = await invoke(handler, { route: '/search?q=scope&scope=s2' });
|
|
376
|
+
assert.equal(own.json.data.items.length, 1);
|
|
377
|
+
const asyncSearch = await invoke(handler, { route: '/search?q=scope&async=true&scope=s2' });
|
|
378
|
+
assert.equal(asyncSearch.status, 200);
|
|
379
|
+
assert.equal(asyncSearch.json.data.items.length, 1);
|
|
380
|
+
const other = await invoke(handler, { route: '/search?q=scope&scope=global' });
|
|
381
|
+
assert.equal(other.json.data.items.length, 0);
|
|
382
|
+
await invoke(handler, { method: 'POST', route: '/working/add', body: { text: 's2 焦点' }, headers: { 'x-cortex-scope': 's2' } });
|
|
383
|
+
const working = await invoke(handler, { route: '/working?scope=s2' });
|
|
384
|
+
assert.equal(working.json.data.length, 1);
|
|
385
|
+
const global = await invoke(handler, { route: '/working?scope=global' });
|
|
386
|
+
assert.equal(global.json.data.length, 0);
|
|
387
|
+
} finally {
|
|
388
|
+
cortex.close();
|
|
389
|
+
rmSync(dir, { recursive: true, force: true });
|
|
390
|
+
}
|
|
391
|
+
});
|
|
392
|
+
|
|
393
|
+
test('api scope isolation covers facts, graph candidates and as-of', async () => {
|
|
394
|
+
const { dir, cortex, handler } = setup();
|
|
395
|
+
try {
|
|
396
|
+
const s1 = await invoke(handler, {
|
|
397
|
+
method: 'POST',
|
|
398
|
+
route: '/write',
|
|
399
|
+
body: {
|
|
400
|
+
text: 'scope s1 的事实:测试对象喜欢值一。',
|
|
401
|
+
explicit: true,
|
|
402
|
+
facts: [{ subject: '测试对象', predicate: '喜欢', object: '值一', confidence: 0.8 }],
|
|
403
|
+
},
|
|
404
|
+
headers: { 'x-cortex-scope': 's1' },
|
|
405
|
+
});
|
|
406
|
+
assert.equal(s1.status, 200);
|
|
407
|
+
const s2 = await invoke(handler, {
|
|
408
|
+
method: 'POST',
|
|
409
|
+
route: '/write',
|
|
410
|
+
body: {
|
|
411
|
+
text: 'scope s2 的事实:测试对象喜欢值二。',
|
|
412
|
+
explicit: true,
|
|
413
|
+
facts: [{ subject: '测试对象', predicate: '喜欢', object: '值二', confidence: 0.8 }],
|
|
414
|
+
},
|
|
415
|
+
headers: { 'x-cortex-scope': 's2' },
|
|
416
|
+
});
|
|
417
|
+
assert.equal(s2.status, 200);
|
|
418
|
+
|
|
419
|
+
const factsS1 = await invoke(handler, { route: '/as-of?subject=测试对象&predicate=喜欢&scope=s1' });
|
|
420
|
+
assert.equal(factsS1.status, 200);
|
|
421
|
+
assert.equal(factsS1.json.data.facts.length, 1);
|
|
422
|
+
assert.equal(factsS1.json.data.facts[0].object, '值一');
|
|
423
|
+
const factsS2 = await invoke(handler, { route: '/as-of?subject=测试对象&predicate=喜欢&scope=s2' });
|
|
424
|
+
assert.equal(factsS2.json.data.facts.length, 1);
|
|
425
|
+
assert.equal(factsS2.json.data.facts[0].object, '值二');
|
|
426
|
+
|
|
427
|
+
const candidatesS1 = await invoke(handler, { route: '/graph/candidates?scope=s1' });
|
|
428
|
+
const candidatesS2 = await invoke(handler, { route: '/graph/candidates?scope=s2' });
|
|
429
|
+
assert.equal(candidatesS1.json.data.length, 1);
|
|
430
|
+
assert.equal(candidatesS2.json.data.length, 1);
|
|
431
|
+
assert.notEqual(candidatesS1.json.data[0].id, candidatesS2.json.data[0].id);
|
|
432
|
+
|
|
433
|
+
const graphS1 = await invoke(handler, { route: '/graph/edges?scope=s1' });
|
|
434
|
+
assert.equal(graphS1.status, 200);
|
|
435
|
+
assert.equal(graphS1.json.data.edges.length, 0);
|
|
436
|
+
} finally {
|
|
437
|
+
cortex.close();
|
|
438
|
+
rmSync(dir, { recursive: true, force: true });
|
|
439
|
+
}
|
|
440
|
+
});
|
|
441
|
+
|
|
442
|
+
test('api exposes digest, tick, retention apply, evolve propose and guard validate', async () => {
|
|
443
|
+
const { dir, cortex, handler } = setup();
|
|
444
|
+
try {
|
|
445
|
+
const digest = await invoke(handler, { route: '/digest?date=2026-11-11' });
|
|
446
|
+
assert.equal(digest.status, 200);
|
|
447
|
+
assert.match(digest.json.data.content, /记忆摘要/);
|
|
448
|
+
const tick = await invoke(handler, { method: 'POST', route: '/tick', body: { max: 2 } });
|
|
449
|
+
assert.equal(tick.status, 200);
|
|
450
|
+
assert.ok(Array.isArray(tick.json.data.executed));
|
|
451
|
+
const retention = await invoke(handler, { method: 'POST', route: '/retention/apply', body: { dryRun: true } });
|
|
452
|
+
assert.equal(retention.status, 200);
|
|
453
|
+
assert.ok(Number.isInteger(retention.json.data.archived));
|
|
454
|
+
const propose = await invoke(handler, { method: 'POST', route: '/evolve/propose', body: { kind: 'retrieval', config: { budget: 300 }, rationale: 'api' } });
|
|
455
|
+
assert.equal(propose.status, 200);
|
|
456
|
+
assert.equal(propose.json.data.kind, 'retrieval');
|
|
457
|
+
const validate = await invoke(handler, { method: 'POST', route: '/guard/validate', body: { file: path.join(dir, 'missing.sqlite') } });
|
|
458
|
+
assert.equal(validate.status, 200);
|
|
459
|
+
assert.equal(validate.json.data.ok, false);
|
|
460
|
+
const purge = await invoke(handler, { method: 'POST', route: '/working/purge', body: {} });
|
|
461
|
+
assert.equal(purge.status, 200);
|
|
462
|
+
const qa = await invoke(handler, { method: 'POST', route: '/qa', body: { question: '用户喜欢什么?' } });
|
|
463
|
+
assert.equal(qa.status, 200);
|
|
464
|
+
assert.ok(['extractive', 'abstain'].includes(qa.json.data.mode));
|
|
465
|
+
} finally {
|
|
466
|
+
cortex.close();
|
|
467
|
+
rmSync(dir, { recursive: true, force: true });
|
|
468
|
+
}
|
|
469
|
+
});
|
|
470
|
+
|
|
471
|
+
test('api exposes working, goals, emotion and life actions', async () => {
|
|
472
|
+
const { dir, cortex, handler } = setup();
|
|
473
|
+
try {
|
|
474
|
+
const working = await invoke(handler, { method: 'POST', route: '/working/add', body: { text: '写 UI', priority: 0.9 } });
|
|
475
|
+
assert.equal(working.status, 200);
|
|
476
|
+
assert.match(working.json.data.text, /写 UI/);
|
|
477
|
+
const list = await invoke(handler, { route: '/working' });
|
|
478
|
+
assert.equal(list.json.data.length, 1);
|
|
479
|
+
const goal = await invoke(handler, { method: 'POST', route: '/goals/add', body: { title: '完成 API' } });
|
|
480
|
+
assert.equal(goal.status, 200);
|
|
481
|
+
const goals = await invoke(handler, { route: '/goals' });
|
|
482
|
+
assert.equal(goals.json.data.length, 1);
|
|
483
|
+
const emotion = await invoke(handler, { method: 'POST', route: '/emotion/record', body: { text: '助手有点心疼用户。', target: '用户' } });
|
|
484
|
+
assert.equal(emotion.status, 200);
|
|
485
|
+
assert.ok(emotion.json.data.event);
|
|
486
|
+
const affect = await invoke(handler, { route: '/emotion' });
|
|
487
|
+
assert.ok(affect.json.data.targets.length >= 1);
|
|
488
|
+
const diary = await invoke(handler, { method: 'POST', route: '/life/diary', body: { text: '今天把 API 接上了。', mood: '踏实' } });
|
|
489
|
+
assert.equal(diary.status, 200);
|
|
490
|
+
const life = await invoke(handler, { route: '/life' });
|
|
491
|
+
assert.ok(life.json.data.diary.length >= 1);
|
|
492
|
+
const settings = await invoke(handler, { route: '/settings' });
|
|
493
|
+
assert.equal(settings.status, 200);
|
|
494
|
+
assert.equal(settings.json.data.scope, 'global');
|
|
495
|
+
assert.ok(settings.json.data.permissions);
|
|
496
|
+
assert.equal(settings.json.data.permissions.localApi, true);
|
|
497
|
+
const report = await invoke(handler, { route: '/report' });
|
|
498
|
+
assert.equal(report.status, 200);
|
|
499
|
+
assert.ok(report.json.data.health);
|
|
500
|
+
assert.ok(report.json.data.metrics);
|
|
501
|
+
const noConfirm = await invoke(handler, { method: 'POST', route: '/permissions', body: { key: 'llm', value: true } });
|
|
502
|
+
assert.equal(noConfirm.status, 400);
|
|
503
|
+
const changed = await invoke(handler, { method: 'POST', route: '/permissions', body: { key: 'llm', value: true, confirm: true } });
|
|
504
|
+
assert.equal(changed.status, 200);
|
|
505
|
+
assert.equal(changed.json.data.restartRequired, true);
|
|
506
|
+
const permissions = await invoke(handler, { route: '/permissions' });
|
|
507
|
+
assert.equal(permissions.json.data.current.llm, true);
|
|
508
|
+
const restored = await invoke(handler, { method: 'POST', route: '/permissions/rollback', body: {} });
|
|
509
|
+
assert.equal(restored.status, 200);
|
|
510
|
+
assert.equal(restored.json.data.rolledBack, true);
|
|
511
|
+
const models = await invoke(handler, { route: '/models' });
|
|
512
|
+
assert.equal(models.status, 200);
|
|
513
|
+
assert.equal(models.json.data.embedding.mode, 'hash');
|
|
514
|
+
const modelSave = await invoke(handler, { method: 'POST', route: '/models', body: { embedding: { mode: 'local' }, confirm: true } });
|
|
515
|
+
assert.equal(modelSave.status, 200);
|
|
516
|
+
assert.equal(modelSave.json.data.restartRequired, true);
|
|
517
|
+
const savedModels = await invoke(handler, { route: '/models' });
|
|
518
|
+
assert.equal(savedModels.json.data.embedding.mode, 'local');
|
|
519
|
+
assert.equal(savedModels.json.data.embedding.model, 'bge-m3');
|
|
520
|
+
assert.doesNotMatch(JSON.stringify(savedModels.json.data), /"apiKey"/);
|
|
521
|
+
const graph = await invoke(handler, { route: '/graph' });
|
|
522
|
+
assert.equal(graph.status, 200);
|
|
523
|
+
assert.ok(Number.isInteger(graph.json.data.nodes));
|
|
524
|
+
const asOf = await invoke(handler, { route: '/as-of?subject=missing&mode=strict' });
|
|
525
|
+
assert.equal(asOf.status, 200);
|
|
526
|
+
assert.deepEqual(asOf.json.data.facts, []);
|
|
527
|
+
const playbook = await invoke(handler, { route: '/playbook' });
|
|
528
|
+
assert.equal(playbook.status, 200);
|
|
529
|
+
assert.deepEqual(playbook.json.data, []);
|
|
530
|
+
const added = await invoke(handler, { method: 'POST', route: '/playbook', body: { action: 'add', title: '先测试', body: '改完先跑测试' } });
|
|
531
|
+
assert.equal(added.status, 200);
|
|
532
|
+
assert.match(added.json.data.title, /先测试/);
|
|
533
|
+
const rendered = await invoke(handler, { method: 'POST', route: '/playbook', body: { action: 'render' } });
|
|
534
|
+
assert.equal(rendered.status, 200);
|
|
535
|
+
assert.match(rendered.json.data.text, /先测试/);
|
|
536
|
+
const runs = await invoke(handler, { route: '/dream-runs' });
|
|
537
|
+
assert.equal(runs.status, 200);
|
|
538
|
+
assert.ok(Array.isArray(runs.json.data));
|
|
539
|
+
const consolidate = await invoke(handler, { method: 'POST', route: '/consolidate', body: { dryRun: true } });
|
|
540
|
+
assert.equal(consolidate.status, 200);
|
|
541
|
+
assert.ok(consolidate.json.data.run);
|
|
542
|
+
cortex.graphCandidateAction({ action: 'add', fromId: 'ent_api_a', toId: 'ent_api_b', rel: 'test', confidence: 0.8 });
|
|
543
|
+
cortex.graphCandidateAction({ action: 'add', fromId: 'ent_api_c', toId: 'ent_api_d', rel: 'test', confidence: 0.4 });
|
|
544
|
+
const candidates = await invoke(handler, { route: '/graph/candidates' });
|
|
545
|
+
assert.equal(candidates.status, 200);
|
|
546
|
+
assert.equal(candidates.json.data.length, 2);
|
|
547
|
+
|
|
548
|
+
const edges = await invoke(handler, { route: '/graph/edges' });
|
|
549
|
+
assert.equal(edges.status, 200);
|
|
550
|
+
assert.ok(Array.isArray(edges.json.data.edges));
|
|
551
|
+
} finally {
|
|
552
|
+
cortex.close();
|
|
553
|
+
rmSync(dir, { recursive: true, force: true });
|
|
554
|
+
}
|
|
555
|
+
});
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import test from 'node:test';
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
3
|
+
import { appraise, estimateNovelty, estimateRelevance, mergeDriveDeltas } from '../../src/self/appraisal.js';
|
|
4
|
+
|
|
5
|
+
test('warm identity-relevant text raises care and valence', () => {
|
|
6
|
+
const result = appraise('用户说会一直陪着助手,助手很开心。', {
|
|
7
|
+
actor: 'user',
|
|
8
|
+
relationship: { target: '用户', trust: 0.8, care: 0.8 },
|
|
9
|
+
selfState: { drives: { safety: 0.8 } },
|
|
10
|
+
});
|
|
11
|
+
assert.ok(result.valence > 0);
|
|
12
|
+
assert.ok(result.identityRelevance > 0.5);
|
|
13
|
+
assert.ok(result.driveDeltas.care > 0);
|
|
14
|
+
assert.ok(result.relationship.careDelta > 0);
|
|
15
|
+
assert.ok(result.salience > 0.4);
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
test('hostile text raises threat and lowers safety', () => {
|
|
19
|
+
const result = appraise('助手讨厌你,滚,别烦我。', { relationship: { target: '用户', trust: 0.3, care: 0.3 } });
|
|
20
|
+
assert.ok(result.threat > 0.3);
|
|
21
|
+
assert.ok(result.valence < 0);
|
|
22
|
+
assert.ok(result.driveDeltas.safety < 0);
|
|
23
|
+
assert.ok(result.relationship.trustDelta < 0);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
test('novelty falls when recent text is similar', () => {
|
|
27
|
+
const high = estimateNovelty('今天第一次尝试新的记忆插件架构', []);
|
|
28
|
+
const low = estimateNovelty('今天第一次尝试新的记忆插件架构', ['今天第一次尝试新的记忆插件架构']);
|
|
29
|
+
assert.ok(high > low);
|
|
30
|
+
assert.ok(low < 0.2);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
test('relevance responds to goals and identity', () => {
|
|
34
|
+
const goal = estimateRelevance('今天修了插件的 FTS 索引', { goals: ['修插件 FTS 索引'] });
|
|
35
|
+
const unrelated = estimateRelevance('今天天气不错', { goals: ['修插件 FTS 索引'] });
|
|
36
|
+
assert.ok(goal > unrelated);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
test('choice language gives autonomy a small positive delta', () => {
|
|
40
|
+
const result = appraise('你想怎么做都可以,由你决定。');
|
|
41
|
+
assert.ok(result.driveDeltas.autonomy > 0);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
test('mergeDriveDeltas clamps combined deltas', () => {
|
|
45
|
+
const merged = mergeDriveDeltas({ curiosity: 0.4 }, { curiosity: 0.4, care: -0.9 });
|
|
46
|
+
assert.equal(merged.curiosity, 0.5);
|
|
47
|
+
assert.equal(merged.care, -0.5);
|
|
48
|
+
});
|
|
@@ -0,0 +1,33 @@
|
|
|
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('forget deletes attachment metadata with the trace and archive keeps it', () => {
|
|
9
|
+
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-attachments-delete-'));
|
|
10
|
+
const cortex = createCortex({ config: { dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite') } });
|
|
11
|
+
try {
|
|
12
|
+
const archived = cortex.write({
|
|
13
|
+
text: '带附件的归档记忆。',
|
|
14
|
+
explicit: true,
|
|
15
|
+
attachments: [{ kind: 'image', uri: 'https://example.test/archive.png' }],
|
|
16
|
+
});
|
|
17
|
+
assert.equal(archived.trace.metadata.attachments.length, 1);
|
|
18
|
+
assert.equal(cortex.forget({ traceId: archived.trace.id, mode: 'archive' }).removed, true);
|
|
19
|
+
assert.equal(cortex.repos.traces.get(archived.trace.id).state, 'archived');
|
|
20
|
+
assert.equal(cortex.repos.traces.get(archived.trace.id).metadata.attachments.length, 1);
|
|
21
|
+
|
|
22
|
+
const deleted = cortex.write({
|
|
23
|
+
text: '带附件的删除记忆。',
|
|
24
|
+
explicit: true,
|
|
25
|
+
attachments: [{ kind: 'audio', uri: 'https://example.test/voice.mp3' }],
|
|
26
|
+
});
|
|
27
|
+
assert.equal(cortex.forget({ traceId: deleted.trace.id, mode: 'delete' }).removed, true);
|
|
28
|
+
assert.equal(cortex.repos.traces.get(deleted.trace.id), null);
|
|
29
|
+
} finally {
|
|
30
|
+
cortex.close();
|
|
31
|
+
rmSync(dir, { recursive: true, force: true });
|
|
32
|
+
}
|
|
33
|
+
});
|