Skip to main content

write

Function write 

Source
pub fn write(
    root: &Path,
    snapshot: &ContextSnapshot,
) -> Result<StateWriteResult, String>
Expand description

Write all artifacts. Each file is staged to a sibling *.tmp.<pid>.<nanos> path, fsynced, then renamed into place, so an agent reading .context-bar/AGENT.md (or any brief) never observes a truncated mid-write file and the renamed bytes are durable on disk. Renames on the same filesystem are atomic on POSIX and Windows ReplaceFileW.

Examples found in repository?
examples/snapshot.rs (line 39)
27fn run() -> Result<(), String> {
28    let root = env::args()
29        .nth(1)
30        .map(PathBuf::from)
31        .unwrap_or_else(|| env::current_dir().expect("cwd"));
32    let root = root
33        .canonicalize()
34        .map_err(|error| format!("cannot canonicalize {}: {error}", root.display()))?;
35
36    let git = collect_git(&root)?;
37    let files = context_engine::collect_files(&root)?;
38    let snapshot = context_engine::assemble(root.clone(), git, files)?;
39    let result = state_writer::write(&root, &snapshot)?;
40
41    println!("Wrote:");
42    println!("  {}", result.state_path.display());
43    println!("  {}", result.now_brief_path.display());
44    println!("  {}", result.session_brief_path.display());
45    println!("  {}", result.week_brief_path.display());
46    println!("  {}  <- primary agent surface", result.agent_brief_path.display());
47    println!("  {}  <- Claude Code surface", result.claude_brief_path.display());
48    Ok(())
49}