Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

fix(memory): cap the MEMORY.md index at the spec's 200 lines when injected into context

nirholas6c5d85aba32c
diff --git a/src/memory/index.js b/src/memory/index.js
index 1b322f0c1..2ad35daae 100644
--- a/src/memory/index.js
+++ b/src/memory/index.js
@@ -21,6 +21,11 @@ const FRONTMATTER_RE = /^---\n([\s\S]*?)\n---\n?([\s\S]*)$/;
// Built-in modes — reserved names a custom backend may not shadow.
const RESERVED_MODES = new Set(['none', 'local', 'remote', 'ipfs', 'encrypted-ipfs']);
+// Hard ceiling on MEMORY.md lines injected into the LLM context, per
+// specs/MEMORY_SPEC.md. The index bypasses the token budget by design, so this
+// is the only thing standing between a runaway index and a runaway prompt.
+const INDEX_CONTEXT_MAX_LINES = 200;
+
// Registry of custom backends: mode name -> backend definition.
// See Memory.registerBackend and specs/MEMORY_SPEC.md → "Custom backends".
const BACKENDS = new Map();
@@ -465,11 +470,29 @@ export class Memory {
return { cids, memoryCid };
}
+ /**
+ * The index as the LLM sees it. MEMORY.md is injected in full regardless of
+ * the token budget, so an index that grows without bound is the one part of
+ * the block that can crowd out everything else. specs/MEMORY_SPEC.md caps it
+ * at 200 lines; enforce that here, at injection time, so an oversized index
+ * on disk still yields a bounded prompt.
+ * @returns {string}
+ */
+ _indexForContext() {
+ const lines = this.indexText.split('\n');
+ if (lines.length <= INDEX_CONTEXT_MAX_LINES) return this.indexText;
+ const dropped = lines.length - INDEX_CONTEXT_MAX_LINES;
+ return [
+ ...lines.slice(0, INDEX_CONTEXT_MAX_LINES),
+ `_(${dropped} more index ${dropped === 1 ? 'line' : 'lines'} truncated; keep MEMORY.md under ${INDEX_CONTEXT_MAX_LINES} lines)_`,
+ ].join('\n');
+ }
+
// Budget-aware context serialization for LLM injection
contextBlock({ maxTokens = 8192 } = {}) {
// Rough token estimate: 4 chars/token
const budget = maxTokens * 4;
- const parts = ['# Agent Memory', '', this.indexText];
+ const parts = ['# Agent Memory', '', this._indexForContext()];
let used = parts.join('\n').length;
// Include top-matching file bodies until budget exhausted
for (const [file, raw] of this.files) {
diff --git a/tests/memory-modes.test.js b/tests/memory-modes.test.js
index 144a1b440..7e1e34a82 100644
--- a/tests/memory-modes.test.js
+++ b/tests/memory-modes.test.js
@@ -248,3 +248,35 @@ describe('Memory snapshot/fromSnapshot', () => {
expect(restored.indexText).toContain('user_role.md');
});
});
+
+// ── contextBlock index ceiling ───────────────────────────────────────────────
+
+describe('Memory.contextBlock: MEMORY.md index ceiling', () => {
+ it('injects a short index verbatim', async () => {
+ const mem = await Memory.load({ mode: 'local', namespace: 'agent-idx-small' });
+ mem.write('user_role', { name: 'role', description: 'who', type: 'user', body: 'hello' });
+
+ const block = mem.contextBlock({ maxTokens: 8192 });
+ expect(block).toContain('user_role.md');
+ expect(block).not.toContain('index lines truncated');
+ });
+
+ it('caps the index at 200 lines and says how many it dropped', async () => {
+ const mem = await Memory.load({ mode: 'local', namespace: 'agent-idx-big' });
+ // 260 index lines: past the 200-line ceiling specs/MEMORY_SPEC.md declares.
+ mem.indexText = Array.from({ length: 260 }, (_, i) => `- [Memo ${i}](memo_${i}.md) hook ${i}`).join('\n');
+
+ const block = mem.contextBlock({ maxTokens: 8192 });
+ expect(block).toContain('- [Memo 199](memo_199.md)');
+ expect(block).not.toContain('- [Memo 200](memo_200.md)');
+ expect(block).toContain('60 more index lines truncated');
+ });
+
+ it('does not mutate the stored index while truncating for context', async () => {
+ const mem = await Memory.load({ mode: 'local', namespace: 'agent-idx-pure' });
+ mem.indexText = Array.from({ length: 205 }, (_, i) => `- [Memo ${i}](memo_${i}.md)`).join('\n');
+
+ mem.contextBlock({ maxTokens: 8192 });
+ expect(mem.indexText.split('\n')).toHaveLength(205);
+ });
+});