| 1 | package claude |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "slices" |
| 6 | "testing" |
| 7 | |
| 8 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 9 | ) |
| 10 | |
| 11 | // A Research step (spec §4) runs with no native tools at all: argv says |
| 12 | // --tools "", the tool list is the permission callback and exactly |
| 13 | // submit_findings and keep_image, both allowed without a prompt, and no |
| 14 | // partial-message stream. |
| 15 | func TestResearchStepPolicyNoNativeTools(t *testing.T) { |
| 16 | profile, err := provider.Profile(provider.ResearchStepAction) |
| 17 | if err != nil { |
| 18 | t.Fatal(err) |
| 19 | } |
| 20 | b := provider.Binding{Provider: "claude", CredentialHome: "/private/account", Home: "/private/run", RuntimeRoot: "/private/runtime"} |
| 21 | in := provider.PolicyInputs{Action: provider.ResearchStepAction, Tools: profile.MCPTools()} |
| 22 | got, err := PolicyInputs(b, in, profile.Specialist, "model") |
| 23 | if err != nil { |
| 24 | t.Fatal(err) |
| 25 | } |
| 26 | want := []string{"mcp__equipo__permission", "mcp__equipo__submit_findings", "mcp__equipo__keep_image"} |
| 27 | if !slices.Equal(got.Tools, want) { |
| 28 | t.Fatalf("tools %v, want %v", got.Tools, want) |
| 29 | } |
| 30 | i := slices.Index(got.Argv, "--tools") |
| 31 | if i < 0 || i+1 >= len(got.Argv) || got.Argv[i+1] != "" { |
| 32 | t.Fatalf("argv %q", got.Argv) |
| 33 | } |
| 34 | if slices.Contains(got.Argv, "--include-partial-messages") { |
| 35 | t.Fatal("research streams partial messages") |
| 36 | } |
| 37 | if len(got.NativeTools) != 0 { |
| 38 | t.Fatalf("native tools %v", got.NativeTools) |
| 39 | } |
| 40 | var settings struct { |
| 41 | Permissions struct { |
| 42 | Allow, Ask []string |
| 43 | } `json:"permissions"` |
| 44 | } |
| 45 | if err := json.Unmarshal(got.Settings, &settings); err != nil { |
| 46 | t.Fatal(err) |
| 47 | } |
| 48 | if !slices.Equal(settings.Permissions.Allow, profile.MCPTools()) { |
| 49 | t.Fatalf("allow %v", settings.Permissions.Allow) |
| 50 | } |
| 51 | if len(settings.Permissions.Ask) != 0 { |
| 52 | t.Fatalf("native ask rules %q", settings.Permissions.Ask) |
| 53 | } |
| 54 | // Exactly the profile's tools: another submission, a question, a |
| 55 | // coordinator tool or a git tool is refused. |
| 56 | for _, extra := range []string{"mcp__equipo__ask_person", "mcp__equipo__submit_answer", "mcp__equipo__start_task", "mcp__equipo__git_show"} { |
| 57 | more := in |
| 58 | more.Tools = append(profile.MCPTools(), extra) |
| 59 | if _, err := PolicyInputs(b, more, profile.Specialist, "model"); err == nil { |
| 60 | t.Fatalf("research accepted %s", extra) |
| 61 | } |
| 62 | } |
| 63 | // The legacy Research specialist has Read, Glob, Grep and Bash. |
| 64 | for _, specialist := range []string{"Research", "Engineering", "Coordinator", "Reviewer"} { |
| 65 | if _, err := PolicyInputs(b, in, specialist, "model"); err == nil { |
| 66 | t.Fatalf("research step ran as %s", specialist) |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | // The MCP name whitelist gains submit_findings and keep_image for the |
| 72 | // research step only: a launch with no action, or another action's |
| 73 | // inventory, cannot name them. |
| 74 | func TestResearchToolsWhitelistedOnlyForResearch(t *testing.T) { |
| 75 | b := provider.Binding{Provider: "claude", CredentialHome: "/private/account", Home: "/private/run", RuntimeRoot: "/private/runtime"} |
| 76 | for _, tool := range []string{"mcp__equipo__submit_findings", "mcp__equipo__keep_image"} { |
| 77 | for _, specialist := range []string{"Engineering", "Reviewer", provider.ResearchSpecialist} { |
| 78 | in := provider.PolicyInputs{Tools: []string{tool}} |
| 79 | if _, err := PolicyInputs(b, in, specialist, "model"); err == nil { |
| 80 | t.Fatalf("%s accepted %s with no action", specialist, tool) |
| 81 | } |
| 82 | } |
| 83 | for _, action := range []string{"implement", "inspect", "converse"} { |
| 84 | profile, _ := provider.Profile(action) |
| 85 | in := provider.PolicyInputs{Action: action, Tools: append(profile.MCPTools(), tool)} |
| 86 | if _, err := PolicyInputs(b, in, profile.Specialist, "model"); err == nil { |
| 87 | t.Fatalf("%s accepted %s", action, tool) |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | func researchFixture(t *testing.T) *fixture { |
| 94 | t.Helper() |
| 95 | f := fixtureFor(t, "Engineering") |
| 96 | profile, _ := provider.Profile(provider.ResearchStepAction) |
| 97 | in := f.inputs |
| 98 | in.Action = provider.ResearchStepAction |
| 99 | in.Tools = profile.MCPTools() |
| 100 | in, err := PolicyInputs(f.lease.Binding(), in, profile.Specialist, f.launch.Selection.Model) |
| 101 | if err != nil { |
| 102 | t.Fatal(err) |
| 103 | } |
| 104 | if f.launch.Policy, err = provider.BuildPolicy(f.lease.Binding(), in); err != nil { |
| 105 | t.Fatal(err) |
| 106 | } |
| 107 | f.inputs = in |
| 108 | return f |
| 109 | } |
| 110 | |
| 111 | // The launcher re-derives a Research policy from its manifest and |
| 112 | // accepts it; the init frame must report exactly the two Equipo tools. |
| 113 | func TestResearchStepLaunchAndInit(t *testing.T) { |
| 114 | f := researchFixture(t) |
| 115 | if _, err := f.l.validatePolicy(f.lease.Binding(), f.launch.Policy); err != nil { |
| 116 | t.Fatalf("research policy refused: %v", err) |
| 117 | } |
| 118 | model := f.launch.Selection.Model |
| 119 | init := func(tools ...string) []byte { |
| 120 | raw, _ := json.Marshal(map[string]any{"claude_code_version": Version, "model": model, "permissionMode": "default", "tools": tools, "skills": []string{}, "plugins": []any{}, "mcp_servers": []any{map[string]string{"name": "equipo", "status": "connected"}}}) |
| 121 | return raw |
| 122 | } |
| 123 | if err := ValidateInit(f.launch.Policy, model, init("mcp__equipo__keep_image", "mcp__equipo__submit_findings")); err != nil { |
| 124 | t.Fatalf("honest research init refused: %v", err) |
| 125 | } |
| 126 | for _, native := range []string{"Read", "Bash", "WebFetch", "WebSearch", "Glob"} { |
| 127 | if ValidateInit(f.launch.Policy, model, init("mcp__equipo__submit_findings", "mcp__equipo__keep_image", native)) == nil { |
| 128 | t.Fatalf("research init with %s accepted", native) |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | // ValidateInit refuses a native tool on a research launch even when the |
| 134 | // manifest it is handed lists that tool (the manifest is the launch's |
| 135 | // claim; the research profile is code). |
| 136 | func TestResearchInitRefusesNativeToolInManifest(t *testing.T) { |
| 137 | raw, _ := json.Marshal(map[string]any{"Action": provider.ResearchStepAction, "Tools": []string{"Read", "mcp__equipo__permission", "mcp__equipo__submit_findings", "mcp__equipo__keep_image"}}) |
| 138 | p := provider.Policy{Manifest: raw, ManifestHash: provider.ManifestDigest(raw)} |
| 139 | frame, _ := json.Marshal(map[string]any{"claude_code_version": Version, "model": "model", "permissionMode": "default", "tools": []string{"Read", "mcp__equipo__submit_findings", "mcp__equipo__keep_image"}, "skills": []string{}, "plugins": []any{}, "mcp_servers": []any{map[string]string{"name": "equipo", "status": "connected"}}}) |
| 140 | if ValidateInit(p, "model", frame) == nil { |
| 141 | t.Fatal("research init reporting Read accepted") |
| 142 | } |
| 143 | // The same frame on an action that has Read is the generic check's. |
| 144 | raw, _ = json.Marshal(map[string]any{"Action": "inspect", "Tools": []string{"Read", "mcp__equipo__permission", "mcp__equipo__submit_findings", "mcp__equipo__keep_image"}}) |
| 145 | p = provider.Policy{Manifest: raw, ManifestHash: provider.ManifestDigest(raw)} |
| 146 | if err := ValidateInit(p, "model", frame); err != nil { |
| 147 | t.Fatalf("control frame refused: %v", err) |
| 148 | } |
| 149 | } |
| 150 | |