agx-core 0.2.0

Agent-trace parsers, timeline model, cost/pricing, and corpus aggregation. The pure, TUI-free core of the agx CLI.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
//! Per-step annotations — the first persistent write-back feature.
//!
//! Notes live outside the session file (agx is read-only with respect to
//! session data, always), in a sidecar JSON under `~/.agx/notes/`. Keyed
//! by a FNV-1a hash of the canonical session path so moves-within-the-
//! same-canonical-path keep their notes while renames start fresh (a
//! deliberate trade-off — session UUID extraction varies per format and
//! isn't available for all of them).
//!
//! File format (version 1):
//! ```json
//! {
//!   "version": 1,
//!   "path": "/absolute/path/to/session.jsonl",
//!   "notes": {
//!     "0": {"text": "...", "created_at_ms": 1704000000000, "updated_at_ms": 1704000000000},
//!     "5": {...}
//!   }
//! }
//! ```
//! Key is the 0-based step index as a JSON-string (since JSON objects
//! require string keys). `created_at_ms` never changes; `updated_at_ms`
//! refreshes on every edit.
//!
//! Writes go through a temp-file + rename so a partial write never
//! corrupts an existing notes file. Reads are fault-tolerant: a missing
//! file or a malformed parse yields an empty `Annotations` so the TUI
//! always has something to render against.

use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::fs;
use std::io::Write;
use std::path::{Path, PathBuf};
use std::time::{SystemTime, UNIX_EPOCH};

const CURRENT_VERSION: u32 = 1;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Note {
    pub text: String,
    pub created_at_ms: u64,
    pub updated_at_ms: u64,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct Annotations {
    #[serde(default = "default_version")]
    pub version: u32,
    /// Canonical absolute path of the session file these annotations
    /// belong to. Recorded for reference / portability — the disk-side
    /// filename is derived via `annotations_file_for`.
    #[serde(default)]
    pub path: String,
    /// Step index (0-based, as a JSON-string per the format) → note.
    /// `BTreeMap` keeps iteration order stable for the list overlay
    /// in follow-up work.
    #[serde(default)]
    pub notes: BTreeMap<String, Note>,
}

fn default_version() -> u32 {
    CURRENT_VERSION
}

impl Annotations {
    /// Fresh, empty annotations bound to the given session path.
    pub fn new(session_path: &Path) -> Self {
        Annotations {
            version: CURRENT_VERSION,
            path: session_path
                .canonicalize()
                .unwrap_or_else(|_| session_path.to_path_buf())
                .display()
                .to_string(),
            notes: BTreeMap::new(),
        }
    }

    /// True when no notes are stored. Used by the export integrations
    /// to skip emitting a "notes" section and by tests.
    pub fn is_empty(&self) -> bool {
        self.notes.is_empty()
    }

    /// Get the note for a step index, if any.
    pub fn get(&self, step_idx: usize) -> Option<&Note> {
        self.notes.get(&step_idx.to_string())
    }

    /// True when the given step index has an annotation.
    pub fn has(&self, step_idx: usize) -> bool {
        self.notes.contains_key(&step_idx.to_string())
    }

    /// Upsert a note. Empty / whitespace-only text deletes the entry
    /// (the intuitive "clear the annotation" behavior). Returns `true`
    /// when the set changed anything.
    pub fn set(&mut self, step_idx: usize, text: &str) -> bool {
        let key = step_idx.to_string();
        let trimmed = text.trim();
        if trimmed.is_empty() {
            return self.notes.remove(&key).is_some();
        }
        let now = now_ms();
        match self.notes.get_mut(&key) {
            Some(existing) if existing.text == trimmed => false,
            Some(existing) => {
                existing.text = trimmed.to_string();
                existing.updated_at_ms = now;
                true
            }
            None => {
                self.notes.insert(
                    key,
                    Note {
                        text: trimmed.to_string(),
                        created_at_ms: now,
                        updated_at_ms: now,
                    },
                );
                true
            }
        }
    }

    /// Iterate notes in numeric step-index order. `BTreeMap` iterates
    /// string keys lexicographically, which would put "12" before "2";
    /// we collect and re-sort by the parsed usize instead. Consumed by
    /// the TUI `A` list overlay and by the export writers (md / html /
    /// json) for their per-step note sections.
    pub fn iter(&self) -> impl Iterator<Item = (usize, &Note)> {
        let mut items: Vec<(usize, &Note)> = self
            .notes
            .iter()
            .filter_map(|(k, v)| k.parse::<usize>().ok().map(|idx| (idx, v)))
            .collect();
        items.sort_by_key(|(idx, _)| *idx);
        items.into_iter()
    }

    /// Load annotations for a session from disk. Returns an empty set
    /// (not an error) when the file doesn't exist — the common case
    /// for sessions the user hasn't annotated yet.
    ///
    /// A corrupted / malformed notes file also returns empty rather
    /// than erroring, so one bad file doesn't prevent the TUI from
    /// launching. A stderr warning is emitted so users know to look.
    pub fn load_for(session_path: &Path) -> Self {
        let file = match annotations_file_for(session_path) {
            Ok(p) => p,
            Err(_) => return Self::new(session_path),
        };
        let Ok(contents) = fs::read_to_string(&file) else {
            return Self::new(session_path);
        };
        match serde_json::from_str::<Annotations>(&contents) {
            Ok(mut a) => {
                // Canonicalize the recorded path if it was a no-op
                // before (e.g. first save happened before canonicalize
                // succeeded).
                if a.path.is_empty() {
                    a.path = session_path.display().to_string();
                }
                a
            }
            Err(e) => {
                eprintln!(
                    "agx: ignoring malformed annotations file {}: {}",
                    file.display(),
                    e
                );
                Self::new(session_path)
            }
        }
    }

    /// Write to disk atomically: serialize to a sibling `*.tmp`, then
    /// rename. `rename(2)` is atomic on the same filesystem, so partial
    /// writes never corrupt an existing notes file.
    pub fn save_for(&self, session_path: &Path) -> Result<PathBuf> {
        let dest = annotations_file_for(session_path)?;
        if let Some(parent) = dest.parent() {
            fs::create_dir_all(parent).with_context(|| format!("creating {}", parent.display()))?;
        }
        let json = serde_json::to_string_pretty(self)?;
        let tmp = dest.with_extension("json.tmp");
        {
            let mut f = fs::File::create(&tmp)
                .with_context(|| format!("creating temp file {}", tmp.display()))?;
            f.write_all(json.as_bytes())
                .with_context(|| format!("writing {}", tmp.display()))?;
            f.sync_all().ok();
        }
        fs::rename(&tmp, &dest)
            .with_context(|| format!("renaming {}{}", tmp.display(), dest.display()))?;
        Ok(dest)
    }
}

/// Resolve the annotations file path for a given session.
///
/// Scheme: `<agx_dir>/notes/<session_stem>-<hash8>.json` where
/// `<hash8>` is the first 8 hex chars of FNV-1a-64 over the canonical
/// session path. Human-readable stem + short unique tag → collisions
/// are vanishingly unlikely in practice while filenames stay
/// recognizable when users inspect the directory.
pub fn annotations_file_for(session_path: &Path) -> Result<PathBuf> {
    let canonical = session_path
        .canonicalize()
        .unwrap_or_else(|_| session_path.to_path_buf());
    let key = canonical.display().to_string();
    let hash = fnv1a_64(key.as_bytes());
    let stem = canonical
        .file_stem()
        .and_then(|s| s.to_str())
        .unwrap_or("session");
    let filename = format!("{stem}-{:08x}.json", hash as u32);
    Ok(agx_home_dir()?.join("notes").join(filename))
}

/// Root directory for agx's persistent state. `AGX_HOME` overrides for
/// tests; otherwise `~/.agx`. Returns an error when the HOME
/// environment variable is unset (which is very unusual on the
/// platforms we target, but we surface it explicitly rather than
/// silently dropping writes).
pub fn agx_home_dir() -> Result<PathBuf> {
    if let Some(override_dir) = std::env::var_os("AGX_HOME") {
        return Ok(PathBuf::from(override_dir));
    }
    let home = std::env::var_os("HOME").ok_or_else(|| anyhow::anyhow!("$HOME is not set"))?;
    Ok(PathBuf::from(home).join(".agx"))
}

fn now_ms() -> u64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| {
            let millis = d.as_millis();
            u64::try_from(millis).unwrap_or(u64::MAX)
        })
        .unwrap_or(0)
}

/// FNV-1a 64-bit. Deterministic (unlike `std::collections::hash_map::DefaultHasher`
/// whose seed is process-random) so notes files don't change name
/// across agx invocations. 5-line implementation, no new crate dep.
fn fnv1a_64(bytes: &[u8]) -> u64 {
    let mut hash: u64 = 0xcbf2_9ce4_8422_2325;
    for b in bytes {
        hash ^= u64::from(*b);
        hash = hash.wrapping_mul(0x100_0000_01b3);
    }
    hash
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::{Mutex, MutexGuard};
    use tempfile::TempDir;

    // Cargo runs tests in parallel, so mutating the process-wide
    // `AGX_HOME` env var races across threads. Serialize access via
    // a module-level mutex — each test holds the guard for its full
    // lifetime (returned alongside the `TempDir` so both drop at
    // end-of-scope together).
    static ENV_LOCK: Mutex<()> = Mutex::new(());

    fn test_home() -> (TempDir, MutexGuard<'static, ()>) {
        let guard = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
        let tmp = TempDir::new().unwrap();
        unsafe {
            std::env::set_var("AGX_HOME", tmp.path());
        }
        (tmp, guard)
    }

    #[test]
    fn new_is_empty_and_bound_to_path() {
        let _home = test_home();
        let a = Annotations::new(Path::new("/tmp/foo.jsonl"));
        assert!(a.is_empty());
        assert_eq!(a.version, CURRENT_VERSION);
        assert!(!a.path.is_empty());
    }

    #[test]
    fn set_inserts_and_trims_whitespace() {
        let _home = test_home();
        let mut a = Annotations::new(Path::new("/tmp/foo.jsonl"));
        let changed = a.set(0, "  hello  ");
        assert!(changed);
        let note = a.get(0).unwrap();
        assert_eq!(note.text, "hello");
        assert!(note.created_at_ms > 0);
        assert_eq!(note.created_at_ms, note.updated_at_ms);
    }

    #[test]
    fn set_with_empty_text_deletes() {
        let _home = test_home();
        let mut a = Annotations::new(Path::new("/tmp/foo.jsonl"));
        a.set(3, "real note");
        assert!(a.has(3));
        let changed = a.set(3, "   ");
        assert!(changed);
        assert!(!a.has(3));
    }

    #[test]
    fn set_to_identical_text_is_a_noop() {
        let _home = test_home();
        let mut a = Annotations::new(Path::new("/tmp/foo.jsonl"));
        a.set(1, "same");
        let changed = a.set(1, "same");
        assert!(!changed);
    }

    #[test]
    fn set_updates_updated_at() {
        let _home = test_home();
        let mut a = Annotations::new(Path::new("/tmp/foo.jsonl"));
        a.set(0, "first");
        let before = a.get(0).unwrap().updated_at_ms;
        std::thread::sleep(std::time::Duration::from_millis(2));
        a.set(0, "second");
        let after = a.get(0).unwrap().updated_at_ms;
        assert!(after > before);
        // created_at_ms stays the same across edits.
        assert_eq!(a.get(0).unwrap().created_at_ms, before);
    }

    #[test]
    fn iter_yields_notes_in_step_index_order() {
        let _home = test_home();
        let mut a = Annotations::new(Path::new("/tmp/foo.jsonl"));
        a.set(5, "five");
        a.set(1, "one");
        a.set(12, "twelve");
        let got: Vec<usize> = a.iter().map(|(idx, _)| idx).collect();
        assert_eq!(got, vec![1, 5, 12]);
    }

    #[test]
    fn save_then_load_round_trip() {
        let _home = test_home();
        let session = Path::new("/tmp/session-foo.jsonl");
        let mut a = Annotations::new(session);
        a.set(2, "this went wrong");
        a.set(7, "revisit this edit");
        let written = a.save_for(session).unwrap();
        assert!(written.exists(), "expected saved notes file to exist");

        let loaded = Annotations::load_for(session);
        assert_eq!(loaded.notes.len(), 2);
        assert_eq!(loaded.get(2).unwrap().text, "this went wrong");
        assert_eq!(loaded.get(7).unwrap().text, "revisit this edit");
    }

    #[test]
    fn load_for_nonexistent_file_returns_empty_without_error() {
        let _home = test_home();
        let a = Annotations::load_for(Path::new("/tmp/nonexistent.jsonl"));
        assert!(a.is_empty());
    }

    #[test]
    fn load_for_malformed_file_returns_empty_without_panic() {
        let home = test_home();
        let session = Path::new("/tmp/session-mal.jsonl");
        let target = annotations_file_for(session).unwrap();
        fs::create_dir_all(target.parent().unwrap()).unwrap();
        fs::write(&target, "{not valid json").unwrap();
        let a = Annotations::load_for(session);
        assert!(a.is_empty());
        // Keep `home` alive so the TempDir isn't dropped mid-test.
        let _ = home;
    }

    #[test]
    fn annotations_file_for_produces_readable_stem_plus_hash() {
        let _home = test_home();
        let path = annotations_file_for(Path::new("/tmp/abcd.jsonl")).unwrap();
        let name = path.file_name().unwrap().to_str().unwrap();
        assert!(name.starts_with("abcd-"), "unexpected filename: {name}");
        assert!(name.ends_with(".json"), "unexpected filename: {name}");
        // Format: <stem>-<8-hex>.json → stem + 1 dash + 8 chars + 5 chars
        assert_eq!(name.len(), "abcd".len() + 1 + 8 + ".json".len());
    }

    #[test]
    fn annotations_file_for_different_paths_differ_in_hash_suffix() {
        let _home = test_home();
        let a = annotations_file_for(Path::new("/tmp/a/session.jsonl")).unwrap();
        let b = annotations_file_for(Path::new("/tmp/b/session.jsonl")).unwrap();
        assert_ne!(a.file_name(), b.file_name());
    }

    #[test]
    fn fnv1a_64_is_deterministic() {
        // The whole point of rolling our own FNV is determinism across
        // process launches — std's hashmap hasher has a random seed.
        let h1 = fnv1a_64(b"/tmp/foo.jsonl");
        let h2 = fnv1a_64(b"/tmp/foo.jsonl");
        assert_eq!(h1, h2);
        assert_ne!(h1, fnv1a_64(b"/tmp/bar.jsonl"));
    }
}