| 1 | use serde::{Deserialize, Serialize}; |
| 2 | use std::collections::BTreeMap; |
| 3 | |
| 4 | /// On-disk schema for the `[fleet]` table (#3165). See `config.example.toml` |
| 5 | /// and `docs/FLEET.md` for documentation. |
| 6 | #[derive(Debug, Clone, Serialize, Deserialize, Default)] |
| 7 | pub struct FleetConfigToml { |
| 8 | /// User-defined and built-in role presets. |
| 9 | /// |
| 10 | /// Each role defines default tool profiles, capabilities, and execution |
| 11 | /// requests that task specs can reference by name. Built-in roles |
| 12 | /// (`smoke-runner`, `reviewer`, `builder`, `read-only`) are always |
| 13 | /// available; user-defined roles in config override or extend them. |
| 14 | #[serde(default)] |
| 15 | pub roles: BTreeMap<String, FleetRolePreset>, |
| 16 | /// Fleet profile vocabulary (#3167). Profiles group role semantics, |
| 17 | /// loadout hints, route identity, and delegation bounds. Runtime authority |
| 18 | /// is intentionally not a profile property. |
| 19 | #[serde(default, skip_serializing_if = "BTreeMap::is_empty")] |
| 20 | pub profiles: BTreeMap<String, FleetProfile>, |
| 21 | /// Headless worker execution hardening (#3027). |
| 22 | #[serde(default)] |
| 23 | pub exec: FleetExecConfig, |
| 24 | } |
| 25 | |
| 26 | /// Largest representable absolute depth; admission rejects arithmetic overflow. |
| 27 | pub const MAX_SPAWN_DEPTH_CEILING: u32 = u32::MAX; |
| 28 | |
| 29 | /// Headless worker execution constraints (#3027). |
| 30 | /// |
| 31 | /// These limits apply to all fleet workers and sub-agents spawned through |
| 32 | /// the headless worker runtime. Task specs can tighten but not loosen them. |
| 33 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 34 | pub struct FleetExecConfig { |
| 35 | /// Tools that are always allowed regardless of role or task spec. |
| 36 | #[serde(default, skip_serializing_if = "Vec::is_empty")] |
| 37 | pub allowed_tools: Vec<String>, |
| 38 | /// Tools that are always disallowed, overriding role and task spec. |
| 39 | #[serde(default, skip_serializing_if = "Vec::is_empty")] |
| 40 | pub disallowed_tools: Vec<String>, |
| 41 | /// Optional hard ceiling on sub-agent steps (tool calls + model turns). |
| 42 | /// Zero keeps the normal agent loop unbounded; a positive value terminates |
| 43 | /// workers that exceed the explicit operator cap. |
| 44 | #[serde(default = "default_fleet_max_turns")] |
| 45 | pub max_turns: u32, |
| 46 | /// Recursive child-agent budget for headless fleet workers. |
| 47 | /// Defaults to [`codewhale_protocol::fleet::DEFAULT_SPAWN_DEPTH`]. |
| 48 | /// Set zero to block child delegation while preserving the root worker. |
| 49 | #[serde(default = "default_fleet_max_spawn_depth")] |
| 50 | pub max_spawn_depth: u32, |
| 51 | /// Extra system prompt text appended to every headless worker. |
| 52 | /// Useful for injecting org-wide policy or behavior constraints. |
| 53 | #[serde(default, skip_serializing_if = "String::is_empty")] |
| 54 | pub append_system_prompt: String, |
| 55 | /// Output format for fleet worker results. |
| 56 | /// `"text"` (default) or `"stream-json"` for newline-delimited JSON events. |
| 57 | #[serde(default = "default_fleet_output_format")] |
| 58 | pub output_format: String, |
| 59 | } |
| 60 | |
| 61 | /// Fleet workers run until the model finishes unless an operator supplies a |
| 62 | /// positive `max_turns` value. Individual task budgets may still opt into a |
| 63 | /// narrower explicit model-turn cap through `budget.max_steps`; tool-call |
| 64 | /// admission is enforced independently through `budget.max_tool_calls`. |
| 65 | pub const FLEET_DEFAULT_MAX_TURNS: u32 = 0; |
| 66 | |
| 67 | pub(crate) fn default_fleet_max_turns() -> u32 { |
| 68 | FLEET_DEFAULT_MAX_TURNS |
| 69 | } |
| 70 | |
| 71 | pub(crate) fn default_fleet_max_spawn_depth() -> u32 { |
| 72 | codewhale_protocol::fleet::DEFAULT_SPAWN_DEPTH |
| 73 | } |
| 74 | |
| 75 | pub(crate) fn default_fleet_output_format() -> String { |
| 76 | "text".to_string() |
| 77 | } |
| 78 | |
| 79 | impl Default for FleetExecConfig { |
| 80 | fn default() -> Self { |
| 81 | Self { |
| 82 | allowed_tools: Vec::new(), |
| 83 | disallowed_tools: Vec::new(), |
| 84 | max_turns: default_fleet_max_turns(), |
| 85 | max_spawn_depth: default_fleet_max_spawn_depth(), |
| 86 | append_system_prompt: String::new(), |
| 87 | output_format: default_fleet_output_format(), |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /// Fleet org-chart profile. |
| 93 | /// |
| 94 | /// A profile is an additive config record for future fleet scheduling policy. |
| 95 | /// Loading one must not grant runtime permissions by itself: shell and trust |
| 96 | /// escalation default off, and approvals default on. |
| 97 | #[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)] |
| 98 | pub struct FleetProfile { |
| 99 | /// Org-chart slot this profile describes. |
| 100 | #[serde(default)] |
| 101 | pub slot: FleetSlot, |
| 102 | /// Semantic role name and optional instruction overlay. |
| 103 | #[serde(default)] |
| 104 | pub role: FleetRole, |
| 105 | /// Model class / route-role hint. This is data only in this slice. |
| 106 | #[serde(default)] |
| 107 | pub loadout: FleetLoadout, |
| 108 | /// Optional explicit model id for this profile on the active/resolved route. |
| 109 | /// |
| 110 | /// This is not an auth or endpoint selector. Provider-scoped routing still |
| 111 | /// validates the executable provider/model/wire-model decision. |
| 112 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 113 | pub model: Option<String>, |
| 114 | /// Optional explicit provider id for this profile's model (#4093). |
| 115 | /// |
| 116 | /// Present only when the profile was created against a specific, |
| 117 | /// credential-checked provider (e.g. via the Fleet setup model picker), |
| 118 | /// so a worker can be pinned to a route independent of the parent/current |
| 119 | /// session provider. `None` means "no route pin" (inherit), matching |
| 120 | /// `model: None`; a profile must never carry `provider` without `model`. |
| 121 | /// |
| 122 | /// EPIC #2608 explicit-config-only mandate: this field is the ONLY |
| 123 | /// authority for the profile's provider. It is never inferred by sniffing |
| 124 | /// a substring/prefix out of `model` — callers that need the provider for |
| 125 | /// this profile must read this field, not guess from the model id. |
| 126 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 127 | pub provider: Option<String>, |
| 128 | /// Optional explicit reasoning/thinking tier for this profile (#4137). |
| 129 | /// |
| 130 | /// This is a safe, non-secret route tuning value. `None` means inherit the |
| 131 | /// operator/session reasoning tier. Concrete values are normalized by the |
| 132 | /// TUI loader before they are used at runtime. |
| 133 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 134 | pub reasoning_effort: Option<String>, |
| 135 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 136 | pub external_agent: Option<String>, |
| 137 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 138 | pub external_agent_transport: Option<String>, |
| 139 | /// Legacy ignored input retained for old Fleet-profile files. Runtime |
| 140 | /// authority is derived after identity selection and is never persisted in |
| 141 | /// this profile. |
| 142 | #[doc(hidden)] |
| 143 | #[serde(default, skip_serializing)] |
| 144 | pub permissions: FleetProfilePermissions, |
| 145 | /// Delegation hints for future manager policy. |
| 146 | #[serde(default)] |
| 147 | pub delegation: FleetDelegationHints, |
| 148 | } |
| 149 | |
| 150 | /// Semantic role declaration for a fleet profile. |
| 151 | /// |
| 152 | /// TOML may use either `role = "reviewer"` or a role table with `name` and |
| 153 | /// `instructions`. |
| 154 | #[derive(Debug, Clone, Serialize, PartialEq, Eq)] |
| 155 | pub struct FleetRole { |
| 156 | /// Stable role name, e.g. `scout`, `implementer`, or `verifier`. |
| 157 | pub name: String, |
| 158 | /// Optional short description for config UIs and docs. |
| 159 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 160 | pub description: Option<String>, |
| 161 | /// Optional instruction overlay to apply when the role is later consumed. |
| 162 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 163 | pub instructions: Option<String>, |
| 164 | } |
| 165 | |
| 166 | impl Default for FleetRole { |
| 167 | fn default() -> Self { |
| 168 | Self { |
| 169 | name: "general".to_string(), |
| 170 | description: None, |
| 171 | instructions: None, |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | impl<'de> Deserialize<'de> for FleetRole { |
| 177 | fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error> |
| 178 | where |
| 179 | D: serde::Deserializer<'de>, |
| 180 | { |
| 181 | #[derive(Deserialize)] |
| 182 | #[serde(untagged)] |
| 183 | enum FleetRoleWire { |
| 184 | Name(String), |
| 185 | Full { |
| 186 | #[serde(default)] |
| 187 | name: Option<String>, |
| 188 | #[serde(default)] |
| 189 | description: Option<String>, |
| 190 | #[serde(default)] |
| 191 | instructions: Option<String>, |
| 192 | }, |
| 193 | } |
| 194 | |
| 195 | match FleetRoleWire::deserialize(deserializer)? { |
| 196 | FleetRoleWire::Name(name) => Ok(Self { |
| 197 | name, |
| 198 | ..Self::default() |
| 199 | }), |
| 200 | FleetRoleWire::Full { |
| 201 | name, |
| 202 | description, |
| 203 | instructions, |
| 204 | } => Ok(Self { |
| 205 | name: name.unwrap_or_else(|| Self::default().name), |
| 206 | description, |
| 207 | instructions, |
| 208 | }), |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | /// Org-chart slot for grouping fleet profiles. |
| 214 | #[derive(Debug, Clone, PartialEq, Eq, Default)] |
| 215 | pub enum FleetSlot { |
| 216 | Manager, |
| 217 | Scout, |
| 218 | Planner, |
| 219 | Implementer, |
| 220 | Reviewer, |
| 221 | Verifier, |
| 222 | Operator, |
| 223 | Summarizer, |
| 224 | #[default] |
| 225 | General, |
| 226 | Custom(String), |
| 227 | } |
| 228 | |
| 229 | impl FleetSlot { |
| 230 | #[must_use] |
| 231 | pub fn as_str(&self) -> &str { |
| 232 | match self { |
| 233 | Self::Manager => "manager", |
| 234 | Self::Scout => "scout", |
| 235 | Self::Planner => "planner", |
| 236 | Self::Implementer => "implementer", |
| 237 | Self::Reviewer => "reviewer", |
| 238 | Self::Verifier => "verifier", |
| 239 | Self::Operator => "operator", |
| 240 | Self::Summarizer => "summarizer", |
| 241 | Self::General => "general", |
| 242 | Self::Custom(value) => value.as_str(), |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | #[must_use] |
| 247 | pub fn from_name(value: &str) -> Self { |
| 248 | match value.trim() { |
| 249 | "manager" | "coordinator" => Self::Manager, |
| 250 | "scout" | "research" | "research-worker" => Self::Scout, |
| 251 | "planner" | "plan" | "awaiter" => Self::Planner, |
| 252 | "implementer" | "builder" => Self::Implementer, |
| 253 | "reviewer" => Self::Reviewer, |
| 254 | "verifier" | "tester" => Self::Verifier, |
| 255 | "operator" | "incident" | "incident-worker" => Self::Operator, |
| 256 | "summarizer" | "reducer" => Self::Summarizer, |
| 257 | "general" | "" => Self::General, |
| 258 | // Removed slots (e.g. the old "tool-heavy") and unknown names parse |
| 259 | // as Custom. Note the runtime side no longer treats an undeclared |
| 260 | // role as write-capable: it fails closed to the read-only `explore` |
| 261 | // posture (#5575), so this is narrower than the removed variants. |
| 262 | other => Self::Custom(other.to_string()), |
| 263 | } |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | impl Serialize for FleetSlot { |
| 268 | fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error> |
| 269 | where |
| 270 | S: serde::Serializer, |
| 271 | { |
| 272 | serializer.serialize_str(self.as_str()) |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | impl<'de> Deserialize<'de> for FleetSlot { |
| 277 | fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error> |
| 278 | where |
| 279 | D: serde::Deserializer<'de>, |
| 280 | { |
| 281 | let value = String::deserialize(deserializer)?; |
| 282 | Ok(Self::from_name(&value)) |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | /// Model class or route-role hint for a profile. |
| 287 | #[derive(Debug, Clone, PartialEq, Eq, Default)] |
| 288 | pub enum FleetLoadout { |
| 289 | /// Reuse the active session route (the operator's model). Default. |
| 290 | #[default] |
| 291 | Inherit, |
| 292 | /// Route to the provider's faster/cheaper model class for wide fan-out. |
| 293 | Fast, |
| 294 | /// Unrecognized loadout names parse here (including the retired |
| 295 | /// strong/balanced/deep-reasoning/code/review/tool-heavy tiers, which |
| 296 | /// never routed differently). Treated as auto routing. |
| 297 | Custom(String), |
| 298 | } |
| 299 | |
| 300 | impl FleetLoadout { |
| 301 | #[must_use] |
| 302 | pub fn as_str(&self) -> &str { |
| 303 | match self { |
| 304 | Self::Inherit => "inherit", |
| 305 | Self::Fast => "fast", |
| 306 | Self::Custom(value) => value.as_str(), |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | #[must_use] |
| 311 | pub fn from_name(value: &str) -> Self { |
| 312 | match value.trim() { |
| 313 | "inherit" | "default" | "auto" | "" => Self::Inherit, |
| 314 | "fast" => Self::Fast, |
| 315 | // Retired tiers (strong/balanced/deep-reasoning/code/review/ |
| 316 | // tool-heavy) and unknown names parse as Custom → auto routing, |
| 317 | // exactly what those tiers resolved to before removal. |
| 318 | other => Self::Custom(other.to_string()), |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | impl Serialize for FleetLoadout { |
| 324 | fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error> |
| 325 | where |
| 326 | S: serde::Serializer, |
| 327 | { |
| 328 | serializer.serialize_str(self.as_str()) |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | impl<'de> Deserialize<'de> for FleetLoadout { |
| 333 | fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error> |
| 334 | where |
| 335 | D: serde::Deserializer<'de>, |
| 336 | { |
| 337 | let value = String::deserialize(deserializer)?; |
| 338 | Ok(Self::from_name(&value)) |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | /// Legacy Fleet-profile permission payload retained only for source and input |
| 343 | /// compatibility. Runtime ignores it; Fleet identity cannot grant authority. |
| 344 | #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] |
| 345 | pub struct FleetProfilePermissions { |
| 346 | #[doc(hidden)] |
| 347 | #[serde(default, skip_serializing)] |
| 348 | pub allow_shell: bool, |
| 349 | #[doc(hidden)] |
| 350 | #[serde(default, skip_serializing)] |
| 351 | pub trust: bool, |
| 352 | #[doc(hidden)] |
| 353 | #[serde(default = "default_fleet_profile_approval_required", skip_serializing)] |
| 354 | pub approval_required: bool, |
| 355 | } |
| 356 | |
| 357 | pub(crate) fn default_fleet_profile_approval_required() -> bool { |
| 358 | true |
| 359 | } |
| 360 | |
| 361 | impl Default for FleetProfilePermissions { |
| 362 | fn default() -> Self { |
| 363 | Self { |
| 364 | allow_shell: false, |
| 365 | trust: false, |
| 366 | approval_required: true, |
| 367 | } |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | /// Delegation hints for future fleet manager scheduling. |
| 372 | #[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)] |
| 373 | pub struct FleetDelegationHints { |
| 374 | /// Optional profile-level child spawn depth. `None` means inherit existing |
| 375 | /// fleet/sub-agent config. |
| 376 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 377 | pub max_spawn_depth: Option<u32>, |
| 378 | /// Optional profile-level worker concurrency hint. |
| 379 | #[serde( |
| 380 | default, |
| 381 | alias = "concurrency", |
| 382 | skip_serializing_if = "Option::is_none" |
| 383 | )] |
| 384 | pub max_concurrency: Option<usize>, |
| 385 | } |
| 386 | |
| 387 | /// A named role preset that bundles common worker settings. |
| 388 | /// |
| 389 | /// Task specs reference a role name (e.g. `"role": "reviewer"`), and the |
| 390 | /// fleet manager fills in any missing fields from the preset. User-defined |
| 391 | /// roles in `[fleet.roles]` override built-in defaults with the same name. |
| 392 | /// |
| 393 | /// Token budgets and tool-call limits are task-level decisions — they don't |
| 394 | /// belong on role presets. Use `timeout_seconds` as the safety bound. |
| 395 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 396 | pub struct FleetRolePreset { |
| 397 | /// Short description of what this role is for. |
| 398 | #[serde(skip_serializing_if = "Option::is_none")] |
| 399 | pub description: Option<String>, |
| 400 | /// Default tool profile (`"read-only"`, `"read-write"`, or `"custom"`). |
| 401 | #[serde(skip_serializing_if = "Option::is_none")] |
| 402 | pub tool_profile: Option<String>, |
| 403 | /// Default set of tool names available to this role. |
| 404 | #[serde(default, skip_serializing_if = "Vec::is_empty")] |
| 405 | pub tools: Vec<String>, |
| 406 | /// Default capability tags (e.g. `"rust"`, `"git"`, `"gh"`). |
| 407 | #[serde(default, skip_serializing_if = "Vec::is_empty")] |
| 408 | pub capabilities: Vec<String>, |
| 409 | /// Default timeout in seconds for tasks using this role. |
| 410 | #[serde(skip_serializing_if = "Option::is_none")] |
| 411 | pub timeout_seconds: Option<u64>, |
| 412 | } |
| 413 | |
| 414 | impl FleetConfigToml { |
| 415 | /// Resolve a role preset by name. Checks user-defined roles first, |
| 416 | /// then falls back to built-in role defaults. |
| 417 | #[must_use] |
| 418 | pub fn resolve_role(&self, name: &str) -> Option<FleetRolePreset> { |
| 419 | self.roles |
| 420 | .get(name) |
| 421 | .cloned() |
| 422 | .or_else(|| built_in_role_presets().get(name).cloned()) |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | /// Built-in role presets that are always available without config. |
| 427 | #[must_use] |
| 428 | pub fn built_in_role_presets() -> BTreeMap<String, FleetRolePreset> { |
| 429 | [ |
| 430 | ( |
| 431 | "smoke-runner".to_string(), |
| 432 | FleetRolePreset { |
| 433 | description: Some("Lightweight read-only smoke check worker".to_string()), |
| 434 | tool_profile: Some("read-only".to_string()), |
| 435 | tools: vec![], |
| 436 | capabilities: vec![], |
| 437 | timeout_seconds: Some(300), |
| 438 | }, |
| 439 | ), |
| 440 | ( |
| 441 | "reviewer".to_string(), |
| 442 | FleetRolePreset { |
| 443 | description: Some("Read-only code and documentation review".to_string()), |
| 444 | tool_profile: Some("read-only".to_string()), |
| 445 | tools: vec![], |
| 446 | capabilities: vec![], |
| 447 | timeout_seconds: Some(600), |
| 448 | }, |
| 449 | ), |
| 450 | ( |
| 451 | "builder".to_string(), |
| 452 | FleetRolePreset { |
| 453 | description: Some( |
| 454 | "Read-write builder with compilation and test access".to_string(), |
| 455 | ), |
| 456 | tool_profile: Some("read-write".to_string()), |
| 457 | tools: vec![], |
| 458 | capabilities: vec![], |
| 459 | timeout_seconds: Some(1800), |
| 460 | }, |
| 461 | ), |
| 462 | ( |
| 463 | "read-only".to_string(), |
| 464 | FleetRolePreset { |
| 465 | description: Some( |
| 466 | "Minimal read-only observer with no writes or secrets".to_string(), |
| 467 | ), |
| 468 | tool_profile: Some("read-only".to_string()), |
| 469 | tools: vec![], |
| 470 | capabilities: vec![], |
| 471 | timeout_seconds: Some(300), |
| 472 | }, |
| 473 | ), |
| 474 | ] |
| 475 | .into() |
| 476 | } |
| 477 |