workmux 0.1.253

An opinionated workflow tool that orchestrates git worktrees and tmux
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
use std::borrow::Cow;
use std::path::PathBuf;
use std::time::{SystemTime, UNIX_EPOCH};

use anyhow::{Result, anyhow};
use serde::Serialize;
use tabled::{
    Table, Tabled,
    settings::{Padding, Style, object::Columns},
};

use crate::git;
use crate::multiplexer::{AgentPane, AgentStatus, create_backend, detect_backend_strict};
use crate::state::StateStore;
use crate::util;
use crate::workflow;

#[derive(Serialize)]
struct StatusEntry {
    worktree: String,
    branch: String,
    project: Option<String>,
    project_path: Option<PathBuf>,
    status: String,
    elapsed_secs: Option<u64>,
    title: Option<String>,
    pane_id: String,
    workdir: PathBuf,
    agent_kind: Option<String>,
    session: Option<String>,
    window_name: Option<String>,
    updated_ts: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    git: Option<GitInfo>,
}

#[derive(Serialize)]
struct StatusContext {
    backend: String,
    instance: String,
}

#[derive(Serialize)]
struct StatusScope {
    repository: Option<PathBuf>,
    all: bool,
    targets: Vec<String>,
}

#[derive(Serialize)]
struct StatusTargetError {
    target: String,
    code: &'static str,
    message: String,
}

#[derive(Serialize)]
struct StatusOutput {
    context: StatusContext,
    scope: StatusScope,
    state_files_total: usize,
    state_files_invalid: usize,
    state_files_invalid_unattributed: usize,
    state_files_invalid_matching_context: usize,
    state_files_matching_context: usize,
    reconciled_agent_count: usize,
    agents: Vec<StatusEntry>,
    target_errors: Vec<StatusTargetError>,
}

#[derive(Serialize, Clone)]
struct GitInfo {
    has_staged: bool,
    has_unstaged: bool,
    has_unmerged_commits: bool,
}

struct StatusRow {
    worktree: String,
    status: String,
    elapsed: String,
    git: String,
    title: String,
}

impl Tabled for StatusRow {
    const LENGTH: usize = 5;

    fn fields(&self) -> Vec<Cow<'_, str>> {
        vec![
            Cow::Borrowed(&self.worktree),
            Cow::Borrowed(&self.status),
            Cow::Borrowed(&self.elapsed),
            Cow::Borrowed(&self.git),
            Cow::Borrowed(&self.title),
        ]
    }

    fn headers() -> Vec<Cow<'static, str>> {
        vec![
            Cow::Borrowed("WORKTREE"),
            Cow::Borrowed("STATUS"),
            Cow::Borrowed("ELAPSED"),
            Cow::Borrowed("GIT"),
            Cow::Borrowed("TITLE"),
        ]
    }
}

fn git_label(git: &Option<GitInfo>) -> String {
    let Some(g) = git else {
        return "-".to_string();
    };
    let mut parts = Vec::new();
    if g.has_staged {
        parts.push("staged");
    }
    if g.has_unstaged {
        parts.push("unstaged");
    }
    if g.has_unmerged_commits {
        parts.push("unmerged");
    }
    if parts.is_empty() {
        "clean".to_string()
    } else {
        parts.join(",")
    }
}

fn status_label(status: Option<AgentStatus>) -> String {
    match status {
        Some(AgentStatus::Working) => "working".to_string(),
        Some(AgentStatus::Waiting) => "waiting".to_string(),
        Some(AgentStatus::Done) => "done".to_string(),
        None => "-".to_string(),
    }
}

fn optional_name(name: &str) -> Option<String> {
    (!name.is_empty()).then(|| name.to_string())
}

fn normalized_branch(branch: String) -> String {
    if branch.is_empty() {
        "(detached)".to_string()
    } else {
        branch
    }
}

fn project_identity(worktree: &std::path::Path) -> (Option<String>, Option<PathBuf>) {
    let Ok(root) = git::get_main_worktree_root_in(Some(worktree)) else {
        return (None, None);
    };
    let project = root
        .file_name()
        .map(|name| name.to_string_lossy().into_owned());
    (project, Some(root))
}

fn status_entry(
    agent: &AgentPane,
    worktree: String,
    branch: String,
    now: u64,
    git: Option<GitInfo>,
) -> StatusEntry {
    let (project, project_path) = project_identity(&agent.path);
    StatusEntry {
        worktree,
        branch,
        project,
        project_path,
        status: status_label(agent.status),
        elapsed_secs: agent.status_ts.map(|ts| now.saturating_sub(ts)),
        title: agent.pane_title.clone(),
        pane_id: agent.pane_id.clone(),
        workdir: agent.path.clone(),
        agent_kind: agent.agent_kind.clone(),
        session: optional_name(&agent.session),
        window_name: optional_name(&agent.window_name),
        updated_ts: agent.updated_ts,
        git,
    }
}

/// Compute git info for a worktree path.
///
/// Runs git commands with the worktree's directory as the working dir,
/// so it works correctly for cross-project agents.
fn compute_git_info(wt_path: &std::path::Path, branch: &str) -> Result<GitInfo> {
    let has_staged = git::has_staged_changes(wt_path)?;
    let has_unstaged = git::has_unstaged_changes(wt_path)?;
    let main = git::get_default_branch_in(Some(wt_path))?;
    let base = git::get_merge_base_in(Some(wt_path), &main)?;
    let unmerged = git::get_unmerged_branches_in(Some(wt_path), &base)?;

    Ok(GitInfo {
        has_staged,
        has_unstaged,
        has_unmerged_commits: unmerged.contains(branch),
    })
}

pub fn run(worktrees: &[String], json: bool, all: bool, show_git: bool) -> Result<()> {
    let mux = create_backend(detect_backend_strict()?);
    let store = StateStore::open_read_only()?;
    let mut report = store.load_reconciled_agent_report(mux.as_ref())?;
    let agent_panes = std::mem::take(&mut report.agents);
    let invalid_state_failures =
        report.state_files_invalid_matching_context + report.state_files_invalid_unattributed;
    if invalid_state_failures > 0 {
        return Err(anyhow!(
            "{} invalid agent state file(s) for {} instance {} or with unattributable filenames; inspect the workmux agents state directory",
            invalid_state_failures,
            report.backend,
            report.instance
        ));
    }

    let now = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.as_secs())
        .unwrap_or(0);

    let reconciled_agent_count = agent_panes.len();
    let mut entries: Vec<StatusEntry> = Vec::new();
    let mut target_errors = Vec::new();
    let repository;

    if worktrees.is_empty() {
        if !all && git::get_repo_root_if_present()?.is_some() {
            let all_worktrees = git::list_worktrees()?;
            repository = Some(git::get_main_worktree_root()?);
            let has_scoped_agents = all_worktrees.iter().any(|(wt_path, _)| {
                !workflow::match_agents_to_worktree(&agent_panes, wt_path).is_empty()
            });
            let unmerged_branches = if show_git && has_scoped_agents {
                let main = git::get_default_branch()?;
                let base = git::get_merge_base(&main)?;
                git::get_unmerged_branches(&base)?
            } else {
                std::collections::HashSet::new()
            };

            for (wt_path, branch) in &all_worktrees {
                let matching = workflow::match_agents_to_worktree(&agent_panes, wt_path);
                if matching.is_empty() {
                    continue;
                }
                let worktree_name = wt_path
                    .file_name()
                    .and_then(|name| name.to_str())
                    .unwrap_or("unknown")
                    .to_string();
                let git_info = if show_git {
                    Some(GitInfo {
                        has_staged: git::has_staged_changes(wt_path)?,
                        has_unstaged: git::has_unstaged_changes(wt_path)?,
                        has_unmerged_commits: unmerged_branches.contains(branch),
                    })
                } else {
                    None
                };

                entries.extend(matching.into_iter().map(|agent| {
                    status_entry(
                        agent,
                        worktree_name.clone(),
                        branch.clone(),
                        now,
                        git_info.clone(),
                    )
                }));
            }
        } else {
            repository = None;
            for agent in &agent_panes {
                let worktree_path =
                    workflow::find_worktree_root(&agent.path).unwrap_or_else(|| agent.path.clone());
                let worktree_name = worktree_path
                    .file_name()
                    .and_then(|name| name.to_str())
                    .unwrap_or("unknown")
                    .to_string();
                let branch = match git::get_branch_for_worktree(&worktree_path) {
                    Ok(branch) => normalized_branch(branch),
                    Err(error) if show_git => return Err(error),
                    Err(_) => worktree_name.clone(),
                };
                let git_info = if show_git {
                    Some(compute_git_info(&worktree_path, &branch)?)
                } else {
                    None
                };
                entries.push(status_entry(agent, worktree_name, branch, now, git_info));
            }
        }
    } else {
        repository = if git::get_repo_root_if_present()?.is_some() {
            Some(git::get_main_worktree_root()?)
        } else {
            None
        };
        for name in worktrees {
            let (wt_path, matching) =
                match workflow::resolve_worktree_agents_from_snapshot(name, &agent_panes) {
                    Ok(resolved) => resolved,
                    Err(error) => {
                        target_errors.push(StatusTargetError {
                            target: name.clone(),
                            code: "target_resolution_failed",
                            message: error.to_string(),
                        });
                        continue;
                    }
                };
            if matching.is_empty() {
                continue;
            }
            let worktree_name = wt_path
                .file_name()
                .and_then(|name| name.to_str())
                .unwrap_or("unknown")
                .to_string();
            let branch = match git::get_branch_for_worktree(&wt_path) {
                Ok(branch) => branch,
                Err(error) if show_git => return Err(error),
                Err(_) => worktree_name.clone(),
            };
            let git_info = if show_git {
                Some(compute_git_info(&wt_path, &branch)?)
            } else {
                None
            };

            entries.extend(matching.iter().map(|agent| {
                status_entry(
                    agent,
                    worktree_name.clone(),
                    branch.clone(),
                    now,
                    git_info.clone(),
                )
            }));
        }
    }

    let target_failure_count = target_errors.len();
    if json {
        let output = StatusOutput {
            context: StatusContext {
                backend: report.backend,
                instance: report.instance,
            },
            scope: StatusScope {
                repository,
                all,
                targets: worktrees.to_vec(),
            },
            state_files_total: report.state_files_total,
            state_files_invalid: report.state_files_invalid,
            state_files_invalid_unattributed: report.state_files_invalid_unattributed,
            state_files_invalid_matching_context: report.state_files_invalid_matching_context,
            state_files_matching_context: report.state_files_matching_context,
            reconciled_agent_count,
            agents: entries,
            target_errors,
        };
        println!("{}", serde_json::to_string_pretty(&output)?);
    } else {
        for error in &target_errors {
            eprintln!("{}: {}", error.target, error.message);
        }

        if entries.is_empty() && target_errors.is_empty() {
            if report.state_files_total > 0 && report.state_files_matching_context == 0 {
                eprintln!(
                    "{} agent state file(s) exist, but none match {} instance {}",
                    report.state_files_total, report.backend, report.instance
                );
            } else if reconciled_agent_count > 0 {
                eprintln!(
                    "{reconciled_agent_count} tracked agent(s) exist outside the requested scope"
                );
            }
            println!("No active agents");
        } else if !entries.is_empty() {
            let rows: Vec<StatusRow> = entries
                .iter()
                .map(|e| {
                    let worktree = if e.branch != e.worktree {
                        format!("{} ({})", e.worktree, e.branch)
                    } else {
                        e.worktree.clone()
                    };
                    StatusRow {
                        worktree,
                        status: e.status.clone(),
                        elapsed: e
                            .elapsed_secs
                            .map(util::format_elapsed_secs)
                            .unwrap_or("-".to_string()),
                        git: git_label(&e.git),
                        title: e.title.clone().unwrap_or("-".to_string()),
                    }
                })
                .collect();

            let mut table = Table::new(rows);
            table
                .with(Style::blank())
                .modify(Columns::new(..), Padding::new(0, 1, 0, 0));
            if !show_git {
                table.with(tabled::settings::Remove::column(
                    tabled::settings::location::ByColumnName::new("GIT"),
                ));
            }
            println!("{table}");
        }
    }

    if target_failure_count > 0 {
        return Err(anyhow!(
            "failed to resolve {target_failure_count} status target(s)"
        ));
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn git_info_fails_for_non_repository_path() {
        let dir = tempfile::tempdir().unwrap();

        assert!(compute_git_info(dir.path(), "feature").is_err());
    }
}