Debian Codegraph @

act_turn_executor.go

raw
1 package clnkr
2
3 import (
4 "context"
5 "fmt"
6 "path/filepath"
7 "strings"
8
9 "github.com/clnkr-ai/clnkr/internal/core/transcript"
10 )
11
12 type actTurnExecutor struct {
13 agent *Agent
14 }
15
16 const timeoutGuidance = "The previous command timed out. Use a narrower command, inspect partial output if available, or choose a cheaper verification path. Do not rerun the same long command unchanged."
17
18 func (e actTurnExecutor) Execute(
19 ctx context.Context,
20 act *ActTurn,
21 skipped []BashAction,
22 ) (StepResult, error) {
23 a := e.agent
24 if act == nil || len(act.Bash.Commands) == 0 {
25 return StepResult{Turn: act}, fmt.Errorf("execute act turn: %w", ErrMissingCommand)
26 }
27
28 outputs, execCount := make([]string, 0, len(act.Bash.Commands)), 0
29 var execErr error
30
31 for i, action := range act.Bash.Commands {
32 payload, commandErr := e.executeCommand(ctx, action)
33 execCount++
34
35 outputs = append(outputs, payload)
36 if execErr = commandErr; execErr != nil {
37 for _, notRun := range act.Bash.Commands[i+1:] {
38 if payload, ok := e.agent.appendSkippedToolResult(
39 notRun,
40 "previous command failed",
41 ); ok {
42 outputs = append(outputs, payload)
43 }
44 }
45 break
46 }
47 }
48
49 for _, action := range skipped {
50 if payload, ok := a.appendSkippedToolResult(action, "max steps"); ok {
51 outputs = append(outputs, payload)
52 }
53 }
54 a.appendStateMessageIfNeeded()
55 return StepResult{
56 Turn: act,
57 Output: strings.Join(outputs, "\n\n"),
58 ExecErr: execErr,
59 ExecCount: execCount,
60 }, nil
61 }
62
63 func (e actTurnExecutor) executeCommand(ctx context.Context, action BashAction) (string, error) {
64 a := e.agent
65 if setter, ok := a.executor.(ExecutorStateSetter); ok {
66 setter.SetEnv(a.env)
67 }
68
69 execDir := e.executionDir(action)
70 a.notify(EventCommandStart{Command: action.Command, Dir: execDir})
71
72 execResult, commandErr := a.executor.Execute(ctx, action.Command, execDir)
73 e.applyPostCommandState(execResult)
74 payload := e.formatCommandResult(execResult)
75 if commandErr != nil {
76 a.notify(EventDebug{Message: fmt.Sprintf("command error: %v", commandErr)})
77 }
78 a.notify(EventCommandDone{
79 Command: action.Command,
80 Stdout: execResult.Stdout,
81 Stderr: execResult.Stderr,
82 ExitCode: execResult.ExitCode,
83 Feedback: execResult.Feedback,
84 Err: commandErr,
85 })
86 e.appendCommandMessage(action, execResult, payload)
87 return payload, commandErr
88 }
89
90 func (e actTurnExecutor) executionDir(action BashAction) string {
91 if action.Workdir == "" {
92 return e.agent.cwd
93 }
94 if filepath.IsAbs(action.Workdir) {
95 return action.Workdir
96 }
97 return filepath.Join(e.agent.cwd, action.Workdir)
98 }
99
100 func (e actTurnExecutor) applyPostCommandState(result CommandResult) {
101 a := e.agent
102 if result.PostCwd != "" {
103 a.cwd = result.PostCwd
104 }
105 // PostEnv is a full next-turn snapshot; nil means no new snapshot captured.
106 if result.PostEnv != nil {
107 a.env = result.PostEnv
108 }
109 a.notify(EventDebug{Message: fmt.Sprintf("cwd: %s", a.cwd)})
110 }
111
112 func (e actTurnExecutor) formatCommandResult(result CommandResult) string {
113 guidance := ""
114 if result.Outcome.Type == CommandOutcomeTimeout {
115 guidance = timeoutGuidance
116 }
117 return transcript.FormatCommandResult(transcript.CommandResult{
118 Command: result.Command,
119 Stdout: result.Stdout,
120 Stderr: result.Stderr,
121 ExitCode: result.ExitCode,
122 Outcome: result.Outcome,
123 Feedback: result.Feedback,
124 Guidance: guidance,
125 })
126 }
127
128 func (e actTurnExecutor) appendCommandMessage(
129 action BashAction,
130 result CommandResult,
131 payload string,
132 ) {
133 msg := Message{Role: "user", Content: payload}
134 if action.ID != "" {
135 msg.BashToolResult = &BashToolResult{
136 ID: action.ID,
137 Content: payload,
138 IsError: commandResultIsError(result),
139 }
140 }
141 e.agent.messages = append(e.agent.messages, msg)
142 }