Skip to content

Commit 548231c

Browse files
committed
fix(checkpoints): review follow-ups row 11 (excluded-path ordering, negation rules, flipped flag)
A: the excluded-path filter now runs BEFORE the no-commit rejection - an all-excluded step stages nothing, so it resolves instead of failing when unrelated workspace cleanliness decides the tool outcome. B: isPathIgnored drops check-ignore -v: under -v a negation rule (e.g. '!.env.example' after '.env*') prints a match and misclassifies a re-included path as ignored; without -v only genuinely ignored paths print, and the verdict still comes from the output, not from promise settlement (simple-git raw() resolves on a silent non-zero exit). C: when the task flag is off but the persisted setting still requests checkpoints (an earlier init/save failure flipped the flag), a write-metadata save throws instead of silently losing rollback metadata; only a user-disabled setting resolves silently.
1 parent 075adc7 commit 548231c

5 files changed

Lines changed: 110 additions & 32 deletions

File tree

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,26 @@ describe("checkpointSave change-journal wiring (B2)", () => {
177177
expect(sayMock).toHaveBeenCalledTimes(1)
178178
})
179179

180+
it("resolves an all-excluded write when the checkpoint produces no commit", async () => {
181+
// An excluded path is never staged, so an all-excluded step has nothing
182+
// to commit: the save must resolve (the write succeeded and no rollback
183+
// metadata is expected) instead of failing on the no-commit rejection,
184+
// whose outcome would otherwise depend on unrelated workspace changes.
185+
saveCheckpointSpy.mockResolvedValueOnce(undefined)
186+
mockTask.checkpointService.isPathIgnored = vi.fn().mockResolvedValue(true)
187+
188+
await expect(
189+
checkpointSave(mockTask as Task, false, true, {
190+
path: "node_modules/pkg/index.js",
191+
operation: "create",
192+
diffStats: { additions: 1, deletions: 0 },
193+
}),
194+
).resolves.toBeUndefined()
195+
196+
expect(await loadChanges(tmpStorageDir, TASK_ID)).toEqual([])
197+
expect(mockTask.say as Mock).not.toHaveBeenCalled()
198+
})
199+
180200
it("omits diffStats in the journal entry when not provided", async () => {
181201
await checkpointSave(mockTask as Task, false, true, { path: "src/bar.ts", operation: "update" })
182202

@@ -248,8 +268,11 @@ describe("checkpointSave change-journal wiring (B2)", () => {
248268
// Documented contract: user-disabled checkpoints means the absence of
249269
// rollback metadata is the user's choice, so the save must not fail the
250270
// write tool (no journal entry, no change card, no throw). Genuine
251-
// init-failure coverage lives in checkpointSave.spec.ts.
271+
// init-failure coverage lives in checkpointSave.spec.ts. The
272+
// persisted setting agrees the user disabled checkpoints (an unset
273+
// setting would mean an earlier failure flipped the task flag).
252274
mockTask.enableCheckpoints = false
275+
mockProvider.getState = vi.fn().mockResolvedValue({ enableCheckpoints: false })
253276

254277
await expect(checkpointSave(mockTask as Task, false, true, write)).resolves.toBeUndefined()
255278
expect(saveCheckpointSpy).not.toHaveBeenCalled()

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,12 @@ describe("checkpointSave change-card emission (B3a)", () => {
223223
// Documented contract: user-disabled checkpoints means the absence of
224224
// rollback metadata is the user's choice, so the save must not fail the
225225
// write tool.
226-
const { task, say, saveCheckpoint } = makeTask({ enableCheckpoints: false })
226+
const { task, say, saveCheckpoint } = makeTask({
227+
enableCheckpoints: false,
228+
// The persisted setting agrees the user disabled checkpoints; an unset or
229+
// true setting would mean an earlier failure flipped the task flag.
230+
state: { enableCheckpoints: false },
231+
})
227232

228233
await expect(
229234
checkpointSave(task, false, true, { path: "src/a.ts", operation: "create", diff: "+x" }),
@@ -246,6 +251,20 @@ describe("checkpointSave change-card emission (B3a)", () => {
246251
expect(say).not.toHaveBeenCalled()
247252
})
248253

254+
it("fails a write-metadata save when an earlier failure flipped the task flag", async () => {
255+
// getCheckpointService and the save-failure handlers flip
256+
// task.enableCheckpoints off; the persisted setting still says the user
257+
// wants checkpoints, so a later write-metadata save must throw too
258+
// instead of silently proceeding with no rollback metadata.
259+
const { task, say } = makeTask({ enableCheckpoints: false, state: { enableCheckpoints: true } })
260+
261+
await expect(
262+
checkpointSave(task, false, true, { path: "src/a.ts", operation: "create", diff: "+x" }),
263+
).rejects.toThrow(/could not initialize the checkpoint service/)
264+
265+
expect(say).not.toHaveBeenCalled()
266+
})
267+
249268
it("keeps the journal append when a card emission failure occurs", async () => {
250269
const { task, say } = makeTask({})
251270
say.mockImplementation(async (type: string) => {

‎src/core/checkpoints/index.ts‎

Lines changed: 43 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,19 @@ export async function checkpointSave(
279279
const service = await getCheckpointService(task)
280280

281281
if (!service) {
282-
if (hasWriteMetadata && checkpointsRequested) {
282+
let requested = checkpointsRequested
283+
if (hasWriteMetadata && !requested) {
284+
// The task flag also flips off when an EARLIER failure disabled
285+
// checkpoints (a failed service init, or the save-failure handlers
286+
// below). The persisted setting still carries the user's intent
287+
// (unset defaults to enabled, matching Task construction), so a
288+
// write-metadata save after such a failure must throw too instead
289+
// of silently proceeding with no rollback metadata. Only a user who
290+
// disabled checkpoints in settings resolves silently.
291+
const state = await task.providerRef.deref()?.getState()
292+
requested = state?.enableCheckpoints ?? true
293+
}
294+
if (hasWriteMetadata && requested) {
283295
throw new Error(
284296
"Checkpoint save with write metadata could not initialize the checkpoint service; the write has no rollback metadata.",
285297
)
@@ -335,10 +347,36 @@ export async function checkpointSave(
335347
})
336348
}
337349

338-
// A save that resolves without a commit (e.g. a write to a Git-ignored
339-
// file the shadow repository never stages) produced no rollback point:
340-
// journaling is skipped and a later restore reports no entry. With
341-
// write metadata that is a partial persistence, not a success.
350+
// Paths excluded from the shadow repository (node_modules, build
351+
// output, .gitignore'd files) never enter the journal: the shadow
352+
// commit cannot contain them, so a journal entry would route a later
353+
// rollback into restoreFile's delete branch, where absence from the
354+
// commit would delete the user's live file. Skipping excluded paths
355+
// at journaling time keeps rollback metadata honest and removes
356+
// excluded-path noise from the journal and the change card. The
357+
// filter runs BEFORE the no-commit rejection below: an all-excluded
358+
// step stages nothing, so requiring a commit would make the tool's
359+
// outcome depend on unrelated workspace changes.
360+
const writes: CheckpointWriteInfo[] = []
361+
if (hasWriteMetadata) {
362+
for (const w of journalWrites ?? []) {
363+
if (await service.isPathIgnored(w.path)) {
364+
continue
365+
}
366+
writes.push(w)
367+
}
368+
if (writes.length === 0) {
369+
// Every write landed on an excluded path: nothing to journal and
370+
// nothing to show, and no rollback metadata is expected - the save
371+
// resolves whether or not unrelated changes produced a commit.
372+
return result
373+
}
374+
}
375+
376+
// A save that resolves without a commit despite non-excluded writes
377+
// produced no rollback point: journaling is skipped and a later
378+
// restore reports no entry. With write metadata that is a partial
379+
// persistence, not a success.
342380
if (hasWriteMetadata && !result?.commit) {
343381
throw new Error(
344382
"Checkpoint save with write metadata did not produce a commit; the write has no rollback metadata.",
@@ -352,25 +390,6 @@ export async function checkpointSave(
352390
// changes can still produce a commit, and emitting a card for it would
353391
// show a change card with zero files. The checkpoint itself still runs.
354392
if (result?.commit && hasWriteMetadata) {
355-
// Paths excluded from the shadow repository (node_modules, build
356-
// output, .gitignore'd files) never enter the journal: the shadow
357-
// commit cannot contain them, so a journal entry would route a later
358-
// rollback into restoreFile's delete branch, where absence from the
359-
// commit would delete the user's live file. Skipping excluded paths
360-
// at journaling time keeps rollback metadata honest and removes
361-
// excluded-path noise from the journal and the change card.
362-
const writes: CheckpointWriteInfo[] = []
363-
for (const w of journalWrites ?? []) {
364-
if (await service.isPathIgnored(w.path)) {
365-
continue
366-
}
367-
writes.push(w)
368-
}
369-
if (writes.length === 0) {
370-
// Every write landed on an excluded path: nothing to journal and
371-
// nothing to show; the checkpoint commit itself still ran.
372-
return result
373-
}
374393
if (!journalDir) {
375394
// Defensive: the pre-commit guard above normally makes this
376395
// unreachable. If the location is nonetheless gone while the commit

‎src/services/checkpoints/ShadowCheckpointService.ts‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -584,12 +584,16 @@ export abstract class ShadowCheckpointService extends EventEmitter {
584584
try {
585585
// --no-index evaluates the exclude rules alone: index or commit
586586
// membership is irrelevant to "the shadow repo never tracks this".
587-
// The verdict comes from the -v output, not from the promise
588-
// settling: simple-git's raw() resolves (rather than rejects) on a
589-
// silent non-zero exit, so "not ignored" (exit 1, no output) arrives
590-
// as an empty success here - the same defect documented in
591-
// fileExistsInCommit. An ignored path prints its matching rule line.
592-
const match = await this.git.raw(["check-ignore", "--no-index", "-v", "--", gitPath])
587+
// No -v: verbose mode prints a line for ANY matching rule, and a
588+
// negation rule (e.g. "!.env.example" after ".env*") means the path
589+
// is NOT ignored - under -v it would be misclassified as ignored.
590+
// Without -v only genuinely ignored paths print a line. The verdict
591+
// still comes from the output, not from the promise settling:
592+
// simple-git's raw() resolves (rather than rejects) on a silent
593+
// non-zero exit, so "not ignored" (exit 1, no output) arrives as an
594+
// empty success here - the same defect documented in
595+
// fileExistsInCommit.
596+
const match = await this.git.raw(["check-ignore", "--no-index", "--", gitPath])
593597
return match.trim().length > 0
594598
} catch {
595599
// A git fatal (corrupt repo) leaves the path unproven-ignored;

‎src/services/checkpoints/__tests__/ShadowCheckpointService.spec.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,19 @@ describe.each([[RepoPerTaskCheckpointService, "RepoPerTaskCheckpointService"]])(
222222
expect(await fileExistsAtPath(ignoredFile)).toBe(true)
223223
})
224224

225+
it("treats a negation rule as not-ignored in isPathIgnored", async () => {
226+
// A negation rule re-includes a path matched by an earlier rule
227+
// (".env*" then "!.env.example"). check-ignore -v prints the negation
228+
// match and would misclassify the file as ignored; without -v only
229+
// genuinely ignored paths print, so isPathIgnored must agree with
230+
// what the shadow repo actually stages.
231+
const excludesPath = path.join(service.checkpointsDir, ".git", "info", "exclude")
232+
await fs.appendFile(excludesPath, "\n.env*\n!.env.example\n")
233+
234+
expect(await service.isPathIgnored(".env")).toBe(true)
235+
expect(await service.isPathIgnored(".env.example")).toBe(false)
236+
})
237+
225238
it("accepts a native backslashed path on Windows (git pathspecs are POSIX)", async () => {
226239
if (process.platform !== "win32") {
227240
// Off-Windows the journal paths are already POSIX, so there is

0 commit comments

Comments
 (0)