fix(adopt): confine, gate and splice writes instead of overwriting blindly

apply_plan was the only path in the package that writes to disk and the only one
that skipped confined_path, so an absolute path in a hand-edited plan file
replaced the repository root outright (pathlib join semantics) and wrote
anywhere. It also treated patch-managed-block as a full-file write, meaning the
first plan to emit that action would have destroyed the target CLAUDE.md rather
than patching it -- two bugs that only stayed harmless because build_plan never
emitted the action. Learned that forbiddenChanges and the plan's own
"path-confinement" validation entry were decorative: nothing ever checked them.

- confine every change path via the existing confined_path helper
- implement real managed-block splicing between markers; refuse malformed or
  duplicated markers rather than guessing
- enforce forbiddenChanges at apply time
- re-check the worktree at apply time and refuse only when a dirty path is one
  the plan writes, leaving unrelated user edits alone
- restore rollback by what was on disk, not by what we think we finished
- add gitutil.dirty_paths/ignored; is_dirty threw the path list away
- replace the CLI's guess-the-code-from-the-message mapping with AdoptionError

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Alexander Baranov
2026-08-24 18:25:45 +03:00
co-authored by Claude Opus 5
parent 36d5d01d43
commit c918bd8373
3 changed files with 179 additions and 17 deletions
+52
View File
@@ -33,3 +33,55 @@ def tracked_files(root: Path) -> set[str]:
if result.returncode != 0:
return set()
return {line.strip() for line in result.stdout.splitlines() if line.strip()}
def dirty_paths(root: Path) -> set[str]:
"""Return every repository-relative path with staged, unstaged or untracked changes.
`is_dirty` collapses this to a bool; adoption needs the paths themselves so
`apply` can refuse to write into a file the user is already editing. Uses ``-z``
so paths containing spaces or non-ASCII are neither quoted nor mangled, and so
rename entries split reliably: a rename emits the destination path, then a
second NUL-terminated field holding the original path.
"""
result = _run(["status", "--porcelain", "-z"], root)
if result.returncode != 0:
return set()
fields = [field for field in result.stdout.split("\0") if field]
paths: set[str] = set()
index = 0
while index < len(fields):
entry = fields[index]
index += 1
if len(entry) < 4:
continue
status, path = entry[:2], entry[3:]
paths.add(path)
if ("R" in status or "C" in status) and index < len(fields):
paths.add(fields[index])
index += 1
return paths
def ignored(root: Path, relative_paths: list[str]) -> set[str]:
"""Return the subset of ``relative_paths`` that Git ignores.
Used to keep the secret-shaped scan quiet about gitignored material and to
avoid walking large ignored trees. Returns an empty set when Git is
unavailable, so callers degrade to reporting everything rather than nothing.
"""
if not relative_paths:
return set()
# `check-ignore` needs the candidates on stdin; `_run` does not pipe input,
# so issue the call directly here rather than widening the shared helper.
result = subprocess.run(
["git", "check-ignore", "-z", "--stdin"],
cwd=root,
input="\0".join(relative_paths),
capture_output=True,
text=True,
check=False,
)
if result.returncode not in (0, 1):
return set()
return {field for field in result.stdout.split("\0") if field}