dsh-cortex-memory 0.4.11 → 0.4.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +111 -0
- package/LICENSE +1 -1
- package/README.md +63 -38
- package/bench/ab-engine.mjs +60 -0
- package/bench/ann-bench.mjs +78 -0
- package/bench/hnsw-bench.mjs +79 -0
- package/bench/locomo.js +89 -0
- package/bench/longmemeval.js +83 -0
- package/bench/metrics.js +67 -0
- package/bench/migration-bench.mjs +69 -0
- package/bench/run.js +77 -0
- package/bench/synthetic.js +99 -0
- package/bench/vector-ab.mjs +76 -0
- package/docs/compatibility.md +39 -0
- package/docs/install-windows.md +145 -0
- package/docs/models.md +183 -0
- package/docs/multimodal.md +112 -0
- package/docs/permissions.md +53 -0
- package/docs/privacy.md +50 -0
- package/docs/scopes.md +142 -0
- package/examples/README.md +73 -0
- package/examples/setup-cortex-bge.ps1 +66 -0
- package/examples/uninstall-cortex.ps1 +92 -0
- package/index.js +23 -0
- package/package.json +11 -5
- package/src/core/config.js +27 -0
- package/src/engines/extract.js +10 -10
- package/src/engines/retrieve.js +30 -5
- package/src/guardian/guardian.js +2 -15
- package/src/guardian/invariants.js +9 -5
- package/src/host/runtime.js +10 -14
- package/src/self/evolution.js +32 -5
- package/src/self/policies.js +21 -2
- package/src/service/cortex.js +3 -2
- package/src/store/database.js +9 -1
- package/src/store/schema.sql +2 -1
- package/src/vector/hnsw.js +5 -5
- package/test/integration/_helpers.js +128 -0
- package/test/integration/acceptance.test.js +803 -0
- package/test/integration/ann-load.test.js +61 -0
- package/test/integration/api-contract.test.js +1476 -0
- package/test/integration/backup-privacy.test.js +214 -0
- package/test/integration/consolidate.test.js +75 -0
- package/test/integration/edge-cases.test.js +1088 -0
- package/test/integration/exchange.test.js +326 -0
- package/test/integration/life-loop.test.js +188 -0
- package/test/integration/load.test.js +751 -0
- package/test/integration/markdown-sync.test.js +35 -0
- package/test/integration/policy-query-api.test.js +306 -0
- package/test/integration/resilience-writes.test.js +241 -0
- package/test/integration/tools-contract.test.js +1324 -0
- package/test/unit/ann-edge.test.js +102 -0
- package/test/unit/ann.test.js +77 -0
- package/test/unit/api-client.test.js +576 -0
- package/test/unit/api-validate.test.js +19 -0
- package/test/unit/api.test.js +555 -0
- package/test/unit/appraisal.test.js +48 -0
- package/test/unit/attachments.test.js +33 -0
- package/test/unit/autonomy.test.js +53 -0
- package/test/unit/batch.test.js +46 -0
- package/test/unit/bench.test.js +40 -0
- package/test/unit/benchmark-synthetic.test.js +52 -0
- package/test/unit/client-contract.test.js +29 -0
- package/test/unit/compression.test.js +76 -0
- package/test/unit/core.test.js +77 -0
- package/test/unit/develop.test.js +130 -0
- package/test/unit/development.test.js +68 -0
- package/test/unit/digest.test.js +26 -0
- package/test/unit/dsr.test.js +111 -0
- package/test/unit/edges-candidates.test.js +113 -0
- package/test/unit/emotion.test.js +61 -0
- package/test/unit/encode.test.js +98 -0
- package/test/unit/evolution.test.js +130 -0
- package/test/unit/external-ann-search.test.js +50 -0
- package/test/unit/external-ann.test.js +29 -0
- package/test/unit/facts-belief.test.js +32 -0
- package/test/unit/facts.test.js +107 -0
- package/test/unit/feature-overrides.test.js +42 -0
- package/test/unit/fts-migration.test.js +42 -0
- package/test/unit/graph.test.js +79 -0
- package/test/unit/growth-reflect.test.js +123 -0
- package/test/unit/guard-runtime.test.js +40 -0
- package/test/unit/guardian.test.js +91 -0
- package/test/unit/health-privacy.test.js +102 -0
- package/test/unit/hnsw.test.js +35 -0
- package/test/unit/host-runtime.test.js +142 -0
- package/test/unit/host.test.js +58 -0
- package/test/unit/identity.test.js +98 -0
- package/test/unit/induct-evaluate.test.js +41 -0
- package/test/unit/ingest-extracted.test.js +35 -0
- package/test/unit/initiative.test.js +90 -0
- package/test/unit/jobs.test.js +93 -0
- package/test/unit/json-snapshot.test.js +57 -0
- package/test/unit/life-consolidate.test.js +34 -0
- package/test/unit/life.test.js +149 -0
- package/test/unit/llm-facts-integration.test.js +27 -0
- package/test/unit/llm-facts.test.js +16 -0
- package/test/unit/llm.test.js +45 -0
- package/test/unit/locomo.test.js +45 -0
- package/test/unit/longmemeval.test.js +56 -0
- package/test/unit/markdown-import.test.js +44 -0
- package/test/unit/markdown.test.js +34 -0
- package/test/unit/memory-edit.test.js +112 -0
- package/test/unit/metrics.test.js +26 -0
- package/test/unit/migration.test.js +38 -0
- package/test/unit/model-config.test.js +52 -0
- package/test/unit/narrative.test.js +62 -0
- package/test/unit/playbook-consolidate.test.js +24 -0
- package/test/unit/privacy-scope.test.js +39 -0
- package/test/unit/profile.test.js +20 -0
- package/test/unit/prompt.test.js +42 -0
- package/test/unit/providers.test.js +42 -0
- package/test/unit/qa.test.js +100 -0
- package/test/unit/query.test.js +53 -0
- package/test/unit/retention.test.js +31 -0
- package/test/unit/retrieval-ranking.test.js +31 -0
- package/test/unit/retrieve.test.js +117 -0
- package/test/unit/rotation.test.js +25 -0
- package/test/unit/scanner.test.js +50 -0
- package/test/unit/scenario.test.js +78 -0
- package/test/unit/scheduler.test.js +136 -0
- package/test/unit/self-state.test.js +94 -0
- package/test/unit/service.test.js +72 -0
- package/test/unit/shiori-import.test.js +77 -0
- package/test/unit/skills.test.js +104 -0
- package/test/unit/store.test.js +111 -0
- package/test/unit/temporal-graph.test.js +44 -0
- package/test/unit/tiers.test.js +39 -0
- package/test/unit/tokenizer-fts.test.js +35 -0
- package/test/unit/tools.test.js +122 -0
- package/test/unit/upgrade-0412.test.js +104 -0
- package/test/unit/upgrade-0413.test.js +84 -0
- package/test/unit/upgrade-0414.test.js +101 -0
- package/test/unit/vector-ann.test.js +53 -0
- package/test/unit/vector-async.test.js +69 -0
- package/test/unit/vector.test.js +68 -0
- package/test/unit/working.test.js +80 -0
package/src/store/database.js
CHANGED
|
@@ -9,7 +9,7 @@ import { ftsText } from '../memory/tokenizer.js';
|
|
|
9
9
|
|
|
10
10
|
const HERE = path.dirname(fileURLToPath(import.meta.url));
|
|
11
11
|
const SCHEMA_FILE = path.join(HERE, 'schema.sql');
|
|
12
|
-
export const SCHEMA_VERSION =
|
|
12
|
+
export const SCHEMA_VERSION = 7;
|
|
13
13
|
export const FTS_INDEX_VERSION = 2;
|
|
14
14
|
|
|
15
15
|
function ensureTraceColumns(db) {
|
|
@@ -28,6 +28,13 @@ function ensureSummonColumns(db) {
|
|
|
28
28
|
}
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
+
function ensurePolicyColumns(db) {
|
|
32
|
+
const columns = db.prepare('PRAGMA table_info(policies)').all();
|
|
33
|
+
if (!columns.some((column) => column.name === 'canary_json')) {
|
|
34
|
+
db.exec('ALTER TABLE policies ADD COLUMN canary_json TEXT');
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
31
38
|
function ensureFactsColumns(db) {
|
|
32
39
|
const columns = db.prepare('PRAGMA table_info(facts)').all();
|
|
33
40
|
if (!columns.some((column) => column.name === 'superseded_at')) {
|
|
@@ -86,6 +93,7 @@ export function migrate(db, { version = SCHEMA_VERSION } = {}) {
|
|
|
86
93
|
ensureTraceColumns(db);
|
|
87
94
|
ensureFactsColumns(db);
|
|
88
95
|
ensureSummonColumns(db);
|
|
96
|
+
ensurePolicyColumns(db);
|
|
89
97
|
const row = db.prepare('SELECT value FROM meta WHERE key = ?').get('schema_version');
|
|
90
98
|
if (!row) {
|
|
91
99
|
db.prepare('INSERT INTO meta(key, value, updated_at) VALUES (?, ?, ?)').run('schema_version', String(version), nowIso());
|
package/src/store/schema.sql
CHANGED
|
@@ -259,7 +259,8 @@ CREATE TABLE IF NOT EXISTS policies (
|
|
|
259
259
|
metrics_json TEXT,
|
|
260
260
|
status TEXT NOT NULL DEFAULT 'candidate',
|
|
261
261
|
created_at TEXT NOT NULL,
|
|
262
|
-
activated_at TEXT
|
|
262
|
+
activated_at TEXT,
|
|
263
|
+
canary_json TEXT
|
|
263
264
|
);
|
|
264
265
|
|
|
265
266
|
CREATE TABLE IF NOT EXISTS audit (
|
package/src/vector/hnsw.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Small dependency-free HNSW index for Cortex.
|
|
3
3
|
*
|
|
4
|
-
* This is
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
4
|
+
* This is the default ANN engine in `src/vector/index.js` (`annEngine: 'hnsw'`);
|
|
5
|
+
* the older single-layer NSW implementation remains selectable as `'nsw'`.
|
|
6
|
+
* The implementation uses simple sorted frontiers (not a binary heap) because
|
|
7
|
+
* it is easier to audit; run `bench/hnsw-bench.mjs` before changing parameters
|
|
8
|
+
* for a large corpus.
|
|
9
9
|
*/
|
|
10
10
|
function mulberry32(seed) {
|
|
11
11
|
let state = seed >>> 0;
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared helpers for the integration suite.
|
|
3
|
+
*
|
|
4
|
+
* The tests are deliberately black-box: every helper here only creates real
|
|
5
|
+
* cortex instances in throwaway directories, drives real repository/engine
|
|
6
|
+
* code, and cleans up afterwards. Nothing mocks the module under test.
|
|
7
|
+
*/
|
|
8
|
+
import { mkdtempSync, mkdirSync, rmSync, writeFileSync } from 'node:fs';
|
|
9
|
+
import os from 'node:os';
|
|
10
|
+
import path from 'node:path';
|
|
11
|
+
import { Readable } from 'node:stream';
|
|
12
|
+
import { createCortex } from '../../src/service/cortex.js';
|
|
13
|
+
import { API_BASE } from '../../src/api/routes.js';
|
|
14
|
+
|
|
15
|
+
export const DAY_MS = 86_400_000;
|
|
16
|
+
/** Fixed reference instant: 2026-11-11T10:00:00+08:00. */
|
|
17
|
+
export const BASE_MS = Date.parse('2026-11-11T10:00:00+08:00');
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Create a cortex backed by a fresh temporary directory.
|
|
21
|
+
* Returns the directory (for cleanup) and the service.
|
|
22
|
+
*/
|
|
23
|
+
export function makeCortex(config = {}) {
|
|
24
|
+
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-integration-'));
|
|
25
|
+
const dataRoot = config.dataRoot ?? path.join(dir, 'data');
|
|
26
|
+
const dbFile = config.dbFile ?? path.join(dataRoot, 'cortex.sqlite');
|
|
27
|
+
const cortex = createCortex({ config: { ...config, dataRoot, dbFile } });
|
|
28
|
+
return { dir, cortex };
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Close every cortex first, then delete every temporary directory. Safe to
|
|
33
|
+
* call from a finally block even when the test failed halfway through setup.
|
|
34
|
+
*/
|
|
35
|
+
export function cleanup(...contexts) {
|
|
36
|
+
for (const context of contexts) {
|
|
37
|
+
try {
|
|
38
|
+
context?.cortex?.close();
|
|
39
|
+
} catch {
|
|
40
|
+
/* closing is best-effort, cleanup must not mask the real assertion */
|
|
41
|
+
}
|
|
42
|
+
if (context?.dir) rmSync(context.dir, { recursive: true, force: true });
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Request stream shaped the way the API handler expects node:http objects. */
|
|
47
|
+
export function apiRequest({
|
|
48
|
+
method = 'GET',
|
|
49
|
+
route = '/ping',
|
|
50
|
+
body = null,
|
|
51
|
+
headers = {},
|
|
52
|
+
remoteAddress = '127.0.0.1',
|
|
53
|
+
host = '127.0.0.1:7430',
|
|
54
|
+
origin = 'http://127.0.0.1:7430',
|
|
55
|
+
site = 'same-origin',
|
|
56
|
+
url = null,
|
|
57
|
+
} = {}) {
|
|
58
|
+
const stream = Readable.from(body === null ? [] : [Buffer.from(JSON.stringify(body), 'utf8')]);
|
|
59
|
+
stream.method = method;
|
|
60
|
+
stream.url = url ?? `${API_BASE}${route}`;
|
|
61
|
+
const merged = {
|
|
62
|
+
host,
|
|
63
|
+
origin,
|
|
64
|
+
'sec-fetch-site': site,
|
|
65
|
+
...(body === null ? {} : { 'content-type': 'application/json' }),
|
|
66
|
+
...headers,
|
|
67
|
+
};
|
|
68
|
+
for (const [key, value] of Object.entries(merged)) {
|
|
69
|
+
if (value === undefined) delete merged[key];
|
|
70
|
+
}
|
|
71
|
+
stream.headers = merged;
|
|
72
|
+
stream.socket = { remoteAddress };
|
|
73
|
+
return stream;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Minimal ServerResponse recorder: enough for the route handler. */
|
|
77
|
+
export function apiResponse() {
|
|
78
|
+
return {
|
|
79
|
+
statusCode: 0,
|
|
80
|
+
headers: null,
|
|
81
|
+
body: '',
|
|
82
|
+
writeHead(code, headers) {
|
|
83
|
+
this.statusCode = code;
|
|
84
|
+
this.headers = headers ?? null;
|
|
85
|
+
},
|
|
86
|
+
end(chunk) {
|
|
87
|
+
if (chunk != null) this.body += String(chunk);
|
|
88
|
+
},
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export async function invokeApi(handler, options = {}) {
|
|
93
|
+
const response = apiResponse();
|
|
94
|
+
await handler(apiRequest(options), response);
|
|
95
|
+
return { status: response.statusCode, headers: response.headers, json: JSON.parse(response.body || '{}') };
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Split an exported MEMORY.md mirror into its per-memory frontmatter blocks.
|
|
100
|
+
*
|
|
101
|
+
* The exporter writes one multi-entry document; the importer expects one
|
|
102
|
+
* memory per file. This helper converts the mirror into the vault shape the
|
|
103
|
+
* importer documents, using the same textual boundaries the exporter emits.
|
|
104
|
+
*/
|
|
105
|
+
export function splitMirrorEntries(markdown) {
|
|
106
|
+
const entries = [];
|
|
107
|
+
const pattern = /^---\n([\s\S]*?)\n---\n\n([\s\S]*?)(?=\n---\n|$)/gm;
|
|
108
|
+
for (const match of String(markdown ?? '').matchAll(pattern)) {
|
|
109
|
+
entries.push({ frontmatter: match[1], body: match[2].trim() });
|
|
110
|
+
}
|
|
111
|
+
return entries;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** Write one frontmatter memory file into a vault directory. */
|
|
115
|
+
export function writeVaultEntry(dir, name, { frontmatter, body }) {
|
|
116
|
+
mkdirSync(dir, { recursive: true });
|
|
117
|
+
const file = path.join(dir, name);
|
|
118
|
+
writeFileSync(file, `---\n${frontmatter}\n---\n\n${body}\n`, 'utf8');
|
|
119
|
+
return file;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** Write a raw file, mainly for hand-authored vault fixtures. */
|
|
123
|
+
export function writeRaw(dir, name, content) {
|
|
124
|
+
mkdirSync(dir, { recursive: true });
|
|
125
|
+
const file = path.join(dir, name);
|
|
126
|
+
writeFileSync(file, content, 'utf8');
|
|
127
|
+
return file;
|
|
128
|
+
}
|