blob: aba8d9834bb29c359988c79184e4a4abf09357dd [file] [log] [blame]
Earl Lee2e463fb2025-04-17 11:22:22 -07001package loop
2
3import (
4 "context"
5 "encoding/json"
Earl Lee2e463fb2025-04-17 11:22:22 -07006
Josh Bleecher Snyderf4047bb2025-05-05 23:02:56 +00007 "sketch.dev/claudetool/codereview"
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -07008 "sketch.dev/llm"
Earl Lee2e463fb2025-04-17 11:22:22 -07009)
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 Snyder93202652025-05-08 02:05:57 +000016func makeDoneTool(codereview *codereview.CodeReviewer) *llm.Tool {
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -070017 return &llm.Tool{
Earl Lee2e463fb2025-04-17 11:22:22 -070018 Name: "done",
Josh Bleecher Snyder220a36d2025-05-08 19:06:26 +000019 Description: doneDescription,
20 InputSchema: json.RawMessage(doneChecklistJSONSchema),
Josh Bleecher Snyder43b60b92025-07-21 14:57:10 -070021 Run: func(ctx context.Context, input json.RawMessage) llm.ToolOut {
Earl Lee2e463fb2025-04-17 11:22:22 -070022 // Cannot be done with a messy git.
23 if err := codereview.RequireNormalGitState(ctx); err != nil {
Josh Bleecher Snyder43b60b92025-07-21 14:57:10 -070024 return llm.ErrorToolOut(err)
Earl Lee2e463fb2025-04-17 11:22:22 -070025 }
26 if err := codereview.RequireNoUncommittedChanges(ctx); err != nil {
Josh Bleecher Snyder43b60b92025-07-21 14:57:10 -070027 return llm.ErrorToolOut(err)
Earl Lee2e463fb2025-04-17 11:22:22 -070028 }
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 Snyder43b60b92025-07-21 14:57:10 -070034 return llm.ErrorfToolOut("codereview tool has not been run for commit %v", head)
Earl Lee2e463fb2025-04-17 11:22:22 -070035 }
36 }
Josh Bleecher Snyder43b60b92025-07-21 14:57:10 -070037 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 Lee2e463fb2025-04-17 11:22:22 -070038 },
39 }
40}
41
Earl Lee2e463fb2025-04-17 11:22:22 -070042// TODO: this is ugly, maybe JSON-encode a deeply nested map[string]any instead? also ugly.
43const (
Josh Bleecher Snyder93202652025-05-08 02:05:57 +000044 doneDescription = `Use this tool when you have achieved the user's goal. The parameters form a checklist which you should evaluate.`
45 doneChecklistJSONSchema = `{
Earl Lee2e463fb2025-04-17 11:22:22 -070046 "$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 Snyder8dff12f2025-05-12 19:48:36 +000056 "checked_guidance": {
57 "$ref": "#/definitions/checklistItem",
58 "description": "I checked for and followed any directory-specific guidance files for all modified files."
59 },
Earl Lee2e463fb2025-04-17 11:22:22 -070060 "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 Snyder039fc342025-05-14 21:24:12 +000074 "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 Snyderc6a2c242025-04-22 18:04:16 -070075 }
Earl Lee2e463fb2025-04-17 11:22:22 -070076 },
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)