Skip to content

Commit 70afcfa

Browse files
committed
fix(checkpoints): user-disabled checkpoint saves resolve silently for write-metadata saves
Write-metadata saves now throw only when the task still expects checkpoints (enableCheckpoints true at save start = genuine init-failure). When the user disabled checkpoints, the absence of rollback metadata is the user's choice and the save resolves silently, matching pre-B3 tool behavior. Documented in the PR description.
1 parent 305ef64 commit 70afcfa

3 files changed

Lines changed: 51 additions & 17 deletions

File tree

‎src/core/checkpoints/__tests__/checkpointJournal.test.ts‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -205,14 +205,14 @@ describe("checkpointSave change-journal wiring (B2)", () => {
205205
consoleErrorSpy.mockRestore()
206206
})
207207

208-
it("fails a write-metadata save when the checkpoint service is unavailable", async () => {
209-
// A checkpoint initialization failure (missing Git or global storage)
210-
// leaves the service unavailable; a write-metadata save must surface
211-
// an explicit failure instead of a tool success over an orphaned
212-
// write with no journal entry and no change card.
208+
it("resolves a write-metadata save when the user disabled checkpoints", async () => {
209+
// Documented contract: user-disabled checkpoints means the absence of
210+
// rollback metadata is the user's choice, so the save must not fail the
211+
// write tool (no journal entry, no change card, no throw). Genuine
212+
// init-failure coverage lives in checkpointSave.spec.ts.
213213
mockTask.enableCheckpoints = false
214214

215-
await expect(checkpointSave(mockTask as Task, false, true, write)).rejects.toThrow(/checkpoint service/)
215+
await expect(checkpointSave(mockTask as Task, false, true, write)).resolves.toBeUndefined()
216216
expect(saveCheckpointSpy).not.toHaveBeenCalled()
217217
expect(await loadChanges(tmpStorageDir, TASK_ID)).toEqual([])
218218
})

‎src/core/checkpoints/__tests__/checkpointSave.spec.ts‎

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,35 @@ vi.mock("@roo-code/telemetry", () => ({
2525
* (journal dir + setting state), and say. Structural cast at the boundary,
2626
* matching the documented test-double style of the tool specs.
2727
*/
28-
function makeTask(options: { saveCheckpoint?: unknown; state?: Record<string, unknown>; enableCheckpoints?: boolean }) {
28+
function makeTask(options: {
29+
saveCheckpoint?: unknown
30+
state?: Record<string, unknown>
31+
enableCheckpoints?: boolean
32+
storageDir?: string | null
33+
}) {
2934
const say = vi.fn().mockResolvedValue(undefined)
3035
// An explicit `saveCheckpoint: undefined` (a checkpoint that produced no
3136
// commit) must be preserved as-is; only an omitted option falls back to
3237
// the default commit result.
3338
const saveCheckpoint = vi
3439
.fn()
3540
.mockResolvedValue("saveCheckpoint" in options ? options.saveCheckpoint : { commit: "sha-card-1" })
41+
// `storageDir: null` simulates a checkpoint-service init failure: the
42+
// service lookup dereferences globalStorageUri, throws, and resolves to
43+
// no service while the task still expects checkpoints (enableCheckpoints
44+
// stays true until the failed lookup flips it).
3645
const providerDeref = {
37-
context: { globalStorageUri: { fsPath: globalStorageDir } },
46+
context: { globalStorageUri: options.storageDir === null ? undefined : { fsPath: globalStorageDir } },
3847
getState: vi.fn().mockResolvedValue(options.state ?? {}),
3948
}
4049

4150
const task = {
4251
taskId: "task-card",
4352
cwd: "/workspace",
4453
enableCheckpoints: options.enableCheckpoints ?? true,
45-
checkpointService: {
46-
saveCheckpoint,
47-
isInitialized: true,
48-
},
54+
// With `storageDir: null` there is no preset service either, so the
55+
// lookup runs for real and fails on the missing storage dir.
56+
checkpointService: options.storageDir === null ? undefined : { saveCheckpoint, isInitialized: true },
4957
providerRef: { deref: vi.fn().mockReturnValue(providerDeref) },
5058
say,
5159
} as unknown as Task
@@ -208,8 +216,25 @@ describe("checkpointSave change-card emission (B3a)", () => {
208216
expect(cardCalls).toHaveLength(0)
209217
})
210218

211-
it("fails a write-metadata save when checkpoints are disabled for the task and emits no card", async () => {
212-
const { task, say } = makeTask({ enableCheckpoints: false })
219+
it("resolves a write-metadata save when checkpoints are disabled for the task and emits no card", async () => {
220+
// Documented contract: user-disabled checkpoints means the absence of
221+
// rollback metadata is the user's choice, so the save must not fail the
222+
// write tool.
223+
const { task, say, saveCheckpoint } = makeTask({ enableCheckpoints: false })
224+
225+
await expect(
226+
checkpointSave(task, false, true, { path: "src/a.ts", operation: "create", diff: "+x" }),
227+
).resolves.toBeUndefined()
228+
229+
expect(saveCheckpoint).not.toHaveBeenCalled()
230+
expect(say).not.toHaveBeenCalled()
231+
})
232+
233+
it("fails a write-metadata save when the checkpoint-service lookup fails and emits no card", async () => {
234+
// Documented contract: with checkpoints requested (enableCheckpoints
235+
// true) a failed service lookup is a genuine init-failure - the write
236+
// would be orphaned, so the save must surface it.
237+
const { task, say } = makeTask({ storageDir: null })
213238

214239
await expect(
215240
checkpointSave(task, false, true, { path: "src/a.ts", operation: "create", diff: "+x" }),

‎src/core/checkpoints/index.ts‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,16 +261,25 @@ export async function checkpointSave(
261261
write?: CheckpointWriteInfo | CheckpointWriteInfo[],
262262
) {
263263
// A save that carries write metadata must end with rollback metadata
264-
// existing: every failure below (service unavailable, no commit, no
264+
// existing: every failure below (service init-failure, no commit, no
265265
// journal location, journal write failure) surfaces explicitly instead
266-
// of resolving to a tool success over an orphaned write.
266+
// of resolving to a tool success over an orphaned write. A user-disabled
267+
// checkpoint setting is NOT such a failure: absence of rollback metadata
268+
// is then the user's explicit choice, so the save resolves silently
269+
// (documented contract, see PR description).
267270
const journalWrites = write === undefined ? undefined : Array.isArray(write) ? write : [write]
268271
const hasWriteMetadata = !!journalWrites && journalWrites.length > 0
269272

273+
// Captured before the lookup: getCheckpointService flips the flag off when
274+
// initialization fails, so reading it after the call cannot distinguish
275+
// "user disabled checkpoints" (resolve silently) from "init failed while
276+
// the user expects checkpoints" (throw).
277+
const checkpointsRequested = task.enableCheckpoints
278+
270279
const service = await getCheckpointService(task)
271280

272281
if (!service) {
273-
if (hasWriteMetadata) {
282+
if (hasWriteMetadata && checkpointsRequested) {
274283
throw new Error(
275284
"Checkpoint save with write metadata could not initialize the checkpoint service; the write has no rollback metadata.",
276285
)

0 commit comments

Comments
 (0)