| 1 | package codex |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "os" |
| 7 | "slices" |
| 8 | "testing" |
| 9 | |
| 10 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 11 | ) |
| 12 | |
| 13 | func TestPlanWorkflowProviderPolicies(t *testing.T) { |
| 14 | for _, action := range []string{"coordinate", "scope", "recommend", "investigate", "implement", "copy", "design", "review"} { |
| 15 | profile, err := provider.Profile(action) |
| 16 | if err != nil { |
| 17 | t.Fatal(err) |
| 18 | } |
| 19 | in := provider.PolicyInputs{Action: action, Tools: profile.MCPTools()} |
| 20 | b := provider.Binding{Provider: "codex", CredentialHome: "/private/account", Home: "/private/run", RuntimeRoot: "/private/runtime"} |
| 21 | got, err := PolicyInputs(b, in, "model") |
| 22 | if err != nil { |
| 23 | t.Fatalf("%s: %v", action, err) |
| 24 | } |
| 25 | for _, name := range profile.MCPTools() { |
| 26 | if !slices.Contains(got.Tools, name) { |
| 27 | t.Fatalf("%s missing %s", action, name) |
| 28 | } |
| 29 | } |
| 30 | var settings map[string]any |
| 31 | if json.Unmarshal(got.Settings, &settings) != nil { |
| 32 | t.Fatal("invalid settings") |
| 33 | } |
| 34 | fs := settings["permissions"].(map[string]any)["equipo"].(map[string]any)["filesystem"].(map[string]any) |
| 35 | if (action == "coordinate" || action == "scope" || action == "recommend" || action == "investigate" || action == "review") && fs["/workspace"] != "read" { |
| 36 | t.Fatal("read-only profile writes") |
| 37 | } |
| 38 | |
| 39 | in.Tools = append(in.Tools, "mcp__equipo__submit_review") |
| 40 | if _, err = PolicyInputs(b, in, "model"); err == nil { |
| 41 | t.Fatal("broadened inventory accepted") |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | func TestPlanWorkflowProviderPoliciesLauncher(t *testing.T) { |
| 47 | t.Parallel() |
| 48 | for _, action := range []string{"coordinate", "scope", "recommend", "investigate", "implement", "copy", "design", "review"} { |
| 49 | t.Run(action, func(t *testing.T) { |
| 50 | t.Parallel() |
| 51 | f := fixtureFor(t) |
| 52 | p, _ := provider.Profile(action) |
| 53 | in := f.inputs |
| 54 | in.Action = action |
| 55 | in.Tools = p.MCPTools() |
| 56 | var err error |
| 57 | in, err = PolicyInputs(f.lease.Binding(), in, f.launch.Selection.Model) |
| 58 | if err != nil { |
| 59 | t.Fatal(err) |
| 60 | } |
| 61 | f.launch.Policy, err = provider.BuildPolicy(f.lease.Binding(), in) |
| 62 | if err != nil { |
| 63 | t.Fatal(err) |
| 64 | } |
| 65 | session, events := openFixture(t, f) |
| 66 | if err = f.l.StartTurn(context.Background(), session, provider.Turn{ID: "plan-turn", AdmissionID: "plan-admission", Prompt: "fixture"}); err != nil { |
| 67 | t.Fatal(err) |
| 68 | } |
| 69 | for e := range events { |
| 70 | if e.Kind == "turn.completed" { |
| 71 | break |
| 72 | } |
| 73 | if e.Kind == "turn.failed" { |
| 74 | t.Fatal("fixture failed") |
| 75 | } |
| 76 | } |
| 77 | if err = f.l.Close(context.Background(), session); err != nil { |
| 78 | t.Fatal(err) |
| 79 | } |
| 80 | }) |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | func TestCoordinatorToolCatalogBound(t *testing.T) { |
| 85 | f := fixtureFor(t) |
| 86 | profile, _ := provider.Profile("coordinate") |
| 87 | in := f.inputs |
| 88 | in.Action = "coordinate" |
| 89 | in.Tools = profile.MCPTools() |
| 90 | var err error |
| 91 | in, err = PolicyInputs(f.lease.Binding(), in, f.launch.Selection.Model) |
| 92 | if err != nil { |
| 93 | t.Fatal(err) |
| 94 | } |
| 95 | p, err := provider.BuildPolicy(f.lease.Binding(), in) |
| 96 | if err != nil { |
| 97 | t.Fatal(err) |
| 98 | } |
| 99 | if _, err = f.l.validatePolicy(f.lease.Binding(), p); err != nil { |
| 100 | t.Fatal(err) |
| 101 | } |
| 102 | for _, m := range p.Mounts { |
| 103 | if m.Target == provider.CoordinatorCatalogPath { |
| 104 | raw, err := os.ReadFile(m.Source) |
| 105 | if err != nil { |
| 106 | t.Fatal(err) |
| 107 | } |
| 108 | if err = p.ValidateFileDigest(m.Source, provider.ManifestDigest(raw)); err != nil { |
| 109 | t.Fatal("catalog missing pinned digest", err) |
| 110 | } |
| 111 | if err = os.WriteFile(m.Source, []byte(`{"models":[]}`), 0600); err != nil { |
| 112 | t.Fatal(err) |
| 113 | } |
| 114 | if p.ValidateSources(f.lease.Binding()) == nil { |
| 115 | t.Fatal("changed catalog accepted") |
| 116 | } |
| 117 | return |
| 118 | } |
| 119 | } |
| 120 | t.Fatal("catalog not mounted") |
| 121 | } |
| 122 | |
| 123 | func TestCoordinatorOnlyMCPServer(t *testing.T) { |
| 124 | for _, mode := range []string{"", "extra-mcp", "mcp"} { |
| 125 | t.Run(mode, func(t *testing.T) { |
| 126 | f := fixtureFor(t) |
| 127 | f.host.set(mode) |
| 128 | in := f.inputs |
| 129 | in.Action = "coordinate" |
| 130 | in.Tools = provider.ActionProfile{Action: "coordinate"}.MCPTools() |
| 131 | in, err := PolicyInputs(f.lease.Binding(), in, f.launch.Selection.Model) |
| 132 | if err != nil { |
| 133 | t.Fatal(err) |
| 134 | } |
| 135 | f.launch.Policy, err = provider.BuildPolicy(f.lease.Binding(), in) |
| 136 | if err != nil { |
| 137 | t.Fatal(err) |
| 138 | } |
| 139 | session, err := f.l.Open(context.Background(), f.launch) |
| 140 | if mode != "" { |
| 141 | if err == nil { |
| 142 | f.l.Close(context.Background(), session) |
| 143 | t.Fatal("additional or replaced MCP server accepted") |
| 144 | } |
| 145 | if f.host.count("thread/start") != 0 { |
| 146 | t.Fatal("thread started with untrusted MCP configuration") |
| 147 | } |
| 148 | return |
| 149 | } |
| 150 | if err != nil { |
| 151 | t.Fatal(err) |
| 152 | } |
| 153 | defer f.l.Close(context.Background(), session) |
| 154 | if _, err = f.l.Events(context.Background(), session); err != nil { |
| 155 | t.Fatal(err) |
| 156 | } |
| 157 | f.host.set("extra-mcp") |
| 158 | if f.l.StartTurn(context.Background(), session, provider.Turn{ID: "turn", AdmissionID: "admission", Prompt: "fixture"}) == nil { |
| 159 | t.Fatal("extra MCP server accepted before turn") |
| 160 | } |
| 161 | if f.host.count("turn/start") != 0 { |
| 162 | t.Fatal("prompt sent with extra server") |
| 163 | } |
| 164 | }) |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | func TestCoordinatorNativePolicyHash(t *testing.T) { |
| 169 | f := fixtureFor(t) |
| 170 | in := f.inputs |
| 171 | in.Action = "coordinate" |
| 172 | in.Tools = provider.ActionProfile{Action: "coordinate"}.MCPTools() |
| 173 | in, err := PolicyInputs(f.lease.Binding(), in, f.launch.Selection.Model) |
| 174 | if err != nil { |
| 175 | t.Fatal(err) |
| 176 | } |
| 177 | p, err := provider.BuildPolicy(f.lease.Binding(), in) |
| 178 | if err != nil { |
| 179 | t.Fatal(err) |
| 180 | } |
| 181 | var m map[string]json.RawMessage |
| 182 | if json.Unmarshal(p.Manifest, &m) != nil { |
| 183 | t.Fatal("bad manifest") |
| 184 | } |
| 185 | for _, native := range [][]string{nil, {"list_mcp_resources"}, {"list_mcp_resources", "list_mcp_resource_templates", "read_mcp_resource", "exec_command"}} { |
| 186 | changed := in |
| 187 | changed.NativeTools = native |
| 188 | if _, err := provider.BuildPolicy(f.lease.Binding(), changed); err == nil { |
| 189 | t.Fatal("changed native policy accepted") |
| 190 | } |
| 191 | } |
| 192 | if !sameJSON(m["NativeTools"], []byte(`["list_mcp_resources","list_mcp_resource_templates","read_mcp_resource"]`)) { |
| 193 | t.Fatal("native resource policy missing from manifest hash") |
| 194 | } |
| 195 | delete(m, "NativeTools") |
| 196 | old, err := json.Marshal(m) |
| 197 | if err != nil || provider.ManifestDigest(old) == p.ManifestHash { |
| 198 | t.Fatal("native policy did not change hash") |
| 199 | } |
| 200 | } |
| 201 | |