infinitydb 0.0.0-stage → 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 +119 -0
- package/README.md +186 -3
- package/index.d.ts +45 -0
- package/index.js +829 -0
- package/package.json +39 -6
package/LICENSE
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work.
|
|
38
|
+
|
|
39
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
40
|
+
form, that is based on (or derived from) the Work and for which the
|
|
41
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
42
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
43
|
+
of this License, Derivative Works shall not include works that remain
|
|
44
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
45
|
+
the Work and Derivative Works thereof.
|
|
46
|
+
|
|
47
|
+
"Contribution" shall mean any work of authorship, including
|
|
48
|
+
the original version of the Work and any modifications or additions
|
|
49
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
50
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
51
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
52
|
+
the copyright owner.
|
|
53
|
+
|
|
54
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
55
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
56
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
57
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
58
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
59
|
+
Work and such Derivative Works in Source or Object form.
|
|
60
|
+
|
|
61
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
62
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
63
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
64
|
+
(except as stated in this section) patent license to make, have made,
|
|
65
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
66
|
+
where such license applies only to those patent claims licensable
|
|
67
|
+
by such Contributor that are necessarily infringed by their
|
|
68
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
69
|
+
with the Work to which such Contribution(s) was submitted.
|
|
70
|
+
|
|
71
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
72
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
73
|
+
modifications, and in Source or Object form, provided that You
|
|
74
|
+
meet the following conditions:
|
|
75
|
+
|
|
76
|
+
(a) You must give any other recipients of the Work or
|
|
77
|
+
Derivative Works a copy of this License; and
|
|
78
|
+
|
|
79
|
+
(b) You must cause any modified files to carry prominent notices
|
|
80
|
+
stating that You changed the files; and
|
|
81
|
+
|
|
82
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
83
|
+
that You distribute, all copyright, patent, trademark, and
|
|
84
|
+
attribution notices from the Source form of the Work; and
|
|
85
|
+
|
|
86
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
87
|
+
distribution, then any Derivative Works that You distribute must
|
|
88
|
+
include a readable copy of the attribution notices contained
|
|
89
|
+
within such NOTICE file.
|
|
90
|
+
|
|
91
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
92
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
93
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
94
|
+
this License, without any additional terms or conditions.
|
|
95
|
+
|
|
96
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
97
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
98
|
+
except as required for reasonable and customary use in describing the
|
|
99
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
100
|
+
|
|
101
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
102
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
103
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
104
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
105
|
+
implied, including, without limitation, any warranties or conditions
|
|
106
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
107
|
+
PARTICULAR PURPOSE.
|
|
108
|
+
|
|
109
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
110
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
111
|
+
shall any Contributor be liable to You for damages, including any
|
|
112
|
+
direct, indirect, special, incidental, or exemplary damages of any
|
|
113
|
+
character arising as a result of this License or out of the use or
|
|
114
|
+
inability to use the Work.
|
|
115
|
+
|
|
116
|
+
END OF TERMS AND CONDITIONS
|
|
117
|
+
|
|
118
|
+
Copyright (c) 2026 InfinityDB AI Contributors
|
|
119
|
+
|
package/README.md
CHANGED
|
@@ -1,3 +1,186 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
# InfinityDB
|
|
2
|
+
|
|
3
|
+
> High-Performance Neurosymbolic Agent Memory Engine for JavaScript and TypeScript.
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/infinitydb)
|
|
6
|
+
[](https://opensource.org/licenses/Apache-2.0)
|
|
7
|
+
[](https://www.typescriptlang.org/)
|
|
8
|
+
|
|
9
|
+
InfinityDB gives AI agents long-term neurosymbolic memory with deterministic recall, dynamic persona conflict resolution, and cryptographic SHA-256 audit proofs. Recall and write latency depend on dataset size, hardware, storage configuration, and workload.
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install infinitydb
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
yarn add infinitydb
|
|
21
|
+
# or
|
|
22
|
+
pnpm add infinitydb
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
---
|
|
26
|
+
|
|
27
|
+
## Quick Start
|
|
28
|
+
|
|
29
|
+
### 1. Ingestion and Causal Recall
|
|
30
|
+
|
|
31
|
+
```javascript
|
|
32
|
+
const { InfinityDB } = require("infinitydb");
|
|
33
|
+
// Or in TypeScript:
|
|
34
|
+
// import { InfinityDB } from "infinitydb";
|
|
35
|
+
|
|
36
|
+
const db = new InfinityDB();
|
|
37
|
+
|
|
38
|
+
// Ingest agent dialogue / user interactions (L0 Log -> L1 Atomic Facts -> L2 Episode Graph)
|
|
39
|
+
const logId = db.add(
|
|
40
|
+
"Sarah prefers TypeScript over JavaScript because of strict static typing. Her project budget is $15,000.",
|
|
41
|
+
"usr_sarah"
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
// Recall relevant memory with causal facts, policy checks, and proof chains
|
|
45
|
+
const memory = db.recall("what language does Sarah prefer?", { userId: "usr_sarah" });
|
|
46
|
+
|
|
47
|
+
console.log("Latency:", memory.latencyMs, "ms");
|
|
48
|
+
console.log("Recalled Facts:", memory.results);
|
|
49
|
+
console.log("Causal Graph:", memory.causalGraph);
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### 2. Dynamic Persona & Automatic Conflict Resolution
|
|
53
|
+
|
|
54
|
+
```javascript
|
|
55
|
+
// Initial preference
|
|
56
|
+
db.add("I prefer TypeScript for backend APIs", "usr_sarah");
|
|
57
|
+
console.log(db.getPersona("usr_sarah"));
|
|
58
|
+
// => { preference: ["TypeScript for backend APIs"] }
|
|
59
|
+
|
|
60
|
+
// User changes their mind later:
|
|
61
|
+
db.add("I now prefer Rust for mission-critical core engines", "usr_sarah");
|
|
62
|
+
|
|
63
|
+
// Conflicting older preference is automatically reconciled
|
|
64
|
+
console.log(db.getPersona("usr_sarah"));
|
|
65
|
+
// => { preference: ["Rust for mission-critical core engines"] }
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### 3. Explicit temporal supersession and historical recall
|
|
69
|
+
|
|
70
|
+
When a user explicitly replaces a fact, preserve the original fact and its provenance while marking the newer fact as its successor:
|
|
71
|
+
|
|
72
|
+
```javascript
|
|
73
|
+
const oldLog = db.add("I prefer Python for data work.", "usr_sarah");
|
|
74
|
+
const oldFact = Array.from(db.atoms.values()).find(a => a.sourceId === oldLog);
|
|
75
|
+
const cutoff = oldFact.ts + 0.001;
|
|
76
|
+
|
|
77
|
+
const newLog = db.add("I prefer Rust for systems work.", "usr_sarah");
|
|
78
|
+
const newFact = Array.from(db.atoms.values()).find(a => a.sourceId === newLog);
|
|
79
|
+
|
|
80
|
+
db.supersedeFact(oldFact.id, newFact.id, "usr_sarah");
|
|
81
|
+
|
|
82
|
+
// Current recall returns the active fact; historical recall can still see the old one.
|
|
83
|
+
db.recall("Python", { userId: "usr_sarah" }).results; // []
|
|
84
|
+
db.recall("Python", { userId: "usr_sarah", asOf: cutoff }).results;
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
Supersession is explicit; the SDK does not infer semantic contradictions. Both facts and their proof records remain stored. Relations are tenant-scoped and cycle-checked. SQLite mode persists relations across reopen.
|
|
88
|
+
|
|
89
|
+
### 4. Cryptographic Audit Proofs (SHA-256 Chain)
|
|
90
|
+
|
|
91
|
+
Every atomic fact is hashed and anchored into an immutable tamper-evident proof chain:
|
|
92
|
+
|
|
93
|
+
```javascript
|
|
94
|
+
const atomId = memory.results[0].id;
|
|
95
|
+
const proof = db.getProof(atomId);
|
|
96
|
+
|
|
97
|
+
console.log(proof);
|
|
98
|
+
// {
|
|
99
|
+
// atomId: "ATOM_...",
|
|
100
|
+
// hash: "a4f8c2...",
|
|
101
|
+
// verified: true,
|
|
102
|
+
// timestamp: 1729100000000
|
|
103
|
+
// }
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### 5. Enterprise Policies & Guardrails
|
|
107
|
+
|
|
108
|
+
```javascript
|
|
109
|
+
// Register a global policy or a tenant-scoped policy
|
|
110
|
+
db.addPolicy("Never log plaintext passwords or API keys", "security", 2.0);
|
|
111
|
+
db.addPolicy("Tenant-specific retention rule", "retention", 1.5, "usr_sarah");
|
|
112
|
+
|
|
113
|
+
const recall = db.recall("database credentials", { userId: "usr_sarah" });
|
|
114
|
+
console.log(recall.policies); // Global + usr_sarah policies only
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
### 6. Tenant data deletion
|
|
118
|
+
|
|
119
|
+
```javascript
|
|
120
|
+
// Remove this tenant’s stored logs, facts, episodes, and persona state
|
|
121
|
+
const result = db.deleteUser("usr_sarah");
|
|
122
|
+
console.log(result); // Per-layer deletion counts
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
This API reports database deletion results only; it does not certify legal or regulatory compliance.
|
|
126
|
+
|
|
127
|
+
### 7. Backwards-Compatible Key-Value Store
|
|
128
|
+
|
|
129
|
+
```javascript
|
|
130
|
+
db.put("session:123", "active");
|
|
131
|
+
const val = db.get("session:123");
|
|
132
|
+
db.delete("session:123");
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
### 8. Durable SQLite storage (Node.js)
|
|
137
|
+
|
|
138
|
+
Install the package dependencies, then provide a database path to enable durable storage:
|
|
139
|
+
|
|
140
|
+
```javascript
|
|
141
|
+
const { InfinityDB } = require("infinitydb");
|
|
142
|
+
const db = new InfinityDB({ dbPath: "./data/infinitydb.sqlite" });
|
|
143
|
+
|
|
144
|
+
db.add("I prefer SQLite for durable memory.", "user_123");
|
|
145
|
+
console.log(db.recall("SQLite", { userId: "user_123" }).results);
|
|
146
|
+
|
|
147
|
+
// Close cleanly before process shutdown. Reopen with the same dbPath to recover state.
|
|
148
|
+
db.close();
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
When `dbPath` is omitted, the SDK remains in-memory for backwards compatibility.
|
|
152
|
+
The SQLite-backed mode uses WAL, FULL synchronous commits, a busy timeout, and normalized
|
|
153
|
+
L0/L1/L2/L3/L4/L6 tables with transactional row upserts. Existing v1 JSON-snapshot databases
|
|
154
|
+
are migrated transactionally to schema v2 on first open. SDK instances refresh their view
|
|
155
|
+
from SQLite before public reads and mutations, and sequence allocation is serialized by a
|
|
156
|
+
SQLite write transaction. This is not yet a guarantee of serializable concurrent read-modify-write
|
|
157
|
+
operations across processes; use a single writer until that coordination layer is added.
|
|
158
|
+
Back up the database using SQLite's backup facilities.
|
|
159
|
+
|
|
160
|
+
---
|
|
161
|
+
|
|
162
|
+
## Architecture (L0 – L6 Brain)
|
|
163
|
+
|
|
164
|
+
| Layer | Component | Function |
|
|
165
|
+
| :--- | :--- | :--- |
|
|
166
|
+
| **L0** | Raw Interaction Store | Append-only raw interaction logs |
|
|
167
|
+
| **L1** | Atomic Fact Extractor | Deterministic, zero-hallucination semantic extraction |
|
|
168
|
+
| **L2** | Episodic Graph | Causal edge tracking between related memory episodes |
|
|
169
|
+
| **L3** | Dynamic Persona | Identity state with automatic chronological conflict resolution |
|
|
170
|
+
| **L4** | Enterprise Policy Engine | Real-time policy and compliance injection |
|
|
171
|
+
| **L5** | Ebbinghaus Decay | Recency and frequency weighted memory decay |
|
|
172
|
+
| **L6** | SHA-256 Proof Chain | Cryptographically verifiable audit trail |
|
|
173
|
+
|
|
174
|
+
---
|
|
175
|
+
|
|
176
|
+
## TypeScript Support
|
|
177
|
+
|
|
178
|
+
InfinityDB includes full type definitions out of the box (`index.d.ts`). No `@types/infinitydb` required.
|
|
179
|
+
|
|
180
|
+
---
|
|
181
|
+
|
|
182
|
+
## Links
|
|
183
|
+
|
|
184
|
+
- **Documentation & Benchmarks**: [https://github.com/infinitydb-ai/infinitydb](https://github.com/infinitydb-ai/infinitydb)
|
|
185
|
+
- **Python Package (PyPI)**: [`pip install infinitydb-ai`](https://pypi.org/project/infinitydb-ai/)
|
|
186
|
+
- **License**: Apache 2.0
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
export interface MemoryResult {
|
|
2
|
+
id: string;
|
|
3
|
+
fact: string;
|
|
4
|
+
category: string;
|
|
5
|
+
sourceId: string;
|
|
6
|
+
score: number;
|
|
7
|
+
retention: number;
|
|
8
|
+
ts: number;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface RecallResponse {
|
|
12
|
+
results: MemoryResult[];
|
|
13
|
+
causalGraph: Array<{ cause: string; effect: string; relation: string }>;
|
|
14
|
+
policies: Array<{ id: string; rule: string; domain: string; priority: number }>;
|
|
15
|
+
latencyMs: number;
|
|
16
|
+
tokensSaved: string | null;
|
|
17
|
+
proofAvailable: boolean;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface ProofResponse {
|
|
21
|
+
atomId: string;
|
|
22
|
+
hash: string;
|
|
23
|
+
sourceLogId: string;
|
|
24
|
+
rawSnippet: string;
|
|
25
|
+
charRange: [number, number];
|
|
26
|
+
verified: boolean;
|
|
27
|
+
timestamp: number;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export class InfinityDB {
|
|
31
|
+
constructor(options?: { dbPath?: string } & Record<string, any>);
|
|
32
|
+
add(conversation: string, userId?: string, sessionId?: string | null, importance?: number): string;
|
|
33
|
+
recall(query: string, options?: { n?: number; userId?: string | null; asOf?: number }): RecallResponse;
|
|
34
|
+
supersedeFact(oldAtomId: string, newAtomId: string, userId: string): string;
|
|
35
|
+
getProof(atomId: string): ProofResponse | null;
|
|
36
|
+
getPersona(userId?: string): Record<string, string>;
|
|
37
|
+
addPolicy(rule: string, domain?: string, priority?: number, tenantId?: string | null): string;
|
|
38
|
+
exportGraph(userId?: string): string;
|
|
39
|
+
deleteUser(userId: string): Record<string, any>;
|
|
40
|
+
stats(): Record<string, any>;
|
|
41
|
+
close(): void;
|
|
42
|
+
put(key: string, content: string): { key: string; content: string; createdAt: string };
|
|
43
|
+
get(key: string): { key: string; content: string; createdAt: string } | null;
|
|
44
|
+
delete(key: string): boolean;
|
|
45
|
+
}
|
package/index.js
ADDED
|
@@ -0,0 +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._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 };
|
package/package.json
CHANGED
|
@@ -1,6 +1,39 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "infinitydb",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "infinitydb",
|
|
3
|
+
"version": "1.4.0",
|
|
4
|
+
"description": "InfinityDB - High-performance agent memory engine. Remember Everything. Forget Nothing That Matters.",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"types": "index.d.ts",
|
|
7
|
+
"license": "Apache-2.0",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/infinitydb-ai/infinitydb.git"
|
|
11
|
+
},
|
|
12
|
+
"homepage": "https://github.com/infinitydb-ai/infinitydb#readme",
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/infinitydb-ai/infinitydb/issues"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"agent-memory",
|
|
18
|
+
"ai-agents",
|
|
19
|
+
"llm-memory",
|
|
20
|
+
"vector-database",
|
|
21
|
+
"rag",
|
|
22
|
+
"sqlite",
|
|
23
|
+
"long-term-memory"
|
|
24
|
+
],
|
|
25
|
+
"author": "InfinityDB AI <contact@infinitydb.ai>",
|
|
26
|
+
"files": [
|
|
27
|
+
"index.js",
|
|
28
|
+
"index.d.ts",
|
|
29
|
+
"README.md",
|
|
30
|
+
"LICENSE"
|
|
31
|
+
],
|
|
32
|
+
"scripts": {
|
|
33
|
+
"test": "node test.js && node benchmark.test.js",
|
|
34
|
+
"benchmark": "node benchmark.js"
|
|
35
|
+
},
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"better-sqlite3": "^11.10.0"
|
|
38
|
+
}
|
|
39
|
+
}
|