apoc 0.4.0

Agentic process control for LLMs
Documentation
//! Fail-closed managed-run log storage.

use std::io::{Read, Seek};
use std::path::Path;

const REDACTION_MARKER: &str = "redaction.required";

pub(crate) fn require_redaction(directory: &Path) -> Result<(), String> {
    std::fs::write(directory.join(REDACTION_MARKER), []).map_err(|error| error.to_string())
}

pub(crate) fn redaction_pending(path: Option<&str>) -> bool {
    path.and_then(|path| Path::new(path).parent())
        .is_some_and(|directory| directory.join(REDACTION_MARKER).exists())
}

pub(crate) fn finalize_redaction(paths: &[&Path], secrets: &[&String]) -> Result<(), String> {
    let marker = paths
        .first()
        .and_then(|path| path.parent())
        .map(|directory| directory.join(REDACTION_MARKER));
    let required = marker.as_ref().is_some_and(|path| path.exists());
    if !required {
        return Ok(());
    }
    if paths.iter().all(|path| !path.exists()) {
        if let Some(marker) = marker {
            std::fs::remove_file(marker).map_err(|error| error.to_string())?;
        }
        return Ok(());
    }

    let result = if secrets.iter().all(|secret| secret.is_empty()) {
        rewrite_or_remove(paths, |_| {
            Err("resolved secrets are unavailable after recovery".to_string())
        })
    } else {
        rewrite_or_remove(paths, |path| redact(path, secrets))
    };
    if (result.is_ok() || paths.iter().all(|path| !path.exists()))
        && let Some(marker) = marker
    {
        std::fs::remove_file(marker).map_err(|error| error.to_string())?;
    }
    result
}

pub(crate) fn read_tail(path: Option<&str>, bytes: usize) -> Result<(String, bool), String> {
    let Some(path) = path else {
        return Ok((String::new(), false));
    };
    let mut file = match std::fs::File::open(path) {
        Ok(file) => file,
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
            return Ok((String::new(), false));
        }
        Err(error) => return Err(error.to_string()),
    };
    let length = file.metadata().map_err(|error| error.to_string())?.len();
    file.seek(std::io::SeekFrom::Start(
        length.saturating_sub(bytes as u64),
    ))
    .map_err(|error| error.to_string())?;
    let mut value = Vec::new();
    file.read_to_end(&mut value)
        .map_err(|error| error.to_string())?;
    Ok((
        String::from_utf8_lossy(&value).into_owned(),
        length > bytes as u64,
    ))
}

fn redact(path: &Path, secrets: &[&String]) -> Result<(), String> {
    let bytes = match std::fs::read(path) {
        Ok(bytes) => bytes,
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(()),
        Err(error) => return Err(error.to_string()),
    };
    let mut output = String::from_utf8_lossy(&bytes).into_owned();
    for secret in secrets {
        if !secret.is_empty() {
            output = output.replace(secret.as_str(), "[REDACTED]");
        }
    }
    std::fs::write(path, output).map_err(|error| error.to_string())
}

fn rewrite_or_remove(
    paths: &[&Path],
    mut rewrite: impl FnMut(&Path) -> Result<(), String>,
) -> Result<(), String> {
    let errors = paths
        .iter()
        .filter_map(|path| {
            rewrite(path)
                .err()
                .map(|error| format!("{}: {error}", path.display()))
        })
        .collect::<Vec<_>>();
    if errors.is_empty() {
        return Ok(());
    }

    let cleanup_errors = paths
        .iter()
        .filter_map(|path| match std::fs::remove_file(path) {
            Ok(()) => None,
            Err(error) if error.kind() == std::io::ErrorKind::NotFound => None,
            Err(error) => Some(format!("{}: {error}", path.display())),
        })
        .collect::<Vec<_>>();
    if cleanup_errors.is_empty() {
        Err(errors.join("; "))
    } else {
        Err(format!(
            "{}; cleanup failed: {}",
            errors.join("; "),
            cleanup_errors.join("; ")
        ))
    }
}

#[cfg(test)]
mod tests {
    use tempfile::tempdir;

    use super::*;

    #[test]
    fn sanitizes_every_log() {
        let directory = tempdir().unwrap();
        let stdout = directory.path().join("stdout.log");
        let stderr = directory.path().join("stderr.log");
        let secret = "synthetic-secret".to_string();
        std::fs::write(&stdout, format!("before {secret} after")).unwrap();
        std::fs::write(&stderr, format!("{secret} twice {secret}")).unwrap();

        require_redaction(directory.path()).unwrap();
        finalize_redaction(&[&stdout, &stderr], &[&secret]).unwrap();

        assert_eq!(
            std::fs::read_to_string(stdout).unwrap(),
            "before [REDACTED] after"
        );
        assert_eq!(
            std::fs::read_to_string(stderr).unwrap(),
            "[REDACTED] twice [REDACTED]"
        );
        assert!(!directory.path().join(REDACTION_MARKER).exists());
    }

    #[test]
    fn missing_logs_complete_redaction() {
        let directory = tempdir().unwrap();
        let stdout = directory.path().join("stdout.log");
        let stderr = directory.path().join("stderr.log");
        require_redaction(directory.path()).unwrap();

        finalize_redaction(&[&stdout, &stderr], &[]).unwrap();

        assert!(!directory.path().join(REDACTION_MARKER).exists());
    }

    #[test]
    fn rewrite_failure_removes_every_log() {
        let directory = tempdir().unwrap();
        let stdout = directory.path().join("stdout.log");
        let stderr = directory.path().join("stderr.log");
        std::fs::write(&stdout, "secret").unwrap();
        std::fs::write(&stderr, "secret").unwrap();

        let error = rewrite_or_remove(&[&stdout, &stderr], |_| Err("synthetic failure".into()))
            .unwrap_err();

        assert!(error.contains("synthetic failure"));
        assert!(!stdout.exists());
        assert!(!stderr.exists());
    }

    #[test]
    fn recovery_without_secret_values_removes_every_log() {
        let directory = tempdir().unwrap();
        let stdout = directory.path().join("stdout.log");
        let stderr = directory.path().join("stderr.log");
        std::fs::write(&stdout, "secret").unwrap();
        std::fs::write(&stderr, "secret").unwrap();
        require_redaction(directory.path()).unwrap();

        let error = finalize_redaction(&[&stdout, &stderr], &[]).unwrap_err();

        assert!(error.contains("unavailable after recovery"));
        assert!(!stdout.exists());
        assert!(!stderr.exists());
        assert!(!directory.path().join(REDACTION_MARKER).exists());
    }
}