blob: a14d244c160757e424c456c49a0e5dc0369d8585 [file] [log] [blame]
Earl Lee2e463fb2025-04-17 11:22:22 -07001package loop
2
3import (
4 "context"
5 "encoding/json"
6 "fmt"
7 "log/slog"
8 "net/http"
9 "os"
10 "os/exec"
11 "runtime/debug"
12 "slices"
13 "strings"
14 "sync"
15 "time"
16
17 "sketch.dev/ant"
18 "sketch.dev/claudetool"
19)
20
21const (
22 userCancelMessage = "user requested agent to stop handling responses"
23)
24
25type CodingAgent interface {
26 // Init initializes an agent inside a docker container.
27 Init(AgentInit) error
28
29 // Ready returns a channel closed after Init successfully called.
30 Ready() <-chan struct{}
31
32 // URL reports the HTTP URL of this agent.
33 URL() string
34
35 // UserMessage enqueues a message to the agent and returns immediately.
36 UserMessage(ctx context.Context, msg string)
37
38 // WaitForMessage blocks until the agent has a response to give.
39 // Use AgentMessage.EndOfTurn to help determine if you want to
40 // drain the agent.
41 WaitForMessage(ctx context.Context) AgentMessage
42
43 // Loop begins the agent loop returns only when ctx is cancelled.
44 Loop(ctx context.Context)
45
46 CancelInnerLoop(cause error)
47
48 CancelToolUse(toolUseID string, cause error) error
49
50 // Returns a subset of the agent's message history.
51 Messages(start int, end int) []AgentMessage
52
53 // Returns the current number of messages in the history
54 MessageCount() int
55
56 TotalUsage() ant.CumulativeUsage
57 OriginalBudget() ant.Budget
58
59 // WaitForMessageCount returns when the agent has at more than clientMessageCount messages or the context is done.
60 WaitForMessageCount(ctx context.Context, greaterThan int)
61
62 WorkingDir() string
63
64 // Diff returns a unified diff of changes made since the agent was instantiated.
65 // If commit is non-nil, it shows the diff for just that specific commit.
66 Diff(commit *string) (string, error)
67
68 // InitialCommit returns the Git commit hash that was saved when the agent was instantiated.
69 InitialCommit() string
70
71 // Title returns the current title of the conversation.
72 Title() string
73
74 // OS returns the operating system of the client.
75 OS() string
76}
77
78type CodingAgentMessageType string
79
80const (
81 UserMessageType CodingAgentMessageType = "user"
82 AgentMessageType CodingAgentMessageType = "agent"
83 ErrorMessageType CodingAgentMessageType = "error"
84 BudgetMessageType CodingAgentMessageType = "budget" // dedicated for "out of budget" errors
85 ToolUseMessageType CodingAgentMessageType = "tool"
86 CommitMessageType CodingAgentMessageType = "commit" // for displaying git commits
87 AutoMessageType CodingAgentMessageType = "auto" // for automated notifications like autoformatting
88
89 cancelToolUseMessage = "Stop responding to my previous message. Wait for me to ask you something else before attempting to use any more tools."
90)
91
92type AgentMessage struct {
93 Type CodingAgentMessageType `json:"type"`
94 // EndOfTurn indicates that the AI is done working and is ready for the next user input.
95 EndOfTurn bool `json:"end_of_turn"`
96
97 Content string `json:"content"`
98 ToolName string `json:"tool_name,omitempty"`
99 ToolInput string `json:"input,omitempty"`
100 ToolResult string `json:"tool_result,omitempty"`
101 ToolError bool `json:"tool_error,omitempty"`
102 ToolCallId string `json:"tool_call_id,omitempty"`
103
104 // ToolCalls is a list of all tool calls requested in this message (name and input pairs)
105 ToolCalls []ToolCall `json:"tool_calls,omitempty"`
106
Sean McCulloughd9f13372025-04-21 15:08:49 -0700107 // ToolResponses is a list of all responses to tool calls requested in this message (name and input pairs)
108 ToolResponses []AgentMessage `json:"toolResponses,omitempty"`
109
Earl Lee2e463fb2025-04-17 11:22:22 -0700110 // Commits is a list of git commits for a commit message
111 Commits []*GitCommit `json:"commits,omitempty"`
112
113 Timestamp time.Time `json:"timestamp"`
114 ConversationID string `json:"conversation_id"`
115 ParentConversationID *string `json:"parent_conversation_id,omitempty"`
116 Usage *ant.Usage `json:"usage,omitempty"`
117
118 // Message timing information
119 StartTime *time.Time `json:"start_time,omitempty"`
120 EndTime *time.Time `json:"end_time,omitempty"`
121 Elapsed *time.Duration `json:"elapsed,omitempty"`
122
123 // Turn duration - the time taken for a complete agent turn
124 TurnDuration *time.Duration `json:"turnDuration,omitempty"`
125
126 Idx int `json:"idx"`
127}
128
129// GitCommit represents a single git commit for a commit message
130type GitCommit struct {
131 Hash string `json:"hash"` // Full commit hash
132 Subject string `json:"subject"` // Commit subject line
133 Body string `json:"body"` // Full commit message body
134 PushedBranch string `json:"pushed_branch,omitempty"` // If set, this commit was pushed to this branch
135}
136
137// ToolCall represents a single tool call within an agent message
138type ToolCall struct {
Sean McCulloughd9f13372025-04-21 15:08:49 -0700139 Name string `json:"name"`
140 Input string `json:"input"`
141 ToolCallId string `json:"tool_call_id"`
142 ResultMessage *AgentMessage `json:"result_message,omitempty"`
143 Args string `json:"args,omitempty"`
144 Result string `json:"result,omitempty"`
Earl Lee2e463fb2025-04-17 11:22:22 -0700145}
146
147func (a *AgentMessage) Attr() slog.Attr {
148 var attrs []any = []any{
149 slog.String("type", string(a.Type)),
150 }
151 if a.EndOfTurn {
152 attrs = append(attrs, slog.Bool("end_of_turn", a.EndOfTurn))
153 }
154 if a.Content != "" {
155 attrs = append(attrs, slog.String("content", a.Content))
156 }
157 if a.ToolName != "" {
158 attrs = append(attrs, slog.String("tool_name", a.ToolName))
159 }
160 if a.ToolInput != "" {
161 attrs = append(attrs, slog.String("tool_input", a.ToolInput))
162 }
163 if a.Elapsed != nil {
164 attrs = append(attrs, slog.Int64("elapsed", a.Elapsed.Nanoseconds()))
165 }
166 if a.TurnDuration != nil {
167 attrs = append(attrs, slog.Int64("turnDuration", a.TurnDuration.Nanoseconds()))
168 }
169 if a.ToolResult != "" {
170 attrs = append(attrs, slog.String("tool_result", a.ToolResult))
171 }
172 if a.ToolError {
173 attrs = append(attrs, slog.Bool("tool_error", a.ToolError))
174 }
175 if len(a.ToolCalls) > 0 {
176 toolCallAttrs := make([]any, 0, len(a.ToolCalls))
177 for i, tc := range a.ToolCalls {
178 toolCallAttrs = append(toolCallAttrs, slog.Group(
179 fmt.Sprintf("tool_call_%d", i),
180 slog.String("name", tc.Name),
181 slog.String("input", tc.Input),
182 ))
183 }
184 attrs = append(attrs, slog.Group("tool_calls", toolCallAttrs...))
185 }
186 if a.ConversationID != "" {
187 attrs = append(attrs, slog.String("convo_id", a.ConversationID))
188 }
189 if a.ParentConversationID != nil {
190 attrs = append(attrs, slog.String("parent_convo_id", *a.ParentConversationID))
191 }
192 if a.Usage != nil && !a.Usage.IsZero() {
193 attrs = append(attrs, a.Usage.Attr())
194 }
195 // TODO: timestamp, convo ids, idx?
196 return slog.Group("agent_message", attrs...)
197}
198
199func errorMessage(err error) AgentMessage {
200 // It's somewhat unknowable whether error messages are "end of turn" or not, but it seems like the best approach.
201 if os.Getenv(("DEBUG")) == "1" {
202 return AgentMessage{Type: ErrorMessageType, Content: err.Error() + " Stacktrace: " + string(debug.Stack()), EndOfTurn: true}
203 }
204
205 return AgentMessage{Type: ErrorMessageType, Content: err.Error(), EndOfTurn: true}
206}
207
208func budgetMessage(err error) AgentMessage {
209 return AgentMessage{Type: BudgetMessageType, Content: err.Error(), EndOfTurn: true}
210}
211
212// ConvoInterface defines the interface for conversation interactions
213type ConvoInterface interface {
214 CumulativeUsage() ant.CumulativeUsage
215 ResetBudget(ant.Budget)
216 OverBudget() error
217 SendMessage(message ant.Message) (*ant.MessageResponse, error)
218 SendUserTextMessage(s string, otherContents ...ant.Content) (*ant.MessageResponse, error)
219 ToolResultContents(ctx context.Context, resp *ant.MessageResponse) ([]ant.Content, error)
220 ToolResultCancelContents(resp *ant.MessageResponse) ([]ant.Content, error)
221 CancelToolUse(toolUseID string, cause error) error
222}
223
224type Agent struct {
225 convo ConvoInterface
226 config AgentConfig // config for this agent
227 workingDir string
228 repoRoot string // workingDir may be a subdir of repoRoot
229 url string
230 lastHEAD string // hash of the last HEAD that was pushed to the host (only when under docker)
231 initialCommit string // hash of the Git HEAD when the agent was instantiated or Init()
232 gitRemoteAddr string // HTTP URL of the host git repo (only when under docker)
233 ready chan struct{} // closed when the agent is initialized (only when under docker)
234 startedAt time.Time
235 originalBudget ant.Budget
236 title string
237 codereview *claudetool.CodeReviewer
238
239 // Time when the current turn started (reset at the beginning of InnerLoop)
240 startOfTurn time.Time
241
242 // Inbox - for messages from the user to the agent.
243 // sent on by UserMessage
244 // . e.g. when user types into the chat textarea
245 // read from by GatherMessages
246 inbox chan string
247
248 // Outbox
249 // sent on by pushToOutbox
250 // via OnToolResult and OnResponse callbacks
251 // read from by WaitForMessage
252 // called by termui inside its repl loop.
253 outbox chan AgentMessage
254
255 // protects cancelInnerLoop
256 cancelInnerLoopMu sync.Mutex
257 // cancels potentially long-running tool_use calls or chains of them
258 cancelInnerLoop context.CancelCauseFunc
259
260 // protects following
261 mu sync.Mutex
262
263 // Stores all messages for this agent
264 history []AgentMessage
265
266 listeners []chan struct{}
267
268 // Track git commits we've already seen (by hash)
269 seenCommits map[string]bool
270}
271
272func (a *Agent) URL() string { return a.url }
273
274// Title returns the current title of the conversation.
275// If no title has been set, returns an empty string.
276func (a *Agent) Title() string {
277 a.mu.Lock()
278 defer a.mu.Unlock()
279 return a.title
280}
281
282// OS returns the operating system of the client.
283func (a *Agent) OS() string {
284 return a.config.ClientGOOS
285}
286
287// SetTitle sets the title of the conversation.
288func (a *Agent) SetTitle(title string) {
289 a.mu.Lock()
290 defer a.mu.Unlock()
291 a.title = title
292 // Notify all listeners that the state has changed
293 for _, ch := range a.listeners {
294 close(ch)
295 }
296 a.listeners = a.listeners[:0]
297}
298
299// OnToolResult implements ant.Listener.
300func (a *Agent) OnToolResult(ctx context.Context, convo *ant.Convo, toolName string, toolInput json.RawMessage, content ant.Content, result *string, err error) {
301 m := AgentMessage{
302 Type: ToolUseMessageType,
303 Content: content.Text,
304 ToolResult: content.ToolResult,
305 ToolError: content.ToolError,
306 ToolName: toolName,
307 ToolInput: string(toolInput),
308 ToolCallId: content.ToolUseID,
309 StartTime: content.StartTime,
310 EndTime: content.EndTime,
311 }
312
313 // Calculate the elapsed time if both start and end times are set
314 if content.StartTime != nil && content.EndTime != nil {
315 elapsed := content.EndTime.Sub(*content.StartTime)
316 m.Elapsed = &elapsed
317 }
318
319 m.ConversationID = convo.ID
320 if convo.Parent != nil {
321 m.ParentConversationID = &convo.Parent.ID
322 }
323 a.pushToOutbox(ctx, m)
324}
325
326// OnRequest implements ant.Listener.
327func (a *Agent) OnRequest(ctx context.Context, convo *ant.Convo, msg *ant.Message) {
328 // No-op.
329 // We already get tool results from the above. We send user messages to the outbox in the agent loop.
330}
331
332// OnResponse implements ant.Listener. Responses contain messages from the LLM
333// that need to be displayed (as well as tool calls that we send along when
334// they're done). (It would be reasonable to also mention tool calls when they're
335// started, but we don't do that yet.)
336func (a *Agent) OnResponse(ctx context.Context, convo *ant.Convo, resp *ant.MessageResponse) {
337 endOfTurn := false
338 if resp.StopReason != ant.StopReasonToolUse {
339 endOfTurn = true
340 }
341 m := AgentMessage{
342 Type: AgentMessageType,
343 Content: collectTextContent(resp),
344 EndOfTurn: endOfTurn,
345 Usage: &resp.Usage,
346 StartTime: resp.StartTime,
347 EndTime: resp.EndTime,
348 }
349
350 // Extract any tool calls from the response
351 if resp.StopReason == ant.StopReasonToolUse {
352 var toolCalls []ToolCall
353 for _, part := range resp.Content {
354 if part.Type == "tool_use" {
355 toolCalls = append(toolCalls, ToolCall{
356 Name: part.ToolName,
357 Input: string(part.ToolInput),
358 ToolCallId: part.ID,
359 })
360 }
361 }
362 m.ToolCalls = toolCalls
363 }
364
365 // Calculate the elapsed time if both start and end times are set
366 if resp.StartTime != nil && resp.EndTime != nil {
367 elapsed := resp.EndTime.Sub(*resp.StartTime)
368 m.Elapsed = &elapsed
369 }
370
371 m.ConversationID = convo.ID
372 if convo.Parent != nil {
373 m.ParentConversationID = &convo.Parent.ID
374 }
375 a.pushToOutbox(ctx, m)
376}
377
378// WorkingDir implements CodingAgent.
379func (a *Agent) WorkingDir() string {
380 return a.workingDir
381}
382
383// MessageCount implements CodingAgent.
384func (a *Agent) MessageCount() int {
385 a.mu.Lock()
386 defer a.mu.Unlock()
387 return len(a.history)
388}
389
390// Messages implements CodingAgent.
391func (a *Agent) Messages(start int, end int) []AgentMessage {
392 a.mu.Lock()
393 defer a.mu.Unlock()
394 return slices.Clone(a.history[start:end])
395}
396
397func (a *Agent) OriginalBudget() ant.Budget {
398 return a.originalBudget
399}
400
401// AgentConfig contains configuration for creating a new Agent.
402type AgentConfig struct {
403 Context context.Context
404 AntURL string
405 APIKey string
406 HTTPC *http.Client
407 Budget ant.Budget
408 GitUsername string
409 GitEmail string
410 SessionID string
411 ClientGOOS string
412 ClientGOARCH string
413 UseAnthropicEdit bool
414}
415
416// NewAgent creates a new Agent.
417// It is not usable until Init() is called.
418func NewAgent(config AgentConfig) *Agent {
419 agent := &Agent{
420 config: config,
421 ready: make(chan struct{}),
422 inbox: make(chan string, 100),
423 outbox: make(chan AgentMessage, 100),
424 startedAt: time.Now(),
425 originalBudget: config.Budget,
426 seenCommits: make(map[string]bool),
427 }
428 return agent
429}
430
431type AgentInit struct {
432 WorkingDir string
433 NoGit bool // only for testing
434
435 InDocker bool
436 Commit string
437 GitRemoteAddr string
438 HostAddr string
439}
440
441func (a *Agent) Init(ini AgentInit) error {
442 ctx := a.config.Context
443 if ini.InDocker {
444 cmd := exec.CommandContext(ctx, "git", "stash")
445 cmd.Dir = ini.WorkingDir
446 if out, err := cmd.CombinedOutput(); err != nil {
447 return fmt.Errorf("git stash: %s: %v", out, err)
448 }
Philip Zeyligerd0ac1ea2025-04-21 20:04:19 -0700449 cmd = exec.CommandContext(ctx, "git", "remote", "add", "sketch-host", ini.GitRemoteAddr)
450 cmd.Dir = ini.WorkingDir
451 if out, err := cmd.CombinedOutput(); err != nil {
452 return fmt.Errorf("git remote add: %s: %v", out, err)
453 }
454 cmd = exec.CommandContext(ctx, "git", "fetch", "sketch-host")
Earl Lee2e463fb2025-04-17 11:22:22 -0700455 cmd.Dir = ini.WorkingDir
456 if out, err := cmd.CombinedOutput(); err != nil {
457 return fmt.Errorf("git fetch: %s: %w", out, err)
458 }
459 cmd = exec.CommandContext(ctx, "git", "checkout", "-f", ini.Commit)
460 cmd.Dir = ini.WorkingDir
461 if out, err := cmd.CombinedOutput(); err != nil {
462 return fmt.Errorf("git checkout %s: %s: %w", ini.Commit, out, err)
463 }
Earl Lee2e463fb2025-04-17 11:22:22 -0700464 a.lastHEAD = ini.Commit
465 a.gitRemoteAddr = ini.GitRemoteAddr
466 a.initialCommit = ini.Commit
467 if ini.HostAddr != "" {
468 a.url = "http://" + ini.HostAddr
469 }
470 }
471 a.workingDir = ini.WorkingDir
472
473 if !ini.NoGit {
474 repoRoot, err := repoRoot(ctx, a.workingDir)
475 if err != nil {
476 return fmt.Errorf("repoRoot: %w", err)
477 }
478 a.repoRoot = repoRoot
479
480 commitHash, err := resolveRef(ctx, a.repoRoot, "HEAD")
481 if err != nil {
482 return fmt.Errorf("resolveRef: %w", err)
483 }
484 a.initialCommit = commitHash
485
486 codereview, err := claudetool.NewCodeReviewer(ctx, a.repoRoot, a.initialCommit)
487 if err != nil {
488 return fmt.Errorf("Agent.Init: claudetool.NewCodeReviewer: %w", err)
489 }
490 a.codereview = codereview
491 }
492 a.lastHEAD = a.initialCommit
493 a.convo = a.initConvo()
494 close(a.ready)
495 return nil
496}
497
498// initConvo initializes the conversation.
499// It must not be called until all agent fields are initialized,
500// particularly workingDir and git.
501func (a *Agent) initConvo() *ant.Convo {
502 ctx := a.config.Context
503 convo := ant.NewConvo(ctx, a.config.APIKey)
504 if a.config.HTTPC != nil {
505 convo.HTTPC = a.config.HTTPC
506 }
507 if a.config.AntURL != "" {
508 convo.URL = a.config.AntURL
509 }
510 convo.PromptCaching = true
511 convo.Budget = a.config.Budget
512
513 var editPrompt string
514 if a.config.UseAnthropicEdit {
515 editPrompt = "Then use the str_replace_editor tool to make those edits. For short complete file replacements, you may use the bash tool with cat and heredoc stdin."
516 } else {
517 editPrompt = "Then use the patch tool to make those edits. Combine all edits to any given file into a single patch tool call."
518 }
519
520 convo.SystemPrompt = fmt.Sprintf(`
521You are an expert coding assistant and architect, with a specialty in Go.
522You are assisting the user to achieve their goals.
523
524Start by asking concise clarifying questions as needed.
525Once the intent is clear, work autonomously.
526
527Call the title tool early in the conversation to provide a brief summary of
528what the chat is about.
529
530Break down the overall goal into a series of smaller steps.
531(The first step is often: "Make a plan.")
532Then execute each step using tools.
533Update the plan if you have encountered problems or learned new information.
534
535When in doubt about a step, follow this broad workflow:
536
537- Think about how the current step fits into the overall plan.
538- Do research. Good tool choices: bash, think, keyword_search
539- Make edits.
540- Repeat.
541
542To make edits reliably and efficiently, first think about the intent of the edit,
543and what set of patches will achieve that intent.
544%s
545
546For renames or refactors, consider invoking gopls (via bash).
547
548The done tool provides a checklist of items you MUST verify and
549review before declaring that you are done. Before executing
550the done tool, run all the tools the done tool checklist asks
551for, including creating a git commit. Do not forget to run tests.
552
553<platform>
554%s/%s
555</platform>
556<pwd>
557%v
558</pwd>
559<git_root>
560%v
561</git_root>
562`, editPrompt, a.config.ClientGOOS, a.config.ClientGOARCH, a.workingDir, a.repoRoot)
563
564 // Register all tools with the conversation
565 // When adding, removing, or modifying tools here, double-check that the termui tool display
566 // template in termui/termui.go has pretty-printing support for all tools.
567 convo.Tools = []*ant.Tool{
568 claudetool.Bash, claudetool.Keyword,
569 claudetool.Think, a.titleTool(), makeDoneTool(a.codereview, a.config.GitUsername, a.config.GitEmail),
570 a.codereview.Tool(),
571 }
572 if a.config.UseAnthropicEdit {
573 convo.Tools = append(convo.Tools, claudetool.AnthropicEditTool)
574 } else {
575 convo.Tools = append(convo.Tools, claudetool.Patch)
576 }
577 convo.Listener = a
578 return convo
579}
580
581func (a *Agent) titleTool() *ant.Tool {
582 // titleTool creates the title tool that sets the conversation title.
583 title := &ant.Tool{
584 Name: "title",
585 Description: `Use this tool early in the conversation, BEFORE MAKING ANY GIT COMMITS, to summarize what the chat is about briefly.`,
586 InputSchema: json.RawMessage(`{
587 "type": "object",
588 "properties": {
589 "title": {
590 "type": "string",
591 "description": "A brief title summarizing what this chat is about"
592 }
593 },
594 "required": ["title"]
595}`),
596 Run: func(ctx context.Context, input json.RawMessage) (string, error) {
597 var params struct {
598 Title string `json:"title"`
599 }
600 if err := json.Unmarshal(input, &params); err != nil {
601 return "", err
602 }
603 a.SetTitle(params.Title)
604 return fmt.Sprintf("Title set to: %s", params.Title), nil
605 },
606 }
607 return title
608}
609
610func (a *Agent) Ready() <-chan struct{} {
611 return a.ready
612}
613
614func (a *Agent) UserMessage(ctx context.Context, msg string) {
615 a.pushToOutbox(ctx, AgentMessage{Type: UserMessageType, Content: msg})
616 a.inbox <- msg
617}
618
619func (a *Agent) WaitForMessage(ctx context.Context) AgentMessage {
620 // TODO: Should this drain any outbox messages in case there are multiple?
621 select {
622 case msg := <-a.outbox:
623 return msg
624 case <-ctx.Done():
625 return errorMessage(ctx.Err())
626 }
627}
628
629func (a *Agent) CancelToolUse(toolUseID string, cause error) error {
630 return a.convo.CancelToolUse(toolUseID, cause)
631}
632
633func (a *Agent) CancelInnerLoop(cause error) {
634 a.cancelInnerLoopMu.Lock()
635 defer a.cancelInnerLoopMu.Unlock()
636 if a.cancelInnerLoop != nil {
637 a.cancelInnerLoop(cause)
638 }
639}
640
641func (a *Agent) Loop(ctxOuter context.Context) {
642 for {
643 select {
644 case <-ctxOuter.Done():
645 return
646 default:
647 ctxInner, cancel := context.WithCancelCause(ctxOuter)
648 a.cancelInnerLoopMu.Lock()
649 // Set .cancelInnerLoop so the user can cancel whatever is happening
650 // inside InnerLoop(ctxInner) without canceling this outer Loop execution.
651 // This CancelInnerLoop func is intended be called from other goroutines,
652 // hence the mutex.
653 a.cancelInnerLoop = cancel
654 a.cancelInnerLoopMu.Unlock()
655 a.InnerLoop(ctxInner)
656 cancel(nil)
657 }
658 }
659}
660
661func (a *Agent) pushToOutbox(ctx context.Context, m AgentMessage) {
662 if m.Timestamp.IsZero() {
663 m.Timestamp = time.Now()
664 }
665
666 // If this is an end-of-turn message, calculate the turn duration and add it to the message
667 if m.EndOfTurn && m.Type == AgentMessageType {
668 turnDuration := time.Since(a.startOfTurn)
669 m.TurnDuration = &turnDuration
670 slog.InfoContext(ctx, "Turn completed", "turnDuration", turnDuration)
671 }
672
673 slog.InfoContext(ctx, "agent message", m.Attr())
674
675 a.mu.Lock()
676 defer a.mu.Unlock()
677 m.Idx = len(a.history)
678 a.history = append(a.history, m)
679 a.outbox <- m
680
681 // Notify all listeners:
682 for _, ch := range a.listeners {
683 close(ch)
684 }
685 a.listeners = a.listeners[:0]
686}
687
688func (a *Agent) GatherMessages(ctx context.Context, block bool) ([]ant.Content, error) {
689 var m []ant.Content
690 if block {
691 select {
692 case <-ctx.Done():
693 return m, ctx.Err()
694 case msg := <-a.inbox:
695 m = append(m, ant.Content{Type: "text", Text: msg})
696 }
697 }
698 for {
699 select {
700 case msg := <-a.inbox:
701 m = append(m, ant.Content{Type: "text", Text: msg})
702 default:
703 return m, nil
704 }
705 }
706}
707
708func (a *Agent) InnerLoop(ctx context.Context) {
709 // Reset the start of turn time
710 a.startOfTurn = time.Now()
711
712 // Wait for at least one message from the user.
713 msgs, err := a.GatherMessages(ctx, true)
714 if err != nil { // e.g. the context was canceled while blocking in GatherMessages
715 return
716 }
717 // We do this as we go, but let's also do it at the end of the turn
718 defer func() {
719 if _, err := a.handleGitCommits(ctx); err != nil {
720 // Just log the error, don't stop execution
721 slog.WarnContext(ctx, "Failed to check for new git commits", "error", err)
722 }
723 }()
724
725 userMessage := ant.Message{
726 Role: "user",
727 Content: msgs,
728 }
729 // convo.SendMessage does the actual network call to send this to anthropic. This blocks until the response is ready.
730 // TODO: pass ctx to SendMessage, and figure out how to square that ctx with convo's own .Ctx. Who owns the scope of this call?
731 resp, err := a.convo.SendMessage(userMessage)
732 if err != nil {
733 a.pushToOutbox(ctx, errorMessage(err))
734 return
735 }
736 for {
737 // TODO: here and below where we check the budget,
738 // we should review the UX: is it clear what happened?
739 // is it clear how to resume?
740 // should we let the user set a new budget?
741 if err := a.overBudget(ctx); err != nil {
742 return
743 }
744 if resp.StopReason != ant.StopReasonToolUse {
745 break
746 }
747 var results []ant.Content
748 cancelled := false
749 select {
750 case <-ctx.Done():
751 // Don't actually run any of the tools, but rather build a response
752 // for each tool_use message letting the LLM know that user canceled it.
753 results, err = a.convo.ToolResultCancelContents(resp)
754 if err != nil {
755 a.pushToOutbox(ctx, errorMessage(err))
756 }
757 cancelled = true
758 default:
759 ctx = claudetool.WithWorkingDir(ctx, a.workingDir)
760 // fall-through, when the user has not canceled the inner loop:
761 results, err = a.convo.ToolResultContents(ctx, resp)
762 if ctx.Err() != nil { // e.g. the user canceled the operation
763 cancelled = true
764 } else if err != nil {
765 a.pushToOutbox(ctx, errorMessage(err))
766 }
767 }
768
769 // Check for git commits. Currently we do this here, after we collect
770 // tool results, since that's when we know commits could have happened.
771 // We could instead do this when the turn ends, but I think it makes sense
772 // to do this as we go.
773 newCommits, err := a.handleGitCommits(ctx)
774 if err != nil {
775 // Just log the error, don't stop execution
776 slog.WarnContext(ctx, "Failed to check for new git commits", "error", err)
777 }
778 var autoqualityMessages []string
779 if len(newCommits) == 1 {
780 formatted := a.codereview.Autoformat(ctx)
781 if len(formatted) > 0 {
782 msg := fmt.Sprintf(`
783I ran autoformatters and they updated these files:
784
785%s
786
787Please amend your latest git commit with these changes and then continue with what you were doing.`,
788 strings.Join(formatted, "\n"),
789 )[1:]
790 a.pushToOutbox(ctx, AgentMessage{
791 Type: AutoMessageType,
792 Content: msg,
793 Timestamp: time.Now(),
794 })
795 autoqualityMessages = append(autoqualityMessages, msg)
796 }
797 }
798
799 if err := a.overBudget(ctx); err != nil {
800 return
801 }
802
803 // Include, along with the tool results (which must go first for whatever reason),
804 // any messages that the user has sent along while the tool_use was executing concurrently.
805 msgs, err = a.GatherMessages(ctx, false)
806 if err != nil {
807 return
808 }
809 // Inject any auto-generated messages from quality checks.
810 for _, msg := range autoqualityMessages {
811 msgs = append(msgs, ant.Content{Type: "text", Text: msg})
812 }
813 if cancelled {
814 msgs = append(msgs, ant.Content{Type: "text", Text: cancelToolUseMessage})
815 // EndOfTurn is false here so that the client of this agent keeps processing
816 // messages from WaitForMessage() and gets the response from the LLM (usually
817 // something like "okay, I'll wait further instructions", but the user should
818 // be made aware of it regardless).
819 a.pushToOutbox(ctx, AgentMessage{Type: ErrorMessageType, Content: userCancelMessage, EndOfTurn: false})
820 } else if err := a.convo.OverBudget(); err != nil {
821 budgetMsg := "We've exceeded our budget. Please ask the user to confirm before continuing by ending the turn."
822 msgs = append(msgs, ant.Content{Type: "text", Text: budgetMsg})
823 a.pushToOutbox(ctx, budgetMessage(fmt.Errorf("warning: %w (ask to keep trying, if you'd like)", err)))
824 }
825 results = append(results, msgs...)
826 resp, err = a.convo.SendMessage(ant.Message{
827 Role: "user",
828 Content: results,
829 })
830 if err != nil {
831 a.pushToOutbox(ctx, errorMessage(fmt.Errorf("error: failed to continue conversation: %s", err.Error())))
832 break
833 }
834 if cancelled {
835 return
836 }
837 }
838}
839
840func (a *Agent) overBudget(ctx context.Context) error {
841 if err := a.convo.OverBudget(); err != nil {
842 m := budgetMessage(err)
843 m.Content = m.Content + "\n\nBudget reset."
844 a.pushToOutbox(ctx, budgetMessage(err))
845 a.convo.ResetBudget(a.originalBudget)
846 return err
847 }
848 return nil
849}
850
851func collectTextContent(msg *ant.MessageResponse) string {
852 // Collect all text content
853 var allText strings.Builder
854 for _, content := range msg.Content {
855 if content.Type == "text" && content.Text != "" {
856 if allText.Len() > 0 {
857 allText.WriteString("\n\n")
858 }
859 allText.WriteString(content.Text)
860 }
861 }
862 return allText.String()
863}
864
865func (a *Agent) TotalUsage() ant.CumulativeUsage {
866 a.mu.Lock()
867 defer a.mu.Unlock()
868 return a.convo.CumulativeUsage()
869}
870
871// WaitForMessageCount returns when the agent has at more than clientMessageCount messages or the context is done.
872func (a *Agent) WaitForMessageCount(ctx context.Context, greaterThan int) {
873 for a.MessageCount() <= greaterThan {
874 a.mu.Lock()
875 ch := make(chan struct{})
876 // Deletion happens when we notify.
877 a.listeners = append(a.listeners, ch)
878 a.mu.Unlock()
879
880 select {
881 case <-ctx.Done():
882 return
883 case <-ch:
884 continue
885 }
886 }
887}
888
889// Diff returns a unified diff of changes made since the agent was instantiated.
890func (a *Agent) Diff(commit *string) (string, error) {
891 if a.initialCommit == "" {
892 return "", fmt.Errorf("no initial commit reference available")
893 }
894
895 // Find the repository root
896 ctx := context.Background()
897
898 // If a specific commit hash is provided, show just that commit's changes
899 if commit != nil && *commit != "" {
900 // Validate that the commit looks like a valid git SHA
901 if !isValidGitSHA(*commit) {
902 return "", fmt.Errorf("invalid git commit SHA format: %s", *commit)
903 }
904
905 // Get the diff for just this commit
906 cmd := exec.CommandContext(ctx, "git", "show", "--unified=10", *commit)
907 cmd.Dir = a.repoRoot
908 output, err := cmd.CombinedOutput()
909 if err != nil {
910 return "", fmt.Errorf("failed to get diff for commit %s: %w - %s", *commit, err, string(output))
911 }
912 return string(output), nil
913 }
914
915 // Otherwise, get the diff between the initial commit and the current state using exec.Command
916 cmd := exec.CommandContext(ctx, "git", "diff", "--unified=10", a.initialCommit)
917 cmd.Dir = a.repoRoot
918 output, err := cmd.CombinedOutput()
919 if err != nil {
920 return "", fmt.Errorf("failed to get diff: %w - %s", err, string(output))
921 }
922
923 return string(output), nil
924}
925
926// InitialCommit returns the Git commit hash that was saved when the agent was instantiated.
927func (a *Agent) InitialCommit() string {
928 return a.initialCommit
929}
930
931// handleGitCommits() highlights new commits to the user. When running
932// under docker, new HEADs are pushed to a branch according to the title.
933func (a *Agent) handleGitCommits(ctx context.Context) ([]*GitCommit, error) {
934 if a.repoRoot == "" {
935 return nil, nil
936 }
937
938 head, err := resolveRef(ctx, a.repoRoot, "HEAD")
939 if err != nil {
940 return nil, err
941 }
942 if head == a.lastHEAD {
943 return nil, nil // nothing to do
944 }
945 defer func() {
946 a.lastHEAD = head
947 }()
948
949 // Get new commits. Because it's possible that the agent does rebases, fixups, and
950 // so forth, we use, as our fixed point, the "initialCommit", and we limit ourselves
951 // to the last 100 commits.
952 var commits []*GitCommit
953
954 // Get commits since the initial commit
955 // Format: <hash>\0<subject>\0<body>\0
956 // This uses NULL bytes as separators to avoid issues with newlines in commit messages
957 // Limit to 100 commits to avoid overwhelming the user
958 cmd := exec.CommandContext(ctx, "git", "log", "-n", "100", "--pretty=format:%H%x00%s%x00%b%x00", "^"+a.initialCommit, head)
959 cmd.Dir = a.repoRoot
960 output, err := cmd.Output()
961 if err != nil {
962 return nil, fmt.Errorf("failed to get git log: %w", err)
963 }
964
965 // Parse git log output and filter out already seen commits
966 parsedCommits := parseGitLog(string(output))
967
968 var headCommit *GitCommit
969
970 // Filter out commits we've already seen
971 for _, commit := range parsedCommits {
972 if commit.Hash == head {
973 headCommit = &commit
974 }
975
976 // Skip if we've seen this commit before. If our head has changed, always include that.
977 if a.seenCommits[commit.Hash] && commit.Hash != head {
978 continue
979 }
980
981 // Mark this commit as seen
982 a.seenCommits[commit.Hash] = true
983
984 // Add to our list of new commits
985 commits = append(commits, &commit)
986 }
987
988 if a.gitRemoteAddr != "" {
989 if headCommit == nil {
990 // I think this can only happen if we have a bug or if there's a race.
991 headCommit = &GitCommit{}
992 headCommit.Hash = head
993 headCommit.Subject = "unknown"
994 commits = append(commits, headCommit)
995 }
996
997 cleanTitle := titleToBranch(a.title)
998 if cleanTitle == "" {
999 cleanTitle = a.config.SessionID
1000 }
1001 branch := "sketch/" + cleanTitle
1002
1003 // TODO: I don't love the force push here. We could see if the push is a fast-forward, and,
1004 // if it's not, we could make a backup with a unique name (perhaps append a timestamp) and
1005 // then use push with lease to replace.
1006 cmd = exec.Command("git", "push", "--force", a.gitRemoteAddr, "HEAD:refs/heads/"+branch)
1007 cmd.Dir = a.workingDir
1008 if out, err := cmd.CombinedOutput(); err != nil {
1009 a.pushToOutbox(ctx, errorMessage(fmt.Errorf("git push to host: %s: %v", out, err)))
1010 } else {
1011 headCommit.PushedBranch = branch
1012 }
1013 }
1014
1015 // If we found new commits, create a message
1016 if len(commits) > 0 {
1017 msg := AgentMessage{
1018 Type: CommitMessageType,
1019 Timestamp: time.Now(),
1020 Commits: commits,
1021 }
1022 a.pushToOutbox(ctx, msg)
1023 }
1024 return commits, nil
1025}
1026
1027func titleToBranch(s string) string {
1028 // Convert to lowercase
1029 s = strings.ToLower(s)
1030
1031 // Replace spaces with hyphens
1032 s = strings.ReplaceAll(s, " ", "-")
1033
1034 // Remove any character that isn't a-z or hyphen
1035 var result strings.Builder
1036 for _, r := range s {
1037 if (r >= 'a' && r <= 'z') || r == '-' {
1038 result.WriteRune(r)
1039 }
1040 }
1041 return result.String()
1042}
1043
1044// parseGitLog parses the output of git log with format '%H%x00%s%x00%b%x00'
1045// and returns an array of GitCommit structs.
1046func parseGitLog(output string) []GitCommit {
1047 var commits []GitCommit
1048
1049 // No output means no commits
1050 if len(output) == 0 {
1051 return commits
1052 }
1053
1054 // Split by NULL byte
1055 parts := strings.Split(output, "\x00")
1056
1057 // Process in triplets (hash, subject, body)
1058 for i := 0; i < len(parts); i++ {
1059 // Skip empty parts
1060 if parts[i] == "" {
1061 continue
1062 }
1063
1064 // This should be a hash
1065 hash := strings.TrimSpace(parts[i])
1066
1067 // Make sure we have at least a subject part available
1068 if i+1 >= len(parts) {
1069 break // No more parts available
1070 }
1071
1072 // Get the subject
1073 subject := strings.TrimSpace(parts[i+1])
1074
1075 // Get the body if available
1076 body := ""
1077 if i+2 < len(parts) {
1078 body = strings.TrimSpace(parts[i+2])
1079 }
1080
1081 // Skip to the next triplet
1082 i += 2
1083
1084 commits = append(commits, GitCommit{
1085 Hash: hash,
1086 Subject: subject,
1087 Body: body,
1088 })
1089 }
1090
1091 return commits
1092}
1093
1094func repoRoot(ctx context.Context, dir string) (string, error) {
1095 cmd := exec.CommandContext(ctx, "git", "rev-parse", "--show-toplevel")
1096 stderr := new(strings.Builder)
1097 cmd.Stderr = stderr
1098 cmd.Dir = dir
1099 out, err := cmd.Output()
1100 if err != nil {
1101 return "", fmt.Errorf("git rev-parse failed: %w\n%s", err, stderr)
1102 }
1103 return strings.TrimSpace(string(out)), nil
1104}
1105
1106func resolveRef(ctx context.Context, dir, refName string) (string, error) {
1107 cmd := exec.CommandContext(ctx, "git", "rev-parse", refName)
1108 stderr := new(strings.Builder)
1109 cmd.Stderr = stderr
1110 cmd.Dir = dir
1111 out, err := cmd.Output()
1112 if err != nil {
1113 return "", fmt.Errorf("git rev-parse failed: %w\n%s", err, stderr)
1114 }
1115 // TODO: validate that out is valid hex
1116 return strings.TrimSpace(string(out)), nil
1117}
1118
1119// isValidGitSHA validates if a string looks like a valid git SHA hash.
1120// Git SHAs are hexadecimal strings of at least 4 characters but typically 7, 8, or 40 characters.
1121func isValidGitSHA(sha string) bool {
1122 // Git SHA must be a hexadecimal string with at least 4 characters
1123 if len(sha) < 4 || len(sha) > 40 {
1124 return false
1125 }
1126
1127 // Check if the string only contains hexadecimal characters
1128 for _, char := range sha {
1129 if !(char >= '0' && char <= '9') && !(char >= 'a' && char <= 'f') && !(char >= 'A' && char <= 'F') {
1130 return false
1131 }
1132 }
1133
1134 return true
1135}