use std::collections::BTreeMap;
use super::source::{FILES, PackFiles};
use crate::RepoPath;
pub type Protected = BTreeMap<String, String>;
pub const INSTRUCTION_FILES: &[&str] = &["/AGENTS.md", "/CLAUDE.md"];
pub fn covers(written: &str, path: &str) -> bool {
let (written, path) = (written.to_lowercase(), path.to_lowercase());
if written.ends_with('/') {
path.starts_with(&written)
} else {
path == written
}
}
pub fn check(pack: &PackFiles, roots: &[RepoPath], protected: &Protected) -> Vec<String> {
pack.files
.iter()
.filter_map(|(rel, bytes)| refusal(rel, bytes, roots, protected))
.collect()
}
pub fn refusal(
rel: &str,
bytes: &[u8],
roots: &[RepoPath],
protected: &Protected,
) -> Option<String> {
if std::str::from_utf8(bytes).is_err() {
return Some(format!(
"{rel} is not UTF-8 text, and every file of a pack is text"
));
}
let rest = rel.strip_prefix(FILES)?;
let segments: Vec<&str> = rest.split('/').collect();
if segments
.iter()
.any(|s| s.is_empty() || *s == "." || *s == "..")
{
return Some(format!(
"{rel}: the path leaves its place in the repository"
));
}
let dest = format!("/{rest}");
let lower = dest.to_lowercase();
let first = segments[0].to_lowercase();
if first == ".sokf" || segments.iter().any(|s| s.eq_ignore_ascii_case(".git")) {
return Some(format!(
"{rel} lands at {dest}, among git's or sokf's own files, which no pack writes"
));
}
if let Some(root) = roots.iter().find(|r| {
let r = r.as_str().to_lowercase();
lower == r || lower.starts_with(&format!("{}/", r.trim_end_matches('/')))
}) {
return Some(format!(
"{rel} lands at {dest}, inside the knowledge root {root}; a file of a store belongs under schemas/ or documents/"
));
}
let instructions = INSTRUCTION_FILES
.iter()
.find(|p| covers(p, &dest))
.map(|_| "the instructions block of a harness");
let who = instructions.or_else(|| {
protected
.iter()
.find(|(p, _)| covers(p, &dest))
.map(|(_, who)| who.as_str())
})?;
Some(format!("{rel} lands at {dest}, which {who} writes"))
}
#[cfg(test)]
mod tests {
use std::collections::BTreeSet;
use super::*;
use crate::pack::PackManifest;
fn pack(files: &[(&str, &[u8])]) -> PackFiles {
PackFiles {
manifest: PackManifest {
name: "m".into(),
version: "1.0.0".into(),
requires_sokf: "0.1.0".into(),
description: "d".into(),
depends: BTreeMap::new(),
},
files: files
.iter()
.map(|(r, b)| (r.to_string(), b.to_vec()))
.collect(),
executable: BTreeSet::new(),
}
}
fn refused(files: &[(&str, &[u8])]) -> Vec<String> {
let roots = [RepoPath::parse("/knowledge").unwrap()];
let protected = BTreeMap::from([
("/.claude/settings.json".into(), "the claude harness".into()),
("/.claude/skills/sokf/".into(), "the claude harness".into()),
]);
check(&pack(files), &roots, &protected)
}
#[test]
fn a_file_lands_only_where_a_pack_may_write() {
assert!(
refused(&[
("files/rustfmt.toml", b"x"),
("files/.github/workflows/ci.yml", b"x"),
("files/knowledge.toml", b"x"),
("files/.claude/skills/house/SKILL.md", b"x"),
("schemas/note.md", b"x"),
])
.is_empty()
);
for rel in [
"files/.git/config",
"files/.GIT/hooks/pre-commit",
"files/sub/.git/config",
"files/.sokf/packs.lock",
"files/.Sokf/config.toml",
"files/knowledge/notes/a.md",
"files/Knowledge/a.md",
"files/AGENTS.md",
"files/claude.md",
"files/.claude/settings.json",
"files/.claude/skills/sokf/SKILL.md",
"files/a/../b",
] {
let found = refused(&[(rel, b"x")]);
assert_eq!(found.len(), 1, "{rel}: {found:?}");
assert!(found[0].starts_with(rel), "{found:?}");
}
let found = refused(&[("schemas/logo.md", &[0xff, 0xfe])]);
assert_eq!(found.len(), 1, "{found:?}");
assert!(found[0].contains("is not UTF-8"), "{found:?}");
}
}