agent-doc 0.28.2

Interactive document sessions with AI agents
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
//! # Module: git
//!
//! ## Spec
//! - `commit(file)`: stages and commits a session document with an auto-generated
//!   `agent-doc(<stem>): <timestamp>` message, skipping hooks (`--no-verify`).  Relative paths are
//!   resolved against the git superproject root first, then the toplevel.  When a snapshot exists,
//!   the snapshot content (with ` (HEAD)` markers added to the shallowest new headings) is staged
//!   via `git hash-object + update-index` so the working tree is never touched; user keystrokes
//!   typed after the snapshot was taken remain uncommitted (green gutter).  Falls back to
//!   `git add -f` when hash-object fails.  After a successful commit, strips `(HEAD)` from the
//!   snapshot, repositions the boundary marker in the snapshot, and fires an IPC reposition signal
//!   (`try_ipc_reposition_boundary`) to the IDE plugin so the working tree boundary is updated
//!   via the plugin's Document API.
//! - `show_head(file)`: returns the file content from `HEAD` as `Some(String)`, or `None` if not
//!   tracked.
//! - `last_commit_mtime(file)`: returns the author timestamp of the most recent commit touching the
//!   file, or `None` if none exists.
//! - `create_branch(file)`: creates and checks out `agent-doc/<stem>`, or switches to it if it
//!   already exists.
//! - `squash_session(file)`: soft-resets to before the first `agent-doc` commit touching the file
//!   and recommits as a single squashed commit.
//! - `add_head_marker` (private): compares the snapshot against `HEAD` to identify newly added
//!   headings; marks only the shallowest (root-level) new headings with ` (HEAD)` so the IDE shows
//!   a single modified line per response section as a visual boundary.  Falls back to bold-text
//!   pseudo-headers (`**...**` on its own line) when no markdown headings are found.
//!
//! ## Agentic Contracts
//! - `commit` never modifies the working tree file directly; all staging is done through the git
//!   index.  The only disk write is to the snapshot file.
//! - `commit` captures all git stdout to stderr so callers that reserve stdout for JSON (e.g.,
//!   `preflight`) are not polluted.
//! - All public functions resolve paths relative to the superproject root when running inside a
//!   submodule, so git commands always run in the correct repo.
//! - `show_head` and `last_commit_mtime` return `Ok(None)` (not `Err`) when the file has no git
//!   history.
//!
//! ## Evals
//! - strip_head_markers_from_headings: heading lines with ` (HEAD)` suffix → suffix removed; non-heading lines unchanged
//! - strip_head_markers_preserves_non_heading_lines: body text containing "(HEAD)" → preserved verbatim
//! - strip_head_markers_bold_text: bold-text pseudo-header `**Re: Something** (HEAD)` → suffix removed
//! - add_head_marker_strips_old_markers: old `(HEAD)` heading stripped; new heading acquires `(HEAD)`
//! - add_head_marker_bold_text_fallback: no markdown headings → bold-text pseudo-header gets `(HEAD)`; real heading present → bold text skipped
//! - reposition_boundary_to_end_basic: stale boundary before user prompt → boundary repositioned after prompt
//! - reposition_boundary_no_exchange: doc with no exchange component → content returned unchanged
//! - reposition_boundary_preserves_user_edits: user text between response and boundary → all user text preserved, boundary after it
//! - reposition_boundary_cleans_multiple_stale: document with 2 stale boundaries → all removed, exactly 1 fresh boundary at end after user text

use anyhow::Result;
use std::path::Path;
use std::process::Command;

/// Resolve a relative path against the git root (superproject root if in a submodule).
/// Returns (git_root, resolved_file_path) so callers can run git commands in the correct repo.
pub(crate) fn resolve_to_git_root(file: &Path) -> Result<(std::path::PathBuf, std::path::PathBuf)> {
    if file.is_absolute() {
        // Find git root from the file's directory
        let parent = file.parent().unwrap_or(Path::new("/"));
        let root = git_toplevel_at(parent)
            .unwrap_or_else(|| std::env::current_dir().unwrap_or_default());
        return Ok((root, file.to_path_buf()));
    }

    // Try superproject first (handles submodule CWD case)
    let output = Command::new("git")
        .args(["rev-parse", "--show-superproject-working-tree"])
        .output();
    if let Ok(ref o) = output {
        let root = String::from_utf8_lossy(&o.stdout).trim().to_string();
        if !root.is_empty() {
            let root_path = std::path::PathBuf::from(&root);
            let resolved = root_path.join(file);
            if resolved.exists() {
                return Ok((root_path, resolved));
            }
        }
    }

    // Try git toplevel
    let output = Command::new("git")
        .args(["rev-parse", "--show-toplevel"])
        .output();
    if let Ok(ref o) = output {
        let root = String::from_utf8_lossy(&o.stdout).trim().to_string();
        if !root.is_empty() {
            let root_path = std::path::PathBuf::from(&root);
            let resolved = root_path.join(file);
            if resolved.exists() {
                return Ok((root_path, resolved));
            }
        }
    }

    // Fallback: use as-is (relative to CWD)
    let cwd = std::env::current_dir().unwrap_or_default();
    Ok((cwd, file.to_path_buf()))
}

/// Get git toplevel from a specific directory.
fn git_toplevel_at(dir: &Path) -> Option<std::path::PathBuf> {
    Command::new("git")
        .current_dir(dir)
        .args(["rev-parse", "--show-toplevel"])
        .output()
        .ok()
        .and_then(|o| {
            let s = String::from_utf8_lossy(&o.stdout).trim().to_string();
            if s.is_empty() { None } else { Some(std::path::PathBuf::from(s)) }
        })
}

/// Commit a file with an auto-generated message. Skips hooks.
/// Relative paths are resolved against the git root (superproject if in a submodule).
/// Git commands run from the resolved git root, so this works even when CWD is a submodule.
pub fn commit(file: &Path) -> Result<()> {
    let t_total = std::time::Instant::now();

    let (git_root, resolved) = resolve_to_git_root(file)?;
    let timestamp = chrono_timestamp();
    let doc_name = file
        .file_stem()
        .and_then(|n| n.to_str())
        .unwrap_or("unknown");
    let msg = format!("agent-doc({}): {}", doc_name, timestamp);

    // Selective commit: stage only the snapshot content (agent response),
    // leaving user edits in the working tree as uncommitted.
    //
    // If a snapshot exists, use git hash-object + update-index to stage the
    // snapshot version without touching the working tree file. This means:
    // - Agent response → committed (no git gutter)
    // - User's subsequent edits → uncommitted (green git gutter)
    let snapshot_content = crate::snapshot::load(file)?;
    let t_staging = std::time::Instant::now();
    if let Some(ref snap) = snapshot_content {
        // Add (HEAD) marker to the last ### Re: heading in the snapshot.
        // The working tree keeps the heading WITHOUT the marker, creating
        // a single modified line (blue gutter) as a visual boundary.
        let staged_content = add_head_marker(snap, file);

        let rel_path = resolved.strip_prefix(&git_root)
            .unwrap_or(&resolved);

        if let Ok(hash) = hash_object(&git_root, &staged_content) {
            let cacheinfo = format!("100644,{},{}", hash, rel_path.to_string_lossy());
            let status = Command::new("git")
                .current_dir(&git_root)
                .args(["update-index", "--add", "--cacheinfo", &cacheinfo])
                .status()?;
            if !status.success() {
                eprintln!("[commit] update-index failed, falling back to git add");
                git_add_force(&git_root, &resolved)?;
            }
        } else {
            git_add_force(&git_root, &resolved)?;
        }
    } else {
        // No snapshot — fall back to staging the entire file
        git_add_force(&git_root, &resolved)?;
    }
    let elapsed_staging = t_staging.elapsed().as_millis();
    if elapsed_staging > 0 {
        eprintln!("[perf] commit.staging (hash_object+update-index): {}ms", elapsed_staging);
    }

    // Commit — ignore failure (nothing to commit is fine).
    // Use .output() to capture stdout (prevents git status leaking to stdout
    // when called from `preflight` which reserves stdout for JSON).
    let t_commit = std::time::Instant::now();
    let commit_output = Command::new("git")
        .current_dir(&git_root)
        .args(["commit", "-m", &msg, "--no-verify"])
        .output();
    let commit_status = commit_output.as_ref().map(|o| o.status);
    let elapsed_commit = t_commit.elapsed().as_millis();
    if elapsed_commit > 0 {
        eprintln!("[perf] commit.git_commit: {}ms", elapsed_commit);
    }

    // Log commit output to stderr (captured from git to avoid stdout pollution)
    if let Ok(ref o) = commit_output {
        let stdout = String::from_utf8_lossy(&o.stdout);
        for line in stdout.lines() {
            if !line.trim().is_empty() {
                eprintln!("{}", line);
            }
        }
    }

    // After commit, strip (HEAD) markers from the snapshot so the working tree
    // is clean. The committed content has (HEAD) markers; the working tree should not.
    // This creates the blue gutter diff the user sees.
    if let Ok(ref s) = commit_status
        && s.success()
    {
            // Strip (HEAD) from snapshot
            if let Some(ref snap) = snapshot_content {
                let clean_snap = strip_head_markers(snap);
                if clean_snap != *snap
                    && let Err(e) = crate::snapshot::save(file, &clean_snap)
                {
                    eprintln!("[commit] failed to clean snapshot: {}", e);
                }
            }
            // Note: working tree is NOT modified here. The staged content has (HEAD)
            // markers, the working tree does not. This creates the blue gutter diff.
            // Previously we stripped HEAD markers from the working tree, but that was
            // unnecessary (staging doesn't modify the working tree) and caused file
            // cache conflicts in the IDE.

            // Reposition boundary in snapshot AND via IPC to the plugin.
            // Working tree is NEVER written directly — that causes IDE "externally modified"
            // dialogs and loses user keystrokes. The IPC signal tells the plugin to
            // reposition in its Document buffer, which handles stale boundaries too.
            let t_reposition = std::time::Instant::now();
            let snap_changed = reposition_boundary_in_snapshot(file);
            // Send IPC reposition signal to plugin only if boundary actually moved.
            // Skipping no-op repositions eliminates ~64% of unnecessary Document API writes.
            if snap_changed {
                crate::write::try_ipc_reposition_boundary(file);
            }
            let elapsed_reposition = t_reposition.elapsed().as_millis();
            if elapsed_reposition > 0 {
                eprintln!("[perf] commit.reposition: {}ms", elapsed_reposition);
            }
    }

    let elapsed_total = t_total.elapsed().as_millis();
    if elapsed_total > 0 {
        eprintln!("[perf] commit total: {}ms", elapsed_total);
    }

    Ok(())
}

/// Reposition boundary in snapshot only (not working tree).
///
/// After commit, moves the boundary to the end of exchange in the snapshot.
/// The working tree is NOT modified — writing to it while the user is typing
/// causes the IDE to reload from disk, losing in-progress keystrokes.
/// The plugin handles working-tree boundary reposition via the
/// `reposition_boundary: true` IPC flag sent during `agent-doc write`.
/// Returns true if the boundary was actually repositioned (content changed).
fn reposition_boundary_in_snapshot(file: &Path) -> bool {
    // Check for active run — don't reposition if a run is in progress
    if let Ok(canonical) = file.canonicalize()
        && let Ok(pending_path) = crate::snapshot::pending_path_for(&canonical)
        && pending_path.exists()
    {
        eprintln!("[commit] skipping boundary reposition — active run detected");
        return false;
    }

    // Reposition in snapshot only — use template::reposition_boundary_to_end()
    // which removes ALL stale boundaries and inserts a single fresh one.
    if let Ok(Some(snap_content)) = crate::snapshot::load(file) {
        let new_snap = crate::template::reposition_boundary_to_end(&snap_content);
        if new_snap != snap_content {
            if let Err(e) = crate::snapshot::save(file, &new_snap) {
                eprintln!("[commit] failed to update snapshot after boundary reposition: {}", e);
                return false;
            }
            eprintln!("[commit] repositioned boundary in snapshot");
            return true;
        }
    }
    false
}

/// Strip ` (HEAD)` suffix from markdown heading lines and bold-text pseudo-headers.
fn strip_head_markers(content: &str) -> String {
    let mut lines: Vec<&str> = Vec::new();
    for line in content.lines() {
        lines.push(line);
    }
    let result: String = lines.iter().map(|line| {
        let trimmed = line.trim_start();
        if let Some(stripped) = line.strip_suffix(" (HEAD)") {
            // Strip from markdown headings
            if trimmed.starts_with('#') {
                return stripped;
            }
            // Strip from bold-text pseudo-headers (e.g., "**Re: Foo** (HEAD)")
            let without_suffix = stripped.trim_end();
            if trimmed.starts_with("**") && without_suffix.trim_start().ends_with("**") {
                return stripped;
            }
        }
        line
    }).collect::<Vec<&str>>().join("\n");
    if content.ends_with('\n') { format!("{}\n", result) } else { result }
}


/// Add ` (HEAD)` suffix to ALL new markdown headings in the agent's appended content.
///
/// Matches any heading level (`#` through `######`). Compares the snapshot
/// against git HEAD to find which headings are new (added by the agent).
/// Only the top-level (shallowest) headings in the new content get marked —
/// sub-headings within a response section are left unmarked.
///
/// When git HEAD is unavailable, falls back to marking the last heading only.
fn add_head_marker(content: &str, file: &Path) -> String {
    let head_content = show_head(file).ok().flatten();

    // Step 1: Strip ALL existing (HEAD) markers from heading lines and bold-text pseudo-headers.
    // This prevents accumulation across commit cycles.
    let mut cleaned_lines: Vec<String> = Vec::new();
    for line in content.lines() {
        let trimmed = line.trim_start();
        if trimmed.ends_with(" (HEAD)") {
            if trimmed.starts_with('#') {
                cleaned_lines.push(line[..line.len() - 7].to_string());
                continue;
            }
            // Bold-text pseudo-header: "**...** (HEAD)"
            let without_suffix = line[..line.len() - 7].trim_end();
            if trimmed.starts_with("**") && without_suffix.trim_start().ends_with("**") {
                cleaned_lines.push(line[..line.len() - 7].to_string());
                continue;
            }
        }
        cleaned_lines.push(line.to_string());
    }
    let cleaned = cleaned_lines.join("\n");
    // Preserve trailing newline
    let cleaned = if content.ends_with('\n') && !cleaned.ends_with('\n') {
        format!("{}\n", cleaned)
    } else {
        cleaned
    };

    // Also strip (HEAD) from git HEAD content for accurate comparison
    let head_cleaned = head_content.as_ref().map(|h| {
        h.lines()
            .map(|line| {
                let trimmed = line.trim_start();
                if trimmed.ends_with(" (HEAD)") {
                    if trimmed.starts_with('#') {
                        return &line[..line.len() - 7];
                    }
                    let without_suffix = line[..line.len() - 7].trim_end();
                    if trimmed.starts_with("**") && without_suffix.trim_start().ends_with("**") {
                        return &line[..line.len() - 7];
                    }
                }
                line
            })
            .collect::<Vec<&str>>()
            .join("\n")
    });

    // Step 2: Collect all heading positions from cleaned content
    let mut heading_positions: Vec<(usize, usize, usize)> = Vec::new();
    let mut offset = 0usize;
    for line in cleaned.lines() {
        let trimmed = line.trim_start();
        let line_end = offset + line.len();
        if trimmed.starts_with('#') {
            let level = trimmed.chars().take_while(|c| *c == '#').count();
            if level <= 6 && trimmed.len() > level && trimmed.as_bytes()[level] == b' ' {
                heading_positions.push((offset, line_end, level));
            }
        }
        offset = line_end + 1;
    }

    // Fallback: if no markdown headings found, scan for bold-text pseudo-headers
    // (lines matching `**...**` at start of line). Treat the first one found as a
    // pseudo-heading so it can receive the (HEAD) marker.
    if heading_positions.is_empty() {
        let mut offset = 0usize;
        for line in cleaned.lines() {
            let trimmed = line.trim_start();
            let line_end = offset + line.len();
            let trimmed_end = trimmed.trim_end();
            if trimmed_end.starts_with("**") && trimmed_end.ends_with("**") && trimmed_end.len() > 4 {
                // Use level 99 as a sentinel — bold pseudo-headers are always "shallowest"
                // since there are no real headings to compete with.
                heading_positions.push((offset, line_end, 99));
            }
            offset = line_end + 1;
        }
    }

    if heading_positions.is_empty() {
        return cleaned;
    }

    // Step 3: Filter to headings NOT in git HEAD (= new headings from latest response)
    let new_headings: Vec<(usize, usize, usize)> = if let Some(ref hc) = head_cleaned {
        heading_positions.into_iter().filter(|(start, end, _)| {
            let heading_text = &cleaned[*start..*end];
            !hc.contains(heading_text)
        }).collect()
    } else {
        // No HEAD available → mark last heading only
        vec![*heading_positions.last().unwrap()]
    };

    if new_headings.is_empty() {
        return cleaned;
    }

    // Step 4: Only mark root-level (shallowest) headings
    let min_level = new_headings.iter().map(|(_, _, level)| *level).min().unwrap();
    let root_ends: Vec<usize> = new_headings.iter()
        .filter(|(_, _, level)| *level == min_level)
        .map(|(_, end, _)| *end)
        .collect();

    // Step 5: Insert (HEAD) markers in reverse order to preserve offsets
    let mut result = cleaned;
    for pos in root_ends.iter().rev() {
        result.insert_str(*pos, " (HEAD)");
    }
    result
}

/// Write content to git's object database and return the blob hash.
fn hash_object(git_root: &Path, content: &str) -> Result<String> {
    let output = Command::new("git")
        .current_dir(git_root)
        .args(["hash-object", "-w", "--stdin"])
        .stdin(std::process::Stdio::piped())
        .stdout(std::process::Stdio::piped())
        .spawn()
        .and_then(|mut child| {
            use std::io::Write;
            if let Some(ref mut stdin) = child.stdin {
                stdin.write_all(content.as_bytes())?;
            }
            child.wait_with_output()
        })?;
    if !output.status.success() {
        anyhow::bail!("git hash-object failed");
    }
    Ok(String::from_utf8_lossy(&output.stdout).trim().to_string())
}

/// Force-add a file to the index (fallback path).
fn git_add_force(git_root: &Path, resolved: &Path) -> Result<()> {
    let status = Command::new("git")
        .current_dir(git_root)
        .args(["add", "-f", &resolved.to_string_lossy()])
        .status()?;
    if !status.success() {
        anyhow::bail!("git add failed");
    }
    Ok(())
}

/// Create and checkout a branch for the session.
pub fn create_branch(file: &Path) -> Result<()> {
    let stem = file
        .file_stem()
        .map(|s| s.to_string_lossy().to_string())
        .unwrap_or_else(|| "session".to_string());
    let branch_name = format!("agent-doc/{}", stem);

    let status = Command::new("git")
        .args(["checkout", "-b", &branch_name])
        .status()?;
    if !status.success() {
        // Branch may already exist — try switching to it
        let status = Command::new("git")
            .args(["checkout", &branch_name])
            .status()?;
        if !status.success() {
            anyhow::bail!("failed to create or switch to branch {}", branch_name);
        }
    }
    Ok(())
}

/// Squash all agent-doc commits touching a file into one.
pub fn squash_session(file: &Path) -> Result<()> {
    let file_str = file.to_string_lossy();

    // Find the first agent-doc commit for this file
    let output = Command::new("git")
        .args([
            "log",
            "--oneline",
            "--reverse",
            "--grep=^agent-doc",
            "--",
            &file_str,
        ])
        .output()?;
    let stdout = String::from_utf8_lossy(&output.stdout);
    let first_line = stdout.lines().next();
    let first_hash = match first_line {
        Some(line) => line.split_whitespace().next().unwrap_or(""),
        None => {
            eprintln!("No agent-doc commits found for {}", file.display());
            return Ok(());
        }
    };

    // Soft reset to the commit before the first agent-doc commit
    let status = Command::new("git")
        .args(["reset", "--soft", &format!("{}~1", first_hash)])
        .status()?;
    if !status.success() {
        anyhow::bail!("git reset failed");
    }

    // Recommit as a single squashed commit
    let status = Command::new("git")
        .args([
            "commit",
            "-m",
            &format!("agent-doc: squashed session for {}", file.display()),
            "--no-verify",
        ])
        .status()?;
    if !status.success() {
        anyhow::bail!("git commit failed during squash");
    }

    eprintln!("Squashed agent-doc commits for {}", file.display());
    Ok(())
}

/// Get the content of a file from the last agent-doc commit (or HEAD).
/// Returns None if the file is not tracked or no commits exist.
pub fn show_head(file: &Path) -> Result<Option<String>> {
    let (git_root, resolved) = resolve_to_git_root(file)?;

    // Get the file path relative to the git root
    let rel_path = if resolved.is_absolute() {
        resolved
            .strip_prefix(&git_root)
            .unwrap_or(&resolved)
            .to_path_buf()
    } else {
        resolved.clone()
    };

    let output = Command::new("git")
        .current_dir(&git_root)
        .args(["show", &format!("HEAD:{}", rel_path.to_string_lossy())])
        .output()?;

    if !output.status.success() {
        // File not tracked or no commits — not an error
        return Ok(None);
    }

    Ok(Some(String::from_utf8_lossy(&output.stdout).to_string()))
}

/// Get the author timestamp of the last commit touching a file.
/// Returns None if the file has no commits.
pub fn last_commit_mtime(file: &Path) -> Result<Option<std::time::SystemTime>> {
    let (git_root, resolved) = resolve_to_git_root(file)?;

    let rel_path = if resolved.is_absolute() {
        resolved
            .strip_prefix(&git_root)
            .unwrap_or(&resolved)
            .to_path_buf()
    } else {
        resolved.clone()
    };

    let output = Command::new("git")
        .current_dir(&git_root)
        .args([
            "log",
            "-1",
            "--format=%ct",
            "--",
            &rel_path.to_string_lossy(),
        ])
        .output()?;

    if !output.status.success() {
        return Ok(None);
    }

    let ts_str = String::from_utf8_lossy(&output.stdout).trim().to_string();
    if ts_str.is_empty() {
        return Ok(None);
    }

    let epoch: u64 = ts_str.parse().unwrap_or(0);
    if epoch == 0 {
        return Ok(None);
    }

    Ok(Some(std::time::UNIX_EPOCH + std::time::Duration::from_secs(epoch)))
}

fn chrono_timestamp() -> String {
    // Use date command for simplicity — no extra dependency
    let output = Command::new("date")
        .args(["+%Y-%m-%d %H:%M:%S"])
        .output()
        .ok();
    match output {
        Some(o) => String::from_utf8_lossy(&o.stdout).trim().to_string(),
        None => "unknown".to_string(),
    }
}

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

    #[test]
    fn strip_head_markers_from_headings() {
        let input = "# Title\n### Re: Foo (HEAD)\nSome text with (HEAD) in it\n### Re: Bar (HEAD)\n";
        let result = strip_head_markers(input);
        assert_eq!(result, "# Title\n### Re: Foo\nSome text with (HEAD) in it\n### Re: Bar\n");
    }

    #[test]
    fn strip_head_markers_preserves_non_heading_lines() {
        let input = "Normal line (HEAD)\n### Heading (HEAD)\n";
        let result = strip_head_markers(input);
        assert_eq!(result, "Normal line (HEAD)\n### Heading\n");
    }

    #[test]
    fn add_head_marker_strips_old_markers() {
        let content = "### Re: Old (HEAD)\n### Re: New\n";
        let result = add_head_marker(content, Path::new("/nonexistent/file.md"));
        assert!(!result.contains("### Re: Old (HEAD)"), "old heading should not have (HEAD)");
        assert!(result.contains("### Re: New (HEAD)") || result.contains("### Re: Old\n"), "old (HEAD) should be stripped");
    }

    #[test]
    fn add_head_marker_bold_text_fallback() {
        // No markdown headings — bold-text pseudo-header should get (HEAD)
        let content = "Some intro text.\n**Re: Something**\nBody paragraph.\n";
        let result = add_head_marker(content, Path::new("/nonexistent/file.md"));
        assert!(
            result.contains("**Re: Something** (HEAD)"),
            "bold-text pseudo-header should get (HEAD) marker, got: {result}"
        );
    }

    #[test]
    fn add_head_marker_prefers_real_headings() {
        // Both a real heading and bold text — only the heading should get (HEAD)
        let content = "### Re: Something\n**Bold text**\nBody.\n";
        let result = add_head_marker(content, Path::new("/nonexistent/file.md"));
        assert!(
            result.contains("### Re: Something (HEAD)"),
            "real heading should get (HEAD), got: {result}"
        );
        assert!(
            !result.contains("**Bold text** (HEAD)"),
            "bold text should NOT get (HEAD) when real headings exist, got: {result}"
        );
    }

    #[test]
    fn strip_head_markers_bold_text() {
        let input = "**Re: Something** (HEAD)\nSome text.\n";
        let result = strip_head_markers(input);
        assert_eq!(result, "**Re: Something**\nSome text.\n");
    }

    #[test]
    fn reposition_boundary_to_end_basic() {
        let content = "<!-- agent:exchange patch=append -->\nResponse.\n<!-- agent:boundary:abc123 -->\nUser prompt.\n<!-- /agent:exchange -->\n";
        let result = agent_doc::template::reposition_boundary_to_end(content);
        // Boundary should be after user prompt, before close tag
        assert!(result.contains("User prompt.\n<!-- agent:boundary:"));
        assert!(result.contains("-->\n<!-- /agent:exchange -->"));
        // Old boundary consumed
        assert!(!result.contains("abc123"));
    }

    #[test]
    fn reposition_boundary_no_exchange() {
        let content = "# No exchange component\nJust text.\n";
        let result = agent_doc::template::reposition_boundary_to_end(content);
        // Should return unchanged if no exchange
        assert_eq!(result.trim(), content.trim());
    }

    #[test]
    fn reposition_boundary_preserves_user_edits() {
        let content = "<!-- agent:exchange patch=append -->\n### Re: Answer\nAgent response.\n<!-- agent:boundary:old-id -->\nUser's new prompt here.\nMore user text.\n<!-- /agent:exchange -->\n";
        let result = agent_doc::template::reposition_boundary_to_end(content);
        assert!(result.contains("User's new prompt here."), "user edit must be preserved");
        assert!(result.contains("More user text."), "user edit must be preserved");
        let boundary_pos = result.find("<!-- agent:boundary:").unwrap();
        let user_pos = result.find("User's new prompt here.").unwrap();
        assert!(boundary_pos > user_pos, "boundary must be after user text");
    }

    #[test]
    fn reposition_boundary_cleans_multiple_stale() {
        // Simulate a document with multiple stale boundary markers
        let content = "<!-- agent:exchange patch=append -->\n\
            First response.\n\
            <!-- agent:boundary:aaa111 -->\n\
            Second response.\n\
            <!-- agent:boundary:bbb222 -->\n\
            User prompt.\n\
            <!-- /agent:exchange -->\n";
        let result = agent_doc::template::reposition_boundary_to_end(content);
        // All old boundaries should be removed
        assert!(!result.contains("aaa111"), "first stale boundary must be removed");
        assert!(!result.contains("bbb222"), "second stale boundary must be removed");
        // Exactly one fresh boundary should exist
        let boundary_count = result.matches("<!-- agent:boundary:").count();
        assert_eq!(boundary_count, 1, "exactly one boundary marker should remain");
        // The single boundary should be after user prompt
        let boundary_pos = result.find("<!-- agent:boundary:").unwrap();
        let user_pos = result.find("User prompt.").unwrap();
        assert!(boundary_pos > user_pos, "boundary must be after user text");
    }
}