Skip to content

Commit 3e42783

Browse files
axisrowclaude
andcommitted
fix(worktree): preserve ignored-only worktree changes (no silent loss)
Cycle-3 review (Codex, final cycle) found a fourth data-loss path in the same fail-open class. `git add -A` skips git-ignored paths (.env, dist/, .output, build artifacts) — so a worktree whose only changes landed in ignored files produces an empty patch. cleanupWorktreeSession then returned "No changes to apply" and force-removed the worktree + branch, irreversibly destroying the artifacts. Confirmed empirically; this very repo ignores exactly .env/dist/ .output — paths Codex/rescue writes to routinely. Fix: when keep produces an empty tracked patch, check hasIgnoredChanges() (`git status --porcelain --ignored -uall`, !! entries). If ignored work exists, preserve the worktree and branch and surface the situation in the result detail instead of removing. Fail-safe: never lose work silently. If the user wants those ignored files gone, they remove the worktree explicitly. Regression test: worktree.test.mjs "keep preserves the worktree when only ignored files changed" — creates dist/artifact.txt in an ignored worktree, asserts keep returns applied:false + "ignored files" message AND the worktree + artifact survive. Verified: worktree 15/15, process 5/5, git 11/11 green; full npm test 158 pass / 4 pre-existing (unchanged). Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_0189cGaohsYdpkB4otKZnpAt
1 parent 5748958 commit 3e42783

3 files changed

Lines changed: 59 additions & 0 deletions

File tree

‎plugins/codex/scripts/lib/git.mjs‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,21 @@ export function deleteWorktreeBranch(repoRoot, branch) {
345345
git(repoRoot, ["branch", "-D", branch]);
346346
}
347347

348+
// Detect uncommitted work that `git add -A` skipped because it is git-ignored
349+
// (e.g. .env, dist/, build artifacts Codex wrote). A worktree whose only changes
350+
// are ignored would produce an empty patch and be mistaken for "no changes" —
351+
// force-removing it would irreversibly destroy that work. Returns true if any
352+
// ignored file in the worktree differs from the base worktree state.
353+
export function hasIgnoredChanges(worktreePath) {
354+
// --porcelain: !! marks ignored entries. Only ignored files that exist as
355+
// working-tree content count; an empty worktree (fresh from HEAD) lists none.
356+
const result = git(worktreePath, ["status", "--porcelain", "--ignored", "-uall"]);
357+
if (result.status !== 0) {
358+
return false;
359+
}
360+
return result.stdout.split("\n").some((line) => line.startsWith("!! ") && line.length > 3);
361+
}
362+
348363
export function getWorktreeDiff(worktreePath, baseCommit) {
349364
// gitChecked (not git): a failed snapshot (e.g. index lock held by a concurrent
350365
// git op) must throw, not be classified as "no changes" — otherwise callers would

‎plugins/codex/scripts/lib/worktree.mjs‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
deleteWorktreeBranch,
55
getWorktreeDiff,
66
applyWorktreePatch,
7+
hasIgnoredChanges,
78
ensureGitRepository
89
} from "./git.mjs";
910

@@ -22,6 +23,16 @@ export function cleanupWorktreeSession(session, { keep = false } = {}) {
2223
if (!result.applied && result.detail !== "No changes to apply.") {
2324
return result;
2425
}
26+
// "No changes to apply" via `git add -A` skips git-ignored paths. If the
27+
// worktree holds ignored-only work (e.g. Codex wrote .env, dist/, build
28+
// artifacts), force-removing it would irreversibly destroy it. Preserve the
29+
// worktree + branch and surface the situation instead. (openai#137 review.)
30+
if (!result.applied && hasIgnoredChanges(session.worktreePath)) {
31+
return {
32+
applied: false,
33+
detail: `No tracked changes to apply, but the worktree has ignored files (e.g. .env, dist/) at ${session.worktreePath}. Preserving the worktree and branch ${session.branch} — inspect and copy them manually before removing.`
34+
};
35+
}
2536
removeWorktree(session.repoRoot, session.worktreePath);
2637
deleteWorktreeBranch(session.repoRoot, session.branch);
2738
return result;

‎tests/worktree.test.mjs‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,3 +289,36 @@ test("cleanupWorktreeSession keep round-trips a non-UTF-8 byte (0xff) without co
289289
cleanupSession(session);
290290
}
291291
});
292+
293+
// Regression: "No changes to apply" must not force-remove a worktree that holds
294+
// ignored-only work. `git add -A` skips git-ignored paths (.env, dist/, build
295+
// artifacts), so an ignored-only worktree produces an empty patch. Before the
296+
// fix, keep would force-remove it and lose the artifacts irreversibly. Now the
297+
// worktree + branch are preserved and the result surfaces the situation.
298+
test("cleanupWorktreeSession keep preserves the worktree when only ignored files changed", () => {
299+
const { repoRoot } = createRepoWithInitialCommit();
300+
// Make this test repo ignore dist/ so the worktree's artifact is ignored.
301+
fs.writeFileSync(path.join(repoRoot, ".gitignore"), "dist/\n");
302+
assert.equal(run("git", ["add", ".gitignore"], { cwd: repoRoot }).status, 0);
303+
assert.equal(run("git", ["commit", "-m", "gitignore"], { cwd: repoRoot }).status, 0);
304+
305+
const session = createWorktreeSession(repoRoot);
306+
307+
try {
308+
fs.mkdirSync(path.join(session.worktreePath, "dist"), { recursive: true });
309+
fs.writeFileSync(path.join(session.worktreePath, "dist", "artifact.txt"), "precious\n");
310+
311+
const result = cleanupWorktreeSession(session, { keep: true });
312+
313+
assert.equal(result.applied, false);
314+
assert.match(result.detail, /ignored files/);
315+
// The worktree and its ignored artifact MUST be preserved.
316+
assert.equal(fs.existsSync(session.worktreePath), true);
317+
assert.equal(
318+
fs.readFileSync(path.join(session.worktreePath, "dist", "artifact.txt"), "utf8"),
319+
"precious\n"
320+
);
321+
} finally {
322+
cleanupSession(session);
323+
}
324+
});

0 commit comments

Comments
 (0)