| 1 | package thin |
| 2 | |
| 3 | import ( |
| 4 | "bufio" |
| 5 | "bytes" |
| 6 | "context" |
| 7 | "encoding/json" |
| 8 | "io" |
| 9 | "net" |
| 10 | "slices" |
| 11 | "strings" |
| 12 | "testing" |
| 13 | |
| 14 | "amadan.net/oficina/equipo/internal/runner/connect" |
| 15 | "amadan.net/oficina/equipo/internal/wire" |
| 16 | ) |
| 17 | |
| 18 | // A Codex step approves only its own tools, and only the call Codex |
| 19 | // started with those exact arguments; shell, patch and anything else the |
| 20 | // app-server asks is refused. |
| 21 | func TestCodexStepApprovals(t *testing.T) { |
| 22 | var out bytes.Buffer |
| 23 | rpc := connect.NewAppServer(strings.NewReader(""), &out) |
| 24 | items := map[string]codexItem{ |
| 25 | "a": {ID: "a", Type: "mcpToolCall", Server: "equipo", Tool: "submit_review", Arguments: json.RawMessage(`{"verdict":"approve","headline":"Approved.","summary":"ok"}`)}, |
| 26 | "b": {ID: "b", Type: "mcpToolCall", Server: "equipo", Tool: "status", Arguments: json.RawMessage(`{}`)}, |
| 27 | } |
| 28 | names := []string{"submit_review", "ask_person"} |
| 29 | elicit := func(server, args string) json.RawMessage { |
| 30 | return json.RawMessage(`{"serverName":"` + server + `","mode":"form","_meta":{"codex_approval_kind":"mcp_tool_call","tool_params":` + args + `}}`) |
| 31 | } |
| 32 | for _, tc := range []struct { |
| 33 | name, method string |
| 34 | params json.RawMessage |
| 35 | want string |
| 36 | }{ |
| 37 | {"own tool, same arguments", "mcpServer/elicitation/request", elicit("equipo", `{"summary":"ok","verdict":"approve","headline":"Approved."}`), `"accept"`}, |
| 38 | {"own tool, other arguments", "mcpServer/elicitation/request", elicit("equipo", `{"verdict":"changes_requested","headline":"x","summary":"ok"}`), `"decline"`}, |
| 39 | {"a tool the step does not have", "mcpServer/elicitation/request", elicit("equipo", `{}`), `"decline"`}, |
| 40 | {"another server", "mcpServer/elicitation/request", elicit("other", `{"verdict":"approve","headline":"Approved.","summary":"ok"}`), `"decline"`}, |
| 41 | {"shell", "item/commandExecution/requestApproval", json.RawMessage(`{"command":"curl example.com"}`), `refused by policy`}, |
| 42 | {"patch", "item/fileChange/requestApproval", json.RawMessage(`{}`), `refused by policy`}, |
| 43 | } { |
| 44 | t.Run(tc.name, func(t *testing.T) { |
| 45 | out.Reset() |
| 46 | accepted := answerApproval(rpc, connect.Message{ID: json.RawMessage(`"s1"`), Method: tc.method, Params: tc.params}, items, names) |
| 47 | if !strings.Contains(out.String(), tc.want) || tc.want == `"decline"` && strings.Contains(out.String(), `"accept"`) { |
| 48 | t.Fatalf("answer %s, want %s", out.String(), tc.want) |
| 49 | } |
| 50 | // A refusal is counted by the step (its reason when it then |
| 51 | // commits nothing is tools_refused). |
| 52 | if accepted != (tc.want == `"accept"`) { |
| 53 | t.Fatalf("accepted %v for %s", accepted, out.String()) |
| 54 | } |
| 55 | }) |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | // A Codex step's tool socket lists only the step's tools: Codex asks |
| 60 | // through the app-server, so there is no permission tool to call. |
| 61 | func TestCodexStepSocketHasNoPermission(t *testing.T) { |
| 62 | a, b := net.Pipe() |
| 63 | defer a.Close() |
| 64 | call := func(context.Context, string, string, json.RawMessage) (string, bool) { return "Recorded.", false } |
| 65 | go serveMCP(context.Background(), b, toolSet{list: reviewTools, names: []string{"submit_review", "ask_person"}}, textOnly(call)) |
| 66 | r := bufio.NewReader(a) |
| 67 | ask := func(q string) map[string]any { |
| 68 | t.Helper() |
| 69 | if _, err := io.WriteString(a, q+"\n"); err != nil { |
| 70 | t.Fatal(err) |
| 71 | } |
| 72 | line, err := r.ReadBytes('\n') |
| 73 | var v map[string]any |
| 74 | if err != nil || json.Unmarshal(line, &v) != nil { |
| 75 | t.Fatal(err) |
| 76 | } |
| 77 | return v |
| 78 | } |
| 79 | ask(`{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-06-18"}}`) |
| 80 | var names []string |
| 81 | for _, tool := range ask(`{"jsonrpc":"2.0","id":2,"method":"tools/list"}`)["result"].(map[string]any)["tools"].([]any) { |
| 82 | names = append(names, tool.(map[string]any)["name"].(string)) |
| 83 | } |
| 84 | if !slices.Equal(names, []string{"submit_review", "ask_person"}) { |
| 85 | t.Fatalf("tools %v", names) |
| 86 | } |
| 87 | if v := ask(`{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"permission","arguments":{"tool_name":"Bash","input":{}}}}`); v["error"] == nil { |
| 88 | t.Fatalf("permission answered on a Codex step: %v", v) |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | // What a Codex step shows while it runs: commands and file changes, |
| 93 | // without the /workspace prefix, and the tools it calls. |
| 94 | func TestCodexStepActivity(t *testing.T) { |
| 95 | edit := codexItem{Type: "fileChange"} |
| 96 | edit.Changes = append(edit.Changes, struct { |
| 97 | Path string `json:"path"` |
| 98 | }{Path: "/workspace/README.md"}) |
| 99 | for _, tc := range []struct { |
| 100 | item codexItem |
| 101 | kind, subject, line string |
| 102 | }{ |
| 103 | {codexItem{Type: "commandExecution", Command: "go test ./...\nsecond line"}, wire.ActivityRun, "go test ./...", "Bash go test ./..."}, |
| 104 | {edit, wire.ActivityEdit, "README.md", "Edit README.md"}, |
| 105 | {codexItem{Type: "mcpToolCall", Server: "equipo", Tool: "submit_candidate"}, wire.ActivityOther, "", "submit_candidate"}, |
| 106 | {codexItem{Type: "reasoning", Text: "secret plan"}, "", "", ""}, |
| 107 | } { |
| 108 | kind, subject, line := itemActivity(tc.item) |
| 109 | if kind != tc.kind || subject != tc.subject || line != tc.line { |
| 110 | t.Fatalf("%+v: %q %q %q", tc.item, kind, subject, line) |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |