package claude import ( "encoding/json" "errors" "fmt" "slices" "testing" "amadan.net/oficina/equipo/internal/runner/provider" ) // glanceActions are every launch action but the glance. var glanceOthers = []string{"coordinate", "converse", "assist", "reproduce", "scope", "recommend", "investigate", "research", "implement", "copy", "design", "review", "inspect", provider.ResearchStepAction, "answer"} // The glance (the Assistant's instant first line) runs with no tool at // all: --tools "", only the permission callback, which allows nothing, // the workspace read-only, and any tool asked for is refused. It mirrors // TestAssistLaunchPinsSixTools with zero. func TestGlanceLaunchHasNoTools(t *testing.T) { profile, err := provider.Profile(provider.GlanceAction) if err != nil { t.Fatal(err) } if !profile.ReadOnly || len(profile.Tools()) != 0 || profile.Submission != "" { t.Fatalf("profile %+v tools %v", profile, profile.Tools()) } b := provider.Binding{Provider: "claude", CredentialHome: "/private/account", Home: "/private/run", RuntimeRoot: "/private/runtime"} in := provider.PolicyInputs{Action: provider.GlanceAction, Tools: profile.MCPTools()} got, err := PolicyInputs(b, in, profile.Specialist, "claude-opus-5-5") if err != nil { t.Fatal(err) } if !slices.Equal(got.Tools, []string{"mcp__equipo__permission"}) { t.Fatalf("tools %v", got.Tools) } if i := slices.Index(got.Argv, "--tools"); i < 0 || got.Argv[i+1] != "" { t.Fatalf("argv %q", got.Argv) } var settings struct { Permissions struct{ Allow, Ask []string } Sandbox struct { Filesystem struct{ DenyWrite []string } `json:"filesystem"` } } if json.Unmarshal(got.Settings, &settings) != nil || len(settings.Permissions.Allow) != 0 || !slices.Contains(settings.Sandbox.Filesystem.DenyWrite, "/workspace") { t.Fatalf("settings %s", got.Settings) } if len(settings.Permissions.Ask) != 0 { t.Fatalf("native ask rules %q", settings.Permissions.Ask) } for _, tool := range []string{"mcp__equipo__ask_person", "mcp__equipo__look", "mcp__equipo__use", "mcp__equipo__read_notes", "mcp__equipo__submit_answer", "mcp__equipo__submit_findings", "Read"} { more := in more.Tools = []string{tool} if _, err := PolicyInputs(b, more, profile.Specialist, "claude-opus-5-5"); !errors.Is(err, provider.ErrPolicy) { t.Fatalf("a glance with %s: %v", tool, err) } } for _, specialist := range []string{"Coordinator", "Engineering", "Reviewer", provider.ResearchSpecialist} { if _, err := PolicyInputs(b, in, specialist, "claude-opus-5-5"); err == nil { t.Fatalf("a glance ran as %s", specialist) } } // The init frame of a glance: exactly no tool, on the small model. policy := provider.Policy{Manifest: mustJSON(t, map[string]any{"Action": provider.GlanceAction, "Tools": got.Tools})} init := func(model string, tools []string) []byte { return mustJSON(t, 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"}}}) } if err := ValidateInit(policy, "claude-opus-5-5", init(GlanceModel, []string{})); err != nil { t.Fatalf("a glance's init: %v", err) } if err := ValidateInit(policy, "claude-opus-5-5", init("claude-opus-5-5", []string{})); err == nil { t.Fatal("a glance ran on the connection's own model") } if err := ValidateInit(policy, "claude-opus-5-5", init(GlanceModel, []string{"mcp__equipo__look"})); err == nil { t.Fatal("a glance's init reported a tool") } } // Only the glance launches the small model: the connection's bound // model stays the session's identity (the thread policy, the selection), // argv asks for claude-haiku-5-5, and every other action runs on the // bound model, whatever it is. Nothing can name the small model: it is // not an input. func TestOnlyGlanceUsesTheSmallModel(t *testing.T) { if GlanceModel != "claude-haiku-5-5" || provider.GlanceModel("claude") != GlanceModel { t.Fatalf("glance model %q %q", GlanceModel, provider.GlanceModel("claude")) } b := provider.Binding{Provider: "claude", CredentialHome: "/private/account", Home: "/private/run", RuntimeRoot: "/private/runtime"} model := func(action, bound string) (string, threadPolicy) { t.Helper() profile, err := provider.Profile(action) if err != nil { t.Fatal(err) } got, err := PolicyInputs(b, provider.PolicyInputs{Action: action, Tools: profile.MCPTools()}, profile.Specialist, bound) if err != nil { t.Fatalf("%s: %v", action, err) } var tp threadPolicy if json.Unmarshal(got.ThreadPolicy, &tp) != nil { t.Fatal("thread policy") } i := slices.Index(got.Argv, "--model") if slices.Contains(got.Argv, "--effort") && action == provider.GlanceAction { t.Fatalf("a glance asks for an effort: %q", got.Argv) } return got.Argv[i+1], tp } for _, bound := range []string{"claude-opus-5-5", "claude-sonnet-5-5"} { if got, tp := model(provider.GlanceAction, bound); got != GlanceModel || tp.Model != bound { t.Fatalf("glance on %s: argv %s, thread %s", bound, got, tp.Model) } for _, action := range glanceOthers { if got, tp := model(action, bound); got != bound || tp.Model != bound { t.Fatalf("%s on %s ran %s (thread %s)", action, bound, got, tp.Model) } } } if got := provider.LaunchModel("assist", "claude", "claude-opus-5-5"); got != "claude-opus-5-5" { t.Fatalf("assist launches %s", got) } if got := provider.LaunchModel(provider.GlanceAction, "pi", "tensorx-1"); got != "tensorx-1" { t.Fatalf("a glance on a provider without a small model launches %s", got) } } func mustJSON(t *testing.T, v any) []byte { t.Helper() raw, err := json.Marshal(v) if err != nil { t.Fatal(fmt.Sprint(err)) } return raw }