aboutsummaryrefslogtreecommitdiff
path: root/modules/test/test-memory-graph-recovery.js
diff options
context:
space:
mode:
authorAgando <roduccomm@gmail.com>2026-10-27 14:59:06 +0100
committerAgando <roduccomm@gmail.com>2026-10-27 14:59:06 +0100
commit6f2255946a834f2458613450533cd2906eb31e7d (patch)
tree85677c588783e030606c4f82cc99b6d2ce138d75 /modules/test/test-memory-graph-recovery.js
parentd2ec6fe99cc6f79013e689732fdc7f77089a8ed7 (diff)
fix(newos4): bound one turn's tool context, honest skill-create, quarantine a corrupt memory graph
The three items the last review and probes left open, each with a test that is red on HEAD. - MAINT-023 (module-aiassist.js): one agentic turn grew without limit — every role:'tool' body was pushed verbatim and only this.context was trimmed BETWEEN turns (measured 6 -> 112 705 tokens across 8 model calls against maxContextTokens 12 000). Observations are now clipped to maxToolResultChars (6 000) with a note saying how much was withheld and to re-read with offset/limit, and _boundToolContext() elides the oldest tool bodies once the running total passes toolContextBudgetChars (24 000), always keeping the newest 4 verbatim. The ceiling is turn-independent: the same ~26 k chars at 8 turns and at 20. - MAINT-024 (modules/ext/skill-pack-core.js): the command printed "✅ Skill created" with fs.writeFile()'s boolean discarded (probe: fresh VFS, success printed, no file), stripped invalid characters instead of rejecting the name (My-Skill -> y-kill, DevOps_Thing -> evpshing), and treated an unreadable /path argument as the skill body. Now: kebab-case is validated (uppercase normalises and the final name is reported; anything else is refused), /skills is created on first use, the write and index results are checked, and an unreadable body path is an error. - MAINT-003 (module-memory-graph.js): a corrupt store looked exactly like a fresh install — fsReadJSON returns null on a parse failure, so 85 nodes could vanish with no warning and no copy. The probe confirmed the loss: seed 6 nodes, truncate nodes.json and export.json, reload -> 1 node, no message, nothing in .trash. _quarantineCorrupt() now keeps the unreadable file as <name>.corrupt, warns, and then loads from whatever remains. Tests: test-tool-context-bound.js (11), test-skill-create.js (16), test-memory-graph-recovery.js (8). RED on HEAD: tool-context crashes on _clipToolResult (exit 1), skill-create 16 assertions fail on the missing file/index, memory-graph B1/B2/C1 fail (no warning, no .corrupt copy). Suite now 13/13 files green; maintain --fix reports "node tests 13/13 pass". Live on v4.6.107: skill-create My-Skill -> ✅ "my-skill"; DevOps_Thing -> Invalid skill name; /does/not/exist.md -> Cannot read skill body; only my-skill in the index.
Diffstat (limited to 'modules/test/test-memory-graph-recovery.js')
-rw-r--r--modules/test/test-memory-graph-recovery.js90
1 files changed, 90 insertions, 0 deletions
diff --git a/modules/test/test-memory-graph-recovery.js b/modules/test/test-memory-graph-recovery.js
new file mode 100644
index 0000000..533a9e2
--- /dev/null
+++ b/modules/test/test-memory-graph-recovery.js
@@ -0,0 +1,90 @@
+// modules/test/test-memory-graph-recovery.js — run: node
+// MAINT-003: a corrupt memory-graph store must not look like a fresh install. The graph still
+// starts from what remains, but the unreadable file is kept aside and the user is told.
+const fsMod = require('fs'), path = require('path');
+const ROOT = path.join(__dirname, '..', 'core');
+const store = {};
+global.localStorage = { getItem: k => (k in store ? store[k] : null), setItem: (k, v) => { store[k] = String(v); }, removeItem: k => { delete store[k]; } };
+global.indexedDB = { open() { throw new Error('no idb'); }, deleteDatabase() { throw new Error('no idb'); } };
+global.window = global;
+
+const load = (f, c) => new Function(fsMod.readFileSync(path.join(ROOT, f), 'utf8') + '\nreturn ' + c + ';')();
+const VFS = load('virtual-fs.js', 'VirtualFileSystem');
+const MG = load('module-memory-graph.js', 'MemoryGraphModule');
+
+let fails = 0, passes = 0;
+function ok(name, cond, detail) {
+ if (cond) { passes++; return; }
+ fails++;
+ console.log('FAIL ' + name + (detail ? ' — ' + detail : ''));
+}
+
+function mk() {
+ const out = [];
+ const vfs = new VFS();
+ vfs.saveToStorage = () => true;
+ const term = { print: (m, t) => out.push({ text: String(m), type: t || '' }), log: () => {}, fs: vfs };
+ const mg = new MG();
+ mg.init(term);
+ return { mg, vfs, out };
+}
+const text = out => out.map(o => o.text).join(' | ');
+
+// A. a healthy store survives a reload
+{
+ const a = mk();
+ for (let i = 0; i < 4; i++) a.mg.addNode('concept', 'keep-' + i, 'keep ' + i);
+ a.mg._saveNow();
+ const seeded = Object.keys(a.mg._nodes).length;
+
+ const out = [];
+ const mg2 = new MG();
+ mg2.init({ print: (m, t) => out.push({ text: String(m), type: t || '' }), log: () => {}, fs: a.vfs });
+ ok('A1 nodes survive a reload', Object.keys(mg2._nodes).length === seeded, 'seeded ' + seeded + ' reloaded ' + Object.keys(mg2._nodes).length);
+ ok('A2 no quarantine warning on a healthy store', !/unreadable/.test(text(out)), text(out));
+}
+
+// B. a corrupt store is quarantined and reported, not silently discarded
+{
+ const a = mk();
+ for (let i = 0; i < 4; i++) a.mg.addNode('concept', 'lose-' + i, 'lose ' + i);
+ a.mg._saveNow();
+ const original = a.vfs.readFile('/.memory_graph/nodes.json');
+ a.vfs.writeFile('/.memory_graph/nodes.json', '{"nodes":['); // truncated write
+ a.vfs.writeFile('/.memory_graph/export.json', 'not json at all');
+
+ const out = [];
+ const mg2 = new MG();
+ mg2.init({ print: (m, t) => out.push({ text: String(m), type: t || '' }), log: () => {}, fs: a.vfs });
+
+ ok('B1 the user is warned', /unreadable/.test(text(out)), text(out).slice(0, 200));
+ ok('B2 the corrupt file is kept aside', a.vfs.readFile('/.memory_graph/nodes.json.corrupt') === '{"nodes":[',
+ String(a.vfs.readFile('/.memory_graph/nodes.json.corrupt')));
+ ok('B3 the quarantine copy held the broken text, not the old good one', original !== a.vfs.readFile('/.memory_graph/nodes.json.corrupt'));
+ ok('B4 the graph still starts', Object.keys(mg2._nodes).length >= 1, String(Object.keys(mg2._nodes).length));
+}
+
+// C. the export path is quarantined too, and the nodes.json fallback still loads
+{
+ const a = mk();
+ a.mg.addNode('concept', 'fallback', 'fallback node');
+ a.mg._saveNow();
+ // the loader expects {nodes: [...], edges: [...]}
+ a.vfs.fsWriteJSON('/.memory_graph/export.json', { nodes: Object.values(a.mg._nodes), edges: [] });
+ a.vfs.writeFile('/.memory_graph/nodes.json', 'broken'); // corrupt fallback
+ const before = Object.keys(a.mg._nodes).length;
+
+ const out = [];
+ const mg2 = new MG();
+ mg2.init({ print: (m, t) => out.push({ text: String(m), type: t || '' }), log: () => {}, fs: a.vfs });
+ ok('C1 corrupt nodes.json is quarantined', !!a.vfs.readFile('/.memory_graph/nodes.json.corrupt'));
+ ok('C2 the valid export still loads the graph', Object.keys(mg2._nodes).length >= before,
+ 'before ' + before + ' after ' + Object.keys(mg2._nodes).length);
+}
+
+if (fails === 0) {
+ console.log('test-memory-graph-recovery: ' + passes + ' passed, 0 failed');
+} else {
+ console.log('test-memory-graph-recovery: ' + passes + ' passed, ' + fails + ' FAILED');
+ process.exit(1);
+}