Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(memory): preserve forget markers when an append fails
Afloatee20fa0a0baa
diff --git a/server/memory-append-forgotten.test.ts b/server/memory-append-forgotten.test.tsnew file mode 100644index 0000000000..2ee37b7709--- /dev/null+++ b/server/memory-append-forgotten.test.ts@@ -0,0 +1,61 @@+import { mkdirSync, readFileSync, rmSync } from "node:fs";+import { join } from "node:path";++import { afterEach, beforeEach, describe, expect, it } from "vitest";++import { DATA_DIR } from "./config.ts";+import { passageMatcher } from "./memory-entries.ts";+import { botMemoryUpdate } from "./memory-forget.ts";+import { isForgotten, planLearned, rememberForgotten } from "./profile-learned.ts";+import { MEMORY_ENTRY_MAX_CHARS, MEMORY_ENTRY_MAX_LINES, readMemoryFile, workspaceDir, writeMemoryFile } from "./workspace.ts";++const bot = `append-forgotten-${process.pid}`;+const fact = "The person prefers long replies";+const profile = join(DATA_DIR, "profile-learned.json");+const options = { source: "chat", aboutMe: null };++beforeEach(() => {+ rmSync(profile, { force: true });+ writeMemoryFile(bot, "- Existing note.\n");+ rememberForgotten("long replies", passageMatcher("long replies")!);+ rememberForgotten("cello", passageMatcher("cello")!);+});++afterEach(() => {+ rmSync(profile, { force: true });+ rmSync(workspaceDir(bot), { recursive: true, force: true });+});++describe("a rejected append preserves the person's forget request", () => {+ it.each([+ { reason: "an incompatible oldText", input: { text: fact, oldText: "Existing note." } },+ { reason: "an invalid expiry", input: { text: fact, until: "not-a-date" } },+ { reason: "too many characters", input: { text: fact + " " + "x".repeat(MEMORY_ENTRY_MAX_CHARS) } },+ { reason: "too many lines", input: { text: fact + "\nline".repeat(MEMORY_ENTRY_MAX_LINES) } },+ ])("keeps the profile unchanged after $reason", ({ input }) => {+ const before = readFileSync(profile, "utf8");+ expect(botMemoryUpdate(bot, { action: "append", ...input }, options)).toMatchObject({ ok: false, code: "invalid" });+ expect(readMemoryFile(bot).text).toBe("- Existing note.\n");+ expect(readFileSync(profile, "utf8")).toBe(before);+ expect(isForgotten(fact)).toBe(true);+ expect(planLearned({ botId: bot, botName: "Scout" }, [fact], "", "2026-11-17")).toEqual([]);+ });++ it("keeps the marker when the memory file cannot be read", () => {+ const before = readFileSync(profile, "utf8");+ const memory = join(workspaceDir(bot), "MEMORY.md");+ rmSync(memory);+ mkdirSync(memory);+ expect(() => botMemoryUpdate(bot, { action: "append", text: fact }, options)).toThrow();+ expect(readFileSync(profile, "utf8")).toBe(before);+ expect(isForgotten(fact)).toBe(true);+ });++ it("unblocks only the restored fact after the append succeeds", () => {+ expect(botMemoryUpdate(bot, { action: "append", text: fact }, options)).toMatchObject({ ok: true });+ expect(readMemoryFile(bot).text).toContain(fact);+ expect(isForgotten(fact)).toBe(false);+ expect(isForgotten("The person plays the cello")).toBe(true);+ expect(planLearned({ botId: bot, botName: "Scout" }, [fact], "", "2026-11-17").map((entry) => entry.text)).toEqual([fact]);+ });+});diff --git a/server/memory-forget.ts b/server/memory-forget.tsindex 09c9a6cf98..99a31ed42a 100644--- a/server/memory-forget.ts+++ b/server/memory-forget.ts@@ -116,9 +116,9 @@ export function botMemoryUpdate(rememberForgotten(passage, mentions);return { ok: true, forgotten, aboutMe };}- if (body.action === "append" && typeof body.text === "string") clearForgotten(body.text);const result = updateMemory(layer, { action: body.action, text: body.text, oldText: body.oldText, ...(body.until !== undefined ? { until: body.until } : {}) } as MemoryUpdate, { source: opts.source });- // A supersede restores the fact only once the old entry was really replaced; a stale old_text keeps the marker.- if (body.action === "supersede" && result.ok && typeof body.text === "string") clearForgotten(body.text);+ // Restore a fact only once it was saved: rejected arguments, a stale old_text,+ // or a failed file write must keep the person's forget request in force.+ if ((body.action === "append" || body.action === "supersede") && result.ok && typeof body.text === "string") clearForgotten(body.text);return result;}