feat: add apply_patch and ast_context tools#475
Open
poshinchen wants to merge 1 commit into
Open
Conversation
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
Adds two new tools aimed at reducing context usage and edit failures for coding agents.
ast_contextreturns a structural outline of a Python source file (imports, module-level assignments, classes with methods, free functions), each tagged with a line range. This lets an agent fetch the shape of a file without reading the full body, addressing theEXCESSIVE_FILE_READSfailure mode.apply_patchapplies one or more hash-anchored edits to a file. Each patch carries ananchor_hash(8-char sha256 prefix of theoldtext), anoldspan, and anewspan. The edit lands only if the anchor verifies andoldmatches exactly once in the file. All patches must succeed for the file to be written (atomic via tempfile rename); on any failure the file is left untouched and per-entry status is reported back.This is a port of the hash-anchor idea from the TerminalBench writeup: https://news.ycombinator.com/item?id=47920787
Related Issues
N/A
Documentation PR
N/A
Type of Change
New feature
Testing
How have you tested the change?
hatch run prepareAdded unit tests:
tests/test_ast_context.pycovers imports, assignments, class/method extraction, function signatures with defaults and kw-only args, syntax errors, missing files, and the include flags.tests/test_apply_patch.pycovers happy-path application, anchor mismatch, not-found, ambiguous-match, multi-patch atomicity (no write on partial failure), and missing files.Checklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.