refactor(recipe): switch refactor step to strict JSON output
- Update result-refactor.md to output JSON array of file changes instead of code blocks - Modify runner.go to parse JSON directly, removing regex-based extraction - Add project_languages and extensions to recipe metadata - Improve error handling and output consistency in steps
This commit is contained in:
parent
d4f3e3adbc
commit
7cb9eb3eb7
@ -13,6 +13,13 @@ parameters:
|
|||||||
default: true
|
default: true
|
||||||
description: If true, only generate patches
|
description: If true, only generate patches
|
||||||
|
|
||||||
|
project_languages:
|
||||||
|
- go
|
||||||
|
|
||||||
|
extensions:
|
||||||
|
go:
|
||||||
|
- .go
|
||||||
|
|
||||||
allowed_shell_commands:
|
allowed_shell_commands:
|
||||||
- go test ./...
|
- go test ./...
|
||||||
- go fmt ./...
|
- go fmt ./...
|
||||||
@ -33,23 +40,30 @@ Refactors all error handling in the target package to use the new Result[T] patt
|
|||||||
### Step 1: Discover files
|
### Step 1: Discover files
|
||||||
**Objective:** Find every file that needs changing.
|
**Objective:** Find every file that needs changing.
|
||||||
**Instructions:** Recursively scan `{{.package_path}}` for `.go` files containing `if err != nil`.
|
**Instructions:** Recursively scan `{{.package_path}}` for `.go` files containing `if err != nil`.
|
||||||
If no files are found or the path does not exist, output exactly: "No files found matching the criteria."
|
**Expected output:** A clean list of full file paths (one per line). If none, say "No files found matching the criteria."
|
||||||
**Expected output:** A clean numbered list of full file paths (one per line).
|
|
||||||
|
|
||||||
### Step 2: Refactor each file
|
### Step 2: Refactor each file
|
||||||
**Objective:** Generate the updated code.
|
**Objective:** Generate the updated code.
|
||||||
**Instructions:** For each file from Step 1:
|
**Instructions:** For each file from Step 1:
|
||||||
- Read the full original content.
|
- Read the full original content.
|
||||||
- Refactor it to use `Result[T]` instead of naked errors (follow existing style, preserve comments).
|
- Refactor it to use `Result[T]` instead of naked errors (follow existing style, preserve all comments).
|
||||||
- Return *ONLY* the complete new file inside a ```go code block (no explanations).
|
- Return **ONLY** a single JSON array in this exact format (no extra text, no markdown):
|
||||||
**Expected output:** One ```go block per file, clearly labelled with the filename.
|
```json
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"file": "internal/example.go",
|
||||||
|
"content": "the complete refactored file here"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
```
|
||||||
|
|
||||||
### Step 3: Apply or patch
|
### Step 3: Apply or patch
|
||||||
**Objective:** Safely write changes or create reviewable output.
|
**Objective:**
|
||||||
|
Safely write changes or create reviewable output.
|
||||||
**Instructions:**
|
**Instructions:**
|
||||||
- If `dry_run` is true → create a unified diff patch file for review.
|
- If dry_run is true → create a unified diff patch file for review.
|
||||||
- If false → write the new files (backup originals as `.bak`).
|
- If false → write the new files (backup originals as .bak).
|
||||||
**Expected output:** Confirmation of what was written + full path to any patch file.
|
**Expected output:** Confirmation of what was written + full path to any patch file.
|
||||||
|
|
||||||
### Final Summary
|
**Final Summary**
|
||||||
Give me a concise executive summary: number of files changed, any warnings or patterns you noticed, and your recommended next step.
|
Give me a concise executive summary: number of files changed, any warnings or patterns you noticed, and your recommended next step.
|
||||||
@ -2,10 +2,10 @@ package recipe
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
_ "bufio"
|
_ "bufio"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"gmgauthier.com/grokkit/internal/grok"
|
"gmgauthier.com/grokkit/internal/grok"
|
||||||
@ -99,7 +99,7 @@ func (r *Runner) discoverFiles() []string {
|
|||||||
return files
|
return files
|
||||||
}
|
}
|
||||||
|
|
||||||
// handleApplyStep with robust regex that matches Grok's current output
|
// handleApplyStep parses strict JSON from the refactor step — no regex at all
|
||||||
func (r *Runner) handleApplyStep(previousResults []string) {
|
func (r *Runner) handleApplyStep(previousResults []string) {
|
||||||
if len(previousResults) == 0 {
|
if len(previousResults) == 0 {
|
||||||
fmt.Println(" ⚠️ No previous results to apply — skipping.")
|
fmt.Println(" ⚠️ No previous results to apply — skipping.")
|
||||||
@ -107,16 +107,36 @@ func (r *Runner) handleApplyStep(previousResults []string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
lastResult := previousResults[len(previousResults)-1]
|
lastResult := previousResults[len(previousResults)-1]
|
||||||
blocks := extractCodeBlocks(lastResult)
|
|
||||||
|
|
||||||
if len(blocks) == 0 {
|
// Find the JSON array in the response
|
||||||
fmt.Println(" ⚠️ No code blocks found to apply — skipping.")
|
start := strings.Index(lastResult, "[")
|
||||||
|
end := strings.LastIndex(lastResult, "]") + 1
|
||||||
|
if start == -1 || end == 0 {
|
||||||
|
fmt.Println(" ⚠️ No JSON found in previous step — skipping.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
jsonStr := lastResult[start:end]
|
||||||
|
|
||||||
|
type FileChange struct {
|
||||||
|
File string `json:"file"`
|
||||||
|
Content string `json:"content"`
|
||||||
|
}
|
||||||
|
|
||||||
|
var changes []FileChange
|
||||||
|
if err := json.Unmarshal([]byte(jsonStr), &changes); err != nil {
|
||||||
|
fmt.Printf(" ⚠️ Could not parse JSON: %v\n", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(changes) == 0 {
|
||||||
|
fmt.Println(" ⚠️ No files to apply — skipping.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println(" 📄 Dry-run mode: creating patch file...")
|
fmt.Println(" 📄 Dry-run mode: creating patch file...")
|
||||||
patchPath := filepath.Join(".", "recipe-refactor.patch")
|
patchPath := filepath.Join(".", "recipe-refactor.patch")
|
||||||
if err := createUnifiedPatch(blocks, patchPath); err != nil {
|
if err := createUnifiedPatch(changes, patchPath); err != nil {
|
||||||
fmt.Printf(" ❌ Failed to create patch: %v\n", err)
|
fmt.Printf(" ❌ Failed to create patch: %v\n", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -124,32 +144,18 @@ func (r *Runner) handleApplyStep(previousResults []string) {
|
|||||||
fmt.Println(" Review it, then run with dry_run=false to apply.")
|
fmt.Println(" Review it, then run with dry_run=false to apply.")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Double-quoted regex — no raw-string backtick problems ever again
|
func createUnifiedPatch(changes []struct{ File, Content string }, patchPath string) error {
|
||||||
var blockRe = regexp.MustCompile(`(?s)```go\n// \s*(.+?\.go)\n(.*?)\n````)
|
|
||||||
|
|
||||||
func extractCodeBlocks(text string) map[string]string {
|
|
||||||
blocks := make(map[string]string)
|
|
||||||
matches := blockRe.FindAllStringSubmatch(text, -1)
|
|
||||||
for _, m := range matches {
|
|
||||||
if len(m) == 3 {
|
|
||||||
blocks[m[1]] = m[2]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return blocks
|
|
||||||
}
|
|
||||||
|
|
||||||
func createUnifiedPatch(blocks map[string]string, patchPath string) error {
|
|
||||||
f, err := os.Create(patchPath)
|
f, err := os.Create(patchPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
|
||||||
for path, content := range blocks {
|
for _, ch := range changes {
|
||||||
fmt.Fprintf(f, "--- %s\n+++ %s\n@@ -0,0 +1,%d @@\n", path, path, strings.Count(content, "\n")+1)
|
fmt.Fprintf(f, "--- %s\n+++ %s\n@@ -0,0 +1,%d @@\n", ch.File, ch.File, strings.Count(ch.Content, "\n")+1)
|
||||||
for _, line := range strings.Split(content, "\n") {
|
for _, line := range strings.Split(ch.Content, "\n") {
|
||||||
fmt.Fprintf(f, "+%s\n", line)
|
fmt.Fprintf(f, "+%s\n", line)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user