Skip to content

fix: keep a file's mode, final newline and encoding through repo writes - #89

Merged
youdie006 merged 2 commits into
mainfrom
fix/repo-write-tools
Nov 9, 2026
Merged

youdie006 merged 2 commits into
mainfrom
fix/repo-write-tools

Conversation

@youdie006

Copy link
Copy Markdown
Owner

Problems

Found 2026-11-09 by calling the MCP repo tool handlers (createMcpToolHandlers) against throwaway git repos:

  1. File mode reset on apply. replaceVerifiedUtf8File writes a temp file and renames it into place. Repo writes passed no mode, so the temp file got the default (0666 & ~umask). An edited build.sh went 755 -> 644, and repo_stage_reviewed_paths staged mode change 100755 => 100644. A 600 file became 644.
  2. Reviewed paths read as pathspecs.
    • A reviewed pages/[id].tsx also staged an unreviewed local edit of pages/i.tsx, while the receipt listed one path.
    • A reviewed x[0-9].txt next to a modified x1.txt failed as "Staged content changed". The index restore then wrote x1.txt's blob under x[0-9].txt: ls-files matched the pattern, and update-index --cacheinfo is literal.
    • A file named :!draft.md left every other modified and untracked file staged.
  3. Final newline dropped. repo_read_file joins the lines it returns with no newline after the last one, so writing back what was read removed the file's final newline.
  4. Non-UTF-8 files corrupted. The write path decoded lossily and wrote U+FFFD back.
    • In an EUC-KR file, editing only the last line destroyed the Korean text of the first, and the diff showed that line as unchanged.
    • A UTF-16 file was rewritten as UTF-8 without its BOM.
  5. Size limit checked too early. The dry run checked it before CRLF and the BOM were restored, so apply refused content the dry run had accepted, blaming the target file.

Changes

  • src/safe-file.ts: replaceVerifiedUtf8File uses the replaced file's mode (& 0o777) unless a mode is given. Every other caller that writes secrets already passes a mode. New requireUtf8 read option (isUtf8) throws NotUtf8Error.
  • src/repo-write.ts:
    • git add, ls-files --stage and rm --cached run with --literal-pathspecs.
    • keepFileConventions keeps a final newline (CRLF for a CRLF file) unless the new content is empty.
    • The write path reads strictly and maps NotUtf8Error to a message saying why.
    • The dry run checks the limit again on the content that will be written.
  • src/mcp.ts: the repo_write_file_dry_run tool description and docs/http-mcp.md describe the kept conventions and the refusal.
  • CHANGELOG.md: one entry per defect.

Verification

  • tests/repo-write-keep.test.ts: 7 cases, 6 fail against main. The seventh, emptying a file, is a guard for the newline rule. All 7 pass here.
  • tests/repo-write-line-endings.test.ts and tests/repo-utf16.test.ts still pass.
  • The original repro scripts no longer reproduce any of the five.
  • npx tsc --noEmit passes. Full suite: 1870 passed, 3 skipped. The first run failed only the two package-bin tests, because the fresh worktree had no dist/ yet; they pass after npm run build.
  • Not covered: Windows. The mode test and the :! file-name test are skipped there, and CI runs the rest.

Five defects in the MCP repo write tools, found 2026-11-09 by driving the
tool handlers against throwaway repos:

- apply reset the file mode. The replacement is a temp file renamed into
  place and was created with the default mode, so an edited build.sh went
  from 755 to 644 and repo_stage_reviewed_paths staged the mode change; a
  600 file became 644. The replacement now takes the replaced file's mode.
- repo_stage_reviewed_paths passed reviewed paths to git as pathspecs. A
  reviewed pages/[id].tsx also staged an unreviewed pages/i.tsx; a reviewed
  x[0-9].txt next to a modified x1.txt failed, and the index restore wrote
  x1.txt's blob under x[0-9].txt; ":!draft.md" left every other change
  staged. git add, ls-files and rm --cached now run with
  --literal-pathspecs.
- Writing back what repo_read_file returned dropped the final newline,
  since the read joins lines without one after the last. A file that ends
  with a newline keeps it (CRLF for a CRLF file) unless the new content is
  empty.
- A file that is not UTF-8 was read lossily and written back with its
  bytes replaced (an EUC-KR line destroyed while the diff showed it
  unchanged); UTF-16 was re-encoded as UTF-8 without its BOM. The write
  path now reads strictly and the dry run refuses such a file.
- The dry run checked the size limit before restoring CRLF and a BOM, so
  apply refused content the dry run had accepted. The limit is now also
  checked on the content that will be written.

Verified: tests/repo-write-keep.test.ts fails 6 of 7 against main and
passes here; the subagent's repro scripts no longer reproduce any of the
five; npx tsc --noEmit; full suite 1870 passed, 3 skipped after npm run
build (the first run failed only package-bin tests, from a worktree with
no dist yet).

Claude-Session: https://claude.ai/code/session_01EMqNwnnhHXznd2P2PDsaDV
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 9, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-09T01:14:16.374870Z afd8ae4 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@github-actions

github-actions Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

What this touches

  • bridge store
  • MCP
  • tests

6 file(s), 189 line(s) changed. Tests changed.

Worth a look

  • Careful read: touches what gets written to a repo.

This is an automated first pass, not a review. CI is the gate; a person decides.

@youdie006
youdie006 merged commit 8d4290c into main Nov 9, 2026
7 checks passed
youdie006 added a commit that referenced this pull request Nov 10, 2026
… through repo writes) to the container image

Same change as main PR #89 (merge 8d4290c), released in 0.40.51, applied to
src, tests and docs without changes. The CHANGELOG lines are main's with
the PR noted.

Claude-Session: https://claude.ai/code/session_01EMqNwnnhHXznd2P2PDsaDV
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant