batty-cli 0.11.63

Supervised agent execution for software teams
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
//! Team configuration parsed from `.batty/team_config/team.yaml`.

mod types;

pub use types::*;

use std::collections::HashSet;
use std::path::{Path, PathBuf};

use anyhow::{Context, Result, bail};

use super::TEAM_CONFIG_DIR;
use crate::agent;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PlanningDirectiveFile {
    ReplenishmentContext,
    ReviewPolicy,
    EscalationPolicy,
}

impl PlanningDirectiveFile {
    pub fn file_name(self) -> &'static str {
        match self {
            Self::ReplenishmentContext => "replenishment_context.md",
            Self::ReviewPolicy => "review_policy.md",
            Self::EscalationPolicy => "escalation_policy.md",
        }
    }

    pub fn path_for(self, project_root: &Path) -> PathBuf {
        project_root
            .join(".batty")
            .join(TEAM_CONFIG_DIR)
            .join(self.file_name())
    }
}

/// A single validation check result.
#[derive(Debug, Clone)]
pub struct ValidationCheck {
    pub name: String,
    pub passed: bool,
    pub detail: String,
}

impl TeamConfig {
    pub fn trunk_branch(&self) -> &str {
        self.trunk_branch.as_str()
    }

    pub fn resolve_claude_auth(&self, role: &RoleDef) -> ClaudeAuth {
        ClaudeAuth {
            mode: role.auth_mode.unwrap_or_default(),
            env: role.auth_env.clone(),
        }
    }

    pub fn role_def(&self, role_name: &str) -> Option<&RoleDef> {
        self.roles.iter().find(|role| role.name == role_name)
    }

    pub fn role_barrier_group(&self, role_name: &str) -> Option<&str> {
        self.role_def(role_name)
            .and_then(|role| role.barrier_group.as_deref())
    }

    pub fn orchestrator_enabled(&self) -> bool {
        self.workflow_mode.enables_runtime_surface() && self.orchestrator_pane
    }

    /// Resolve the effective agent for a role.
    ///
    /// Resolution order: role-level agent > team-level agent > "claude".
    pub fn resolve_agent(&self, role: &RoleDef) -> Option<String> {
        if role.role_type == RoleType::User {
            return None;
        }
        Some(
            role.agent
                .clone()
                .or_else(|| self.agent.clone())
                .unwrap_or_else(|| "claude".to_string()),
        )
    }

    /// Check if a role is allowed to send messages to another role.
    ///
    /// Uses `talks_to` if configured. If `talks_to` is empty for a role,
    /// falls back to the default hierarchy:
    /// - User <-> Architect
    /// - Architect <-> Manager
    /// - Manager <-> Engineer
    ///
    /// The `from` and `to` are role definition names (not member instance names).
    /// "human" is always allowed to talk to any role.
    pub fn can_talk(&self, from_role: &str, to_role: &str) -> bool {
        // human (CLI user) can always send to anyone
        if from_role == "human" {
            return true;
        }
        // daemon-generated messages (standups, nudges) always allowed
        if from_role == "daemon" {
            return true;
        }
        // external senders (e.g. email-router, slack-bridge) can send to anyone
        if self.external_senders.iter().any(|s| s == from_role) {
            return true;
        }

        let from_def = self.roles.iter().find(|r| r.name == from_role);
        let Some(from_def) = from_def else {
            return false;
        };

        // If talks_to is explicitly configured, use it
        if !from_def.talks_to.is_empty() {
            return from_def.talks_to.iter().any(|t| t == to_role);
        }

        // Default hierarchy: user<->architect, architect<->manager, manager<->engineer
        let to_def = self.roles.iter().find(|r| r.name == to_role);
        let Some(to_def) = to_def else {
            return false;
        };

        matches!(
            (from_def.role_type, to_def.role_type),
            (RoleType::User, RoleType::Architect)
                | (RoleType::Architect, RoleType::User)
                | (RoleType::Architect, RoleType::Manager)
                | (RoleType::Manager, RoleType::Architect)
                | (RoleType::Manager, RoleType::Engineer)
                | (RoleType::Engineer, RoleType::Manager)
        )
    }

    /// Load team config from a YAML file.
    pub fn load(path: &Path) -> Result<Self> {
        let content = std::fs::read_to_string(path)
            .with_context(|| format!("failed to read {}", path.display()))?;
        let config: TeamConfig = serde_yaml::from_str(&content)
            .with_context(|| format!("failed to parse {}", path.display()))?;
        Ok(config)
    }

    /// Validate the team config. Returns an error if invalid.
    pub fn validate(&self) -> Result<()> {
        if self.name.is_empty() {
            bail!("team name cannot be empty");
        }

        if self.trunk_branch.trim().is_empty() {
            bail!("trunk_branch cannot be empty");
        }

        if self.trunk_branch.chars().any(char::is_whitespace) {
            bail!(
                "trunk_branch '{}' is invalid: branch names cannot contain whitespace",
                self.trunk_branch
            );
        }

        if self.roles.is_empty() {
            bail!("team must have at least one role");
        }

        let valid_agents = agent::KNOWN_AGENT_NAMES.join(", ");

        // Validate team-level agent if specified.
        if let Some(team_agent) = self.agent.as_deref() {
            if agent::adapter_from_name(team_agent).is_none() {
                bail!(
                    "unknown team-level agent '{}'; valid agents: {}",
                    team_agent,
                    valid_agents
                );
            }
        }

        let mut role_names: HashSet<&str> = HashSet::new();
        for role in &self.roles {
            if role.name.is_empty() {
                bail!("role has empty name — every role requires a non-empty 'name' field");
            }

            if !role_names.insert(&role.name) {
                bail!("duplicate role name: '{}'", role.name);
            }

            // Non-user roles need an agent — either their own or the team default.
            if role.role_type != RoleType::User && role.agent.is_none() && self.agent.is_none() {
                bail!(
                    "role '{}' has no agent configured — \
                     set a role-level 'agent' field or a team-level 'agent' default; \
                     valid agents: {}",
                    role.name,
                    valid_agents
                );
            }

            if role.role_type == RoleType::User && role.agent.is_some() {
                bail!(
                    "role '{}' is a user but has an agent configured; users use channels instead",
                    role.name
                );
            }

            if role.instances == 0 {
                bail!("role '{}' has zero instances", role.name);
            }

            if let Some(agent_name) = role.agent.as_deref()
                && agent::adapter_from_name(agent_name).is_none()
            {
                bail!(
                    "role '{}' uses unknown agent '{}'; valid agents: {}",
                    role.name,
                    agent_name,
                    valid_agents
                );
            }

            for (instance_name, override_cfg) in &role.instance_overrides {
                if let Some(agent_name) = override_cfg.agent.as_deref()
                    && !super::multi_provider::is_known_instance_override_backend(agent_name)
                {
                    bail!(
                        "role '{}' instance override '{}' uses unknown agent '{}'; valid agents: {}",
                        role.name,
                        instance_name,
                        agent_name,
                        valid_agents
                    );
                }
            }

            let effective_agent = role.agent.as_deref().or(self.agent.as_deref());
            if (role.auth_mode.is_some() || !role.auth_env.is_empty())
                && !matches!(effective_agent, Some("claude" | "claude-code"))
            {
                bail!(
                    "role '{}' configures Claude auth but effective agent is not claude",
                    role.name
                );
            }
            if role.auth_mode != Some(ClaudeAuthMode::Custom) && !role.auth_env.is_empty() {
                bail!(
                    "role '{}' sets auth_env but auth_mode is not 'custom'",
                    role.name
                );
            }
            for env_name in &role.auth_env {
                if !is_valid_env_name(env_name) {
                    bail!(
                        "role '{}' has invalid auth_env entry '{}'; expected shell env name",
                        role.name,
                        env_name
                    );
                }
            }
        }

        if self.workflow_policy.clean_room_mode {
            if self.workflow_policy.handoff_directory.trim().is_empty() {
                bail!("workflow_policy.handoff_directory cannot be empty in clean_room_mode");
            }

            for group_name in self.workflow_policy.barrier_groups.keys() {
                if group_name.trim().is_empty() {
                    bail!("workflow_policy.barrier_groups cannot contain an empty group name");
                }
            }

            for (group_name, roles) in &self.workflow_policy.barrier_groups {
                for role_name in roles {
                    if !role_names.contains(role_name.as_str()) {
                        bail!(
                            "workflow_policy.barrier_groups['{}'] references unknown role '{}'",
                            group_name,
                            role_name
                        );
                    }
                }
            }

            for role in &self.roles {
                if let Some(group) = role.barrier_group.as_deref()
                    && !self.workflow_policy.barrier_groups.is_empty()
                    && !self.workflow_policy.barrier_groups.contains_key(group)
                {
                    bail!(
                        "role '{}' references unknown barrier_group '{}'",
                        role.name,
                        group
                    );
                }
            }
        }

        // Validate talks_to references exist
        let all_role_names: Vec<&str> = role_names.iter().copied().collect();
        for role in &self.roles {
            for target in &role.talks_to {
                if !role_names.contains(target.as_str()) {
                    bail!(
                        "role '{}' references unknown role '{}' in talks_to; \
                         defined roles: {}",
                        role.name,
                        target,
                        all_role_names.join(", ")
                    );
                }
            }
        }

        if let Some(sender) = &self.automation_sender
            && !role_names.contains(sender.as_str())
            && sender != "human"
        {
            bail!(
                "automation_sender references unknown role '{}'; \
                 defined roles: {}",
                sender,
                all_role_names.join(", ")
            );
        }

        // Validate layout zones if present
        if let Some(layout) = &self.layout {
            let total_pct: u32 = layout.zones.iter().map(|z| z.width_pct).sum();
            if total_pct > 100 {
                bail!("layout zone widths sum to {}%, exceeds 100%", total_pct);
            }
        }

        Ok(())
    }

    pub fn validate_project_refs(&self, project_root: &Path) -> Result<()> {
        validate_trunk_branch_exists(project_root, self.trunk_branch())
    }

    /// Run all validation checks, collecting results for each check.
    /// Returns a list of (check_name, passed, detail) tuples.
    pub fn validate_verbose(&self) -> Vec<ValidationCheck> {
        let mut checks = Vec::new();

        // 1. Team name
        let name_ok = !self.name.is_empty();
        checks.push(ValidationCheck {
            name: "team_name".to_string(),
            passed: name_ok,
            detail: if name_ok {
                format!("team name: '{}'", self.name)
            } else {
                "team name is empty".to_string()
            },
        });

        // 2. Roles present
        let roles_ok = !self.roles.is_empty();
        checks.push(ValidationCheck {
            name: "roles_present".to_string(),
            passed: roles_ok,
            detail: if roles_ok {
                format!("{} role(s) defined", self.roles.len())
            } else {
                "no roles defined".to_string()
            },
        });

        if !roles_ok {
            return checks;
        }

        // 3. Team-level agent
        let team_agent_ok = match self.agent.as_deref() {
            Some(name) => agent::adapter_from_name(name).is_some(),
            None => true,
        };
        checks.push(ValidationCheck {
            name: "team_agent".to_string(),
            passed: team_agent_ok,
            detail: match self.agent.as_deref() {
                Some(name) if team_agent_ok => format!("team agent: '{name}'"),
                Some(name) => format!("unknown team agent: '{name}'"),
                None => "no team-level agent (roles must set their own)".to_string(),
            },
        });

        // 4. Per-role checks
        let mut role_names: HashSet<&str> = HashSet::new();
        for role in &self.roles {
            let unique = role_names.insert(&role.name);
            checks.push(ValidationCheck {
                name: format!("role_unique:{}", role.name),
                passed: unique,
                detail: if unique {
                    format!("role '{}' is unique", role.name)
                } else {
                    format!("duplicate role name: '{}'", role.name)
                },
            });

            let has_agent =
                role.role_type == RoleType::User || role.agent.is_some() || self.agent.is_some();
            checks.push(ValidationCheck {
                name: format!("role_agent:{}", role.name),
                passed: has_agent,
                detail: if has_agent {
                    let effective = role
                        .agent
                        .as_deref()
                        .or(self.agent.as_deref())
                        .unwrap_or("(user)");
                    format!("role '{}' agent: {effective}", role.name)
                } else {
                    format!("role '{}' has no agent", role.name)
                },
            });

            if let Some(agent_name) = role.agent.as_deref() {
                let valid = agent::adapter_from_name(agent_name).is_some();
                checks.push(ValidationCheck {
                    name: format!("role_agent_valid:{}", role.name),
                    passed: valid,
                    detail: if valid {
                        format!("role '{}' agent '{}' is valid", role.name, agent_name)
                    } else {
                        format!("role '{}' uses unknown agent '{}'", role.name, agent_name)
                    },
                });
            }

            for (instance_name, override_cfg) in &role.instance_overrides {
                if let Some(agent_name) = override_cfg.agent.as_deref() {
                    let valid =
                        super::multi_provider::is_known_instance_override_backend(agent_name);
                    checks.push(ValidationCheck {
                        name: format!("role_instance_agent_valid:{}:{}", role.name, instance_name),
                        passed: valid,
                        detail: if valid {
                            format!(
                                "role '{}' instance override '{}' agent '{}' is valid",
                                role.name, instance_name, agent_name
                            )
                        } else {
                            format!(
                                "role '{}' instance override '{}' uses unknown agent '{}'",
                                role.name, instance_name, agent_name
                            )
                        },
                    });
                }
            }

            let instances_ok = role.instances > 0;
            checks.push(ValidationCheck {
                name: format!("role_instances:{}", role.name),
                passed: instances_ok,
                detail: format!("role '{}' instances: {}", role.name, role.instances),
            });
        }

        // 5. talks_to references
        for role in &self.roles {
            for target in &role.talks_to {
                let valid = role_names.contains(target.as_str());
                checks.push(ValidationCheck {
                    name: format!("talks_to:{}→{}", role.name, target),
                    passed: valid,
                    detail: if valid {
                        format!("role '{}' → '{}' is valid", role.name, target)
                    } else {
                        format!(
                            "role '{}' references unknown role '{}' in talks_to",
                            role.name, target
                        )
                    },
                });
            }
        }

        // 6. automation_sender
        if let Some(sender) = &self.automation_sender {
            let valid = role_names.contains(sender.as_str()) || sender == "human";
            checks.push(ValidationCheck {
                name: "automation_sender".to_string(),
                passed: valid,
                detail: if valid {
                    format!("automation_sender '{sender}' is valid")
                } else {
                    format!("automation_sender references unknown role '{sender}'")
                },
            });
        }

        // 7. Layout zones
        if let Some(layout) = &self.layout {
            let total_pct: u32 = layout.zones.iter().map(|z| z.width_pct).sum();
            let valid = total_pct <= 100;
            checks.push(ValidationCheck {
                name: "layout_zones".to_string(),
                passed: valid,
                detail: if valid {
                    format!("layout zones sum to {total_pct}%")
                } else {
                    format!("layout zones sum to {total_pct}%, exceeds 100%")
                },
            });
        }

        // 8. Backend health checks (warnings, not failures)
        for (agent_name, health) in self.backend_health_results() {
            let healthy = health.is_healthy();
            checks.push(ValidationCheck {
                name: format!("backend_health:{agent_name}"),
                passed: healthy,
                detail: if healthy {
                    format!("backend '{agent_name}' is available")
                } else {
                    format!(
                        "backend '{agent_name}' binary not found on PATH (status: {})",
                        health.as_str()
                    )
                },
            });
        }

        checks
    }

    /// Collect unique configured backends and their health status.
    pub fn backend_health_results(&self) -> Vec<(String, agent::BackendHealth)> {
        let mut seen = HashSet::new();
        let mut results = Vec::new();
        for role in &self.roles {
            if let Some(agent_name) = self.resolve_agent(role) {
                if seen.insert(agent_name.clone()) {
                    let health = agent::health_check_by_name(&agent_name)
                        .unwrap_or(agent::BackendHealth::Unreachable);
                    results.push((agent_name, health));
                }
            }
        }
        results
    }

    /// Return warning messages for any unhealthy backends.
    pub fn check_backend_health(&self) -> Vec<String> {
        self.backend_health_results()
            .into_iter()
            .filter(|(_, health)| !health.is_healthy())
            .map(|(name, health)| {
                format!(
                    "backend '{name}' binary not found on PATH (status: {})",
                    health.as_str()
                )
            })
            .collect()
    }
}

pub fn validate_trunk_branch_exists(project_root: &Path, trunk_branch: &str) -> Result<()> {
    if !crate::team::git_cmd::is_git_repo(project_root) {
        return Ok(());
    }

    let local_ref = format!("refs/heads/{trunk_branch}");
    let remote_ref = format!("refs/remotes/origin/{trunk_branch}");
    let local_exists = crate::team::git_cmd::show_ref_exists(project_root, &local_ref)
        .with_context(|| format!("failed to inspect configured trunk branch `{trunk_branch}`"))?;
    let remote_exists = crate::team::git_cmd::show_ref_exists(project_root, &remote_ref)
        .with_context(|| format!("failed to inspect configured trunk branch `{trunk_branch}`"))?;

    if !local_exists && !remote_exists {
        bail!(
            "configured trunk_branch `{trunk_branch}` does not exist in {} (expected `{}` or `{}`)",
            project_root.display(),
            local_ref,
            remote_ref
        );
    }

    Ok(())
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ClaudeAuth {
    pub mode: ClaudeAuthMode,
    pub env: Vec<String>,
}

fn is_valid_env_name(value: &str) -> bool {
    let mut chars = value.chars();
    match chars.next() {
        Some(first) if first == '_' || first.is_ascii_alphabetic() => {}
        _ => return false,
    }
    chars.all(|ch| ch == '_' || ch.is_ascii_alphanumeric())
}

pub fn load_planning_directive(
    project_root: &Path,
    directive: PlanningDirectiveFile,
    max_chars: usize,
) -> Result<Option<String>> {
    let path = directive.path_for(project_root);
    match std::fs::read_to_string(&path) {
        Ok(content) => {
            let trimmed = content.trim();
            if trimmed.is_empty() {
                return Ok(None);
            }

            let total_chars = trimmed.chars().count();
            let truncated = trimmed.chars().take(max_chars).collect::<String>();
            if total_chars > max_chars {
                Ok(Some(format!(
                    "{truncated}\n\n[truncated to {max_chars} chars from {}]",
                    directive.file_name()
                )))
            } else {
                Ok(Some(truncated))
            }
        }
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(None),
        Err(error) => Err(error)
            .with_context(|| format!("failed to read planning directive {}", path.display())),
    }
}

#[cfg(test)]
mod tests;
#[cfg(test)]
mod tests_advanced;
#[cfg(test)]
mod tests_proptest;