| 1 |
package clnkr |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"context" |
| 6 |
"errors" |
| 7 |
"fmt" |
| 8 |
"os" |
| 9 |
"os/exec" |
| 10 |
"syscall" |
| 11 |
"time" |
| 12 |
|
| 13 |
"github.com/clnkr-ai/clnkr/internal/core/gitfeedback" |
| 14 |
"github.com/clnkr-ai/clnkr/internal/core/shellstate" |
| 15 |
) |
| 16 |
|
| 17 |
// ShellExecutor runs bash commands, captures post-command shell state, and |
| 18 |
// always runs commands in their own process group. |
| 19 |
type ShellExecutor struct { |
| 20 |
// BaseEnv, when non-nil, is the complete base environment snapshot for |
| 21 |
// command execution. Direct mutation is caller-owned. |
| 22 |
BaseEnv map[string]string |
| 23 |
} |
| 24 |
|
| 25 |
// SetEnv copies a full environment snapshot for future command execution. |
| 26 |
func (e *ShellExecutor) SetEnv(env map[string]string) { |
| 27 |
if env == nil { |
| 28 |
e.BaseEnv = nil |
| 29 |
return |
| 30 |
} |
| 31 |
e.BaseEnv = make(map[string]string, len(env)) |
| 32 |
for k, v := range env { |
| 33 |
e.BaseEnv[k] = v |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
// Execute runs a command in dir and returns separated stdout/stderr plus exit code. |
| 38 |
func (e *ShellExecutor) Execute( |
| 39 |
ctx context.Context, |
| 40 |
command string, |
| 41 |
dir string, |
| 42 |
) (CommandResult, error) { |
| 43 |
baseline := gitfeedback.Detect(dir) |
| 44 |
|
| 45 |
wrapped, stateFile, cleanup, err := shellstate.Wrap(command) |
| 46 |
if err != nil { |
| 47 |
return CommandResult{ |
| 48 |
Command: command, |
| 49 |
Outcome: commandOutcomeError(err), |
| 50 |
}, fmt.Errorf( |
| 51 |
"wrap command: %w", |
| 52 |
err, |
| 53 |
) |
| 54 |
} |
| 55 |
defer cleanup() |
| 56 |
|
| 57 |
cmd := exec.CommandContext(ctx, "bash", "-c", wrapped) |
| 58 |
cmd.WaitDelay = 500 * time.Millisecond |
| 59 |
cmd.Dir = dir |
| 60 |
baseEnv := os.Environ() |
| 61 |
if e.BaseEnv != nil { |
| 62 |
baseEnv = shellstate.EnvMapToList(e.BaseEnv) |
| 63 |
} |
| 64 |
cmd.Env = append([]string{}, baseEnv...) |
| 65 |
// Appended last; exec uses last-wins for duplicate keys. |
| 66 |
cmd.Env = append(cmd.Env, |
| 67 |
"PAGER=cat", |
| 68 |
"MANPAGER=cat", |
| 69 |
"GIT_PAGER=cat", |
| 70 |
"LESS=-R", |
| 71 |
) |
| 72 |
if stateFile != "" { |
| 73 |
cmd.Env = append(cmd.Env, "CLNKR_STATE_FILE="+stateFile) |
| 74 |
} |
| 75 |
|
| 76 |
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} |
| 77 |
cmd.Cancel = func() error { return syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL) } |
| 78 |
|
| 79 |
var stdout, stderr bytes.Buffer |
| 80 |
cmd.Stdout = &stdout |
| 81 |
cmd.Stderr = &stderr |
| 82 |
|
| 83 |
err = cmd.Run() |
| 84 |
result := CommandResult{Command: command, Stdout: stdout.String(), Stderr: stderr.String()} |
| 85 |
|
| 86 |
if cmd.ProcessState != nil { |
| 87 |
result.ExitCode = cmd.ProcessState.ExitCode() |
| 88 |
} |
| 89 |
var exitErr *exec.ExitError |
| 90 |
if errors.As(err, &exitErr) { |
| 91 |
result.ExitCode = exitErr.ExitCode() |
| 92 |
} |
| 93 |
result.Outcome = classifyCommandOutcome(ctx, err, result.ExitCode) |
| 94 |
|
| 95 |
if err == nil { |
| 96 |
result, stateErr := e.applyPostState(result, stateFile) |
| 97 |
if stateErr != nil { |
| 98 |
result.Outcome = commandOutcomeError(stateErr) |
| 99 |
return result, stateErr |
| 100 |
} |
| 101 |
return applyCommandFeedback(result, baseline, dir), nil |
| 102 |
} |
| 103 |
|
| 104 |
result, stateErr := e.applyPostState(result, stateFile) |
| 105 |
if stateErr != nil { |
| 106 |
return result, fmt.Errorf("run command: %w (shell state: %v)", err, stateErr) |
| 107 |
} |
| 108 |
result = applyCommandFeedback(result, baseline, dir) |
| 109 |
return result, fmt.Errorf("run command: %w", err) |
| 110 |
} |
| 111 |
|
| 112 |
func classifyCommandOutcome(ctx context.Context, err error, exitCode int) CommandOutcome { |
| 113 |
if err == nil { |
| 114 |
return commandOutcomeExit(exitCode) |
| 115 |
} |
| 116 |
if errors.Is(ctx.Err(), context.DeadlineExceeded) { |
| 117 |
return CommandOutcome{Type: CommandOutcomeTimeout} |
| 118 |
} |
| 119 |
if errors.Is(ctx.Err(), context.Canceled) { |
| 120 |
return CommandOutcome{Type: CommandOutcomeCancelled} |
| 121 |
} |
| 122 |
var exitErr *exec.ExitError |
| 123 |
if errors.As(err, &exitErr) { |
| 124 |
return commandOutcomeExit(exitCode) |
| 125 |
} |
| 126 |
return commandOutcomeError(err) |
| 127 |
} |
| 128 |
|
| 129 |
func commandOutcomeExit(code int) CommandOutcome { |
| 130 |
return CommandOutcome{Type: CommandOutcomeExit, ExitCode: &code} |
| 131 |
} |
| 132 |
|
| 133 |
func commandOutcomeError(err error) CommandOutcome { |
| 134 |
message := "" |
| 135 |
if err != nil { |
| 136 |
message = err.Error() |
| 137 |
} |
| 138 |
return CommandOutcome{Type: CommandOutcomeError, Message: message} |
| 139 |
} |
| 140 |
|
| 141 |
func (e *ShellExecutor) applyPostState( |
| 142 |
result CommandResult, |
| 143 |
stateFile string, |
| 144 |
) (CommandResult, error) { |
| 145 |
snapshot, err := shellstate.Load(stateFile) |
| 146 |
if err != nil { |
| 147 |
return result, err |
| 148 |
} |
| 149 |
result.PostCwd = snapshot.Cwd |
| 150 |
result.PostEnv = snapshot.Env |
| 151 |
return result, nil |
| 152 |
} |
| 153 |
|
| 154 |
func applyCommandFeedback( |
| 155 |
result CommandResult, |
| 156 |
baseline gitfeedback.Baseline, |
| 157 |
dir string, |
| 158 |
) CommandResult { |
| 159 |
finalCwd := dir |
| 160 |
if result.PostCwd != "" { |
| 161 |
finalCwd = result.PostCwd |
| 162 |
} |
| 163 |
|
| 164 |
summary, ok := baseline.Collect(finalCwd) |
| 165 |
if !ok { |
| 166 |
return result |
| 167 |
} |
| 168 |
result.Feedback = CommandFeedback{ |
| 169 |
ChangedFiles: summary.ChangedFiles, |
| 170 |
Diff: summary.Diff, |
| 171 |
} |
| 172 |
return result |
| 173 |
} |