heddle-cli 0.2.0

An AI-native version control system
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
// SPDX-License-Identifier: Apache-2.0
//! Log command.

use std::{
    collections::BTreeMap,
    fs,
    io::{self, BufRead},
    path::{Path, PathBuf},
};

use anyhow::{Context, Result};
use objects::object::{Agent, ChangeId, State};
use repo::{ChangedPathFilters, HistoryQuery, Repository, format_confidence, is_synthetic_root};
use serde::Serialize;

use super::{history_target::resolve_state_id, snapshot::ensure_current_state};
use crate::{
    cli::{Cli, should_output_json, style},
    config::UserConfig,
};

#[derive(Clone, Debug)]
pub struct LogCommandOptions {
    pub state: Option<String>,
    pub limit: usize,
    pub all: bool,
    pub graph: bool,
    pub oneline: bool,
    pub reflog: bool,
    pub agent: Option<String>,
    pub paths: Vec<String>,
    /// Lower bound for the walk. Resolved through
    /// `resolve_state_id` so it accepts marker names, short
    /// state IDs, or any other spec the state resolver supports. Walk
    /// stops as soon as we encounter this state — the bound itself is
    /// excluded from the output (matches `git log --since`'s
    /// half-open semantics for time bounds).
    pub since: Option<String>,
}

#[derive(Serialize)]
struct LogOutput {
    repository_capability: String,
    storage_model: String,
    states: Vec<StateEntry>,
    /// Carried for the human-readable renderer only. Not part of the
    /// JSON contract: import-hint information is exposed via
    /// `heddle bridge git status --json` instead.
    #[serde(skip)]
    git_overlay_import_hint: Option<LogGitOverlayImportHintOutput>,
}

#[derive(Serialize)]
struct LogGitOverlayImportHintOutput {
    current_branch: String,
    missing_branch_count: usize,
    missing_branches: Vec<String>,
    recommended_command: String,
}

#[derive(Serialize)]
struct StateEntry {
    change_id: String,
    content_hash: String,
    intent: Option<String>,
    principal: String,
    /// Raw principal name + email so we can render a styled
    /// `name <email>` pair (bold/dim) without re-parsing the
    /// pre-formatted `principal` string. Skipped from JSON
    /// serialization to keep the wire format unchanged — only the
    /// human-readable renderer reads them.
    #[serde(skip)]
    principal_name: String,
    #[serde(skip)]
    principal_email: String,
    agent: Option<String>,
    confidence: Option<f32>,
    created_at: String,
    parents: Vec<String>,
    git_checkpoint: Option<String>,
}

#[derive(Serialize)]
struct ReflogOutput {
    repository_capability: String,
    storage_model: String,
    entries: Vec<ReflogEntry>,
}

#[derive(Clone, Debug, Serialize)]
struct ReflogEntry {
    source: String,
    reference: String,
    old_oid: String,
    new_oid: String,
    actor: String,
    timestamp: Option<String>,
    message: String,
}

impl From<&State> for StateEntry {
    fn from(state: &State) -> Self {
        Self {
            change_id: state.change_id.short(),
            content_hash: state.tree.short(),
            intent: state.intent.clone(),
            principal: state.attribution.principal.to_string(),
            principal_name: state.attribution.principal.name.clone(),
            principal_email: state.attribution.principal.email.clone(),
            agent: state.attribution.agent.as_ref().map(Agent::to_string),
            confidence: state.confidence,
            created_at: state.created_at.format("%Y-%m-%d %H:%M:%S").to_string(),
            parents: state.parents.iter().map(ChangeId::short).collect(),
            git_checkpoint: None,
        }
    }
}

pub async fn cmd_log(cli: &Cli, options: LogCommandOptions) -> Result<()> {
    let repo = Repository::open(cli.repo.as_ref().unwrap_or(&std::env::current_dir()?))?;

    if options.reflog {
        return cmd_log_reflog(cli, &repo, options.limit, options.oneline);
    }

    // Get starting state
    let start_id = if let Some(ref spec) = options.state {
        if matches!(spec.as_str(), "HEAD" | "@") && repo.current_state()?.is_none() {
            ensure_current_state(
                &repo,
                &UserConfig::load_default().unwrap_or_default(),
                Some("Bootstrap git-overlay before viewing log".to_string()),
            )?;
        }
        Some(resolve_state_id(&repo, spec)?)
    } else {
        Some(ensure_current_state(
            &repo,
            &UserConfig::load_default().unwrap_or_default(),
            Some("Bootstrap git-overlay before viewing log".to_string()),
        )?)
    };

    // Resolve the `--since` bound (if any) and pass it down as the
    // walker's `stop_at`. The bound is honored *before* `--agent` /
    // `--path` filters, so a bound state that itself doesn't match the
    // active filter still terminates the walk correctly. (Previously
    // we applied the bound *after* filtering, which silently leaked
    // matches older than the bound when the bound state was filtered
    // out.) The bound is exclusive — matches git's `--since` shape.
    let since_id = if let Some(ref spec) = options.since {
        Some(resolve_state_id(&repo, spec)?)
    } else {
        None
    };

    let changed_paths = ChangedPathFilters::try_from_paths(options.paths)?;
    let query = HistoryQuery::new(start_id)
        .with_limit(options.limit)
        .with_agent_filter(options.agent)
        .with_changed_paths(changed_paths)
        .with_stop_at(since_id);
    let states = repo.query_history(&query)?;

    // Hide the synthetic genesis root (`heddle init` seed). It carries
    // a stable system attribution rather than the user's principal —
    // see `repo::seed_default_thread`. Surfacing it in user-facing log
    // output would always show the seed state above the user's first
    // real snapshot and report a "Heddle <init@heddle>" or (in older
    // repos) "Unknown" principal that the user never authored.
    let output = LogOutput {
        repository_capability: repo.capability_label().to_string(),
        storage_model: repo.storage_model_label().to_string(),
        git_overlay_import_hint: repo.git_overlay_import_hint()?.map(|hint| {
            LogGitOverlayImportHintOutput {
                current_branch: hint.current_branch,
                missing_branch_count: hint.missing_branch_count,
                missing_branches: hint.missing_branches,
                recommended_command: hint.recommended_command,
            }
        }),
        states: states
            .iter()
            .filter(|state| !is_synthetic_root(state))
            .map(|state| {
                let mut entry = StateEntry::from(state);
                entry.git_checkpoint = repo
                    .latest_git_checkpoint_for_change(&state.change_id)
                    .ok()
                    .flatten()
                    .map(|record| record.git_commit);
                entry
            })
            .collect(),
    };

    let as_json = should_output_json(cli, Some(repo.config()));
    if as_json {
        println!("{}", serde_json::to_string(&output)?);
    } else {
        // Render to stdout via the writer-based helpers so the same
        // formatting logic is unit-testable against a `Vec<u8>` —
        // see `tests::print_oneline_no_ansi_when_disabled`.
        let stdout = std::io::stdout();
        let mut handle = stdout.lock();
        if options.oneline {
            let _ = write_oneline(&mut handle, &output);
        } else {
            let _ = write_full(&mut handle, &output);
        }
    }

    // Discoverability tip after a successful log: nudge toward
    // `heddle query` for filtered history. Once per session per repo.
    crate::cli::tips::maybe_emit(
        repo.root(),
        Some(repo.config()),
        crate::cli::tips::Tip::QueryFromLog,
        as_json,
    );

    Ok(())
}

fn cmd_log_reflog(cli: &Cli, repo: &Repository, limit: usize, oneline: bool) -> Result<()> {
    let output = ReflogOutput {
        repository_capability: repo.capability_label().to_string(),
        storage_model: repo.storage_model_label().to_string(),
        entries: collect_reflog_entries(repo.root(), limit)?,
    };

    if should_output_json(cli, Some(repo.config())) {
        println!("{}", serde_json::to_string(&output)?);
    } else {
        let stdout = std::io::stdout();
        let mut handle = stdout.lock();
        if oneline {
            let _ = write_reflog_oneline(&mut handle, &output);
        } else {
            let _ = write_reflog_full(&mut handle, &output);
        }
    }

    Ok(())
}

fn collect_reflog_entries(root: &Path, limit: usize) -> Result<Vec<ReflogEntry>> {
    let mut entries = Vec::new();
    for (source, logs_dir) in reflog_roots(root)? {
        collect_reflog_dir(&source, &logs_dir, &mut entries)
            .with_context(|| format!("reading reflog entries from {}", logs_dir.display()))?;
    }

    entries.sort_by(|a, b| {
        b.timestamp
            .cmp(&a.timestamp)
            .then_with(|| a.reference.cmp(&b.reference))
            .then_with(|| a.message.cmp(&b.message))
    });
    entries.truncate(limit);
    Ok(entries)
}

fn reflog_roots(root: &Path) -> Result<Vec<(String, PathBuf)>> {
    let mut roots = Vec::new();

    if let Some(git_dir) = checkout_git_dir(root)? {
        let logs = git_dir.join("logs");
        if logs.is_dir() {
            roots.push(("checkout".to_string(), logs));
        }
    }

    let mirror_logs = root.join(".heddle").join("git").join("logs");
    if mirror_logs.is_dir() {
        roots.push(("mirror".to_string(), mirror_logs));
    }

    Ok(roots)
}

fn checkout_git_dir(root: &Path) -> Result<Option<PathBuf>> {
    let dot_git = root.join(".git");
    if dot_git.is_dir() {
        return Ok(Some(dot_git));
    }
    if !dot_git.is_file() {
        return Ok(None);
    }

    let contents = fs::read_to_string(&dot_git)
        .with_context(|| format!("reading gitdir pointer {}", dot_git.display()))?;
    let Some(path) = contents.trim().strip_prefix("gitdir:") else {
        return Ok(None);
    };
    let path = PathBuf::from(path.trim());
    if path.is_absolute() {
        Ok(Some(path))
    } else {
        Ok(Some(root.join(path)))
    }
}

fn collect_reflog_dir(source: &str, logs_dir: &Path, entries: &mut Vec<ReflogEntry>) -> Result<()> {
    let mut stack = vec![logs_dir.to_path_buf()];
    while let Some(dir) = stack.pop() {
        for entry in fs::read_dir(&dir)? {
            let entry = entry?;
            let path = entry.path();
            if path.is_dir() {
                stack.push(path);
                continue;
            }
            let Ok(reference) = path.strip_prefix(logs_dir) else {
                continue;
            };
            let reference = reference
                .to_string_lossy()
                .replace(std::path::MAIN_SEPARATOR, "/");
            read_reflog_file(source, &reference, &path, entries)?;
        }
    }
    Ok(())
}

fn read_reflog_file(
    source: &str,
    reference: &str,
    path: &Path,
    entries: &mut Vec<ReflogEntry>,
) -> Result<()> {
    let file = fs::File::open(path)?;
    for line in io::BufReader::new(file).lines() {
        if let Some(entry) = parse_reflog_line(source, reference, &line?) {
            entries.push(entry);
        }
    }
    Ok(())
}

fn parse_reflog_line(source: &str, reference: &str, line: &str) -> Option<ReflogEntry> {
    let (metadata, message) = line.split_once('\t').unwrap_or((line, ""));
    let mut parts = metadata.split_whitespace();
    let old_oid = parts.next()?.to_string();
    let new_oid = parts.next()?.to_string();
    let mut actor_parts = Vec::new();
    let mut timestamp = None;

    for part in parts {
        if part.parse::<i64>().is_ok() {
            timestamp = Some(part.to_string());
            break;
        }
        actor_parts.push(part);
    }

    Some(ReflogEntry {
        source: source.to_string(),
        reference: reference.to_string(),
        old_oid,
        new_oid,
        actor: actor_parts.join(" "),
        timestamp,
        message: message.to_string(),
    })
}

fn write_reflog_oneline<W: std::io::Write>(
    out: &mut W,
    output: &ReflogOutput,
) -> std::io::Result<()> {
    for entry in &output.entries {
        writeln!(
            out,
            "{} {} {} {}",
            style::dim(&entry.source),
            style::change_id(short_oid(&entry.new_oid)),
            style::dim(&entry.reference),
            style::bold(&entry.message)
        )?;
    }
    Ok(())
}

fn write_reflog_full<W: std::io::Write>(out: &mut W, output: &ReflogOutput) -> std::io::Result<()> {
    writeln!(
        out,
        "Repository mode: {} ({})",
        output.repository_capability, output.storage_model
    )?;
    writeln!(out, "Reflog: {} entrie(s)", output.entries.len())?;
    if output.entries.is_empty() {
        writeln!(
            out,
            "Next step: {}",
            style::dim("make a checkpoint, fetch, pull, push, or run `heddle bridge git import`")
        )?;
        return Ok(());
    }

    let mut by_ref: BTreeMap<(&str, &str), Vec<&ReflogEntry>> = BTreeMap::new();
    for entry in &output.entries {
        by_ref
            .entry((&entry.source, &entry.reference))
            .or_default()
            .push(entry);
    }

    for ((source, reference), entries) in by_ref {
        writeln!(out)?;
        writeln!(
            out,
            "{} {}",
            style::bold(reference),
            style::dim(&format!("({source})"))
        )?;
        for entry in entries {
            writeln!(
                out,
                "  {} {} -> {} {}",
                style::dim(entry.timestamp.as_deref().unwrap_or("unknown-time")),
                style::dim(short_oid(&entry.old_oid)),
                style::accent(short_oid(&entry.new_oid)),
                style::bold(&entry.message)
            )?;
            if !entry.actor.is_empty() {
                writeln!(out, "    by {}", style::dim(&entry.actor))?;
            }
        }
    }
    Ok(())
}

fn short_oid(oid: &str) -> &str {
    oid.get(..12).unwrap_or(oid)
}

fn write_oneline<W: std::io::Write>(out: &mut W, output: &LogOutput) -> std::io::Result<()> {
    for entry in &output.states {
        let intent = entry.intent.as_deref().unwrap_or("(no intent)");
        let checkpoint = if entry.git_checkpoint.is_some() {
            " [git]"
        } else {
            ""
        };
        // Three columns of decreasing emphasis: id (dim, structural),
        // hash (dim, structural), intent (bold, the part you read).
        writeln!(
            out,
            "{} {} {}{}",
            style::change_id(&entry.change_id),
            style::dim(&entry.content_hash),
            style::bold(intent),
            checkpoint,
        )?;
    }
    Ok(())
}

fn write_full<W: std::io::Write>(out: &mut W, output: &LogOutput) -> std::io::Result<()> {
    writeln!(
        out,
        "Repository mode: {} ({})",
        output.repository_capability, output.storage_model
    )?;
    if let Some(hint) = &output.git_overlay_import_hint {
        writeln!(
            out,
            "Git import: {} other branch(es) still live only in Git ({})",
            hint.missing_branch_count,
            crate::cli::render::preview_list(&hint.missing_branches, hint.missing_branch_count,)
        )?;
        writeln!(out, "Next step: {}", style::dim(&hint.recommended_command))?;
    }
    writeln!(out)?;
    for (i, entry) in output.states.iter().enumerate() {
        if i > 0 {
            writeln!(out)?;
        }

        // Header: change-id and tree hash dim, timestamp dim.
        // Nothing carries semantic color here — these are
        // structurally important but not the focus.
        writeln!(
            out,
            "{} ({}) {}",
            style::change_id(&entry.change_id),
            style::dim(&entry.content_hash),
            style::dim(&entry.created_at),
        )?;

        if let Some(intent) = &entry.intent {
            // Intent is the editorial line — bold, no color.
            writeln!(out, "  {}", style::bold(intent))?;
        }

        writeln!(
            out,
            "  Principal: {}",
            style::principal(&entry.principal_name, &entry.principal_email)
        )?;

        if let Some(agent) = &entry.agent {
            writeln!(out, "  Agent: {}", style::dim(agent))?;
        }

        // Confidence only renders when set — the per-entry "Confidence: —"
        // sentinel was high-noise on long logs and conveyed "value absent"
        // information the absence-of-line itself already encodes. JSON output
        // still carries `confidence: null` so agents can distinguish.
        if entry.confidence.is_some() {
            let confidence_text = format_confidence(entry.confidence);
            writeln!(
                out,
                "  Confidence: {}",
                style::confidence(entry.confidence, &confidence_text)
            )?;
        }
        // Durability only renders when there's something to say — a Git
        // checkpoint binds the capture into Git history. The "Capture
        // durability: local only" fallback used to repeat on every entry and
        // told the user nothing they couldn't infer from the absence of a
        // Git-checkpoint line.
        if let Some(git_checkpoint) = &entry.git_checkpoint {
            writeln!(
                out,
                "  Git checkpoint: {}",
                style::dim(&git_checkpoint[..std::cmp::min(12, git_checkpoint.len())])
            )?;
        }
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use serial_test::serial;

    use super::*;

    fn sample_entry() -> StateEntry {
        StateEntry {
            change_id: "hd-abc123".to_string(),
            content_hash: "deadbeef".to_string(),
            intent: Some("Capture audit pipeline".to_string()),
            principal: "Ada <ada@example.com>".to_string(),
            principal_name: "Ada".to_string(),
            principal_email: "ada@example.com".to_string(),
            agent: Some("anthropic/claude-opus-4".to_string()),
            confidence: Some(0.95),
            created_at: "2026-05-01 12:00:00".to_string(),
            parents: vec![],
            git_checkpoint: Some("abc123def456".to_string()),
        }
    }

    /// Render-site smoke test: with color disabled, neither the
    /// oneline nor the full renderer must emit any ANSI escape.
    /// This is the integration check that `style::*` helpers
    /// short-circuit to plain text — if a future helper forgets
    /// the gate, this test catches the leak before it reaches a
    /// user pipe or test snapshot.
    #[test]
    #[serial(color_state)]
    fn render_sites_no_ansi_when_disabled() {
        style::force_for_test(false);
        let output = LogOutput {
            repository_capability: "git-overlay".to_string(),
            storage_model: "git+heddle-sidecar".to_string(),
            git_overlay_import_hint: None,
            states: vec![sample_entry()],
        };

        let mut buf = Vec::new();
        write_oneline(&mut buf, &output).unwrap();
        let s = String::from_utf8(buf).unwrap();
        assert!(!s.contains('\x1b'), "oneline leaked ANSI: {s:?}");
        assert!(s.contains("hd-abc123"));
        assert!(s.contains("Capture audit pipeline"));

        let mut buf = Vec::new();
        write_full(&mut buf, &output).unwrap();
        let s = String::from_utf8(buf).unwrap();
        assert!(!s.contains('\x1b'), "full leaked ANSI: {s:?}");
        assert!(s.contains("Ada <ada@example.com>"));
        assert!(s.contains("Confidence: 0.95"));
    }

    /// With color enabled, the same renderer must emit escapes —
    /// otherwise the gate is stuck off and we'd silently ship a
    /// monochrome CLI. This pairs with the negative test above to
    /// pin both directions of the gate.
    #[test]
    #[serial(color_state)]
    fn render_sites_emit_ansi_when_enabled() {
        style::force_for_test(true);
        let output = LogOutput {
            repository_capability: "git-overlay".to_string(),
            storage_model: "git+heddle-sidecar".to_string(),
            git_overlay_import_hint: None,
            states: vec![sample_entry()],
        };
        let mut buf = Vec::new();
        write_oneline(&mut buf, &output).unwrap();
        let s = String::from_utf8(buf).unwrap();
        assert!(s.contains('\x1b'), "expected ANSI in oneline: {s:?}");
    }
}