refactor(recipe/runner): improve regex flexibility and simplify patch creation

- Update discoverFiles comment to be more concise.
- Enhance blockRe regex to optionally match leading "// " for filenames, supporting varied Grok output formats.
- Revise handleApplyStep comment to reflect regex changes.
- Simplify createUnifiedPatch by removing unnecessary error checks on fmt.Fprintf and defer closure.
This commit is contained in:
Greg Gauthier 2026-03-06 22:15:33 +00:00
parent 40d40f7669
commit b2b8c1a482

View File

@ -77,7 +77,7 @@ Execute this step now. Respond ONLY with the expected output format — no expla
return nil
}
// discoverFiles does a real filesystem scan (used by "Discover files" steps)
// discoverFiles does a real filesystem scan
func (r *Runner) discoverFiles() []string {
var files []string
root := "internal"
@ -99,7 +99,7 @@ func (r *Runner) discoverFiles() []string {
return files
}
// handleApplyStep (dry-run patch + confirmation)
// handleApplyStep now uses a more flexible regex that matches both formats Grok uses
func (r *Runner) handleApplyStep(previousResults []string) {
if len(previousResults) == 0 {
fmt.Println(" ⚠️ No previous results to apply — skipping.")
@ -124,8 +124,8 @@ func (r *Runner) handleApplyStep(previousResults []string) {
fmt.Println(" Review it, then run with dry_run=false to apply.")
}
// regex split across lines to avoid backtick collision
var blockRe = regexp.MustCompile(`(?s)^//\s*(.+?\.go)\n` +
// Flexible regex that works whether Grok puts "// filename" or just "filename"
var blockRe = regexp.MustCompile(`(?s)^(?://\s*)?(.+?\.go)\n` +
"```go\n" +
`(.*?)\n` +
"```")
@ -146,23 +146,12 @@ func createUnifiedPatch(blocks map[string]string, patchPath string) error {
if err != nil {
return err
}
defer func(f *os.File) {
err := f.Close()
if err != nil {
return
}
}(f)
defer f.Close()
for path, content := range blocks {
_, err := fmt.Fprintf(f, "--- %s\n+++ %s\n@@ -0,0 +1,%d @@\n", path, path, strings.Count(content, "\n")+1)
if err != nil {
return err
}
fmt.Fprintf(f, "--- %s\n+++ %s\n@@ -0,0 +1,%d @@\n", path, path, strings.Count(content, "\n")+1)
for _, line := range strings.Split(content, "\n") {
_, err := fmt.Fprintf(f, "+%s\n", line)
if err != nil {
return err
}
fmt.Fprintf(f, "+%s\n", line)
}
}
return nil