use crate::Guard;
use fancy_regex::Regex;
use jsrt::sync;
use serde_json::{Value, json};
use std::collections::{BTreeMap, HashMap, HashSet};
use std::path::{Component, Path, PathBuf};
use std::sync::mpsc::{Sender, channel};
use std::sync::{Arc, Condvar, LazyLock, Mutex, PoisonError};
use std::time::Duration;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Verdict {
Block(String),
Allow(Option<String>),
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct CellChanges {
pub files: Vec<PathBuf>,
pub checked: Vec<PathBuf>,
pub deleted: Vec<PathBuf>,
pub complete: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AfterOutcome {
Clean,
Advisory,
Findings,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AfterCellReport {
pub message: Option<String>,
pub outcome: AfterOutcome,
}
pub trait FileWriteGuard: Send + Sync {
fn name(&self) -> &str;
fn enabled(&self) -> bool {
true
}
fn checks_writes(&self) -> bool {
true
}
fn checks_shell(&self) -> bool {
true
}
fn timeout(&self) -> Option<Duration> {
Some(DEFAULT_BEFORE_WRITE_TIMEOUT)
}
fn before_write(&self, _path: &Path, _content: &str, _cwd: &Path, _lane: &str) -> Result<Verdict, String> {
Ok(Verdict::Allow(None))
}
fn before_shell(&self, _command: &str, _cwd: &Path) -> Result<Verdict, String> {
Ok(Verdict::Allow(None))
}
fn watches_cells(&self) -> bool {
false
}
fn after_cell_changes(
&self,
_changes: &CellChanges,
_cwd: &Path,
_lane: &str,
) -> Result<Option<AfterCellReport>, String> {
Ok(None)
}
fn after_timeout(&self) -> Duration {
DEFAULT_AFTER_CELL_TIMEOUT
}
}
pub trait CellObserver: Send + Sync {
fn enabled(&self) -> bool {
true
}
fn cell_changed(&self, changes: &CellChanges, cwd: &Path, lane: &str) -> Vec<String>;
fn before_shell(&self, _command: &str, _lane: &str) -> Option<String> {
None
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Snapshot {
pub root: PathBuf,
pub entries: BTreeMap<String, String>,
pub complete: bool,
}
pub type Snapshotter = Arc<dyn Fn(&Path) -> Result<Snapshot, String> + Send + Sync>;
pub const OUTSIDE_WAIT: Duration = Duration::from_secs(60);
#[derive(Clone, Default)]
pub struct OutsideChanges {
state: Arc<(Mutex<OutsideState>, Condvar)>,
}
#[derive(Default)]
struct OutsideState {
running: usize,
after: HashMap<PathBuf, Option<String>>,
}
pub struct OutsideWork {
changes: OutsideChanges,
}
impl OutsideChanges {
pub fn begin(&self) -> OutsideWork {
sync::lock(&self.state.0).running += 1;
OutsideWork { changes: self.clone() }
}
fn wait(&self, limit: Duration) {
let (state, ended) = &*self.state;
let mut state = sync::lock(state);
let end = std::time::Instant::now() + limit;
while state.running > 0 {
let left = end.saturating_duration_since(std::time::Instant::now());
if left.is_zero() {
return;
}
state = ended
.wait_timeout(state, left)
.unwrap_or_else(PoisonError::into_inner)
.0;
}
}
fn explains(&self, path: &Path, signature: Option<&String>) -> bool {
sync::lock(&self.state.0)
.after
.get(path)
.is_some_and(|left| left.as_ref() == signature)
}
}
impl OutsideWork {
pub fn record<R>(self, root: &Path, snapshot: &Snapshotter, work: impl FnOnce() -> R) -> R {
let before = snapshot(root);
let result = work();
if let (Ok(before), Ok(after)) = (before, snapshot(root)) {
let (added, modified, deleted, _) = diff_snapshots(&before, &after);
let mut state = sync::lock(&self.changes.state.0);
for relative in added.iter().chain(&modified) {
let signature = after.entries.get(relative).cloned();
state.after.insert(resolve_lexical(&after.root, relative), signature);
}
for relative in &deleted {
state.after.insert(resolve_lexical(&after.root, relative), None);
}
}
result
}
}
impl Drop for OutsideWork {
fn drop(&mut self) {
let (state, ended) = &*self.changes.state;
let mut state = sync::lock(state);
state.running = state.running.saturating_sub(1);
ended.notify_all();
}
}
pub const DEFAULT_AFTER_CELL_TIMEOUT: Duration = Duration::from_secs(120);
pub const DEFAULT_BEFORE_WRITE_TIMEOUT: Duration = Duration::from_millis(5_000);
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct GuardStats {
pub name: String,
pub checks: u64,
pub blocked: u64,
pub unchecked: u64,
pub after_checks: u64,
pub after_findings: u64,
pub total_ms: u64,
pub turn_ms: u64,
}
impl GuardStats {
pub fn to_json(&self) -> Value {
json!({
"name": self.name,
"checks": self.checks,
"blocked": self.blocked,
"unchecked": self.unchecked,
"afterChecks": self.after_checks,
"afterFindings": self.after_findings,
"totalMs": self.total_ms,
"turnMs": self.turn_ms,
})
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GuardRecord {
pub guard: String,
pub phase: &'static str,
pub outcome: &'static str,
pub ms: u64,
pub paths: Vec<String>,
pub lane: String,
pub detail: Option<String>,
}
impl GuardRecord {
pub fn to_json(&self) -> Value {
let mut record = json!({
"guard": self.guard,
"phase": self.phase,
"outcome": self.outcome,
"ms": self.ms,
"paths": self.paths,
"lane": self.lane,
});
if let Some(detail) = &self.detail {
record["detail"] = json!(detail);
}
record
}
}
pub type OnRecord = Arc<dyn Fn(&GuardRecord) + Send + Sync>;
pub type Clock = Arc<dyn Fn() -> u64 + Send + Sync>;
fn js_seconds(timeout: Duration) -> String {
jsrt::number::to_string(jsrt::number::round(timeout.as_millis() as f64 / 100.0) / 10.0)
}
enum Answer<T> {
Value(T),
Error(String),
TimedOut,
}
fn timed<T: Send + 'static>(
timeout: Option<Duration>,
call: impl FnOnce() -> Result<T, String> + Send + 'static,
) -> Answer<T> {
let (sender, answer) = channel();
std::thread::spawn(move || {
let _ = sender.send(call());
});
let received = match timeout {
Some(timeout) => answer.recv_timeout(timeout).ok(),
None => answer.recv().ok(),
};
match received {
Some(Ok(value)) => Answer::Value(value),
Some(Err(error)) => Answer::Error(error),
None => Answer::TimedOut,
}
}
type Pending = std::sync::mpsc::Receiver<Answer<Verdict>>;
#[derive(Clone)]
struct Registered {
guard: Arc<dyn FileWriteGuard>,
stats: Arc<Mutex<GuardStats>>,
}
const MAX_PENDING_CHARS: usize = 6_000;
fn re(pattern: &str) -> Regex {
Regex::new(pattern).expect("static pattern")
}
static DEV_NULL: LazyLock<Regex> = LazyLock::new(|| re(r"[0-9]*>>?\s*/dev/null(?![A-Za-z0-9_])"));
static FD_DUP: LazyLock<Regex> = LazyLock::new(|| re(r"[0-9]*>&[0-9]+"));
static WRITERS: LazyLock<[Regex; 5]> = LazyLock::new(|| {
[
re(r"(^|[^<>-])>{1,2}\|?(?!&)"),
re(r"(^|[\s;&|(])(tee|cp|mv|install)\s"),
re(r"(^|[\s;&|(])sed\s+(?:[^|;&]*\s)?(-i|--in-place)"),
re(r"(^|[\s;&|(])perl\s+(?:[^|;&]*\s)?-[a-zA-Z]*i"),
re(r"(^|[\s;&|(])dd\s[^|;&]*\bof="),
]
});
pub fn may_write_files(command: &str) -> bool {
let text = DEV_NULL.replace_all(command, " ");
let text = FD_DUP.replace_all(&text, " ");
WRITERS
.iter()
.any(|pattern| pattern.is_match(text.as_ref()).unwrap_or(false))
}
pub fn resolve_lexical(cwd: &Path, path: &str) -> PathBuf {
let mut out = PathBuf::from("/");
for component in cwd.join(path).components() {
match component {
Component::ParentDir => {
out.pop();
}
Component::Normal(part) => out.push(part),
_ => {}
}
}
out
}
pub fn inside(from: &Path, to: &Path) -> Option<String> {
let relative = to.strip_prefix(from).ok()?.to_string_lossy().into_owned();
(!relative.is_empty() && !relative.starts_with("..")).then_some(relative)
}
#[derive(Clone)]
pub struct FileHooks {
inner: Arc<Inner>,
}
struct Inner {
cwd: PathBuf,
guards: Mutex<Vec<Registered>>,
observers: Mutex<Vec<Arc<dyn CellObserver>>>,
on_record: Mutex<Option<OnRecord>>,
now: Mutex<Clock>,
snapshot: Option<Snapshotter>,
checked: Mutex<HashMap<PathBuf, String>>,
pending: Mutex<HashMap<String, Vec<String>>>,
latest: Mutex<Option<Snapshot>>,
baselines: Mutex<HashMap<String, Snapshot>>,
claimed: Mutex<HashMap<String, HashMap<String, Option<String>>>>,
live: Mutex<HashSet<String>>,
outside: Mutex<Option<OutsideChanges>>,
protected: Mutex<Vec<Protected>>,
queued: Mutex<usize>,
idle: Condvar,
worker: Mutex<Option<Sender<CellEvent>>>,
}
enum CellEvent {
Started(String, Option<Snapshot>),
Ended(String),
}
struct Protected {
path: PathBuf,
named: String,
}
const PROTECTOR: &str = "Ultron";
pub fn diff_snapshots(before: &Snapshot, after: &Snapshot) -> (Vec<String>, Vec<String>, Vec<String>, bool) {
let mut added = Vec::new();
let mut modified = Vec::new();
for (path, value) in &after.entries {
match before.entries.get(path) {
None => added.push(path.clone()),
Some(previous) if previous != value => modified.push(path.clone()),
Some(_) => {}
}
}
let deleted = before
.entries
.keys()
.filter(|path| !after.entries.contains_key(*path))
.cloned()
.collect();
(added, modified, deleted, before.complete && after.complete)
}
fn js_cut(text: &str, limit: usize) -> (String, usize) {
let units: Vec<u16> = text.encode_utf16().collect();
if units.len() <= limit {
return (text.to_string(), units.len());
}
(String::from_utf16_lossy(&units[..limit]), units.len())
}
impl FileHooks {
pub fn new(cwd: impl Into<PathBuf>) -> Self {
FileHooks {
inner: Arc::new(Inner {
cwd: cwd.into(),
guards: Mutex::new(Vec::new()),
observers: Mutex::new(Vec::new()),
on_record: Mutex::new(None),
now: Mutex::new({
let started = std::time::Instant::now();
Arc::new(move || started.elapsed().as_millis() as u64)
}),
snapshot: None,
checked: Mutex::new(HashMap::new()),
pending: Mutex::new(HashMap::new()),
latest: Mutex::new(None),
baselines: Mutex::new(HashMap::new()),
claimed: Mutex::new(HashMap::new()),
live: Mutex::new(HashSet::new()),
outside: Mutex::new(None),
protected: Mutex::new(Vec::new()),
queued: Mutex::new(0),
idle: Condvar::new(),
worker: Mutex::new(None),
}),
}
}
pub fn with_snapshots(cwd: impl Into<PathBuf>, snapshot: Snapshotter) -> Self {
let mut hooks = FileHooks::new(cwd);
if let Some(inner) = Arc::get_mut(&mut hooks.inner) {
inner.snapshot = Some(snapshot);
}
hooks
}
pub fn add(&mut self, guard: Box<dyn FileWriteGuard>) {
let stats = GuardStats {
name: guard.name().to_string(),
..GuardStats::default()
};
sync::lock(&self.inner.guards).push(Registered {
guard: Arc::from(guard),
stats: Arc::new(Mutex::new(stats)),
});
}
pub fn protect(&self, paths: Vec<PathBuf>, home: &Path) {
let mut protected = Vec::new();
for path in paths {
let named = path.strip_prefix(home).unwrap_or(&path).to_string_lossy().into_owned();
if let Ok(real) = std::fs::canonicalize(&path).or_else(|_| {
let parent = std::fs::canonicalize(path.parent().unwrap_or(&path))?;
Ok::<_, std::io::Error>(parent.join(path.file_name().unwrap_or_default()))
}) && real != path
{
protected.push(Protected {
path: real,
named: named.clone(),
});
}
protected.push(Protected { path, named });
}
*sync::lock(&self.inner.protected) = protected;
}
fn protected_by(&self, path: &Path) -> Option<PathBuf> {
let real = path
.ancestors()
.find_map(|existing| {
let resolved = std::fs::canonicalize(existing).ok()?;
Some(resolved.join(path.strip_prefix(existing).ok()?))
})
.unwrap_or_else(|| path.to_path_buf());
sync::lock(&self.inner.protected)
.iter()
.find(|p| {
[path, real.as_path()]
.iter()
.any(|candidate| candidate.starts_with(&p.path))
})
.map(|p| p.path.clone())
}
fn protected_in_command(&self, command: &str) -> Option<PathBuf> {
sync::lock(&self.inner.protected)
.iter()
.find(|p| command.contains(&p.named))
.map(|p| p.path.clone())
}
pub fn observe(&self, observer: Arc<dyn CellObserver>) {
sync::lock(&self.inner.observers).push(observer);
}
pub fn ignore_outside(&self, changes: OutsideChanges) {
*sync::lock(&self.inner.outside) = Some(changes);
}
pub fn on_record(&self, on_record: OnRecord) {
*sync::lock(&self.inner.on_record) = Some(on_record);
}
pub fn set_clock(&self, now: Clock) {
*sync::lock(&self.inner.now) = now;
}
pub fn begin_turn(&self) {
for registered in sync::lock(&self.inner.guards).iter() {
sync::lock(®istered.stats).turn_ms = 0;
}
}
pub fn stats(&self) -> Vec<GuardStats> {
sync::lock(&self.inner.guards)
.iter()
.map(|registered| sync::lock(®istered.stats).clone())
.collect()
}
pub fn watches_cells(&self) -> bool {
self.inner.watches_cells()
}
pub fn cell_started(&self, lane: &str) {
let inner = &self.inner;
if !inner.watches_cells() {
return;
}
let concurrent = {
let mut live = sync::lock(&inner.live);
live.insert(lane.to_string());
live.len() > 1
};
let picture = (concurrent || sync::lock(&inner.latest).is_none())
.then(|| inner.picture())
.flatten();
self.send(CellEvent::Started(lane.to_string(), picture));
}
pub fn cell_ended(&self, lane: &str) {
let inner = &self.inner;
let watched = sync::lock(&inner.live).remove(lane);
if !inner.watches_cells() {
return;
}
if !watched {
if sync::lock(&inner.latest).is_none() {
let picture = inner.picture();
let mut latest = sync::lock(&inner.latest);
if latest.is_none() {
*latest = picture;
}
return;
}
self.send(CellEvent::Started(lane.to_string(), None));
}
self.send(CellEvent::Ended(lane.to_string()));
}
fn send(&self, event: CellEvent) {
let inner = &self.inner;
*sync::lock(&inner.queued) += 1;
let mut worker = sync::lock(&inner.worker);
let sender = worker.get_or_insert_with(|| {
let (sender, events) = channel::<CellEvent>();
let inner = Arc::downgrade(&self.inner);
std::thread::spawn(move || {
for event in events {
let Some(inner) = inner.upgrade() else {
return;
};
match event {
CellEvent::Started(lane, picture) => inner.started(lane, picture),
CellEvent::Ended(lane) => inner.after_cell(&lane),
}
let mut queued = sync::lock(&inner.queued);
*queued -= 1;
inner.idle.notify_all();
}
});
sender
});
if sender.send(event).is_err() {
*sync::lock(&inner.queued) -= 1;
}
}
pub fn settled(&self) {
let mut queued = sync::lock(&self.inner.queued);
while *queued > 0 {
queued = self.inner.idle.wait(queued).unwrap_or_else(PoisonError::into_inner);
}
}
pub fn take_pending(&self, lane: &str) -> Option<String> {
let pending = sync::lock(&self.inner.pending).remove(lane)?;
if pending.is_empty() {
return None;
}
let text = pending.join("\n");
let (head, total) = js_cut(&text, MAX_PENDING_CHARS);
Some(if total > MAX_PENDING_CHARS {
format!("{head}\n… ({} more characters of findings)", total - MAX_PENDING_CHARS)
} else {
text
})
}
fn display(&self, path: &Path) -> String {
self.inner.display(path)
}
fn refuse_protected(&self, lane: &str, phase: &'static str, protected: &Path) -> String {
let reason = format!(
"[{PROTECTOR}] {} is the user's to change (the profile's settings, keys and skills): a cell may not write \
it. Ask the user to make the change.",
protected.display()
);
self.inner.record(GuardRecord {
guard: PROTECTOR.into(),
phase,
outcome: "blocked",
ms: 0,
paths: vec![protected.display().to_string()],
lane: lane.to_string(),
detail: Some(reason.clone()),
});
reason
}
fn guards(&self) -> Vec<Registered> {
sync::lock(&self.inner.guards).clone()
}
}
impl Inner {
fn watching(guard: &dyn FileWriteGuard) -> bool {
guard.enabled() && guard.watches_cells()
}
fn watches_cells(&self) -> bool {
self.snapshot.is_some()
&& (sync::lock(&self.guards).iter().any(|r| Inner::watching(&*r.guard))
|| sync::lock(&self.observers).iter().any(|observer| observer.enabled()))
}
fn now(&self) -> u64 {
let clock = sync::lock(&self.now).clone();
clock()
}
fn record(&self, record: GuardRecord) {
let on_record = sync::lock(&self.on_record).clone();
if let Some(on_record) = on_record {
on_record(&record);
}
}
fn display(&self, path: &Path) -> String {
inside(&self.cwd, path).unwrap_or_else(|| path.to_string_lossy().into_owned())
}
fn picture(&self) -> Option<Snapshot> {
self.snapshot.as_ref().and_then(|snapshot| snapshot(&self.cwd).ok())
}
fn started(&self, lane: String, picture: Option<Snapshot>) {
let mut latest = sync::lock(&self.latest);
let Some(baseline) = picture.or_else(|| latest.clone()) else {
return;
};
latest.get_or_insert_with(|| baseline.clone());
sync::lock(&self.claimed).remove(&lane);
sync::lock(&self.baselines).insert(lane, baseline);
}
fn after_cell(&self, lane: &str) {
let before = sync::lock(&self.baselines).remove(lane);
let outside = sync::lock(&self.outside).clone();
if let Some(outside) = &outside {
outside.wait(OUTSIDE_WAIT);
}
let after = self.picture();
if let Some(after) = &after {
*sync::lock(&self.latest) = Some(after.clone());
}
let (Some(before), Some(after)) = (before, after) else {
return;
};
if before.root != after.root {
return;
}
let (mut added, mut modified, mut deleted, complete) = diff_snapshots(&before, &after);
{
let mut claimed = sync::lock(&self.claimed);
if let Some(theirs) = claimed.remove(lane) {
let unchanged = |relative: &String| theirs.get(relative) == Some(&after.entries.get(relative).cloned());
added.retain(|relative| !unchanged(relative));
modified.retain(|relative| !unchanged(relative));
deleted.retain(|relative| !unchanged(relative));
}
for other in sync::lock(&self.baselines).keys() {
let theirs = claimed.entry(other.clone()).or_default();
for relative in added.iter().chain(&modified).chain(&deleted) {
theirs.insert(relative.clone(), after.entries.get(relative).cloned());
}
}
}
if let Some(outside) = &outside {
let theirs = |relative: &String| {
outside.explains(&resolve_lexical(&after.root, relative), after.entries.get(relative))
};
added.retain(|relative| !theirs(relative));
modified.retain(|relative| !theirs(relative));
deleted.retain(|relative| !theirs(relative));
}
let elsewhere: Vec<PathBuf> = {
let mut checked = sync::lock(&self.checked);
let outside: Vec<PathBuf> = checked
.keys()
.filter(|p| inside(&after.root, p).is_none())
.cloned()
.collect();
outside
.into_iter()
.filter(|path| {
let approved = checked.remove(path);
approved.is_some_and(|approved| {
std::fs::read(path).is_ok_and(|now| jsrt::hash::sha256_hex(&now) == approved)
})
})
.collect()
};
if added.is_empty() && modified.is_empty() && deleted.is_empty() && elsewhere.is_empty() {
return;
}
let mut changes = CellChanges {
complete,
checked: elsewhere,
..CellChanges::default()
};
for relative in added.iter().chain(&modified) {
let path = resolve_lexical(&after.root, relative);
let approved = sync::lock(&self.checked).remove(&path);
if let Some(approved) = approved
&& std::fs::read(&path).is_ok_and(|current| jsrt::hash::sha256_hex(¤t) == approved)
{
changes.checked.push(path);
continue;
}
changes.files.push(path);
}
for relative in &deleted {
let path = resolve_lexical(&after.root, relative);
sync::lock(&self.checked).remove(&path);
changes.deleted.push(path);
}
let guards: Vec<Registered> = sync::lock(&self.guards)
.iter()
.filter(|r| Inner::watching(&*r.guard))
.cloned()
.collect();
let paths: Vec<String> = changes
.files
.iter()
.chain(&changes.checked)
.map(|path| self.display(path))
.collect();
let changes = Arc::new(changes);
let calls: Vec<_> = guards
.into_iter()
.map(|registered| {
let started = self.now();
let (task, cwd, lane_name, checked) = (
registered.guard.clone(),
self.cwd.clone(),
lane.to_string(),
changes.clone(),
);
let limit = registered.guard.after_timeout();
let (sender, receiver) = channel();
std::thread::spawn(move || {
let _ = sender.send(timed(Some(limit), move || {
task.after_cell_changes(&checked, &cwd, &lane_name)
}));
});
(registered, started, receiver)
})
.collect();
let mut found = Vec::new();
for (registered, started, receiver) in calls {
let answer = receiver.recv().unwrap_or(Answer::TimedOut);
let ms = self.now().saturating_sub(started);
let name = registered.guard.name().to_string();
let (text, outcome) = match answer {
Answer::TimedOut => (
Some(format!("{name} did not finish checking the files this cell changed.")),
"unchecked",
),
Answer::Error(error) => (
Some(format!("{name} could not check the files this cell changed: {error}")),
"unchecked",
),
Answer::Value(None) => (None, "clean"),
Answer::Value(Some(report)) => (
report.message.filter(|m| !m.is_empty()),
match report.outcome {
AfterOutcome::Clean => "clean",
AfterOutcome::Advisory => "advisory",
AfterOutcome::Findings => "findings",
},
),
};
{
let mut stats = sync::lock(®istered.stats);
stats.total_ms += ms;
stats.turn_ms += ms;
stats.after_checks += 1;
if matches!(outcome, "findings" | "unchecked") {
stats.after_findings += 1;
}
}
self.record(GuardRecord {
guard: name.clone(),
phase: "after_cell",
outcome,
ms,
paths: paths.clone(),
lane: lane.to_string(),
detail: text.clone(),
});
if let Some(text) = text {
found.push(format!("[{name}] {text}"));
}
}
let observers: Vec<Arc<dyn CellObserver>> = sync::lock(&self.observers)
.iter()
.filter(|observer| observer.enabled())
.cloned()
.collect();
for observer in observers {
found.extend(observer.cell_changed(&changes, &self.cwd, lane));
}
if !found.is_empty() {
sync::lock(&self.pending)
.entry(lane.to_string())
.or_default()
.extend(found);
}
}
}
impl Guard for FileHooks {
fn before_shell(&self, lane: &str, command: &str) -> Option<String> {
let observers: Vec<Arc<dyn CellObserver>> = sync::lock(&self.inner.observers)
.iter()
.filter(|observer| observer.enabled())
.cloned()
.collect();
if let Some(reason) = observers
.iter()
.find_map(|observer| observer.before_shell(command, lane))
{
return Some(reason);
}
if let Some(path) = may_write_files(command)
.then(|| self.protected_in_command(command))
.flatten()
{
return Some(self.refuse_protected(lane, "before_shell", &path));
}
let guards: Vec<Registered> = self
.guards()
.into_iter()
.filter(|r| r.guard.checks_shell() && r.guard.enabled())
.collect();
if guards.is_empty() || !may_write_files(command) {
return None;
}
let display = if command.encode_utf16().count() > 80 {
format!("{}...", js_cut(command, 77).0)
} else {
command.to_string()
};
let calls: Vec<(Registered, u64, Pending)> = guards
.into_iter()
.map(|registered| {
let started = self.inner.now();
let timeout = registered.guard.timeout();
let (guard, command, cwd) = (registered.guard.clone(), command.to_string(), self.inner.cwd.clone());
let (sender, receiver) = channel();
std::thread::spawn(move || {
let _ = sender.send(timed(timeout, move || guard.before_shell(&command, &cwd)));
});
(registered, started, receiver)
})
.collect();
let mut reasons = Vec::new();
for (registered, started, receiver) in calls {
let answer = receiver.recv().unwrap_or(Answer::TimedOut);
let ms = self.inner.now().saturating_sub(started);
let name = registered.guard.name().to_string();
let mut stats = sync::lock(®istered.stats);
stats.total_ms += ms;
stats.turn_ms += ms;
stats.checks += 1;
let (outcome, reason) = match answer {
Answer::TimedOut | Answer::Error(_) => {
stats.unchecked += 1;
("unchecked", None)
}
Answer::Value(Verdict::Block(reason)) => {
stats.blocked += 1;
("blocked", Some(format!("[{name}] {reason}")))
}
Answer::Value(Verdict::Allow(_)) => ("allowed", None),
};
drop(stats);
self.inner.record(GuardRecord {
guard: name,
phase: "before_shell",
outcome,
ms,
paths: vec![display.clone()],
lane: lane.to_string(),
detail: reason.clone(),
});
reasons.extend(reason);
}
(!reasons.is_empty()).then(|| reasons.join("\n"))
}
fn before_write(&self, lane: &str, path: &str, content: &str) -> Value {
let path = resolve_lexical(&self.inner.cwd, path);
if let Some(protected) = self.protected_by(&path) {
let reason = self.refuse_protected(lane, "before_write", &protected);
return json!({ "blocked": true, "reason": reason, "notes": [] });
}
let display = self.display(&path);
let calls: Vec<(Registered, u64, Option<Duration>, Pending)> = self
.guards()
.into_iter()
.filter(|r| r.guard.checks_writes() && r.guard.enabled())
.map(|registered| {
let started = self.inner.now();
let timeout = registered.guard.timeout();
let (guard, path, content, cwd, lane) = (
registered.guard.clone(),
path.clone(),
content.to_string(),
self.inner.cwd.clone(),
lane.to_string(),
);
let (sender, receiver) = channel();
std::thread::spawn(move || {
let _ = sender.send(timed(timeout, move || guard.before_write(&path, &content, &cwd, &lane)));
});
(registered, started, timeout, receiver)
})
.collect();
let mut reasons = Vec::new();
let mut notes = Vec::new();
for (registered, started, timeout, receiver) in calls {
let answer = receiver.recv().unwrap_or(Answer::TimedOut);
let ms = self.inner.now().saturating_sub(started);
let name = registered.guard.name().to_string();
let mut stats = sync::lock(®istered.stats);
stats.total_ms += ms;
stats.turn_ms += ms;
stats.checks += 1;
let (outcome, reason, note) = match answer {
Answer::TimedOut => {
stats.unchecked += 1;
let limit = js_seconds(timeout.unwrap_or_default());
let note = format!(
"[{name}] {name} did not check this write to {display} (no answer within {limit} s); it was written unchecked."
);
("unchecked", None, Some(note))
}
Answer::Error(error) => {
stats.unchecked += 1;
let note = format!(
"[{name}] {name} did not check this write to {display} ({error}); it was written unchecked."
);
("unchecked", None, Some(note))
}
Answer::Value(Verdict::Block(reason)) => {
stats.blocked += 1;
("blocked", Some(format!("[{name}] {reason}")), None)
}
Answer::Value(Verdict::Allow(Some(message))) if !message.is_empty() => {
("allowed", None, Some(format!("[{name}] {message}")))
}
Answer::Value(Verdict::Allow(_)) => ("allowed", None, None),
};
drop(stats);
self.inner.record(GuardRecord {
guard: name,
phase: "before_write",
outcome,
ms,
paths: vec![display.clone()],
lane: lane.to_string(),
detail: reason.clone(),
});
reasons.extend(reason);
notes.extend(note);
}
if reasons.is_empty() {
sync::lock(&self.inner.checked).insert(path, jsrt::hash::sha256_hex(content.as_bytes()));
json!({ "blocked": false, "notes": notes })
} else {
json!({ "blocked": true, "reason": reasons.join("\n"), "notes": notes })
}
}
fn guard_stats(&self) -> Vec<Value> {
self.stats().iter().map(GuardStats::to_json).collect()
}
fn begin_turn(&self) {
FileHooks::begin_turn(self);
}
fn cell_started(&self, lane: &str) {
FileHooks::cell_started(self, lane);
}
fn cell_ended(&self, lane: &str) {
FileHooks::cell_ended(self, lane);
}
fn take_pending(&self, lane: &str) -> Option<String> {
FileHooks::take_pending(self, lane)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn host_inside_matches_path_relative() {
let root = Path::new("/r");
assert_eq!(inside(root, Path::new("/r/a/b.txt")).as_deref(), Some("a/b.txt"));
assert_eq!(inside(root, Path::new("/r")), None);
assert_eq!(inside(root, Path::new("/rx/a")), None);
assert_eq!(inside(root, Path::new("/r/..x")), None);
assert_eq!(resolve_lexical(root, "a/../b/./c"), Path::new("/r/b/c"));
assert_eq!(resolve_lexical(root, "/abs/../x"), Path::new("/x"));
}
}