Files
hx/crates/hx-lens/src/watch.rs
T
johannesandClaude Sonnet 5.5 a94632ea3d chore: delete code comments, add ratchet lint and ADRs
Classify every comment as tautology, stale, excuse or decision record.
Tautology/stale/excuse comments are deleted (excuses filed as issues
#196-#236), decisions moved to docs/adr/0001-0034.

- scripts/lint-comments.sh + .comment-baseline: per-file comment count
  may only go down; wired into tests/all.sh
- .claude/skills/delete-comment: repeatable audit workflow
- cargo fmt applied to touched crates

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-21 13:37:32 +01:00

92 lines
2.6 KiB
Rust

use crate::seq::{SeqLog, Source};
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::{Arc, Mutex};
use std::time::{Duration, SystemTime};
#[derive(Default)]
pub struct Watch {
mtimes: Mutex<HashMap<PathBuf, Option<SystemTime>>>,
}
fn mtime(p: &PathBuf) -> Option<SystemTime> {
std::fs::metadata(p).and_then(|m| m.modified()).ok()
}
impl Watch {
pub fn track(&self, p: PathBuf) {
let m = mtime(&p);
self.mtimes
.lock()
.unwrap_or_else(|e| e.into_inner())
.insert(p, m);
}
pub fn scan(&self, seq: &SeqLog, src: Source) -> Vec<PathBuf> {
let mut map = self.mtimes.lock().unwrap_or_else(|e| e.into_inner());
let mut changed = vec![];
for (p, old) in map.iter_mut() {
let now = mtime(p);
if now != *old {
*old = now;
seq.bump(p, src);
changed.push(p.clone());
}
}
changed
}
pub fn spawn(
self: &Arc<Self>,
seq: Arc<SeqLog>,
every: Duration,
) -> tokio::task::JoinHandle<()> {
let w = self.clone();
tokio::spawn(async move {
let mut t = tokio::time::interval(every);
loop {
t.tick().await;
w.scan(&seq, Source::External);
}
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn external_bumps_once() {
let d = tempfile::tempdir().unwrap();
let f = d.path().join("a.rs");
std::fs::write(&f, "1").unwrap();
let (w, s) = (Watch::default(), SeqLog::default());
w.track(f.clone());
assert!(w.scan(&s, Source::External).is_empty());
std::thread::sleep(Duration::from_millis(20));
std::fs::write(&f, "2").unwrap();
assert_eq!(w.scan(&s, Source::External), std::slice::from_ref(&f));
assert!(w.scan(&s, Source::External).is_empty());
assert_eq!(s.project(), 1);
std::thread::sleep(Duration::from_millis(20));
std::fs::write(&f, "3").unwrap();
w.track(f);
assert!(w.scan(&s, Source::External).is_empty());
}
#[test]
fn agent_scan_not_external() {
let d = tempfile::tempdir().unwrap();
let f = d.path().join("a.rs");
std::fs::write(&f, "1").unwrap();
let (w, s) = (Watch::default(), SeqLog::default());
w.track(f.clone());
std::thread::sleep(Duration::from_millis(20));
std::fs::write(&f, "2").unwrap();
assert_eq!(w.scan(&s, Source::Agent).len(), 1);
assert!(w.scan(&s, Source::External).is_empty());
assert!(s.since(0).len() == 1);
}
}