2026-03-06 18:35:58 +00:00
|
|
|
package recipe
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"fmt"
|
|
|
|
|
"os"
|
2026-03-07 18:22:39 +00:00
|
|
|
"path/filepath"
|
2026-03-06 18:35:58 +00:00
|
|
|
"regexp"
|
|
|
|
|
"strings"
|
2026-03-07 18:22:39 +00:00
|
|
|
"sync"
|
2026-03-06 18:35:58 +00:00
|
|
|
"text/template"
|
|
|
|
|
|
|
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
|
)
|
|
|
|
|
|
2026-03-07 17:11:13 +00:00
|
|
|
// Global safe read-only whitelist
|
2026-03-07 18:22:39 +00:00
|
|
|
// LoadSafeCommands reads the user's safe shell commands config (with fallback)
|
|
|
|
|
var safeCommands = sync.OnceValue(func() map[string]bool {
|
|
|
|
|
cfgPath := filepath.Join(os.Getenv("HOME"), ".config", "grokkit", "safe_shell_commands.yaml")
|
|
|
|
|
|
|
|
|
|
data, err := os.ReadFile(cfgPath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
// Fallback to a built-in safe list
|
2026-03-07 18:32:43 +00:00
|
|
|
fmt.Println("Could not read safe shell commands config, using built-in fallback")
|
2026-03-07 18:22:39 +00:00
|
|
|
return map[string]bool{
|
|
|
|
|
"ls": true, "pwd": true, "cat": true, "tree": true,
|
|
|
|
|
"find": true, "grep": true, "rg": true,
|
|
|
|
|
"git status": true, "git log": true, "git diff": true, "git branch": true,
|
|
|
|
|
"go test": true, "go vet": true, "go fmt": true, "go mod tidy": true,
|
|
|
|
|
"make test": true,
|
|
|
|
|
"pytest": true, "poetry run pytest": true, "ctest": true,
|
|
|
|
|
"python -m pytest": true, "python": true, "poetry": true,
|
|
|
|
|
}
|
2026-03-07 18:32:43 +00:00
|
|
|
|
2026-03-07 18:22:39 +00:00
|
|
|
}
|
2026-03-07 18:32:43 +00:00
|
|
|
fmt.Println("Using safe shell commands config:", cfgPath)
|
2026-03-07 18:22:39 +00:00
|
|
|
|
|
|
|
|
var cfg struct {
|
|
|
|
|
SafeCommands []string `yaml:"safe_commands"`
|
|
|
|
|
}
|
|
|
|
|
err = yaml.Unmarshal(data, &cfg)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m := make(map[string]bool)
|
|
|
|
|
for _, c := range cfg.SafeCommands {
|
|
|
|
|
m[strings.ToLower(strings.TrimSpace(c))] = true
|
|
|
|
|
}
|
|
|
|
|
return m
|
|
|
|
|
})
|
2026-03-07 17:11:13 +00:00
|
|
|
|
2026-03-06 18:35:58 +00:00
|
|
|
var (
|
2026-03-06 21:48:35 +00:00
|
|
|
// stepRe still finds the headings (this one is solid)
|
2026-03-06 18:35:58 +00:00
|
|
|
stepRe = regexp.MustCompile(`(?m)^### Step (\d+): (.+)$`)
|
|
|
|
|
)
|
|
|
|
|
|
2026-03-06 20:32:04 +00:00
|
|
|
func Load(path string, userParams map[string]any) (*Recipe, error) {
|
2026-03-06 18:35:58 +00:00
|
|
|
b, err := os.ReadFile(path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
parts := bytes.SplitN(b, []byte("---"), 3)
|
|
|
|
|
if len(parts) < 3 {
|
|
|
|
|
return nil, fmt.Errorf("missing YAML frontmatter")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var r Recipe
|
|
|
|
|
if err := yaml.Unmarshal(parts[1], &r); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("yaml parse: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-07 18:22:39 +00:00
|
|
|
// === SAFETY CHECK using user-configurable whitelist ===
|
|
|
|
|
safeMap := safeCommands()
|
2026-03-07 17:11:13 +00:00
|
|
|
for _, cmd := range r.AllowedShellCommands {
|
2026-03-07 18:22:39 +00:00
|
|
|
trimmed := strings.ToLower(strings.TrimSpace(cmd))
|
2026-03-07 17:59:59 +00:00
|
|
|
allowed := false
|
2026-03-07 18:22:39 +00:00
|
|
|
for safe := range safeMap {
|
2026-03-07 17:59:59 +00:00
|
|
|
if strings.HasPrefix(trimmed, safe) {
|
|
|
|
|
allowed = true
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if !allowed {
|
|
|
|
|
return nil, fmt.Errorf("\u001B[31mRecipe contains unsafe shell command: %q. "+
|
|
|
|
|
"Remove or replace the dangerous command in your recipe.\u001B[0m", cmd)
|
2026-03-07 17:11:13 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-07 00:07:21 +00:00
|
|
|
// Apply defaults + user --param overrides
|
2026-03-06 20:32:04 +00:00
|
|
|
if r.Parameters == nil {
|
|
|
|
|
r.Parameters = make(map[string]Parameter)
|
|
|
|
|
}
|
2026-03-07 00:07:21 +00:00
|
|
|
r.ResolvedParams = make(map[string]any)
|
2026-03-06 20:32:04 +00:00
|
|
|
for name, p := range r.Parameters {
|
|
|
|
|
if v, ok := userParams[name]; ok {
|
2026-03-07 00:07:21 +00:00
|
|
|
r.ResolvedParams[name] = v
|
2026-03-06 20:32:04 +00:00
|
|
|
} else if p.Default != nil {
|
2026-03-07 00:07:21 +00:00
|
|
|
r.ResolvedParams[name] = p.Default
|
2026-03-06 20:32:04 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-07 00:07:21 +00:00
|
|
|
// Render templates with resolved values
|
2026-03-06 18:35:58 +00:00
|
|
|
tpl, err := template.New("recipe").Parse(string(parts[2]))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
var rendered bytes.Buffer
|
2026-03-07 00:07:21 +00:00
|
|
|
if err := tpl.Execute(&rendered, r.ResolvedParams); err != nil {
|
2026-03-06 18:35:58 +00:00
|
|
|
return nil, err
|
|
|
|
|
}
|
2026-03-06 20:46:30 +00:00
|
|
|
body := rendered.String()
|
2026-03-06 18:35:58 +00:00
|
|
|
|
2026-03-06 20:46:30 +00:00
|
|
|
// Extract Overview
|
|
|
|
|
if idx := strings.Index(body, "## Execution Steps"); idx != -1 {
|
|
|
|
|
r.Overview = strings.TrimSpace(body[:idx])
|
2026-03-06 20:32:04 +00:00
|
|
|
}
|
|
|
|
|
|
2026-03-06 21:48:35 +00:00
|
|
|
// Extract steps with robust multi-line parsing
|
2026-03-06 18:35:58 +00:00
|
|
|
matches := stepRe.FindAllStringSubmatch(body, -1)
|
|
|
|
|
for i, m := range matches {
|
|
|
|
|
stepNum := i + 1
|
|
|
|
|
title := m[2]
|
|
|
|
|
|
|
|
|
|
start := strings.Index(body, m[0])
|
|
|
|
|
end := len(body)
|
|
|
|
|
if i+1 < len(matches) {
|
2026-03-06 20:46:30 +00:00
|
|
|
nextStart := strings.Index(body[start:], matches[i+1][0])
|
|
|
|
|
end = start + nextStart
|
2026-03-06 18:35:58 +00:00
|
|
|
}
|
2026-03-06 20:46:30 +00:00
|
|
|
|
2026-03-06 18:35:58 +00:00
|
|
|
section := body[start:end]
|
|
|
|
|
|
|
|
|
|
step := Step{Number: stepNum, Title: title}
|
2026-03-06 21:48:35 +00:00
|
|
|
|
|
|
|
|
// Simple, reliable label-based parsing (handles multi-line + blank lines)
|
|
|
|
|
labels := []string{"**Objective:**", "**Instructions:**", "**Expected output:**"}
|
|
|
|
|
for _, label := range labels {
|
|
|
|
|
labelStart := strings.Index(section, label)
|
|
|
|
|
if labelStart == -1 {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
contentStart := labelStart + len(label)
|
|
|
|
|
contentEnd := len(section)
|
|
|
|
|
|
|
|
|
|
// Find next label or end of section
|
|
|
|
|
for _, nextLabel := range labels {
|
|
|
|
|
next := strings.Index(section[contentStart:], nextLabel)
|
|
|
|
|
if next != -1 {
|
|
|
|
|
contentEnd = contentStart + next
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
content := strings.TrimSpace(section[contentStart:contentEnd])
|
|
|
|
|
|
|
|
|
|
switch label {
|
2026-03-06 18:35:58 +00:00
|
|
|
case "**Objective:**":
|
2026-03-06 21:48:35 +00:00
|
|
|
step.Objective = content
|
2026-03-06 18:35:58 +00:00
|
|
|
case "**Instructions:**":
|
2026-03-06 21:48:35 +00:00
|
|
|
step.Instructions = content
|
2026-03-06 18:35:58 +00:00
|
|
|
case "**Expected output:**":
|
2026-03-06 21:48:35 +00:00
|
|
|
step.Expected = content
|
2026-03-06 18:35:58 +00:00
|
|
|
}
|
|
|
|
|
}
|
2026-03-06 21:48:35 +00:00
|
|
|
|
2026-03-06 18:35:58 +00:00
|
|
|
r.Steps = append(r.Steps, step)
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-06 21:48:35 +00:00
|
|
|
// Final summary (everything after last step)
|
2026-03-06 20:46:30 +00:00
|
|
|
if len(matches) > 0 {
|
|
|
|
|
lastMatch := matches[len(matches)-1][0]
|
|
|
|
|
lastIdx := strings.LastIndex(body, lastMatch)
|
|
|
|
|
r.FinalSummaryPrompt = strings.TrimSpace(body[lastIdx+len(lastMatch):])
|
|
|
|
|
}
|
2026-03-06 18:35:58 +00:00
|
|
|
|
|
|
|
|
return &r, nil
|
|
|
|
|
}
|