package codex import ( "bytes" "context" "encoding/json" "fmt" "testing" "amadan.net/oficina/equipo/internal/runner/provider" ) func nativeRequest(t *testing.T, s *sessionState, id, method string) []byte { t.Helper() params := map[string]any{"threadId": s.session.ID, "turnId": "provider-turn", "itemId": "native-item", "startedAtMs": 1} if method == "item/commandExecution/requestApproval" { params["command"] = "printf '%s' hello" params["cwd"] = "/workspace" params["proposedExecpolicyAmendment"] = []string{"printf"} } else { 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"}}}}}) if err := s.handle(provider.Frame{Sequence: 98, Bytes: item}); err != nil { t.Fatal(err) } } raw := marshal(map[string]any{"id": json.RawMessage(id), "method": method, "params": params}) if err := s.handle(provider.Frame{Sequence: 99, Bytes: raw}); err != nil { t.Fatal(err) } return raw } func TestCodexApprovalAllowOnce(t *testing.T) { for _, method := range []string{"item/commandExecution/requestApproval", "item/fileChange/requestApproval"} { t.Run(method, func(t *testing.T) { l, s, w, _ := answerFixture(t) before := marshal(s.launch.Policy) for _, id := range []string{"7", `"7"`} { raw := nativeRequest(t, s, id, method) checked := 0 a := provider.Answer{RequestID: id, TurnID: "local-turn", Behavior: "allow", Authorize: func(_ context.Context, ev provider.Event) error { checked++ if ev.RequestID != id || !bytes.Equal(ev.Payload, raw) { t.Fatal("original request changed") } if method == "item/fileChange/requestApproval" && !bytes.Contains(ev.ToolCall, []byte("/workspace/a.txt")) { t.Fatal("file payload missing") } return nil }} if err := l.Respond(context.Background(), s.session, a); err != nil { t.Fatal("allow once refused", err) } wire := <-w.sent if string(wire) != fmt.Sprintf(`{"id":%s,"result":{"decision":"accept"}}`+"\n", id) { t.Fatalf("broader or wrong response: %s", wire) } if checked != 1 { t.Fatal("original not authorized") } if l.Respond(context.Background(), s.session, a) == nil { t.Fatal("request replay approved") } } if !bytes.Equal(before, marshal(s.launch.Policy)) { t.Fatal("policy hash, manifest, network or namespace mounts changed") } }) } } func TestCodexApprovalPersistentRefused(t *testing.T) { l, s, w, _ := answerFixture(t) nativeRequest(t, s, "7", "item/commandExecution/requestApproval") for _, behavior := range []string{"acceptForSession", "approved_for_session", "allow_always", "acceptWithExecpolicyAmendment", "applyNetworkPolicyAmendment", "approved_mcp_policy_amendment"} { 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 { t.Fatal("persistent option accepted", behavior) } } for _, input := range []string{`{"decision":"acceptForSession"}`, `{"execpolicy_amendment":["printf"]}`, `{"network_policy_amendment":{"host":"example.com","action":"allow"}}`, `{"grantRoot":"/"}`} { 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 { t.Fatal("amendment accepted", input) } } select { case wire := <-w.sent: t.Fatalf("refusal sent a response: %s", wire) default: } if err := l.Respond(context.Background(), s.session, provider.Answer{RequestID: "7", TurnID: "local-turn", Behavior: "deny"}); err != nil { t.Fatal(err) } if !bytes.Contains(<-w.sent, []byte(`"decision":"decline"`)) { t.Fatal("deny changed") } } func TestCodexApprovalLegacyOnce(t *testing.T) { for _, method := range []string{"execCommandApproval", "applyPatchApproval"} { t.Run(method, func(t *testing.T) { l, s, w, _ := answerFixture(t) params := map[string]any{"conversationId": s.session.ID, "callId": "call"} if method == "execCommandApproval" { params["command"] = []string{"/bin/sh", "-c", "printf hello"} params["cwd"] = "/workspace" params["parsedCmd"] = []any{} } else { params["fileChanges"] = map[string]any{"/workspace/a.txt": map[string]any{"type": "add", "content": "hello"}} } raw := marshal(map[string]any{"id": 7, "method": method, "params": params}) if err := s.handle(provider.Frame{Sequence: 99, Bytes: raw}); err != nil { t.Fatal("pinned legacy request not relayed", err) } a := provider.Answer{RequestID: "7", TurnID: "local-turn", Behavior: "allow", Authorize: func(_ context.Context, ev provider.Event) error { if !bytes.Equal(ev.Payload, raw) { t.Fatal("argv or paths changed") } return nil }} if err := l.Respond(context.Background(), s.session, a); err != nil { t.Fatal(err) } if wire := <-w.sent; string(wire) != `{"id":7,"result":{"decision":"approved"}}`+"\n" { t.Fatalf("legacy scope broadened: %s", wire) } denied := marshal(map[string]any{"id": 9, "method": method, "params": params}) if err := s.handle(provider.Frame{Sequence: 100, Bytes: denied}); err != nil { t.Fatal(err) } if err := l.Respond(context.Background(), s.session, provider.Answer{RequestID: "9", TurnID: "local-turn", Behavior: "deny"}); err != nil { t.Fatal(err) } if wire := <-w.sent; string(wire) != `{"id":9,"result":{"decision":{"denied":{"rejection":""}}}}`+"\n" { t.Fatalf("legacy deny changed: %s", wire) } select { case wire := <-w.sent: t.Fatalf("extra response: %s", wire) default: } }) } } func TestCodexApprovalFileCorrelation(t *testing.T) { for _, change := range []string{"completed", "changed", "missing", "wrong-item", "wrong-turn", "wrong-session"} { t.Run(change, func(t *testing.T) { l, s, w, _ := answerFixture(t) nativeRequest(t, s, "7", "item/fileChange/requestApproval") // Change the captured item after the approval was raised. s.mu.Lock() switch change { case "completed", "missing": delete(s.nativeCalls, "native-item") case "changed": s.nativeCalls["native-item"] = bytes.ReplaceAll(s.nativeCalls["native-item"], []byte("a.txt"), []byte("changed.txt")) case "wrong-item": s.nativeCalls["native-item"] = bytes.ReplaceAll(s.nativeCalls["native-item"], []byte("native-item"), []byte("other")) case "wrong-turn": s.nativeCalls["native-item"] = bytes.ReplaceAll(s.nativeCalls["native-item"], []byte("provider-turn"), []byte("other-turn")) case "wrong-session": s.nativeCalls["native-item"] = bytes.ReplaceAll(s.nativeCalls["native-item"], []byte(s.session.ID), []byte("other-session")) } s.mu.Unlock() 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 { t.Fatal("changed file item approved", change) } select { case wire := <-w.sent: t.Fatalf("stale file approval sent: %s", wire) default: } }) } } func TestCodexApprovalResolvedDuringAuthorization(t *testing.T) { l, s, w, _ := answerFixture(t) nativeRequest(t, s, "7", "item/commandExecution/requestApproval") err := l.Respond(context.Background(), s.session, provider.Answer{RequestID: "7", TurnID: "local-turn", Behavior: "allow", Authorize: func(context.Context, provider.Event) error { s.mu.Lock() delete(s.requests, "7") s.mu.Unlock() return nil }}) if err == nil { t.Fatal("resolved request approved") } select { case wire := <-w.sent: t.Fatalf("resolved approval sent: %s", wire) default: } } func TestCodexApprovalRootGrantRefused(t *testing.T) { l, s, w, _ := answerFixture(t) nativeRequest(t, s, "7", "item/fileChange/requestApproval") s.mu.Lock() ev := s.requests["7"] var frame map[string]any if err := json.Unmarshal(ev.Payload, &frame); err != nil { t.Fatal(err) } frame["params"].(map[string]any)["grantRoot"] = "/workspace" ev.Payload = marshal(frame) s.requests["7"] = ev s.mu.Unlock() 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 { t.Fatal("session root grant accepted") } select { case wire := <-w.sent: t.Fatalf("persistent root grant sent: %s", wire) default: } } func TestCodexApprovalNativeShape(t *testing.T) { for _, method := range []string{"item/commandExecution/requestApproval", "item/fileChange/requestApproval"} { l, s, _, _ := answerFixture(t) _ = l nativeRequest(t, s, "7", method) s.mu.Lock() original := s.requests["7"] s.mu.Unlock() for _, change := range []string{"id", "session", "provider-turn", "item", "turn", "command", "cwd", "file-path", "file-diff", "file-item", "file-turn", "file-session"} { t.Run(method+"/"+change, func(t *testing.T) { ev := original var frame map[string]any if err := json.Unmarshal(ev.Payload, &frame); err != nil { t.Fatal(err) } params := frame["params"].(map[string]any) switch change { case "id": ev.RequestID = `"7"` case "session": ev.SessionID = "other" case "provider-turn": ev.ProviderTurnID = "other" case "item": params["itemId"] = "" case "turn": params["turnId"] = "" case "command", "cwd": if method != "item/commandExecution/requestApproval" { t.Skip() } params[change] = "" default: if method != "item/fileChange/requestApproval" { t.Skip() } var call map[string]any if err := json.Unmarshal(ev.ToolCall, &call); err != nil { t.Fatal(err) } cp := call["params"].(map[string]any) item := cp["item"].(map[string]any) switch change { case "file-path": item["changes"].([]any)[0].(map[string]any)["path"] = "" case "file-diff": delete(item["changes"].([]any)[0].(map[string]any), "diff") case "file-item": item["id"] = "other" case "file-turn": cp["turnId"] = "other" case "file-session": cp["threadId"] = "other" } ev.ToolCall = marshal(call) } ev.Payload = marshal(frame) if _, err := NativeApproval(ev); err == nil { t.Fatal("incomplete or mismatched native action accepted", change) } }) } } } func TestCodexApprovalAuthorizationRefused(t *testing.T) { l, s, w, _ := answerFixture(t) nativeRequest(t, s, "7", "item/commandExecution/requestApproval") 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 }}) if err == nil { t.Fatal("revoked authorization reported as delivered") } select { case wire := <-w.sent: t.Fatalf("revoked approval produced a response: %s", wire) default: } }