infinitydb 1.0.0 → 1.4.0
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/LICENSE +118 -118
- package/README.md +186 -137
- package/index.d.ts +45 -43
- package/index.js +829 -330
- package/package.json +39 -35
package/index.js
CHANGED
|
@@ -1,330 +1,829 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const crypto = require("crypto");
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* InfinityDB - High-performance Node.js Agent Memory Engine.
|
|
7
|
-
* Implements L0-L6 brain architecture:
|
|
8
|
-
* L0: Raw logs offloaded from LLM context
|
|
9
|
-
* L1: Atomic facts (zero-LLM regex/rule extraction)
|
|
10
|
-
* L2: Causal & temporal graph
|
|
11
|
-
* L3: Dynamic persona with conflict auto-resolution
|
|
12
|
-
* L4: Enterprise policies & SOPs
|
|
13
|
-
* L5: Ebbinghaus forgetting curve decay
|
|
14
|
-
* L6: Cryptographic SHA-256 proof chain
|
|
15
|
-
*/
|
|
16
|
-
class InfinityDB {
|
|
17
|
-
constructor(options = {}) {
|
|
18
|
-
this.options = options;
|
|
19
|
-
this.
|
|
20
|
-
this.
|
|
21
|
-
this.
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
this.
|
|
38
|
-
this.
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
if (!
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
const
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const crypto = require("crypto");
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* InfinityDB - High-performance Node.js Agent Memory Engine.
|
|
7
|
+
* Implements L0-L6 brain architecture:
|
|
8
|
+
* L0: Raw logs offloaded from LLM context
|
|
9
|
+
* L1: Atomic facts (zero-LLM regex/rule extraction)
|
|
10
|
+
* L2: Causal & temporal graph
|
|
11
|
+
* L3: Dynamic persona with conflict auto-resolution
|
|
12
|
+
* L4: Enterprise policies & SOPs
|
|
13
|
+
* L5: Ebbinghaus forgetting curve decay
|
|
14
|
+
* L6: Cryptographic SHA-256 proof chain
|
|
15
|
+
*/
|
|
16
|
+
class InfinityDB {
|
|
17
|
+
constructor(options = {}) {
|
|
18
|
+
this.options = options;
|
|
19
|
+
this._seq = 0;
|
|
20
|
+
this._lastTimestamp = 0;
|
|
21
|
+
this._storage = null;
|
|
22
|
+
if (options.dbPath) {
|
|
23
|
+
const Database = require("better-sqlite3");
|
|
24
|
+
this._storage = new Database(options.dbPath);
|
|
25
|
+
this._storage.pragma("journal_mode = WAL");
|
|
26
|
+
this._storage.pragma("synchronous = FULL");
|
|
27
|
+
this._storage.pragma("busy_timeout = 5000");
|
|
28
|
+
this._initStorage();
|
|
29
|
+
}
|
|
30
|
+
this.logs = new Map();
|
|
31
|
+
this.atoms = new Map();
|
|
32
|
+
this.episodes = new Map();
|
|
33
|
+
this.personas = new Map();
|
|
34
|
+
this.policies = new Map();
|
|
35
|
+
this.proofs = new Map();
|
|
36
|
+
this.legacyMemories = new Map();
|
|
37
|
+
this.relations = [];
|
|
38
|
+
this._loadState();
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
_initStorage() {
|
|
42
|
+
this._storage.exec(`
|
|
43
|
+
CREATE TABLE IF NOT EXISTS infinitydb_meta (key TEXT PRIMARY KEY, value TEXT NOT NULL);
|
|
44
|
+
CREATE TABLE IF NOT EXISTS l0_logs (id TEXT PRIMARY KEY, user_id TEXT NOT NULL, payload TEXT NOT NULL);
|
|
45
|
+
CREATE INDEX IF NOT EXISTS idx_js_l0_user ON l0_logs(user_id);
|
|
46
|
+
CREATE TABLE IF NOT EXISTS l1_atoms (id TEXT PRIMARY KEY, user_id TEXT NOT NULL, source_id TEXT NOT NULL, fact TEXT NOT NULL, ts REAL NOT NULL, payload TEXT NOT NULL);
|
|
47
|
+
CREATE TABLE IF NOT EXISTS l1_atom_relations (old_atom_id TEXT NOT NULL, new_atom_id TEXT NOT NULL, user_id TEXT NOT NULL, relation TEXT NOT NULL CHECK(relation='supersedes'), created_at REAL NOT NULL, PRIMARY KEY(old_atom_id,new_atom_id), CHECK(old_atom_id <> new_atom_id));
|
|
48
|
+
CREATE INDEX IF NOT EXISTS idx_js_atom_relations_user_time ON l1_atom_relations(user_id, created_at);
|
|
49
|
+
CREATE INDEX IF NOT EXISTS idx_js_l1_user_ts ON l1_atoms(user_id, ts);
|
|
50
|
+
CREATE INDEX IF NOT EXISTS idx_js_l1_source ON l1_atoms(source_id);
|
|
51
|
+
CREATE VIRTUAL TABLE IF NOT EXISTS l1_atoms_fts
|
|
52
|
+
USING fts5(fact, content='l1_atoms', content_rowid='rowid',
|
|
53
|
+
tokenize='unicode61 remove_diacritics 2');
|
|
54
|
+
CREATE TRIGGER IF NOT EXISTS l1_atoms_fts_insert AFTER INSERT ON l1_atoms BEGIN
|
|
55
|
+
INSERT INTO l1_atoms_fts(rowid, fact) VALUES (new.rowid, new.fact);
|
|
56
|
+
END;
|
|
57
|
+
CREATE TRIGGER IF NOT EXISTS l1_atoms_fts_delete AFTER DELETE ON l1_atoms BEGIN
|
|
58
|
+
INSERT INTO l1_atoms_fts(l1_atoms_fts, rowid, fact)
|
|
59
|
+
VALUES ('delete', old.rowid, old.fact);
|
|
60
|
+
END;
|
|
61
|
+
CREATE TRIGGER IF NOT EXISTS l1_atoms_fts_update AFTER UPDATE OF fact ON l1_atoms
|
|
62
|
+
WHEN old.fact <> new.fact BEGIN
|
|
63
|
+
INSERT INTO l1_atoms_fts(l1_atoms_fts, rowid, fact)
|
|
64
|
+
VALUES ('delete', old.rowid, old.fact);
|
|
65
|
+
INSERT INTO l1_atoms_fts(rowid, fact) VALUES (new.rowid, new.fact);
|
|
66
|
+
END;
|
|
67
|
+
CREATE TABLE IF NOT EXISTS l2_episodes (id TEXT PRIMARY KEY, user_id TEXT NOT NULL, source_id TEXT NOT NULL, payload TEXT NOT NULL);
|
|
68
|
+
CREATE INDEX IF NOT EXISTS idx_js_l2_user ON l2_episodes(user_id);
|
|
69
|
+
CREATE TABLE IF NOT EXISTS l3_personas (id TEXT PRIMARY KEY, user_id TEXT NOT NULL, payload TEXT NOT NULL);
|
|
70
|
+
CREATE INDEX IF NOT EXISTS idx_js_l3_user ON l3_personas(user_id);
|
|
71
|
+
CREATE TABLE IF NOT EXISTS l4_policies (id TEXT PRIMARY KEY, payload TEXT NOT NULL);
|
|
72
|
+
CREATE TABLE IF NOT EXISTS l6_proofs (atom_id TEXT PRIMARY KEY, source_log_id TEXT NOT NULL, payload TEXT NOT NULL);
|
|
73
|
+
CREATE INDEX IF NOT EXISTS idx_js_l6_source ON l6_proofs(source_log_id);
|
|
74
|
+
CREATE TABLE IF NOT EXISTS legacy_memories (memory_key TEXT PRIMARY KEY, payload TEXT NOT NULL);
|
|
75
|
+
`);
|
|
76
|
+
const version = this._storage.prepare("SELECT value FROM infinitydb_meta WHERE key='schema_version'").get();
|
|
77
|
+
if (!version) {
|
|
78
|
+
const legacyTable = this._storage.prepare("SELECT 1 FROM sqlite_master WHERE type='table' AND name='infinitydb_state'").get();
|
|
79
|
+
this._storage.transaction(() => {
|
|
80
|
+
if (legacyTable) {
|
|
81
|
+
const row = this._storage.prepare("SELECT schema_version, payload FROM infinitydb_state WHERE id=1").get();
|
|
82
|
+
if (row) {
|
|
83
|
+
if (row.schema_version !== 1) throw new Error(`Unsupported InfinityDB storage schema version: ${row.schema_version}`);
|
|
84
|
+
const state = JSON.parse(row.payload);
|
|
85
|
+
this._seq = state.seq || 0;
|
|
86
|
+
this._writeEntries("l0_logs", "id", state.logs || [], entry => [entry[1].id, entry[1].userId, JSON.stringify(entry[1])]);
|
|
87
|
+
this._writeEntries("l1_atoms", "id", state.atoms || [], entry => [entry[1].id, entry[1].userId, entry[1].sourceId, entry[1].fact, entry[1].ts, JSON.stringify(entry[1])]);
|
|
88
|
+
this._writeEntries("l2_episodes", "id", state.episodes || [], entry => [entry[1].id, entry[1].userId, entry[1].sourceId, JSON.stringify(entry[1])]);
|
|
89
|
+
this._writeEntries("l3_personas", "id", state.personas || [], entry => [entry[0], entry[1].userId, JSON.stringify(entry[1])]);
|
|
90
|
+
this._writeEntries("l4_policies", "id", state.policies || [], entry => [entry[1].id, JSON.stringify(entry[1])]);
|
|
91
|
+
this._writeEntries("l6_proofs", "atom_id", state.proofs || [], entry => [entry[1].atomId, entry[1].sourceLogId, JSON.stringify(entry[1])]);
|
|
92
|
+
this._writeEntries("legacy_memories", "memory_key", state.legacyMemories || [], v => [v[0], JSON.stringify(v[1])]);
|
|
93
|
+
}
|
|
94
|
+
this._storage.exec("DROP TABLE infinitydb_state");
|
|
95
|
+
}
|
|
96
|
+
this._storage.prepare("INSERT INTO infinitydb_meta(key,value) VALUES ('schema_version','2')").run();
|
|
97
|
+
this._storage.prepare("INSERT INTO infinitydb_meta(key,value) VALUES ('sequence',?)").run(String(this._seq));
|
|
98
|
+
})();
|
|
99
|
+
} else if (version.value !== "2") {
|
|
100
|
+
throw new Error(`Unsupported InfinityDB storage schema version: ${version.value}`);
|
|
101
|
+
}
|
|
102
|
+
const ftsVersion = this._storage.prepare("SELECT value FROM infinitydb_meta WHERE key='l1_atoms_fts_version'").get();
|
|
103
|
+
if (!ftsVersion) {
|
|
104
|
+
this._storage.transaction(() => {
|
|
105
|
+
this._storage.prepare("INSERT INTO l1_atoms_fts(l1_atoms_fts) VALUES ('rebuild')").run();
|
|
106
|
+
this._storage.prepare("INSERT INTO infinitydb_meta(key,value) VALUES ('l1_atoms_fts_version','1')").run();
|
|
107
|
+
})();
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
_writeEntries(table, keyColumn, entries, mapper) {
|
|
112
|
+
const columns = {
|
|
113
|
+
l0_logs: "id, user_id, payload", l1_atoms: "id, user_id, source_id, fact, ts, payload",
|
|
114
|
+
l2_episodes: "id, user_id, source_id, payload", l3_personas: "id, user_id, payload",
|
|
115
|
+
l4_policies: "id, payload", l6_proofs: "atom_id, source_log_id, payload",
|
|
116
|
+
legacy_memories: "memory_key, payload",
|
|
117
|
+
}[table];
|
|
118
|
+
const values = columns.split(", ").map(() => "?").join(", ");
|
|
119
|
+
const updates = columns.split(", ").slice(1).map(col => `${col}=excluded.${col}`).join(", ");
|
|
120
|
+
const stmt = this._storage.prepare(`INSERT INTO ${table} (${columns}) VALUES (${values}) ON CONFLICT(${keyColumn}) DO UPDATE SET ${updates}`);
|
|
121
|
+
for (const entry of entries) stmt.run(...mapper(entry));
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
_loadState() {
|
|
125
|
+
if (!this._storage) return;
|
|
126
|
+
const loadSnapshot = () => {
|
|
127
|
+
const get = (sql, key) => this._storage.prepare(sql).all().map(row => [row[key], JSON.parse(row.payload)]);
|
|
128
|
+
this._seq = Number(this._storage.prepare("SELECT value FROM infinitydb_meta WHERE key='sequence'").get()?.value || 0);
|
|
129
|
+
this.logs = new Map(get("SELECT id, payload FROM l0_logs", "id"));
|
|
130
|
+
this.atoms = new Map(get("SELECT id, payload FROM l1_atoms", "id"));
|
|
131
|
+
this.episodes = new Map(get("SELECT id, payload FROM l2_episodes", "id"));
|
|
132
|
+
this.personas = new Map(get("SELECT id, payload FROM l3_personas", "id"));
|
|
133
|
+
this.policies = new Map(get("SELECT id, payload FROM l4_policies", "id"));
|
|
134
|
+
this.proofs = new Map(get("SELECT atom_id AS id, payload FROM l6_proofs", "id"));
|
|
135
|
+
this.legacyMemories = new Map(get("SELECT memory_key AS id, payload FROM legacy_memories", "id"));
|
|
136
|
+
this.relations = this._storage.prepare("SELECT old_atom_id AS oldAtomId, new_atom_id AS newAtomId, user_id AS userId, relation, created_at AS createdAt FROM l1_atom_relations").all();
|
|
137
|
+
this._persisted = {
|
|
138
|
+
logs: this._snapshotMap(this.logs),
|
|
139
|
+
atoms: this._snapshotMap(this.atoms),
|
|
140
|
+
episodes: this._snapshotMap(this.episodes),
|
|
141
|
+
personas: this._snapshotMap(this.personas),
|
|
142
|
+
policies: this._snapshotMap(this.policies),
|
|
143
|
+
proofs: this._snapshotMap(this.proofs),
|
|
144
|
+
legacyMemories: this._snapshotMap(this.legacyMemories),
|
|
145
|
+
relations: new Set(this.relations.map(r => `${r.oldAtomId}\0${r.newAtomId}`)),
|
|
146
|
+
};
|
|
147
|
+
for (const atom of this.atoms.values()) {
|
|
148
|
+
if (atom.ts > this._lastTimestamp) this._lastTimestamp = atom.ts;
|
|
149
|
+
}
|
|
150
|
+
};
|
|
151
|
+
this._storage.transaction(loadSnapshot)();
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
_refreshState() {
|
|
155
|
+
if (this._storage) this._loadState();
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
_allocateSequence() {
|
|
159
|
+
if (!this._storage) return ++this._seq;
|
|
160
|
+
const allocate = this._storage.transaction(() => {
|
|
161
|
+
const current = Number(this._storage.prepare(
|
|
162
|
+
"SELECT value FROM infinitydb_meta WHERE key='sequence'"
|
|
163
|
+
).get()?.value || 0);
|
|
164
|
+
const next = current + 1;
|
|
165
|
+
this._storage.prepare(
|
|
166
|
+
"UPDATE infinitydb_meta SET value=? WHERE key='sequence'"
|
|
167
|
+
).run(String(next));
|
|
168
|
+
return next;
|
|
169
|
+
});
|
|
170
|
+
this._seq = allocate();
|
|
171
|
+
return this._seq;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
_snapshotMap(map) {
|
|
175
|
+
return new Map(Array.from(map.entries(), ([key, value]) => [key, JSON.stringify(value)]));
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
_changedEntries(map, snapshot) {
|
|
179
|
+
return Array.from(map.entries()).filter(([key, value]) =>
|
|
180
|
+
!snapshot.has(key) || snapshot.get(key) !== JSON.stringify(value)
|
|
181
|
+
);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
_persist() {
|
|
185
|
+
if (!this._storage) return;
|
|
186
|
+
const previous = this._persisted || {};
|
|
187
|
+
const snapshots = {
|
|
188
|
+
logs: this._snapshotMap(this.logs),
|
|
189
|
+
atoms: this._snapshotMap(this.atoms),
|
|
190
|
+
episodes: this._snapshotMap(this.episodes),
|
|
191
|
+
personas: this._snapshotMap(this.personas),
|
|
192
|
+
policies: this._snapshotMap(this.policies),
|
|
193
|
+
proofs: this._snapshotMap(this.proofs),
|
|
194
|
+
legacyMemories: this._snapshotMap(this.legacyMemories),
|
|
195
|
+
};
|
|
196
|
+
const removed = (current, snapshot) => Array.from(snapshot.keys()).filter(key => !current.has(key));
|
|
197
|
+
this._storage.transaction(() => {
|
|
198
|
+
this._writeEntries("l0_logs", "id", this._changedEntries(this.logs, previous.logs || new Map()), v => [v[1].id, v[1].userId, JSON.stringify(v[1])]);
|
|
199
|
+
this._writeEntries("l1_atoms", "id", this._changedEntries(this.atoms, previous.atoms || new Map()), v => [v[1].id, v[1].userId, v[1].sourceId, v[1].fact, v[1].ts, JSON.stringify(v[1])]);
|
|
200
|
+
this._writeEntries("l2_episodes", "id", this._changedEntries(this.episodes, previous.episodes || new Map()), v => [v[1].id, v[1].userId, v[1].sourceId, JSON.stringify(v[1])]);
|
|
201
|
+
this._writeEntries("l3_personas", "id", this._changedEntries(this.personas, previous.personas || new Map()), v => [v[0], v[1].userId, JSON.stringify(v[1])]);
|
|
202
|
+
this._writeEntries("l4_policies", "id", this._changedEntries(this.policies, previous.policies || new Map()), v => [v[1].id, JSON.stringify(v[1])]);
|
|
203
|
+
this._writeEntries("l6_proofs", "atom_id", this._changedEntries(this.proofs, previous.proofs || new Map()), v => [v[1].atomId, v[1].sourceLogId, JSON.stringify(v[1])]);
|
|
204
|
+
this._writeEntries("legacy_memories", "memory_key", this._changedEntries(this.legacyMemories, previous.legacyMemories || new Map()), v => [v[0], JSON.stringify(v[1])]);
|
|
205
|
+
|
|
206
|
+
const deletes = [
|
|
207
|
+
["l0_logs", "id", removed(this.logs, previous.logs || new Map())],
|
|
208
|
+
["l1_atoms", "id", removed(this.atoms, previous.atoms || new Map())],
|
|
209
|
+
["l2_episodes", "id", removed(this.episodes, previous.episodes || new Map())],
|
|
210
|
+
["l3_personas", "id", removed(this.personas, previous.personas || new Map())],
|
|
211
|
+
["l4_policies", "id", removed(this.policies, previous.policies || new Map())],
|
|
212
|
+
["l6_proofs", "atom_id", removed(this.proofs, previous.proofs || new Map())],
|
|
213
|
+
["legacy_memories", "memory_key", removed(this.legacyMemories, previous.legacyMemories || new Map())],
|
|
214
|
+
];
|
|
215
|
+
for (const [table, column, keys] of deletes) {
|
|
216
|
+
if (!keys.length) continue;
|
|
217
|
+
const stmt = this._storage.prepare(`DELETE FROM ${table} WHERE ${column}=?`);
|
|
218
|
+
for (const key of keys) stmt.run(key);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
const relationInsert = this._storage.prepare("INSERT OR IGNORE INTO l1_atom_relations (old_atom_id,new_atom_id,user_id,relation,created_at) VALUES (?,?,?,?,?)");
|
|
222
|
+
const relationKeys = new Set(this.relations.map(r => `${r.oldAtomId}\0${r.newAtomId}`));
|
|
223
|
+
for (const relation of this.relations) {
|
|
224
|
+
const key = `${relation.oldAtomId}\0${relation.newAtomId}`;
|
|
225
|
+
if (!(previous.relations || new Set()).has(key)) {
|
|
226
|
+
relationInsert.run(relation.oldAtomId, relation.newAtomId, relation.userId, relation.relation, relation.createdAt);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
for (const key of (previous.relations || new Set())) {
|
|
230
|
+
if (!relationKeys.has(key)) {
|
|
231
|
+
const [oldAtomId, newAtomId] = key.split("\0");
|
|
232
|
+
this._storage.prepare("DELETE FROM l1_atom_relations WHERE old_atom_id=? AND new_atom_id=?").run(oldAtomId, newAtomId);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
})();
|
|
236
|
+
this._persisted = {
|
|
237
|
+
...snapshots,
|
|
238
|
+
relations: new Set(this.relations.map(r => `${r.oldAtomId}\0${r.newAtomId}`)),
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
close() {
|
|
243
|
+
if (this._storage && this._storage.open) this._storage.close();
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
_sha256(str) {
|
|
247
|
+
return crypto.createHash("sha256").update(str).digest("hex");
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
// Legacy key-value compatibility
|
|
251
|
+
put(key, content) {
|
|
252
|
+
this._refreshState();
|
|
253
|
+
if (!key || !key.trim()) throw new Error("key cannot be empty");
|
|
254
|
+
if (!content || !content.trim()) throw new Error("content cannot be empty");
|
|
255
|
+
const mem = { key, content, createdAt: new Date().toISOString() };
|
|
256
|
+
const previousState = {
|
|
257
|
+
logs: new Map(this.logs),
|
|
258
|
+
atoms: new Map(this.atoms),
|
|
259
|
+
episodes: new Map(this.episodes),
|
|
260
|
+
personas: new Map(this.personas),
|
|
261
|
+
proofs: new Map(this.proofs),
|
|
262
|
+
legacyMemories: new Map(this.legacyMemories),
|
|
263
|
+
lastTimestamp: this._lastTimestamp,
|
|
264
|
+
};
|
|
265
|
+
try {
|
|
266
|
+
this._add(`Memory key ${key}: ${content}`, "legacy", null, 0.9, false);
|
|
267
|
+
this.legacyMemories.set(key, mem);
|
|
268
|
+
this._persist();
|
|
269
|
+
} catch (error) {
|
|
270
|
+
this.logs = previousState.logs;
|
|
271
|
+
this.atoms = previousState.atoms;
|
|
272
|
+
this.episodes = previousState.episodes;
|
|
273
|
+
this.personas = previousState.personas;
|
|
274
|
+
this.proofs = previousState.proofs;
|
|
275
|
+
this.legacyMemories = previousState.legacyMemories;
|
|
276
|
+
this._lastTimestamp = previousState.lastTimestamp;
|
|
277
|
+
throw error;
|
|
278
|
+
}
|
|
279
|
+
return mem;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
get(key) {
|
|
283
|
+
this._refreshState();
|
|
284
|
+
return this.legacyMemories.get(key) || null;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
delete(key) {
|
|
288
|
+
this._refreshState();
|
|
289
|
+
if (!this.legacyMemories.has(key)) return false;
|
|
290
|
+
const previous = this.legacyMemories.get(key);
|
|
291
|
+
this.legacyMemories.delete(key);
|
|
292
|
+
try {
|
|
293
|
+
this._persist();
|
|
294
|
+
} catch (error) {
|
|
295
|
+
this.legacyMemories.set(key, previous);
|
|
296
|
+
throw error;
|
|
297
|
+
}
|
|
298
|
+
return true;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
// Core L0-L6 API
|
|
302
|
+
add(conversation, userId = "default", sessionId = null, importance = 0.9) {
|
|
303
|
+
return this._add(conversation, userId, sessionId, importance, true);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
_add(conversation, userId, sessionId, importance, persist = true) {
|
|
307
|
+
this._refreshState();
|
|
308
|
+
if (typeof conversation !== "string" || !conversation.trim()) {
|
|
309
|
+
throw new TypeError("conversation must be a non-empty string");
|
|
310
|
+
}
|
|
311
|
+
if (typeof userId !== "string" || !userId.trim()) {
|
|
312
|
+
throw new TypeError("userId must be a non-empty string");
|
|
313
|
+
}
|
|
314
|
+
if (typeof importance !== "number" || !Number.isFinite(importance)) {
|
|
315
|
+
throw new TypeError("importance must be a finite number between 0 and 1");
|
|
316
|
+
}
|
|
317
|
+
if (importance < 0 || importance > 1) {
|
|
318
|
+
throw new RangeError("importance must be between 0 and 1");
|
|
319
|
+
}
|
|
320
|
+
// Keep the in-memory view aligned with SQLite if the durable write fails.
|
|
321
|
+
const previousState = {
|
|
322
|
+
logs: new Map(this.logs),
|
|
323
|
+
atoms: new Map(this.atoms),
|
|
324
|
+
episodes: new Map(this.episodes),
|
|
325
|
+
personas: new Map(this.personas),
|
|
326
|
+
proofs: new Map(this.proofs),
|
|
327
|
+
lastTimestamp: this._lastTimestamp,
|
|
328
|
+
};
|
|
329
|
+
const now = Math.max(Date.now() / 1000, this._lastTimestamp + 0.001);
|
|
330
|
+
this._lastTimestamp = now;
|
|
331
|
+
this._allocateSequence();
|
|
332
|
+
const logId = `L0_${this._sha256(conversation + now + this._seq).slice(0, 16)}`;
|
|
333
|
+
|
|
334
|
+
// L0: Store raw
|
|
335
|
+
this.logs.set(logId, {
|
|
336
|
+
id: logId,
|
|
337
|
+
userId,
|
|
338
|
+
sessionId,
|
|
339
|
+
raw: conversation,
|
|
340
|
+
ts: now,
|
|
341
|
+
});
|
|
342
|
+
|
|
343
|
+
// L1: Zero-LLM Deterministic Rule Extraction
|
|
344
|
+
const patterns = [
|
|
345
|
+
{ regex: /(?:I am|I'm|my name is)\s+([A-Z][a-zA-Z0-9_\-\s]+?)(?:[.,\n]|$)/i, cat: "identity", weight: 0.95 },
|
|
346
|
+
{ regex: /(?:I work as|I am a|my role is)\s+([^.,\n]+)/i, cat: "role", weight: 0.90 },
|
|
347
|
+
{ regex: /(?:I prefer|I like|I love|I use|my preference is)\s+([^.,\n]+)/i, cat: "preference", weight: 0.92 },
|
|
348
|
+
{ regex: /(?:I hate|I dislike|I avoid|never use)\s+([^.,\n]+)/i, cat: "dislike", weight: 0.88 },
|
|
349
|
+
{ regex: /(?:my budget is|budget of|budget:\s*)\s*([$€£¥]?[0-9,]+(?:\s*[kKmMbB])?)/i, cat: "budget", weight: 0.94 },
|
|
350
|
+
{ regex: /(?:I live in|based in|located in)\s+([^.,\n]+)/i, cat: "location", weight: 0.85 },
|
|
351
|
+
{ regex: /(?:our goal is|aiming to|target is|deadline is)\s+([^.,\n]+)/i, cat: "goal", weight: 0.91 },
|
|
352
|
+
{ regex: /(?:must comply with|required to|constraint:\s*)\s+([^.,\n]+)/i, cat: "constraint", weight: 0.95 },
|
|
353
|
+
];
|
|
354
|
+
|
|
355
|
+
let extractedAny = false;
|
|
356
|
+
for (const pat of patterns) {
|
|
357
|
+
let match;
|
|
358
|
+
const re = new RegExp(pat.regex.source, "gi");
|
|
359
|
+
while ((match = re.exec(conversation)) !== null) {
|
|
360
|
+
extractedAny = true;
|
|
361
|
+
const fact = match[0].trim();
|
|
362
|
+
const atomId = `L1_${this._sha256(logId + fact).slice(0, 16)}`;
|
|
363
|
+
|
|
364
|
+
this.atoms.set(atomId, {
|
|
365
|
+
id: atomId,
|
|
366
|
+
userId,
|
|
367
|
+
fact,
|
|
368
|
+
category: pat.cat,
|
|
369
|
+
sourceId: logId,
|
|
370
|
+
importance: pat.weight * importance,
|
|
371
|
+
accessCount: 0,
|
|
372
|
+
ts: now,
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
// L6: Cryptographic Proof Record
|
|
376
|
+
const proofHash = this._sha256(fact + logId);
|
|
377
|
+
const snippet = conversation.slice(Math.max(0, match.index - 20), Math.min(conversation.length, match.index + fact.length + 20));
|
|
378
|
+
this.proofs.set(atomId, {
|
|
379
|
+
atomId,
|
|
380
|
+
hash: proofHash,
|
|
381
|
+
sourceLogId: logId,
|
|
382
|
+
rawSnippet: snippet,
|
|
383
|
+
charStart: match.index,
|
|
384
|
+
charEnd: match.index + fact.length,
|
|
385
|
+
ts: now,
|
|
386
|
+
});
|
|
387
|
+
|
|
388
|
+
// L3: Persona Auto-Resolution
|
|
389
|
+
const pKey = `${userId}:${pat.cat}`;
|
|
390
|
+
const existing = this.personas.get(pKey);
|
|
391
|
+
if (!existing || now >= existing.updatedAt) {
|
|
392
|
+
this.personas.set(pKey, {
|
|
393
|
+
userId,
|
|
394
|
+
key: pat.cat,
|
|
395
|
+
value: fact,
|
|
396
|
+
confidence: pat.weight,
|
|
397
|
+
updatedAt: now,
|
|
398
|
+
sourceAtomId: atomId,
|
|
399
|
+
});
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
if (!extractedAny) {
|
|
405
|
+
// Store offsets for the exact trimmed sentence so L6 verifies the
|
|
406
|
+
// original source range rather than an unrelated prefix of the log.
|
|
407
|
+
const sentencePattern = /[^.!?\n]+/g;
|
|
408
|
+
let sentenceMatch;
|
|
409
|
+
let extractedCount = 0;
|
|
410
|
+
while ((sentenceMatch = sentencePattern.exec(conversation)) !== null && extractedCount < 3) {
|
|
411
|
+
const rawSentence = sentenceMatch[0];
|
|
412
|
+
const leadingWhitespace = rawSentence.length - rawSentence.trimStart().length;
|
|
413
|
+
const fact = rawSentence.trim();
|
|
414
|
+
if (fact.length <= 8) continue;
|
|
415
|
+
const charStart = sentenceMatch.index + leadingWhitespace;
|
|
416
|
+
const charEnd = charStart + fact.length;
|
|
417
|
+
const atomId = `L1_${this._sha256(logId + fact).slice(0, 16)}`;
|
|
418
|
+
this.atoms.set(atomId, {
|
|
419
|
+
id: atomId,
|
|
420
|
+
userId,
|
|
421
|
+
fact,
|
|
422
|
+
category: "general",
|
|
423
|
+
sourceId: logId,
|
|
424
|
+
importance: 0.75 * importance,
|
|
425
|
+
accessCount: 0,
|
|
426
|
+
ts: now,
|
|
427
|
+
});
|
|
428
|
+
this.proofs.set(atomId, {
|
|
429
|
+
atomId,
|
|
430
|
+
hash: this._sha256(fact + logId),
|
|
431
|
+
sourceLogId: logId,
|
|
432
|
+
rawSnippet: conversation.slice(Math.max(0, charStart - 20), Math.min(conversation.length, charEnd + 20)),
|
|
433
|
+
charStart,
|
|
434
|
+
charEnd,
|
|
435
|
+
ts: now,
|
|
436
|
+
});
|
|
437
|
+
extractedCount++;
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
// L2: Causal Episode Extraction
|
|
442
|
+
const causalRegex = /(.+?)\s+(?:because|since|as a result of|so that)\s+([^.,\n]+)/gi;
|
|
443
|
+
let cMatch;
|
|
444
|
+
while ((cMatch = causalRegex.exec(conversation)) !== null) {
|
|
445
|
+
const effectPart = cMatch[1].trim().slice(-80);
|
|
446
|
+
const causePart = cMatch[2].trim().slice(0, 80);
|
|
447
|
+
const epId = `L2_${this._sha256(logId + causePart + effectPart).slice(0, 16)}`;
|
|
448
|
+
this.episodes.set(epId, {
|
|
449
|
+
id: epId,
|
|
450
|
+
userId,
|
|
451
|
+
cause: causePart,
|
|
452
|
+
effect: effectPart,
|
|
453
|
+
relation: "causes",
|
|
454
|
+
sourceId: logId,
|
|
455
|
+
ts: now,
|
|
456
|
+
});
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
try {
|
|
460
|
+
if (persist) this._persist();
|
|
461
|
+
} catch (error) {
|
|
462
|
+
this.logs = previousState.logs;
|
|
463
|
+
this.atoms = previousState.atoms;
|
|
464
|
+
this.episodes = previousState.episodes;
|
|
465
|
+
this.personas = previousState.personas;
|
|
466
|
+
this.proofs = previousState.proofs;
|
|
467
|
+
this._lastTimestamp = previousState.lastTimestamp;
|
|
468
|
+
throw error;
|
|
469
|
+
}
|
|
470
|
+
return logId;
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
supersedeFact(oldAtomId, newAtomId, userId) {
|
|
474
|
+
this._refreshState();
|
|
475
|
+
if (![oldAtomId, newAtomId, userId].every(v => typeof v === "string" && v.trim())) {
|
|
476
|
+
throw new TypeError("atom ids and userId must be non-empty strings");
|
|
477
|
+
}
|
|
478
|
+
if (oldAtomId === newAtomId) throw new Error("a fact cannot supersede itself");
|
|
479
|
+
|
|
480
|
+
const validateAndBuild = (oldAtom, newAtom, relations) => {
|
|
481
|
+
if (!oldAtom || !newAtom) throw new Error("both facts must exist");
|
|
482
|
+
if (oldAtom.userId !== userId || newAtom.userId !== userId) {
|
|
483
|
+
throw new Error("both facts must belong to the supplied userId");
|
|
484
|
+
}
|
|
485
|
+
const byOld = new Map();
|
|
486
|
+
for (const edge of relations) {
|
|
487
|
+
if (!byOld.has(edge.oldAtomId)) byOld.set(edge.oldAtomId, []);
|
|
488
|
+
byOld.get(edge.oldAtomId).push(edge.newAtomId);
|
|
489
|
+
}
|
|
490
|
+
const pending = [newAtomId], visited = new Set();
|
|
491
|
+
while (pending.length) {
|
|
492
|
+
const current = pending.pop();
|
|
493
|
+
if (current === oldAtomId) throw new Error("supersession would create a cycle");
|
|
494
|
+
if (visited.has(current)) continue;
|
|
495
|
+
visited.add(current);
|
|
496
|
+
pending.push(...(byOld.get(current) || []));
|
|
497
|
+
}
|
|
498
|
+
if (relations.some(r => r.oldAtomId === oldAtomId && r.newAtomId === newAtomId)) {
|
|
499
|
+
throw new Error("supersession relation already exists");
|
|
500
|
+
}
|
|
501
|
+
const createdAt = Math.max(Date.now() / 1000, oldAtom.ts, newAtom.ts) + 0.001;
|
|
502
|
+
return { oldAtomId, newAtomId, userId, relation: "supersedes", createdAt };
|
|
503
|
+
};
|
|
504
|
+
|
|
505
|
+
let relation;
|
|
506
|
+
if (this._storage) {
|
|
507
|
+
// Acquire the write reservation before reading the graph. This makes
|
|
508
|
+
// validation and insertion one serialized SQLite operation across
|
|
509
|
+
// connections, and a failed insert cannot mutate the in-memory view.
|
|
510
|
+
const persistRelation = this._storage.transaction(() => {
|
|
511
|
+
const oldAtom = this._storage.prepare("SELECT payload FROM l1_atoms WHERE id=?").get(oldAtomId);
|
|
512
|
+
const newAtom = this._storage.prepare("SELECT payload FROM l1_atoms WHERE id=?").get(newAtomId);
|
|
513
|
+
const relations = this._storage.prepare(
|
|
514
|
+
"SELECT old_atom_id AS oldAtomId, new_atom_id AS newAtomId, user_id AS userId, relation, created_at AS createdAt FROM l1_atom_relations"
|
|
515
|
+
).all();
|
|
516
|
+
const built = validateAndBuild(
|
|
517
|
+
oldAtom ? JSON.parse(oldAtom.payload) : null,
|
|
518
|
+
newAtom ? JSON.parse(newAtom.payload) : null,
|
|
519
|
+
relations
|
|
520
|
+
);
|
|
521
|
+
this._storage.prepare(
|
|
522
|
+
"INSERT INTO l1_atom_relations (old_atom_id,new_atom_id,user_id,relation,created_at) VALUES (?,?,?,?,?)"
|
|
523
|
+
).run(oldAtomId, newAtomId, userId, "supersedes", built.createdAt);
|
|
524
|
+
return built;
|
|
525
|
+
}).immediate();
|
|
526
|
+
relation = persistRelation;
|
|
527
|
+
} else {
|
|
528
|
+
relation = validateAndBuild(
|
|
529
|
+
this.atoms.get(oldAtomId),
|
|
530
|
+
this.atoms.get(newAtomId),
|
|
531
|
+
this.relations
|
|
532
|
+
);
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
this.relations.push(relation);
|
|
536
|
+
return this._sha256(`${userId}:${oldAtomId}:${newAtomId}:${relation.createdAt}`).slice(0, 24);
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
recall(query, options = {}) {
|
|
540
|
+
this._refreshState();
|
|
541
|
+
if (typeof query !== "string") throw new TypeError("query must be a string");
|
|
542
|
+
const n = options.n === undefined ? 5 : options.n;
|
|
543
|
+
if (!Number.isInteger(n) || n <= 0) {
|
|
544
|
+
throw new RangeError("n must be a positive integer");
|
|
545
|
+
}
|
|
546
|
+
if (options.userId !== undefined &&
|
|
547
|
+
(typeof options.userId !== "string" || !options.userId.trim())) {
|
|
548
|
+
throw new TypeError("userId must be a non-empty string");
|
|
549
|
+
}
|
|
550
|
+
if (options.asOf !== undefined && (typeof options.asOf !== "number" || !Number.isFinite(options.asOf))) {
|
|
551
|
+
throw new TypeError("asOf must be a finite Unix timestamp");
|
|
552
|
+
}
|
|
553
|
+
const start = performance.now();
|
|
554
|
+
const userId = options.userId || null;
|
|
555
|
+
const now = Date.now() / 1000;
|
|
556
|
+
const asOf = options.asOf;
|
|
557
|
+
const qTerms = (query.toLowerCase().match(/\w+/g) || []).filter(t => t.length > 1);
|
|
558
|
+
|
|
559
|
+
const scored = [];
|
|
560
|
+
if (qTerms.length === 0) {
|
|
561
|
+
return {
|
|
562
|
+
results: [],
|
|
563
|
+
causalGraph: [],
|
|
564
|
+
policies: [],
|
|
565
|
+
latencyMs: Math.round((performance.now() - start) * 100) / 100,
|
|
566
|
+
tokensSaved: null,
|
|
567
|
+
proofAvailable: false,
|
|
568
|
+
};
|
|
569
|
+
}
|
|
570
|
+
let candidateAtoms = this.atoms.entries();
|
|
571
|
+
if (this._storage) {
|
|
572
|
+
const matchQuery = qTerms.map(term => '"' + term.replace(/"/g, '""') + '"').join(" OR ");
|
|
573
|
+
const rows = this._storage.prepare(
|
|
574
|
+
"SELECT a.id, a.payload FROM l1_atoms_fts f JOIN l1_atoms a ON a.rowid=f.rowid WHERE l1_atoms_fts MATCH ?"
|
|
575
|
+
).all(matchQuery);
|
|
576
|
+
candidateAtoms = rows.map(row => [row.id, JSON.parse(row.payload)]);
|
|
577
|
+
}
|
|
578
|
+
for (const [id, atom] of candidateAtoms) {
|
|
579
|
+
if (userId && atom.userId !== userId) continue;
|
|
580
|
+
if (asOf !== undefined && atom.ts > asOf) continue;
|
|
581
|
+
if (this.relations.some(r => r.oldAtomId === id && (asOf === undefined || r.createdAt <= asOf))) continue;
|
|
582
|
+
const factLower = atom.fact.toLowerCase();
|
|
583
|
+
let termMatches = 0;
|
|
584
|
+
for (const term of qTerms) {
|
|
585
|
+
if (factLower.includes(term)) termMatches++;
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
let termScore = termMatches * 2.5;
|
|
589
|
+
if (factLower.includes(query.toLowerCase())) termScore += 4.0;
|
|
590
|
+
|
|
591
|
+
// L5 Ebbinghaus decay
|
|
592
|
+
const deltaDays = (now - atom.ts) / 86400.0;
|
|
593
|
+
const stability = 1.0 + atom.importance * 3.0 + Math.log1p(atom.accessCount);
|
|
594
|
+
const retention = Math.exp(-deltaDays / Math.max(stability, 0.1));
|
|
595
|
+
const totalScore = (termScore + atom.importance * 1.5) * retention;
|
|
596
|
+
|
|
597
|
+
if (termMatches > 0) {
|
|
598
|
+
scored.push({
|
|
599
|
+
id: atom.id,
|
|
600
|
+
fact: atom.fact,
|
|
601
|
+
category: atom.category,
|
|
602
|
+
sourceId: atom.sourceId,
|
|
603
|
+
score: Math.round(totalScore * 1000) / 1000,
|
|
604
|
+
retention: Math.round(retention * 1000) / 1000,
|
|
605
|
+
ts: atom.ts,
|
|
606
|
+
});
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
scored.sort((a, b) => b.score - a.score);
|
|
611
|
+
const topK = scored.slice(0, n);
|
|
612
|
+
|
|
613
|
+
// Reinforce access count only when the updated counts are durable.
|
|
614
|
+
const previousAccessCounts = [];
|
|
615
|
+
for (const item of topK) {
|
|
616
|
+
const at = this.atoms.get(item.id);
|
|
617
|
+
if (at) {
|
|
618
|
+
previousAccessCounts.push([at, at.accessCount]);
|
|
619
|
+
at.accessCount++;
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
try {
|
|
624
|
+
this._persist();
|
|
625
|
+
} catch (error) {
|
|
626
|
+
for (const [atom, accessCount] of previousAccessCounts) {
|
|
627
|
+
atom.accessCount = accessCount;
|
|
628
|
+
}
|
|
629
|
+
throw error;
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
// Causal graph insights
|
|
633
|
+
const causalGraph = [];
|
|
634
|
+
for (const item of topK) {
|
|
635
|
+
for (const ep of this.episodes.values()) {
|
|
636
|
+
if (ep.sourceId === item.sourceId) {
|
|
637
|
+
causalGraph.push({ cause: ep.cause, effect: ep.effect, relation: ep.relation });
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
// Global policies apply to every tenant; scoped policies require a matching userId.
|
|
643
|
+
const policies = Array.from(this.policies.values())
|
|
644
|
+
.filter(policy => !policy.tenantId || (userId && policy.tenantId === userId))
|
|
645
|
+
.sort((a, b) => b.priority - a.priority)
|
|
646
|
+
.slice(0, 3);
|
|
647
|
+
|
|
648
|
+
const elapsedMs = performance.now() - start;
|
|
649
|
+
return {
|
|
650
|
+
results: topK,
|
|
651
|
+
causalGraph,
|
|
652
|
+
policies,
|
|
653
|
+
latencyMs: Math.round(elapsedMs * 100) / 100,
|
|
654
|
+
tokensSaved: null,
|
|
655
|
+
proofAvailable: true,
|
|
656
|
+
};
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
getProof(atomId) {
|
|
660
|
+
this._refreshState();
|
|
661
|
+
const p = this.proofs.get(atomId);
|
|
662
|
+
if (!p) return null;
|
|
663
|
+
const source = this.logs.get(p.sourceLogId);
|
|
664
|
+
const atom = this.atoms.get(atomId);
|
|
665
|
+
let verified = false;
|
|
666
|
+
if (source && atom &&
|
|
667
|
+
Number.isInteger(p.charStart) && Number.isInteger(p.charEnd) &&
|
|
668
|
+
p.charStart >= 0 && p.charEnd >= p.charStart &&
|
|
669
|
+
p.charEnd <= source.raw.length) {
|
|
670
|
+
const sourceFact = source.raw.slice(p.charStart, p.charEnd);
|
|
671
|
+
const expected = this._sha256(atom.fact + p.sourceLogId);
|
|
672
|
+
const actualBuffer = Buffer.from(p.hash, "hex");
|
|
673
|
+
const expectedBuffer = Buffer.from(expected, "hex");
|
|
674
|
+
verified = sourceFact === atom.fact &&
|
|
675
|
+
actualBuffer.length === expectedBuffer.length &&
|
|
676
|
+
crypto.timingSafeEqual(actualBuffer, expectedBuffer);
|
|
677
|
+
}
|
|
678
|
+
return {
|
|
679
|
+
atomId: p.atomId,
|
|
680
|
+
hash: p.hash,
|
|
681
|
+
sourceLogId: p.sourceLogId,
|
|
682
|
+
rawSnippet: p.rawSnippet,
|
|
683
|
+
charRange: [p.charStart, p.charEnd],
|
|
684
|
+
verified,
|
|
685
|
+
timestamp: p.ts,
|
|
686
|
+
};
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
getPersona(userId = "default") {
|
|
690
|
+
this._refreshState();
|
|
691
|
+
const out = {};
|
|
692
|
+
for (const [key, p] of this.personas.entries()) {
|
|
693
|
+
if (p.userId === userId) {
|
|
694
|
+
out[p.key] = p.value;
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
return out;
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
addPolicy(rule, domain = "general", priority = 1.0, tenantId = null) {
|
|
701
|
+
this._refreshState();
|
|
702
|
+
if (typeof rule !== "string" || !rule.trim()) throw new TypeError("rule must be a non-empty string");
|
|
703
|
+
if (typeof domain !== "string" || !domain.trim()) throw new TypeError("domain must be a non-empty string");
|
|
704
|
+
if (typeof priority !== "number" || !Number.isFinite(priority)) throw new TypeError("priority must be a finite number");
|
|
705
|
+
if (tenantId !== null && (typeof tenantId !== "string" || !tenantId.trim())) {
|
|
706
|
+
throw new TypeError("tenantId must be null or a non-empty string");
|
|
707
|
+
}
|
|
708
|
+
const scope = tenantId || "global";
|
|
709
|
+
const pid = `L4_${this._sha256(rule.trim() + domain.trim() + scope).slice(0, 16)}`;
|
|
710
|
+
const existing = this.policies.get(pid);
|
|
711
|
+
this.policies.set(pid, { id: pid, rule: rule.trim(), domain: domain.trim(), priority,
|
|
712
|
+
tenantId, createdAt: existing?.createdAt ?? Date.now() / 1000 });
|
|
713
|
+
try {
|
|
714
|
+
this._persist();
|
|
715
|
+
} catch (error) {
|
|
716
|
+
if (existing) this.policies.set(pid, existing);
|
|
717
|
+
else this.policies.delete(pid);
|
|
718
|
+
throw error;
|
|
719
|
+
}
|
|
720
|
+
return pid;
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
exportGraph(userId = "default") {
|
|
724
|
+
const lines = ["graph TD"];
|
|
725
|
+
let idx = 0;
|
|
726
|
+
for (const ep of this.episodes.values()) {
|
|
727
|
+
if (ep.userId === userId) {
|
|
728
|
+
const c = ep.cause.replace(/["\n]/g, "").slice(0, 30);
|
|
729
|
+
const e = ep.effect.replace(/["\n]/g, "").slice(0, 30);
|
|
730
|
+
lines.push(` C${idx}["${c}"] -->|${ep.relation}| E${idx}["${e}"]`);
|
|
731
|
+
idx++;
|
|
732
|
+
}
|
|
733
|
+
}
|
|
734
|
+
if (idx === 0) return "graph TD\n Empty[No causal memory recorded]";
|
|
735
|
+
return lines.join("\n");
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
deleteUser(userId) {
|
|
739
|
+
this._refreshState();
|
|
740
|
+
let c0 = 0, c1 = 0, c2 = 0, c3 = 0;
|
|
741
|
+
const logIds = [];
|
|
742
|
+
const atomIds = [];
|
|
743
|
+
const episodeIds = [];
|
|
744
|
+
const personaKeys = [];
|
|
745
|
+
const policyIds = [];
|
|
746
|
+
|
|
747
|
+
for (const [id, log] of this.logs.entries()) {
|
|
748
|
+
if (log.userId === userId) { logIds.push(id); c0++; }
|
|
749
|
+
}
|
|
750
|
+
for (const [id, atom] of this.atoms.entries()) {
|
|
751
|
+
if (atom.userId === userId) { atomIds.push(id); c1++; }
|
|
752
|
+
}
|
|
753
|
+
for (const [id, ep] of this.episodes.entries()) {
|
|
754
|
+
if (ep.userId === userId) { episodeIds.push(id); c2++; }
|
|
755
|
+
}
|
|
756
|
+
for (const [key, persona] of this.personas.entries()) {
|
|
757
|
+
if (persona.userId === userId) { personaKeys.push(key); c3++; }
|
|
758
|
+
}
|
|
759
|
+
for (const [id, policy] of this.policies.entries()) {
|
|
760
|
+
if (policy.tenantId === userId) policyIds.push(id);
|
|
761
|
+
}
|
|
762
|
+
|
|
763
|
+
// Commit durable deletion before mutating the in-memory view. If SQLite
|
|
764
|
+
// rejects any statement, the transaction rolls back and this instance
|
|
765
|
+
// continues to expose the unchanged tenant state.
|
|
766
|
+
if (this._storage) {
|
|
767
|
+
this._storage.transaction(() => {
|
|
768
|
+
this._storage.prepare("DELETE FROM l0_logs WHERE user_id=?").run(userId);
|
|
769
|
+
this._storage.prepare("DELETE FROM l1_atom_relations WHERE user_id=?").run(userId);
|
|
770
|
+
this._storage.prepare("DELETE FROM l1_atoms WHERE user_id=?").run(userId);
|
|
771
|
+
this._storage.prepare("DELETE FROM l2_episodes WHERE user_id=?").run(userId);
|
|
772
|
+
this._storage.prepare("DELETE FROM l3_personas WHERE user_id=?").run(userId);
|
|
773
|
+
// L4 policies are stored as JSON payloads keyed by ID (not tenant_id columns).
|
|
774
|
+
const deletePolicy = this._storage.prepare("DELETE FROM l4_policies WHERE id=?");
|
|
775
|
+
for (const id of policyIds) deletePolicy.run(id);
|
|
776
|
+
this._storage.prepare("DELETE FROM l6_proofs WHERE source_log_id NOT IN (SELECT id FROM l0_logs)").run();
|
|
777
|
+
})();
|
|
778
|
+
}
|
|
779
|
+
|
|
780
|
+
for (const id of logIds) this.logs.delete(id);
|
|
781
|
+
for (const id of atomIds) {
|
|
782
|
+
this.atoms.delete(id);
|
|
783
|
+
this.proofs.delete(id);
|
|
784
|
+
}
|
|
785
|
+
for (const id of episodeIds) this.episodes.delete(id);
|
|
786
|
+
for (const key of personaKeys) this.personas.delete(key);
|
|
787
|
+
for (const id of policyIds) this.policies.delete(id);
|
|
788
|
+
this.relations = this.relations.filter(r => r.userId !== userId);
|
|
789
|
+
|
|
790
|
+
// The normalized rows were already deleted in the transaction above.
|
|
791
|
+
// Advance the persistence baseline without issuing a second write
|
|
792
|
+
// transaction, so a later persistence failure cannot split deletion
|
|
793
|
+
// across two commits or leave the snapshot describing deleted rows.
|
|
794
|
+
if (this._storage) {
|
|
795
|
+
this._persisted = {
|
|
796
|
+
logs: this._snapshotMap(this.logs),
|
|
797
|
+
atoms: this._snapshotMap(this.atoms),
|
|
798
|
+
episodes: this._snapshotMap(this.episodes),
|
|
799
|
+
personas: this._snapshotMap(this.personas),
|
|
800
|
+
policies: this._snapshotMap(this.policies),
|
|
801
|
+
proofs: this._snapshotMap(this.proofs),
|
|
802
|
+
legacyMemories: this._snapshotMap(this.legacyMemories),
|
|
803
|
+
relations: new Set(this.relations.map(r => `${r.oldAtomId}\\0${r.newAtomId}`)),
|
|
804
|
+
};
|
|
805
|
+
}
|
|
806
|
+
return {
|
|
807
|
+
l0LogsDeleted: c0,
|
|
808
|
+
l1AtomsDeleted: c1,
|
|
809
|
+
l2EpisodesDeleted: c2,
|
|
810
|
+
l3PersonaDeleted: c3,
|
|
811
|
+
l4TenantPoliciesDeleted: policyIds.length,
|
|
812
|
+
};
|
|
813
|
+
}
|
|
814
|
+
|
|
815
|
+
stats() {
|
|
816
|
+
this._refreshState();
|
|
817
|
+
return {
|
|
818
|
+
l0RawLogs: this.logs.size,
|
|
819
|
+
l1AtomFacts: this.atoms.size,
|
|
820
|
+
l2CausalEpisodes: this.episodes.size,
|
|
821
|
+
l3Personas: this.personas.size,
|
|
822
|
+
l4Policies: this.policies.size,
|
|
823
|
+
l6CryptographicProofs: this.proofs.size,
|
|
824
|
+
p95RecallTargetMs: "<5.0",
|
|
825
|
+
};
|
|
826
|
+
}
|
|
827
|
+
}
|
|
828
|
+
|
|
829
|
+
module.exports = { InfinityDB };
|