@seanhogg/builderforce-memory-mcp 2026.10.3 → 2026.10.22
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 +22 -5
- package/dist/backend.d.ts +26 -0
- package/dist/backend.d.ts.map +1 -1
- package/dist/backends/memory-store.d.ts +50 -2
- package/dist/backends/memory-store.d.ts.map +1 -1
- package/dist/backends/memory-store.js +88 -10
- package/dist/backends/memory-store.js.map +1 -1
- package/dist/bin/http.js +7 -1
- package/dist/bin/http.js.map +1 -1
- package/dist/bin/install.js +6 -3
- package/dist/bin/install.js.map +1 -1
- package/dist/bin/stdio.d.ts +9 -4
- package/dist/bin/stdio.d.ts.map +1 -1
- package/dist/bin/stdio.js +20 -5
- package/dist/bin/stdio.js.map +1 -1
- package/dist/compaction-tools.d.ts +15 -0
- package/dist/compaction-tools.d.ts.map +1 -0
- package/dist/compaction-tools.js +192 -0
- package/dist/compaction-tools.js.map +1 -0
- package/dist/compaction.d.ts +114 -8
- package/dist/compaction.d.ts.map +1 -1
- package/dist/compaction.js +161 -7
- package/dist/compaction.js.map +1 -1
- package/dist/index.d.ts +13 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +11 -7
- package/dist/index.js.map +1 -1
- package/dist/install/claude-hooks.d.ts +5 -0
- package/dist/install/claude-hooks.d.ts.map +1 -1
- package/dist/install/claude-hooks.js +69 -9
- package/dist/install/claude-hooks.js.map +1 -1
- package/dist/install/server-spec.d.ts +33 -7
- package/dist/install/server-spec.d.ts.map +1 -1
- package/dist/install/server-spec.js +59 -9
- package/dist/install/server-spec.js.map +1 -1
- package/dist/model/recall-probe.d.ts +29 -0
- package/dist/model/recall-probe.d.ts.map +1 -0
- package/dist/model/recall-probe.js +72 -0
- package/dist/model/recall-probe.js.map +1 -0
- package/dist/persistence/fact-archive.d.ts +47 -0
- package/dist/persistence/fact-archive.d.ts.map +1 -0
- package/dist/persistence/fact-archive.js +85 -0
- package/dist/persistence/fact-archive.js.map +1 -0
- package/dist/persistence/shared-json-file.d.ts +24 -1
- package/dist/persistence/shared-json-file.d.ts.map +1 -1
- package/dist/persistence/shared-json-file.js +46 -1
- package/dist/persistence/shared-json-file.js.map +1 -1
- package/dist/restore.d.ts +56 -0
- package/dist/restore.d.ts.map +1 -0
- package/dist/restore.js +118 -0
- package/dist/restore.js.map +1 -0
- package/dist/tools.d.ts +13 -0
- package/dist/tools.d.ts.map +1 -1
- package/dist/tools.js +11 -90
- package/dist/tools.js.map +1 -1
- package/package.json +6 -6
- package/server.json +2 -2
- package/src/backend.ts +29 -0
- package/src/backends/memory-store.ts +141 -11
- package/src/bin/http.ts +7 -1
- package/src/bin/install.ts +7 -3
- package/src/bin/stdio.ts +20 -5
- package/src/compaction-tools.ts +206 -0
- package/src/compaction.ts +228 -9
- package/src/index.ts +55 -10
- package/src/install/claude-hooks.ts +72 -9
- package/src/install/server-spec.ts +74 -8
- package/src/model/recall-probe.ts +81 -0
- package/src/persistence/fact-archive.ts +101 -0
- package/src/persistence/shared-json-file.ts +54 -1
- package/src/restore.ts +144 -0
- package/src/tools.ts +24 -93
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* FactArchive — where a compacted fact's full original lives once its body is a stub.
|
|
3
|
+
*
|
|
4
|
+
* The archive is deliberately NOT a field on the stored entry: the store snapshot is
|
|
5
|
+
* rebuilt from the store on every write, rewritten in place by other processes, and
|
|
6
|
+
* capped/evicted at {@link DEFAULT_MAX_ENTRIES}; an archived original riding inside
|
|
7
|
+
* it would be lost to any one of those. A separate sidecar (`memory.archive.json`,
|
|
8
|
+
* see `defaultArchiveFile`) is only ever merged into, never pruned, so an evicted or
|
|
9
|
+
* forgotten stub still has its original.
|
|
10
|
+
*
|
|
11
|
+
* Two implementations, one contract: in-memory (a backend with no disk mirror) and
|
|
12
|
+
* file-backed (shared across processes through {@link SharedJsonFile}, with its
|
|
13
|
+
* rotating backups and freshness check).
|
|
14
|
+
*/
|
|
15
|
+
import { mergeArchive, parseArchive } from "../compaction.js";
|
|
16
|
+
import { SharedJsonFile } from "./shared-json-file.js";
|
|
17
|
+
function isJson(text) {
|
|
18
|
+
try {
|
|
19
|
+
JSON.parse(text);
|
|
20
|
+
return true;
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
/** Process-local archive — the default for a backend with no disk mirror. */
|
|
27
|
+
export class MemoryFactArchive {
|
|
28
|
+
rows = Object.create(null);
|
|
29
|
+
get(key) {
|
|
30
|
+
return this.rows[key];
|
|
31
|
+
}
|
|
32
|
+
all() {
|
|
33
|
+
return this.rows;
|
|
34
|
+
}
|
|
35
|
+
putMany(facts) {
|
|
36
|
+
for (const f of facts)
|
|
37
|
+
this.rows[f.key] = f;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Archive mirrored to a JSON file several processes share. Re-reads the file when it
|
|
42
|
+
* changed underneath this process; every write merges into the latest on-disk text,
|
|
43
|
+
* so two writers add to one archive rather than overwriting each other's rows.
|
|
44
|
+
*/
|
|
45
|
+
export class FileFactArchive {
|
|
46
|
+
rows = {};
|
|
47
|
+
text = null;
|
|
48
|
+
file;
|
|
49
|
+
constructor(path, fs) {
|
|
50
|
+
this.file = new SharedJsonFile(path, fs);
|
|
51
|
+
this.refresh();
|
|
52
|
+
}
|
|
53
|
+
get(key) {
|
|
54
|
+
this.refresh();
|
|
55
|
+
return this.rows[key];
|
|
56
|
+
}
|
|
57
|
+
all() {
|
|
58
|
+
this.refresh();
|
|
59
|
+
return this.rows;
|
|
60
|
+
}
|
|
61
|
+
putMany(facts) {
|
|
62
|
+
if (facts.length === 0)
|
|
63
|
+
return;
|
|
64
|
+
this.refresh();
|
|
65
|
+
// An archive that exists but will not parse is refused, never overwritten:
|
|
66
|
+
// merging into "{}" would drop every row it holds. The throw reaches the
|
|
67
|
+
// compactor BEFORE any stub is written, so nothing is compacted either.
|
|
68
|
+
if (this.text && this.text.trim() && !isJson(this.text)) {
|
|
69
|
+
throw new Error(`fact archive ${this.file.path} is unreadable; refusing to overwrite it`);
|
|
70
|
+
}
|
|
71
|
+
const next = mergeArchive(this.text, facts);
|
|
72
|
+
this.file.write(next);
|
|
73
|
+
this.text = next;
|
|
74
|
+
this.rows = parseArchive(next);
|
|
75
|
+
}
|
|
76
|
+
refresh() {
|
|
77
|
+
const changed = this.file.readIfChanged();
|
|
78
|
+
if (!changed)
|
|
79
|
+
return;
|
|
80
|
+
this.text = changed.text;
|
|
81
|
+
this.rows = parseArchive(changed.text);
|
|
82
|
+
changed.accept();
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
//# sourceMappingURL=fact-archive.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fact-archive.js","sourceRoot":"","sources":["../../src/persistence/fact-archive.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,OAAO,EAAE,YAAY,EAAE,YAAY,EAAqB,MAAM,kBAAkB,CAAC;AACjF,OAAO,EAAE,cAAc,EAAqB,MAAM,uBAAuB,CAAC;AAE1E,SAAS,MAAM,CAAC,IAAY;IACxB,IAAI,CAAC;QACD,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QACjB,OAAO,IAAI,CAAC;IAChB,CAAC;IAAC,MAAM,CAAC;QACL,OAAO,KAAK,CAAC;IACjB,CAAC;AACL,CAAC;AAWD,6EAA6E;AAC7E,MAAM,OAAO,iBAAiB;IACT,IAAI,GAAiC,MAAM,CAAC,MAAM,CAAC,IAAI,CAAiC,CAAC;IAE1G,GAAG,CAAC,GAAW;QACX,OAAO,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IAC1B,CAAC;IAED,GAAG;QACC,OAAO,IAAI,CAAC,IAAI,CAAC;IACrB,CAAC;IAED,OAAO,CAAC,KAAqB;QACzB,KAAK,MAAM,CAAC,IAAI,KAAK;YAAE,IA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O;QACX,MAAM,OAAO,GAAG,IAAI,CAAC,IAAI,CAAC,aAAa,EAAE,CAAC;QAC1C,IAAI,CAAC,OAAO;YAAE,OAAO;QACrB,IAAI,CAAC,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;QACzB,IAAI,CAAC,IAAI,GAAG,YAAY,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;QACvC,OAAO,CAAC,MAAM,EAAE,CAAC;IACrB,CAAC;CACJ"}
|
|
@@ -10,6 +10,11 @@
|
|
|
10
10
|
* The loop guard is the stamp recorded immediately AFTER each of our own writes: the
|
|
11
11
|
* next check stats the file, sees the same mtime+size, and returns without reading a
|
|
12
12
|
* byte. A stat per call is the entire steady-state cost.
|
|
13
|
+
*
|
|
14
|
+
* Every overwrite first rotates the previous file into `<file>.bak.1` (shifting
|
|
15
|
+
* `.bak.1`→`.bak.2`→`.bak.3`, the oldest dropping off), so the last
|
|
16
|
+
* {@link DEFAULT_BACKUPS} versions of a shared store are always recoverable — a
|
|
17
|
+
* bad rewrite (from any process) is never the only copy.
|
|
13
18
|
*/
|
|
14
19
|
export type SharedFileFs = {
|
|
15
20
|
readFileSync(path: string, enc: "utf8"): string;
|
|
@@ -17,6 +22,8 @@ export type SharedFileFs = {
|
|
|
17
22
|
/** When present, writes land atomically (temp file + rename). */
|
|
18
23
|
renameSync?(from: string, to: string): void;
|
|
19
24
|
unlinkSync?(path: string): void;
|
|
25
|
+
/** Used to snapshot the current file into a backup before an overwrite. */
|
|
26
|
+
copyFileSync?(from: string, to: string): void;
|
|
20
27
|
existsSync(path: string): boolean;
|
|
21
28
|
statSync(path: string): {
|
|
22
29
|
mtimeMs: number;
|
|
@@ -39,12 +46,21 @@ export interface ChangedFile {
|
|
|
39
46
|
*/
|
|
40
47
|
accept(): void;
|
|
41
48
|
}
|
|
49
|
+
/** Rotating backups kept beside a shared file by default (`.bak.1` newest … `.bak.3`). */
|
|
50
|
+
export declare const DEFAULT_BACKUPS = 3;
|
|
51
|
+
/** The path of the `n`-th backup of `file` (1 = newest). */
|
|
52
|
+
export declare function backupFile(file: string, n: number): string;
|
|
53
|
+
export interface SharedJsonFileOptions {
|
|
54
|
+
/** Rotating backups to keep before each overwrite. Default {@link DEFAULT_BACKUPS}; 0 disables. */
|
|
55
|
+
backups?: number;
|
|
56
|
+
}
|
|
42
57
|
export declare class SharedJsonFile {
|
|
43
58
|
readonly path: string;
|
|
44
59
|
private readonly fs;
|
|
45
60
|
/** How the file looked when this process last wrote or absorbed it. */
|
|
46
61
|
private stamp;
|
|
47
|
-
|
|
62
|
+
private readonly backups;
|
|
63
|
+
constructor(path: string, fs: SharedFileFs, opts?: SharedJsonFileOptions);
|
|
48
64
|
/**
|
|
49
65
|
* The file's text when it changed since we last touched it — and on first use,
|
|
50
66
|
* which IS the boot hydration (one routine, so disk and memory never diverge by
|
|
@@ -61,5 +77,12 @@ export declare class SharedJsonFile {
|
|
|
61
77
|
* file open), the write falls back to writing in place.
|
|
62
78
|
*/
|
|
63
79
|
write(text: string): void;
|
|
80
|
+
/**
|
|
81
|
+
* Shift `.bak.(n-1)` → `.bak.n` down to `.bak.1`, then copy the current file to
|
|
82
|
+
* `.bak.1`. Best effort: a backup that cannot be written never blocks the write
|
|
83
|
+
* it protects.
|
|
84
|
+
*/
|
|
85
|
+
private rotateBackups;
|
|
86
|
+
private moveFile;
|
|
64
87
|
}
|
|
65
88
|
//# sourceMappingURL=shared-json-file.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"shared-json-file.d.ts","sourceRoot":"","sources":["../../src/persistence/shared-json-file.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"shared-json-file.d.ts","sourceRoot":"","sources":["../../src/persistence/shared-json-file.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAEH,MAAM,MAAM,YAAY,GAAG;IACvB,YAAY,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,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AIG;IACH,aAAa,IAAI,WAAW,GAAG,IAAI;IAyBnC;;;;;;;;OAQG;IACH,KAAK,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI;IA2BzB;;;;OAIG;IACH,OAAO,CAAC,aAAa;IAerB,OAAO,CAAC,QAAQ;CAOnB"}
|
|
@@ -10,6 +10,11 @@
|
|
|
10
10
|
* The loop guard is the stamp recorded immediately AFTER each of our own writes: the
|
|
11
11
|
* next check stats the file, sees the same mtime+size, and returns without reading a
|
|
12
12
|
* byte. A stat per call is the entire steady-state cost.
|
|
13
|
+
*
|
|
14
|
+
* Every overwrite first rotates the previous file into `<file>.bak.1` (shifting
|
|
15
|
+
* `.bak.1`→`.bak.2`→`.bak.3`, the oldest dropping off), so the last
|
|
16
|
+
* {@link DEFAULT_BACKUPS} versions of a shared store are always recoverable — a
|
|
17
|
+
* bad rewrite (from any process) is never the only copy.
|
|
13
18
|
*/
|
|
14
19
|
/**
|
|
15
20
|
* FNV-1a over the file text. Non-cryptographic on purpose: this only has to separate
|
|
@@ -25,14 +30,22 @@ export function contentHash(text) {
|
|
|
25
30
|
}
|
|
26
31
|
return (h >>> 0).toString(16);
|
|
27
32
|
}
|
|
33
|
+
/** Rotating backups kept beside a shared file by default (`.bak.1` newest … `.bak.3`). */
|
|
34
|
+
export const DEFAULT_BACKUPS = 3;
|
|
35
|
+
/** The path of the `n`-th backup of `file` (1 = newest). */
|
|
36
|
+
export function backupFile(file, n) {
|
|
37
|
+
return `${file}.bak.${n}`;
|
|
38
|
+
}
|
|
28
39
|
export class SharedJsonFile {
|
|
29
40
|
path;
|
|
30
41
|
fs;
|
|
31
42
|
/** How the file looked when this process last wrote or absorbed it. */
|
|
32
43
|
stamp = null;
|
|
33
|
-
|
|
44
|
+
backups;
|
|
45
|
+
constructor(path, fs, opts = {}) {
|
|
34
46
|
this.path = path;
|
|
35
47
|
this.fs = fs;
|
|
48
|
+
this.backups = Math.max(0, Math.floor(opts.backups ?? DEFAULT_BACKUPS));
|
|
36
49
|
}
|
|
37
50
|
/**
|
|
38
51
|
* The file's text when it changed since we last touched it — and on first use,
|
|
@@ -77,6 +90,7 @@ export class SharedJsonFile {
|
|
|
77
90
|
* file open), the write falls back to writing in place.
|
|
78
91
|
*/
|
|
79
92
|
write(text) {
|
|
93
|
+
this.rotateBackups();
|
|
80
94
|
const rename = this.fs.renameSync?.bind(this.fs);
|
|
81
95
|
let written = false;
|
|
82
96
|
if (rename) {
|
|
@@ -106,5 +120,36 @@ export class SharedJsonFile {
|
|
|
106
120
|
this.stamp = null;
|
|
107
121
|
}
|
|
108
122
|
}
|
|
123
|
+
/**
|
|
124
|
+
* Shift `.bak.(n-1)` → `.bak.n` down to `.bak.1`, then copy the current file to
|
|
125
|
+
* `.bak.1`. Best effort: a backup that cannot be written never blocks the write
|
|
126
|
+
* it protects.
|
|
127
|
+
*/
|
|
128
|
+
rotateBackups() {
|
|
129
|
+
if (this.backups === 0 || !this.fs.existsSync(this.path))
|
|
130
|
+
return;
|
|
131
|
+
try {
|
|
132
|
+
for (let i = this.backups; i > 1; i--) {
|
|
133
|
+
const from = backupFile(this.path, i - 1);
|
|
134
|
+
if (this.fs.existsSync(from))
|
|
135
|
+
this.moveFile(from, backupFile(this.path, i));
|
|
136
|
+
}
|
|
137
|
+
const newest = backupFile(this.path, 1);
|
|
138
|
+
if (this.fs.copyFileSync)
|
|
139
|
+
this.fs.copyFileSync(this.path, newest);
|
|
140
|
+
else
|
|
141
|
+
this.fs.writeFileSync(newest, this.fs.readFileSync(this.path, "utf8"));
|
|
142
|
+
}
|
|
143
|
+
catch {
|
|
144
|
+
/* best effort */
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
moveFile(from, to) {
|
|
148
|
+
if (this.fs.renameSync) {
|
|
149
|
+
this.fs.renameSync(from, to);
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
this.fs.writeFileSync(to, this.fs.readFileSync(from, "utf8"));
|
|
153
|
+
}
|
|
109
154
|
}
|
|
110
155
|
//# sourceMappingURL=shared-json-file.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"shared-json-file.js","sourceRoot":"","sources":["../../src/persistence/shared-json-file.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"shared-json-file.js","sourceRoot":"","sources":["../../src/persistence/shared-json-file.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAsBH;;;;;GAKG;AACH,MAAM,UAAU,WAAW,CAAC,IAAY;IACpC,IAAI,CAAC,GAAG,UAAU,CAAC;IACnB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QACnC,CAAC,IAAI,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;QACxB,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC,EAAE,UAAU,CAAC,CAAC;IACjC,CAAC;IACD,OAAO,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC;AAClC,CAAC;AAYD,0FAA0F;AAC1F,MAAM,CAAC,MAAM,eAAe,GAAG,CAAC,CAAC;AAEjC,4DAA4D;AAC5D,MAAM,UAAU,UAAU,CAAC,IAAY,EAAE,CAAS;IAC9C,OAAO,GAAG,IAAI,QAAQ,CAAC,EAAE,CAAC;AAC9B,CAAC;AAOD,MAAM,OAAO,cAAc;IAMV;IACQ;IANrB,uEAAuE;IAC/D,KAAK,GAAqB,IAAI,CAAC;IACtB,OAAO,CAAS;IAEjC,YACa,IAAY,EACJ,EAAgB,EACjC,OAA8B,EAAE;QAFvB,SAAI,GAAJ,IAAI,CAAQ;QACJ,OAAE,GAAF,EAAE,CAAc;QAGjC,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,OAAO,IAAI,eAAe,CAAC,CAAC,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,CAAC,EAAE,CAAC,UAAU,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;YAC7B,OAAO;QACX,CAAC;QACD,IAAI,CAAC,EAAE,CAAC,aAAa,CAAC,EAAE,EAAE,IAAI,CAAC,EAAE,CAAC,YAAY,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC,CAAC;IAClE,CAAC;CACJ"}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Restore — bringing the full text of a compaction stub back from an export.
|
|
3
|
+
*
|
|
4
|
+
* A stub written before compaction archived its originals is the ONLY copy left:
|
|
5
|
+
* the text is gone (`contentLost`). The text often still exists elsewhere — a
|
|
6
|
+
* memory export (`[{ key, content }]`), or the Evermind coordinator's `mem:` record
|
|
7
|
+
* dump (`[{ id, text }]`) — and this module decides which record restores which
|
|
8
|
+
* stub. Pure and dependency-free; the `memory_restore` tool does the I/O.
|
|
9
|
+
*
|
|
10
|
+
* Matching, in order:
|
|
11
|
+
* 1. by key — a record's `key`, or its `id` with any `mem:` prefix dropped;
|
|
12
|
+
* 2. by text — the record's first line starts with the stub's pointer line (the
|
|
13
|
+
* original first line the stub kept). Only a UNIQUE match restores; two stubs
|
|
14
|
+
* sharing a pointer are reported ambiguous rather than guessed.
|
|
15
|
+
*/
|
|
16
|
+
import { type ArchivedFact } from "./compaction.js";
|
|
17
|
+
/** One record from a restore source, normalised. */
|
|
18
|
+
export interface RestoreRecord {
|
|
19
|
+
/** The memory key the record names, when it names one. */
|
|
20
|
+
key?: string;
|
|
21
|
+
/** The full text. */
|
|
22
|
+
content: string;
|
|
23
|
+
}
|
|
24
|
+
/** A stub that may be restored. */
|
|
25
|
+
export interface RestoreTarget {
|
|
26
|
+
key: string;
|
|
27
|
+
/** The stored stub body. */
|
|
28
|
+
stub: string;
|
|
29
|
+
}
|
|
30
|
+
export interface RestoreMatch {
|
|
31
|
+
key: string;
|
|
32
|
+
content: string;
|
|
33
|
+
via: "key" | "text";
|
|
34
|
+
}
|
|
35
|
+
export interface RestorePlan {
|
|
36
|
+
matches: RestoreMatch[];
|
|
37
|
+
/** Records that matched no target. */
|
|
38
|
+
unmatched: number;
|
|
39
|
+
/** Targets more than one stub could claim by text, left alone. */
|
|
40
|
+
ambiguous: string[];
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Parse a restore source: a JSON array, an object wrapping one (`{ entries: [...] }`
|
|
44
|
+
* and similar), or JSON Lines. Rows without text, or whose text is itself a stub,
|
|
45
|
+
* are dropped — restoring a stub with a stub restores nothing.
|
|
46
|
+
*/
|
|
47
|
+
export declare function parseRestoreSource(text: string): RestoreRecord[];
|
|
48
|
+
/** Shortest pointer line a text match may rest on — below it, a prefix proves nothing. */
|
|
49
|
+
export declare const MIN_POINTER_CHARS = 12;
|
|
50
|
+
/** Decide which record restores which stub. */
|
|
51
|
+
export declare function planRestore(records: RestoreRecord[], targets: RestoreTarget[]): RestorePlan;
|
|
52
|
+
/** The Evermind version a stub names (`[absorbed→Evermind v7] …` → 7), 0 when unreadable. */
|
|
53
|
+
export declare function stubVersion(stub: string): number;
|
|
54
|
+
/** The archive row that makes a restored stub lossless again. */
|
|
55
|
+
export declare function restoredArchive(match: RestoreMatch, stub: string, now?: number): ArchivedFact;
|
|
56
|
+
//# sourceMappingURL=restore.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"restore.d.ts","sourceRoot":"","sources":["../src/restore.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EAA+C,KAAK,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAEjG,oDAAoD;AACpD,MAAM,WAAW,aAAa;IAC1B,0DAA0D;IAC1D,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,qBAAqB;IACrB,OAAO,EAAE,MAAM,CAAC;CACnB;AAED,mCAAmC;AACnC,MAAM,WAAW,aAAa;IAC1B,GAAG,EAAE,MAAM,CAAC;IACZ,4BAA4B;IAC5B,IAAI,EAAE,MAAM,CAAC;CAChB;AAED,MAAM,WAAW,YAAY;IACzB,GAAG,EAAE,MAAM,CAAC;IACZ,OAAO,EAAE,MAAM,CAAC;IAChB,GAAG,EAAE,KAAK,GAAG,MAAM,CAAC;CACvB;AAED,MAAM,WAAW,WAAW;IACxB,OAAO,EAAE,YAAY,EAAE,CAAC;IACxB,sCAAsC;IACtC,SAAS,EAAE,MAAM,CAAC;IAClB,kEAAkE;IAClE,SAAS,EAAE,MAAM,EAAE,CAAC;CACvB;AA6BD;;;;GAIG;AACH,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,MAAM,GAAG,aAAa,EAAE,CAgBhE;AAID,0FAA0F;AAC1F,eAAO,MAAM,iBAAiB,KAAK,CAAC;AAEpC,+CAA+C;AAC/C,wBAAgB,WAAW,CAAC,OAAO,EAAE,aAAa,EAAE,EAAE,OAAO,EAAE,aAAa,EAAE,GAAG,WAAW,CA0B3F;AAKD,6FAA6F;AAC7F,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAGhD;AAED,iEAAiE;AACjE,wBAAgB,eAAe,CAAC,KAAK,EAAE,YAAY,EAAE,IAAI,EAAE,MAAM,EAAE,GAAG,SAAa,GAAG,YAAY,CAEjG"}
|
package/dist/restore.js
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Restore — bringing the full text of a compaction stub back from an export.
|
|
3
|
+
*
|
|
4
|
+
* A stub written before compaction archived its originals is the ONLY copy left:
|
|
5
|
+
* the text is gone (`contentLost`). The text often still exists elsewhere — a
|
|
6
|
+
* memory export (`[{ key, content }]`), or the Evermind coordinator's `mem:` record
|
|
7
|
+
* dump (`[{ id, text }]`) — and this module decides which record restores which
|
|
8
|
+
* stub. Pure and dependency-free; the `memory_restore` tool does the I/O.
|
|
9
|
+
*
|
|
10
|
+
* Matching, in order:
|
|
11
|
+
* 1. by key — a record's `key`, or its `id` with any `mem:` prefix dropped;
|
|
12
|
+
* 2. by text — the record's first line starts with the stub's pointer line (the
|
|
13
|
+
* original first line the stub kept). Only a UNIQUE match restores; two stubs
|
|
14
|
+
* sharing a pointer are reported ambiguous rather than guessed.
|
|
15
|
+
*/
|
|
16
|
+
import { STUB_PREFIX, firstLine, isStub, stubPointer } from "./compaction.js";
|
|
17
|
+
/** Array-valued properties a wrapped export may keep its rows under. */
|
|
18
|
+
const WRAPPER_KEYS = ["entries", "memories", "records", "items", "data", "facts"];
|
|
19
|
+
function rowsOf(parsed) {
|
|
20
|
+
if (Array.isArray(parsed))
|
|
21
|
+
return parsed;
|
|
22
|
+
if (parsed && typeof parsed === "object") {
|
|
23
|
+
const obj = parsed;
|
|
24
|
+
for (const k of WRAPPER_KEYS) {
|
|
25
|
+
const v = obj[k];
|
|
26
|
+
if (Array.isArray(v))
|
|
27
|
+
return v;
|
|
28
|
+
}
|
|
29
|
+
// A lone record (a one-line JSON Lines file parses as plain JSON).
|
|
30
|
+
if (typeof obj["content"] === "string" || typeof obj["text"] === "string")
|
|
31
|
+
return [obj];
|
|
32
|
+
}
|
|
33
|
+
return [];
|
|
34
|
+
}
|
|
35
|
+
function toRecord(raw) {
|
|
36
|
+
if (!raw || typeof raw !== "object")
|
|
37
|
+
return null;
|
|
38
|
+
const r = raw;
|
|
39
|
+
const content = typeof r["content"] === "string" ? r["content"] : typeof r["text"] === "string" ? r["text"] : null;
|
|
40
|
+
if (content === null || !content.trim() || isStub(content))
|
|
41
|
+
return null;
|
|
42
|
+
const named = typeof r["key"] === "string" ? r["key"] : typeof r["id"] === "string" ? r["id"] : undefined;
|
|
43
|
+
const key = named?.replace(/^mem:/, "").trim();
|
|
44
|
+
return key ? { key, content } : { content };
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Parse a restore source: a JSON array, an object wrapping one (`{ entries: [...] }`
|
|
48
|
+
* and similar), or JSON Lines. Rows without text, or whose text is itself a stub,
|
|
49
|
+
* are dropped — restoring a stub with a stub restores nothing.
|
|
50
|
+
*/
|
|
51
|
+
export function parseRestoreSource(text) {
|
|
52
|
+
let rows;
|
|
53
|
+
try {
|
|
54
|
+
rows = rowsOf(JSON.parse(text));
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
rows = [];
|
|
58
|
+
for (const line of text.split(/\r?\n/)) {
|
|
59
|
+
if (!line.trim())
|
|
60
|
+
continue;
|
|
61
|
+
try {
|
|
62
|
+
rows.push(JSON.parse(line));
|
|
63
|
+
}
|
|
64
|
+
catch {
|
|
65
|
+
/* not a record */
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return rows.map(toRecord).filter((r) => r !== null);
|
|
70
|
+
}
|
|
71
|
+
const norm = (s) => s.toLowerCase().replace(/\s+/g, " ").trim();
|
|
72
|
+
/** Shortest pointer line a text match may rest on — below it, a prefix proves nothing. */
|
|
73
|
+
export const MIN_POINTER_CHARS = 12;
|
|
74
|
+
/** Decide which record restores which stub. */
|
|
75
|
+
export function planRestore(records, targets) {
|
|
76
|
+
const byKey = new Map(targets.map((t) => [t.key, t]));
|
|
77
|
+
const pointers = targets.map((t) => ({ key: t.key, pointer: norm(stubPointer(t.stub)) }));
|
|
78
|
+
const matched = new Map();
|
|
79
|
+
const ambiguous = new Set();
|
|
80
|
+
let unmatched = 0;
|
|
81
|
+
for (const r of records) {
|
|
82
|
+
if (r.key && byKey.has(r.key)) {
|
|
83
|
+
if (!matched.has(r.key))
|
|
84
|
+
matched.set(r.key, { key: r.key, content: r.content, via: "key" });
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
const head = norm(firstLine(r.content, 100_000));
|
|
88
|
+
const hits = pointers.filter((p) => p.pointer.length >= MIN_POINTER_CHARS && head.startsWith(p.pointer));
|
|
89
|
+
if (hits.length === 1) {
|
|
90
|
+
const key = hits[0].key;
|
|
91
|
+
if (!matched.has(key))
|
|
92
|
+
matched.set(key, { key, content: r.content, via: "text" });
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
if (hits.length > 1)
|
|
96
|
+
for (const h of hits)
|
|
97
|
+
ambiguous.add(h.key);
|
|
98
|
+
else
|
|
99
|
+
unmatched++;
|
|
100
|
+
}
|
|
101
|
+
// A key claimed unambiguously by key-match stays restored even if a text match was ambiguous.
|
|
102
|
+
const matches = [...matched.values()];
|
|
103
|
+
for (const m of matches)
|
|
104
|
+
ambiguous.delete(m.key);
|
|
105
|
+
return { matches, unmatched, ambiguous: [...ambiguous] };
|
|
106
|
+
}
|
|
107
|
+
const escapeRegExp = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
108
|
+
const STUB_VERSION = new RegExp(`^\\s*${escapeRegExp(STUB_PREFIX)} v(\\d+)\\]`);
|
|
109
|
+
/** The Evermind version a stub names (`[absorbed→Evermind v7] …` → 7), 0 when unreadable. */
|
|
110
|
+
export function stubVersion(stub) {
|
|
111
|
+
const m = STUB_VERSION.exec(stub);
|
|
112
|
+
return m ? Number(m[1]) : 0;
|
|
113
|
+
}
|
|
114
|
+
/** The archive row that makes a restored stub lossless again. */
|
|
115
|
+
export function restoredArchive(match, stub, now = Date.now()) {
|
|
116
|
+
return { key: match.key, content: match.content, stub, version: stubVersion(stub), archivedAt: now };
|
|
117
|
+
}
|
|
118
|
+
//# sourceMappingURL=restore.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"restore.js","sourceRoot":"","sources":["../src/restore.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,EAAE,WAAW,EAAqB,MAAM,iBAAiB,CAAC;AA+BjG,wEAAwE;AACxE,MAAM,YAAY,GAAG,CAAC,SAAS,EAAE,UAAU,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC;AAElF,SAAS,MAAM,CAAC,MAAe;IAC3B,IAAI,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC;QAAE,OA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IAAY,EAAE,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE;IAC/E,OAAO,EAAE,GAAG,EAAE,KAAK,CAAC,GAAG,EAAE,OAAO,EAAE,KAAK,CAAC,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,WAAW,CAAC,IAAI,CAAC,EAAE,UAAU,EAAE,GAAG,EAAE,CAAC;AACzG,CAAC"}
|
package/dist/tools.d.ts
CHANGED
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
* store back into context is more expensive than inlining it.
|
|
15
15
|
*/
|
|
16
16
|
import type { MemoryBackend } from "./backend.js";
|
|
17
|
+
import { type RecallProbe } from "./compaction.js";
|
|
17
18
|
import type { ExperienceHost } from "./experience/host.js";
|
|
18
19
|
import { type MemoryTool } from "./tool-core.js";
|
|
19
20
|
export type { MemoryTool, ToolResult } from "./tool-core.js";
|
|
@@ -37,6 +38,18 @@ export interface MemoryToolsOptions {
|
|
|
37
38
|
* beside the memory tools. See {@link createExperienceHost}.
|
|
38
39
|
*/
|
|
39
40
|
experience?: ExperienceHost | null;
|
|
41
|
+
/**
|
|
42
|
+
* The local model `memory_compact` proves absorption with before it stubs a
|
|
43
|
+
* fact (see `planLosslessCompaction`). Null/absent → compaction stubs nothing
|
|
44
|
+
* and says why. The bins pass `lazyRecallProbe()` (the configured Evermind).
|
|
45
|
+
*/
|
|
46
|
+
recallProbe?: RecallProbe | null;
|
|
47
|
+
/**
|
|
48
|
+
* Reads a restore source by path for `memory_restore`. Only hosts that may read
|
|
49
|
+
* their own disk pass it (the stdio bin); without it the tool takes inline JSON
|
|
50
|
+
* only, so a networked server never reads a caller-named file.
|
|
51
|
+
*/
|
|
52
|
+
readRestoreFile?: (path: string) => Promise<string>;
|
|
40
53
|
}
|
|
41
54
|
/**
|
|
42
55
|
* Builds the memory tool set bound to `backend`. Write tools are included only
|
package/dist/tools.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tools.d.ts","sourceRoot":"","sources":["../src/tools.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAGH,OAAO,KAAK,EAAE,aAAa,
|
|
1
|
+
{"version":3,"file":"tools.d.ts","sourceRoot":"","sources":["../src/tools.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAGH,OAAO,KAAK,EAAE,aAAa,EAAyC,MAAM,cAAc,CAAC;AACzF,OAAO,EAAsB,KAAK,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAGvE,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAE3D,OAAO,EAAkB,KAAK,UAAU,EAAmB,MAAM,gBAAgB,CAAC;AAElF,YAAY,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAE7D,MAAM,WAAW,kBAAkB;IAC/B,2EAA2E;IAC3E,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,iFAAiF;IACjF,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,oGAAoG;IACpG,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB;;;OAGG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,mFAAmF;IACnF,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB;;;;OAIG;IACH,UAAU,CAAC,EAAE,cAAc,GAAG,IAAI,CAAC;IACnC;;;;OAIG;IACH,WAAW,CAAC,EAAE,WAAW,GAAG,IAAI,CAAC;IACjC;;;;OAIG;IACH,eAAe,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,CAAC,CAAC;CACvD;AAiDD;;;GAGG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,aAAa,EAAE,IAAI,GAAE,kBAAuB,GAAG,UAAU,EAAE,CAyJpG"}
|
package/dist/tools.js
CHANGED
|
@@ -14,7 +14,8 @@
|
|
|
14
14
|
* store back into context is more expensive than inlining it.
|
|
15
15
|
*/
|
|
16
16
|
import { z } from "zod";
|
|
17
|
-
import {
|
|
17
|
+
import { CONTENT_LOST_LABEL } from "./compaction.js";
|
|
18
|
+
import { appendCompactionTools } from "./compaction-tools.js";
|
|
18
19
|
import { buildExperienceTools } from "./experience/tools.js";
|
|
19
20
|
import { appendGatewayCostTools } from "./gateway-tools.js";
|
|
20
21
|
import { clip, fail, ok } from "./tool-core.js";
|
|
@@ -32,6 +33,8 @@ const RANKER_LINE = {
|
|
|
32
33
|
embedding: "Ranked by: semantic recall (SSM embedding fused with lexical).",
|
|
33
34
|
lexical: "Ranked by: lexical recall (fallback - no embedding model loaded).",
|
|
34
35
|
};
|
|
36
|
+
/** A stub whose original is gone is surfaced WITH the label, never as a plain fact. */
|
|
37
|
+
const lostLabel = (h) => (h.contentLost ? ` ${CONTENT_LOST_LABEL}` : "");
|
|
35
38
|
function renderHits(hits, maxChars, method) {
|
|
36
39
|
const ranker = method ? `\n${RANKER_LINE[method]}` : "";
|
|
37
40
|
if (hits.length === 0)
|
|
@@ -40,7 +43,7 @@ function renderHits(hits, maxChars, method) {
|
|
|
40
43
|
.map((h) => {
|
|
41
44
|
const tags = h.tags?.length ? ` tags=[${h.tags.join(", ")}]` : "";
|
|
42
45
|
const score = h.score != null ? ` score=${h.score.toFixed(3)}` : "";
|
|
43
|
-
return `• ${h.key}${score}${tags}\n ${clip(h.content, maxChars)}`;
|
|
46
|
+
return `• ${h.key}${score}${tags}${lostLabel(h)}\n ${clip(h.content, maxChars)}`;
|
|
44
47
|
})
|
|
45
48
|
.join("\n") + ranker);
|
|
46
49
|
}
|
|
@@ -54,18 +57,6 @@ async function rankedRecall(backend, query, topK) {
|
|
|
54
57
|
return backend.recallRanked(query, topK);
|
|
55
58
|
return { hits: await backend.recall(query, topK) };
|
|
56
59
|
}
|
|
57
|
-
const SKIP_REASONS = {
|
|
58
|
-
not_found: "not found",
|
|
59
|
-
already_compacted: "already compacted",
|
|
60
|
-
not_smaller: "already shorter than its stub",
|
|
61
|
-
};
|
|
62
|
-
/** One-line compaction receipt: what shrank, what it saved, and what was left alone. */
|
|
63
|
-
function renderCompaction(compacted, bytesSaved, skipped) {
|
|
64
|
-
const lines = [`Compacted ${compacted} memory(ies), ${bytesSaved} character(s) reclaimed.`];
|
|
65
|
-
for (const s of skipped)
|
|
66
|
-
lines.push(`• skipped ${s.key} — ${SKIP_REASONS[s.reason]}`);
|
|
67
|
-
return lines.join("\n");
|
|
68
|
-
}
|
|
69
60
|
/**
|
|
70
61
|
* Builds the memory tool set bound to `backend`. Write tools are included only
|
|
71
62
|
* when `writable` is not false AND the backend actually implements them.
|
|
@@ -119,7 +110,9 @@ export function buildMemoryTools(backend, opts = {}) {
|
|
|
119
110
|
if (!key)
|
|
120
111
|
return fail("key is required.");
|
|
121
112
|
const hit = await backend.get(key);
|
|
122
|
-
return hit
|
|
113
|
+
return hit
|
|
114
|
+
? ok(`• ${hit.key}${lostLabel(hit)}\n ${hit.content}`)
|
|
115
|
+
: ok(`No memory found for key "${key}".`);
|
|
123
116
|
}
|
|
124
117
|
catch (err) {
|
|
125
118
|
return fail(`get failed: ${String(err)}`);
|
|
@@ -190,81 +183,9 @@ export function buildMemoryTools(backend, opts = {}) {
|
|
|
190
183
|
},
|
|
191
184
|
});
|
|
192
185
|
}
|
|
193
|
-
// memory_compact — the
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
// server's back instead is what races: the server still holds the full bodies and
|
|
197
|
-
// re-snapshots over them on its next write. Requires read + write, so it is
|
|
198
|
-
// registered alongside the other write tools.
|
|
199
|
-
if (writable && backend.remember) {
|
|
200
|
-
tools.push({
|
|
201
|
-
name: "memory_compact",
|
|
202
|
-
description: "Shrink memories that have already been absorbed elsewhere (folded into a model, " +
|
|
203
|
-
"summarised into a longer-lived note) down to a one-line pointer stub, reclaiming the " +
|
|
204
|
-
"context they were eating. Call this AFTER something durable has learned the facts — " +
|
|
205
|
-
"never as a way to tidy up, because the full body is gone afterwards. Already-compacted " +
|
|
206
|
-
"keys are skipped, so repeating the call is safe.",
|
|
207
|
-
inputSchema: {
|
|
208
|
-
keys: z.array(z.string()).min(1).describe("Exact keys of the memories to compact."),
|
|
209
|
-
stub: z
|
|
210
|
-
.string()
|
|
211
|
-
.optional()
|
|
212
|
-
.describe("Explicit replacement body applied to every listed key. Omit to generate " +
|
|
213
|
-
"'[absorbed→Evermind vN] <first line>' from each entry's own first line."),
|
|
214
|
-
version: z
|
|
215
|
-
.number()
|
|
216
|
-
.int()
|
|
217
|
-
.min(0)
|
|
218
|
-
.optional()
|
|
219
|
-
.describe("Version of the model that absorbed these facts; appears in the generated stub."),
|
|
220
|
-
maxChars: z
|
|
221
|
-
.number()
|
|
222
|
-
.int()
|
|
223
|
-
.min(20)
|
|
224
|
-
.max(500)
|
|
225
|
-
.optional()
|
|
226
|
-
.describe(`Max characters of the generated pointer line (default ${DEFAULT_STUB_CHARS}).`),
|
|
227
|
-
},
|
|
228
|
-
handler: async (args) => {
|
|
229
|
-
try {
|
|
230
|
-
const raw = args["keys"];
|
|
231
|
-
const keys = Array.isArray(raw) ? raw.map((k) => String(k)).filter(Boolean) : [];
|
|
232
|
-
if (keys.length === 0)
|
|
233
|
-
return fail("keys is required.");
|
|
234
|
-
// ONE read pass: the hit carries both the body the plan reasons about
|
|
235
|
-
// and the tags/importance the rewrite must preserve.
|
|
236
|
-
const hits = new Map();
|
|
237
|
-
for (const key of keys) {
|
|
238
|
-
const hit = await backend.get(key);
|
|
239
|
-
if (hit)
|
|
240
|
-
hits.set(key, hit);
|
|
241
|
-
}
|
|
242
|
-
const plan = planCompaction(keys.map((key) => ({ key, content: hits.get(key)?.content })), {
|
|
243
|
-
stub: typeof args["stub"] === "string" ? args["stub"] : undefined,
|
|
244
|
-
version: args["version"] == null ? undefined : Number(args["version"]),
|
|
245
|
-
maxChars: args["maxChars"] == null ? undefined : Number(args["maxChars"]),
|
|
246
|
-
});
|
|
247
|
-
// Preserve tags/importance — compaction shortens a body, it does not
|
|
248
|
-
// reclassify the fact.
|
|
249
|
-
const writes = plan.writes.map((w) => {
|
|
250
|
-
const prev = hits.get(w.key);
|
|
251
|
-
return { key: w.key, content: w.content, tags: prev?.tags, importance: prev?.importance };
|
|
252
|
-
});
|
|
253
|
-
if (writes.length > 0) {
|
|
254
|
-
if (backend.rememberMany)
|
|
255
|
-
await backend.rememberMany(writes);
|
|
256
|
-
else
|
|
257
|
-
for (const w of writes)
|
|
258
|
-
await backend.remember(w);
|
|
259
|
-
}
|
|
260
|
-
return ok(renderCompaction(plan.writes.length, plan.bytesSaved, plan.skipped));
|
|
261
|
-
}
|
|
262
|
-
catch (err) {
|
|
263
|
-
return fail(`compact failed: ${String(err)}`);
|
|
264
|
-
}
|
|
265
|
-
},
|
|
266
|
-
});
|
|
267
|
-
}
|
|
186
|
+
// memory_compact + memory_restore — the lossless compaction pair (compaction-tools.ts).
|
|
187
|
+
if (writable && backend.remember)
|
|
188
|
+
appendCompactionTools(tools, backend, opts);
|
|
268
189
|
if (writable && backend.forget) {
|
|
269
190
|
tools.push({
|
|
270
191
|
name: "memory_forget",
|