|
| 1 | +// Contents assertions for checkpointSave against a real shadow git. |
| 2 | +// |
| 3 | +// The task-start baseline cases in src/core/task/__tests__/Task.spec.ts stub Task#checkpointSave |
| 4 | +// (the spy at line 7181 resolves a commit, the one at 7451 resolves one empty save and then a |
| 5 | +// commit, the one at 7476 resolves a commit) and assert how often the stub was called and with |
| 6 | +// which flags. The checkpointSave cases in ./checkpoint.test.ts go one step further and stub the |
| 7 | +// checkpoint service itself. Neither layer can show that the commit a save returns holds the |
| 8 | +// workspace file's bytes: a save that staged the wrong worktree, recorded nothing, or committed |
| 9 | +// bytes that had moved on would satisfy every call count and flag assertion above. This file runs |
| 10 | +// the real RepoPerTaskCheckpointService over a real workspace git repo in a temporary directory |
| 11 | +// and asserts the contents stored in the shadow git's own commits. |
| 12 | + |
| 13 | +import fs from "fs/promises" |
| 14 | +import os from "os" |
| 15 | +import path from "path" |
| 16 | + |
| 17 | +import { simpleGit } from "simple-git" |
| 18 | + |
| 19 | +import { RepoPerTaskCheckpointService } from "../../../services/checkpoints" |
| 20 | +import { BLOCKED_ENV_KEYS } from "../../../services/checkpoints/ShadowCheckpointService" |
| 21 | + |
| 22 | +import { checkpointSave, getCheckpointService } from "../index" |
| 23 | +import type { Task } from "../../task/Task" |
| 24 | + |
| 25 | +// checkpointSave reports every recorded checkpoint through TelemetryService.instance; stubbing |
| 26 | +// the client keeps the real telemetry upload wiring out of a contents test. |
| 27 | +vi.mock("@roo-code/telemetry", () => ({ |
| 28 | + TelemetryService: { |
| 29 | + instance: { |
| 30 | + captureCheckpointCreated: vi.fn(), |
| 31 | + captureCheckpointRestored: vi.fn(), |
| 32 | + captureCheckpointDiffed: vi.fn(), |
| 33 | + }, |
| 34 | + }, |
| 35 | +})) |
| 36 | + |
| 37 | +const tmpDir = path.join(os.tmpdir(), "checkpointSaveShadowGit") |
| 38 | +const CHECKPOINTED_FILE = "checkpointed.txt" |
| 39 | + |
| 40 | +// simple-git ≥3.36 blocks env vars it treats as code-execution vectors. Strip them for this |
| 41 | +// suite so a developer's global GIT_EDITOR or GIT_SSH_COMMAND does not fail the real git calls |
| 42 | +// below. Safe under vitest's default "forks" pool, where each worker owns its process.env. |
| 43 | +const savedEnv: Partial<Record<string, string>> = {} |
| 44 | + |
| 45 | +beforeAll(() => { |
| 46 | + for (const key of BLOCKED_ENV_KEYS) { |
| 47 | + savedEnv[key] = process.env[key] |
| 48 | + delete process.env[key] |
| 49 | + } |
| 50 | +}) |
| 51 | + |
| 52 | +afterAll(async () => { |
| 53 | + for (const key of BLOCKED_ENV_KEYS) { |
| 54 | + if (savedEnv[key] !== undefined) { |
| 55 | + process.env[key] = savedEnv[key] |
| 56 | + } else { |
| 57 | + delete process.env[key] |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + await fs.rm(tmpDir, { recursive: true, force: true }) |
| 62 | +}, 60_000) |
| 63 | + |
| 64 | +/** |
| 65 | + * The read/write surface that checkpointSave and getCheckpointService actually touch: |
| 66 | + * the task identity and workspace, the checkpoint flags and timeout, the abort flag, the |
| 67 | + * provider used for logging and webview messages, and the say() the shadow service's |
| 68 | + * "checkpoint" event calls. A real Task would pull the whole extension host into a contents |
| 69 | + * test without adding any proof, so the double is cast once here rather than per case. |
| 70 | + */ |
| 71 | +type CheckpointSaveSurface = { |
| 72 | + taskId: string |
| 73 | + cwd: string |
| 74 | + enableCheckpoints: boolean |
| 75 | + checkpointService: RepoPerTaskCheckpointService | undefined |
| 76 | + checkpointServiceInitializing: boolean |
| 77 | + checkpointTimeout: number |
| 78 | + abort: boolean |
| 79 | + clineMessages: never[] |
| 80 | + apiConversationHistory: never[] |
| 81 | + say: (...args: unknown[]) => Promise<unknown> |
| 82 | + providerRef: { |
| 83 | + deref: () => { |
| 84 | + context: { globalStorageUri: { fsPath: string } } |
| 85 | + log: (message: string) => void |
| 86 | + postMessageToWebview: (message: unknown) => void |
| 87 | + } |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +/** |
| 92 | + * checkpointSave resolves with a commit sha, resolves with nothing when the save found no changes |
| 93 | + * or the per-task queue dropped it, and returns void when no checkpoint service exists. Only the |
| 94 | + * first shape can hold recorded contents, so the other two are failures here. |
| 95 | + */ |
| 96 | +function committed(result: unknown, label: string): string { |
| 97 | + if (typeof result === "object" && result !== null && "commit" in result && typeof result.commit === "string") { |
| 98 | + return result.commit |
| 99 | + } |
| 100 | + |
| 101 | + throw new Error(`${label} resolved with no commit, so nothing was recorded in the shadow git`) |
| 102 | +} |
| 103 | + |
| 104 | +async function setupCheckpointTask(taskId: string) { |
| 105 | + const workspaceDir = path.join(tmpDir, `workspace-${taskId}`) |
| 106 | + const shadowDir = path.join(tmpDir, `storage-${taskId}`) |
| 107 | + await fs.mkdir(workspaceDir, { recursive: true }) |
| 108 | + await fs.mkdir(shadowDir, { recursive: true }) |
| 109 | + |
| 110 | + // A committed workspace state: the shadow git records its base commit from the workspace as it |
| 111 | + // stands at initialization, so the write under test has to land after that initialization for |
| 112 | + // its contents to be the delta the save is asked to record. |
| 113 | + const workspaceGit = simpleGit(workspaceDir) |
| 114 | + await workspaceGit.init() |
| 115 | + await workspaceGit.addConfig("user.name", "Roo Code") |
| 116 | + await workspaceGit.addConfig("user.email", "[email protected]") |
| 117 | + await workspaceGit.addConfig("commit.gpgSign", "false") |
| 118 | + await fs.writeFile(path.join(workspaceDir, CHECKPOINTED_FILE), "contents before the baseline") |
| 119 | + await workspaceGit.add(".") |
| 120 | + await workspaceGit.commit("record the workspace baseline") |
| 121 | + |
| 122 | + const surface: CheckpointSaveSurface = { |
| 123 | + taskId, |
| 124 | + cwd: workspaceDir, |
| 125 | + enableCheckpoints: true, |
| 126 | + checkpointService: undefined, |
| 127 | + checkpointServiceInitializing: false, |
| 128 | + checkpointTimeout: 30, |
| 129 | + abort: false, |
| 130 | + clineMessages: [], |
| 131 | + apiConversationHistory: [], |
| 132 | + say: vi.fn(async () => {}), |
| 133 | + providerRef: { |
| 134 | + deref: () => ({ |
| 135 | + context: { globalStorageUri: { fsPath: shadowDir } }, |
| 136 | + log: () => {}, |
| 137 | + postMessageToWebview: vi.fn(), |
| 138 | + }), |
| 139 | + }, |
| 140 | + } |
| 141 | + |
| 142 | + const task = surface as unknown as Task |
| 143 | + const service = await getCheckpointService(task) |
| 144 | + |
| 145 | + if (!service) { |
| 146 | + throw new Error("no checkpoint service after initialization, so no shadow git exists to read") |
| 147 | + } |
| 148 | + |
| 149 | + const baseHash = service.baseHash |
| 150 | + |
| 151 | + if (!baseHash) { |
| 152 | + throw new Error("the shadow git reported no base commit after initialization") |
| 153 | + } |
| 154 | + |
| 155 | + return { task, service, workspaceGit, baseHash } |
| 156 | +} |
| 157 | + |
| 158 | +describe("checkpointSave contents in a real shadow git", () => { |
| 159 | + it("records the written bytes in the commit the task-start baseline save returns", async () => { |
| 160 | + const { task, service, workspaceGit, baseHash } = await setupCheckpointTask("contents-at-baseline") |
| 161 | + |
| 162 | + // The write reaches the working tree only. The workspace repo never commits it, so the shadow |
| 163 | + // git is the single place the bytes can be recorded. |
| 164 | + await fs.writeFile(path.join(service.workspaceDir, CHECKPOINTED_FILE), "contents the baseline must record") |
| 165 | + |
| 166 | + // The same flags Task's baseline block uses: force (allow-empty) and suppressMessage. |
| 167 | + const commit = committed(await checkpointSave(task, true, true), "the task-start baseline save") |
| 168 | + |
| 169 | + const diffs = await service.getDiff({ from: baseHash, to: commit }) |
| 170 | + expect(diffs).toHaveLength(1) |
| 171 | + expect(diffs[0].paths.relative).toBe(CHECKPOINTED_FILE) |
| 172 | + expect(diffs[0].content.before).toBe("contents before the baseline") |
| 173 | + expect(diffs[0].content.after).toBe("contents the baseline must record") |
| 174 | + |
| 175 | + // The workspace repo's own commit still holds the pre-write bytes, so the record above came |
| 176 | + // out of the shadow git and not out of the user-visible repository. |
| 177 | + expect(await workspaceGit.raw(["show", `HEAD:${CHECKPOINTED_FILE}`])).toBe("contents before the baseline") |
| 178 | + }, 30_000) |
| 179 | + |
| 180 | + it("keeps each save's own bytes: the second commit holds the second write and the first still holds the first", async () => { |
| 181 | + const { task, service, baseHash } = await setupCheckpointTask("contents-across-two-saves") |
| 182 | + const checkpointedFile = path.join(service.workspaceDir, CHECKPOINTED_FILE) |
| 183 | + |
| 184 | + await fs.writeFile(checkpointedFile, "first write") |
| 185 | + const commitOne = committed(await checkpointSave(task, false, true), "the first per-write save") |
| 186 | + |
| 187 | + await fs.writeFile(checkpointedFile, "second write") |
| 188 | + const commitTwo = committed(await checkpointSave(task, false, true), "the second per-write save") |
| 189 | + |
| 190 | + const baseToOne = await service.getDiff({ from: baseHash, to: commitOne }) |
| 191 | + expect(baseToOne).toHaveLength(1) |
| 192 | + expect(baseToOne[0].content.before).toBe("contents before the baseline") |
| 193 | + expect(baseToOne[0].content.after).toBe("first write") |
| 194 | + |
| 195 | + const oneToTwo = await service.getDiff({ from: commitOne, to: commitTwo }) |
| 196 | + expect(oneToTwo).toHaveLength(1) |
| 197 | + expect(oneToTwo[0].content.before).toBe("first write") |
| 198 | + expect(oneToTwo[0].content.after).toBe("second write") |
| 199 | + |
| 200 | + // Read the first commit again after the second save: the earlier record still holds its own |
| 201 | + // bytes. A call count says two saves happened; only the contents show that each one recorded |
| 202 | + // the state its caller asked for. |
| 203 | + const baseToOneAfterSecond = await service.getDiff({ from: baseHash, to: commitOne }) |
| 204 | + expect(baseToOneAfterSecond[0].content.after).toBe("first write") |
| 205 | + |
| 206 | + const baseToTwo = await service.getDiff({ from: baseHash, to: commitTwo }) |
| 207 | + expect(baseToTwo[0].content.after).toBe("second write") |
| 208 | + }, 30_000) |
| 209 | + |
| 210 | + it("records a file the workspace repo never tracked with the bytes it held when the save ran", async () => { |
| 211 | + const { task, service, workspaceGit, baseHash } = await setupCheckpointTask("contents-of-untracked-file") |
| 212 | + const newFile = path.join(service.workspaceDir, "written-by-the-tool.txt") |
| 213 | + await fs.writeFile(newFile, "the bytes a write handed to the workspace") |
| 214 | + |
| 215 | + const commit = committed(await checkpointSave(task, false, true), "the save after the new-file write") |
| 216 | + |
| 217 | + const diffs = await service.getDiff({ from: baseHash, to: commit }) |
| 218 | + expect(diffs).toHaveLength(1) |
| 219 | + expect(diffs[0].paths.relative).toBe("written-by-the-tool.txt") |
| 220 | + expect(diffs[0].content.before).toBe("") |
| 221 | + expect(diffs[0].content.after).toBe("the bytes a write handed to the workspace") |
| 222 | + |
| 223 | + // Still untracked in the user's repository: the shadow git captured it, the workspace repo did |
| 224 | + // not, which is the difference the contents assertion is for. |
| 225 | + expect(await workspaceGit.raw(["status", "--porcelain"])).toContain("?? written-by-the-tool.txt") |
| 226 | + }, 30_000) |
| 227 | +}) |
0 commit comments