use crate::config::{TracevaultConfig, UserContext};
use crate::context::{resolve_context_paths, Context, WorktreeScope};
use crate::program::TOP;
use std::io;
use std::path::{Path, PathBuf};
pub fn find_project_root(start: &Path) -> Result<PathBuf, Box<dyn std::error::Error>> {
for ancestor in start.ancestors() {
if crate::layout::has_repo_dir(ancestor) {
return Ok(ancestor.to_path_buf());
}
}
Err(format!(
"No .visdom/ directory found in '{}' or any parent directory. \
Run `{TOP} init` first.",
start.display()
)
.into())
}
fn parse_param(raw: &str) -> Result<(String, String), Box<dyn std::error::Error>> {
let pos = raw
.find('=')
.ok_or_else(|| format!("invalid --param '{raw}': expected key=value"))?;
let key = raw[..pos].to_string();
let value = raw[pos + 1..].to_string();
Ok((key, value))
}
fn split_labels(labels: &[String]) -> Vec<String> {
labels
.iter()
.flat_map(|l| l.split(','))
.map(|s| s.trim().to_string())
.filter(|s| !s.is_empty())
.collect()
}
fn user_context_path(cwd: &Path) -> Result<PathBuf, Box<dyn std::error::Error>> {
user_context_path_in(cwd, &crate::config::tv_config_root())
}
fn user_context_path_in(
cwd: &Path,
config_root: &Path,
) -> Result<PathBuf, Box<dyn std::error::Error>> {
let repo_uc = match find_project_root(cwd) {
Ok(root) => TracevaultConfig::try_load(&root)
.map_err(|e| {
format!("cannot resolve --user path: malformed .visdom/tracing.toml: {e}")
})?
.and_then(|c| c.user_context),
Err(_) => None, };
if let Some(uc) = repo_uc {
let bare_disable = matches!(uc, UserContext::Toggle(false))
|| matches!(
uc,
UserContext::Full {
enable: false,
path: None
}
);
if !bare_disable {
return Ok(uc.path());
}
}
let user_cfg = crate::config::try_load_user_config_in(config_root)
.map_err(|e| format!("cannot resolve --user path: malformed user config.toml: {e}"))?;
Ok(user_cfg
.and_then(|c| c.user_context)
.map(|uc| uc.path_in(config_root))
.unwrap_or_else(|| crate::config::default_user_context_path_in(config_root)))
}
pub fn run_set(
cwd: &Path,
flow: Option<String>,
labels: Vec<String>,
params: Vec<String>,
global: bool,
user: bool,
) -> Result<(), Box<dyn std::error::Error>> {
let mut parsed_params = std::collections::BTreeMap::new();
for raw in ¶ms {
let (k, v) = parse_param(raw)?;
parsed_params.insert(k, Some(v));
}
let ctx = Context {
flow_id: flow,
labels: split_labels(&labels),
params: parsed_params,
};
if user {
let path = user_context_path(cwd)?;
ctx.save_to(&path)
.map_err(|e: io::Error| format!("failed to save context: {e}"))?;
println!("Context set.");
return Ok(());
}
let paths = resolve_context_paths(cwd);
if !paths.is_set_up() {
return Err(crate::program::say("no .visdom/ found — run 'tracevault init' first").into());
}
if global || matches!(paths.scope, WorktreeScope::Primary) {
ctx.save_global(&paths)
} else {
ctx.save_worktree(&paths)
}
.map_err(|e: io::Error| format!("failed to save context: {e}"))?;
println!("Context set.");
Ok(())
}
#[allow(clippy::too_many_arguments)]
pub fn run_update(
cwd: &Path,
flow: Option<String>,
labels: Vec<String>,
params: Vec<String>,
remove_labels: Vec<String>,
remove_params: Vec<String>,
global: bool,
user: bool,
) -> Result<(), Box<dyn std::error::Error>> {
if user {
let path = user_context_path(cwd)?;
let mut ctx = Context::load_from(&path);
apply_update_mutations(
&mut ctx,
flow,
&labels,
¶ms,
&remove_labels,
&remove_params,
)?;
ctx.save_to(&path)
.map_err(|e: io::Error| format!("failed to save context: {e}"))?;
println!("Context updated.");
return Ok(());
}
let paths = resolve_context_paths(cwd);
if !paths.is_set_up() {
return Err(crate::program::say("no .visdom/ found — run 'tracevault init' first").into());
}
let use_global = global || matches!(paths.scope, WorktreeScope::Primary);
let mut ctx = if use_global {
Context::load_global(&paths)
} else {
Context::load_worktree(&paths).unwrap_or_default()
};
apply_update_mutations(
&mut ctx,
flow,
&labels,
¶ms,
&remove_labels,
&remove_params,
)?;
if use_global {
ctx.save_global(&paths)
} else {
ctx.save_worktree(&paths)
}
.map_err(|e: io::Error| format!("failed to save context: {e}"))?;
println!("Context updated.");
Ok(())
}
fn apply_update_mutations(
ctx: &mut Context,
flow: Option<String>,
labels: &[String],
params: &[String],
remove_labels: &[String],
remove_params: &[String],
) -> Result<(), Box<dyn std::error::Error>> {
if let Some(f) = flow {
ctx.flow_id = Some(f);
}
let new_labels = split_labels(labels);
for l in new_labels {
if !ctx.labels.contains(&l) {
ctx.labels.push(l);
}
}
for raw in params {
let (k, v) = parse_param(raw)?;
ctx.params.insert(k, Some(v));
}
let remove_set: std::collections::HashSet<String> =
split_labels(remove_labels).into_iter().collect();
ctx.labels.retain(|l| !remove_set.contains(l));
for k in remove_params {
ctx.params.insert(k.clone(), None);
}
Ok(())
}
pub fn run_show(cwd: &Path) -> Result<(), Box<dyn std::error::Error>> {
run_show_in(cwd, &crate::config::tv_config_root())
}
pub fn run_show_in(cwd: &Path, config_root: &Path) -> Result<(), Box<dyn std::error::Error>> {
let repo_uc = find_project_root(cwd)
.ok()
.and_then(|root| TracevaultConfig::load(&root))
.and_then(|c| c.user_context);
let user_path =
crate::config::resolve_user_context_in(repo_uc, config_root).resolve_in(config_root);
let user_enabled = user_path.is_some();
let (paths, user, global, worktree, effective) =
Context::effective_with_parts(cwd, user_path.as_deref());
if !paths.is_set_up() && find_project_root(cwd).is_err() {
eprintln!("hint: No .visdom/ directory found. Run `{TOP} init` first.");
}
let is_linked = matches!(paths.scope, WorktreeScope::Linked { .. });
print!(
"{}",
show_report(
user.as_ref(),
user_enabled,
user_path.as_deref(),
&global,
worktree.as_ref(),
is_linked,
&effective,
)
);
Ok(())
}
const LAYER_USER: &str = "user";
const LAYER_REPO: &str = "repo";
const LAYER_WORKTREE: &str = "worktree";
fn param_source(
user: Option<&Context>,
global: &Context,
worktree: Option<&Context>,
key: &str,
) -> &'static str {
if let Some(w) = worktree {
if matches!(w.params.get(key), Some(Some(_))) {
return LAYER_WORKTREE;
}
}
if matches!(global.params.get(key), Some(Some(_))) {
return LAYER_REPO;
}
if let Some(u) = user {
if matches!(u.params.get(key), Some(Some(_))) {
return LAYER_USER;
}
}
"?"
}
fn label_source(
user: Option<&Context>,
global: &Context,
worktree: Option<&Context>,
label: &str,
) -> &'static str {
if let Some(u) = user {
if u.labels.iter().any(|l| l == label) {
return LAYER_USER;
}
}
if global.labels.iter().any(|l| l == label) {
return LAYER_REPO;
}
if let Some(w) = worktree {
if w.labels.iter().any(|l| l == label) {
return LAYER_WORKTREE;
}
}
"?"
}
fn flow_source(
user: Option<&Context>,
global: &Context,
worktree: Option<&Context>,
) -> &'static str {
if let Some(w) = worktree {
if w.flow_id.is_some() {
return LAYER_WORKTREE;
}
}
if global.flow_id.is_some() {
return LAYER_REPO;
}
if let Some(u) = user {
if u.flow_id.is_some() {
return LAYER_USER;
}
}
"?"
}
#[allow(clippy::too_many_arguments)]
fn show_report(
user: Option<&Context>,
user_enabled: bool,
user_path: Option<&Path>,
global: &Context,
worktree: Option<&Context>,
is_linked: bool,
eff: &crate::context::EffectiveContext,
) -> String {
use std::fmt::Write;
let mut out = String::new();
let file_desc = match user_path {
Some(p) if user_enabled => p.display().to_string(),
_ => "disabled".to_string(),
};
let _ = writeln!(
out,
"=== User (enabled: {user_enabled}, file: {file_desc}) ==="
);
match user {
Some(u) => {
let _ = writeln!(
out,
"{}",
serde_json::to_string_pretty(u).unwrap_or_default()
);
}
None => {
let _ = writeln!(out, "(none)");
}
}
let _ = writeln!(out, "\n=== Global ===");
let _ = writeln!(
out,
"{}",
serde_json::to_string_pretty(global).unwrap_or_default()
);
if is_linked {
let _ = writeln!(out, "\n=== This worktree ===");
match worktree {
Some(wt) => {
let _ = writeln!(
out,
"{}",
serde_json::to_string_pretty(wt).unwrap_or_default()
);
}
None => {
let _ = writeln!(out, "(none)");
}
}
}
let _ = writeln!(out, "\n=== Effective ===");
match &eff.flow_id {
Some(f) => {
let source = flow_source(user, global, worktree);
let _ = writeln!(out, "flow_id = {f} [from {source}]");
}
None => {
let _ = writeln!(out, "flow_id = (none)");
}
}
let _ = writeln!(out, "labels:");
if eff.labels.is_empty() {
let _ = writeln!(out, " (none)");
} else {
for label in &eff.labels {
let source = label_source(user, global, worktree, label);
let _ = writeln!(out, " {label} [from {source}]");
}
}
let _ = writeln!(out, "params:");
if eff.params.is_empty() {
let _ = writeln!(out, " (none)");
} else {
for (k, v) in &eff.params {
let source = param_source(user, global, worktree, k);
let _ = writeln!(out, " {k} = {v} [from {source}]");
}
}
out
}
pub fn run_clear(cwd: &Path, global: bool, user: bool) -> Result<(), Box<dyn std::error::Error>> {
if user {
let path = user_context_path(cwd)?;
Context::default()
.save_to(&path)
.map_err(|e: io::Error| format!("failed to clear context: {e}"))?;
println!("Context cleared.");
return Ok(());
}
let paths = resolve_context_paths(cwd);
if !paths.is_set_up() {
return Err(crate::program::say("no .visdom/ found — run 'tracevault init' first").into());
}
let use_global = global || matches!(paths.scope, WorktreeScope::Primary);
if use_global {
Context::default()
.save_global(&paths)
.map_err(|e: io::Error| format!("failed to clear context: {e}"))?;
} else {
let wt_path = paths.worktree_path();
let legacy = wt_path.as_deref().and_then(crate::layout::legacy_twin);
for path in wt_path.into_iter().chain(legacy) {
match std::fs::remove_file(&path) {
Ok(()) => {}
Err(e) if e.kind() == io::ErrorKind::NotFound => {} Err(e) => return Err(format!("failed to clear context: {e}").into()),
}
}
}
println!("Context cleared.");
Ok(())
}
pub fn run_source(
cwd: &Path,
enable: bool,
disable: bool,
path: Option<String>,
default: bool,
) -> Result<(), Box<dyn std::error::Error>> {
let root = find_project_root(cwd)?;
let mut config = match TracevaultConfig::try_load(&root) {
Ok(Some(cfg)) => cfg,
Ok(None) => {
return Err(crate::program::say(
"no .visdom/tracing.toml found — run 'tracevault init' first",
)
.into())
}
Err(e) => return Err(format!("malformed .visdom/tracing.toml: {e}").into()),
};
let new_user_context = if disable {
UserContext::Toggle(false)
} else if let Some(p) = path {
UserContext::Path(p)
} else if default || enable {
UserContext::Toggle(true)
} else {
return Err("specify one of --enable, --disable, --path <file>, --default".into());
};
config.user_context = Some(new_user_context);
config.save(&root)?;
println!("Updated user_context.");
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use std::collections::BTreeMap;
use std::fs;
#[test]
fn show_report_includes_user_section_and_repo_provenance() {
let user = Context {
flow_id: None,
labels: vec![],
params: BTreeMap::from([("owner".to_string(), Some("alice".to_string()))]),
};
let global = Context {
flow_id: None,
labels: vec![],
params: BTreeMap::from([("owner".to_string(), Some("bob".to_string()))]),
};
let eff = Context::merge_layers(&[&user, &global]);
let report = show_report(
Some(&user),
true,
Some(Path::new("/tmp/user.json")),
&global,
None,
false,
&eff,
);
assert!(
report.contains("=== User"),
"missing User section: {report}"
);
assert!(
report.contains("owner = bob [from repo]"),
"missing repo provenance for overridden param: {report}"
);
}
fn temp_project() -> tempfile::TempDir {
let dir = tempfile::tempdir().unwrap();
let tv_dir = dir.path().join(".visdom");
fs::create_dir_all(&tv_dir).unwrap();
fs::write(
tv_dir.join("tracing.toml"),
TracevaultConfig::default().to_toml(),
)
.unwrap();
dir
}
#[test]
fn source_enable_disable_and_path() {
let dir = temp_project();
run_source(dir.path(), true, false, None, false).unwrap();
let cfg = TracevaultConfig::load(dir.path()).unwrap();
assert!(cfg.user_context.unwrap().resolve().is_some());
run_source(dir.path(), false, true, None, false).unwrap();
let cfg = TracevaultConfig::load(dir.path()).unwrap();
assert!(cfg.user_context.unwrap().resolve().is_none());
run_source(dir.path(), false, false, Some("/p".to_string()), false).unwrap();
let cfg = TracevaultConfig::load(dir.path()).unwrap();
assert_eq!(
cfg.user_context.unwrap().resolve(),
Some(std::path::PathBuf::from("/p"))
);
}
#[test]
fn source_default_flag_enables() {
let dir = temp_project();
run_source(dir.path(), false, false, None, true).unwrap();
let cfg = TracevaultConfig::load(dir.path()).unwrap();
assert!(cfg.user_context.unwrap().resolve().is_some());
}
#[test]
fn source_keeps_the_rest_of_the_config() {
let dir = tempfile::tempdir().unwrap();
fs::create_dir_all(dir.path().join(".visdom")).unwrap();
let original = "repo_id = \"r1\"\napi_key = \"tvk_x\"\nuser_context = true\n\
default_project = \"payments\"\nfuture_setting = 1\n";
fs::write(dir.path().join(".visdom/tracing.toml"), original).unwrap();
run_source(dir.path(), false, true, None, false).unwrap();
let content = fs::read_to_string(dir.path().join(".visdom/tracing.toml")).unwrap();
assert_eq!(
content,
format!(
"# Visdom tracing configuration (this checkout's own)\n{}agent = \"claude-code\"\n",
original.replace("user_context = true", "user_context = false")
)
);
}
#[test]
fn source_no_flags_errors() {
let dir = temp_project();
let err = run_source(dir.path(), false, false, None, false).unwrap_err();
assert!(err.to_string().contains("--enable"));
}
#[test]
fn parse_param_valid() {
assert_eq!(
parse_param("key=value").unwrap(),
("key".to_string(), "value".to_string())
);
}
#[test]
fn parse_param_value_contains_equals() {
assert_eq!(
parse_param("key=a=b").unwrap(),
("key".to_string(), "a=b".to_string())
);
}
#[test]
fn parse_param_no_equals_errors() {
let err = parse_param("noequals").unwrap_err();
assert!(err.to_string().contains("expected key=value"));
assert!(err.to_string().contains("noequals"));
}
#[test]
fn split_labels_comma_separated() {
let result = split_labels(&["a,b,c".to_string(), "d".to_string()]);
assert_eq!(result, vec!["a", "b", "c", "d"]);
}
fn temp_project_with_user_context(uc_path: &Path) -> tempfile::TempDir {
let dir = tempfile::tempdir().unwrap();
let tv_dir = dir.path().join(".visdom");
fs::create_dir_all(&tv_dir).unwrap();
let config = TracevaultConfig {
user_context: Some(UserContext::Path(uc_path.display().to_string())),
..TracevaultConfig::default()
};
fs::write(tv_dir.join("tracing.toml"), config.to_toml()).unwrap();
dir
}
#[test]
fn set_user_writes_resolved_user_context_file() {
let uc_dir = tempfile::tempdir().unwrap();
let uc_path = uc_dir.path().join("uc.json");
let project = temp_project_with_user_context(&uc_path);
run_set(
project.path(),
None,
vec!["team".to_string()],
vec!["env=prod".to_string()],
false,
true,
)
.unwrap();
let ctx = Context::load_from(&uc_path);
assert_eq!(ctx.labels, vec!["team".to_string()]);
assert_eq!(ctx.params.get("env"), Some(&Some("prod".to_string())));
}
#[test]
fn update_user_merges_into_resolved_user_context_file() {
let uc_dir = tempfile::tempdir().unwrap();
let uc_path = uc_dir.path().join("uc.json");
let project = temp_project_with_user_context(&uc_path);
run_set(
project.path(),
None,
vec!["team".to_string()],
vec!["env=prod".to_string()],
false,
true,
)
.unwrap();
run_update(
project.path(),
None,
vec!["extra".to_string()],
vec![],
vec![],
vec!["env".to_string()],
false,
true,
)
.unwrap();
let ctx = Context::load_from(&uc_path);
assert_eq!(ctx.labels, vec!["team".to_string(), "extra".to_string()]);
assert_eq!(ctx.params.get("env"), Some(&None));
}
#[test]
fn user_context_path_no_checkout_uses_global_config() {
let cfg_root = tempfile::tempdir().unwrap();
std::fs::write(
crate::config::user_config_path_in(cfg_root.path()),
"user_context = true\n",
)
.unwrap();
let cwd = tempfile::tempdir().unwrap(); let p = user_context_path_in(cwd.path(), cfg_root.path()).unwrap();
assert_eq!(
p,
crate::config::default_user_context_path_in(cfg_root.path())
);
}
#[test]
fn user_context_path_no_checkout_defaults_when_global_absent() {
let cfg_root = tempfile::tempdir().unwrap();
let cwd = tempfile::tempdir().unwrap();
let p = user_context_path_in(cwd.path(), cfg_root.path()).unwrap();
assert_eq!(
p,
crate::config::default_user_context_path_in(cfg_root.path())
);
}
#[test]
fn run_show_in_detached_uses_user_level_config_without_error() {
let cfg_root = tempfile::tempdir().unwrap();
std::fs::write(
crate::config::user_config_path_in(cfg_root.path()),
"user_context = true\n",
)
.unwrap();
let ctx = crate::config::default_user_context_path_in(cfg_root.path());
crate::context::Context::default().save_to(&ctx).unwrap();
let cwd = tempfile::tempdir().unwrap(); assert!(run_show_in(cwd.path(), cfg_root.path()).is_ok());
}
#[test]
fn user_context_path_explicit_off_repo_falls_through_to_user_level() {
let repo = tempfile::tempdir().unwrap();
let tv = repo.path().join(".visdom");
std::fs::create_dir_all(&tv).unwrap();
std::fs::write(
tv.join("tracing.toml"),
"agent = \"claude-code\"\nuser_context = false\n",
)
.unwrap();
let cfg_root = tempfile::tempdir().unwrap();
let custom = cfg_root.path().join("mine.json");
std::fs::write(
crate::config::user_config_path_in(cfg_root.path()),
format!("user_context = \"{}\"\n", custom.display()),
)
.unwrap();
let p = user_context_path_in(repo.path(), cfg_root.path()).unwrap();
assert_eq!(
p, custom,
"explicit-off repo must not hijack the --user edit to the default file"
);
}
#[test]
fn clear_user_resets_resolved_user_context_file() {
let uc_dir = tempfile::tempdir().unwrap();
let uc_path = uc_dir.path().join("uc.json");
let project = temp_project_with_user_context(&uc_path);
run_set(
project.path(),
None,
vec!["team".to_string()],
vec![],
false,
true,
)
.unwrap();
run_clear(project.path(), false, true).unwrap();
let ctx = Context::load_from(&uc_path);
assert_eq!(ctx, Context::default());
}
}