oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package codex
2
3import (
4 "bytes"
5 "context"
6 "encoding/json"
7 "fmt"
8 "testing"
9
10 "amadan.net/oficina/equipo/internal/runner/provider"
11)
12
13func nativeRequest(t *testing.T, s *sessionState, id, method string) []byte {
14 t.Helper()
15 params := map[string]any{"threadId": s.session.ID, "turnId": "provider-turn", "itemId": "native-item", "startedAtMs": 1}
16 if method == "item/commandExecution/requestApproval" {
17 params["command"] = "printf '%s' hello"
18 params["cwd"] = "/workspace"
19 params["proposedExecpolicyAmendment"] = []string{"printf"}
20 } else {
21 item := marshal(map[string]any{"method": "item/started", "params": map[string]any{"threadId": s.session.ID, "turnId": "provider-turn", "item": map[string]any{"id": "native-item", "type": "fileChange", "status": "inProgress", "changes": []any{map[string]any{"path": "/workspace/a.txt", "kind": map[string]any{"type": "update", "move_path": "/workspace/b.txt"}, "diff": "-old\n+new"}}}}})
22 if err := s.handle(provider.Frame{Sequence: 98, Bytes: item}); err != nil {
23 t.Fatal(err)
24 }
25 }
26 raw := marshal(map[string]any{"id": json.RawMessage(id), "method": method, "params": params})
27 if err := s.handle(provider.Frame{Sequence: 99, Bytes: raw}); err != nil {
28 t.Fatal(err)
29 }
30 return raw
31}
32
33func TestCodexApprovalAllowOnce(t *testing.T) {
34 for _, method := range []string{"item/commandExecution/requestApproval", "item/fileChange/requestApproval"} {
35 t.Run(method, func(t *testing.T) {
36 l, s, w, _ := answerFixture(t)
37 before := marshal(s.launch.Policy)
38 for _, id := range []string{"7", `"7"`} {
39 raw := nativeRequest(t, s, id, method)
40 checked := 0
41 a := provider.Answer{RequestID: id, TurnID: "local-turn", Behavior: "allow", Authorize: func(_ context.Context, ev provider.Event) error {
42 checked++
43 if ev.RequestID != id || !bytes.Equal(ev.Payload, raw) {
44 t.Fatal("original request changed")
45 }
46 if method == "item/fileChange/requestApproval" && !bytes.Contains(ev.ToolCall, []byte("/workspace/a.txt")) {
47 t.Fatal("file payload missing")
48 }
49 return nil
50 }}
51 if err := l.Respond(context.Background(), s.session, a); err != nil {
52 t.Fatal("allow once refused", err)
53 }
54 wire := <-w.sent
55 if string(wire) != fmt.Sprintf(`{"id":%s,"result":{"decision":"accept"}}`+"\n", id) {
56 t.Fatalf("broader or wrong response: %s", wire)
57 }
58 if checked != 1 {
59 t.Fatal("original not authorized")
60 }
61 if l.Respond(context.Background(), s.session, a) == nil {
62 t.Fatal("request replay approved")
63 }
64 }
65 if !bytes.Equal(before, marshal(s.launch.Policy)) {
66 t.Fatal("policy hash, manifest, network or namespace mounts changed")
67 }
68 })
69 }
70}
71
72func TestCodexApprovalPersistentRefused(t *testing.T) {
73 l, s, w, _ := answerFixture(t)
74 nativeRequest(t, s, "7", "item/commandExecution/requestApproval")
75 for _, behavior := range []string{"acceptForSession", "approved_for_session", "allow_always", "acceptWithExecpolicyAmendment", "applyNetworkPolicyAmendment", "approved_mcp_policy_amendment"} {
76 if l.Respond(context.Background(), s.session, provider.Answer{RequestID: "7", TurnID: "local-turn", Behavior: behavior, Authorize: func(context.Context, provider.Event) error { return nil }}) == nil {
77 t.Fatal("persistent option accepted", behavior)
78 }
79 }
80 for _, input := range []string{`{"decision":"acceptForSession"}`, `{"execpolicy_amendment":["printf"]}`, `{"network_policy_amendment":{"host":"example.com","action":"allow"}}`, `{"grantRoot":"/"}`} {
81 if l.Respond(context.Background(), s.session, provider.Answer{RequestID: "7", TurnID: "local-turn", Behavior: "allow", Input: json.RawMessage(input), Authorize: func(context.Context, provider.Event) error { return nil }}) == nil {
82 t.Fatal("amendment accepted", input)
83 }
84 }
85 select {
86 case wire := <-w.sent:
87 t.Fatalf("refusal sent a response: %s", wire)
88 default:
89 }
90 if err := l.Respond(context.Background(), s.session, provider.Answer{RequestID: "7", TurnID: "local-turn", Behavior: "deny"}); err != nil {
91 t.Fatal(err)
92 }
93 if !bytes.Contains(<-w.sent, []byte(`"decision":"decline"`)) {
94 t.Fatal("deny changed")
95 }
96}
97
98func TestCodexApprovalLegacyOnce(t *testing.T) {
99 for _, method := range []string{"execCommandApproval", "applyPatchApproval"} {
100 t.Run(method, func(t *testing.T) {
101 l, s, w, _ := answerFixture(t)
102 params := map[string]any{"conversationId": s.session.ID, "callId": "call"}
103 if method == "execCommandApproval" {
104 params["command"] = []string{"/bin/sh", "-c", "printf hello"}
105 params["cwd"] = "/workspace"
106 params["parsedCmd"] = []any{}
107 } else {
108 params["fileChanges"] = map[string]any{"/workspace/a.txt": map[string]any{"type": "add", "content": "hello"}}
109 }
110 raw := marshal(map[string]any{"id": 7, "method": method, "params": params})
111 if err := s.handle(provider.Frame{Sequence: 99, Bytes: raw}); err != nil {
112 t.Fatal("pinned legacy request not relayed", err)
113 }
114 a := provider.Answer{RequestID: "7", TurnID: "local-turn", Behavior: "allow", Authorize: func(_ context.Context, ev provider.Event) error {
115 if !bytes.Equal(ev.Payload, raw) {
116 t.Fatal("argv or paths changed")
117 }
118 return nil
119 }}
120 if err := l.Respond(context.Background(), s.session, a); err != nil {
121 t.Fatal(err)
122 }
123 if wire := <-w.sent; string(wire) != `{"id":7,"result":{"decision":"approved"}}`+"\n" {
124 t.Fatalf("legacy scope broadened: %s", wire)
125 }
126 denied := marshal(map[string]any{"id": 9, "method": method, "params": params})
127 if err := s.handle(provider.Frame{Sequence: 100, Bytes: denied}); err != nil {
128 t.Fatal(err)
129 }
130 if err := l.Respond(context.Background(), s.session, provider.Answer{RequestID: "9", TurnID: "local-turn", Behavior: "deny"}); err != nil {
131 t.Fatal(err)
132 }
133 if wire := <-w.sent; string(wire) != `{"id":9,"result":{"decision":{"denied":{"rejection":""}}}}`+"\n" {
134 t.Fatalf("legacy deny changed: %s", wire)
135 }
136 select {
137 case wire := <-w.sent:
138 t.Fatalf("extra response: %s", wire)
139 default:
140 }
141
142 })
143 }
144}
145
146func TestCodexApprovalFileCorrelation(t *testing.T) {
147 for _, change := range []string{"completed", "changed", "missing", "wrong-item", "wrong-turn", "wrong-session"} {
148 t.Run(change, func(t *testing.T) {
149 l, s, w, _ := answerFixture(t)
150 nativeRequest(t, s, "7", "item/fileChange/requestApproval")
151 // Change the captured item after the approval was raised.
152 s.mu.Lock()
153 switch change {
154 case "completed", "missing":
155 delete(s.nativeCalls, "native-item")
156 case "changed":
157 s.nativeCalls["native-item"] = bytes.ReplaceAll(s.nativeCalls["native-item"], []byte("a.txt"), []byte("changed.txt"))
158 case "wrong-item":
159 s.nativeCalls["native-item"] = bytes.ReplaceAll(s.nativeCalls["native-item"], []byte("native-item"), []byte("other"))
160 case "wrong-turn":
161 s.nativeCalls["native-item"] = bytes.ReplaceAll(s.nativeCalls["native-item"], []byte("provider-turn"), []byte("other-turn"))
162 case "wrong-session":
163 s.nativeCalls["native-item"] = bytes.ReplaceAll(s.nativeCalls["native-item"], []byte(s.session.ID), []byte("other-session"))
164 }
165 s.mu.Unlock()
166 if l.Respond(context.Background(), s.session, provider.Answer{RequestID: "7", TurnID: "local-turn", Behavior: "allow", Authorize: func(context.Context, provider.Event) error { return nil }}) == nil {
167 t.Fatal("changed file item approved", change)
168 }
169 select {
170 case wire := <-w.sent:
171 t.Fatalf("stale file approval sent: %s", wire)
172 default:
173 }
174 })
175 }
176}
177
178func TestCodexApprovalResolvedDuringAuthorization(t *testing.T) {
179 l, s, w, _ := answerFixture(t)
180 nativeRequest(t, s, "7", "item/commandExecution/requestApproval")
181 err := l.Respond(context.Background(), s.session, provider.Answer{RequestID: "7", TurnID: "local-turn", Behavior: "allow", Authorize: func(context.Context, provider.Event) error {
182 s.mu.Lock()
183 delete(s.requests, "7")
184 s.mu.Unlock()
185 return nil
186 }})
187 if err == nil {
188 t.Fatal("resolved request approved")
189 }
190 select {
191 case wire := <-w.sent:
192 t.Fatalf("resolved approval sent: %s", wire)
193 default:
194 }
195}
196
197func TestCodexApprovalRootGrantRefused(t *testing.T) {
198 l, s, w, _ := answerFixture(t)
199 nativeRequest(t, s, "7", "item/fileChange/requestApproval")
200 s.mu.Lock()
201 ev := s.requests["7"]
202 var frame map[string]any
203 if err := json.Unmarshal(ev.Payload, &frame); err != nil {
204 t.Fatal(err)
205 }
206 frame["params"].(map[string]any)["grantRoot"] = "/workspace"
207 ev.Payload = marshal(frame)
208 s.requests["7"] = ev
209 s.mu.Unlock()
210 if l.Respond(context.Background(), s.session, provider.Answer{RequestID: "7", TurnID: "local-turn", Behavior: "allow", Authorize: func(context.Context, provider.Event) error { return nil }}) == nil {
211 t.Fatal("session root grant accepted")
212 }
213 select {
214 case wire := <-w.sent:
215 t.Fatalf("persistent root grant sent: %s", wire)
216 default:
217 }
218}
219
220func TestCodexApprovalNativeShape(t *testing.T) {
221 for _, method := range []string{"item/commandExecution/requestApproval", "item/fileChange/requestApproval"} {
222 l, s, _, _ := answerFixture(t)
223 _ = l
224 nativeRequest(t, s, "7", method)
225 s.mu.Lock()
226 original := s.requests["7"]
227 s.mu.Unlock()
228 for _, change := range []string{"id", "session", "provider-turn", "item", "turn", "command", "cwd", "file-path", "file-diff", "file-item", "file-turn", "file-session"} {
229 t.Run(method+"/"+change, func(t *testing.T) {
230 ev := original
231 var frame map[string]any
232 if err := json.Unmarshal(ev.Payload, &frame); err != nil {
233 t.Fatal(err)
234 }
235 params := frame["params"].(map[string]any)
236 switch change {
237 case "id":
238 ev.RequestID = `"7"`
239 case "session":
240 ev.SessionID = "other"
241 case "provider-turn":
242 ev.ProviderTurnID = "other"
243 case "item":
244 params["itemId"] = ""
245 case "turn":
246 params["turnId"] = ""
247 case "command", "cwd":
248 if method != "item/commandExecution/requestApproval" {
249 t.Skip()
250 }
251 params[change] = ""
252 default:
253 if method != "item/fileChange/requestApproval" {
254 t.Skip()
255 }
256 var call map[string]any
257 if err := json.Unmarshal(ev.ToolCall, &call); err != nil {
258 t.Fatal(err)
259 }
260 cp := call["params"].(map[string]any)
261 item := cp["item"].(map[string]any)
262 switch change {
263 case "file-path":
264 item["changes"].([]any)[0].(map[string]any)["path"] = ""
265 case "file-diff":
266 delete(item["changes"].([]any)[0].(map[string]any), "diff")
267 case "file-item":
268 item["id"] = "other"
269 case "file-turn":
270 cp["turnId"] = "other"
271 case "file-session":
272 cp["threadId"] = "other"
273 }
274 ev.ToolCall = marshal(call)
275 }
276 ev.Payload = marshal(frame)
277 if _, err := NativeApproval(ev); err == nil {
278 t.Fatal("incomplete or mismatched native action accepted", change)
279 }
280 })
281 }
282 }
283}
284
285func TestCodexApprovalAuthorizationRefused(t *testing.T) {
286 l, s, w, _ := answerFixture(t)
287 nativeRequest(t, s, "7", "item/commandExecution/requestApproval")
288 err := l.Respond(context.Background(), s.session, provider.Answer{RequestID: "7", TurnID: "local-turn", Behavior: "allow", Authorize: func(context.Context, provider.Event) error { return provider.ErrPolicy }})
289 if err == nil {
290 t.Fatal("revoked authorization reported as delivered")
291 }
292 select {
293 case wire := <-w.sent:
294 t.Fatalf("revoked approval produced a response: %s", wire)
295 default:
296 }
297}
298