| 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 | "$schema": "http://json-schema.org/draft-07/schema#", |
| 47 | "title": "Checklist", |
| 48 | "description": "A schema for tracking checklist items with status and comments", |
| 49 | "type": "object", |
| 50 | "required": ["checklist_items"], |
| 51 | "properties": { |
| 52 | "checklist_items": { |
| 53 | "type": "object", |
| 54 | "description": "Collection of checklist items", |
| 55 | "properties": { |
| Josh Bleecher Snyder | 8dff12f | 2025-05-12 19:48:36 +0000 | [diff] [blame] | 56 | "checked_guidance": { |
| 57 | "$ref": "#/definitions/checklistItem", |
| 58 | "description": "I checked for and followed any directory-specific guidance files for all modified files." |
| 59 | }, |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 60 | "wrote_tests": { |
| 61 | "$ref": "#/definitions/checklistItem", |
| 62 | "description": "If code was changed, tests were written or updated." |
| 63 | }, |
| 64 | "passes_tests": { |
| 65 | "$ref": "#/definitions/checklistItem", |
| 66 | "description": "If any commits were made, tests pass." |
| 67 | }, |
| 68 | "code_reviewed": { |
| 69 | "$ref": "#/definitions/checklistItem", |
| 70 | "description": "If any commits were made, the codereview tool was run and its output was addressed." |
| 71 | }, |
| 72 | "git_commit": { |
| 73 | "$ref": "#/definitions/checklistItem", |
| Josh Bleecher Snyder | 039fc34 | 2025-05-14 21:24:12 +0000 | [diff] [blame] | 74 | "description": "Create git commits for any code changes you made. A git hook will add Co-Authored-By and Change-ID trailers. The git user is already configured correctly." |
| Josh Bleecher Snyder | c6a2c24 | 2025-04-22 18:04:16 -0700 | [diff] [blame] | 75 | } |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 76 | }, |
| 77 | "additionalProperties": { |
| 78 | "$ref": "#/definitions/checklistItem" |
| 79 | } |
| 80 | } |
| 81 | }, |
| 82 | "definitions": { |
| 83 | "checklistItem": { |
| 84 | "type": "object", |
| 85 | "required": ["status"], |
| 86 | "properties": { |
| 87 | "status": { |
| 88 | "type": "string", |
| 89 | "description": "Current status of the checklist item", |
| 90 | "enum": ["yes", "no", "not applicable", "other"] |
| 91 | }, |
| 92 | "description": { |
| 93 | "type": "string", |
| 94 | "description": "Description of what this checklist item verifies" |
| 95 | }, |
| 96 | "comments": { |
| 97 | "type": "string", |
| 98 | "description": "Additional comments or context for this checklist item" |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | }` |
| 104 | ) |