-
Notifications
You must be signed in to change notification settings - Fork 253
feat(checkpoints): per-file and per-step rollback service (B3c, #1375) #1410
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
easonLiangWorldedtech
wants to merge
8
commits into
Zoo-Code-Org:main
Choose a base branch
from
easonLiangWorldedtech:feat/rollback-service-b3c
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
abfbe7f
feat(checkpoints): per-write checkpoints, task-start baseline, and pe…
easonliang28 93a8329
feat(checkpoints): per-task change journal with torn-tail repair (B2,…
easonliang28 2500ab3
feat(checkpoints): per-step change cards and changeCardDetail setting…
easonliang28 d0c60bf
feat(checkpoints): per-file and per-step rollback service (B3c, #1375)
easonliang28 d64389c
fix(fws): validate checkpoint availability + contain symlinked restor…
easonliang28 630f273
fix(checkpoints): roll change-card files back to their pre-step state…
easonliang28 77c809c
ci: retry ubuntu unit test (flaky vitest-worker teardown race in Task…
easonliang28 39afe1b
fix(checkpoints): reject stale-card rollback and fail on unreadable c…
easonliang28 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| import { describe, expect, it } from "vitest" | ||
|
|
||
| import type { ChangeCardData } from "@roo-code/types" | ||
|
|
||
| import { | ||
| buildChangeCard, | ||
| buildChangeCardPayload, | ||
| isAutoApprovedStep, | ||
| resolveChangeCardDetail, | ||
| type ChangeCardWrite, | ||
| } from "../changeCard" | ||
|
|
||
| describe("changeCard (B3a)", () => { | ||
| function write(overrides: Partial<ChangeCardWrite> = {}): ChangeCardWrite { | ||
| return { | ||
| path: "src/a.ts", | ||
| diffStats: { additions: 2, deletions: 1 }, | ||
| diff: "--- a/src/a.ts\n+++ b/src/a.ts\n@@ -1 +1,2 @@\n-old\n+new-1\n+new-2", | ||
| ...overrides, | ||
| } | ||
| } | ||
|
|
||
| describe("isAutoApprovedStep", () => { | ||
| it("returns false for an empty step", () => { | ||
| expect(isAutoApprovedStep([])).toBe(false) | ||
| }) | ||
|
|
||
| it("returns true only when every write was auto-approved", () => { | ||
| expect(isAutoApprovedStep([write({ autoApproved: true }), write({ autoApproved: true })])).toBe(true) | ||
| expect(isAutoApprovedStep([write({ autoApproved: true }), write()])).toBe(false) | ||
| expect(isAutoApprovedStep([write()])).toBe(false) | ||
| }) | ||
| }) | ||
|
|
||
| describe("resolveChangeCardDetail", () => { | ||
| it("forces summary for auto-approved steps even when the setting is full", () => { | ||
| const writes = [write({ autoApproved: true })] | ||
| expect(resolveChangeCardDetail(writes, "full")).toBe("summary") | ||
| expect(resolveChangeCardDetail(writes, undefined)).toBe("summary") | ||
| }) | ||
|
|
||
| it("follows the setting for interactive steps, defaulting to summary when unset", () => { | ||
| const writes = [write()] | ||
| expect(resolveChangeCardDetail(writes, "full")).toBe("full") | ||
| expect(resolveChangeCardDetail(writes, "summary")).toBe("summary") | ||
| expect(resolveChangeCardDetail(writes, undefined)).toBe("summary") | ||
| }) | ||
| }) | ||
|
|
||
| describe("buildChangeCard", () => { | ||
| it("carries the inline diff per file for full detail on a multi-file step", () => { | ||
| const card = buildChangeCard( | ||
| "sha-1", | ||
| [write(), write({ path: "src/b.ts", diffStats: { additions: 1, deletions: 0 }, diff: "+b" })], | ||
| "full", | ||
| ) | ||
|
|
||
| expect(card).toEqual({ | ||
| checkpointIds: ["sha-1"], | ||
| files: [ | ||
| { | ||
| path: "src/a.ts", | ||
| additions: 2, | ||
| deletions: 1, | ||
| diff: "--- a/src/a.ts\n+++ b/src/a.ts\n@@ -1 +1,2 @@\n-old\n+new-1\n+new-2", | ||
| }, | ||
| { path: "src/b.ts", additions: 1, deletions: 0, diff: "+b" }, | ||
| ], | ||
| totalFiles: 2, | ||
| detail: "full", | ||
| }) | ||
| }) | ||
|
|
||
| it("omits the diff per file for summary detail (lazy fetch is B3b)", () => { | ||
| const card = buildChangeCard("sha-1", [write()], "summary") | ||
|
|
||
| expect(card.files).toEqual([{ path: "src/a.ts", additions: 2, deletions: 1 }]) | ||
| expect(card.files[0]).not.toHaveProperty("diff") | ||
| expect(card.detail).toBe("summary") | ||
| expect(card.totalFiles).toBe(1) | ||
| }) | ||
|
|
||
| it("defaults missing diffStats to zero counts and keeps full detail without diff for a write without one", () => { | ||
| const card = buildChangeCard("sha-1", [write({ diffStats: undefined, diff: undefined })], "full") | ||
|
|
||
| expect(card.files[0]).toEqual({ path: "src/a.ts", additions: 0, deletions: 0 }) | ||
| }) | ||
| }) | ||
|
|
||
| describe("buildChangeCardPayload", () => { | ||
| it("resolves the detail level and builds the payload in one call", () => { | ||
| // The expectations are typed against the shared ChangeCardData | ||
| // contract in @roo-code/types, so the builder's output is checked | ||
| // against the same single source of truth the webview consumes. | ||
| // Interactive step with the full setting: diff inline. | ||
| const full: ChangeCardData = buildChangeCardPayload("sha-1", [write()], "full") | ||
| expect(full.detail).toBe("full") | ||
| expect(full.files[0].diff).toBe("--- a/src/a.ts\n+++ b/src/a.ts\n@@ -1 +1,2 @@\n-old\n+new-1\n+new-2") | ||
|
|
||
| // Auto-approved step with the full setting: compact summary, no diff. | ||
| const compact: ChangeCardData = buildChangeCardPayload("sha-1", [write({ autoApproved: true })], "full") | ||
| expect(compact.detail).toBe("summary") | ||
| expect(compact.files[0]).not.toHaveProperty("diff") | ||
| expect(compact.checkpointIds).toEqual(["sha-1"]) | ||
| expect(compact.totalFiles).toBe(1) | ||
| }) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| import fs from "fs/promises" | ||
| import os from "os" | ||
| import path from "path" | ||
|
|
||
| import { afterEach, beforeEach, describe, expect, it } from "vitest" | ||
|
|
||
| import { appendChange, journalPath, loadChanges, type ChangeJournalEntry } from "../changeJournal" | ||
|
|
||
| describe("changeJournal", () => { | ||
| const taskId = "test-task" | ||
|
|
||
| let tmpRoot: string | ||
|
|
||
| beforeEach(async () => { | ||
| tmpRoot = await fs.mkdtemp(path.join(os.tmpdir(), "b2-journal-")) | ||
| }) | ||
|
|
||
| afterEach(async () => { | ||
| await fs.rm(tmpRoot, { recursive: true, force: true }) | ||
| }) | ||
|
|
||
| function entry(overrides: Partial<ChangeJournalEntry> = {}): ChangeJournalEntry { | ||
| return { | ||
| path: "src/foo.ts", | ||
| operation: "create", | ||
| checkpointId: "abc12345", | ||
| ...overrides, | ||
| } | ||
| } | ||
|
|
||
| describe("appendChange", () => { | ||
| it("writes one JSON line per entry with the documented field shape", async () => { | ||
| await appendChange(tmpRoot, taskId, entry({ operation: "create", checkpointId: "aaa" })) | ||
|
|
||
| const raw = await fs.readFile(journalPath(tmpRoot, taskId), "utf8") | ||
| const lines = raw.split("\n").filter((line) => line !== "") | ||
| expect(lines).toHaveLength(1) | ||
| const parsed = JSON.parse(lines[0]) as ChangeJournalEntry | ||
| expect(parsed.path).toBe("src/foo.ts") | ||
| expect(parsed.operation).toBe("create") | ||
| expect(parsed.checkpointId).toBe("aaa") | ||
| }) | ||
|
|
||
| it("appends multiple entries sequentially", async () => { | ||
| await appendChange(tmpRoot, taskId, entry({ checkpointId: "a" })) | ||
| await appendChange(tmpRoot, taskId, entry({ checkpointId: "b" })) | ||
|
|
||
| const raw = await fs.readFile(journalPath(tmpRoot, taskId), "utf8") | ||
| expect(raw.split("\n").filter((line) => line !== "")).toHaveLength(2) | ||
| }) | ||
| }) | ||
|
|
||
| describe("loadChanges", () => { | ||
| it("returns [] for an absent journal file", async () => { | ||
| expect(await loadChanges(tmpRoot, taskId)).toEqual([]) | ||
| }) | ||
|
|
||
| it("returns [] for an empty journal file", async () => { | ||
| await fs.mkdir(path.dirname(journalPath(tmpRoot, taskId)), { recursive: true }) | ||
| await fs.writeFile(journalPath(tmpRoot, taskId), "") | ||
|
|
||
| expect(await loadChanges(tmpRoot, taskId)).toEqual([]) | ||
| }) | ||
|
|
||
| it("propagates non-ENOENT read failures instead of reporting an empty journal", async () => { | ||
| // A directory at the journal path makes readFile fail with EISDIR — | ||
| // a stand-in for any permission or I/O failure (EACCES etc.). Such a | ||
| // failure must not be swallowed into "no changes": it would let a | ||
| // rollback report a no-op success without reading the history. | ||
| await fs.mkdir(journalPath(tmpRoot, taskId), { recursive: true }) | ||
|
|
||
| await expect(loadChanges(tmpRoot, taskId)).rejects.toMatchObject({ code: "EISDIR" }) | ||
| }) | ||
|
|
||
| it("parses all entries in order with a clean tail", async () => { | ||
| await appendChange(tmpRoot, taskId, entry({ checkpointId: "x" })) | ||
| await appendChange(tmpRoot, taskId, entry({ checkpointId: "y" })) | ||
| await appendChange(tmpRoot, taskId, entry({ checkpointId: "z" })) | ||
|
|
||
| const result = await loadChanges(tmpRoot, taskId) | ||
| expect(result).toHaveLength(3) | ||
| expect(result[0].checkpointId).toBe("x") | ||
| expect(result[1].checkpointId).toBe("y") | ||
| expect(result[2].checkpointId).toBe("z") | ||
| }) | ||
|
|
||
| it("parses a journal whose final line has no trailing newline", async () => { | ||
| await appendChange(tmpRoot, taskId, entry({ checkpointId: "ok" })) | ||
|
|
||
| // Rewrite the file without the trailing newline of the last line. | ||
| const filePath = journalPath(tmpRoot, taskId) | ||
| const content = (await fs.readFile(filePath, "utf8")).replace(/\n$/, "") | ||
| await fs.writeFile(filePath, content) | ||
|
|
||
| const result = await loadChanges(tmpRoot, taskId) | ||
| expect(result).toHaveLength(1) | ||
| expect(result[0].checkpointId).toBe("ok") | ||
| }) | ||
|
|
||
| it("discards a torn final line and returns the complete entries", async () => { | ||
| await appendChange(tmpRoot, taskId, entry({ checkpointId: "ok" })) | ||
|
|
||
| // Append a second line truncated mid-content, with no trailing newline. | ||
| await fs.appendFile(journalPath(tmpRoot, taskId), '{"path":"src/half.ts","operation":"upd') | ||
|
|
||
| const result = await loadChanges(tmpRoot, taskId) | ||
| expect(result).toHaveLength(1) | ||
| expect(result[0].checkpointId).toBe("ok") | ||
| }) | ||
|
|
||
| it("skips a corrupt middle line and still loads the later valid entries", async () => { | ||
| await appendChange(tmpRoot, taskId, entry({ checkpointId: "ok" })) | ||
|
|
||
| // Corrupt the first line in place, then append a valid entry after it. | ||
| await fs.writeFile( | ||
| journalPath(tmpRoot, taskId), | ||
| '"{"path":"src/corrupt.ts","operation":"update"\n' + JSON.stringify(entry({ checkpointId: "after" })) + "\n", | ||
| ) | ||
|
|
||
| const result = await loadChanges(tmpRoot, taskId) | ||
| expect(result).toHaveLength(1) | ||
| expect(result[0].checkpointId).toBe("after") | ||
| }) | ||
|
|
||
| it("does not throw when the entire journal is torn", async () => { | ||
| await appendChange(tmpRoot, taskId, entry({ checkpointId: "first" })) | ||
|
|
||
| // Truncate to a single character — definitely invalid JSON. | ||
| await fs.writeFile(journalPath(tmpRoot, taskId), "{") | ||
|
|
||
| expect(await loadChanges(tmpRoot, taskId)).toEqual([]) | ||
| }) | ||
|
|
||
| it("includes diffStats when present", async () => { | ||
| await appendChange(tmpRoot, taskId, entry({ checkpointId: "s", diffStats: { additions: 5, deletions: 2 } })) | ||
|
|
||
| const result = await loadChanges(tmpRoot, taskId) | ||
| expect(result[0].diffStats).toEqual({ additions: 5, deletions: 2 }) | ||
| }) | ||
|
|
||
| it("omits diffStats when not provided", async () => { | ||
| await appendChange(tmpRoot, taskId, entry({ checkpointId: "n" })) | ||
|
|
||
| const result = await loadChanges(tmpRoot, taskId) | ||
| expect(result[0].diffStats).toBeUndefined() | ||
| }) | ||
| }) | ||
| }) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.