Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

Memory journal: stay bounded when one entry exceeds the cap

erywim6dbc38e3c60c
diff --git a/server/memory-journal.test.ts b/server/memory-journal.test.ts
index b436e4a088..b5132dced7 100644
--- a/server/memory-journal.test.ts
+++ b/server/memory-journal.test.ts
@@ -229,6 +229,24 @@ describe("journalMemoryWrite / readMemoryJournal", () => {
}
});
+ it("keeps the journal bounded when one entry alone is larger than the cap", async () => {
+ const bot = freshBot();
+ const previous = process.env.OMB_MEMORY_JOURNAL_MAX_BYTES;
+ process.env.OMB_MEMORY_JOURNAL_MAX_BYTES = "1024";
+ try {
+ journalMemoryWrite(bot, "memory/huge.md", `${"x".repeat(64_000)}\n`, { actor: "bot", via: "turn" });
+ await flushMemoryJournal(bot);
+ expect(statSync(journalFile(bot)).size).toBe(0);
+ journalMemoryWrite(bot, "memory/small.md", "small\n", { actor: "bot", via: "turn" });
+ await flushMemoryJournal(bot);
+ expect(readMemoryJournal(bot, 1)[0].path).toBe("memory/small.md");
+ expect(statSync(journalFile(bot)).size).toBeLessThanOrEqual(1024);
+ } finally {
+ if (previous === undefined) delete process.env.OMB_MEMORY_JOURNAL_MAX_BYTES;
+ else process.env.OMB_MEMORY_JOURNAL_MAX_BYTES = previous;
+ }
+ });
+
it("reads the cap from the environment only when it is a sane byte count", () => {
expect(journalMaxBytes({})).toBe(DEFAULT_JOURNAL_MAX_BYTES);
expect(journalMaxBytes({ OMB_MEMORY_JOURNAL_MAX_BYTES: "0" })).toBe(DEFAULT_JOURNAL_MAX_BYTES);
diff --git a/server/memory-journal.ts b/server/memory-journal.ts
index 268c13ee63..f11fb89b20 100644
--- a/server/memory-journal.ts
+++ b/server/memory-journal.ts
@@ -195,7 +195,12 @@ export function trimMemoryJournal(botId: string, maxBytes = journalMaxBytes()):
// The first line of a tail read is usually cut mid-row; everything after it is a whole record.
const firstBreak = text.indexOf("\n");
const kept = firstBreak >= 0 ? text.slice(firstBreak + 1) : "";
- if (!kept.trim()) return false;
+ // No whole record fits in the window: the newest one alone is past the cap, so keeping any of it would leave the
+ // journal unbounded. Emptying the file is the only bounded state.
+ if (!kept.trim()) {
+ writeFileAtomic(file, "", { mode: 0o600 });
+ return true;
+ }
writeFileAtomic(file, kept, { mode: 0o600 });
return true;
}
@@ -249,6 +254,7 @@ export async function adoptMemoryJournal(botId: string, entries: readonly Memory
const at = (line: string) => { try { return Number((JSON.parse(line) as { at?: unknown }).at) || 0; } catch { return 0; } };
mkdirSync(JOURNAL_DIR, { recursive: true, mode: 0o700 });
writeFileAtomic(journalFile(botId), `${rows.map((line, index) => ({ line, index, at: at(line) })).sort((a, b) => a.at - b.at || a.index - b.index).map((row) => row.line).join("\n")}\n`, { mode: 0o600 });
+ trimMemoryJournal(botId);
return added.length;
}
diff --git a/server/memory-move.ts b/server/memory-move.ts
index 9acbcdf761..79131a5631 100644
--- a/server/memory-move.ts
+++ b/server/memory-move.ts
@@ -6,7 +6,7 @@ import { dirname, join, relative } from "node:path";
import { writeFileAtomic } from "./atomic.ts";
import { serverLine } from "./server-lines.ts";
-import { flushMemoryJournal, journalFile, JOURNAL_DIR } from "./memory-journal.ts";
+import { flushMemoryJournal, journalFile, JOURNAL_DIR, trimMemoryJournal } from "./memory-journal.ts";
import { ensureWorkspace, isMemorySeed, readMemoryText, syncMemoryIndex, workspaceDir } from "./workspace.ts";
function filesUnder(dir: string): string[] {
@@ -57,7 +57,10 @@ export async function moveMemoryLayer(from: string, to: string, language?: strin
try { return [JSON.stringify({ ...JSON.parse(line), botId: to, before: null, canRevert: false, revertUnavailableReason: serverLine(language, "server.memory.movedNote") })]; } catch { return []; }
});
mkdirSync(JOURNAL_DIR, { recursive: true, mode: 0o700 });
- if (rows.length) appendFileSync(journalFile(to), `${rows.join("\n")}\n`, { mode: 0o600 });
+ if (rows.length) {
+ appendFileSync(journalFile(to), `${rows.join("\n")}\n`, { mode: 0o600 });
+ trimMemoryJournal(to);
+ }
rmSync(journal, { force: true });
moved ||= rows.length > 0;
}