blob: 1d21a48c4a7615e265f881bf91a61b1ce92f77f1 [file] [log] [blame]
Earl Lee2e463fb2025-04-17 11:22:22 -07001package loop
2
3import (
4 "context"
Josh Bleecher Snyderdbe02302025-04-29 16:44:23 -07005 _ "embed"
Earl Lee2e463fb2025-04-17 11:22:22 -07006 "encoding/json"
7 "fmt"
Josh Bleecher Snyder3e2111b2025-04-30 17:53:28 +00008 "io"
Earl Lee2e463fb2025-04-17 11:22:22 -07009 "log/slog"
10 "net/http"
11 "os"
12 "os/exec"
Pokey Rule7a113622025-05-12 10:58:45 +010013 "path/filepath"
Earl Lee2e463fb2025-04-17 11:22:22 -070014 "runtime/debug"
15 "slices"
Philip Zeyligerb8a8f352025-06-02 07:39:37 -070016 "strconv"
Earl Lee2e463fb2025-04-17 11:22:22 -070017 "strings"
18 "sync"
Josh Bleecher Snyder5cca56f2025-05-06 01:10:16 +000019 "text/template"
Earl Lee2e463fb2025-04-17 11:22:22 -070020 "time"
21
Josh Bleecher Snyder3e2111b2025-04-30 17:53:28 +000022 "sketch.dev/browser"
Earl Lee2e463fb2025-04-17 11:22:22 -070023 "sketch.dev/claudetool"
Josh Bleecher Snyderd499fd62025-04-30 01:31:29 +000024 "sketch.dev/claudetool/bashkit"
Autoformatter4962f152025-05-06 17:24:20 +000025 "sketch.dev/claudetool/browse"
Josh Bleecher Snyderf4047bb2025-05-05 23:02:56 +000026 "sketch.dev/claudetool/codereview"
Josh Bleecher Snydera997be62025-05-07 22:52:46 +000027 "sketch.dev/claudetool/onstart"
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -070028 "sketch.dev/llm"
Philip Zeyliger72252cb2025-05-10 17:00:08 -070029 "sketch.dev/llm/ant"
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -070030 "sketch.dev/llm/conversation"
Philip Zeyligerc17ffe32025-06-05 19:49:13 -070031 "sketch.dev/skabandclient"
Earl Lee2e463fb2025-04-17 11:22:22 -070032)
33
34const (
35 userCancelMessage = "user requested agent to stop handling responses"
36)
37
Philip Zeyligerb7c58752025-05-01 10:10:17 -070038type MessageIterator interface {
39 // Next blocks until the next message is available. It may
40 // return nil if the underlying iterator context is done.
41 Next() *AgentMessage
42 Close()
43}
44
Earl Lee2e463fb2025-04-17 11:22:22 -070045type CodingAgent interface {
46 // Init initializes an agent inside a docker container.
47 Init(AgentInit) error
48
49 // Ready returns a channel closed after Init successfully called.
50 Ready() <-chan struct{}
51
52 // URL reports the HTTP URL of this agent.
53 URL() string
54
55 // UserMessage enqueues a message to the agent and returns immediately.
56 UserMessage(ctx context.Context, msg string)
57
Philip Zeyligerb7c58752025-05-01 10:10:17 -070058 // Returns an iterator that finishes when the context is done and
59 // starts with the given message index.
60 NewIterator(ctx context.Context, nextMessageIdx int) MessageIterator
Earl Lee2e463fb2025-04-17 11:22:22 -070061
Philip Zeyligereab12de2025-05-14 02:35:53 +000062 // Returns an iterator that notifies of state transitions until the context is done.
63 NewStateTransitionIterator(ctx context.Context) StateTransitionIterator
64
Earl Lee2e463fb2025-04-17 11:22:22 -070065 // Loop begins the agent loop returns only when ctx is cancelled.
66 Loop(ctx context.Context)
67
Philip Zeyligerbe7802a2025-06-04 20:15:25 +000068 // BranchPrefix returns the configured branch prefix
69 BranchPrefix() string
70
Sean McCulloughedc88dc2025-04-30 02:55:01 +000071 CancelTurn(cause error)
Earl Lee2e463fb2025-04-17 11:22:22 -070072
73 CancelToolUse(toolUseID string, cause error) error
74
75 // Returns a subset of the agent's message history.
76 Messages(start int, end int) []AgentMessage
77
78 // Returns the current number of messages in the history
79 MessageCount() int
80
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -070081 TotalUsage() conversation.CumulativeUsage
82 OriginalBudget() conversation.Budget
Earl Lee2e463fb2025-04-17 11:22:22 -070083
Earl Lee2e463fb2025-04-17 11:22:22 -070084 WorkingDir() string
Josh Bleecher Snyderc5848f32025-05-28 18:50:58 +000085 RepoRoot() string
Earl Lee2e463fb2025-04-17 11:22:22 -070086
87 // Diff returns a unified diff of changes made since the agent was instantiated.
88 // If commit is non-nil, it shows the diff for just that specific commit.
89 Diff(commit *string) (string, error)
90
Philip Zeyliger49edc922025-05-14 09:45:45 -070091 // SketchGitBase returns the commit that's the "base" for Sketch's work. It
92 // starts out as the commit where sketch started, but a user can move it if need
93 // be, for example in the case of a rebase. It is stored as a git tag.
94 SketchGitBase() string
Earl Lee2e463fb2025-04-17 11:22:22 -070095
Philip Zeyligerd3ac1122025-05-14 02:54:18 +000096 // SketchGitBase returns the symbolic name for the "base" for Sketch's work.
97 // (Typically, this is "sketch-base")
98 SketchGitBaseRef() string
99
Josh Bleecher Snyder19969a92025-06-05 14:34:02 -0700100 // Slug returns the slug identifier for this session.
101 Slug() string
Earl Lee2e463fb2025-04-17 11:22:22 -0700102
Josh Bleecher Snyder47b19362025-04-30 01:34:14 +0000103 // BranchName returns the git branch name for the conversation.
104 BranchName() string
105
Josh Bleecher Snyder19969a92025-06-05 14:34:02 -0700106 // IncrementRetryNumber increments the retry number for branch naming conflicts.
107 IncrementRetryNumber()
108
Earl Lee2e463fb2025-04-17 11:22:22 -0700109 // OS returns the operating system of the client.
110 OS() string
Philip Zeyliger99a9a022025-04-27 15:15:25 +0000111
Philip Zeyligerc72fff52025-04-29 20:17:54 +0000112 // SessionID returns the unique session identifier.
113 SessionID() string
114
Philip Zeyliger75bd37d2025-05-22 18:49:14 +0000115 // DetectGitChanges checks for new git commits and pushes them if found
Philip Zeyliger9bca61e2025-05-22 12:40:06 -0700116 DetectGitChanges(ctx context.Context) error
Philip Zeyliger75bd37d2025-05-22 18:49:14 +0000117
Philip Zeyliger99a9a022025-04-27 15:15:25 +0000118 // OutstandingLLMCallCount returns the number of outstanding LLM calls.
119 OutstandingLLMCallCount() int
120
121 // OutstandingToolCalls returns the names of outstanding tool calls.
122 OutstandingToolCalls() []string
Philip Zeyliger18532b22025-04-23 21:11:46 +0000123 OutsideOS() string
124 OutsideHostname() string
125 OutsideWorkingDir() string
Philip Zeyligerd1402952025-04-23 03:54:37 +0000126 GitOrigin() string
Josh Bleecher Snyder3e2111b2025-04-30 17:53:28 +0000127 // OpenBrowser is a best-effort attempt to open a browser at url in outside sketch.
128 OpenBrowser(url string)
Philip Zeyliger2c4db092025-04-28 16:57:50 -0700129
Philip Zeyliger2c4db092025-04-28 16:57:50 -0700130 // IsInContainer returns true if the agent is running in a container
131 IsInContainer() bool
132 // FirstMessageIndex returns the index of the first message in the current conversation
133 FirstMessageIndex() int
Sean McCulloughd9d45812025-04-30 16:53:41 -0700134
135 CurrentStateName() string
Josh Bleecher Snyder112b9232025-05-23 11:26:33 -0700136 // CurrentTodoContent returns the current todo list data as JSON, or empty string if no todos exist
137 CurrentTodoContent() string
Philip Zeyligerb8a8f352025-06-02 07:39:37 -0700138
139 // CompactConversation compacts the current conversation by generating a summary
140 // and restarting the conversation with that summary as the initial context
141 CompactConversation(ctx context.Context) error
Sean McCullough138ec242025-06-02 22:42:06 +0000142 // GetPortMonitor returns the port monitor instance for accessing port events
143 GetPortMonitor() *PortMonitor
Earl Lee2e463fb2025-04-17 11:22:22 -0700144}
145
146type CodingAgentMessageType string
147
148const (
149 UserMessageType CodingAgentMessageType = "user"
150 AgentMessageType CodingAgentMessageType = "agent"
151 ErrorMessageType CodingAgentMessageType = "error"
152 BudgetMessageType CodingAgentMessageType = "budget" // dedicated for "out of budget" errors
153 ToolUseMessageType CodingAgentMessageType = "tool"
Philip Zeyligerb8a8f352025-06-02 07:39:37 -0700154 CommitMessageType CodingAgentMessageType = "commit" // for displaying git commits
155 AutoMessageType CodingAgentMessageType = "auto" // for automated notifications like autoformatting
156 CompactMessageType CodingAgentMessageType = "compact" // for conversation compaction notifications
Earl Lee2e463fb2025-04-17 11:22:22 -0700157
158 cancelToolUseMessage = "Stop responding to my previous message. Wait for me to ask you something else before attempting to use any more tools."
159)
160
161type AgentMessage struct {
162 Type CodingAgentMessageType `json:"type"`
163 // EndOfTurn indicates that the AI is done working and is ready for the next user input.
164 EndOfTurn bool `json:"end_of_turn"`
165
166 Content string `json:"content"`
167 ToolName string `json:"tool_name,omitempty"`
168 ToolInput string `json:"input,omitempty"`
169 ToolResult string `json:"tool_result,omitempty"`
170 ToolError bool `json:"tool_error,omitempty"`
171 ToolCallId string `json:"tool_call_id,omitempty"`
172
173 // ToolCalls is a list of all tool calls requested in this message (name and input pairs)
174 ToolCalls []ToolCall `json:"tool_calls,omitempty"`
175
Sean McCulloughd9f13372025-04-21 15:08:49 -0700176 // ToolResponses is a list of all responses to tool calls requested in this message (name and input pairs)
177 ToolResponses []AgentMessage `json:"toolResponses,omitempty"`
178
Earl Lee2e463fb2025-04-17 11:22:22 -0700179 // Commits is a list of git commits for a commit message
180 Commits []*GitCommit `json:"commits,omitempty"`
181
182 Timestamp time.Time `json:"timestamp"`
183 ConversationID string `json:"conversation_id"`
184 ParentConversationID *string `json:"parent_conversation_id,omitempty"`
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -0700185 Usage *llm.Usage `json:"usage,omitempty"`
Earl Lee2e463fb2025-04-17 11:22:22 -0700186
187 // Message timing information
188 StartTime *time.Time `json:"start_time,omitempty"`
189 EndTime *time.Time `json:"end_time,omitempty"`
190 Elapsed *time.Duration `json:"elapsed,omitempty"`
191
192 // Turn duration - the time taken for a complete agent turn
193 TurnDuration *time.Duration `json:"turnDuration,omitempty"`
194
Josh Bleecher Snyder4d544932025-05-07 13:33:53 +0000195 // HideOutput indicates that this message should not be rendered in the UI.
196 // This is useful for subconversations that generate output that shouldn't be shown to the user.
197 HideOutput bool `json:"hide_output,omitempty"`
198
Josh Bleecher Snyder112b9232025-05-23 11:26:33 -0700199 // TodoContent contains the agent's todo file content when it has changed
200 TodoContent *string `json:"todo_content,omitempty"`
201
Earl Lee2e463fb2025-04-17 11:22:22 -0700202 Idx int `json:"idx"`
203}
204
Josh Bleecher Snyder4d544932025-05-07 13:33:53 +0000205// SetConvo sets m.ConversationID, m.ParentConversationID, and m.HideOutput based on convo.
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -0700206func (m *AgentMessage) SetConvo(convo *conversation.Convo) {
Josh Bleecher Snyder50a1d622025-04-29 09:59:03 -0700207 if convo == nil {
208 m.ConversationID = ""
209 m.ParentConversationID = nil
210 return
211 }
212 m.ConversationID = convo.ID
Josh Bleecher Snyder4d544932025-05-07 13:33:53 +0000213 m.HideOutput = convo.Hidden
Josh Bleecher Snyder50a1d622025-04-29 09:59:03 -0700214 if convo.Parent != nil {
215 m.ParentConversationID = &convo.Parent.ID
216 }
217}
218
Earl Lee2e463fb2025-04-17 11:22:22 -0700219// GitCommit represents a single git commit for a commit message
220type GitCommit struct {
221 Hash string `json:"hash"` // Full commit hash
222 Subject string `json:"subject"` // Commit subject line
223 Body string `json:"body"` // Full commit message body
224 PushedBranch string `json:"pushed_branch,omitempty"` // If set, this commit was pushed to this branch
225}
226
227// ToolCall represents a single tool call within an agent message
228type ToolCall struct {
Sean McCulloughd9f13372025-04-21 15:08:49 -0700229 Name string `json:"name"`
230 Input string `json:"input"`
231 ToolCallId string `json:"tool_call_id"`
232 ResultMessage *AgentMessage `json:"result_message,omitempty"`
233 Args string `json:"args,omitempty"`
234 Result string `json:"result,omitempty"`
Earl Lee2e463fb2025-04-17 11:22:22 -0700235}
236
237func (a *AgentMessage) Attr() slog.Attr {
238 var attrs []any = []any{
239 slog.String("type", string(a.Type)),
240 }
Philip Zeyligerb7c58752025-05-01 10:10:17 -0700241 attrs = append(attrs, slog.Int("idx", a.Idx))
Earl Lee2e463fb2025-04-17 11:22:22 -0700242 if a.EndOfTurn {
243 attrs = append(attrs, slog.Bool("end_of_turn", a.EndOfTurn))
244 }
245 if a.Content != "" {
246 attrs = append(attrs, slog.String("content", a.Content))
247 }
248 if a.ToolName != "" {
249 attrs = append(attrs, slog.String("tool_name", a.ToolName))
250 }
251 if a.ToolInput != "" {
252 attrs = append(attrs, slog.String("tool_input", a.ToolInput))
253 }
254 if a.Elapsed != nil {
255 attrs = append(attrs, slog.Int64("elapsed", a.Elapsed.Nanoseconds()))
256 }
257 if a.TurnDuration != nil {
258 attrs = append(attrs, slog.Int64("turnDuration", a.TurnDuration.Nanoseconds()))
259 }
Philip Zeyliger72252cb2025-05-10 17:00:08 -0700260 if len(a.ToolResult) > 0 {
261 attrs = append(attrs, slog.Any("tool_result", a.ToolResult))
Earl Lee2e463fb2025-04-17 11:22:22 -0700262 }
263 if a.ToolError {
264 attrs = append(attrs, slog.Bool("tool_error", a.ToolError))
265 }
266 if len(a.ToolCalls) > 0 {
267 toolCallAttrs := make([]any, 0, len(a.ToolCalls))
268 for i, tc := range a.ToolCalls {
269 toolCallAttrs = append(toolCallAttrs, slog.Group(
270 fmt.Sprintf("tool_call_%d", i),
271 slog.String("name", tc.Name),
272 slog.String("input", tc.Input),
273 ))
274 }
275 attrs = append(attrs, slog.Group("tool_calls", toolCallAttrs...))
276 }
277 if a.ConversationID != "" {
278 attrs = append(attrs, slog.String("convo_id", a.ConversationID))
279 }
280 if a.ParentConversationID != nil {
281 attrs = append(attrs, slog.String("parent_convo_id", *a.ParentConversationID))
282 }
283 if a.Usage != nil && !a.Usage.IsZero() {
284 attrs = append(attrs, a.Usage.Attr())
285 }
286 // TODO: timestamp, convo ids, idx?
287 return slog.Group("agent_message", attrs...)
288}
289
290func errorMessage(err error) AgentMessage {
291 // It's somewhat unknowable whether error messages are "end of turn" or not, but it seems like the best approach.
292 if os.Getenv(("DEBUG")) == "1" {
293 return AgentMessage{Type: ErrorMessageType, Content: err.Error() + " Stacktrace: " + string(debug.Stack()), EndOfTurn: true}
294 }
295
296 return AgentMessage{Type: ErrorMessageType, Content: err.Error(), EndOfTurn: true}
297}
298
299func budgetMessage(err error) AgentMessage {
300 return AgentMessage{Type: BudgetMessageType, Content: err.Error(), EndOfTurn: true}
301}
302
303// ConvoInterface defines the interface for conversation interactions
304type ConvoInterface interface {
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -0700305 CumulativeUsage() conversation.CumulativeUsage
Philip Zeyligerb8a8f352025-06-02 07:39:37 -0700306 LastUsage() llm.Usage
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -0700307 ResetBudget(conversation.Budget)
Earl Lee2e463fb2025-04-17 11:22:22 -0700308 OverBudget() error
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -0700309 SendMessage(message llm.Message) (*llm.Response, error)
310 SendUserTextMessage(s string, otherContents ...llm.Content) (*llm.Response, error)
Philip Zeyliger2c4db092025-04-28 16:57:50 -0700311 GetID() string
Josh Bleecher Snyder64f2aa82025-05-14 18:31:05 +0000312 ToolResultContents(ctx context.Context, resp *llm.Response) ([]llm.Content, bool, error)
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -0700313 ToolResultCancelContents(resp *llm.Response) ([]llm.Content, error)
Earl Lee2e463fb2025-04-17 11:22:22 -0700314 CancelToolUse(toolUseID string, cause error) error
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -0700315 SubConvoWithHistory() *conversation.Convo
Earl Lee2e463fb2025-04-17 11:22:22 -0700316}
317
Philip Zeyligerf2872992025-05-22 10:35:28 -0700318// AgentGitState holds the state necessary for pushing to a remote git repo
319// when HEAD changes. If gitRemoteAddr is set, then we push to sketch/
320// any time we notice we need to.
321type AgentGitState struct {
322 mu sync.Mutex // protects following
323 lastHEAD string // hash of the last HEAD that was pushed to the host
324 gitRemoteAddr string // HTTP URL of the host git repo
Josh Bleecher Snyder664404e2025-06-04 21:56:42 +0000325 upstream string // upstream branch for git work
Philip Zeyligerf2872992025-05-22 10:35:28 -0700326 seenCommits map[string]bool // Track git commits we've already seen (by hash)
Josh Bleecher Snyder19969a92025-06-05 14:34:02 -0700327 slug string // Human-readable session identifier
328 retryNumber int // Number to append when branch conflicts occur
Philip Zeyligerf2872992025-05-22 10:35:28 -0700329}
330
Josh Bleecher Snyder19969a92025-06-05 14:34:02 -0700331func (ags *AgentGitState) SetSlug(slug string) {
Philip Zeyligerf2872992025-05-22 10:35:28 -0700332 ags.mu.Lock()
333 defer ags.mu.Unlock()
Josh Bleecher Snyder19969a92025-06-05 14:34:02 -0700334 if ags.slug != slug {
335 ags.retryNumber = 0
336 }
337 ags.slug = slug
Philip Zeyligerf2872992025-05-22 10:35:28 -0700338}
339
Josh Bleecher Snyder19969a92025-06-05 14:34:02 -0700340func (ags *AgentGitState) Slug() string {
Philip Zeyligerf2872992025-05-22 10:35:28 -0700341 ags.mu.Lock()
342 defer ags.mu.Unlock()
Josh Bleecher Snyder19969a92025-06-05 14:34:02 -0700343 return ags.slug
344}
345
346func (ags *AgentGitState) IncrementRetryNumber() {
347 ags.mu.Lock()
348 defer ags.mu.Unlock()
349 ags.retryNumber++
350}
351
352// HasSeenCommits returns true if any commits have been processed
353func (ags *AgentGitState) HasSeenCommits() bool {
354 ags.mu.Lock()
355 defer ags.mu.Unlock()
356 return len(ags.seenCommits) > 0
357}
358
359func (ags *AgentGitState) RetryNumber() int {
360 ags.mu.Lock()
361 defer ags.mu.Unlock()
362 return ags.retryNumber
363}
364
365func (ags *AgentGitState) BranchName(prefix string) string {
366 ags.mu.Lock()
367 defer ags.mu.Unlock()
368 return ags.branchNameLocked(prefix)
369}
370
371func (ags *AgentGitState) branchNameLocked(prefix string) string {
372 if ags.slug == "" {
373 return ""
374 }
375 if ags.retryNumber == 0 {
376 return prefix + ags.slug
377 }
378 return fmt.Sprintf("%s%s%d", prefix, ags.slug, ags.retryNumber)
Philip Zeyligerf2872992025-05-22 10:35:28 -0700379}
380
Josh Bleecher Snyder664404e2025-06-04 21:56:42 +0000381func (ags *AgentGitState) Upstream() string {
382 ags.mu.Lock()
383 defer ags.mu.Unlock()
384 return ags.upstream
385}
386
Earl Lee2e463fb2025-04-17 11:22:22 -0700387type Agent struct {
Philip Zeyliger2c4db092025-04-28 16:57:50 -0700388 convo ConvoInterface
389 config AgentConfig // config for this agent
Philip Zeyligerf2872992025-05-22 10:35:28 -0700390 gitState AgentGitState
Philip Zeyliger2c4db092025-04-28 16:57:50 -0700391 workingDir string
392 repoRoot string // workingDir may be a subdir of repoRoot
393 url string
394 firstMessageIndex int // index of the first message in the current conversation
Josh Bleecher Snyder3e2111b2025-04-30 17:53:28 +0000395 outsideHTTP string // base address of the outside webserver (only when under docker)
Philip Zeyliger2c4db092025-04-28 16:57:50 -0700396 ready chan struct{} // closed when the agent is initialized (only when under docker)
Josh Bleecher Snydera997be62025-05-07 22:52:46 +0000397 codebase *onstart.Codebase
Philip Zeyliger2c4db092025-04-28 16:57:50 -0700398 startedAt time.Time
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -0700399 originalBudget conversation.Budget
Josh Bleecher Snyderf4047bb2025-05-05 23:02:56 +0000400 codereview *codereview.CodeReviewer
Sean McCullough96b60dd2025-04-30 09:49:10 -0700401 // State machine to track agent state
402 stateMachine *StateMachine
Philip Zeyliger18532b22025-04-23 21:11:46 +0000403 // Outside information
404 outsideHostname string
405 outsideOS string
406 outsideWorkingDir string
Philip Zeyligerd1402952025-04-23 03:54:37 +0000407 // URL of the git remote 'origin' if it exists
408 gitOrigin string
Earl Lee2e463fb2025-04-17 11:22:22 -0700409
410 // Time when the current turn started (reset at the beginning of InnerLoop)
411 startOfTurn time.Time
412
413 // Inbox - for messages from the user to the agent.
414 // sent on by UserMessage
415 // . e.g. when user types into the chat textarea
416 // read from by GatherMessages
417 inbox chan string
418
Sean McCulloughedc88dc2025-04-30 02:55:01 +0000419 // protects cancelTurn
420 cancelTurnMu sync.Mutex
Earl Lee2e463fb2025-04-17 11:22:22 -0700421 // cancels potentially long-running tool_use calls or chains of them
Sean McCulloughedc88dc2025-04-30 02:55:01 +0000422 cancelTurn context.CancelCauseFunc
Earl Lee2e463fb2025-04-17 11:22:22 -0700423
424 // protects following
425 mu sync.Mutex
426
427 // Stores all messages for this agent
428 history []AgentMessage
429
Philip Zeyligerb7c58752025-05-01 10:10:17 -0700430 // Iterators add themselves here when they're ready to be notified of new messages.
431 subscribers []chan *AgentMessage
Earl Lee2e463fb2025-04-17 11:22:22 -0700432
Philip Zeyliger99a9a022025-04-27 15:15:25 +0000433 // Track outstanding LLM call IDs
434 outstandingLLMCalls map[string]struct{}
435
436 // Track outstanding tool calls by ID with their names
437 outstandingToolCalls map[string]string
Sean McCullough364f7412025-06-02 00:55:44 +0000438
439 // Port monitoring
440 portMonitor *PortMonitor
Earl Lee2e463fb2025-04-17 11:22:22 -0700441}
442
Philip Zeyligerb7c58752025-05-01 10:10:17 -0700443// NewIterator implements CodingAgent.
444func (a *Agent) NewIterator(ctx context.Context, nextMessageIdx int) MessageIterator {
445 a.mu.Lock()
446 defer a.mu.Unlock()
447
448 return &MessageIteratorImpl{
449 agent: a,
450 ctx: ctx,
451 nextMessageIdx: nextMessageIdx,
452 ch: make(chan *AgentMessage, 100),
453 }
454}
455
456type MessageIteratorImpl struct {
457 agent *Agent
458 ctx context.Context
459 nextMessageIdx int
460 ch chan *AgentMessage
461 subscribed bool
462}
463
464func (m *MessageIteratorImpl) Close() {
465 m.agent.mu.Lock()
466 defer m.agent.mu.Unlock()
467 // Delete ourselves from the subscribers list
468 m.agent.subscribers = slices.DeleteFunc(m.agent.subscribers, func(x chan *AgentMessage) bool {
469 return x == m.ch
470 })
471 close(m.ch)
472}
473
474func (m *MessageIteratorImpl) Next() *AgentMessage {
475 // We avoid subscription at creation to let ourselves catch up to "current state"
476 // before subscribing.
477 if !m.subscribed {
478 m.agent.mu.Lock()
479 if m.nextMessageIdx < len(m.agent.history) {
480 msg := &m.agent.history[m.nextMessageIdx]
481 m.nextMessageIdx++
482 m.agent.mu.Unlock()
483 return msg
484 }
485 // The next message doesn't exist yet, so let's subscribe
486 m.agent.subscribers = append(m.agent.subscribers, m.ch)
487 m.subscribed = true
488 m.agent.mu.Unlock()
489 }
490
491 for {
492 select {
493 case <-m.ctx.Done():
494 m.agent.mu.Lock()
495 // Delete ourselves from the subscribers list
496 m.agent.subscribers = slices.DeleteFunc(m.agent.subscribers, func(x chan *AgentMessage) bool {
497 return x == m.ch
498 })
499 m.subscribed = false
500 m.agent.mu.Unlock()
501 return nil
502 case msg, ok := <-m.ch:
503 if !ok {
504 // Close may have been called
505 return nil
506 }
507 if msg.Idx == m.nextMessageIdx {
508 m.nextMessageIdx++
509 return msg
510 }
511 slog.Debug("Out of order messages", "expected", m.nextMessageIdx, "got", msg.Idx, "m", msg.Content)
512 panic("out of order message")
513 }
514 }
515}
516
Sean McCulloughd9d45812025-04-30 16:53:41 -0700517// Assert that Agent satisfies the CodingAgent interface.
518var _ CodingAgent = &Agent{}
519
520// StateName implements CodingAgent.
521func (a *Agent) CurrentStateName() string {
522 if a.stateMachine == nil {
523 return ""
524 }
Josh Bleecher Snydered17fdf2025-05-23 17:26:07 +0000525 return a.stateMachine.CurrentState().String()
Sean McCulloughd9d45812025-04-30 16:53:41 -0700526}
527
Josh Bleecher Snyder112b9232025-05-23 11:26:33 -0700528// CurrentTodoContent returns the current todo list data as JSON.
529// It returns an empty string if no todos exist.
530func (a *Agent) CurrentTodoContent() string {
531 todoPath := claudetool.TodoFilePath(a.config.SessionID)
532 content, err := os.ReadFile(todoPath)
533 if err != nil {
534 return ""
535 }
536 return string(content)
537}
538
Philip Zeyligerb8a8f352025-06-02 07:39:37 -0700539// generateConversationSummary asks the LLM to create a comprehensive summary of the current conversation
540func (a *Agent) generateConversationSummary(ctx context.Context) (string, error) {
541 msg := `You are being asked to create a comprehensive summary of our conversation so far. This summary will be used to restart our conversation with a shorter history while preserving all important context.
542
543IMPORTANT: Focus ONLY on the actual conversation with the user. Do NOT include any information from system prompts, tool descriptions, or general instructions. Only summarize what the user asked for and what we accomplished together.
544
545Please create a detailed summary that includes:
546
5471. **User's Request**: What did the user originally ask me to do? What was their goal?
548
5492. **Work Completed**: What have we accomplished together? Include any code changes, files created/modified, problems solved, etc.
550
5513. **Key Technical Decisions**: What important technical choices were made during our work and why?
552
5534. **Current State**: What is the current state of the project? What files, tools, or systems are we working with?
554
5555. **Next Steps**: What still needs to be done to complete the user's request?
556
5576. **Important Context**: Any crucial information about the user's codebase, environment, constraints, or specific preferences they mentioned.
558
559Focus on actionable information that would help me continue the user's work seamlessly. Ignore any general tool capabilities or system instructions - only include what's relevant to this specific user's project and goals.
560
561Reply with ONLY the summary content - no meta-commentary about creating the summary.`
562
563 userMessage := llm.UserStringMessage(msg)
564 // Use a subconversation with history to get the summary
565 // TODO: We don't have any tools here, so we should have enough tokens
566 // to capture a summary, but we may need to modify the history (e.g., remove
567 // TODO data) to save on some tokens.
568 convo := a.convo.SubConvoWithHistory()
569
570 // Modify the system prompt to provide context about the original task
571 originalSystemPrompt := convo.SystemPrompt
Josh Bleecher Snyder068f4bb2025-06-05 19:12:22 +0000572 convo.SystemPrompt = `You are creating a conversation summary for context compaction. The original system prompt contained instructions about being a software engineer and architect for Sketch (an agentic coding environment), with various tools and capabilities for code analysis, file modification, git operations, browser automation, and project management.
Philip Zeyligerb8a8f352025-06-02 07:39:37 -0700573
574Your task is to create a focused summary as requested below. Focus only on the actual user conversation and work accomplished, not the system capabilities or tool descriptions.
575
Josh Bleecher Snyder068f4bb2025-06-05 19:12:22 +0000576Original context: You are working in a coding environment with full access to development tools.`
Philip Zeyligerb8a8f352025-06-02 07:39:37 -0700577
578 resp, err := convo.SendMessage(userMessage)
579 if err != nil {
580 a.pushToOutbox(ctx, errorMessage(err))
581 return "", err
582 }
583 textContent := collectTextContent(resp)
584
585 // Restore original system prompt (though this subconvo will be discarded)
586 convo.SystemPrompt = originalSystemPrompt
587
588 return textContent, nil
589}
590
591// CompactConversation compacts the current conversation by generating a summary
592// and restarting the conversation with that summary as the initial context
593func (a *Agent) CompactConversation(ctx context.Context) error {
594 summary, err := a.generateConversationSummary(ctx)
595 if err != nil {
596 return fmt.Errorf("failed to generate conversation summary: %w", err)
597 }
598
599 a.mu.Lock()
600
601 // Get usage information before resetting conversation
602 lastUsage := a.convo.LastUsage()
603 contextWindow := a.config.Service.TokenContextWindow()
604 currentContextSize := lastUsage.InputTokens + lastUsage.CacheReadInputTokens + lastUsage.CacheCreationInputTokens
605
606 // Reset conversation state but keep all other state (git, working dir, etc.)
607 a.firstMessageIndex = len(a.history)
608 a.convo = a.initConvo()
609
610 a.mu.Unlock()
611
612 // Create informative compaction message with token details
613 compactionMsg := fmt.Sprintf("📜 Conversation compacted to manage token limits. Previous context preserved in summary below.\n\n"+
614 "**Token Usage:** %d / %d tokens (%.1f%% of context window)",
615 currentContextSize, contextWindow, float64(currentContextSize)/float64(contextWindow)*100)
616
617 a.pushToOutbox(ctx, AgentMessage{
618 Type: CompactMessageType,
619 Content: compactionMsg,
620 })
621
622 a.pushToOutbox(ctx, AgentMessage{
623 Type: UserMessageType,
624 Content: fmt.Sprintf("Here's a summary of our previous work:\n\n%s\n\nPlease continue with the work based on this summary.", summary),
625 })
626 a.inbox <- fmt.Sprintf("Here's a summary of our previous work:\n\n%s\n\nPlease continue with the work based on this summary.", summary)
627
628 return nil
629}
630
Earl Lee2e463fb2025-04-17 11:22:22 -0700631func (a *Agent) URL() string { return a.url }
632
Josh Bleecher Snyder47b19362025-04-30 01:34:14 +0000633// BranchName returns the git branch name for the conversation.
634func (a *Agent) BranchName() string {
Josh Bleecher Snyder19969a92025-06-05 14:34:02 -0700635 return a.gitState.BranchName(a.config.BranchPrefix)
636}
637
638// Slug returns the slug identifier for this conversation.
639func (a *Agent) Slug() string {
640 return a.gitState.Slug()
641}
642
643// IncrementRetryNumber increments the retry number for branch naming conflicts
644func (a *Agent) IncrementRetryNumber() {
645 a.gitState.IncrementRetryNumber()
Josh Bleecher Snyder47b19362025-04-30 01:34:14 +0000646}
647
Philip Zeyliger99a9a022025-04-27 15:15:25 +0000648// OutstandingLLMCallCount returns the number of outstanding LLM calls.
649func (a *Agent) OutstandingLLMCallCount() int {
650 a.mu.Lock()
651 defer a.mu.Unlock()
652 return len(a.outstandingLLMCalls)
653}
654
655// OutstandingToolCalls returns the names of outstanding tool calls.
656func (a *Agent) OutstandingToolCalls() []string {
657 a.mu.Lock()
658 defer a.mu.Unlock()
659
660 tools := make([]string, 0, len(a.outstandingToolCalls))
661 for _, toolName := range a.outstandingToolCalls {
662 tools = append(tools, toolName)
663 }
664 return tools
665}
666
Earl Lee2e463fb2025-04-17 11:22:22 -0700667// OS returns the operating system of the client.
668func (a *Agent) OS() string {
669 return a.config.ClientGOOS
670}
671
Philip Zeyligerc72fff52025-04-29 20:17:54 +0000672func (a *Agent) SessionID() string {
673 return a.config.SessionID
674}
675
Philip Zeyliger18532b22025-04-23 21:11:46 +0000676// OutsideOS returns the operating system of the outside system.
677func (a *Agent) OutsideOS() string {
678 return a.outsideOS
Philip Zeyligerd1402952025-04-23 03:54:37 +0000679}
680
Philip Zeyliger18532b22025-04-23 21:11:46 +0000681// OutsideHostname returns the hostname of the outside system.
682func (a *Agent) OutsideHostname() string {
683 return a.outsideHostname
Philip Zeyligerd1402952025-04-23 03:54:37 +0000684}
685
Philip Zeyliger18532b22025-04-23 21:11:46 +0000686// OutsideWorkingDir returns the working directory on the outside system.
687func (a *Agent) OutsideWorkingDir() string {
688 return a.outsideWorkingDir
Philip Zeyligerd1402952025-04-23 03:54:37 +0000689}
690
691// GitOrigin returns the URL of the git remote 'origin' if it exists.
692func (a *Agent) GitOrigin() string {
693 return a.gitOrigin
694}
695
Josh Bleecher Snyder3e2111b2025-04-30 17:53:28 +0000696func (a *Agent) OpenBrowser(url string) {
697 if !a.IsInContainer() {
698 browser.Open(url)
699 return
700 }
701 // We're in Docker, need to send a request to the Git server
702 // to signal that the outer process should open the browser.
Josh Bleecher Snyder99570462025-05-05 10:26:14 -0700703 // We don't get to specify a URL, because we are untrusted.
Josh Bleecher Snyder3e2111b2025-04-30 17:53:28 +0000704 httpc := &http.Client{Timeout: 5 * time.Second}
Josh Bleecher Snyder99570462025-05-05 10:26:14 -0700705 resp, err := httpc.Post(a.outsideHTTP+"/browser", "text/plain", nil)
Josh Bleecher Snyder3e2111b2025-04-30 17:53:28 +0000706 if err != nil {
Josh Bleecher Snyder99570462025-05-05 10:26:14 -0700707 slog.Debug("browser launch request connection failed", "err", err)
Josh Bleecher Snyder3e2111b2025-04-30 17:53:28 +0000708 return
709 }
710 defer resp.Body.Close()
711 if resp.StatusCode == http.StatusOK {
712 return
713 }
714 body, _ := io.ReadAll(resp.Body)
715 slog.Debug("browser launch request execution failed", "status", resp.Status, "body", string(body))
716}
717
Sean McCullough96b60dd2025-04-30 09:49:10 -0700718// CurrentState returns the current state of the agent's state machine.
719func (a *Agent) CurrentState() State {
720 return a.stateMachine.CurrentState()
721}
722
Philip Zeyliger2c4db092025-04-28 16:57:50 -0700723func (a *Agent) IsInContainer() bool {
724 return a.config.InDocker
725}
726
727func (a *Agent) FirstMessageIndex() int {
728 a.mu.Lock()
729 defer a.mu.Unlock()
730 return a.firstMessageIndex
731}
732
Josh Bleecher Snyder19969a92025-06-05 14:34:02 -0700733// SetSlug sets a human-readable identifier for the conversation.
734func (a *Agent) SetSlug(slug string) {
Earl Lee2e463fb2025-04-17 11:22:22 -0700735 a.mu.Lock()
736 defer a.mu.Unlock()
Philip Zeyligerb7c58752025-05-01 10:10:17 -0700737
Josh Bleecher Snyder19969a92025-06-05 14:34:02 -0700738 a.gitState.SetSlug(slug)
Josh Bleecher Snyder31785ae2025-05-06 01:50:58 +0000739 convo, ok := a.convo.(*conversation.Convo)
740 if ok {
Josh Bleecher Snyder19969a92025-06-05 14:34:02 -0700741 convo.ExtraData["branch"] = a.BranchName()
Josh Bleecher Snyder31785ae2025-05-06 01:50:58 +0000742 }
Earl Lee2e463fb2025-04-17 11:22:22 -0700743}
744
Philip Zeyliger99a9a022025-04-27 15:15:25 +0000745// OnToolCall implements ant.Listener and tracks the start of a tool call.
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -0700746func (a *Agent) OnToolCall(ctx context.Context, convo *conversation.Convo, id string, toolName string, toolInput json.RawMessage, content llm.Content) {
Philip Zeyliger99a9a022025-04-27 15:15:25 +0000747 // Track the tool call
748 a.mu.Lock()
749 a.outstandingToolCalls[id] = toolName
750 a.mu.Unlock()
751}
752
Philip Zeyliger72252cb2025-05-10 17:00:08 -0700753// contentToString converts []llm.Content to a string, concatenating all text content and skipping non-text types.
754// If there's only one element in the array and it's a text type, it returns that text directly.
755// It also processes nested ToolResult arrays recursively.
756func contentToString(contents []llm.Content) string {
757 if len(contents) == 0 {
758 return ""
759 }
760
761 // If there's only one element and it's a text type, return it directly
762 if len(contents) == 1 && contents[0].Type == llm.ContentTypeText {
763 return contents[0].Text
764 }
765
766 // Otherwise, concatenate all text content
767 var result strings.Builder
768 for _, content := range contents {
769 if content.Type == llm.ContentTypeText {
770 result.WriteString(content.Text)
771 } else if content.Type == llm.ContentTypeToolResult && len(content.ToolResult) > 0 {
772 // Recursively process nested tool results
773 result.WriteString(contentToString(content.ToolResult))
774 }
775 }
776
777 return result.String()
778}
779
Earl Lee2e463fb2025-04-17 11:22:22 -0700780// OnToolResult implements ant.Listener.
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -0700781func (a *Agent) OnToolResult(ctx context.Context, convo *conversation.Convo, toolID string, toolName string, toolInput json.RawMessage, content llm.Content, result *string, err error) {
Philip Zeyliger99a9a022025-04-27 15:15:25 +0000782 // Remove the tool call from outstanding calls
783 a.mu.Lock()
784 delete(a.outstandingToolCalls, toolID)
785 a.mu.Unlock()
786
Earl Lee2e463fb2025-04-17 11:22:22 -0700787 m := AgentMessage{
788 Type: ToolUseMessageType,
789 Content: content.Text,
Philip Zeyliger72252cb2025-05-10 17:00:08 -0700790 ToolResult: contentToString(content.ToolResult),
Earl Lee2e463fb2025-04-17 11:22:22 -0700791 ToolError: content.ToolError,
792 ToolName: toolName,
793 ToolInput: string(toolInput),
794 ToolCallId: content.ToolUseID,
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -0700795 StartTime: content.ToolUseStartTime,
796 EndTime: content.ToolUseEndTime,
Earl Lee2e463fb2025-04-17 11:22:22 -0700797 }
798
799 // Calculate the elapsed time if both start and end times are set
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -0700800 if content.ToolUseStartTime != nil && content.ToolUseEndTime != nil {
801 elapsed := content.ToolUseEndTime.Sub(*content.ToolUseStartTime)
Earl Lee2e463fb2025-04-17 11:22:22 -0700802 m.Elapsed = &elapsed
803 }
804
Josh Bleecher Snyder50a1d622025-04-29 09:59:03 -0700805 m.SetConvo(convo)
Earl Lee2e463fb2025-04-17 11:22:22 -0700806 a.pushToOutbox(ctx, m)
807}
808
809// OnRequest implements ant.Listener.
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -0700810func (a *Agent) OnRequest(ctx context.Context, convo *conversation.Convo, id string, msg *llm.Message) {
Philip Zeyliger99a9a022025-04-27 15:15:25 +0000811 a.mu.Lock()
812 defer a.mu.Unlock()
813 a.outstandingLLMCalls[id] = struct{}{}
Earl Lee2e463fb2025-04-17 11:22:22 -0700814 // We already get tool results from the above. We send user messages to the outbox in the agent loop.
815}
816
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -0700817// OnResponse implements conversation.Listener. Responses contain messages from the LLM
Earl Lee2e463fb2025-04-17 11:22:22 -0700818// that need to be displayed (as well as tool calls that we send along when
819// they're done). (It would be reasonable to also mention tool calls when they're
820// started, but we don't do that yet.)
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -0700821func (a *Agent) OnResponse(ctx context.Context, convo *conversation.Convo, id string, resp *llm.Response) {
Philip Zeyliger99a9a022025-04-27 15:15:25 +0000822 // Remove the LLM call from outstanding calls
823 a.mu.Lock()
824 delete(a.outstandingLLMCalls, id)
825 a.mu.Unlock()
826
Josh Bleecher Snyder50a1d622025-04-29 09:59:03 -0700827 if resp == nil {
828 // LLM API call failed
829 m := AgentMessage{
830 Type: ErrorMessageType,
831 Content: "API call failed, type 'continue' to try again",
832 }
833 m.SetConvo(convo)
834 a.pushToOutbox(ctx, m)
835 return
836 }
837
Earl Lee2e463fb2025-04-17 11:22:22 -0700838 endOfTurn := false
Josh Bleecher Snyder4fcde4a2025-05-05 18:28:13 -0700839 if convo.Parent == nil { // subconvos never end the turn
840 switch resp.StopReason {
841 case llm.StopReasonToolUse:
842 // Check whether any of the tool calls are for tools that should end the turn
843 ToolSearch:
844 for _, part := range resp.Content {
845 if part.Type != llm.ContentTypeToolUse {
846 continue
847 }
Sean McCullough021557a2025-05-05 23:20:53 +0000848 // Find the tool by name
849 for _, tool := range convo.Tools {
Josh Bleecher Snyder4fcde4a2025-05-05 18:28:13 -0700850 if tool.Name == part.ToolName {
851 endOfTurn = tool.EndsTurn
852 break ToolSearch
Sean McCullough021557a2025-05-05 23:20:53 +0000853 }
854 }
Sean McCullough021557a2025-05-05 23:20:53 +0000855 }
Josh Bleecher Snyder4fcde4a2025-05-05 18:28:13 -0700856 default:
857 endOfTurn = true
Sean McCullough021557a2025-05-05 23:20:53 +0000858 }
Earl Lee2e463fb2025-04-17 11:22:22 -0700859 }
860 m := AgentMessage{
861 Type: AgentMessageType,
862 Content: collectTextContent(resp),
863 EndOfTurn: endOfTurn,
864 Usage: &resp.Usage,
865 StartTime: resp.StartTime,
866 EndTime: resp.EndTime,
867 }
868
869 // Extract any tool calls from the response
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -0700870 if resp.StopReason == llm.StopReasonToolUse {
Earl Lee2e463fb2025-04-17 11:22:22 -0700871 var toolCalls []ToolCall
872 for _, part := range resp.Content {
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -0700873 if part.Type == llm.ContentTypeToolUse {
Earl Lee2e463fb2025-04-17 11:22:22 -0700874 toolCalls = append(toolCalls, ToolCall{
875 Name: part.ToolName,
876 Input: string(part.ToolInput),
877 ToolCallId: part.ID,
878 })
879 }
880 }
881 m.ToolCalls = toolCalls
882 }
883
884 // Calculate the elapsed time if both start and end times are set
885 if resp.StartTime != nil && resp.EndTime != nil {
886 elapsed := resp.EndTime.Sub(*resp.StartTime)
887 m.Elapsed = &elapsed
888 }
889
Josh Bleecher Snyder50a1d622025-04-29 09:59:03 -0700890 m.SetConvo(convo)
Earl Lee2e463fb2025-04-17 11:22:22 -0700891 a.pushToOutbox(ctx, m)
892}
893
894// WorkingDir implements CodingAgent.
895func (a *Agent) WorkingDir() string {
896 return a.workingDir
897}
898
Josh Bleecher Snyderc5848f32025-05-28 18:50:58 +0000899// RepoRoot returns the git repository root directory.
900func (a *Agent) RepoRoot() string {
901 return a.repoRoot
902}
903
Earl Lee2e463fb2025-04-17 11:22:22 -0700904// MessageCount implements CodingAgent.
905func (a *Agent) MessageCount() int {
906 a.mu.Lock()
907 defer a.mu.Unlock()
908 return len(a.history)
909}
910
911// Messages implements CodingAgent.
912func (a *Agent) Messages(start int, end int) []AgentMessage {
913 a.mu.Lock()
914 defer a.mu.Unlock()
915 return slices.Clone(a.history[start:end])
916}
917
Philip Zeyligerb8a8f352025-06-02 07:39:37 -0700918// ShouldCompact checks if the conversation should be compacted based on token usage
919func (a *Agent) ShouldCompact() bool {
920 // Get the threshold from environment variable, default to 0.94 (94%)
921 // (Because default Claude output is 8192 tokens, which is 4% of 200,000 tokens,
922 // and a little bit of buffer.)
923 thresholdRatio := 0.94
924 if envThreshold := os.Getenv("SKETCH_COMPACT_THRESHOLD_RATIO"); envThreshold != "" {
925 if parsed, err := strconv.ParseFloat(envThreshold, 64); err == nil && parsed > 0 && parsed <= 1.0 {
926 thresholdRatio = parsed
927 }
928 }
929
930 // Get the most recent usage to check current context size
931 lastUsage := a.convo.LastUsage()
932
933 if lastUsage.InputTokens == 0 {
934 // No API calls made yet
935 return false
936 }
937
938 // Calculate the current context size from the last API call
939 // This includes all tokens that were part of the input context:
940 // - Input tokens (user messages, system prompt, conversation history)
941 // - Cache read tokens (cached parts of the context)
942 // - Cache creation tokens (new parts being cached)
943 currentContextSize := lastUsage.InputTokens + lastUsage.CacheReadInputTokens + lastUsage.CacheCreationInputTokens
944
945 // Get the service's token context window
946 service := a.config.Service
947 contextWindow := service.TokenContextWindow()
948
949 // Calculate threshold
950 threshold := uint64(float64(contextWindow) * thresholdRatio)
951
952 // Check if we've exceeded the threshold
953 return currentContextSize >= threshold
954}
955
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -0700956func (a *Agent) OriginalBudget() conversation.Budget {
Earl Lee2e463fb2025-04-17 11:22:22 -0700957 return a.originalBudget
958}
959
Josh Bleecher Snyder664404e2025-06-04 21:56:42 +0000960// Upstream returns the upstream branch for git work
961func (a *Agent) Upstream() string {
962 return a.gitState.Upstream()
963}
964
Earl Lee2e463fb2025-04-17 11:22:22 -0700965// AgentConfig contains configuration for creating a new Agent.
966type AgentConfig struct {
Josh Bleecher Snyderb421a242025-05-29 23:22:55 +0000967 Context context.Context
968 Service llm.Service
969 Budget conversation.Budget
970 GitUsername string
971 GitEmail string
972 SessionID string
973 ClientGOOS string
974 ClientGOARCH string
975 InDocker bool
976 OneShot bool
977 WorkingDir string
Philip Zeyliger18532b22025-04-23 21:11:46 +0000978 // Outside information
979 OutsideHostname string
980 OutsideOS string
981 OutsideWorkingDir string
Philip Zeyligerbc8c8dc2025-05-21 13:19:13 -0700982
983 // Outtie's HTTP to, e.g., open a browser
984 OutsideHTTP string
985 // Outtie's Git server
986 GitRemoteAddr string
Josh Bleecher Snyder664404e2025-06-04 21:56:42 +0000987 // Upstream branch for git work
988 Upstream string
Philip Zeyligerbc8c8dc2025-05-21 13:19:13 -0700989 // Commit to checkout from Outtie
990 Commit string
Philip Zeyligerbe7802a2025-06-04 20:15:25 +0000991 // Prefix for git branches created by sketch
992 BranchPrefix string
Philip Zeyligerc17ffe32025-06-05 19:49:13 -0700993 // Skaband client for session history (optional)
994 SkabandClient *skabandclient.SkabandClient
Earl Lee2e463fb2025-04-17 11:22:22 -0700995}
996
997// NewAgent creates a new Agent.
998// It is not usable until Init() is called.
999func NewAgent(config AgentConfig) *Agent {
Philip Zeyligerbe7802a2025-06-04 20:15:25 +00001000 // Set default branch prefix if not specified
1001 if config.BranchPrefix == "" {
1002 config.BranchPrefix = "sketch/"
1003 }
1004
Earl Lee2e463fb2025-04-17 11:22:22 -07001005 agent := &Agent{
Philip Zeyligerf2872992025-05-22 10:35:28 -07001006 config: config,
1007 ready: make(chan struct{}),
1008 inbox: make(chan string, 100),
1009 subscribers: make([]chan *AgentMessage, 0),
1010 startedAt: time.Now(),
1011 originalBudget: config.Budget,
1012 gitState: AgentGitState{
1013 seenCommits: make(map[string]bool),
1014 gitRemoteAddr: config.GitRemoteAddr,
Josh Bleecher Snyder664404e2025-06-04 21:56:42 +00001015 upstream: config.Upstream,
Philip Zeyligerf2872992025-05-22 10:35:28 -07001016 },
Philip Zeyliger99a9a022025-04-27 15:15:25 +00001017 outsideHostname: config.OutsideHostname,
1018 outsideOS: config.OutsideOS,
1019 outsideWorkingDir: config.OutsideWorkingDir,
1020 outstandingLLMCalls: make(map[string]struct{}),
1021 outstandingToolCalls: make(map[string]string),
Sean McCullough96b60dd2025-04-30 09:49:10 -07001022 stateMachine: NewStateMachine(),
Philip Zeyligerbc8c8dc2025-05-21 13:19:13 -07001023 workingDir: config.WorkingDir,
1024 outsideHTTP: config.OutsideHTTP,
Sean McCullough364f7412025-06-02 00:55:44 +00001025 portMonitor: NewPortMonitor(),
Earl Lee2e463fb2025-04-17 11:22:22 -07001026 }
1027 return agent
1028}
1029
1030type AgentInit struct {
Philip Zeyligerbc8c8dc2025-05-21 13:19:13 -07001031 NoGit bool // only for testing
Earl Lee2e463fb2025-04-17 11:22:22 -07001032
Philip Zeyligerbc8c8dc2025-05-21 13:19:13 -07001033 InDocker bool
1034 HostAddr string
Earl Lee2e463fb2025-04-17 11:22:22 -07001035}
1036
1037func (a *Agent) Init(ini AgentInit) error {
Josh Bleecher Snyder9c07e1d2025-04-28 19:25:37 -07001038 if a.convo != nil {
1039 return fmt.Errorf("Agent.Init: already initialized")
1040 }
Earl Lee2e463fb2025-04-17 11:22:22 -07001041 ctx := a.config.Context
Philip Zeyliger716bfee2025-05-21 18:32:31 -07001042 slog.InfoContext(ctx, "agent initializing")
Philip Zeyligerbc8c8dc2025-05-21 13:19:13 -07001043
Philip Zeyliger2f0eb692025-06-04 09:53:42 -07001044 if !ini.NoGit {
1045 // Capture the original origin before we potentially replace it below
1046 a.gitOrigin = getGitOrigin(ctx, a.workingDir)
1047 }
1048
Philip Zeyliger222bf412025-06-04 16:42:58 +00001049 // If a remote git addr was specified, we configure the origin remote
Philip Zeyligerf2872992025-05-22 10:35:28 -07001050 if a.gitState.gitRemoteAddr != "" {
1051 slog.InfoContext(ctx, "Configuring git remote", slog.String("remote", a.gitState.gitRemoteAddr))
Philip Zeyliger222bf412025-06-04 16:42:58 +00001052
1053 // Remove existing origin remote if it exists
1054 cmd := exec.CommandContext(ctx, "git", "remote", "remove", "origin")
Philip Zeyligerf2872992025-05-22 10:35:28 -07001055 cmd.Dir = a.workingDir
1056 if out, err := cmd.CombinedOutput(); err != nil {
Philip Zeyliger222bf412025-06-04 16:42:58 +00001057 // Ignore error if origin doesn't exist
1058 slog.DebugContext(ctx, "git remote remove origin (ignoring if not exists)", slog.String("output", string(out)))
Philip Zeyligerf2872992025-05-22 10:35:28 -07001059 }
Philip Zeyliger222bf412025-06-04 16:42:58 +00001060
1061 // Add the new remote as origin
1062 cmd = exec.CommandContext(ctx, "git", "remote", "add", "origin", a.gitState.gitRemoteAddr)
Philip Zeyligerf2872992025-05-22 10:35:28 -07001063 cmd.Dir = a.workingDir
1064 if out, err := cmd.CombinedOutput(); err != nil {
Philip Zeyliger222bf412025-06-04 16:42:58 +00001065 return fmt.Errorf("git remote add origin: %s: %v", out, err)
Philip Zeyligerf2872992025-05-22 10:35:28 -07001066 }
Philip Zeyliger222bf412025-06-04 16:42:58 +00001067
Philip Zeyligerf2872992025-05-22 10:35:28 -07001068 }
1069
1070 // If a commit was specified, we fetch and reset to it.
1071 if a.config.Commit != "" && a.gitState.gitRemoteAddr != "" {
Philip Zeyliger716bfee2025-05-21 18:32:31 -07001072 slog.InfoContext(ctx, "updating git repo", slog.String("commit", a.config.Commit))
1073
Earl Lee2e463fb2025-04-17 11:22:22 -07001074 cmd := exec.CommandContext(ctx, "git", "stash")
Philip Zeyligerbc8c8dc2025-05-21 13:19:13 -07001075 cmd.Dir = a.workingDir
Earl Lee2e463fb2025-04-17 11:22:22 -07001076 if out, err := cmd.CombinedOutput(); err != nil {
1077 return fmt.Errorf("git stash: %s: %v", out, err)
1078 }
Philip Zeyliger222bf412025-06-04 16:42:58 +00001079 cmd = exec.CommandContext(ctx, "git", "fetch", "--prune", "origin")
Philip Zeyligerbc8c8dc2025-05-21 13:19:13 -07001080 cmd.Dir = a.workingDir
Earl Lee2e463fb2025-04-17 11:22:22 -07001081 if out, err := cmd.CombinedOutput(); err != nil {
1082 return fmt.Errorf("git fetch: %s: %w", out, err)
1083 }
Philip Zeyligerbc8c8dc2025-05-21 13:19:13 -07001084 cmd = exec.CommandContext(ctx, "git", "checkout", "-f", a.config.Commit)
1085 cmd.Dir = a.workingDir
Pokey Rule7a113622025-05-12 10:58:45 +01001086 if checkoutOut, err := cmd.CombinedOutput(); err != nil {
1087 // Remove git hooks if they exist and retry
1088 // Only try removing hooks if we haven't already removed them during fetch
Philip Zeyligerbc8c8dc2025-05-21 13:19:13 -07001089 hookPath := filepath.Join(a.workingDir, ".git", "hooks")
Pokey Rule7a113622025-05-12 10:58:45 +01001090 if _, statErr := os.Stat(hookPath); statErr == nil {
1091 slog.WarnContext(ctx, "git checkout failed, removing hooks and retrying",
1092 slog.String("error", err.Error()),
1093 slog.String("output", string(checkoutOut)))
Philip Zeyligerbc8c8dc2025-05-21 13:19:13 -07001094 if removeErr := removeGitHooks(ctx, a.workingDir); removeErr != nil {
Pokey Rule7a113622025-05-12 10:58:45 +01001095 slog.WarnContext(ctx, "failed to remove git hooks", slog.String("error", removeErr.Error()))
1096 }
1097
1098 // Retry the checkout operation
Philip Zeyligerbc8c8dc2025-05-21 13:19:13 -07001099 cmd = exec.CommandContext(ctx, "git", "checkout", "-f", a.config.Commit)
1100 cmd.Dir = a.workingDir
Pokey Rule7a113622025-05-12 10:58:45 +01001101 if retryOut, retryErr := cmd.CombinedOutput(); retryErr != nil {
Philip Zeyligerbc8c8dc2025-05-21 13:19:13 -07001102 return fmt.Errorf("git checkout %s failed even after removing hooks: %s: %w", a.config.Commit, retryOut, retryErr)
Pokey Rule7a113622025-05-12 10:58:45 +01001103 }
1104 } else {
Philip Zeyligerbc8c8dc2025-05-21 13:19:13 -07001105 return fmt.Errorf("git checkout %s: %s: %w", a.config.Commit, checkoutOut, err)
Pokey Rule7a113622025-05-12 10:58:45 +01001106 }
Earl Lee2e463fb2025-04-17 11:22:22 -07001107 }
Earl Lee2e463fb2025-04-17 11:22:22 -07001108 }
Philip Zeyligerbc8c8dc2025-05-21 13:19:13 -07001109
1110 if ini.HostAddr != "" {
1111 a.url = "http://" + ini.HostAddr
1112 }
Earl Lee2e463fb2025-04-17 11:22:22 -07001113
1114 if !ini.NoGit {
1115 repoRoot, err := repoRoot(ctx, a.workingDir)
1116 if err != nil {
1117 return fmt.Errorf("repoRoot: %w", err)
1118 }
1119 a.repoRoot = repoRoot
1120
Earl Lee2e463fb2025-04-17 11:22:22 -07001121 if err != nil {
1122 return fmt.Errorf("resolveRef: %w", err)
1123 }
Philip Zeyliger49edc922025-05-14 09:45:45 -07001124
Josh Bleecher Snyderfea9e272025-06-02 21:21:59 +00001125 if a.IsInContainer() {
Philip Zeyligerf75ba2c2025-06-02 17:02:51 -07001126 if err := setupGitHooks(a.repoRoot); err != nil {
1127 slog.WarnContext(ctx, "failed to set up git hooks", "err", err)
1128 }
Philip Zeyligerbc8c8dc2025-05-21 13:19:13 -07001129 }
1130
Philip Zeyliger49edc922025-05-14 09:45:45 -07001131 cmd := exec.CommandContext(ctx, "git", "tag", "-f", a.SketchGitBaseRef(), "HEAD")
1132 cmd.Dir = repoRoot
1133 if out, err := cmd.CombinedOutput(); err != nil {
1134 return fmt.Errorf("git tag -f %s %s: %s: %w", a.SketchGitBaseRef(), "HEAD", out, err)
1135 }
Earl Lee2e463fb2025-04-17 11:22:22 -07001136
Josh Bleecher Snyder0e5b8c62025-05-14 20:58:20 +00001137 slog.Info("running codebase analysis")
1138 codebase, err := onstart.AnalyzeCodebase(ctx, a.repoRoot)
1139 if err != nil {
1140 slog.Warn("failed to analyze codebase", "error", err)
Josh Bleecher Snydera997be62025-05-07 22:52:46 +00001141 }
Josh Bleecher Snyder0e5b8c62025-05-14 20:58:20 +00001142 a.codebase = codebase
Josh Bleecher Snydera997be62025-05-07 22:52:46 +00001143
Josh Bleecher Snyder9daa5182025-05-16 18:34:00 +00001144 codereview, err := codereview.NewCodeReviewer(ctx, a.repoRoot, a.SketchGitBaseRef())
Earl Lee2e463fb2025-04-17 11:22:22 -07001145 if err != nil {
Josh Bleecher Snyderf4047bb2025-05-05 23:02:56 +00001146 return fmt.Errorf("Agent.Init: codereview.NewCodeReviewer: %w", err)
Earl Lee2e463fb2025-04-17 11:22:22 -07001147 }
1148 a.codereview = codereview
Philip Zeyligerd1402952025-04-23 03:54:37 +00001149
Earl Lee2e463fb2025-04-17 11:22:22 -07001150 }
Philip Zeyligerf2872992025-05-22 10:35:28 -07001151 a.gitState.lastHEAD = a.SketchGitBase()
Earl Lee2e463fb2025-04-17 11:22:22 -07001152 a.convo = a.initConvo()
1153 close(a.ready)
1154 return nil
1155}
1156
Josh Bleecher Snyderdbe02302025-04-29 16:44:23 -07001157//go:embed agent_system_prompt.txt
1158var agentSystemPrompt string
1159
Earl Lee2e463fb2025-04-17 11:22:22 -07001160// initConvo initializes the conversation.
1161// It must not be called until all agent fields are initialized,
1162// particularly workingDir and git.
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -07001163func (a *Agent) initConvo() *conversation.Convo {
Earl Lee2e463fb2025-04-17 11:22:22 -07001164 ctx := a.config.Context
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -07001165 convo := conversation.New(ctx, a.config.Service)
Earl Lee2e463fb2025-04-17 11:22:22 -07001166 convo.PromptCaching = true
1167 convo.Budget = a.config.Budget
Josh Bleecher Snyder5cca56f2025-05-06 01:10:16 +00001168 convo.SystemPrompt = a.renderSystemPrompt()
Josh Bleecher Snyder31785ae2025-05-06 01:50:58 +00001169 convo.ExtraData = map[string]any{"session_id": a.config.SessionID}
Earl Lee2e463fb2025-04-17 11:22:22 -07001170
Josh Bleecher Snyderd499fd62025-04-30 01:31:29 +00001171 // Define a permission callback for the bash tool to check if the branch name is set before allowing git commits
1172 bashPermissionCheck := func(command string) error {
Josh Bleecher Snyder19969a92025-06-05 14:34:02 -07001173 if a.gitState.Slug() != "" {
1174 return nil // branch is set up
Josh Bleecher Snyderd499fd62025-04-30 01:31:29 +00001175 }
Josh Bleecher Snyderd499fd62025-04-30 01:31:29 +00001176 willCommit, err := bashkit.WillRunGitCommit(command)
1177 if err != nil {
Josh Bleecher Snyder19969a92025-06-05 14:34:02 -07001178 return nil // fail open
Josh Bleecher Snyderd499fd62025-04-30 01:31:29 +00001179 }
Josh Bleecher Snyderd499fd62025-04-30 01:31:29 +00001180 if willCommit {
Josh Bleecher Snyder19969a92025-06-05 14:34:02 -07001181 return fmt.Errorf("you must use the set-slug tool before making git commits")
Josh Bleecher Snyderd499fd62025-04-30 01:31:29 +00001182 }
Josh Bleecher Snyderd499fd62025-04-30 01:31:29 +00001183 return nil
1184 }
1185
Josh Bleecher Snyder495c1fa2025-05-29 00:37:22 +00001186 bashTool := claudetool.NewBashTool(bashPermissionCheck, claudetool.EnableBashToolJITInstall)
Josh Bleecher Snyderd499fd62025-04-30 01:31:29 +00001187
Earl Lee2e463fb2025-04-17 11:22:22 -07001188 // Register all tools with the conversation
1189 // When adding, removing, or modifying tools here, double-check that the termui tool display
1190 // template in termui/termui.go has pretty-printing support for all tools.
Philip Zeyliger33d282f2025-05-03 04:01:54 +00001191
1192 var browserTools []*llm.Tool
Philip Zeyliger80b488d2025-05-10 18:21:54 -07001193 _, supportsScreenshots := a.config.Service.(*ant.Service)
1194 var bTools []*llm.Tool
1195 var browserCleanup func()
1196
1197 bTools, browserCleanup = browse.RegisterBrowserTools(a.config.Context, supportsScreenshots)
1198 // Add cleanup function to context cancel
1199 go func() {
1200 <-a.config.Context.Done()
1201 browserCleanup()
1202 }()
1203 browserTools = bTools
Philip Zeyliger33d282f2025-05-03 04:01:54 +00001204
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -07001205 convo.Tools = []*llm.Tool{
Josh Bleecher Snyderb421a242025-05-29 23:22:55 +00001206 bashTool, claudetool.Keyword, claudetool.Patch,
Josh Bleecher Snyder19969a92025-06-05 14:34:02 -07001207 claudetool.Think, claudetool.TodoRead, claudetool.TodoWrite, a.setSlugTool(), a.commitMessageStyleTool(), makeDoneTool(a.codereview),
Josh Bleecher Snydera4092d22025-05-14 18:32:53 -07001208 a.codereview.Tool(), claudetool.AboutSketch,
Josh Bleecher Snyder31785ae2025-05-06 01:50:58 +00001209 }
1210
Josh Bleecher Snyderb529e732025-05-07 22:06:46 +00001211 // One-shot mode is non-interactive, multiple choice requires human response
1212 if !a.config.OneShot {
Josh Bleecher Snydera5c971e2025-05-14 10:49:08 -07001213 convo.Tools = append(convo.Tools, multipleChoiceTool)
Earl Lee2e463fb2025-04-17 11:22:22 -07001214 }
Philip Zeyliger33d282f2025-05-03 04:01:54 +00001215
1216 convo.Tools = append(convo.Tools, browserTools...)
Philip Zeyligerc17ffe32025-06-05 19:49:13 -07001217
1218 // Add session history tools if skaband client is available
1219 if a.config.SkabandClient != nil {
1220 sessionHistoryTools := claudetool.CreateSessionHistoryTools(a.config.SkabandClient, a.config.SessionID, a.gitOrigin)
1221 convo.Tools = append(convo.Tools, sessionHistoryTools...)
1222 }
1223
Earl Lee2e463fb2025-04-17 11:22:22 -07001224 convo.Listener = a
1225 return convo
1226}
1227
Josh Bleecher Snydera5c971e2025-05-14 10:49:08 -07001228var multipleChoiceTool = &llm.Tool{
1229 Name: "multiplechoice",
1230 Description: "Present the user with an quick way to answer to your question using one of a short list of possible answers you would expect from the user.",
1231 EndsTurn: true,
1232 InputSchema: json.RawMessage(`{
Sean McCullough485afc62025-04-28 14:28:39 -07001233 "type": "object",
1234 "description": "The question and a list of answers you would expect the user to choose from.",
1235 "properties": {
1236 "question": {
1237 "type": "string",
1238 "description": "The text of the multiple-choice question you would like the user, e.g. 'What kinds of test cases would you like me to add?'"
1239 },
1240 "responseOptions": {
1241 "type": "array",
1242 "description": "The set of possible answers to let the user quickly choose from, e.g. ['Basic unit test coverage', 'Error return values', 'Malformed input'].",
1243 "items": {
1244 "type": "object",
1245 "properties": {
1246 "caption": {
1247 "type": "string",
1248 "description": "The caption, e.g. 'Basic coverage', 'Error return values', or 'Malformed input' for the response button. Do NOT include options for responses that would end the conversation like 'Ok', 'No thank you', 'This looks good'"
1249 },
1250 "responseText": {
1251 "type": "string",
1252 "description": "The full text of the response, e.g. 'Add unit tests for basic test coverage', 'Add unit tests for error return values', or 'Add unit tests for malformed input'"
1253 }
1254 },
1255 "required": ["caption", "responseText"]
1256 }
1257 }
1258 },
1259 "required": ["question", "responseOptions"]
1260}`),
Josh Bleecher Snydera5c971e2025-05-14 10:49:08 -07001261 Run: func(ctx context.Context, input json.RawMessage) ([]llm.Content, error) {
1262 // The Run logic for "multiplechoice" tool is a no-op on the server.
1263 // The UI will present a list of options for the user to select from,
1264 // and that's it as far as "executing" the tool_use goes.
1265 // When the user *does* select one of the presented options, that
1266 // responseText gets sent as a chat message on behalf of the user.
1267 return llm.TextContent("end your turn and wait for the user to respond"), nil
1268 },
Sean McCullough485afc62025-04-28 14:28:39 -07001269}
1270
1271type MultipleChoiceOption struct {
1272 Caption string `json:"caption"`
1273 ResponseText string `json:"responseText"`
1274}
1275
1276type MultipleChoiceParams struct {
1277 Question string `json:"question"`
1278 ResponseOptions []MultipleChoiceOption `json:"responseOptions"`
1279}
1280
Josh Bleecher Snyderfff269b2025-04-30 01:49:39 +00001281// branchExists reports whether branchName exists, either locally or in well-known remotes.
1282func branchExists(dir, branchName string) bool {
1283 refs := []string{
1284 "refs/heads/",
1285 "refs/remotes/origin/",
Josh Bleecher Snyderfff269b2025-04-30 01:49:39 +00001286 }
1287 for _, ref := range refs {
1288 cmd := exec.Command("git", "show-ref", "--verify", "--quiet", ref+branchName)
1289 cmd.Dir = dir
1290 if cmd.Run() == nil { // exit code 0 means branch exists
1291 return true
1292 }
1293 }
1294 return false
1295}
1296
Josh Bleecher Snyder19969a92025-06-05 14:34:02 -07001297func (a *Agent) setSlugTool() *llm.Tool {
1298 return &llm.Tool{
1299 Name: "set-slug",
1300 Description: `Set a short slug as an identifier for this conversation.`,
Earl Lee2e463fb2025-04-17 11:22:22 -07001301 InputSchema: json.RawMessage(`{
1302 "type": "object",
1303 "properties": {
Josh Bleecher Snyder19969a92025-06-05 14:34:02 -07001304 "slug": {
Earl Lee2e463fb2025-04-17 11:22:22 -07001305 "type": "string",
Josh Bleecher Snyder19969a92025-06-05 14:34:02 -07001306 "description": "A 2-3 word alphanumeric hyphenated slug, imperative tense"
Earl Lee2e463fb2025-04-17 11:22:22 -07001307 }
1308 },
Josh Bleecher Snyder19969a92025-06-05 14:34:02 -07001309 "required": ["slug"]
Earl Lee2e463fb2025-04-17 11:22:22 -07001310}`),
Philip Zeyliger72252cb2025-05-10 17:00:08 -07001311 Run: func(ctx context.Context, input json.RawMessage) ([]llm.Content, error) {
Earl Lee2e463fb2025-04-17 11:22:22 -07001312 var params struct {
Josh Bleecher Snyder19969a92025-06-05 14:34:02 -07001313 Slug string `json:"slug"`
Earl Lee2e463fb2025-04-17 11:22:22 -07001314 }
1315 if err := json.Unmarshal(input, &params); err != nil {
Philip Zeyliger72252cb2025-05-10 17:00:08 -07001316 return nil, err
Earl Lee2e463fb2025-04-17 11:22:22 -07001317 }
Josh Bleecher Snyder19969a92025-06-05 14:34:02 -07001318 // Prevent slug changes if there have been git changes
1319 // This lets the agent change its mind about a good slug,
1320 // while ensuring that once a branch has been pushed, it remains stable.
1321 if s := a.Slug(); s != "" && s != params.Slug && a.gitState.HasSeenCommits() {
1322 return nil, fmt.Errorf("slug already set to %q", s)
Josh Bleecher Snydera9b38222025-04-29 18:05:06 -07001323 }
Josh Bleecher Snyder19969a92025-06-05 14:34:02 -07001324 if params.Slug == "" {
1325 return nil, fmt.Errorf("slug parameter cannot be empty")
Josh Bleecher Snydera9b38222025-04-29 18:05:06 -07001326 }
Josh Bleecher Snyder19969a92025-06-05 14:34:02 -07001327 slug := cleanSlugName(params.Slug)
1328 if slug == "" {
1329 return nil, fmt.Errorf("slug parameter could not be converted to a valid slug")
1330 }
1331 a.SetSlug(slug)
1332 // TODO: do this by a call to outie, rather than semi-guessing from innie
1333 if branchExists(a.workingDir, a.BranchName()) {
1334 return nil, fmt.Errorf("slug %q already exists; please choose a different slug", slug)
1335 }
1336 return llm.TextContent("OK"), nil
Josh Bleecher Snydera2a31502025-05-07 12:37:18 +00001337 },
1338 }
Josh Bleecher Snydera2a31502025-05-07 12:37:18 +00001339}
1340
Josh Bleecher Snyder19969a92025-06-05 14:34:02 -07001341func (a *Agent) commitMessageStyleTool() *llm.Tool {
1342 description := `Provides git commit message style guidance. MANDATORY: You must use this tool before making any git commits.`
Josh Bleecher Snydera2a31502025-05-07 12:37:18 +00001343 preCommit := &llm.Tool{
Josh Bleecher Snyder19969a92025-06-05 14:34:02 -07001344 Name: "commit-message-style",
Josh Bleecher Snydera2a31502025-05-07 12:37:18 +00001345 Description: description,
Josh Bleecher Snyder19969a92025-06-05 14:34:02 -07001346 InputSchema: llm.EmptySchema(),
Philip Zeyliger72252cb2025-05-10 17:00:08 -07001347 Run: func(ctx context.Context, input json.RawMessage) ([]llm.Content, error) {
Josh Bleecher Snyder6aaf6af2025-05-07 20:47:13 +00001348 styleHint, err := claudetool.CommitMessageStyleHint(ctx, a.repoRoot)
1349 if err != nil {
1350 slog.DebugContext(ctx, "failed to get commit message style hint", "err", err)
1351 }
Josh Bleecher Snyder19969a92025-06-05 14:34:02 -07001352 return llm.TextContent(styleHint), nil
Earl Lee2e463fb2025-04-17 11:22:22 -07001353 },
1354 }
Josh Bleecher Snyderd7970e62025-05-01 01:56:28 +00001355 return preCommit
Earl Lee2e463fb2025-04-17 11:22:22 -07001356}
1357
1358func (a *Agent) Ready() <-chan struct{} {
1359 return a.ready
1360}
1361
Philip Zeyligerbe7802a2025-06-04 20:15:25 +00001362// BranchPrefix returns the configured branch prefix
1363func (a *Agent) BranchPrefix() string {
1364 return a.config.BranchPrefix
1365}
1366
Earl Lee2e463fb2025-04-17 11:22:22 -07001367func (a *Agent) UserMessage(ctx context.Context, msg string) {
1368 a.pushToOutbox(ctx, AgentMessage{Type: UserMessageType, Content: msg})
1369 a.inbox <- msg
1370}
1371
Earl Lee2e463fb2025-04-17 11:22:22 -07001372func (a *Agent) CancelToolUse(toolUseID string, cause error) error {
1373 return a.convo.CancelToolUse(toolUseID, cause)
1374}
1375
Sean McCulloughedc88dc2025-04-30 02:55:01 +00001376func (a *Agent) CancelTurn(cause error) {
1377 a.cancelTurnMu.Lock()
1378 defer a.cancelTurnMu.Unlock()
1379 if a.cancelTurn != nil {
Sean McCullough96b60dd2025-04-30 09:49:10 -07001380 // Force state transition to cancelled state
1381 ctx := a.config.Context
1382 a.stateMachine.ForceTransition(ctx, StateCancelled, "User cancelled turn: "+cause.Error())
Sean McCulloughedc88dc2025-04-30 02:55:01 +00001383 a.cancelTurn(cause)
Earl Lee2e463fb2025-04-17 11:22:22 -07001384 }
1385}
1386
1387func (a *Agent) Loop(ctxOuter context.Context) {
Sean McCullough364f7412025-06-02 00:55:44 +00001388 // Start port monitoring when the agent loop begins
1389 // Only monitor ports when running in a container
1390 if a.IsInContainer() {
1391 a.portMonitor.Start(ctxOuter)
1392 }
1393
Earl Lee2e463fb2025-04-17 11:22:22 -07001394 for {
1395 select {
1396 case <-ctxOuter.Done():
1397 return
1398 default:
1399 ctxInner, cancel := context.WithCancelCause(ctxOuter)
Sean McCulloughedc88dc2025-04-30 02:55:01 +00001400 a.cancelTurnMu.Lock()
1401 // Set .cancelTurn so the user can cancel whatever is happening
Sean McCullough885a16a2025-04-30 02:49:25 +00001402 // inside the conversation loop without canceling this outer Loop execution.
Sean McCulloughedc88dc2025-04-30 02:55:01 +00001403 // This cancelTurn func is intended be called from other goroutines,
Earl Lee2e463fb2025-04-17 11:22:22 -07001404 // hence the mutex.
Sean McCulloughedc88dc2025-04-30 02:55:01 +00001405 a.cancelTurn = cancel
1406 a.cancelTurnMu.Unlock()
Sean McCullough9f4b8082025-04-30 17:34:07 +00001407 err := a.processTurn(ctxInner) // Renamed from InnerLoop to better reflect its purpose
1408 if err != nil {
1409 slog.ErrorContext(ctxOuter, "Error in processing turn", "error", err)
1410 }
Earl Lee2e463fb2025-04-17 11:22:22 -07001411 cancel(nil)
1412 }
1413 }
1414}
1415
1416func (a *Agent) pushToOutbox(ctx context.Context, m AgentMessage) {
1417 if m.Timestamp.IsZero() {
1418 m.Timestamp = time.Now()
1419 }
1420
Philip Zeyliger72252cb2025-05-10 17:00:08 -07001421 // If this is a ToolUseMessage and ToolResult is set but Content is not, copy the ToolResult to Content
1422 if m.Type == ToolUseMessageType && m.ToolResult != "" && m.Content == "" {
1423 m.Content = m.ToolResult
1424 }
1425
Earl Lee2e463fb2025-04-17 11:22:22 -07001426 // If this is an end-of-turn message, calculate the turn duration and add it to the message
1427 if m.EndOfTurn && m.Type == AgentMessageType {
1428 turnDuration := time.Since(a.startOfTurn)
1429 m.TurnDuration = &turnDuration
1430 slog.InfoContext(ctx, "Turn completed", "turnDuration", turnDuration)
1431 }
1432
Earl Lee2e463fb2025-04-17 11:22:22 -07001433 a.mu.Lock()
1434 defer a.mu.Unlock()
1435 m.Idx = len(a.history)
Philip Zeyligerb7c58752025-05-01 10:10:17 -07001436 slog.InfoContext(ctx, "agent message", m.Attr())
Earl Lee2e463fb2025-04-17 11:22:22 -07001437 a.history = append(a.history, m)
Earl Lee2e463fb2025-04-17 11:22:22 -07001438
Philip Zeyligerb7c58752025-05-01 10:10:17 -07001439 // Notify all subscribers
1440 for _, ch := range a.subscribers {
1441 ch <- &m
Earl Lee2e463fb2025-04-17 11:22:22 -07001442 }
Earl Lee2e463fb2025-04-17 11:22:22 -07001443}
1444
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -07001445func (a *Agent) GatherMessages(ctx context.Context, block bool) ([]llm.Content, error) {
1446 var m []llm.Content
Earl Lee2e463fb2025-04-17 11:22:22 -07001447 if block {
1448 select {
1449 case <-ctx.Done():
1450 return m, ctx.Err()
1451 case msg := <-a.inbox:
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -07001452 m = append(m, llm.StringContent(msg))
Earl Lee2e463fb2025-04-17 11:22:22 -07001453 }
1454 }
1455 for {
1456 select {
1457 case msg := <-a.inbox:
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -07001458 m = append(m, llm.StringContent(msg))
Earl Lee2e463fb2025-04-17 11:22:22 -07001459 default:
1460 return m, nil
1461 }
1462 }
1463}
1464
Sean McCullough885a16a2025-04-30 02:49:25 +00001465// processTurn handles a single conversation turn with the user
Sean McCullough9f4b8082025-04-30 17:34:07 +00001466func (a *Agent) processTurn(ctx context.Context) error {
Earl Lee2e463fb2025-04-17 11:22:22 -07001467 // Reset the start of turn time
1468 a.startOfTurn = time.Now()
1469
Sean McCullough96b60dd2025-04-30 09:49:10 -07001470 // Transition to waiting for user input state
1471 a.stateMachine.Transition(ctx, StateWaitingForUserInput, "Starting turn")
1472
Sean McCullough885a16a2025-04-30 02:49:25 +00001473 // Process initial user message
1474 initialResp, err := a.processUserMessage(ctx)
1475 if err != nil {
Sean McCullough96b60dd2025-04-30 09:49:10 -07001476 a.stateMachine.Transition(ctx, StateError, "Error processing user message: "+err.Error())
Sean McCullough9f4b8082025-04-30 17:34:07 +00001477 return err
1478 }
1479
1480 // Handle edge case where both initialResp and err are nil
1481 if initialResp == nil {
1482 err := fmt.Errorf("unexpected nil response from processUserMessage with no error")
Sean McCullough96b60dd2025-04-30 09:49:10 -07001483 a.stateMachine.Transition(ctx, StateError, "Error processing user message: "+err.Error())
1484
Sean McCullough9f4b8082025-04-30 17:34:07 +00001485 a.pushToOutbox(ctx, errorMessage(err))
1486 return err
Earl Lee2e463fb2025-04-17 11:22:22 -07001487 }
Sean McCullough885a16a2025-04-30 02:49:25 +00001488
Earl Lee2e463fb2025-04-17 11:22:22 -07001489 // We do this as we go, but let's also do it at the end of the turn
1490 defer func() {
1491 if _, err := a.handleGitCommits(ctx); err != nil {
1492 // Just log the error, don't stop execution
1493 slog.WarnContext(ctx, "Failed to check for new git commits", "error", err)
1494 }
1495 }()
1496
Sean McCullougha1e0e492025-05-01 10:51:08 -07001497 // Main response loop - continue as long as the model is using tools or a tool use fails.
Sean McCullough885a16a2025-04-30 02:49:25 +00001498 resp := initialResp
1499 for {
1500 // Check if we are over budget
1501 if err := a.overBudget(ctx); err != nil {
Sean McCullough96b60dd2025-04-30 09:49:10 -07001502 a.stateMachine.Transition(ctx, StateBudgetExceeded, "Budget exceeded: "+err.Error())
Sean McCullough9f4b8082025-04-30 17:34:07 +00001503 return err
Sean McCullough885a16a2025-04-30 02:49:25 +00001504 }
1505
Philip Zeyligerb8a8f352025-06-02 07:39:37 -07001506 // Check if we should compact the conversation
1507 if a.ShouldCompact() {
1508 a.stateMachine.Transition(ctx, StateCompacting, "Token usage threshold reached, compacting conversation")
1509 if err := a.CompactConversation(ctx); err != nil {
1510 a.stateMachine.Transition(ctx, StateError, "Error during compaction: "+err.Error())
1511 return err
1512 }
1513 // After compaction, end this turn and start fresh
1514 a.stateMachine.Transition(ctx, StateEndOfTurn, "Compaction completed, ending turn")
1515 return nil
1516 }
1517
Sean McCullough885a16a2025-04-30 02:49:25 +00001518 // If the model is not requesting to use a tool, we're done
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -07001519 if resp.StopReason != llm.StopReasonToolUse {
Sean McCullough96b60dd2025-04-30 09:49:10 -07001520 a.stateMachine.Transition(ctx, StateEndOfTurn, "LLM completed response, ending turn")
Sean McCullough885a16a2025-04-30 02:49:25 +00001521 break
1522 }
1523
Sean McCullough96b60dd2025-04-30 09:49:10 -07001524 // Transition to tool use requested state
1525 a.stateMachine.Transition(ctx, StateToolUseRequested, "LLM requested tool use")
1526
Sean McCullough885a16a2025-04-30 02:49:25 +00001527 // Handle tool execution
1528 continueConversation, toolResp := a.handleToolExecution(ctx, resp)
1529 if !continueConversation {
Sean McCullough9f4b8082025-04-30 17:34:07 +00001530 return nil
Sean McCullough885a16a2025-04-30 02:49:25 +00001531 }
1532
Sean McCullougha1e0e492025-05-01 10:51:08 -07001533 if toolResp == nil {
1534 return fmt.Errorf("cannot continue conversation with a nil tool response")
1535 }
1536
Sean McCullough885a16a2025-04-30 02:49:25 +00001537 // Set the response for the next iteration
1538 resp = toolResp
1539 }
Sean McCullough9f4b8082025-04-30 17:34:07 +00001540
1541 return nil
Sean McCullough885a16a2025-04-30 02:49:25 +00001542}
1543
1544// processUserMessage waits for user messages and sends them to the model
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -07001545func (a *Agent) processUserMessage(ctx context.Context) (*llm.Response, error) {
Sean McCullough885a16a2025-04-30 02:49:25 +00001546 // Wait for at least one message from the user
1547 msgs, err := a.GatherMessages(ctx, true)
1548 if err != nil { // e.g. the context was canceled while blocking in GatherMessages
Sean McCullough96b60dd2025-04-30 09:49:10 -07001549 a.stateMachine.Transition(ctx, StateError, "Error gathering messages: "+err.Error())
Sean McCullough885a16a2025-04-30 02:49:25 +00001550 return nil, err
1551 }
1552
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -07001553 userMessage := llm.Message{
1554 Role: llm.MessageRoleUser,
Earl Lee2e463fb2025-04-17 11:22:22 -07001555 Content: msgs,
1556 }
Sean McCullough885a16a2025-04-30 02:49:25 +00001557
Sean McCullough96b60dd2025-04-30 09:49:10 -07001558 // Transition to sending to LLM state
1559 a.stateMachine.Transition(ctx, StateSendingToLLM, "Sending user message to LLM")
1560
Sean McCullough885a16a2025-04-30 02:49:25 +00001561 // Send message to the model
Earl Lee2e463fb2025-04-17 11:22:22 -07001562 resp, err := a.convo.SendMessage(userMessage)
1563 if err != nil {
Sean McCullough96b60dd2025-04-30 09:49:10 -07001564 a.stateMachine.Transition(ctx, StateError, "Error sending to LLM: "+err.Error())
Earl Lee2e463fb2025-04-17 11:22:22 -07001565 a.pushToOutbox(ctx, errorMessage(err))
Sean McCullough885a16a2025-04-30 02:49:25 +00001566 return nil, err
Earl Lee2e463fb2025-04-17 11:22:22 -07001567 }
Earl Lee2e463fb2025-04-17 11:22:22 -07001568
Sean McCullough96b60dd2025-04-30 09:49:10 -07001569 // Transition to processing LLM response state
1570 a.stateMachine.Transition(ctx, StateProcessingLLMResponse, "Processing LLM response")
1571
Sean McCullough885a16a2025-04-30 02:49:25 +00001572 return resp, nil
1573}
1574
1575// handleToolExecution processes a tool use request from the model
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -07001576func (a *Agent) handleToolExecution(ctx context.Context, resp *llm.Response) (bool, *llm.Response) {
1577 var results []llm.Content
Sean McCullough885a16a2025-04-30 02:49:25 +00001578 cancelled := false
Josh Bleecher Snyder64f2aa82025-05-14 18:31:05 +00001579 toolEndsTurn := false
Sean McCullough885a16a2025-04-30 02:49:25 +00001580
Sean McCullough96b60dd2025-04-30 09:49:10 -07001581 // Transition to checking for cancellation state
1582 a.stateMachine.Transition(ctx, StateCheckingForCancellation, "Checking if user requested cancellation")
1583
Sean McCullough885a16a2025-04-30 02:49:25 +00001584 // Check if the operation was cancelled by the user
1585 select {
1586 case <-ctx.Done():
1587 // Don't actually run any of the tools, but rather build a response
1588 // for each tool_use message letting the LLM know that user canceled it.
1589 var err error
1590 results, err = a.convo.ToolResultCancelContents(resp)
Earl Lee2e463fb2025-04-17 11:22:22 -07001591 if err != nil {
Sean McCullough96b60dd2025-04-30 09:49:10 -07001592 a.stateMachine.Transition(ctx, StateError, "Error creating cancellation response: "+err.Error())
Sean McCullough885a16a2025-04-30 02:49:25 +00001593 a.pushToOutbox(ctx, errorMessage(err))
Earl Lee2e463fb2025-04-17 11:22:22 -07001594 }
Sean McCullough885a16a2025-04-30 02:49:25 +00001595 cancelled = true
Sean McCullough96b60dd2025-04-30 09:49:10 -07001596 a.stateMachine.Transition(ctx, StateCancelled, "Operation cancelled by user")
Sean McCullough885a16a2025-04-30 02:49:25 +00001597 default:
Sean McCullough96b60dd2025-04-30 09:49:10 -07001598 // Transition to running tool state
1599 a.stateMachine.Transition(ctx, StateRunningTool, "Executing requested tool")
1600
Josh Bleecher Snyder112b9232025-05-23 11:26:33 -07001601 // Add working directory and session ID to context for tool execution
Sean McCullough885a16a2025-04-30 02:49:25 +00001602 ctx = claudetool.WithWorkingDir(ctx, a.workingDir)
Josh Bleecher Snyder112b9232025-05-23 11:26:33 -07001603 ctx = claudetool.WithSessionID(ctx, a.config.SessionID)
Sean McCullough885a16a2025-04-30 02:49:25 +00001604
1605 // Execute the tools
1606 var err error
Josh Bleecher Snyder64f2aa82025-05-14 18:31:05 +00001607 results, toolEndsTurn, err = a.convo.ToolResultContents(ctx, resp)
Sean McCullough885a16a2025-04-30 02:49:25 +00001608 if ctx.Err() != nil { // e.g. the user canceled the operation
1609 cancelled = true
Sean McCullough96b60dd2025-04-30 09:49:10 -07001610 a.stateMachine.Transition(ctx, StateCancelled, "Operation cancelled during tool execution")
Sean McCullough885a16a2025-04-30 02:49:25 +00001611 } else if err != nil {
Sean McCullough96b60dd2025-04-30 09:49:10 -07001612 a.stateMachine.Transition(ctx, StateError, "Error executing tool: "+err.Error())
Sean McCullough885a16a2025-04-30 02:49:25 +00001613 a.pushToOutbox(ctx, errorMessage(err))
1614 }
1615 }
1616
1617 // Process git commits that may have occurred during tool execution
Sean McCullough96b60dd2025-04-30 09:49:10 -07001618 a.stateMachine.Transition(ctx, StateCheckingGitCommits, "Checking for git commits")
Sean McCullough885a16a2025-04-30 02:49:25 +00001619 autoqualityMessages := a.processGitChanges(ctx)
1620
1621 // Check budget again after tool execution
Sean McCullough96b60dd2025-04-30 09:49:10 -07001622 a.stateMachine.Transition(ctx, StateCheckingBudget, "Checking budget after tool execution")
Sean McCullough885a16a2025-04-30 02:49:25 +00001623 if err := a.overBudget(ctx); err != nil {
Sean McCullough96b60dd2025-04-30 09:49:10 -07001624 a.stateMachine.Transition(ctx, StateBudgetExceeded, "Budget exceeded after tool execution: "+err.Error())
Sean McCullough885a16a2025-04-30 02:49:25 +00001625 return false, nil
1626 }
1627
1628 // Continue the conversation with tool results and any user messages
Josh Bleecher Snyder64f2aa82025-05-14 18:31:05 +00001629 shouldContinue, resp := a.continueTurnWithToolResults(ctx, results, autoqualityMessages, cancelled)
1630 return shouldContinue && !toolEndsTurn, resp
Sean McCullough885a16a2025-04-30 02:49:25 +00001631}
1632
Philip Zeyliger75bd37d2025-05-22 18:49:14 +00001633// DetectGitChanges checks for new git commits and pushes them if found
Philip Zeyliger9bca61e2025-05-22 12:40:06 -07001634func (a *Agent) DetectGitChanges(ctx context.Context) error {
Philip Zeyliger75bd37d2025-05-22 18:49:14 +00001635 // Check for git commits
1636 _, err := a.handleGitCommits(ctx)
1637 if err != nil {
Philip Zeyliger75bd37d2025-05-22 18:49:14 +00001638 slog.WarnContext(ctx, "Failed to check for new git commits", "error", err)
Philip Zeyliger9bca61e2025-05-22 12:40:06 -07001639 return fmt.Errorf("failed to check for new git commits: %w", err)
Philip Zeyliger75bd37d2025-05-22 18:49:14 +00001640 }
Philip Zeyliger9bca61e2025-05-22 12:40:06 -07001641 return nil
Philip Zeyliger75bd37d2025-05-22 18:49:14 +00001642}
1643
1644// processGitChanges checks for new git commits, runs autoformatters if needed, and returns any messages generated
1645// This is used internally by the agent loop
Sean McCullough885a16a2025-04-30 02:49:25 +00001646func (a *Agent) processGitChanges(ctx context.Context) []string {
1647 // Check for git commits after tool execution
1648 newCommits, err := a.handleGitCommits(ctx)
1649 if err != nil {
1650 // Just log the error, don't stop execution
1651 slog.WarnContext(ctx, "Failed to check for new git commits", "error", err)
1652 return nil
1653 }
1654
Josh Bleecher Snyderc72ceb22025-05-05 23:30:15 +00001655 // Run mechanical checks if there was exactly one new commit.
1656 if len(newCommits) != 1 {
1657 return nil
1658 }
Sean McCullough885a16a2025-04-30 02:49:25 +00001659 var autoqualityMessages []string
Josh Bleecher Snyderc72ceb22025-05-05 23:30:15 +00001660 a.stateMachine.Transition(ctx, StateRunningAutoformatters, "Running mechanical checks on new commit")
1661 msg := a.codereview.RunMechanicalChecks(ctx)
1662 if msg != "" {
1663 a.pushToOutbox(ctx, AgentMessage{
1664 Type: AutoMessageType,
1665 Content: msg,
1666 Timestamp: time.Now(),
1667 })
1668 autoqualityMessages = append(autoqualityMessages, msg)
Earl Lee2e463fb2025-04-17 11:22:22 -07001669 }
Sean McCullough885a16a2025-04-30 02:49:25 +00001670
1671 return autoqualityMessages
1672}
1673
1674// continueTurnWithToolResults continues the conversation with tool results
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -07001675func (a *Agent) continueTurnWithToolResults(ctx context.Context, results []llm.Content, autoqualityMessages []string, cancelled bool) (bool, *llm.Response) {
Sean McCullough885a16a2025-04-30 02:49:25 +00001676 // Get any messages the user sent while tools were executing
Sean McCullough96b60dd2025-04-30 09:49:10 -07001677 a.stateMachine.Transition(ctx, StateGatheringAdditionalMessages, "Gathering additional user messages")
Sean McCullough885a16a2025-04-30 02:49:25 +00001678 msgs, err := a.GatherMessages(ctx, false)
1679 if err != nil {
Sean McCullough96b60dd2025-04-30 09:49:10 -07001680 a.stateMachine.Transition(ctx, StateError, "Error gathering additional messages: "+err.Error())
Sean McCullough885a16a2025-04-30 02:49:25 +00001681 return false, nil
1682 }
1683
1684 // Inject any auto-generated messages from quality checks
1685 for _, msg := range autoqualityMessages {
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -07001686 msgs = append(msgs, llm.StringContent(msg))
Sean McCullough885a16a2025-04-30 02:49:25 +00001687 }
1688
1689 // Handle cancellation by appending a message about it
1690 if cancelled {
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -07001691 msgs = append(msgs, llm.StringContent(cancelToolUseMessage))
Sean McCullough885a16a2025-04-30 02:49:25 +00001692 // EndOfTurn is false here so that the client of this agent keeps processing
Philip Zeyligerb7c58752025-05-01 10:10:17 -07001693 // further messages; the conversation is not over.
Sean McCullough885a16a2025-04-30 02:49:25 +00001694 a.pushToOutbox(ctx, AgentMessage{Type: ErrorMessageType, Content: userCancelMessage, EndOfTurn: false})
1695 } else if err := a.convo.OverBudget(); err != nil {
1696 // Handle budget issues by appending a message about it
1697 budgetMsg := "We've exceeded our budget. Please ask the user to confirm before continuing by ending the turn."
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -07001698 msgs = append(msgs, llm.StringContent(budgetMsg))
Sean McCullough885a16a2025-04-30 02:49:25 +00001699 a.pushToOutbox(ctx, budgetMessage(fmt.Errorf("warning: %w (ask to keep trying, if you'd like)", err)))
1700 }
1701
1702 // Combine tool results with user messages
1703 results = append(results, msgs...)
1704
1705 // Send the combined message to continue the conversation
Sean McCullough96b60dd2025-04-30 09:49:10 -07001706 a.stateMachine.Transition(ctx, StateSendingToolResults, "Sending tool results back to LLM")
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -07001707 resp, err := a.convo.SendMessage(llm.Message{
1708 Role: llm.MessageRoleUser,
Sean McCullough885a16a2025-04-30 02:49:25 +00001709 Content: results,
1710 })
1711 if err != nil {
Sean McCullough96b60dd2025-04-30 09:49:10 -07001712 a.stateMachine.Transition(ctx, StateError, "Error sending tool results: "+err.Error())
Sean McCullough885a16a2025-04-30 02:49:25 +00001713 a.pushToOutbox(ctx, errorMessage(fmt.Errorf("error: failed to continue conversation: %s", err.Error())))
1714 return true, nil // Return true to continue the conversation, but with no response
1715 }
1716
Sean McCullough96b60dd2025-04-30 09:49:10 -07001717 // Transition back to processing LLM response
1718 a.stateMachine.Transition(ctx, StateProcessingLLMResponse, "Processing LLM response to tool results")
1719
Sean McCullough885a16a2025-04-30 02:49:25 +00001720 if cancelled {
1721 return false, nil
1722 }
1723
1724 return true, resp
Earl Lee2e463fb2025-04-17 11:22:22 -07001725}
1726
1727func (a *Agent) overBudget(ctx context.Context) error {
1728 if err := a.convo.OverBudget(); err != nil {
Sean McCullough96b60dd2025-04-30 09:49:10 -07001729 a.stateMachine.Transition(ctx, StateBudgetExceeded, "Budget exceeded: "+err.Error())
Earl Lee2e463fb2025-04-17 11:22:22 -07001730 m := budgetMessage(err)
1731 m.Content = m.Content + "\n\nBudget reset."
David Crawshaw35c72bc2025-05-20 11:17:10 -07001732 a.pushToOutbox(ctx, m)
Earl Lee2e463fb2025-04-17 11:22:22 -07001733 a.convo.ResetBudget(a.originalBudget)
1734 return err
1735 }
1736 return nil
1737}
1738
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -07001739func collectTextContent(msg *llm.Response) string {
Earl Lee2e463fb2025-04-17 11:22:22 -07001740 // Collect all text content
1741 var allText strings.Builder
1742 for _, content := range msg.Content {
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -07001743 if content.Type == llm.ContentTypeText && content.Text != "" {
Earl Lee2e463fb2025-04-17 11:22:22 -07001744 if allText.Len() > 0 {
1745 allText.WriteString("\n\n")
1746 }
1747 allText.WriteString(content.Text)
1748 }
1749 }
1750 return allText.String()
1751}
1752
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -07001753func (a *Agent) TotalUsage() conversation.CumulativeUsage {
Earl Lee2e463fb2025-04-17 11:22:22 -07001754 a.mu.Lock()
1755 defer a.mu.Unlock()
1756 return a.convo.CumulativeUsage()
1757}
1758
Earl Lee2e463fb2025-04-17 11:22:22 -07001759// Diff returns a unified diff of changes made since the agent was instantiated.
1760func (a *Agent) Diff(commit *string) (string, error) {
Philip Zeyliger49edc922025-05-14 09:45:45 -07001761 if a.SketchGitBase() == "" {
Earl Lee2e463fb2025-04-17 11:22:22 -07001762 return "", fmt.Errorf("no initial commit reference available")
1763 }
1764
1765 // Find the repository root
1766 ctx := context.Background()
1767
1768 // If a specific commit hash is provided, show just that commit's changes
1769 if commit != nil && *commit != "" {
1770 // Validate that the commit looks like a valid git SHA
1771 if !isValidGitSHA(*commit) {
1772 return "", fmt.Errorf("invalid git commit SHA format: %s", *commit)
1773 }
1774
1775 // Get the diff for just this commit
1776 cmd := exec.CommandContext(ctx, "git", "show", "--unified=10", *commit)
1777 cmd.Dir = a.repoRoot
1778 output, err := cmd.CombinedOutput()
1779 if err != nil {
1780 return "", fmt.Errorf("failed to get diff for commit %s: %w - %s", *commit, err, string(output))
1781 }
1782 return string(output), nil
1783 }
1784
1785 // Otherwise, get the diff between the initial commit and the current state using exec.Command
Philip Zeyliger49edc922025-05-14 09:45:45 -07001786 cmd := exec.CommandContext(ctx, "git", "diff", "--unified=10", a.SketchGitBaseRef())
Earl Lee2e463fb2025-04-17 11:22:22 -07001787 cmd.Dir = a.repoRoot
1788 output, err := cmd.CombinedOutput()
1789 if err != nil {
1790 return "", fmt.Errorf("failed to get diff: %w - %s", err, string(output))
1791 }
1792
1793 return string(output), nil
1794}
1795
Philip Zeyliger49edc922025-05-14 09:45:45 -07001796// SketchGitBaseRef distinguishes between the typical container version, where sketch-base is
1797// unambiguous, and the "unsafe" version, where we need to use a session id to disambiguate.
1798func (a *Agent) SketchGitBaseRef() string {
1799 if a.IsInContainer() {
1800 return "sketch-base"
1801 } else {
1802 return "sketch-base-" + a.SessionID()
1803 }
1804}
1805
1806// SketchGitBase returns the Git commit hash that was saved when the agent was instantiated.
1807func (a *Agent) SketchGitBase() string {
1808 cmd := exec.CommandContext(context.Background(), "git", "rev-parse", a.SketchGitBaseRef())
1809 cmd.Dir = a.repoRoot
1810 output, err := cmd.CombinedOutput()
1811 if err != nil {
1812 slog.Warn("could not identify sketch-base", slog.String("error", err.Error()))
1813 return "HEAD"
1814 }
1815 return string(strings.TrimSpace(string(output)))
Earl Lee2e463fb2025-04-17 11:22:22 -07001816}
1817
Pokey Rule7a113622025-05-12 10:58:45 +01001818// removeGitHooks removes the Git hooks directory from the repository
1819func removeGitHooks(_ context.Context, repoPath string) error {
1820 hooksDir := filepath.Join(repoPath, ".git", "hooks")
1821
1822 // Check if hooks directory exists
1823 if _, err := os.Stat(hooksDir); os.IsNotExist(err) {
1824 // Directory doesn't exist, nothing to do
1825 return nil
1826 }
1827
1828 // Remove the hooks directory
1829 err := os.RemoveAll(hooksDir)
1830 if err != nil {
1831 return fmt.Errorf("failed to remove git hooks directory: %w", err)
1832 }
1833
1834 // Create an empty hooks directory to prevent git from recreating default hooks
Autoformattere577ef72025-05-12 10:29:00 +00001835 err = os.MkdirAll(hooksDir, 0o755)
Pokey Rule7a113622025-05-12 10:58:45 +01001836 if err != nil {
1837 return fmt.Errorf("failed to create empty git hooks directory: %w", err)
1838 }
1839
1840 return nil
1841}
1842
Philip Zeyligerf2872992025-05-22 10:35:28 -07001843func (a *Agent) handleGitCommits(ctx context.Context) ([]*GitCommit, error) {
Philip Zeyligerbe7802a2025-06-04 20:15:25 +00001844 msgs, commits, error := a.gitState.handleGitCommits(ctx, a.SessionID(), a.repoRoot, a.SketchGitBaseRef(), a.config.BranchPrefix)
Philip Zeyligerf2872992025-05-22 10:35:28 -07001845 for _, msg := range msgs {
1846 a.pushToOutbox(ctx, msg)
1847 }
1848 return commits, error
1849}
1850
Earl Lee2e463fb2025-04-17 11:22:22 -07001851// handleGitCommits() highlights new commits to the user. When running
Josh Bleecher Snyder19969a92025-06-05 14:34:02 -07001852// under docker, new HEADs are pushed to a branch according to the slug.
Philip Zeyligerbe7802a2025-06-04 20:15:25 +00001853func (ags *AgentGitState) handleGitCommits(ctx context.Context, sessionID string, repoRoot string, baseRef string, branchPrefix string) ([]AgentMessage, []*GitCommit, error) {
Philip Zeyligerf2872992025-05-22 10:35:28 -07001854 ags.mu.Lock()
1855 defer ags.mu.Unlock()
1856
1857 msgs := []AgentMessage{}
1858 if repoRoot == "" {
1859 return msgs, nil, nil
Earl Lee2e463fb2025-04-17 11:22:22 -07001860 }
1861
Philip Zeyligerf2872992025-05-22 10:35:28 -07001862 head, err := resolveRef(ctx, repoRoot, "HEAD")
Earl Lee2e463fb2025-04-17 11:22:22 -07001863 if err != nil {
Philip Zeyligerf2872992025-05-22 10:35:28 -07001864 return msgs, nil, err
Earl Lee2e463fb2025-04-17 11:22:22 -07001865 }
Philip Zeyligerf2872992025-05-22 10:35:28 -07001866 if head == ags.lastHEAD {
1867 return msgs, nil, nil // nothing to do
Earl Lee2e463fb2025-04-17 11:22:22 -07001868 }
1869 defer func() {
Philip Zeyligerf2872992025-05-22 10:35:28 -07001870 ags.lastHEAD = head
Earl Lee2e463fb2025-04-17 11:22:22 -07001871 }()
1872
1873 // Get new commits. Because it's possible that the agent does rebases, fixups, and
1874 // so forth, we use, as our fixed point, the "initialCommit", and we limit ourselves
1875 // to the last 100 commits.
1876 var commits []*GitCommit
1877
1878 // Get commits since the initial commit
1879 // Format: <hash>\0<subject>\0<body>\0
1880 // This uses NULL bytes as separators to avoid issues with newlines in commit messages
1881 // Limit to 100 commits to avoid overwhelming the user
Philip Zeyligerf2872992025-05-22 10:35:28 -07001882 cmd := exec.CommandContext(ctx, "git", "log", "-n", "100", "--pretty=format:%H%x00%s%x00%b%x00", "^"+baseRef, head)
1883 cmd.Dir = repoRoot
Earl Lee2e463fb2025-04-17 11:22:22 -07001884 output, err := cmd.Output()
1885 if err != nil {
Philip Zeyligerf2872992025-05-22 10:35:28 -07001886 return msgs, nil, fmt.Errorf("failed to get git log: %w", err)
Earl Lee2e463fb2025-04-17 11:22:22 -07001887 }
1888
1889 // Parse git log output and filter out already seen commits
1890 parsedCommits := parseGitLog(string(output))
1891
1892 var headCommit *GitCommit
1893
1894 // Filter out commits we've already seen
1895 for _, commit := range parsedCommits {
1896 if commit.Hash == head {
1897 headCommit = &commit
1898 }
1899
1900 // Skip if we've seen this commit before. If our head has changed, always include that.
Philip Zeyligerf2872992025-05-22 10:35:28 -07001901 if ags.seenCommits[commit.Hash] && commit.Hash != head {
Earl Lee2e463fb2025-04-17 11:22:22 -07001902 continue
1903 }
1904
1905 // Mark this commit as seen
Philip Zeyligerf2872992025-05-22 10:35:28 -07001906 ags.seenCommits[commit.Hash] = true
Earl Lee2e463fb2025-04-17 11:22:22 -07001907
1908 // Add to our list of new commits
1909 commits = append(commits, &commit)
1910 }
1911
Philip Zeyligerf2872992025-05-22 10:35:28 -07001912 if ags.gitRemoteAddr != "" {
Earl Lee2e463fb2025-04-17 11:22:22 -07001913 if headCommit == nil {
1914 // I think this can only happen if we have a bug or if there's a race.
1915 headCommit = &GitCommit{}
1916 headCommit.Hash = head
1917 headCommit.Subject = "unknown"
1918 commits = append(commits, headCommit)
1919 }
1920
Earl Lee2e463fb2025-04-17 11:22:22 -07001921 // TODO: I don't love the force push here. We could see if the push is a fast-forward, and,
1922 // if it's not, we could make a backup with a unique name (perhaps append a timestamp) and
1923 // then use push with lease to replace.
Philip Zeyliger113e2052025-05-09 21:59:40 +00001924
Josh Bleecher Snyder19969a92025-06-05 14:34:02 -07001925 // Try up to 10 times with incrementing retry numbers if the branch is checked out on the remote
Philip Zeyliger113e2052025-05-09 21:59:40 +00001926 var out []byte
1927 var err error
Josh Bleecher Snyder19969a92025-06-05 14:34:02 -07001928 originalRetryNumber := ags.retryNumber
1929 originalBranchName := ags.branchNameLocked(branchPrefix)
Philip Zeyliger113e2052025-05-09 21:59:40 +00001930 for retries := range 10 {
1931 if retries > 0 {
Josh Bleecher Snyder19969a92025-06-05 14:34:02 -07001932 ags.IncrementRetryNumber()
Philip Zeyliger113e2052025-05-09 21:59:40 +00001933 }
1934
Josh Bleecher Snyder19969a92025-06-05 14:34:02 -07001935 branch := ags.branchNameLocked(branchPrefix)
Philip Zeyligerf2872992025-05-22 10:35:28 -07001936 cmd = exec.Command("git", "push", "--force", ags.gitRemoteAddr, "HEAD:refs/heads/"+branch)
1937 cmd.Dir = repoRoot
Philip Zeyliger113e2052025-05-09 21:59:40 +00001938 out, err = cmd.CombinedOutput()
1939
1940 if err == nil {
1941 // Success! Break out of the retry loop
1942 break
1943 }
1944
1945 // Check if this is the "refusing to update checked out branch" error
1946 if !strings.Contains(string(out), "refusing to update checked out branch") {
1947 // This is a different error, so don't retry
1948 break
1949 }
Philip Zeyliger113e2052025-05-09 21:59:40 +00001950 }
1951
1952 if err != nil {
Philip Zeyligerf2872992025-05-22 10:35:28 -07001953 msgs = append(msgs, errorMessage(fmt.Errorf("git push to host: %s: %v", out, err)))
Earl Lee2e463fb2025-04-17 11:22:22 -07001954 } else {
Josh Bleecher Snyder19969a92025-06-05 14:34:02 -07001955 finalBranch := ags.branchNameLocked(branchPrefix)
1956 headCommit.PushedBranch = finalBranch
1957 if ags.retryNumber != originalRetryNumber {
1958 // Notify user that the branch name was changed, and why
Philip Zeyliger59e1c162025-06-02 12:54:34 +00001959 msgs = append(msgs, AgentMessage{
1960 Type: AutoMessageType,
1961 Timestamp: time.Now(),
Josh Bleecher Snyder19969a92025-06-05 14:34:02 -07001962 Content: fmt.Sprintf("Branch renamed from %s to %s because the original branch is currently checked out on the remote.", originalBranchName, finalBranch),
Philip Zeyliger59e1c162025-06-02 12:54:34 +00001963 })
Philip Zeyliger113e2052025-05-09 21:59:40 +00001964 }
Earl Lee2e463fb2025-04-17 11:22:22 -07001965 }
1966 }
1967
1968 // If we found new commits, create a message
1969 if len(commits) > 0 {
1970 msg := AgentMessage{
1971 Type: CommitMessageType,
1972 Timestamp: time.Now(),
1973 Commits: commits,
1974 }
Philip Zeyligerf2872992025-05-22 10:35:28 -07001975 msgs = append(msgs, msg)
Earl Lee2e463fb2025-04-17 11:22:22 -07001976 }
Philip Zeyligerf2872992025-05-22 10:35:28 -07001977 return msgs, commits, nil
Earl Lee2e463fb2025-04-17 11:22:22 -07001978}
1979
Josh Bleecher Snyder19969a92025-06-05 14:34:02 -07001980func cleanSlugName(s string) string {
Josh Bleecher Snyder1ae976b2025-04-30 00:06:43 +00001981 return strings.Map(func(r rune) rune {
1982 // lowercase
1983 if r >= 'A' && r <= 'Z' {
1984 return r + 'a' - 'A'
Earl Lee2e463fb2025-04-17 11:22:22 -07001985 }
Josh Bleecher Snyder1ae976b2025-04-30 00:06:43 +00001986 // replace spaces with dashes
1987 if r == ' ' {
1988 return '-'
1989 }
1990 // allow alphanumerics and dashes
1991 if (r >= 'a' && r <= 'z') || r == '-' || (r >= '0' && r <= '9') {
1992 return r
1993 }
1994 return -1
1995 }, s)
Earl Lee2e463fb2025-04-17 11:22:22 -07001996}
1997
1998// parseGitLog parses the output of git log with format '%H%x00%s%x00%b%x00'
1999// and returns an array of GitCommit structs.
2000func parseGitLog(output string) []GitCommit {
2001 var commits []GitCommit
2002
2003 // No output means no commits
2004 if len(output) == 0 {
2005 return commits
2006 }
2007
2008 // Split by NULL byte
2009 parts := strings.Split(output, "\x00")
2010
2011 // Process in triplets (hash, subject, body)
2012 for i := 0; i < len(parts); i++ {
2013 // Skip empty parts
2014 if parts[i] == "" {
2015 continue
2016 }
2017
2018 // This should be a hash
2019 hash := strings.TrimSpace(parts[i])
2020
2021 // Make sure we have at least a subject part available
2022 if i+1 >= len(parts) {
2023 break // No more parts available
2024 }
2025
2026 // Get the subject
2027 subject := strings.TrimSpace(parts[i+1])
2028
2029 // Get the body if available
2030 body := ""
2031 if i+2 < len(parts) {
2032 body = strings.TrimSpace(parts[i+2])
2033 }
2034
2035 // Skip to the next triplet
2036 i += 2
2037
2038 commits = append(commits, GitCommit{
2039 Hash: hash,
2040 Subject: subject,
2041 Body: body,
2042 })
2043 }
2044
2045 return commits
2046}
2047
2048func repoRoot(ctx context.Context, dir string) (string, error) {
2049 cmd := exec.CommandContext(ctx, "git", "rev-parse", "--show-toplevel")
2050 stderr := new(strings.Builder)
2051 cmd.Stderr = stderr
2052 cmd.Dir = dir
2053 out, err := cmd.Output()
2054 if err != nil {
Philip Zeyligerbc8c8dc2025-05-21 13:19:13 -07002055 return "", fmt.Errorf("git rev-parse (in %s) failed: %w\n%s", dir, err, stderr)
Earl Lee2e463fb2025-04-17 11:22:22 -07002056 }
2057 return strings.TrimSpace(string(out)), nil
2058}
2059
2060func resolveRef(ctx context.Context, dir, refName string) (string, error) {
2061 cmd := exec.CommandContext(ctx, "git", "rev-parse", refName)
2062 stderr := new(strings.Builder)
2063 cmd.Stderr = stderr
2064 cmd.Dir = dir
2065 out, err := cmd.Output()
2066 if err != nil {
2067 return "", fmt.Errorf("git rev-parse failed: %w\n%s", err, stderr)
2068 }
2069 // TODO: validate that out is valid hex
2070 return strings.TrimSpace(string(out)), nil
2071}
2072
2073// isValidGitSHA validates if a string looks like a valid git SHA hash.
2074// Git SHAs are hexadecimal strings of at least 4 characters but typically 7, 8, or 40 characters.
2075func isValidGitSHA(sha string) bool {
2076 // Git SHA must be a hexadecimal string with at least 4 characters
2077 if len(sha) < 4 || len(sha) > 40 {
2078 return false
2079 }
2080
2081 // Check if the string only contains hexadecimal characters
2082 for _, char := range sha {
2083 if !(char >= '0' && char <= '9') && !(char >= 'a' && char <= 'f') && !(char >= 'A' && char <= 'F') {
2084 return false
2085 }
2086 }
2087
2088 return true
2089}
Philip Zeyligerd1402952025-04-23 03:54:37 +00002090
2091// getGitOrigin returns the URL of the git remote 'origin' if it exists
2092func getGitOrigin(ctx context.Context, dir string) string {
2093 cmd := exec.CommandContext(ctx, "git", "config", "--get", "remote.origin.url")
2094 cmd.Dir = dir
2095 stderr := new(strings.Builder)
2096 cmd.Stderr = stderr
2097 out, err := cmd.Output()
2098 if err != nil {
2099 return ""
2100 }
2101 return strings.TrimSpace(string(out))
2102}
Philip Zeyliger2c4db092025-04-28 16:57:50 -07002103
Josh Bleecher Snyder5cca56f2025-05-06 01:10:16 +00002104// systemPromptData contains the data used to render the system prompt template
2105type systemPromptData struct {
Josh Bleecher Snyder5cca56f2025-05-06 01:10:16 +00002106 ClientGOOS string
2107 ClientGOARCH string
2108 WorkingDir string
2109 RepoRoot string
2110 InitialCommit string
Josh Bleecher Snydera997be62025-05-07 22:52:46 +00002111 Codebase *onstart.Codebase
Josh Bleecher Snyder5cca56f2025-05-06 01:10:16 +00002112}
2113
2114// renderSystemPrompt renders the system prompt template.
2115func (a *Agent) renderSystemPrompt() string {
Josh Bleecher Snyder5cca56f2025-05-06 01:10:16 +00002116 data := systemPromptData{
Josh Bleecher Snyder5cca56f2025-05-06 01:10:16 +00002117 ClientGOOS: a.config.ClientGOOS,
2118 ClientGOARCH: a.config.ClientGOARCH,
2119 WorkingDir: a.workingDir,
2120 RepoRoot: a.repoRoot,
Philip Zeyliger49edc922025-05-14 09:45:45 -07002121 InitialCommit: a.SketchGitBase(),
Josh Bleecher Snydera997be62025-05-07 22:52:46 +00002122 Codebase: a.codebase,
Josh Bleecher Snyder5cca56f2025-05-06 01:10:16 +00002123 }
2124
2125 tmpl, err := template.New("system").Parse(agentSystemPrompt)
2126 if err != nil {
2127 panic(fmt.Sprintf("failed to parse system prompt template: %v", err))
2128 }
2129 buf := new(strings.Builder)
2130 err = tmpl.Execute(buf, data)
2131 if err != nil {
2132 panic(fmt.Sprintf("failed to execute system prompt template: %v", err))
2133 }
Josh Bleecher Snydera997be62025-05-07 22:52:46 +00002134 // fmt.Printf("system prompt: %s\n", buf.String())
Josh Bleecher Snyder5cca56f2025-05-06 01:10:16 +00002135 return buf.String()
2136}
Philip Zeyligereab12de2025-05-14 02:35:53 +00002137
2138// StateTransitionIterator provides an iterator over state transitions.
2139type StateTransitionIterator interface {
2140 // Next blocks until a new state transition is available or context is done.
2141 // Returns nil if the context is cancelled.
2142 Next() *StateTransition
2143 // Close removes the listener and cleans up resources.
2144 Close()
2145}
2146
2147// StateTransitionIteratorImpl implements StateTransitionIterator using a state machine listener.
2148type StateTransitionIteratorImpl struct {
2149 agent *Agent
2150 ctx context.Context
2151 ch chan StateTransition
2152 unsubscribe func()
2153}
2154
2155// Next blocks until a new state transition is available or the context is cancelled.
2156func (s *StateTransitionIteratorImpl) Next() *StateTransition {
2157 select {
2158 case <-s.ctx.Done():
2159 return nil
2160 case transition, ok := <-s.ch:
2161 if !ok {
2162 return nil
2163 }
2164 transitionCopy := transition
2165 return &transitionCopy
2166 }
2167}
2168
2169// Close removes the listener and cleans up resources.
2170func (s *StateTransitionIteratorImpl) Close() {
2171 if s.unsubscribe != nil {
2172 s.unsubscribe()
2173 s.unsubscribe = nil
2174 }
2175}
2176
2177// NewStateTransitionIterator returns an iterator that receives state transitions.
2178func (a *Agent) NewStateTransitionIterator(ctx context.Context) StateTransitionIterator {
2179 a.mu.Lock()
2180 defer a.mu.Unlock()
2181
2182 // Create channel to receive state transitions
2183 ch := make(chan StateTransition, 10)
2184
2185 // Add a listener to the state machine
2186 unsubscribe := a.stateMachine.AddTransitionListener(ch)
2187
2188 return &StateTransitionIteratorImpl{
2189 agent: a,
2190 ctx: ctx,
2191 ch: ch,
2192 unsubscribe: unsubscribe,
2193 }
2194}
Josh Bleecher Snyder039fc342025-05-14 21:24:12 +00002195
2196// setupGitHooks creates or updates git hooks in the specified working directory.
2197func setupGitHooks(workingDir string) error {
2198 hooksDir := filepath.Join(workingDir, ".git", "hooks")
2199
2200 _, err := os.Stat(hooksDir)
2201 if os.IsNotExist(err) {
2202 return fmt.Errorf("git hooks directory does not exist: %s", hooksDir)
2203 }
2204 if err != nil {
2205 return fmt.Errorf("error checking git hooks directory: %w", err)
2206 }
2207
2208 // Define the post-commit hook content
2209 postCommitHook := `#!/bin/bash
2210echo "<post_commit_hook>"
2211echo "Please review this commit message and fix it if it is incorrect."
2212echo "This hook only echos the commit message; it does not modify it."
2213echo "Bash escaping is a common source of issues; to fix that, create a temp file and use 'git commit --amend -F COMMIT_MSG_FILE'."
2214echo "<last_commit_message>"
2215git log -1 --pretty=%B
2216echo "</last_commit_message>"
2217echo "</post_commit_hook>"
2218`
2219
2220 // Define the prepare-commit-msg hook content
2221 prepareCommitMsgHook := `#!/bin/bash
2222# Add Co-Authored-By and Change-ID trailers to commit messages
2223# Check if these trailers already exist before adding them
2224
2225commit_file="$1"
2226COMMIT_SOURCE="$2"
2227
2228# Skip for merges, squashes, or when using a commit template
2229if [ "$COMMIT_SOURCE" = "template" ] || [ "$COMMIT_SOURCE" = "merge" ] || \
2230 [ "$COMMIT_SOURCE" = "squash" ]; then
2231 exit 0
2232fi
2233
2234commit_msg=$(cat "$commit_file")
2235
2236needs_co_author=true
2237needs_change_id=true
2238
2239# Check if commit message already has Co-Authored-By trailer
2240if grep -q "Co-Authored-By: sketch <hello@sketch.dev>" "$commit_file"; then
2241 needs_co_author=false
2242fi
2243
2244# Check if commit message already has Change-ID trailer
2245if grep -q "Change-ID: s[a-f0-9]\+k" "$commit_file"; then
2246 needs_change_id=false
2247fi
2248
2249# Only modify if at least one trailer needs to be added
2250if [ "$needs_co_author" = true ] || [ "$needs_change_id" = true ]; then
Josh Bleecher Snyderb509a5d2025-05-23 15:49:42 +00002251 # Ensure there's a proper blank line before trailers
2252 if [ -s "$commit_file" ]; then
2253 # Check if file ends with newline by reading last character
2254 last_char=$(tail -c 1 "$commit_file")
2255
2256 if [ "$last_char" != "" ]; then
2257 # File doesn't end with newline - add two newlines (complete line + blank line)
2258 echo "" >> "$commit_file"
2259 echo "" >> "$commit_file"
2260 else
2261 # File ends with newline - check if we already have a blank line
2262 last_line=$(tail -1 "$commit_file")
2263 if [ -n "$last_line" ]; then
2264 # Last line has content - add one newline for blank line
2265 echo "" >> "$commit_file"
2266 fi
2267 # If last line is empty, we already have a blank line - don't add anything
2268 fi
Josh Bleecher Snyder039fc342025-05-14 21:24:12 +00002269 fi
2270
2271 # Add trailers if needed
2272 if [ "$needs_co_author" = true ]; then
2273 echo "Co-Authored-By: sketch <hello@sketch.dev>" >> "$commit_file"
2274 fi
2275
2276 if [ "$needs_change_id" = true ]; then
2277 change_id=$(openssl rand -hex 8)
2278 echo "Change-ID: s${change_id}k" >> "$commit_file"
2279 fi
2280fi
2281`
2282
2283 // Update or create the post-commit hook
2284 err = updateOrCreateHook(filepath.Join(hooksDir, "post-commit"), postCommitHook, "<last_commit_message>")
2285 if err != nil {
2286 return fmt.Errorf("failed to set up post-commit hook: %w", err)
2287 }
2288
2289 // Update or create the prepare-commit-msg hook
2290 err = updateOrCreateHook(filepath.Join(hooksDir, "prepare-commit-msg"), prepareCommitMsgHook, "Add Co-Authored-By and Change-ID trailers")
2291 if err != nil {
2292 return fmt.Errorf("failed to set up prepare-commit-msg hook: %w", err)
2293 }
2294
2295 return nil
2296}
2297
2298// updateOrCreateHook creates a new hook file or updates an existing one
2299// by appending the new content if it doesn't already contain it.
2300func updateOrCreateHook(hookPath, content, distinctiveLine string) error {
2301 // Check if the hook already exists
2302 buf, err := os.ReadFile(hookPath)
2303 if os.IsNotExist(err) {
2304 // Hook doesn't exist, create it
2305 err = os.WriteFile(hookPath, []byte(content), 0o755)
2306 if err != nil {
2307 return fmt.Errorf("failed to create hook: %w", err)
2308 }
2309 return nil
2310 }
2311 if err != nil {
2312 return fmt.Errorf("error reading existing hook: %w", err)
2313 }
2314
2315 // Hook exists, check if our content is already in it by looking for a distinctive line
2316 code := string(buf)
2317 if strings.Contains(code, distinctiveLine) {
2318 // Already contains our content, nothing to do
2319 return nil
2320 }
2321
2322 // Append our content to the existing hook
2323 f, err := os.OpenFile(hookPath, os.O_APPEND|os.O_WRONLY, 0o755)
2324 if err != nil {
2325 return fmt.Errorf("failed to open hook for appending: %w", err)
2326 }
2327 defer f.Close()
2328
2329 // Ensure there's a newline at the end of the existing content if needed
2330 if len(code) > 0 && !strings.HasSuffix(code, "\n") {
2331 _, err = f.WriteString("\n")
2332 if err != nil {
2333 return fmt.Errorf("failed to add newline to hook: %w", err)
2334 }
2335 }
2336
2337 // Add a separator before our content
2338 _, err = f.WriteString("\n# === Added by Sketch ===\n" + content)
2339 if err != nil {
2340 return fmt.Errorf("failed to append to hook: %w", err)
2341 }
2342
2343 return nil
2344}
Sean McCullough138ec242025-06-02 22:42:06 +00002345
2346// GetPortMonitor returns the port monitor instance for accessing port events
2347func (a *Agent) GetPortMonitor() *PortMonitor {
2348 return a.portMonitor
2349}