| 1 | // Package specialist is the closed menu of specialists defined as data |
| 2 | // (Research spec §6; release plan §2.8). A specialist row may set its |
| 3 | // name, brief and model preference; everything else, its capabilities, |
| 4 | // tools, mounts and inputs, comes from a profile here, in code. The |
| 5 | // instance validates a coordinator's proposal against it, and later |
| 6 | // dispatch and the tool socket hold to the same profile. Stdlib only, so |
| 7 | // the runner can share it. |
| 8 | package specialist |
| 9 | |
| 10 | import ( |
| 11 | "crypto/sha256" |
| 12 | "embed" |
| 13 | "encoding/hex" |
| 14 | "errors" |
| 15 | "fmt" |
| 16 | "slices" |
| 17 | "strings" |
| 18 | "unicode" |
| 19 | "unicode/utf8" |
| 20 | ) |
| 21 | |
| 22 | // Capabilities on the menu. web.research covers the runner's fetch of |
| 23 | // approved URLs, submit_findings and keep_image, and nothing else; it is |
| 24 | // never granted beside any other capability or tool, so it never meets |
| 25 | // page.render. branch.edit edits the task's branch and submits a |
| 26 | // candidate, as Engineering does (never a merge, a push to the default |
| 27 | // branch, a preview, a publish, or settings, places or grants); |
| 28 | // page.render is render_page (spec 2026-11-28-dynamic-specialists.md, |
| 29 | // sections 1 and 3). |
| 30 | const ( |
| 31 | CapabilityWebResearch = "web.research" |
| 32 | CapabilityBranchEdit = "branch.edit" |
| 33 | CapabilityPageRender = "page.render" |
| 34 | ) |
| 35 | |
| 36 | // capabilities are those with words (Describe). |
| 37 | var capabilities = []string{CapabilityWebResearch, CapabilityBranchEdit, CapabilityPageRender} |
| 38 | |
| 39 | // Profiles. |
| 40 | const ( |
| 41 | ProfileResearch = "research-v1" |
| 42 | ProfileDesign = "design-v1" |
| 43 | ) |
| 44 | |
| 45 | // Tools a profile may serve on its step's tool socket. |
| 46 | const ( |
| 47 | ToolSubmitFindings = "submit_findings" |
| 48 | ToolKeepImage = "keep_image" |
| 49 | ToolRenderPage = "render_page" |
| 50 | ToolSubmitCandidate = "submit_candidate" |
| 51 | ToolAskPerson = "ask_person" |
| 52 | ) |
| 53 | |
| 54 | // Bounds on what a row may set. |
| 55 | const ( |
| 56 | MaxBrief = 2 << 10 |
| 57 | MaxName = 40 |
| 58 | ) |
| 59 | |
| 60 | // Profile is one closed profile: exactly these capabilities and tools, |
| 61 | // and its prompt pack. Changing any of them is a new id (design-v2); the |
| 62 | // golden test pins each profile. |
| 63 | type Profile struct { |
| 64 | ID string |
| 65 | Capabilities []string |
| 66 | Tools []string |
| 67 | // Pack is the SHA-256 (hex) of the profile's prompt pack, |
| 68 | // packs/<ID>.md, embedded here; "" for a profile whose prompt is in |
| 69 | // the runner's code (research-v1). PackText serves the file only |
| 70 | // while its digest is this. |
| 71 | Pack string |
| 72 | // Policy is the executable policy a step of the profile runs |
| 73 | // under; the zero Policy for a profile whose step the runner's own |
| 74 | // code pins (research-v1, as its prompt). |
| 75 | Policy Policy |
| 76 | } |
| 77 | |
| 78 | // Policy is a profile's executable policy: Action, the runner's launch |
| 79 | // action profile (provider.Profile), whose Equipo tools are exactly the |
| 80 | // profile's Tools; Native, the CLI's native tools, in order; and |
| 81 | // ReadOnly, the paths beyond Engineering's file permissions (/workspace, |
| 82 | // and /research to read) that its native file tools may read and never |
| 83 | // write. The runner's policy is tested against it. |
| 84 | type Policy struct { |
| 85 | Action string |
| 86 | Native []string |
| 87 | ReadOnly []string |
| 88 | } |
| 89 | |
| 90 | // ActionDesignStep is the runner's launch action for a design-v1 step, |
| 91 | // and RenderMount where the step's captures are, read-only to it. |
| 92 | const ( |
| 93 | ActionDesignStep = "design_step" |
| 94 | RenderMount = "/render" |
| 95 | ) |
| 96 | |
| 97 | // packs are the prompt packs, reviewed files in the repository, never |
| 98 | // fetched at run time. design-v1.md is our own design discipline, |
| 99 | // drawing on the method of the impeccable skill |
| 100 | // (github.com/pbakaus/impeccable, Apache-2.0) without its text. |
| 101 | // |
| 102 | //go:embed packs/*.md |
| 103 | var packs embed.FS |
| 104 | |
| 105 | var menu = []Profile{ |
| 106 | {ID: ProfileResearch, Capabilities: []string{CapabilityWebResearch}, Tools: []string{ToolSubmitFindings, ToolKeepImage}}, |
| 107 | {ID: ProfileDesign, Capabilities: []string{CapabilityBranchEdit, CapabilityPageRender}, Tools: []string{ToolRenderPage, ToolSubmitCandidate, ToolAskPerson}, Pack: "de40167d0d843f4f99ecc633b7022ac8631db74c74c98ed5fdbd4ea732d3334a", |
| 108 | Policy: Policy{Action: ActionDesignStep, Native: []string{"Bash", "Read", "Write", "Edit", "Glob", "Grep"}, ReadOnly: []string{RenderMount}}}, |
| 109 | } |
| 110 | |
| 111 | func init() { |
| 112 | if err := checkMenu(menu); err != nil { |
| 113 | panic(err) |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | // checkMenu is what the menu must hold to: ids said once; a profile |
| 118 | // with web.research holds nothing else (no other capability, only |
| 119 | // research's tools), so no profile pairs it with page.render; every |
| 120 | // capability has words; every pack is its file's. |
| 121 | func checkMenu(m []Profile) error { |
| 122 | ids := map[string]bool{} |
| 123 | for _, p := range m { |
| 124 | if ids[p.ID] { |
| 125 | return fmt.Errorf("specialist: profile %s twice", p.ID) |
| 126 | } |
| 127 | ids[p.ID] = true |
| 128 | if slices.Contains(p.Capabilities, CapabilityWebResearch) && (len(p.Capabilities) != 1 || exclusive(p.Capabilities, p.Tools)) { |
| 129 | return fmt.Errorf("specialist: %s holds web.research with something else", p.ID) |
| 130 | } |
| 131 | for _, c := range p.Capabilities { |
| 132 | if Describe(c) == "" { |
| 133 | return fmt.Errorf("specialist: %s: no words for %s", p.ID, c) |
| 134 | } |
| 135 | } |
| 136 | if _, ok := p.PackText(); p.Pack != "" && !ok { |
| 137 | return fmt.Errorf("specialist: %s: its pack is not packs/%s.md", p.ID, p.ID) |
| 138 | } |
| 139 | } |
| 140 | return nil |
| 141 | } |
| 142 | |
| 143 | // PackText is the profile's prompt pack, if it has one and the embedded |
| 144 | // file's SHA-256 is its Pack. |
| 145 | func (p Profile) PackText() (string, bool) { |
| 146 | if p.Pack == "" { |
| 147 | return "", false |
| 148 | } |
| 149 | raw, err := packs.ReadFile("packs/" + p.ID + ".md") |
| 150 | if err != nil { |
| 151 | return "", false |
| 152 | } |
| 153 | sum := sha256.Sum256(raw) |
| 154 | if hex.EncodeToString(sum[:]) != p.Pack { |
| 155 | return "", false |
| 156 | } |
| 157 | return string(raw), true |
| 158 | } |
| 159 | |
| 160 | // Profiles is the menu, as copies. |
| 161 | func Profiles() []Profile { |
| 162 | out := make([]Profile, len(menu)) |
| 163 | for i, p := range menu { |
| 164 | out[i] = p.clone() |
| 165 | } |
| 166 | return out |
| 167 | } |
| 168 | |
| 169 | // Lookup is a profile by id, as a copy. |
| 170 | func Lookup(id string) (Profile, bool) { |
| 171 | i := slices.IndexFunc(menu, func(p Profile) bool { return p.ID == id }) |
| 172 | if i < 0 { |
| 173 | return Profile{}, false |
| 174 | } |
| 175 | return menu[i].clone(), true |
| 176 | } |
| 177 | |
| 178 | func (p Profile) clone() Profile { |
| 179 | p.Capabilities, p.Tools = slices.Clone(p.Capabilities), slices.Clone(p.Tools) |
| 180 | p.Policy.Native, p.Policy.ReadOnly = slices.Clone(p.Policy.Native), slices.Clone(p.Policy.ReadOnly) |
| 181 | return p |
| 182 | } |
| 183 | |
| 184 | // Describe is the copy key for a capability's plain-language words, "" |
| 185 | // for one not on the menu. |
| 186 | func Describe(capability string) string { |
| 187 | if !slices.Contains(capabilities, capability) { |
| 188 | return "" |
| 189 | } |
| 190 | return "chat.specialist.capability." + capability |
| 191 | } |
| 192 | |
| 193 | // Refusals. Each is a fixed reason the caller turns into its own words. |
| 194 | var ( |
| 195 | ErrUnknownProfile = errors.New("specialist: unknown profile") |
| 196 | // ErrExclusive: web.research beside anything that is not its |
| 197 | // profile's (another capability, or any other tool). |
| 198 | ErrExclusive = errors.New("specialist: web.research with something else") |
| 199 | // ErrNotOnMenu: capabilities or tools that are not exactly the |
| 200 | // profile's. |
| 201 | ErrNotOnMenu = errors.New("specialist: not exactly the profile") |
| 202 | ErrName = errors.New("specialist: name") |
| 203 | ErrBrief = errors.New("specialist: brief") |
| 204 | ) |
| 205 | |
| 206 | // Proposal is what the coordinator asks for: a profile, the row's name |
| 207 | // and brief, and the capabilities and tools it believes the profile has. |
| 208 | type Proposal struct { |
| 209 | Profile string |
| 210 | Name string |
| 211 | Brief string |
| 212 | Capabilities []string |
| 213 | Tools []string |
| 214 | } |
| 215 | |
| 216 | // builtIn are the names a data-defined specialist may not take. |
| 217 | var builtIn = []string{"equipo", "coordinator", "copy", "engineering", "review", "checks", "merge", "preview"} |
| 218 | |
| 219 | // Validate checks a proposal against the closed menu and returns it |
| 220 | // cleaned: name and brief trimmed, capabilities and tools in the |
| 221 | // profile's order. The capability and tool sets must be exactly the |
| 222 | // profile's (in any order, each once); web.research beside anything |
| 223 | // else is refused first, whatever the profile. |
| 224 | func Validate(in Proposal) (Proposal, error) { |
| 225 | p, ok := Lookup(in.Profile) |
| 226 | if !ok { |
| 227 | return Proposal{}, ErrUnknownProfile |
| 228 | } |
| 229 | if exclusive(in.Capabilities, in.Tools) { |
| 230 | return Proposal{}, ErrExclusive |
| 231 | } |
| 232 | if !same(in.Capabilities, p.Capabilities) || !same(in.Tools, p.Tools) { |
| 233 | return Proposal{}, ErrNotOnMenu |
| 234 | } |
| 235 | name, ok := cleanName(in.Name) |
| 236 | if !ok { |
| 237 | return Proposal{}, ErrName |
| 238 | } |
| 239 | brief, ok := cleanBrief(in.Brief) |
| 240 | if !ok { |
| 241 | return Proposal{}, ErrBrief |
| 242 | } |
| 243 | return Proposal{Profile: p.ID, Name: name, Brief: brief, Capabilities: p.Capabilities, Tools: p.Tools}, nil |
| 244 | } |
| 245 | |
| 246 | // exclusive reports web.research asked for beside any other capability, |
| 247 | // or with any tool that is not one of its profile's two. |
| 248 | func exclusive(capabilities, tools []string) bool { |
| 249 | if !slices.Contains(capabilities, CapabilityWebResearch) { |
| 250 | return false |
| 251 | } |
| 252 | for _, c := range capabilities { |
| 253 | if c != CapabilityWebResearch { |
| 254 | return true |
| 255 | } |
| 256 | } |
| 257 | seen := map[string]bool{} |
| 258 | for _, t := range tools { |
| 259 | if t != ToolSubmitFindings && t != ToolKeepImage || seen[t] { |
| 260 | return true |
| 261 | } |
| 262 | seen[t] = true |
| 263 | } |
| 264 | return false |
| 265 | } |
| 266 | |
| 267 | // same: got is want as a set, each element once. |
| 268 | func same(got, want []string) bool { |
| 269 | if len(got) != len(want) { |
| 270 | return false |
| 271 | } |
| 272 | seen := map[string]bool{} |
| 273 | for _, g := range got { |
| 274 | if seen[g] || !slices.Contains(want, g) { |
| 275 | return false |
| 276 | } |
| 277 | seen[g] = true |
| 278 | } |
| 279 | return true |
| 280 | } |
| 281 | |
| 282 | func cleanName(s string) (string, bool) { |
| 283 | s = strings.TrimSpace(s) |
| 284 | if s == "" || len(s) > MaxName || !utf8.ValidString(s) || slices.Contains(builtIn, strings.ToLower(s)) { |
| 285 | return "", false |
| 286 | } |
| 287 | for i, r := range s { |
| 288 | ok := unicode.IsLetter(r) || unicode.IsDigit(r) || i > 0 && (r == ' ' || r == '-' || r == '\'') |
| 289 | if !ok { |
| 290 | return "", false |
| 291 | } |
| 292 | } |
| 293 | return s, true |
| 294 | } |
| 295 | |
| 296 | func cleanBrief(s string) (string, bool) { |
| 297 | s = strings.TrimSpace(s) |
| 298 | if s == "" || len(s) > MaxBrief || !utf8.ValidString(s) { |
| 299 | return "", false |
| 300 | } |
| 301 | for _, r := range s { |
| 302 | if unicode.IsControl(r) && r != '\n' && r != '\t' { |
| 303 | return "", false |
| 304 | } |
| 305 | } |
| 306 | return s, true |
| 307 | } |
| 308 | |