Skip to main content

batty_cli/
project_registry.rs

1use std::collections::HashSet;
2use std::path::{Path, PathBuf};
3
4use anyhow::{Context, Result, bail};
5use serde::{Deserialize, Serialize};
6use serde_json::Value;
7
8use crate::tmux;
9
10const REGISTRY_KIND: &str = "batty.projectRegistry";
11pub const REGISTRY_SCHEMA_VERSION: u32 = 2;
12const REGISTRY_FILENAME: &str = "project-registry.json";
13const REGISTRY_PATH_ENV: &str = "BATTY_PROJECT_REGISTRY_PATH";
14
15const ROUTING_STATE_KIND: &str = "batty.projectRoutingState";
16pub const ROUTING_STATE_SCHEMA_VERSION: u32 = 1;
17const ROUTING_STATE_FILENAME: &str = "project-routing-state.json";
18const ROUTING_STATE_PATH_ENV: &str = "BATTY_PROJECT_ROUTING_STATE_PATH";
19
20#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
21#[serde(rename_all = "camelCase")]
22pub struct ProjectRegistry {
23    pub kind: String,
24    pub schema_version: u32,
25    #[serde(default)]
26    pub projects: Vec<RegisteredProject>,
27}
28
29impl Default for ProjectRegistry {
30    fn default() -> Self {
31        Self {
32            kind: REGISTRY_KIND.to_string(),
33            schema_version: REGISTRY_SCHEMA_VERSION,
34            projects: Vec::new(),
35        }
36    }
37}
38
39#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
40#[serde(rename_all = "camelCase")]
41pub struct RegisteredProject {
42    pub project_id: String,
43    pub name: String,
44    #[serde(default)]
45    pub aliases: Vec<String>,
46    pub project_root: PathBuf,
47    pub board_dir: PathBuf,
48    pub team_name: String,
49    pub session_name: String,
50    #[serde(default)]
51    pub channel_bindings: Vec<ProjectChannelBinding>,
52    pub owner: Option<String>,
53    #[serde(default)]
54    pub tags: Vec<String>,
55    #[serde(default)]
56    pub policy_flags: ProjectPolicyFlags,
57    pub created_at: u64,
58    pub updated_at: u64,
59}
60
61#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
62#[serde(rename_all = "camelCase")]
63pub struct ProjectChannelBinding {
64    pub channel: String,
65    pub binding: String,
66    #[serde(default, skip_serializing_if = "Option::is_none")]
67    pub thread_binding: Option<String>,
68}
69
70#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
71#[serde(rename_all = "camelCase")]
72pub struct ProjectPolicyFlags {
73    #[serde(default)]
74    pub allow_openclaw_supervision: bool,
75    #[serde(default)]
76    pub allow_cross_project_routing: bool,
77    #[serde(default)]
78    pub allow_shared_service_routing: bool,
79    #[serde(default)]
80    pub archived: bool,
81}
82
83#[derive(Debug, Clone, PartialEq, Eq)]
84pub struct ProjectRegistration {
85    pub project_id: String,
86    pub name: String,
87    pub aliases: Vec<String>,
88    pub project_root: PathBuf,
89    pub board_dir: PathBuf,
90    pub team_name: String,
91    pub session_name: String,
92    pub channel_bindings: Vec<ProjectChannelBinding>,
93    pub owner: Option<String>,
94    pub tags: Vec<String>,
95    pub policy_flags: ProjectPolicyFlags,
96}
97
98#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
99#[serde(rename_all = "camelCase")]
100pub enum ProjectLifecycleState {
101    Running,
102    Stopped,
103    Degraded,
104    Recovering,
105}
106
107#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
108#[serde(rename_all = "camelCase")]
109pub enum ProjectLifecycleAction {
110    Start,
111    Stop,
112    Restart,
113}
114
115#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
116#[serde(rename_all = "camelCase")]
117pub struct ProjectHealthSummary {
118    pub paused: bool,
119    pub watchdog_state: String,
120    pub unhealthy_members: Vec<String>,
121    pub member_count: usize,
122    pub active_member_count: usize,
123    pub pending_inbox_count: usize,
124    pub triage_backlog_count: usize,
125}
126
127#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
128#[serde(rename_all = "camelCase")]
129pub struct ProjectPipelineMetrics {
130    pub active_task_count: usize,
131    pub review_queue_count: usize,
132    pub runnable_count: u32,
133    pub blocked_count: u32,
134    pub stale_in_progress_count: u32,
135    pub stale_review_count: u32,
136    pub auto_merge_rate: Option<f64>,
137    pub rework_rate: Option<f64>,
138    pub avg_review_latency_secs: Option<f64>,
139}
140
141#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
142#[serde(rename_all = "camelCase")]
143pub struct ProjectStatusDto {
144    pub project_id: String,
145    pub name: String,
146    pub team_name: String,
147    pub session_name: String,
148    pub project_root: PathBuf,
149    pub lifecycle: ProjectLifecycleState,
150    pub running: bool,
151    pub health: ProjectHealthSummary,
152    pub pipeline: ProjectPipelineMetrics,
153}
154
155#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
156#[serde(rename_all = "camelCase")]
157pub struct ProjectLifecycleActionResult {
158    pub project_id: String,
159    pub action: ProjectLifecycleAction,
160    pub changed: bool,
161    pub lifecycle: ProjectLifecycleState,
162    pub running: bool,
163    pub audit_message: String,
164    pub status: ProjectStatusDto,
165}
166
167#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
168#[serde(rename_all = "camelCase")]
169pub struct ProjectRoutingState {
170    pub kind: String,
171    pub schema_version: u32,
172    #[serde(default)]
173    pub selections: Vec<ActiveProjectSelection>,
174}
175
176impl Default for ProjectRoutingState {
177    fn default() -> Self {
178        Self {
179            kind: ROUTING_STATE_KIND.to_string(),
180            schema_version: ROUTING_STATE_SCHEMA_VERSION,
181            selections: Vec::new(),
182        }
183    }
184}
185
186#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
187#[serde(rename_all = "camelCase")]
188pub struct ActiveProjectSelection {
189    pub project_id: String,
190    pub scope: ActiveProjectScope,
191    pub updated_at: u64,
192}
193
194#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
195#[serde(tag = "kind", rename_all = "camelCase")]
196pub enum ActiveProjectScope {
197    Global,
198    Channel {
199        channel: String,
200        binding: String,
201    },
202    Thread {
203        channel: String,
204        binding: String,
205        thread_binding: String,
206    },
207}
208
209#[derive(Debug, Clone, PartialEq, Eq)]
210pub struct ProjectRoutingRequest {
211    pub message: String,
212    pub channel: Option<String>,
213    pub binding: Option<String>,
214    pub thread_binding: Option<String>,
215}
216
217#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
218#[serde(rename_all = "snake_case")]
219pub enum RoutingConfidence {
220    High,
221    Medium,
222    Low,
223}
224
225#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
226#[serde(rename_all = "camelCase")]
227pub struct ProjectRoutingCandidate {
228    pub project_id: String,
229    pub reason: String,
230    pub score: u32,
231}
232
233#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
234#[serde(rename_all = "camelCase")]
235pub struct ProjectRoutingDecision {
236    pub selected_project_id: Option<String>,
237    pub requires_confirmation: bool,
238    pub confidence: RoutingConfidence,
239    pub reason: String,
240    #[serde(default)]
241    pub candidates: Vec<ProjectRoutingCandidate>,
242}
243
244#[derive(Debug, Deserialize)]
245#[serde(rename_all = "camelCase")]
246struct ProjectRegistryV1 {
247    kind: String,
248    schema_version: u32,
249    #[serde(default)]
250    projects: Vec<RegisteredProjectV1>,
251}
252
253#[derive(Debug, Deserialize)]
254#[serde(rename_all = "camelCase")]
255struct RegisteredProjectV1 {
256    project_id: String,
257    name: String,
258    project_root: PathBuf,
259    board_dir: PathBuf,
260    team_name: String,
261    session_name: String,
262    #[serde(default)]
263    channel_bindings: Vec<ProjectChannelBindingV1>,
264    owner: Option<String>,
265    #[serde(default)]
266    tags: Vec<String>,
267    #[serde(default)]
268    policy_flags: ProjectPolicyFlags,
269    created_at: u64,
270    updated_at: u64,
271}
272
273#[derive(Debug, Deserialize)]
274#[serde(rename_all = "camelCase")]
275struct ProjectChannelBindingV1 {
276    channel: String,
277    binding: String,
278}
279
280pub fn registry_path() -> Result<PathBuf> {
281    if let Some(path) = std::env::var_os(REGISTRY_PATH_ENV) {
282        return Ok(PathBuf::from(path));
283    }
284
285    let home = std::env::var("HOME").context("cannot determine home directory")?;
286    Ok(PathBuf::from(home).join(".batty").join(REGISTRY_FILENAME))
287}
288
289pub fn routing_state_path() -> Result<PathBuf> {
290    if let Some(path) = std::env::var_os(ROUTING_STATE_PATH_ENV) {
291        return Ok(PathBuf::from(path));
292    }
293
294    let home = std::env::var("HOME").context("cannot determine home directory")?;
295    Ok(PathBuf::from(home)
296        .join(".batty")
297        .join(ROUTING_STATE_FILENAME))
298}
299
300pub fn load_registry() -> Result<ProjectRegistry> {
301    load_registry_at(&registry_path()?)
302}
303
304pub fn register_project(registration: ProjectRegistration) -> Result<RegisteredProject> {
305    register_project_at(&registry_path()?, registration)
306}
307
308pub fn unregister_project(project_id: &str) -> Result<Option<RegisteredProject>> {
309    unregister_project_at(&registry_path()?, project_id)
310}
311
312pub fn list_projects() -> Result<Vec<RegisteredProject>> {
313    let mut projects = load_registry()?.projects;
314    projects.sort_by(|left, right| left.project_id.cmp(&right.project_id));
315    Ok(projects)
316}
317
318pub fn get_project(project_id: &str) -> Result<Option<RegisteredProject>> {
319    get_project_at(&registry_path()?, project_id)
320}
321
322pub fn get_project_status(project_id: &str) -> Result<ProjectStatusDto> {
323    let Some(project) = get_project(project_id)? else {
324        bail!("project '{}' is not registered", project_id);
325    };
326    project_status(&project)
327}
328
329pub fn start_project(project_id: &str) -> Result<ProjectLifecycleActionResult> {
330    let Some(project) = get_project(project_id)? else {
331        bail!("project '{}' is not registered", project_id);
332    };
333    let status = project_status(&project)?;
334    if status.running {
335        return Ok(ProjectLifecycleActionResult {
336            project_id: project.project_id.clone(),
337            action: ProjectLifecycleAction::Start,
338            changed: false,
339            lifecycle: status.lifecycle,
340            running: status.running,
341            audit_message: format!("project '{}' is already running", project.project_id),
342            status,
343        });
344    }
345
346    crate::team::start_team(&project.project_root, false)?;
347    let status = project_status(&project)?;
348    Ok(ProjectLifecycleActionResult {
349        project_id: project.project_id.clone(),
350        action: ProjectLifecycleAction::Start,
351        changed: true,
352        lifecycle: status.lifecycle,
353        running: status.running,
354        audit_message: format!(
355            "started project '{}' in session {}",
356            project.project_id, project.session_name
357        ),
358        status,
359    })
360}
361
362pub fn stop_project(project_id: &str) -> Result<ProjectLifecycleActionResult> {
363    let Some(project) = get_project(project_id)? else {
364        bail!("project '{}' is not registered", project_id);
365    };
366    let status = project_status(&project)?;
367    if !status.running {
368        return Ok(ProjectLifecycleActionResult {
369            project_id: project.project_id.clone(),
370            action: ProjectLifecycleAction::Stop,
371            changed: false,
372            lifecycle: status.lifecycle,
373            running: status.running,
374            audit_message: format!("project '{}' is already stopped", project.project_id),
375            status,
376        });
377    }
378
379    crate::team::stop_team(&project.project_root)?;
380    let status = project_status(&project)?;
381    Ok(ProjectLifecycleActionResult {
382        project_id: project.project_id.clone(),
383        action: ProjectLifecycleAction::Stop,
384        changed: true,
385        lifecycle: status.lifecycle,
386        running: status.running,
387        audit_message: format!(
388            "stopped project '{}' and recorded shutdown summary",
389            project.project_id
390        ),
391        status,
392    })
393}
394
395pub fn restart_project(project_id: &str) -> Result<ProjectLifecycleActionResult> {
396    let Some(project) = get_project(project_id)? else {
397        bail!("project '{}' is not registered", project_id);
398    };
399    let before = project_status(&project)?;
400    if before.running {
401        crate::team::stop_team(&project.project_root)?;
402    }
403    crate::team::start_team(&project.project_root, false)?;
404    let status = project_status(&project)?;
405    Ok(ProjectLifecycleActionResult {
406        project_id: project.project_id.clone(),
407        action: ProjectLifecycleAction::Restart,
408        changed: true,
409        lifecycle: status.lifecycle,
410        running: status.running,
411        audit_message: if before.running {
412            format!(
413                "restarted project '{}' in session {}",
414                project.project_id, project.session_name
415            )
416        } else {
417            format!(
418                "started stopped project '{}' via restart in session {}",
419                project.project_id, project.session_name
420            )
421        },
422        status,
423    })
424}
425
426pub fn load_routing_state() -> Result<ProjectRoutingState> {
427    load_routing_state_at(&routing_state_path()?)
428}
429
430pub fn set_active_project(
431    project_id: &str,
432    scope: ActiveProjectScope,
433) -> Result<ActiveProjectSelection> {
434    set_active_project_at(&registry_path()?, &routing_state_path()?, project_id, scope)
435}
436
437pub fn resolve_project_for_message(
438    request: &ProjectRoutingRequest,
439) -> Result<ProjectRoutingDecision> {
440    resolve_project_for_message_at(&registry_path()?, &routing_state_path()?, request)
441}
442
443pub fn load_registry_at(path: &Path) -> Result<ProjectRegistry> {
444    if !path.exists() {
445        return Ok(ProjectRegistry::default());
446    }
447
448    let content = std::fs::read_to_string(path)
449        .with_context(|| format!("failed to read registry {}", path.display()))?;
450    let raw: Value = serde_json::from_str(&content)
451        .with_context(|| format!("failed to parse registry {}", path.display()))?;
452
453    let Some(kind) = raw.get("kind").and_then(Value::as_str) else {
454        bail!("registry {} is missing kind", path.display());
455    };
456    if kind != REGISTRY_KIND {
457        bail!(
458            "registry {} has unsupported kind '{}' (expected '{}')",
459            path.display(),
460            kind,
461            REGISTRY_KIND
462        );
463    }
464
465    let Some(schema_version) = raw
466        .get("schemaVersion")
467        .and_then(Value::as_u64)
468        .map(|value| value as u32)
469    else {
470        bail!("registry {} is missing schemaVersion", path.display());
471    };
472
473    let registry = match schema_version {
474        1 => migrate_registry_v1(raw)?,
475        REGISTRY_SCHEMA_VERSION => serde_json::from_value(raw)
476            .with_context(|| format!("failed to decode registry {}", path.display()))?,
477        other => {
478            bail!(
479                "registry {} uses unsupported schemaVersion {}",
480                path.display(),
481                other
482            )
483        }
484    };
485    validate_registry(&registry)?;
486    Ok(registry)
487}
488
489pub fn load_routing_state_at(path: &Path) -> Result<ProjectRoutingState> {
490    if !path.exists() {
491        return Ok(ProjectRoutingState::default());
492    }
493
494    let content = std::fs::read_to_string(path)
495        .with_context(|| format!("failed to read routing state {}", path.display()))?;
496    let raw: Value = serde_json::from_str(&content)
497        .with_context(|| format!("failed to parse routing state {}", path.display()))?;
498
499    let Some(kind) = raw.get("kind").and_then(Value::as_str) else {
500        bail!("routing state {} is missing kind", path.display());
501    };
502    if kind != ROUTING_STATE_KIND {
503        bail!(
504            "routing state {} has unsupported kind '{}' (expected '{}')",
505            path.display(),
506            kind,
507            ROUTING_STATE_KIND
508        );
509    }
510
511    let Some(schema_version) = raw
512        .get("schemaVersion")
513        .and_then(Value::as_u64)
514        .map(|value| value as u32)
515    else {
516        bail!("routing state {} is missing schemaVersion", path.display());
517    };
518    if schema_version != ROUTING_STATE_SCHEMA_VERSION {
519        bail!(
520            "routing state {} uses unsupported schemaVersion {}",
521            path.display(),
522            schema_version
523        );
524    }
525
526    let state: ProjectRoutingState = serde_json::from_value(raw)
527        .with_context(|| format!("failed to decode routing state {}", path.display()))?;
528    validate_routing_state(&state)?;
529    Ok(state)
530}
531
532pub fn register_project_at(
533    path: &Path,
534    registration: ProjectRegistration,
535) -> Result<RegisteredProject> {
536    let mut registry = load_registry_at(path)?;
537    let project = normalize_registration(registration)?;
538    ensure_unique(&registry, &project)?;
539
540    registry.projects.push(project.clone());
541    registry
542        .projects
543        .sort_by(|left, right| left.project_id.cmp(&right.project_id));
544    save_registry(path, &registry)?;
545    Ok(project)
546}
547
548pub fn unregister_project_at(path: &Path, project_id: &str) -> Result<Option<RegisteredProject>> {
549    let mut registry = load_registry_at(path)?;
550    if let Some(index) = registry
551        .projects
552        .iter()
553        .position(|project| project.project_id == project_id)
554    {
555        let removed = registry.projects.remove(index);
556        save_registry(path, &registry)?;
557        Ok(Some(removed))
558    } else {
559        Ok(None)
560    }
561}
562
563pub fn get_project_at(path: &Path, project_id: &str) -> Result<Option<RegisteredProject>> {
564    let registry = load_registry_at(path)?;
565    Ok(registry
566        .projects
567        .into_iter()
568        .find(|project| project.project_id == project_id))
569}
570
571pub fn set_active_project_at(
572    registry_path: &Path,
573    state_path: &Path,
574    project_id: &str,
575    scope: ActiveProjectScope,
576) -> Result<ActiveProjectSelection> {
577    let registry = load_registry_at(registry_path)?;
578    if registry
579        .projects
580        .iter()
581        .all(|project| project.project_id != project_id)
582    {
583        bail!("project '{}' is not registered", project_id);
584    }
585
586    let mut state = load_routing_state_at(state_path)?;
587    let selection = ActiveProjectSelection {
588        project_id: project_id.to_string(),
589        scope,
590        updated_at: crate::team::now_unix(),
591    };
592
593    state
594        .selections
595        .retain(|existing| !same_scope(&existing.scope, &selection.scope));
596    state.selections.push(selection.clone());
597    sort_selections(&mut state.selections);
598    save_routing_state(state_path, &state)?;
599    Ok(selection)
600}
601
602pub fn resolve_project_for_message_at(
603    registry_path: &Path,
604    state_path: &Path,
605    request: &ProjectRoutingRequest,
606) -> Result<ProjectRoutingDecision> {
607    let registry = load_registry_at(registry_path)?;
608    let routing_state = load_routing_state_at(state_path).unwrap_or_default();
609    let projects = registry
610        .projects
611        .iter()
612        .filter(|project| !project.policy_flags.archived)
613        .collect::<Vec<_>>();
614
615    if projects.is_empty() {
616        return Ok(ProjectRoutingDecision {
617            selected_project_id: None,
618            requires_confirmation: true,
619            confidence: RoutingConfidence::Low,
620            reason: "No active projects are registered.".to_string(),
621            candidates: Vec::new(),
622        });
623    }
624
625    let control_action = looks_like_control_action(&request.message);
626    if projects.len() == 1 {
627        let project = projects[0];
628        let requires_confirmation = control_action;
629        return Ok(ProjectRoutingDecision {
630            selected_project_id: Some(project.project_id.clone()),
631            requires_confirmation,
632            confidence: if requires_confirmation {
633                RoutingConfidence::Medium
634            } else {
635                RoutingConfidence::High
636            },
637            reason: if requires_confirmation {
638                format!(
639                    "Only one registered project exists ({}), but this looks like a control action and should be confirmed.",
640                    project.project_id
641                )
642            } else {
643                format!(
644                    "Selected {} because it is the only registered project.",
645                    project.project_id
646                )
647            },
648            candidates: vec![ProjectRoutingCandidate {
649                project_id: project.project_id.clone(),
650                reason: "only registered project".to_string(),
651                score: 100,
652            }],
653        });
654    }
655
656    let mut candidates = projects
657        .iter()
658        .filter_map(|project| score_project(project, &routing_state, request))
659        .collect::<Vec<_>>();
660    candidates.sort_by(|left, right| {
661        right
662            .score
663            .cmp(&left.score)
664            .then_with(|| left.project_id.cmp(&right.project_id))
665    });
666
667    let Some(top) = candidates.first().cloned() else {
668        return Ok(ProjectRoutingDecision {
669            selected_project_id: None,
670            requires_confirmation: true,
671            confidence: RoutingConfidence::Low,
672            reason: "Message did not identify a project with high confidence. Ask the user to choose a projectId.".to_string(),
673            candidates,
674        });
675    };
676
677    let ambiguous = candidates
678        .get(1)
679        .is_some_and(|second| second.score + 10 >= top.score);
680    let requires_confirmation = ambiguous || !auto_route_allowed(&top, control_action);
681
682    Ok(ProjectRoutingDecision {
683        selected_project_id: (!ambiguous).then_some(top.project_id.clone()),
684        requires_confirmation,
685        confidence: routing_confidence(top.score),
686        reason: routing_reason(&top, ambiguous, control_action),
687        candidates,
688    })
689}
690
691pub fn parse_channel_binding(spec: &str) -> Result<ProjectChannelBinding> {
692    let Some((channel, binding)) = spec.split_once('=') else {
693        bail!("invalid channel binding '{spec}'; expected <channel>=<binding>");
694    };
695
696    let channel = trim_required("channelBinding.channel", channel)?;
697    let binding = trim_required("channelBinding.binding", binding)?;
698    Ok(ProjectChannelBinding {
699        channel,
700        binding,
701        thread_binding: None,
702    })
703}
704
705pub fn parse_thread_binding(spec: &str) -> Result<ProjectChannelBinding> {
706    let Some((channel_spec, thread_binding)) = spec.split_once('#') else {
707        bail!("invalid thread binding '{spec}'; expected <channel>=<binding>#<thread-binding>");
708    };
709    let mut binding = parse_channel_binding(channel_spec)?;
710    binding.thread_binding = Some(trim_required(
711        "channelBinding.threadBinding",
712        thread_binding,
713    )?);
714    Ok(binding)
715}
716
717fn migrate_registry_v1(raw: Value) -> Result<ProjectRegistry> {
718    let legacy: ProjectRegistryV1 =
719        serde_json::from_value(raw).context("failed to decode legacy schemaVersion 1 registry")?;
720    if legacy.kind != REGISTRY_KIND {
721        bail!("registry kind must be '{}'", REGISTRY_KIND);
722    }
723    if legacy.schema_version != 1 {
724        bail!("legacy registry schemaVersion must be 1");
725    }
726
727    Ok(ProjectRegistry {
728        kind: REGISTRY_KIND.to_string(),
729        schema_version: REGISTRY_SCHEMA_VERSION,
730        projects: legacy
731            .projects
732            .into_iter()
733            .map(|project| RegisteredProject {
734                project_id: project.project_id,
735                name: project.name,
736                aliases: Vec::new(),
737                project_root: project.project_root,
738                board_dir: project.board_dir,
739                team_name: project.team_name,
740                session_name: project.session_name,
741                channel_bindings: project
742                    .channel_bindings
743                    .into_iter()
744                    .map(|binding| ProjectChannelBinding {
745                        channel: binding.channel,
746                        binding: binding.binding,
747                        thread_binding: None,
748                    })
749                    .collect(),
750                owner: project.owner,
751                tags: project.tags,
752                policy_flags: project.policy_flags,
753                created_at: project.created_at,
754                updated_at: project.updated_at,
755            })
756            .collect(),
757    })
758}
759
760fn save_registry(path: &Path, registry: &ProjectRegistry) -> Result<()> {
761    validate_registry(registry)?;
762
763    if let Some(parent) = path.parent() {
764        std::fs::create_dir_all(parent)
765            .with_context(|| format!("failed to create registry dir {}", parent.display()))?;
766    }
767
768    let content = serde_json::to_string_pretty(registry)?;
769    std::fs::write(path, format!("{content}\n"))
770        .with_context(|| format!("failed to write registry {}", path.display()))?;
771    Ok(())
772}
773
774fn save_routing_state(path: &Path, state: &ProjectRoutingState) -> Result<()> {
775    validate_routing_state(state)?;
776
777    if let Some(parent) = path.parent() {
778        std::fs::create_dir_all(parent)
779            .with_context(|| format!("failed to create routing state dir {}", parent.display()))?;
780    }
781
782    let content = serde_json::to_string_pretty(state)?;
783    std::fs::write(path, format!("{content}\n"))
784        .with_context(|| format!("failed to write routing state {}", path.display()))?;
785    Ok(())
786}
787
788fn validate_registry(registry: &ProjectRegistry) -> Result<()> {
789    if registry.kind != REGISTRY_KIND {
790        bail!("registry kind must be '{}'", REGISTRY_KIND);
791    }
792    if registry.schema_version != REGISTRY_SCHEMA_VERSION {
793        bail!("registry schemaVersion must be {}", REGISTRY_SCHEMA_VERSION);
794    }
795
796    let mut project_ids = HashSet::new();
797    let mut project_roots = HashSet::new();
798    let mut team_names = HashSet::new();
799    let mut session_names = HashSet::new();
800    let mut aliases = HashSet::new();
801
802    for project in &registry.projects {
803        validate_project(project)?;
804
805        if !project_ids.insert(project.project_id.clone()) {
806            bail!("duplicate projectId '{}'", project.project_id);
807        }
808        if !project_roots.insert(project.project_root.clone()) {
809            bail!("duplicate projectRoot '{}'", project.project_root.display());
810        }
811        if !team_names.insert(project.team_name.clone()) {
812            bail!("duplicate teamName '{}'", project.team_name);
813        }
814        if !session_names.insert(project.session_name.clone()) {
815            bail!("duplicate sessionName '{}'", project.session_name);
816        }
817        for alias in &project.aliases {
818            if !aliases.insert(alias.clone()) {
819                bail!("duplicate alias '{}'", alias);
820            }
821        }
822    }
823
824    Ok(())
825}
826
827fn validate_routing_state(state: &ProjectRoutingState) -> Result<()> {
828    if state.kind != ROUTING_STATE_KIND {
829        bail!("routing state kind must be '{}'", ROUTING_STATE_KIND);
830    }
831    if state.schema_version != ROUTING_STATE_SCHEMA_VERSION {
832        bail!(
833            "routing state schemaVersion must be {}",
834            ROUTING_STATE_SCHEMA_VERSION
835        );
836    }
837
838    let mut scopes = HashSet::new();
839    for selection in &state.selections {
840        validate_project_id(&selection.project_id)?;
841        if !scopes.insert(selection.scope.clone()) {
842            bail!("duplicate active-project scope in routing state");
843        }
844    }
845    Ok(())
846}
847
848fn ensure_unique(registry: &ProjectRegistry, project: &RegisteredProject) -> Result<()> {
849    if registry
850        .projects
851        .iter()
852        .any(|existing| existing.project_id == project.project_id)
853    {
854        bail!("projectId '{}' is already registered", project.project_id);
855    }
856    if registry
857        .projects
858        .iter()
859        .any(|existing| existing.project_root == project.project_root)
860    {
861        bail!(
862            "projectRoot '{}' is already registered",
863            project.project_root.display()
864        );
865    }
866    if registry
867        .projects
868        .iter()
869        .any(|existing| existing.team_name == project.team_name)
870    {
871        bail!("teamName '{}' is already registered", project.team_name);
872    }
873    if registry
874        .projects
875        .iter()
876        .any(|existing| existing.session_name == project.session_name)
877    {
878        bail!(
879            "sessionName '{}' is already registered",
880            project.session_name
881        );
882    }
883
884    let existing_aliases = registry
885        .projects
886        .iter()
887        .flat_map(|existing| existing.aliases.iter())
888        .cloned()
889        .collect::<HashSet<_>>();
890    for alias in &project.aliases {
891        if existing_aliases.contains(alias) {
892            bail!("alias '{}' is already registered", alias);
893        }
894    }
895    Ok(())
896}
897
898fn normalize_registration(registration: ProjectRegistration) -> Result<RegisteredProject> {
899    validate_project_id(&registration.project_id)?;
900
901    let name = trim_required("name", &registration.name)?;
902    let team_name = trim_required("teamName", &registration.team_name)?;
903    let session_name = trim_required("sessionName", &registration.session_name)?;
904    let owner = registration
905        .owner
906        .as_deref()
907        .map(str::trim)
908        .and_then(|value| {
909            if value.is_empty() {
910                None
911            } else {
912                Some(value.to_string())
913            }
914        });
915
916    let project_root = normalize_path(&registration.project_root)?;
917    let board_dir = normalize_path(&registration.board_dir)?;
918    if !board_dir.starts_with(&project_root) {
919        bail!(
920            "boardDir '{}' must be inside projectRoot '{}'",
921            board_dir.display(),
922            project_root.display()
923        );
924    }
925
926    let aliases = normalize_labels("alias", registration.aliases)?;
927    let tags = normalize_labels("tag", registration.tags)?;
928
929    let mut seen_bindings = HashSet::new();
930    let mut channel_bindings = Vec::with_capacity(registration.channel_bindings.len());
931    for binding in registration.channel_bindings {
932        let channel = trim_required("channelBinding.channel", &binding.channel)?;
933        let binding_value = trim_required("channelBinding.binding", &binding.binding)?;
934        let thread_binding = binding
935            .thread_binding
936            .as_deref()
937            .map(|value| trim_required("channelBinding.threadBinding", value))
938            .transpose()?;
939        let binding_key = (
940            channel.clone(),
941            binding_value.clone(),
942            thread_binding.clone().unwrap_or_default(),
943        );
944        if !seen_bindings.insert(binding_key) {
945            bail!("duplicate channel/thread binding for channel '{}'", channel);
946        }
947        channel_bindings.push(ProjectChannelBinding {
948            channel,
949            binding: binding_value,
950            thread_binding,
951        });
952    }
953    channel_bindings.sort_by(|left, right| {
954        left.channel
955            .cmp(&right.channel)
956            .then_with(|| left.binding.cmp(&right.binding))
957            .then_with(|| left.thread_binding.cmp(&right.thread_binding))
958    });
959
960    let now = crate::team::now_unix();
961    let project = RegisteredProject {
962        project_id: registration.project_id,
963        name,
964        aliases,
965        project_root,
966        board_dir,
967        team_name,
968        session_name,
969        channel_bindings,
970        owner,
971        tags,
972        policy_flags: registration.policy_flags,
973        created_at: now,
974        updated_at: now,
975    };
976
977    validate_project(&project)?;
978    Ok(project)
979}
980
981fn project_status(project: &RegisteredProject) -> Result<ProjectStatusDto> {
982    let report = load_project_status_report(project)?;
983    let lifecycle = resolve_lifecycle_state(&report);
984    let workflow_metrics = report.workflow_metrics.unwrap_or_default();
985
986    Ok(ProjectStatusDto {
987        project_id: project.project_id.clone(),
988        name: project.name.clone(),
989        team_name: project.team_name.clone(),
990        session_name: project.session_name.clone(),
991        project_root: project.project_root.clone(),
992        lifecycle,
993        running: report.running,
994        health: ProjectHealthSummary {
995            paused: report.paused,
996            watchdog_state: report.watchdog.state,
997            unhealthy_members: report.health.unhealthy_members,
998            member_count: report.health.member_count,
999            active_member_count: report.health.active_member_count,
1000            pending_inbox_count: report.health.pending_inbox_count,
1001            triage_backlog_count: report.health.triage_backlog_count,
1002        },
1003        pipeline: ProjectPipelineMetrics {
1004            active_task_count: report.active_tasks.len(),
1005            review_queue_count: report.review_queue.len(),
1006            runnable_count: workflow_metrics.runnable_count,
1007            blocked_count: workflow_metrics.blocked_count,
1008            stale_in_progress_count: workflow_metrics.stale_in_progress_count,
1009            stale_review_count: workflow_metrics.stale_review_count,
1010            auto_merge_rate: workflow_metrics.auto_merge_rate,
1011            rework_rate: workflow_metrics.rework_rate,
1012            avg_review_latency_secs: workflow_metrics.avg_review_latency_secs,
1013        },
1014    })
1015}
1016
1017fn resolve_lifecycle_state(
1018    report: &crate::team::status::TeamStatusJsonReport,
1019) -> ProjectLifecycleState {
1020    if !report.running {
1021        ProjectLifecycleState::Stopped
1022    } else if report.watchdog.state == "restarting" {
1023        ProjectLifecycleState::Recovering
1024    } else if report.paused
1025        || report.watchdog.state == "circuit-open"
1026        || !report.health.unhealthy_members.is_empty()
1027    {
1028        ProjectLifecycleState::Degraded
1029    } else {
1030        ProjectLifecycleState::Running
1031    }
1032}
1033
1034fn load_project_status_report(
1035    project: &RegisteredProject,
1036) -> Result<crate::team::status::TeamStatusJsonReport> {
1037    let config_path = crate::team::team_config_path(&project.project_root);
1038    if !config_path.exists() {
1039        bail!(
1040            "no team config found for project '{}' at {}",
1041            project.project_id,
1042            config_path.display()
1043        );
1044    }
1045
1046    let team_config = crate::team::config::TeamConfig::load(&config_path)?;
1047    let members = crate::team::hierarchy::resolve_hierarchy(&team_config)?;
1048    let session_running = tmux::session_exists(&project.session_name);
1049    let runtime_statuses = if session_running {
1050        crate::team::status::list_runtime_member_statuses(&project.session_name).unwrap_or_default()
1051    } else {
1052        std::collections::HashMap::new()
1053    };
1054    let pending_inbox_counts =
1055        crate::team::status::pending_inbox_counts(&project.project_root, &members);
1056    let triage_backlog_counts =
1057        crate::team::status::triage_backlog_counts(&project.project_root, &members);
1058    let owned_task_buckets =
1059        crate::team::status::owned_task_buckets(&project.project_root, &members);
1060    let branch_mismatches =
1061        crate::team::status::branch_mismatch_by_member(&project.project_root, &members);
1062    let worktree_staleness =
1063        crate::team::status::worktree_staleness_by_member(&project.project_root, &members);
1064    let agent_health = crate::team::status::agent_health_by_member(&project.project_root, &members);
1065    let paused = crate::team::pause_marker_path(&project.project_root).exists();
1066    let rows = crate::team::status::build_team_status_rows(
1067        &members,
1068        session_running,
1069        &runtime_statuses,
1070        &pending_inbox_counts,
1071        &triage_backlog_counts,
1072        &owned_task_buckets,
1073        &branch_mismatches,
1074        &worktree_staleness,
1075        &agent_health,
1076    );
1077    let workflow_metrics =
1078        crate::team::status::workflow_metrics_section(&project.project_root, &members)
1079            .map(|(_, metrics)| metrics);
1080    let watchdog =
1081        crate::team::status::load_watchdog_status(&project.project_root, session_running);
1082    let (active_tasks, review_queue) =
1083        crate::team::status::board_status_task_queues(&project.project_root)?;
1084
1085    Ok(crate::team::status::build_team_status_json_report(
1086        crate::team::status::TeamStatusJsonReportInput {
1087            team: team_config.name,
1088            session: project.session_name.clone(),
1089            session_running,
1090            paused,
1091            watchdog,
1092            workflow_metrics,
1093            active_tasks,
1094            review_queue,
1095            engineer_profiles: None,
1096            optional_subsystems: None,
1097            members: rows,
1098        },
1099    ))
1100}
1101
1102fn validate_project(project: &RegisteredProject) -> Result<()> {
1103    validate_project_id(&project.project_id)?;
1104    trim_required("name", &project.name)?;
1105    trim_required("teamName", &project.team_name)?;
1106    trim_required("sessionName", &project.session_name)?;
1107    if !project.project_root.is_absolute() {
1108        bail!(
1109            "projectRoot '{}' must be absolute",
1110            project.project_root.display()
1111        );
1112    }
1113    if !project.board_dir.is_absolute() {
1114        bail!(
1115            "boardDir '{}' must be absolute",
1116            project.board_dir.display()
1117        );
1118    }
1119    if !project.board_dir.starts_with(&project.project_root) {
1120        bail!(
1121            "boardDir '{}' must be inside projectRoot '{}'",
1122            project.board_dir.display(),
1123            project.project_root.display()
1124        );
1125    }
1126    for alias in &project.aliases {
1127        validate_label("alias", alias)?;
1128    }
1129    for tag in &project.tags {
1130        validate_label("tag", tag)?;
1131    }
1132    Ok(())
1133}
1134
1135fn validate_project_id(project_id: &str) -> Result<()> {
1136    if project_id.is_empty() {
1137        bail!("projectId cannot be empty");
1138    }
1139    if !project_id
1140        .chars()
1141        .all(|ch| ch.is_ascii_lowercase() || ch.is_ascii_digit() || matches!(ch, '-' | '_' | '.'))
1142    {
1143        bail!(
1144            "projectId '{}' must use lowercase ASCII letters, digits, '.', '-', or '_'",
1145            project_id
1146        );
1147    }
1148    Ok(())
1149}
1150
1151fn validate_label(field_name: &str, value: &str) -> Result<()> {
1152    if value.is_empty() {
1153        bail!("{field_name} cannot be empty");
1154    }
1155    if !value
1156        .chars()
1157        .all(|ch| ch.is_ascii_lowercase() || ch.is_ascii_digit() || matches!(ch, '-' | '_' | '.'))
1158    {
1159        bail!(
1160            "{field_name} '{}' must use lowercase ASCII letters, digits, '.', '-', or '_'",
1161            value
1162        );
1163    }
1164    Ok(())
1165}
1166
1167fn trim_required(field_name: &str, value: &str) -> Result<String> {
1168    let trimmed = value.trim();
1169    if trimmed.is_empty() {
1170        bail!("{field_name} cannot be empty");
1171    }
1172    Ok(trimmed.to_string())
1173}
1174
1175fn normalize_labels(field_name: &str, values: Vec<String>) -> Result<Vec<String>> {
1176    let mut labels = values
1177        .into_iter()
1178        .map(|value| value.trim().to_ascii_lowercase())
1179        .filter(|value| !value.is_empty())
1180        .collect::<Vec<_>>();
1181    labels.sort();
1182    labels.dedup();
1183    for label in &labels {
1184        validate_label(field_name, label)?;
1185    }
1186    Ok(labels)
1187}
1188
1189fn normalize_path(path: &Path) -> Result<PathBuf> {
1190    let absolute = if path.is_absolute() {
1191        path.to_path_buf()
1192    } else {
1193        std::env::current_dir()
1194            .context("failed to determine current directory")?
1195            .join(path)
1196    };
1197
1198    if absolute.exists() {
1199        absolute
1200            .canonicalize()
1201            .with_context(|| format!("failed to canonicalize {}", absolute.display()))
1202    } else {
1203        Ok(absolute)
1204    }
1205}
1206
1207fn sort_selections(selections: &mut [ActiveProjectSelection]) {
1208    selections.sort_by(|left, right| {
1209        scope_rank(&left.scope)
1210            .cmp(&scope_rank(&right.scope))
1211            .then_with(|| left.project_id.cmp(&right.project_id))
1212    });
1213}
1214
1215fn scope_rank(scope: &ActiveProjectScope) -> (&'static str, &str, &str, &str) {
1216    match scope {
1217        ActiveProjectScope::Global => ("global", "", "", ""),
1218        ActiveProjectScope::Channel { channel, binding } => ("channel", channel, binding, ""),
1219        ActiveProjectScope::Thread {
1220            channel,
1221            binding,
1222            thread_binding,
1223        } => ("thread", channel, binding, thread_binding),
1224    }
1225}
1226
1227fn same_scope(left: &ActiveProjectScope, right: &ActiveProjectScope) -> bool {
1228    match (left, right) {
1229        (ActiveProjectScope::Global, ActiveProjectScope::Global) => true,
1230        (
1231            ActiveProjectScope::Channel {
1232                channel: left_channel,
1233                binding: left_binding,
1234            },
1235            ActiveProjectScope::Channel {
1236                channel: right_channel,
1237                binding: right_binding,
1238            },
1239        ) => left_channel == right_channel && left_binding == right_binding,
1240        (
1241            ActiveProjectScope::Thread {
1242                channel: left_channel,
1243                binding: left_binding,
1244                thread_binding: left_thread,
1245            },
1246            ActiveProjectScope::Thread {
1247                channel: right_channel,
1248                binding: right_binding,
1249                thread_binding: right_thread,
1250            },
1251        ) => {
1252            left_channel == right_channel
1253                && left_binding == right_binding
1254                && left_thread == right_thread
1255        }
1256        _ => false,
1257    }
1258}
1259
1260fn score_project(
1261    project: &RegisteredProject,
1262    routing_state: &ProjectRoutingState,
1263    request: &ProjectRoutingRequest,
1264) -> Option<ProjectRoutingCandidate> {
1265    let message = request.message.to_ascii_lowercase();
1266    let tokens = normalized_tokens(&message);
1267    let mut score = 0u32;
1268    let mut reasons = Vec::new();
1269
1270    if tokens.iter().any(|token| token == &project.project_id) {
1271        score = score.max(100);
1272        reasons.push("explicit projectId mention".to_string());
1273    }
1274
1275    if project
1276        .aliases
1277        .iter()
1278        .any(|alias| tokens.iter().any(|token| token == alias))
1279    {
1280        score = score.max(98);
1281        reasons.push("explicit alias mention".to_string());
1282    }
1283
1284    if phrase_match(&message, &project.name.to_ascii_lowercase()) {
1285        score = score.max(95);
1286        reasons.push("project name mention".to_string());
1287    }
1288
1289    let mentioned_tags = project
1290        .tags
1291        .iter()
1292        .filter(|tag| tokens.iter().any(|token| token == *tag))
1293        .cloned()
1294        .collect::<Vec<_>>();
1295    if !mentioned_tags.is_empty() {
1296        score = score.max(70);
1297        reasons.push(format!("tag match ({})", mentioned_tags.join(", ")));
1298    }
1299
1300    if let Some(reason) = thread_binding_match(project, request) {
1301        score = score.max(100);
1302        reasons.push(reason);
1303    } else if let Some(reason) = channel_binding_match(project, request) {
1304        score = score.max(90);
1305        reasons.push(reason);
1306    }
1307
1308    if let Some(reason) = active_selection_match(project, routing_state, request) {
1309        score = score.max(reason.0);
1310        reasons.push(reason.1);
1311    }
1312
1313    if score == 0 {
1314        None
1315    } else {
1316        Some(ProjectRoutingCandidate {
1317            project_id: project.project_id.clone(),
1318            reason: reasons.join("; "),
1319            score,
1320        })
1321    }
1322}
1323
1324fn thread_binding_match(
1325    project: &RegisteredProject,
1326    request: &ProjectRoutingRequest,
1327) -> Option<String> {
1328    let channel = request.channel.as_deref()?;
1329    let binding = request.binding.as_deref()?;
1330    let thread_binding = request.thread_binding.as_deref()?;
1331    project.channel_bindings.iter().find_map(|candidate| {
1332        (candidate.channel == channel
1333            && candidate.binding == binding
1334            && candidate.thread_binding.as_deref() == Some(thread_binding))
1335        .then(|| "thread binding match".to_string())
1336    })
1337}
1338
1339fn channel_binding_match(
1340    project: &RegisteredProject,
1341    request: &ProjectRoutingRequest,
1342) -> Option<String> {
1343    let channel = request.channel.as_deref()?;
1344    let binding = request.binding.as_deref()?;
1345    project.channel_bindings.iter().find_map(|candidate| {
1346        (candidate.channel == channel
1347            && candidate.binding == binding
1348            && candidate.thread_binding.is_none())
1349        .then(|| "channel binding match".to_string())
1350    })
1351}
1352
1353fn active_selection_match(
1354    project: &RegisteredProject,
1355    routing_state: &ProjectRoutingState,
1356    request: &ProjectRoutingRequest,
1357) -> Option<(u32, String)> {
1358    routing_state
1359        .selections
1360        .iter()
1361        .find(|selection| selection.project_id == project.project_id)
1362        .and_then(|selection| match &selection.scope {
1363            ActiveProjectScope::Thread {
1364                channel,
1365                binding,
1366                thread_binding,
1367            } => (request.channel.as_deref() == Some(channel.as_str())
1368                && request.binding.as_deref() == Some(binding.as_str())
1369                && request.thread_binding.as_deref() == Some(thread_binding.as_str()))
1370            .then(|| (96, "active project selected for this thread".to_string())),
1371            ActiveProjectScope::Channel { channel, binding } => (request.channel.as_deref()
1372                == Some(channel.as_str())
1373                && request.binding.as_deref() == Some(binding.as_str()))
1374            .then(|| (80, "active project selected for this channel".to_string())),
1375            ActiveProjectScope::Global => Some((65, "global active project selection".to_string())),
1376        })
1377}
1378
1379fn normalized_tokens(value: &str) -> Vec<String> {
1380    let mut current = String::new();
1381    let mut tokens = Vec::new();
1382    for ch in value.chars() {
1383        if ch.is_ascii_alphanumeric() || matches!(ch, '-' | '_' | '.') {
1384            current.push(ch);
1385        } else if !current.is_empty() {
1386            tokens.push(std::mem::take(&mut current));
1387        }
1388    }
1389    if !current.is_empty() {
1390        tokens.push(current);
1391    }
1392    tokens
1393}
1394
1395fn phrase_match(message: &str, phrase: &str) -> bool {
1396    if phrase.is_empty() {
1397        return false;
1398    }
1399    message.contains(phrase)
1400}
1401
1402fn looks_like_control_action(message: &str) -> bool {
1403    let tokens = normalized_tokens(&message.to_ascii_lowercase());
1404    tokens.iter().any(|token| {
1405        matches!(
1406            token.as_str(),
1407            "stop"
1408                | "restart"
1409                | "delete"
1410                | "archive"
1411                | "merge"
1412                | "ship"
1413                | "deploy"
1414                | "kill"
1415                | "pause"
1416                | "resume"
1417                | "assign"
1418                | "unregister"
1419                | "register"
1420                | "instruct"
1421        )
1422    })
1423}
1424
1425fn auto_route_allowed(candidate: &ProjectRoutingCandidate, control_action: bool) -> bool {
1426    if candidate.score >= 98 {
1427        return true;
1428    }
1429    if candidate.reason.contains("thread binding match") {
1430        return true;
1431    }
1432    if control_action {
1433        return candidate.reason.contains("explicit projectId mention")
1434            || candidate.reason.contains("explicit alias mention")
1435            || candidate.reason.contains("thread binding match");
1436    }
1437    candidate.score >= 80
1438}
1439
1440fn routing_confidence(score: u32) -> RoutingConfidence {
1441    if score >= 95 {
1442        RoutingConfidence::High
1443    } else if score >= 75 {
1444        RoutingConfidence::Medium
1445    } else {
1446        RoutingConfidence::Low
1447    }
1448}
1449
1450fn routing_reason(top: &ProjectRoutingCandidate, ambiguous: bool, control_action: bool) -> String {
1451    if ambiguous {
1452        return format!(
1453            "Routing is ambiguous across multiple projects. Top match was {} because {}.",
1454            top.project_id, top.reason
1455        );
1456    }
1457    if control_action && !auto_route_allowed(top, true) {
1458        return format!(
1459            "Matched {} because {}, but this looks like a control action and requires confirmation.",
1460            top.project_id, top.reason
1461        );
1462    }
1463    format!("Selected {} because {}.", top.project_id, top.reason)
1464}
1465
1466#[cfg(test)]
1467mod tests {
1468    use super::*;
1469
1470    fn sample_registration(project_root: &Path) -> ProjectRegistration {
1471        ProjectRegistration {
1472            project_id: "alpha".to_string(),
1473            name: "Alpha".to_string(),
1474            aliases: vec!["batty".to_string()],
1475            project_root: project_root.to_path_buf(),
1476            board_dir: project_root
1477                .join(".batty")
1478                .join("team_config")
1479                .join("board"),
1480            team_name: "alpha".to_string(),
1481            session_name: "batty-alpha".to_string(),
1482            channel_bindings: vec![
1483                ProjectChannelBinding {
1484                    channel: "telegram".to_string(),
1485                    binding: "chat:123".to_string(),
1486                    thread_binding: None,
1487                },
1488                ProjectChannelBinding {
1489                    channel: "slack".to_string(),
1490                    binding: "channel:C123".to_string(),
1491                    thread_binding: Some("thread:abc".to_string()),
1492                },
1493            ],
1494            owner: Some("ops".to_string()),
1495            tags: vec!["core".to_string(), "pilot".to_string()],
1496            policy_flags: ProjectPolicyFlags {
1497                allow_openclaw_supervision: true,
1498                allow_cross_project_routing: false,
1499                allow_shared_service_routing: true,
1500                archived: false,
1501            },
1502        }
1503    }
1504
1505    fn write_beta(registry_path: &Path, root: &Path) {
1506        let beta_root = root.join("beta");
1507        std::fs::create_dir_all(beta_root.join(".batty/team_config/board")).unwrap();
1508        register_project_at(
1509            registry_path,
1510            ProjectRegistration {
1511                project_id: "beta".to_string(),
1512                name: "Beta".to_string(),
1513                aliases: vec!["other".to_string()],
1514                project_root: beta_root.clone(),
1515                board_dir: beta_root.join(".batty/team_config/board"),
1516                team_name: "beta".to_string(),
1517                session_name: "batty-beta".to_string(),
1518                channel_bindings: vec![ProjectChannelBinding {
1519                    channel: "telegram".to_string(),
1520                    binding: "chat:999".to_string(),
1521                    thread_binding: None,
1522                }],
1523                owner: None,
1524                tags: vec!["backend".to_string()],
1525                policy_flags: ProjectPolicyFlags::default(),
1526            },
1527        )
1528        .unwrap();
1529    }
1530
1531    #[test]
1532    fn register_and_get_round_trip() {
1533        let tmp = tempfile::tempdir().unwrap();
1534        let project_root = tmp.path().join("alpha");
1535        std::fs::create_dir_all(project_root.join(".batty/team_config/board")).unwrap();
1536        let registry_path = tmp.path().join("project-registry.json");
1537
1538        let created =
1539            register_project_at(&registry_path, sample_registration(&project_root)).unwrap();
1540        assert_eq!(created.project_id, "alpha");
1541        assert_eq!(created.aliases, vec!["batty"]);
1542        assert_eq!(created.channel_bindings.len(), 2);
1543
1544        let fetched = get_project_at(&registry_path, "alpha").unwrap().unwrap();
1545        assert_eq!(fetched, created);
1546
1547        let listed = load_registry_at(&registry_path).unwrap();
1548        assert_eq!(listed.projects.len(), 1);
1549        assert_eq!(listed.schema_version, REGISTRY_SCHEMA_VERSION);
1550    }
1551
1552    #[test]
1553    fn load_migrates_schema_version_one() {
1554        let tmp = tempfile::tempdir().unwrap();
1555        let registry_path = tmp.path().join("project-registry.json");
1556        std::fs::write(
1557            &registry_path,
1558            r#"{
1559  "kind": "batty.projectRegistry",
1560  "schemaVersion": 1,
1561  "projects": [
1562    {
1563      "projectId": "alpha",
1564      "name": "Alpha",
1565      "projectRoot": "/tmp/alpha",
1566      "boardDir": "/tmp/alpha/.batty/team_config/board",
1567      "teamName": "alpha",
1568      "sessionName": "batty-alpha",
1569      "channelBindings": [{ "channel": "telegram", "binding": "chat:123" }],
1570      "owner": null,
1571      "tags": ["core"],
1572      "policyFlags": {
1573        "allowOpenclawSupervision": true,
1574        "allowCrossProjectRouting": false,
1575        "allowSharedServiceRouting": false,
1576        "archived": false
1577      },
1578      "createdAt": 1,
1579      "updatedAt": 1
1580    }
1581  ]
1582}
1583"#,
1584        )
1585        .unwrap();
1586
1587        let registry = load_registry_at(&registry_path).unwrap();
1588        assert_eq!(registry.schema_version, 2);
1589        assert!(registry.projects[0].aliases.is_empty());
1590        assert_eq!(
1591            registry.projects[0].channel_bindings[0].thread_binding,
1592            None
1593        );
1594    }
1595
1596    #[test]
1597    fn parse_thread_binding_requires_hash_separator() {
1598        let error = parse_thread_binding("slack=channel:C123").unwrap_err();
1599        assert!(
1600            error
1601                .to_string()
1602                .contains("expected <channel>=<binding>#<thread-binding>")
1603        );
1604    }
1605
1606    #[test]
1607    fn set_active_project_upserts_scope() {
1608        let tmp = tempfile::tempdir().unwrap();
1609        let registry_path = tmp.path().join("project-registry.json");
1610        let state_path = tmp.path().join("project-routing-state.json");
1611        let project_root = tmp.path().join("alpha");
1612        std::fs::create_dir_all(project_root.join(".batty/team_config/board")).unwrap();
1613        register_project_at(&registry_path, sample_registration(&project_root)).unwrap();
1614
1615        set_active_project_at(
1616            &registry_path,
1617            &state_path,
1618            "alpha",
1619            ActiveProjectScope::Global,
1620        )
1621        .unwrap();
1622        set_active_project_at(
1623            &registry_path,
1624            &state_path,
1625            "alpha",
1626            ActiveProjectScope::Channel {
1627                channel: "telegram".to_string(),
1628                binding: "chat:123".to_string(),
1629            },
1630        )
1631        .unwrap();
1632
1633        let state = load_routing_state_at(&state_path).unwrap();
1634        assert_eq!(state.selections.len(), 2);
1635    }
1636
1637    #[test]
1638    fn resolve_prefers_explicit_alias() {
1639        let tmp = tempfile::tempdir().unwrap();
1640        let registry_path = tmp.path().join("project-registry.json");
1641        let state_path = tmp.path().join("project-routing-state.json");
1642        let project_root = tmp.path().join("alpha");
1643        std::fs::create_dir_all(project_root.join(".batty/team_config/board")).unwrap();
1644        register_project_at(&registry_path, sample_registration(&project_root)).unwrap();
1645        write_beta(&registry_path, tmp.path());
1646
1647        let decision = resolve_project_for_message_at(
1648            &registry_path,
1649            &state_path,
1650            &ProjectRoutingRequest {
1651                message: "check batty".to_string(),
1652                channel: None,
1653                binding: None,
1654                thread_binding: None,
1655            },
1656        )
1657        .unwrap();
1658
1659        assert_eq!(decision.selected_project_id.as_deref(), Some("alpha"));
1660        assert!(!decision.requires_confirmation);
1661        assert_eq!(decision.confidence, RoutingConfidence::High);
1662    }
1663
1664    #[test]
1665    fn resolve_uses_thread_binding_as_high_confidence() {
1666        let tmp = tempfile::tempdir().unwrap();
1667        let registry_path = tmp.path().join("project-registry.json");
1668        let state_path = tmp.path().join("project-routing-state.json");
1669        let project_root = tmp.path().join("alpha");
1670        std::fs::create_dir_all(project_root.join(".batty/team_config/board")).unwrap();
1671        register_project_at(&registry_path, sample_registration(&project_root)).unwrap();
1672        write_beta(&registry_path, tmp.path());
1673
1674        let decision = resolve_project_for_message_at(
1675            &registry_path,
1676            &state_path,
1677            &ProjectRoutingRequest {
1678                message: "check status".to_string(),
1679                channel: Some("slack".to_string()),
1680                binding: Some("channel:C123".to_string()),
1681                thread_binding: Some("thread:abc".to_string()),
1682            },
1683        )
1684        .unwrap();
1685
1686        assert_eq!(decision.selected_project_id.as_deref(), Some("alpha"));
1687        assert!(!decision.requires_confirmation);
1688        assert!(decision.reason.contains("thread binding"));
1689    }
1690
1691    #[test]
1692    fn resolve_requires_confirmation_for_control_action_from_global_active_project() {
1693        let tmp = tempfile::tempdir().unwrap();
1694        let registry_path = tmp.path().join("project-registry.json");
1695        let state_path = tmp.path().join("project-routing-state.json");
1696        let project_root = tmp.path().join("alpha");
1697        std::fs::create_dir_all(project_root.join(".batty/team_config/board")).unwrap();
1698        register_project_at(&registry_path, sample_registration(&project_root)).unwrap();
1699        write_beta(&registry_path, tmp.path());
1700        set_active_project_at(
1701            &registry_path,
1702            &state_path,
1703            "alpha",
1704            ActiveProjectScope::Global,
1705        )
1706        .unwrap();
1707
1708        let decision = resolve_project_for_message_at(
1709            &registry_path,
1710            &state_path,
1711            &ProjectRoutingRequest {
1712                message: "restart it".to_string(),
1713                channel: None,
1714                binding: None,
1715                thread_binding: None,
1716            },
1717        )
1718        .unwrap();
1719
1720        assert_eq!(decision.selected_project_id.as_deref(), Some("alpha"));
1721        assert!(decision.requires_confirmation);
1722        assert_eq!(decision.confidence, RoutingConfidence::Low);
1723    }
1724
1725    #[test]
1726    fn resolve_requires_clarification_when_only_generic_tag_matches() {
1727        let tmp = tempfile::tempdir().unwrap();
1728        let registry_path = tmp.path().join("project-registry.json");
1729        let state_path = tmp.path().join("project-routing-state.json");
1730        let alpha_root = tmp.path().join("alpha");
1731        std::fs::create_dir_all(alpha_root.join(".batty/team_config/board")).unwrap();
1732        register_project_at(&registry_path, sample_registration(&alpha_root)).unwrap();
1733        let gamma_root = tmp.path().join("gamma");
1734        std::fs::create_dir_all(gamma_root.join(".batty/team_config/board")).unwrap();
1735        register_project_at(
1736            &registry_path,
1737            ProjectRegistration {
1738                project_id: "gamma".to_string(),
1739                name: "Gamma".to_string(),
1740                aliases: vec!["gamma".to_string()],
1741                project_root: gamma_root.clone(),
1742                board_dir: gamma_root.join(".batty/team_config/board"),
1743                team_name: "gamma".to_string(),
1744                session_name: "batty-gamma".to_string(),
1745                channel_bindings: Vec::new(),
1746                owner: None,
1747                tags: vec!["core".to_string()],
1748                policy_flags: ProjectPolicyFlags::default(),
1749            },
1750        )
1751        .unwrap();
1752
1753        let decision = resolve_project_for_message_at(
1754            &registry_path,
1755            &state_path,
1756            &ProjectRoutingRequest {
1757                message: "check the core project".to_string(),
1758                channel: None,
1759                binding: None,
1760                thread_binding: None,
1761            },
1762        )
1763        .unwrap();
1764
1765        assert!(decision.selected_project_id.is_none());
1766        assert!(decision.requires_confirmation);
1767        assert!(
1768            decision.reason.contains("ambiguous") || decision.reason.contains("high confidence")
1769        );
1770    }
1771
1772    #[test]
1773    fn resolve_lifecycle_state_maps_stopped_recovering_and_degraded() {
1774        let base = crate::team::status::TeamStatusJsonReport {
1775            team: "batty".to_string(),
1776            session: "batty-batty".to_string(),
1777            running: true,
1778            paused: false,
1779            watchdog: crate::team::status::WatchdogStatus {
1780                state: "running".to_string(),
1781                restart_count: 0,
1782                current_backoff_secs: None,
1783                last_exit_reason: None,
1784            },
1785            health: crate::team::status::TeamStatusHealth {
1786                session_running: true,
1787                paused: false,
1788                member_count: 3,
1789                active_member_count: 1,
1790                pending_inbox_count: 0,
1791                triage_backlog_count: 0,
1792                unhealthy_members: Vec::new(),
1793            },
1794            workflow_metrics: None,
1795            active_tasks: Vec::new(),
1796            review_queue: Vec::new(),
1797            engineer_profiles: None,
1798            members: Vec::new(),
1799            optional_subsystems: None,
1800        };
1801
1802        let mut stopped = base.clone();
1803        stopped.running = false;
1804        assert_eq!(
1805            resolve_lifecycle_state(&stopped),
1806            ProjectLifecycleState::Stopped
1807        );
1808
1809        let mut recovering = base.clone();
1810        recovering.watchdog.state = "restarting".to_string();
1811        assert_eq!(
1812            resolve_lifecycle_state(&recovering),
1813            ProjectLifecycleState::Recovering
1814        );
1815
1816        let mut degraded = base.clone();
1817        degraded.health.unhealthy_members.push("eng-1".to_string());
1818        assert_eq!(
1819            resolve_lifecycle_state(&degraded),
1820            ProjectLifecycleState::Degraded
1821        );
1822
1823        assert_eq!(
1824            resolve_lifecycle_state(&base),
1825            ProjectLifecycleState::Running
1826        );
1827    }
1828
1829    #[test]
1830    fn project_status_dto_serializes_stable_camel_case_shape() {
1831        let dto = ProjectStatusDto {
1832            project_id: "alpha".to_string(),
1833            name: "Alpha".to_string(),
1834            team_name: "alpha-team".to_string(),
1835            session_name: "batty-alpha".to_string(),
1836            project_root: PathBuf::from("/tmp/alpha"),
1837            lifecycle: ProjectLifecycleState::Recovering,
1838            running: true,
1839            health: ProjectHealthSummary {
1840                paused: false,
1841                watchdog_state: "restarting".to_string(),
1842                unhealthy_members: vec!["eng-1".to_string()],
1843                member_count: 4,
1844                active_member_count: 2,
1845                pending_inbox_count: 3,
1846                triage_backlog_count: 1,
1847            },
1848            pipeline: ProjectPipelineMetrics {
1849                active_task_count: 2,
1850                review_queue_count: 1,
1851                runnable_count: 5,
1852                blocked_count: 1,
1853                stale_in_progress_count: 0,
1854                stale_review_count: 1,
1855                auto_merge_rate: Some(0.75),
1856                rework_rate: Some(0.2),
1857                avg_review_latency_secs: Some(120.0),
1858            },
1859        };
1860
1861        let value = serde_json::to_value(&dto).unwrap();
1862        assert_eq!(value["projectId"], "alpha");
1863        assert_eq!(value["teamName"], "alpha-team");
1864        assert_eq!(value["sessionName"], "batty-alpha");
1865        assert_eq!(value["lifecycle"], "recovering");
1866        assert_eq!(value["health"]["watchdogState"], "restarting");
1867        assert_eq!(value["pipeline"]["activeTaskCount"], 2);
1868        assert_eq!(value["pipeline"]["avgReviewLatencySecs"], 120.0);
1869    }
1870}