Debian Codegraph @

protocol.go

raw
1 package clnkr
2
3 import (
4 "errors"
5 "fmt"
6 "strings"
7 )
8
9 // Turn is a sealed interface for structured model turns.
10 type Turn interface{ turn() }
11
12 type BashAction struct {
13 ID string `json:"-"`
14 Command string `json:"command"`
15 Workdir string `json:"workdir,omitempty"`
16 }
17
18 type BashBatch struct {
19 Commands []BashAction `json:"commands"`
20 }
21
22 type ActTurn struct {
23 Bash BashBatch `json:"bash"`
24 Reasoning string `json:"reasoning,omitempty"`
25 }
26
27 type ClarifyTurn struct {
28 Question string `json:"question"`
29 Reasoning string `json:"reasoning,omitempty"`
30 }
31
32 type DoneTurn struct {
33 Summary string `json:"summary"`
34 Verification CompletionVerification `json:"verification"`
35 KnownRisks []string `json:"known_risks"`
36 Reasoning string `json:"reasoning,omitempty"`
37 }
38
39 type VerificationStatus string
40
41 const (
42 VerificationVerified VerificationStatus = "verified"
43 VerificationPartiallyVerified VerificationStatus = "partially_verified"
44 VerificationNotVerified VerificationStatus = "not_verified"
45 )
46
47 type CompletionVerification struct {
48 Status VerificationStatus `json:"status"`
49 Checks []VerificationCheck `json:"checks"`
50 }
51
52 type VerificationCheck struct {
53 Command string `json:"command"`
54 Outcome string `json:"outcome"`
55 Evidence string `json:"evidence"`
56 }
57
58 func (ActTurn) turn() {}
59 func (ClarifyTurn) turn() {}
60 func (DoneTurn) turn() {}
61
62 func turnPointer(turn Turn) Turn {
63 switch v := turn.(type) {
64 case ActTurn:
65 return &v
66 case ClarifyTurn:
67 return &v
68 case DoneTurn:
69 return &v
70 default:
71 return turn
72 }
73 }
74
75 type jsonEnvelope struct {
76 Type string `json:"type"`
77 Bash *jsonBashEnvelope `json:"bash,omitempty"`
78 Question string `json:"question,omitempty"`
79 Summary string `json:"summary,omitempty"`
80 Verification *CompletionVerification `json:"verification,omitempty"`
81 KnownRisks any `json:"known_risks,omitempty"`
82 Reasoning string `json:"reasoning,omitempty"`
83 }
84
85 type jsonBashEnvelope struct {
86 Commands []jsonCommand `json:"commands"`
87 }
88
89 type jsonCommand struct {
90 Command string `json:"command"`
91 Workdir *string `json:"workdir"`
92 }
93
94 var (
95 ErrInvalidJSON = errors.New("invalid JSON")
96 ErrMissingCommand = errors.New("act turn requires at least one command")
97 ErrEmptyClarify = errors.New("clarify turn requires non-empty question")
98 ErrEmptySummary = errors.New("done turn requires non-empty summary")
99 ErrUnknownTurnType = errors.New("unknown turn type")
100 ErrContextLengthExceeded = errors.New("context length exceeded")
101 )
102
103 const protocolActExample = `{"type":"act","bash":{"commands":[{"command":"...","workdir":null}]}}`
104
105 const protocolDoneExample = `{"type":"done","summary":"...","verification":{"status":"verified","checks":[{"command":"...","outcome":"passed","evidence":"..."}]},"known_risks":[]}`
106
107 var protocolErrorTargets = []error{
108 ErrInvalidJSON,
109 ErrMissingCommand,
110 ErrEmptyClarify,
111 ErrEmptySummary,
112 ErrUnknownTurnType,
113 }
114
115 var protocolErrorReasons = []string{
116 "invalid_json",
117 "missing_command",
118 "empty_clarify",
119 "empty_summary",
120 "unknown_turn_type",
121 }
122
123 func errorToReason(err error) string {
124 for i, target := range protocolErrorTargets {
125 if errors.Is(err, target) {
126 return protocolErrorReasons[i]
127 }
128 }
129 return "unknown"
130 }
131
132 func protocolCorrectionMessageFor(err error, protocol ActProtocol) string {
133 detail := err.Error()
134 var hint string
135 if normalizeActProtocol(protocol) == ActProtocolToolCalls {
136 hint = "Your previous response was ignored and no command ran. For command execution, call the bash tool instead of emitting JSON. For clarification or completion, respond with exactly one JSON object whose type is \"clarify\" or \"done\". If type is \"done\", include summary, \"verification\", and \"known_risks\". verification.status must be one of verified, partially_verified, or not_verified. verification.checks must be an array of concrete checks with command, outcome, and evidence. Do not emit a JSON act turn in tool-call mode. Do not jump to done unless prior command results in this conversation already prove the task is complete."
137 } else {
138 hint = fmt.Sprintf(
139 "Your previous response was ignored and no command ran. Respond with exactly one JSON object for the next turn from the current state. Use this act example as the shape guide: %s. Set type to exactly one of \"act\", \"clarify\", or \"done\". If type is \"act\", include bash. If type is \"clarify\", include question. If type is \"done\", include summary, \"verification\", and \"known_risks\". verification.status must be one of verified, partially_verified, or not_verified. verification.checks must be an array of concrete checks with command, outcome, and evidence. When a field does not apply, omit it or set it to null. Include reasoning in every response; use null if you have nothing to add. Do not emit multiple JSON objects in one response. Do not emit an act turn and a done turn together. If you intended to run commands, resend only that act turn. Do not jump to done unless prior command results in this conversation already prove the task is complete.",
140 protocolActExample,
141 )
142 }
143 switch {
144 case strings.Contains(detail, "invalid character '|' in string escape code"):
145 hint += ` Your command contains \| which is not a valid JSON escape. Use \\| for a literal backslash-pipe, or just | if you do not want a backslash.`
146 case strings.Contains(detail, "invalid character '`' in string escape code"):
147 hint += " Your command contains \\` which is not a valid JSON escape. Use \\\\` for a literal backslash-backtick, or just ` if you do not want a backslash."
148 }
149 return fmt.Sprintf("[protocol_error]\nreason: %s\nhint: %s\ndetail: %s\n[/protocol_error]",
150 errorToReason(err), hint, detail)
151 }