Repository navigation
fix: reject directory paths and stale shas in delete_file - #3790
shhhhhhjokerist wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
🟡 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
shaprecondition 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)
| "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.", | ||
| }, |
There was a problem hiding this comment.
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.
| // 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}) |
There was a problem hiding this comment.
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.
| 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 | ||
| } |
There was a problem hiding this comment.
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".
ab1be7d to
674d55d
Compare


Summary
delete_filenow 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 optionalshaargument turns the call into "delete only if the file is unchanged".Why
Fixes #3757.
delete_filebuilds the tree entry withSHA: nil, so the commit deletes the whole subtree whenpathis 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
DELETEendpoint is not an alternative here: it does not sign the resulting commit, while the Git Data API does, which is whydelete_fileuses 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 ashathat no longer matches the blob on the branch.pkg/github/repositories.go:shais a new optional input; thepathdescription now says a directory path is rejected.pkg/github/repository_tool_types.go:DeleteFileInput.Shacarries the optional precondition.pkg/github/repositories_test.go: the existing success case now mocks the contents lookup, plusTest_DeleteFile_Precondition(stale sha / directory / not found) andTest_DeleteFile_AcceptsMatchingSha.README.md: regenerated withgenerate-docsfor the two description changes.MCP impact
delete_filegains an optionalsha; directory and missing paths now return a tool error instead of a commit. Existing calls withoutshabehave as before for regular files.Prompts tested (tool changes only)
sha mismatcherror when the file changed.path "docs" is a directory; delete_file only removes a single fileinstead of deleting every file below it.Security / limits
destructiveHintstaystrue; the change narrows what a confirmed call can destroy.Tool renaming
Lint & tests
golangci-lint run ./pkg/github/...→ 0 issues (go vet ./pkg/githubalso clean)go test ./pkg/github -run 'Test_DeleteFile'passes, and fails on the base revision with the new precondition tests./script/teston this Windows checkout: 15 pre-existing failures unrelated to this change (assign_copilot_to_issuebase64 icon snapshots,protocol=2026-07-28typed outputs,Test_RequestCopilotReview); the same 15 tests fail onmainwithout this change.Docs