agent-doc 0.32.3

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
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
//! # Module: watch
//!
//! ## Spec
//! - `start(config, WatchConfig)` runs the watch daemon. Acquires a PID file
//!   (`.agent-doc/watch.pid`) to prevent duplicate daemons; bails if one is already alive.
//! - `is_running()` checks the PID file and `/proc/<pid>` for daemon liveness.
//! - `ensure_running()` lazily starts the daemon if it is not running; spawns the
//!   `agent-doc watch` subprocess from the project root. Returns `Ok(true)` if started,
//!   `Ok(false)` if already running.
//! - `stop()` signals shutdown by removing the PID file; the daemon detects this on its
//!   next loop tick and exits cleanly.
//! - `status()` prints daemon running state and PID to stdout.
//! - The daemon resolves the project root (directory containing `.agent-doc/`) at startup
//!   and `chdir`s there so relative paths (PID file, sessions.json) are always correct.
//! - Session discovery: `discover_entries()` reads `sessions.json` and classifies each
//!   document by frontmatter mode:
//!   - `FileWatch` — append/template mode: monitored via `notify` file-system watcher.
//!   - `StreamCapture` — CRDT mode: polled by capturing the associated tmux pane.
//! - File-watch path: events are debounced (`debounce_ms`). After debounce, `run::run()`
//!   is called on the changed file.
//! - Loop prevention for file-watch: agent-triggered changes (within `debounce * 3` of
//!   last run) increment a per-file cycle counter; hard cap at `max_cycles`. Content hash
//!   equality stops the loop early (convergence detection).
//! - Stream-capture path: every 500 ms poll tick, `sessions::capture_pane()` is called;
//!   new lines (from `extract_new_lines()`) are flushed to the document via
//!   `stream::flush_to_document()`.
//! - CRDT documents also get reactive file-watching with zero debounce (tracked in
//!   `reactive_paths: HashSet<PathBuf>`).
//! - Session registry is rescanned every 10 s to pick up newly registered documents.
//! - Dead stream panes (pane no longer alive in tmux) are pruned on rescan.
//! - Idle timeout: daemon exits automatically after 60 s with no active sessions.
//! - PID file removal (external `stop` or daemon crash) triggers clean shutdown on next tick.
//! - `extract_new_lines(old, new)` finds the first diverging line between two captures
//!   and returns all new lines from that point onward.
//!
//! ## Agentic Contracts
//! - `start()`, `stop()`, `status()`, `is_running()`, and `ensure_running()` are the
//!   public API surface; all loop internals are private.
//! - `ensure_running()` is safe to call from any subcommand needing the daemon; it
//!   is idempotent and returns promptly when the daemon is already live.
//! - `extract_new_lines()` is pure and allocation-only; no I/O or side effects.
//! - The daemon never panics on individual file submit errors; errors are logged to
//!   stderr and the loop continues.
//!
//! ## Evals
//! - pid_file_roundtrip: write_pid → read_pid matches process id; remove_pid → read_pid returns None
//! - pid_alive_self: current process PID → true
//! - pid_alive_nonexistent: PID 4294967295 → false
//! - discover_empty_registry: no sessions.json → empty vec
//! - hash_deterministic: same file content read twice → identical hash
//! - hash_changes_with_content: file content changed → different hash
//! - loop_prevention_counter: increment then reset cycle_count → correct values
//! - convergence_detection: last_hash set → value preserved in state
//! - extract_new_lines_appended: new has extra lines at end → returns only new lines
//! - extract_new_lines_modified: diverges at line 2 → returns from divergence point onward
//! - extract_new_lines_identical: same content → empty string
//! - extract_new_lines_empty_old: old empty → all new lines returned
//! - extract_new_lines_empty_new: new empty → empty string
//! - stream_state_tracks_capture: two captures → incremental new content extracted correctly
//! - doc_mode_eq: FileWatch == FileWatch; StreamCapture != FileWatch

use anyhow::{bail, Context, Result};
use std::collections::{HashMap, HashSet};
use std::hash::{DefaultHasher, Hash, Hasher};
use std::path::{Path, PathBuf};
use std::sync::mpsc;
use std::time::{Duration, Instant};

use notify::{EventKind, RecursiveMode, Watcher};

use crate::{config::Config, frontmatter, sessions, stream, run};

const PID_FILE: &str = ".agent-doc/watch.pid";

/// Default idle timeout before daemon auto-exits (seconds).
const IDLE_TIMEOUT_SECS: u64 = 60;

/// Configuration for the watch daemon.
pub struct WatchConfig {
    pub debounce_ms: u64,
    pub max_cycles: u32,
}

/// Per-file state for loop prevention (file-watch mode).
struct FileState {
    last_run: Option<Instant>,
    cycle_count: u32,
    last_hash: Option<u64>,
}

impl FileState {
    fn new() -> Self {
        Self {
            last_run: None,
            cycle_count: 0,
            last_hash: None,
        }
    }
}

/// Per-file state for stream-mode capture polling.
struct StreamState {
    pane: String,
    last_capture: String,
    target: String,
    max_lines: usize,
}

/// Entry discovered from sessions registry with mode info.
struct WatchEntry {
    path: PathBuf,
    pane: String,
    mode: DocMode,
    target: String,
    max_lines: usize,
    /// Reactive mode: skip debounce for stream-mode documents.
    /// CRDT merge handles concurrent edits, so no debounce is needed.
    reactive: bool,
}

/// Document mode determines how the watch daemon handles the file.
#[derive(Debug, PartialEq)]
enum DocMode {
    /// append/template — use notify-based file watching, submit on change
    FileWatch,
    /// stream — poll tmux pane, flush new output to document
    StreamCapture,
}

/// Hash file content for convergence detection.
fn hash_content(path: &Path) -> Option<u64> {
    let content = std::fs::read_to_string(path).ok()?;
    // Strip boundary markers before hashing to prevent feedback loops.
    // Boundary repositions change the marker ID each cycle, making the hash
    // different even when no meaningful content changed. Without this,
    // reactive-mode documents (zero debounce) enter an infinite loop:
    // IPC write → boundary change → watch detects → re-run → IPC write → ...
    let content = strip_boundaries_for_hash(&content);
    let mut hasher = DefaultHasher::new();
    content.hash(&mut hasher);
    Some(hasher.finish())
}

/// Limit content to the last N lines to prevent indefinite growth.
fn limit_lines(content: &str, max_lines: usize) -> String {
    let lines: Vec<&str> = content.lines().collect();
    if lines.len() <= max_lines {
        return content.to_string();
    }
    lines[lines.len() - max_lines..].join("\n")
}

/// Strip boundary markers from content for hash comparison.
/// This ensures boundary-only changes don't trigger re-runs.
fn strip_boundaries_for_hash(content: &str) -> String {
    content
        .lines()
        .filter(|line| {
            let trimmed = line.trim();
            !(trimmed.starts_with("<!-- agent:boundary:") && trimmed.ends_with(" -->"))
        })
        .collect::<Vec<_>>()
        .join("\n")
}

/// Check if a PID is alive via /proc.
fn pid_alive(pid: u32) -> bool {
    Path::new(&format!("/proc/{}", pid)).exists()
}

/// Read the PID from the PID file.
fn read_pid() -> Option<u32> {
    let content = std::fs::read_to_string(PID_FILE).ok()?;
    content.trim().parse().ok()
}

/// Write our PID to the PID file.
fn write_pid() -> Result<()> {
    let pid_path = Path::new(PID_FILE);
    if let Some(parent) = pid_path.parent() {
        std::fs::create_dir_all(parent)?;
    }
    std::fs::write(pid_path, format!("{}", std::process::id()))?;
    Ok(())
}

/// Remove the PID file.
fn remove_pid() {
    let _ = std::fs::remove_file(PID_FILE);
}

/// Check if the watch daemon is currently running.
pub fn is_running() -> bool {
    read_pid().is_some_and(pid_alive)
}

/// Ensure the watch daemon is running. If not, spawn it in the background.
///
/// Called from claim/pre-flight to implement lazy start.
/// Returns Ok(true) if daemon was started, Ok(false) if already running.
pub fn ensure_running() -> Result<bool> {
    if is_running() {
        return Ok(false);
    }

    // Resolve project root (where .agent-doc/ lives)
    let cwd = std::env::current_dir().unwrap_or_default();
    let project_root = crate::snapshot::find_project_root(&cwd)
        .context("could not find .agent-doc/ directory — not in an agent-doc project")?;

    // Spawn daemon in background from project root
    let exe = std::env::current_exe().context("failed to resolve agent-doc binary path")?;
    std::process::Command::new(exe)
        .arg("watch")
        .current_dir(&project_root)
        .stdin(std::process::Stdio::null())
        .stdout(std::process::Stdio::null())
        .stderr(std::process::Stdio::piped())
        .spawn()
        .context("failed to spawn watch daemon")?;

    // Wait briefly for daemon to write PID file
    for _ in 0..10 {
        std::thread::sleep(Duration::from_millis(50));
        if is_running() {
            return Ok(true);
        }
    }

    // Best-effort: daemon may still be starting
    Ok(true)
}

/// Start the watch daemon.
///
/// Watches files registered in sessions.json for changes. On file change
/// (after debounce), runs `run::run()` on the changed file.
/// For stream-mode documents, polls tmux panes and flushes new output.
///
/// Loop prevention:
/// - Changes within the debounce window after a submit are treated as agent-triggered.
/// - Agent-triggered changes increment a cycle counter.
/// - If content hash matches previous submit, stop (convergence).
/// - Hard cap at `max_cycles` agent-triggered cycles per file.
///
/// Idle timeout:
/// - If no active sessions remain for 60s, daemon auto-exits.
pub fn start(config: &Config, watch_config: WatchConfig) -> Result<()> {
    // Resolve project root and cd there (critical for finding .agent-doc/)
    let cwd = std::env::current_dir().unwrap_or_default();
    if let Some(root) = find_project_root(&cwd)
        && root != cwd
    {
        std::env::set_current_dir(&root)
            .with_context(|| format!("failed to cd to project root {}", root.display()))?;
        eprintln!("Resolved project root: {}", root.display());
    }

    // Check if already running
    if let Some(pid) = read_pid() {
        if pid_alive(pid) {
            bail!("watch daemon already running (PID {})", pid);
        }
        // Stale PID file — clean up
        remove_pid();
    }

    write_pid()?;
    eprintln!("Watch daemon started (PID {})", std::process::id());

    // Install signal handler for clean shutdown
    let running = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(true));
    {
        let running = running.clone();
        ctrlc_handler(move || {
            running.store(false, std::sync::atomic::Ordering::SeqCst);
        });
    }

    let result = run_event_loop(config, &watch_config, &running);

    remove_pid();
    eprintln!("Watch daemon stopped.");
    result
}

/// Simple signal handler registration (best-effort).
fn ctrlc_handler<F: Fn() + Send + 'static>(f: F) {
    std::thread::spawn(move || {
        signal_wait();
        f();
    });
}

/// Wait for SIGTERM or SIGINT (Linux-specific, best-effort).
fn signal_wait() {
    loop {
        std::thread::sleep(Duration::from_secs(3600));
    }
}

/// The main event loop.
fn run_event_loop(
    config: &Config,
    watch_config: &WatchConfig,
    running: &std::sync::atomic::AtomicBool,
) -> Result<()> {
    let debounce = Duration::from_millis(watch_config.debounce_ms);
    let idle_timeout = Duration::from_secs(IDLE_TIMEOUT_SECS);
    let (tx, rx) = mpsc::channel();

    let mut watcher = notify::recommended_watcher(move |res: notify::Result<notify::Event>| {
        if let Ok(event) = res {
            let _ = tx.send(event);
        }
    })
    .context("failed to create file watcher")?;

    // Discover files from sessions registry (with mode detection)
    let entries = discover_entries()?;
    let mut watched_files: Vec<PathBuf> = Vec::new();
    let mut reactive_paths: HashSet<PathBuf> = HashSet::new();
    let mut stream_states: HashMap<PathBuf, StreamState> = HashMap::new();

    for entry in &entries {
        match entry.mode {
            DocMode::FileWatch => {
                if let Err(e) = watcher.watch(&entry.path, RecursiveMode::NonRecursive) {
                    eprintln!("Warning: could not watch {}: {}", entry.path.display(), e);
                } else {
                    watched_files.push(entry.path.clone());
                }
            }
            DocMode::StreamCapture => {
                // Stream-mode: tmux capture polling
                stream_states.insert(
                    entry.path.clone(),
                    StreamState {
                        pane: entry.pane.clone(),
                        last_capture: String::new(),
                        target: entry.target.clone(),
                        max_lines: entry.max_lines,
                    },
                );
                // Reactive: also file-watch with zero debounce (CRDT handles concurrency)
                if entry.reactive {
                    if let Err(e) = watcher.watch(&entry.path, RecursiveMode::NonRecursive) {
                        eprintln!("Warning: could not watch {}: {}", entry.path.display(), e);
                    } else {
                        watched_files.push(entry.path.clone());
                        reactive_paths.insert(entry.path.clone());
                    }
                }
            }
        }
    }

    let file_count = watched_files.len();
    let stream_count = stream_states.len();

    if file_count == 0 && stream_count == 0 {
        eprintln!("No session files found. Watching for new sessions...");
    } else {
        eprintln!(
            "Watching {} file(s), {} stream(s)",
            file_count, stream_count
        );
    }

    let mut states: HashMap<PathBuf, FileState> = HashMap::new();
    let mut pending: HashMap<PathBuf, Instant> = HashMap::new();
    let mut last_rescan = Instant::now();
    let mut idle_since: Option<Instant> = None;

    let tmux = sessions::Tmux::default_server();

    while running.load(std::sync::atomic::Ordering::Relaxed) {
        // Check PID file still exists (external stop)
        if !Path::new(PID_FILE).exists() {
            eprintln!("PID file removed — shutting down.");
            break;
        }

        // Idle timeout: exit if no active sessions for IDLE_TIMEOUT_SECS
        let has_active = !watched_files.is_empty() || !stream_states.is_empty();
        if has_active {
            idle_since = None;
        } else {
            let idle_start = *idle_since.get_or_insert_with(Instant::now);
            if Instant::now().duration_since(idle_start) >= idle_timeout {
                eprintln!("No active sessions for {}s — shutting down.", IDLE_TIMEOUT_SECS);
                break;
            }
        }

        // Rescan for new files periodically (every 10s)
        if last_rescan.elapsed() > Duration::from_secs(10) {
            let new_entries = discover_entries().unwrap_or_default();
            for entry in &new_entries {
                match entry.mode {
                    DocMode::FileWatch => {
                        if !watched_files.contains(&entry.path) {
                            if let Err(e) =
                                watcher.watch(&entry.path, RecursiveMode::NonRecursive)
                            {
                                eprintln!(
                                    "Warning: could not watch {}: {}",
                                    entry.path.display(),
                                    e
                                );
                            } else {
                                eprintln!("Now watching {}", entry.path.display());
                                watched_files.push(entry.path.clone());
                            }
                        }
                    }
                    DocMode::StreamCapture => {
                        if !stream_states.contains_key(&entry.path) {
                            eprintln!("Now streaming {}", entry.path.display());
                            stream_states.insert(
                                entry.path.clone(),
                                StreamState {
                                    pane: entry.pane.clone(),
                                    last_capture: String::new(),
                                    target: entry.target.clone(),
                                    max_lines: entry.max_lines,
                                },
                            );
                        }
                        // Add reactive file-watch for stream-mode docs
                        if entry.reactive && !reactive_paths.contains(&entry.path) {
                            if !watched_files.contains(&entry.path) {
                                if let Err(e) =
                                    watcher.watch(&entry.path, RecursiveMode::NonRecursive)
                                {
                                    eprintln!(
                                        "Warning: could not watch {}: {}",
                                        entry.path.display(),
                                        e
                                    );
                                } else {
                                    eprintln!(
                                        "Now watching {} (reactive)",
                                        entry.path.display()
                                    );
                                    watched_files.push(entry.path.clone());
                                }
                            }
                            reactive_paths.insert(entry.path.clone());
                        }
                    }
                }
            }

            // Prune dead stream entries (pane no longer alive)
            let dead_streams: Vec<PathBuf> = stream_states
                .iter()
                .filter(|(_, ss)| !tmux.pane_alive(&ss.pane))
                .map(|(p, _)| p.clone())
                .collect();
            for path in dead_streams {
                eprintln!("Stream pane dead for {} — removing", path.display());
                stream_states.remove(&path);
            }

            last_rescan = Instant::now();
        }

        // Poll stream-mode documents (tmux capture)
        for (path, ss) in &mut stream_states {
            match sessions::capture_pane(&tmux, &ss.pane) {
                Ok(captured) => {
                    if captured != ss.last_capture {
                        // Extract new lines since last capture, limited to last 50 lines
                        // to prevent the console component from growing indefinitely
                        let new_content = extract_new_lines(&ss.last_capture, &captured);
                        let limited = limit_lines(&new_content, ss.max_lines);
                        if !limited.is_empty() {
                            match stream::flush_to_document(path, &limited, &ss.target, "") {
                                Ok(()) => {
                                    eprint!(".");
                                }
                                Err(e) => {
                                    eprintln!(
                                        "[watch-stream] flush error for {}: {}",
                                        path.display(),
                                        e
                                    );
                                }
                            }
                        }
                        ss.last_capture = captured;
                    }
                }
                Err(e) => {
                    eprintln!(
                        "[watch-stream] capture error for {}: {}",
                        path.display(),
                        e
                    );
                }
            }
        }

        // Receive file-change events with timeout
        match rx.recv_timeout(Duration::from_millis(500)) {
            Ok(event) => {
                if matches!(
                    event.kind,
                    EventKind::Modify(_) | EventKind::Create(_)
                ) {
                    for path in event.paths {
                        let canonical = path.canonicalize().unwrap_or(path);
                        if watched_files.iter().any(|w| {
                            w.canonicalize().unwrap_or_else(|_| w.clone()) == canonical
                        }) {
                            pending.insert(canonical, Instant::now());
                        }
                    }
                }
            }
            Err(mpsc::RecvTimeoutError::Timeout) => {}
            Err(mpsc::RecvTimeoutError::Disconnected) => break,
        }

        // Process debounced file-change events (reactive paths skip debounce)
        let now = Instant::now();
        let ready: Vec<PathBuf> = pending
            .iter()
            .filter(|(path, when)| {
                let effective_debounce = if reactive_paths.contains(*path) {
                    Duration::ZERO
                } else {
                    debounce
                };
                now.duration_since(**when) >= effective_debounce
            })
            .map(|(path, _)| path.clone())
            .collect();

        for path in ready {
            pending.remove(&path);

            let state = states.entry(path.clone()).or_insert_with(FileState::new);

            // Check if this is an agent-triggered change
            let is_agent_change = state
                .last_run
                .is_some_and(|t| now.duration_since(t) < debounce * 3);

            if is_agent_change {
                state.cycle_count += 1;

                // Check cycle limit
                if state.cycle_count > watch_config.max_cycles {
                    eprintln!(
                        "Max cycles ({}) reached for {} — skipping",
                        watch_config.max_cycles,
                        path.display()
                    );
                    continue;
                }

                // Check convergence
                let current_hash = hash_content(&path);
                if current_hash.is_some() && current_hash == state.last_hash {
                    eprintln!("Converged for {} — skipping", path.display());
                    state.cycle_count = 0;
                    continue;
                }
                state.last_hash = current_hash;
            } else {
                // User change — reset cycle counter
                state.cycle_count = 0;
                state.last_hash = hash_content(&path);
            }

            // Skip if file has an active agent-doc operation (prevents duplicate
            // responses from watch daemon competing with skill/stream writes)
            let file_str = path.to_string_lossy().to_string();
            if agent_doc::debounce::is_busy(&file_str) {
                eprintln!("[watch] skipping {} — busy (active operation in progress)", path.display());
                continue;
            }

            // Submit
            eprintln!("Change detected: {}", path.display());
            match run::run(&path, false, None, None, false, false, config) {
                Ok(()) => {
                    state.last_run = Some(Instant::now());
                    eprintln!("Submit complete: {}", path.display());
                }
                Err(e) => {
                    eprintln!("Submit failed for {}: {}", path.display(), e);
                }
            }
        }
    }

    Ok(())
}

/// Discover files from sessions registry with mode detection.
///
/// Reads each document's frontmatter to determine whether it's
/// file-watched (append/template) or stream-captured (stream mode).
fn discover_entries() -> Result<Vec<WatchEntry>> {
    let registry = sessions::load()?;
    let mut entries = Vec::new();
    for entry in registry.values() {
        let path = PathBuf::from(&entry.file);
        if !path.exists() {
            continue;
        }
        let canonical = path.canonicalize().unwrap_or(path);

        // Detect mode from frontmatter
        let (mode, target, reactive, max_lines) = match std::fs::read_to_string(&canonical) {
            Ok(content) => match frontmatter::parse(&content) {
                Ok((fm, _)) => {
                    let resolved = fm.resolve_mode();
                    if resolved.is_crdt() {
                        let target = fm
                            .stream_config
                            .as_ref()
                            .and_then(|sc| sc.target.clone())
                            .unwrap_or_else(|| "console".to_string());
                        let max_lines = fm
                            .stream_config
                            .as_ref()
                            .and_then(|sc| sc.max_lines)
                            .unwrap_or(50);
                        (DocMode::StreamCapture, target, true, max_lines)
                    } else {
                        (DocMode::FileWatch, String::new(), false, 50)
                    }
                }
                Err(_) => (DocMode::FileWatch, String::new(), false, 50),
            },
            Err(_) => (DocMode::FileWatch, String::new(), false, 50),
        };

        entries.push(WatchEntry {
            path: canonical,
            pane: entry.pane.clone(),
            mode,
            target,
            max_lines,
            reactive,
        });
    }
    Ok(entries)
}

/// Discover only file paths (backward-compat wrapper used by tests).
#[cfg(test)]
fn discover_files() -> Result<Vec<PathBuf>> {
    Ok(discover_entries()?
        .into_iter()
        .map(|e| e.path)
        .collect())
}

/// Extract new lines from a pane capture by diffing against the previous capture.
///
/// Compares line-by-line: finds the first divergence point and returns
/// all lines from that point onward in the new capture.
fn extract_new_lines(old: &str, new: &str) -> String {
    let old_lines: Vec<&str> = old.lines().collect();
    let new_lines: Vec<&str> = new.lines().collect();

    // Find the first line that differs
    let common_prefix = old_lines
        .iter()
        .zip(new_lines.iter())
        .take_while(|(a, b)| a == b)
        .count();

    // Everything after the common prefix in the new capture is new content
    if common_prefix < new_lines.len() {
        new_lines[common_prefix..].join("\n")
    } else {
        String::new()
    }
}

/// Stop the watch daemon by removing the PID file.
pub fn stop() -> Result<()> {
    match read_pid() {
        Some(pid) => {
            if pid_alive(pid) {
                remove_pid();
                eprintln!("Signaled watch daemon (PID {}) to stop.", pid);
            } else {
                remove_pid();
                eprintln!(
                    "Watch daemon (PID {}) was not running. Cleaned up PID file.",
                    pid
                );
            }
        }
        None => {
            eprintln!("No watch daemon running.");
        }
    }
    Ok(())
}

/// Check the status of the watch daemon.
pub fn status() -> Result<()> {
    match read_pid() {
        Some(pid) => {
            if pid_alive(pid) {
                println!("Watch daemon running (PID {})", pid);
            } else {
                println!("Watch daemon not running (stale PID file: {})", pid);
            }
        }
        None => {
            println!("Watch daemon not running.");
        }
    }
    Ok(())
}

/// Find the project root by walking up from `path` looking for `.agent-doc/`.
fn find_project_root(path: &Path) -> Option<PathBuf> {
    let mut current = if path.is_file() {
        path.parent()?
    } else {
        path
    };
    loop {
        if current.join(".agent-doc").is_dir() {
            return Some(current.to_path_buf());
        }
        current = current.parent()?;
    }
}

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

    #[test]
    fn pid_file_roundtrip() {
        let dir = TempDir::new().unwrap();
        let _guard = std::env::set_current_dir(dir.path());
        std::fs::create_dir_all(".agent-doc").unwrap();

        write_pid().unwrap();
        let pid = read_pid().unwrap();
        assert_eq!(pid, std::process::id());

        remove_pid();
        assert!(read_pid().is_none());
    }

    #[test]
    fn pid_alive_self() {
        assert!(pid_alive(std::process::id()));
    }

    #[test]
    fn pid_alive_nonexistent() {
        assert!(!pid_alive(4_294_967_295));
    }

    #[test]
    fn discover_empty_registry() {
        let dir = TempDir::new().unwrap();
        let _guard = std::env::set_current_dir(dir.path());
        let files = discover_files().unwrap();
        assert!(files.is_empty());
    }

    #[test]
    fn hash_deterministic() {
        let dir = TempDir::new().unwrap();
        let path = dir.path().join("test.md");
        std::fs::write(&path, "hello world").unwrap();

        let h1 = hash_content(&path).unwrap();
        let h2 = hash_content(&path).unwrap();
        assert_eq!(h1, h2);
    }

    #[test]
    fn hash_changes_with_content() {
        let dir = TempDir::new().unwrap();
        let path = dir.path().join("test.md");

        std::fs::write(&path, "version 1").unwrap();
        let h1 = hash_content(&path).unwrap();

        std::fs::write(&path, "version 2").unwrap();
        let h2 = hash_content(&path).unwrap();

        assert_ne!(h1, h2);
    }

    #[test]
    fn loop_prevention_counter() {
        let mut state = FileState::new();
        assert_eq!(state.cycle_count, 0);
        state.cycle_count += 1;
        assert_eq!(state.cycle_count, 1);
        state.cycle_count = 0; // user change reset
        assert_eq!(state.cycle_count, 0);
    }

    #[test]
    fn convergence_detection() {
        let mut state = FileState::new();
        state.last_hash = Some(42);
        assert_eq!(state.last_hash, Some(42));
    }

    #[test]
    fn extract_new_lines_appended() {
        let old = "line 1\nline 2\nline 3";
        let new = "line 1\nline 2\nline 3\nline 4\nline 5";
        let result = extract_new_lines(old, new);
        assert_eq!(result, "line 4\nline 5");
    }

    #[test]
    fn extract_new_lines_modified() {
        let old = "line 1\nline 2\nline 3";
        let new = "line 1\nchanged\nline 3\nline 4";
        let result = extract_new_lines(old, new);
        // Diverges at line 2; returns from there onward
        assert_eq!(result, "changed\nline 3\nline 4");
    }

    #[test]
    fn extract_new_lines_identical() {
        let old = "line 1\nline 2";
        let new = "line 1\nline 2";
        let result = extract_new_lines(old, new);
        assert_eq!(result, "");
    }

    #[test]
    fn extract_new_lines_empty_old() {
        let old = "";
        let new = "line 1\nline 2";
        let result = extract_new_lines(old, new);
        assert_eq!(result, "line 1\nline 2");
    }

    #[test]
    fn extract_new_lines_empty_new() {
        let old = "line 1\nline 2";
        let new = "";
        let result = extract_new_lines(old, new);
        assert_eq!(result, "");
    }

    #[test]
    fn stream_state_tracks_capture() {
        let mut ss = StreamState {
            pane: "%42".to_string(),
            last_capture: String::new(),
            target: "console".to_string(),
            max_lines: 50,
        };
        let capture = "claude output line 1\nclaude output line 2".to_string();
        let new_content = extract_new_lines(&ss.last_capture, &capture);
        assert_eq!(new_content, "claude output line 1\nclaude output line 2");
        ss.last_capture = capture;

        // Second capture with more lines
        let capture2 = "claude output line 1\nclaude output line 2\nclaude output line 3".to_string();
        let new_content2 = extract_new_lines(&ss.last_capture, &capture2);
        assert_eq!(new_content2, "claude output line 3");
        ss.last_capture = capture2;
    }

    #[test]
    fn doc_mode_eq() {
        assert_eq!(DocMode::FileWatch, DocMode::FileWatch);
        assert_eq!(DocMode::StreamCapture, DocMode::StreamCapture);
        assert_ne!(DocMode::FileWatch, DocMode::StreamCapture);
    }

    #[test]
    #[ignore] // Integration test — requires notify to work with real filesystem
    fn watcher_detects_change() {
        use std::time::Duration;

        let dir = TempDir::new().unwrap();
        let path = dir.path().join("test.md");
        std::fs::write(&path, "initial").unwrap();

        let (tx, rx) = mpsc::channel();
        let mut watcher = notify::recommended_watcher(move |res: notify::Result<notify::Event>| {
            if let Ok(event) = res {
                let _ = tx.send(event);
            }
        })
        .unwrap();

        watcher
            .watch(&path, RecursiveMode::NonRecursive)
            .unwrap();

        // Give watcher time to initialize
        std::thread::sleep(Duration::from_millis(100));

        std::fs::write(&path, "modified").unwrap();

        let event = rx.recv_timeout(Duration::from_secs(2)).unwrap();
        assert!(!event.paths.is_empty());
    }
}