with-watch 0.1.2

Watch command inputs and rerun commands when they change
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
use std::{
    ffi::OsString,
    fs,
    path::PathBuf,
    process::{Child, Command, ExitStatus, Stdio},
    thread,
    time::Duration,
};

use tracing::{debug, info, warn};

use crate::{
    analysis::{CommandAdapterId, CommandAnalysisStatus, SideEffectProfile},
    error::{Result, WithWatchError},
    snapshot::{capture_snapshot, ChangeDetectionMode, CommandSource, SnapshotState, WatchInput},
    watch::{CollectedEvents, WatchLoop},
};

const DEFAULT_POLL_TIMEOUT: Duration = Duration::from_millis(50);
const DEFAULT_DEBOUNCE_WINDOW: Duration = Duration::from_millis(200);
const WITH_WATCH_TEST_RUN_MARKER_DIR_ENV: &str = "WITH_WATCH_TEST_RUN_MARKER_DIR";

#[derive(Debug, Clone)]
pub struct ExecutionPlan {
    pub source: CommandSource,
    pub detection_mode: ChangeDetectionMode,
    pub inputs: Vec<WatchInput>,
    pub delegated_command: DelegatedCommand,
    pub metadata: ExecutionMetadata,
}

impl ExecutionPlan {
    pub fn passthrough(
        argv: Vec<OsString>,
        inputs: Vec<WatchInput>,
        detection_mode: ChangeDetectionMode,
        metadata: ExecutionMetadata,
    ) -> Self {
        Self {
            source: CommandSource::Argv,
            detection_mode,
            inputs,
            delegated_command: DelegatedCommand::Argv(argv),
            metadata,
        }
    }

    pub fn shell(
        expression: String,
        inputs: Vec<WatchInput>,
        detection_mode: ChangeDetectionMode,
        metadata: ExecutionMetadata,
    ) -> Self {
        Self {
            source: CommandSource::Shell,
            detection_mode,
            inputs,
            delegated_command: DelegatedCommand::Shell(expression),
            metadata,
        }
    }

    pub fn exec(
        argv: Vec<OsString>,
        inputs: Vec<WatchInput>,
        detection_mode: ChangeDetectionMode,
        metadata: ExecutionMetadata,
    ) -> Self {
        Self {
            source: CommandSource::Exec,
            detection_mode,
            inputs,
            delegated_command: DelegatedCommand::Argv(argv),
            metadata,
        }
    }
}

#[derive(Debug, Clone)]
pub struct ExecutionMetadata {
    pub adapter_ids: Vec<CommandAdapterId>,
    pub fallback_used: bool,
    pub default_watch_root_used: bool,
    pub filtered_output_count: usize,
    pub side_effect_profile: SideEffectProfile,
    pub status: CommandAnalysisStatus,
}

impl ExecutionMetadata {
    pub fn adapter_field(&self) -> String {
        self.adapter_ids
            .iter()
            .map(|adapter| adapter.as_str())
            .collect::<Vec<_>>()
            .join(",")
    }
}

#[derive(Debug, Clone)]
pub enum DelegatedCommand {
    Argv(Vec<OsString>),
    Shell(String),
}

impl DelegatedCommand {
    fn spawn_log_summary(&self) -> DelegatedCommandLogSummary {
        match self {
            Self::Argv(argv) => {
                let program_name = argv
                    .first()
                    .map(program_name)
                    .unwrap_or_else(|| "<missing>".to_string());
                DelegatedCommandLogSummary {
                    execution_kind: "argv",
                    program_name,
                    arg_count: argv.len().saturating_sub(1),
                }
            }
            Self::Shell(_) => DelegatedCommandLogSummary {
                execution_kind: "shell",
                program_name: "sh".to_string(),
                arg_count: 2,
            },
        }
    }

    fn display_name(&self) -> String {
        match self {
            Self::Argv(argv) => argv
                .iter()
                .map(|value| value.to_string_lossy().into_owned())
                .collect::<Vec<_>>()
                .join(" "),
            Self::Shell(expression) => expression.clone(),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
struct DelegatedCommandLogSummary {
    execution_kind: &'static str,
    program_name: String,
    arg_count: usize,
}

#[derive(Debug, Clone, Copy)]
pub struct RunnerOptions {
    pub debounce_window: Duration,
    pub poll_timeout: Duration,
    pub max_runs: Option<usize>,
}

impl Default for RunnerOptions {
    fn default() -> Self {
        Self {
            debounce_window: DEFAULT_DEBOUNCE_WINDOW,
            poll_timeout: DEFAULT_POLL_TIMEOUT,
            max_runs: None,
        }
    }
}

impl RunnerOptions {
    pub fn from_environment() -> Self {
        let mut options = Self::default();

        // Test-only hooks for deterministic integration coverage. They keep the public
        // CLI surface stable while allowing `cargo test` to stop the
        // long-running watch loop and shorten debounce windows. Remove them
        // when we have a better end-to-end harness.
        if let Ok(raw_max_runs) = std::env::var("WITH_WATCH_TEST_MAX_RUNS") {
            if let Ok(parsed) = raw_max_runs.parse::<usize>() {
                options.max_runs = Some(parsed);
            }
        }

        if let Ok(raw_debounce_ms) = std::env::var("WITH_WATCH_TEST_DEBOUNCE_MS") {
            if let Ok(parsed) = raw_debounce_ms.parse::<u64>() {
                options.debounce_window = Duration::from_millis(parsed);
            }
        }

        options
    }
}

pub fn run(plan: ExecutionPlan, options: RunnerOptions) -> Result<i32> {
    let mut watch_loop = WatchLoop::new(&plan.inputs)?;
    let mut baseline = capture_snapshot(&plan.inputs, plan.detection_mode)?;
    let mut child = Some(spawn_command(&plan.delegated_command)?);
    let mut completed_runs = 0usize;
    let mut pending_rerun = false;
    let mut suppressed_self_change_snapshot = None::<SnapshotState>;

    info!(
        command_source = plan.source.as_str(),
        detection_mode = plan.detection_mode.as_str(),
        input_count = plan.inputs.len(),
        adapter_id = plan.metadata.adapter_field(),
        fallback_used = plan.metadata.fallback_used,
        default_watch_root_used = plan.metadata.default_watch_root_used,
        filtered_output_count = plan.metadata.filtered_output_count,
        side_effect_profile = plan.metadata.side_effect_profile.as_str(),
        analysis_status = plan.metadata.status.as_str(),
        "Starting with-watch run loop"
    );

    loop {
        if let Some(active_child) = child.as_mut() {
            if let Some(status) =
                active_child
                    .try_wait()
                    .map_err(|source| WithWatchError::Wait {
                        command: plan.delegated_command.display_name(),
                        source,
                    })?
            {
                completed_runs += 1;
                let last_exit_code = exit_code_from_status(status);
                let post_run_snapshot = capture_snapshot(&plan.inputs, plan.detection_mode)?;
                let inputs_changed_since_baseline =
                    post_run_snapshot.is_meaningfully_different(&baseline, plan.detection_mode);
                let additional_change_after_suppression = suppressed_self_change_snapshot
                    .as_ref()
                    .is_some_and(|snapshot| {
                        post_run_snapshot.is_meaningfully_different(snapshot, plan.detection_mode)
                    });
                let should_rerun = if plan.metadata.side_effect_profile
                    == SideEffectProfile::WritesWatchedInputs
                {
                    pending_rerun || additional_change_after_suppression
                } else {
                    pending_rerun && inputs_changed_since_baseline
                };

                if pending_rerun
                    && plan.metadata.side_effect_profile == SideEffectProfile::WritesWatchedInputs
                {
                    debug!(
                        rerun_queued = true,
                        side_effect_profile = plan.metadata.side_effect_profile.as_str(),
                        "Queued rerun after additional changes during self-mutating command \
                         activity"
                    );
                } else if additional_change_after_suppression
                    && plan.metadata.side_effect_profile == SideEffectProfile::WritesWatchedInputs
                {
                    debug!(
                        rerun_queued = true,
                        side_effect_profile = plan.metadata.side_effect_profile.as_str(),
                        "Queued rerun because post-run state diverged from the suppressed \
                         self-change snapshot"
                    );
                } else if suppressed_self_change_snapshot.is_some()
                    && plan.metadata.side_effect_profile == SideEffectProfile::WritesWatchedInputs
                {
                    debug!(
                        rerun_suppressed = true,
                        side_effect_profile = plan.metadata.side_effect_profile.as_str(),
                        "Suppressing rerun after self-mutating command activity"
                    );
                }

                baseline = post_run_snapshot;
                pending_rerun = false;
                suppressed_self_change_snapshot = None;
                child = None;
                write_test_run_marker(completed_runs);

                info!(
                    completed_runs,
                    last_exit_code,
                    command_source = plan.source.as_str(),
                    rerun_queued = should_rerun,
                    "Delegated command finished"
                );

                if options
                    .max_runs
                    .is_some_and(|limit| completed_runs >= limit)
                {
                    return Ok(last_exit_code);
                }

                if should_rerun {
                    child = Some(spawn_command(&plan.delegated_command)?);
                    continue;
                }
            }
        }

        if let Some(events) =
            watch_loop.collect_events(options.poll_timeout, options.debounce_window)
        {
            handle_watch_events(&events);

            let current_snapshot = capture_snapshot(&plan.inputs, plan.detection_mode)?;
            let reference_snapshot = if child.is_some()
                && plan.metadata.side_effect_profile == SideEffectProfile::WritesWatchedInputs
            {
                suppressed_self_change_snapshot
                    .as_ref()
                    .unwrap_or(&baseline)
            } else {
                &baseline
            };

            if current_snapshot.is_meaningfully_different(reference_snapshot, plan.detection_mode) {
                debug!(
                    event_count = events.event_count,
                    path_count = events.path_count,
                    child_running = child.is_some(),
                    "Observed meaningful input changes"
                );

                if child.is_some() {
                    if plan.metadata.side_effect_profile == SideEffectProfile::WritesWatchedInputs
                        && suppressed_self_change_snapshot.is_none()
                    {
                        suppressed_self_change_snapshot = Some(current_snapshot);
                        debug!(
                            rerun_suppressed = true,
                            side_effect_profile = plan.metadata.side_effect_profile.as_str(),
                            "Suppressed the first in-run snapshot change for a self-mutating \
                             command"
                        );
                    } else {
                        pending_rerun = true;
                    }
                } else {
                    baseline = current_snapshot;
                    child = Some(spawn_command(&plan.delegated_command)?);
                }
            } else if child.is_some() {
                debug!(
                    rerun_suppressed = true,
                    "Ignored non-meaningful filesystem churn"
                );
            }
        } else if child.is_none() {
            thread::sleep(Duration::from_millis(10));
        }
    }
}

fn handle_watch_events(events: &CollectedEvents) {
    if events.error_count > 0 {
        warn!(
            error_count = events.error_count,
            event_count = events.event_count,
            path_count = events.path_count,
            "Watcher reported recoverable errors; forcing a rescan"
        );
    } else {
        debug!(
            event_count = events.event_count,
            path_count = events.path_count,
            "Collected filesystem events"
        );
    }
}

fn spawn_command(command: &DelegatedCommand) -> Result<Child> {
    log_delegated_command_spawn(command);
    match command {
        DelegatedCommand::Argv(argv) => spawn_argv(argv),
        DelegatedCommand::Shell(expression) => spawn_shell(expression),
    }
}

fn log_delegated_command_spawn(command: &DelegatedCommand) {
    let summary = command.spawn_log_summary();
    info!(
        execution_kind = summary.execution_kind,
        program = summary.program_name,
        arg_count = summary.arg_count,
        "Spawning delegated command"
    );
}

fn spawn_argv(argv: &[OsString]) -> Result<Child> {
    let program = argv
        .first()
        .cloned()
        .ok_or(WithWatchError::MissingCommand)?;

    Command::new(&program)
        .args(argv.iter().skip(1))
        .stdin(Stdio::inherit())
        .stdout(Stdio::inherit())
        .stderr(Stdio::inherit())
        .spawn()
        .map_err(|source| WithWatchError::Spawn {
            command: program.to_string_lossy().into_owned(),
            source,
        })
}

fn spawn_shell(expression: &str) -> Result<Child> {
    #[cfg(not(unix))]
    {
        let _ = expression;
        Err(WithWatchError::UnsupportedShellPlatform)
    }

    #[cfg(unix)]
    {
        Command::new("/bin/sh")
            .arg("-c")
            .arg(expression)
            .stdin(Stdio::inherit())
            .stdout(Stdio::inherit())
            .stderr(Stdio::inherit())
            .spawn()
            .map_err(|source| WithWatchError::Spawn {
                command: expression.to_string(),
                source,
            })
    }
}

fn program_name(program: &OsString) -> String {
    std::path::Path::new(program)
        .file_name()
        .unwrap_or(program.as_os_str())
        .to_string_lossy()
        .into_owned()
}

fn exit_code_from_status(status: ExitStatus) -> i32 {
    status.code().unwrap_or(1)
}

fn write_test_run_marker(completed_runs: usize) {
    let Ok(marker_dir) = std::env::var(WITH_WATCH_TEST_RUN_MARKER_DIR_ENV) else {
        return;
    };

    let marker_path = PathBuf::from(marker_dir).join(format!("run-{completed_runs}.done"));
    if let Some(parent) = marker_path.parent() {
        if let Err(error) = fs::create_dir_all(parent) {
            warn!(
                path = parent.display().to_string(),
                %error,
                "Failed to create test run marker directory"
            );
            return;
        }
    }

    if let Err(error) = fs::write(&marker_path, completed_runs.to_string()) {
        warn!(
            path = marker_path.display().to_string(),
            %error,
            "Failed to write test run marker"
        );
    }
}

#[cfg(test)]
mod tests {
    use std::{
        ffi::OsString,
        io::{self, Write},
        sync::{Arc, Mutex},
    };

    use tracing::Level;

    use super::{log_delegated_command_spawn, DelegatedCommand};

    #[test]
    fn argv_spawn_logging_omits_argument_values() {
        let output = capture_logs(|| {
            log_delegated_command_spawn(&DelegatedCommand::Argv(vec![
                OsString::from("env"),
                OsString::from("TOKEN=secret"),
                OsString::from("cmd"),
            ]));
        });

        assert!(output.contains("execution_kind=\"argv\""));
        assert!(output.contains("program=\"env\""));
        assert!(output.contains("arg_count=2"));
        assert!(!output.contains("TOKEN=secret"));
        assert!(!output.contains("cmd"));
    }

    #[test]
    fn shell_spawn_logging_omits_expression_text() {
        let output = capture_logs(|| {
            log_delegated_command_spawn(&DelegatedCommand::Shell(
                "TOKEN=secret grep -f patterns.txt file.txt".to_string(),
            ));
        });

        assert!(output.contains("execution_kind=\"shell\""));
        assert!(output.contains("program=\"sh\""));
        assert!(output.contains("arg_count=2"));
        assert!(!output.contains("TOKEN=secret"));
        assert!(!output.contains("patterns.txt"));
    }

    fn capture_logs(callback: impl FnOnce()) -> String {
        let buffer = Arc::new(Mutex::new(Vec::new()));
        let writer = SharedWriter(buffer.clone());
        let subscriber = tracing_subscriber::fmt()
            .with_ansi(false)
            .with_target(false)
            .with_level(false)
            .without_time()
            .with_max_level(Level::INFO)
            .with_writer(move || writer.clone())
            .finish();

        tracing::subscriber::with_default(subscriber, callback);

        let output = buffer.lock().expect("lock buffer").clone();
        String::from_utf8(output).expect("utf8 log output")
    }

    #[derive(Clone)]
    struct SharedWriter(Arc<Mutex<Vec<u8>>>);

    impl Write for SharedWriter {
        fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
            self.0
                .lock()
                .expect("lock log buffer")
                .extend_from_slice(buf);
            Ok(buf.len())
        }

        fn flush(&mut self) -> io::Result<()> {
            Ok(())
        }
    }
}