mirror of
https://github.com/jparkerweb/plan2code.git
synced 2026-12-04 00:15:41 -08:00
129 lines
4.7 KiB
Rust
129 lines
4.7 KiB
Rust
//! The watcher's timing, the silence rule and the generic watch loop, called by
|
|||
|
|
//! both the Implementation Loop and the Review Loop.
|
||
|
|
|
||
|
|
use std::time::{Duration, SystemTime};
|
||
|
|
|
||
|
|
use crate::loop_driver::LoopSetup;
|
||
|
|
|
||
|
|
/// How often the watcher looks at the running loop.
|
||
|
|
pub const WATCH_EVERY: Duration = Duration::from_secs(2);
|
||
|
|
/// How often silence looks at the session's files and the tree.
|
||
|
|
pub const ACTIVITY_EVERY: Duration = Duration::from_secs(30);
|
||
|
|
|
||
|
|
/// What a step of the watch loop says to do next.
|
||
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||
|
|
pub enum WatchStep {
|
||
|
|
Continue,
|
||
|
|
Stop,
|
||
|
|
}
|
||
|
|
|
||
|
|
/// Look every `every` until `step` says `Stop`. A panic in `step` ends the
|
||
|
|
/// watch and comes back as its text (`Some`), so the caller can end its loop
|
||
|
|
/// with the "watcher failed" stop instead of leaving a loop running with
|
||
|
|
/// nothing watching it; a normal stop returns `None`.
|
||
|
|
pub fn watch_loop<F: FnMut() -> WatchStep>(every: Duration, mut step: F) -> Option<String> {
|
||
|
|
loop {
|
||
|
|
std::thread::sleep(every);
|
||
|
|
let seen = std::panic::catch_unwind(std::panic::AssertUnwindSafe(&mut step));
|
||
|
|
match seen {
|
||
|
|
Ok(WatchStep::Continue) => {}
|
||
|
|
Ok(WatchStep::Stop) => return None,
|
||
|
|
Err(payload) => {
|
||
|
|
return Some(
|
||
|
|
payload
|
||
|
|
.downcast_ref::<&str>()
|
||
|
|
.map(|s| (*s).to_string())
|
||
|
|
.or_else(|| payload.downcast_ref::<String>().cloned())
|
||
|
|
.unwrap_or_else(|| "an unknown error".to_string()),
|
||
|
|
);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/// Whether the agent has been quiet too long. A waiting question never counts
|
||
|
|
/// (the person may take all night). The limit is the setup's silence minutes
|
||
|
|
/// or the skill's own `quietMinutes`, whichever is longer, at most 60.
|
||
|
|
pub fn silence_expired(
|
||
|
|
now: SystemTime,
|
||
|
|
last_activity: SystemTime,
|
||
|
|
setup: &LoopSetup,
|
||
|
|
quiet_minutes: Option<u32>,
|
||
|
|
waiting: bool,
|
||
|
|
) -> bool {
|
||
|
|
if waiting {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
let minutes = setup
|
||
|
|
.silence_minutes
|
||
|
|
.max(quiet_minutes.unwrap_or(0))
|
||
|
|
.min(60);
|
||
|
|
let limit = Duration::from_secs(u64::from(minutes) * 60);
|
||
|
|
now.duration_since(last_activity)
|
||
|
|
.is_ok_and(|quiet| quiet > limit)
|
||
|
|
}
|
||
|
|
|
||
|
|
#[cfg(test)]
|
||
|
|
mod tests {
|
||
|
|
use super::*;
|
||
|
|
use crate::loop_driver::{FixMode, OnQuestion};
|
||
|
|
use std::time::UNIX_EPOCH;
|
||
|
|
|
||
|
|
fn setup() -> LoopSetup {
|
||
|
|
LoopSetup {
|
||
|
|
fix_mode: FixMode::Levels,
|
||
|
|
on_question: OnQuestion::Pause,
|
||
|
|
..LoopSetup::default()
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
fn at(minutes: u64) -> SystemTime {
|
||
|
|
UNIX_EPOCH + Duration::from_secs(minutes * 60)
|
||
|
|
}
|
||
|
|
|
||
|
|
#[test]
|
||
|
|
fn the_watch_loop_stops_on_stop_and_returns_the_text_of_a_panic() {
|
||
|
|
let none = Duration::ZERO;
|
||
|
|
let mut calls = 0;
|
||
|
|
let stopped = watch_loop(none, || {
|
||
|
|
calls += 1;
|
||
|
|
if calls < 3 {
|
||
|
|
WatchStep::Continue
|
||
|
|
} else {
|
||
|
|
WatchStep::Stop
|
||
|
|
}
|
||
|
|
});
|
||
|
|
assert_eq!((stopped, calls), (None, 3));
|
||
|
|
let quiet = std::panic::take_hook();
|
||
|
|
std::panic::set_hook(Box::new(|_| {}));
|
||
|
|
let literal = watch_loop(none, || panic!("literal"));
|
||
|
|
let text = "formatted".to_string();
|
||
|
|
let owned = watch_loop(none, || panic!("{text}"));
|
||
|
|
let other = watch_loop(none, || std::panic::panic_any(1_u8));
|
||
|
|
std::panic::set_hook(quiet);
|
||
|
|
assert_eq!(literal.as_deref(), Some("literal"));
|
||
|
|
assert_eq!(owned.as_deref(), Some("formatted"));
|
||
|
|
assert_eq!(other.as_deref(), Some("an unknown error"));
|
||
|
|
}
|
||
|
|
|
||
|
|
#[test]
|
||
|
|
fn silence_waits_past_the_limit_and_never_while_a_question_waits() {
|
||
|
|
let s = setup(); // 20 minutes
|
||
|
|
assert!(!silence_expired(at(120), at(100), &s, None, false));
|
||
|
|
assert!(silence_expired(at(121), at(100), &s, None, false));
|
||
|
|
// A waiting question has no time limit.
|
||
|
|
assert!(!silence_expired(at(1000), at(0), &s, None, true));
|
||
|
|
// The skill's own quiet minutes stretch the limit, never shrink it.
|
||
|
|
assert!(!silence_expired(at(140), at(100), &s, Some(45), false));
|
||
|
|
assert!(silence_expired(at(146), at(100), &s, Some(45), false));
|
||
|
|
assert!(silence_expired(at(121), at(100), &s, Some(5), false));
|
||
|
|
// Capped at 60 minutes whatever either side asks for.
|
||
|
|
assert!(silence_expired(at(161), at(100), &s, Some(500), false));
|
||
|
|
let mut long = setup();
|
||
|
|
long.silence_minutes = 60;
|
||
|
|
assert!(!silence_expired(at(160), at(100), &long, Some(90), false));
|
||
|
|
assert!(silence_expired(at(161), at(100), &long, Some(90), false));
|
||
|
|
// A clock that went backwards is not silence.
|
||
|
|
assert!(!silence_expired(at(100), at(200), &s, None, false));
|
||
|
|
}
|
||
|
|
}
|