Skip to content

feat: add apply_patch and ast_context tools#475

Open
poshinchen wants to merge 1 commit into
strands-agents:mainfrom
poshinchen:tools/ast_and_patch
Open

feat: add apply_patch and ast_context tools#475
poshinchen wants to merge 1 commit into
strands-agents:mainfrom
poshinchen:tools/ast_and_patch

Conversation

@poshinchen
Copy link
Copy Markdown
Contributor

@poshinchen poshinchen commented May 19, 2026

Description

Adds two new tools aimed at reducing context usage and edit failures for coding agents.

ast_context returns 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 the EXCESSIVE_FILE_READS failure mode.

apply_patch applies one or more hash-anchored edits to a file. Each patch carries an anchor_hash (8-char sha256 prefix of the old text), an old span, and a new span. The edit lands only if the anchor verifies and old matches 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?

  • I ran hatch run prepare

Added unit tests:

  • tests/test_ast_context.py covers 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.py covers happy-path application, anchor mismatch, not-found, ambiguous-match, multi-patch atomicity (no write on partial failure), and missing files.

Checklist

  • I have read the CONTRIBUTING document
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

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