| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 1 | package loop |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 6 | |
| Josh Bleecher Snyder | f4047bb | 2025-05-05 23:02:56 +0000 | [diff] [blame] | 7 | "sketch.dev/claudetool/codereview" |
| Josh Bleecher Snyder | 4f84ab7 | 2025-04-22 16:40:54 -0700 | [diff] [blame] | 8 | "sketch.dev/llm" |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 9 | ) |
| 10 | |
| 11 | // makeDoneTool creates a tool that provides a checklist to the agent. There |
| 12 | // are some duplicative instructions here and in the system prompt, and it's |
| 13 | // not as reliable as it could be. Historically, we've found that Claude ignores |
| 14 | // the tool results here, so we don't tell the tool to say "hey, really check this" |
| 15 | // at the moment, though we've tried. |
| Josh Bleecher Snyder | 9320265 | 2025-05-08 02:05:57 +0000 | [diff] [blame] | 16 | func makeDoneTool(codereview *codereview.CodeReviewer) *llm.Tool { |
| Josh Bleecher Snyder | 4f84ab7 | 2025-04-22 16:40:54 -0700 | [diff] [blame] | 17 | return &llm.Tool{ |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 18 | Name: "done", |
| Josh Bleecher Snyder | 220a36d | 2025-05-08 19:06:26 +0000 | [diff] [blame] | 19 | Description: doneDescription, |
| 20 | InputSchema: json.RawMessage(doneChecklistJSONSchema), |
| Josh Bleecher Snyder | 43b60b9 | 2025-07-21 14:57:10 -0700 | [diff] [blame] | 21 | Run: func(ctx context.Context, input json.RawMessage) llm.ToolOut { |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 22 | // Cannot be done with a messy git. |
| 23 | if err := codereview.RequireNormalGitState(ctx); err != nil { |
| Josh Bleecher Snyder | 43b60b9 | 2025-07-21 14:57:10 -0700 | [diff] [blame] | 24 | return llm.ErrorToolOut(err) |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 25 | } |
| 26 | if err := codereview.RequireNoUncommittedChanges(ctx); err != nil { |
| Josh Bleecher Snyder | 43b60b9 | 2025-07-21 14:57:10 -0700 | [diff] [blame] | 27 | return llm.ErrorToolOut(err) |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 28 | } |
| 29 | // Ensure that the current commit has been reviewed. |
| 30 | head, err := codereview.CurrentCommit(ctx) |
| 31 | if err == nil { |
| 32 | needsReview := !codereview.IsInitialCommit(head) && !codereview.HasReviewed(head) |
| 33 | if needsReview { |
| Josh Bleecher Snyder | 43b60b9 | 2025-07-21 14:57:10 -0700 | [diff] [blame] | 34 | return llm.ErrorfToolOut("codereview tool has not been run for commit %v", head) |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 35 | } |
| 36 | } |
| Josh Bleecher Snyder | 43b60b9 | 2025-07-21 14:57:10 -0700 | [diff] [blame] | 37 | return llm.ToolOut{LLMContent: llm.TextContent("Please ask the user to review your work. Be concise - users are more likely to read shorter comments.")} |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 38 | }, |
| 39 | } |
| 40 | } |
| 41 | |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 42 | // TODO: this is ugly, maybe JSON-encode a deeply nested map[string]any instead? also ugly. |
| 43 | const ( |
| Josh Bleecher Snyder | 9320265 | 2025-05-08 02:05:57 +0000 | [diff] [blame] | 44 | doneDescription = `Use this tool when you have achieved the user's goal. The parameters form a checklist which you should evaluate.` |
| 45 | doneChecklistJSONSchema = `{ |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 46 | "type": "object", |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 47 | "properties": { |
| Josh Bleecher Snyder | 9c74cb9 | 2025-07-24 12:52:04 -0700 | [diff] [blame] | 48 | "checked_guidance": { |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 49 | "type": "object", |
| 50 | "required": ["status"], |
| 51 | "properties": { |
| Josh Bleecher Snyder | 9c74cb9 | 2025-07-24 12:52:04 -0700 | [diff] [blame] | 52 | "status": {"type": "string", "enum": ["yes", "no", "n/a"]}, |
| 53 | "comments": {"type": "string"} |
| 54 | }, |
| 55 | "description": "Checked for and followed any directory-specific guidance files for all modified files." |
| 56 | }, |
| 57 | "tested": { |
| 58 | "type": "object", |
| 59 | "required": ["status"], |
| 60 | "properties": { |
| 61 | "status": {"type": "string", "enum": ["yes", "no", "n/a"]}, |
| 62 | "comments": {"type": "string"} |
| 63 | }, |
| 64 | "description": "If code was changed, tests were written or updated, and all tests pass." |
| 65 | }, |
| 66 | "code_reviewed": { |
| 67 | "type": "object", |
| 68 | "required": ["status"], |
| 69 | "properties": { |
| 70 | "status": {"type": "string", "enum": ["yes", "no", "n/a"]}, |
| 71 | "comments": {"type": "string"} |
| 72 | }, |
| 73 | "description": "If any commits were made, the codereview tool was run and its output addressed." |
| 74 | }, |
| 75 | "git_commit": { |
| 76 | "type": "object", |
| 77 | "required": ["status"], |
| 78 | "properties": { |
| 79 | "status": {"type": "string", "enum": ["yes", "no", "n/a"]}, |
| 80 | "comments": {"type": "string"} |
| 81 | }, |
| 82 | "description": "All code changes were committed. A git hook adds Co-Authored-By and Change-ID trailers. The git user is already configured correctly." |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | }` |
| 86 | ) |