blob: d0f1ce7d813154be5622f323247623b27bfa7e52 [file] [log] [blame]
Earl Lee2e463fb2025-04-17 11:22:22 -07001package loop
2
3import (
Josh Bleecher Snydera9b38222025-04-29 18:05:06 -07004 "cmp"
Earl Lee2e463fb2025-04-17 11:22:22 -07005 "context"
Josh Bleecher Snyderdbe02302025-04-29 16:44:23 -07006 _ "embed"
Earl Lee2e463fb2025-04-17 11:22:22 -07007 "encoding/json"
8 "fmt"
9 "log/slog"
10 "net/http"
11 "os"
12 "os/exec"
13 "runtime/debug"
14 "slices"
15 "strings"
16 "sync"
17 "time"
18
19 "sketch.dev/ant"
20 "sketch.dev/claudetool"
Josh Bleecher Snyderd499fd62025-04-30 01:31:29 +000021 "sketch.dev/claudetool/bashkit"
Earl Lee2e463fb2025-04-17 11:22:22 -070022)
23
24const (
25 userCancelMessage = "user requested agent to stop handling responses"
26)
27
28type CodingAgent interface {
29 // Init initializes an agent inside a docker container.
30 Init(AgentInit) error
31
32 // Ready returns a channel closed after Init successfully called.
33 Ready() <-chan struct{}
34
35 // URL reports the HTTP URL of this agent.
36 URL() string
37
38 // UserMessage enqueues a message to the agent and returns immediately.
39 UserMessage(ctx context.Context, msg string)
40
41 // WaitForMessage blocks until the agent has a response to give.
42 // Use AgentMessage.EndOfTurn to help determine if you want to
43 // drain the agent.
44 WaitForMessage(ctx context.Context) AgentMessage
45
46 // Loop begins the agent loop returns only when ctx is cancelled.
47 Loop(ctx context.Context)
48
49 CancelInnerLoop(cause error)
50
51 CancelToolUse(toolUseID string, cause error) error
52
53 // Returns a subset of the agent's message history.
54 Messages(start int, end int) []AgentMessage
55
56 // Returns the current number of messages in the history
57 MessageCount() int
58
59 TotalUsage() ant.CumulativeUsage
60 OriginalBudget() ant.Budget
61
62 // WaitForMessageCount returns when the agent has at more than clientMessageCount messages or the context is done.
63 WaitForMessageCount(ctx context.Context, greaterThan int)
64
65 WorkingDir() string
66
67 // Diff returns a unified diff of changes made since the agent was instantiated.
68 // If commit is non-nil, it shows the diff for just that specific commit.
69 Diff(commit *string) (string, error)
70
71 // InitialCommit returns the Git commit hash that was saved when the agent was instantiated.
72 InitialCommit() string
73
74 // Title returns the current title of the conversation.
75 Title() string
76
Josh Bleecher Snyder47b19362025-04-30 01:34:14 +000077 // BranchName returns the git branch name for the conversation.
78 BranchName() string
79
Earl Lee2e463fb2025-04-17 11:22:22 -070080 // OS returns the operating system of the client.
81 OS() string
Philip Zeyliger99a9a022025-04-27 15:15:25 +000082
Philip Zeyligerc72fff52025-04-29 20:17:54 +000083 // SessionID returns the unique session identifier.
84 SessionID() string
85
Philip Zeyliger99a9a022025-04-27 15:15:25 +000086 // OutstandingLLMCallCount returns the number of outstanding LLM calls.
87 OutstandingLLMCallCount() int
88
89 // OutstandingToolCalls returns the names of outstanding tool calls.
90 OutstandingToolCalls() []string
Philip Zeyliger18532b22025-04-23 21:11:46 +000091 OutsideOS() string
92 OutsideHostname() string
93 OutsideWorkingDir() string
Philip Zeyligerd1402952025-04-23 03:54:37 +000094 GitOrigin() string
Earl Lee2e463fb2025-04-17 11:22:22 -070095}
96
97type CodingAgentMessageType string
98
99const (
100 UserMessageType CodingAgentMessageType = "user"
101 AgentMessageType CodingAgentMessageType = "agent"
102 ErrorMessageType CodingAgentMessageType = "error"
103 BudgetMessageType CodingAgentMessageType = "budget" // dedicated for "out of budget" errors
104 ToolUseMessageType CodingAgentMessageType = "tool"
105 CommitMessageType CodingAgentMessageType = "commit" // for displaying git commits
106 AutoMessageType CodingAgentMessageType = "auto" // for automated notifications like autoformatting
107
108 cancelToolUseMessage = "Stop responding to my previous message. Wait for me to ask you something else before attempting to use any more tools."
109)
110
111type AgentMessage struct {
112 Type CodingAgentMessageType `json:"type"`
113 // EndOfTurn indicates that the AI is done working and is ready for the next user input.
114 EndOfTurn bool `json:"end_of_turn"`
115
116 Content string `json:"content"`
117 ToolName string `json:"tool_name,omitempty"`
118 ToolInput string `json:"input,omitempty"`
119 ToolResult string `json:"tool_result,omitempty"`
120 ToolError bool `json:"tool_error,omitempty"`
121 ToolCallId string `json:"tool_call_id,omitempty"`
122
123 // ToolCalls is a list of all tool calls requested in this message (name and input pairs)
124 ToolCalls []ToolCall `json:"tool_calls,omitempty"`
125
Sean McCulloughd9f13372025-04-21 15:08:49 -0700126 // ToolResponses is a list of all responses to tool calls requested in this message (name and input pairs)
127 ToolResponses []AgentMessage `json:"toolResponses,omitempty"`
128
Earl Lee2e463fb2025-04-17 11:22:22 -0700129 // Commits is a list of git commits for a commit message
130 Commits []*GitCommit `json:"commits,omitempty"`
131
132 Timestamp time.Time `json:"timestamp"`
133 ConversationID string `json:"conversation_id"`
134 ParentConversationID *string `json:"parent_conversation_id,omitempty"`
135 Usage *ant.Usage `json:"usage,omitempty"`
136
137 // Message timing information
138 StartTime *time.Time `json:"start_time,omitempty"`
139 EndTime *time.Time `json:"end_time,omitempty"`
140 Elapsed *time.Duration `json:"elapsed,omitempty"`
141
142 // Turn duration - the time taken for a complete agent turn
143 TurnDuration *time.Duration `json:"turnDuration,omitempty"`
144
145 Idx int `json:"idx"`
146}
147
Josh Bleecher Snyder50a1d622025-04-29 09:59:03 -0700148// SetConvo sets m.ConversationID and m.ParentConversationID based on convo.
149func (m *AgentMessage) SetConvo(convo *ant.Convo) {
150 if convo == nil {
151 m.ConversationID = ""
152 m.ParentConversationID = nil
153 return
154 }
155 m.ConversationID = convo.ID
156 if convo.Parent != nil {
157 m.ParentConversationID = &convo.Parent.ID
158 }
159}
160
Earl Lee2e463fb2025-04-17 11:22:22 -0700161// GitCommit represents a single git commit for a commit message
162type GitCommit struct {
163 Hash string `json:"hash"` // Full commit hash
164 Subject string `json:"subject"` // Commit subject line
165 Body string `json:"body"` // Full commit message body
166 PushedBranch string `json:"pushed_branch,omitempty"` // If set, this commit was pushed to this branch
167}
168
169// ToolCall represents a single tool call within an agent message
170type ToolCall struct {
Sean McCulloughd9f13372025-04-21 15:08:49 -0700171 Name string `json:"name"`
172 Input string `json:"input"`
173 ToolCallId string `json:"tool_call_id"`
174 ResultMessage *AgentMessage `json:"result_message,omitempty"`
175 Args string `json:"args,omitempty"`
176 Result string `json:"result,omitempty"`
Earl Lee2e463fb2025-04-17 11:22:22 -0700177}
178
179func (a *AgentMessage) Attr() slog.Attr {
180 var attrs []any = []any{
181 slog.String("type", string(a.Type)),
182 }
183 if a.EndOfTurn {
184 attrs = append(attrs, slog.Bool("end_of_turn", a.EndOfTurn))
185 }
186 if a.Content != "" {
187 attrs = append(attrs, slog.String("content", a.Content))
188 }
189 if a.ToolName != "" {
190 attrs = append(attrs, slog.String("tool_name", a.ToolName))
191 }
192 if a.ToolInput != "" {
193 attrs = append(attrs, slog.String("tool_input", a.ToolInput))
194 }
195 if a.Elapsed != nil {
196 attrs = append(attrs, slog.Int64("elapsed", a.Elapsed.Nanoseconds()))
197 }
198 if a.TurnDuration != nil {
199 attrs = append(attrs, slog.Int64("turnDuration", a.TurnDuration.Nanoseconds()))
200 }
201 if a.ToolResult != "" {
202 attrs = append(attrs, slog.String("tool_result", a.ToolResult))
203 }
204 if a.ToolError {
205 attrs = append(attrs, slog.Bool("tool_error", a.ToolError))
206 }
207 if len(a.ToolCalls) > 0 {
208 toolCallAttrs := make([]any, 0, len(a.ToolCalls))
209 for i, tc := range a.ToolCalls {
210 toolCallAttrs = append(toolCallAttrs, slog.Group(
211 fmt.Sprintf("tool_call_%d", i),
212 slog.String("name", tc.Name),
213 slog.String("input", tc.Input),
214 ))
215 }
216 attrs = append(attrs, slog.Group("tool_calls", toolCallAttrs...))
217 }
218 if a.ConversationID != "" {
219 attrs = append(attrs, slog.String("convo_id", a.ConversationID))
220 }
221 if a.ParentConversationID != nil {
222 attrs = append(attrs, slog.String("parent_convo_id", *a.ParentConversationID))
223 }
224 if a.Usage != nil && !a.Usage.IsZero() {
225 attrs = append(attrs, a.Usage.Attr())
226 }
227 // TODO: timestamp, convo ids, idx?
228 return slog.Group("agent_message", attrs...)
229}
230
231func errorMessage(err error) AgentMessage {
232 // It's somewhat unknowable whether error messages are "end of turn" or not, but it seems like the best approach.
233 if os.Getenv(("DEBUG")) == "1" {
234 return AgentMessage{Type: ErrorMessageType, Content: err.Error() + " Stacktrace: " + string(debug.Stack()), EndOfTurn: true}
235 }
236
237 return AgentMessage{Type: ErrorMessageType, Content: err.Error(), EndOfTurn: true}
238}
239
240func budgetMessage(err error) AgentMessage {
241 return AgentMessage{Type: BudgetMessageType, Content: err.Error(), EndOfTurn: true}
242}
243
244// ConvoInterface defines the interface for conversation interactions
245type ConvoInterface interface {
246 CumulativeUsage() ant.CumulativeUsage
247 ResetBudget(ant.Budget)
248 OverBudget() error
249 SendMessage(message ant.Message) (*ant.MessageResponse, error)
250 SendUserTextMessage(s string, otherContents ...ant.Content) (*ant.MessageResponse, error)
251 ToolResultContents(ctx context.Context, resp *ant.MessageResponse) ([]ant.Content, error)
252 ToolResultCancelContents(resp *ant.MessageResponse) ([]ant.Content, error)
253 CancelToolUse(toolUseID string, cause error) error
254}
255
256type Agent struct {
257 convo ConvoInterface
258 config AgentConfig // config for this agent
259 workingDir string
260 repoRoot string // workingDir may be a subdir of repoRoot
261 url string
262 lastHEAD string // hash of the last HEAD that was pushed to the host (only when under docker)
263 initialCommit string // hash of the Git HEAD when the agent was instantiated or Init()
264 gitRemoteAddr string // HTTP URL of the host git repo (only when under docker)
265 ready chan struct{} // closed when the agent is initialized (only when under docker)
266 startedAt time.Time
267 originalBudget ant.Budget
268 title string
Josh Bleecher Snydera9b38222025-04-29 18:05:06 -0700269 branchName string
Earl Lee2e463fb2025-04-17 11:22:22 -0700270 codereview *claudetool.CodeReviewer
Philip Zeyliger18532b22025-04-23 21:11:46 +0000271 // Outside information
272 outsideHostname string
273 outsideOS string
274 outsideWorkingDir string
Philip Zeyligerd1402952025-04-23 03:54:37 +0000275 // URL of the git remote 'origin' if it exists
276 gitOrigin string
Earl Lee2e463fb2025-04-17 11:22:22 -0700277
278 // Time when the current turn started (reset at the beginning of InnerLoop)
279 startOfTurn time.Time
280
281 // Inbox - for messages from the user to the agent.
282 // sent on by UserMessage
283 // . e.g. when user types into the chat textarea
284 // read from by GatherMessages
285 inbox chan string
286
287 // Outbox
288 // sent on by pushToOutbox
289 // via OnToolResult and OnResponse callbacks
290 // read from by WaitForMessage
291 // called by termui inside its repl loop.
292 outbox chan AgentMessage
293
294 // protects cancelInnerLoop
295 cancelInnerLoopMu sync.Mutex
296 // cancels potentially long-running tool_use calls or chains of them
297 cancelInnerLoop context.CancelCauseFunc
298
299 // protects following
300 mu sync.Mutex
301
302 // Stores all messages for this agent
303 history []AgentMessage
304
305 listeners []chan struct{}
306
307 // Track git commits we've already seen (by hash)
308 seenCommits map[string]bool
Philip Zeyliger99a9a022025-04-27 15:15:25 +0000309
310 // Track outstanding LLM call IDs
311 outstandingLLMCalls map[string]struct{}
312
313 // Track outstanding tool calls by ID with their names
314 outstandingToolCalls map[string]string
Earl Lee2e463fb2025-04-17 11:22:22 -0700315}
316
317func (a *Agent) URL() string { return a.url }
318
319// Title returns the current title of the conversation.
320// If no title has been set, returns an empty string.
321func (a *Agent) Title() string {
322 a.mu.Lock()
323 defer a.mu.Unlock()
324 return a.title
325}
326
Josh Bleecher Snyder47b19362025-04-30 01:34:14 +0000327// BranchName returns the git branch name for the conversation.
328func (a *Agent) BranchName() string {
329 a.mu.Lock()
330 defer a.mu.Unlock()
331 return a.branchName
332}
333
Philip Zeyliger99a9a022025-04-27 15:15:25 +0000334// OutstandingLLMCallCount returns the number of outstanding LLM calls.
335func (a *Agent) OutstandingLLMCallCount() int {
336 a.mu.Lock()
337 defer a.mu.Unlock()
338 return len(a.outstandingLLMCalls)
339}
340
341// OutstandingToolCalls returns the names of outstanding tool calls.
342func (a *Agent) OutstandingToolCalls() []string {
343 a.mu.Lock()
344 defer a.mu.Unlock()
345
346 tools := make([]string, 0, len(a.outstandingToolCalls))
347 for _, toolName := range a.outstandingToolCalls {
348 tools = append(tools, toolName)
349 }
350 return tools
351}
352
Earl Lee2e463fb2025-04-17 11:22:22 -0700353// OS returns the operating system of the client.
354func (a *Agent) OS() string {
355 return a.config.ClientGOOS
356}
357
Philip Zeyligerc72fff52025-04-29 20:17:54 +0000358func (a *Agent) SessionID() string {
359 return a.config.SessionID
360}
361
Philip Zeyliger18532b22025-04-23 21:11:46 +0000362// OutsideOS returns the operating system of the outside system.
363func (a *Agent) OutsideOS() string {
364 return a.outsideOS
Philip Zeyligerd1402952025-04-23 03:54:37 +0000365}
366
Philip Zeyliger18532b22025-04-23 21:11:46 +0000367// OutsideHostname returns the hostname of the outside system.
368func (a *Agent) OutsideHostname() string {
369 return a.outsideHostname
Philip Zeyligerd1402952025-04-23 03:54:37 +0000370}
371
Philip Zeyliger18532b22025-04-23 21:11:46 +0000372// OutsideWorkingDir returns the working directory on the outside system.
373func (a *Agent) OutsideWorkingDir() string {
374 return a.outsideWorkingDir
Philip Zeyligerd1402952025-04-23 03:54:37 +0000375}
376
377// GitOrigin returns the URL of the git remote 'origin' if it exists.
378func (a *Agent) GitOrigin() string {
379 return a.gitOrigin
380}
381
Josh Bleecher Snydera9b38222025-04-29 18:05:06 -0700382// SetTitleBranch sets the title and branch name of the conversation.
383func (a *Agent) SetTitleBranch(title, branchName string) {
Earl Lee2e463fb2025-04-17 11:22:22 -0700384 a.mu.Lock()
385 defer a.mu.Unlock()
386 a.title = title
Josh Bleecher Snydera9b38222025-04-29 18:05:06 -0700387 a.branchName = branchName
Earl Lee2e463fb2025-04-17 11:22:22 -0700388 // Notify all listeners that the state has changed
389 for _, ch := range a.listeners {
390 close(ch)
391 }
392 a.listeners = a.listeners[:0]
393}
394
Philip Zeyliger99a9a022025-04-27 15:15:25 +0000395// OnToolCall implements ant.Listener and tracks the start of a tool call.
396func (a *Agent) OnToolCall(ctx context.Context, convo *ant.Convo, id string, toolName string, toolInput json.RawMessage, content ant.Content) {
397 // Track the tool call
398 a.mu.Lock()
399 a.outstandingToolCalls[id] = toolName
400 a.mu.Unlock()
401}
402
Earl Lee2e463fb2025-04-17 11:22:22 -0700403// OnToolResult implements ant.Listener.
Philip Zeyliger99a9a022025-04-27 15:15:25 +0000404func (a *Agent) OnToolResult(ctx context.Context, convo *ant.Convo, toolID string, toolName string, toolInput json.RawMessage, content ant.Content, result *string, err error) {
405 // Remove the tool call from outstanding calls
406 a.mu.Lock()
407 delete(a.outstandingToolCalls, toolID)
408 a.mu.Unlock()
409
Earl Lee2e463fb2025-04-17 11:22:22 -0700410 m := AgentMessage{
411 Type: ToolUseMessageType,
412 Content: content.Text,
413 ToolResult: content.ToolResult,
414 ToolError: content.ToolError,
415 ToolName: toolName,
416 ToolInput: string(toolInput),
417 ToolCallId: content.ToolUseID,
418 StartTime: content.StartTime,
419 EndTime: content.EndTime,
420 }
421
422 // Calculate the elapsed time if both start and end times are set
423 if content.StartTime != nil && content.EndTime != nil {
424 elapsed := content.EndTime.Sub(*content.StartTime)
425 m.Elapsed = &elapsed
426 }
427
Josh Bleecher Snyder50a1d622025-04-29 09:59:03 -0700428 m.SetConvo(convo)
Earl Lee2e463fb2025-04-17 11:22:22 -0700429 a.pushToOutbox(ctx, m)
430}
431
432// OnRequest implements ant.Listener.
Philip Zeyliger99a9a022025-04-27 15:15:25 +0000433func (a *Agent) OnRequest(ctx context.Context, convo *ant.Convo, id string, msg *ant.Message) {
434 a.mu.Lock()
435 defer a.mu.Unlock()
436 a.outstandingLLMCalls[id] = struct{}{}
Earl Lee2e463fb2025-04-17 11:22:22 -0700437 // We already get tool results from the above. We send user messages to the outbox in the agent loop.
438}
439
440// OnResponse implements ant.Listener. Responses contain messages from the LLM
441// that need to be displayed (as well as tool calls that we send along when
442// they're done). (It would be reasonable to also mention tool calls when they're
443// started, but we don't do that yet.)
Philip Zeyliger99a9a022025-04-27 15:15:25 +0000444func (a *Agent) OnResponse(ctx context.Context, convo *ant.Convo, id string, resp *ant.MessageResponse) {
445 // Remove the LLM call from outstanding calls
446 a.mu.Lock()
447 delete(a.outstandingLLMCalls, id)
448 a.mu.Unlock()
449
Josh Bleecher Snyder50a1d622025-04-29 09:59:03 -0700450 if resp == nil {
451 // LLM API call failed
452 m := AgentMessage{
453 Type: ErrorMessageType,
454 Content: "API call failed, type 'continue' to try again",
455 }
456 m.SetConvo(convo)
457 a.pushToOutbox(ctx, m)
458 return
459 }
460
Earl Lee2e463fb2025-04-17 11:22:22 -0700461 endOfTurn := false
462 if resp.StopReason != ant.StopReasonToolUse {
463 endOfTurn = true
464 }
465 m := AgentMessage{
466 Type: AgentMessageType,
467 Content: collectTextContent(resp),
468 EndOfTurn: endOfTurn,
469 Usage: &resp.Usage,
470 StartTime: resp.StartTime,
471 EndTime: resp.EndTime,
472 }
473
474 // Extract any tool calls from the response
475 if resp.StopReason == ant.StopReasonToolUse {
476 var toolCalls []ToolCall
477 for _, part := range resp.Content {
478 if part.Type == "tool_use" {
479 toolCalls = append(toolCalls, ToolCall{
480 Name: part.ToolName,
481 Input: string(part.ToolInput),
482 ToolCallId: part.ID,
483 })
484 }
485 }
486 m.ToolCalls = toolCalls
487 }
488
489 // Calculate the elapsed time if both start and end times are set
490 if resp.StartTime != nil && resp.EndTime != nil {
491 elapsed := resp.EndTime.Sub(*resp.StartTime)
492 m.Elapsed = &elapsed
493 }
494
Josh Bleecher Snyder50a1d622025-04-29 09:59:03 -0700495 m.SetConvo(convo)
Earl Lee2e463fb2025-04-17 11:22:22 -0700496 a.pushToOutbox(ctx, m)
497}
498
499// WorkingDir implements CodingAgent.
500func (a *Agent) WorkingDir() string {
501 return a.workingDir
502}
503
504// MessageCount implements CodingAgent.
505func (a *Agent) MessageCount() int {
506 a.mu.Lock()
507 defer a.mu.Unlock()
508 return len(a.history)
509}
510
511// Messages implements CodingAgent.
512func (a *Agent) Messages(start int, end int) []AgentMessage {
513 a.mu.Lock()
514 defer a.mu.Unlock()
515 return slices.Clone(a.history[start:end])
516}
517
518func (a *Agent) OriginalBudget() ant.Budget {
519 return a.originalBudget
520}
521
522// AgentConfig contains configuration for creating a new Agent.
523type AgentConfig struct {
524 Context context.Context
525 AntURL string
526 APIKey string
527 HTTPC *http.Client
528 Budget ant.Budget
529 GitUsername string
530 GitEmail string
531 SessionID string
532 ClientGOOS string
533 ClientGOARCH string
534 UseAnthropicEdit bool
Philip Zeyliger18532b22025-04-23 21:11:46 +0000535 // Outside information
536 OutsideHostname string
537 OutsideOS string
538 OutsideWorkingDir string
Earl Lee2e463fb2025-04-17 11:22:22 -0700539}
540
541// NewAgent creates a new Agent.
542// It is not usable until Init() is called.
543func NewAgent(config AgentConfig) *Agent {
544 agent := &Agent{
Philip Zeyliger99a9a022025-04-27 15:15:25 +0000545 config: config,
546 ready: make(chan struct{}),
547 inbox: make(chan string, 100),
548 outbox: make(chan AgentMessage, 100),
549 startedAt: time.Now(),
550 originalBudget: config.Budget,
551 seenCommits: make(map[string]bool),
552 outsideHostname: config.OutsideHostname,
553 outsideOS: config.OutsideOS,
554 outsideWorkingDir: config.OutsideWorkingDir,
555 outstandingLLMCalls: make(map[string]struct{}),
556 outstandingToolCalls: make(map[string]string),
Earl Lee2e463fb2025-04-17 11:22:22 -0700557 }
558 return agent
559}
560
561type AgentInit struct {
562 WorkingDir string
563 NoGit bool // only for testing
564
565 InDocker bool
566 Commit string
567 GitRemoteAddr string
568 HostAddr string
569}
570
571func (a *Agent) Init(ini AgentInit) error {
Josh Bleecher Snyder9c07e1d2025-04-28 19:25:37 -0700572 if a.convo != nil {
573 return fmt.Errorf("Agent.Init: already initialized")
574 }
Earl Lee2e463fb2025-04-17 11:22:22 -0700575 ctx := a.config.Context
576 if ini.InDocker {
577 cmd := exec.CommandContext(ctx, "git", "stash")
578 cmd.Dir = ini.WorkingDir
579 if out, err := cmd.CombinedOutput(); err != nil {
580 return fmt.Errorf("git stash: %s: %v", out, err)
581 }
Philip Zeyligerd0ac1ea2025-04-21 20:04:19 -0700582 cmd = exec.CommandContext(ctx, "git", "remote", "add", "sketch-host", ini.GitRemoteAddr)
583 cmd.Dir = ini.WorkingDir
584 if out, err := cmd.CombinedOutput(); err != nil {
585 return fmt.Errorf("git remote add: %s: %v", out, err)
586 }
587 cmd = exec.CommandContext(ctx, "git", "fetch", "sketch-host")
Earl Lee2e463fb2025-04-17 11:22:22 -0700588 cmd.Dir = ini.WorkingDir
589 if out, err := cmd.CombinedOutput(); err != nil {
590 return fmt.Errorf("git fetch: %s: %w", out, err)
591 }
592 cmd = exec.CommandContext(ctx, "git", "checkout", "-f", ini.Commit)
593 cmd.Dir = ini.WorkingDir
594 if out, err := cmd.CombinedOutput(); err != nil {
595 return fmt.Errorf("git checkout %s: %s: %w", ini.Commit, out, err)
596 }
Earl Lee2e463fb2025-04-17 11:22:22 -0700597 a.lastHEAD = ini.Commit
598 a.gitRemoteAddr = ini.GitRemoteAddr
599 a.initialCommit = ini.Commit
600 if ini.HostAddr != "" {
601 a.url = "http://" + ini.HostAddr
602 }
603 }
604 a.workingDir = ini.WorkingDir
605
606 if !ini.NoGit {
607 repoRoot, err := repoRoot(ctx, a.workingDir)
608 if err != nil {
609 return fmt.Errorf("repoRoot: %w", err)
610 }
611 a.repoRoot = repoRoot
612
613 commitHash, err := resolveRef(ctx, a.repoRoot, "HEAD")
614 if err != nil {
615 return fmt.Errorf("resolveRef: %w", err)
616 }
617 a.initialCommit = commitHash
618
619 codereview, err := claudetool.NewCodeReviewer(ctx, a.repoRoot, a.initialCommit)
620 if err != nil {
621 return fmt.Errorf("Agent.Init: claudetool.NewCodeReviewer: %w", err)
622 }
623 a.codereview = codereview
Philip Zeyligerd1402952025-04-23 03:54:37 +0000624
625 a.gitOrigin = getGitOrigin(ctx, ini.WorkingDir)
Earl Lee2e463fb2025-04-17 11:22:22 -0700626 }
627 a.lastHEAD = a.initialCommit
628 a.convo = a.initConvo()
629 close(a.ready)
630 return nil
631}
632
Josh Bleecher Snyderdbe02302025-04-29 16:44:23 -0700633//go:embed agent_system_prompt.txt
634var agentSystemPrompt string
635
Earl Lee2e463fb2025-04-17 11:22:22 -0700636// initConvo initializes the conversation.
637// It must not be called until all agent fields are initialized,
638// particularly workingDir and git.
639func (a *Agent) initConvo() *ant.Convo {
640 ctx := a.config.Context
641 convo := ant.NewConvo(ctx, a.config.APIKey)
642 if a.config.HTTPC != nil {
643 convo.HTTPC = a.config.HTTPC
644 }
645 if a.config.AntURL != "" {
646 convo.URL = a.config.AntURL
647 }
648 convo.PromptCaching = true
649 convo.Budget = a.config.Budget
650
651 var editPrompt string
652 if a.config.UseAnthropicEdit {
653 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."
654 } else {
655 editPrompt = "Then use the patch tool to make those edits. Combine all edits to any given file into a single patch tool call."
656 }
657
Josh Bleecher Snyderdbe02302025-04-29 16:44:23 -0700658 convo.SystemPrompt = fmt.Sprintf(agentSystemPrompt, editPrompt, a.config.ClientGOOS, a.config.ClientGOARCH, a.workingDir, a.repoRoot, a.initialCommit)
Earl Lee2e463fb2025-04-17 11:22:22 -0700659
Josh Bleecher Snyderd499fd62025-04-30 01:31:29 +0000660 // Define a permission callback for the bash tool to check if the branch name is set before allowing git commits
661 bashPermissionCheck := func(command string) error {
662 // Check if branch name is set
663 a.mu.Lock()
664 branchSet := a.branchName != ""
665 a.mu.Unlock()
666
667 // If branch is set, all commands are allowed
668 if branchSet {
669 return nil
670 }
671
672 // If branch is not set, check if this is a git commit command
673 willCommit, err := bashkit.WillRunGitCommit(command)
674 if err != nil {
675 // If there's an error checking, we should allow the command to proceed
676 return nil
677 }
678
679 // If it's a git commit and branch is not set, return an error
680 if willCommit {
681 return fmt.Errorf("you must use the title tool before making git commits")
682 }
683
684 return nil
685 }
686
687 // Create a custom bash tool with the permission check
688 bashTool := claudetool.NewBashTool(bashPermissionCheck)
689
Earl Lee2e463fb2025-04-17 11:22:22 -0700690 // Register all tools with the conversation
691 // When adding, removing, or modifying tools here, double-check that the termui tool display
692 // template in termui/termui.go has pretty-printing support for all tools.
693 convo.Tools = []*ant.Tool{
Josh Bleecher Snyderd499fd62025-04-30 01:31:29 +0000694 bashTool, claudetool.Keyword,
Earl Lee2e463fb2025-04-17 11:22:22 -0700695 claudetool.Think, a.titleTool(), makeDoneTool(a.codereview, a.config.GitUsername, a.config.GitEmail),
696 a.codereview.Tool(),
697 }
698 if a.config.UseAnthropicEdit {
699 convo.Tools = append(convo.Tools, claudetool.AnthropicEditTool)
700 } else {
701 convo.Tools = append(convo.Tools, claudetool.Patch)
702 }
703 convo.Listener = a
704 return convo
705}
706
Josh Bleecher Snyderfff269b2025-04-30 01:49:39 +0000707// branchExists reports whether branchName exists, either locally or in well-known remotes.
708func branchExists(dir, branchName string) bool {
709 refs := []string{
710 "refs/heads/",
711 "refs/remotes/origin/",
712 "refs/remotes/sketch-host/",
713 }
714 for _, ref := range refs {
715 cmd := exec.Command("git", "show-ref", "--verify", "--quiet", ref+branchName)
716 cmd.Dir = dir
717 if cmd.Run() == nil { // exit code 0 means branch exists
718 return true
719 }
720 }
721 return false
722}
723
Earl Lee2e463fb2025-04-17 11:22:22 -0700724func (a *Agent) titleTool() *ant.Tool {
Earl Lee2e463fb2025-04-17 11:22:22 -0700725 title := &ant.Tool{
726 Name: "title",
Josh Bleecher Snydera9b38222025-04-29 18:05:06 -0700727 Description: `Sets the conversation title and creates a git branch for tracking work. MANDATORY: You must use this tool before making any git commits.`,
Earl Lee2e463fb2025-04-17 11:22:22 -0700728 InputSchema: json.RawMessage(`{
729 "type": "object",
730 "properties": {
731 "title": {
732 "type": "string",
Josh Bleecher Snydera9b38222025-04-29 18:05:06 -0700733 "description": "A concise, descriptive title summarizing what this conversation is about"
734 },
735 "branch_name": {
736 "type": "string",
737 "description": "A 2-3 word alphanumeric hyphenated slug for the git branch name"
Earl Lee2e463fb2025-04-17 11:22:22 -0700738 }
739 },
Josh Bleecher Snydera9b38222025-04-29 18:05:06 -0700740 "required": ["title", "branch_name"]
Earl Lee2e463fb2025-04-17 11:22:22 -0700741}`),
742 Run: func(ctx context.Context, input json.RawMessage) (string, error) {
743 var params struct {
Josh Bleecher Snydera9b38222025-04-29 18:05:06 -0700744 Title string `json:"title"`
745 BranchName string `json:"branch_name"`
Earl Lee2e463fb2025-04-17 11:22:22 -0700746 }
747 if err := json.Unmarshal(input, &params); err != nil {
748 return "", err
749 }
Josh Bleecher Snydera9b38222025-04-29 18:05:06 -0700750 // It's unfortunate to not allow title changes,
751 // but it avoids having multiple branches.
752 t := a.Title()
753 if t != "" {
754 return "", fmt.Errorf("title already set to: %s", t)
755 }
756
757 if params.BranchName == "" {
758 return "", fmt.Errorf("branch_name parameter cannot be empty")
759 }
760 if params.Title == "" {
761 return "", fmt.Errorf("title parameter cannot be empty")
762 }
763
764 branchName := "sketch/" + cleanBranchName(params.BranchName)
Josh Bleecher Snyderfff269b2025-04-30 01:49:39 +0000765 if branchExists(a.workingDir, branchName) {
766 return "", fmt.Errorf("branch %q already exists; please choose a different branch name", branchName)
767 }
768
Josh Bleecher Snydera9b38222025-04-29 18:05:06 -0700769 a.SetTitleBranch(params.Title, branchName)
770
771 response := fmt.Sprintf("Title set to %q, branch name set to %q", params.Title, branchName)
772 return response, nil
Earl Lee2e463fb2025-04-17 11:22:22 -0700773 },
774 }
775 return title
776}
777
778func (a *Agent) Ready() <-chan struct{} {
779 return a.ready
780}
781
782func (a *Agent) UserMessage(ctx context.Context, msg string) {
783 a.pushToOutbox(ctx, AgentMessage{Type: UserMessageType, Content: msg})
784 a.inbox <- msg
785}
786
787func (a *Agent) WaitForMessage(ctx context.Context) AgentMessage {
788 // TODO: Should this drain any outbox messages in case there are multiple?
789 select {
790 case msg := <-a.outbox:
791 return msg
792 case <-ctx.Done():
793 return errorMessage(ctx.Err())
794 }
795}
796
797func (a *Agent) CancelToolUse(toolUseID string, cause error) error {
798 return a.convo.CancelToolUse(toolUseID, cause)
799}
800
801func (a *Agent) CancelInnerLoop(cause error) {
802 a.cancelInnerLoopMu.Lock()
803 defer a.cancelInnerLoopMu.Unlock()
804 if a.cancelInnerLoop != nil {
805 a.cancelInnerLoop(cause)
806 }
807}
808
809func (a *Agent) Loop(ctxOuter context.Context) {
810 for {
811 select {
812 case <-ctxOuter.Done():
813 return
814 default:
815 ctxInner, cancel := context.WithCancelCause(ctxOuter)
816 a.cancelInnerLoopMu.Lock()
817 // Set .cancelInnerLoop so the user can cancel whatever is happening
818 // inside InnerLoop(ctxInner) without canceling this outer Loop execution.
819 // This CancelInnerLoop func is intended be called from other goroutines,
820 // hence the mutex.
821 a.cancelInnerLoop = cancel
822 a.cancelInnerLoopMu.Unlock()
823 a.InnerLoop(ctxInner)
824 cancel(nil)
825 }
826 }
827}
828
829func (a *Agent) pushToOutbox(ctx context.Context, m AgentMessage) {
830 if m.Timestamp.IsZero() {
831 m.Timestamp = time.Now()
832 }
833
834 // If this is an end-of-turn message, calculate the turn duration and add it to the message
835 if m.EndOfTurn && m.Type == AgentMessageType {
836 turnDuration := time.Since(a.startOfTurn)
837 m.TurnDuration = &turnDuration
838 slog.InfoContext(ctx, "Turn completed", "turnDuration", turnDuration)
839 }
840
841 slog.InfoContext(ctx, "agent message", m.Attr())
842
843 a.mu.Lock()
844 defer a.mu.Unlock()
845 m.Idx = len(a.history)
846 a.history = append(a.history, m)
847 a.outbox <- m
848
849 // Notify all listeners:
850 for _, ch := range a.listeners {
851 close(ch)
852 }
853 a.listeners = a.listeners[:0]
854}
855
856func (a *Agent) GatherMessages(ctx context.Context, block bool) ([]ant.Content, error) {
857 var m []ant.Content
858 if block {
859 select {
860 case <-ctx.Done():
861 return m, ctx.Err()
862 case msg := <-a.inbox:
863 m = append(m, ant.Content{Type: "text", Text: msg})
864 }
865 }
866 for {
867 select {
868 case msg := <-a.inbox:
869 m = append(m, ant.Content{Type: "text", Text: msg})
870 default:
871 return m, nil
872 }
873 }
874}
875
876func (a *Agent) InnerLoop(ctx context.Context) {
877 // Reset the start of turn time
878 a.startOfTurn = time.Now()
879
880 // Wait for at least one message from the user.
881 msgs, err := a.GatherMessages(ctx, true)
882 if err != nil { // e.g. the context was canceled while blocking in GatherMessages
883 return
884 }
885 // We do this as we go, but let's also do it at the end of the turn
886 defer func() {
887 if _, err := a.handleGitCommits(ctx); err != nil {
888 // Just log the error, don't stop execution
889 slog.WarnContext(ctx, "Failed to check for new git commits", "error", err)
890 }
891 }()
892
893 userMessage := ant.Message{
894 Role: "user",
895 Content: msgs,
896 }
897 // convo.SendMessage does the actual network call to send this to anthropic. This blocks until the response is ready.
898 // 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?
899 resp, err := a.convo.SendMessage(userMessage)
900 if err != nil {
901 a.pushToOutbox(ctx, errorMessage(err))
902 return
903 }
904 for {
905 // TODO: here and below where we check the budget,
906 // we should review the UX: is it clear what happened?
907 // is it clear how to resume?
908 // should we let the user set a new budget?
909 if err := a.overBudget(ctx); err != nil {
910 return
911 }
912 if resp.StopReason != ant.StopReasonToolUse {
913 break
914 }
915 var results []ant.Content
916 cancelled := false
917 select {
918 case <-ctx.Done():
919 // Don't actually run any of the tools, but rather build a response
920 // for each tool_use message letting the LLM know that user canceled it.
921 results, err = a.convo.ToolResultCancelContents(resp)
922 if err != nil {
923 a.pushToOutbox(ctx, errorMessage(err))
924 }
925 cancelled = true
926 default:
927 ctx = claudetool.WithWorkingDir(ctx, a.workingDir)
928 // fall-through, when the user has not canceled the inner loop:
929 results, err = a.convo.ToolResultContents(ctx, resp)
930 if ctx.Err() != nil { // e.g. the user canceled the operation
931 cancelled = true
932 } else if err != nil {
933 a.pushToOutbox(ctx, errorMessage(err))
934 }
935 }
936
937 // Check for git commits. Currently we do this here, after we collect
938 // tool results, since that's when we know commits could have happened.
939 // We could instead do this when the turn ends, but I think it makes sense
940 // to do this as we go.
941 newCommits, err := a.handleGitCommits(ctx)
942 if err != nil {
943 // Just log the error, don't stop execution
944 slog.WarnContext(ctx, "Failed to check for new git commits", "error", err)
945 }
946 var autoqualityMessages []string
947 if len(newCommits) == 1 {
948 formatted := a.codereview.Autoformat(ctx)
949 if len(formatted) > 0 {
950 msg := fmt.Sprintf(`
951I ran autoformatters and they updated these files:
952
953%s
954
955Please amend your latest git commit with these changes and then continue with what you were doing.`,
956 strings.Join(formatted, "\n"),
957 )[1:]
958 a.pushToOutbox(ctx, AgentMessage{
959 Type: AutoMessageType,
960 Content: msg,
961 Timestamp: time.Now(),
962 })
963 autoqualityMessages = append(autoqualityMessages, msg)
964 }
965 }
966
967 if err := a.overBudget(ctx); err != nil {
968 return
969 }
970
971 // Include, along with the tool results (which must go first for whatever reason),
972 // any messages that the user has sent along while the tool_use was executing concurrently.
973 msgs, err = a.GatherMessages(ctx, false)
974 if err != nil {
975 return
976 }
977 // Inject any auto-generated messages from quality checks.
978 for _, msg := range autoqualityMessages {
979 msgs = append(msgs, ant.Content{Type: "text", Text: msg})
980 }
981 if cancelled {
982 msgs = append(msgs, ant.Content{Type: "text", Text: cancelToolUseMessage})
983 // EndOfTurn is false here so that the client of this agent keeps processing
984 // messages from WaitForMessage() and gets the response from the LLM (usually
985 // something like "okay, I'll wait further instructions", but the user should
986 // be made aware of it regardless).
987 a.pushToOutbox(ctx, AgentMessage{Type: ErrorMessageType, Content: userCancelMessage, EndOfTurn: false})
988 } else if err := a.convo.OverBudget(); err != nil {
989 budgetMsg := "We've exceeded our budget. Please ask the user to confirm before continuing by ending the turn."
990 msgs = append(msgs, ant.Content{Type: "text", Text: budgetMsg})
991 a.pushToOutbox(ctx, budgetMessage(fmt.Errorf("warning: %w (ask to keep trying, if you'd like)", err)))
992 }
993 results = append(results, msgs...)
994 resp, err = a.convo.SendMessage(ant.Message{
995 Role: "user",
996 Content: results,
997 })
998 if err != nil {
999 a.pushToOutbox(ctx, errorMessage(fmt.Errorf("error: failed to continue conversation: %s", err.Error())))
1000 break
1001 }
1002 if cancelled {
1003 return
1004 }
1005 }
1006}
1007
1008func (a *Agent) overBudget(ctx context.Context) error {
1009 if err := a.convo.OverBudget(); err != nil {
1010 m := budgetMessage(err)
1011 m.Content = m.Content + "\n\nBudget reset."
1012 a.pushToOutbox(ctx, budgetMessage(err))
1013 a.convo.ResetBudget(a.originalBudget)
1014 return err
1015 }
1016 return nil
1017}
1018
1019func collectTextContent(msg *ant.MessageResponse) string {
1020 // Collect all text content
1021 var allText strings.Builder
1022 for _, content := range msg.Content {
1023 if content.Type == "text" && content.Text != "" {
1024 if allText.Len() > 0 {
1025 allText.WriteString("\n\n")
1026 }
1027 allText.WriteString(content.Text)
1028 }
1029 }
1030 return allText.String()
1031}
1032
1033func (a *Agent) TotalUsage() ant.CumulativeUsage {
1034 a.mu.Lock()
1035 defer a.mu.Unlock()
1036 return a.convo.CumulativeUsage()
1037}
1038
1039// WaitForMessageCount returns when the agent has at more than clientMessageCount messages or the context is done.
1040func (a *Agent) WaitForMessageCount(ctx context.Context, greaterThan int) {
1041 for a.MessageCount() <= greaterThan {
1042 a.mu.Lock()
1043 ch := make(chan struct{})
1044 // Deletion happens when we notify.
1045 a.listeners = append(a.listeners, ch)
1046 a.mu.Unlock()
1047
1048 select {
1049 case <-ctx.Done():
1050 return
1051 case <-ch:
1052 continue
1053 }
1054 }
1055}
1056
1057// Diff returns a unified diff of changes made since the agent was instantiated.
1058func (a *Agent) Diff(commit *string) (string, error) {
1059 if a.initialCommit == "" {
1060 return "", fmt.Errorf("no initial commit reference available")
1061 }
1062
1063 // Find the repository root
1064 ctx := context.Background()
1065
1066 // If a specific commit hash is provided, show just that commit's changes
1067 if commit != nil && *commit != "" {
1068 // Validate that the commit looks like a valid git SHA
1069 if !isValidGitSHA(*commit) {
1070 return "", fmt.Errorf("invalid git commit SHA format: %s", *commit)
1071 }
1072
1073 // Get the diff for just this commit
1074 cmd := exec.CommandContext(ctx, "git", "show", "--unified=10", *commit)
1075 cmd.Dir = a.repoRoot
1076 output, err := cmd.CombinedOutput()
1077 if err != nil {
1078 return "", fmt.Errorf("failed to get diff for commit %s: %w - %s", *commit, err, string(output))
1079 }
1080 return string(output), nil
1081 }
1082
1083 // Otherwise, get the diff between the initial commit and the current state using exec.Command
1084 cmd := exec.CommandContext(ctx, "git", "diff", "--unified=10", a.initialCommit)
1085 cmd.Dir = a.repoRoot
1086 output, err := cmd.CombinedOutput()
1087 if err != nil {
1088 return "", fmt.Errorf("failed to get diff: %w - %s", err, string(output))
1089 }
1090
1091 return string(output), nil
1092}
1093
1094// InitialCommit returns the Git commit hash that was saved when the agent was instantiated.
1095func (a *Agent) InitialCommit() string {
1096 return a.initialCommit
1097}
1098
1099// handleGitCommits() highlights new commits to the user. When running
1100// under docker, new HEADs are pushed to a branch according to the title.
1101func (a *Agent) handleGitCommits(ctx context.Context) ([]*GitCommit, error) {
1102 if a.repoRoot == "" {
1103 return nil, nil
1104 }
1105
1106 head, err := resolveRef(ctx, a.repoRoot, "HEAD")
1107 if err != nil {
1108 return nil, err
1109 }
1110 if head == a.lastHEAD {
1111 return nil, nil // nothing to do
1112 }
1113 defer func() {
1114 a.lastHEAD = head
1115 }()
1116
1117 // Get new commits. Because it's possible that the agent does rebases, fixups, and
1118 // so forth, we use, as our fixed point, the "initialCommit", and we limit ourselves
1119 // to the last 100 commits.
1120 var commits []*GitCommit
1121
1122 // Get commits since the initial commit
1123 // Format: <hash>\0<subject>\0<body>\0
1124 // This uses NULL bytes as separators to avoid issues with newlines in commit messages
1125 // Limit to 100 commits to avoid overwhelming the user
1126 cmd := exec.CommandContext(ctx, "git", "log", "-n", "100", "--pretty=format:%H%x00%s%x00%b%x00", "^"+a.initialCommit, head)
1127 cmd.Dir = a.repoRoot
1128 output, err := cmd.Output()
1129 if err != nil {
1130 return nil, fmt.Errorf("failed to get git log: %w", err)
1131 }
1132
1133 // Parse git log output and filter out already seen commits
1134 parsedCommits := parseGitLog(string(output))
1135
1136 var headCommit *GitCommit
1137
1138 // Filter out commits we've already seen
1139 for _, commit := range parsedCommits {
1140 if commit.Hash == head {
1141 headCommit = &commit
1142 }
1143
1144 // Skip if we've seen this commit before. If our head has changed, always include that.
1145 if a.seenCommits[commit.Hash] && commit.Hash != head {
1146 continue
1147 }
1148
1149 // Mark this commit as seen
1150 a.seenCommits[commit.Hash] = true
1151
1152 // Add to our list of new commits
1153 commits = append(commits, &commit)
1154 }
1155
1156 if a.gitRemoteAddr != "" {
1157 if headCommit == nil {
1158 // I think this can only happen if we have a bug or if there's a race.
1159 headCommit = &GitCommit{}
1160 headCommit.Hash = head
1161 headCommit.Subject = "unknown"
1162 commits = append(commits, headCommit)
1163 }
1164
Josh Bleecher Snydera9b38222025-04-29 18:05:06 -07001165 branch := cmp.Or(a.branchName, "sketch/"+a.config.SessionID)
Earl Lee2e463fb2025-04-17 11:22:22 -07001166
1167 // TODO: I don't love the force push here. We could see if the push is a fast-forward, and,
1168 // if it's not, we could make a backup with a unique name (perhaps append a timestamp) and
1169 // then use push with lease to replace.
1170 cmd = exec.Command("git", "push", "--force", a.gitRemoteAddr, "HEAD:refs/heads/"+branch)
1171 cmd.Dir = a.workingDir
1172 if out, err := cmd.CombinedOutput(); err != nil {
1173 a.pushToOutbox(ctx, errorMessage(fmt.Errorf("git push to host: %s: %v", out, err)))
1174 } else {
1175 headCommit.PushedBranch = branch
1176 }
1177 }
1178
1179 // If we found new commits, create a message
1180 if len(commits) > 0 {
1181 msg := AgentMessage{
1182 Type: CommitMessageType,
1183 Timestamp: time.Now(),
1184 Commits: commits,
1185 }
1186 a.pushToOutbox(ctx, msg)
1187 }
1188 return commits, nil
1189}
1190
Josh Bleecher Snydera9b38222025-04-29 18:05:06 -07001191func cleanBranchName(s string) string {
Josh Bleecher Snyder1ae976b2025-04-30 00:06:43 +00001192 return strings.Map(func(r rune) rune {
1193 // lowercase
1194 if r >= 'A' && r <= 'Z' {
1195 return r + 'a' - 'A'
Earl Lee2e463fb2025-04-17 11:22:22 -07001196 }
Josh Bleecher Snyder1ae976b2025-04-30 00:06:43 +00001197 // replace spaces with dashes
1198 if r == ' ' {
1199 return '-'
1200 }
1201 // allow alphanumerics and dashes
1202 if (r >= 'a' && r <= 'z') || r == '-' || (r >= '0' && r <= '9') {
1203 return r
1204 }
1205 return -1
1206 }, s)
Earl Lee2e463fb2025-04-17 11:22:22 -07001207}
1208
1209// parseGitLog parses the output of git log with format '%H%x00%s%x00%b%x00'
1210// and returns an array of GitCommit structs.
1211func parseGitLog(output string) []GitCommit {
1212 var commits []GitCommit
1213
1214 // No output means no commits
1215 if len(output) == 0 {
1216 return commits
1217 }
1218
1219 // Split by NULL byte
1220 parts := strings.Split(output, "\x00")
1221
1222 // Process in triplets (hash, subject, body)
1223 for i := 0; i < len(parts); i++ {
1224 // Skip empty parts
1225 if parts[i] == "" {
1226 continue
1227 }
1228
1229 // This should be a hash
1230 hash := strings.TrimSpace(parts[i])
1231
1232 // Make sure we have at least a subject part available
1233 if i+1 >= len(parts) {
1234 break // No more parts available
1235 }
1236
1237 // Get the subject
1238 subject := strings.TrimSpace(parts[i+1])
1239
1240 // Get the body if available
1241 body := ""
1242 if i+2 < len(parts) {
1243 body = strings.TrimSpace(parts[i+2])
1244 }
1245
1246 // Skip to the next triplet
1247 i += 2
1248
1249 commits = append(commits, GitCommit{
1250 Hash: hash,
1251 Subject: subject,
1252 Body: body,
1253 })
1254 }
1255
1256 return commits
1257}
1258
1259func repoRoot(ctx context.Context, dir string) (string, error) {
1260 cmd := exec.CommandContext(ctx, "git", "rev-parse", "--show-toplevel")
1261 stderr := new(strings.Builder)
1262 cmd.Stderr = stderr
1263 cmd.Dir = dir
1264 out, err := cmd.Output()
1265 if err != nil {
1266 return "", fmt.Errorf("git rev-parse failed: %w\n%s", err, stderr)
1267 }
1268 return strings.TrimSpace(string(out)), nil
1269}
1270
1271func resolveRef(ctx context.Context, dir, refName string) (string, error) {
1272 cmd := exec.CommandContext(ctx, "git", "rev-parse", refName)
1273 stderr := new(strings.Builder)
1274 cmd.Stderr = stderr
1275 cmd.Dir = dir
1276 out, err := cmd.Output()
1277 if err != nil {
1278 return "", fmt.Errorf("git rev-parse failed: %w\n%s", err, stderr)
1279 }
1280 // TODO: validate that out is valid hex
1281 return strings.TrimSpace(string(out)), nil
1282}
1283
1284// isValidGitSHA validates if a string looks like a valid git SHA hash.
1285// Git SHAs are hexadecimal strings of at least 4 characters but typically 7, 8, or 40 characters.
1286func isValidGitSHA(sha string) bool {
1287 // Git SHA must be a hexadecimal string with at least 4 characters
1288 if len(sha) < 4 || len(sha) > 40 {
1289 return false
1290 }
1291
1292 // Check if the string only contains hexadecimal characters
1293 for _, char := range sha {
1294 if !(char >= '0' && char <= '9') && !(char >= 'a' && char <= 'f') && !(char >= 'A' && char <= 'F') {
1295 return false
1296 }
1297 }
1298
1299 return true
1300}
Philip Zeyligerd1402952025-04-23 03:54:37 +00001301
1302// getGitOrigin returns the URL of the git remote 'origin' if it exists
1303func getGitOrigin(ctx context.Context, dir string) string {
1304 cmd := exec.CommandContext(ctx, "git", "config", "--get", "remote.origin.url")
1305 cmd.Dir = dir
1306 stderr := new(strings.Builder)
1307 cmd.Stderr = stderr
1308 out, err := cmd.Output()
1309 if err != nil {
1310 return ""
1311 }
1312 return strings.TrimSpace(string(out))
1313}