use crate::file_hooks::{AfterCellReport, AfterOutcome, CellChanges, FileWriteGuard, Verdict, inside};
use loki::CheckError;
use loki::run::Report;
use serde_json::{Map, Value};
use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use std::sync::Mutex;
use std::sync::atomic::{AtomicBool, Ordering};
const MAX_HOOK_FILES: usize = 40;
fn count(n: usize, noun: &str) -> String {
format!("{n} {noun}{}", if n == 1 { "" } else { "s" })
}
fn loki_file_as_committed(root: &Path, relative: &str) -> bool {
if !relative.starts_with(".loki/") {
return false;
}
let git = |args: &[&str]| {
Command::new("git")
.arg("-C")
.arg(root)
.args(args)
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null())
.status()
.is_ok_and(|status| status.success())
};
git(&["ls-files", "--error-unmatch", "--", relative]) && git(&["diff", "--quiet", "HEAD", "--", relative])
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LokiMode {
On,
Advise,
Off,
}
const OFF_VALUES: [&str; 5] = ["off", "0", "false", "no", "none"];
pub fn loki_timeout(value: Option<&str>) -> Option<std::time::Duration> {
let n: f64 = value?.trim().parse().ok()?;
(jsrt::number::is_safe_integer(n) && n > 0.0).then(|| std::time::Duration::from_millis(n as u64))
}
pub fn loki_mode(env: Option<&str>, settings: &Map<String, Value>) -> LokiMode {
let raw = env.map(|v| v.trim().to_lowercase()).filter(|v| !v.is_empty());
match raw.as_deref() {
Some(v) if OFF_VALUES.contains(&v) => LokiMode::Off,
Some("advise") => LokiMode::Advise,
Some(_) => LokiMode::On,
None => match settings.get("mode").and_then(Value::as_str) {
Some("off") => LokiMode::Off,
Some("advise") => LokiMode::Advise,
_ => LokiMode::On,
},
}
}
pub fn git_root(cwd: &Path) -> Option<PathBuf> {
let output = Command::new("git")
.args(["rev-parse", "--show-toplevel"])
.current_dir(cwd)
.env("GIT_OPTIONAL_LOCKS", "0")
.stdin(Stdio::null())
.stderr(Stdio::null())
.output()
.ok()?;
let root = String::from_utf8_lossy(&output.stdout).trim().to_string();
(output.status.success() && !root.is_empty()).then(|| PathBuf::from(root))
}
pub struct LokiGuard {
root: PathBuf,
mode: LokiMode,
shown_notices: Mutex<Vec<String>>,
open: Mutex<HashMap<&'static str, HashMap<String, HashSet<String>>>>,
paused_shown: AtomicBool,
hook: HookRunner,
post_write: bool,
timeout: Option<std::time::Duration>,
hygiene: loki::hygiene::Level,
}
pub type HookRunner = std::sync::Arc<dyn Fn(&Path, &[String]) -> Result<Report, CheckError> + Send + Sync>;
struct Unseen {
fresh: Vec<String>,
earlier: usize,
paths: Vec<String>,
}
impl LokiGuard {
pub fn new(root: impl Into<PathBuf>, mode: LokiMode) -> Self {
LokiGuard {
root: root.into(),
mode,
shown_notices: Mutex::new(Vec::new()),
open: Mutex::new(HashMap::new()),
paused_shown: AtomicBool::new(false),
hook: std::sync::Arc::new(|root: &Path, files: &[String]| loki::run::hook(root, files, None)),
post_write: true,
timeout: None,
hygiene: loki::hygiene::Level::Off,
}
}
pub fn with_hygiene(mut self, level: loki::hygiene::Level) -> Self {
self.hygiene = level;
self
}
pub fn with_timeout(mut self, timeout: Option<std::time::Duration>) -> Self {
self.timeout = timeout;
self
}
pub fn with_post_write(mut self, post_write: bool) -> Self {
self.post_write = post_write;
self
}
pub fn with_project_python(mut self, python: Option<String>) -> Self {
self.hook =
std::sync::Arc::new(move |root: &Path, files: &[String]| loki::run::hook(root, files, python.as_deref()));
self
}
pub fn with_hook(mut self, hook: HookRunner) -> Self {
self.hook = hook;
self
}
fn unseen(&self, tier: &'static str, lines: &[String], checked: &[String]) -> Unseen {
let mut open = self.open.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
let memory = open.entry(tier).or_default();
let mut now: Vec<(String, HashSet<String>)> = Vec::new();
let mut fresh = Vec::new();
let mut stale: Vec<String> = Vec::new();
for line in lines {
let (path, key) = loki::finding_key(&self.root, line);
if !path.is_empty() && memory.get(&path).is_some_and(|keys| keys.contains(&key)) {
if !stale.contains(&path) {
stale.push(path.clone());
}
} else {
fresh.push(line.clone());
}
if !path.is_empty() {
match now.iter_mut().find(|(p, _)| *p == path) {
Some((_, keys)) => {
keys.insert(key);
}
None => now.push((path, HashSet::from([key]))),
}
}
}
for path in checked {
memory.remove(path);
}
for (path, keys) in now {
memory.insert(path, keys);
}
Unseen {
earlier: lines.len() - fresh.len(),
fresh,
paths: stale,
}
}
fn notice(&self, lines: &[String]) -> Option<String> {
let mut shown = self
.shown_notices
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
let mut fresh = Vec::new();
for line in lines {
if !shown.contains(line) {
shown.push(line.clone());
fresh.push(
line.strip_prefix("NOT CHECKED")
.unwrap_or(line)
.trim_start()
.to_string(),
);
}
}
(!fresh.is_empty()).then(|| format!("not checked in this session (said once): {}", fresh.join("; ")))
}
fn refused(&self, reason: String, advise: &str) -> Verdict {
if self.mode == LokiMode::Advise {
Verdict::Allow(Some(format!("{advise}:\n{reason}")))
} else {
Verdict::Block(reason)
}
}
}
impl FileWriteGuard for LokiGuard {
fn name(&self) -> &str {
"Loki"
}
fn timeout(&self) -> Option<std::time::Duration> {
self.timeout
}
fn before_write(&self, path: &Path, content: &str, _cwd: &Path, _lane: &str) -> Result<Verdict, String> {
let (root, relative) = match inside(&self.root, path) {
Some(relative) => (self.root.clone(), relative),
None => {
let Some((root, relative)) = path
.ancestors()
.skip(1)
.find(|dir| dir.is_dir())
.and_then(git_root)
.filter(|root| root.join(".loki").is_dir())
.and_then(|root| inside(&root, path).map(|relative| (root, relative)))
else {
return Ok(Verdict::Allow(None));
};
(root, relative)
}
};
let report = match loki::run::protect_write(&root, &relative, content) {
Ok(report) => report,
Err(error) => {
return Ok(self.refused(
format!("loki: {}", error.describe()),
"would block this write (advise-only mode)",
));
}
};
if report.blocked() {
return Ok(self.refused(
report.refusal_lines().join("\n"),
"would block this write (advise-only mode)",
));
}
let not_checked: Vec<String> = report.not_checked().cloned().collect();
let mut lines: Vec<String> = report.advisory().cloned().collect();
lines.extend(self.notice(¬_checked));
Ok(Verdict::Allow((!lines.is_empty()).then(|| lines.join("\n"))))
}
fn watches_cells(&self) -> bool {
self.post_write && self.mode != LokiMode::Off
}
fn after_cell_changes(
&self,
changes: &CellChanges,
_cwd: &Path,
_lane: &str,
) -> Result<Option<AfterCellReport>, String> {
let mut targets: Vec<String> = Vec::new();
for path in changes.files.iter().chain(&changes.checked) {
if let Some(relative) = inside(&self.root, path)
&& !targets.contains(&relative)
&& !loki_file_as_committed(&self.root, &relative)
{
targets.push(relative);
}
}
if targets.is_empty() {
return Ok(None);
}
let checked: Vec<String> = targets.iter().take(MAX_HOOK_FILES).cloned().collect();
let report = (self.hook)(&self.root, &checked).map_err(|error| error.describe())?;
if report.blocked_by_policy_changes() {
if self.paused_shown.swap(true, Ordering::SeqCst) {
return Ok(None);
}
return Ok(Some(AfterCellReport {
outcome: AfterOutcome::Clean,
message: Some("post-write checks are paused: .loki/ has uncommitted changes, and Loki checks against the committed policy. Before-write checks of edit() and write() still run. Commit .loki/ to resume them (git add .loki && git commit -m 'Add Loki guardrails' -- .loki).".into()),
}));
}
let mut report = report;
if !report.blocked() {
report
.warnings
.extend(loki::hygiene::written(&self.root, &checked, self.hygiene));
}
let advisory: Vec<String> = report.advisory().cloned().collect();
let not_checked: Vec<String> = report.not_checked().cloned().collect();
let blocking = self.unseen("blocking", &report.blocking, &checked);
let advisory = self.unseen("advisory", &advisory, &checked);
let mut sections = Vec::new();
if !blocking.fresh.is_empty() {
sections.push(format!(
"{} from this cell's changes; fix these:\n{}",
count(blocking.fresh.len(), "new finding"),
blocking.fresh.join("\n")
));
}
if blocking.earlier > 0 {
sections.push(format!(
"{} reported earlier {} still open in {}.",
count(blocking.earlier, "finding"),
if blocking.earlier == 1 { "is" } else { "are" },
blocking.paths.join(", ")
));
}
if !advisory.fresh.is_empty() {
sections.push(format!(
"FYI (advisory, not blocking; no change required):\n{}",
advisory.fresh.join("\n")
));
}
sections.extend(self.notice(¬_checked));
if targets.len() > checked.len() {
sections.push(format!(
"({} more changed files were not checked)",
targets.len() - checked.len()
));
}
let outcome = if !report.blocking.is_empty() {
AfterOutcome::Findings
} else if !advisory.fresh.is_empty() {
AfterOutcome::Advisory
} else {
AfterOutcome::Clean
};
Ok((!sections.is_empty()).then(|| AfterCellReport {
outcome,
message: Some(sections.join("\n")),
}))
}
fn before_shell(&self, command: &str, cwd: &Path) -> Result<Verdict, String> {
Ok(match loki::shell::command_refusal(&self.root, command, cwd) {
None => Verdict::Allow(None),
Some(reason) => self.refused(
reason.trim().to_string(),
"would refuse this command (advise-only mode)",
),
})
}
}