nanocoder
nanocoder copied to clipboard
[Feature] Architecture: Syntax-island editing with deterministic reconstruction
Description
Allow models to edit isolated AST nodes (like a single function body or JSX subtree) while the deterministic harness protects and reconstructs the rest of the file.
Use Case
When small models rewrite code, they often accidentally delete closing brackets of parent scopes, remove unrelated imports, or mess up surrounding scaffolding, breaking the compilation of the file.
Proposed Solution
- The harness identifies the smallest editable "syntax island" (e.g., the body of
validateToken()). - The model is only provided the island and is told to return only the new island.
- The harness parses the returned text in a synthetic wrapper. If it compiles, the harness mechanically splices it back into the original file's AST.
- Everything outside the island remains byte-for-byte locked.
Alternatives Considered
- Regex replacements (fragile and easily broken).
- Asking the model to "be careful" (probabilistic and unsafe).
Additional Context
- [x] I have searched existing issues to ensure this is not a duplicate
- [x] This feature aligns with the project's goals (local-first AI assistance)
Taking this issue! let me know what you think about the implementation
Implementation Architecture
1. AST Island Extractor & Parser (source/tools/file-ops/syntax-islands.ts)
- Node Locator: Uses TypeScript AST parser (
ts.createSourceFile) to find target syntax nodes (function declarations, arrow functions, class methods, interface blocks, or JSX subtrees) by name, symbol, or line span. - Island Isolation: Extracts the exact AST node bounds (start offset, end offset, indentation level, and leading comments).
- Synthetic Validation: When the model supplies a replacement node, wrap it in an isolated synthetic compilation context to verify valid syntax and balanced tokens before touching the disk.
2. Deterministic AST Splicer (source/tools/file-ops/island-splicer.ts)
- Preserves 100% of the surrounding file byte-for-byte (imports, parent classes, sibling functions, comments, line endings).
- Normalizes indentation of the spliced replacement to match the parent AST scope indentation.
- Verifies post-splice file validity (ensuring zero syntax regressions).
3. Tool Surface (source/tools/file-ops/syntax-island-tool.ts)
- Expose an
island_edittool (and/or an AST-scoped mode in existing edit tools):- Parameters:
path,symbol(ornodeKind+name), andreplacement. - Formatter & validator providing clear visual diffs and structured feedback to the model if a syntax error is detected in the replacement.
- Parameters:
4. Automated Testing (source/tools/file-ops/syntax-island.spec.ts)
- Unit tests covering:
- Replacing function bodies with complex nested closures without breaking parent braces.
- Editing JSX components / elements without dropping surrounding JSX parent tags.
- Preserving preceding/trailing imports, comments, and non-target functions byte-for-byte.
- Rejection of invalid replacement syntax with actionable error messages returned to the model.