agent_trace/commands/
add.rs1use crate::git_store::CommitInfo;
2use crate::observability::CliOutput;
3use crate::store::Store;
4use crate::types::{Action, Actor, DocType};
5use anyhow::{bail, Result};
6use std::path::Path;
7
8pub fn run(
9 store_root: &Path,
10 doc_type: DocType,
11 file: &Path,
12 output: &dyn CliOutput,
13) -> Result<()> {
14 if !store_root.join(file).exists() && !file.exists() {
15 bail!("File not found: {}", file.display());
16 }
17
18 let rel = if file.is_absolute() {
20 file.strip_prefix(store_root).unwrap_or(file).to_path_buf()
21 } else {
22 file.to_path_buf()
23 };
24
25 let mut store = Store::open(store_root)?;
26
27 if store.manifest.is_tracked(&rel) {
28 bail!("File is already tracked: {}", rel.display());
29 }
30
31 store.manifest.register(&rel, doc_type.clone(), "")?;
32 store.manifest.save(store_root)?;
33
34 let agent_trace_content = crate::agent_trace_md::generate(store_root, &store.manifest);
36 std::fs::write(store_root.join("AGENT-TRACE.md"), &agent_trace_content)?;
37
38 let info = CommitInfo {
39 action: Action::Create,
40 files: vec![
41 (rel.clone(), Action::Create, doc_type.clone()),
42 (
43 std::path::PathBuf::from("AGENT-TRACE.md"),
44 Action::Modify,
45 DocType::Reference,
46 ),
47 ],
48 actor: Actor::User,
49 summary: format!("add {}: {}", doc_type, rel.display()),
50 agent_name: None,
51 session_id: None,
52 };
53 store.commit(&info)?;
54
55 output.line(&format!("Added {} as {}", rel.display(), doc_type))?;
56 Ok(())
57}