feature/recipe_implementation #5

Merged
gmgauthier merged 54 commits from feature/recipe_implementation into master 2026-03-07 23:17:54 +00:00
Showing only changes of commit 8efca62109 - Show all commits

View File

@ -24,6 +24,9 @@ func NewRunner(r *Recipe, client *grok.Client, model string) *Runner {
func (r *Runner) Run() error { func (r *Runner) Run() error {
fmt.Printf("🍳 Starting recipe: %s v%s\n\n", r.Recipe.Name, r.Recipe.Version) fmt.Printf("🍳 Starting recipe: %s v%s\n\n", r.Recipe.Name, r.Recipe.Version)
// Resolve package_path once at the start (handles ~, ., etc.)
workDir := r.resolvePackagePath()
var previousResults []string var previousResults []string
var refactorJSONs []string var refactorJSONs []string
@ -34,7 +37,7 @@ func (r *Runner) Run() error {
switch { switch {
case strings.Contains(titleLower, "discover") || strings.Contains(titleLower, "find"): case strings.Contains(titleLower, "discover") || strings.Contains(titleLower, "find"):
files := r.discoverFiles() files := r.discoverFiles(workDir)
result := strings.Join(files, "\n") result := strings.Join(files, "\n")
previousResults = append(previousResults, "Discovered files:\n"+result) previousResults = append(previousResults, "Discovered files:\n"+result)
fmt.Println(result) fmt.Println(result)
@ -47,16 +50,14 @@ func (r *Runner) Run() error {
r.handleApplyStep(refactorJSONs) r.handleApplyStep(refactorJSONs)
continue continue
// === NEW: Safe shell command execution ===
case strings.Contains(titleLower, "initialize") || strings.Contains(titleLower, "create") || case strings.Contains(titleLower, "initialize") || strings.Contains(titleLower, "create") ||
strings.Contains(titleLower, "run") || strings.Contains(titleLower, "shell") || strings.Contains(titleLower, "run") || strings.Contains(titleLower, "shell") ||
strings.Contains(titleLower, "poetry") || strings.Contains(titleLower, "git") || strings.Contains(titleLower, "poetry") || strings.Contains(titleLower, "git") ||
strings.Contains(titleLower, "tea"): strings.Contains(titleLower, "tea"):
r.executeShellCommands(step, previousResults) r.executeShellCommands(step, previousResults, workDir)
continue continue
default: default:
// fallback for any other step
prompt := fmt.Sprintf(`Recipe Overview: prompt := fmt.Sprintf(`Recipe Overview:
%s %s
@ -91,11 +92,8 @@ Execute this step now. Respond ONLY with the expected output format — no expla
return nil return nil
} }
// discoverFiles — fully generic using recipe metadata // resolvePackagePath expands ~, ., and makes the path absolute
func (r *Runner) discoverFiles() []string { func (r *Runner) resolvePackagePath() string {
var files []string
// 1. Use explicit --param package_path if provided
root := "." root := "."
if v, ok := r.Recipe.ResolvedParams["package_path"]; ok { if v, ok := r.Recipe.ResolvedParams["package_path"]; ok {
if s, ok := v.(string); ok && s != "" { if s, ok := v.(string); ok && s != "" {
@ -103,14 +101,26 @@ func (r *Runner) discoverFiles() []string {
} }
} }
// 2. Smart defaults if no param was given // Expand ~
if root == "." { if strings.HasPrefix(root, "~/") {
if _, err := os.Stat("src"); err == nil { home, _ := os.UserHomeDir()
root = "src" root = filepath.Join(home, root[2:])
} } else if root == "~" {
root, _ = os.UserHomeDir()
} }
// 3. Build allowed extensions from recipe frontmatter // Make absolute
abs, err := filepath.Abs(root)
if err != nil {
abs = root
}
return abs
}
// discoverFiles now takes the resolved absolute path
func (r *Runner) discoverFiles(root string) []string {
var files []string
allowedExt := make(map[string]bool) allowedExt := make(map[string]bool)
for _, lang := range r.Recipe.ProjectLanguages { for _, lang := range r.Recipe.ProjectLanguages {
if exts, ok := r.Recipe.Extensions[lang]; ok { if exts, ok := r.Recipe.Extensions[lang]; ok {
@ -120,19 +130,13 @@ func (r *Runner) discoverFiles() []string {
} }
} }
// 4. Use a configurable search pattern (defaults to Go-style)
searchFor := r.Recipe.SearchPattern
if searchFor == "" {
searchFor = "if err != nil"
}
_ = filepath.WalkDir(root, func(path string, d os.DirEntry, err error) error { _ = filepath.WalkDir(root, func(path string, d os.DirEntry, err error) error {
if err != nil || d.IsDir() { if err != nil || d.IsDir() {
return nil return nil
} }
if allowedExt[filepath.Ext(path)] { if allowedExt[filepath.Ext(path)] {
b, _ := os.ReadFile(path) b, _ := os.ReadFile(path)
if strings.Contains(string(b), searchFor) { if strings.Contains(string(b), r.Recipe.SearchPattern) {
files = append(files, path) files = append(files, path)
} }
} }
@ -255,8 +259,9 @@ func createUnifiedPatch(changes []FileChange, patchPath string) error {
return nil return nil
} }
// NEW: Safe shell command execution // executeShellCommands — safe, whitelisted, boundary-enforced
func (r *Runner) executeShellCommands(step Step, previousResults []string) { func (r *Runner) executeShellCommands(step Step, previousResults []string, workDir string) {
prompt := fmt.Sprintf(`You are executing shell commands for this step. prompt := fmt.Sprintf(`You are executing shell commands for this step.
Recipe Overview: Recipe Overview:
@ -292,7 +297,7 @@ Only use commands from the allowed list. Never use cd, rm -rf, or anything that
response := r.Client.Stream(messages, r.Model) response := r.Client.Stream(messages, r.Model)
fmt.Println() fmt.Println()
// Parse JSON command list // Parse JSON...
type ShellCommand struct { type ShellCommand struct {
Command string `json:"command"` Command string `json:"command"`
Args []string `json:"args"` Args []string `json:"args"`
@ -311,14 +316,6 @@ Only use commands from the allowed list. Never use cd, rm -rf, or anything that
return return
} }
// Resolve boundary
cwd := "."
if v, ok := r.Recipe.ResolvedParams["package_path"]; ok {
if s, ok := v.(string); ok && s != "" {
cwd = s
}
}
for _, cmd := range cmds { for _, cmd := range cmds {
fullCmd := cmd.Command fullCmd := cmd.Command
if len(cmd.Args) > 0 { if len(cmd.Args) > 0 {
@ -340,9 +337,9 @@ Only use commands from the allowed list. Never use cd, rm -rf, or anything that
continue continue
} }
// Execute with strict cwd // Execute with strict cwd = resolved package_path
execCmd := exec.Command(cmd.Command, cmd.Args...) execCmd := exec.Command(cmd.Command, cmd.Args...)
execCmd.Dir = cwd execCmd.Dir = workDir
output, err := execCmd.CombinedOutput() output, err := execCmd.CombinedOutput()
if err != nil { if err != nil {