workmux 0.1.211

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
use anyhow::{Context, Result, anyhow};
use regex::Regex;

use crate::git;
use crate::multiplexer::util::prefixed;
use crate::multiplexer::{MuxHandle, WindowTarget};
use crate::prompt::Prompt;
use tracing::info;

use super::context::WorkflowContext;
use super::setup;
use super::types::{CreateResult, SetupOptions};
use crate::config::MuxMode;

/// Open a tmux window for an existing worktree
pub fn open(
    name: &str,
    context: &WorkflowContext,
    options: SetupOptions,
    new_window: bool,
    mode_override: Option<MuxMode>,
    prompt_file_only: Option<&Prompt>,
) -> Result<CreateResult> {
    info!(
        name = name,
        run_hooks = options.run_hooks,
        run_file_ops = options.run_file_ops,
        new_window = new_window,
        mode_override = ?mode_override,
        "open:start"
    );

    // Validate mutual exclusion of panes/windows config (mode-independent)
    if context.config.panes.is_some() && context.config.windows.is_some() {
        anyhow::bail!("Cannot specify both 'panes' and 'windows' in configuration.");
    }
    if let Some(panes) = &context.config.panes {
        crate::config::validate_panes_config(panes)?;
    }

    // Pre-flight checks
    context.ensure_mux_running()?;

    // This command requires the worktree to already exist
    // Smart resolution: try handle first, then branch name
    let (worktree_path, branch_name) = git::find_worktree_in(name, Some(&context.execution_dir))
        .map_err(|_| {
            anyhow!(
                "Worktree '{}' not found. Use 'workmux list' to see available worktrees.",
                name
            )
        })?;

    // Derive base handle from the worktree path (in case user provided branch name)
    let base_handle = worktree_path
        .file_name()
        .ok_or_else(|| anyhow!("Invalid worktree path: no directory name"))?
        .to_string_lossy()
        .to_string();

    // Resolve mode using canonical base_handle (not the CLI-provided name which may be a branch).
    // Precedence: CLI override > stored git metadata > config default (from options.mode)
    let stored_mode = git::get_worktree_mode_opt_in(&base_handle, Some(&context.execution_dir));
    let mode = mode_override.or(stored_mode).unwrap_or(options.mode);
    if mode == MuxMode::Session && options.window_session_name.is_some() {
        anyhow::bail!("--parent-session requires window mode");
    }
    let cli_target_window_name = options.target_window_name.clone();
    let cli_target_session_name = options.target_session_name.clone();
    let cli_window_session_name = options.window_session_name.clone();
    let explicit_primary_target_name = match mode {
        MuxMode::Window => cli_target_window_name.as_deref(),
        MuxMode::Session => cli_target_session_name.as_deref(),
    };

    if mode == MuxMode::Session && context.mux.name() != "tmux" {
        anyhow::bail!(
            "Session mode (--mode session / --session) is only supported with tmux.\n\
             Current backend: {}. Use window mode instead.",
            context.mux.name()
        );
    }

    // Validate windows config requires session mode (after canonical mode resolution)
    if let Some(windows) = &context.config.windows {
        if mode != MuxMode::Session {
            anyhow::bail!(
                "'windows' configuration requires 'mode: session'. \
                 Add 'mode: session' to your config."
            );
        }
        crate::config::validate_windows_config(windows)?;
    }

    let prior_mode = stored_mode.unwrap_or(MuxMode::Window);
    if let Some(explicit_target_name) = explicit_primary_target_name {
        let explicit_target = MuxHandle::new(
            context.mux.as_ref(),
            mode,
            &context.prefix,
            explicit_target_name,
        );
        if explicit_target.exists()? {
            let stored_target_name = match mode {
                MuxMode::Window => {
                    git::get_worktree_target_window_in(&base_handle, Some(&context.execution_dir))
                }
                MuxMode::Session => {
                    git::get_worktree_target_session_in(&base_handle, Some(&context.execution_dir))
                }
            };
            if stored_target_name.as_deref() != Some(explicit_target_name) {
                anyhow::bail!(
                    "A {} {} named '{}' already exists. Use a different target name.",
                    context.mux.name(),
                    explicit_target.kind(),
                    explicit_target.full_name()
                );
            }
        }
    }

    if prior_mode != mode {
        match prior_mode {
            MuxMode::Window => {
                // Kill all matching window targets (base + any -N numeric duplicates only)
                let all_names = context.mux.get_all_window_names()?;
                let prior_window_name =
                    git::get_worktree_target_window_in(&base_handle, Some(&context.execution_dir))
                        .unwrap_or_else(|| base_handle.clone());
                let full_base = prefixed(&context.prefix, &prior_window_name);
                let full_base_dash = format!("{}-", full_base);
                for name in &all_names {
                    let is_exact = *name == full_base;
                    let is_numeric_suffix = name
                        .strip_prefix(&full_base_dash)
                        .is_some_and(|s| !s.is_empty() && s.chars().all(|c| c.is_ascii_digit()));

                    if is_exact || is_numeric_suffix {
                        info!(
                            handle = base_handle,
                            window = name,
                            "open:closing window before mode conversion"
                        );
                        MuxHandle::kill_full(context.mux.as_ref(), prior_mode, name)?;
                    }
                }
            }
            MuxMode::Session => {
                let prior_session_name =
                    git::get_worktree_target_session_in(&base_handle, Some(&context.execution_dir))
                        .unwrap_or_else(|| base_handle.clone());
                let full_name = prefixed(&context.prefix, &prior_session_name);
                if MuxHandle::exists_full(context.mux.as_ref(), prior_mode, &full_name)? {
                    info!(
                        handle = base_handle,
                        session = full_name,
                        "open:closing session before mode conversion"
                    );
                    MuxHandle::kill_full(context.mux.as_ref(), prior_mode, &full_name)?;
                }
            }
        }
    }

    let target_window_name = options
        .target_window_name
        .clone()
        .or_else(|| git::get_worktree_target_window_in(&base_handle, Some(&context.execution_dir)));
    let target_session_name = options.target_session_name.clone().or_else(|| {
        git::get_worktree_target_session_in(&base_handle, Some(&context.execution_dir))
    });
    let window_session_name = options.window_session_name.clone().or_else(|| {
        git::get_worktree_window_session_in(&base_handle, Some(&context.execution_dir))
    });

    // Update options with the resolved mode
    let options = SetupOptions {
        mode,
        target_window_name: if mode == MuxMode::Window {
            target_window_name.clone()
        } else {
            None
        },
        target_session_name: if mode == MuxMode::Session {
            target_session_name.clone()
        } else {
            None
        },
        window_session_name: if mode == MuxMode::Window {
            window_session_name.clone()
        } else {
            None
        },
        ..options
    };

    let target_name = match mode {
        MuxMode::Window => target_window_name.as_deref().unwrap_or(&base_handle),
        MuxMode::Session => target_session_name.as_deref().unwrap_or(&base_handle),
    };
    let target = MuxHandle::new(context.mux.as_ref(), mode, &context.prefix, target_name);
    let window_target = WindowTarget::new(target.full_name(), window_session_name.clone());
    let target_exists = if mode == MuxMode::Window {
        context.mux.window_target_exists(&window_target)?
    } else {
        target.exists()?
    };

    // If target exists and we're not forcing new, switch to it
    if target_exists && !new_window {
        // Backfill mode metadata for legacy worktrees on successful switch
        if stored_mode != Some(mode) {
            let mode_str = if mode == MuxMode::Session {
                "session"
            } else {
                "window"
            };
            let _ = git::set_worktree_meta_in(
                &base_handle,
                "mode",
                mode_str,
                Some(&context.execution_dir),
            );
            if let Some(target_window_name) = &options.target_window_name {
                let _ = git::set_worktree_meta_in(
                    &base_handle,
                    "target-window",
                    target_window_name,
                    Some(&context.execution_dir),
                );
            }
            if let Some(target_session_name) = &options.target_session_name {
                let _ = git::set_worktree_meta_in(
                    &base_handle,
                    "target-session",
                    target_session_name,
                    Some(&context.execution_dir),
                );
            }
            if let Some(window_session_name) = &options.window_session_name {
                let _ = git::set_worktree_meta_in(
                    &base_handle,
                    "window-session",
                    window_session_name,
                    Some(&context.execution_dir),
                );
            }
        }
        if options.focus_window {
            if mode == MuxMode::Window && window_target.parent_session().is_some() {
                context.mux.select_window_target(&window_target)?;
            } else {
                target.select()?;
            }
        }
        info!(
            handle = base_handle,
            branch = branch_name,
            path = %worktree_path.display(),
            kind = target.kind(),
            focus = options.focus_window,
            "open:switched to existing target"
        );
        return Ok(CreateResult {
            worktree_path,
            branch_name,
            post_create_hooks_run: 0,
            base_branch: None,
            did_switch: true,
            mux_target_full_name: target.full_name(),
            resolved_handle: base_handle,
            mode,
        });
    }

    // Session mode doesn't support --new (duplicate sessions would be orphaned on cleanup)
    if new_window && target.is_session() {
        return Err(anyhow!(
            "--new is not supported in session mode. Each worktree can only have one session."
        ));
    }

    // Persist mode metadata if it's missing or changing (backfill legacy worktrees).
    // Placed after early-exit checks to avoid side effects on failed commands.
    if stored_mode != Some(mode) {
        let mode_str = if mode == MuxMode::Session {
            "session"
        } else {
            "window"
        };
        git::set_worktree_meta_in(&base_handle, "mode", mode_str, Some(&context.execution_dir))
            .context("Failed to persist worktree mode")?;
        info!(
            handle = base_handle,
            mode = mode_str,
            "open:persisted worktree mode"
        );
    }
    if let Some(target_window_name) = &cli_target_window_name {
        git::set_worktree_meta_in(
            &base_handle,
            "target-window",
            target_window_name,
            Some(&context.execution_dir),
        )
        .context("Failed to persist target window")?;
    }
    if mode == MuxMode::Session {
        if let Some(target_session_name) = &cli_target_session_name {
            git::set_worktree_meta_in(
                &base_handle,
                "target-session",
                target_session_name,
                Some(&context.execution_dir),
            )
            .context("Failed to persist target session")?;
        }
    } else if let Some(window_session_name) = &cli_window_session_name {
        git::set_worktree_meta_in(
            &base_handle,
            "window-session",
            window_session_name,
            Some(&context.execution_dir),
        )
        .context("Failed to persist window session")?;
    }

    // Determine handle: use suffix if forcing new target and one exists
    let (handle, after_window) = if new_window && target_exists {
        let unique_handle = resolve_unique_handle(context, target_name)?;
        // Insert after the last window in the target name group (base or -N suffixes)
        let after = context
            .mux
            .find_last_window_with_base_handle(&context.prefix, target_name)
            .unwrap_or(None);
        (unique_handle, after)
    } else {
        (target_name.to_string(), None)
    };

    // Compute working directory from config location
    let working_dir = if !context.config_rel_dir.as_os_str().is_empty() {
        let subdir_in_worktree = worktree_path.join(&context.config_rel_dir);
        if subdir_in_worktree.exists() {
            Some(subdir_in_worktree)
        } else {
            None
        }
    } else {
        None
    };

    // Use config_source_dir for file operations (the directory where config was found)
    let config_root = Some(context.config_source_dir.clone());

    // In file-only mode, write prompt file to the worktree before pane setup
    // so editors/plugins can detect it on startup.
    if let Some(prompt) = prompt_file_only {
        setup::write_prompt_file(Some(&worktree_path), &branch_name, prompt)?;
    }

    let options_with_workdir = SetupOptions {
        working_dir,
        config_root,
        target_window_name: if mode == MuxMode::Window {
            Some(handle.clone())
        } else {
            None
        },
        ..options
    };

    // Setup the environment
    let result = setup::setup_environment(
        context.mux.as_ref(),
        &branch_name,
        &handle,
        &worktree_path,
        &context.config,
        &options_with_workdir,
        None,
        after_window,
    )?;
    info!(
        handle = handle,
        branch = branch_name,
        path = %result.worktree_path.display(),
        hooks_run = result.post_create_hooks_run,
        "open:completed"
    );
    Ok(result)
}

/// Find a unique handle by appending a suffix if necessary.
///
/// If `base_handle` is "my-feature" and windows exist for:
/// - wm-my-feature
/// - wm-my-feature-2
///
/// This returns "my-feature-3".
///
/// Note: Only called in window mode (session mode rejects --new).
fn resolve_unique_handle(context: &WorkflowContext, base_handle: &str) -> Result<String> {
    let all_names = context.mux.get_all_window_names()?;
    let prefix = &context.prefix;
    let full_base = prefixed(prefix, base_handle);

    // If base name doesn't exist, use it directly
    if !all_names.contains(&full_base) {
        return Ok(base_handle.to_string());
    }

    // Find the highest existing suffix
    // Pattern matches: {prefix}{handle}-{number}
    let escaped_base = regex::escape(&full_base);
    let pattern = format!(r"^{}-(\d+)$", escaped_base);
    let re = Regex::new(&pattern).expect("Invalid regex pattern");

    let mut max_suffix: u32 = 1; // Start at 1 so first duplicate is -2

    for name in &all_names {
        if let Some(caps) = re.captures(name)
            && let Some(num_match) = caps.get(1)
            && let Ok(num) = num_match.as_str().parse::<u32>()
        {
            max_suffix = max_suffix.max(num);
        }
    }

    let new_handle = format!("{}-{}", base_handle, max_suffix + 1);

    info!(
        base_handle = base_handle,
        new_handle = new_handle,
        "open:generated unique handle for duplicate"
    );

    Ok(new_handle)
}