use std::collections::{BTreeMap, BTreeSet, HashMap};
use std::sync::Arc;
use git2::{ObjectType, Repository};
use crate::discovery::DiscoveryLimits;
use crate::domain::index::{
FileClassification, FileProcessingResult, IndexTargets, LanguageId, MetadataOnlyReason,
RepositoryId, SourceId,
};
use crate::knowledge::{StableContentAdmission, classify_stable_content};
use crate::parsing::process_file_with_classification;
use super::store::{IndexedFile, LiveIndex, SharedIndexHandle};
use super::worktree_topology::{CheckedOutWorktree, checked_out_worktrees};
const DEFAULT_MAX_BLOB_MATERIALIZE_BYTES: u64 = 8 * 1024 * 1024;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct LocalRefScoutBudget {
pub max_entries: u64,
pub max_blob_materialize_bytes: u64,
}
impl Default for LocalRefScoutBudget {
fn default() -> Self {
Self {
max_entries: DiscoveryLimits::default().max_files,
max_blob_materialize_bytes: DEFAULT_MAX_BLOB_MATERIALIZE_BYTES,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum RefBlobDecision {
Ingest,
CatalogOnly,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct RefBlobEntry {
pub relative_path: String,
pub object_id: String,
pub size: u64,
pub language: Option<LanguageId>,
pub classification: FileClassification,
pub decision: RefBlobDecision,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum RefScoutCoverage {
Complete,
Degraded,
}
#[derive(Clone, Debug)]
pub struct LocalRefCatalog {
pub ref_name: String,
pub tip_object_id: String,
pub tree_object_id: String,
pub entries: Vec<RefBlobEntry>,
pub coverage: RefScoutCoverage,
pub distinct_blob_object_ids: usize,
pub total_distinct_blob_bytes: u64,
}
pub fn scout_local_ref(
repository: &Repository,
ref_name: &str,
budget: &LocalRefScoutBudget,
) -> Result<LocalRefCatalog, String> {
let object = repository
.revparse_single(ref_name)
.map_err(|_| format!("Error: local ref '{ref_name}' could not be resolved."))?;
let commit = object
.peel_to_commit()
.map_err(|_| format!("Error: local ref '{ref_name}' does not resolve to a commit."))?;
let root_tree = commit
.tree()
.map_err(|_| format!("Error: local ref '{ref_name}' tip commit has no tree."))?;
let odb = repository
.odb()
.map_err(|_| "Error: repository object database is unavailable.".to_string())?;
let tree_object_id = root_tree.id().to_string();
let mut entries: Vec<RefBlobEntry> = Vec::new();
let mut distinct: BTreeSet<git2::Oid> = BTreeSet::new();
let mut total_distinct_blob_bytes = 0u64;
let mut coverage = RefScoutCoverage::Complete;
let mut visited = 0u64;
let mut stack: Vec<(git2::Tree<'_>, String)> = vec![(root_tree, String::new())];
'walk: while let Some((tree, prefix)) = stack.pop() {
for entry in tree.iter() {
if visited >= budget.max_entries {
coverage = RefScoutCoverage::Degraded;
break 'walk;
}
visited += 1;
let Ok(name) = std::str::from_utf8(entry.name_bytes()) else {
coverage = RefScoutCoverage::Degraded;
continue;
};
let path = if prefix.is_empty() {
name.to_string()
} else {
format!("{prefix}/{name}")
};
match entry.kind() {
Some(ObjectType::Tree) => {
let subtree = repository.find_tree(entry.id()).map_err(|_| {
format!("Error: subtree '{path}' could not be read from '{ref_name}'.")
})?;
stack.push((subtree, path));
}
Some(ObjectType::Blob) => {
let oid = entry.id();
let (size, _kind) = odb.read_header(oid).map_err(|_| {
format!("Error: blob header for '{path}' is unreadable in '{ref_name}'.")
})?;
let size = size as u64;
if distinct.insert(oid) {
total_distinct_blob_bytes = total_distinct_blob_bytes.saturating_add(size);
}
let decision = if size > budget.max_blob_materialize_bytes {
RefBlobDecision::CatalogOnly
} else {
RefBlobDecision::Ingest
};
entries.push(RefBlobEntry {
relative_path: path.clone(),
object_id: oid.to_string(),
size,
language: LanguageId::from_path(&path),
classification: FileClassification::for_code_path(&path),
decision,
});
}
_ => {}
}
}
}
entries.sort_by(|left, right| left.relative_path.cmp(&right.relative_path));
Ok(LocalRefCatalog {
ref_name: ref_name.to_string(),
tip_object_id: commit.id().to_string(),
tree_object_id,
entries,
coverage,
distinct_blob_object_ids: distinct.len(),
total_distinct_blob_bytes,
})
}
#[derive(Clone, Debug, Default)]
pub struct RefBlobBytes {
by_object_id: BTreeMap<String, Arc<[u8]>>,
}
impl RefBlobBytes {
pub fn get(&self, object_id: &str) -> Option<&Arc<[u8]>> {
self.by_object_id.get(object_id)
}
pub fn distinct_len(&self) -> usize {
self.by_object_id.len()
}
}
pub fn materialize_ingest_blobs(
repository: &Repository,
catalog: &LocalRefCatalog,
) -> Result<RefBlobBytes, String> {
let mut by_object_id: BTreeMap<String, Arc<[u8]>> = BTreeMap::new();
for entry in &catalog.entries {
if entry.decision != RefBlobDecision::Ingest || by_object_id.contains_key(&entry.object_id)
{
continue;
}
let oid = git2::Oid::from_str(&entry.object_id)
.map_err(|_| format!("Error: blob object id '{}' is invalid.", entry.object_id))?;
let blob = repository
.find_blob(oid)
.map_err(|_| format!("Error: blob '{}' could not be read.", entry.object_id))?;
by_object_id.insert(entry.object_id.clone(), Arc::from(blob.content()));
}
Ok(RefBlobBytes { by_object_id })
}
#[derive(Debug)]
pub enum RefBlobIngest {
Withheld(MetadataOnlyReason),
Indexed {
targets: IndexTargets,
file: Box<IndexedFile>,
},
}
enum RefBlobRoute {
Withheld(MetadataOnlyReason),
Parse {
targets: IndexTargets,
language: LanguageId,
classification: FileClassification,
},
}
fn classify_ref_blob(entry: &RefBlobEntry, bytes: &[u8]) -> RefBlobRoute {
if let Some(rule_id) = crate::knowledge::sensitive_path_rule(&entry.relative_path) {
return RefBlobRoute::Withheld(MetadataOnlyReason::SensitivePath {
rule_id: rule_id.to_string(),
});
}
if let Some(reason) = crate::discovery::path_admission_reason(
std::path::Path::new(&entry.relative_path),
entry.size,
) {
return RefBlobRoute::Withheld(reason);
}
let sniff_len = bytes.len().min(crate::domain::index::BINARY_SNIFF_BYTES);
if crate::discovery::is_binary_content(&bytes[..sniff_len]) {
return RefBlobRoute::Withheld(MetadataOnlyReason::Binary);
}
let targets = IndexTargets::for_path(&entry.relative_path, entry.language.as_ref());
match classify_stable_content(&entry.relative_path, targets, bytes) {
StableContentAdmission::MetadataOnly(reason) => RefBlobRoute::Withheld(reason),
StableContentAdmission::Admitted => {
let classification =
FileClassification::for_indexed_path(&entry.relative_path, targets);
let language = entry.language.clone().unwrap_or(LanguageId::Text);
RefBlobRoute::Parse {
targets,
language,
classification,
}
}
}
}
#[cfg_attr(not(test), allow(dead_code))]
pub(crate) fn route_ref_blob(entry: &RefBlobEntry, bytes: Vec<u8>) -> RefBlobIngest {
match classify_ref_blob(entry, &bytes) {
RefBlobRoute::Withheld(reason) => RefBlobIngest::Withheld(reason),
RefBlobRoute::Parse {
targets,
language,
classification,
} => {
let result = process_file_with_classification(
&entry.relative_path,
&bytes,
language,
classification,
);
RefBlobIngest::Indexed {
targets,
file: Box::new(IndexedFile::from_parse_result(result, bytes)),
}
}
}
}
pub fn build_ref_source_index(
repository: &Repository,
catalog: &LocalRefCatalog,
) -> Result<LiveIndex, String> {
let blobs = materialize_ingest_blobs(repository, catalog)?;
Ok(LiveIndex::from_source_files(
route_catalog_files(catalog, &blobs).files,
))
}
struct RefRouteOutcome {
files: HashMap<String, Arc<IndexedFile>>,
#[cfg_attr(not(test), allow(dead_code))]
parses_performed: usize,
}
fn route_catalog_files(catalog: &LocalRefCatalog, blobs: &RefBlobBytes) -> RefRouteOutcome {
let mut files: HashMap<String, Arc<IndexedFile>> = HashMap::new();
let mut parse_cache: HashMap<
(String, FileClassification, LanguageId, bool, bool),
FileProcessingResult,
> = HashMap::new();
let mut parses_performed = 0usize;
for entry in &catalog.entries {
if entry.decision != RefBlobDecision::Ingest {
continue;
}
let Some(bytes) = blobs.get(&entry.object_id) else {
continue;
};
let RefBlobRoute::Parse {
language,
classification,
..
} = classify_ref_blob(entry, bytes)
else {
continue; };
let is_tsx = LanguageId::is_tsx_path(&entry.relative_path);
let is_c_header = LanguageId::is_c_header_path(&entry.relative_path);
let key = (
entry.object_id.clone(),
classification,
language.clone(),
is_tsx,
is_c_header,
);
let mut result = match parse_cache.get(&key) {
Some(cached) => cached.clone(),
None => {
parses_performed += 1;
let parsed = process_file_with_classification(
&entry.relative_path,
bytes,
language,
classification,
);
parse_cache.insert(key, parsed.clone());
parsed
}
};
result.relative_path = entry.relative_path.clone();
files.insert(
entry.relative_path.clone(),
Arc::new(IndexedFile::from_parse_result(result, bytes.to_vec())),
);
}
RefRouteOutcome {
files,
parses_performed,
}
}
pub fn ingest_and_publish_local_ref(
handle: &SharedIndexHandle,
repository: &Repository,
ref_name: &str,
repository_id: RepositoryId,
budget: &LocalRefScoutBudget,
) -> Result<SourceId, String> {
let catalog = scout_local_ref(repository, ref_name, budget)?;
let index = build_ref_source_index(repository, &catalog)?;
let coverage = match catalog.coverage {
RefScoutCoverage::Complete => crate::domain::CoverageStatus::Complete,
RefScoutCoverage::Degraded => crate::domain::CoverageStatus::Degraded,
};
let generation = handle.build_ref_source_generation(
index,
repository_id,
ref_name,
&catalog.tip_object_id,
coverage,
);
let source_id = generation
.source
.as_ref()
.expect("ref-source generation carries a source identity")
.source_id
.clone();
handle.publish_ref_source(generation);
Ok(source_id)
}
#[derive(Debug)]
pub struct ReconcileOutcome {
pub published: Vec<SourceId>,
pub removed: Vec<SourceId>,
pub checked_out: Vec<CheckedOutWorktree>,
pub failed: Vec<(String, String)>,
pub skipped: bool,
}
fn purge_repo_ref_lanes(handle: &SharedIndexHandle, repository_id: &RepositoryId) {
let lane_prefix = format!("symforge:git-ref:{}:", repository_id.as_str());
let current = handle.published_source_set();
let lanes: Vec<SourceId> = current
.sources
.keys()
.filter(|id| *id != ¤t.current_source_id)
.filter(|id| id.as_str().starts_with(lane_prefix.as_str()))
.cloned()
.collect();
for source_id in &lanes {
handle.remove_ref_source(source_id);
}
}
fn fail_closed_topology_error(
handle: &SharedIndexHandle,
repository_id: &RepositoryId,
message: String,
) -> String {
purge_repo_ref_lanes(handle, repository_id);
message
}
fn remove_failed_ref_lane_if_stale(
handle: &SharedIndexHandle,
source_id: &SourceId,
current_tip: Option<&str>,
) -> bool {
let published = handle.published_source_set();
let Some(lane) = published.sources.get(source_id) else {
return false;
};
let published_tip = lane
.source_version
.as_ref()
.and_then(|version| version.commit.as_deref());
if current_tip.is_some_and(|tip| published_tip == Some(tip)) {
return false;
}
handle.remove_ref_source(source_id)
}
pub fn reconcile_local_ref_topology(
handle: &SharedIndexHandle,
repository: &Repository,
repository_id: RepositoryId,
budget: &LocalRefScoutBudget,
) -> Result<ReconcileOutcome, String> {
let Some(_reconcile_guard) = handle.try_lock_ref_reconcile() else {
return Ok(ReconcileOutcome {
published: Vec::new(),
removed: Vec::new(),
checked_out: Vec::new(),
failed: Vec::new(),
skipped: true,
});
};
let checked_out = checked_out_worktrees(repository)
.map_err(|error| fail_closed_topology_error(handle, &repository_id, error))?;
if checked_out.iter().any(|worktree| !worktree.head_resolved) {
purge_repo_ref_lanes(handle, &repository_id);
return Err(
"Error: a checked-out worktree's HEAD could not be resolved; local-ref \
reconcile fails closed to avoid publishing a checked-out branch as a P1 lane."
.to_string(),
);
}
let mut checked_out_refs: BTreeSet<String> = checked_out
.iter()
.filter_map(|w| w.head_ref.clone())
.collect();
match repository.head() {
Ok(head) if head.is_branch() => match head.name() {
Ok(name) => {
checked_out_refs.insert(name.to_string());
}
Err(_) => {
purge_repo_ref_lanes(handle, &repository_id);
return Err(
"Error: the main repository HEAD is a branch whose name could \
not be decoded; local-ref reconcile fails closed to avoid \
publishing a checked-out branch as a P1 lane."
.to_string(),
);
}
},
Ok(_) => {} Err(err) if err.code() == git2::ErrorCode::UnbornBranch => {}
Err(err) => {
purge_repo_ref_lanes(handle, &repository_id);
return Err(format!(
"Error: the main repository HEAD could not be read ({err}); \
local-ref reconcile fails closed."
));
}
}
if repository.is_worktree() {
let main = match Repository::open(repository.commondir()) {
Ok(main) => main,
Err(err) => {
purge_repo_ref_lanes(handle, &repository_id);
return Err(format!(
"Error: the main worktree repository could not be opened from the \
common dir ({err}); local-ref reconcile fails closed."
));
}
};
match main.head() {
Ok(head) if head.is_branch() => match head.name() {
Ok(name) => {
checked_out_refs.insert(name.to_string());
}
Err(_) => {
purge_repo_ref_lanes(handle, &repository_id);
return Err(
"Error: the main worktree HEAD is a branch whose name could \
not be decoded; local-ref reconcile fails closed to avoid \
publishing a checked-out branch as a P1 lane."
.to_string(),
);
}
},
Ok(_) => {} Err(err) if err.code() == git2::ErrorCode::UnbornBranch => {}
Err(err) => {
purge_repo_ref_lanes(handle, &repository_id);
return Err(format!(
"Error: the main worktree HEAD could not be read ({err}); \
local-ref reconcile fails closed."
));
}
}
}
let mut local_branch_refs: Vec<String> = Vec::new();
let branches = repository
.branches(Some(git2::BranchType::Local))
.map_err(|err| {
fail_closed_topology_error(
handle,
&repository_id,
format!("Error: local branches are unavailable: {err}."),
)
})?;
for branch in branches {
let (branch, _kind) = branch.map_err(|err| {
fail_closed_topology_error(
handle,
&repository_id,
format!("Error: a local branch entry is unreadable: {err}."),
)
})?;
if let Ok(name) = branch.get().name() {
local_branch_refs.push(name.to_string());
}
}
local_branch_refs.sort();
let lane_prefix = format!("symforge:git-ref:{}:", repository_id.as_str());
let mut published: Vec<SourceId> = Vec::new();
let mut removed: Vec<SourceId> = Vec::new();
let mut failed: Vec<(String, String)> = Vec::new();
for ref_name in &local_branch_refs {
if checked_out_refs.contains(ref_name) {
continue;
}
let source_id = SourceId::new(format!("{lane_prefix}{ref_name}"));
let current_tip = repository
.refname_to_id(ref_name)
.ok()
.map(|oid| oid.to_string());
if let Some(tip) = current_tip.as_deref() {
let unchanged = handle
.published_source_set()
.sources
.get(&source_id)
.and_then(|lane| lane.source_version.as_ref())
.map(|version| version.commit.as_deref() == Some(tip))
.unwrap_or(false);
if unchanged {
continue;
}
}
match ingest_and_publish_local_ref(
handle,
repository,
ref_name,
repository_id.clone(),
budget,
) {
Ok(source_id) => published.push(source_id),
Err(error) => {
if remove_failed_ref_lane_if_stale(handle, &source_id, current_tip.as_deref()) {
removed.push(source_id);
}
failed.push((ref_name.clone(), error));
}
}
}
let current = handle.published_source_set();
for source_id in current.sources.keys() {
if source_id == ¤t.current_source_id {
continue;
}
if !source_id.as_str().starts_with("symforge:git-ref:") {
continue;
}
let Some(ref_name) = source_id.as_str().strip_prefix(&lane_prefix) else {
continue;
};
let still_bare = local_branch_refs.iter().any(|branch| branch == ref_name)
&& !checked_out_refs.contains(ref_name);
if !still_bare && handle.remove_ref_source(source_id) {
removed.push(source_id.clone());
}
}
Ok(ReconcileOutcome {
published,
removed,
checked_out,
failed,
skipped: false,
})
}
#[cfg(test)]
mod tests {
use super::*;
use std::path::Path;
fn init_repo(root: &Path) -> Repository {
git2::Repository::init(root).expect("init repo")
}
fn commit_files(root: &Path, files: &[(&str, &[u8])], message: &str) -> git2::Oid {
let repository = git2::Repository::open(root).expect("open repo");
for (relative, bytes) in files {
let full = root.join(relative);
if let Some(parent) = full.parent() {
std::fs::create_dir_all(parent).expect("mkdir");
}
std::fs::write(&full, bytes).expect("write file");
}
let mut index = repository.index().expect("index");
index
.add_all(["*"], git2::IndexAddOption::DEFAULT, None)
.expect("stage");
index.write().expect("write index");
let tree_id = index.write_tree().expect("write tree");
let tree = repository.find_tree(tree_id).expect("tree");
let signature =
git2::Signature::now("SymForge Test", "symforge@example.invalid").expect("sig");
let parent = repository
.head()
.ok()
.and_then(|head| head.peel_to_commit().ok());
let parents: Vec<&git2::Commit<'_>> = parent.iter().collect();
repository
.commit(
Some("HEAD"),
&signature,
&signature,
message,
&tree,
&parents,
)
.expect("commit")
}
#[test]
fn enumerates_blobs_by_object_id_with_classification() {
let dir = tempfile::tempdir().expect("tempdir");
let root = dir.path();
init_repo(root);
commit_files(
root,
&[
("src/lib.rs", b"pub fn a() {}\n"),
("docs/guide.md", b"# Guide\n"),
("vendor/dep/lib.rs", b"pub fn v() {}\n"),
],
"initial",
);
let repository = git2::Repository::open(root).expect("open");
let catalog = scout_local_ref(
&repository,
"refs/heads/master",
&LocalRefScoutBudget::default(),
)
.or_else(|_| scout_local_ref(&repository, "HEAD", &LocalRefScoutBudget::default()))
.expect("scout");
assert_eq!(catalog.coverage, RefScoutCoverage::Complete);
let paths: Vec<&str> = catalog
.entries
.iter()
.map(|entry| entry.relative_path.as_str())
.collect();
assert_eq!(paths, ["docs/guide.md", "src/lib.rs", "vendor/dep/lib.rs"]);
assert!(
catalog
.entries
.iter()
.all(|entry| entry.object_id.len() >= 40)
);
assert!(
catalog
.entries
.iter()
.find(|entry| entry.relative_path == "vendor/dep/lib.rs")
.expect("vendor entry")
.classification
.is_vendor
);
assert_eq!(
catalog
.entries
.iter()
.find(|entry| entry.relative_path == "src/lib.rs")
.expect("rust entry")
.language,
Some(LanguageId::Rust)
);
assert!(
catalog
.entries
.iter()
.all(|entry| entry.decision == RefBlobDecision::Ingest)
);
assert_eq!(catalog.distinct_blob_object_ids, 3);
}
#[test]
fn giant_blob_is_catalog_only_without_materialization() {
let dir = tempfile::tempdir().expect("tempdir");
let root = dir.path();
init_repo(root);
let big = vec![b'x'; 4096];
commit_files(
root,
&[("small.md", b"# small\n"), ("big.bin", &big)],
"initial",
);
let repository = git2::Repository::open(root).expect("open");
let budget = LocalRefScoutBudget {
max_entries: 1_000,
max_blob_materialize_bytes: 1024,
};
let catalog = scout_local_ref(&repository, "HEAD", &budget).expect("scout");
let big_entry = catalog
.entries
.iter()
.find(|entry| entry.relative_path == "big.bin")
.expect("big entry");
assert_eq!(big_entry.decision, RefBlobDecision::CatalogOnly);
assert_eq!(big_entry.size, 4096);
let small_entry = catalog
.entries
.iter()
.find(|entry| entry.relative_path == "small.md")
.expect("small entry");
assert_eq!(small_entry.decision, RefBlobDecision::Ingest);
}
#[test]
fn identical_bytes_share_object_id_across_paths_with_distinct_classification() {
let dir = tempfile::tempdir().expect("tempdir");
let root = dir.path();
init_repo(root);
let shared = b"identical content\n";
commit_files(
root,
&[("notes.md", shared), ("data.txt", shared)],
"initial",
);
let repository = git2::Repository::open(root).expect("open");
let catalog =
scout_local_ref(&repository, "HEAD", &LocalRefScoutBudget::default()).expect("scout");
let md = catalog
.entries
.iter()
.find(|entry| entry.relative_path == "notes.md")
.expect("md entry");
let txt = catalog
.entries
.iter()
.find(|entry| entry.relative_path == "data.txt")
.expect("txt entry");
assert_eq!(
md.object_id, txt.object_id,
"identical bytes share one blob"
);
assert_eq!(catalog.distinct_blob_object_ids, 1);
assert_eq!(md.language, Some(LanguageId::Markdown));
assert_eq!(txt.language, Some(LanguageId::Text));
}
#[test]
fn entry_budget_degrades_coverage_without_unbounded_collection() {
let dir = tempfile::tempdir().expect("tempdir");
let root = dir.path();
init_repo(root);
commit_files(
root,
&[
("a.rs", b"//a\n"),
("b.rs", b"//b\n"),
("c.rs", b"//c\n"),
("d.rs", b"//d\n"),
],
"initial",
);
let repository = git2::Repository::open(root).expect("open");
let budget = LocalRefScoutBudget {
max_entries: 2,
max_blob_materialize_bytes: DEFAULT_MAX_BLOB_MATERIALIZE_BYTES,
};
let catalog = scout_local_ref(&repository, "HEAD", &budget).expect("scout");
assert_eq!(catalog.coverage, RefScoutCoverage::Degraded);
assert_eq!(catalog.entries.len(), 2);
}
#[test]
fn tree_entries_count_against_the_scout_budget() {
let dir = tempfile::tempdir().expect("tempdir");
let root = dir.path();
init_repo(root);
commit_files(
root,
&[
("d0/f.rs", b"//0\n"),
("d1/f.rs", b"//1\n"),
("d2/f.rs", b"//2\n"),
("d3/f.rs", b"//3\n"),
],
"initial",
);
let repository = git2::Repository::open(root).expect("open");
let budget = LocalRefScoutBudget {
max_entries: 5,
max_blob_materialize_bytes: DEFAULT_MAX_BLOB_MATERIALIZE_BYTES,
};
let catalog = scout_local_ref(&repository, "HEAD", &budget).expect("scout");
assert_eq!(
catalog.coverage,
RefScoutCoverage::Degraded,
"subtree entries must count against the budget, degrading coverage"
);
}
#[test]
fn missing_ref_is_typed_error() {
let dir = tempfile::tempdir().expect("tempdir");
let root = dir.path();
init_repo(root);
commit_files(root, &[("a.rs", b"//a\n")], "initial");
let repository = git2::Repository::open(root).expect("open");
let error = scout_local_ref(
&repository,
"refs/heads/does-not-exist",
&LocalRefScoutBudget::default(),
)
.expect_err("missing ref must error");
assert!(error.contains("could not be resolved"), "{error}");
}
#[test]
fn materializes_shared_object_id_once_and_bytes_match_source() {
let dir = tempfile::tempdir().expect("tempdir");
let root = dir.path();
init_repo(root);
let shared = b"identical content\n";
commit_files(
root,
&[("notes.md", shared), ("data.txt", shared)],
"initial",
);
let repository = git2::Repository::open(root).expect("open");
let catalog =
scout_local_ref(&repository, "HEAD", &LocalRefScoutBudget::default()).expect("scout");
let bytes = materialize_ingest_blobs(&repository, &catalog).expect("materialize");
assert_eq!(bytes.distinct_len(), 1, "identical bytes materialized once");
let object_id = &catalog.entries[0].object_id;
assert_eq!(
bytes.get(object_id).expect("shared blob bytes").as_ref(),
shared
);
}
#[test]
fn catalog_only_blob_bytes_are_not_materialized() {
let dir = tempfile::tempdir().expect("tempdir");
let root = dir.path();
init_repo(root);
let big = vec![b'x'; 4096];
commit_files(
root,
&[("small.md", b"# small\n"), ("big.bin", &big)],
"initial",
);
let repository = git2::Repository::open(root).expect("open");
let budget = LocalRefScoutBudget {
max_entries: 1_000,
max_blob_materialize_bytes: 1024,
};
let catalog = scout_local_ref(&repository, "HEAD", &budget).expect("scout");
let bytes = materialize_ingest_blobs(&repository, &catalog).expect("materialize");
let big_id = &catalog
.entries
.iter()
.find(|entry| entry.relative_path == "big.bin")
.expect("big entry")
.object_id;
let small_id = &catalog
.entries
.iter()
.find(|entry| entry.relative_path == "small.md")
.expect("small entry")
.object_id;
assert!(
bytes.get(big_id).is_none(),
"catalog-only blob bytes must never be materialized"
);
assert!(bytes.get(small_id).is_some(), "ingest blob is materialized");
assert_eq!(bytes.distinct_len(), 1);
}
fn route_single(root: &Path, relative: &str, bytes: &[u8]) -> RefBlobIngest {
init_repo(root);
commit_files(root, &[(relative, bytes)], "initial");
let repository = git2::Repository::open(root).expect("open");
let catalog =
scout_local_ref(&repository, "HEAD", &LocalRefScoutBudget::default()).expect("scout");
let blobs = materialize_ingest_blobs(&repository, &catalog).expect("materialize");
let entry = catalog
.entries
.iter()
.find(|entry| entry.relative_path == relative)
.expect("entry");
let bytes = blobs.get(&entry.object_id).expect("bytes").to_vec();
route_ref_blob(entry, bytes)
}
#[test]
fn routes_clean_code_blob_through_shared_parser() {
let dir = tempfile::tempdir().expect("tempdir");
let outcome = route_single(dir.path(), "src/lib.rs", b"pub fn routed() -> u32 { 7 }\n");
match outcome {
RefBlobIngest::Indexed { targets, file } => {
assert_eq!(targets, IndexTargets::Code);
assert!(
file.symbols.iter().any(|symbol| symbol.name == "routed"),
"shared parser must extract the ref blob's symbol"
);
assert!(file.classification.is_code());
}
other => panic!("expected Indexed, got {other:?}"),
}
}
#[test]
fn routes_markdown_blob_to_knowledge_lane() {
let dir = tempfile::tempdir().expect("tempdir");
let outcome = route_single(dir.path(), "docs/guide.md", b"# Guide\n\nBody text.\n");
match outcome {
RefBlobIngest::Indexed { targets, .. } => {
assert_eq!(targets, IndexTargets::Knowledge);
}
other => panic!("expected Indexed, got {other:?}"),
}
}
#[test]
fn withholds_secret_positive_blob_as_metadata_only() {
let dir = tempfile::tempdir().expect("tempdir");
let secret =
b"-----BEGIN RSA PRIVATE KEY-----\nMIIBOgIBAAJBAK\n-----END RSA PRIVATE KEY-----\n";
let outcome = route_single(dir.path(), "config/key.txt", secret);
assert!(
matches!(outcome, RefBlobIngest::Withheld(_)),
"secret-positive ref blob must be withheld, got {outcome:?}"
);
}
#[test]
fn builds_queryable_ref_source_index_excluding_withheld_blobs() {
let dir = tempfile::tempdir().expect("tempdir");
let root = dir.path();
init_repo(root);
let secret =
b"-----BEGIN RSA PRIVATE KEY-----\nMIIBOgIBAAJBAK\n-----END RSA PRIVATE KEY-----\n";
commit_files(
root,
&[
("src/lib.rs", b"pub fn indexed_symbol() -> u32 { 1 }\n"),
("docs/guide.md", b"# Guide\n\nProse.\n"),
("config/key.txt", secret),
],
"initial",
);
let repository = git2::Repository::open(root).expect("open");
let catalog =
scout_local_ref(&repository, "HEAD", &LocalRefScoutBudget::default()).expect("scout");
let index = build_ref_source_index(&repository, &catalog).expect("build index");
assert!(!index.is_empty, "ref-source index must not be empty");
assert!(
index.files.contains_key("src/lib.rs"),
"routed code blob must be indexed"
);
assert!(
index.files.contains_key("docs/guide.md"),
"routed knowledge blob must be indexed"
);
assert!(
!index.files.contains_key("config/key.txt"),
"secret-withheld blob must never enter the index"
);
assert!(
index.files["src/lib.rs"]
.symbols
.iter()
.any(|symbol| symbol.name == "indexed_symbol"),
"ref-source index must carry parsed symbols"
);
}
#[test]
fn ingest_and_publish_makes_a_queryable_ref_lane_without_touching_current() {
let dir = tempfile::tempdir().expect("tempdir");
let root = dir.path();
init_repo(root);
commit_files(
root,
&[("src/lib.rs", b"pub fn ref_lane_symbol() -> bool { true }\n")],
"initial",
);
let repository = git2::Repository::open(root).expect("open");
let handle = SharedIndexHandle::new(LiveIndex::from_source_files(HashMap::new()));
let before = handle.published_source_set();
let current_id = before.current_source_id.clone();
let source_id = ingest_and_publish_local_ref(
&handle,
&repository,
"HEAD",
RepositoryId::new("repo-e2e"),
&LocalRefScoutBudget::default(),
)
.expect("ingest and publish");
let set = handle.published_source_set();
assert_eq!(set.registry_generation, before.registry_generation + 1);
assert_eq!(
set.current_source_id, current_id,
"current lane identity is stable"
);
let lane = set.sources.get(&source_id).expect("published ref lane");
assert!(
lane.live.files.contains_key("src/lib.rs"),
"the ref lane is queryable"
);
assert!(
lane.live.files["src/lib.rs"]
.symbols
.iter()
.any(|symbol| symbol.name == "ref_lane_symbol")
);
match &lane.source.as_ref().expect("lane source").location {
crate::domain::index::SourceLocation::GitRef { name } => assert_eq!(name, "HEAD"),
other => panic!("expected GitRef location, got {other:?}"),
}
assert_eq!(
lane.source_version
.as_ref()
.expect("lane version")
.working_tree,
crate::domain::index::WorkingTreeState::NotApplicable
);
}
fn commit_bare_branch_with_doc(
repository: &Repository,
branch: &str,
dir_name: &str,
file_name: &str,
bytes: &[u8],
) {
let blob = repository.blob(bytes).expect("write blob");
let mut dir_builder = repository.treebuilder(None).expect("dir treebuilder");
dir_builder
.insert(file_name, blob, 0o100644)
.expect("insert blob into dir tree");
let dir_tree = dir_builder.write().expect("write dir tree");
let mut root_builder = repository.treebuilder(None).expect("root treebuilder");
root_builder
.insert(dir_name, dir_tree, 0o040000)
.expect("insert dir into root tree");
let root_tree_id = root_builder.write().expect("write root tree");
let root_tree = repository.find_tree(root_tree_id).expect("find root tree");
let signature =
git2::Signature::now("SymForge Test", "symforge@example.invalid").expect("signature");
let commit_id = repository
.commit(
None,
&signature,
&signature,
"ref-only doc",
&root_tree,
&[],
)
.expect("commit ref tree");
let commit = repository.find_commit(commit_id).expect("find commit");
repository
.branch(branch, &commit, false)
.expect("create bare branch");
}
#[test]
fn source_isolation_never_crosses_ref_and_current_boundaries() {
use crate::domain::index::SourceLocation;
let dir = tempfile::tempdir().expect("tempdir");
let root = dir.path();
init_repo(root);
commit_files(
root,
&[(
"docs/current-only.md",
b"# Current Only\n\nThe quantum meridian anchors the current worktree lane.\n",
)],
"current",
);
let repository = git2::Repository::open(root).expect("open");
commit_bare_branch_with_doc(
&repository,
"ref-only",
"docs",
"ref-only.md",
b"# Ref Only\n\nThe helical cascade governs the divergent ref lane.\n",
);
let handle = LiveIndex::load(root).expect("load current lane");
let ref_id = ingest_and_publish_local_ref(
&handle,
&repository,
"refs/heads/ref-only",
RepositoryId::new("repo-isolation"),
&LocalRefScoutBudget::default(),
)
.expect("publish ref lane");
let set = handle.published_source_set();
let current = set.current_generation();
let ref_gen = set.sources.get(&ref_id).expect("ref lane generation");
let current_source = current.source.as_ref().expect("current source identity");
let ref_source = ref_gen.source.as_ref().expect("ref source identity");
assert!(
matches!(current_source.location, SourceLocation::WorkingTree { .. }),
"current lane is a WorkingTree source, got {:?}",
current_source.location
);
match &ref_source.location {
SourceLocation::GitRef { name } => assert_eq!(name, "refs/heads/ref-only"),
other => panic!("ref lane must be a GitRef source, got {other:?}"),
}
assert_ne!(
current_source.source_id, ref_source.source_id,
"the two lanes carry distinct source ids"
);
assert!(current.live.files.contains_key("docs/current-only.md"));
assert!(
!current.live.files.contains_key("docs/ref-only.md"),
"the current lane never carries the ref lane's document"
);
assert!(ref_gen.live.files.contains_key("docs/ref-only.md"));
assert!(
!ref_gen.live.files.contains_key("docs/current-only.md"),
"the ref lane never carries the current lane's document"
);
assert!(
ref_gen
.bridge
.cards
.iter()
.any(|card| card.anchor.path == "docs/ref-only.md"),
"the ref bridge has a card for its own document"
);
for card in &ref_gen.bridge.cards {
assert_eq!(
&card.anchor.source,
ref_source.as_ref(),
"a ref bridge card carries the ref source identity"
);
assert_ne!(
card.anchor.path, "docs/current-only.md",
"the ref bridge never references the current lane's document"
);
}
assert!(
current
.bridge
.cards
.iter()
.any(|card| card.anchor.path == "docs/current-only.md"),
"the current bridge has a card for its own document"
);
for card in ¤t.bridge.cards {
assert_eq!(
&card.anchor.source,
current_source.as_ref(),
"a current bridge card carries the current source identity"
);
assert_ne!(
card.anchor.path, "docs/ref-only.md",
"the current bridge never references the ref lane's document"
);
}
assert_eq!(
ref_gen
.authority
.source
.as_ref()
.expect("ref authority source"),
ref_source.as_ref(),
"the ref authority carries the ref source identity"
);
for record in &ref_gen.authority.records {
assert_eq!(&record.unit.source, ref_source.as_ref());
assert_ne!(record.unit.path, "docs/current-only.md");
}
assert_eq!(
current
.authority
.source
.as_ref()
.expect("current authority source"),
current_source.as_ref(),
"the current authority carries the current source identity"
);
for record in ¤t.authority.records {
assert_eq!(&record.unit.source, current_source.as_ref());
assert_ne!(record.unit.path, "docs/ref-only.md");
}
}
#[test]
fn identical_blob_is_parsed_once_across_same_classification_paths() {
let dir = tempfile::tempdir().expect("tempdir");
let root = dir.path();
init_repo(root);
let body: &[u8] = b"pub fn shared() -> u32 { 7 }\n";
commit_files(
root,
&[("one.rs", body), ("two.rs", body), ("notes.md", body)],
"initial",
);
let repository = git2::Repository::open(root).expect("open");
let catalog =
scout_local_ref(&repository, "HEAD", &LocalRefScoutBudget::default()).expect("scout");
let blobs = materialize_ingest_blobs(&repository, &catalog).expect("materialize");
assert_eq!(
blobs.distinct_len(),
1,
"identical bytes share one object id"
);
let outcome = route_catalog_files(&catalog, &blobs);
assert_eq!(
outcome.files.len(),
3,
"every path is mapped to its own file"
);
assert_eq!(
outcome.parses_performed, 2,
"same-class duplicate parsed once; the md path re-derives its own parse"
);
assert_eq!(
outcome.files["two.rs"].relative_path, "two.rs",
"each mapping keeps its own path label"
);
}
#[test]
fn identical_blob_reparsed_per_path_selected_grammar_flavor() {
let dir = tempfile::tempdir().expect("tempdir");
let root = dir.path();
init_repo(root);
let source: &[u8] = b"export const x = 1;\n";
commit_files(root, &[("a.ts", source), ("b.tsx", source)], "initial");
let repository = git2::Repository::open(root).expect("open");
let catalog =
scout_local_ref(&repository, "HEAD", &LocalRefScoutBudget::default()).expect("scout");
let blobs = materialize_ingest_blobs(&repository, &catalog).expect("materialize");
assert_eq!(
blobs.distinct_len(),
1,
"identical bytes share one object id"
);
let outcome = route_catalog_files(&catalog, &blobs);
assert_eq!(outcome.files.len(), 2, "both paths are mapped");
assert_eq!(
outcome.parses_performed, 2,
".ts and .tsx select different grammars and must not share a parse"
);
}
#[test]
fn degraded_scout_publishes_degraded_ref_manifest_coverage() {
let dir = tempfile::tempdir().expect("tempdir");
let root = dir.path();
init_repo(root);
commit_files(
root,
&[
("a.rs", b"//a\n"),
("b.rs", b"//b\n"),
("c.rs", b"//c\n"),
("d.rs", b"//d\n"),
],
"initial",
);
let repository = git2::Repository::open(root).expect("open");
let handle = SharedIndexHandle::new(LiveIndex::from_source_files(HashMap::new()));
let budget = LocalRefScoutBudget {
max_entries: 2,
max_blob_materialize_bytes: DEFAULT_MAX_BLOB_MATERIALIZE_BYTES,
};
let source_id = ingest_and_publish_local_ref(
&handle,
&repository,
"HEAD",
RepositoryId::new("repo-degraded"),
&budget,
)
.expect("ingest and publish");
let set = handle.published_source_set();
let lane = set.sources.get(&source_id).expect("published ref lane");
assert_eq!(
lane.manifest.as_ref().expect("ref lane manifest").coverage,
crate::domain::CoverageStatus::Degraded,
"a degraded scout must publish Degraded ref coverage"
);
}
#[cfg(feature = "server")]
#[test]
fn search_scoped_composes_local_ref_lane_and_reports_typed_empty_worktrees() {
use crate::protocol::knowledge_search::search_scoped;
use crate::protocol::search_tools::{KnowledgeSourceScope, SearchKnowledgeInput};
let dir = tempfile::tempdir().expect("tempdir");
let root = dir.path();
init_repo(root);
commit_files(
root,
&[(
"docs/topic.md",
b"# Topic\n\nThe orbital lattice resonates across the source.\n",
)],
"initial",
);
let repository = git2::Repository::open(root).expect("open");
let handle = SharedIndexHandle::new(LiveIndex::from_source_files(HashMap::new()));
ingest_and_publish_local_ref(
&handle,
&repository,
"HEAD",
RepositoryId::new("repo-scope"),
&LocalRefScoutBudget::default(),
)
.expect("publish ref");
let set = handle.published_source_set();
let input = |scope| SearchKnowledgeInput {
query: "orbital lattice".to_string(),
path_prefix: None,
source_scope: Some(scope),
authority_scope: None,
project: None,
projects: None,
limit: None,
max_tokens: None,
};
let local_refs = search_scoped(&set, &input(KnowledgeSourceScope::LocalRefs));
assert!(
local_refs.contains("Source scope searched: local_refs"),
"{local_refs}"
);
assert!(local_refs.contains("ref:HEAD"), "{local_refs}");
let worktrees = search_scoped(&set, &input(KnowledgeSourceScope::Worktrees));
assert!(worktrees.contains("no_sources_in_scope"), "{worktrees}");
let all = search_scoped(&set, &input(KnowledgeSourceScope::All));
assert!(all.contains("Source scope searched: all"), "{all}");
assert!(all.contains("ref:HEAD"), "{all}");
}
#[cfg(feature = "server")]
#[test]
fn review_scoped_composes_local_ref_lane_and_reports_typed_empty_worktrees() {
use crate::protocol::knowledge_review::review_scoped;
use crate::protocol::search_tools::{
KnowledgeSourceScope, ReviewKnowledgeInput, ReviewKnowledgeMode,
};
let dir = tempfile::tempdir().expect("tempdir");
let root = dir.path();
init_repo(root);
commit_files(
root,
&[(
"docs/topic.md",
b"# Topic\n\nThe orbital lattice resonates across the source.\n",
)],
"initial",
);
let repository = git2::Repository::open(root).expect("open");
let handle = SharedIndexHandle::new(LiveIndex::from_source_files(HashMap::new()));
ingest_and_publish_local_ref(
&handle,
&repository,
"HEAD",
RepositoryId::new("repo-review"),
&LocalRefScoutBudget::default(),
)
.expect("publish ref");
let set = handle.published_source_set();
let input = |scope| ReviewKnowledgeInput {
mode: ReviewKnowledgeMode::Summary,
path: None,
path_prefix: None,
source_scope: Some(scope),
project: None,
projects: None,
limit: None,
max_tokens: None,
};
let local_refs = review_scoped(&set, &input(KnowledgeSourceScope::LocalRefs))
.expect("local_refs review");
assert!(local_refs.rendered.contains("top_result_hash="));
assert!(
local_refs.rendered.contains("source_scope=local_refs"),
"{}",
local_refs.rendered
);
let worktrees = review_scoped(&set, &input(KnowledgeSourceScope::Worktrees));
assert!(
worktrees
.as_ref()
.is_err_and(|error| error.contains("no_sources_in_scope")),
"{worktrees:?}"
);
let all = review_scoped(&set, &input(KnowledgeSourceScope::All)).expect("all review");
assert!(
all.rendered.contains("source_scope=all"),
"{}",
all.rendered
);
}
#[cfg(feature = "server")]
#[test]
fn all_scope_lists_current_lane_before_ref_lanes() {
use crate::protocol::knowledge_search::select_scoped_sources;
use crate::protocol::search_tools::KnowledgeSourceScope;
let dir = tempfile::tempdir().expect("tempdir");
let root = dir.path();
init_repo(root);
commit_files(root, &[("src/lib.rs", b"pub fn a() {}\n")], "initial");
let repository = git2::Repository::open(root).expect("open");
let handle = SharedIndexHandle::new(LiveIndex::from_source_files(HashMap::new()));
let ref_id = ingest_and_publish_local_ref(
&handle,
&repository,
"HEAD",
RepositoryId::new("repo-order"),
&LocalRefScoutBudget::default(),
)
.expect("publish ref");
let set = handle.published_source_set();
let all = select_scoped_sources(&set, KnowledgeSourceScope::All);
assert_eq!(all.len(), 2, "current + one ref lane");
assert_eq!(
all[1].source.as_ref().expect("ref lane source").source_id,
ref_id,
"the ref lane is ordered after the current lane"
);
}
#[test]
fn ref_movement_replaces_the_lane_and_bumps_registry() {
let dir = tempfile::tempdir().expect("tempdir");
let root = dir.path();
init_repo(root);
commit_files(root, &[("src/lib.rs", b"pub fn a() {}\n")], "initial");
let repository = git2::Repository::open(root).expect("open");
let handle = SharedIndexHandle::new(LiveIndex::from_source_files(HashMap::new()));
let id = ingest_and_publish_local_ref(
&handle,
&repository,
"HEAD",
RepositoryId::new("repo-move"),
&LocalRefScoutBudget::default(),
)
.expect("publish ref");
let set1 = handle.published_source_set();
let tip1 = set1.sources[&id]
.source_version
.as_ref()
.expect("ref version")
.commit
.clone();
let registry1 = set1.registry_generation;
commit_files(root, &[("src/b.rs", b"pub fn b() {}\n")], "second");
let id2 = ingest_and_publish_local_ref(
&handle,
&repository,
"HEAD",
RepositoryId::new("repo-move"),
&LocalRefScoutBudget::default(),
)
.expect("republish moved ref");
assert_eq!(id, id2, "the same ref keeps its source id");
let set2 = handle.published_source_set();
let tip2 = set2.sources[&id]
.source_version
.as_ref()
.expect("ref version")
.commit
.clone();
assert_ne!(tip1, tip2, "ref movement updated the recorded tip");
assert!(
set2.registry_generation > registry1,
"a source-map change advances registry_generation"
);
assert_eq!(
set2.sources.len(),
2,
"the moved ref replaced its lane rather than duplicating it"
);
}
#[test]
fn failed_ref_ingestion_leaves_the_current_lane_untouched() {
let dir = tempfile::tempdir().expect("tempdir");
let root = dir.path();
init_repo(root);
commit_files(root, &[("src/lib.rs", b"pub fn a() {}\n")], "initial");
let repository = git2::Repository::open(root).expect("open");
let handle = SharedIndexHandle::new(LiveIndex::from_source_files(HashMap::new()));
let before = handle.published_source_set();
let before_registry = before.registry_generation;
let before_len = before.sources.len();
let current_id = before.current_source_id.clone();
let result = ingest_and_publish_local_ref(
&handle,
&repository,
"refs/heads/does-not-exist",
RepositoryId::new("repo-fail"),
&LocalRefScoutBudget::default(),
);
assert!(result.is_err(), "a missing ref must fail to ingest");
let after = handle.published_source_set();
assert_eq!(
after.registry_generation, before_registry,
"a failed ref ingestion must not advance registry_generation"
);
assert_eq!(
after.sources.len(),
before_len,
"a failed ref ingestion must not add a lane"
);
assert!(
after.sources.contains_key(¤t_id),
"the current worktree lane stays present and queryable"
);
}
fn add_worktree_on_branch(repository: &Repository, path: &Path, name: &str, branch: &str) {
let head_commit = repository
.head()
.expect("head")
.peel_to_commit()
.expect("peel");
repository
.branch(branch, &head_commit, false)
.expect("branch");
let reference = repository
.find_reference(&format!("refs/heads/{branch}"))
.expect("reference");
let mut opts = git2::WorktreeAddOptions::new();
opts.reference(Some(&reference));
repository
.worktree(name, path, Some(&opts))
.expect("add worktree");
}
#[test]
fn reconcile_publishes_bare_branch_and_excludes_checked_out_worktree_branch() {
let dir = tempfile::tempdir().expect("tempdir");
let root = dir.path();
init_repo(root);
commit_files(root, &[("src/lib.rs", b"pub fn base() {}\n")], "initial");
let repository = git2::Repository::open(root).expect("open");
let main_ref = repository
.head()
.expect("head")
.name()
.expect("head name")
.to_string();
let head_commit = repository
.head()
.expect("head")
.peel_to_commit()
.expect("peel");
repository
.branch("bare", &head_commit, false)
.expect("bare branch");
let wt_parent = tempfile::tempdir().expect("wt tempdir");
let wt_path = wt_parent.path().join("checked");
add_worktree_on_branch(&repository, &wt_path, "checked-wt", "checked");
let handle = SharedIndexHandle::new(LiveIndex::from_source_files(HashMap::new()));
let repository_id = RepositoryId::new("repo-topology");
let outcome = reconcile_local_ref_topology(
&handle,
&repository,
repository_id.clone(),
&LocalRefScoutBudget::default(),
)
.expect("reconcile");
let bare_id = SourceId::new(format!(
"symforge:git-ref:{}:refs/heads/bare",
repository_id.as_str()
));
let checked_id = SourceId::new(format!(
"symforge:git-ref:{}:refs/heads/checked",
repository_id.as_str()
));
let main_id = SourceId::new(format!(
"symforge:git-ref:{}:{main_ref}",
repository_id.as_str()
));
let set = handle.published_source_set();
assert!(
set.sources.contains_key(&bare_id),
"the bare branch gets a P1 lane"
);
assert!(
!set.sources.contains_key(&checked_id),
"the checked-out worktree branch is NOT a P1 lane"
);
assert!(
!set.sources.contains_key(&main_id),
"the current worktree HEAD (P0) is never a P1 lane"
);
assert!(outcome.published.contains(&bare_id));
assert!(!outcome.published.contains(&checked_id));
assert!(
outcome
.checked_out
.iter()
.any(|w| w.head_ref.as_deref() == Some("refs/heads/checked")),
"the checked-out worktree is surfaced for its own ProjectInstance routing"
);
}
#[test]
fn reconcile_removes_lane_for_deleted_branch_and_advances_registry() {
let dir = tempfile::tempdir().expect("tempdir");
let root = dir.path();
init_repo(root);
commit_files(root, &[("src/lib.rs", b"pub fn base() {}\n")], "initial");
let repository = git2::Repository::open(root).expect("open");
let head_commit = repository
.head()
.expect("head")
.peel_to_commit()
.expect("peel");
repository
.branch("doomed", &head_commit, false)
.expect("branch");
let handle = SharedIndexHandle::new(LiveIndex::from_source_files(HashMap::new()));
let repository_id = RepositoryId::new("repo-delete");
let doomed_id = SourceId::new(format!(
"symforge:git-ref:{}:refs/heads/doomed",
repository_id.as_str()
));
let first = reconcile_local_ref_topology(
&handle,
&repository,
repository_id.clone(),
&LocalRefScoutBudget::default(),
)
.expect("first reconcile");
assert!(
first.published.contains(&doomed_id),
"bare branch published"
);
let registry_after_publish = handle.published_source_set().registry_generation;
assert!(
handle
.published_source_set()
.sources
.contains_key(&doomed_id),
"the bare branch lane exists before deletion"
);
repository
.find_branch("doomed", git2::BranchType::Local)
.expect("find branch")
.delete()
.expect("delete branch");
let second = reconcile_local_ref_topology(
&handle,
&repository,
repository_id.clone(),
&LocalRefScoutBudget::default(),
)
.expect("second reconcile");
assert!(
second.removed.contains(&doomed_id),
"the deleted branch's lane is removed"
);
let set = handle.published_source_set();
assert!(
!set.sources.contains_key(&doomed_id),
"the deleted branch's lane is gone"
);
assert!(
set.registry_generation > registry_after_publish,
"removing a lane advances registry_generation"
);
}
#[test]
fn reconcile_ingests_offline_with_no_remote_configured() {
let dir = tempfile::tempdir().expect("tempdir");
let root = dir.path();
init_repo(root);
commit_files(root, &[("src/lib.rs", b"pub fn offline() {}\n")], "initial");
let repository = git2::Repository::open(root).expect("open");
let head_commit = repository
.head()
.expect("head")
.peel_to_commit()
.expect("peel");
repository
.branch("offline-ref", &head_commit, false)
.expect("branch");
assert!(
repository.remotes().expect("remotes").is_empty(),
"the fixture genuinely has no git remote"
);
let handle = SharedIndexHandle::new(LiveIndex::from_source_files(HashMap::new()));
let repository_id = RepositoryId::new("repo-offline");
let outcome = reconcile_local_ref_topology(
&handle,
&repository,
repository_id.clone(),
&LocalRefScoutBudget::default(),
)
.expect("offline reconcile succeeds without a remote");
let ref_id = SourceId::new(format!(
"symforge:git-ref:{}:refs/heads/offline-ref",
repository_id.as_str()
));
assert!(
outcome.published.contains(&ref_id),
"the offline ref lane is published without any fetch"
);
assert!(
handle.published_source_set().sources[&ref_id]
.live
.files
.contains_key("src/lib.rs"),
"the offline ref lane is queryable"
);
}
fn commit_explicit_paths(root: &Path, files: &[(&str, &[u8])], message: &str) {
let repository = git2::Repository::open(root).expect("open repo");
let mut index = repository.index().expect("index");
for (relative, bytes) in files {
let full = root.join(relative);
if let Some(parent) = full.parent() {
std::fs::create_dir_all(parent).expect("mkdir");
}
std::fs::write(&full, bytes).expect("write file");
index.add_path(Path::new(relative)).expect("stage path");
}
index.write().expect("write index");
let tree_id = index.write_tree().expect("write tree");
let tree = repository.find_tree(tree_id).expect("tree");
let signature =
git2::Signature::now("SymForge Test", "symforge@example.invalid").expect("sig");
let parent = repository
.head()
.ok()
.and_then(|head| head.peel_to_commit().ok());
let parents: Vec<&git2::Commit<'_>> = parent.iter().collect();
repository
.commit(
Some("HEAD"),
&signature,
&signature,
message,
&tree,
&parents,
)
.expect("commit");
}
#[test]
fn sensitive_path_blob_is_withheld_by_path_even_when_content_is_clean() {
let dir = tempfile::tempdir().expect("tempdir");
let root = dir.path();
init_repo(root);
let env_bytes: &[u8] = b"API_KEY=your_api_key_here\n";
commit_explicit_paths(
root,
&[
(".env", env_bytes),
(".ssh/id_ed25519", b"benign key material placeholder\n"),
("notes.txt", env_bytes),
("src/lib.rs", b"pub fn kept() {}\n"),
],
"initial",
);
let repository = git2::Repository::open(root).expect("open");
let catalog =
scout_local_ref(&repository, "HEAD", &LocalRefScoutBudget::default()).expect("scout");
let blobs = materialize_ingest_blobs(&repository, &catalog).expect("materialize");
let route = |relative: &str| {
let entry = catalog
.entries
.iter()
.find(|entry| entry.relative_path == relative)
.expect("entry present");
let bytes = blobs.get(&entry.object_id).expect("bytes").to_vec();
route_ref_blob(entry, bytes)
};
match route(".env") {
RefBlobIngest::Withheld(MetadataOnlyReason::SensitivePath { rule_id }) => {
assert_eq!(rule_id, "path.environment-credentials");
}
other => panic!("`.env` must be withheld by sensitive path, got {other:?}"),
}
match route(".ssh/id_ed25519") {
RefBlobIngest::Withheld(MetadataOnlyReason::SensitivePath { rule_id }) => {
assert_eq!(rule_id, "path.private-key-material");
}
other => panic!("`id_ed25519` must be withheld by sensitive path, got {other:?}"),
}
assert!(
matches!(route("notes.txt"), RefBlobIngest::Indexed { .. }),
"identical env bytes at a benign path must be admitted"
);
let index = build_ref_source_index(&repository, &catalog).expect("build index");
assert!(
!index.files.contains_key(".env"),
"the sensitive `.env` blob must never enter the ref index"
);
assert!(
!index.files.contains_key(".ssh/id_ed25519"),
"the private-key blob must never enter the ref index"
);
assert!(
index.files.contains_key("src/lib.rs"),
"clean code stays indexed"
);
assert!(
index.files.contains_key("notes.txt"),
"clean env-at-benign-path stays indexed"
);
}
#[test]
fn dependency_lockfile_ref_blob_is_withheld_matching_filesystem_admission() {
let dir = tempfile::tempdir().expect("tempdir");
let outcome = route_single(
dir.path(),
"deps/package-lock.json",
b"{\n \"name\": \"x\",\n \"lockfileVersion\": 3\n}\n",
);
assert!(
matches!(
outcome,
RefBlobIngest::Withheld(MetadataOnlyReason::Lockfile)
),
"a committed lockfile must be withheld as Lockfile, got {outcome:?}"
);
}
#[test]
fn oversized_data_ref_blob_is_withheld_from_odb_header_matching_filesystem() {
let dir = tempfile::tempdir().expect("tempdir");
let big_csv = vec![b'a'; 2 * 1024 * 1024];
let outcome = route_single(dir.path(), "data/export.csv", &big_csv);
assert!(
matches!(
outcome,
RefBlobIngest::Withheld(MetadataOnlyReason::OversizedData)
),
"a 2 MiB .csv must be withheld as OversizedData, got {outcome:?}"
);
}
#[test]
fn binary_ref_blob_is_withheld_via_shared_sniff_matching_filesystem() {
let dir = tempfile::tempdir().expect("tempdir");
let binary: &[u8] = b"\x00\x01\x02BINARY\x00PAYLOAD\x00\xff\xfe";
let outcome = route_single(dir.path(), "assets/blob.dat", binary);
assert!(
matches!(outcome, RefBlobIngest::Withheld(MetadataOnlyReason::Binary)),
"a small binary blob must be withheld as Binary, got {outcome:?}"
);
}
#[test]
fn reconcile_deletion_pass_runs_despite_a_branch_publish_failure() {
let dir = tempfile::tempdir().expect("tempdir");
let root = dir.path();
init_repo(root);
commit_files(root, &[("src/lib.rs", b"pub fn base() {}\n")], "initial");
let repository = git2::Repository::open(root).expect("open");
let head_commit = repository
.head()
.expect("head")
.peel_to_commit()
.expect("peel");
repository
.branch("doomed", &head_commit, false)
.expect("doomed branch");
let handle = SharedIndexHandle::new(LiveIndex::from_source_files(HashMap::new()));
let repository_id = RepositoryId::new("repo-resilient");
let doomed_id = SourceId::new(format!(
"symforge:git-ref:{}:refs/heads/doomed",
repository_id.as_str()
));
let first = reconcile_local_ref_topology(
&handle,
&repository,
repository_id.clone(),
&LocalRefScoutBudget::default(),
)
.expect("first reconcile");
assert!(
first.published.contains(&doomed_id),
"doomed lane published"
);
let blob_oid = repository.blob(b"not a commit\n").expect("blob");
repository
.reference("refs/heads/broken", blob_oid, true, "broken ref")
.expect("broken ref");
repository
.find_branch("doomed", git2::BranchType::Local)
.expect("find doomed")
.delete()
.expect("delete doomed");
let second = reconcile_local_ref_topology(
&handle,
&repository,
repository_id.clone(),
&LocalRefScoutBudget::default(),
)
.expect("second reconcile still succeeds");
assert!(
second
.failed
.iter()
.any(|(name, _)| name == "refs/heads/broken"),
"the broken branch's publish failure is collected, not fatal: {:?}",
second.failed
);
assert!(
second.removed.contains(&doomed_id),
"the deletion pass still ran despite the publish failure"
);
assert!(
!handle
.published_source_set()
.sources
.contains_key(&doomed_id),
"the stale lane for the deleted branch is gone"
);
}
#[test]
fn moved_ref_publish_failure_removes_previous_lane() {
let dir = tempfile::tempdir().expect("tempdir");
let root = dir.path();
init_repo(root);
commit_files(root, &[("src/lib.rs", b"pub fn base() {}\n")], "initial");
let repository = git2::Repository::open(root).expect("open");
let head_commit = repository
.head()
.expect("head")
.peel_to_commit()
.expect("peel");
repository
.branch("bare", &head_commit, false)
.expect("bare branch");
let handle = SharedIndexHandle::new(LiveIndex::from_source_files(HashMap::new()));
let repository_id = RepositoryId::new("repo-stale-tip");
let bare_id = SourceId::new(format!(
"symforge:git-ref:{}:refs/heads/bare",
repository_id.as_str()
));
reconcile_local_ref_topology(
&handle,
&repository,
repository_id.clone(),
&LocalRefScoutBudget::default(),
)
.expect("first reconcile");
assert!(
handle.published_source_set().sources.contains_key(&bare_id),
"the bare lane is published before the ref moves"
);
let blob_oid = repository.blob(b"not a commit\n").expect("blob");
repository
.reference(
"refs/heads/bare",
blob_oid,
true,
"move branch to bad object",
)
.expect("move branch ref");
let second = reconcile_local_ref_topology(
&handle,
&repository,
repository_id.clone(),
&LocalRefScoutBudget::default(),
)
.expect("publish failure is collected, not fatal");
assert!(
second
.failed
.iter()
.any(|(name, _)| name == "refs/heads/bare"),
"the moved bad ref's publish failure is reported: {:?}",
second.failed
);
assert!(
second.removed.contains(&bare_id),
"the previous lane for the moved bad ref is invalidated"
);
assert!(
!handle.published_source_set().sources.contains_key(&bare_id),
"the stale lane is not left queryable after the tip moved"
);
}
#[test]
fn reconcile_from_a_linked_worktree_excludes_the_main_worktree_branch() {
let dir = tempfile::tempdir().expect("tempdir");
let root = dir.path();
init_repo(root);
commit_files(root, &[("src/lib.rs", b"pub fn base() {}\n")], "initial");
let main = git2::Repository::open(root).expect("open main");
let main_ref = main
.head()
.expect("head")
.name()
.expect("head name")
.to_string();
let wt_parent = tempfile::tempdir().expect("wt tempdir");
let wt_path = wt_parent.path().join("feature-wt");
add_worktree_on_branch(&main, &wt_path, "feature-wt", "feature");
let worktree_repo = git2::Repository::open(&wt_path).expect("open worktree");
assert!(
worktree_repo.is_worktree(),
"the project is opened at a linked worktree"
);
let handle = SharedIndexHandle::new(LiveIndex::from_source_files(HashMap::new()));
let repository_id = RepositoryId::new("repo-wt-open");
let outcome = reconcile_local_ref_topology(
&handle,
&worktree_repo,
repository_id.clone(),
&LocalRefScoutBudget::default(),
)
.expect("reconcile from a linked worktree");
let main_id = SourceId::new(format!(
"symforge:git-ref:{}:{main_ref}",
repository_id.as_str()
));
let feature_id = SourceId::new(format!(
"symforge:git-ref:{}:refs/heads/feature",
repository_id.as_str()
));
let set = handle.published_source_set();
assert!(
!set.sources.contains_key(&main_id),
"the MAIN worktree branch is never a P1 lane when reconciling from a linked worktree"
);
assert!(
!outcome.published.contains(&main_id),
"the main branch is not among the published lanes"
);
assert!(
!set.sources.contains_key(&feature_id),
"the linked worktree's own branch is also not a P1 lane"
);
}
#[test]
fn fail_closed_topology_error_purges_existing_ref_lanes() {
let dir = tempfile::tempdir().expect("tempdir");
let root = dir.path();
init_repo(root);
commit_files(root, &[("src/lib.rs", b"pub fn base() {}\n")], "initial");
let repository = git2::Repository::open(root).expect("open");
let head_commit = repository
.head()
.expect("head")
.peel_to_commit()
.expect("peel");
repository
.branch("bare", &head_commit, false)
.expect("bare branch");
let handle = SharedIndexHandle::new(LiveIndex::from_source_files(HashMap::new()));
let repository_id = RepositoryId::new("repo-topology-error");
let bare_id = SourceId::new(format!(
"symforge:git-ref:{}:refs/heads/bare",
repository_id.as_str()
));
reconcile_local_ref_topology(
&handle,
&repository,
repository_id.clone(),
&LocalRefScoutBudget::default(),
)
.expect("first reconcile");
assert!(
handle.published_source_set().sources.contains_key(&bare_id),
"the bare lane is published on the clean pass"
);
let error = fail_closed_topology_error(
&handle,
&repository_id,
"Error: local branches are unavailable.".to_string(),
);
assert_eq!(error, "Error: local branches are unavailable.");
assert!(
!handle.published_source_set().sources.contains_key(&bare_id),
"the old ref lane is purged when topology cannot be read"
);
}
#[test]
fn fail_closed_reconcile_purges_stale_ref_lanes() {
let dir = tempfile::tempdir().expect("tempdir");
let root = dir.path();
init_repo(root);
commit_files(root, &[("src/lib.rs", b"pub fn base() {}\n")], "initial");
let repository = git2::Repository::open(root).expect("open");
let head_commit = repository
.head()
.expect("head")
.peel_to_commit()
.expect("peel");
repository
.branch("bare", &head_commit, false)
.expect("bare branch");
let handle = SharedIndexHandle::new(LiveIndex::from_source_files(HashMap::new()));
let repository_id = RepositoryId::new("repo-failclosed");
let bare_id = SourceId::new(format!(
"symforge:git-ref:{}:refs/heads/bare",
repository_id.as_str()
));
reconcile_local_ref_topology(
&handle,
&repository,
repository_id.clone(),
&LocalRefScoutBudget::default(),
)
.expect("first reconcile");
assert!(
handle.published_source_set().sources.contains_key(&bare_id),
"the bare lane is published on the clean pass"
);
let wt_parent = tempfile::tempdir().expect("wt tempdir");
let wt_path = wt_parent.path().join("checked");
add_worktree_on_branch(&repository, &wt_path, "checked-wt", "checked");
let admin_head = root
.join(".git")
.join("worktrees")
.join("checked-wt")
.join("HEAD");
std::fs::write(&admin_head, b"\x00\x01 not a ref\n").expect("corrupt worktree HEAD");
let result = reconcile_local_ref_topology(
&handle,
&repository,
repository_id.clone(),
&LocalRefScoutBudget::default(),
);
assert!(
result.is_err(),
"an unprovable topology fails the reconcile closed, got {result:?}"
);
assert!(
!handle.published_source_set().sources.contains_key(&bare_id),
"the pre-existing bare P1 lane is purged on the fail-closed abort"
);
}
#[test]
fn reconcile_does_not_publish_a_moved_worktree_branch() {
let dir = tempfile::tempdir().expect("tempdir");
let root = dir.path();
init_repo(root);
commit_files(root, &[("src/lib.rs", b"pub fn base() {}\n")], "initial");
let repository = git2::Repository::open(root).expect("open");
let wt_parent = tempfile::tempdir().expect("wt tempdir");
let wt_path = wt_parent.path().join("moved-wt");
add_worktree_on_branch(&repository, &wt_path, "moved-wt", "moved");
std::fs::remove_dir_all(&wt_path).expect("remove worktree working dir");
let handle = SharedIndexHandle::new(LiveIndex::from_source_files(HashMap::new()));
let repository_id = RepositoryId::new("repo-moved");
let outcome = reconcile_local_ref_topology(
&handle,
&repository,
repository_id.clone(),
&LocalRefScoutBudget::default(),
)
.expect("reconcile with a moved worktree");
let moved_id = SourceId::new(format!(
"symforge:git-ref:{}:refs/heads/moved",
repository_id.as_str()
));
assert!(
!outcome.published.contains(&moved_id),
"the moved worktree's branch is not published as a bare lane"
);
assert!(
!handle
.published_source_set()
.sources
.contains_key(&moved_id),
"no P1 lane exists for the moved worktree's branch"
);
assert!(
outcome
.checked_out
.iter()
.any(|w| w.head_ref.as_deref() == Some("refs/heads/moved")),
"the moved worktree is surfaced as checked-out via its admin HEAD"
);
}
#[test]
fn unchanged_branch_tip_skips_republish() {
let dir = tempfile::tempdir().expect("tempdir");
let root = dir.path();
init_repo(root);
commit_files(root, &[("src/lib.rs", b"pub fn base() {}\n")], "initial");
let repository = git2::Repository::open(root).expect("open");
let head_commit = repository
.head()
.expect("head")
.peel_to_commit()
.expect("peel");
repository
.branch("bare", &head_commit, false)
.expect("bare branch");
let handle = SharedIndexHandle::new(LiveIndex::from_source_files(HashMap::new()));
let repository_id = RepositoryId::new("repo-s1");
let bare_id = SourceId::new(format!(
"symforge:git-ref:{}:refs/heads/bare",
repository_id.as_str()
));
let first = reconcile_local_ref_topology(
&handle,
&repository,
repository_id.clone(),
&LocalRefScoutBudget::default(),
)
.expect("first reconcile");
assert!(
first.published.contains(&bare_id),
"the first pass publishes the bare lane"
);
let gen_after_first = handle
.published_source_set()
.sources
.get(&bare_id)
.expect("lane present after first pass")
.publication_generation;
let second = reconcile_local_ref_topology(
&handle,
&repository,
repository_id.clone(),
&LocalRefScoutBudget::default(),
)
.expect("second reconcile");
assert!(
!second.published.contains(&bare_id),
"an unchanged tip is NOT republished"
);
let gen_after_second = handle
.published_source_set()
.sources
.get(&bare_id)
.expect("lane still present after second pass")
.publication_generation;
assert_eq!(
gen_after_first, gen_after_second,
"publication_generation is untouched when the tip did not move"
);
}
#[test]
fn ref_tip_move_advances_lane_generations_without_touching_current() {
let dir = tempfile::tempdir().expect("tempdir");
let root = dir.path();
init_repo(root);
commit_files(root, &[("src/lib.rs", b"pub fn a() {}\n")], "initial");
let repository = git2::Repository::open(root).expect("open");
let handle = SharedIndexHandle::new(LiveIndex::from_source_files(HashMap::new()));
let current_before = handle.published_source_set().current_generation();
let cur_pub = current_before.publication_generation;
let cur_content = current_before.content_generation;
let id = ingest_and_publish_local_ref(
&handle,
&repository,
"HEAD",
RepositoryId::new("repo-gen"),
&LocalRefScoutBudget::default(),
)
.expect("publish ref");
let set1 = handle.published_source_set();
let lane1 = set1.sources.get(&id).expect("ref lane");
let pub1 = lane1.publication_generation;
let content1 = lane1.content_generation;
assert!(
pub1 > 0,
"the ref lane carries a meaningful publication generation, got {pub1}"
);
assert!(
content1 > 0,
"the ref lane carries a meaningful content generation, got {content1}"
);
commit_files(root, &[("src/b.rs", b"pub fn b() {}\n")], "second");
ingest_and_publish_local_ref(
&handle,
&repository,
"HEAD",
RepositoryId::new("repo-gen"),
&LocalRefScoutBudget::default(),
)
.expect("republish moved ref");
let set2 = handle.published_source_set();
let lane2 = set2.sources.get(&id).expect("ref lane");
assert!(
lane2.publication_generation > pub1,
"a tip move advances the lane publication generation"
);
assert!(
lane2.content_generation > content1,
"a tip move advances the lane content generation"
);
let pub2 = lane2.publication_generation;
let content2 = lane2.content_generation;
ingest_and_publish_local_ref(
&handle,
&repository,
"HEAD",
RepositoryId::new("repo-gen"),
&LocalRefScoutBudget::default(),
)
.expect("republish same tip");
let set3 = handle.published_source_set();
let lane3 = set3.sources.get(&id).expect("ref lane");
assert!(
lane3.publication_generation > pub2,
"an identical-tip republish still advances publication generation"
);
assert_eq!(
lane3.content_generation, content2,
"an identical-tip republish keeps content generation stable"
);
let current_after = set3.current_generation();
assert_eq!(
current_after.publication_generation, cur_pub,
"a P1 republish must not advance the current lane publication generation"
);
assert_eq!(
current_after.content_generation, cur_content,
"a P1 republish must not advance the current lane content generation"
);
}
#[test]
fn unresolved_worktree_head_fails_reconcile_closed_and_publishes_nothing() {
let dir = tempfile::tempdir().expect("tempdir");
let root = dir.path();
init_repo(root);
commit_files(root, &[("src/lib.rs", b"pub fn base() {}\n")], "initial");
let repository = git2::Repository::open(root).expect("open");
let head_commit = repository
.head()
.expect("head")
.peel_to_commit()
.expect("peel");
repository
.branch("bare", &head_commit, false)
.expect("bare branch");
let wt_parent = tempfile::tempdir().expect("wt tempdir");
let wt_path = wt_parent.path().join("wt");
add_worktree_on_branch(&repository, &wt_path, "wt", "wt-branch");
let admin_head = repository.path().join("worktrees").join("wt").join("HEAD");
std::fs::write(&admin_head, b"ref: refs/heads/ghost-does-not-exist\n")
.expect("corrupt worktree HEAD");
let checked_out = checked_out_worktrees(&repository).expect("classify");
let wt = checked_out
.iter()
.find(|w| w.name == "wt")
.expect("worktree present");
assert!(
!wt.head_resolved,
"an unreadable worktree HEAD must be marked unresolved"
);
assert_eq!(wt.head_ref, None);
let handle = SharedIndexHandle::new(LiveIndex::from_source_files(HashMap::new()));
let repository_id = RepositoryId::new("repo-failclosed");
let result = reconcile_local_ref_topology(
&handle,
&repository,
repository_id.clone(),
&LocalRefScoutBudget::default(),
);
assert!(
result.is_err(),
"an unresolved worktree HEAD must fail the reconcile closed, got {result:?}"
);
let bare_id = SourceId::new(format!(
"symforge:git-ref:{}:refs/heads/bare",
repository_id.as_str()
));
assert!(
!handle.published_source_set().sources.contains_key(&bare_id),
"no bare lane is published when the worktree topology is unprovable"
);
}
#[test]
fn empty_repo_unborn_head_reconciles_cleanly_without_failing_closed() {
let dir = tempfile::tempdir().expect("tempdir");
let root = dir.path();
init_repo(root); let repository = git2::Repository::open(root).expect("open");
let handle = SharedIndexHandle::new(LiveIndex::from_source_files(HashMap::new()));
let before = handle.published_source_set().registry_generation;
let outcome = reconcile_local_ref_topology(
&handle,
&repository,
RepositoryId::new("repo-unborn"),
&LocalRefScoutBudget::default(),
)
.expect("an unborn main HEAD must reconcile cleanly, not fail closed");
assert!(
outcome.published.is_empty(),
"unborn repo publishes no lanes"
);
assert!(!outcome.skipped);
assert_eq!(
handle.published_source_set().registry_generation,
before,
"an unborn repo leaves the registry unchanged"
);
}
#[test]
fn concurrent_reconcile_is_single_flighted_and_skips() {
let dir = tempfile::tempdir().expect("tempdir");
let root = dir.path();
init_repo(root);
commit_files(root, &[("src/lib.rs", b"pub fn base() {}\n")], "initial");
{
let repository = git2::Repository::open(root).expect("open");
let head_commit = repository
.head()
.expect("head")
.peel_to_commit()
.expect("peel");
repository
.branch("bare", &head_commit, false)
.expect("bare branch");
}
let handle = SharedIndexHandle::new(LiveIndex::from_source_files(HashMap::new()));
let repository_id = RepositoryId::new("repo-singleflight");
let bare_id = SourceId::new(format!(
"symforge:git-ref:{}:refs/heads/bare",
repository_id.as_str()
));
let contender = std::thread::scope(|scope| {
let guard = handle
.try_lock_ref_reconcile()
.expect("acquire the reconcile guard");
let outcome = scope
.spawn(|| {
let repo = git2::Repository::open(root).expect("open in contender");
reconcile_local_ref_topology(
&handle,
&repo,
repository_id.clone(),
&LocalRefScoutBudget::default(),
)
.expect("the contender reconcile returns Ok(skipped)")
})
.join()
.expect("join contender");
drop(guard);
outcome
});
assert!(
contender.skipped,
"a reconcile attempted concurrently while the guard is held must skip"
);
assert!(
contender.published.is_empty() && contender.removed.is_empty(),
"the skipped pass mutates nothing"
);
assert!(
!handle.published_source_set().sources.contains_key(&bare_id),
"the skipped pass published no lane"
);
let runner = std::thread::scope(|scope| {
scope
.spawn(|| {
let repo = git2::Repository::open(root).expect("open in runner");
reconcile_local_ref_topology(
&handle,
&repo,
repository_id.clone(),
&LocalRefScoutBudget::default(),
)
.expect("the guard-free reconcile runs")
})
.join()
.expect("join runner")
});
assert!(
!runner.skipped,
"the guard-free pass runs (skipped == false)"
);
assert!(
runner.published.contains(&bare_id),
"the runner publishes the bare lane"
);
assert!(
handle.published_source_set().sources.contains_key(&bare_id),
"the final lane set is correct"
);
}
#[cfg(feature = "server")]
#[test]
fn source_isolation_holds_through_scoped_query_composition() {
use crate::protocol::knowledge_review::review_scoped;
use crate::protocol::knowledge_search::search_scoped;
use crate::protocol::search_tools::{
KnowledgeSourceScope, ReviewKnowledgeInput, ReviewKnowledgeMode, SearchKnowledgeInput,
};
let dir = tempfile::tempdir().expect("tempdir");
let root = dir.path();
init_repo(root);
commit_files(
root,
&[(
"docs/current-only.md",
b"# Current Only\n\nThe quantum meridian anchors the current worktree lane.\n",
)],
"current",
);
let repository = git2::Repository::open(root).expect("open");
commit_bare_branch_with_doc(
&repository,
"ref-only",
"docs",
"ref-only.md",
b"# Ref Only\n\nThe helical cascade governs the divergent ref lane.\n",
);
let handle = LiveIndex::load(root).expect("load current lane");
ingest_and_publish_local_ref(
&handle,
&repository,
"refs/heads/ref-only",
RepositoryId::new("repo-compose-iso"),
&LocalRefScoutBudget::default(),
)
.expect("publish ref lane");
let set = handle.published_source_set();
let search = |scope, query: &str| {
search_scoped(
&set,
&SearchKnowledgeInput {
query: query.to_string(),
path_prefix: None,
source_scope: Some(scope),
authority_scope: None,
project: None,
projects: None,
limit: None,
max_tokens: None,
},
)
};
assert!(
search(KnowledgeSourceScope::Current, "quantum meridian")
.contains("docs/current-only.md"),
"current scope must find its own document"
);
assert!(
search(KnowledgeSourceScope::LocalRefs, "helical cascade").contains("docs/ref-only.md"),
"local_refs scope must find its own document"
);
let current_for_ref_term = search(KnowledgeSourceScope::Current, "helical cascade");
assert!(
!current_for_ref_term.contains("docs/ref-only.md"),
"current scope leaked a ref-lane document: {current_for_ref_term}"
);
let refs_for_current_term = search(KnowledgeSourceScope::LocalRefs, "quantum meridian");
assert!(
!refs_for_current_term.contains("docs/current-only.md"),
"local_refs scope leaked the current document: {refs_for_current_term}"
);
let review = |scope| {
review_scoped(
&set,
&ReviewKnowledgeInput {
mode: ReviewKnowledgeMode::Summary,
path: None,
path_prefix: None,
source_scope: Some(scope),
project: None,
projects: None,
limit: None,
max_tokens: None,
},
)
};
let refs_review = review(KnowledgeSourceScope::LocalRefs).expect("local_refs review");
assert!(
!refs_review.rendered.contains("docs/current-only.md"),
"local_refs review leaked the current document: {}",
refs_review.rendered
);
let current_review = review(KnowledgeSourceScope::Current).expect("current review");
assert!(
!current_review.rendered.contains("docs/ref-only.md"),
"current review leaked a ref-lane document: {}",
current_review.rendered
);
}
struct EnvVarGuard {
key: &'static str,
previous: Option<std::ffi::OsString>,
}
#[allow(unsafe_code)] impl EnvVarGuard {
fn set_os(key: &'static str, value: &std::ffi::OsStr) -> Self {
let previous = std::env::var_os(key);
unsafe {
std::env::set_var(key, value);
}
Self { key, previous }
}
fn set_str(key: &'static str, value: &str) -> Self {
Self::set_os(key, value.as_ref())
}
fn unset(key: &'static str) -> Self {
let previous = std::env::var_os(key);
unsafe {
std::env::remove_var(key);
}
Self { key, previous }
}
}
#[allow(unsafe_code)] impl Drop for EnvVarGuard {
fn drop(&mut self) {
match &self.previous {
Some(previous) => unsafe {
std::env::set_var(self.key, previous);
},
None => unsafe {
std::env::remove_var(self.key);
},
}
}
}
fn write_git_spawn_canary_stubs(bin_dir: &Path, canary: &Path) {
let canary_display = canary.to_string_lossy();
let batch = format!("@echo off\r\necho spawned >> \"{canary_display}\"\r\nexit /b 0\r\n");
for name in ["git.bat", "git-lfs.bat", "git.cmd", "git-lfs.cmd"] {
std::fs::write(bin_dir.join(name), batch.as_bytes())
.expect("write windows canary stub");
}
let script = format!("#!/bin/sh\necho spawned >> \"{canary_display}\"\nexit 0\n");
for name in ["git", "git-lfs"] {
let path = bin_dir.join(name);
std::fs::write(&path, script.as_bytes()).expect("write unix canary stub");
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
let mut perms = std::fs::metadata(&path)
.expect("stub metadata")
.permissions();
perms.set_mode(0o755);
std::fs::set_permissions(&path, perms).expect("chmod canary stub");
}
}
}
#[test]
#[ignore]
fn process_spawn_spy_confirms_offline_ingestion_never_shells_out() {
let bin_dir = tempfile::tempdir().expect("bin tempdir");
let canary = bin_dir.path().join("spawned.canary");
write_git_spawn_canary_stubs(bin_dir.path(), &canary);
let previous_path = std::env::var_os("PATH").unwrap_or_default();
let mut search = bin_dir.path().as_os_str().to_owned();
search.push(if cfg!(windows) { ";" } else { ":" });
search.push(&previous_path);
let _path = EnvVarGuard::set_os("PATH", &search);
let _exec = EnvVarGuard::set_os("GIT_EXEC_PATH", bin_dir.path().as_os_str());
let _prompt = EnvVarGuard::set_str("GIT_TERMINAL_PROMPT", "0");
let dir = tempfile::tempdir().expect("repo tempdir");
let root = dir.path();
init_repo(root);
commit_files(
root,
&[
("src/lib.rs", b"pub fn spy() {}\n"),
("docs/readme.md", b"# spy\n"),
],
"initial",
);
let repository = git2::Repository::open(root).expect("open");
let head_commit = repository
.head()
.expect("head")
.peel_to_commit()
.expect("peel");
repository
.branch("spy-ref", &head_commit, false)
.expect("branch");
assert!(
repository.remotes().expect("remotes").is_empty(),
"the spy fixture has no git remote — a successful ingest needs no fetch"
);
let handle = SharedIndexHandle::new(LiveIndex::from_source_files(HashMap::new()));
let repository_id = RepositoryId::new("repo-spy");
let outcome = reconcile_local_ref_topology(
&handle,
&repository,
repository_id.clone(),
&LocalRefScoutBudget::default(),
)
.expect("offline reconcile succeeds under the process-spawn spy");
let ref_id = SourceId::new(format!(
"symforge:git-ref:{}:refs/heads/spy-ref",
repository_id.as_str()
));
assert!(
outcome.published.contains(&ref_id),
"the local ref lane is published — the full ingest path actually ran"
);
assert!(
!canary.exists(),
"L-R05: a git/git-lfs subprocess was spawned during local-ref ingestion \
(canary file {} exists) — this path must be pure libgit2 FFI with no \
Git/LFS child process",
canary.display()
);
}
#[test]
#[ignore]
fn local_ref_gate_default_path_cost_is_negligible() {
const LOCAL_REF_LANES_ENV: &str = "SYMFORGE_LOCAL_REF_LANES";
let _gate = EnvVarGuard::unset(LOCAL_REF_LANES_ENV);
let gate_read = || {
std::env::var(LOCAL_REF_LANES_ENV)
.ok()
.is_some_and(|value| {
matches!(
value.trim().to_ascii_lowercase().as_str(),
"1" | "true" | "on"
)
})
};
const ITERATIONS: u32 = 100_000;
for _ in 0..1_000 {
std::hint::black_box(gate_read());
}
let mut enabled = 0u64;
let start = std::time::Instant::now();
for _ in 0..ITERATIONS {
if std::hint::black_box(gate_read()) {
enabled += 1;
}
}
let elapsed = start.elapsed();
assert_eq!(
std::hint::black_box(enabled),
0,
"gate is OFF by default, so no iteration reports enabled"
);
let per_call_ns = elapsed.as_nanos() as f64 / f64::from(ITERATIONS);
println!(
"L-V02 default-path gate read: {ITERATIONS} iterations in {elapsed:?} = \
{per_call_ns:.1} ns/call"
);
assert!(
per_call_ns < 50_000.0,
"L-V02: default-path gate read averaged {per_call_ns:.1} ns/call, over the \
50000 ns budget — the current-only default path gained non-negligible cost"
);
}
}