| 1 | package claude |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "os" |
| 7 | "reflect" |
| 8 | "slices" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | |
| 12 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 13 | "amadan.net/oficina/equipo/internal/specialist" |
| 14 | ) |
| 15 | |
| 16 | // engineeringPermissionRequests are the native file and shell requests |
| 17 | // whose answers pin Engineering's permission callback, /render's above |
| 18 | // all (a path Engineering never reads). |
| 19 | var engineeringPermissionRequests = []string{ |
| 20 | `{"tool_name":"Read","input":{"file_path":"/workspace/README.md"}}`, |
| 21 | `{"tool_name":"Write","input":{"file_path":"/workspace/mockups/tk1/index.html","content":"<p>x</p>"}}`, |
| 22 | `{"tool_name":"Edit","input":{"file_path":"/workspace/README.md","old_string":"teh","new_string":"the"}}`, |
| 23 | `{"tool_name":"Glob","input":{"pattern":"*.md","path":"/workspace"}}`, |
| 24 | `{"tool_name":"Grep","input":{"pattern":"x","path":"/workspace"}}`, |
| 25 | `{"tool_name":"Read","input":{"file_path":"/research/img1-logo.png"}}`, |
| 26 | `{"tool_name":"Write","input":{"file_path":"/research/img9.png","content":"x"}}`, |
| 27 | `{"tool_name":"Read","input":{"file_path":"/render/1-work-1.png"}}`, |
| 28 | `{"tool_name":"Glob","input":{"pattern":"*.png","path":"/render"}}`, |
| 29 | `{"tool_name":"Grep","input":{"pattern":"x","path":"/render/"}}`, |
| 30 | `{"tool_name":"Write","input":{"file_path":"/render/planted.png","content":"x"}}`, |
| 31 | `{"tool_name":"Edit","input":{"file_path":"/render/1-work-1.png","old_string":"a","new_string":"b"}}`, |
| 32 | `{"tool_name":"Read","input":{"file_path":"/render/../etc/passwd"}}`, |
| 33 | `{"tool_name":"Read","input":{"file_path":"/renders/a.png"}}`, |
| 34 | `{"tool_name":"Read","input":{"file_path":"/etc/passwd"}}`, |
| 35 | `{"tool_name":"Read","input":{"file_path":"/tmp/a"}}`, |
| 36 | `{"tool_name":"Write","input":{"file_path":"/tool/equipo.sock","content":"x"}}`, |
| 37 | `{"tool_name":"Bash","input":{"command":"ls /render"}}`, |
| 38 | `{"tool_name":"Bash","input":{"command":"ls","dangerouslyDisableSandbox":true}}`, |
| 39 | `{"tool_name":"WebFetch","input":{"url":"https://example.com"}}`, |
| 40 | `{"tool_name":"mcp__equipo__render_page","input":{}}`, |
| 41 | `not json`, |
| 42 | } |
| 43 | |
| 44 | // fixedBinding is a binding with fixed paths, so a policy's bytes are |
| 45 | // the same on every run. |
| 46 | var fixedBinding = provider.Binding{Provider: "claude", CredentialHome: "/private/account", Home: "/private/run", RuntimeRoot: "/private/runtime"} |
| 47 | |
| 48 | func policyGolden(t *testing.T, action, role string) []byte { |
| 49 | t.Helper() |
| 50 | p, err := provider.Profile(action) |
| 51 | if err != nil { |
| 52 | t.Fatal(err) |
| 53 | } |
| 54 | got, err := PolicyInputs(fixedBinding, provider.PolicyInputs{Action: action, Tools: p.MCPTools(), DeniedPaths: []string{"/private/state"}}, role, "claude-opus-5-5") |
| 55 | if err != nil { |
| 56 | t.Fatal(err) |
| 57 | } |
| 58 | raw, _ := json.MarshalIndent(map[string]any{"tools": got.Tools, "argv": got.Argv, "denied": got.DeniedPaths, "settings": string(got.Settings), "mcp": string(got.MCP), "thread": string(got.ThreadPolicy), "turn": string(got.TurnPolicy)}, "", " ") |
| 59 | return append(raw, '\n') |
| 60 | } |
| 61 | |
| 62 | // TestEngineeringPolicyUnchanged: Engineering's Claude policy (tools, |
| 63 | // argv, settings with every allow, ask and deny rule, the MCP and thread |
| 64 | // policies) and its permission callback's answer to each request are |
| 65 | // byte for byte what they were before Design (testdata, recorded at |
| 66 | // 6cc538be). |
| 67 | func TestEngineeringPolicyUnchanged(t *testing.T) { |
| 68 | want, err := os.ReadFile("testdata/engineering-policy.golden.json") |
| 69 | if err != nil { |
| 70 | t.Fatal(err) |
| 71 | } |
| 72 | if got := policyGolden(t, "implement", "Engineering"); string(got) != string(want) { |
| 73 | t.Fatalf("Engineering's policy changed:\n%s", got) |
| 74 | } |
| 75 | want, err = os.ReadFile("testdata/engineering-permission.golden.txt") |
| 76 | if err != nil { |
| 77 | t.Fatal(err) |
| 78 | } |
| 79 | f := fixtureFor(t, "Engineering") |
| 80 | var got strings.Builder |
| 81 | for _, q := range engineeringPermissionRequests { |
| 82 | got.WriteString(q + "\n" + string(StepPermission(json.RawMessage(q), f.launch.Policy)) + "\n") |
| 83 | } |
| 84 | if got.String() != string(want) { |
| 85 | t.Fatalf("Engineering's permission answers changed:\n%s", got.String()) |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | // TestDesignPolicyIsEngineeringsPlusRender: the Design step's Claude |
| 90 | // policy is Engineering's with exactly these differences: its Equipo |
| 91 | // tools are design-v1's (render_page, submit_candidate, ask_person, no |
| 92 | // submit_render), allowed without a prompt; its thread is the Design |
| 93 | // role's; and /render is denied to every write (the sandbox's and |
| 94 | // Write and Edit's), never to a read. Its native tools are design-v1's |
| 95 | // Policy.Native, Engineering's. |
| 96 | func TestDesignPolicyIsEngineeringsPlusRender(t *testing.T) { |
| 97 | menu, _ := specialist.Lookup(specialist.ProfileDesign) |
| 98 | p, err := provider.Profile(provider.DesignStepAction) |
| 99 | if err != nil { |
| 100 | t.Fatal(err) |
| 101 | } |
| 102 | got, err := PolicyInputs(fixedBinding, provider.PolicyInputs{Action: provider.DesignStepAction, Tools: p.MCPTools(), DeniedPaths: []string{"/private/state"}}, "Design", "claude-opus-5-5") |
| 103 | if err != nil { |
| 104 | t.Fatal(err) |
| 105 | } |
| 106 | wantTools := append(append(slices.Clone(menu.Policy.Native), "mcp__equipo__permission"), "mcp__equipo__render_page", "mcp__equipo__submit_candidate", "mcp__equipo__ask_person") |
| 107 | if !slices.Equal(got.Tools, wantTools) { |
| 108 | t.Fatalf("tools %q, want %q", got.Tools, wantTools) |
| 109 | } |
| 110 | eng, err := PolicyInputs(fixedBinding, provider.PolicyInputs{Action: "implement", Tools: mustProfile(t, "implement").MCPTools(), DeniedPaths: []string{"/private/state"}}, "Engineering", "claude-opus-5-5") |
| 111 | if err != nil { |
| 112 | t.Fatal(err) |
| 113 | } |
| 114 | if !slices.Equal(got.Tools[:len(menu.Policy.Native)], eng.Tools[:len(menu.Policy.Native)]) || !reflect.DeepEqual(got.Argv, eng.Argv) || !reflect.DeepEqual(got.DeniedPaths, eng.DeniedPaths) || string(got.MCP) != string(eng.MCP) || string(got.TurnPolicy) != string(eng.TurnPolicy) { |
| 115 | t.Fatalf("design differs from Engineering beyond its tools and /render:\n%q\n%q", got.Argv, eng.Argv) |
| 116 | } |
| 117 | if string(got.ThreadPolicy) != `{"specialist":"Design","model":"claude-opus-5-5","start":"session-id","resume":"resume"}` { |
| 118 | t.Fatalf("thread %s", got.ThreadPolicy) |
| 119 | } |
| 120 | // Engineering's settings with design's allow list and /render's |
| 121 | // write denials are design's settings, nothing else. |
| 122 | var want, have map[string]any |
| 123 | json.Unmarshal(eng.Settings, &want) |
| 124 | json.Unmarshal(got.Settings, &have) |
| 125 | perms := want["permissions"].(map[string]any) |
| 126 | perms["allow"] = []any{"mcp__equipo__render_page", "mcp__equipo__submit_candidate", "mcp__equipo__ask_person"} |
| 127 | deny := perms["deny"].([]any) |
| 128 | for _, read := range menu.Policy.ReadOnly { |
| 129 | for _, tool := range []string{"Write", "Edit"} { |
| 130 | deny = append(deny, tool+"(/"+read+")", tool+"(/"+read+"/**)") |
| 131 | } |
| 132 | fs := want["sandbox"].(map[string]any)["filesystem"].(map[string]any) |
| 133 | fs["denyWrite"] = append(fs["denyWrite"].([]any), read) |
| 134 | } |
| 135 | perms["deny"] = deny |
| 136 | if !reflect.DeepEqual(have, want) { |
| 137 | a, _ := json.Marshal(have) |
| 138 | b, _ := json.Marshal(want) |
| 139 | t.Fatalf("design settings\n%s\nwant\n%s", a, b) |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | func mustProfile(t *testing.T, action string) provider.ActionProfile { |
| 144 | t.Helper() |
| 145 | p, err := provider.Profile(action) |
| 146 | if err != nil { |
| 147 | t.Fatal(err) |
| 148 | } |
| 149 | return p |
| 150 | } |
| 151 | |
| 152 | // TestDesignPolicyRefusals: the launcher's allowlist takes the design |
| 153 | // action with exactly its three tools under the Design role, and refuses |
| 154 | // the old submit_render, a missing or extra tool, another role, and |
| 155 | // render_page anywhere else. |
| 156 | func TestDesignPolicyRefusals(t *testing.T) { |
| 157 | tools := provider.DesignMCPTools() |
| 158 | for name, c := range map[string]struct { |
| 159 | action, role string |
| 160 | tools []string |
| 161 | }{ |
| 162 | "the old submit_render": {provider.DesignStepAction, "Design", append(slices.Clone(tools), "mcp__equipo__submit_render")}, |
| 163 | "no render_page": {provider.DesignStepAction, "Design", tools[1:]}, |
| 164 | "another order": {provider.DesignStepAction, "Design", []string{tools[1], tools[0], tools[2]}}, |
| 165 | "research's tools": {provider.DesignStepAction, "Design", []string{"mcp__equipo__submit_findings", "mcp__equipo__keep_image"}}, |
| 166 | "a coordinator tool": {provider.DesignStepAction, "Design", append(slices.Clone(tools), "mcp__equipo__start_task")}, |
| 167 | "as Engineering": {provider.DesignStepAction, "Engineering", tools}, |
| 168 | "as Copy": {provider.DesignStepAction, "Copy", tools}, |
| 169 | "render_page for implement": {"implement", "Engineering", append(mustProfile(t, "implement").MCPTools(), "mcp__equipo__render_page")}, |
| 170 | "render_page for design": {"design", "Design", append(mustProfile(t, "design").MCPTools(), "mcp__equipo__render_page")}, |
| 171 | "render_page with no action": {"", "Design", []string{"mcp__equipo__render_page"}}, |
| 172 | } { |
| 173 | if _, err := PolicyInputs(fixedBinding, provider.PolicyInputs{Action: c.action, Tools: c.tools}, c.role, "model"); err == nil { |
| 174 | t.Errorf("%s accepted", name) |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | func designFixture(t *testing.T) *fixture { |
| 180 | t.Helper() |
| 181 | f := fixtureFor(t, "Engineering") |
| 182 | in := f.inputs |
| 183 | in.Action = provider.DesignStepAction |
| 184 | in.Tools = provider.DesignMCPTools() |
| 185 | in, err := PolicyInputs(f.lease.Binding(), in, provider.DesignSpecialist, f.launch.Selection.Model) |
| 186 | if err != nil { |
| 187 | t.Fatal(err) |
| 188 | } |
| 189 | if f.launch.Policy, err = provider.BuildPolicy(f.lease.Binding(), in); err != nil { |
| 190 | t.Fatal(err) |
| 191 | } |
| 192 | f.inputs = in |
| 193 | return f |
| 194 | } |
| 195 | |
| 196 | // TestDesignLaunch: the launcher re-derives the design policy from its |
| 197 | // manifest and launches it (a real launch of the fake CLI, whose init |
| 198 | // reports the manifest's tools); an init with the old submit_render, or |
| 199 | // without render_page, is refused. |
| 200 | func TestDesignLaunch(t *testing.T) { |
| 201 | f := designFixture(t) |
| 202 | if _, err := f.l.validatePolicy(f.lease.Binding(), f.launch.Policy); err != nil { |
| 203 | t.Fatalf("design policy refused: %v", err) |
| 204 | } |
| 205 | session, events := openFixture(t, f) |
| 206 | if err := f.l.StartTurn(context.Background(), session, provider.Turn{ID: "design-turn", AdmissionID: "design-admission", Prompt: "fixture"}); err != nil { |
| 207 | t.Fatal(err) |
| 208 | } |
| 209 | if ev := terminal(t, events); ev.Kind != "turn.completed" { |
| 210 | t.Fatalf("design launch: %+v", ev) |
| 211 | } |
| 212 | model := f.launch.Selection.Model |
| 213 | init := func(tools ...string) []byte { |
| 214 | 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"}}}) |
| 215 | return raw |
| 216 | } |
| 217 | native := []string{"Bash", "Read", "Write", "Edit", "Glob", "Grep"} |
| 218 | if err := ValidateInit(f.launch.Policy, model, init(append(slices.Clone(native), provider.DesignMCPTools()...)...)); err != nil { |
| 219 | t.Fatalf("honest design init refused: %v", err) |
| 220 | } |
| 221 | for name, tools := range map[string][]string{ |
| 222 | "submit_render": append(append(slices.Clone(native), provider.DesignMCPTools()...), "mcp__equipo__submit_render"), |
| 223 | "no render_page": append(slices.Clone(native), "mcp__equipo__submit_candidate", "mcp__equipo__ask_person"), |
| 224 | "WebFetch": append(append(slices.Clone(native), provider.DesignMCPTools()...), "WebFetch"), |
| 225 | } { |
| 226 | if ValidateInit(f.launch.Policy, model, init(tools...)) == nil { |
| 227 | t.Errorf("design init with %s accepted", name) |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | // TestDesignPermission: the Design step's callback reads /render (Read, |
| 233 | // Glob, Grep) and writes nothing there; everything else is answered as |
| 234 | // Engineering's callback answers it, byte for byte. Handed any policy |
| 235 | // but the Design step's, it is Engineering's callback: /render is not |
| 236 | // read. |
| 237 | func TestDesignPermission(t *testing.T) { |
| 238 | f := designFixture(t) |
| 239 | eng := fixtureFor(t, "Engineering") |
| 240 | behavior := func(out json.RawMessage) (string, string) { |
| 241 | var v struct{ Behavior, Message string } |
| 242 | if json.Unmarshal(out, &v) != nil { |
| 243 | t.Fatalf("not JSON: %s", out) |
| 244 | } |
| 245 | return v.Behavior, v.Message |
| 246 | } |
| 247 | reads := map[string]bool{ |
| 248 | `{"tool_name":"Read","input":{"file_path":"/render/1-work-1.png"}}`: true, |
| 249 | `{"tool_name":"Glob","input":{"pattern":"*.png","path":"/render"}}`: true, |
| 250 | `{"tool_name":"Grep","input":{"pattern":"x","path":"/render/"}}`: true, |
| 251 | } |
| 252 | writes := map[string]bool{ |
| 253 | `{"tool_name":"Write","input":{"file_path":"/render/planted.png","content":"x"}}`: true, |
| 254 | `{"tool_name":"Edit","input":{"file_path":"/render/1-work-1.png","old_string":"a","new_string":"b"}}`: true, |
| 255 | } |
| 256 | for _, q := range engineeringPermissionRequests { |
| 257 | got := DesignPermission(json.RawMessage(q), f.launch.Policy) |
| 258 | b, m := behavior(got) |
| 259 | switch { |
| 260 | case reads[q]: |
| 261 | if b != "allow" { |
| 262 | t.Errorf("refused %s: %s", q, m) |
| 263 | } |
| 264 | case writes[q]: |
| 265 | if b != "deny" || !strings.Contains(m, "/render is read-only") { |
| 266 | t.Errorf("%s: %s %s", q, b, m) |
| 267 | } |
| 268 | default: |
| 269 | if want := StepPermission(json.RawMessage(q), eng.launch.Policy); string(got) != string(want) { |
| 270 | t.Errorf("%s: %s, Engineering's %s", q, got, want) |
| 271 | } |
| 272 | } |
| 273 | // With Engineering's policy it is Engineering's callback. |
| 274 | if got, want := DesignPermission(json.RawMessage(q), eng.launch.Policy), StepPermission(json.RawMessage(q), eng.launch.Policy); string(got) != string(want) { |
| 275 | t.Errorf("%s under Engineering's policy: %s, want %s", q, got, want) |
| 276 | } |
| 277 | } |
| 278 | for _, q := range []string{ |
| 279 | `{"tool_name":"Read","input":{"file_path":"/render/../home/runner/.credentials.json"}}`, |
| 280 | `{"tool_name":"Read","input":{"file_path":"/render/../etc/passwd"}}`, |
| 281 | `{"tool_name":"Read","input":{"file_path":"/renderer/a.png"}}`, |
| 282 | `{"tool_name":"Read","input":{"file_path":"render/1-work-1.png"}}`, |
| 283 | `{"tool_name":"Glob","input":{"pattern":"../home/*","path":"/render"}}`, |
| 284 | `{"tool_name":"Write","input":{"file_path":"/render","content":"x"}}`, |
| 285 | `{"tool_name":"Read","input":{"file_path":"/render/a.png"},"extra":1}`, |
| 286 | } { |
| 287 | if b, _ := behavior(DesignPermission(json.RawMessage(q), f.launch.Policy)); b == "allow" { |
| 288 | t.Errorf("allowed %s", q) |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | |