# Research Log: Fix Audit Findings (2026-03-06-2126)

## Context
The full codebase audit identified 8 issues centered primarily around the new `replace_body` tool (`edit.rs`) and the `resolve_body_range` trait method in `pathfinder-treesitter`. 

## Investigations

1. **Blocking I/O (`edit.rs:177`)**: The fix is simple `std::fs::write` -> `tokio::fs::write().await`. It achieves the desired in-place write through the Tokio API without blocking the runtime.
2. **Duplicated `SurgeonError` mapping (`edit.rs`, `symbols.rs`, `repo_map.rs`)**: 
   - A single helper function can map `SurgeonError` + `path` to `PathfinderError` + `ErrorData`.
   - The path is slightly different per tool (`SemanticPath` vs `PathBuf`), but this can be handled easily by a helper function in `helpers.rs`.
3. **`resolve_body_range` redundancy**: 
   - It performs tokenization natively instead of reusing the `cached_parse` pipeline. 
   - Refactoring to `cached_parse(workspace_root, &semantic_path.file_path)` avoids duplicating code and keeps cache logic centralized.
4. **Body range semantics (inclusive vs exclusive)**: 
   - Wrote a test script `/tmp/test_ts.rs` to evaluate behavior of `find_body_bytes(tree, src, ..)` for Go and Python.
   - For Go (brace block), `body.start_byte()` is the `{` character and `body.end_byte()` is the character *just after* `}`.
   - For Python (indentation block), `body.start_byte()` and `body.end_byte()` span precisely the lines of the body, starting with the first character of the body.
   - The current `edit.rs` code assumes braces exist. It causes Python replacement to duplicate indentation and insert erroneous newlines.
   - Fix: Inspect source bytes at `start_byte` and `end_byte - 1` to verify if they are `{` and `}` respectively. Branch the string splicing logic based on this determination.
5. **Integration tests for `replace_body`**: 
   - Tests exist, but use `MockSurgeon`. 
   - We must add a `test_replace_body_real_parser` using `TreeSitterSurgeon` against real source files (Go and Python).
6. **`SupportedLanguage` match -> `as_str()`**: 
   - Self-explanatory, we'll add an `as_str()` method to use internally.
7. **Hardcoded 4-space indent**:
   - Python PEP-8 requires 4 spaces. Go specifies tabs (which `dedent_then_reindent` likely considers equivalent depending on implementation, but 4 spaces works cross-language for AST). 
   - For now, we'll keep hardcoded 4 but document it, as TypeScript 2-space users will eventually need a configurable indent.
8. **Clippy `manual_let_else`**: 
   - Simple syntactic fix in `edit.rs` test cases.

## Conclusion
The fixes are straightforward. A comprehensive plan has been drafted in `implementation_plan.md`.
