use crate::constants::paths::ENV_RAW_DUMP;
use anyhow::{Context, Result};
use std::fs;
use std::path::PathBuf;
use super::temp::create_ralph_temp_dir;
pub fn safeguard_text_dump_redacted(label: &str, content: &str) -> Result<PathBuf> {
use crate::redaction::redact_text;
let redacted_content = redact_text(content);
safeguard_text_dump_internal(label, &redacted_content, true)
}
pub fn safeguard_text_dump(label: &str, content: &str, is_debug_mode: bool) -> Result<PathBuf> {
let raw_dump_enabled = std::env::var(ENV_RAW_DUMP)
.map(|v| v == "1" || v.eq_ignore_ascii_case("true"))
.unwrap_or(false);
if !raw_dump_enabled && !is_debug_mode {
anyhow::bail!(
"Raw safeguard dumps require explicit opt-in. \
Set {}=1 or use --debug mode. \
Consider using safeguard_text_dump_redacted() for safe dumping.",
ENV_RAW_DUMP
);
}
if raw_dump_enabled {
log::warn!(
"SECURITY: Writing raw safeguard dump ({}=1). Secrets may be written to disk.",
ENV_RAW_DUMP
);
}
safeguard_text_dump_internal(label, content, false)
}
fn safeguard_text_dump_internal(label: &str, content: &str, _is_redacted: bool) -> Result<PathBuf> {
let temp_dir = create_ralph_temp_dir(label)?;
let output_path = temp_dir.path().join("output.txt");
fs::write(&output_path, content)
.with_context(|| format!("write safeguard dump to {}", output_path.display()))?;
let dir_path = temp_dir.keep();
Ok(dir_path.join("output.txt"))
}