blob: 4261a980c6c3dce8ee4b6e09ae515a6bddcfc1ef [file] [log] [blame]
Earl Lee2e463fb2025-04-17 11:22:22 -07001package loop
2
3import (
4 "context"
5 "encoding/json"
6 "fmt"
7
Josh Bleecher Snyderf4047bb2025-05-05 23:02:56 +00008 "sketch.dev/claudetool/codereview"
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -07009 "sketch.dev/llm"
Earl Lee2e463fb2025-04-17 11:22:22 -070010)
11
12// makeDoneTool creates a tool that provides a checklist to the agent. There
13// are some duplicative instructions here and in the system prompt, and it's
14// not as reliable as it could be. Historically, we've found that Claude ignores
15// the tool results here, so we don't tell the tool to say "hey, really check this"
16// at the moment, though we've tried.
Josh Bleecher Snyder93202652025-05-08 02:05:57 +000017func makeDoneTool(codereview *codereview.CodeReviewer) *llm.Tool {
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -070018 return &llm.Tool{
Earl Lee2e463fb2025-04-17 11:22:22 -070019 Name: "done",
Josh Bleecher Snyder220a36d2025-05-08 19:06:26 +000020 Description: doneDescription,
21 InputSchema: json.RawMessage(doneChecklistJSONSchema),
Earl Lee2e463fb2025-04-17 11:22:22 -070022 Run: func(ctx context.Context, input json.RawMessage) (string, error) {
23 // Cannot be done with a messy git.
24 if err := codereview.RequireNormalGitState(ctx); err != nil {
25 return "", err
26 }
27 if err := codereview.RequireNoUncommittedChanges(ctx); err != nil {
28 return "", err
29 }
30 // Ensure that the current commit has been reviewed.
31 head, err := codereview.CurrentCommit(ctx)
32 if err == nil {
33 needsReview := !codereview.IsInitialCommit(head) && !codereview.HasReviewed(head)
34 if needsReview {
35 return "", fmt.Errorf("codereview tool has not been run for commit %v", head)
36 }
37 }
38 return `Please ask the user to review your work. Be concise - users are more likely to read shorter comments.`, nil
39 },
40 }
41}
42
Earl Lee2e463fb2025-04-17 11:22:22 -070043// TODO: this is ugly, maybe JSON-encode a deeply nested map[string]any instead? also ugly.
44const (
Josh Bleecher Snyder93202652025-05-08 02:05:57 +000045 doneDescription = `Use this tool when you have achieved the user's goal. The parameters form a checklist which you should evaluate.`
46 doneChecklistJSONSchema = `{
Earl Lee2e463fb2025-04-17 11:22:22 -070047 "$schema": "http://json-schema.org/draft-07/schema#",
48 "title": "Checklist",
49 "description": "A schema for tracking checklist items with status and comments",
50 "type": "object",
51 "required": ["checklist_items"],
52 "properties": {
53 "checklist_items": {
54 "type": "object",
55 "description": "Collection of checklist items",
56 "properties": {
57 "wrote_tests": {
58 "$ref": "#/definitions/checklistItem",
59 "description": "If code was changed, tests were written or updated."
60 },
61 "passes_tests": {
62 "$ref": "#/definitions/checklistItem",
63 "description": "If any commits were made, tests pass."
64 },
65 "code_reviewed": {
66 "$ref": "#/definitions/checklistItem",
67 "description": "If any commits were made, the codereview tool was run and its output was addressed."
68 },
69 "git_commit": {
70 "$ref": "#/definitions/checklistItem",
Josh Bleecher Snyder93202652025-05-08 02:05:57 +000071 "description": "Create git commits for any code changes you made, adding --trailer 'Co-Authored-By: sketch <hello@sketch.dev>' and --trailer 'Change-ID: s$(openssl rand -hex 8)k'. The git user is already configured correctly."
Josh Bleecher Snyderc6a2c242025-04-22 18:04:16 -070072 }
Earl Lee2e463fb2025-04-17 11:22:22 -070073 },
74 "additionalProperties": {
75 "$ref": "#/definitions/checklistItem"
76 }
77 }
78 },
79 "definitions": {
80 "checklistItem": {
81 "type": "object",
82 "required": ["status"],
83 "properties": {
84 "status": {
85 "type": "string",
86 "description": "Current status of the checklist item",
87 "enum": ["yes", "no", "not applicable", "other"]
88 },
89 "description": {
90 "type": "string",
91 "description": "Description of what this checklist item verifies"
92 },
93 "comments": {
94 "type": "string",
95 "description": "Additional comments or context for this checklist item"
96 }
97 }
98 }
99 }
100}`
101)