march-cli 0.1.22 → 0.1.23
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/README.md +88 -0
- package/README.zh.md +88 -0
- package/package.json +1 -1
- package/src/context/system-core/base.md +23 -15
- package/src/memory/database.mjs +0 -219
- package/src/memory/glossary.mjs +0 -124
- package/src/memory/graph/graph-cascades.mjs +0 -109
- package/src/memory/graph/graph-diagnostics.mjs +0 -73
- package/src/memory/graph/graph-path-removal.mjs +0 -50
- package/src/memory/graph/graph-path-utils.mjs +0 -17
- package/src/memory/graph/graph-primitives.mjs +0 -103
- package/src/memory/graph/graph-read.mjs +0 -159
- package/src/memory/graph.mjs +0 -282
- package/src/memory/search.mjs +0 -142
- package/src/memory/snapshot.mjs +0 -86
- package/src/memory/system-views.mjs +0 -120
- package/src/memory/tools.mjs +0 -282
package/src/memory/graph.mjs
DELETED
|
@@ -1,282 +0,0 @@
|
|
|
1
|
-
import { randomUUID } from "node:crypto";
|
|
2
|
-
import { ROOT_NODE_UUID } from "./database.mjs";
|
|
3
|
-
import {
|
|
4
|
-
getChildren,
|
|
5
|
-
getMemoryByPath,
|
|
6
|
-
getRecentMemories,
|
|
7
|
-
} from "./graph/graph-read.mjs";
|
|
8
|
-
import { getGraphDiagnostics } from "./graph/graph-diagnostics.mjs";
|
|
9
|
-
import {
|
|
10
|
-
cascadeCreatePaths,
|
|
11
|
-
cascadeDeleteNode,
|
|
12
|
-
deprecateNodeMemories,
|
|
13
|
-
} from "./graph/graph-cascades.mjs";
|
|
14
|
-
import { removeGraphPath } from "./graph/graph-path-removal.mjs";
|
|
15
|
-
import {
|
|
16
|
-
graphUri,
|
|
17
|
-
leafName,
|
|
18
|
-
pathExists,
|
|
19
|
-
} from "./graph/graph-path-utils.mjs";
|
|
20
|
-
import {
|
|
21
|
-
ensureNode,
|
|
22
|
-
getNextChildNumber,
|
|
23
|
-
getOrCreateEdge,
|
|
24
|
-
insertMemory,
|
|
25
|
-
insertPath,
|
|
26
|
-
resolveGraphPath,
|
|
27
|
-
wouldCreateCycle,
|
|
28
|
-
} from "./graph/graph-primitives.mjs";
|
|
29
|
-
|
|
30
|
-
export class GraphService {
|
|
31
|
-
constructor(db, { changesetStore = null, searchIndexer = null, namespace = "" } = {}) {
|
|
32
|
-
this.db = db;
|
|
33
|
-
this.changesetStore = changesetStore;
|
|
34
|
-
this.searchIndexer = searchIndexer;
|
|
35
|
-
this.namespace = namespace;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Resolve namespace: explicit arg takes precedence, then instance default.
|
|
40
|
-
*/
|
|
41
|
-
#ns(explicit = undefined) {
|
|
42
|
-
return explicit !== undefined ? explicit : this.namespace;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
// ═══════════════════════════════════════════════════════════════════
|
|
46
|
-
// Read Operations
|
|
47
|
-
// ═══════════════════════════════════════════════════════════════════
|
|
48
|
-
|
|
49
|
-
getMemoryByPath(path, domain = "core", namespace = "") {
|
|
50
|
-
return getMemoryByPath(this.db, path, domain, namespace);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
getChildren(nodeUuid = ROOT_NODE_UUID, contextDomain = null, contextPath = null, namespace = "") {
|
|
54
|
-
return getChildren(this.db, nodeUuid, contextDomain, contextPath, this.#ns(namespace));
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
getRecentMemories(limit = 10, namespace = "") {
|
|
58
|
-
return getRecentMemories(this.db, limit, namespace);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
resolvePath(path, domain = "core", namespace = "") {
|
|
62
|
-
return resolveGraphPath(this.db, path, domain, namespace);
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
getPathsForNode(nodeUuid, namespace = "") {
|
|
66
|
-
return this.db.prepare(
|
|
67
|
-
"SELECT * FROM paths WHERE node_uuid = ? AND namespace = ?"
|
|
68
|
-
).all(nodeUuid, namespace);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
getCurrentMemory(nodeUuid) {
|
|
72
|
-
return this.db.prepare(
|
|
73
|
-
"SELECT * FROM memories WHERE node_uuid = ? AND deprecated = 0 ORDER BY id DESC LIMIT 1"
|
|
74
|
-
).get(nodeUuid);
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
touchNode(nodeUuid) {
|
|
78
|
-
this.db.prepare(
|
|
79
|
-
"UPDATE nodes SET last_accessed_at = datetime('now') WHERE uuid = ?"
|
|
80
|
-
).run(nodeUuid);
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
getDiagnostics(namespace = "", daysStale = 30, maxChildren = 10) {
|
|
84
|
-
return getGraphDiagnostics(this.db, namespace, daysStale, maxChildren);
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
cascadeDeleteNode(nodeUuid) {
|
|
88
|
-
return cascadeDeleteNode(this.db, nodeUuid);
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
#createEdgeWithPaths(parentUuid, childUuid, name, domain, path, priority = 0, disclosure = null, namespace = "") {
|
|
92
|
-
const { edge, created } = getOrCreateEdge(this.db, parentUuid, childUuid, name, priority, disclosure);
|
|
93
|
-
insertPath(this.db, namespace, domain, path, edge.id, childUuid);
|
|
94
|
-
cascadeCreatePaths(this.db, childUuid, domain, path, namespace);
|
|
95
|
-
return { edge, edge_id: edge.id, edge_created: created };
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
// ═══════════════════════════════════════════════════════════════════
|
|
99
|
-
// Public Write API
|
|
100
|
-
// ═══════════════════════════════════════════════════════════════════
|
|
101
|
-
|
|
102
|
-
createMemory(parentPath, content, priority, { title = null, disclosure = null, domain = "core", namespace = "" } = {}) {
|
|
103
|
-
const parentUuid = parentPath === ""
|
|
104
|
-
? ROOT_NODE_UUID
|
|
105
|
-
: resolveGraphPath(this.db, parentPath, domain, namespace)?.node_uuid;
|
|
106
|
-
|
|
107
|
-
if (!parentUuid && parentPath !== "") {
|
|
108
|
-
throw new Error(`Parent '${domain}://${parentPath}' does not exist.`);
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
const finalPath = title
|
|
112
|
-
? (parentPath ? `${parentPath}/${title}` : title)
|
|
113
|
-
: `${parentPath ? parentPath + "/" : ""}${getNextChildNumber(this.db, parentUuid, this.#ns(namespace))}`;
|
|
114
|
-
|
|
115
|
-
if (pathExists(this.db, namespace, domain, finalPath)) throw new Error(`Path '${graphUri(domain, finalPath)}' already exists`);
|
|
116
|
-
|
|
117
|
-
const newUuid = randomUUID();
|
|
118
|
-
ensureNode(this.db, newUuid);
|
|
119
|
-
const memory = insertMemory(this.db, newUuid, content);
|
|
120
|
-
const edgeName = title ?? leafName(finalPath);
|
|
121
|
-
|
|
122
|
-
const created = this.#createEdgeWithPaths(parentUuid, newUuid, edgeName, domain, finalPath, priority, disclosure, namespace);
|
|
123
|
-
|
|
124
|
-
if (this.changesetStore) {
|
|
125
|
-
this.changesetStore.record({ memoryId: memory.id, nodeUuid: newUuid, beforeContent: null, afterContent: content });
|
|
126
|
-
}
|
|
127
|
-
if (this.searchIndexer) {
|
|
128
|
-
this.searchIndexer.index(newUuid, content, namespace);
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
return {
|
|
132
|
-
id: memory.id, node_uuid: newUuid, domain, path: finalPath,
|
|
133
|
-
uri: graphUri(domain, finalPath), priority,
|
|
134
|
-
};
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
updateMemory(path, { content = null, priority = null, disclosure = null, domain = "core", namespace = "" } = {}) {
|
|
138
|
-
if (path === "") throw new Error("Cannot update the root node.");
|
|
139
|
-
if (content === null && priority === null && disclosure === null) {
|
|
140
|
-
throw new Error("At least one of content, priority, or disclosure must be set.");
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
const resolved = resolveGraphPath(this.db, path, domain, namespace);
|
|
144
|
-
if (!resolved || !resolved.edge) {
|
|
145
|
-
throw new Error(`Path '${graphUri(domain, path)}' not found`);
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
const { edge, node_uuid: nodeUuid } = resolved;
|
|
149
|
-
let oldMemoryId = null;
|
|
150
|
-
let newMemoryId = null;
|
|
151
|
-
|
|
152
|
-
// Get current memory
|
|
153
|
-
const currentMem = this.db.prepare(
|
|
154
|
-
"SELECT id FROM memories WHERE node_uuid = ? AND deprecated = 0 ORDER BY created_at DESC LIMIT 1"
|
|
155
|
-
).get(nodeUuid);
|
|
156
|
-
oldMemoryId = currentMem?.id ?? null;
|
|
157
|
-
|
|
158
|
-
// Update edge metadata
|
|
159
|
-
if (priority !== null) {
|
|
160
|
-
this.db.prepare("UPDATE edges SET priority = ? WHERE id = ?").run(priority, edge.id);
|
|
161
|
-
}
|
|
162
|
-
if (disclosure !== null) {
|
|
163
|
-
this.db.prepare("UPDATE edges SET disclosure = ? WHERE id = ?").run(disclosure, edge.id);
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
// Content change → new memory version
|
|
167
|
-
if (content !== null) {
|
|
168
|
-
const newMem = insertMemory(this.db, nodeUuid, content, false);
|
|
169
|
-
newMemoryId = newMem.id;
|
|
170
|
-
deprecateNodeMemories(this.db, nodeUuid, newMemoryId);
|
|
171
|
-
this.db.prepare(
|
|
172
|
-
"UPDATE memories SET deprecated = 0, migrated_to = NULL WHERE id = ?"
|
|
173
|
-
).run(newMemoryId);
|
|
174
|
-
|
|
175
|
-
// Record changeset + re-index
|
|
176
|
-
if (this.changesetStore) {
|
|
177
|
-
const oldContent = oldMemoryId
|
|
178
|
-
? this.db.prepare("SELECT content FROM memories WHERE id = ?").get(oldMemoryId)?.content ?? null
|
|
179
|
-
: null;
|
|
180
|
-
this.changesetStore.record({ memoryId: newMemoryId, nodeUuid, beforeContent: oldContent, afterContent: content });
|
|
181
|
-
}
|
|
182
|
-
if (this.searchIndexer) {
|
|
183
|
-
this.searchIndexer.index(nodeUuid, content, namespace);
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
return {
|
|
188
|
-
domain, path, uri: graphUri(domain, path),
|
|
189
|
-
old_memory_id: oldMemoryId, new_memory_id: newMemoryId ?? oldMemoryId, node_uuid: nodeUuid,
|
|
190
|
-
};
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
rollbackToMemory(targetMemoryId) {
|
|
194
|
-
const target = this.db.prepare("SELECT * FROM memories WHERE id = ?").get(targetMemoryId);
|
|
195
|
-
if (!target) throw new Error(`Memory ID ${targetMemoryId} not found`);
|
|
196
|
-
|
|
197
|
-
if (!target.deprecated) {
|
|
198
|
-
return { restored_memory_id: targetMemoryId, was_already_active: true };
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
deprecateNodeMemories(this.db, target.node_uuid, targetMemoryId);
|
|
202
|
-
this.db.prepare(
|
|
203
|
-
"UPDATE memories SET deprecated = 0, migrated_to = NULL WHERE id = ?"
|
|
204
|
-
).run(targetMemoryId);
|
|
205
|
-
|
|
206
|
-
return { restored_memory_id: targetMemoryId, node_uuid: target.node_uuid };
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
addPath(newPath, targetPath, { newDomain = "core", targetDomain = "core", priority = 0, disclosure = null, namespace = "" } = {}) {
|
|
210
|
-
if (newPath === "") throw new Error("Cannot create alias at root path.");
|
|
211
|
-
|
|
212
|
-
const target = resolveGraphPath(this.db, targetPath, targetDomain, namespace);
|
|
213
|
-
if (!target) throw new Error(`Target '${graphUri(targetDomain, targetPath)}' not found`);
|
|
214
|
-
|
|
215
|
-
const parentUuid = newPath.includes("/")
|
|
216
|
-
? resolveGraphPath(this.db, newPath.substring(0, newPath.lastIndexOf("/")), newDomain, namespace)?.node_uuid ?? ROOT_NODE_UUID
|
|
217
|
-
: ROOT_NODE_UUID;
|
|
218
|
-
|
|
219
|
-
if (pathExists(this.db, namespace, newDomain, newPath)) throw new Error(`Path '${graphUri(newDomain, newPath)}' already exists`);
|
|
220
|
-
|
|
221
|
-
if (wouldCreateCycle(this.db, parentUuid, target.node_uuid)) {
|
|
222
|
-
throw new Error(`Cannot create alias: would create a cycle.`);
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
const result = this.#createEdgeWithPaths(
|
|
226
|
-
parentUuid, target.node_uuid,
|
|
227
|
-
leafName(newPath), newDomain, newPath,
|
|
228
|
-
priority, disclosure ?? target.edge?.disclosure, namespace,
|
|
229
|
-
);
|
|
230
|
-
|
|
231
|
-
return {
|
|
232
|
-
new_uri: graphUri(newDomain, newPath),
|
|
233
|
-
target_uri: graphUri(targetDomain, targetPath),
|
|
234
|
-
node_uuid: target.node_uuid,
|
|
235
|
-
edge_id: result.edge_id, edge_created: result.edge_created,
|
|
236
|
-
};
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
removePath(path, domain = "core", namespace = "") {
|
|
240
|
-
return removeGraphPath(this.db, path, domain, namespace);
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
restorePath(path, domain, nodeUuid, { parentUuid = null, priority = 0, disclosure = null, namespace = "" } = {}) {
|
|
244
|
-
if (path === "") throw new Error("Cannot restore root path.");
|
|
245
|
-
|
|
246
|
-
// Check node exists and has an active memory
|
|
247
|
-
const node = this.db.prepare("SELECT uuid FROM nodes WHERE uuid = ?").get(nodeUuid);
|
|
248
|
-
if (!node) throw new Error(`Node '${nodeUuid}' not found`);
|
|
249
|
-
|
|
250
|
-
const activeMem = this.db.prepare(
|
|
251
|
-
"SELECT id FROM memories WHERE node_uuid = ? AND deprecated = 0"
|
|
252
|
-
).get(nodeUuid);
|
|
253
|
-
if (!activeMem) {
|
|
254
|
-
const latest = this.db.prepare(
|
|
255
|
-
"SELECT id FROM memories WHERE node_uuid = ? ORDER BY created_at DESC LIMIT 1"
|
|
256
|
-
).get(nodeUuid);
|
|
257
|
-
if (!latest) throw new Error(`Node '${nodeUuid}' has no memory versions`);
|
|
258
|
-
this.db.prepare(
|
|
259
|
-
"UPDATE memories SET deprecated = 0, migrated_to = NULL WHERE id = ?"
|
|
260
|
-
).run(latest.id);
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
if (pathExists(this.db, namespace, domain, path)) throw new Error(`Path '${graphUri(domain, path)}' already exists`);
|
|
264
|
-
|
|
265
|
-
if (!parentUuid) {
|
|
266
|
-
if (path.includes("/")) {
|
|
267
|
-
const parentPath = path.substring(0, path.lastIndexOf("/"));
|
|
268
|
-
const parent = resolveGraphPath(this.db, parentPath, domain, namespace);
|
|
269
|
-
parentUuid = parent?.node_uuid ?? ROOT_NODE_UUID;
|
|
270
|
-
} else {
|
|
271
|
-
parentUuid = ROOT_NODE_UUID;
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
const edgeName = leafName(path);
|
|
276
|
-
const { edge } = getOrCreateEdge(this.db, parentUuid, nodeUuid, edgeName, priority, disclosure);
|
|
277
|
-
insertPath(this.db, namespace, domain, path, edge.id, nodeUuid);
|
|
278
|
-
|
|
279
|
-
return { uri: graphUri(domain, path), node_uuid: nodeUuid };
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
|
package/src/memory/search.mjs
DELETED
|
@@ -1,142 +0,0 @@
|
|
|
1
|
-
export class SearchIndexer {
|
|
2
|
-
constructor(db) {
|
|
3
|
-
this.db = db;
|
|
4
|
-
this.#ensureSchema();
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
#ensureSchema() {
|
|
8
|
-
this.db.exec(`
|
|
9
|
-
CREATE VIRTUAL TABLE IF NOT EXISTS search_fts USING fts5(
|
|
10
|
-
node_uuid,
|
|
11
|
-
namespace,
|
|
12
|
-
content,
|
|
13
|
-
tokenize = 'porter unicode61'
|
|
14
|
-
);
|
|
15
|
-
`);
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Index a memory's content for full-text search.
|
|
20
|
-
* Replaces any existing entry for the same (node_uuid, namespace).
|
|
21
|
-
*/
|
|
22
|
-
index(nodeUuid, content, namespace = "") {
|
|
23
|
-
this.remove(nodeUuid, namespace);
|
|
24
|
-
this.db.prepare(
|
|
25
|
-
"INSERT INTO search_fts (node_uuid, namespace, content) VALUES (?, ?, ?)",
|
|
26
|
-
).run(nodeUuid, namespace, content);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Remove a document from the FTS index.
|
|
31
|
-
*/
|
|
32
|
-
remove(nodeUuid, namespace = "") {
|
|
33
|
-
this.db.prepare(
|
|
34
|
-
"DELETE FROM search_fts WHERE node_uuid = ? AND namespace = ?",
|
|
35
|
-
).run(nodeUuid, namespace);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Full-text search with ranking.
|
|
40
|
-
* Returns matching node UUIDs, ranked by relevance.
|
|
41
|
-
*/
|
|
42
|
-
search(query, { namespace = "", limit = 20, offset = 0 } = {}) {
|
|
43
|
-
// Sanitize FTS5 query: escape special chars, wrap in quotes for phrase matching
|
|
44
|
-
const sanitized = query.replace(/["*()]/g, "").trim();
|
|
45
|
-
if (!sanitized) return [];
|
|
46
|
-
|
|
47
|
-
const ftsQuery = sanitized.split(/\s+/).map(w => `"${w}"`).join(" OR ");
|
|
48
|
-
|
|
49
|
-
let sql;
|
|
50
|
-
let params;
|
|
51
|
-
if (namespace) {
|
|
52
|
-
sql = `
|
|
53
|
-
SELECT node_uuid, namespace, content,
|
|
54
|
-
rank AS score,
|
|
55
|
-
snippet(search_fts, 1, '<mark>', '</mark>', '...', 32) AS snippet
|
|
56
|
-
FROM search_fts
|
|
57
|
-
WHERE search_fts MATCH ? AND namespace = ?
|
|
58
|
-
ORDER BY rank
|
|
59
|
-
LIMIT ? OFFSET ?
|
|
60
|
-
`;
|
|
61
|
-
params = [ftsQuery, namespace, limit, offset];
|
|
62
|
-
} else {
|
|
63
|
-
sql = `
|
|
64
|
-
SELECT node_uuid, namespace, content,
|
|
65
|
-
rank AS score,
|
|
66
|
-
snippet(search_fts, 1, '<mark>', '</mark>', '...', 32) AS snippet
|
|
67
|
-
FROM search_fts
|
|
68
|
-
WHERE search_fts MATCH ?
|
|
69
|
-
ORDER BY rank
|
|
70
|
-
LIMIT ? OFFSET ?
|
|
71
|
-
`;
|
|
72
|
-
params = [ftsQuery, limit, offset];
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
return this.db.prepare(sql).all(...params);
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* Quick count of indexed documents.
|
|
80
|
-
*/
|
|
81
|
-
count(namespace = "") {
|
|
82
|
-
if (namespace) {
|
|
83
|
-
return this.db.prepare("SELECT COUNT(*) AS cnt FROM search_fts WHERE namespace = ?").get(namespace).cnt;
|
|
84
|
-
}
|
|
85
|
-
return this.db.prepare("SELECT COUNT(*) AS cnt FROM search_fts").get().cnt;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Delete all entries for a namespace.
|
|
90
|
-
*/
|
|
91
|
-
clearNamespace(namespace) {
|
|
92
|
-
const result = this.db.prepare("DELETE FROM search_fts WHERE namespace = ?").run(namespace);
|
|
93
|
-
return result.changes;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* Rebuild the FTS index from the search_documents table.
|
|
98
|
-
*/
|
|
99
|
-
rebuildFromDocuments() {
|
|
100
|
-
this.db.exec("DELETE FROM search_fts");
|
|
101
|
-
const docs = this.db.prepare("SELECT * FROM search_documents").all();
|
|
102
|
-
const insert = this.db.prepare(
|
|
103
|
-
"INSERT INTO search_fts (node_uuid, namespace, content) VALUES (?, ?, ?)",
|
|
104
|
-
);
|
|
105
|
-
for (const doc of docs) {
|
|
106
|
-
insert.run(doc.node_uuid, doc.namespace, doc.content);
|
|
107
|
-
}
|
|
108
|
-
return docs.length;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* Suggest completions based on prefix matching.
|
|
113
|
-
*/
|
|
114
|
-
suggest(prefix, { namespace = "", limit = 8 } = {}) {
|
|
115
|
-
const sanitized = prefix.replace(/[*"]/g, "").trim();
|
|
116
|
-
if (!sanitized || sanitized.length < 2) return [];
|
|
117
|
-
|
|
118
|
-
let sql;
|
|
119
|
-
let params;
|
|
120
|
-
if (namespace) {
|
|
121
|
-
sql = `
|
|
122
|
-
SELECT DISTINCT content
|
|
123
|
-
FROM search_fts
|
|
124
|
-
WHERE search_fts MATCH ? AND namespace = ?
|
|
125
|
-
ORDER BY rank
|
|
126
|
-
LIMIT ?
|
|
127
|
-
`;
|
|
128
|
-
params = [`"${sanitized}"*`, namespace, limit];
|
|
129
|
-
} else {
|
|
130
|
-
sql = `
|
|
131
|
-
SELECT DISTINCT content
|
|
132
|
-
FROM search_fts
|
|
133
|
-
WHERE search_fts MATCH ?
|
|
134
|
-
ORDER BY rank
|
|
135
|
-
LIMIT ?
|
|
136
|
-
`;
|
|
137
|
-
params = [`"${sanitized}"*`, limit];
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
return this.db.prepare(sql).all(...params).map(r => r.content);
|
|
141
|
-
}
|
|
142
|
-
}
|
package/src/memory/snapshot.mjs
DELETED
|
@@ -1,86 +0,0 @@
|
|
|
1
|
-
export class ChangesetStore {
|
|
2
|
-
constructor(db) {
|
|
3
|
-
this.db = db;
|
|
4
|
-
this.#ensureSchema();
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
#ensureSchema() {
|
|
8
|
-
this.db.exec(`
|
|
9
|
-
CREATE TABLE IF NOT EXISTS changesets (
|
|
10
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
11
|
-
memory_id INTEGER NOT NULL REFERENCES memories(id),
|
|
12
|
-
node_uuid TEXT NOT NULL REFERENCES nodes(uuid),
|
|
13
|
-
before_content TEXT,
|
|
14
|
-
after_content TEXT,
|
|
15
|
-
created_at TEXT NOT NULL DEFAULT (datetime('now'))
|
|
16
|
-
);
|
|
17
|
-
CREATE INDEX IF NOT EXISTS idx_changesets_node ON changesets(node_uuid);
|
|
18
|
-
CREATE INDEX IF NOT EXISTS idx_changesets_memory ON changesets(memory_id);
|
|
19
|
-
`);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Record a before/after snapshot for a memory update.
|
|
24
|
-
* @param {number} memoryId - the NEW memory id
|
|
25
|
-
* @param {string} nodeUuid
|
|
26
|
-
* @param {string|null} beforeContent - previous content (null if creation)
|
|
27
|
-
* @param {string} afterContent - new content
|
|
28
|
-
*/
|
|
29
|
-
record({ memoryId, nodeUuid, beforeContent, afterContent }) {
|
|
30
|
-
const result = this.db.prepare(
|
|
31
|
-
"INSERT INTO changesets (memory_id, node_uuid, before_content, after_content) VALUES (?, ?, ?, ?)",
|
|
32
|
-
).run(memoryId, nodeUuid, beforeContent ?? null, afterContent);
|
|
33
|
-
return { id: Number(result.lastInsertRowid), memory_id: memoryId, node_uuid: nodeUuid };
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Get all changesets for a node, most recent first.
|
|
38
|
-
*/
|
|
39
|
-
getHistory(nodeUuid, limit = 50) {
|
|
40
|
-
return this.db.prepare(
|
|
41
|
-
"SELECT * FROM changesets WHERE node_uuid = ? ORDER BY id DESC LIMIT ?",
|
|
42
|
-
).all(nodeUuid, limit);
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Get the most recent changesets across all nodes.
|
|
47
|
-
*/
|
|
48
|
-
getRecent(limit = 30) {
|
|
49
|
-
return this.db.prepare(
|
|
50
|
-
"SELECT * FROM changesets ORDER BY id DESC LIMIT ?",
|
|
51
|
-
).all(limit);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Get a specific changeset by id.
|
|
56
|
-
*/
|
|
57
|
-
getById(id) {
|
|
58
|
-
return this.db.prepare("SELECT * FROM changesets WHERE id = ?").get(id);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Get the diff summary for a changeset — useful for display.
|
|
63
|
-
*/
|
|
64
|
-
diff(changeset) {
|
|
65
|
-
const before = changeset.before_content ?? "";
|
|
66
|
-
const after = changeset.after_content ?? "";
|
|
67
|
-
if (!before) return { type: "create", linesAdded: after.split("\n").length };
|
|
68
|
-
if (!after) return { type: "delete", linesRemoved: before.split("\n").length };
|
|
69
|
-
|
|
70
|
-
const beforeLines = before.split("\n");
|
|
71
|
-
const afterLines = after.split("\n");
|
|
72
|
-
const added = afterLines.filter((l) => !before.includes(l)).length;
|
|
73
|
-
const removed = beforeLines.filter((l) => !after.includes(l)).length;
|
|
74
|
-
return { type: "update", linesAdded: Math.max(0, afterLines.length - beforeLines.length), linesRemoved: Math.max(0, beforeLines.length - afterLines.length), changedLines: added + removed };
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* Delete changesets older than the given number of days.
|
|
79
|
-
*/
|
|
80
|
-
pruneOlderThan(days) {
|
|
81
|
-
const result = this.db.prepare(
|
|
82
|
-
"DELETE FROM changesets WHERE created_at < datetime('now', ?)",
|
|
83
|
-
).run(`-${days} days`);
|
|
84
|
-
return result.changes;
|
|
85
|
-
}
|
|
86
|
-
}
|
|
@@ -1,120 +0,0 @@
|
|
|
1
|
-
import { ROOT_NODE_UUID } from "./database.mjs";
|
|
2
|
-
|
|
3
|
-
export class SystemViews {
|
|
4
|
-
constructor(db, graph, glossaryService, namespace = "") {
|
|
5
|
-
this.db = db;
|
|
6
|
-
this.graph = graph;
|
|
7
|
-
this.glossarySvc = glossaryService;
|
|
8
|
-
this.namespace = namespace;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* system://boot — children of the root node (project boot entries).
|
|
13
|
-
*/
|
|
14
|
-
boot() {
|
|
15
|
-
const children = this.graph.getChildren(ROOT_NODE_UUID, null, null, this.namespace);
|
|
16
|
-
return children.map(c => ({
|
|
17
|
-
name: c.name,
|
|
18
|
-
node_uuid: c.node_uuid,
|
|
19
|
-
priority: c.priority,
|
|
20
|
-
disclosure: c.disclosure,
|
|
21
|
-
content: c.content_snippet ?? null,
|
|
22
|
-
}));
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* system://index — all paths with their node UUIDs, grouped by domain.
|
|
27
|
-
*/
|
|
28
|
-
index() {
|
|
29
|
-
const rows = this.db.prepare(
|
|
30
|
-
"SELECT domain, path, node_uuid, namespace FROM paths WHERE namespace = ? OR namespace = 'global' ORDER BY domain, path",
|
|
31
|
-
).all(this.namespace);
|
|
32
|
-
const domains = {};
|
|
33
|
-
for (const row of rows) {
|
|
34
|
-
const d = row.domain || "core";
|
|
35
|
-
if (!domains[d]) domains[d] = [];
|
|
36
|
-
domains[d].push({ path: row.path, node_uuid: row.node_uuid, namespace: row.namespace });
|
|
37
|
-
}
|
|
38
|
-
return domains;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* system://recent — most recently updated memories.
|
|
43
|
-
*/
|
|
44
|
-
recent(limit = 20) {
|
|
45
|
-
return this.graph.getRecentMemories(limit, this.namespace);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* system://glossary — all glossary keywords with linked nodes.
|
|
50
|
-
*/
|
|
51
|
-
glossaryList() {
|
|
52
|
-
return this.glossarySvc.getAllKeywords();
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* system://diagnostic — health and stats overview.
|
|
57
|
-
*/
|
|
58
|
-
diagnostic() {
|
|
59
|
-
const ns = this.namespace;
|
|
60
|
-
const nodeCount = this.db.prepare("SELECT COUNT(*) AS cnt FROM nodes").get().cnt;
|
|
61
|
-
const memoryCount = this.db.prepare("SELECT COUNT(DISTINCT m.id) AS cnt FROM memories m JOIN paths p ON p.node_uuid = m.node_uuid WHERE m.deprecated = 0 AND (p.namespace = ? OR p.namespace = 'global')").get(ns).cnt;
|
|
62
|
-
const deprecatedCount = this.db.prepare("SELECT COUNT(*) AS cnt FROM memories WHERE deprecated = 1").get().cnt;
|
|
63
|
-
const edgeCount = this.db.prepare("SELECT COUNT(DISTINCT e.id) AS cnt FROM edges e JOIN paths p ON p.edge_id = e.id WHERE p.namespace = ? OR p.namespace = 'global'").get(ns).cnt;
|
|
64
|
-
const pathCount = this.db.prepare("SELECT COUNT(*) AS cnt FROM paths WHERE namespace = ? OR namespace = 'global'").get(ns).cnt;
|
|
65
|
-
const keywordCount = this.db.prepare("SELECT COUNT(*) AS cnt FROM glossary_keywords WHERE namespace = ? OR namespace = 'global'").get(ns).cnt;
|
|
66
|
-
const ftsCount = this.#ftsCount();
|
|
67
|
-
const changesetCount = this.#changesetCount();
|
|
68
|
-
|
|
69
|
-
// Stale nodes: no access in 30 days
|
|
70
|
-
const cutoff = new Date(Date.now() - 30 * 86400000).toISOString();
|
|
71
|
-
const staleNodes = this.db.prepare(
|
|
72
|
-
"SELECT COUNT(*) AS cnt FROM nodes WHERE last_accessed_at IS NOT NULL AND last_accessed_at < ?",
|
|
73
|
-
).get(cutoff).cnt;
|
|
74
|
-
|
|
75
|
-
// Orphan nodes: nodes with no paths pointing to them
|
|
76
|
-
const orphanNodes = this.db.prepare(`
|
|
77
|
-
SELECT COUNT(*) AS cnt FROM nodes n
|
|
78
|
-
WHERE n.uuid != ?
|
|
79
|
-
AND NOT EXISTS (SELECT 1 FROM paths WHERE node_uuid = n.uuid)
|
|
80
|
-
AND NOT EXISTS (SELECT 1 FROM edges WHERE child_uuid = n.uuid)
|
|
81
|
-
`).get(ROOT_NODE_UUID).cnt;
|
|
82
|
-
|
|
83
|
-
// Version chain depth stats
|
|
84
|
-
const versionStats = this.db.prepare(`
|
|
85
|
-
SELECT MAX(vc) AS max_depth, AVG(vc) AS avg_depth
|
|
86
|
-
FROM (SELECT COUNT(*) AS vc FROM memories GROUP BY node_uuid)
|
|
87
|
-
`).get();
|
|
88
|
-
|
|
89
|
-
return {
|
|
90
|
-
counts: {
|
|
91
|
-
nodes: nodeCount,
|
|
92
|
-
memories: memoryCount,
|
|
93
|
-
deprecated_memories: deprecatedCount,
|
|
94
|
-
edges: edgeCount,
|
|
95
|
-
paths: pathCount,
|
|
96
|
-
keywords: keywordCount,
|
|
97
|
-
fts_documents: ftsCount,
|
|
98
|
-
changesets: changesetCount,
|
|
99
|
-
},
|
|
100
|
-
health: {
|
|
101
|
-
stale_nodes: staleNodes,
|
|
102
|
-
orphan_nodes: orphanNodes,
|
|
103
|
-
max_version_depth: versionStats.max_depth ?? 0,
|
|
104
|
-
avg_version_depth: Math.round((versionStats.avg_depth ?? 0) * 100) / 100,
|
|
105
|
-
},
|
|
106
|
-
};
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
#ftsCount() {
|
|
110
|
-
try {
|
|
111
|
-
return this.db.prepare("SELECT COUNT(*) AS cnt FROM search_fts").get().cnt;
|
|
112
|
-
} catch { return 0; }
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
#changesetCount() {
|
|
116
|
-
try {
|
|
117
|
-
return this.db.prepare("SELECT COUNT(*) AS cnt FROM changesets").get().cnt;
|
|
118
|
-
} catch { return 0; }
|
|
119
|
-
}
|
|
120
|
-
}
|