package codex import ( "context" "encoding/json" "os" "slices" "testing" "amadan.net/oficina/equipo/internal/runner/provider" ) func TestPlanWorkflowProviderPolicies(t *testing.T) { for _, action := range []string{"coordinate", "scope", "recommend", "investigate", "implement", "copy", "design", "review"} { profile, err := provider.Profile(action) if err != nil { t.Fatal(err) } in := provider.PolicyInputs{Action: action, Tools: profile.MCPTools()} b := provider.Binding{Provider: "codex", CredentialHome: "/private/account", Home: "/private/run", RuntimeRoot: "/private/runtime"} got, err := PolicyInputs(b, in, "model") if err != nil { t.Fatalf("%s: %v", action, err) } for _, name := range profile.MCPTools() { if !slices.Contains(got.Tools, name) { t.Fatalf("%s missing %s", action, name) } } var settings map[string]any if json.Unmarshal(got.Settings, &settings) != nil { t.Fatal("invalid settings") } fs := settings["permissions"].(map[string]any)["equipo"].(map[string]any)["filesystem"].(map[string]any) if (action == "coordinate" || action == "scope" || action == "recommend" || action == "investigate" || action == "review") && fs["/workspace"] != "read" { t.Fatal("read-only profile writes") } in.Tools = append(in.Tools, "mcp__equipo__submit_review") if _, err = PolicyInputs(b, in, "model"); err == nil { t.Fatal("broadened inventory accepted") } } } func TestPlanWorkflowProviderPoliciesLauncher(t *testing.T) { t.Parallel() for _, action := range []string{"coordinate", "scope", "recommend", "investigate", "implement", "copy", "design", "review"} { t.Run(action, func(t *testing.T) { t.Parallel() f := fixtureFor(t) p, _ := provider.Profile(action) in := f.inputs in.Action = action in.Tools = p.MCPTools() var err error in, err = PolicyInputs(f.lease.Binding(), in, f.launch.Selection.Model) if err != nil { t.Fatal(err) } f.launch.Policy, err = provider.BuildPolicy(f.lease.Binding(), in) if err != nil { t.Fatal(err) } session, events := openFixture(t, f) if err = f.l.StartTurn(context.Background(), session, provider.Turn{ID: "plan-turn", AdmissionID: "plan-admission", Prompt: "fixture"}); err != nil { t.Fatal(err) } for e := range events { if e.Kind == "turn.completed" { break } if e.Kind == "turn.failed" { t.Fatal("fixture failed") } } if err = f.l.Close(context.Background(), session); err != nil { t.Fatal(err) } }) } } func TestCoordinatorToolCatalogBound(t *testing.T) { f := fixtureFor(t) profile, _ := provider.Profile("coordinate") in := f.inputs in.Action = "coordinate" in.Tools = profile.MCPTools() var err error in, err = PolicyInputs(f.lease.Binding(), in, f.launch.Selection.Model) if err != nil { t.Fatal(err) } p, err := provider.BuildPolicy(f.lease.Binding(), in) if err != nil { t.Fatal(err) } if _, err = f.l.validatePolicy(f.lease.Binding(), p); err != nil { t.Fatal(err) } for _, m := range p.Mounts { if m.Target == provider.CoordinatorCatalogPath { raw, err := os.ReadFile(m.Source) if err != nil { t.Fatal(err) } if err = p.ValidateFileDigest(m.Source, provider.ManifestDigest(raw)); err != nil { t.Fatal("catalog missing pinned digest", err) } if err = os.WriteFile(m.Source, []byte(`{"models":[]}`), 0600); err != nil { t.Fatal(err) } if p.ValidateSources(f.lease.Binding()) == nil { t.Fatal("changed catalog accepted") } return } } t.Fatal("catalog not mounted") } func TestCoordinatorOnlyMCPServer(t *testing.T) { for _, mode := range []string{"", "extra-mcp", "mcp"} { t.Run(mode, func(t *testing.T) { f := fixtureFor(t) f.host.set(mode) in := f.inputs in.Action = "coordinate" in.Tools = provider.ActionProfile{Action: "coordinate"}.MCPTools() in, err := PolicyInputs(f.lease.Binding(), in, f.launch.Selection.Model) if err != nil { t.Fatal(err) } f.launch.Policy, err = provider.BuildPolicy(f.lease.Binding(), in) if err != nil { t.Fatal(err) } session, err := f.l.Open(context.Background(), f.launch) if mode != "" { if err == nil { f.l.Close(context.Background(), session) t.Fatal("additional or replaced MCP server accepted") } if f.host.count("thread/start") != 0 { t.Fatal("thread started with untrusted MCP configuration") } return } if err != nil { t.Fatal(err) } defer f.l.Close(context.Background(), session) if _, err = f.l.Events(context.Background(), session); err != nil { t.Fatal(err) } f.host.set("extra-mcp") if f.l.StartTurn(context.Background(), session, provider.Turn{ID: "turn", AdmissionID: "admission", Prompt: "fixture"}) == nil { t.Fatal("extra MCP server accepted before turn") } if f.host.count("turn/start") != 0 { t.Fatal("prompt sent with extra server") } }) } } func TestCoordinatorNativePolicyHash(t *testing.T) { f := fixtureFor(t) in := f.inputs in.Action = "coordinate" in.Tools = provider.ActionProfile{Action: "coordinate"}.MCPTools() in, err := PolicyInputs(f.lease.Binding(), in, f.launch.Selection.Model) if err != nil { t.Fatal(err) } p, err := provider.BuildPolicy(f.lease.Binding(), in) if err != nil { t.Fatal(err) } var m map[string]json.RawMessage if json.Unmarshal(p.Manifest, &m) != nil { t.Fatal("bad manifest") } for _, native := range [][]string{nil, {"list_mcp_resources"}, {"list_mcp_resources", "list_mcp_resource_templates", "read_mcp_resource", "exec_command"}} { changed := in changed.NativeTools = native if _, err := provider.BuildPolicy(f.lease.Binding(), changed); err == nil { t.Fatal("changed native policy accepted") } } if !sameJSON(m["NativeTools"], []byte(`["list_mcp_resources","list_mcp_resource_templates","read_mcp_resource"]`)) { t.Fatal("native resource policy missing from manifest hash") } delete(m, "NativeTools") old, err := json.Marshal(m) if err != nil || provider.ManifestDigest(old) == p.ManifestHash { t.Fatal("native policy did not change hash") } }