Overview
Thehashline_edit tool provides safe, precise file editing using hash-anchored line references (LINE#ID). Every line reference includes a content hash that validates the line hasn’t changed since it was read, preventing race conditions and stale edits.
Source: src/tools/hashline-edit/
How It Works
LINE#ID Format
Every line in the codebase gets a unique reference:line_number: 1-based line numberhash_id: Two characters from setZPMQVRWSNKTXJBYH(CID alphabet)
Validation Pipeline
When you submit an edit:- Hash Validation: Tool recomputes hash for referenced lines
- Mismatch Detection: If hash differs, edit is rejected with updated LINE#ID tags
- Batch Application: All edits validated against same file snapshot
- Bottom-Up Ordering: Edits applied from end of file to preserve line numbers
- Diff Generation: Unified diff shown for verification
Tool Definition
Absolute path to file to edit
Array of edit operations to apply (empty when
delete=true)Each edit object:Operation type:
"replace" | "append" | "prepend"Primary anchor in LINE#ID format (e.g.,
"42#VK")Required for replace, optional for append/prependRange end anchor in LINE#ID formatOnly for range operations (e.g., replace lines 42-50)
Replacement or inserted content
string: Single linestring[]: Multiple lines (preferred)nullor[]: Delete (withreplaceop only)
Delete file instead of editing (requires
edits=[])Rename output file path after edits
Operations
Replace
Replace single line or range:Append
Insert after anchor (or EOF if no anchor):Prepend
Insert before anchor (or BOF if no anchor):Usage Examples
Single Line Edit
Multi-Line Range Replacement
Multiple Operations (One File)
Delete File
Rename After Edit
Error Handling
Hash Mismatch
When file changes between read and edit:Invalid Reference Format
LINE#ID pattern (e.g., "42#VK").
Overlapping Ranges
Autocorrect Features
The tool automatically handles:- Merged Lines: Auto-expanded back to original line count
- Indentation: Restored from original lines
- Line Endings: BOM and CRLF preserved
- Diff Markers:
>>>prefix and diff markers auto-stripped from content
Best Practices
✅ Do
- Copy exact LINE#ID tags from read output
- Batch related edits in single call per file
- Anchor to structural boundaries (function/class/brace lines)
- Re-read after successful edit before next edit on same file
- Preserve formatting (indentation, trailing commas, brace style)
❌ Don’t
- Never guess LINE#ID tags — always copy from read output
- Don’t anchor to blank lines — use structural code lines
- Don’t include
|contentsuffix — use onlyLINE#IDin anchors - Don’t rewrite approximately — use exact current tokens
- Don’t create overlapping ranges — validate edit boundaries
Implementation Details
Execution Pipeline
Hash Computation
Source:src/tools/hashline-edit/hash-computation.ts
Validation
Source:src/tools/hashline-edit/validation.ts
Related Tools
- Read Tool: Get file content with LINE#ID tags
- Edit Tool: Alternative editing without hash validation
- Write Tool: Overwrite entire file
Source Files
| File | Purpose |
|---|---|
tools.ts:14 | Tool factory and schema definition |
hashline-edit-executor.ts | Main execution pipeline |
validation.ts:64 | LINE#ID validation and hash checking |
hash-computation.ts | Hash generation from line content |
edit-operations.ts | Apply replace/append/prepend operations |
normalize-edits.ts | Parse and validate edit schemas |
tool-description.ts:1 | Tool description constant |