// Package specialist is the closed menu of specialists defined as data // (Research spec §6; release plan §2.8). A specialist row may set its // name, brief and model preference; everything else, its capabilities, // tools, mounts and inputs, comes from a profile here, in code. The // instance validates a coordinator's proposal against it, and later // dispatch and the tool socket hold to the same profile. Stdlib only, so // the runner can share it. package specialist import ( "crypto/sha256" "embed" "encoding/hex" "errors" "fmt" "slices" "strings" "unicode" "unicode/utf8" ) // Capabilities on the menu. web.research covers the runner's fetch of // approved URLs, submit_findings and keep_image, and nothing else; it is // never granted beside any other capability or tool, so it never meets // page.render. branch.edit edits the task's branch and submits a // candidate, as Engineering does (never a merge, a push to the default // branch, a preview, a publish, or settings, places or grants); // page.render is render_page (spec 2026-11-28-dynamic-specialists.md, // sections 1 and 3). const ( CapabilityWebResearch = "web.research" CapabilityBranchEdit = "branch.edit" CapabilityPageRender = "page.render" ) // capabilities are those with words (Describe). var capabilities = []string{CapabilityWebResearch, CapabilityBranchEdit, CapabilityPageRender} // Profiles. const ( ProfileResearch = "research-v1" ProfileDesign = "design-v1" ) // Tools a profile may serve on its step's tool socket. const ( ToolSubmitFindings = "submit_findings" ToolKeepImage = "keep_image" ToolRenderPage = "render_page" ToolSubmitCandidate = "submit_candidate" ToolAskPerson = "ask_person" ) // Bounds on what a row may set. const ( MaxBrief = 2 << 10 MaxName = 40 ) // Profile is one closed profile: exactly these capabilities and tools, // and its prompt pack. Changing any of them is a new id (design-v2); the // golden test pins each profile. type Profile struct { ID string Capabilities []string Tools []string // Pack is the SHA-256 (hex) of the profile's prompt pack, // packs/.md, embedded here; "" for a profile whose prompt is in // the runner's code (research-v1). PackText serves the file only // while its digest is this. Pack string // Policy is the executable policy a step of the profile runs // under; the zero Policy for a profile whose step the runner's own // code pins (research-v1, as its prompt). Policy Policy } // Policy is a profile's executable policy: Action, the runner's launch // action profile (provider.Profile), whose Equipo tools are exactly the // profile's Tools; Native, the CLI's native tools, in order; and // ReadOnly, the paths beyond Engineering's file permissions (/workspace, // and /research to read) that its native file tools may read and never // write. The runner's policy is tested against it. type Policy struct { Action string Native []string ReadOnly []string } // ActionDesignStep is the runner's launch action for a design-v1 step, // and RenderMount where the step's captures are, read-only to it. const ( ActionDesignStep = "design_step" RenderMount = "/render" ) // packs are the prompt packs, reviewed files in the repository, never // fetched at run time. design-v1.md is our own design discipline, // drawing on the method of the impeccable skill // (github.com/pbakaus/impeccable, Apache-2.0) without its text. // //go:embed packs/*.md var packs embed.FS var menu = []Profile{ {ID: ProfileResearch, Capabilities: []string{CapabilityWebResearch}, Tools: []string{ToolSubmitFindings, ToolKeepImage}}, {ID: ProfileDesign, Capabilities: []string{CapabilityBranchEdit, CapabilityPageRender}, Tools: []string{ToolRenderPage, ToolSubmitCandidate, ToolAskPerson}, Pack: "de40167d0d843f4f99ecc633b7022ac8631db74c74c98ed5fdbd4ea732d3334a", Policy: Policy{Action: ActionDesignStep, Native: []string{"Bash", "Read", "Write", "Edit", "Glob", "Grep"}, ReadOnly: []string{RenderMount}}}, } func init() { if err := checkMenu(menu); err != nil { panic(err) } } // checkMenu is what the menu must hold to: ids said once; a profile // with web.research holds nothing else (no other capability, only // research's tools), so no profile pairs it with page.render; every // capability has words; every pack is its file's. func checkMenu(m []Profile) error { ids := map[string]bool{} for _, p := range m { if ids[p.ID] { return fmt.Errorf("specialist: profile %s twice", p.ID) } ids[p.ID] = true if slices.Contains(p.Capabilities, CapabilityWebResearch) && (len(p.Capabilities) != 1 || exclusive(p.Capabilities, p.Tools)) { return fmt.Errorf("specialist: %s holds web.research with something else", p.ID) } for _, c := range p.Capabilities { if Describe(c) == "" { return fmt.Errorf("specialist: %s: no words for %s", p.ID, c) } } if _, ok := p.PackText(); p.Pack != "" && !ok { return fmt.Errorf("specialist: %s: its pack is not packs/%s.md", p.ID, p.ID) } } return nil } // PackText is the profile's prompt pack, if it has one and the embedded // file's SHA-256 is its Pack. func (p Profile) PackText() (string, bool) { if p.Pack == "" { return "", false } raw, err := packs.ReadFile("packs/" + p.ID + ".md") if err != nil { return "", false } sum := sha256.Sum256(raw) if hex.EncodeToString(sum[:]) != p.Pack { return "", false } return string(raw), true } // Profiles is the menu, as copies. func Profiles() []Profile { out := make([]Profile, len(menu)) for i, p := range menu { out[i] = p.clone() } return out } // Lookup is a profile by id, as a copy. func Lookup(id string) (Profile, bool) { i := slices.IndexFunc(menu, func(p Profile) bool { return p.ID == id }) if i < 0 { return Profile{}, false } return menu[i].clone(), true } func (p Profile) clone() Profile { p.Capabilities, p.Tools = slices.Clone(p.Capabilities), slices.Clone(p.Tools) p.Policy.Native, p.Policy.ReadOnly = slices.Clone(p.Policy.Native), slices.Clone(p.Policy.ReadOnly) return p } // Describe is the copy key for a capability's plain-language words, "" // for one not on the menu. func Describe(capability string) string { if !slices.Contains(capabilities, capability) { return "" } return "chat.specialist.capability." + capability } // Refusals. Each is a fixed reason the caller turns into its own words. var ( ErrUnknownProfile = errors.New("specialist: unknown profile") // ErrExclusive: web.research beside anything that is not its // profile's (another capability, or any other tool). ErrExclusive = errors.New("specialist: web.research with something else") // ErrNotOnMenu: capabilities or tools that are not exactly the // profile's. ErrNotOnMenu = errors.New("specialist: not exactly the profile") ErrName = errors.New("specialist: name") ErrBrief = errors.New("specialist: brief") ) // Proposal is what the coordinator asks for: a profile, the row's name // and brief, and the capabilities and tools it believes the profile has. type Proposal struct { Profile string Name string Brief string Capabilities []string Tools []string } // builtIn are the names a data-defined specialist may not take. var builtIn = []string{"equipo", "coordinator", "copy", "engineering", "review", "checks", "merge", "preview"} // Validate checks a proposal against the closed menu and returns it // cleaned: name and brief trimmed, capabilities and tools in the // profile's order. The capability and tool sets must be exactly the // profile's (in any order, each once); web.research beside anything // else is refused first, whatever the profile. func Validate(in Proposal) (Proposal, error) { p, ok := Lookup(in.Profile) if !ok { return Proposal{}, ErrUnknownProfile } if exclusive(in.Capabilities, in.Tools) { return Proposal{}, ErrExclusive } if !same(in.Capabilities, p.Capabilities) || !same(in.Tools, p.Tools) { return Proposal{}, ErrNotOnMenu } name, ok := cleanName(in.Name) if !ok { return Proposal{}, ErrName } brief, ok := cleanBrief(in.Brief) if !ok { return Proposal{}, ErrBrief } return Proposal{Profile: p.ID, Name: name, Brief: brief, Capabilities: p.Capabilities, Tools: p.Tools}, nil } // exclusive reports web.research asked for beside any other capability, // or with any tool that is not one of its profile's two. func exclusive(capabilities, tools []string) bool { if !slices.Contains(capabilities, CapabilityWebResearch) { return false } for _, c := range capabilities { if c != CapabilityWebResearch { return true } } seen := map[string]bool{} for _, t := range tools { if t != ToolSubmitFindings && t != ToolKeepImage || seen[t] { return true } seen[t] = true } return false } // same: got is want as a set, each element once. func same(got, want []string) bool { if len(got) != len(want) { return false } seen := map[string]bool{} for _, g := range got { if seen[g] || !slices.Contains(want, g) { return false } seen[g] = true } return true } func cleanName(s string) (string, bool) { s = strings.TrimSpace(s) if s == "" || len(s) > MaxName || !utf8.ValidString(s) || slices.Contains(builtIn, strings.ToLower(s)) { return "", false } for i, r := range s { ok := unicode.IsLetter(r) || unicode.IsDigit(r) || i > 0 && (r == ' ' || r == '-' || r == '\'') if !ok { return "", false } } return s, true } func cleanBrief(s string) (string, bool) { s = strings.TrimSpace(s) if s == "" || len(s) > MaxBrief || !utf8.ValidString(s) { return "", false } for _, r := range s { if unicode.IsControl(r) && r != '\n' && r != '\t' { return "", false } } return s, true }