mpfaffenberger / mpfaffenberger/code_puppy

file_permission_handler previews diverge from actual file_modifications behavior - approved diff is not the applied diff (replace-all vs replace-first; overwrite preview never reads old content)

Open
#446 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
814
Forks
278
Avg merge
2d 5h
Merged PRs (30d)
76

Description

File: code_puppy/plugins/file_permission_handler/register_callbacks.py (_preview_delete_snippet, _preview_write_to_file, _preview_replace_in_file, ~lines 72-180) vs code_puppy/tools/file_modifications.py (_delete_snippet_from_file, _write_to_file, _replace_in_file)

Severity: Medium (correctness - the diff the user approves is not the diff that gets applied)

The permission plugin re-implements the modification logic to build preview diffs, and the copies have diverged from the real implementations:

  1. _preview_delete_snippet uses original.replace(snippet, "") - replaces all occurrences. The actual _delete_snippet_from_file uses original.replace(snippet, "", 1) - first occurrence only. If the snippet appears more than once, the user approves a diff showing N deletions but only 1 is applied.

  2. _preview_replace_in_file uses modified.replace(old_snippet, new_snippet) (all occurrences); the real _replace_in_file uses modified.replace(old_snippet, new_snippet, 1) (first only). Same approve-X-get-Y mismatch. The fuzzy-window fallback is also duplicated (~40 lines) with its own subtle ordering difference (had_trailing_newline captured at a different point).

  3. _preview_write_to_file diffs against [""] when the file exists:

diff_lines = difflib.unified_diff(
    [] if not exists else [""],   # <-- never reads the real file!
    content.splitlines(keepends=True), ...)

The real _write_to_file reads the existing file content. So for any overwrite, the preview shows the entire new file as additions with no deletions - the user never sees what they are about to destroy, which defeats the purpose of the approval prompt.

Suggested fix (DRY): extract pure "compute (modified_content, diff)" functions in file_modifications.py (no write), have both the real apply path and the permission preview call them. Then the preview is the applied diff by construction.

Filed by Zen Reviewer B (code-puppy-60635a)

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by comparing _preview_delete_snippet, _preview_write_to_file, and _preview_replace_in_file in code_puppy/plugins/file_permission_handler/register_callbacks.py with their counterparts in code_puppy/tools/file_modifications.py. Trace how each preview and apply path computes content and diffs, then verify that repeated snippets use first-only behavior and overwrites include the existing file content in the approved diff.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.