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.
- package/CHANGELOG.md +2423 -2369
- package/README.md +8 -8
- package/bench/governance.mjs +265 -0
- package/bench/migration-bench.mjs +1 -1
- package/bench/semantic-cn.js +4 -4
- package/bench/synthetic.js +3 -3
- package/bin/copy-db.mjs +1 -1
- package/bin/repair.mjs +7 -7
- package/client.js +1 -1
- package/docs/install-windows.md +2 -2
- package/docs/models.md +1 -1
- package/docs/quota-compaction-design.md +3 -3
- package/icon.svg +12 -2
- package/index.js +5 -6
- package/package.json +7 -8
- package/src/adapters/shiori.js +1 -1
- package/src/engines/compression.js +8 -3
- package/src/engines/consolidate.js +1 -1
- package/src/engines/encode.js +3 -3
- package/src/engines/extract.js +1 -1
- package/src/engines/health.js +1 -1
- package/src/engines/life.js +2 -2
- package/src/engines/narrative.js +4 -8
- package/src/engines/retrieve.js +41 -4
- package/src/host/scheduler.js +5 -4
- package/src/memory/tokenizer.js +1 -1
- package/src/self/alias.js +4 -5
- package/src/self/memory-policy.js +59 -0
- package/src/service/cortex.js +20 -2
- package/src/store/repositories/facts.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,34 +0,0 @@
|
|
|
1
|
-
import test from 'node:test';
|
|
2
|
-
import assert from 'node:assert/strict';
|
|
3
|
-
import { mkdtempSync, rmSync } from 'node:fs';
|
|
4
|
-
import os from 'node:os';
|
|
5
|
-
import path from 'node:path';
|
|
6
|
-
import { openDatabase, migrate } from '../../src/store/database.js';
|
|
7
|
-
import { findDuplicateEpisodes } from '../../src/engines/summary-dedupe.js';
|
|
8
|
-
|
|
9
|
-
test('重合窗口 + 内容几乎一致才判为重复', () => {
|
|
10
|
-
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-dedupe-'));
|
|
11
|
-
const { db } = openDatabase({ dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite') });
|
|
12
|
-
migrate(db);
|
|
13
|
-
try {
|
|
14
|
-
const now = '2026-11-15T10:00:00.000Z';
|
|
15
|
-
const insert = db.prepare(`INSERT INTO summaries(id, scope_id, level, period_start, period_end, content, source_ids_json, coverage, model, created_at)
|
|
16
|
-
VALUES (?, 'global', 'episode', ?, ?, ?, ?, 0.5, 'extractive-v2-weighted', ?)`);
|
|
17
|
-
// 同一窗口(重叠 ≈99%)、内容完全相同
|
|
18
|
-
insert.run('a', '2026-11-11T13:50:00.000Z', '2026-11-12T13:40:00.000Z', '这段时间记住了:用户否决了单图网格绑定,决定借模型。', '["t1","t2"]', '2026-11-12T13:48:00.000Z');
|
|
19
|
-
insert.run('b', '2026-11-11T13:52:00.000Z', '2026-11-12T13:44:00.000Z', '这段时间记住了:用户否决了单图网格绑定,决定借模型。', '["t2","t3"]', '2026-11-12T15:35:00.000Z');
|
|
20
|
-
// 完全不同的窗口与内容
|
|
21
|
-
insert.run('c', '2026-11-13T00:00:00.000Z', '2026-11-14T00:00:00.000Z', '另一段时间:修好了向量回填,覆盖率从 0.22 到 1.0。', '["t9"]', now);
|
|
22
|
-
|
|
23
|
-
const result = findDuplicateEpisodes({ db, scopeId: 'global' });
|
|
24
|
-
assert.equal(result.candidates, 3);
|
|
25
|
-
assert.equal(result.clusters.length, 1, '只有 a/b 该成簇');
|
|
26
|
-
const cluster = result.clusters[0];
|
|
27
|
-
assert.equal(cluster.keep.id, 'b', '保留较新的一条');
|
|
28
|
-
assert.deepEqual(cluster.remove.map((item) => item.id), ['a']);
|
|
29
|
-
assert.deepEqual(cluster.unionSources.sort(), ['t1', 't2', 't3']);
|
|
30
|
-
} finally {
|
|
31
|
-
db.close();
|
|
32
|
-
rmSync(dir, { recursive: true, force: true });
|
|
33
|
-
}
|
|
34
|
-
});
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import test from 'node:test';
|
|
2
|
-
import assert from 'node:assert/strict';
|
|
3
|
-
import { clampThreshold } from '../../src/engines/consolidate.js';
|
|
4
|
-
|
|
5
|
-
test('相似度阈值被夹在 0.5–0.99,非法值回落到 0.8', () => {
|
|
6
|
-
assert.equal(clampThreshold(0.1), 0.5);
|
|
7
|
-
// 0 当作「没传」处理,回落到默认 0.8(比按 0 全合安全)。
|
|
8
|
-
assert.equal(clampThreshold(0), 0.8);
|
|
9
|
-
assert.equal(clampThreshold(1.5), 0.99);
|
|
10
|
-
assert.equal(clampThreshold(0.8), 0.8);
|
|
11
|
-
assert.equal(clampThreshold('0.7'), 0.7);
|
|
12
|
-
assert.equal(clampThreshold(undefined), 0.8);
|
|
13
|
-
assert.equal(clampThreshold(Number.NaN), 0.8);
|
|
14
|
-
});
|
|
@@ -1,130 +0,0 @@
|
|
|
1
|
-
import test from 'node:test';
|
|
2
|
-
import assert from 'node:assert/strict';
|
|
3
|
-
import { mkdtempSync, rmSync } from 'node:fs';
|
|
4
|
-
import os from 'node:os';
|
|
5
|
-
import path from 'node:path';
|
|
6
|
-
import { openDatabase, migrate, closeDatabase } from '../../src/store/database.js';
|
|
7
|
-
import { createEventRepository } from '../../src/store/repositories/events.js';
|
|
8
|
-
import { createTraceRepository } from '../../src/store/repositories/traces.js';
|
|
9
|
-
import { createEntityRepository } from '../../src/store/repositories/entities.js';
|
|
10
|
-
import { createFactRepository } from '../../src/store/repositories/facts.js';
|
|
11
|
-
import { createAuditRepository } from '../../src/store/repositories/audit.js';
|
|
12
|
-
import { createSelfStore } from '../../src/self/state.js';
|
|
13
|
-
import { createAppraisalStore } from '../../src/self/appraisals.js';
|
|
14
|
-
import { createInitiativeStore } from '../../src/self/initiative.js';
|
|
15
|
-
import { createIdentityStore } from '../../src/self/identity.js';
|
|
16
|
-
import { createRelationshipStore } from '../../src/self/relationship.js';
|
|
17
|
-
import { createEncodeEngine } from '../../src/engines/encode.js';
|
|
18
|
-
import { createRetrievalEngine } from '../../src/engines/retrieve.js';
|
|
19
|
-
import { createDevelopEngine } from '../../src/engines/develop.js';
|
|
20
|
-
|
|
21
|
-
function setup() {
|
|
22
|
-
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-develop-'));
|
|
23
|
-
const { db } = openDatabase({ dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite') });
|
|
24
|
-
migrate(db);
|
|
25
|
-
const events = createEventRepository(db);
|
|
26
|
-
const traces = createTraceRepository(db);
|
|
27
|
-
const entities = createEntityRepository(db);
|
|
28
|
-
const facts = createFactRepository(db);
|
|
29
|
-
const audit = createAuditRepository(db);
|
|
30
|
-
const selfStore = createSelfStore(db);
|
|
31
|
-
const appraisalStore = createAppraisalStore(db);
|
|
32
|
-
const initiativeStore = createInitiativeStore(db);
|
|
33
|
-
const identityStore = createIdentityStore(db);
|
|
34
|
-
const relationshipStore = createRelationshipStore(db);
|
|
35
|
-
const encode = createEncodeEngine({ events, traces, entities, facts, audit, config: { minCompressionValue: 0.01 } });
|
|
36
|
-
const retrieve = createRetrievalEngine({ traces, config: { contextBudget: 500 } });
|
|
37
|
-
const develop = createDevelopEngine({
|
|
38
|
-
events, traces, encode, retrieve, selfStore, appraisalStore, initiativeStore, identityStore, relationshipStore,
|
|
39
|
-
});
|
|
40
|
-
return { dir, db, events, traces, selfStore, appraisalStore, initiativeStore, identityStore, relationshipStore, develop };
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
test('a warm turn moves care, writes a trace, records an appraisal and deepens the relationship', () => {
|
|
44
|
-
const { dir, db, selfStore, appraisalStore, relationshipStore, develop } = setup();
|
|
45
|
-
try {
|
|
46
|
-
const before = selfStore.get('global');
|
|
47
|
-
const relationBefore = relationshipStore.ensure('global', '用户');
|
|
48
|
-
const result = develop.step({
|
|
49
|
-
text: '用户说会一直陪着助手,助手很开心。',
|
|
50
|
-
explicit: true,
|
|
51
|
-
now: Date.parse('2026-11-11T12:00:00+08:00'),
|
|
52
|
-
});
|
|
53
|
-
assert.equal(result.encoded.skipped, false);
|
|
54
|
-
assert.ok(result.self.drives.care > before.drives.care);
|
|
55
|
-
assert.ok(result.self.mood.valence > 0);
|
|
56
|
-
assert.equal(appraisalStore.list().length, 1);
|
|
57
|
-
assert.equal(result.appraisalRow.traceId, result.encoded.trace.id);
|
|
58
|
-
assert.ok(result.relationship.care >= relationBefore.care);
|
|
59
|
-
assert.ok(result.identity.content.boundaries.length > 0);
|
|
60
|
-
} finally {
|
|
61
|
-
closeDatabase(db);
|
|
62
|
-
rmSync(dir, { recursive: true, force: true });
|
|
63
|
-
}
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
test('a hostile turn lowers safety and proposes a boundary question', () => {
|
|
67
|
-
const { dir, db, selfStore, develop } = setup();
|
|
68
|
-
try {
|
|
69
|
-
selfStore.update('global', { drives: { safety: 0.4 } });
|
|
70
|
-
const result = develop.step({
|
|
71
|
-
text: '助手讨厌你,滚,别烦我。',
|
|
72
|
-
explicit: true,
|
|
73
|
-
now: Date.parse('2026-11-11T12:00:00+08:00'),
|
|
74
|
-
});
|
|
75
|
-
assert.ok(result.appraisal.threat > 0.5);
|
|
76
|
-
assert.ok(result.self.drives.safety < 0.4);
|
|
77
|
-
assert.ok(result.intentions.some((item) => item.kind === 'question'));
|
|
78
|
-
} finally {
|
|
79
|
-
closeDatabase(db);
|
|
80
|
-
rmSync(dir, { recursive: true, force: true });
|
|
81
|
-
}
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
test('low coherence with no recent consolidation proposes a consolidation', () => {
|
|
85
|
-
const { dir, db, selfStore, develop } = setup();
|
|
86
|
-
try {
|
|
87
|
-
selfStore.update('global', { drives: { coherence: 0.2 } });
|
|
88
|
-
const result = develop.step({
|
|
89
|
-
text: '今天整理了一些旧记忆。',
|
|
90
|
-
explicit: true,
|
|
91
|
-
now: Date.parse('2026-11-11T12:00:00+08:00'),
|
|
92
|
-
});
|
|
93
|
-
assert.ok(result.intentions.some((item) => item.kind === 'consolidate'));
|
|
94
|
-
} finally {
|
|
95
|
-
closeDatabase(db);
|
|
96
|
-
rmSync(dir, { recursive: true, force: true });
|
|
97
|
-
}
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
test('a recall query is returned alongside the write', () => {
|
|
101
|
-
const { dir, db, develop } = setup();
|
|
102
|
-
try {
|
|
103
|
-
develop.step({ text: '用户说会买米饭。', explicit: true, now: Date.parse('2026-11-11T11:00:00+08:00') });
|
|
104
|
-
const result = develop.step({
|
|
105
|
-
text: '今天又想到米饭了。',
|
|
106
|
-
explicit: true,
|
|
107
|
-
now: Date.parse('2026-11-11T12:00:00+08:00'),
|
|
108
|
-
recallQuery: '米饭',
|
|
109
|
-
recallBudget: 300,
|
|
110
|
-
});
|
|
111
|
-
assert.ok(result.recall.items.some((item) => item.snippet.includes('米饭')));
|
|
112
|
-
} finally {
|
|
113
|
-
closeDatabase(db);
|
|
114
|
-
rmSync(dir, { recursive: true, force: true });
|
|
115
|
-
}
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
test('snapshot exposes self state and pending intentions', () => {
|
|
119
|
-
const { dir, db, develop } = setup();
|
|
120
|
-
try {
|
|
121
|
-
develop.step({ text: '今天有点累,但还想继续。', explicit: true, now: Date.parse('2026-11-11T12:00:00+08:00') });
|
|
122
|
-
const snapshot = develop.snapshot('global');
|
|
123
|
-
assert.equal(snapshot.self.scopeId, 'global');
|
|
124
|
-
assert.ok(Array.isArray(snapshot.pendingIntentions));
|
|
125
|
-
assert.ok(snapshot.traceCount >= 1);
|
|
126
|
-
} finally {
|
|
127
|
-
closeDatabase(db);
|
|
128
|
-
rmSync(dir, { recursive: true, force: true });
|
|
129
|
-
}
|
|
130
|
-
});
|
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
import test from 'node:test';
|
|
2
|
-
import assert from 'node:assert/strict';
|
|
3
|
-
import { mkdtempSync, rmSync } from 'node:fs';
|
|
4
|
-
import os from 'node:os';
|
|
5
|
-
import path from 'node:path';
|
|
6
|
-
import { openDatabase, migrate, closeDatabase } from '../../src/store/database.js';
|
|
7
|
-
import { computeMaturity, stageFor, nextStage, createDevelopmentStore, createDevelopmentEngine } from '../../src/self/development.js';
|
|
8
|
-
import { createSelfStore } from '../../src/self/state.js';
|
|
9
|
-
import { createTraceRepository } from '../../src/store/repositories/traces.js';
|
|
10
|
-
import { createEventRepository } from '../../src/store/repositories/events.js';
|
|
11
|
-
import { createFeedbackRepository } from '../../src/store/repositories/feedback.js';
|
|
12
|
-
import { createSkillRepository } from '../../src/store/repositories/skills.js';
|
|
13
|
-
import { createRelationshipStore } from '../../src/self/relationship.js';
|
|
14
|
-
|
|
15
|
-
function setup() {
|
|
16
|
-
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-development-'));
|
|
17
|
-
const { db } = openDatabase({ dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite') });
|
|
18
|
-
migrate(db);
|
|
19
|
-
return { dir, db };
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
test('maturity maps to developmental stages', () => {
|
|
23
|
-
const low = computeMaturity({ traces: 1, feedback: 0, calibration: null, activeSkills: 0, summaries: 0 });
|
|
24
|
-
assert.equal(stageFor(low.maturity), 'infancy');
|
|
25
|
-
const mid = computeMaturity({
|
|
26
|
-
traces: 100, feedback: 25, calibration: { brier: 0.2 }, activeSkills: 2, summaries: 4,
|
|
27
|
-
relationships: [{ familiarity: 0.5, trust: 0.7 }],
|
|
28
|
-
});
|
|
29
|
-
assert.ok(['childhood', 'adolescence'].includes(stageFor(mid.maturity)));
|
|
30
|
-
const high = computeMaturity({
|
|
31
|
-
traces: 500, feedback: 100, calibration: { brier: 0.05 }, activeSkills: 10, summaries: 20,
|
|
32
|
-
relationships: [{ familiarity: 1, trust: 1 }],
|
|
33
|
-
});
|
|
34
|
-
assert.equal(stageFor(high.maturity), 'adulthood');
|
|
35
|
-
assert.equal(nextStage('infancy'), 'childhood');
|
|
36
|
-
assert.equal(nextStage('adulthood'), null);
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
test('development store records history and engine updates self stage', () => {
|
|
40
|
-
const { dir, db } = setup();
|
|
41
|
-
try {
|
|
42
|
-
const events = createEventRepository(db);
|
|
43
|
-
const traces = createTraceRepository(db);
|
|
44
|
-
const feedback = createFeedbackRepository(db);
|
|
45
|
-
const skills = createSkillRepository(db);
|
|
46
|
-
const selfStore = createSelfStore(db);
|
|
47
|
-
const relationships = createRelationshipStore(db);
|
|
48
|
-
const store = createDevelopmentStore(db);
|
|
49
|
-
const auditCalls = [];
|
|
50
|
-
const audit = { record: (entry) => auditCalls.push(entry) };
|
|
51
|
-
const engine = createDevelopmentEngine({ traces, feedback, skills, relationships, selfStore, store, audit });
|
|
52
|
-
for (let i = 0; i < 5; i += 1) {
|
|
53
|
-
const event = events.append({ kind: 'observation', payload: { text: `记忆 ${i}` } });
|
|
54
|
-
traces.insert({ eventId: event.id, content: `记忆 ${i}`, importance: 0.5 });
|
|
55
|
-
}
|
|
56
|
-
const before = selfStore.get('global').stage;
|
|
57
|
-
const result = engine.evaluate({ now: Date.parse('2026-11-11T12:00:00+08:00') });
|
|
58
|
-
assert.equal(result.stage, 'infancy');
|
|
59
|
-
assert.equal(result.counts.traceCount, 5);
|
|
60
|
-
assert.equal(store.latest('global').stage, 'infancy');
|
|
61
|
-
assert.equal(store.history('global').length, 1);
|
|
62
|
-
assert.equal(selfStore.get('global').stage, result.stage);
|
|
63
|
-
if (before !== result.stage) assert.ok(auditCalls.length >= 0);
|
|
64
|
-
} finally {
|
|
65
|
-
closeDatabase(db);
|
|
66
|
-
rmSync(dir, { recursive: true, force: true });
|
|
67
|
-
}
|
|
68
|
-
});
|
package/test/unit/digest.test.js
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import test from 'node:test';
|
|
2
|
-
import assert from 'node:assert/strict';
|
|
3
|
-
import { mkdtempSync, rmSync } from 'node:fs';
|
|
4
|
-
import os from 'node:os';
|
|
5
|
-
import path from 'node:path';
|
|
6
|
-
import { createCortex } from '../../src/service/cortex.js';
|
|
7
|
-
|
|
8
|
-
test('daily digest counts traces and persists a digest summary', () => {
|
|
9
|
-
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-digest-'));
|
|
10
|
-
const cortex = createCortex({ config: { dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite') } });
|
|
11
|
-
try {
|
|
12
|
-
const at = Date.parse('2026-11-11T10:00:00+08:00');
|
|
13
|
-
cortex.write({ text: '今天修好了 API。', explicit: true, importance: 0.8, now: at });
|
|
14
|
-
cortex.write({ text: '今天写了一篇日记。', explicit: true, now: at + 1000 });
|
|
15
|
-
cortex.emotion.record({ text: '助手有点开心。', target: '用户', at: '2026-11-11T10:05:00+08:00' });
|
|
16
|
-
const digest = cortex.digest.daily({ date: '2026-11-11', now: at + 2000 });
|
|
17
|
-
assert.equal(digest.counts.traces, 2);
|
|
18
|
-
assert.equal(digest.counts.emotions, 1);
|
|
19
|
-
assert.match(digest.content, /记忆摘要/);
|
|
20
|
-
assert.ok(digest.summary);
|
|
21
|
-
assert.ok(cortex.repos.summaries.list('global', { limit: 10 }).length >= 1);
|
|
22
|
-
} finally {
|
|
23
|
-
cortex.close();
|
|
24
|
-
rmSync(dir, { recursive: true, force: true });
|
|
25
|
-
}
|
|
26
|
-
});
|
package/test/unit/dsr.test.js
DELETED
|
@@ -1,111 +0,0 @@
|
|
|
1
|
-
import test from 'node:test';
|
|
2
|
-
import assert from 'node:assert/strict';
|
|
3
|
-
import {
|
|
4
|
-
GRADES,
|
|
5
|
-
DEFAULT_WEIGHTS,
|
|
6
|
-
decayFactor,
|
|
7
|
-
retrievability,
|
|
8
|
-
initialStability,
|
|
9
|
-
nextDifficulty,
|
|
10
|
-
nextStability,
|
|
11
|
-
review,
|
|
12
|
-
gradeFromSignal,
|
|
13
|
-
archiveDecision,
|
|
14
|
-
computeRetrievability,
|
|
15
|
-
} from '../../src/memory/dsr.js';
|
|
16
|
-
|
|
17
|
-
const NOW = Date.parse('2026-11-11T00:00:00.000Z');
|
|
18
|
-
const daysAgo = (days) => new Date(NOW - days * 86400000).toISOString();
|
|
19
|
-
|
|
20
|
-
test('retrievability is 0.9 at t=S and decreases with time', () => {
|
|
21
|
-
assert.ok(Math.abs(retrievability(10, 10, -0.5) - 0.9) < 1e-6);
|
|
22
|
-
assert.ok(retrievability(10, 20, -0.5) < retrievability(10, 3, -0.5));
|
|
23
|
-
assert.ok(retrievability(20, 10, -0.5) > retrievability(10, 10, -0.5));
|
|
24
|
-
assert.ok(retrievability(1, 1000, -0.5) >= 0);
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
test('decay factor is finite and negative-decay safe', () => {
|
|
28
|
-
assert.ok(Number.isFinite(decayFactor(-0.5)));
|
|
29
|
-
assert.ok(decayFactor(-1) > 0);
|
|
30
|
-
assert.ok(decayFactor(-0.1) > 0);
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
test('initial stability ranks identity above working and rewards importance', () => {
|
|
34
|
-
const identity = initialStability({ type: 'identity', importance: 0.9 });
|
|
35
|
-
const working = initialStability({ type: 'working', importance: 0.1 });
|
|
36
|
-
const low = initialStability({ type: 'episodic', importance: 0.1 });
|
|
37
|
-
const high = initialStability({ type: 'episodic', importance: 0.9 });
|
|
38
|
-
assert.ok(identity > working);
|
|
39
|
-
assert.ok(high > low);
|
|
40
|
-
assert.ok(identity <= 3650);
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
test('difficulty is clamped and mean-reverts', () => {
|
|
44
|
-
assert.equal(nextDifficulty(1, GRADES.easy), 1);
|
|
45
|
-
assert.equal(nextDifficulty(10, GRADES.again), 10);
|
|
46
|
-
const middle = nextDifficulty(5, GRADES.good);
|
|
47
|
-
assert.ok(middle >= 1 && middle <= 10);
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
test('review raises stability for good and lowers it for again', () => {
|
|
51
|
-
const base = {
|
|
52
|
-
stability: 10,
|
|
53
|
-
difficulty: 5,
|
|
54
|
-
recordedAt: daysAgo(10),
|
|
55
|
-
lastReviewAt: daysAgo(10),
|
|
56
|
-
reviewCount: 0,
|
|
57
|
-
lapseCount: 0,
|
|
58
|
-
};
|
|
59
|
-
const good = review(base, GRADES.good, { now: NOW, maximumStability: 3650 });
|
|
60
|
-
assert.ok(good.stability > base.stability);
|
|
61
|
-
assert.equal(good.reviewCount, 1);
|
|
62
|
-
assert.equal(good.lapseCount, 0);
|
|
63
|
-
const again = review(base, GRADES.again, { now: NOW, maximumStability: 3650 });
|
|
64
|
-
assert.ok(again.stability < base.stability);
|
|
65
|
-
assert.equal(again.lapseCount, 1);
|
|
66
|
-
assert.ok(again.reviewCount === 1);
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
test('stability never exceeds the configured ceiling', () => {
|
|
70
|
-
const next = nextStability({
|
|
71
|
-
stability: 3000,
|
|
72
|
-
difficulty: 1,
|
|
73
|
-
retrievability: 0.9,
|
|
74
|
-
grade: GRADES.easy,
|
|
75
|
-
weights: DEFAULT_WEIGHTS,
|
|
76
|
-
maximumStability: 100,
|
|
77
|
-
});
|
|
78
|
-
assert.ok(next <= 100);
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
test('feedback signals map to grades', () => {
|
|
82
|
-
assert.equal(gradeFromSignal('helpful'), GRADES.easy);
|
|
83
|
-
assert.equal(gradeFromSignal('used'), GRADES.easy);
|
|
84
|
-
assert.equal(gradeFromSignal('ignored'), GRADES.hard);
|
|
85
|
-
assert.equal(gradeFromSignal('corrected'), GRADES.again);
|
|
86
|
-
assert.equal(gradeFromSignal('unknown'), GRADES.good);
|
|
87
|
-
});
|
|
88
|
-
|
|
89
|
-
test('archive is blocked for identity, important or young traces', () => {
|
|
90
|
-
const base = {
|
|
91
|
-
id: 't1',
|
|
92
|
-
state: 'active',
|
|
93
|
-
kind: 'observation',
|
|
94
|
-
importance: 0.1,
|
|
95
|
-
stability: 1,
|
|
96
|
-
recordedAt: daysAgo(100),
|
|
97
|
-
lastAccessAt: daysAgo(100),
|
|
98
|
-
};
|
|
99
|
-
assert.equal(archiveDecision({ ...base, kind: 'identity' }, { now: NOW }).archive, false);
|
|
100
|
-
assert.equal(archiveDecision({ ...base, importance: 0.9 }, { now: NOW, coverage: 1 }).archive, false);
|
|
101
|
-
assert.equal(archiveDecision({ ...base, recordedAt: daysAgo(1), lastAccessAt: daysAgo(1) }, { now: NOW, coverage: 1 }).archive, false);
|
|
102
|
-
assert.equal(archiveDecision(base, { now: NOW, coverage: 0.1 }).archive, false);
|
|
103
|
-
const decision = archiveDecision(base, { now: NOW, coverage: 0.95 });
|
|
104
|
-
assert.equal(decision.archive, true);
|
|
105
|
-
assert.equal(decision.reason, 'compressed');
|
|
106
|
-
});
|
|
107
|
-
|
|
108
|
-
test('computeRetrievability uses the last review/access time', () => {
|
|
109
|
-
const trace = { stability: 10, lastReviewAt: daysAgo(10) };
|
|
110
|
-
assert.ok(Math.abs(computeRetrievability(trace, NOW, -0.5) - 0.9) < 1e-6);
|
|
111
|
-
});
|
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
import test from 'node:test';
|
|
2
|
-
import assert from 'node:assert/strict';
|
|
3
|
-
import { mkdtempSync, rmSync } from 'node:fs';
|
|
4
|
-
import os from 'node:os';
|
|
5
|
-
import path from 'node:path';
|
|
6
|
-
import { openDatabase, migrate, closeDatabase } from '../../src/store/database.js';
|
|
7
|
-
import { createEdgeRepository } from '../../src/store/repositories/edges.js';
|
|
8
|
-
import { createCortex } from '../../src/service/cortex.js';
|
|
9
|
-
|
|
10
|
-
function setup() {
|
|
11
|
-
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-edge-candidates-'));
|
|
12
|
-
const { db } = openDatabase({ dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite') });
|
|
13
|
-
migrate(db);
|
|
14
|
-
return { dir, db, edges: createEdgeRepository(db) };
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
test('candidate edges stay out of active graph until approved', () => {
|
|
18
|
-
const { dir, db, edges } = setup();
|
|
19
|
-
try {
|
|
20
|
-
const candidate = edges.addCandidate({
|
|
21
|
-
scopeId: 'global',
|
|
22
|
-
fromId: 'ent_a',
|
|
23
|
-
toId: 'ent_b',
|
|
24
|
-
rel: 'related',
|
|
25
|
-
confidence: 0.6,
|
|
26
|
-
sourceTraceId: 'trc_test',
|
|
27
|
-
evidence: 'co-occurrence',
|
|
28
|
-
});
|
|
29
|
-
assert.equal(candidate.metadata.status, 'candidate');
|
|
30
|
-
assert.equal(edges.list({ scopeId: 'global' }).length, 0);
|
|
31
|
-
assert.equal(edges.listCandidates({ scopeId: 'global' }).length, 1);
|
|
32
|
-
|
|
33
|
-
const approved = edges.approveCandidate(candidate.id);
|
|
34
|
-
assert.equal(approved.metadata.status, 'active');
|
|
35
|
-
assert.equal(edges.list({ scopeId: 'global' }).length, 1);
|
|
36
|
-
assert.equal(edges.listCandidates({ scopeId: 'global' }).length, 0);
|
|
37
|
-
} finally {
|
|
38
|
-
closeDatabase(db);
|
|
39
|
-
rmSync(dir, { recursive: true, force: true });
|
|
40
|
-
}
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
test('explicit facts queue candidate edges, but graph stays clean until approved', () => {
|
|
44
|
-
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-edge-queue-'));
|
|
45
|
-
const cortex = createCortex({ config: { dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite') } });
|
|
46
|
-
try {
|
|
47
|
-
const written = cortex.write({
|
|
48
|
-
text: '甲和乙明确相关。',
|
|
49
|
-
explicit: true,
|
|
50
|
-
facts: [{ subject: '甲', predicate: 'related', object: '乙', confidence: 0.8 }],
|
|
51
|
-
});
|
|
52
|
-
assert.equal(written.edgeCandidates.length, 1);
|
|
53
|
-
assert.equal(cortex.graphCandidates({ scopeId: 'global' }).length, 1);
|
|
54
|
-
assert.equal(cortex.graphData({ scopeId: 'global' }).edges.length, 0);
|
|
55
|
-
|
|
56
|
-
const candidateId = written.edgeCandidates[0].id;
|
|
57
|
-
const approved = cortex.graphCandidateAction({ action: 'approve', id: candidateId });
|
|
58
|
-
assert.equal(approved.metadata.status, 'active');
|
|
59
|
-
assert.equal(cortex.graphCandidates({ scopeId: 'global' }).length, 0);
|
|
60
|
-
assert.equal(cortex.graphData({ scopeId: 'global' }).edges.length, 1);
|
|
61
|
-
const audit = cortex.repos.audit.list({ targetType: 'edge', targetId: candidateId });
|
|
62
|
-
assert.ok(audit.some((row) => row.action === 'edge.candidate.approve'));
|
|
63
|
-
} finally {
|
|
64
|
-
cortex.close();
|
|
65
|
-
rmSync(dir, { recursive: true, force: true });
|
|
66
|
-
}
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
test('autoFacts queue candidate edges only when graphCandidateAutoFacts is enabled', () => {
|
|
70
|
-
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-edge-autofacts-'));
|
|
71
|
-
const cortex = createCortex({ config: { dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite'), features: { autoFacts: true, graphCandidateAutoFacts: true } } });
|
|
72
|
-
try {
|
|
73
|
-
const written = cortex.write({ text: '用户喜欢米饭。', explicit: true });
|
|
74
|
-
assert.ok(written.edgeCandidates.length >= 1);
|
|
75
|
-
assert.ok(cortex.graphCandidates({ scopeId: 'global' }).length >= 1);
|
|
76
|
-
assert.equal(cortex.graphData({ scopeId: 'global' }).edges.length, 0);
|
|
77
|
-
} finally {
|
|
78
|
-
cortex.close();
|
|
79
|
-
rmSync(dir, { recursive: true, force: true });
|
|
80
|
-
}
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
test('autoFacts do not queue candidate edges by default', () => {
|
|
84
|
-
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-edge-autofacts-off-'));
|
|
85
|
-
const cortex = createCortex({ config: { dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite'), features: { autoFacts: true } } });
|
|
86
|
-
try {
|
|
87
|
-
const written = cortex.write({ text: '用户喜欢米饭。', explicit: true });
|
|
88
|
-
assert.equal(written.edgeCandidates?.length ?? 0, 0);
|
|
89
|
-
assert.equal(cortex.graphCandidates({ scopeId: 'global' }).length, 0);
|
|
90
|
-
} finally {
|
|
91
|
-
cortex.close();
|
|
92
|
-
rmSync(dir, { recursive: true, force: true });
|
|
93
|
-
}
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
test('rejected candidates and candidate downgrade protection', () => {
|
|
97
|
-
const { dir, db, edges } = setup();
|
|
98
|
-
try {
|
|
99
|
-
const active = edges.upsert({ scopeId: 'global', fromId: 'ent_a', toId: 'ent_b', rel: 'related', weight: 1 });
|
|
100
|
-
const attempted = edges.addCandidate({ scopeId: 'global', fromId: 'ent_a', toId: 'ent_b', rel: 'related', confidence: 0.2 });
|
|
101
|
-
assert.equal(attempted.id, active.id);
|
|
102
|
-
assert.equal(edges.get(active.id).metadata.status, undefined);
|
|
103
|
-
|
|
104
|
-
const candidate = edges.addCandidate({ scopeId: 'global', fromId: 'ent_c', toId: 'ent_d', rel: 'related', confidence: 0.4 });
|
|
105
|
-
const rejected = edges.rejectCandidate(candidate.id);
|
|
106
|
-
assert.equal(rejected.metadata.status, 'rejected');
|
|
107
|
-
assert.equal(edges.list({ scopeId: 'global' }).length, 1);
|
|
108
|
-
assert.equal(edges.listCandidates({ scopeId: 'global' }).length, 0);
|
|
109
|
-
} finally {
|
|
110
|
-
closeDatabase(db);
|
|
111
|
-
rmSync(dir, { recursive: true, force: true });
|
|
112
|
-
}
|
|
113
|
-
});
|
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
import test from 'node:test';
|
|
2
|
-
import assert from 'node:assert/strict';
|
|
3
|
-
import { mkdtempSync, rmSync } from 'node:fs';
|
|
4
|
-
import os from 'node:os';
|
|
5
|
-
import path from 'node:path';
|
|
6
|
-
import { openDatabase, migrate, closeDatabase } from '../../src/store/database.js';
|
|
7
|
-
import { createEmotionModel } from '../../src/self/emotion.js';
|
|
8
|
-
import { createCortex } from '../../src/service/cortex.js';
|
|
9
|
-
|
|
10
|
-
function setup() {
|
|
11
|
-
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-emotion-'));
|
|
12
|
-
const { db } = openDatabase({ dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite') });
|
|
13
|
-
migrate(db);
|
|
14
|
-
return { dir, db, emotion: createEmotionModel({ db }) };
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
test('emotion detection covers multiple categories', () => {
|
|
18
|
-
const { dir, db, emotion } = setup();
|
|
19
|
-
try {
|
|
20
|
-
const result = emotion.detect('用户今天很难过,助手有点心疼他,又有点害怕。');
|
|
21
|
-
const names = result.emotions.map((item) => item.name);
|
|
22
|
-
assert.ok(names.includes('哀'));
|
|
23
|
-
assert.ok(names.includes('怜'));
|
|
24
|
-
assert.ok(names.includes('惧'));
|
|
25
|
-
} finally {
|
|
26
|
-
closeDatabase(db);
|
|
27
|
-
rmSync(dir, { recursive: true, force: true });
|
|
28
|
-
}
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
test('emotion records form a target attitude', () => {
|
|
32
|
-
const { dir, db, emotion } = setup();
|
|
33
|
-
try {
|
|
34
|
-
emotion.record({ text: '用户今天很难过,助手有点心疼他。', target: '用户' });
|
|
35
|
-
emotion.record({ text: '用户说会一直陪着助手,助手很开心。', target: '用户' });
|
|
36
|
-
const targets = emotion.targets();
|
|
37
|
-
assert.equal(targets.length, 1);
|
|
38
|
-
assert.equal(targets[0].target, '用户');
|
|
39
|
-
assert.ok(targets[0].count >= 2);
|
|
40
|
-
assert.match(targets[0].attitude, /又/);
|
|
41
|
-
assert.match(emotion.text(), /用户/);
|
|
42
|
-
assert.ok(emotion.events().length >= 2);
|
|
43
|
-
assert.equal(emotion.lexicon().categories, 24);
|
|
44
|
-
} finally {
|
|
45
|
-
closeDatabase(db);
|
|
46
|
-
rmSync(dir, { recursive: true, force: true });
|
|
47
|
-
}
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
test('service self exposes emotion text and develop records it', () => {
|
|
51
|
-
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-emotion-service-'));
|
|
52
|
-
const cortex = createCortex({ config: { dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite') } });
|
|
53
|
-
try {
|
|
54
|
-
const result = cortex.step({ text: '用户今天很难过,助手有点心疼他。', explicit: true });
|
|
55
|
-
assert.ok(result.emotion?.event);
|
|
56
|
-
// Default persona has no userAddress, so aliases fold onto the neutral '你'.
|
|
57
|
-
assert.match(cortex.self().emotion, /你/);
|
|
58
|
-
} finally {
|
|
59
|
-
cortex.close();
|
|
60
|
-
rmSync(dir, { recursive: true, force: true });
|
|
61
|
-
}
|
|
62
|
-
});
|
package/test/unit/encode.test.js
DELETED
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
import test from 'node:test';
|
|
2
|
-
import assert from 'node:assert/strict';
|
|
3
|
-
import { mkdtempSync, rmSync } from 'node:fs';
|
|
4
|
-
import os from 'node:os';
|
|
5
|
-
import path from 'node:path';
|
|
6
|
-
import { openDatabase, migrate, closeDatabase } from '../../src/store/database.js';
|
|
7
|
-
import { createEventRepository } from '../../src/store/repositories/events.js';
|
|
8
|
-
import { createTraceRepository } from '../../src/store/repositories/traces.js';
|
|
9
|
-
import { createEntityRepository } from '../../src/store/repositories/entities.js';
|
|
10
|
-
import { createFactRepository } from '../../src/store/repositories/facts.js';
|
|
11
|
-
import { createAuditRepository } from '../../src/store/repositories/audit.js';
|
|
12
|
-
import { createEncodeEngine, scoreTraceText, compressionValue } from '../../src/engines/encode.js';
|
|
13
|
-
|
|
14
|
-
function setup() {
|
|
15
|
-
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-encode-'));
|
|
16
|
-
const { db } = openDatabase({ dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite') });
|
|
17
|
-
migrate(db);
|
|
18
|
-
const events = createEventRepository(db);
|
|
19
|
-
const traces = createTraceRepository(db);
|
|
20
|
-
const entities = createEntityRepository(db);
|
|
21
|
-
const facts = createFactRepository(db);
|
|
22
|
-
const audit = createAuditRepository(db);
|
|
23
|
-
const engine = createEncodeEngine({ events, traces, entities, facts, audit, config: { minCompressionValue: 0.01 } });
|
|
24
|
-
return { dir, db, events, traces, entities, facts, audit, engine };
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
test('scoreTraceText detects promises, preferences and emotion polarity', () => {
|
|
28
|
-
const promise = scoreTraceText('用户说会买米饭,会一直在一起。');
|
|
29
|
-
assert.equal(promise.kind, 'promise');
|
|
30
|
-
assert.ok(promise.importance > 0.5);
|
|
31
|
-
const preference = scoreTraceText('助手喜欢米饭,不喜欢冷掉的米饭。');
|
|
32
|
-
assert.equal(preference.kind, 'preference');
|
|
33
|
-
const sad = scoreTraceText('用户今天很难过,助手有点心疼。');
|
|
34
|
-
assert.equal(sad.kind, 'emotion');
|
|
35
|
-
assert.ok(sad.valence < 0);
|
|
36
|
-
assert.ok(sad.salience > 0.5);
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
test('compression value falls as text gets longer', () => {
|
|
40
|
-
const short = compressionValue('米饭', { importance: 0.8, salience: 0.8 });
|
|
41
|
-
const long = compressionValue('米饭'.repeat(200), { importance: 0.8, salience: 0.8 });
|
|
42
|
-
assert.ok(short > long);
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
test('encode writes a trace, audit row and explicit facts', () => {
|
|
46
|
-
const { dir, db, events, traces, facts, audit, engine } = setup();
|
|
47
|
-
try {
|
|
48
|
-
const result = engine.encode({
|
|
49
|
-
text: '用户说会买米饭,助手会一直等。',
|
|
50
|
-
actor: 'user',
|
|
51
|
-
explicit: true,
|
|
52
|
-
facts: [{ subject: '用户', predicate: 'likes', object: '米饭' }],
|
|
53
|
-
});
|
|
54
|
-
assert.equal(result.skipped, false);
|
|
55
|
-
assert.equal(result.trace.kind, 'promise');
|
|
56
|
-
assert.equal(events.count(), 1);
|
|
57
|
-
assert.equal(traces.count(), 1);
|
|
58
|
-
assert.equal(result.facts.length, 1);
|
|
59
|
-
assert.equal(result.facts[0].action, 'created');
|
|
60
|
-
const ownerFact = facts.list({ scopeId: 'global' });
|
|
61
|
-
assert.equal(ownerFact.length, 1);
|
|
62
|
-
assert.equal(audit.list({ limit: 10 }).some((item) => item.action === 'trace.create'), true);
|
|
63
|
-
} finally {
|
|
64
|
-
closeDatabase(db);
|
|
65
|
-
rmSync(dir, { recursive: true, force: true });
|
|
66
|
-
}
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
test('encode is idempotent by operationId and skips duplicate text', () => {
|
|
70
|
-
const { dir, db, events, traces, engine } = setup();
|
|
71
|
-
try {
|
|
72
|
-
const first = engine.encode({ text: '用户今天很开心,助手也开心。', operationId: 'op-1', explicit: true });
|
|
73
|
-
const replay = engine.encode({ text: '用户今天很开心,助手也开心。', operationId: 'op-1', explicit: true });
|
|
74
|
-
assert.equal(replay.skipped, true);
|
|
75
|
-
assert.equal(replay.reason, 'idempotent');
|
|
76
|
-
assert.equal(replay.trace.id, first.trace.id);
|
|
77
|
-
assert.equal(events.count(), 1);
|
|
78
|
-
const duplicate = engine.encode({ text: '用户今天很开心,助手也开心。', explicit: true });
|
|
79
|
-
assert.equal(duplicate.skipped, true);
|
|
80
|
-
assert.equal(duplicate.reason, 'duplicate');
|
|
81
|
-
assert.equal(traces.count(), 1);
|
|
82
|
-
} finally {
|
|
83
|
-
closeDatabase(db);
|
|
84
|
-
rmSync(dir, { recursive: true, force: true });
|
|
85
|
-
}
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
test('short and stop-phrase inputs are skipped before writing an event', () => {
|
|
89
|
-
const { dir, db, events, engine } = setup();
|
|
90
|
-
try {
|
|
91
|
-
assert.equal(engine.encode({ text: '嗯' }).skipped, true);
|
|
92
|
-
assert.equal(engine.encode({ text: '好' }).skipped, true);
|
|
93
|
-
assert.equal(events.count(), 0);
|
|
94
|
-
} finally {
|
|
95
|
-
closeDatabase(db);
|
|
96
|
-
rmSync(dir, { recursive: true, force: true });
|
|
97
|
-
}
|
|
98
|
-
});
|