Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Memory journal: a fixed 32 MiB cap, with no environment override
milind-soni19ba448626c9
diff --git a/server/bot-box/env.test.ts b/server/bot-box/env.test.tsindex 272bef95c3..9b587e404b 100644--- a/server/bot-box/env.test.ts+++ b/server/bot-box/env.test.ts@@ -42,7 +42,7 @@ const SERVER_ONLY_OMB: ReadonlySet<string> = new Set(["OMB_GREENFERENCE_API_KEY", "OMB_GROQ_API_KEY", "OMB_HANDOFF_MAX_FAILURES", "OMB_HANDOFF_MAX_TURNS", "OMB_HELPER_INSTANCE", "OMB_HELPER_MODEL", "OMB_HOOKS", "OMB_HOOK_","OMB_HOSTED_", "OMB_HOSTED_MODELS", "OMB_HOSTED_MODEL_TOKEN", "OMB_HOSTED_MODEL_URL", "OMB_IDLE_ENGINES_MAX", "OMB_IMAGE_ENGINES", "OMB_INSTALLATION_CREDENTIAL","OMB_JEV_API_KEY", "OMB_LAN", "OMB_LICENSE_KEY", "OMB_LINUX_GPU", "OMB_LIVE_PROVIDER", "OMB_LOCAL_VM_UPDATE_WAIT_MS",- "OMB_LOOPBACK_TRUST", "OMB_LOOP_GUARD", "OMB_LOOP_GUARD_STOP_AT", "OMB_LOOP_GUARD_UNATTENDED_STOP_AT", "OMB_MEMORY_JOURNAL_MAX_BYTES", "OMB_META_API_KEY", "OMB_MODELGO_API_KEY", "OMB_NETLIFY_API_URL", "OMB_OPENAI_API_KEY",+ "OMB_LOOPBACK_TRUST", "OMB_LOOP_GUARD", "OMB_LOOP_GUARD_STOP_AT", "OMB_LOOP_GUARD_UNATTENDED_STOP_AT", "OMB_META_API_KEY", "OMB_MODELGO_API_KEY", "OMB_NETLIFY_API_URL", "OMB_OPENAI_API_KEY","OMB_OPENAI_IMAGE_KEY", "OMB_OPENAI_LIVE_KEY", "OMB_OPENAI_LIVE_URL", "OMB_OPENROUTER_API_KEY","OMB_OPENROUTER_DECIDER_KEY", "OMB_OWN_MCP_HTTP", "OMB_SESSION_TOOL_BUDGET", "OMB_PERSON_FIRST_MS", "OMB_PHONE_PUSH", "OMB_PHONE_RELAY", "OMB_PHONE_RELAY_URL", "OMB_PHONE_SECRET_PUBLIC_KEY", "OMB_PODMAN_SERVICE", "OMB_PORT", "OMB_SERVES_TUNNEL", "OMB_POSTIZ_API_KEY","OMB_POSTIZ_CLOUD_URL", "OMB_PROCESS_ID", "OMB_PUBLIC_IPV4", "OMB_PUBLIC_URL", "OMB_PUSH_CREDENTIAL", "OMB_QUOTA_SWITCH",diff --git a/server/memory-journal.test.ts b/server/memory-journal.test.tsindex b5132dced7..76bc91f150 100644--- a/server/memory-journal.test.ts+++ b/server/memory-journal.test.ts@@ -6,15 +6,14 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";import {BEFORE_CAP,- DEFAULT_JOURNAL_MAX_BYTES,DIFF_CAP,JOURNAL_DIR,+ JOURNAL_MAX_BYTES,beginMemoryTurn,endMemoryTurn,forgetBotMemoryJournal,flushMemoryJournal,journalFile,- journalMaxBytes,journalMemoryDelete,journalMemoryWrite,journalTurnWrite,@@ -213,44 +212,26 @@ describe("journalMemoryWrite / readMemoryJournal", () => {if (process.platform !== "win32") expect(statSync(journalFile(bot)).mode & 0o777).toBe(0o600);});- it("honours OMB_MEMORY_JOURNAL_MAX_BYTES after each write", async () => {- const bot = freshBot();- const previous = process.env.OMB_MEMORY_JOURNAL_MAX_BYTES;- process.env.OMB_MEMORY_JOURNAL_MAX_BYTES = "1024";- try {- for (let i = 0; i < 40; i++) journalMemoryWrite(bot, `memory/cap-${i}.md`, `row ${i}\n`, { actor: "bot", via: "turn" });- await flushMemoryJournal(bot);- expect(statSync(journalFile(bot)).size).toBeLessThan(4096);- expect(readMemoryJournal(bot, 1)[0].path).toBe("memory/cap-39.md");- expect(journalMaxBytes()).toBe(1024);- } finally {- if (previous === undefined) delete process.env.OMB_MEMORY_JOURNAL_MAX_BYTES;- else process.env.OMB_MEMORY_JOURNAL_MAX_BYTES = previous;- }- });-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;- }+ journalMemoryWrite(bot, "memory/huge.md", `${"x".repeat(64_000)}\n`, { actor: "bot", via: "turn" });+ await flushMemoryJournal(bot);+ expect(trimMemoryJournal(bot, 1024)).toBe(true);+ expect(statSync(journalFile(bot)).size).toBe(0);+ journalMemoryWrite(bot, "memory/small.md", "small\n", { actor: "bot", via: "turn" });+ await flushMemoryJournal(bot);+ expect(trimMemoryJournal(bot, 1024)).toBe(false);+ expect(readMemoryJournal(bot, 1)[0].path).toBe("memory/small.md");});- 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);- expect(journalMaxBytes({ OMB_MEMORY_JOURNAL_MAX_BYTES: "2048" })).toBe(2048);+ it("leaves a journal under its cap alone after a write", async () => {+ const bot = freshBot();+ journalMemoryWrite(bot, "memory/one.md", "one\n", { actor: "bot", via: "turn" });+ await flushMemoryJournal(bot);+ const before = readFileSync(journalFile(bot), "utf8");+ expect(statSync(journalFile(bot)).size).toBeLessThan(JOURNAL_MAX_BYTES);+ expect(trimMemoryJournal(bot)).toBe(false);+ expect(readFileSync(journalFile(bot), "utf8")).toBe(before);});it("skips a torn last line rather than failing the read", async () => {diff --git a/server/memory-journal.ts b/server/memory-journal.tsindex 90f018921c..67b5b90108 100644--- a/server/memory-journal.ts+++ b/server/memory-journal.ts@@ -46,8 +46,7 @@ export const DIFF_CAP = 8_000;export const BEFORE_CAP = 512 * 1024;const MAX_JOURNAL_READ_BYTES = 8 * 1024 * 1024;/** Past this size the oldest rows go: they sit outside the read window already, so nothing reachable is lost. */-export const DEFAULT_JOURNAL_MAX_BYTES = 32 * 1024 * 1024;-const MIN_JOURNAL_MAX_BYTES = 1024;+export const JOURNAL_MAX_BYTES = 32 * 1024 * 1024;const MAX_ROWS = 500;const LCS_CELL_CAP = 4_000_000;const CONTEXT = 2;@@ -161,17 +160,8 @@ export function unifiedDiff(before: string, after: string, path: string, cap = Dreturn { text, added, removed, truncated };}--/** The journal's byte cap: `OMB_MEMORY_JOURNAL_MAX_BYTES` (at least 1 KB), else 32 MiB. */-export function journalMaxBytes(env: NodeJS.ProcessEnv = process.env): number {- const raw = env.OMB_MEMORY_JOURNAL_MAX_BYTES;- if (!raw || !/^\d+$/.test(raw)) return DEFAULT_JOURNAL_MAX_BYTES;- const value = Number(raw);- return Number.isSafeInteger(value) && value >= MIN_JOURNAL_MAX_BYTES ? value : DEFAULT_JOURNAL_MAX_BYTES;-}-/** Drops the oldest rows once the file passes `maxBytes`, keeping the newest rows the read window can still reach. */-export function trimMemoryJournal(botId: string, maxBytes = journalMaxBytes()): boolean {+export function trimMemoryJournal(botId: string, maxBytes = JOURNAL_MAX_BYTES): boolean {const file = journalFile(botId);let size: number;try { size = statSync(file).size; } catch { return false; }