| 1 | package runnerproto |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/json" |
| 6 | "slices" |
| 7 | "strings" |
| 8 | "time" |
| 9 | |
| 10 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 11 | ) |
| 12 | |
| 13 | type Question struct { |
| 14 | ID string `json:"id"` |
| 15 | TaskID string `json:"task_id"` |
| 16 | AssignmentID string `json:"assignment_id"` |
| 17 | ServerID string `json:"server_id"` |
| 18 | AgentID string `json:"agent_id"` |
| 19 | SessionID string `json:"session_id"` |
| 20 | PermissionID string `json:"permission_id"` |
| 21 | Kind string `json:"kind"` |
| 22 | Attempt int64 `json:"attempt"` |
| 23 | Fence int64 `json:"fence"` |
| 24 | Revision int64 `json:"revision"` |
| 25 | RawRequest json.RawMessage `json:"raw_request"` |
| 26 | AnsweredBy string `json:"answered_by"` |
| 27 | Answer *QuestionAnswer `json:"answer,omitempty"` |
| 28 | ExpiresAt time.Time `json:"expires_at"` |
| 29 | } |
| 30 | type QuestionAnswer struct { |
| 31 | Behavior string `json:"behavior"` |
| 32 | UpdatedInput json.RawMessage `json:"updated_input,omitempty"` |
| 33 | } |
| 34 | |
| 35 | func (q Question) Validate() error { |
| 36 | for _, v := range []string{q.ID, q.TaskID, q.ServerID, q.AgentID, q.SessionID, q.PermissionID, q.Kind} { |
| 37 | if v == "" || len(v) > 256 { |
| 38 | return ErrInvalid |
| 39 | } |
| 40 | } |
| 41 | var raw map[string]any |
| 42 | if DecodeLimit(bytes.NewReader(q.RawRequest), &raw, MaxEventBytes) != nil || raw == nil || raw["id"] != q.PermissionID || raw["kind"] != q.Kind { |
| 43 | return ErrInvalid |
| 44 | } |
| 45 | return nil |
| 46 | } |
| 47 | func (a QuestionAnswer) Validate(kind string) error { |
| 48 | if !slices.Contains([]string{"person", "question", "tool", "plan", "mode", "other"}, kind) { |
| 49 | return ErrInvalid |
| 50 | } |
| 51 | if a.Behavior == "deny" && len(a.UpdatedInput) == 0 { |
| 52 | return nil |
| 53 | } |
| 54 | if (kind != "question" && kind != "person") || a.Behavior != "allow" { |
| 55 | return ErrInvalid |
| 56 | } |
| 57 | var updated struct { |
| 58 | Answers map[string]string `json:"answers"` |
| 59 | } |
| 60 | if DecodeLimit(bytes.NewReader(a.UpdatedInput), &updated, MaxEventBytes) != nil || len(updated.Answers) == 0 { |
| 61 | return ErrInvalid |
| 62 | } |
| 63 | for k, v := range updated.Answers { |
| 64 | if strings.TrimSpace(k) == "" || len(k) > 4096 || len(v) > 8192 { |
| 65 | return ErrInvalid |
| 66 | } |
| 67 | } |
| 68 | return nil |
| 69 | } |
| 70 | |
| 71 | // ValidateAnswer permits only the pinned structured question adapters. Unknown |
| 72 | // provider shapes can be retained without turning them into an approval. |
| 73 | func (q Question) ValidateAnswer(a QuestionAnswer) error { |
| 74 | // A direct Claude native approval permits the unchanged action only. The |
| 75 | // adapter separately checks the original input against its certified policy. |
| 76 | if q.Kind == "tool" && a.Behavior == "allow" && len(a.UpdatedInput) == 0 { |
| 77 | var raw struct { |
| 78 | Direct bool |
| 79 | Provider, Name string |
| 80 | Input json.RawMessage |
| 81 | } |
| 82 | if DecodeLimit(bytes.NewReader(q.RawRequest), new(map[string]any), MaxEventBytes) != nil || json.Unmarshal(q.RawRequest, &raw) != nil { |
| 83 | return ErrInvalid |
| 84 | } |
| 85 | if _, err := q.NativeApproval(); err == nil { |
| 86 | return nil |
| 87 | } |
| 88 | if raw.Direct && raw.Provider == "claude" && slices.Contains([]string{"Read", "Write", "Edit", "Glob", "Grep", "Bash"}, raw.Name) { |
| 89 | return nil |
| 90 | } |
| 91 | return ErrInvalid |
| 92 | } |
| 93 | |
| 94 | if a.Validate(q.Kind) != nil { |
| 95 | return ErrInvalid |
| 96 | } |
| 97 | if a.Behavior == "deny" { |
| 98 | return nil |
| 99 | } |
| 100 | if q.Kind == "person" { |
| 101 | var request struct { |
| 102 | ID string `json:"id"` |
| 103 | Kind string `json:"kind"` |
| 104 | Question string `json:"question"` |
| 105 | } |
| 106 | var input struct { |
| 107 | Answers map[string]string `json:"answers"` |
| 108 | } |
| 109 | if DecodeLimit(bytes.NewReader(q.RawRequest), &request, MaxEventBytes) != nil || request.Kind != "person" || strings.TrimSpace(request.Question) == "" || json.Unmarshal(a.UpdatedInput, &input) != nil || len(input.Answers) != 1 || strings.TrimSpace(input.Answers["answer"]) == "" { |
| 110 | return ErrInvalid |
| 111 | } |
| 112 | return nil |
| 113 | } |
| 114 | |
| 115 | var raw struct { |
| 116 | Provider string `json:"provider"` |
| 117 | Name string `json:"name"` |
| 118 | Input struct { |
| 119 | Question string `json:"question"` |
| 120 | Questions []struct { |
| 121 | Question string `json:"question"` |
| 122 | Header string `json:"header"` |
| 123 | } `json:"questions"` |
| 124 | } `json:"input"` |
| 125 | } |
| 126 | if json.Unmarshal(q.RawRequest, &raw) != nil { |
| 127 | return ErrInvalid |
| 128 | } |
| 129 | if raw.Provider == "claude" && raw.Name == "ask_person" { |
| 130 | var input struct { |
| 131 | Answers map[string]string `json:"answers"` |
| 132 | } |
| 133 | if strings.TrimSpace(raw.Input.Question) == "" || json.Unmarshal(a.UpdatedInput, &input) != nil || len(input.Answers) != 1 || strings.TrimSpace(input.Answers["answer"]) == "" { |
| 134 | return ErrInvalid |
| 135 | } |
| 136 | return nil |
| 137 | } |
| 138 | if len(raw.Input.Questions) == 0 { |
| 139 | return ErrInvalid |
| 140 | } |
| 141 | if (raw.Provider != "claude" || raw.Name != "AskUserQuestion") && (raw.Provider != "codex" || raw.Name != "request_user_input") { |
| 142 | return ErrInvalid |
| 143 | } |
| 144 | var input struct { |
| 145 | Answers map[string]string `json:"answers"` |
| 146 | } |
| 147 | _ = json.Unmarshal(a.UpdatedInput, &input) |
| 148 | keys := map[string]bool{} |
| 149 | for _, question := range raw.Input.Questions { |
| 150 | key := question.Question |
| 151 | if raw.Provider == "codex" { |
| 152 | key = question.Header |
| 153 | } |
| 154 | if key == "" || keys[key] { |
| 155 | return ErrInvalid |
| 156 | } |
| 157 | keys[key] = true |
| 158 | } |
| 159 | if len(input.Answers) != len(keys) { |
| 160 | return ErrInvalid |
| 161 | } |
| 162 | for k, v := range input.Answers { |
| 163 | if !keys[k] || strings.TrimSpace(v) == "" { |
| 164 | return ErrInvalid |
| 165 | } |
| 166 | } |
| 167 | return nil |
| 168 | } |
| 169 | |
| 170 | // NativeApproval validates the saved envelope as well as its original Codex |
| 171 | // frame. The local turn and provider turn are deliberately separate identities. |
| 172 | func (q Question) NativeApproval() (provider.NativeRequest, error) { |
| 173 | var raw struct { |
| 174 | Direct bool |
| 175 | Provider, Name string |
| 176 | TurnID string `json:"turn_id"` |
| 177 | ProviderTurnID string `json:"provider_turn_id"` |
| 178 | Request json.RawMessage |
| 179 | ToolCall json.RawMessage `json:"tool_call"` |
| 180 | Input json.RawMessage |
| 181 | } |
| 182 | if q.Validate() != nil || q.Kind != "tool" || json.Unmarshal(q.RawRequest, &raw) != nil || !raw.Direct || (raw.Provider != "codex" && raw.Provider != "claude") || raw.TurnID == "" { |
| 183 | return provider.NativeRequest{}, ErrInvalid |
| 184 | } |
| 185 | if raw.Provider == "claude" { |
| 186 | var original struct { |
| 187 | Tool string `json:"tool_name"` |
| 188 | ToolUseID string `json:"tool_use_id"` |
| 189 | Input json.RawMessage |
| 190 | } |
| 191 | if json.Unmarshal(raw.Request, &original) != nil || original.Tool == "" || original.Tool != raw.Name || !SameRequestJSON(raw.Input, original.Input) { |
| 192 | return provider.NativeRequest{}, ErrInvalid |
| 193 | } |
| 194 | item := original.ToolUseID |
| 195 | if item == "" { |
| 196 | item = q.PermissionID |
| 197 | } |
| 198 | return provider.NativeRequest{RequestID: q.PermissionID, ItemID: item, TurnID: raw.TurnID, SessionID: q.SessionID, Request: raw.Request}, nil |
| 199 | } |
| 200 | var frame struct { |
| 201 | Method string |
| 202 | Params json.RawMessage |
| 203 | } |
| 204 | if json.Unmarshal(raw.Request, &frame) != nil || raw.Name != frame.Method || !SameRequestJSON(raw.Input, frame.Params) { |
| 205 | return provider.NativeRequest{}, ErrInvalid |
| 206 | } |
| 207 | return provider.NativeApproval(provider.Event{Kind: "request", SessionID: q.SessionID, TurnID: raw.TurnID, ProviderTurnID: raw.ProviderTurnID, RequestID: q.PermissionID, Payload: raw.Request, ToolCall: raw.ToolCall}) |
| 208 | } |
| 209 | |
| 210 | // SameRequestJSON preserves string bytes and JSON number spelling while allowing |
| 211 | // object key order to change during durable encoding. |
| 212 | func SameRequestJSON(a, b json.RawMessage) bool { |
| 213 | decode := func(raw json.RawMessage) (any, error) { |
| 214 | var v any |
| 215 | d := json.NewDecoder(bytes.NewReader(raw)) |
| 216 | d.UseNumber() |
| 217 | err := d.Decode(&v) |
| 218 | return v, err |
| 219 | } |
| 220 | x, ex := decode(a) |
| 221 | y, ey := decode(b) |
| 222 | return ex == nil && ey == nil && EventHash(x) == EventHash(y) |
| 223 | } |
| 224 | |