Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
MCP saved results: prune only our own result- files; never prune the file just written
AdityaUmalee3836a696b51
diff --git a/server/drivers/chat-mcp-tools.test.ts b/server/drivers/chat-mcp-tools.test.tsindex ba71742bfc..190351e936 100644--- a/server/drivers/chat-mcp-tools.test.ts+++ b/server/drivers/chat-mcp-tools.test.ts@@ -429,7 +429,7 @@ describe("Chat MCP session", () => {const result = await (await f.mount(false, false, undefined, folder)).execute("audit_write", { value: "test" }, f.controller.signal);const saved = readdirSync(join(folder, SAVED_RESULTS_FOLDER));expect(saved).toHaveLength(1);- expect(saved[0]).toMatch(/^audit_write-\d+-[0-9a-f]{6}\.txt$/);+ expect(saved[0]).toMatch(/^result-audit_write-\d+-[0-9a-f]{6}\.txt$/);const file = join(folder, SAVED_RESULTS_FOLDER, saved[0]);expect(readFileSync(file, "utf8")).toBe(big);expect(result.ok).toBe(true);@@ -445,7 +445,7 @@ describe("Chat MCP session", () => {const session = await f.mount(false, false, undefined, folder, "../../escape");await session.execute(session.definitions[0].function.name, { value: "test" }, f.controller.signal);expect(readdirSync(folder)).toEqual([SAVED_RESULTS_FOLDER]);- expect(readdirSync(join(folder, SAVED_RESULTS_FOLDER))[0]).toMatch(/^[a-z0-9_]+-\d+-[0-9a-f]{6}\.txt$/);+ expect(readdirSync(join(folder, SAVED_RESULTS_FOLDER))[0]).toMatch(/^result-[a-z0-9_]+-\d+-[0-9a-f]{6}\.txt$/);});it("is cut as before when the results folder is a link elsewhere", async () => {@@ -487,17 +487,33 @@ describe("Chat MCP session", () => {const folder = workFolder();const results = join(folder, SAVED_RESULTS_FOLDER);mkdirSync(results);- const old = Array.from({ length: SAVED_RESULTS_KEPT }, (_, i) => `audit_write-${1_700_000_000_000 + i}-abcdef.txt`);+ const old = Array.from({ length: SAVED_RESULTS_KEPT }, (_, i) => `result-audit_write-${1_700_000_000_000 + i}-abcdef.txt`);for (const name of old) writeFileSync(join(results, name), "old");writeFileSync(join(results, "notes.md"), "not a saved result");+ writeFileSync(join(results, "export-1700000000000-abcdef.txt"), "the person's own file with a look-alike name");await (await f.mount(false, false, undefined, folder)).execute("audit_write", { value: "test" }, f.controller.signal);const left = readdirSync(results);- expect(left).toHaveLength(SAVED_RESULTS_KEPT + 1);+ expect(left).toHaveLength(SAVED_RESULTS_KEPT + 2);expect(left).toContain("notes.md");+ expect(left).toContain("export-1700000000000-abcdef.txt");expect(left).not.toContain(old[0]);expect(left).toContain(old[1]);});+ it("never removes the file it just wrote, even when the clock went back", async () => {+ const f = bigFixture();+ const folder = workFolder();+ const results = join(folder, SAVED_RESULTS_FOLDER);+ mkdirSync(results);+ const later = Array.from({ length: SAVED_RESULTS_KEPT }, (_, i) => `result-audit_write-${Date.now() + 3_600_000 + i}-abcdef.txt`);+ for (const name of later) writeFileSync(join(results, name), "later");+ const result = await (await f.mount(false, false, undefined, folder)).execute("audit_write", { value: "test" }, f.controller.signal);+ const file = /saved in (\S+?)\]/.exec(result.text)?.[1] ?? "";+ expect(readFileSync(file, "utf8")).toBe(big);+ expect(readdirSync(results)).toHaveLength(SAVED_RESULTS_KEPT);+ expect(readdirSync(results)).not.toContain(later[0]);+ });+it("leaves a result under 50KB as it is, with no file", async () => {const f = fixture(`if (message.method === "tools/call") { reply(message,{content:[{type:"text",text:"small"}]}); continue; }`);const folder = workFolder();diff --git a/server/drivers/chat-mcp-tools.ts b/server/drivers/chat-mcp-tools.tsindex a44345ed99..739c8769bd 100644--- a/server/drivers/chat-mcp-tools.ts+++ b/server/drivers/chat-mcp-tools.ts@@ -287,14 +287,15 @@ function boundedText(value: string, bytes = Buffer.from(value)): string {export const SAVED_RESULTS_FOLDER = "mcp-results";/** Only the newest saved results are kept, so repeated big results cannot fill the disk. */export const SAVED_RESULTS_KEPT = 20;-const SAVED_RESULT_NAME = /-(\d+)-[0-9a-f]{6}\.txt$/;+const SAVED_RESULT_NAME = /^result-.+-(\d+)-[0-9a-f]{6}\.txt$/;-async function pruneSavedResults(dir: string): Promise<void> {+/** The file just written (`keep`) is never removed, even if the clock went back. */+async function pruneSavedResults(dir: string, keep: string): Promise<void> {const saved = (await readdir(dir)).flatMap((name) => {- const match = SAVED_RESULT_NAME.exec(name);+ const match = name === keep ? null : SAVED_RESULT_NAME.exec(name);return match ? [{ name, at: Number(match[1]) }] : [];}).sort((a, b) => b.at - a.at);- await Promise.all(saved.slice(SAVED_RESULTS_KEPT).map(({ name }) => unlink(join(dir, name))));+ await Promise.all(saved.slice(SAVED_RESULTS_KEPT - 1).map(({ name }) => unlink(join(dir, name))));}/** A result over the limit is kept whole (secrets masked by `scrub`) in `folder`/mcp-results and the model reads its start and the file's path.@@ -307,11 +308,11 @@ async function savedOrBounded(value: string, tool: string, folder: string | undeawait mkdir(dir, { recursive: true, mode: 0o700 });// Security: never follow a link the folder's contents put here to write outside the working folder.if (!(await lstat(dir)).isDirectory()) throw new Error("not a folder");- const file = join(dir, `${tool}-${Date.now()}-${randomBytes(3).toString("hex")}.txt`);+ const name = `result-${tool}-${Date.now()}-${randomBytes(3).toString("hex")}.txt`, file = join(dir, name);// Key-shaped values are masked before the result reaches disk, as for every other kept tool result.const kept = scrub(value);await writeFile(file, kept, { flag: "wx", mode: 0o600 });- await pruneSavedResults(dir).catch(() => {});+ await pruneSavedResults(dir, name).catch(() => {});const kb = Math.ceil(bytes.length / 1024), masked = kept === value ? "" : " (secret-looking values in it are masked)";return `${preview(bytes)}\n[MCP result is ${kb}KB; only the first 50KB is shown above. The full result is saved in ${file}${masked}]`;} catch {