Repository navigation
fix: prevent write_file from clobbering external edits via staleness check (#65604) #65618
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1604,11 +1604,14 @@ def write_file_tool(path: str, content: str, task_id: str = "default", | |
|
|
||
| if _resolved is None: | ||
| stale_warning = _check_file_staleness(path, task_id) | ||
| if stale_warning: | ||
| return tool_error( | ||
| f"Refusing to write: {stale_warning} " | ||
| "Use read_file to get the current content, then retry the write." | ||
| ) | ||
| file_ops = _get_file_ops(task_id) | ||
| result = file_ops.write_file(path, content) | ||
| result_dict = result.to_dict() | ||
| if stale_warning: | ||
| result_dict["_warning"] = stale_warning | ||
| if not result_dict.get("error"): | ||
| _mark_verification_stale(task_id, [path], session_id=session_id) | ||
| _update_read_timestamp(path, task_id) | ||
|
|
@@ -1622,15 +1625,23 @@ def write_file_tool(path: str, content: str, task_id: str = "default", | |
| # fire — its message names the sibling subagent. | ||
| cross_warning = file_state.check_stale(task_id, _resolved) | ||
| stale_warning = _check_file_staleness(path, task_id) | ||
| # Block on staleness — refuse to clobber external edits or | ||
| # sibling-subagent changes. The caller must re-read first. | ||
| staleness_block = cross_warning or stale_warning | ||
| if staleness_block: | ||
| return tool_error( | ||
| f"Refusing to write: {staleness_block} " | ||
| "Use read_file to get the current content, then retry the write." | ||
| ) | ||
| # Workspace-divergence warning: relative path resolving outside the | ||
| # terminal's cwd (the worktree-cwd bug). Lowest priority of the three. | ||
| # terminal's cwd (the worktree-cwd bug). Lowest priority — kept as | ||
| # a soft warning because it doesn't risk data loss. | ||
| cwd_warning = _path_resolution_warning(path, Path(_resolved), task_id) | ||
| file_ops = _get_file_ops(task_id) | ||
| result = file_ops.write_file(_resolved, content) | ||
| result_dict = result.to_dict() | ||
| effective_warning = cross_warning or stale_warning or cwd_warning | ||
| if effective_warning: | ||
| result_dict["_warning"] = effective_warning | ||
| if cwd_warning: | ||
| result_dict["_warning"] = cwd_warning | ||
| # Always report the ABSOLUTE path actually written, so a wrong-cwd | ||
| # mismatch is visible in the response instead of silently routing | ||
| # the edit to the wrong checkout. | ||
|
|
@@ -1741,7 +1752,8 @@ def _reject_v4a_traversal(v4a_path: str) -> str | None: | |
|
|
||
| # Collect warnings — cross-agent registry first (names sibling), | ||
| # then per-task tracker as a fallback. | ||
| stale_warnings: list[str] = [] | ||
| stale_blocks: list[str] = [] | ||
| cwd_warnings: list[str] = [] | ||
| _path_to_resolved: dict[str, str] = {} | ||
| for _p in _paths_to_check: | ||
| try: | ||
|
|
@@ -1750,13 +1762,25 @@ def _reject_v4a_traversal(v4a_path: str) -> str | None: | |
| _r = None | ||
| _path_to_resolved[_p] = _r | ||
| _cross = file_state.check_stale(task_id, _r) if _r else None | ||
| _sw = _cross or _check_file_staleness(_p, task_id) | ||
| if not _sw and _r: | ||
| _per_task = _check_file_staleness(_p, task_id) | ||
| _staleness = _cross or _per_task | ||
| if _staleness: | ||
| stale_blocks.append(_staleness) | ||
| elif _r: | ||
| # Workspace-divergence warning (worktree-cwd bug): relative | ||
| # path resolving outside the terminal's cwd. | ||
| _sw = _path_resolution_warning(_p, Path(_r), task_id) | ||
| if _sw: | ||
| stale_warnings.append(_sw) | ||
| _cwd = _path_resolution_warning(_p, Path(_r), task_id) | ||
| if _cwd: | ||
| cwd_warnings.append(_cwd) | ||
|
|
||
| # Block on staleness — refuse to apply a patch that would | ||
| # clobber external edits or sibling-subagent changes. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This broadens the write_file fix into a hard behavioral change for targeted patches. Current patch staleness is warning-only; please retain that behavior unless the patch semantic change is intentionally approved. |
||
| if stale_blocks: | ||
| msg = stale_blocks[0] if len(stale_blocks) == 1 else " | ".join(stale_blocks) | ||
| return tool_error( | ||
| f"Refusing to patch: {msg} " | ||
| "Use read_file to get the current content, then retry the patch." | ||
| ) | ||
|
|
||
| file_ops = _get_file_ops(task_id) | ||
|
|
||
|
|
@@ -1780,8 +1804,8 @@ def _reject_v4a_traversal(v4a_path: str) -> str | None: | |
| return tool_error(f"Unknown mode: {mode}") | ||
|
|
||
| result_dict = result.to_dict() | ||
| if stale_warnings: | ||
| result_dict["_warning"] = stale_warnings[0] if len(stale_warnings) == 1 else " | ".join(stale_warnings) | ||
| if cwd_warnings: | ||
| result_dict["_warning"] = cwd_warnings[0] if len(cwd_warnings) == 1 else " | ".join(cwd_warnings) | ||
| # Report the ABSOLUTE path(s) actually patched so a wrong-cwd | ||
| # mismatch (e.g. a worktree session editing the main checkout) is | ||
| # visible in the response instead of silently landing elsewhere. | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This only blocks when a tracker entry already exists.
_check_file_staleness()returnsNonefor an unread path, so a task can still fully overwrite an existing file without a fresh baseline. Add an existing-file/full-read baseline check here.