|
| 1 | +import { afterEach, describe, expect, spyOn, test } from "bun:test" |
| 2 | +import fs from "fs/promises" |
| 3 | +import path from "path" |
| 4 | +import { Agent } from "../../src/agent/agent" |
| 5 | +import { Global } from "../../src/global" |
| 6 | +import { Identifier } from "../../src/id/id" |
| 7 | +import { Instance } from "../../src/project/instance" |
| 8 | +import { Provider } from "../../src/provider/provider" |
| 9 | +import { Session } from "../../src/session" |
| 10 | +import { LLM } from "../../src/session/llm" |
| 11 | +import { MessageV2 } from "../../src/session/message-v2" |
| 12 | +import { SessionProcessor } from "../../src/session/processor" |
| 13 | +import { SessionSummary } from "../../src/session/summary" |
| 14 | +import { Snapshot } from "../../src/snapshot" |
| 15 | +import { Storage } from "../../src/storage/storage" |
| 16 | +import type { Config } from "../../src/config/config" |
| 17 | +import { tmpdir } from "../fixture/fixture" |
| 18 | + |
| 19 | +afterEach(() => Snapshot.setDefault(true)) |
| 20 | + |
| 21 | +/** |
| 22 | + * Runs one model step that writes `out.txt` in a git worktree and returns what the |
| 23 | + * step persisted: its parts, the session diff and whether the shadow repo exists. |
| 24 | + */ |
| 25 | +async function step(config: Partial<Config.Info> = {}) { |
| 26 | + await using tmp = await tmpdir({ |
| 27 | + git: true, |
| 28 | + config: { |
| 29 | + enabled_providers: ["alibaba"], |
| 30 | + provider: { alibaba: { options: { apiKey: "test-key" } } }, |
| 31 | + ...config, |
| 32 | + }, |
| 33 | + }) |
| 34 | + |
| 35 | + return await Instance.provide({ |
| 36 | + directory: tmp.path, |
| 37 | + fn: async () => { |
| 38 | + const session = await Session.create({ title: "Snapshot fixture" }) |
| 39 | + const agent = await Agent.get("build") |
| 40 | + const model = await Provider.getModel("alibaba", "qwen-plus") |
| 41 | + const user = (await Session.updateMessage({ |
| 42 | + id: Identifier.ascending("message"), |
| 43 | + sessionID: session.id, |
| 44 | + role: "user", |
| 45 | + time: { created: Date.now() }, |
| 46 | + agent: agent.name, |
| 47 | + model: { providerID: model.providerID, modelID: model.id }, |
| 48 | + })) as MessageV2.User |
| 49 | + const assistant = (await Session.updateMessage({ |
| 50 | + id: Identifier.ascending("message"), |
| 51 | + sessionID: session.id, |
| 52 | + role: "assistant", |
| 53 | + parentID: user.id, |
| 54 | + modelID: model.id, |
| 55 | + providerID: model.providerID, |
| 56 | + mode: agent.name, |
| 57 | + agent: agent.name, |
| 58 | + path: { cwd: tmp.path, root: tmp.path }, |
| 59 | + cost: 0, |
| 60 | + tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } }, |
| 61 | + time: { created: Date.now() }, |
| 62 | + })) as MessageV2.Assistant |
| 63 | + |
| 64 | + const stream = spyOn(LLM, "stream").mockResolvedValue({ |
| 65 | + fullStream: (async function* () { |
| 66 | + yield { type: "start-step" } |
| 67 | + await Bun.write(path.join(tmp.path, "out.txt"), "written during the step\n") |
| 68 | + yield { |
| 69 | + type: "finish-step", |
| 70 | + finishReason: "stop", |
| 71 | + usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, |
| 72 | + } |
| 73 | + })(), |
| 74 | + } as unknown as Awaited<ReturnType<typeof LLM.stream>>) |
| 75 | + |
| 76 | + try { |
| 77 | + await SessionProcessor.create({ |
| 78 | + assistantMessage: assistant, |
| 79 | + sessionID: session.id, |
| 80 | + model, |
| 81 | + abort: new AbortController().signal, |
| 82 | + }).process({ |
| 83 | + user, |
| 84 | + sessionID: session.id, |
| 85 | + model, |
| 86 | + agent, |
| 87 | + abort: new AbortController().signal, |
| 88 | + system: [], |
| 89 | + messages: [], |
| 90 | + tools: {}, |
| 91 | + }) |
| 92 | + } finally { |
| 93 | + stream.mockRestore() |
| 94 | + } |
| 95 | + // The processor summarizes in the background; await it so the result is settled. |
| 96 | + await SessionSummary.summarize({ sessionID: session.id, messageID: user.id }) |
| 97 | + |
| 98 | + const parts = (await Session.messages({ sessionID: session.id })).flatMap((message) => message.parts) |
| 99 | + return { |
| 100 | + parts, |
| 101 | + diff: await Storage.read<Snapshot.FileDiff[]>(["session_diff", session.id]).catch(() => undefined), |
| 102 | + summary: (await Session.get(session.id)).summary, |
| 103 | + shadow: await fs |
| 104 | + .stat(path.join(Global.Path.data, "snapshot", Instance.project.id)) |
| 105 | + .then(() => true) |
| 106 | + .catch(() => false), |
| 107 | + } |
| 108 | + }, |
| 109 | + }) |
| 110 | +} |
| 111 | + |
| 112 | +describe("headless run snapshots (#146)", () => { |
| 113 | + test("a headless run takes no snapshot, records no patch and writes no session diff", async () => { |
| 114 | + // what `aictrl run` sets before it bootstraps |
| 115 | + Snapshot.setDefault(false) |
| 116 | + const result = await step() |
| 117 | + |
| 118 | + expect(result.shadow).toBe(false) |
| 119 | + const start = result.parts.find((part) => part.type === "step-start") |
| 120 | + const finish = result.parts.find((part) => part.type === "step-finish") |
| 121 | + expect(start?.type === "step-start" ? start.snapshot : "missing").toBeUndefined() |
| 122 | + expect(finish?.type === "step-finish" ? finish.snapshot : "missing").toBeUndefined() |
| 123 | + expect(result.parts.filter((part) => part.type === "patch")).toHaveLength(0) |
| 124 | + expect(result.diff).toBeUndefined() |
| 125 | + expect(result.summary).toBeUndefined() |
| 126 | + }) |
| 127 | + |
| 128 | + test("config `snapshot: true` re-enables snapshots, patches and the session diff in a headless run", async () => { |
| 129 | + // what `aictrl run` sets before it bootstraps |
| 130 | + Snapshot.setDefault(false) |
| 131 | + const result = await step({ snapshot: true }) |
| 132 | + |
| 133 | + expect(result.shadow).toBe(true) |
| 134 | + const start = result.parts.find((part) => part.type === "step-start") |
| 135 | + expect(start?.type === "step-start" ? start.snapshot : undefined).toBeString() |
| 136 | + const patch = result.parts.find((part) => part.type === "patch") |
| 137 | + expect(patch?.type === "patch" ? patch.files.map((file) => path.basename(file)) : []).toEqual(["out.txt"]) |
| 138 | + expect(result.diff?.map((item) => item.file)).toEqual(["out.txt"]) |
| 139 | + expect(result.summary).toMatchObject({ files: 1, additions: 1 }) |
| 140 | + }) |
| 141 | + |
| 142 | + test("sessions outside a headless run keep snapshots by default", async () => { |
| 143 | + const result = await step() |
| 144 | + |
| 145 | + expect(result.shadow).toBe(true) |
| 146 | + expect(result.parts.filter((part) => part.type === "patch")).toHaveLength(1) |
| 147 | + expect(result.diff?.map((item) => item.file)).toEqual(["out.txt"]) |
| 148 | + }) |
| 149 | + |
| 150 | + test("config `snapshot: false` still disables snapshots outside a headless run", async () => { |
| 151 | + const result = await step({ snapshot: false }) |
| 152 | + |
| 153 | + expect(result.shadow).toBe(false) |
| 154 | + expect(result.parts.filter((part) => part.type === "patch")).toHaveLength(0) |
| 155 | + expect(result.diff).toBeUndefined() |
| 156 | + }) |
| 157 | +}) |
0 commit comments