dsh-cortex-memory 0.4.146 → 0.4.148

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (184) hide show
  1. package/CHANGELOG.md +2423 -2369
  2. package/README.md +8 -8
  3. package/bench/governance.mjs +265 -0
  4. package/bench/migration-bench.mjs +1 -1
  5. package/bench/semantic-cn.js +4 -4
  6. package/bench/synthetic.js +3 -3
  7. package/bin/copy-db.mjs +1 -1
  8. package/bin/repair.mjs +7 -7
  9. package/client.js +1 -1
  10. package/docs/install-windows.md +2 -2
  11. package/docs/models.md +1 -1
  12. package/docs/quota-compaction-design.md +3 -3
  13. package/icon.svg +12 -2
  14. package/index.js +5 -6
  15. package/package.json +7 -8
  16. package/src/adapters/shiori.js +1 -1
  17. package/src/engines/compression.js +8 -3
  18. package/src/engines/consolidate.js +1 -1
  19. package/src/engines/encode.js +3 -3
  20. package/src/engines/extract.js +1 -1
  21. package/src/engines/health.js +1 -1
  22. package/src/engines/life.js +2 -2
  23. package/src/engines/narrative.js +4 -8
  24. package/src/engines/retrieve.js +41 -4
  25. package/src/host/scheduler.js +5 -4
  26. package/src/memory/tokenizer.js +1 -1
  27. package/src/self/alias.js +4 -5
  28. package/src/self/memory-policy.js +59 -0
  29. package/src/service/cortex.js +20 -2
  30. package/src/store/repositories/facts.js +1 -1
  31. package/src/store/repositories/traces.js +1 -1
  32. package/test/integration/_helpers.js +0 -128
  33. package/test/integration/acceptance.test.js +0 -803
  34. package/test/integration/ann-load.test.js +0 -61
  35. package/test/integration/api-contract.test.js +0 -1476
  36. package/test/integration/backup-privacy.test.js +0 -214
  37. package/test/integration/consolidate.test.js +0 -79
  38. package/test/integration/edge-cases.test.js +0 -1089
  39. package/test/integration/exchange.test.js +0 -326
  40. package/test/integration/life-loop.test.js +0 -189
  41. package/test/integration/load.test.js +0 -751
  42. package/test/integration/markdown-sync.test.js +0 -35
  43. package/test/integration/policy-query-api.test.js +0 -306
  44. package/test/integration/resilience-writes.test.js +0 -241
  45. package/test/integration/tools-contract.test.js +0 -1324
  46. package/test/unit/alias.test.js +0 -87
  47. package/test/unit/ann-edge.test.js +0 -102
  48. package/test/unit/ann.test.js +0 -77
  49. package/test/unit/api-client.test.js +0 -576
  50. package/test/unit/api-validate.test.js +0 -19
  51. package/test/unit/api.test.js +0 -555
  52. package/test/unit/appraisal.test.js +0 -48
  53. package/test/unit/attachments.test.js +0 -33
  54. package/test/unit/auto-reflect.test.js +0 -141
  55. package/test/unit/autonomy.test.js +0 -53
  56. package/test/unit/batch.test.js +0 -46
  57. package/test/unit/bench.test.js +0 -40
  58. package/test/unit/benchmark-synthetic.test.js +0 -52
  59. package/test/unit/capture-filter.test.js +0 -296
  60. package/test/unit/cli-default-root.test.js +0 -21
  61. package/test/unit/client-contract.test.js +0 -32
  62. package/test/unit/compaction-preview.test.js +0 -91
  63. package/test/unit/compression.test.js +0 -76
  64. package/test/unit/consolidate-dedupe.test.js +0 -174
  65. package/test/unit/consolidate-errors.test.js +0 -63
  66. package/test/unit/consolidate-gate.test.js +0 -26
  67. package/test/unit/consolidate-preview.test.js +0 -168
  68. package/test/unit/consolidate-protection.test.js +0 -156
  69. package/test/unit/consolidate-reason.test.js +0 -206
  70. package/test/unit/consolidate-retry.test.js +0 -91
  71. package/test/unit/consolidate-selection.test.js +0 -103
  72. package/test/unit/core.test.js +0 -77
  73. package/test/unit/coverage-rewrite.test.js +0 -38
  74. package/test/unit/dedupe-episodes.test.js +0 -34
  75. package/test/unit/dedupe-threshold.test.js +0 -14
  76. package/test/unit/develop.test.js +0 -130
  77. package/test/unit/development.test.js +0 -68
  78. package/test/unit/digest.test.js +0 -26
  79. package/test/unit/dsr.test.js +0 -111
  80. package/test/unit/edges-candidates.test.js +0 -113
  81. package/test/unit/emotion.test.js +0 -62
  82. package/test/unit/encode.test.js +0 -98
  83. package/test/unit/evolution.test.js +0 -130
  84. package/test/unit/external-ann-search.test.js +0 -50
  85. package/test/unit/external-ann.test.js +0 -29
  86. package/test/unit/extract-question.test.js +0 -41
  87. package/test/unit/fact-guard.test.js +0 -51
  88. package/test/unit/fact-noise-llm.test.js +0 -44
  89. package/test/unit/facts-belief.test.js +0 -32
  90. package/test/unit/facts-compat.test.js +0 -62
  91. package/test/unit/facts.test.js +0 -107
  92. package/test/unit/feature-overrides.test.js +0 -42
  93. package/test/unit/fts-migration.test.js +0 -42
  94. package/test/unit/graph.test.js +0 -79
  95. package/test/unit/growth-reflect.test.js +0 -124
  96. package/test/unit/guard-runtime.test.js +0 -40
  97. package/test/unit/guardian.test.js +0 -91
  98. package/test/unit/health-consolidation.test.js +0 -52
  99. package/test/unit/health-fts-scope.test.js +0 -33
  100. package/test/unit/health-privacy.test.js +0 -102
  101. package/test/unit/health-summaries.test.js +0 -24
  102. package/test/unit/health-text-maintain.test.js +0 -33
  103. package/test/unit/health-today.test.js +0 -45
  104. package/test/unit/hnsw.test.js +0 -35
  105. package/test/unit/host-runtime.test.js +0 -142
  106. package/test/unit/host.test.js +0 -58
  107. package/test/unit/identity.test.js +0 -98
  108. package/test/unit/induct-evaluate.test.js +0 -41
  109. package/test/unit/ingest-extracted.test.js +0 -35
  110. package/test/unit/inhibition.test.js +0 -90
  111. package/test/unit/initiative.test.js +0 -90
  112. package/test/unit/jobs.test.js +0 -93
  113. package/test/unit/json-snapshot.test.js +0 -57
  114. package/test/unit/label-entities-skip.test.js +0 -18
  115. package/test/unit/life-consolidate.test.js +0 -34
  116. package/test/unit/life.test.js +0 -149
  117. package/test/unit/llm-facts-integration.test.js +0 -27
  118. package/test/unit/llm-facts.test.js +0 -16
  119. package/test/unit/llm.test.js +0 -45
  120. package/test/unit/locomo.test.js +0 -45
  121. package/test/unit/longmemeval.test.js +0 -56
  122. package/test/unit/maintain-dispute.test.js +0 -62
  123. package/test/unit/maintain-duration.test.js +0 -194
  124. package/test/unit/markdown-import.test.js +0 -44
  125. package/test/unit/markdown.test.js +0 -34
  126. package/test/unit/memory-edit.test.js +0 -112
  127. package/test/unit/metrics.test.js +0 -26
  128. package/test/unit/migration.test.js +0 -38
  129. package/test/unit/model-config.test.js +0 -52
  130. package/test/unit/mood-saturation.test.js +0 -33
  131. package/test/unit/narrative-weave.test.js +0 -271
  132. package/test/unit/narrative.test.js +0 -62
  133. package/test/unit/playbook-consolidate.test.js +0 -24
  134. package/test/unit/privacy-scope.test.js +0 -39
  135. package/test/unit/profile-paths.test.js +0 -21
  136. package/test/unit/profile.test.js +0 -26
  137. package/test/unit/prompt.test.js +0 -42
  138. package/test/unit/providers.test.js +0 -42
  139. package/test/unit/qa.test.js +0 -100
  140. package/test/unit/query.test.js +0 -53
  141. package/test/unit/reflect-backfill.test.js +0 -158
  142. package/test/unit/reflect-coverage.test.js +0 -35
  143. package/test/unit/reflect-overview.test.js +0 -22
  144. package/test/unit/reflect-phrasing.test.js +0 -75
  145. package/test/unit/reflect-sampling.test.js +0 -80
  146. package/test/unit/reflect-source-budget.test.js +0 -24
  147. package/test/unit/relationship-dynamics.test.js +0 -29
  148. package/test/unit/retention.test.js +0 -31
  149. package/test/unit/retrieval-ranking.test.js +0 -31
  150. package/test/unit/retrieve.test.js +0 -117
  151. package/test/unit/revive-on-read.test.js +0 -84
  152. package/test/unit/rotation.test.js +0 -25
  153. package/test/unit/scanner.test.js +0 -50
  154. package/test/unit/scenario.test.js +0 -78
  155. package/test/unit/scheduler-autoconsolidate.test.js +0 -95
  156. package/test/unit/scheduler-autodedupe.test.js +0 -52
  157. package/test/unit/scheduler-autosleep.test.js +0 -50
  158. package/test/unit/scheduler-cadence.test.js +0 -33
  159. package/test/unit/scheduler-deep.test.js +0 -62
  160. package/test/unit/scheduler-tick-gate.test.js +0 -38
  161. package/test/unit/scheduler.test.js +0 -137
  162. package/test/unit/self-state.test.js +0 -94
  163. package/test/unit/service.test.js +0 -72
  164. package/test/unit/shiori-import.test.js +0 -77
  165. package/test/unit/skills.test.js +0 -104
  166. package/test/unit/spacing.test.js +0 -14
  167. package/test/unit/store.test.js +0 -111
  168. package/test/unit/summon-left-slot.test.js +0 -51
  169. package/test/unit/summon-rank.test.js +0 -51
  170. package/test/unit/temporal-graph.test.js +0 -44
  171. package/test/unit/tiers-gist.test.js +0 -140
  172. package/test/unit/tiers.test.js +0 -39
  173. package/test/unit/tokenizer-fts.test.js +0 -35
  174. package/test/unit/tools.test.js +0 -122
  175. package/test/unit/upgrade-0412.test.js +0 -104
  176. package/test/unit/upgrade-0413.test.js +0 -84
  177. package/test/unit/upgrade-0414.test.js +0 -101
  178. package/test/unit/vector-ann.test.js +0 -53
  179. package/test/unit/vector-async.test.js +0 -69
  180. package/test/unit/vector-coverage.test.js +0 -34
  181. package/test/unit/vector-override.test.js +0 -37
  182. package/test/unit/vector-typed-array.test.js +0 -47
  183. package/test/unit/vector.test.js +0 -68
  184. package/test/unit/working.test.js +0 -99
@@ -1,576 +0,0 @@
1
- import test from 'node:test';
2
- import assert from 'node:assert/strict';
3
- import { createApiClient, ApiError, API_BASE } from '../../src/api/client.js';
4
-
5
- function json(body, { status = 200, headers = {} } = {}) {
6
- return new Response(JSON.stringify(body), {
7
- status,
8
- headers: { 'content-type': 'application/json', ...headers },
9
- });
10
- }
11
-
12
- function ok(data) {
13
- return json({ ok: true, data });
14
- }
15
-
16
- function harness(options = {}) {
17
- const calls = [];
18
- const queue = [];
19
- const fetchImpl = async (url, init = {}) => {
20
- calls.push({ url: String(url), init });
21
- if (queue.length === 0) throw new Error(`unexpected fetch: ${url}`);
22
- const next = queue.shift();
23
- if (next instanceof Error) throw next;
24
- if (typeof next === 'function') return next(url, init);
25
- return next;
26
- };
27
- const client = createApiClient({ fetchImpl, ...options });
28
- return { client, calls, queue };
29
- }
30
-
31
- function parseBody(call) {
32
- assert.equal(typeof call.init.body, 'string');
33
- return JSON.parse(call.init.body);
34
- }
35
-
36
- function pathOf(call) {
37
- return new URL(call.url).pathname;
38
- }
39
-
40
- test('default base URL targets /api/cortex and unwraps the ping envelope', async () => {
41
- const { client, calls, queue } = harness();
42
- const at = '2026-11-11T00:00:00.000Z';
43
- queue.push(json({ ok: true, at }));
44
-
45
- const result = await client.ping();
46
-
47
- assert.deepEqual(result, { ok: true, at });
48
- assert.equal(calls.length, 1);
49
- assert.equal(calls[0].url, 'http://127.0.0.1:7430/api/cortex/ping');
50
- assert.equal(calls[0].init.method, 'GET');
51
- assert.equal(calls[0].init.body, undefined);
52
- assert.equal(calls[0].init.headers['x-cortex'], undefined);
53
- });
54
-
55
- test('base URL joining preserves paths and never creates a double slash', async () => {
56
- const cases = [
57
- ['http://127.0.0.1:9100/', '/api/cortex/ping'],
58
- ['http://127.0.0.1:9100/api/cortex', '/api/cortex/ping'],
59
- ['http://127.0.0.1:9100/api/cortex/', '/api/cortex/ping'],
60
- ['http://127.0.0.1:9100/proxy/', '/proxy/api/cortex/ping'],
61
- ];
62
-
63
- for (const [baseUrl, expected] of cases) {
64
- const { client, calls, queue } = harness({ baseUrl });
65
- queue.push(json({ ok: true, at: '2026-11-11T00:00:00.000Z' }));
66
- await client.ping();
67
- const url = new URL(calls[0].url);
68
- assert.equal(url.pathname, expected, baseUrl);
69
- assert.ok(!url.pathname.includes('//'), `double slash in ${url.pathname}`);
70
- }
71
- });
72
-
73
- test('baseUrl and fetchImpl are validated up front', () => {
74
- assert.throws(() => createApiClient({ baseUrl: 'not a url' }), /absolute http/);
75
- assert.throws(() => createApiClient({ baseUrl: 'ftp://127.0.0.1:7430' }), /http or https/);
76
- assert.throws(() => createApiClient({ fetchImpl: null }), /fetchImpl/);
77
- });
78
-
79
- test('simple GET methods map to their exact routes and return data', async () => {
80
- const { client, calls, queue } = harness();
81
- const methods = [
82
- ['health', '/health'],
83
- ['self', '/self'],
84
- ['stats', '/stats'],
85
- ['settings', '/settings'],
86
- ['metrics', '/metrics'],
87
- ['graph', '/graph'],
88
- ['retention', '/retention'],
89
- ['skills', '/skills'],
90
- ];
91
-
92
- for (const [method, route] of methods) {
93
- queue.push(ok({ method }));
94
- const result = await client[method]();
95
- assert.deepEqual(result, { method });
96
- const call = calls.at(-1);
97
- assert.equal(call.init.method, 'GET');
98
- assert.equal(pathOf(call), `${API_BASE}${route}`);
99
- }
100
- assert.equal(calls.length, methods.length);
101
- });
102
-
103
- test('GET search sends q, limit and budget as query parameters', async () => {
104
- const { client, calls, queue } = harness();
105
- queue.push(ok({ items: [{ id: 't1' }] }));
106
-
107
- const result = await client.search('白 饭', { limit: 3, budget: 1500 });
108
-
109
- assert.equal(result.items[0].id, 't1');
110
- const url = new URL(calls[0].url);
111
- assert.equal(url.pathname, '/api/cortex/search');
112
- assert.equal(url.searchParams.get('q'), '白 饭');
113
- assert.equal(url.searchParams.get('limit'), '3');
114
- assert.equal(url.searchParams.get('budget'), '1500');
115
- });
116
-
117
- test('GET search omits unset optional parameters', async () => {
118
- const { client, calls, queue } = harness();
119
- queue.push(ok({ items: [] }));
120
-
121
- await client.search('only-query');
122
-
123
- const url = new URL(calls[0].url);
124
- assert.equal(url.searchParams.get('q'), 'only-query');
125
- assert.equal(url.searchParams.has('limit'), false);
126
- assert.equal(url.searchParams.has('budget'), false);
127
- });
128
-
129
- test('trace, timeline, audit and goals list build their query strings', async () => {
130
- const { client, calls, queue } = harness();
131
-
132
- queue.push(ok({ id: 'trace with space' }));
133
- await client.trace('trace with space');
134
- let url = new URL(calls.at(-1).url);
135
- assert.equal(url.pathname, '/api/cortex/trace');
136
- assert.equal(url.searchParams.get('id'), 'trace with space');
137
-
138
- queue.push(ok({ anchor: null, items: [] }));
139
- await client.timeline('t1', { before: 0, after: 7 });
140
- url = new URL(calls.at(-1).url);
141
- assert.equal(url.pathname, '/api/cortex/timeline');
142
- assert.equal(url.searchParams.get('id'), 't1');
143
- assert.equal(url.searchParams.get('before'), '0');
144
- assert.equal(url.searchParams.get('after'), '7');
145
-
146
- queue.push(ok([]));
147
- await client.audit({ limit: 25 });
148
- url = new URL(calls.at(-1).url);
149
- assert.equal(url.pathname, '/api/cortex/audit');
150
- assert.equal(url.searchParams.get('limit'), '25');
151
-
152
- queue.push(ok([]));
153
- await client.goals('list', { status: 'active' });
154
- url = new URL(calls.at(-1).url);
155
- assert.equal(url.pathname, '/api/cortex/goals');
156
- assert.equal(url.searchParams.get('status'), 'active');
157
-
158
- queue.push(ok([]));
159
- await client.audit();
160
- assert.equal(new URL(calls.at(-1).url).searchParams.has('limit'), false);
161
- });
162
-
163
- test('POST write sends JSON, x-cortex and the exact body', async () => {
164
- const { client, calls, queue } = harness();
165
- queue.push(ok({ trace: { id: 'tr1' } }));
166
-
167
- const input = { text: '用户说会买米饭。', tags: ['米饭'], explicit: true };
168
- const result = await client.write(input);
169
-
170
- assert.deepEqual(result, { trace: { id: 'tr1' } });
171
- const call = calls[0];
172
- assert.equal(call.init.method, 'POST');
173
- assert.equal(pathOf(call), '/api/cortex/write');
174
- assert.equal(call.init.headers['content-type'], 'application/json');
175
- assert.equal(call.init.headers['x-cortex'], '1');
176
- assert.deepEqual(parseBody(call), input);
177
- });
178
-
179
- test('custom tokenHeader is sent as the x-cortex value', async () => {
180
- const { client, calls, queue } = harness({ tokenHeader: '2' });
181
- queue.push(ok({ trace: { id: 'tr2' } }));
182
-
183
- await client.write({ text: 'x' });
184
-
185
- assert.equal(calls[0].init.headers['x-cortex'], '2');
186
- });
187
-
188
- test('scopeId is sent on every request and validated', async () => {
189
- const { client, calls, queue } = harness({ scopeId: 'session:42' });
190
- queue.push(ok({ items: [] }));
191
- await client.search('米饭');
192
- assert.equal(calls[0].init.headers['x-cortex-scope'], 'session:42');
193
- assert.throws(() => createApiClient({ fetchImpl: async () => new Response('{}'), scopeId: 'bad scope' }), /scopeId/);
194
- });
195
-
196
- test('writeMany wraps items in the documented batch body', async () => {
197
- const { client, calls, queue } = harness();
198
- const items = [{ text: 'a' }, { text: 'b' }];
199
- queue.push(ok([{ trace: { id: 'a' } }, { trace: { id: 'b' } }]));
200
-
201
- const result = await client.writeMany(items);
202
-
203
- assert.equal(result.length, 2);
204
- assert.deepEqual(parseBody(calls[0]), { items });
205
- assert.equal(pathOf(calls[0]), '/api/cortex/write');
206
- });
207
-
208
- test('feedback, forget, reflect and compress send only supported fields', async () => {
209
- const { client, calls, queue } = harness();
210
-
211
- queue.push(ok({ updated: true }));
212
- await client.feedback({ id: 't1', signal: 'used', predicted: 0.75, outcome: 1, ignored: 'x' });
213
- assert.deepEqual(parseBody(calls.at(-1)), { id: 't1', signal: 'used', predicted: 0.75, outcome: 1 });
214
- assert.equal(pathOf(calls.at(-1)), '/api/cortex/feedback');
215
-
216
- queue.push(ok({ archived: true }));
217
- await client.forget({ id: 't1' });
218
- assert.deepEqual(parseBody(calls.at(-1)), { id: 't1' });
219
-
220
- queue.push(ok({ archived: true }));
221
- await client.forget({ id: 't2', mode: 'delete' });
222
- assert.deepEqual(parseBody(calls.at(-1)), { id: 't2', mode: 'delete' });
223
- assert.equal(pathOf(calls.at(-1)), '/api/cortex/forget');
224
-
225
- queue.push(ok({ refreshed: true }));
226
- await client.reflect();
227
- assert.deepEqual(parseBody(calls.at(-1)), {});
228
-
229
- queue.push(ok({ refreshed: true }));
230
- await client.reflect({ windowDays: 3, force: true });
231
- assert.deepEqual(parseBody(calls.at(-1)), { windowDays: 3, force: true });
232
- assert.equal(pathOf(calls.at(-1)), '/api/cortex/reflect');
233
-
234
- queue.push(ok({ archived: 0 }));
235
- await client.compress({ limit: 10 });
236
- assert.deepEqual(parseBody(calls.at(-1)), { limit: 10 });
237
- assert.equal(pathOf(calls.at(-1)), '/api/cortex/compress');
238
-
239
- queue.push(ok({ archived: 0 }));
240
- await client.compress();
241
- assert.deepEqual(parseBody(calls.at(-1)), {});
242
-
243
- queue.push(ok({ answer: '米饭 [1]', mode: 'extractive' }));
244
- await client.qa({ question: '用户喜欢什么?' });
245
- assert.equal(pathOf(calls.at(-1)), '/api/cortex/qa');
246
- assert.deepEqual(parseBody(calls.at(-1)), { question: '用户喜欢什么?' });
247
- });
248
-
249
- test('guard maps check, backup and repair to the real routes', async () => {
250
- const { client, calls, queue } = harness();
251
-
252
- queue.push(ok({ issues: [] }));
253
- await client.guard();
254
- assert.equal(calls.at(-1).init.method, 'GET');
255
- assert.equal(pathOf(calls.at(-1)), '/api/cortex/guard');
256
-
257
- queue.push(ok({ path: 'C:/backup.sqlite' }));
258
- await client.guard('backup', { file: 'C:/backup.sqlite', target: 'ignored', dir: 'ignored' });
259
- assert.equal(calls.at(-1).init.method, 'POST');
260
- assert.equal(pathOf(calls.at(-1)), '/api/cortex/guard/backup');
261
- assert.deepEqual(parseBody(calls.at(-1)), { file: 'C:/backup.sqlite' });
262
-
263
- queue.push(ok({ before: 1, after: 1, reindexed: 1 }));
264
- await client.guard('repair');
265
- assert.equal(pathOf(calls.at(-1)), '/api/cortex/guard/repair');
266
- assert.deepEqual(parseBody(calls.at(-1)), {});
267
-
268
- queue.push(ok({ total: 0, removed: [] }));
269
- await client.guard('prune', { dir: 'C:/tmp/backups', retain: 3, dryRun: true });
270
- assert.equal(pathOf(calls.at(-1)), '/api/cortex/guard/prune');
271
- assert.deepEqual(parseBody(calls.at(-1)), { dir: 'C:/tmp/backups', retain: 3, dryRun: true });
272
-
273
- queue.push(ok({ facts: [] }));
274
- await client.asOf({ subject: '用户', mode: 'historical', asOf: '2026-11-10T00:00:00Z' });
275
- assert.equal(pathOf(calls.at(-1)), '/api/cortex/as-of');
276
- assert.match(calls.at(-1).url, /subject=/);
277
- assert.match(calls.at(-1).url, /asOf=/);
278
-
279
- queue.push(ok({ ok: true }));
280
- await client.guard('validate', { file: 'C:/backup.sqlite', target: 'ignored' });
281
- assert.equal(pathOf(calls.at(-1)), '/api/cortex/guard/validate');
282
- assert.deepEqual(parseBody(calls.at(-1)), { file: 'C:/backup.sqlite' });
283
-
284
- queue.push(ok({ restored: true }));
285
- await client.guard('restore', { file: 'C:/backup.sqlite', target: 'C:/restored.sqlite' });
286
- assert.equal(pathOf(calls.at(-1)), '/api/cortex/guard/restore');
287
- assert.deepEqual(parseBody(calls.at(-1)), { file: 'C:/backup.sqlite', target: 'C:/restored.sqlite' });
288
-
289
- queue.push(ok({ root: 'C:/mirror' }));
290
- await client.guard('export', { dir: 'C:/mirror', file: 'ignored' });
291
- assert.equal(pathOf(calls.at(-1)), '/api/cortex/guard/export');
292
- assert.deepEqual(parseBody(calls.at(-1)), { dir: 'C:/mirror' });
293
-
294
- const before = calls.length;
295
- assert.throws(() => client.guard('nonsense'), /guard action/);
296
- assert.equal(calls.length, before);
297
- });
298
-
299
- test('evolve maps status, optimize, approve and rollback', async () => {
300
- const { client, calls, queue } = harness();
301
-
302
- queue.push(ok({ candidates: [] }));
303
- await client.evolve();
304
- assert.equal(calls.at(-1).init.method, 'GET');
305
- assert.equal(pathOf(calls.at(-1)), '/api/cortex/evolution');
306
-
307
- queue.push(ok({ proposals: [] }));
308
- await client.evolve('optimize');
309
- assert.equal(pathOf(calls.at(-1)), '/api/cortex/evolve/optimize');
310
- assert.deepEqual(parseBody(calls.at(-1)), {});
311
-
312
- queue.push(ok({ id: 'p1' }));
313
- await client.evolve('approve', { id: 'p1', config: { ignored: true }, rationale: 'ignored' });
314
- assert.equal(pathOf(calls.at(-1)), '/api/cortex/evolve/approve');
315
- assert.deepEqual(parseBody(calls.at(-1)), { id: 'p1' });
316
-
317
- queue.push(ok({ id: 'p0' }));
318
- await client.evolve('rollback', { kind: 'retrieval', id: 'p0', config: { ignored: true } });
319
- assert.equal(pathOf(calls.at(-1)), '/api/cortex/evolve/rollback');
320
- assert.deepEqual(parseBody(calls.at(-1)), { kind: 'retrieval', id: 'p0' });
321
-
322
- queue.push(ok({ id: 'p2' }));
323
- await client.evolve('propose', { kind: 'retrieval', config: { budget: 400 }, rationale: 'try' });
324
- assert.equal(pathOf(calls.at(-1)), '/api/cortex/evolve/propose');
325
- assert.deepEqual(parseBody(calls.at(-1)), { kind: 'retrieval', config: { budget: 400 }, rationale: 'try' });
326
-
327
- const before = calls.length;
328
- assert.throws(() => client.evolve('nonsense'), /evolve action/);
329
- assert.equal(calls.length, before);
330
- });
331
-
332
- test('working actions use list/add/update/remove and the update-based pin', async () => {
333
- const { client, calls, queue } = harness();
334
-
335
- queue.push(ok([]));
336
- await client.working();
337
- assert.equal(calls.at(-1).init.method, 'GET');
338
- assert.equal(pathOf(calls.at(-1)), '/api/cortex/working');
339
-
340
- queue.push(ok({ id: 'w1' }));
341
- await client.working('add', { text: '写测试', kind: 'focus', priority: 0.9, pinned: true, ttlMs: 60000 });
342
- assert.equal(pathOf(calls.at(-1)), '/api/cortex/working/add');
343
- assert.deepEqual(parseBody(calls.at(-1)), { text: '写测试', kind: 'focus', priority: 0.9, pinned: true, ttlMs: 60000 });
344
-
345
- queue.push(ok({ id: 'w1' }));
346
- await client.working('update', { id: 'w1', text: '改', priority: 0.4, pinned: false });
347
- assert.equal(pathOf(calls.at(-1)), '/api/cortex/working/update');
348
- assert.deepEqual(parseBody(calls.at(-1)), { id: 'w1', text: '改', priority: 0.4, pinned: false });
349
-
350
- queue.push(ok({ removed: true }));
351
- await client.working('remove', { id: 'w1' });
352
- assert.equal(pathOf(calls.at(-1)), '/api/cortex/working/remove');
353
- assert.deepEqual(parseBody(calls.at(-1)), { id: 'w1' });
354
-
355
- queue.push(ok({ id: 'w1', pinned: true }));
356
- await client.working('pin', { id: 'w1' });
357
- assert.equal(pathOf(calls.at(-1)), '/api/cortex/working/update');
358
- assert.deepEqual(parseBody(calls.at(-1)), { id: 'w1', pinned: true });
359
-
360
- queue.push(ok({ purged: 2 }));
361
- await client.working('purge');
362
- assert.equal(pathOf(calls.at(-1)), '/api/cortex/working/purge');
363
- assert.deepEqual(parseBody(calls.at(-1)), {});
364
-
365
- const before = calls.length;
366
- assert.throws(() => client.working('nonsense'), /working action/);
367
- assert.equal(calls.length, before);
368
- });
369
-
370
- test('goals actions use list/add/update/complete', async () => {
371
- const { client, calls, queue } = harness();
372
-
373
- queue.push(ok([]));
374
- await client.goals();
375
- assert.equal(calls.at(-1).init.method, 'GET');
376
- assert.equal(pathOf(calls.at(-1)), '/api/cortex/goals');
377
-
378
- queue.push(ok({ id: 'g1' }));
379
- await client.goals('add', { title: '完成客户端', description: 'd', priority: 0.8, dueAt: '2026-12-01' });
380
- assert.equal(pathOf(calls.at(-1)), '/api/cortex/goals/add');
381
- assert.deepEqual(parseBody(calls.at(-1)), { title: '完成客户端', description: 'd', priority: 0.8, dueAt: '2026-12-01' });
382
-
383
- queue.push(ok({ id: 'g1' }));
384
- await client.goals('update', { id: 'g1', progress: 0.5, status: 'active', dueAt: 'ignored' });
385
- assert.equal(pathOf(calls.at(-1)), '/api/cortex/goals/update');
386
- assert.deepEqual(parseBody(calls.at(-1)), { id: 'g1', progress: 0.5, status: 'active' });
387
-
388
- queue.push(ok({ id: 'g1', status: 'completed' }));
389
- await client.goals('complete', { id: 'g1' });
390
- assert.equal(pathOf(calls.at(-1)), '/api/cortex/goals/complete');
391
- assert.deepEqual(parseBody(calls.at(-1)), { id: 'g1' });
392
-
393
- const before = calls.length;
394
- assert.throws(() => client.goals('remove', { id: 'g1' }), /goals action/);
395
- assert.equal(calls.length, before);
396
- });
397
-
398
- test('emotion and life actions map to their routes', async () => {
399
- const { client, calls, queue } = harness();
400
-
401
- queue.push(ok({ text: '(空)', targets: [], events: [] }));
402
- await client.emotion();
403
- assert.equal(calls.at(-1).init.method, 'GET');
404
- assert.equal(pathOf(calls.at(-1)), '/api/cortex/emotion');
405
-
406
- queue.push(ok({ emotions: [] }));
407
- await client.emotion('record', { text: '有点心疼', target: '用户', traceId: 't1' });
408
- assert.equal(pathOf(calls.at(-1)), '/api/cortex/emotion/record');
409
- assert.deepEqual(parseBody(calls.at(-1)), { text: '有点心疼', target: '用户', traceId: 't1' });
410
-
411
- queue.push(ok({ diary: [], dreams: [], sleeps: [] }));
412
- await client.life();
413
- assert.equal(calls.at(-1).init.method, 'GET');
414
- assert.equal(pathOf(calls.at(-1)), '/api/cortex/life');
415
-
416
- queue.push(ok({ id: 'd1' }));
417
- await client.life('diary', { text: '今天把 API 接上了。', mood: '踏实' });
418
- assert.equal(pathOf(calls.at(-1)), '/api/cortex/life/diary');
419
- assert.deepEqual(parseBody(calls.at(-1)), { text: '今天把 API 接上了。', mood: '踏实' });
420
-
421
- queue.push(ok({ id: 'dream1' }));
422
- await client.life('dream');
423
- assert.equal(pathOf(calls.at(-1)), '/api/cortex/life/dream');
424
- assert.deepEqual(parseBody(calls.at(-1)), {});
425
-
426
- queue.push(ok({ purgedWorking: 0 }));
427
- await client.life('sleep', { windowDays: 2 });
428
- assert.equal(pathOf(calls.at(-1)), '/api/cortex/life/sleep');
429
- assert.deepEqual(parseBody(calls.at(-1)), { windowDays: 2 });
430
- });
431
-
432
- test('non-2xx responses become ApiError with status, code and context', async () => {
433
- const { client, queue } = harness();
434
- queue.push(json({ ok: false, error: 'not found' }, { status: 404 }));
435
-
436
- await assert.rejects(client.trace('missing'), (error) => {
437
- assert.ok(error instanceof ApiError);
438
- assert.equal(error.name, 'ApiError');
439
- assert.equal(error.status, 404);
440
- assert.equal(error.code, 'HTTP_404');
441
- assert.equal(error.message, 'not found');
442
- assert.equal(error.method, 'GET');
443
- assert.match(error.url, /\/api\/cortex\/trace\?id=missing$/);
444
- assert.deepEqual(error.payload, { ok: false, error: 'not found' });
445
- return true;
446
- });
447
- });
448
-
449
- test('400/403 responses keep their server messages and codes', async () => {
450
- const { client, queue } = harness();
451
-
452
- queue.push(json({ ok: false, error: 'text must be a string' }, { status: 400 }));
453
- await assert.rejects(client.write({ text: 7 }), (error) => {
454
- assert.equal(error.status, 400);
455
- assert.equal(error.code, 'HTTP_400');
456
- assert.equal(error.message, 'text must be a string');
457
- return true;
458
- });
459
-
460
- queue.push(json({ ok: false, error: 'missing x-cortex header' }, { status: 403 }));
461
- await assert.rejects(client.write({ text: 'x' }), (error) => {
462
- assert.equal(error.status, 403);
463
- assert.equal(error.code, 'HTTP_403');
464
- assert.equal(error.message, 'missing x-cortex header');
465
- return true;
466
- });
467
- });
468
-
469
- test('an ok:false envelope on HTTP 200 is still an error', async () => {
470
- const { client, queue } = harness();
471
- queue.push(json({ ok: false, error: 'engine refused' }, { status: 200 }));
472
-
473
- await assert.rejects(client.health(), (error) => {
474
- assert.ok(error instanceof ApiError);
475
- assert.equal(error.status, 200);
476
- assert.equal(error.code, 'API_ERROR');
477
- assert.equal(error.message, 'engine refused');
478
- return true;
479
- });
480
- });
481
-
482
- test('non-JSON and empty error bodies produce HTTP_status errors', async () => {
483
- const { client, queue } = harness();
484
- queue.push(new Response('boom', { status: 500, headers: { 'content-type': 'text/plain' } }));
485
-
486
- await assert.rejects(client.health(), (error) => {
487
- assert.ok(error instanceof ApiError);
488
- assert.equal(error.status, 500);
489
- assert.equal(error.code, 'HTTP_500');
490
- assert.match(error.message, /HTTP 500/);
491
- return true;
492
- });
493
- });
494
-
495
- test('network failures are wrapped as NETWORK_ERROR with cause', async () => {
496
- const { client, queue } = harness();
497
- const cause = new Error('socket closed');
498
- queue.push(cause);
499
-
500
- await assert.rejects(client.health(), (error) => {
501
- assert.ok(error instanceof ApiError);
502
- assert.equal(error.status, 0);
503
- assert.equal(error.code, 'NETWORK_ERROR');
504
- assert.match(error.message, /socket closed/);
505
- assert.equal(error.cause, cause);
506
- return true;
507
- });
508
- });
509
-
510
- test('invalid JSON on a successful response is INVALID_JSON', async () => {
511
- const { client, queue } = harness();
512
- queue.push(new Response('not json', { status: 200 }));
513
-
514
- await assert.rejects(client.health(), (error) => {
515
- assert.ok(error instanceof ApiError);
516
- assert.equal(error.status, 200);
517
- assert.equal(error.code, 'INVALID_JSON');
518
- return true;
519
- });
520
- });
521
-
522
- test('204 responses resolve to null without parsing a body', async () => {
523
- const { client, queue } = harness();
524
- queue.push(new Response(null, { status: 204 }));
525
-
526
- assert.equal(await client.health(), null);
527
- });
528
-
529
- test('tick and digest use the real HTTP routes', async () => {
530
- const { client, calls, queue } = harness();
531
-
532
- queue.push(ok({ executed: [], proposals: [] }));
533
- await client.tick({ max: 3 });
534
- assert.equal(calls.at(-1).init.method, 'POST');
535
- assert.equal(pathOf(calls.at(-1)), '/api/cortex/tick');
536
- assert.deepEqual(parseBody(calls.at(-1)), { max: 3 });
537
-
538
- queue.push(ok({ date: '2026-11-11', counts: { traces: 1 } }));
539
- await client.digest({ date: '2026-11-11' });
540
- assert.equal(calls.at(-1).init.method, 'GET');
541
- assert.equal(pathOf(calls.at(-1)), '/api/cortex/digest');
542
- assert.match(calls.at(-1).url, /[?&]date=2026-11-11/);
543
- });
544
-
545
- test('local misuse fails fast with TypeError before fetch', async () => {
546
- const { client, calls } = harness();
547
-
548
- assert.throws(() => client.search(42), /query must be a string/);
549
- assert.throws(() => client.trace(''), /trace id/);
550
- assert.throws(() => client.write(null), /write input/);
551
- assert.throws(() => client.writeMany({ items: [] }), /must be an array/);
552
- assert.throws(() => client.feedback(null), /feedback input/);
553
- assert.throws(() => client.forget(null), /forget input/);
554
- assert.throws(() => client.reflect(null), /reflect input/);
555
- assert.throws(() => client.working('list', null), /working options/);
556
- assert.throws(() => client.guard('backup', null), /guard options/);
557
- assert.throws(() => client.evolve('approve', null), /evolve options/);
558
- assert.throws(() => client.emotion('record', null), /emotion options/);
559
- assert.throws(() => client.life('sleep', null), /life options/);
560
- assert.throws(() => client.goals('list', null), /goals options/);
561
- assert.throws(() => client.compress(null), /compress options/);
562
- assert.equal(calls.length, 0);
563
- });
564
-
565
- test('envelope unwrapping handles null and primitive data values', async () => {
566
- const { client, queue } = harness();
567
-
568
- queue.push(json({ ok: true, data: null }));
569
- assert.equal(await client.health(), null);
570
-
571
- queue.push(json({ ok: true, data: 0 }));
572
- assert.equal(await client.health(), 0);
573
-
574
- queue.push(json({ ok: true, data: false }));
575
- assert.equal(await client.health(), false);
576
- });
@@ -1,19 +0,0 @@
1
- import test from 'node:test';
2
- import assert from 'node:assert/strict';
3
- import { validateWrite, validateFeedback, validateForget, validateSearch } from '../../src/api/validate.js';
4
-
5
- test('write validation accepts and normalizes a body', () => {
6
- const value = validateWrite({ text: ' 用户喜欢米饭。 ', tags: [' 米饭 ', ''], importance: 0.8, explicit: false });
7
- assert.equal(value.text, '用户喜欢米饭。');
8
- assert.deepEqual(value.tags, ['米饭']);
9
- assert.equal(value.importance, 0.8);
10
- assert.equal(value.explicit, false);
11
- });
12
-
13
- test('invalid bodies are rejected', () => {
14
- assert.throws(() => validateWrite({ text: '' }), /text/);
15
- assert.throws(() => validateWrite({ text: 'x', importance: 2 }), /importance/);
16
- assert.throws(() => validateSearch({ query: 'x', limit: 99 }), /limit/);
17
- assert.throws(() => validateFeedback({ id: 't', signal: '' }), /signal/);
18
- assert.throws(() => validateForget({ id: 't', mode: 'nuke' }), /mode/);
19
- });