merry-coding 0.1.2

Coding prompts, project rules, tool catalog, permission policy, and subagent orchestration for Merry.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
//! Low-level bounded Git command execution, working-tree snapshotting, and PID
//! liveness probing owned by [`super::WorktreeIsolationManager`].

use super::{
    FINALIZE_GIT_TIMEOUT, MAX_UNTRACKED_FILE_BYTES, MAX_UNTRACKED_TOTAL_BYTES, SETUP_GIT_TIMEOUT,
    WorktreeError,
};
use std::{
    fs,
    io::{Read, Write},
    path::{Component, Path, PathBuf},
    process::{Command, Stdio},
    time::{Duration, Instant},
};

pub(super) fn git_toplevel(path: &Path) -> Result<PathBuf, WorktreeError> {
    let out = run_git_stdout(path, &["rev-parse", "--show-toplevel"], SETUP_GIT_TIMEOUT).map_err(
        |error| WorktreeError::NotAGitRepository {
            workspace: path.to_path_buf(),
            detail: error.to_string(),
        },
    )?;
    let raw = PathBuf::from(out.trim());
    Ok(fs::canonicalize(&raw).unwrap_or(raw))
}

pub(super) fn git_common_dir(path: &Path) -> Result<PathBuf, WorktreeError> {
    let out = run_git_stdout(path, &["rev-parse", "--git-common-dir"], SETUP_GIT_TIMEOUT)?;
    let raw = PathBuf::from(out.trim());
    let resolved = if raw.is_absolute() {
        raw
    } else {
        path.join(raw)
    };
    Ok(fs::canonicalize(&resolved).unwrap_or(resolved))
}

pub(super) fn git_dir(path: &Path) -> Result<PathBuf, WorktreeError> {
    let out = run_git_stdout(path, &["rev-parse", "--git-dir"], SETUP_GIT_TIMEOUT)?;
    let raw = PathBuf::from(out.trim());
    let resolved = if raw.is_absolute() {
        raw
    } else {
        path.join(raw)
    };
    Ok(fs::canonicalize(&resolved).unwrap_or(resolved))
}

pub(super) fn verify_head_exists(repo_toplevel: &Path) -> Result<(), WorktreeError> {
    run_git_stdout(
        repo_toplevel,
        &["rev-parse", "--verify", "HEAD"],
        SETUP_GIT_TIMEOUT,
    )
    .map_err(|error| WorktreeError::NoHeadCommit {
        repo_root: repo_toplevel.to_path_buf(),
        detail: error.to_string(),
    })?;
    Ok(())
}

pub(super) fn validate_worktree_git_dir(
    worktree_git_dir: &Path,
    git_common_dir: &Path,
) -> Result<(), WorktreeError> {
    let expected_parent = git_common_dir.join("worktrees");
    if !worktree_git_dir.starts_with(&expected_parent) || worktree_git_dir == expected_parent {
        return Err(WorktreeError::InvalidWorktreeAdminDir {
            path: worktree_git_dir.to_path_buf(),
            common_dir: git_common_dir.to_path_buf(),
        });
    }
    Ok(())
}

pub(super) fn snapshot_working_tree(
    repo_toplevel: &Path,
    worktree_path: &Path,
) -> Result<(), WorktreeError> {
    // 1. Export tracked working-tree modifications without mutating the main tree.
    let diff = run_git_bytes(
        repo_toplevel,
        &["diff", "HEAD", "--binary"],
        SETUP_GIT_TIMEOUT,
    )?;
    if !diff.is_empty() {
        run_git_with_stdin(
            worktree_path,
            &["apply", "--binary", "--whitespace=nowarn"],
            &diff,
            SETUP_GIT_TIMEOUT,
        )?;
    }

    // 2. Copy non-ignored untracked regular files within bounded size limits.
    let untracked = run_git_bytes(
        repo_toplevel,
        &["ls-files", "--others", "--exclude-standard", "-z"],
        SETUP_GIT_TIMEOUT,
    )?;
    let mut copied_total: u64 = 0;
    for rel in parse_nul_paths(&untracked) {
        let rel_path = Path::new(&rel);
        if rel_path.is_absolute()
            || rel_path.components().any(|c| {
                matches!(
                    c,
                    Component::ParentDir | Component::RootDir | Component::Prefix(_)
                )
            })
        {
            continue;
        }
        let src = repo_toplevel.join(rel_path);
        let Ok(meta) = fs::symlink_metadata(&src) else {
            continue;
        };
        if !meta.file_type().is_file() {
            continue;
        }
        let len = meta.len();
        if len > MAX_UNTRACKED_FILE_BYTES
            || copied_total.saturating_add(len) > MAX_UNTRACKED_TOTAL_BYTES
        {
            tracing::warn!(
                path = %rel,
                bytes = len,
                "skipping oversized untracked file during worktree working_tree snapshot"
            );
            continue;
        }
        let dst = worktree_path.join(rel_path);
        if let Some(parent) = dst.parent() {
            fs::create_dir_all(parent).map_err(|source| WorktreeError::Io {
                context: format!(
                    "failed to create parent directory `{}` in worktree snapshot",
                    parent.display()
                ),
                source,
            })?;
        }
        fs::copy(&src, &dst).map_err(|source| WorktreeError::Io {
            context: format!(
                "failed to copy untracked file `{}` into worktree snapshot",
                src.display()
            ),
            source,
        })?;
        copied_total = copied_total.saturating_add(len);
    }

    // 3. Create an ephemeral unsigned baseline commit in the detached worktree so
    //    `git diff <baseline_oid>` later isolates only this child's net edits.
    let status = run_git_bytes(
        worktree_path,
        &["status", "--porcelain=v1", "-z"],
        SETUP_GIT_TIMEOUT,
    )?;
    if !status.is_empty() {
        run_git_stdout(worktree_path, &["add", "-A"], SETUP_GIT_TIMEOUT)?;
        run_git_stdout(
            worktree_path,
            &[
                "-c",
                "user.name=Merry Worktree",
                "-c",
                "user.email=merry@local",
                "-c",
                "commit.gpgsign=false",
                "commit",
                "--no-verify",
                "-m",
                "merry:worktree-baseline",
            ],
            SETUP_GIT_TIMEOUT,
        )?;
    }
    Ok(())
}

pub(super) fn collect_diff_stat(
    worktree_path: &Path,
    baseline_oid: &str,
    untracked_paths: &[String],
) -> Option<String> {
    let mut sections = Vec::new();
    if let Ok(stat) = run_git_stdout(
        worktree_path,
        &["diff", "--stat", baseline_oid],
        FINALIZE_GIT_TIMEOUT,
    ) {
        let trimmed = stat.trim();
        if !trimmed.is_empty() {
            sections.push(trimmed.to_owned());
        }
    }
    if !untracked_paths.is_empty() {
        sections.push(format!("Untracked files: {}", untracked_paths.join(", ")));
    }
    (!sections.is_empty()).then(|| sections.join("\n"))
}

pub(super) fn remove_clean_worktree(
    repo_toplevel: &Path,
    worktree_path: &Path,
    repo_marker_path: &Path,
) -> bool {
    let arg = worktree_path.to_string_lossy().into_owned();
    let ok = run_git_stdout(
        repo_toplevel,
        &["worktree", "remove", "--force", &arg],
        FINALIZE_GIT_TIMEOUT,
    )
    .is_ok();
    if ok && !worktree_path.exists() {
        let _ = fs::remove_file(repo_marker_path);
        if let Some(session_dir) = worktree_path.parent() {
            let _ = fs::remove_dir(session_dir);
        }
        true
    } else {
        false
    }
}

pub(super) fn best_effort_remove_worktree(
    repo_toplevel: &Path,
    worktree_path: &Path,
    repo_marker_path: &Path,
) {
    let arg = worktree_path.to_string_lossy().into_owned();
    let _ = run_git_stdout(
        repo_toplevel,
        &["worktree", "remove", "--force", &arg],
        FINALIZE_GIT_TIMEOUT,
    );
    let _ = fs::remove_dir_all(worktree_path);
    let _ = fs::remove_file(repo_marker_path);
}

pub(super) fn map_relative_path(
    repo_relative: &str,
    workspace_subpath: Option<&Path>,
) -> Option<String> {
    let normalized = repo_relative.trim();
    if normalized.is_empty() {
        return None;
    }
    match workspace_subpath {
        Some(sub) => Path::new(normalized)
            .strip_prefix(sub)
            .ok()
            .map(|p| p.to_string_lossy().into_owned())
            .or_else(|| Some(normalized.to_owned())),
        None => Some(normalized.to_owned()),
    }
}

pub(super) fn parse_nul_paths(bytes: &[u8]) -> Vec<String> {
    bytes
        .split(|byte| *byte == 0)
        .filter(|slice| !slice.is_empty())
        .map(|slice| String::from_utf8_lossy(slice).into_owned())
        .collect()
}

pub(super) fn parse_porcelain_z(bytes: &[u8]) -> Vec<(String, String)> {
    let mut entries = Vec::new();
    let mut parts = bytes.split(|byte| *byte == 0);
    while let Some(raw) = parts.next() {
        if raw.len() < 4 {
            continue;
        }
        let xy = String::from_utf8_lossy(&raw[..2]).into_owned();
        let path = String::from_utf8_lossy(&raw[3..]).into_owned();
        // Rename (`R`) and copy (`C`) records emit an extra NUL-terminated source path.
        if xy.starts_with('R') || xy.starts_with('C') || xy.ends_with('R') || xy.ends_with('C') {
            let _ = parts.next();
        }
        if !path.is_empty() {
            entries.push((xy, path));
        }
    }
    entries
}

pub(super) fn run_git_stdout(
    cwd: &Path,
    args: &[&str],
    timeout: Duration,
) -> Result<String, WorktreeError> {
    let bytes = run_git_bytes(cwd, args, timeout)?;
    Ok(String::from_utf8_lossy(&bytes).into_owned())
}

pub(super) fn run_git_bytes(
    cwd: &Path,
    args: &[&str],
    timeout: Duration,
) -> Result<Vec<u8>, WorktreeError> {
    run_git_raw(cwd, args, None, timeout)
}

fn run_git_with_stdin(
    cwd: &Path,
    args: &[&str],
    stdin_bytes: &[u8],
    timeout: Duration,
) -> Result<Vec<u8>, WorktreeError> {
    run_git_raw(cwd, args, Some(stdin_bytes), timeout)
}

fn run_git_raw(
    cwd: &Path,
    args: &[&str],
    stdin_bytes: Option<&[u8]>,
    timeout: Duration,
) -> Result<Vec<u8>, WorktreeError> {
    let command_label = format!("git {}", args.join(" "));
    let mut cmd = Command::new("git");
    cmd.arg("-C")
        .arg(cwd)
        .args(args)
        .stdin(if stdin_bytes.is_some() {
            Stdio::piped()
        } else {
            Stdio::null()
        })
        .stdout(Stdio::piped())
        .stderr(Stdio::piped());
    let mut child = cmd
        .spawn()
        .map_err(|source| WorktreeError::GitCommandFailed {
            command: command_label.clone(),
            detail: source.to_string(),
        })?;

    let mut child_stdin = child.stdin.take();
    let mut child_stdout = child.stdout.take();
    let mut child_stderr = child.stderr.take();

    let (status, stdout, stderr) = std::thread::scope(|scope| {
        let stdin_handle = stdin_bytes.map(|bytes| {
            scope.spawn(move || {
                if let Some(mut stdin) = child_stdin.take() {
                    let _ = stdin.write_all(bytes);
                }
            })
        });
        let stdout_handle = scope.spawn(move || {
            let mut buf = Vec::new();
            if let Some(mut out) = child_stdout.take() {
                let _ = out.read_to_end(&mut buf);
            }
            buf
        });
        let stderr_handle = scope.spawn(move || {
            let mut buf = Vec::new();
            if let Some(mut err) = child_stderr.take() {
                let _ = err.read_to_end(&mut buf);
            }
            buf
        });

        let deadline = Instant::now() + timeout;
        let status = loop {
            match child.try_wait() {
                Ok(Some(status)) => break Ok(status),
                Ok(None) => {
                    if Instant::now() >= deadline {
                        let _ = child.kill();
                        let _ = child.wait();
                        break Err(format!("timed out after {}s", timeout.as_secs()));
                    }
                    std::thread::sleep(Duration::from_millis(10));
                }
                Err(error) => break Err(error.to_string()),
            }
        };
        if let Some(handle) = stdin_handle {
            let _ = handle.join();
        }
        let stdout = stdout_handle.join().unwrap_or_default();
        let stderr = stderr_handle.join().unwrap_or_default();
        (status, stdout, stderr)
    });

    match status {
        Ok(exit) if exit.success() => Ok(stdout),
        Ok(exit) => {
            let detail = String::from_utf8_lossy(&stderr).trim().to_owned();
            Err(WorktreeError::GitCommandFailed {
                command: command_label,
                detail: if detail.is_empty() {
                    format!("exited with status {exit}")
                } else {
                    detail
                },
            })
        }
        Err(detail) => Err(WorktreeError::GitCommandFailed {
            command: command_label,
            detail,
        }),
    }
}

pub(super) fn is_pid_alive(pid: u32) -> bool {
    if pid == 0 {
        return false;
    }
    if pid == std::process::id() {
        return true;
    }
    #[cfg(target_os = "linux")]
    {
        let stat_path = format!("/proc/{pid}/stat");
        let Ok(stat) = fs::read_to_string(&stat_path) else {
            return false;
        };
        if let Some(after_comm) = stat.rsplit_once(')') {
            let state = after_comm.1.trim_start().chars().next();
            return state != Some('Z') && state != Some('X');
        }
        true
    }
    #[cfg(all(unix, not(target_os = "linux")))]
    {
        match Command::new("kill")
            .arg("-0")
            .arg(pid.to_string())
            .stdout(Stdio::null())
            .stderr(Stdio::piped())
            .output()
        {
            Ok(output) => {
                if output.status.success() {
                    return true;
                }
                let stderr = String::from_utf8_lossy(&output.stderr);
                stderr.contains("Operation not permitted")
            }
            Err(_) => true,
        }
    }
    #[cfg(windows)]
    {
        is_windows_pid_alive(pid)
    }
    #[cfg(not(any(unix, windows)))]
    {
        let _ = pid;
        true
    }
}

/// Checks whether a Windows process ID is still running via
/// `OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION)` and `GetExitCodeProcess`.
///
/// Reviewed FFI boundary: Win32 exposes no safe std API for checking arbitrary
/// PID liveness.
#[cfg(windows)]
#[allow(unsafe_code)]
fn is_windows_pid_alive(pid: u32) -> bool {
    use windows_sys::Win32::{
        Foundation::{CloseHandle, ERROR_ACCESS_DENIED, GetLastError, STILL_ACTIVE},
        System::Threading::{GetExitCodeProcess, OpenProcess, PROCESS_QUERY_LIMITED_INFORMATION},
    };

    // SAFETY: `OpenProcess` takes only plain integer flags/PID and returns a
    // nullable OS handle that we close before returning.
    let handle = unsafe { OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, 0, pid) };
    if handle.is_null() {
        // SAFETY: `GetLastError` reads thread-local Win32 error state.
        return unsafe { GetLastError() } == ERROR_ACCESS_DENIED;
    }

    let mut exit_code: u32 = 0;
    // SAFETY: `handle` is a valid non-null process handle owned by this
    // function, and `exit_code` is a valid writable `u32` pointer.
    let ok = unsafe { GetExitCodeProcess(handle, &mut exit_code) };
    // SAFETY: `handle` was returned by `OpenProcess` above and is closed once.
    unsafe {
        CloseHandle(handle);
    }
    ok != 0 && exit_code == STILL_ACTIVE as u32
}