Skip to content

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

45.1 KB
Raw Blame
import { chmodSync, existsSync, readdirSync, readFileSync, rmSync, statSync, writeFileSync } from "node:fs";
import { join } from "node:path";
import { beforeEach, describe, expect, it } from "vitest";

import { DATA_DIR } from "./config.ts";
import { closeMessageDb, recallMemory } from "./message-db.ts";

import {
  appendMemoryLog,
  ensureWorkspace,
  ensureTaskWorkspace,
  listMemoryLogs,
  readMemoryLog,
  isMemoryTopicName,
  listMemoryTopics,
  loadMemory,
  memoryNotesPrompt,
  memoryRulesPrompt,
  memorySystemPrompt,
  REMEMBER_AND_FORGET,
  supportsWorkspaceFiles,
  readMemoryFile,
  readMemoryTopic,
  searchMemoryFiles,
  syncMemoryIndex,
  workspaceDir,
  workspaceLocationsPrompt,
  writeMemoryTopic,
  writeMemoryFile,
  updateMemory,
  memoryDate,
  memoryEntry,
  memoryLineCount,
  memorySourceLabel,
  MEMORY_ENTRY_MAX_CHARS,
  MEMORY_ENTRY_MAX_LINES,
  MEMORY_MAX_BYTES,
  MEMORY_MAX_LINES,
  WORKSPACES_DIR,
  TASK_WORKSPACES_DIR,
} from "./workspace.ts";

const BOT = "bot-workspace-test";

describe("workspace", () => {
  beforeEach(() => {
    // The search index lives in the messages database; wiping the workspaces without it would leave stale rows.
    closeMessageDb();
    rmSync(DATA_DIR, { recursive: true, force: true });
    rmSync(WORKSPACES_DIR, { recursive: true, force: true });
    rmSync(TASK_WORKSPACES_DIR, { recursive: true, force: true });
  });

  it("describes current and earlier file locations without moving or exposing their contents", () => {
    const shared = ensureWorkspace(BOT);
    const thread = ensureTaskWorkspace(BOT, "thread-first");
    writeFileSync(join(shared, "old.txt"), "existing private file contents");
    const prompt = workspaceLocationsPrompt(BOT, thread, '/projects/quoted "folder"');
    expect(prompt).toContain(JSON.stringify(thread));
    expect(prompt).toContain(JSON.stringify(shared));
    expect(prompt).toContain(JSON.stringify(join(TASK_WORKSPACES_DIR, BOT)));
    expect(prompt).toContain(JSON.stringify('/projects/quoted "folder"'));
    expect(prompt).not.toContain("existing private file contents");
    expect(prompt).toContain("Do not move old files");
    expect(readFileSync(join(shared, "old.txt"), "utf8")).toBe("existing private file contents");
    expect(existsSync(join(thread, "old.txt"))).toBe(false);
    expect(workspaceLocationsPrompt(BOT, undefined)).toContain("inspect the working directory");
  });

  it("tells a bot with attach_file to deliver a file saved outside its folders, and no one else", () => {
    const thread = ensureTaskWorkspace(BOT, "thread-first");
    expect(workspaceLocationsPrompt(BOT, thread, undefined, { attachFile: true }))
      .toContain("also attach it with attach_file in the same turn");
    expect(workspaceLocationsPrompt(BOT, thread)).not.toContain("attach_file");
  });

  it("tells every bot that a picture it shows inline must be in the conversation's folders", () => {
    const thread = ensureTaskWorkspace(BOT, "thread-first");
    for (const prompt of [workspaceLocationsPrompt(BOT, thread), workspaceLocationsPrompt(BOT, thread, undefined, { attachFile: true })]) {
      expect(prompt).toContain("Chat links and inline images open only files in currentWorkingFolder and sharedBotFolder.");
    }
    expect(workspaceLocationsPrompt(BOT, thread)).toContain("Save a file or picture you want to show in chat there.");
  });

  it("creates distinct private task desks outside shared memory and refuses path traversal", () => {
    const shared = ensureWorkspace(BOT);
    const first = ensureTaskWorkspace(BOT, "thread-first");
    const second = ensureTaskWorkspace(BOT, "thread-second");
    expect(first).toBe(join(TASK_WORKSPACES_DIR, BOT, "thread-first"));
    expect(first.startsWith(shared)).toBe(false);
    expect(first).not.toBe(second);
    writeFileSync(join(first, "draft.txt"), "first thread only");
    expect(ensureTaskWorkspace(BOT, "thread-first")).toBe(first);
    expect(readFileSync(join(first, "draft.txt"), "utf8")).toBe("first thread only");
    expect(existsSync(join(second, "draft.txt"))).toBe(false);
    if (process.platform !== "win32") expect(statSync(first).mode & 0o777).toBe(0o700);
    for (const bad of ["", "..", "../other", "a/b", "a\\b", "/absolute", "a%2Fb", "x".repeat(129)]) {
      expect(() => ensureTaskWorkspace(bad, "thread")).toThrow("Invalid");
      expect(() => ensureTaskWorkspace(BOT, bad)).toThrow("Invalid");
    }
  });

  it("creates the workspace with a memory dir and a seeded MEMORY.md, idempotently", () => {
    const dir = ensureWorkspace(BOT);
    expect(dir).toBe(workspaceDir(BOT));
    expect(existsSync(join(dir, "memory"))).toBe(true);
    const seed = readFileSync(join(dir, "MEMORY.md"), "utf8");
    expect(seed).toContain("# Memory");

    writeFileSync(join(dir, "MEMORY.md"), "# Memory\n- the user prefers pnpm\n");
    ensureWorkspace(BOT);
    expect(readFileSync(join(dir, "MEMORY.md"), "utf8")).toContain("prefers pnpm");
  });

  it("treats a missing or seed-only MEMORY.md as empty", () => {
    expect(loadMemory(BOT)).toBeNull();
    ensureWorkspace(BOT);
    expect(loadMemory(BOT)).toBeNull();
  });

  it("treats localized factory memory seeds as empty", () => {
    for (const [language, heading] of [["pt-BR", "# Memória"], ["es", "# Memoria"], ["en", "# Memory"], ["zh-TW", "# 記憶"], ["zh-Hant", "# 記憶"], ["zh_HK", "# 記憶"], ["zh-MO", "# 記憶"]] as const) {
      const bot = `${BOT}-${language}`;
      const dir = ensureWorkspace(bot, language);
      expect(readFileSync(join(dir, "MEMORY.md"), "utf8")).toContain(heading);
      expect(loadMemory(bot)).toBeNull();
      expect(readMemoryFile(bot)).toEqual({ text: "", truncated: false });
    }
  });

  it("loads written memory whole when under budget", () => {
    const dir = ensureWorkspace(BOT);
    writeFileSync(join(dir, "MEMORY.md"), "# Memory\n- fact one\n- fact two\n");
    const memory = loadMemory(BOT);
    expect(memory?.text).toContain("fact two");
    expect(memory?.truncated).toBe(false);
  });

  it("cuts at the line budget and flags the truncation", () => {
    const dir = ensureWorkspace(BOT);
    const lines = Array.from({ length: MEMORY_MAX_LINES + 50 }, (_, i) => `- fact ${i}`);
    writeFileSync(join(dir, "MEMORY.md"), lines.join("\n"));
    const memory = loadMemory(BOT);
    expect(memory?.truncated).toBe(true);
    expect(memory?.text.split("\n")).toHaveLength(MEMORY_MAX_LINES);
    expect(memory?.text).toContain(`- fact ${MEMORY_MAX_LINES - 1}`);
    expect(memory?.text).not.toContain(`- fact ${MEMORY_MAX_LINES}\n`);
  });

  it("cuts at the byte budget without leaving a torn multi-byte character", () => {
    const dir = ensureWorkspace(BOT);
    // few lines, many bytes — multi-byte chars so a naive slice would tear one
    writeFileSync(join(dir, "MEMORY.md"), `# Memory\n${"é".repeat(MEMORY_MAX_BYTES)}`);
    const memory = loadMemory(BOT);
    expect(memory?.truncated).toBe(true);
    expect(Buffer.byteLength(memory!.text, "utf8")).toBeLessThanOrEqual(MEMORY_MAX_BYTES);
    expect(memory!.text).not.toContain("�");
  });

  it("readMemoryFile hands back the WHOLE file, flagging what the budget would cut", () => {
    expect(readMemoryFile(BOT)).toEqual({ text: "", truncated: false });
    ensureWorkspace(BOT);
    expect(readMemoryFile(BOT)).toEqual({ text: "", truncated: false });

    const dir = workspaceDir(BOT);
    const lines = Array.from({ length: MEMORY_MAX_LINES + 50 }, (_, i) => `- fact ${i}`);
    writeFileSync(join(dir, "MEMORY.md"), lines.join("\n"));
    const file = readMemoryFile(BOT);
    expect(file.truncated).toBe(true);
    expect(file.text.split("\n")).toHaveLength(MEMORY_MAX_LINES + 50);

    writeFileSync(join(dir, "MEMORY.md"), "# Memory\n- one fact\n");
    expect(readMemoryFile(BOT)).toEqual({ text: "# Memory\n- one fact\n", truncated: false });
  });

  it("writeMemoryFile round-trips without needing the workspace to exist first", () => {
    writeMemoryFile(BOT, "# Memory\n- written from the panel\n");
    expect(readMemoryFile(BOT).text).toContain("written from the panel");
    expect(loadMemory(BOT)?.text).toContain("written from the panel");
  });

  it("applies independent memory appends and targeted edits to the latest file without losing updates", () => {
    const now = new Date(2026, 8, 10, 12);
    const opts = { source: 'chat "Setup"', now };
    expect(updateMemory(BOT, { action: "append", text: "- Preferred package manager: npm" }, opts)).toMatchObject({
      ok: true, entry: '- 2026-09-10 · from chat "Setup" · Preferred package manager: npm',
    });
    const first = readMemoryFile(BOT).text;
    expect(first).toBe('- 2026-09-10 · from chat "Setup" · Preferred package manager: npm\n');
    expect(updateMemory(BOT, { action: "append", text: "Shipping day: Friday" }, opts).ok).toBe(true);
    const later = { source: 'chat "Setup"', now: new Date(2026, 8, 12, 12) };
    expect(updateMemory(BOT, { action: "replace", oldText: "manager: npm", text: "manager: pnpm" }, later)).toMatchObject({
      ok: true, entry: '- 2026-09-10 · from chat "Setup" · Preferred package manager: pnpm · updated 2026-09-12',
    });
    expect(readMemoryFile(BOT).text).toBe(
      '- 2026-09-10 · from chat "Setup" · Preferred package manager: pnpm · updated 2026-09-12\n' +
      '- 2026-09-10 · from chat "Setup" · Shipping day: Friday\n',
    );
    const beforeConflict = readMemoryFile(BOT).text;
    expect(updateMemory(BOT, { action: "replace", oldText: "manager: npm", text: "manager: yarn" }, later)).toMatchObject({ ok: false, code: "conflict" });
    expect(readMemoryFile(BOT).text).toBe(beforeConflict);
    expect(updateMemory(BOT, { action: "remove", oldText: '- 2026-09-10 · from chat "Setup" · Shipping day: Friday' })).toMatchObject({ ok: true });
    expect(readMemoryFile(BOT).text).toBe('- 2026-09-10 · from chat "Setup" · Preferred package manager: pnpm · updated 2026-09-12\n');
    writeMemoryFile(BOT, "- Edited by the user");
    expect(updateMemory(BOT, { action: "append", text: "Another thread's fact" }, opts)).toEqual({
      ok: true, truncated: false, entry: '- 2026-09-10 · from chat "Setup" · Another thread\'s fact', moved: [],
    });
    expect(readMemoryFile(BOT).text).toBe('- Edited by the user\n- 2026-09-10 · from chat "Setup" · Another thread\'s fact\n');
  });

  it("formats an entry as one dated, sourced line and keeps fenced blocks whole", () => {
    const now = new Date(2026, 8, 10, 12);
    expect(memoryDate(now)).toBe("2026-09-10");
    expect(memoryEntry("  - The user's name is Ada  ", { source: 'chat "Follow-up"', now })).toBe('- 2026-09-10 · from chat "Follow-up" · The user\'s name is Ada');
    expect(memoryEntry("Deploys on Fridays", { now })).toBe("- 2026-09-10 · Deploys on Fridays");
    expect(memoryEntry("first line\n   second line", { source: 'chat "A · B"', now })).toBe('- 2026-09-10 · from chat "A - B" · first line second line');
    const fenced = memoryEntry("Deploy command:\n```sh\nrailway up\n```", { now });
    expect(fenced).toBe("- 2026-09-10 · Deploy command:\n  ```sh\n  railway up\n  ```");
  });

  it("names an entry's source by room, then thread title, then thread id", () => {
    expect(memorySourceLabel({ room: { name: "Launch" }, task: { title: "ignored" }, threadId: "t1" })).toBe('room "Launch"');
    expect(memorySourceLabel({ task: { title: "Follow-up" }, threadId: "t1" })).toBe('chat "Follow-up"');
    expect(memorySourceLabel({ task: { title: "" }, threadId: "t1" })).toBe("thread t1");
    expect(memorySourceLabel({ threadId: "t1" })).toBe("thread t1");
    expect(memorySourceLabel({ task: { title: "t".repeat(80) }, threadId: "t1" })).toBe(`chat "${"t".repeat(60)}…"`);
    expect(memoryEntry("fact", { source: memorySourceLabel({ room: { name: "A · B" }, threadId: "t" }), now: new Date(2026, 8, 10, 12) }))
      .toBe('- 2026-09-10 · from room "A - B" · fact');
  });

  it("replace re-attaches the original prefix when the model retypes the whole entry", () => {
    const now = new Date(2026, 8, 10, 12);
    updateMemory(BOT, { action: "append", text: "Timezone: IST" }, { source: 'chat "Setup"', now });
    updateMemory(BOT, { action: "append", text: "Hand-written line stays" }, { source: 'chat "Setup"', now });
    writeMemoryFile(BOT, `${readMemoryFile(BOT).text}- undated note from the person\n`);
    const later = { source: 'room "Ops"', now: new Date(2026, 8, 11, 12) };
    expect(updateMemory(BOT, { action: "replace", oldText: '- 2026-09-10 · from chat "Setup" · Timezone: IST', text: "- Timezone: CET" }, later)).toMatchObject({
      ok: true, entry: '- 2026-09-10 · from chat "Setup" · Timezone: CET · updated 2026-09-11',
    });
    expect(updateMemory(BOT, { action: "replace", oldText: "Timezone: CET · updated 2026-09-11", text: '- 2026-09-11 · from room "Ops" · Timezone: UTC' }, later)).toMatchObject({
      ok: true, entry: '- 2026-09-10 · from chat "Setup" · Timezone: UTC · updated 2026-09-11',
    });
    expect(updateMemory(BOT, { action: "replace", oldText: "undated note", text: "undated fact" }, later)).toMatchObject({ ok: true });
    expect(readMemoryFile(BOT).text).toBe(
      '- 2026-09-10 · from chat "Setup" · Timezone: UTC · updated 2026-09-11\n' +
      '- 2026-09-10 · from chat "Setup" · Hand-written line stays\n' +
      "- undated fact from the person\n",
    );
  });

  it("supersede strikes the old entry through with the day it stopped being true and appends the new one", () => {
    const now = new Date(2026, 8, 10, 12);
    updateMemory(BOT, { action: "append", text: "Office: Berlin" }, { source: 'chat "Setup"', now });
    writeMemoryFile(BOT, `${readMemoryFile(BOT).text}- Old hand-written office: Paris\nplain paragraph\n`);
    const later = { source: 'chat "Move"', now: new Date(2026, 8, 20, 12) };
    expect(updateMemory(BOT, { action: "supersede", oldText: "Office: Berlin", text: "Office: Lisbon" }, later)).toMatchObject({
      ok: true, entry: '- 2026-09-20 · from chat "Move" · Office: Lisbon',
    });
    expect(updateMemory(BOT, { action: "supersede", oldText: "office: Paris", text: "office: Porto" }, later).ok).toBe(true);
    expect(updateMemory(BOT, { action: "supersede", oldText: "plain paragraph", text: "plain fact" }, later).ok).toBe(true);
    expect(readMemoryFile(BOT).text).toBe(
      '- 2026-09-10 · from chat "Setup" · ~~Office: Berlin~~ · superseded 2026-09-20\n' +
      "- ~~Old hand-written office: Paris~~ · superseded 2026-09-20\n" +
      "~~plain paragraph~~ · superseded 2026-09-20\n" +
      '- 2026-09-20 · from chat "Move" · Office: Lisbon\n' +
      '- 2026-09-20 · from chat "Move" · office: Porto\n' +
      '- 2026-09-20 · from chat "Move" · plain fact\n',
    );
    expect(updateMemory(BOT, { action: "supersede", oldText: "Office: Berlin", text: "Office: Rome" }, later)).toMatchObject({ ok: false, code: "conflict" });
    expect(updateMemory(BOT, { action: "supersede", oldText: "Porto\n- 2026-09-20", text: "x" }, later)).toMatchObject({ ok: false, code: "invalid" });
    expect(updateMemory(BOT, { action: "supersede", oldText: "Office: Lisbon" }, later)).toMatchObject({ ok: false, code: "invalid" });
  });

  it("rejects ambiguous, invalid and over-long memory changes without changing saved notes", () => {
    writeMemoryFile(BOT, "repeated repeated");
    for (const action of ["replace", "remove"] as const) {
      expect(updateMemory(BOT, { action, oldText: "repeated", ...(action === "replace" ? { text: "new" } : {}) }))
        .toMatchObject({ ok: false, code: "conflict" });
    }
    expect(updateMemory(BOT, { action: "append", text: " " })).toMatchObject({ ok: false, code: "invalid" });
    expect(updateMemory(BOT, { action: "replace", oldText: "", text: "replacement" })).toMatchObject({ ok: false, code: "invalid" });
    expect(updateMemory(BOT, { action: "remove", oldText: "repeated", text: "unexpected" })).toMatchObject({ ok: false, code: "invalid" });
    const long = updateMemory(BOT, { action: "append", text: "x".repeat(MEMORY_ENTRY_MAX_CHARS + 1) });
    expect(long).toMatchObject({ ok: false, code: "invalid" });
    expect(long.ok ? "" : long.error).toContain("1001 characters");
    expect(updateMemory(BOT, { action: "replace", oldText: "repeated repeated", text: "y".repeat(MEMORY_ENTRY_MAX_CHARS + 1) })).toMatchObject({ ok: false, code: "invalid" });
    const tall = updateMemory(BOT, { action: "append", text: `Snippet\n\`\`\`\n${"x\n".repeat(MEMORY_ENTRY_MAX_LINES - 2)}\`\`\`` });
    expect(tall).toMatchObject({ ok: false, code: "invalid" });
    expect(tall.ok ? "" : tall.error).toContain(`${MEMORY_ENTRY_MAX_LINES + 1} lines`);
    expect(readMemoryFile(BOT).text).toBe("repeated repeated");
    expect(updateMemory(BOT, { action: "append", text: "x".repeat(MEMORY_ENTRY_MAX_CHARS) })).toMatchObject({ ok: true });
  });

  const archive = () => readMemoryTopic(BOT, "archive.md") ?? "";
  const withinBudget = (text: string) => memoryLineCount(text) <= MEMORY_MAX_LINES && Buffer.byteLength(text, "utf8") <= MEMORY_MAX_BYTES;

  it("never refuses for size: a write to a full file moves the oldest entry to the archive, where search finds it", () => {
    const now = new Date(2026, 9, 3, 12);
    const entries = [
      "- 2026-09-01 · The staging host is kestrel",
      ...Array.from({ length: MEMORY_MAX_LINES - 2 }, (_, i) => `- 2026-09-02 · fact ${i}`),
    ];
    writeMemoryFile(BOT, `# Memory\n${entries.join("\n")}\n`);
    expect(memoryLineCount(readMemoryFile(BOT).text)).toBe(MEMORY_MAX_LINES);
    const saved = updateMemory(BOT, { action: "append", text: "one fact too many" }, { source: 'chat "Full"', now });
    expect(saved).toMatchObject({ ok: true, truncated: false, moved: ["- 2026-09-01 · The staging host is kestrel"] });
    const text = readMemoryFile(BOT).text;
    expect(withinBudget(text)).toBe(true);
    expect(text.startsWith("# Memory\n- 2026-09-02 · fact 0\n")).toBe(true);
    expect(text).toContain('- 2026-10-03 · from chat "Full" · one fact too many\n');
    expect(archive()).toBe("- 2026-09-01 · The staging host is kestrel · moved 2026-10-03\n");
    expect(archive()).toContain("- 2026-09-01 · The staging host is kestrel · moved 2026-10-03\n");
    expect(searchMemoryFiles(BOT, "kestrel")).toEqual([expect.objectContaining({ file: "memory/archive.md" })]);
    expect(loadMemory(BOT, { now })).toMatchObject({ truncated: false, lines: MEMORY_MAX_LINES });
    const big = Array.from({ length: 21 }, (_, i) => `- 2026-09-0${1 + (i % 9)} · big ${i} ${"x".repeat(1_150)}`);
    writeMemoryFile(BOT, `${big.join("\n")}\n`);
    expect(withinBudget(readMemoryFile(BOT).text)).toBe(false);
    const bytes = updateMemory(BOT, { action: "append", text: "small fact" }, { now });
    expect(bytes.ok && bytes.moved.length).toBeGreaterThan(0);
    expect(withinBudget(readMemoryFile(BOT).text)).toBe(true);
    for (const line of big) expect(readMemoryFile(BOT).text.includes(line) !== archive().includes(`${line} · moved`)).toBe(true);
  });

  it("moves struck lines first, then expired ones, then the oldest live ones, never a health fact or a hand-written line", () => {
    const now = new Date(2026, 9, 3, 12);
    const fillers = Array.from({ length: MEMORY_MAX_LINES - 5 }, (_, i) => `- 2026-09-25 · filler ${i}`);
    writeMemoryFile(BOT, [
      "# Memory",
      "- 2026-08-01 · The person is vegetarian",
      "- 2026-08-02 · old live fact",
      "- 2026-09-22 · exam on Friday · until 2026-09-26",
      "- 2026-09-24 · Office: Berlin",
      ...fillers,
    ].join("\n") + "\n");
    const moved = (result: ReturnType<typeof updateMemory>) => (result.ok ? result.moved : ["refused"]);
    expect(moved(updateMemory(BOT, { action: "supersede", oldText: "Office: Berlin", text: "Office: Rome" }, { now })))
      .toEqual(["- 2026-09-24 · ~~Office: Berlin~~ · superseded 2026-10-03"]);
    expect(moved(updateMemory(BOT, { action: "append", text: "fact a" }, { now }))).toEqual(["- 2026-09-22 · exam on Friday · until 2026-09-26"]);
    expect(moved(updateMemory(BOT, { action: "append", text: "fact b" }, { now }))).toEqual(["- 2026-08-02 · old live fact"]);
    expect(moved(updateMemory(BOT, { action: "append", text: "fact c" }, { now }))).toEqual(["- 2026-09-25 · filler 0"]);
    const text = readMemoryFile(BOT).text;
    expect(text.startsWith("# Memory\n- 2026-08-01 · The person is vegetarian\n")).toBe(true);
    expect(text).toContain("- 2026-10-03 · Office: Rome\n");
    expect(withinBudget(text)).toBe(true);
    expect(archive().split("\n").filter((line) => line.endsWith(" · moved 2026-10-03"))).toHaveLength(4);
  });

  it("moves an entry with a code block whole, never leaving its fence behind", () => {
    const now = new Date(2026, 9, 3, 12);
    const old = { now: new Date(2026, 0, 1, 12) };
    const filler = (count: number) => Array.from({ length: count }, (_, i) => `- 2026-09-02 · fact ${i}`);
    expect(updateMemory(BOT, { action: "append", text: "Deploy command:\n```\npnpm deploy --prod\n```" }, old)).toMatchObject({ ok: true });
    writeMemoryFile(BOT, `${readMemoryFile(BOT).text}${filler(MEMORY_MAX_LINES - 4).join("\n")}\n`);
    const saved = updateMemory(BOT, { action: "append", text: "one more" }, { now });
    expect(saved).toMatchObject({ ok: true, truncated: false, moved: ["- 2026-01-01 · Deploy command:\n  ```\n  pnpm deploy --prod\n  ```"] });
    expect(readMemoryFile(BOT).text.startsWith("- 2026-09-02 · fact 0\n")).toBe(true);
    expect(archive()).toBe("- 2026-01-01 · Deploy command: · moved 2026-10-03\n  ```\n  pnpm deploy --prod\n  ```\n");
    expect(searchMemoryFiles(BOT, "pnpm deploy")).toEqual([expect.objectContaining({ file: "memory/archive.md" })]);
    for (const legacy of [["- 2026-01-01 · Deploy command:", "```sh", "railway up", "```"], ["- 2026-01-01 · Snippet ```", "railway up", "```"]]) {
      writeMemoryFile(BOT, `${[...legacy, ...filler(MEMORY_MAX_LINES - legacy.length)].join("\n")}\n`);
      expect(updateMemory(BOT, { action: "append", text: "one more" }, { now })).toMatchObject({ ok: true, truncated: false, moved: [legacy.join("\n")] });
      expect(readMemoryFile(BOT).text).not.toContain("railway up");
      expect(withinBudget(readMemoryFile(BOT).text)).toBe(true);
    }
    rmSync(join(ensureWorkspace(BOT), "MEMORY.md"));
    for (let i = 0; i < 20; i += 1) {
      const block = `Snippet ${i}\n\`\`\`\n${"x\n".repeat(MEMORY_ENTRY_MAX_LINES - 3)}\`\`\``;
      expect(updateMemory(BOT, { action: "append", text: block }, { now })).toMatchObject({ ok: true, truncated: false });
    }
    expect(updateMemory(BOT, { action: "append", text: "the important later fact" }, { now })).toMatchObject({ ok: true, truncated: false });
    expect(loadMemory(BOT, { now })?.text).toContain("the important later fact");
    expect(withinBudget(readMemoryFile(BOT).text)).toBe(true);
  });

  it("saves the entry even when hand-written lines alone fill what loads, and says so", () => {
    const now = new Date(2026, 9, 3, 12);
    const hand = Array.from({ length: MEMORY_MAX_LINES }, (_, i) => `- note ${i}`);
    writeMemoryFile(BOT, `${hand.join("\n")}\n`);
    expect(updateMemory(BOT, { action: "append", text: "still saved" }, { now })).toMatchObject({ ok: true, truncated: true, moved: [] });
    expect(readMemoryFile(BOT).text).toBe(`${hand.join("\n")}\n- 2026-10-03 · still saved\n`);
    expect(archive()).toBe("");
    const dated = ["- 2026-09-01 · The staging host is kestrel", "- 2026-09-02 · Deploys go out on Tuesdays"];
    writeMemoryFile(BOT, `${[...dated, ...hand].join("\n")}\n`);
    expect(updateMemory(BOT, { action: "append", text: "also saved" }, { now })).toMatchObject({ ok: true, truncated: true, moved: [] });
    expect(readMemoryFile(BOT).text.startsWith(`${dated.join("\n")}\n`)).toBe(true);
    expect(archive()).toBe("");
  });

  it.each(["\n", "\r\n"])("keeps a newly appended %j code block when the remaining space is too small", (newline) => {
    const now = new Date(2026, 9, 3, 12);
    const hand = Array.from({ length: MEMORY_MAX_LINES - 1 }, (_, i) => `- project note ${i}`).join("\n") + "\n";
    writeMemoryFile(BOT, hand);
    const text = ["Deploy command:", "```sh", "pnpm deploy --prod", "```"].join(newline);
    expect(updateMemory(BOT, { action: "append", text }, { now }))
      .toMatchObject({ ok: true, truncated: true, moved: [] });
    const saved = readMemoryFile(BOT).text;
    expect(saved.startsWith(hand)).toBe(true);
    expect(saved).toContain("- 2026-10-03 · Deploy command:");
    expect(saved).toContain("  pnpm deploy --prod");
    expect(memoryLineCount(saved)).toBe(MEMORY_MAX_LINES + 3);
    expect(archive()).toBe("");
  });

  // Permissions do not stop root or Windows from reading the file.
  it.skipIf(process.platform === "win32" || process.getuid?.() === 0)("fails the write, changing nothing, when the archive exists but cannot be read", () => {
    const full = Array.from({ length: MEMORY_MAX_LINES }, (_, i) => `- 2026-09-01 · old ${i}\n`).join("");
    writeMemoryFile(BOT, full);
    writeMemoryTopic(BOT, "archive.md", "---\ntitle: Archive\n---\n- 2026-01-01 · kept for the record\n");
    const path = join(ensureWorkspace(BOT), "memory", "archive.md");
    chmodSync(path, 0o000);
    try {
      expect(() => updateMemory(BOT, { action: "append", text: "one more" })).toThrow();
    } finally {
      chmodSync(path, 0o600);
    }
    expect(readMemoryFile(BOT).text).toBe(full);
    expect(archive()).toBe("---\ntitle: Archive\n---\n- 2026-01-01 · kept for the record\n");
  });

  it("brings a file grown past the budget back within it on the next write, losing no line", () => {
    const now = new Date(2026, 9, 3, 12);
    const lines = Array.from({ length: MEMORY_MAX_LINES + 50 }, (_, i) => `- 2026-09-01 · fact ${i}`);
    writeMemoryFile(BOT, `${lines.join("\n")}\n`);
    const result = updateMemory(BOT, { action: "remove", oldText: "- 2026-09-01 · fact 7\n" }, { now });
    expect(result.ok && result.moved.length).toBe(49);
    const text = readMemoryFile(BOT).text;
    expect(memoryLineCount(text)).toBe(MEMORY_MAX_LINES);
    for (const line of lines.filter((line) => line !== "- 2026-09-01 · fact 7")) {
      expect(text.includes(`${line}\n`) !== archive().includes(`${line} · moved`), line).toBe(true);
    }
  });

  it("keeps every note when many threads write at once: none lost, none duplicated", async () => {
    const now = new Date(2026, 9, 3, 12);
    // a full file, so every write below has to make room
    writeMemoryFile(BOT, Array.from({ length: MEMORY_MAX_LINES }, (_, i) => `- 2026-09-01 · old ${i}\n`).join(""));
    const write = async (thread: string, i: number) => {
      await Promise.resolve();
      return updateMemory(BOT, { action: "append", text: `note ${thread}-${i}` }, { source: `thread ${thread}`, now });
    };
    const results = await Promise.all(Array.from({ length: 40 }, (_, i) => [write("a", i), write("b", i)]).flat());
    expect(results.every((result) => result.ok && result.moved.length === 1)).toBe(true);
    const text = readMemoryFile(BOT).text;
    expect(withinBudget(text)).toBe(true);
    for (const thread of ["a", "b"]) {
      for (let i = 0; i < 40; i += 1) {
        const line = `- 2026-10-03 · from thread ${thread} · note ${thread}-${i}`;
        expect([text.includes(`${line}\n`), archive().includes(`${line} · moved`)].filter(Boolean), line).toHaveLength(1);
      }
    }
  });

  it("states the load cut plainly for a hand-grown file, and tells a managed bot the file never fills up", () => {
    const dir = ensureWorkspace(BOT);
    const lines = Array.from({ length: MEMORY_MAX_LINES + 50 }, (_, i) => `- fact ${i}`);
    writeFileSync(join(dir, "MEMORY.md"), `${lines.join("\n")}\n`);
    const memory = loadMemory(BOT);
    expect(memory).toMatchObject({ truncated: true, lines: MEMORY_MAX_LINES + 50 });
    const managed = memorySystemPrompt(BOT, { managedWrites: true });
    expect(managed).toContain(`[MEMORY.md is ${MEMORY_MAX_LINES + 50} lines and ${memory!.bytes} bytes; only the first ${MEMORY_MAX_LINES} lines / ${MEMORY_MAX_BYTES} bytes are shown above.]`);
    expect(managed).toContain(`shown to you at the start of every session (up to ${MEMORY_MAX_LINES} lines / 24 KB).`);
    expect(managed).not.toContain("archive");
    expect(managed).not.toContain("Consolidate");
    expect(managed).not.toContain("short and curated");
    expect(memorySystemPrompt(BOT)).toContain("Keep MEMORY.md short and curated.");
    writeFileSync(join(dir, "MEMORY.md"), `${lines.slice(0, MEMORY_MAX_LINES).join("\n")}\n`);
    expect(loadMemory(BOT)?.truncated).toBe(false);
  });

  // A teammate's hand-off on a Cloud runs in a task folder, with its writes refused: this was the one prompt with no memory path.
  it("tells a turn whose notes would be refused where its topic notes are, with or without a MEMORY.md", () => {
    writeMemoryTopic(BOT, "regulars.md", "---\ntitle: Regulars\n---\n- Code MOSS-42\n");
    const folder = JSON.stringify(workspaceDir(BOT));
    const withoutMemory = memorySystemPrompt(BOT, { managedWrites: true, fileTools: true, writesRefused: true });
    expect(withoutMemory).toContain("- memory/regulars.md");
    expect(withoutMemory).toContain(`paths are relative to ${folder}, not your working folder`);
    writeMemoryFile(BOT, "- The owner likes figs.");
    const withMemory = memorySystemPrompt(BOT, { managedWrites: true, fileTools: true, writesRefused: true });
    expect(withMemory).toContain("- memory/regulars.md");
    expect(withMemory).toContain(`paths are relative to ${folder}, not your working folder`);
    // With no file tools it can't open a path, so it is told how to look instead.
    expect(memorySystemPrompt(BOT, { managedWrites: true, fileTools: false, writesRefused: true })).toContain("look it up with session_search");
  });

  it("tells a turn whose notes would be refused where facts go, and still shows it the memory", () => {
    const empty = memorySystemPrompt(BOT, { managedWrites: true, fileTools: true, writesRefused: true });
    expect(empty).toContain("Notes cannot be saved from this conversation");
    expect(empty).toContain("in your result to that bot");
    writeMemoryFile(BOT, "- The owner likes figs.");
    const refused = memorySystemPrompt(BOT, { managedWrites: true, fileTools: true, writesRefused: true });
    expect(refused).toContain("The owner likes figs.");
    expect(refused).not.toContain("Change MEMORY.md only with memory_update");
    expect(refused).not.toContain("Record only facts");
    expect(memorySystemPrompt(BOT, { managedWrites: true, fileTools: true })).toContain("Change MEMORY.md only with memory_update");
    // asked to remember, it saves at once and says so; asked to forget, it forgets everywhere
    expect(memorySystemPrompt(BOT, { managedWrites: true, fileTools: true })).toContain(REMEMBER_AND_FORGET);
    expect(refused).not.toContain(REMEMBER_AND_FORGET);
    expect(REMEMBER_AND_FORGET).toContain("save it now with memory_update append");
    expect(REMEMBER_AND_FORGET).toContain("call memory_update with action forget");
    // a new instruction that changes an older note replaces it, so memory never keeps both
    expect(REMEMBER_AND_FORGET).toContain("use memory_update supersede on that old entry instead of append");
    expect(REMEMBER_AND_FORGET).toContain("carrying into the new text whatever in the old entry still holds");
    expect(REMEMBER_AND_FORGET).toContain("pass its until date again");
    // a preference for all their bots is saved once, never passed on to each bot
    expect(REMEMBER_AND_FORGET).toContain("saved once with memory_update action prefer");
    expect(REMEMBER_AND_FORGET).toContain("Never pass it on to each bot.");
  });

  it("requires explicit remove to delete a memory passage", () => {
    writeMemoryFile(BOT, "Keep this unique fact.");
    for (const text of ["", " \n\t "]) {
      expect(updateMemory(BOT, { action: "replace", oldText: "unique fact", text }))
        .toMatchObject({ ok: false, code: "invalid" });
      expect(readMemoryFile(BOT).text).toBe("Keep this unique fact.");
    }
    expect(updateMemory(BOT, { action: "remove", oldText: "unique fact" })).toMatchObject({ ok: true });
    expect(readMemoryFile(BOT).text).toBe("Keep this .");
  });

  it("redacts secrets on every server-side memory write, tool and editor alike", () => {
    const key = `sk-ant-api03-${"a".repeat(40)}`;
    const now = new Date(2026, 8, 10, 12);
    const appended = updateMemory(BOT, { action: "append", text: `Anthropic key is ${key}, call with Bearer ${"b".repeat(32)}` }, { source: 'chat "Keys"', now });
    expect(appended.ok).toBe(true);
    const entry = appended.ok ? appended.entry! : "";
    expect(entry).not.toContain(key);
    expect(entry).not.toContain("b".repeat(32));
    expect(entry).toContain("«redacted");
    expect(readMemoryFile(BOT).text).toBe(`${entry}\n`);
    expect(readMemoryFile(BOT).text).toContain('- 2026-09-10 · from chat "Keys" · Anthropic key is «redacted');
    expect(updateMemory(BOT, { action: "replace", oldText: "Anthropic key", text: `Anthropic key ${key} still` }, { now })).toMatchObject({ ok: true });
    expect(readMemoryFile(BOT).text).not.toContain(key);
    writeMemoryFile(BOT, `# Memory\n- token: ghp_${"c".repeat(36)}\n`);
    expect(readMemoryFile(BOT).text).not.toContain("c".repeat(36));
    expect(readMemoryFile(BOT).text).toContain("«redacted");
  });

  it("appends timestamped lines to today's log file, scrubbed, private, and outside the prompt", () => {
    const now = new Date(2026, 8, 10, 14, 3);
    const first = appendMemoryLog(BOT, "shipped 0.1.70 with token ghp_" + "d".repeat(36), { source: 'chat "Deploy"', now });
    expect(first).toMatchObject({ ok: true, file: "memory/log/2026-09-10.md" });
    expect(first.ok ? first.line : "").toBe('- 14:03 · from chat "Deploy" · shipped 0.1.70 with token «redacted 40 chars»');
    expect(appendMemoryLog(BOT, "rollback\ndone", { now: new Date(2026, 8, 10, 15, 30) }).ok).toBe(true);
    expect(readMemoryLog(BOT, "2026-09-10.md")).toBe(
      '- 14:03 · from chat "Deploy" · shipped 0.1.70 with token «redacted 40 chars»\n- 15:30 · rollback done\n',
    );
    appendMemoryLog(BOT, "next day", { now: new Date(2026, 8, 11, 9, 0) });
    expect(listMemoryLogs(BOT)).toEqual(["2026-09-10.md", "2026-09-11.md"]);
    const dir = workspaceDir(BOT);
    if (process.platform !== "win32") {
      expect(statSync(join(dir, "memory", "log")).mode & 0o777).toBe(0o700);
      expect(statSync(join(dir, "memory", "log", "2026-09-10.md")).mode & 0o777).toBe(0o600);
    }
    expect(listMemoryTopics(BOT)).toEqual([]);
    expect(loadMemory(BOT)).toBeNull();
    expect(memorySystemPrompt(BOT)).not.toContain("shipped 0.1.70");
    expect(appendMemoryLog(BOT, "  ")).toMatchObject({ ok: false, code: "invalid" });
    expect(readMemoryLog(BOT, "../MEMORY.md")).toBeNull();
    expect(readMemoryLog("never-ran", "2026-09-10.md")).toBeNull();
  });

  it("makes every memory file searchable for its own bot only, in step with server writes and hand edits", () => {
    const now = new Date(2026, 8, 10, 12);
    updateMemory(BOT, { action: "append", text: "The site audit covers broken links monthly" }, { source: 'chat "Audit"', now });
    appendMemoryLog(BOT, "audit run, three broken links found", { now });
    writeMemoryTopic(BOT, "deploys.md", "railway up from main\n");
    expect(recallMemory("broken links audit", BOT).map((hit) => hit.file).sort()).toEqual(["MEMORY.md", "memory/log/2026-09-10.md"]);
    expect(recallMemory("railway", BOT).map((hit) => hit.file)).toEqual(["memory/deploys.md"]);
    updateMemory("other-bot", { action: "append", text: "broken links audit belongs to the other bot" }, { now });
    const hits = searchMemoryFiles(BOT, "broken links audit");
    expect(hits.map((hit) => hit.file).sort()).toEqual(["MEMORY.md", "memory/log/2026-09-10.md"]);
    expect(hits.find((hit) => hit.file === "MEMORY.md")?.snippet).toContain("The site [audit] covers [broken] [links]");
    expect(searchMemoryFiles(BOT, "other bot")).toEqual([]);
    expect(searchMemoryFiles("other-bot", "broken links").map((hit) => hit.file)).toEqual(["MEMORY.md"]);
    expect(searchMemoryFiles(BOT, "railway").map((hit) => hit.file)).toEqual(["memory/deploys.md"]);
    const dir = workspaceDir(BOT);
    writeFileSync(join(dir, "memory", "deploys.md"), "fly deploy from main, railway retired\n");
    writeFileSync(join(dir, "memory", "hosting.md"), "hand-written topic about railway\n");
    expect(searchMemoryFiles(BOT, "fly deploy").map((hit) => hit.file)).toEqual(["memory/deploys.md"]);
    expect(searchMemoryFiles(BOT, "railway").map((hit) => hit.file).sort()).toEqual(["memory/deploys.md", "memory/hosting.md"]);
    rmSync(join(dir, "memory", "hosting.md"));
    writeFileSync(join(dir, "MEMORY.md"), readFileSync(join(dir, "MEMORY.md"), "utf8").replace(/.*audit.*\n/, ""));
    syncMemoryIndex(BOT);
    expect(searchMemoryFiles(BOT, "hand-written")).toEqual([]);
    expect(searchMemoryFiles(BOT, "broken links audit").map((hit) => hit.file)).toEqual(["memory/log/2026-09-10.md"]);
    rmSync(join(dir, "MEMORY.md"));
    ensureWorkspace(BOT);
    expect(searchMemoryFiles(BOT, "durable notes")).toEqual([]);
    expect(searchMemoryFiles("never-ran", "anything")).toEqual([]);
  });

  it("writeMemoryTopic keeps the name gate, the scrub and the modes", () => {
    expect(() => writeMemoryTopic(BOT, "../MEMORY.md", "x")).toThrow("invalid topic name");
    writeMemoryTopic(BOT, "keys.md", `token: xoxb-${"e".repeat(30)}\n`);
    expect(readMemoryTopic(BOT, "keys.md")).toContain("«redacted");
    expect(readMemoryTopic(BOT, "keys.md")).not.toContain("e".repeat(30));
    if (process.platform !== "win32") expect(statSync(join(workspaceDir(BOT), "memory", "keys.md")).mode & 0o777).toBe(0o600);
  });

  it("accepts plain single-segment topic names and nothing else", () => {
    for (const good of ["deploys.md", "a.md", "my notes.md", "v1.2-rc.md", "under_score.md", "café.md", "旅行.md"]) {
      expect(isMemoryTopicName(good), good).toBe(true);
    }
    for (const bad of [
      "",
      "no-extension",
      "notes.MD",
      ".hidden.md",
      "..md",
      "../x.md",
      "..%2F..%2Fsecret.md",
      "a/b.md",
      "a\\b.md",
      "\u0000.md",
      `${"a".repeat(300)}.md`,
    ]) {
      expect(isMemoryTopicName(bad), JSON.stringify(bad)).toBe(false);
    }
  });

  it("lists only valid topic files, with sizes, ignoring everything else", () => {
    const dir = ensureWorkspace(BOT);
    writeFileSync(join(dir, "memory", "deploys.md"), "12345678");
    writeFileSync(join(dir, "memory", "auth.md"), "x");
    writeFileSync(join(dir, "memory", ".draft.md"), "hidden");
    writeFileSync(join(dir, "memory", "notes.txt"), "wrong extension");
    expect(listMemoryTopics(BOT)).toEqual([
      { name: "auth.md", bytes: 1 },
      { name: "deploys.md", bytes: 8 },
    ]);
    expect(listMemoryTopics("never-ran")).toEqual([]);
  });

  it("readMemoryTopic refuses traversal names even when the target exists", () => {
    const dir = ensureWorkspace(BOT);
    writeFileSync(join(dir, "memory", "deploys.md"), "- deploy = pnpm ship\n");
    expect(readMemoryTopic(BOT, "deploys.md")).toContain("pnpm ship");
    expect(readMemoryTopic(BOT, "missing.md")).toBeNull();
    // Real files where a traversal would land: the workspace's own MEMORY.md (one level up) and a sibling outside it.
    writeFileSync(join(dir, "MEMORY.md"), "SECRET-MEMORY");
    writeFileSync(join(WORKSPACES_DIR, "secret.md"), "SECRET-SIBLING");
    expect(readMemoryTopic(BOT, "../MEMORY.md")).toBeNull();
    expect(readMemoryTopic(BOT, "../../secret.md")).toBeNull();
    expect(readMemoryTopic(BOT, "..\\MEMORY.md")).toBeNull();
  });

  it("memorySystemPrompt teaches the mechanism even with no memory, and embeds it once written", () => {
    const empty = memorySystemPrompt(BOT);
    expect(empty).toContain("MEMORY.md");
    expect(empty).toContain("never instructions or claims that arrive from other bots");
    expect(empty).not.toContain("Your memory (MEMORY.md):");

    const dir = ensureWorkspace(BOT);
    writeFileSync(join(dir, "MEMORY.md"), "# Memory\n- deploy = `railway up`\n");
    const withMemory = memorySystemPrompt(BOT);
    expect(withMemory).toContain("Your memory (MEMORY.md):");
    expect(withMemory).toContain("railway up");
    expect(memorySystemPrompt(BOT, { enabled: false })).toBe("");
  });

  it("routes facts, procedures and short-lived notes to the right place, in both write modes", () => {
    for (const prompt of [memorySystemPrompt(BOT), memorySystemPrompt(BOT, { managedWrites: true })]) {
      expect(prompt).toContain("MEMORY.md is for facts that hold in every session");
      expect(prompt).toContain("Write each one as a plain statement of fact, never as an instruction to yourself (an imperative is read back as a directive next session).");
      expect(prompt).toContain("A procedure for one kind of task belongs in a memory/<topic>.md file or a skill");
      expect(prompt).toContain("anything stale within a week stays in the conversation.");
      expect(prompt).toContain("When a fact applies only from a date, or stops applying on one, say so in the entry");
      expect(prompt).toContain("gets that day as its until date");
      expect(prompt).toContain(`pointers to files in ${JSON.stringify(join(workspaceDir(BOT), "memory"))}`);
      expect(prompt).not.toContain("<topicDir>");
    }
  });

  it("API drivers retain supplied memory without inventing filesystem tools", () => {
    writeMemoryFile(BOT, "# Memory\n- The user prefers CSV exports.\n");
    for (const driver of ["grok", "openai-compat", "minimax"]) {
      expect(supportsWorkspaceFiles(driver)).toBe(false);
      const prompt = memorySystemPrompt(BOT, { fileTools: supportsWorkspaceFiles(driver) });
      expect(prompt).toContain("The user prefers CSV exports.");
      expect(prompt).not.toContain("update it with your file tools");
      expect(prompt).not.toContain("memory_update");
    }
    expect(supportsWorkspaceFiles("claudeAgent")).toBe(true);
    expect(supportsWorkspaceFiles("codex")).toBe(true);
  });

  it("agents MCP enables targeted memory writes without promising native file reads", () => {
    writeMemoryFile(BOT, "# Memory\n- The user prefers CSV exports.\n");
    const prompt = memorySystemPrompt(BOT, { managedWrites: true, fileTools: false });
    expect(prompt).toContain("The user prefers CSV exports.");
    expect(prompt).toContain("Change MEMORY.md only with memory_update; on a conflict, find the current passage with session_search.");
    expect(prompt).not.toContain("update it with your file tools");
  });

  // The rules sit in the cached half of a turn's system prompt and the notes in its volatile half: the rules must hold
  // nothing that changes from turn to turn, and the two together are what memorySystemPrompt says (which, in one piece,
  // leaves a refused turn's rules out).
  it("splits into standing rules and what memory holds now, which together are the whole memory prompt", () => {
    writeMemoryTopic(BOT, "trip.md", "---\ntitle: Trip\n---\n- Flies Friday\n");
    writeMemoryFile(BOT, "- The owner likes figs.");
    for (const opts of [{}, { managedWrites: true }, { managedWrites: true, fileTools: false }, { fileTools: false }, { managedWrites: true, writesRefused: true }, { enabled: false }] as Array<{ writesRefused?: boolean }>) {
      const rules = memoryRulesPrompt(BOT, opts), notes = memoryNotesPrompt(BOT, opts);
      expect((opts.writesRefused ? "" : rules) + notes, JSON.stringify(opts)).toBe(memorySystemPrompt(BOT, opts));
      expect(rules, JSON.stringify(opts)).not.toMatch(/figs|Flies Friday|trip\.md|Your memory \(MEMORY\.md\)/);
    }
    expect(memoryRulesPrompt(BOT, { managedWrites: true })).toContain("Change MEMORY.md only with memory_update.");
    expect(memoryNotesPrompt(BOT, { managedWrites: true })).toContain("The owner likes figs.");
    expect(memoryNotesPrompt(BOT, { managedWrites: true })).toContain("- memory/trip.md");
    // A refusal can change from turn to turn, so it never changes the cached rules: the turn's notes override them.
    expect(memoryRulesPrompt(BOT, { managedWrites: true, writesRefused: true })).toBe(memoryRulesPrompt(BOT, { managedWrites: true }));
    expect(memoryNotesPrompt(BOT, { managedWrites: true, writesRefused: true })).toContain("Notes cannot be saved from this conversation");
    expect(memoryNotesPrompt(BOT, { managedWrites: true, writesRefused: true })).toContain("Whatever your memory instructions say, do not call memory_update");
    // With no way to write, the saved memory is context only, all of it in the notes.
    expect(memoryRulesPrompt(BOT, { fileTools: false })).toBe("");
    expect(memoryNotesPrompt(BOT, { fileTools: false })).toContain("this turn has no memory editing tools");
  });

  it("opts concurrent agents into targeted memory updates while retaining legacy guidance", () => {
    const managed = memorySystemPrompt(BOT, { managedWrites: true });
    expect(managed).toContain("Change MEMORY.md only with memory_update.");
    expect(managed).not.toContain("update it with your file tools");
    expect(memorySystemPrompt(BOT)).toContain("update it with your file tools");
  });
});

describe("writeMemoryFile atomicity", () => {
  it("replaces MEMORY.md in one step, keeps 0600, and leaves no temp sibling behind", () => {
    const botId = "atomic-bot";
    const dir = ensureWorkspace(botId);
    writeMemoryFile(botId, "# Memory\n\n- first");
    writeMemoryFile(botId, "# Memory\n\n- second, longer than the first write was");
    expect(readFileSync(join(dir, "MEMORY.md"), "utf8")).toBe("# Memory\n\n- second, longer than the first write was");
    // POSIX mode bits are not a thing on Windows: its ACLs report 0o666.
    if (process.platform !== "win32") expect(statSync(join(dir, "MEMORY.md")).mode & 0o777).toBe(0o600);
    expect(readdirSync(dir).filter((name) => name.endsWith(".tmp"))).toEqual([]);
  });
});