Skip to content

A local-first multi-agent app development platform, built on PenguinHarness

25.0 KB
Raw Blame
/**
 * Session resume: `agent.resumeSession` and setHistory injection.
 *
 * - Resume source is the Trace file with the latest index; config carries over from session_meta
 *   (Workspace / Model cannot be swapped).
 * - Pairing-fallback placeholders, once constructed, are written into the original trace file;
 *   session_meta is never written twice.
 * - Errors when the session doesn't exist / the workspace is missing / the model is no longer in the project config.
 * - `groupHistoryToUniMessages` groups by adjacent same role; `GenerativeModel.setHistory` injects into AgentHub.
 */
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { createAgent } from "../src/index.js";
import {
  abortEvent,
  assistantText,
  compactionBegin,
  compactionEnd,
  requestBegin,
  requestEnd,
  sessionMeta,
  tokenUsage,
  toolCall,
  toolListReady,
  userText,
} from "../src/omnimessage/index.js";
import type { OmniMessage, TokenCounts } from "../src/omnimessage/index.js";
import { GenerativeModel, groupHistoryToUniMessages } from "../src/llm/index.js";
import { findLatestTraceFile, readTrace } from "../src/trace/index.js";
import { agentsMdPath, tracesDir } from "../src/state/paths.js";
import { stubProviderKeys } from "./provider-keys.js";

// The default project config ships with this model ((provider, model_id) pair reference; model_id is the upstream id).
const MODEL = { provider: "anthropic", model_id: "claude-sonnet-4-6" };

let tmpRoot: string;
let workspace: string;
let prevHome: string | undefined;
let restoreKeys: () => void;

beforeEach(async () => {
  prevHome = process.env.PENGUIN_HOME;
  tmpRoot = await fs.mkdtemp(path.join(os.tmpdir(), "penguin-resume-"));
  workspace = await fs.mkdtemp(path.join(os.tmpdir(), "penguin-resume-ws-"));
  process.env.PENGUIN_HOME = tmpRoot;
  restoreKeys = stubProviderKeys();
});

afterEach(async () => {
  if (prevHome === undefined) delete process.env.PENGUIN_HOME;
  else process.env.PENGUIN_HOME = prevHome;
  restoreKeys();
  await fs.rm(tmpRoot, { recursive: true, force: true });
  await fs.rm(workspace, { recursive: true, force: true });
});

const usage = (total: number): TokenCounts => ({
  cache_read: 0,
  cache_write: 0,
  output: 1,
  total,
});

/** Manually constructs a session's trace file (simulating a record left behind by a previous process). */
async function writeTraceFile(
  root: string,
  sessionId: string,
  messages: OmniMessage[],
  opts?: { dateDir?: string; index?: string },
): Promise<string> {
  const dir = path.join(
    tracesDir(root, "default_project", "default_agent"),
    opts?.dateDir ?? "2026-07-06",
  );
  await fs.mkdir(dir, { recursive: true });
  const file = path.join(dir, `${sessionId}_${opts?.index ?? "001"}.jsonl`);
  await fs.writeFile(file, messages.map((m) => JSON.stringify(m)).join("\n") + "\n", "utf8");
  return file;
}

function metaFor(sessionId: string, workspaceDir: string, model = MODEL): OmniMessage {
  return sessionMeta({
    session_id: sessionId,
    provider: model.provider,
    model_id: model.model_id,
    model_context_window: 1000000,
    system_prompt: "ORIGINAL SYSTEM PROMPT",
    agent_state: "/agent/state",
    workspace: workspaceDir,
    source: "user",
  });
}

describe("agent.resumeSession", () => {
  const SID = "session-2026-07-06-10-00-00-abcdef01";

  it("resumes from the latest trace file and exposes render history", async () => {
    const agent = await createAgent({});
    await writeTraceFile(tmpRoot, SID, [
      metaFor(SID, workspace),
      userText("hello"),
      requestBegin(),
      assistantText("hi there"),
      requestEnd("completed"),
      tokenUsage(usage(42), usage(42)),
    ]);

    const session = await agent.resumeSession({ sessionId: SID });
    expect(session.sessionId).toBe(SID);
    expect(session.provider).toBe(MODEL.provider);
    expect(session.modelId).toBe(MODEL.model_id);
    expect(session.workspaceDir).toBe(workspace);
    const texts = (session.resumedHistory ?? []).map(
      (m) => (m.payload as { text?: string }).text ?? "",
    );
    expect(texts).toEqual(["hello", "hi there"]);
  });

  it("keeps abort events in resumed render history", async () => {
    const agent = await createAgent({});
    await writeTraceFile(tmpRoot, SID, [
      metaFor(SID, workspace),
      userText("long task"),
      requestBegin(),
      assistantText("partial answer", "aborted"),
      requestEnd("aborted"),
      abortEvent(),
    ]);

    const session = await agent.resumeSession({ sessionId: SID });
    expect(
      (session.resumedHistory ?? []).map((m) => (m.payload as { type?: string }).type),
    ).toEqual(["text", "text", "abort"]);
  });

  it("closes a compaction the user quit out of as failed, discarding its draft (issue #288)", async () => {
    // The process died while a compaction ran: the shard ends inside the span (begin +
    // prompt + an unfinished request that had started writing a summary). That compaction
    // simply failed — resume closes the span before appending anything else, so the
    // conversation that follows is not hidden as compaction-internal, and the half-written
    // draft is discarded rather than adopted as a summary.
    const agent = await createAgent({});
    const file = await writeTraceFile(tmpRoot, SID, [
      metaFor(SID, workspace),
      userText("q1"),
      requestBegin(),
      assistantText("a1"),
      tokenUsage(usage(150), usage(150)),
      requestEnd("completed"),
      compactionBegin({ reason: "context", mode: "summarize", context: 150, turns: 1 }),
      userText("COMPACT NOW"),
      requestBegin(),
      assistantText("[summary]half-writ"),
    ]);

    const session = await agent.resumeSession({ sessionId: SID });
    const closed = await readTrace(file);
    const last = closed[closed.length - 1]!;
    expect(last.payload).toMatchObject({
      type: "compaction_end",
      reason: "context",
      mode: "summarize",
      status: "retryable",
    });
    // Nothing was reconstructed from the interrupted compaction: the original context is
    // resumed as it stood before it, with no `[context_summary]` injected.
    const injected = (session.resumedHistory ?? []).some((m) =>
      ((m.payload as { text?: string }).text ?? "").includes("[context_summary]"),
    );
    expect(injected).toBe(false);
    session.dispose();

    // Idempotent: a second resume sees the matched pair and appends nothing.
    const again = await agent.resumeSession({ sessionId: SID });
    again.dispose();
    expect((await readTrace(file)).length).toBe(closed.length);
  });

  it("does not write pairing placeholders to the trace file (resume is side-effect free)", async () => {
    const agent = await createAgent({});
    const file = await writeTraceFile(tmpRoot, SID, [
      metaFor(SID, workspace),
      userText("go"),
      requestBegin(),
      toolCall({ name: "exec_command", arguments: "{}", toolCallId: "tc1" }),
      requestEnd("completed"),
      tokenUsage(usage(10), usage(10)),
      // tc1's output was lost along with the process: the pairing placeholder is synthesized in memory and sent out with the next run, never persisted.
    ]);
    const before = await readTrace(file);

    await agent.resumeSession({ sessionId: SID });
    await agent.resumeSession({ sessionId: SID }); // resuming again has no side effects either
    const after = await readTrace(file);
    expect(after).toHaveLength(before.length);
    expect(
      after.filter((m) =>
        ((m.payload as { output?: string }).output ?? "").includes("interrupted"),
      ),
    ).toHaveLength(0);
    // session_meta is never written twice.
    expect(after.filter((m) => m.type === "session_meta")).toHaveLength(1);
  });

  it("picks the latest index when the context was compacted into multiple files", async () => {
    const agent = await createAgent({});
    await writeTraceFile(tmpRoot, SID, [metaFor(SID, workspace), userText("old context")], {
      index: "001",
    });
    await writeTraceFile(
      tmpRoot,
      SID,
      [
        metaFor(SID, workspace),
        userText("[context_summary]gist[/context_summary]"),
        requestBegin(),
        assistantText("resumed context"),
        requestEnd("completed"),
        tokenUsage(usage(5), usage(5)),
      ],
      { index: "002" },
    );

    const session = await agent.resumeSession({ sessionId: SID });
    const texts = (session.resumedHistory ?? []).map(
      (m) => (m.payload as { text?: string }).text ?? "",
    );
    expect(texts).toEqual(["[context_summary]gist[/context_summary]", "resumed context"]);
  });

  it("errors when the session does not exist", async () => {
    const agent = await createAgent({});
    await expect(agent.resumeSession({ sessionId: "session-none" })).rejects.toThrow(
      /Session does not exist/,
    );
  });

  it("errors when the recorded workspace no longer exists (PRN-004: no auto-create)", async () => {
    const agent = await createAgent({});
    const gone = path.join(workspace, "gone");
    await writeTraceFile(tmpRoot, SID, [metaFor(SID, gone), userText("x")]);
    await expect(agent.resumeSession({ sessionId: SID })).rejects.toThrow(
      /Workspace no longer exists/,
    );
  });

  it("errors when the recorded model is no longer in the project config", async () => {
    const agent = await createAgent({});
    await writeTraceFile(tmpRoot, SID, [
      metaFor(SID, workspace, { provider: "custom", model_id: "vanished-model" }),
      userText("x"),
    ]);
    await expect(agent.resumeSession({ sessionId: SID })).rejects.toThrow(
      /is not in the Project config/,
    );
  });

  it("errors clearly when session_meta lacks provider (old-format trace, no migration)", async () => {
    // An old-format trace's session_meta only has model_id (from the composite-id era): no backward compat, just a clear error.
    const agent = await createAgent({});
    const legacy = metaFor(SID, workspace);
    delete (legacy.payload as { provider?: string }).provider;
    await writeTraceFile(tmpRoot, SID, [legacy, userText("x")]);
    await expect(agent.resumeSession({ sessionId: SID })).rejects.toThrow(/legacy data/);
  });

  it("latestSessionId returns the newest session by embedded timestamp", async () => {
    const agent = await createAgent({});
    expect(await agent.latestSessionId()).toBeNull();
    const older = "session-2026-07-05-09-00-00-aaaaaaaa";
    const newer = "session-2026-07-06-11-00-00-bbbbbbbb";
    await writeTraceFile(tmpRoot, older, [metaFor(older, workspace), userText("older")], {
      dateDir: "2026-07-05",
    });
    await writeTraceFile(tmpRoot, newer, [metaFor(newer, workspace), userText("newer")], {
      dateDir: "2026-07-06",
    });
    expect(await agent.latestSessionId()).toBe(newer);
  });

  it("latestSessionId ignores empty traces that only contain session_meta", async () => {
    const agent = await createAgent({});
    const older = "session-2026-07-05-09-00-00-aaaaaaaa";
    const emptyNewer = "session-2026-07-06-11-00-00-bbbbbbbb";
    await writeTraceFile(tmpRoot, older, [metaFor(older, workspace), userText("older")], {
      dateDir: "2026-07-05",
    });
    await writeTraceFile(tmpRoot, emptyNewer, [metaFor(emptyNewer, workspace)], {
      dateDir: "2026-07-06",
    });
    expect(await agent.latestSessionId()).toBe(older);
  });

  it("manual compact on a new empty session does not create a resumable trace", async () => {
    const agent = await createAgent({});
    const session = await agent.createSession({ workspaceDir: workspace });
    const messages = [];
    for await (const msg of session.compact()) messages.push(msg);
    expect(messages).toHaveLength(0);
    expect(await agent.latestSessionId()).toBeNull();
  });

  it("compactability reflects the replayed turn count before the first run", async () => {
    // A process restart resumes a Session but does not run it, and the engine that holds
    // `sessionTurns` is built lazily by the first run. Availability therefore has to be
    // answered from the state the replay recovered, not from a not-yet-built engine —
    // otherwise a user with a full conversation is told there is nothing to compact
    // (the server turns this reason into a 409 `nothing_to_compact`).
    const agent = await createAgent({});
    await writeTraceFile(tmpRoot, SID, [
      metaFor(SID, workspace),
      userText("q1"),
      requestBegin(),
      assistantText("a1"),
      requestEnd("completed"),
      tokenUsage(usage(42), usage(42)),
    ]);

    const session = await agent.resumeSession({ sessionId: SID });
    expect(session.compactability()).toBe("ok");
    session.dispose();
  });

  it("compactability keeps the just-compacted reason across a restart", async () => {
    // The trace ends on a completed compaction: the resumed context is empty because it was
    // *just compacted*, not because the user has never spoken. Both have zero turns and they
    // must not collapse into the same explanation.
    const agent = await createAgent({});
    await writeTraceFile(tmpRoot, SID, [
      metaFor(SID, workspace),
      userText("q1"),
      requestBegin(),
      assistantText("a1"),
      requestEnd("completed"),
      tokenUsage(usage(150), usage(150)),
      compactionBegin({ reason: "context", mode: "summarize", context: 150, turns: 1 }),
      userText("COMPACT NOW"),
      requestBegin(),
      assistantText("[summary]the story so far[/summary]"),
      requestEnd("completed"),
      compactionEnd({ reason: "context", mode: "summarize", status: "completed" }),
    ]);

    const session = await agent.resumeSession({ sessionId: SID });
    expect(session.compactability()).toBe("just_compacted");
    session.dispose();
  });

  it("compactability stays empty for a resumed session whose context has no completed turn", async () => {
    // The genuine `empty` case survives the fix: the only request in the trace was interrupted,
    // so no turn ever completed and there is truly nothing to fold.
    const agent = await createAgent({});
    await writeTraceFile(tmpRoot, SID, [
      metaFor(SID, workspace),
      userText("q1"),
      requestBegin(),
      assistantText("half-writ"),
      abortEvent(),
    ]);

    const session = await agent.resumeSession({ sessionId: SID });
    expect(session.compactability()).toBe("empty");
    session.dispose();
  });

  it("compactability is empty on a brand-new session that has never run", async () => {
    const agent = await createAgent({});
    const session = await agent.createSession({ workspaceDir: workspace });
    expect(session.compactability()).toBe("empty");
    session.dispose();
  });
});

describe("setHistory injection", () => {
  it("groupHistoryToUniMessages groups adjacent same-role messages into UniMessages", () => {
    const uni = groupHistoryToUniMessages([
      userText("hello"),
      assistantText("hi"),
      toolCall({ name: "exec_command", arguments: '{"cmd":"ls"}', toolCallId: "tc1" }),
      {
        ...userText("ignored-shape"),
        payload: {
          type: "tool_call_output",
          role: "user",
          output: "out",
          tool_call_id: "tc1",
          stop_reason: "completed",
        },
      } as OmniMessage,
      userText("next"),
      assistantText("done"),
    ]);
    expect(uni.map((m) => m.role)).toEqual(["user", "assistant", "user", "assistant"]);
    expect(uni[1]!.content_items.map((c) => c.type)).toEqual(["text", "tool_call"]);
    expect(uni[2]!.content_items.map((c) => c.type)).toEqual(["tool_result", "text"]);
  });

  it("GenerativeModel.setHistory seeds the AgentHub client history", () => {
    // GenerativeModel takes the request id sent to AgentHub (the upstream id), not the storage id.
    const model = new GenerativeModel({ modelId: "claude-sonnet-4-6", tools: [] });
    model.setHistory([userText("hello"), assistantText("hi")]);
    const client = (model as unknown as { client: { getHistory(): unknown[] } }).client;
    expect(client.getHistory()).toHaveLength(2);
  });
});

describe("agent.resumeSession system prompt per context", () => {
  const SID = "session-2026-07-06-13-00-00-abcdef11";
  const promptOf = (session: { metaMessage: OmniMessage }): string =>
    (session.metaMessage.payload as { system_prompt: string }).system_prompt;

  it("opens a context closed by a completed compaction with the current AGENTS.md, not the closed file's recorded prompt", async () => {
    const agent = await createAgent({});
    await writeTraceFile(tmpRoot, SID, [
      metaFor(SID, workspace),
      userText("q1"),
      requestBegin(),
      assistantText("a1"),
      tokenUsage(usage(150), usage(150)),
      requestEnd("completed"),
      compactionBegin({ reason: "context", mode: "summarize", context: 150, turns: 1 }),
      userText("COMPACT NOW"),
      requestBegin(),
      assistantText("[summary]carry on[/summary]"),
      requestEnd("completed"),
      compactionEnd({ reason: "context", mode: "summarize", status: "completed" }),
    ]);
    await fs.writeFile(
      agentsMdPath(tmpRoot, "default_project", "default_agent"),
      "EDITED BEFORE RESUME",
      "utf8",
    );

    const session = await agent.resumeSession({ sessionId: SID });
    try {
      // The closed context is opened here for the first time — nothing was produced under any
      // prompt yet — so it gets the prompt assembled now, as the compaction would have opened it.
      expect(promptOf(session)).toContain("EDITED BEFORE RESUME");
      expect(promptOf(session)).not.toBe("ORIGINAL SYSTEM PROMPT");
    } finally {
      session.dispose();
    }
  });

  it("ignores a legacy recorded thinking level on resume: the level is a per-request parameter", async () => {
    const withLevel = (level: string): OmniMessage => {
      const meta = metaFor(SID, workspace);
      return {
        ...meta,
        payload: { ...meta.payload, thinking_level: level },
      } as unknown as OmniMessage;
    };
    const traceRecords = (meta: OmniMessage): OmniMessage[] => [
      meta,
      userText("hello"),
      requestBegin(),
      assistantText("hi there"),
      requestEnd("completed"),
    ];
    const agent = await createAgent({});
    // A legacy file recording "xhigh" does not shape the resumed context: the meta assembled
    // for it records no level at all — the level resolves per request from the pin and the
    // Agent config, exactly as on any other open.
    await writeTraceFile(tmpRoot, SID, traceRecords(withLevel("xhigh")));
    const session = await agent.resumeSession({ sessionId: SID });
    try {
      expect(
        (session.metaMessage.payload as { thinking_level?: string }).thinking_level,
      ).toBeUndefined();
    } finally {
      session.dispose();
    }
  });

  it("keeps the recorded prompt for an open context: its replayed history was produced under it", async () => {
    const agent = await createAgent({});
    await writeTraceFile(tmpRoot, SID, [
      metaFor(SID, workspace),
      userText("hello"),
      requestBegin(),
      assistantText("hi there"),
      requestEnd("completed"),
    ]);
    await fs.writeFile(
      agentsMdPath(tmpRoot, "default_project", "default_agent"),
      "EDITED BEFORE RESUME",
      "utf8",
    );

    const session = await agent.resumeSession({ sessionId: SID });
    try {
      expect(promptOf(session)).toBe("ORIGINAL SYSTEM PROMPT");
    } finally {
      session.dispose();
    }
  });
});

describe("agent.resumeSession after an in-session model switch", () => {
  const SID = "session-2026-07-06-11-00-00-abcdef02";
  // The default Project config ships both: the Session starts on ORIGINAL and switches to MODEL.
  const ORIGINAL = { provider: "deepseek", model_id: "deepseek-flash" };
  const SUMMARY = "[context_summary]\nthe story so far\n[/context_summary]";
  const engineStateOf = (session: unknown) =>
    (
      session as {
        engineDeps: {
          initialState?: {
            pendingSummary?: OmniMessage;
            openingSummary?: OmniMessage;
            carryOver?: OmniMessage[];
            sessionTurns?: number;
            pendingTraceRotation?: boolean;
          };
        };
      }
    ).engineDeps.initialState;
  /** File 1: a context on `model` with one completed turn, closed by a completed summarize pair of `reason`. */
  const closedFile = (
    model: { provider: string; model_id: string },
    reason: "manual" | "context",
  ) => [
    metaFor(SID, workspace, model),
    userText("q1"),
    requestBegin(),
    assistantText("a1"),
    requestEnd("completed"),
    tokenUsage(usage(150), usage(150)),
    compactionBegin({ reason, mode: "summarize", context: 150, turns: 1 }),
    userText("COMPACT NOW"),
    requestBegin(),
    assistantText("[summary]the story so far[/summary]"),
    requestEnd("completed"),
    compactionEnd({ reason, mode: "summarize", status: "completed" }),
  ];

  it("resumes on the model of the eagerly opened file: history empty, the summary is the carry-over, on the switched-to model", async () => {
    const agent = await createAgent({});
    // The switch closed the original model's context with a plain manual pair, and opened the
    // target's file at once: its session_meta, its toolset, the summary as its first input.
    await writeTraceFile(tmpRoot, SID, closedFile(ORIGINAL, "manual"));
    await writeTraceFile(
      tmpRoot,
      SID,
      [metaFor(SID, workspace, MODEL), toolListReady([]), userText(SUMMARY)],
      { index: "002" },
    );

    const session = await agent.resumeSession({ sessionId: SID });
    try {
      expect(session.provider).toBe(MODEL.provider);
      expect(session.modelId).toBe(MODEL.model_id);
      expect((session.metaMessage.payload as { model_id: string }).model_id).toBe(MODEL.model_id);
      const state = engineStateOf(session);
      expect(state?.pendingSummary).toBeUndefined();
      expect(state?.pendingTraceRotation).toBe(false);
      expect(state?.sessionTurns).toBe(0);
      expect((state?.carryOver ?? []).map((m) => (m.payload as { text: string }).text)).toEqual([
        SUMMARY,
      ]);
      // Already on the file, so pending input rather than a pending summary — and still named
      // as the summary this context opened with.
      expect(state?.openingSummary).toBe(state?.carryOver?.[0]);
      expect(session.compactability()).toBe("just_compacted");
    } finally {
      session.dispose();
    }
  });

  it("a switch made right after the restart carries the summary on: the next file opens with it", async () => {
    const agent = await createAgent({});
    await writeTraceFile(tmpRoot, SID, closedFile(ORIGINAL, "manual"));
    const leaving = await writeTraceFile(
      tmpRoot,
      SID,
      [metaFor(SID, workspace, MODEL), toolListReady([]), userText(SUMMARY)],
      { index: "002" },
    );
    const kind = (m: OmniMessage): string | undefined =>
      m.type === "session_meta" ? "session_meta" : (m.payload as { type?: string }).type;

    const session = await agent.resumeSession({ sessionId: SID });
    try {
      // Nothing ran since the restart: the engine is built by the switch itself.
      const streamed: OmniMessage[] = [];
      for await (const msg of session.switchModel({
        provider: ORIGINAL.provider,
        modelId: ORIGINAL.model_id,
      })) {
        streamed.push(msg);
      }
      // The engine is built first, so the bootstrap of the context being left streams. That
      // context holds the summary and no completed turn: nothing to summarize and no pair —
      // the target's own records and its meta follow.
      expect(streamed.map(kind)).toEqual(["tool_list_ready", "tool_list_ready", "session_meta"]);
      expect(session.provider).toBe(ORIGINAL.provider);
      expect(session.modelId).toBe(ORIGINAL.model_id);

      const located = await findLatestTraceFile(
        tracesDir(tmpRoot, "default_project", "default_agent"),
        SID,
      );
      expect(located!.index).toBe(3);
      const opened = await readTrace(located!.path);
      expect(opened.map(kind)).toEqual(["session_meta", "tool_list_ready", "text"]);
      expect((opened[0]!.payload as { model_id: string }).model_id).toBe(ORIGINAL.model_id);
      expect((opened[2]!.payload as { text: string }).text).toBe(SUMMARY);
      // Nothing was added to the file being left.
      expect((await readTrace(leaving)).map(kind)).toEqual([
        "session_meta",
        "tool_list_ready",
        "text",
      ]);
    } finally {
      session.dispose();
    }
  });

  it("a file a compaction opened, with no completed turn yet, answers just_compacted after a restart", async () => {
    const agent = await createAgent({});
    // An ordinary compaction, then the process died before the new context's first answer: the
    // file past the first holds the summary and a prompt whose request never completed.
    await writeTraceFile(tmpRoot, SID, closedFile(MODEL, "context"));
    await writeTraceFile(
      tmpRoot,
      SID,
      [
        metaFor(SID, workspace),
        toolListReady([]),
        userText(SUMMARY),
        userText("q2"),
        requestBegin(),
        abortEvent(),
      ],
      { index: "002" },
    );

    const session = await agent.resumeSession({ sessionId: SID });
    try {
      expect(engineStateOf(session)?.sessionTurns).toBe(0);
      expect(session.compactability()).toBe("just_compacted");
    } finally {
      session.dispose();
    }
  });
});