dsh-cortex-memory 0.4.146 → 0.4.147
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +2396 -2369
- package/README.md +8 -6
- package/bench/semantic-cn.js +4 -4
- package/bench/synthetic.js +2 -2
- package/bin/copy-db.mjs +1 -1
- package/client.js +1 -1
- package/docs/quota-compaction-design.md +2 -2
- package/icon.svg +12 -2
- package/package.json +1 -1
- package/src/engines/consolidate.js +2 -2
- package/src/engines/health.js +1 -1
- package/src/engines/life.js +2 -2
- package/src/engines/narrative.js +2 -2
- package/src/host/scheduler.js +1 -1
- package/src/store/repositories/traces.js +1 -1
- package/test/integration/_helpers.js +0 -128
- package/test/integration/acceptance.test.js +0 -803
- package/test/integration/ann-load.test.js +0 -61
- package/test/integration/api-contract.test.js +0 -1476
- package/test/integration/backup-privacy.test.js +0 -214
- package/test/integration/consolidate.test.js +0 -79
- package/test/integration/edge-cases.test.js +0 -1089
- package/test/integration/exchange.test.js +0 -326
- package/test/integration/life-loop.test.js +0 -189
- package/test/integration/load.test.js +0 -751
- package/test/integration/markdown-sync.test.js +0 -35
- package/test/integration/policy-query-api.test.js +0 -306
- package/test/integration/resilience-writes.test.js +0 -241
- package/test/integration/tools-contract.test.js +0 -1324
- package/test/unit/alias.test.js +0 -87
- package/test/unit/ann-edge.test.js +0 -102
- package/test/unit/ann.test.js +0 -77
- package/test/unit/api-client.test.js +0 -576
- package/test/unit/api-validate.test.js +0 -19
- package/test/unit/api.test.js +0 -555
- package/test/unit/appraisal.test.js +0 -48
- package/test/unit/attachments.test.js +0 -33
- package/test/unit/auto-reflect.test.js +0 -141
- package/test/unit/autonomy.test.js +0 -53
- package/test/unit/batch.test.js +0 -46
- package/test/unit/bench.test.js +0 -40
- package/test/unit/benchmark-synthetic.test.js +0 -52
- package/test/unit/capture-filter.test.js +0 -296
- package/test/unit/cli-default-root.test.js +0 -21
- package/test/unit/client-contract.test.js +0 -32
- package/test/unit/compaction-preview.test.js +0 -91
- package/test/unit/compression.test.js +0 -76
- package/test/unit/consolidate-dedupe.test.js +0 -174
- package/test/unit/consolidate-errors.test.js +0 -63
- package/test/unit/consolidate-gate.test.js +0 -26
- package/test/unit/consolidate-preview.test.js +0 -168
- package/test/unit/consolidate-protection.test.js +0 -156
- package/test/unit/consolidate-reason.test.js +0 -206
- package/test/unit/consolidate-retry.test.js +0 -91
- package/test/unit/consolidate-selection.test.js +0 -103
- package/test/unit/core.test.js +0 -77
- package/test/unit/coverage-rewrite.test.js +0 -38
- package/test/unit/dedupe-episodes.test.js +0 -34
- package/test/unit/dedupe-threshold.test.js +0 -14
- package/test/unit/develop.test.js +0 -130
- package/test/unit/development.test.js +0 -68
- package/test/unit/digest.test.js +0 -26
- package/test/unit/dsr.test.js +0 -111
- package/test/unit/edges-candidates.test.js +0 -113
- package/test/unit/emotion.test.js +0 -62
- package/test/unit/encode.test.js +0 -98
- package/test/unit/evolution.test.js +0 -130
- package/test/unit/external-ann-search.test.js +0 -50
- package/test/unit/external-ann.test.js +0 -29
- package/test/unit/extract-question.test.js +0 -41
- package/test/unit/fact-guard.test.js +0 -51
- package/test/unit/fact-noise-llm.test.js +0 -44
- package/test/unit/facts-belief.test.js +0 -32
- package/test/unit/facts-compat.test.js +0 -62
- package/test/unit/facts.test.js +0 -107
- package/test/unit/feature-overrides.test.js +0 -42
- package/test/unit/fts-migration.test.js +0 -42
- package/test/unit/graph.test.js +0 -79
- package/test/unit/growth-reflect.test.js +0 -124
- package/test/unit/guard-runtime.test.js +0 -40
- package/test/unit/guardian.test.js +0 -91
- package/test/unit/health-consolidation.test.js +0 -52
- package/test/unit/health-fts-scope.test.js +0 -33
- package/test/unit/health-privacy.test.js +0 -102
- package/test/unit/health-summaries.test.js +0 -24
- package/test/unit/health-text-maintain.test.js +0 -33
- package/test/unit/health-today.test.js +0 -45
- package/test/unit/hnsw.test.js +0 -35
- package/test/unit/host-runtime.test.js +0 -142
- package/test/unit/host.test.js +0 -58
- package/test/unit/identity.test.js +0 -98
- package/test/unit/induct-evaluate.test.js +0 -41
- package/test/unit/ingest-extracted.test.js +0 -35
- package/test/unit/inhibition.test.js +0 -90
- package/test/unit/initiative.test.js +0 -90
- package/test/unit/jobs.test.js +0 -93
- package/test/unit/json-snapshot.test.js +0 -57
- package/test/unit/label-entities-skip.test.js +0 -18
- package/test/unit/life-consolidate.test.js +0 -34
- package/test/unit/life.test.js +0 -149
- package/test/unit/llm-facts-integration.test.js +0 -27
- package/test/unit/llm-facts.test.js +0 -16
- package/test/unit/llm.test.js +0 -45
- package/test/unit/locomo.test.js +0 -45
- package/test/unit/longmemeval.test.js +0 -56
- package/test/unit/maintain-dispute.test.js +0 -62
- package/test/unit/maintain-duration.test.js +0 -194
- package/test/unit/markdown-import.test.js +0 -44
- package/test/unit/markdown.test.js +0 -34
- package/test/unit/memory-edit.test.js +0 -112
- package/test/unit/metrics.test.js +0 -26
- package/test/unit/migration.test.js +0 -38
- package/test/unit/model-config.test.js +0 -52
- package/test/unit/mood-saturation.test.js +0 -33
- package/test/unit/narrative-weave.test.js +0 -271
- package/test/unit/narrative.test.js +0 -62
- package/test/unit/playbook-consolidate.test.js +0 -24
- package/test/unit/privacy-scope.test.js +0 -39
- package/test/unit/profile-paths.test.js +0 -21
- package/test/unit/profile.test.js +0 -26
- package/test/unit/prompt.test.js +0 -42
- package/test/unit/providers.test.js +0 -42
- package/test/unit/qa.test.js +0 -100
- package/test/unit/query.test.js +0 -53
- package/test/unit/reflect-backfill.test.js +0 -158
- package/test/unit/reflect-coverage.test.js +0 -35
- package/test/unit/reflect-overview.test.js +0 -22
- package/test/unit/reflect-phrasing.test.js +0 -75
- package/test/unit/reflect-sampling.test.js +0 -80
- package/test/unit/reflect-source-budget.test.js +0 -24
- package/test/unit/relationship-dynamics.test.js +0 -29
- package/test/unit/retention.test.js +0 -31
- package/test/unit/retrieval-ranking.test.js +0 -31
- package/test/unit/retrieve.test.js +0 -117
- package/test/unit/revive-on-read.test.js +0 -84
- package/test/unit/rotation.test.js +0 -25
- package/test/unit/scanner.test.js +0 -50
- package/test/unit/scenario.test.js +0 -78
- package/test/unit/scheduler-autoconsolidate.test.js +0 -95
- package/test/unit/scheduler-autodedupe.test.js +0 -52
- package/test/unit/scheduler-autosleep.test.js +0 -50
- package/test/unit/scheduler-cadence.test.js +0 -33
- package/test/unit/scheduler-deep.test.js +0 -62
- package/test/unit/scheduler-tick-gate.test.js +0 -38
- package/test/unit/scheduler.test.js +0 -137
- package/test/unit/self-state.test.js +0 -94
- package/test/unit/service.test.js +0 -72
- package/test/unit/shiori-import.test.js +0 -77
- package/test/unit/skills.test.js +0 -104
- package/test/unit/spacing.test.js +0 -14
- package/test/unit/store.test.js +0 -111
- package/test/unit/summon-left-slot.test.js +0 -51
- package/test/unit/summon-rank.test.js +0 -51
- package/test/unit/temporal-graph.test.js +0 -44
- package/test/unit/tiers-gist.test.js +0 -140
- package/test/unit/tiers.test.js +0 -39
- package/test/unit/tokenizer-fts.test.js +0 -35
- package/test/unit/tools.test.js +0 -122
- package/test/unit/upgrade-0412.test.js +0 -104
- package/test/unit/upgrade-0413.test.js +0 -84
- package/test/unit/upgrade-0414.test.js +0 -101
- package/test/unit/vector-ann.test.js +0 -53
- package/test/unit/vector-async.test.js +0 -69
- package/test/unit/vector-coverage.test.js +0 -34
- package/test/unit/vector-override.test.js +0 -37
- package/test/unit/vector-typed-array.test.js +0 -47
- package/test/unit/vector.test.js +0 -68
- package/test/unit/working.test.js +0 -99
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import test from 'node:test';
|
|
2
|
-
import assert from 'node:assert/strict';
|
|
3
|
-
import { mkdtempSync, readFileSync, rmSync, writeFileSync } 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('markdown sync merges a human edit by id instead of creating a duplicate', () => {
|
|
9
|
-
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-md-sync-'));
|
|
10
|
-
const vault = path.join(dir, 'vault');
|
|
11
|
-
const cortex = createCortex({ config: { dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite') } });
|
|
12
|
-
try {
|
|
13
|
-
const written = cortex.write({ text: '一条测试记忆。', title: '原题', explicit: true });
|
|
14
|
-
const id = written.trace.id;
|
|
15
|
-
cortex.exportMarkdown(vault);
|
|
16
|
-
const file = path.join(vault, 'MEMORY.md');
|
|
17
|
-
const before = readFileSync(file, 'utf8');
|
|
18
|
-
assert.match(before, new RegExp(id));
|
|
19
|
-
const edited = before.replace('一条测试记忆。', '人工改过的一条记忆。');
|
|
20
|
-
writeFileSync(file, edited, 'utf8');
|
|
21
|
-
|
|
22
|
-
const dry = cortex.syncMarkdown(vault, { dryRun: true });
|
|
23
|
-
assert.equal(dry.updated, 1);
|
|
24
|
-
assert.equal(cortex.get(id).content, '一条测试记忆。');
|
|
25
|
-
|
|
26
|
-
const result = cortex.syncMarkdown(vault);
|
|
27
|
-
assert.equal(result.updated, 1);
|
|
28
|
-
assert.equal(result.created, 0);
|
|
29
|
-
assert.equal(cortex.get(id).content, '人工改过的一条记忆。');
|
|
30
|
-
assert.equal(cortex.repos.traces.list({ scopeId: 'global', limit: 10 }).length, 1);
|
|
31
|
-
} finally {
|
|
32
|
-
cortex.close();
|
|
33
|
-
rmSync(dir, { recursive: true, force: true });
|
|
34
|
-
}
|
|
35
|
-
});
|
|
@@ -1,306 +0,0 @@
|
|
|
1
|
-
import test from 'node:test';
|
|
2
|
-
import assert from 'node:assert/strict';
|
|
3
|
-
import { createApiHandler } from '../../src/api/routes.js';
|
|
4
|
-
import { BASE_MS, DAY_MS, cleanup, invokeApi, makeCortex } from './_helpers.js';
|
|
5
|
-
|
|
6
|
-
function idsOf(result) {
|
|
7
|
-
return result.items.map((item) => item.id).sort();
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
test('policy propose -> approve -> live retrieval budget -> rollback', () => {
|
|
11
|
-
const context = makeCortex();
|
|
12
|
-
const { cortex } = context;
|
|
13
|
-
try {
|
|
14
|
-
// No policy is active yet: the request budget wins.
|
|
15
|
-
assert.equal(cortex.policies.active('retrieval'), null);
|
|
16
|
-
assert.equal(cortex.search('anything', { budget: 9_999 }).budget.total, 9_999);
|
|
17
|
-
|
|
18
|
-
const tight = cortex.evolution.propose({
|
|
19
|
-
kind: 'retrieval',
|
|
20
|
-
config: { budget: 300 },
|
|
21
|
-
rationale: 'first policy',
|
|
22
|
-
metrics: { note: 'integration' },
|
|
23
|
-
});
|
|
24
|
-
const roomy = cortex.evolution.propose({
|
|
25
|
-
kind: 'retrieval',
|
|
26
|
-
config: { budget: 900 },
|
|
27
|
-
rationale: 'second policy',
|
|
28
|
-
});
|
|
29
|
-
assert.equal(tight.status, 'candidate');
|
|
30
|
-
assert.equal(tight.version, 1);
|
|
31
|
-
assert.equal(roomy.status, 'candidate');
|
|
32
|
-
assert.equal(roomy.version, 2);
|
|
33
|
-
assert.equal(cortex.policies.list({ kind: 'retrieval' }).length, 2);
|
|
34
|
-
assert.equal(cortex.policies.active('retrieval'), null);
|
|
35
|
-
|
|
36
|
-
const activatedTight = cortex.evolution.approve(tight.id);
|
|
37
|
-
assert.equal(activatedTight.id, tight.id);
|
|
38
|
-
assert.equal(activatedTight.status, 'active');
|
|
39
|
-
assert.ok(Number.isFinite(Date.parse(activatedTight.activatedAt)));
|
|
40
|
-
assert.equal(cortex.search('anything', { budget: 9_999 }).budget.total, 300);
|
|
41
|
-
assert.equal(cortex.evolution.status().active.retrieval.id, tight.id);
|
|
42
|
-
|
|
43
|
-
const activatedRoomy = cortex.evolution.approve(roomy.id);
|
|
44
|
-
assert.equal(activatedRoomy.status, 'active');
|
|
45
|
-
assert.equal(cortex.policies.get(tight.id).status, 'superseded');
|
|
46
|
-
assert.equal(cortex.search('anything', { budget: 9_999 }).budget.total, 900);
|
|
47
|
-
|
|
48
|
-
// Rollback by explicit target restores the earlier version.
|
|
49
|
-
const rolledBack = cortex.evolution.rollback('retrieval', tight.id);
|
|
50
|
-
assert.equal(rolledBack.id, tight.id);
|
|
51
|
-
assert.equal(rolledBack.status, 'active');
|
|
52
|
-
assert.equal(cortex.policies.get(roomy.id).status, 'superseded');
|
|
53
|
-
assert.equal(cortex.search('anything', { budget: 9_999 }).budget.total, 300);
|
|
54
|
-
|
|
55
|
-
// Rollback without a target picks the newest non-active version.
|
|
56
|
-
const implicitRollback = cortex.evolution.rollback('retrieval');
|
|
57
|
-
assert.equal(implicitRollback.id, roomy.id);
|
|
58
|
-
assert.equal(cortex.search('anything', { budget: 9_999 }).budget.total, 900);
|
|
59
|
-
const status = cortex.evolution.status();
|
|
60
|
-
assert.equal(status.active.retrieval.id, roomy.id);
|
|
61
|
-
assert.equal(status.active.maintenance, null);
|
|
62
|
-
|
|
63
|
-
const audit = cortex.repos.audit.list({ limit: 50 });
|
|
64
|
-
const actions = audit.map((entry) => entry.action);
|
|
65
|
-
assert.ok(actions.includes('policy.propose'));
|
|
66
|
-
assert.ok(actions.includes('policy.activate'));
|
|
67
|
-
assert.ok(actions.includes('policy.rollback'));
|
|
68
|
-
assert.equal(audit.filter((entry) => entry.action === 'policy.propose').length, 2);
|
|
69
|
-
|
|
70
|
-
assert.equal(cortex.guardian.check().ok, true);
|
|
71
|
-
} finally {
|
|
72
|
-
cleanup(context);
|
|
73
|
-
}
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
test('query filter syntax (kind:, tag:, after:/before:, importance:>) flows through cortex.search', () => {
|
|
77
|
-
const context = makeCortex();
|
|
78
|
-
const { cortex } = context;
|
|
79
|
-
try {
|
|
80
|
-
const oldDecision = cortex.write({
|
|
81
|
-
text: '过滤测试:重要的决定。',
|
|
82
|
-
kind: 'decision',
|
|
83
|
-
tags: ['过滤', '旧'],
|
|
84
|
-
importance: 0.6,
|
|
85
|
-
explicit: true,
|
|
86
|
-
now: BASE_MS - 40 * DAY_MS,
|
|
87
|
-
});
|
|
88
|
-
const recentChat = cortex.write({
|
|
89
|
-
text: '过滤测试:普通对话。',
|
|
90
|
-
kind: 'utterance',
|
|
91
|
-
tags: ['过滤'],
|
|
92
|
-
importance: 0.9,
|
|
93
|
-
explicit: true,
|
|
94
|
-
now: BASE_MS - 2 * DAY_MS,
|
|
95
|
-
});
|
|
96
|
-
const newDecision = cortex.write({
|
|
97
|
-
text: '过滤测试:另一个决定。',
|
|
98
|
-
kind: 'decision',
|
|
99
|
-
tags: ['过滤'],
|
|
100
|
-
importance: 0.6,
|
|
101
|
-
explicit: true,
|
|
102
|
-
now: BASE_MS - DAY_MS,
|
|
103
|
-
});
|
|
104
|
-
const all = [oldDecision.trace.id, recentChat.trace.id, newDecision.trace.id];
|
|
105
|
-
|
|
106
|
-
const baseline = cortex.search('过滤测试', { now: BASE_MS, limit: 10 });
|
|
107
|
-
assert.deepEqual(idsOf(baseline), [...all].sort());
|
|
108
|
-
assert.equal(baseline.query, '过滤测试');
|
|
109
|
-
assert.equal(baseline.intent, 'fact');
|
|
110
|
-
|
|
111
|
-
const byKind = cortex.search('kind:decision 过滤测试', { now: BASE_MS, limit: 10 });
|
|
112
|
-
assert.deepEqual(idsOf(byKind), [oldDecision.trace.id, newDecision.trace.id].sort());
|
|
113
|
-
assert.equal(byKind.query, '过滤测试'); // the filter token is not free text
|
|
114
|
-
|
|
115
|
-
const byTag = cortex.search('tag:旧 过滤测试', { now: BASE_MS, limit: 10 });
|
|
116
|
-
assert.deepEqual(idsOf(byTag), [oldDecision.trace.id]);
|
|
117
|
-
const byBothTags = cortex.search('tag:过滤 tag:旧 过滤测试', { now: BASE_MS, limit: 10 });
|
|
118
|
-
assert.deepEqual(idsOf(byBothTags), [oldDecision.trace.id]);
|
|
119
|
-
const missingTag = cortex.search('tag:不存在 过滤测试', { now: BASE_MS, limit: 10 });
|
|
120
|
-
assert.equal(missingTag.items.length, 0);
|
|
121
|
-
|
|
122
|
-
const after = cortex.search('after:2026-11-01 过滤测试', { now: BASE_MS, limit: 10 });
|
|
123
|
-
assert.deepEqual(idsOf(after), [recentChat.trace.id, newDecision.trace.id].sort());
|
|
124
|
-
const before = cortex.search('before:2026-11-05 过滤测试', { now: BASE_MS, limit: 10 });
|
|
125
|
-
assert.deepEqual(idsOf(before), [oldDecision.trace.id]);
|
|
126
|
-
|
|
127
|
-
const important = cortex.search('importance:>0.8 过滤测试', { now: BASE_MS, limit: 10 });
|
|
128
|
-
assert.deepEqual(idsOf(important), [recentChat.trace.id]);
|
|
129
|
-
const lessImportant = cortex.search('importance:<=0.8 过滤测试', { now: BASE_MS, limit: 10 });
|
|
130
|
-
assert.deepEqual(idsOf(lessImportant), [oldDecision.trace.id, newDecision.trace.id].sort());
|
|
131
|
-
|
|
132
|
-
const combined = cortex.search('kind:decision tag:过滤 after:2026-11-01 过滤测试', { now: BASE_MS, limit: 10 });
|
|
133
|
-
assert.deepEqual(idsOf(combined), [newDecision.trace.id]);
|
|
134
|
-
|
|
135
|
-
const noMatch = cortex.search('kind:reflection 过滤测试', { now: BASE_MS, limit: 10 });
|
|
136
|
-
assert.equal(noMatch.items.length, 0);
|
|
137
|
-
const invalidImportance = cortex.search('importance:>abc 过滤测试', { now: BASE_MS, limit: 10 });
|
|
138
|
-
assert.equal(invalidImportance.items.length, 0);
|
|
139
|
-
|
|
140
|
-
// Unknown prefixes are not filters and stay in the free-text query.
|
|
141
|
-
const unknown = cortex.search('unknown:value 过滤测试', { now: BASE_MS, limit: 10 });
|
|
142
|
-
assert.equal(unknown.query, 'unknown:value 过滤测试');
|
|
143
|
-
assert.ok(unknown.items.length >= 1);
|
|
144
|
-
|
|
145
|
-
// state: combines with includeArchived instead of hiding the fact that an
|
|
146
|
-
// archived trace is still in the database.
|
|
147
|
-
assert.equal(cortex.forget({ traceId: oldDecision.trace.id, mode: 'archive' }).removed, true);
|
|
148
|
-
assert.equal(cortex.get(oldDecision.trace.id).state, 'archived');
|
|
149
|
-
const activeOnly = cortex.search('state:archived 过滤测试', { now: BASE_MS, limit: 10 });
|
|
150
|
-
assert.equal(activeOnly.items.length, 0);
|
|
151
|
-
const withArchived = cortex.search('state:archived 过滤测试', { now: BASE_MS, limit: 10, includeArchived: true });
|
|
152
|
-
assert.deepEqual(idsOf(withArchived), [oldDecision.trace.id]);
|
|
153
|
-
|
|
154
|
-
assert.equal(cortex.guardian.check().ok, true);
|
|
155
|
-
} finally {
|
|
156
|
-
cleanup(context);
|
|
157
|
-
}
|
|
158
|
-
});
|
|
159
|
-
|
|
160
|
-
test('API security: loopback same-origin allowed, cross-origin and unsafe POSTs refused', async () => {
|
|
161
|
-
const context = makeCortex();
|
|
162
|
-
const { cortex } = context;
|
|
163
|
-
try {
|
|
164
|
-
const handler = createApiHandler({ cortex });
|
|
165
|
-
|
|
166
|
-
const ping = await invokeApi(handler);
|
|
167
|
-
assert.equal(ping.status, 200);
|
|
168
|
-
assert.equal(ping.json.ok, true);
|
|
169
|
-
assert.ok(Number.isFinite(Date.parse(ping.json.at)));
|
|
170
|
-
assert.match(ping.headers['content-type'], /application\/json/);
|
|
171
|
-
|
|
172
|
-
const head = await invokeApi(handler, { method: 'HEAD', route: '/ping' });
|
|
173
|
-
assert.equal(head.status, 200);
|
|
174
|
-
assert.equal(head.json.ok, true);
|
|
175
|
-
|
|
176
|
-
const health = await invokeApi(handler, { route: '/health' });
|
|
177
|
-
assert.equal(health.status, 200);
|
|
178
|
-
assert.ok(health.json.data.score > 0);
|
|
179
|
-
|
|
180
|
-
const missingTrace = await invokeApi(handler, { route: '/trace?id=trc_missing' });
|
|
181
|
-
assert.equal(missingTrace.status, 404);
|
|
182
|
-
assert.equal(missingTrace.json.ok, false);
|
|
183
|
-
|
|
184
|
-
// Browser cross-origin requests are refused even on loopback.
|
|
185
|
-
const crossOrigin = await invokeApi(handler, {
|
|
186
|
-
route: '/ping',
|
|
187
|
-
headers: { origin: 'http://evil.example', 'sec-fetch-site': 'cross-site' },
|
|
188
|
-
});
|
|
189
|
-
assert.equal(crossOrigin.status, 403);
|
|
190
|
-
assert.equal(crossOrigin.json.error, 'forbidden');
|
|
191
|
-
|
|
192
|
-
const sameSite = await invokeApi(handler, { route: '/ping', site: 'same-site' });
|
|
193
|
-
assert.equal(sameSite.status, 403);
|
|
194
|
-
|
|
195
|
-
// Non-loopback peer addresses are refused regardless of headers.
|
|
196
|
-
const remote = await invokeApi(handler, { route: '/ping', remoteAddress: '203.0.113.7' });
|
|
197
|
-
assert.equal(remote.status, 403);
|
|
198
|
-
|
|
199
|
-
// A loopback-looking Host header without an origin is accepted; a
|
|
200
|
-
// non-loopback Host header is not.
|
|
201
|
-
const noOrigin = await invokeApi(handler, { route: '/ping', origin: undefined });
|
|
202
|
-
assert.equal(noOrigin.status, 200);
|
|
203
|
-
const badHost = await invokeApi(handler, { route: '/ping', headers: { host: 'evil.example' } });
|
|
204
|
-
assert.equal(badHost.status, 403);
|
|
205
|
-
const missingHost = await invokeApi(handler, { route: '/ping', headers: { host: undefined } });
|
|
206
|
-
assert.equal(missingHost.status, 403);
|
|
207
|
-
|
|
208
|
-
// POST needs the CSRF-style header before anything is read or written.
|
|
209
|
-
const noHeader = await invokeApi(handler, {
|
|
210
|
-
method: 'POST',
|
|
211
|
-
route: '/write',
|
|
212
|
-
body: { text: '没有 x-cortex 的写入。', explicit: true },
|
|
213
|
-
});
|
|
214
|
-
assert.equal(noHeader.status, 403);
|
|
215
|
-
assert.equal(noHeader.json.error, 'missing x-cortex header');
|
|
216
|
-
assert.equal(cortex.repos.traces.count(), 0);
|
|
217
|
-
|
|
218
|
-
const crossPost = await invokeApi(handler, {
|
|
219
|
-
method: 'POST',
|
|
220
|
-
route: '/write',
|
|
221
|
-
body: { text: '跨域写入。', explicit: true },
|
|
222
|
-
headers: { 'x-cortex': '1', origin: 'http://evil.example', 'sec-fetch-site': 'cross-site' },
|
|
223
|
-
});
|
|
224
|
-
assert.equal(crossPost.status, 403);
|
|
225
|
-
assert.equal(cortex.repos.traces.count(), 0);
|
|
226
|
-
|
|
227
|
-
// A proper same-origin POST writes, and the trace is then readable by GET.
|
|
228
|
-
const write = await invokeApi(handler, {
|
|
229
|
-
method: 'POST',
|
|
230
|
-
route: '/write',
|
|
231
|
-
body: { text: '用户说会买米饭。', title: '米饭', explicit: true },
|
|
232
|
-
headers: { 'x-cortex': '1' },
|
|
233
|
-
});
|
|
234
|
-
assert.equal(write.status, 200);
|
|
235
|
-
assert.equal(write.json.ok, true);
|
|
236
|
-
assert.equal(write.json.data.skipped, false);
|
|
237
|
-
const traceId = write.json.data.trace.id;
|
|
238
|
-
assert.ok(traceId);
|
|
239
|
-
|
|
240
|
-
const trace = await invokeApi(handler, { route: `/trace?id=${traceId}` });
|
|
241
|
-
assert.equal(trace.status, 200);
|
|
242
|
-
assert.equal(trace.json.data.id, traceId);
|
|
243
|
-
assert.equal(trace.json.data.content, '用户说会买米饭。');
|
|
244
|
-
assert.equal(trace.json.data.title, '米饭');
|
|
245
|
-
|
|
246
|
-
const search = await invokeApi(handler, { route: '/search?q=米饭' });
|
|
247
|
-
assert.equal(search.status, 200);
|
|
248
|
-
assert.ok(search.json.data.items.some((item) => item.id === traceId));
|
|
249
|
-
|
|
250
|
-
// Body validation returns 400 rather than a corrupt write.
|
|
251
|
-
const invalidWrite = await invokeApi(handler, {
|
|
252
|
-
method: 'POST',
|
|
253
|
-
route: '/write',
|
|
254
|
-
body: { title: '没有正文' },
|
|
255
|
-
headers: { 'x-cortex': '1' },
|
|
256
|
-
});
|
|
257
|
-
assert.equal(invalidWrite.status, 400);
|
|
258
|
-
assert.match(invalidWrite.json.error, /text/);
|
|
259
|
-
assert.equal(cortex.repos.traces.count(), 1);
|
|
260
|
-
|
|
261
|
-
const invalidSearch = await invokeApi(handler, {
|
|
262
|
-
method: 'POST',
|
|
263
|
-
route: '/search',
|
|
264
|
-
body: { limit: 5 },
|
|
265
|
-
headers: { 'x-cortex': '1' },
|
|
266
|
-
});
|
|
267
|
-
assert.equal(invalidSearch.status, 400);
|
|
268
|
-
|
|
269
|
-
// Batch writes are validated per item and remain idempotent through the API.
|
|
270
|
-
const batchBody = {
|
|
271
|
-
items: [
|
|
272
|
-
{ text: '批量 API 第一条。', explicit: true, operationId: 'api-batch:1' },
|
|
273
|
-
{ text: '批量 API 第二条。', explicit: true, operationId: 'api-batch:2' },
|
|
274
|
-
],
|
|
275
|
-
};
|
|
276
|
-
const batch = await invokeApi(handler, {
|
|
277
|
-
method: 'POST',
|
|
278
|
-
route: '/write',
|
|
279
|
-
body: batchBody,
|
|
280
|
-
headers: { 'x-cortex': '1' },
|
|
281
|
-
});
|
|
282
|
-
assert.equal(batch.status, 200);
|
|
283
|
-
assert.equal(batch.json.data.length, 2);
|
|
284
|
-
assert.equal(cortex.repos.traces.count(), 3);
|
|
285
|
-
const replayedBatch = await invokeApi(handler, {
|
|
286
|
-
method: 'POST',
|
|
287
|
-
route: '/write',
|
|
288
|
-
body: batchBody,
|
|
289
|
-
headers: { 'x-cortex': '1' },
|
|
290
|
-
});
|
|
291
|
-
assert.equal(replayedBatch.status, 200);
|
|
292
|
-
assert.equal(replayedBatch.json.data.every((item) => item.skipped === true), true);
|
|
293
|
-
assert.equal(cortex.repos.traces.count(), 3);
|
|
294
|
-
|
|
295
|
-
// Unsupported methods are rejected with an Allow header.
|
|
296
|
-
const method = await invokeApi(handler, { method: 'DELETE', route: '/write' });
|
|
297
|
-
assert.equal(method.status, 405);
|
|
298
|
-
assert.equal(method.headers.allow.includes('GET'), true);
|
|
299
|
-
|
|
300
|
-
const guard = await invokeApi(handler, { route: '/guard' });
|
|
301
|
-
assert.equal(guard.status, 200);
|
|
302
|
-
assert.equal(guard.json.data.ok, true);
|
|
303
|
-
} finally {
|
|
304
|
-
cleanup(context);
|
|
305
|
-
}
|
|
306
|
-
});
|
|
@@ -1,241 +0,0 @@
|
|
|
1
|
-
import test from 'node:test';
|
|
2
|
-
import assert from 'node:assert/strict';
|
|
3
|
-
import { BASE_MS, DAY_MS, cleanup, makeCortex } from './_helpers.js';
|
|
4
|
-
|
|
5
|
-
test('FTS failure degrades cortex.search to recency instead of throwing', () => {
|
|
6
|
-
const context = makeCortex();
|
|
7
|
-
const { cortex } = context;
|
|
8
|
-
try {
|
|
9
|
-
const first = cortex.write({ text: '米饭的承诺:用户会买米饭。', title: '米饭', explicit: true, now: BASE_MS });
|
|
10
|
-
cortex.write({ text: '另一条普通记忆。', title: '普通', explicit: true, now: BASE_MS + 1_000 });
|
|
11
|
-
assert.equal(first.skipped, false);
|
|
12
|
-
|
|
13
|
-
const original = cortex.repos.traces.searchFts;
|
|
14
|
-
try {
|
|
15
|
-
cortex.repos.traces.searchFts = () => {
|
|
16
|
-
throw new Error('fts unavailable');
|
|
17
|
-
};
|
|
18
|
-
const degraded = cortex.search('米饭', { now: BASE_MS + 2_000, limit: 5 });
|
|
19
|
-
assert.ok(degraded.items.length >= 1); // recency source still produced candidates
|
|
20
|
-
const ftsSource = degraded.trace.sources.find((source) => source.source === 'fts');
|
|
21
|
-
assert.ok(ftsSource);
|
|
22
|
-
assert.equal(ftsSource.ok, false);
|
|
23
|
-
assert.match(ftsSource.error, /fts unavailable/);
|
|
24
|
-
assert.ok(degraded.trace.candidates >= 1);
|
|
25
|
-
} finally {
|
|
26
|
-
cortex.repos.traces.searchFts = original;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
// Once the repository method is restored, FTS contributes again.
|
|
30
|
-
const healthy = cortex.search('米饭', { now: BASE_MS + 3_000, limit: 5 });
|
|
31
|
-
const healthyFts = healthy.trace.sources.find((source) => source.source === 'fts');
|
|
32
|
-
assert.equal(healthyFts.ok, true);
|
|
33
|
-
assert.ok(healthyFts.count >= 1);
|
|
34
|
-
assert.equal(healthy.items[0].id, first.trace.id);
|
|
35
|
-
} finally {
|
|
36
|
-
cleanup(context);
|
|
37
|
-
}
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
test('vector index failures never fail writes or searches, and recovery restores fusion', () => {
|
|
41
|
-
const context = makeCortex({ features: { vector: true } });
|
|
42
|
-
const { cortex } = context;
|
|
43
|
-
try {
|
|
44
|
-
assert.ok(cortex.vector);
|
|
45
|
-
assert.equal(cortex.vector.model, 'hash-256');
|
|
46
|
-
assert.equal(cortex.vector.dim, 256);
|
|
47
|
-
assert.equal(cortex.vector.status().count, 0);
|
|
48
|
-
|
|
49
|
-
const first = cortex.write({ text: '向量测试:用户喜欢米饭。', title: '向量一', explicit: true, now: BASE_MS });
|
|
50
|
-
assert.equal(first.skipped, false);
|
|
51
|
-
assert.equal(cortex.vector.status().count, 1);
|
|
52
|
-
|
|
53
|
-
const originalUpsert = cortex.vector.upsert;
|
|
54
|
-
try {
|
|
55
|
-
cortex.vector.upsert = () => {
|
|
56
|
-
throw new Error('embed failed');
|
|
57
|
-
};
|
|
58
|
-
// encode wraps vector.upsert in try/catch: the trace must still be stored.
|
|
59
|
-
const second = cortex.write({ text: '向量测试:第二条记忆。', title: '向量二', explicit: true, now: BASE_MS + 1_000 });
|
|
60
|
-
assert.equal(second.skipped, false);
|
|
61
|
-
assert.ok(second.trace);
|
|
62
|
-
assert.equal(cortex.repos.traces.count(), 2);
|
|
63
|
-
assert.equal(cortex.vector.status().count, 1); // the failed upsert was skipped
|
|
64
|
-
} finally {
|
|
65
|
-
cortex.vector.upsert = originalUpsert;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
const originalSearch = cortex.vector.search;
|
|
69
|
-
try {
|
|
70
|
-
cortex.vector.search = () => {
|
|
71
|
-
throw new Error('vector search failed');
|
|
72
|
-
};
|
|
73
|
-
const degraded = cortex.search('米饭', { now: BASE_MS + 2_000, limit: 5 });
|
|
74
|
-
assert.ok(degraded.items.length >= 1);
|
|
75
|
-
const vectorSource = degraded.trace.sources.find((source) => source.source === 'vector');
|
|
76
|
-
assert.ok(vectorSource);
|
|
77
|
-
assert.equal(vectorSource.ok, false);
|
|
78
|
-
assert.match(vectorSource.error, /vector search failed/);
|
|
79
|
-
} finally {
|
|
80
|
-
cortex.vector.search = originalSearch;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
const healthy = cortex.search('米饭', { now: BASE_MS + 3_000, limit: 5 });
|
|
84
|
-
const vectorSource = healthy.trace.sources.find((source) => source.source === 'vector');
|
|
85
|
-
assert.equal(vectorSource.ok, true);
|
|
86
|
-
assert.ok(cortex.vector.search('米饭', { limit: 5 }).length >= 1);
|
|
87
|
-
assert.equal(cortex.settings().vector.model, 'hash-256');
|
|
88
|
-
|
|
89
|
-
// A rebuild re-embeds every active trace after the earlier failed upsert.
|
|
90
|
-
const rebuilt = cortex.rebuildVector({ scopeId: 'global' });
|
|
91
|
-
assert.equal(rebuilt.rebuilt, 2);
|
|
92
|
-
assert.equal(cortex.vector.status().count, 2);
|
|
93
|
-
assert.equal(cortex.guardian.check().ok, true);
|
|
94
|
-
} finally {
|
|
95
|
-
cleanup(context);
|
|
96
|
-
}
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
test('writeMany is chunked, idempotent, duplicate-aware and atomic per chunk', () => {
|
|
100
|
-
const context = makeCortex();
|
|
101
|
-
const { cortex } = context;
|
|
102
|
-
try {
|
|
103
|
-
const batch = [
|
|
104
|
-
{ text: '批量一。', operationId: 'batch:1', explicit: true, now: BASE_MS },
|
|
105
|
-
{ text: '批量二。', operationId: 'batch:2', explicit: true, now: BASE_MS + 1_000 },
|
|
106
|
-
{ text: '批量三。', operationId: 'batch:3', explicit: true, now: BASE_MS + 2_000 },
|
|
107
|
-
];
|
|
108
|
-
const first = cortex.writeMany(batch, { chunkSize: 2 });
|
|
109
|
-
assert.equal(first.length, 3);
|
|
110
|
-
assert.equal(first.every((result) => result.skipped === false), true);
|
|
111
|
-
assert.equal(cortex.repos.traces.count(), 3);
|
|
112
|
-
assert.equal(cortex.repos.events.count(), 3);
|
|
113
|
-
|
|
114
|
-
const second = cortex.writeMany(batch, { chunkSize: 2 });
|
|
115
|
-
assert.equal(second.length, 3);
|
|
116
|
-
assert.equal(second.every((result) => result.skipped === true), true);
|
|
117
|
-
assert.deepEqual(second.map((result) => result.reason), ['idempotent', 'idempotent', 'idempotent']);
|
|
118
|
-
assert.equal(cortex.repos.traces.count(), 3);
|
|
119
|
-
assert.equal(cortex.repos.events.count(), 3);
|
|
120
|
-
|
|
121
|
-
// A conflicting operationId aborts the whole chunk, including earlier items.
|
|
122
|
-
assert.throws(
|
|
123
|
-
() => cortex.writeMany([
|
|
124
|
-
{ text: '事务内第一条。', operationId: 'transaction:1', explicit: true },
|
|
125
|
-
{ text: '内容不同的重放。', operationId: 'batch:1', explicit: true },
|
|
126
|
-
]),
|
|
127
|
-
/already exists with a different payload/,
|
|
128
|
-
);
|
|
129
|
-
assert.equal(cortex.repos.traces.count(), 3);
|
|
130
|
-
assert.equal(cortex.repos.events.count(), 3);
|
|
131
|
-
assert.equal(cortex.repos.traces.list({ limit: 10 }).some((trace) => trace.content === '事务内第一条。'), false);
|
|
132
|
-
|
|
133
|
-
// The same operationId twice inside one batch is collapsed by the ledger.
|
|
134
|
-
const withinBatch = cortex.writeMany([
|
|
135
|
-
{ text: '批内重复。', operationId: 'batch:dup', explicit: true },
|
|
136
|
-
{ text: '批内重复。', operationId: 'batch:dup', explicit: true },
|
|
137
|
-
]);
|
|
138
|
-
assert.equal(withinBatch[0].skipped, false);
|
|
139
|
-
assert.equal(withinBatch[1].skipped, true);
|
|
140
|
-
assert.equal(withinBatch[1].reason, 'idempotent');
|
|
141
|
-
assert.equal(cortex.repos.traces.count(), 4);
|
|
142
|
-
|
|
143
|
-
// Different operation ids but identical content collapse on the hash index;
|
|
144
|
-
// the ledger still records the attempted write.
|
|
145
|
-
const duplicate = cortex.write({ text: '批量一。', explicit: true, now: BASE_MS + 9_000 });
|
|
146
|
-
assert.equal(duplicate.skipped, true);
|
|
147
|
-
assert.equal(duplicate.reason, 'duplicate');
|
|
148
|
-
assert.equal(duplicate.trace.id, first[0].trace.id);
|
|
149
|
-
assert.equal(cortex.repos.traces.count(), 4);
|
|
150
|
-
assert.equal(cortex.repos.events.count(), 5);
|
|
151
|
-
|
|
152
|
-
const metrics = cortex.metrics();
|
|
153
|
-
// `writes.created` is incremented inside write(), before the chunk commits,
|
|
154
|
-
// so the rolled-back chunk still contributes one attempt-level count.
|
|
155
|
-
assert.ok(metrics.counters['writes.created'] >= 4);
|
|
156
|
-
assert.ok(metrics.counters['writes.skipped'] >= 4);
|
|
157
|
-
assert.equal(cortex.guardian.check().ok, true);
|
|
158
|
-
|
|
159
|
-
const empty = cortex.writeMany([]);
|
|
160
|
-
assert.deepEqual(empty, []);
|
|
161
|
-
} finally {
|
|
162
|
-
cleanup(context);
|
|
163
|
-
}
|
|
164
|
-
});
|
|
165
|
-
|
|
166
|
-
test('retention dry-run does not mutate; apply archives old low-value traces only', () => {
|
|
167
|
-
const context = makeCortex();
|
|
168
|
-
const { cortex } = context;
|
|
169
|
-
try {
|
|
170
|
-
const oldLow = cortex.write({
|
|
171
|
-
text: '保留测试:很久以前的小事。',
|
|
172
|
-
explicit: true,
|
|
173
|
-
importance: 0.05,
|
|
174
|
-
now: BASE_MS - 400 * DAY_MS,
|
|
175
|
-
});
|
|
176
|
-
const oldHigh = cortex.write({
|
|
177
|
-
text: '保留测试:很重要旧事。',
|
|
178
|
-
explicit: true,
|
|
179
|
-
importance: 0.9,
|
|
180
|
-
now: BASE_MS - 400 * DAY_MS,
|
|
181
|
-
});
|
|
182
|
-
const oldReflection = cortex.write({
|
|
183
|
-
text: '保留测试:很久以前的反思。',
|
|
184
|
-
kind: 'reflection',
|
|
185
|
-
explicit: true,
|
|
186
|
-
importance: 0.05,
|
|
187
|
-
now: BASE_MS - 400 * DAY_MS,
|
|
188
|
-
});
|
|
189
|
-
const freshLow = cortex.write({
|
|
190
|
-
text: '保留测试:昨天的小事。',
|
|
191
|
-
explicit: true,
|
|
192
|
-
importance: 0.05,
|
|
193
|
-
now: BASE_MS - DAY_MS,
|
|
194
|
-
});
|
|
195
|
-
assert.equal(cortex.repos.traces.count(), 4);
|
|
196
|
-
|
|
197
|
-
const plan = cortex.retention.plan({ now: BASE_MS, maxAgeDays: 365, minImportance: 0.2, limit: 100 });
|
|
198
|
-
assert.equal(plan.active, 4);
|
|
199
|
-
assert.equal(plan.count, 1);
|
|
200
|
-
assert.deepEqual(plan.candidates.map((candidate) => candidate.id), [oldLow.trace.id]);
|
|
201
|
-
assert.equal(plan.candidates[0].reason, 'over-age');
|
|
202
|
-
assert.ok(plan.candidates[0].ageDays > 365);
|
|
203
|
-
assert.ok(plan.candidates[0].importance < 0.2);
|
|
204
|
-
assert.ok(plan.candidates[0].confidence < 0.75);
|
|
205
|
-
|
|
206
|
-
const eventsBefore = cortex.repos.events.count();
|
|
207
|
-
const dry = cortex.retention.apply(plan, { dryRun: true });
|
|
208
|
-
assert.equal(dry.dryRun, true);
|
|
209
|
-
assert.equal(dry.archived, 1);
|
|
210
|
-
assert.deepEqual(dry.ids, [oldLow.trace.id]);
|
|
211
|
-
assert.equal(cortex.get(oldLow.trace.id).state, 'active');
|
|
212
|
-
assert.equal(cortex.repos.events.count(), eventsBefore);
|
|
213
|
-
|
|
214
|
-
const applied = cortex.retention.apply(plan);
|
|
215
|
-
assert.equal(applied.archived, 1);
|
|
216
|
-
assert.deepEqual(applied.ids, [oldLow.trace.id]);
|
|
217
|
-
const archived = cortex.get(oldLow.trace.id);
|
|
218
|
-
assert.equal(archived.state, 'archived');
|
|
219
|
-
assert.equal(archived.compression, 'retention');
|
|
220
|
-
assert.equal(cortex.get(oldHigh.trace.id).state, 'active');
|
|
221
|
-
assert.equal(cortex.get(oldReflection.trace.id).state, 'active');
|
|
222
|
-
assert.equal(cortex.get(freshLow.trace.id).state, 'active');
|
|
223
|
-
assert.equal(cortex.repos.traces.ftsCount(), 4); // FTS keeps its projection for state flips
|
|
224
|
-
assert.equal(cortex.repos.events.count(), eventsBefore);
|
|
225
|
-
|
|
226
|
-
// A second plan no longer offers the archived trace.
|
|
227
|
-
const secondPlan = cortex.retention.plan({ now: BASE_MS, maxAgeDays: 365, minImportance: 0.2 });
|
|
228
|
-
assert.equal(secondPlan.active, 3);
|
|
229
|
-
assert.deepEqual(secondPlan.candidates, []);
|
|
230
|
-
|
|
231
|
-
// Over-count candidates are explainable too; reflections stay out of retention.
|
|
232
|
-
const overCount = cortex.retention.plan({ now: BASE_MS, maxTraces: 0, maxAgeDays: 3_650, minImportance: 0.2 });
|
|
233
|
-
assert.deepEqual(overCount.candidates.map((candidate) => candidate.id), [freshLow.trace.id]);
|
|
234
|
-
assert.equal(overCount.candidates[0].reason, 'over-count');
|
|
235
|
-
|
|
236
|
-
assert.deepEqual(cortex.retention.apply(null), { archived: 0, dryRun: false });
|
|
237
|
-
assert.equal(cortex.guardian.check().ok, true);
|
|
238
|
-
} finally {
|
|
239
|
-
cleanup(context);
|
|
240
|
-
}
|
|
241
|
-
});
|