| 1 | package codex |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "slices" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | |
| 9 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 10 | ) |
| 11 | |
| 12 | // A Research step on Codex (spec §4) gets the coordinator's surface with |
| 13 | // the step's own tools: every feature off, web search disabled, no |
| 14 | // shell or patch in the catalog, network off, and the Equipo server |
| 15 | // limited to exactly submit_findings and keep_image. |
| 16 | func TestResearchStepCodexPolicy(t *testing.T) { |
| 17 | b := provider.Binding{Provider: "codex", CredentialHome: "/private/account", Home: "/private/run", RuntimeRoot: "/private/runtime"} |
| 18 | profile, err := provider.Profile(provider.ResearchStepAction) |
| 19 | if err != nil { |
| 20 | t.Fatal(err) |
| 21 | } |
| 22 | in := provider.PolicyInputs{Action: provider.ResearchStepAction, Tools: profile.MCPTools(), DeniedPaths: []string{"/private/state"}} |
| 23 | got, err := PolicyInputs(b, in, "gpt-6-astra") |
| 24 | if err != nil { |
| 25 | t.Fatal(err) |
| 26 | } |
| 27 | var settings map[string]any |
| 28 | if json.Unmarshal(got.Settings, &settings) != nil { |
| 29 | t.Fatal("invalid settings") |
| 30 | } |
| 31 | if settings["web_search"] != "disabled" { |
| 32 | t.Fatalf("web_search %v", settings["web_search"]) |
| 33 | } |
| 34 | features := settings["features"].(map[string]any) |
| 35 | for key, v := range features { |
| 36 | // incremental_tools only changes how tools are listed; the MCP |
| 37 | // elicitation switch is how Equipo's own tools ask for approval. |
| 38 | if key == "incremental_tools" || key == "tool_call_mcp_elicitation" { |
| 39 | continue |
| 40 | } |
| 41 | if v != false { |
| 42 | t.Fatalf("feature %s is %v", key, v) |
| 43 | } |
| 44 | } |
| 45 | for _, off := range []string{"shell_tool", "unified_exec", "apply_patch_freeform", "view_image", "web_search", "web_search_request", "standalone_web_search", "browser_use", "computer_use", "js_repl", "code_mode", "multi_agent_v2", "plugins", "memories"} { |
| 46 | if v, ok := features[off]; !ok || v != false { |
| 47 | t.Fatalf("feature %s not off: %v", off, v) |
| 48 | } |
| 49 | } |
| 50 | tools := settings["tools"].(map[string]any) |
| 51 | for _, name := range []string{"update_plan", "experimental_request_user_input"} { |
| 52 | if v, _ := json.Marshal(tools[name]); string(v) != `{"enabled":false}` { |
| 53 | t.Fatalf("tool %s: %s", name, v) |
| 54 | } |
| 55 | } |
| 56 | perm := settings["permissions"].(map[string]any)["equipo"].(map[string]any) |
| 57 | if perm["network"].(map[string]any)["enabled"] != false { |
| 58 | t.Fatal("network on") |
| 59 | } |
| 60 | fs := perm["filesystem"].(map[string]any) |
| 61 | for _, denied := range []string{"/private/account", "/private/run", "/private/state", "/home/runner", "/tool", "/egress", "/config", "/proc"} { |
| 62 | if fs[denied] != "deny" { |
| 63 | t.Fatalf("%s not denied", denied) |
| 64 | } |
| 65 | } |
| 66 | server := settings["mcp_servers"].(map[string]any)["equipo"].(map[string]any) |
| 67 | var enabled []string |
| 68 | raw, _ := json.Marshal(server["enabled_tools"]) |
| 69 | json.Unmarshal(raw, &enabled) |
| 70 | if !slices.Equal(enabled, []string{"submit_findings", "keep_image"}) { |
| 71 | t.Fatalf("enabled tools %v", enabled) |
| 72 | } |
| 73 | if !slices.Equal(got.NativeTools, provider.CoordinatorNativeTools("codex")) { |
| 74 | t.Fatalf("native tools %v", got.NativeTools) |
| 75 | } |
| 76 | if !strings.Contains(string(got.Catalog), `"shell_type":"disabled"`) || !strings.Contains(string(got.Catalog), `"apply_patch_tool_type":null`) { |
| 77 | t.Fatalf("catalog %s", got.Catalog) |
| 78 | } |
| 79 | for _, extra := range []string{"mcp__equipo__ask_person", "mcp__equipo__submit_answer", "mcp__equipo__start_task", "mcp__equipo__git_show"} { |
| 80 | more := in |
| 81 | more.Tools = append(profile.MCPTools(), extra) |
| 82 | if _, err := PolicyInputs(b, more, "gpt-6-astra"); err == nil { |
| 83 | t.Fatalf("research accepted %s", extra) |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | // The research tools are not on any other Codex launch's list. |
| 89 | func TestResearchToolsCodexOnlyForResearch(t *testing.T) { |
| 90 | b := provider.Binding{Provider: "codex", CredentialHome: "/private/account", Home: "/private/run", RuntimeRoot: "/private/runtime"} |
| 91 | for _, tool := range []string{"mcp__equipo__submit_findings", "mcp__equipo__keep_image"} { |
| 92 | if _, err := PolicyInputs(b, provider.PolicyInputs{Tools: []string{tool}}, "gpt-6-astra"); err == nil { |
| 93 | t.Fatalf("no-action launch accepted %s", tool) |
| 94 | } |
| 95 | for _, action := range []string{"implement", "inspect"} { |
| 96 | profile, _ := provider.Profile(action) |
| 97 | if _, err := PolicyInputs(b, provider.PolicyInputs{Action: action, Tools: append(profile.MCPTools(), tool)}, "gpt-6-astra"); err == nil { |
| 98 | t.Fatalf("%s accepted %s", action, tool) |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | // The launcher accepts the rendered Research policy, and config readback |
| 105 | // holds it to the coordinator's tool switches. |
| 106 | func TestResearchStepCodexLaunchAndReadback(t *testing.T) { |
| 107 | f := fixtureFor(t) |
| 108 | in := f.inputs |
| 109 | in.Action = provider.ResearchStepAction |
| 110 | profile, _ := provider.Profile(in.Action) |
| 111 | in.Tools = profile.MCPTools() |
| 112 | in, err := PolicyInputs(f.lease.Binding(), in, "gpt-6-astra") |
| 113 | if err != nil { |
| 114 | t.Fatal(err) |
| 115 | } |
| 116 | p, err := provider.BuildPolicy(f.lease.Binding(), in) |
| 117 | if err != nil { |
| 118 | t.Fatal(err) |
| 119 | } |
| 120 | if _, err = f.l.validatePolicy(f.lease.Binding(), p); err != nil { |
| 121 | t.Fatalf("research policy refused: %v", err) |
| 122 | } |
| 123 | if !p.Research() { |
| 124 | t.Fatal("research predicate") |
| 125 | } |
| 126 | for _, mode := range []string{"valid", "enabled-plan", "enabled-question", "shell", "web-search"} { |
| 127 | t.Run(mode, func(t *testing.T) { |
| 128 | var cfg map[string]any |
| 129 | json.Unmarshal(in.Settings, &cfg) |
| 130 | layer := json.RawMessage(in.Settings) |
| 131 | effective := map[string]any{} |
| 132 | for k, v := range cfg { |
| 133 | effective[k] = v |
| 134 | } |
| 135 | // 0.165.0 omits disabled tools from the typed effective config. |
| 136 | effective["tools"] = map[string]any{"web_search": nil} |
| 137 | switch mode { |
| 138 | case "enabled-plan": |
| 139 | effective["tools"] = map[string]any{"web_search": nil, "update_plan": map[string]any{"enabled": true}} |
| 140 | case "enabled-question": |
| 141 | effective["tools"] = map[string]any{"web_search": nil, "experimental_request_user_input": map[string]any{"enabled": true}} |
| 142 | case "shell": |
| 143 | features := map[string]any{} |
| 144 | for k, v := range cfg["features"].(map[string]any) { |
| 145 | features[k] = v |
| 146 | } |
| 147 | features["shell_tool"] = true |
| 148 | effective["features"] = features |
| 149 | case "web-search": |
| 150 | effective["web_search"] = "live" |
| 151 | } |
| 152 | readback := marshal(map[string]any{"config": effective, "origins": map[string]any{}, "layers": []any{map[string]any{"name": map[string]any{"type": "sessionFlags"}, "version": "fixture", "config": layer}}}) |
| 153 | if got := validateConfig(readback, p); (got == nil) != (mode == "valid") { |
| 154 | t.Fatalf("readback %s: %v", mode, got) |
| 155 | } |
| 156 | }) |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | // The recorded live coordinator readback (update_plan off in the layer) |
| 161 | // is accepted for a research launch as for the coordinator's, and its |
| 162 | // strict modes still refuse. |
| 163 | func TestResearchStepLiveReadback(t *testing.T) { |
| 164 | v, p := liveConfig(t) |
| 165 | p.Manifest = marshal(map[string]any{"Action": provider.ResearchStepAction, "ProviderVersion": Version}) |
| 166 | p.ManifestHash = provider.ManifestDigest(p.Manifest) |
| 167 | if err := validateConfig(marshal(v), p); err != nil { |
| 168 | t.Fatalf("research readback refused: %v", err) |
| 169 | } |
| 170 | v["config"].(map[string]any)["tools"].(map[string]any)["update_plan"] = map[string]any{"enabled": true} |
| 171 | if validateConfig(marshal(v), p) == nil { |
| 172 | t.Fatal("enabled update_plan accepted") |
| 173 | } |
| 174 | } |
| 175 | |