Skip to content

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

fix(memory): accept later stated occurrences of quoted evidence

Afloate07bb9c21b28
diff --git a/docs/verification/memory-layer.md b/docs/verification/memory-layer.md
index c7c4cd57de..f45b04d536 100644
--- a/docs/verification/memory-layer.md
+++ b/docs/verification/memory-layer.md
@@ -9,6 +9,7 @@ is off, and every memory write is a journaled, undoable row. See [the memory gui
```sh
pnpm exec vitest run server/memory-layer.e2e.test.ts
+pnpm exec vitest run server/memory-evidence-repeat.e2e.test.ts server/memory-capture-calls.test.ts server/memory-fact-checks.test.ts
pnpm exec vitest run server/reply-behaviour.e2e.test.ts
pnpm exec vitest run server/memory-forget.test.ts server/drivers/agents-call.test.ts
pnpm exec vitest run server/memory-capture-restart.e2e.test.ts
@@ -35,6 +36,7 @@ Evidence covers:
- Upkeep on: core facts are appended as dated, `(noticed)` entries with their until date; detail is filed into a topic file created with a title and aliases; all journaled as upkeep.
- `Balance is -10` captured twice is kept once; `Balance is 10` is a different fact.
- A fact about the person reaches About me on its own as a dated, attributed line in the prompt; Remove takes it out and a second Remove is refused.
+- A quote first used in a question or hypothetical can still support a later statement in the same message. The isolated Codex fixture verifies the resulting memory file, capture journal and About me entry; repeated questions alone produce none of them.
- The tidy-up archives the expired entry and strikes the contradicted one, reports it, and Undo restores the file.
- A routine run is not captured as a person's conversation and never adds owner facts to About me.
- A turn still waiting for the quiet spell when the server stops is saved as ids only, read back from the transcript after a restart, and captured then (POSIX: a Windows SIGTERM skips the shutdown hooks).
diff --git a/server/memory-capture-calls.test.ts b/server/memory-capture-calls.test.ts
index 82338f33b1..bec91b0b28 100644
--- a/server/memory-capture-calls.test.ts
+++ b/server/memory-capture-calls.test.ts
@@ -258,6 +258,36 @@ describe("capture calls", () => {
expect(aboutMe).toEqual(["The person is vegetarian"]);
});
+ it.each([
+ "Did I say I prefer short replies? I prefer short replies.",
+ "Imagine I prefer short replies. In fact, I prefer short replies.",
+ ])("keeps a real statement after an earlier non-statement of the same evidence: %s", async (person) => {
+ answer = () => JSON.stringify([{ text: "The person prefers short replies", kind: "preference", evidence: "I prefer short replies", aboutUser: true }]);
+ const upkeep = loop();
+ upkeep.noteTurn(LEAD.id, "confirmed", { person, bot: ACK });
+ upkeep.flushThread("confirmed");
+ await upkeep.idle();
+ expect(memory(LEAD.id)).toContain('from chat "confirmed" (noticed) · The person prefers short replies');
+ expect(aboutMe).toEqual(["The person prefers short replies"]);
+ await flushMemoryJournal(LEAD.id);
+ expect(readMemoryJournal(LEAD.id, 10).filter((row) => row.via === "capture").map((row) => row.threadId)).toEqual(["confirmed"]);
+ });
+
+ it.each([
+ "Did I say I prefer short replies? Have I said I prefer short replies?",
+ "Imagine I prefer short replies. Suppose I prefer short replies.",
+ ])("keeps repeated questions and hypotheticals out of persisted memory: %s", async (person) => {
+ answer = () => JSON.stringify([{ text: "The person prefers short replies", kind: "preference", evidence: "I prefer short replies", aboutUser: true }]);
+ const upkeep = loop();
+ upkeep.noteTurn(LEAD.id, "unconfirmed", { person, bot: ACK });
+ upkeep.flushThread("unconfirmed");
+ await upkeep.idle();
+ expect(memory(LEAD.id)).not.toContain("The person prefers short replies");
+ expect(aboutMe).toEqual([]);
+ await flushMemoryJournal(LEAD.id);
+ expect(readMemoryJournal(LEAD.id, 10).filter((row) => row.via === "capture")).toEqual([]);
+ });
+
it("adds to About me only from the owner's own conversations, also when one call reads both", async () => {
answer = () => JSON.stringify([
{ from: 1, text: "The person is vegetarian", kind: "preference", evidence: "I'm vegetarian", aboutUser: true },
diff --git a/server/memory-capture.ts b/server/memory-capture.ts
index 21c4fb183b..6da3629b29 100644
--- a/server/memory-capture.ts
+++ b/server/memory-capture.ts
@@ -210,14 +210,16 @@ const HYPOTHETICAL = /\b(?:suppose|supposing|imagine|pretend|hypothetical(?:ly)?
function statedIn(said: string, evidence: string): boolean {
const text = foldForMatch(said);
const quote = foldForMatch(evidence).replace(/[\s.!]+$/, "");
- const at = quote.length >= 3 ? text.indexOf(quote) : -1;
- if (at < 0 || QUESTION.test(quote)) return false;
- let start = at;
- while (start > 0 && !SENTENCE_END.test(text[start - 1]!)) start -= 1;
- const rest = text.slice(at + quote.length);
- const stop = rest.search(SENTENCE_END);
- const sentence = text.slice(start, at + quote.length + (stop < 0 ? rest.length : stop + 1));
- return !QUESTION.test(sentence.slice(-1)) && !HYPOTHETICAL.test(sentence);
+ if (quote.length < 3 || QUESTION.test(quote)) return false;
+ for (let at = text.indexOf(quote); at >= 0; at = text.indexOf(quote, at + 1)) {
+ let start = at;
+ while (start > 0 && !SENTENCE_END.test(text[start - 1]!)) start -= 1;
+ const rest = text.slice(at + quote.length);
+ const stop = rest.search(SENTENCE_END);
+ const sentence = text.slice(start, at + quote.length + (stop < 0 ? rest.length : stop + 1));
+ if (!QUESTION.test(sentence.slice(-1)) && !HYPOTHETICAL.test(sentence)) return true;
+ }
+ return false;
}
/** A preference or fact is kept only when what was said to the bot states it in so many words; for About me
diff --git a/server/memory-evidence-repeat.e2e.test.ts b/server/memory-evidence-repeat.e2e.test.ts
new file mode 100644
index 0000000000..7b73c3cb84
--- /dev/null
+++ b/server/memory-evidence-repeat.e2e.test.ts
@@ -0,0 +1,60 @@
+import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import { expect, it } from "vitest";
+import { launchVerificationServer, runControlOmb } from "../scripts/control-omb.ts";
+import type { WireBot } from "../shared/wire.ts";
+import { CAPTURE_MARKER } from "./memory-capture.ts";
+import { ORGANIZE_MARKER } from "./memory-organize.ts";
+
+it("captures a repeated quote only when a real statement supports it", async () => {
+ const scratch = mkdtempSync(join(tmpdir(), "omb-memory-evidence-"));
+ const routes = join(scratch, "routes.json");
+ writeFileSync(routes, JSON.stringify({
+ [CAPTURE_MARKER]: JSON.stringify([{
+ text: "The person prefers short replies", kind: "preference",
+ evidence: "I prefer short replies", aboutUser: true,
+ }]),
+ [ORGANIZE_MARKER]: '{"moves": []}',
+ }));
+ const fixture = await launchVerificationServer({ ...process.env, FAKE_CODEX_TEXT_ROUTES: routes },
+ undefined, undefined, undefined, undefined, undefined, ["codex"]);
+ const api = async <T>(path: string, method = "GET", body?: unknown, status = 200): Promise<T> => {
+ const response = await fetch(fixture.info.url + path, {
+ method,
+ headers: { "content-type": "application/json" },
+ ...(body === undefined ? {} : { body: JSON.stringify(body) }),
+ });
+ expect(response.status, `${method} ${path}`).toBe(status);
+ return response.json() as Promise<T>;
+ };
+ try {
+ const { instances } = await api<{ instances: Array<{ instanceId: string; models: { default: string } }> }>("/api/instances");
+ const codex = instances.find((instance) => instance.instanceId === "codex")!;
+ await api("/api/config", "PUT", { memory: { captureQuietMs: 1_000 } });
+ for (const [person, added] of [
+ ["Did I say I prefer short replies? Have I said I prefer short replies?", 0],
+ ["Did I say I prefer short replies? I prefer short replies.", 1],
+ ] as const) {
+ const { bot } = await api<{ bot: WireBot }>("/api/bots", "POST", {
+ name: `Evidence ${added}`, modelSelection: { instanceId: "codex", model: codex.models.default },
+ }, 201);
+ const control = (...args: string[]) => runControlOmb([...args, "--url", fixture.info.url]);
+ await control("send", "--bot", bot.id, "--task", bot.threadId, "--text", person);
+ expect(await control("wait", "--bot", bot.id, "--task", bot.threadId, "--timeout", "30")).toMatchObject({ status: "settled" });
+ type Upkeep = { lastCapture?: { added: number } };
+ await expect.poll(async () => (await api<Upkeep>(`/api/bots/${bot.id}/memory/upkeep`)).lastCapture,
+ { timeout: 20_000 }).toBeDefined();
+ expect((await api<Upkeep>(`/api/bots/${bot.id}/memory/upkeep`)).lastCapture?.added).toBe(added);
+ const { text } = await api<{ text: string }>(`/api/bots/${bot.id}/memory/file?path=MEMORY.md`);
+ expect(text.includes("The person prefers short replies")).toBe(added === 1);
+ const { entries } = await api<{ entries: Array<{ via: string }> }>(`/api/bots/${bot.id}/memory/journal`);
+ expect(entries.filter((row) => row.via === "capture")).toHaveLength(added);
+ const { learned } = await api<{ learned: Array<{ text: string }> }>("/api/profile/learned");
+ expect(learned.map((row) => row.text)).toEqual(added ? ["The person prefers short replies"] : []);
+ }
+ } finally {
+ await fixture.close();
+ rmSync(scratch, { recursive: true, force: true });
+ }
+}, 120_000);
diff --git a/server/memory-fact-checks.test.ts b/server/memory-fact-checks.test.ts
index bb85f04e14..3c215e3296 100644
--- a/server/memory-fact-checks.test.ts
+++ b/server/memory-fact-checks.test.ts
@@ -35,6 +35,16 @@ describe("the checks before a dream saves a fact", () => {
expect(checkDreamFacts("Anna likes tea", [1], ["Anna likes tea"], [person("Suppose Anna likes tea?")])).toBe(false);
});
+ it("accepts a later statement in a cited conversation while still rejecting repeated non-statements", () => {
+ const quote = "Anna likes tea";
+ const verify = (line: string) => checkDreamFacts(quote, [1], [quote], [person(line)]);
+ expect(verify("Did I say Anna likes tea? Anna likes tea.")).toBe(true);
+ expect(verify("Suppose Anna likes tea. Anna likes tea.")).toBe(true);
+ expect(verify("Anna likes tea? Did I say Anna likes tea?")).toBe(false);
+ expect(verify("Suppose Anna likes tea. Imagine Anna likes tea.")).toBe(false);
+ expect(checkDreamFacts(quote, [1], [quote], [{ threadId: "bot", lines: [{ by: "bot", text: "Anna likes tea? Anna likes tea." }] }])).toBe(false);
+ });
+
it("refuses secrets even when the person really said them", () => {
const secret = "password: synthetic-secret-123";
expect(checkDreamFacts(secret, [1], [secret], [person(secret)])).toBe(false);