Skip to content

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

fix(memory): retain moved removals when the history is full

Afloat3a37f663c9b7
diff --git a/server/cloud-send-removals.test.ts b/server/cloud-send-removals.test.ts
new file mode 100644
index 0000000000..989cbb0789
--- /dev/null
+++ b/server/cloud-send-removals.test.ts
@@ -0,0 +1,49 @@
+import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
+import { join } from "node:path";
+import { beforeEach, expect, it } from "vitest";
+import { moveMemoryRoute, type MoveMemoryDeps } from "./cloud-send-memory.ts";
+import { DATA_DIR } from "./config.ts";
+import { learnedState, planLearned } from "./profile-learned.ts";
+
+const path = join(DATA_DIR, "profile-learned.json");
+const deps: MoveMemoryDeps = {
+ bot: () => undefined,
+ teamMemory: { list: () => [], adopt: () => 0 },
+ aboutMe: () => "",
+ saveAboutMe: () => { throw new Error("a removal-only move must not add About me lines"); },
+ owner: () => ({ email: "[email protected]" }) as never,
+};
+
+beforeEach(() => { mkdirSync(DATA_DIR, { recursive: true }); });
+
+async function receive(removed: string[]) {
+ let answer: { status: number; body: unknown } | undefined;
+ await moveMemoryRoute(deps, {
+ path: "/api/cloud-send/memory", method: "POST", url: new URL("http://localhost/api/cloud-send/memory"),
+ req: {}, res: { setHeader() {} }, auth: {},
+ readBody: async () => ({ v: 1, about: { learned: [], removed } }),
+ json: (_res: unknown, status: number, body: unknown) => { answer = { status, body }; },
+ } as never);
+ expect(answer).toMatchObject({ status: 200, body: { about: 0, failed: [] } });
+}
+
+it("keeps a newly moved removal when the stored removal history is already full", async () => {
+ const old = Array.from({ length: 500 }, (_, i) => `Old fact ${i}`);
+ writeFileSync(path, JSON.stringify({ learned: [], removed: old }));
+ await receive(["Prefers phone calls"]);
+
+ // Planning reopens the file, as a later bot turn or restarted server would.
+ expect(planLearned({ botId: "bot", botName: "Nova" }, ["Prefers phone calls"], "", "2026-11-21")).toEqual([]);
+ expect(learnedState().removed).toEqual([...old.slice(1), "Prefers phone calls"]);
+ const once = readFileSync(path, "utf8");
+ await receive(["Prefers phone calls"]);
+ expect(readFileSync(path, "utf8")).toBe(once);
+});
+
+it("retains local removals and forget passages when receiving duplicate removals", async () => {
+ const state = { learned: [], removed: ["Prefers phone calls"], forgotten: ["acme"] };
+ writeFileSync(path, JSON.stringify(state));
+ await receive(["Prefers phone calls"]);
+ expect(JSON.parse(readFileSync(path, "utf8"))).toEqual(state);
+ expect(planLearned({ botId: "bot", botName: "Nova" }, ["Works at Acme", "Prefers phone calls"], "", "2026-11-21")).toEqual([]);
+});
diff --git a/server/profile-learned.ts b/server/profile-learned.ts
index 8766804b9a..2563625753 100644
--- a/server/profile-learned.ts
+++ b/server/profile-learned.ts
@@ -164,6 +164,7 @@ export function adoptLearned(facts: readonly LearnedFact[], removed: readonly st
const next = appendAboutMe(aboutMe, lines);
if (next === null) return null;
const removals = [...new Set([...file.removed, ...removed])].slice(-MAX_REMOVED);
- if (kept.length || removals.length !== file.removed.length) save({ ...file, learned: [...file.learned, ...kept].slice(-MAX_KEPT), removed: removals });
+ const removalsChanged = removals.length !== file.removed.length || removals.some((identity, i) => identity !== file.removed[i]);
+ if (kept.length || removalsChanged) save({ ...file, learned: [...file.learned, ...kept].slice(-MAX_KEPT), removed: removals });
return { aboutMe: next, added: lines.length };
}