Files
pi-config/extensions/pi-lsp-extension/.ralph/loops/2026-03-16T18-22-19-450Z-feature.json

57 lines
4.4 KiB
JSON

{
"id": "1773685339450-feature",
"presetName": "feature",
"prompt": "specs/structural-search-rewrite.md",
"startTime": 1773685339450,
"endTime": 1773686879376,
"outcome": "Task complete ✓",
"iterations": 4,
"history": [
{
"hat": "planner",
"event": "plan.start",
"iteration": 1
},
{
"hat": "builder",
"event": "tasks.ready",
"iteration": 2
},
{
"hat": "reviewer",
"event": "build.done",
"iteration": 3
},
{
"hat": "committer",
"event": "review.approved",
"iteration": 4
}
],
"iterationLogs": [
{
"iteration": 1,
"hatKey": "planner",
"hatName": "📋 Planner",
"event": "tasks.ready",
"summary": "---\n\n## Summary\n\n### 📋 Plan → 🔨 Build → 🔍 Review — Complete\n\nImplemented the **Structural Code Search & Rewrite** feature per `specs/structural-search-rewrite.md`.\n\n### New files (5):\n\n| File | Lines | Purpose |\n|------|-------|---------|\n| `src/tree-sitter/pattern-compiler.ts` | ~200 | Parses metavariable patterns (`$NAME`, `$$$NAME`) into matchable `PatternNode` trees. Handles wrapper contexts for expression/statement fragments. |\n| `src/tree-sitter/search-engine.ts` | ~290 | AST matching engine — walks target trees depth-first, matches against pattern with field-name-aware child matching. Collects files by language, caps at 5000 files. |\n| `src/tree-sitter/rewrite-engine.ts` | ~100 | Substitutes captured metavariable values into replacement templates. Applies changes bottom-up to preserve byte offsets. |\n| `src/tools/code-search.ts` | ~100 | `code_search` tool — pattern, language, path?, max_results?. Returns matches with file:line, matched text, and metavariable bindings. |\n| `src/tools/code-rewrite.ts` | ~140 | `code_rewrite` tool — pattern, replacement, language, path?, dry_run?(default true). Previews or applies structural transformations. |\n\n### Modified files (1):\n\n| File | Change |\n|------|--------|\n| `src/index.ts` | Added imports and `pi.registerTool()` calls for both new tools |\n\n### Key design decisions:\n\n1. **Custom AST matcher instead of tree-sitter queries** — tree-sitter's S-expression queries match by node type, not text content. A custom matcher lets us match literal text for concrete nodes and wildcard for metavariables, which is what users expect.\n\n2. **Field-name-aware child matching** — pattern children with field names (e.g., `function`, `arguments`, `name`) are matched against target children with the same field name. Extra target children (like TypeScript type annotations) are allowed. This prevents false negatives when the target language has more syntax than the pattern mentions.\n\n3. **Wrapper-based pattern parsing** — patterns that a",
"timestamp": 1773686181539
},
{
"iteration": 2,
"hatKey": "builder",
"hatName": "⚙️ Builder",
"event": "build.done",
"summary": "## Summary\n\nThe structural search & rewrite feature is fully implemented and tested. I found and fixed two bugs in the existing code:\n\n### Bug 1: Semicolon stripping in rewrites\n**`src/tree-sitter/rewrite-engine.ts`** — Added `preserveTrailingSemicolon()` so that `var x = 10;` → `const x = 10;` (not `const x = 10`). The matched AST node includes the `;` as part of the statement, and the rewrite engine now preserves it when the replacement template doesn't explicitly include one.\n\n### Bug 2: Pattern compilation fails for Python\n**`src/tree-sitter/pattern-compiler.ts`** — `$ARG` is not a valid Python identifier. Added **placeholder preprocessing**: before parsing, metavars are replaced with language-safe placeholders (`$NAME` → `__META_NAME__`, `$$$NAME` → `__VMETA_NAME__`), then decoded back after the AST is built. Also added Python-specific wrapper strategies (Python uses `module` as root, not `program`).\n\n### Test results\n- **`test-structural-search.ts`**: 52/52 passing ✅\n- **`test-tree-sitter.ts`**: 62/62 passing ✅ (no regressions)\n- **`tsc --noEmit`**: clean compile ✅",
"timestamp": 1773686551683
},
{
"iteration": 3,
"hatKey": "reviewer",
"hatName": "🔎 Reviewer",
"event": "review.approved",
"summary": "**Review complete — PASS ✅**. The implementation is solid and ready for the commit hat. Four minor non-blocking issues noted in the scratchpad for future improvement.",
"timestamp": 1773686779441
}
]
}