@zenera/neo 1.1.53 → 1.1.55
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/graph.d.ts +22 -0
- package/dist/memory/graph.js +72 -7
- package/dist/memory/grep.js +1 -0
- package/dist/memory/index.d.ts +3 -1
- package/dist/memory/index.js +95 -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/store.d.ts +36 -27
- package/dist/memory/store.js +409 -126
- package/dist/memory/tools.js +8 -0
- package/dist/runner.d.ts +20 -4
- package/dist/runner.js +71 -7
- package/dist/state.d.ts +8 -2
- package/dist/types.d.ts +41 -0
- package/dist/types.js +21 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -17,6 +17,7 @@ export * from './memory/graph.ts';
|
|
|
17
17
|
export * from './memory/grep.ts';
|
|
18
18
|
export * from './memory/index.ts';
|
|
19
19
|
export * from './memory/merge.ts';
|
|
20
|
+
export * from './memory/migrate.ts';
|
|
20
21
|
export * from './memory/page.ts';
|
|
21
22
|
export * from './memory/recall.ts';
|
|
22
23
|
export * from './memory/render.ts';
|
package/dist/index.js
CHANGED
|
@@ -17,6 +17,7 @@ export * from "./memory/graph.js";
|
|
|
17
17
|
export * from "./memory/grep.js";
|
|
18
18
|
export * from "./memory/index.js";
|
|
19
19
|
export * from "./memory/merge.js";
|
|
20
|
+
export * from "./memory/migrate.js";
|
|
20
21
|
export * from "./memory/page.js";
|
|
21
22
|
export * from "./memory/recall.js";
|
|
22
23
|
export * from "./memory/render.js";
|
package/dist/kernel.d.ts
CHANGED
|
@@ -8,7 +8,7 @@ import type { Services } from './services.ts';
|
|
|
8
8
|
import { type Skill } from './skills.ts';
|
|
9
9
|
import type { AgentState } from './state.ts';
|
|
10
10
|
import { type CompactionNode, type FinalOutputNode, type UserInputNode } from './trajectory.ts';
|
|
11
|
-
import { type AnyTool, type Input, type TokenUsage, type ToolOutcome } from './types.ts';
|
|
11
|
+
import { type AnyTool, type Input, type SuspendSpec, type TokenUsage, type ToolOutcome } from './types.ts';
|
|
12
12
|
/**
|
|
13
13
|
* The kernel is a module of plain functions: it holds no state and no
|
|
14
14
|
* configuration, so there is nothing to instantiate and nothing hidden. All
|
|
@@ -129,6 +129,16 @@ export declare function applyLlmResponse<TCtx>(state: AgentState, res: ModelResp
|
|
|
129
129
|
* and performs a hand-off when the call was a `transfer_to_*`.
|
|
130
130
|
*/
|
|
131
131
|
export declare function applyToolResult(state: AgentState, callId: string, outcome: ToolOutcome, env: KernelEnv): Promise<AgentState>;
|
|
132
|
+
/**
|
|
133
|
+
* Records that a pending call suspended the run. The call stays pending - no
|
|
134
|
+
* result is written - so the model still sees it unanswered until it is.
|
|
135
|
+
*/
|
|
136
|
+
export declare function applySuspension(state: AgentState, call: {
|
|
137
|
+
callId: string;
|
|
138
|
+
name: string;
|
|
139
|
+
}, spec: SuspendSpec, at: string): AgentState;
|
|
140
|
+
/** Lets a suspended call run again: what it waited for is done. */
|
|
141
|
+
export declare function releaseSuspension(state: AgentState, callId: string): AgentState;
|
|
132
142
|
/** Records a memory recall or mutation performed by the driver. */
|
|
133
143
|
export declare function applyMemoryEffect(state: AgentState, effect: MemoryRecallSpec | MemoryOpSpec, env: KernelEnv): Promise<AgentState>;
|
|
134
144
|
/** Records a skill activation and the tools it unlocked. */
|
package/dist/kernel.js
CHANGED
|
@@ -650,7 +650,30 @@ export async function applyToolResult(state, callId, outcome, env) {
|
|
|
650
650
|
: state.pendingFork?.branches.length
|
|
651
651
|
? 'awaiting_branches'
|
|
652
652
|
: 'awaiting_llm';
|
|
653
|
-
|
|
653
|
+
const suspended = state.suspended?.filter((s) => s.callId !== callId);
|
|
654
|
+
return b.commit({
|
|
655
|
+
phase,
|
|
656
|
+
pendingToolCalls,
|
|
657
|
+
agentName,
|
|
658
|
+
suspended: suspended?.length ? suspended : undefined,
|
|
659
|
+
});
|
|
660
|
+
}
|
|
661
|
+
/**
|
|
662
|
+
* Records that a pending call suspended the run. The call stays pending - no
|
|
663
|
+
* result is written - so the model still sees it unanswered until it is.
|
|
664
|
+
*/
|
|
665
|
+
export function applySuspension(state, call, spec, at) {
|
|
666
|
+
if (!state.pendingToolCalls.includes(call.callId)) {
|
|
667
|
+
throw new Error(`no pending tool call ${call.callId} in run ${state.runId}`);
|
|
668
|
+
}
|
|
669
|
+
const others = (state.suspended ?? []).filter((s) => s.callId !== call.callId);
|
|
670
|
+
const entry = { callId: call.callId, name: call.name, at, ...spec };
|
|
671
|
+
return { ...state, suspended: [...others, entry] };
|
|
672
|
+
}
|
|
673
|
+
/** Lets a suspended call run again: what it waited for is done. */
|
|
674
|
+
export function releaseSuspension(state, callId) {
|
|
675
|
+
const suspended = (state.suspended ?? []).filter((s) => s.callId !== callId);
|
|
676
|
+
return { ...state, suspended: suspended.length ? suspended : undefined };
|
|
654
677
|
}
|
|
655
678
|
function asReason(call) {
|
|
656
679
|
// The reason travels in the call arguments, which are behind a payload; the
|
package/dist/memory/graph.d.ts
CHANGED
|
@@ -20,6 +20,14 @@ export interface NodePatch {
|
|
|
20
20
|
}
|
|
21
21
|
/** Deterministic, so the same link committed twice stays one edge. */
|
|
22
22
|
export declare const edgeKey: (source: string, target: string, relation: Relation) => string;
|
|
23
|
+
/** What changed since the last `settle`, for a store that persists only that. */
|
|
24
|
+
export interface GraphChanges {
|
|
25
|
+
/** added or changed, and still present */
|
|
26
|
+
nodes: readonly string[];
|
|
27
|
+
dropped: readonly string[];
|
|
28
|
+
/** edge keys added or re-stamped; a dropped node takes its edges with it */
|
|
29
|
+
edges: readonly string[];
|
|
30
|
+
}
|
|
23
31
|
export declare class MemoryGraph {
|
|
24
32
|
#private;
|
|
25
33
|
static from(serialized: unknown): MemoryGraph;
|
|
@@ -28,6 +36,20 @@ export declare class MemoryGraph {
|
|
|
28
36
|
get size(): number;
|
|
29
37
|
has(id: string): boolean;
|
|
30
38
|
get(id: string): MemoryNode | undefined;
|
|
39
|
+
/** One edge by key, with its attributes. */
|
|
40
|
+
edge(key: string): {
|
|
41
|
+
source: string;
|
|
42
|
+
target: string;
|
|
43
|
+
attrs: MemoryEdgeAttrs;
|
|
44
|
+
} | undefined;
|
|
45
|
+
get dirty(): boolean;
|
|
46
|
+
/** Changed here and not yet persisted, so a reload must not overwrite it. */
|
|
47
|
+
isDirty(id: string): boolean;
|
|
48
|
+
changes(): GraphChanges;
|
|
49
|
+
/** The changes reached disk. */
|
|
50
|
+
settle(): void;
|
|
51
|
+
/** Mutations that mirror disk rather than change it: a store applying what another wrote. */
|
|
52
|
+
untracked<T>(fn: () => T): T;
|
|
31
53
|
add(draft: NodeDraft, at: string): MemoryNode;
|
|
32
54
|
/**
|
|
33
55
|
* Put a node in exactly as it stands — id, timestamps, revision and counters
|
package/dist/memory/graph.js
CHANGED
|
@@ -4,6 +4,10 @@ import { edgeVisible, visible, } from "./types.js";
|
|
|
4
4
|
export const edgeKey = (source, target, relation) => `${source}|${relation}|${target}`;
|
|
5
5
|
export class MemoryGraph {
|
|
6
6
|
#graph = new MultiDirectedGraph();
|
|
7
|
+
#tracking = true;
|
|
8
|
+
#changed = new Set();
|
|
9
|
+
#dropped = new Set();
|
|
10
|
+
#linked = new Set();
|
|
7
11
|
static from(serialized) {
|
|
8
12
|
const g = new MemoryGraph();
|
|
9
13
|
g.#graph.import(serialized);
|
|
@@ -24,6 +28,59 @@ export class MemoryGraph {
|
|
|
24
28
|
get(id) {
|
|
25
29
|
return this.#graph.hasNode(id) ? this.#graph.getNodeAttributes(id) : undefined;
|
|
26
30
|
}
|
|
31
|
+
/** One edge by key, with its attributes. */
|
|
32
|
+
edge(key) {
|
|
33
|
+
if (!this.#graph.hasEdge(key)) {
|
|
34
|
+
return undefined;
|
|
35
|
+
}
|
|
36
|
+
return {
|
|
37
|
+
source: this.#graph.source(key),
|
|
38
|
+
target: this.#graph.target(key),
|
|
39
|
+
attrs: this.#graph.getEdgeAttributes(key),
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
get dirty() {
|
|
43
|
+
return this.#changed.size + this.#dropped.size + this.#linked.size > 0;
|
|
44
|
+
}
|
|
45
|
+
/** Changed here and not yet persisted, so a reload must not overwrite it. */
|
|
46
|
+
isDirty(id) {
|
|
47
|
+
return this.#changed.has(id) || this.#dropped.has(id);
|
|
48
|
+
}
|
|
49
|
+
changes() {
|
|
50
|
+
return {
|
|
51
|
+
nodes: [...this.#changed],
|
|
52
|
+
dropped: [...this.#dropped],
|
|
53
|
+
edges: [...this.#linked],
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
/** The changes reached disk. */
|
|
57
|
+
settle() {
|
|
58
|
+
this.#changed.clear();
|
|
59
|
+
this.#dropped.clear();
|
|
60
|
+
this.#linked.clear();
|
|
61
|
+
}
|
|
62
|
+
/** Mutations that mirror disk rather than change it: a store applying what another wrote. */
|
|
63
|
+
untracked(fn) {
|
|
64
|
+
const was = this.#tracking;
|
|
65
|
+
this.#tracking = false;
|
|
66
|
+
try {
|
|
67
|
+
return fn();
|
|
68
|
+
}
|
|
69
|
+
finally {
|
|
70
|
+
this.#tracking = was;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
#node(id) {
|
|
74
|
+
if (this.#tracking) {
|
|
75
|
+
this.#changed.add(id);
|
|
76
|
+
this.#dropped.delete(id);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
#edge(key) {
|
|
80
|
+
if (this.#tracking) {
|
|
81
|
+
this.#linked.add(key);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
27
84
|
add(draft, at) {
|
|
28
85
|
const node = {
|
|
29
86
|
id: draft.id,
|
|
@@ -39,6 +96,7 @@ export class MemoryGraph {
|
|
|
39
96
|
...(draft.file ? { file: draft.file } : {}),
|
|
40
97
|
};
|
|
41
98
|
this.#graph.addNode(node.id, node);
|
|
99
|
+
this.#node(node.id);
|
|
42
100
|
return node;
|
|
43
101
|
}
|
|
44
102
|
/**
|
|
@@ -54,6 +112,7 @@ export class MemoryGraph {
|
|
|
54
112
|
else {
|
|
55
113
|
this.#graph.addNode(node.id, next);
|
|
56
114
|
}
|
|
115
|
+
this.#node(node.id);
|
|
57
116
|
}
|
|
58
117
|
update(id, patch, at) {
|
|
59
118
|
const current = this.#graph.getNodeAttributes(id);
|
|
@@ -71,6 +130,7 @@ export class MemoryGraph {
|
|
|
71
130
|
revision: current.revision + 1,
|
|
72
131
|
};
|
|
73
132
|
this.#graph.replaceNodeAttributes(id, next);
|
|
133
|
+
this.#node(id);
|
|
74
134
|
return next;
|
|
75
135
|
}
|
|
76
136
|
/** Reading is what counts as use — see `memory_load`, not `memory_search`. */
|
|
@@ -81,24 +141,29 @@ export class MemoryGraph {
|
|
|
81
141
|
const node = this.#graph.getNodeAttributes(id);
|
|
82
142
|
const next = { ...node, lastUsedAt: at, useCount: node.useCount + 1 };
|
|
83
143
|
this.#graph.replaceNodeAttributes(id, next);
|
|
144
|
+
this.#node(id);
|
|
84
145
|
return next;
|
|
85
146
|
}
|
|
86
147
|
link(source, target, relation, at) {
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
});
|
|
148
|
+
const key = edgeKey(source, target, relation);
|
|
149
|
+
this.#graph.mergeEdgeWithKey(key, source, target, { relation, createdAt: at });
|
|
150
|
+
this.#edge(key);
|
|
91
151
|
}
|
|
92
152
|
/** `link` for an edge that already exists elsewhere: it keeps the stamp it had. */
|
|
93
153
|
adoptEdge(source, target, attrs) {
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
154
|
+
const key = edgeKey(source, target, attrs.relation);
|
|
155
|
+
this.#graph.mergeEdgeWithKey(key, source, target, { ...attrs });
|
|
156
|
+
this.#edge(key);
|
|
97
157
|
}
|
|
98
158
|
/** Drops the node and every edge touching it; the file it names is the store's to unlink. */
|
|
99
159
|
forget(id) {
|
|
100
160
|
const node = this.get(id);
|
|
101
161
|
if (node) {
|
|
162
|
+
if (this.#tracking) {
|
|
163
|
+
this.#graph.forEachEdge(id, (key) => this.#linked.delete(key));
|
|
164
|
+
this.#changed.delete(id);
|
|
165
|
+
this.#dropped.add(id);
|
|
166
|
+
}
|
|
102
167
|
this.#graph.dropNode(id);
|
|
103
168
|
}
|
|
104
169
|
return node;
|
package/dist/memory/grep.js
CHANGED
|
@@ -44,6 +44,7 @@ export async function grepMemory(store, pattern, filter = {}) {
|
|
|
44
44
|
const kinds = filter.kinds?.length ? new Set(filter.kinds) : undefined;
|
|
45
45
|
const cap = filter.hitsPerNode ?? HITS_PER_NODE;
|
|
46
46
|
const want = filter.stale ?? 'exclude';
|
|
47
|
+
store.refresh();
|
|
47
48
|
const superseded = store.graph.superseded(filter.sees);
|
|
48
49
|
const matches = [];
|
|
49
50
|
const skipped = [];
|
package/dist/memory/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { Embedder } from '../embedding.ts';
|
|
2
2
|
import type { IdClock } from '../ids.ts';
|
|
3
|
-
import type
|
|
3
|
+
import { type MemoryGraph } from './graph.ts';
|
|
4
4
|
import type { MemoryStore } from './store.ts';
|
|
5
5
|
import { type MemoryNode, type MemoryQuery, type Recollection } from './types.ts';
|
|
6
6
|
/** Past this a `load` returns the path and lets the agent open it itself. */
|
|
@@ -73,6 +73,8 @@ export declare class MemoryIndex {
|
|
|
73
73
|
get graph(): MemoryGraph;
|
|
74
74
|
get kinds(): readonly string[];
|
|
75
75
|
get relations(): readonly string[];
|
|
76
|
+
/** Catches up with what other runs committed to the same memory. */
|
|
77
|
+
refresh(): void;
|
|
76
78
|
/**
|
|
77
79
|
* Read-only: a search returns nodes the model has not read, so nothing is
|
|
78
80
|
* touched. `stale` is the audit view — it puts back the superseded nodes
|
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,20 +101,30 @@ 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
|
}
|
|
109
130
|
async commit(tx, opts) {
|
|
@@ -112,13 +133,14 @@ export class MemoryIndex {
|
|
|
112
133
|
if (!nodes.length && !edges.length) {
|
|
113
134
|
throw new MemoryError('a commit needs at least one node or edge');
|
|
114
135
|
}
|
|
136
|
+
this.refresh();
|
|
115
137
|
const plan = this.#plan(nodes, edges, opts);
|
|
116
138
|
const vectors = await this.#embed(plan.embed.map((e) => e.text), 'document');
|
|
117
139
|
// Keyed before anything is dropped: `#merge` reshapes `plan.embed`, and
|
|
118
140
|
// the vectors only line up with it positionally until it does.
|
|
119
141
|
const vector = new Map(plan.embed.map((e, i) => [e.id, vectors?.[i]]));
|
|
120
|
-
const merged = this.#merge(plan, vector, opts.sees);
|
|
121
142
|
// Files first, so a failed copy can be undone before the graph knows.
|
|
143
|
+
// A file-bearing node never folds onto a twin, so none is copied in vain.
|
|
122
144
|
const written = [];
|
|
123
145
|
try {
|
|
124
146
|
for (const item of plan.files) {
|
|
@@ -136,26 +158,36 @@ export class MemoryIndex {
|
|
|
136
158
|
const at = opts.clock.now();
|
|
137
159
|
let created = 0;
|
|
138
160
|
let updated = 0;
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
161
|
+
let merged = 0;
|
|
162
|
+
try {
|
|
163
|
+
this.store.write(() => {
|
|
164
|
+
this.#recheck(plan);
|
|
165
|
+
merged = this.#merge(plan, vector, opts.sees);
|
|
166
|
+
for (const item of plan.nodes) {
|
|
167
|
+
if (item.existing) {
|
|
168
|
+
this.graph.update(item.id, item.patch, at);
|
|
169
|
+
updated++;
|
|
170
|
+
}
|
|
171
|
+
else {
|
|
172
|
+
this.graph.add(item.draft, at);
|
|
173
|
+
created++;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
for (const edge of plan.edges) {
|
|
177
|
+
this.graph.link(edge.source, edge.target, edge.relation, at);
|
|
178
|
+
}
|
|
179
|
+
for (const e of plan.embed) {
|
|
180
|
+
const v = vector.get(e.id);
|
|
181
|
+
if (v) {
|
|
182
|
+
this.store.vectors?.set(e.id, v);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
});
|
|
151
186
|
}
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
this.store.vectors?.set(e.id, v);
|
|
156
|
-
}
|
|
187
|
+
catch (err) {
|
|
188
|
+
await Promise.all(written.map((f) => forgetFile(this.store.dir, f)));
|
|
189
|
+
throw err;
|
|
157
190
|
}
|
|
158
|
-
await this.store.commit();
|
|
159
191
|
return {
|
|
160
192
|
ids: plan.ids,
|
|
161
193
|
created,
|
|
@@ -165,6 +197,34 @@ export class MemoryIndex {
|
|
|
165
197
|
merged,
|
|
166
198
|
};
|
|
167
199
|
}
|
|
200
|
+
/**
|
|
201
|
+
* What the plan assumed about nodes it did not create, asked again of the
|
|
202
|
+
* graph as it is now: another run may have forgotten or revised them since.
|
|
203
|
+
* Before any mutation, so a refusal changes nothing.
|
|
204
|
+
*/
|
|
205
|
+
#recheck(plan) {
|
|
206
|
+
const minted = new Set(plan.nodes.filter((n) => !n.existing).map((n) => n.id));
|
|
207
|
+
for (const item of plan.nodes) {
|
|
208
|
+
if (!item.existing) {
|
|
209
|
+
continue;
|
|
210
|
+
}
|
|
211
|
+
const current = this.graph.get(item.id);
|
|
212
|
+
if (!current) {
|
|
213
|
+
throw new MemoryError(`${item.id}: no such memory`, 'another run forgot it while this one was committing');
|
|
214
|
+
}
|
|
215
|
+
const expected = item.patch.expectedRevision;
|
|
216
|
+
if (expected !== undefined && expected !== current.revision) {
|
|
217
|
+
throw new RevisionMismatch(item.id, expected, current.revision);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
for (const edge of plan.edges) {
|
|
221
|
+
for (const end of [edge.source, edge.target]) {
|
|
222
|
+
if (!minted.has(end) && !this.graph.has(end)) {
|
|
223
|
+
throw new MemoryError(`"${end}" is no longer a memory`, 'another run forgot it while this one was committing');
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}
|
|
168
228
|
/**
|
|
169
229
|
* A second run that learns the same thing should join the graph, not fork
|
|
170
230
|
* it. Nothing tells an agent the id of a node it is about to write, so two
|
|
@@ -225,6 +285,7 @@ export class MemoryIndex {
|
|
|
225
285
|
return twins.size;
|
|
226
286
|
}
|
|
227
287
|
async forget(ids, sees) {
|
|
288
|
+
this.refresh();
|
|
228
289
|
const doomed = ids.map((id) => {
|
|
229
290
|
const node = this.graph.get(id);
|
|
230
291
|
if (!node || !visible(node, sees)) {
|
|
@@ -232,14 +293,17 @@ export class MemoryIndex {
|
|
|
232
293
|
}
|
|
233
294
|
return node;
|
|
234
295
|
});
|
|
235
|
-
for
|
|
236
|
-
|
|
296
|
+
// Gone already is what was asked for, so another run beating this one to it is fine.
|
|
297
|
+
const removed = this.store.write(() => doomed.filter((node) => {
|
|
237
298
|
this.store.vectors?.remove(node.id);
|
|
299
|
+
return this.graph.forget(node.id) !== undefined;
|
|
300
|
+
}));
|
|
301
|
+
// Bytes after the graph: a node naming a missing file is worse than a stray file.
|
|
302
|
+
for (const node of removed) {
|
|
238
303
|
if (node.file) {
|
|
239
304
|
await forgetFile(this.store.dir, node.file);
|
|
240
305
|
}
|
|
241
306
|
}
|
|
242
|
-
await this.store.commit();
|
|
243
307
|
return doomed;
|
|
244
308
|
}
|
|
245
309
|
/**
|
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
|