Skip to content

fix: reject directory paths and stale shas in delete_file - #3790

Open
shhhhhhjokerist wants to merge 1 commit into
github:mainfrom
shhhhhhjokerist:fix/delete-file-precondition
Open

shhhhhhjokerist wants to merge 1 commit into
github:mainfrom
shhhhhhjokerist:fix/delete-file-precondition

Conversation

@shhhhhhjokerist

Copy link
Copy Markdown

Summary

delete_file now verifies what is at the requested path before it mutates the tree: directory paths and missing paths are rejected instead of committing a deletion, and an optional sha argument turns the call into "delete only if the file is unchanged".

Why

Fixes #3757.

delete_file builds the tree entry with SHA: nil, so the commit deletes the whole subtree when path is a directory. Because there is no precondition either, a stale read of the file cannot be detected, and deleting a path that does not exist succeeds and produces an empty commit.

The REST contents DELETE endpoint is not an alternative here: it does not sign the resulting commit, while the Git Data API does, which is why delete_file uses tree entries. The guard therefore belongs in the handler, before the tree is created.

What changed

  • pkg/github/repositories.go: look up the path with the contents API after reading the branch head and before creating the tree. Reject directory paths, missing paths, and a sha that no longer matches the blob on the branch.
  • pkg/github/repositories.go: sha is a new optional input; the path description now says a directory path is rejected.
  • pkg/github/repository_tool_types.go: DeleteFileInput.Sha carries the optional precondition.
  • pkg/github/repositories_test.go: the existing success case now mocks the contents lookup, plus Test_DeleteFile_Precondition (stale sha / directory / not found) and Test_DeleteFile_AcceptsMatchingSha.
  • README.md: regenerated with generate-docs for the two description changes.

MCP impact

  • Tool schema or behavior changed
    • delete_file gains an optional sha; directory and missing paths now return a tool error instead of a commit. Existing calls without sha behave as before for regular files.

Prompts tested (tool changes only)

  • "Delete docs/old-setup.md from owner/repo on main" → deletes the file as before.
  • "Delete docs/old-setup.md on main, but only if its sha is still abc123" → deletes when the sha matches and returns a sha mismatch error when the file changed.
  • "Delete the docs directory from owner/repo" → returns path "docs" is a directory; delete_file only removes a single file instead of deleting every file below it.

Security / limits

  • Data exposure, filtering, or token/size limits considered
    • The extra contents lookup is a single read on the same branch the deletion targets. The tool is still destructive and its destructiveHint stays true; the change narrows what a confirmed call can destroy.

Tool renaming

  • I am not renaming tools as part of this PR

Lint & tests

  • Linted locally with golangci-lint run ./pkg/github/... → 0 issues (go vet ./pkg/github also clean)
  • go test ./pkg/github -run 'Test_DeleteFile' passes, and fails on the base revision with the new precondition tests
  • Full ./script/test on this Windows checkout: 15 pre-existing failures unrelated to this change (assign_copilot_to_issue base64 icon snapshots, protocol=2026-07-28 typed outputs, Test_RequestCopilotReview); the same 15 tests fail on main without this change.

Docs

  • Updated (README)

@shhhhhhjokerist
shhhhhhjokerist requested a review from a team as a code owner November 10, 2026 03:17
Copilot AI adaptive review requested due to automatic review settings November 10, 2026 03:17

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Deletion can validate a different tree than it modifies during branch rewinds, while the stale schema snapshot and nil-response cleanup path introduce a test failure and potential panic.

3 open issues
What changed in this PR

This PR addresses #3757 by adding pre-deletion checks to delete_file while preserving its Git Data API commit flow.

Changes:

  • Reject directory and missing paths before creating a deletion tree.
  • Add an optional blob sha precondition to detect stale file reads.
  • Document the new behavior and add tests for rejected paths and matching or stale SHAs.
File Description
README.md Documents directory rejection and the optional sha parameter.
pkg/​github/​repository_tool_types.go Adds the optional SHA precondition to the input type.
pkg/​github/​repositories.go Adds the contents lookup and deletion guards.
pkg/​github/​repositories_test.go Updates success mocks and tests deletion preconditions.

Review effort: Balanced (auto)

Comment on lines +1368 to +1371
"sha": {
Type: "string",
Description: "Blob SHA of the file, as returned by get_file_contents. When set, the delete is rejected if another writer changed the file since it was read. Omit to delete whatever is at the path.",
},

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 674d55d: pkg/github/toolsnaps/delete_file.snap now records the new path description and the sha property. Regenerating on this Windows checkout rewrites the embedded icon bytes with CRLF, so the icon lines were kept as the Unix LF values to match what CI generates.

Comment thread pkg/github/repositories.go Outdated
// entry below deletes a whole subtree when the path is a directory, and
// carries no precondition, so this is the only place to notice that the
// requested blob is missing or that the caller holds a stale sha.
fileContent, dirContent, resp, err := client.Repositories.GetContents(ctx, owner, repo, path, &github.RepositoryContentGetOptions{Ref: branch})

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 674d55d: the lookup is now pinned with Ref: baseCommit.GetSHA() so the contents check and the tree update both resolve from the same commit, including the branch-rewind case you described.

Comment thread pkg/github/repositories.go Outdated
Comment on lines +1438 to +1445
fileContent, dirContent, resp, err := client.Repositories.GetContents(ctx, owner, repo, path, &github.RepositoryContentGetOptions{Ref: branch})
if err != nil {
return ghErrors.NewGitHubAPIErrorResponse(ctx,
fmt.Sprintf("failed to get contents of %s on branch %s", path, branch),
resp,
err,
), nil, nil
}

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 674d55d: the contents response is kept in its own contentsResp variable, closed in its own defer only when non-nil, and passed to the error helper instead of overwriting the
esp the earlier defers capture.

The delete_file tool builds a tree entry with SHA=nil, so the commit
deletes a whole subtree when the path is a directory, and there is no
precondition: a stale read of the file cannot be detected. The REST
contents DELETE is not an option here because it does not sign the
resulting commit; the Git Data API is what gives delete_file verified
commits, so the guard belongs in the handler.

Look up the path before mutating the tree and reject:

- directory paths, which would otherwise delete every file below them;
- paths that are missing on the branch, which currently succeed and
  produce an empty commit;
- an optional sha argument that no longer matches the blob on the
  branch, so callers can turn "delete whatever is there" into
  "delete only if the file is still the one I read".
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.

delete_file: no sha precondition, and a directory path deletes the whole subtree

2 participants