@zenera/neo 1.1.54 → 1.1.56
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/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/kernel.d.ts +11 -1
- package/dist/kernel.js +24 -1
- package/dist/memory/files.d.ts +37 -3
- package/dist/memory/files.js +26 -14
- package/dist/memory/graph.d.ts +22 -0
- package/dist/memory/graph.js +72 -7
- package/dist/memory/grep.js +3 -2
- package/dist/memory/index.d.ts +17 -4
- package/dist/memory/index.js +112 -31
- package/dist/memory/merge.js +29 -17
- package/dist/memory/migrate.d.ts +14 -0
- package/dist/memory/migrate.js +112 -0
- package/dist/memory/render.js +2 -1
- package/dist/memory/store.d.ts +36 -27
- package/dist/memory/store.js +409 -126
- package/dist/memory/tools.js +70 -9
- package/dist/memory/types.d.ts +1 -1
- package/dist/memory/types.js +1 -1
- package/dist/project/config.d.ts +1 -24
- package/dist/project/config.js +9 -4
- package/dist/project/load.d.ts +6 -8
- package/dist/project/load.js +3 -4
- package/dist/runner.d.ts +23 -6
- package/dist/runner.js +72 -8
- package/dist/services.d.ts +8 -6
- package/dist/services.js +7 -6
- package/dist/state.d.ts +8 -2
- package/dist/tools/files.d.ts +28 -37
- package/dist/tools/files.js +106 -84
- package/dist/tools/index.d.ts +1 -0
- package/dist/tools/index.js +3 -0
- package/dist/tools/sandbox.d.ts +2 -269
- package/dist/tools/sandbox.js +10 -741
- package/dist/types.d.ts +42 -0
- package/dist/types.js +22 -0
- package/package.json +2 -1
- package/dist/tools/workspace.d.ts +0 -79
- package/dist/tools/workspace.js +0 -1470
package/dist/memory/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { readFile } from 'node:fs/promises';
|
|
2
2
|
import { forgetFile, hostPath, rememberFile } from "./files.js";
|
|
3
|
+
import { RevisionMismatch } from "./graph.js";
|
|
3
4
|
import { recall } from "./recall.js";
|
|
4
5
|
import { ALL_AGENTS, MEMORY_KINDS, MEMORY_RELATIONS, MemoryError, PREFERENCE_KIND, visible, } from "./types.js";
|
|
5
6
|
// ---------------------------------------------------------------------------
|
|
@@ -15,6 +16,11 @@ import { ALL_AGENTS, MEMORY_KINDS, MEMORY_RELATIONS, MemoryError, PREFERENCE_KIN
|
|
|
15
16
|
// Everything is validated, and every source file stat-ed, before the first
|
|
16
17
|
// mutation, on the same principle as `runPatch` in tools/files.ts: a
|
|
17
18
|
// commit that fails must leave no half-built graph behind.
|
|
19
|
+
//
|
|
20
|
+
// Other runs may be writing the same memory. So the plan is made against the
|
|
21
|
+
// graph as last seen, the slow parts — embedding, copying files — happen
|
|
22
|
+
// outside the store's write lock, and what the plan assumed about existing
|
|
23
|
+
// nodes is checked again inside it, against the graph as it is on disk.
|
|
18
24
|
// ---------------------------------------------------------------------------
|
|
19
25
|
/** Past this a `load` returns the path and lets the agent open it itself. */
|
|
20
26
|
export const INLINE_FILE_BYTES = 32 * 1024;
|
|
@@ -44,6 +50,10 @@ export class MemoryIndex {
|
|
|
44
50
|
get relations() {
|
|
45
51
|
return [...this.#relations];
|
|
46
52
|
}
|
|
53
|
+
/** Catches up with what other runs committed to the same memory. */
|
|
54
|
+
refresh() {
|
|
55
|
+
this.store.refresh();
|
|
56
|
+
}
|
|
47
57
|
/**
|
|
48
58
|
* Read-only: a search returns nodes the model has not read, so nothing is
|
|
49
59
|
* touched. `stale` is the audit view — it puts back the superseded nodes
|
|
@@ -53,6 +63,7 @@ export class MemoryIndex {
|
|
|
53
63
|
*/
|
|
54
64
|
async search(query, sees, opts = {}) {
|
|
55
65
|
const vector = query.text ? await this.#embed([query.text], 'query') : undefined;
|
|
66
|
+
this.refresh();
|
|
56
67
|
return recall({
|
|
57
68
|
graph: this.graph,
|
|
58
69
|
query,
|
|
@@ -82,7 +93,7 @@ export class MemoryIndex {
|
|
|
82
93
|
*/
|
|
83
94
|
async load(ids, sees, clock) {
|
|
84
95
|
const at = clock.now();
|
|
85
|
-
|
|
96
|
+
this.refresh();
|
|
86
97
|
for (const id of ids) {
|
|
87
98
|
const node = this.graph.get(id);
|
|
88
99
|
// Indistinguishable from "no such node" on purpose: a refusal that
|
|
@@ -90,35 +101,63 @@ export class MemoryIndex {
|
|
|
90
101
|
if (!node || !visible(node, sees)) {
|
|
91
102
|
throw new MemoryError(`no memory ${id}`, 'use an id from the legend of a memory_search result');
|
|
92
103
|
}
|
|
93
|
-
|
|
94
|
-
|
|
104
|
+
}
|
|
105
|
+
const touch = () => ids.map((id) => this.graph.touch(id, at) ?? this.graph.get(id));
|
|
106
|
+
// The bump is bookkeeping, and persisting it is what would make a
|
|
107
|
+
// read-only recall rewrite the graph every other run is reading.
|
|
108
|
+
const touched = this.store.readOnly
|
|
109
|
+
? this.graph.untracked(touch)
|
|
110
|
+
: this.store.write(() => {
|
|
111
|
+
// Forgotten by another run since the check above.
|
|
112
|
+
const missing = ids.find((id) => !this.graph.has(id));
|
|
113
|
+
if (missing) {
|
|
114
|
+
throw new MemoryError(`no memory ${missing}`);
|
|
115
|
+
}
|
|
116
|
+
return touch();
|
|
117
|
+
});
|
|
118
|
+
const out = [];
|
|
119
|
+
for (const node of touched) {
|
|
120
|
+
const file = node.file;
|
|
95
121
|
out.push({
|
|
96
|
-
node
|
|
122
|
+
node,
|
|
97
123
|
content: file && file.bytes <= INLINE_FILE_BYTES
|
|
98
124
|
? await readFile(hostPath(this.store.dir, file), 'utf8')
|
|
99
125
|
: undefined,
|
|
100
126
|
});
|
|
101
127
|
}
|
|
102
|
-
// The bump is bookkeeping, and persisting it is what would make a
|
|
103
|
-
// read-only recall rewrite the graph every other run is reading.
|
|
104
|
-
if (!this.store.readOnly) {
|
|
105
|
-
await this.store.commit();
|
|
106
|
-
}
|
|
107
128
|
return out;
|
|
108
129
|
}
|
|
130
|
+
/**
|
|
131
|
+
* A remembered file's record and bytes, for copying it back into a
|
|
132
|
+
* workspace. Visible nodes only, and missing reads the same as hidden.
|
|
133
|
+
* Not a load: copying a file is not reading the memory, so recency is
|
|
134
|
+
* left alone.
|
|
135
|
+
*/
|
|
136
|
+
async fileOf(id, sees) {
|
|
137
|
+
this.refresh();
|
|
138
|
+
const node = this.graph.get(id);
|
|
139
|
+
if (!node || !visible(node, sees)) {
|
|
140
|
+
throw new MemoryError(`no memory ${id}`, 'use an id from the legend of a memory_search result');
|
|
141
|
+
}
|
|
142
|
+
if (!node.file) {
|
|
143
|
+
throw new MemoryError(`${id} holds no file`, 'only nodes committed with a `file` have one to copy');
|
|
144
|
+
}
|
|
145
|
+
return { file: node.file, bytes: await readFile(hostPath(this.store.dir, node.file)) };
|
|
146
|
+
}
|
|
109
147
|
async commit(tx, opts) {
|
|
110
148
|
const nodes = tx.nodes ?? [];
|
|
111
149
|
const edges = tx.edges ?? [];
|
|
112
150
|
if (!nodes.length && !edges.length) {
|
|
113
151
|
throw new MemoryError('a commit needs at least one node or edge');
|
|
114
152
|
}
|
|
153
|
+
this.refresh();
|
|
115
154
|
const plan = this.#plan(nodes, edges, opts);
|
|
116
155
|
const vectors = await this.#embed(plan.embed.map((e) => e.text), 'document');
|
|
117
156
|
// Keyed before anything is dropped: `#merge` reshapes `plan.embed`, and
|
|
118
157
|
// the vectors only line up with it positionally until it does.
|
|
119
158
|
const vector = new Map(plan.embed.map((e, i) => [e.id, vectors?.[i]]));
|
|
120
|
-
const merged = this.#merge(plan, vector, opts.sees);
|
|
121
159
|
// Files first, so a failed copy can be undone before the graph knows.
|
|
160
|
+
// A file-bearing node never folds onto a twin, so none is copied in vain.
|
|
122
161
|
const written = [];
|
|
123
162
|
try {
|
|
124
163
|
for (const item of plan.files) {
|
|
@@ -136,26 +175,36 @@ export class MemoryIndex {
|
|
|
136
175
|
const at = opts.clock.now();
|
|
137
176
|
let created = 0;
|
|
138
177
|
let updated = 0;
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
178
|
+
let merged = 0;
|
|
179
|
+
try {
|
|
180
|
+
this.store.write(() => {
|
|
181
|
+
this.#recheck(plan);
|
|
182
|
+
merged = this.#merge(plan, vector, opts.sees);
|
|
183
|
+
for (const item of plan.nodes) {
|
|
184
|
+
if (item.existing) {
|
|
185
|
+
this.graph.update(item.id, item.patch, at);
|
|
186
|
+
updated++;
|
|
187
|
+
}
|
|
188
|
+
else {
|
|
189
|
+
this.graph.add(item.draft, at);
|
|
190
|
+
created++;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
for (const edge of plan.edges) {
|
|
194
|
+
this.graph.link(edge.source, edge.target, edge.relation, at);
|
|
195
|
+
}
|
|
196
|
+
for (const e of plan.embed) {
|
|
197
|
+
const v = vector.get(e.id);
|
|
198
|
+
if (v) {
|
|
199
|
+
this.store.vectors?.set(e.id, v);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
});
|
|
151
203
|
}
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
this.store.vectors?.set(e.id, v);
|
|
156
|
-
}
|
|
204
|
+
catch (err) {
|
|
205
|
+
await Promise.all(written.map((f) => forgetFile(this.store.dir, f)));
|
|
206
|
+
throw err;
|
|
157
207
|
}
|
|
158
|
-
await this.store.commit();
|
|
159
208
|
return {
|
|
160
209
|
ids: plan.ids,
|
|
161
210
|
created,
|
|
@@ -165,6 +214,34 @@ export class MemoryIndex {
|
|
|
165
214
|
merged,
|
|
166
215
|
};
|
|
167
216
|
}
|
|
217
|
+
/**
|
|
218
|
+
* What the plan assumed about nodes it did not create, asked again of the
|
|
219
|
+
* graph as it is now: another run may have forgotten or revised them since.
|
|
220
|
+
* Before any mutation, so a refusal changes nothing.
|
|
221
|
+
*/
|
|
222
|
+
#recheck(plan) {
|
|
223
|
+
const minted = new Set(plan.nodes.filter((n) => !n.existing).map((n) => n.id));
|
|
224
|
+
for (const item of plan.nodes) {
|
|
225
|
+
if (!item.existing) {
|
|
226
|
+
continue;
|
|
227
|
+
}
|
|
228
|
+
const current = this.graph.get(item.id);
|
|
229
|
+
if (!current) {
|
|
230
|
+
throw new MemoryError(`${item.id}: no such memory`, 'another run forgot it while this one was committing');
|
|
231
|
+
}
|
|
232
|
+
const expected = item.patch.expectedRevision;
|
|
233
|
+
if (expected !== undefined && expected !== current.revision) {
|
|
234
|
+
throw new RevisionMismatch(item.id, expected, current.revision);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
for (const edge of plan.edges) {
|
|
238
|
+
for (const end of [edge.source, edge.target]) {
|
|
239
|
+
if (!minted.has(end) && !this.graph.has(end)) {
|
|
240
|
+
throw new MemoryError(`"${end}" is no longer a memory`, 'another run forgot it while this one was committing');
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
}
|
|
168
245
|
/**
|
|
169
246
|
* A second run that learns the same thing should join the graph, not fork
|
|
170
247
|
* it. Nothing tells an agent the id of a node it is about to write, so two
|
|
@@ -225,6 +302,7 @@ export class MemoryIndex {
|
|
|
225
302
|
return twins.size;
|
|
226
303
|
}
|
|
227
304
|
async forget(ids, sees) {
|
|
305
|
+
this.refresh();
|
|
228
306
|
const doomed = ids.map((id) => {
|
|
229
307
|
const node = this.graph.get(id);
|
|
230
308
|
if (!node || !visible(node, sees)) {
|
|
@@ -232,14 +310,17 @@ export class MemoryIndex {
|
|
|
232
310
|
}
|
|
233
311
|
return node;
|
|
234
312
|
});
|
|
235
|
-
for
|
|
236
|
-
|
|
313
|
+
// Gone already is what was asked for, so another run beating this one to it is fine.
|
|
314
|
+
const removed = this.store.write(() => doomed.filter((node) => {
|
|
237
315
|
this.store.vectors?.remove(node.id);
|
|
316
|
+
return this.graph.forget(node.id) !== undefined;
|
|
317
|
+
}));
|
|
318
|
+
// Bytes after the graph: a node naming a missing file is worse than a stray file.
|
|
319
|
+
for (const node of removed) {
|
|
238
320
|
if (node.file) {
|
|
239
321
|
await forgetFile(this.store.dir, node.file);
|
|
240
322
|
}
|
|
241
323
|
}
|
|
242
|
-
await this.store.commit();
|
|
243
324
|
return doomed;
|
|
244
325
|
}
|
|
245
326
|
/**
|
package/dist/memory/merge.js
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
import { createHash } from 'node:crypto';
|
|
2
2
|
import { readFile, rm, stat, writeFile } from 'node:fs/promises';
|
|
3
|
-
import {
|
|
3
|
+
import { resolve } from 'node:path';
|
|
4
4
|
import { hostPath } from "./files.js";
|
|
5
5
|
import { DUPLICATE_SCORE, flatten } from "./index.js";
|
|
6
|
-
import {
|
|
6
|
+
import { MEMORY_DB, MemoryStore, isMemory } from "./store.js";
|
|
7
7
|
import { comparable, sameContent, twinKey } from "./twin.js";
|
|
8
8
|
import { MemoryError } from "./types.js";
|
|
9
9
|
// ---------------------------------------------------------------------------
|
|
10
10
|
// Folding several memories into one
|
|
11
11
|
//
|
|
12
12
|
// Warming a memory is embarrassingly parallel — N runs, N questions, N answers
|
|
13
|
-
// —
|
|
14
|
-
//
|
|
13
|
+
// — and a batch gives each run its own copy so that none of them sees what the
|
|
14
|
+
// others are learning mid-round. This puts the results back together.
|
|
15
15
|
//
|
|
16
16
|
// Ids are ULIDs and are preserved, which makes the interesting case the one
|
|
17
17
|
// where the same id appears twice: the runs started from a copy of the same
|
|
@@ -50,8 +50,18 @@ export class MergeConflicts extends MemoryError {
|
|
|
50
50
|
* on disk. Report the result and discard the store; do not commit it.
|
|
51
51
|
*/
|
|
52
52
|
export async function mergeMemories(target, sources, opts = {}) {
|
|
53
|
+
const opened = await openSources(target, sources);
|
|
54
|
+
try {
|
|
55
|
+
return await fold(target, opened, opts);
|
|
56
|
+
}
|
|
57
|
+
finally {
|
|
58
|
+
for (const src of opened) {
|
|
59
|
+
src.release();
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
async function fold(target, opened, opts) {
|
|
53
64
|
const dedupe = opts.dedupe !== false;
|
|
54
|
-
const opened = await openSources(target, sources, opts.force === true);
|
|
55
65
|
const reports = new Map(opened.map((src) => [
|
|
56
66
|
src,
|
|
57
67
|
{
|
|
@@ -177,13 +187,12 @@ export async function mergeMemories(target, sources, opts = {}) {
|
|
|
177
187
|
await target.commit();
|
|
178
188
|
return total(report);
|
|
179
189
|
}
|
|
180
|
-
async function openSources(target, sources
|
|
190
|
+
async function openSources(target, sources) {
|
|
181
191
|
if (!sources.length) {
|
|
182
192
|
throw new MemoryError('no memories to merge', 'name at least one memory directory');
|
|
183
193
|
}
|
|
184
194
|
const here = resolve(target.dir);
|
|
185
195
|
const seen = new Set();
|
|
186
|
-
const out = [];
|
|
187
196
|
for (const dir of sources) {
|
|
188
197
|
const path = resolve(dir);
|
|
189
198
|
if (path === here) {
|
|
@@ -193,19 +202,22 @@ async function openSources(target, sources, force) {
|
|
|
193
202
|
throw new MemoryError(`${dir} was named twice`, 'name each memory once');
|
|
194
203
|
}
|
|
195
204
|
seen.add(path);
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
try {
|
|
199
|
-
await stat(join(path, MANIFEST_FILE));
|
|
205
|
+
if (!isMemory(path)) {
|
|
206
|
+
throw new MemoryError(`${dir} is not a memory`, `no ${MEMORY_DB} in it`);
|
|
200
207
|
}
|
|
201
|
-
|
|
202
|
-
|
|
208
|
+
}
|
|
209
|
+
const out = [];
|
|
210
|
+
try {
|
|
211
|
+
for (const path of seen) {
|
|
212
|
+
// Read-only: a run may still be writing a source, and a snapshot of it is enough.
|
|
213
|
+
out.push(await MemoryStore.open(path, { readOnly: true }));
|
|
203
214
|
}
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
215
|
+
}
|
|
216
|
+
catch (err) {
|
|
217
|
+
for (const src of out) {
|
|
218
|
+
src.release();
|
|
207
219
|
}
|
|
208
|
-
|
|
220
|
+
throw err;
|
|
209
221
|
}
|
|
210
222
|
return out;
|
|
211
223
|
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type MemoryEmbedding } from './store.ts';
|
|
2
|
+
/** Where the version 1 files are kept once converted. */
|
|
3
|
+
export declare const LEGACY_DIR = "v1";
|
|
4
|
+
export interface MigrationReport {
|
|
5
|
+
dir: string;
|
|
6
|
+
nodes: number;
|
|
7
|
+
edges: number;
|
|
8
|
+
vectors: number;
|
|
9
|
+
embedding?: MemoryEmbedding;
|
|
10
|
+
/** where the version 1 files went */
|
|
11
|
+
legacy: string;
|
|
12
|
+
}
|
|
13
|
+
export declare function migrateMemory(dir: string): Promise<MigrationReport>;
|
|
14
|
+
//# sourceMappingURL=migrate.d.ts.map
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { existsSync } from 'node:fs';
|
|
2
|
+
import { mkdir, readFile, rename, rm } from 'node:fs/promises';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { liveHolder } from "../lock.js";
|
|
5
|
+
import { MemoryGraph } from "./graph.js";
|
|
6
|
+
import { isMemory, LEGACY_GRAPH_FILE, MEMORY_DB, MEMORY_VERSION, MemoryStore, } from "./store.js";
|
|
7
|
+
import { MemoryError } from "./types.js";
|
|
8
|
+
// ---------------------------------------------------------------------------
|
|
9
|
+
// From the version 1 layout to the database
|
|
10
|
+
//
|
|
11
|
+
// Version 1 kept a memory as `graph.json`, `vectors.f32`, `vectors.json` and a
|
|
12
|
+
// `manifest.json` written last. The conversion reads all of it, writes the
|
|
13
|
+
// database in one transaction, and moves the old files into `v1/` rather than
|
|
14
|
+
// deleting them, so a migration that turns out wrong is undone by moving four
|
|
15
|
+
// files back. `files/` is shared by both layouts and does not move.
|
|
16
|
+
// ---------------------------------------------------------------------------
|
|
17
|
+
/** Where the version 1 files are kept once converted. */
|
|
18
|
+
export const LEGACY_DIR = 'v1';
|
|
19
|
+
const LEGACY_FILES = [LEGACY_GRAPH_FILE, 'vectors.f32', 'vectors.json', 'manifest.json', '.lock'];
|
|
20
|
+
export async function migrateMemory(dir) {
|
|
21
|
+
if (isMemory(dir)) {
|
|
22
|
+
throw new MemoryError(`${dir} is already a version ${MEMORY_VERSION} memory`);
|
|
23
|
+
}
|
|
24
|
+
if (!existsSync(join(dir, LEGACY_GRAPH_FILE))) {
|
|
25
|
+
throw new MemoryError(`${dir} is not a version 1 memory`, `no ${LEGACY_GRAPH_FILE} in it`);
|
|
26
|
+
}
|
|
27
|
+
const held = liveHolder(join(dir, '.lock'));
|
|
28
|
+
if (held) {
|
|
29
|
+
throw new MemoryError(`this memory is in use (pid ${held.pid}, since ${held.startedAt})`, 'wait for that run to finish');
|
|
30
|
+
}
|
|
31
|
+
const manifest = await readJson(join(dir, 'manifest.json'));
|
|
32
|
+
if (manifest?.version !== undefined && manifest.version !== 1) {
|
|
33
|
+
throw new MemoryError(`${dir} is version ${manifest.version}, not version 1`);
|
|
34
|
+
}
|
|
35
|
+
const graph = MemoryGraph.from(JSON.parse(await readFile(join(dir, LEGACY_GRAPH_FILE), 'utf8')));
|
|
36
|
+
const embedding = manifest?.embedding;
|
|
37
|
+
const vectors = embedding
|
|
38
|
+
? await readVectors(dir, embedding.dimensions)
|
|
39
|
+
: new Map();
|
|
40
|
+
const legacy = join(dir, LEGACY_DIR);
|
|
41
|
+
await mkdir(legacy, { recursive: true });
|
|
42
|
+
const moved = [];
|
|
43
|
+
for (const name of LEGACY_FILES) {
|
|
44
|
+
if (existsSync(join(dir, name))) {
|
|
45
|
+
await rename(join(dir, name), join(legacy, name));
|
|
46
|
+
moved.push(name);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
try {
|
|
50
|
+
const store = await MemoryStore.open(dir, embedding ? { embedding } : {});
|
|
51
|
+
try {
|
|
52
|
+
for (const node of graph.nodes()) {
|
|
53
|
+
store.graph.adopt(node);
|
|
54
|
+
}
|
|
55
|
+
for (const link of graph.links()) {
|
|
56
|
+
store.graph.adoptEdge(link.source, link.target, link.attrs);
|
|
57
|
+
}
|
|
58
|
+
for (const [id, vector] of vectors) {
|
|
59
|
+
if (graph.has(id)) {
|
|
60
|
+
store.vectors?.set(id, vector);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
await store.commit();
|
|
64
|
+
}
|
|
65
|
+
finally {
|
|
66
|
+
store.release();
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
catch (err) {
|
|
70
|
+
for (const suffix of ['', '-wal', '-shm']) {
|
|
71
|
+
await rm(join(dir, MEMORY_DB + suffix), { force: true });
|
|
72
|
+
}
|
|
73
|
+
for (const name of moved) {
|
|
74
|
+
await rename(join(legacy, name), join(dir, name));
|
|
75
|
+
}
|
|
76
|
+
throw err;
|
|
77
|
+
}
|
|
78
|
+
return {
|
|
79
|
+
dir,
|
|
80
|
+
nodes: graph.order,
|
|
81
|
+
edges: graph.size,
|
|
82
|
+
vectors: [...vectors.keys()].filter((id) => graph.has(id)).length,
|
|
83
|
+
...(embedding ? { embedding } : {}),
|
|
84
|
+
legacy,
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
async function readJson(path) {
|
|
88
|
+
try {
|
|
89
|
+
return JSON.parse(await readFile(path, 'utf8'));
|
|
90
|
+
}
|
|
91
|
+
catch {
|
|
92
|
+
return undefined;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
/** Vectors of a width that no longer matches the manifest are dropped, as version 1 did. */
|
|
96
|
+
async function readVectors(dir, dimensions) {
|
|
97
|
+
const out = new Map();
|
|
98
|
+
const meta = await readJson(join(dir, 'vectors.json'));
|
|
99
|
+
if (!meta || meta.dims !== dimensions) {
|
|
100
|
+
return out;
|
|
101
|
+
}
|
|
102
|
+
const raw = await readFile(join(dir, 'vectors.f32'));
|
|
103
|
+
const data = new Float32Array(Math.floor(raw.byteLength / 4));
|
|
104
|
+
new Uint8Array(data.buffer).set(raw.subarray(0, data.length * 4));
|
|
105
|
+
meta.ids.forEach((id, row) => {
|
|
106
|
+
if ((row + 1) * dimensions <= data.length) {
|
|
107
|
+
out.set(id, data.slice(row * dimensions, (row + 1) * dimensions));
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
return out;
|
|
111
|
+
}
|
|
112
|
+
//# sourceMappingURL=migrate.js.map
|
package/dist/memory/render.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { storedName } from "./files.js";
|
|
1
2
|
import { SPINE_RELATIONS, } from "./types.js";
|
|
2
3
|
// ---------------------------------------------------------------------------
|
|
3
4
|
// What the model actually reads
|
|
@@ -176,7 +177,7 @@ function block(entry, opts) {
|
|
|
176
177
|
const indent = ' '.repeat(Math.max(TEXT_COLUMN, (entry.depth + 2) * STEP));
|
|
177
178
|
const lines = [`${' '.repeat(entry.depth * STEP)}${header(entry)}`];
|
|
178
179
|
if (node.file) {
|
|
179
|
-
const facts = [node.file
|
|
180
|
+
const facts = [storedName(node.file), bytes(node.file.bytes), used(node, now)];
|
|
180
181
|
lines.push(`${indent}${facts.join(' · ')}`);
|
|
181
182
|
}
|
|
182
183
|
const summary = clipped(node.text, opts.clip ?? TEXT_CLIP);
|
package/dist/memory/store.d.ts
CHANGED
|
@@ -1,44 +1,34 @@
|
|
|
1
|
-
import { type Lock } from '../lock.ts';
|
|
2
1
|
import { MemoryGraph } from './graph.ts';
|
|
3
2
|
import { VectorBlock } from './vectors.ts';
|
|
4
|
-
export declare const MEMORY_VERSION =
|
|
5
|
-
export declare const
|
|
6
|
-
export declare const VECTORS_FILE = "vectors.f32";
|
|
7
|
-
export declare const VECTOR_IDS_FILE = "vectors.json";
|
|
8
|
-
export declare const MANIFEST_FILE = "manifest.json";
|
|
3
|
+
export declare const MEMORY_VERSION = 2;
|
|
4
|
+
export declare const MEMORY_DB = "memory.db";
|
|
9
5
|
export declare const FILES_DIR = "files";
|
|
10
|
-
|
|
6
|
+
/** The version 1 graph; present without a database, it means the memory needs migrating. */
|
|
7
|
+
export declare const LEGACY_GRAPH_FILE = "graph.json";
|
|
8
|
+
/** How long a write waits for another to finish before giving up. */
|
|
9
|
+
export declare const BUSY_TIMEOUT_MS = 5000;
|
|
11
10
|
export interface MemoryEmbedding {
|
|
12
11
|
model: string;
|
|
13
12
|
dimensions: number;
|
|
14
13
|
}
|
|
15
|
-
export interface MemoryManifest {
|
|
16
|
-
version: number;
|
|
17
|
-
/** absent when the project configured no embedder and ranks lexically */
|
|
18
|
-
embedding?: MemoryEmbedding;
|
|
19
|
-
nodes: number;
|
|
20
|
-
edges: number;
|
|
21
|
-
updatedAt: string;
|
|
22
|
-
}
|
|
23
14
|
export interface OpenOptions {
|
|
24
|
-
/** the embedder the caller intends to use; a mismatch with the
|
|
15
|
+
/** the embedder the caller intends to use; a mismatch with the memory is fatal */
|
|
25
16
|
embedding?: MemoryEmbedding;
|
|
26
|
-
/** take the directory lock; off for read-only inspection (`zen memory ls`) */
|
|
27
|
-
lock?: boolean;
|
|
28
17
|
/**
|
|
29
|
-
* Recall, and nothing else. Writing is refused rather than dropped, the
|
|
30
|
-
* directory is never created,
|
|
31
|
-
* may share one graph at once, which is the whole point of it.
|
|
18
|
+
* Recall, and nothing else. Writing is refused rather than dropped, and the
|
|
19
|
+
* directory is never created, so pointing at a mistyped path fails here.
|
|
32
20
|
*/
|
|
33
21
|
readOnly?: boolean;
|
|
22
|
+
/** how long a write waits on another; `BUSY_TIMEOUT_MS` by default */
|
|
23
|
+
busyTimeoutMs?: number;
|
|
34
24
|
}
|
|
35
25
|
export declare class MemoryStore {
|
|
36
26
|
#private;
|
|
37
27
|
readonly dir: string;
|
|
38
|
-
readonly graph: MemoryGraph;
|
|
39
28
|
readonly readOnly: boolean;
|
|
40
29
|
private constructor();
|
|
41
30
|
static open(dir: string, opts?: OpenOptions): Promise<MemoryStore>;
|
|
31
|
+
get graph(): MemoryGraph;
|
|
42
32
|
get embedding(): MemoryEmbedding | undefined;
|
|
43
33
|
/**
|
|
44
34
|
* Records the vectoriser actually in use. A width is only knowable from a
|
|
@@ -50,14 +40,33 @@ export declare class MemoryStore {
|
|
|
50
40
|
/** Absent when no embedder is configured; recall then falls back to term overlap. */
|
|
51
41
|
get vectors(): VectorBlock | undefined;
|
|
52
42
|
filePath(name: string): string;
|
|
53
|
-
/**
|
|
43
|
+
/**
|
|
44
|
+
* Catches up with what other connections committed. Cheap when nothing
|
|
45
|
+
* did: one pragma, answered from shared memory without touching the file.
|
|
46
|
+
*/
|
|
47
|
+
refresh(): void;
|
|
48
|
+
/**
|
|
49
|
+
* One transaction: catch up, let `fn` change the graph, persist what it
|
|
50
|
+
* changed. Whatever `fn` throws rolls the whole thing back, on disk and in
|
|
51
|
+
* the cache.
|
|
52
|
+
*/
|
|
53
|
+
write<T>(fn: () => T): T;
|
|
54
|
+
/** Persists whatever the graph changed outside `write` — a merge folding in memory. */
|
|
54
55
|
commit(): Promise<void>;
|
|
56
|
+
/** Folds the log into the database file and closes, so a committed `.db` is complete. */
|
|
55
57
|
release(): void;
|
|
56
58
|
}
|
|
59
|
+
/** Whether a directory holds a memory anything has written to. */
|
|
60
|
+
export declare function isMemory(dir: string): boolean;
|
|
61
|
+
/**
|
|
62
|
+
* The database as of one moment, written to `file`, whatever other runs are
|
|
63
|
+
* doing to it meanwhile.
|
|
64
|
+
*/
|
|
65
|
+
export declare function snapshotMemory(from: string, file: string): void;
|
|
57
66
|
/**
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
-
*
|
|
67
|
+
* A consistent copy of a memory another run may be writing to: the database
|
|
68
|
+
* as of one moment, then the files. Files written after that moment come
|
|
69
|
+
* along as well, which costs a few unreferenced bytes and nothing else.
|
|
61
70
|
*/
|
|
62
|
-
export declare function
|
|
71
|
+
export declare function copyMemory(from: string, to: string): Promise<void>;
|
|
63
72
|
//# sourceMappingURL=store.d.ts.map
|