use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::io;
use std::path::{Path, PathBuf};
use std::process::Command;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
pub struct Context {
pub flow_id: Option<String>,
pub labels: Vec<String>,
pub params: BTreeMap<String, String>,
}
#[derive(Debug, Clone, PartialEq)]
pub enum WorktreeScope {
Primary,
Linked { key: String },
}
#[derive(Debug, Clone)]
pub struct ContextPaths {
pub tracevault_dir: PathBuf,
pub scope: WorktreeScope,
}
impl ContextPaths {
pub fn global_path(&self) -> PathBuf {
self.tracevault_dir.join("context.json")
}
pub fn worktree_path(&self) -> Option<PathBuf> {
match &self.scope {
WorktreeScope::Primary => None,
WorktreeScope::Linked { key } => Some(
self.tracevault_dir
.join("worktrees")
.join(key)
.join("context.json"),
),
}
}
}
fn git_rev_parse(start_dir: &Path) -> Option<(PathBuf, PathBuf)> {
let output = Command::new("git")
.args([
"-C",
&start_dir.to_string_lossy(),
"rev-parse",
"--git-common-dir",
"--git-dir",
])
.output()
.ok()?;
if !output.status.success() {
return None;
}
let stdout = std::str::from_utf8(&output.stdout).ok()?;
let mut lines = stdout.lines();
let raw_common = lines.next()?.trim();
let raw_dir = lines.next()?.trim();
let canonicalize = |raw: &str| -> Option<PathBuf> {
let p = PathBuf::from(raw);
if p.is_absolute() {
p.canonicalize().ok()
} else {
start_dir.join(&p).canonicalize().ok()
}
};
let git_common_dir = canonicalize(raw_common)?;
let git_dir = canonicalize(raw_dir)?;
Some((git_common_dir, git_dir))
}
pub fn resolve_context_paths(start_dir: &Path) -> ContextPaths {
if let Some((git_common_dir, git_dir)) = git_rev_parse(start_dir) {
if let Some(primary_root) = git_common_dir.parent() {
let tracevault_dir = primary_root.join(".tracevault");
let scope = if git_dir == git_common_dir {
WorktreeScope::Primary
} else {
let key = git_dir
.file_name()
.map(|n| n.to_string_lossy().into_owned())
.unwrap_or_else(|| "default".to_string());
WorktreeScope::Linked { key }
};
return ContextPaths {
tracevault_dir,
scope,
};
}
}
let fallback = crate::paths::resolve_project_root(start_dir);
ContextPaths {
tracevault_dir: fallback.root.join(".tracevault"),
scope: WorktreeScope::Primary,
}
}
impl Context {
pub fn load_from(path: &Path) -> Context {
let content = match std::fs::read_to_string(path) {
Ok(c) => c,
Err(e) if e.kind() == io::ErrorKind::NotFound => return Context::default(),
Err(e) => {
eprintln!(
"tracevault: warning: could not read context file {}: {e}",
path.display()
);
return Context::default();
}
};
match serde_json::from_str::<Context>(&content) {
Ok(ctx) => ctx,
Err(e) => {
eprintln!(
"tracevault: warning: malformed context file {} (using empty context): {e}",
path.display()
);
Context::default()
}
}
}
pub fn save_to(&self, path: &Path) -> io::Result<()> {
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent)?;
}
let normalised = Self {
flow_id: self.flow_id.clone(),
labels: dedup_labels(&self.labels),
params: self.params.clone(),
};
let json = serde_json::to_string_pretty(&normalised)
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
std::fs::write(path, json)
}
pub fn load_global(paths: &ContextPaths) -> Context {
Self::load_from(&paths.global_path())
}
pub fn load_worktree(paths: &ContextPaths) -> Option<Context> {
let wt_path = paths.worktree_path()?;
if !wt_path.exists() {
return None;
}
Some(Self::load_from(&wt_path))
}
pub fn save_global(&self, paths: &ContextPaths) -> io::Result<()> {
self.save_to(&paths.global_path())
}
pub fn save_worktree(&self, paths: &ContextPaths) -> io::Result<()> {
let wt_path = paths.worktree_path().ok_or_else(|| {
io::Error::other("cannot save per-worktree context from primary checkout")
})?;
self.save_to(&wt_path)
}
pub fn merge_effective(global: &Context, worktree: Option<&Context>) -> Context {
let wt = match worktree {
Some(w) => w,
None => return global.clone(),
};
let flow_id = wt.flow_id.clone().or_else(|| global.flow_id.clone());
let combined_labels: Vec<String> = global
.labels
.iter()
.chain(wt.labels.iter())
.cloned()
.collect();
let labels = dedup_labels(&combined_labels);
let mut params = global.params.clone();
for (k, v) in &wt.params {
params.insert(k.clone(), v.clone());
}
Context {
flow_id,
labels,
params,
}
}
pub fn effective(start_dir: &Path) -> Context {
let paths = resolve_context_paths(start_dir);
let global = Self::load_global(&paths);
let worktree = Self::load_worktree(&paths);
Self::merge_effective(&global, worktree.as_ref())
}
pub fn effective_with_parts(
start_dir: &Path,
) -> (ContextPaths, Context, Option<Context>, Context) {
let paths = resolve_context_paths(start_dir);
let global = Self::load_global(&paths);
let worktree = Self::load_worktree(&paths);
let effective = Self::merge_effective(&global, worktree.as_ref());
(paths, global, worktree, effective)
}
}
fn dedup_labels(labels: &[String]) -> Vec<String> {
let mut seen = std::collections::HashSet::new();
labels
.iter()
.map(|l| l.trim().to_string())
.filter(|l| !l.is_empty())
.filter(|l| seen.insert(l.clone()))
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
use std::collections::BTreeMap;
#[test]
fn default_context_is_empty() {
let ctx = Context::default();
assert!(ctx.flow_id.is_none());
assert!(ctx.labels.is_empty());
assert!(ctx.params.is_empty());
}
#[test]
fn save_and_load_round_trip() {
let dir = tempfile::tempdir().unwrap();
let ctx_path = dir.path().join(".tracevault").join("context.json");
let mut params = BTreeMap::new();
params.insert("key1".to_string(), "value1".to_string());
params.insert("key2".to_string(), "value2".to_string());
let ctx = Context {
flow_id: Some("flow-abc-123".to_string()),
labels: vec!["backend".to_string(), "urgent".to_string()],
params,
};
ctx.save_to(&ctx_path).unwrap();
let loaded = Context::load_from(&ctx_path);
assert_eq!(ctx, loaded);
}
#[test]
fn load_missing_file_returns_default() {
let dir = tempfile::tempdir().unwrap();
let missing = dir.path().join(".tracevault").join("context.json");
let ctx = Context::load_from(&missing);
assert_eq!(ctx, Context::default());
}
#[test]
fn label_dedup_on_save() {
let dir = tempfile::tempdir().unwrap();
let ctx_path = dir.path().join(".tracevault").join("context.json");
let ctx = Context {
flow_id: None,
labels: vec![
"alpha".to_string(),
"beta".to_string(),
"alpha".to_string(), " ".to_string(), "gamma".to_string(),
"beta".to_string(), ],
params: BTreeMap::new(),
};
ctx.save_to(&ctx_path).unwrap();
let loaded = Context::load_from(&ctx_path);
assert_eq!(
loaded.labels,
vec!["alpha".to_string(), "beta".to_string(), "gamma".to_string()]
);
}
#[test]
fn load_malformed_json_returns_default() {
let dir = tempfile::tempdir().unwrap();
let tv_dir = dir.path().join(".tracevault");
std::fs::create_dir_all(&tv_dir).unwrap();
let ctx_path = tv_dir.join("context.json");
std::fs::write(&ctx_path, b"not valid json {{{{").unwrap();
let ctx = Context::load_from(&ctx_path);
assert_eq!(ctx, Context::default());
}
#[test]
fn save_creates_tracevault_dir_if_missing() {
let dir = tempfile::tempdir().unwrap();
let ctx_path = dir.path().join(".tracevault").join("context.json");
assert!(!dir.path().join(".tracevault").exists());
let ctx = Context::default();
ctx.save_to(&ctx_path).unwrap();
assert!(ctx_path.exists());
}
fn ctx(flow: Option<&str>, labels: &[&str], params: &[(&str, &str)]) -> Context {
Context {
flow_id: flow.map(str::to_string),
labels: labels.iter().map(|s| s.to_string()).collect(),
params: params
.iter()
.map(|(k, v)| (k.to_string(), v.to_string()))
.collect(),
}
}
#[test]
fn merge_no_worktree_returns_global() {
let global = ctx(Some("g"), &["a"], &[("k", "1")]);
let result = Context::merge_effective(&global, None);
assert_eq!(result, global);
}
#[test]
fn merge_worktree_flow_wins() {
let global = ctx(Some("g"), &[], &[]);
let wt = ctx(Some("w"), &[], &[]);
let result = Context::merge_effective(&global, Some(&wt));
assert_eq!(result.flow_id, Some("w".to_string()));
}
#[test]
fn merge_worktree_flow_none_falls_back_to_global() {
let global = ctx(Some("g"), &[], &[]);
let wt = ctx(None, &[], &[]);
let result = Context::merge_effective(&global, Some(&wt));
assert_eq!(result.flow_id, Some("g".to_string()));
}
#[test]
fn merge_labels_union_dedup_global_first() {
let global = ctx(None, &["a"], &[]);
let wt = ctx(None, &["b"], &[]);
let result = Context::merge_effective(&global, Some(&wt));
assert_eq!(result.labels, vec!["a", "b"]);
}
#[test]
fn merge_labels_no_duplicates() {
let global = ctx(None, &["a", "b"], &[]);
let wt = ctx(None, &["b", "c"], &[]);
let result = Context::merge_effective(&global, Some(&wt));
assert_eq!(result.labels, vec!["a", "b", "c"]);
}
#[test]
fn merge_params_worktree_overwrites_keys() {
let global = ctx(None, &[], &[("k", "1"), ("m", "old")]);
let wt = ctx(None, &[], &[("k", "2"), ("m", "3")]);
let result = Context::merge_effective(&global, Some(&wt));
assert_eq!(result.params["k"], "2");
assert_eq!(result.params["m"], "3");
}
#[test]
fn merge_params_global_keys_not_in_worktree_preserved() {
let global = ctx(None, &[], &[("g_only", "yes")]);
let wt = ctx(None, &[], &[("wt_only", "yes")]);
let result = Context::merge_effective(&global, Some(&wt));
assert_eq!(result.params["g_only"], "yes");
assert_eq!(result.params["wt_only"], "yes");
}
#[test]
fn merge_full_example_from_spec() {
let global = ctx(Some("g"), &["a"], &[("k", "1")]);
let wt = ctx(Some("w"), &["b"], &[("k", "2"), ("m", "3")]);
let result = Context::merge_effective(&global, Some(&wt));
assert_eq!(result.flow_id, Some("w".to_string()));
assert_eq!(result.labels, vec!["a", "b"]);
assert_eq!(result.params["k"], "2");
assert_eq!(result.params["m"], "3");
}
}