← ClaudeAtlas

agent-self-debugger-looplisted

A Python CLI (self_debug.py) that parses pytest stderr/stdout tracebacks, extracts the failing File x.py line N location, reads and rewrites that file via an ast.NodeTransformer to inject sys._getframe()-based log probes and None/missing-attribute guards, re-runs pytest up to 4 attempts, and rolls back via git stash on exhaustion. Pure stdlib (ast, re, subprocess, tempfile, pathlib) plus pytest.
RMAzrim/opencode-artes · ★ 2 · AI & Automation · score 76
Install: claude install-skill RMAzrim/opencode-artes
# Agent Self-Debugger Loop ## 1. System Architecture & Prerequisites ### Runtime requirements - **Python 3.9+** (uses `ast`, `ast.NodeTransformer`, `ast.unparse` available since 3.9). - **pytest** installed and importable in the same interpreter that runs the CLI. - **git** available on `PATH` for the rollback mechanism (`git stash`). - No other third-party packages. The modules used: `ast`, `argparse`, `re`, `sys`, `subprocess`, `tempfile`, `pathlib`, `os`, `json`, `contextlib`, `io`, `types`. ### Architecture The tool is one self-contained CLI module. Its pipeline stages, in order: 1. **Traceback ingestion** — read pytest output from a file or stream, parse `File "x.py", line N` frames and extract the topmost (deepest) frame inside the project. 2. **AST parsing** — load the failing file, build an AST, and locate the exact statement node whose line falls within the failing line range. 3. **Probe injection** — wrap the failing statement with a `try/except` that logs `sys._getframe()` locals/states plus a `print` tracing probe before the statement runs. 4. **Guard patching** — a `NodeTransformer` rewrites attribute accesses and subscripts that previously raised `AttributeError`/`TypeError`/`NameError` into null-safe guarded forms when the failure signature is detectable. 5. **Iterative execution** — re-run pytest (up to `--max-attempts`, default 4). On success the patch is kept; on exhaustion, `git stash` restores the file. 6. **Reporting** — emit a machine-readable JSON s