Skip to content

#1 Persistent memory for AI coding agents based on real-world benchmarks

18.5 KB
Raw Blame
import { describe, it, expect, beforeEach, vi } from "vitest";
import { registerContextFunction } from "../src/functions/context.js";
import {
  addSessionToProjectIndex,
  removeSessionFromProjectIndex,
  getProjectSessionIndex,
  buildProjectSessionIndex,
  ensureProjectSessionIndex,
  rebuildAllProjectSessionIndexes,
  rebuildSessionIndexIfStale,
} from "../src/state/session-index.js";
import { KV } from "../src/state/schema.js";
import type { Session } from "../src/types.js";

function mockKV(listFailures: Set<string> = new Set()) {
  const store = new Map<string, Map<string, unknown>>();
  return {
    get: vi.fn(async <T>(scope: string, key: string): Promise<T | null> => {
      return (store.get(scope)?.get(key) as T) ?? null;
    }),
    set: vi.fn(async <T>(scope: string, key: string, data: T): Promise<T> => {
      if (!store.has(scope)) store.set(scope, new Map());
      store.get(scope)!.set(key, data);
      return data;
    }),
    delete: vi.fn(async (scope: string, key: string): Promise<void> => {
      store.get(scope)?.delete(key);
    }),
    list: vi.fn(async <T>(scope: string): Promise<T[]> => {
      if (listFailures.has(scope)) {
        throw new Error(`list failed for ${scope}`);
      }
      if (!store.has(scope)) return [];
      return Array.from(store.get(scope)!.values()) as T[];
    }),
  };
}

type ContextHandler = (data: {
  sessionId: string;
  project: string;
  budget?: number;
}) => Promise<{ context: string; blocks: number; tokens: number }>;

function wireContext(kv: ReturnType<typeof mockKV>, budget = 4000) {
  let handler: ContextHandler | undefined;
  const sdk = {
    registerFunction: vi.fn((id: string, cb: ContextHandler) => {
      if (id === "mem::context") handler = cb;
    }),
  } as unknown as import("iii-sdk").IIIClient;
  registerContextFunction(sdk, kv as never, budget);
  if (!handler) throw new Error("mem::context not registered");
  return handler;
}

function makeSession(over: Partial<Session> = {}): Session {
  return {
    id: over.id ?? `ses_${Math.random().toString(36).slice(2)}`,
    project: over.project ?? "/tmp/proj",
    cwd: over.cwd ?? "/tmp/proj",
    startedAt: over.startedAt ?? new Date().toISOString(),
    status: over.status ?? "completed",
    observationCount: over.observationCount ?? 0,
    agentId: over.agentId,
  };
}

describe("project session index — maintenance (kv-access finding 3)", () => {
  let kv: ReturnType<typeof mockKV>;

  beforeEach(() => {
    kv = mockKV();
  });

  it("addSessionToProjectIndex creates the index for a new project", async () => {
    await addSessionToProjectIndex(kv as never, "proj-a", {
      id: "ses_1",
      startedAt: "2026-01-01T00:00:00Z",
    });

    const index = await getProjectSessionIndex(kv as never, "proj-a");
    expect(index).toEqual([{ id: "ses_1", startedAt: "2026-01-01T00:00:00Z" }]);
  });

  it("drops the project index when an add cannot be written, so the next read rebuilds it", async () => {
    await addSessionToProjectIndex(kv as never, "proj-a", {
      id: "ses_1",
      startedAt: "2026-01-01T00:00:00Z",
    });
    kv.set.mockRejectedValueOnce(new Error("write failed"));

    await expect(
      addSessionToProjectIndex(kv as never, "proj-a", {
        id: "ses_2",
        startedAt: "2026-01-02T00:00:00Z",
      }),
    ).rejects.toThrow("write failed");

    expect(await getProjectSessionIndex(kv as never, "proj-a")).toBeNull();
  });

  it("keeps entries sorted most-recent-first and dedups by id", async () => {
    await addSessionToProjectIndex(kv as never, "proj-a", {
      id: "ses_1",
      startedAt: "2026-01-01T00:00:00Z",
    });
    await addSessionToProjectIndex(kv as never, "proj-a", {
      id: "ses_2",
      startedAt: "2026-01-03T00:00:00Z",
    });
    await addSessionToProjectIndex(kv as never, "proj-a", {
      id: "ses_1",
      startedAt: "2026-01-05T00:00:00Z",
    });

    const index = await getProjectSessionIndex(kv as never, "proj-a");
    expect(index?.map((e) => e.id)).toEqual(["ses_1", "ses_2"]);
    expect(index?.length).toBe(2);
  });

  it("caps the index at 50 entries, dropping the oldest", async () => {
    for (let i = 0; i < 55; i++) {
      await addSessionToProjectIndex(kv as never, "proj-cap", {
        id: `ses_${i}`,
        startedAt: new Date(2026, 0, 1, 0, 0, i).toISOString(),
      });
    }

    const index = await getProjectSessionIndex(kv as never, "proj-cap");
    expect(index?.length).toBe(50);
    expect(index?.map((e) => e.id)).not.toContain("ses_0");
    expect(index?.map((e) => e.id)).not.toContain("ses_4");
    expect(index?.map((e) => e.id)).toContain("ses_54");
  });

  it("removeSessionFromProjectIndex drops the entry", async () => {
    await addSessionToProjectIndex(kv as never, "proj-a", {
      id: "ses_1",
      startedAt: "2026-01-01T00:00:00Z",
    });
    await addSessionToProjectIndex(kv as never, "proj-a", {
      id: "ses_2",
      startedAt: "2026-01-02T00:00:00Z",
    });

    await removeSessionFromProjectIndex(kv as never, "proj-a", "ses_1");

    const index = await getProjectSessionIndex(kv as never, "proj-a");
    expect(index?.map((e) => e.id)).toEqual(["ses_2"]);
  });

  it("removeSessionFromProjectIndex is a no-op when no index exists yet", async () => {
    await expect(
      removeSessionFromProjectIndex(kv as never, "proj-never-seen", "ses_x"),
    ).resolves.toBeUndefined();
    expect(await getProjectSessionIndex(kv as never, "proj-never-seen")).toBeNull();
  });

  it("buildProjectSessionIndex sorts and caps a raw session list", () => {
    const entries = buildProjectSessionIndex(
      Array.from({ length: 5 }, (_, i) => ({
        id: `ses_${i}`,
        startedAt: new Date(2026, 0, i + 1).toISOString(),
      })),
    );
    expect(entries.map((e) => e.id)).toEqual([
      "ses_4",
      "ses_3",
      "ses_2",
      "ses_1",
      "ses_0",
    ]);
  });

  it("addSessionToProjectIndex builds the initial index from stored sessions, not just the new entry", async () => {
    await kv.set(KV.sessions, "ses_old_1", {
      id: "ses_old_1",
      project: "proj-cold",
      startedAt: "2026-01-01T00:00:00Z",
    });
    await kv.set(KV.sessions, "ses_old_2", {
      id: "ses_old_2",
      project: "proj-cold",
      startedAt: "2026-01-02T00:00:00Z",
    });
    await kv.set(KV.sessions, "ses_other_project", {
      id: "ses_other_project",
      project: "proj-unrelated",
      startedAt: "2026-01-03T00:00:00Z",
    });

    await addSessionToProjectIndex(kv as never, "proj-cold", {
      id: "ses_new",
      startedAt: "2026-01-04T00:00:00Z",
    });

    const index = await getProjectSessionIndex(kv as never, "proj-cold");
    expect(index?.map((e) => e.id).sort()).toEqual([
      "ses_new",
      "ses_old_1",
      "ses_old_2",
    ]);
  });

  it("ensureProjectSessionIndex recheck-under-lock never discards a concurrently added session", async () => {
    await addSessionToProjectIndex(kv as never, "proj-race", {
      id: "ses_concurrent",
      startedAt: "2026-01-02T00:00:00Z",
    });

    const stale = await ensureProjectSessionIndex(kv as never, "proj-race", [
      { id: "ses_stale_scan", startedAt: "2026-01-01T00:00:00Z" },
    ]);

    expect(stale.map((e) => e.id)).toEqual(["ses_concurrent"]);
    const index = await getProjectSessionIndex(kv as never, "proj-race");
    expect(index?.map((e) => e.id)).toEqual(["ses_concurrent"]);
  });

  it("keeps a low-volume agent's sessions in the index alongside a high-volume agent", async () => {
    for (let i = 0; i < 60; i++) {
      await addSessionToProjectIndex(kv as never, "proj-fair", {
        id: `busy_${i}`,
        startedAt: new Date(2026, 0, 1, 0, 0, i).toISOString(),
        agentId: "agent-busy",
      });
    }
    for (let i = 0; i < 3; i++) {
      await addSessionToProjectIndex(kv as never, "proj-fair", {
        id: `quiet_${i}`,
        startedAt: new Date(2025, 0, 1, 0, 0, i).toISOString(),
        agentId: "agent-quiet",
      });
    }

    const index = await getProjectSessionIndex(kv as never, "proj-fair");
    expect(index?.length).toBe(50);
    const quietIds = index?.filter((e) => e.agentId === "agent-quiet").map((e) => e.id);
    expect(quietIds?.sort()).toEqual(["quiet_0", "quiet_1", "quiet_2"]);
  });

  it("drops the entry at the cap boundary without rescanning stored sessions", async () => {
    for (let i = 0; i < 51; i++) {
      const id = `ses_${i}`;
      const startedAt = new Date(2026, 0, 1, 0, 0, i).toISOString();
      await kv.set(KV.sessions, id, { id, project: "proj-replenish", startedAt });
      await addSessionToProjectIndex(kv as never, "proj-replenish", { id, startedAt });
    }

    const before = await getProjectSessionIndex(kv as never, "proj-replenish");
    expect(before?.length).toBe(50);
    expect(before?.map((e) => e.id)).not.toContain("ses_0");

    const newest = before![0].id;
    kv.list.mockClear();
    await removeSessionFromProjectIndex(kv as never, "proj-replenish", newest);

    expect(kv.list.mock.calls.filter((c) => c[0] === KV.sessions)).toHaveLength(0);
    const after = await getProjectSessionIndex(kv as never, "proj-replenish");
    expect(after?.length).toBe(49);
    expect(after?.map((e) => e.id)).not.toContain("ses_0");
    expect(after?.map((e) => e.id)).not.toContain(newest);
  });

  it("does not rescan stored sessions when the index was below the cap", async () => {
    for (let i = 0; i < 5; i++) {
      const id = `small_${i}`;
      const startedAt = new Date(2026, 0, 1, 0, 0, i).toISOString();
      await kv.set(KV.sessions, id, { id, project: "proj-small", startedAt });
      await addSessionToProjectIndex(kv as never, "proj-small", { id, startedAt });
    }
    kv.list.mockClear();
    await removeSessionFromProjectIndex(kv as never, "proj-small", "small_4");
    expect(kv.list.mock.calls.filter((c) => c[0] === KV.sessions)).toHaveLength(0);
    const after = await getProjectSessionIndex(kv as never, "proj-small");
    expect(after?.map((e) => e.id)).toEqual(["small_3", "small_2", "small_1", "small_0"]);
  });

  it("removing 100 sessions one at a time never lists the sessions scope", async () => {
    const ids: string[] = [];
    for (let i = 0; i < 100; i++) {
      const id = `bulk_${i}`;
      const startedAt = new Date(2026, 0, 1, 0, 0, i).toISOString();
      ids.push(id);
      await kv.set(KV.sessions, id, { id, project: "proj-bulk", startedAt });
      await addSessionToProjectIndex(kv as never, "proj-bulk", { id, startedAt });
    }
    kv.list.mockClear();

    for (const id of ids) {
      await removeSessionFromProjectIndex(kv as never, "proj-bulk", id);
    }

    expect(kv.list.mock.calls.filter((c) => c[0] === KV.sessions)).toHaveLength(0);
    const after = await getProjectSessionIndex(kv as never, "proj-bulk");
    expect(after).toEqual([]);
  });

  it("does not persist an index when the cold-seed list fails", async () => {
    const failingKv = mockKV(new Set([KV.sessions]));

    await expect(
      addSessionToProjectIndex(failingKv as never, "proj-fail", {
        id: "ses_new",
        startedAt: "2026-01-01T00:00:00Z",
      }),
    ).rejects.toThrow();

    expect(await getProjectSessionIndex(failingKv as never, "proj-fail")).toBeNull();
    expect(
      failingKv.set.mock.calls.some(([scope]) => scope === KV.projectSessionsIndex),
    ).toBe(false);
  });
});

describe("rebuildAllProjectSessionIndexes / rebuildSessionIndexIfStale", () => {
  let kv: ReturnType<typeof mockKV>;

  beforeEach(() => {
    kv = mockKV();
  });

  it("rebuilds every project's index from a single sessions listing", async () => {
    await kv.set(KV.sessions, "a1", { id: "a1", project: "proj-a", startedAt: "2026-01-01T00:00:00Z" });
    await kv.set(KV.sessions, "a2", { id: "a2", project: "proj-a", startedAt: "2026-01-02T00:00:00Z" });
    await kv.set(KV.sessions, "b1", { id: "b1", project: "proj-b", startedAt: "2026-01-01T00:00:00Z" });
    kv.list.mockClear();

    const result = await rebuildAllProjectSessionIndexes(kv as never);

    expect(result).toEqual({ projects: 2, sessions: 3 });
    expect(kv.list.mock.calls.filter((c) => c[0] === KV.sessions)).toHaveLength(1);

    const indexA = await getProjectSessionIndex(kv as never, "proj-a");
    expect(indexA?.map((e) => e.id).sort()).toEqual(["a1", "a2"]);
    const indexB = await getProjectSessionIndex(kv as never, "proj-b");
    expect(indexB?.map((e) => e.id)).toEqual(["b1"]);
  });

  it("rejects and writes nothing when the sessions listing fails", async () => {
    const failingKv = mockKV(new Set([KV.sessions]));

    await expect(rebuildAllProjectSessionIndexes(failingKv as never)).rejects.toThrow();
    expect(
      failingKv.set.mock.calls.some(([scope]) => scope === KV.projectSessionsIndex),
    ).toBe(false);
  });

  it("rebuilds once per generation and skips a matching marker", async () => {
    await kv.set(KV.sessions, "a1", { id: "a1", project: "proj-a", startedAt: "2026-01-01T00:00:00Z" });

    const first = await rebuildSessionIndexIfStale(kv as never);
    expect(first).toEqual({ projects: 1, sessions: 1 });

    kv.list.mockClear();
    const second = await rebuildSessionIndexIfStale(kv as never);
    expect(second).toBeNull();
    expect(kv.list.mock.calls.filter((c) => c[0] === KV.sessions)).toHaveLength(0);
  });

  function gateNextSessionsListing(target: ReturnType<typeof mockKV>): () => void {
    const listAll = target.list.getMockImplementation()!;
    let release!: () => void;
    const gate = new Promise<void>((resolve) => {
      release = resolve;
    });
    target.list.mockImplementationOnce(async (scope: string) => {
      const snapshot = await listAll(scope);
      await gate;
      return snapshot;
    });
    return release;
  }

  it("keeps a session added to the index while the rebuild was listing", async () => {
    const a1 = { id: "a1", project: "proj-a", startedAt: "2026-01-01T00:00:00Z" };
    await kv.set(KV.sessions, "a1", a1);
    await kv.set(KV.projectSessionsIndex, "proj-a", [{ id: "a1", startedAt: a1.startedAt }]);
    const release = gateNextSessionsListing(kv);

    const rebuild = rebuildAllProjectSessionIndexes(kv as never);
    const a2 = { id: "a2", project: "proj-a", startedAt: "2026-01-02T00:00:00Z" };
    await kv.set(KV.sessions, "a2", a2);
    await addSessionToProjectIndex(kv as never, "proj-a", { id: "a2", startedAt: a2.startedAt });
    release();
    await rebuild;

    const index = await getProjectSessionIndex(kv as never, "proj-a");
    expect(index?.map((e) => e.id).sort()).toEqual(["a1", "a2"]);
  });

  it("does not restore a session removed from the index while the rebuild was listing", async () => {
    const a1 = { id: "a1", project: "proj-a", startedAt: "2026-01-01T00:00:00Z" };
    const a2 = { id: "a2", project: "proj-a", startedAt: "2026-01-02T00:00:00Z" };
    await kv.set(KV.sessions, "a1", a1);
    await kv.set(KV.sessions, "a2", a2);
    await kv.set(KV.projectSessionsIndex, "proj-a", [
      { id: "a2", startedAt: a2.startedAt },
      { id: "a1", startedAt: a1.startedAt },
    ]);
    const release = gateNextSessionsListing(kv);

    const rebuild = rebuildAllProjectSessionIndexes(kv as never);
    await kv.delete(KV.sessions, "a2");
    await removeSessionFromProjectIndex(kv as never, "proj-a", "a2");
    release();
    await rebuild;

    const index = await getProjectSessionIndex(kv as never, "proj-a");
    expect(index?.map((e) => e.id)).toEqual(["a1"]);
  });

  it("does not set the generation marker when the listing fails", async () => {
    const failingKv = mockKV(new Set([KV.sessions]));

    await expect(rebuildSessionIndexIfStale(failingKv as never)).rejects.toThrow();
    expect(
      failingKv.set.mock.calls.some(([scope]) => scope === KV.config),
    ).toBe(false);
  });
});

describe("mem::context — uses the project session index (kv-access finding 3)", () => {
  let kv: ReturnType<typeof mockKV>;
  let handler: ContextHandler;

  beforeEach(() => {
    kv = mockKV();
    handler = wireContext(kv);
  });

  it("does not call kv.list(KV.sessions) when the project index already exists", async () => {
    const other = makeSession({
      id: "ses_other",
      project: "/tmp/proj",
      startedAt: "2026-01-01T00:00:00Z",
    });
    await kv.set(KV.sessions, other.id, other);
    await addSessionToProjectIndex(kv as never, "/tmp/proj", {
      id: other.id,
      startedAt: other.startedAt,
    });
    kv.list.mockClear();

    const result = await handler({
      sessionId: "ses_new",
      project: "/tmp/proj",
    });

    expect(
      kv.list.mock.calls.some(([scope]) => scope === KV.sessions),
    ).toBe(false);
    expect(result).toBeDefined();
  });

  it("falls back to a full list and backfills the index when it is missing", async () => {
    const s1 = makeSession({
      id: "ses_1",
      project: "/tmp/proj",
      startedAt: "2026-01-01T00:00:00Z",
    });
    const s2 = makeSession({
      id: "ses_2",
      project: "/tmp/proj",
      startedAt: "2026-01-02T00:00:00Z",
    });
    const otherProject = makeSession({
      id: "ses_other_proj",
      project: "/tmp/other",
      startedAt: "2026-01-03T00:00:00Z",
    });
    await kv.set(KV.sessions, s1.id, s1);
    await kv.set(KV.sessions, s2.id, s2);
    await kv.set(KV.sessions, otherProject.id, otherProject);

    expect(await getProjectSessionIndex(kv as never, "/tmp/proj")).toBeNull();

    await handler({ sessionId: "ses_new", project: "/tmp/proj" });

    expect(
      kv.list.mock.calls.some(([scope]) => scope === KV.sessions),
    ).toBe(true);

    const index = await getProjectSessionIndex(kv as never, "/tmp/proj");
    expect(index?.map((e) => e.id).sort()).toEqual(["ses_1", "ses_2"]);
  });

  it("includes recent sessions from the index in the rendered context", async () => {
    const s1 = makeSession({
      id: "ses_recent",
      project: "/tmp/proj",
      startedAt: "2026-01-01T00:00:00Z",
      observationCount: 3,
    });
    await kv.set(KV.sessions, s1.id, s1);
    await kv.set(KV.observations(s1.id), "obs_1", {
      id: "obs_1",
      sessionId: s1.id,
      title: "index-visible-marker",
      narrative: "did a thing",
      importance: 8,
      type: "task",
    });
    await addSessionToProjectIndex(kv as never, "/tmp/proj", {
      id: s1.id,
      startedAt: s1.startedAt,
    });

    const result = await handler({
      sessionId: "ses_new",
      project: "/tmp/proj",
    });

    expect(result.context).toContain("index-visible-marker");
  });

  it("skips index entries whose session record was since deleted", async () => {
    await addSessionToProjectIndex(kv as never, "/tmp/proj", {
      id: "ses_gone",
      startedAt: "2026-01-01T00:00:00Z",
    });

    const result = await handler({
      sessionId: "ses_new",
      project: "/tmp/proj",
    });

    expect(result).toBeDefined();
    expect(result.context).not.toContain("ses_gone");
  });
});