agentstatedeveloper-cli 1.4.9

asd CLI — command-line interface for AgentStateDeveloper
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
//! `asd status` — show index health, age, modified files, and sidecar lifecycle.
//!
//! With `--json`, emits a machine-readable object that includes a `trust` block
//! (State Trust Score) and appends a compact snapshot to `.asd/trust-history.jsonl`
//! for drift tracking.

use anyhow::Result;
use clap::{Args, Subcommand};
use serde_json::json;

use agentstatedeveloper_core::{
    AsgLedgerStore, Engine, LedgerStore, SearchFtsDb, SidecarState, compute_index_consistency,
    compute_trust_score, format_age, ledger_counts,
    schema::{LedgerKind, Symbol},
    sidecar_lifecycle_state,
};

use crate::config::Config;

#[derive(Debug, Args)]
pub struct StatusArgs {
    /// Show source files modified since the last index run (requires git).
    #[arg(long)]
    pub show_dirty: bool,

    /// Emit machine-readable JSON instead of the default human text.
    #[arg(long)]
    pub json: bool,

    /// Subcommand: history (show trust-history snapshots).
    #[command(subcommand)]
    pub command: Option<StatusSubcommand>,
}

#[derive(Debug, Subcommand)]
pub enum StatusSubcommand {
    /// Show recent trust-score snapshots from .asd/trust-history.jsonl.
    History(StatusHistoryArgs),
}

#[derive(Debug, Args)]
pub struct StatusHistoryArgs {
    /// Number of recent entries to show (default: 20).
    #[arg(long, default_value = "20")]
    pub last: usize,
    /// Emit raw JSONL instead of a formatted table.
    #[arg(long)]
    pub json: bool,
}

pub fn run(cfg: &Config, args: StatusArgs) -> Result<()> {
    // Dispatch subcommands first.
    if let Some(sub) = args.command {
        match sub {
            StatusSubcommand::History(h) => return show_history(cfg, &h),
        }
    }

    let fts = SearchFtsDb::open(&cfg.db_path)?;

    let project_root = cfg.db_path.parent().unwrap_or(std::path::Path::new("."));
    let sidecar_state = sidecar_lifecycle_state(project_root);

    if !fts.has_data() {
        if args.json {
            println!(
                "{}",
                json!({
                    "state": "empty",
                    "note": "run 'asd index <dir>' to build",
                    "sidecar": sidecar_state_key(&sidecar_state),
                })
            );
        } else {
            println!("ASD index status");
            println!("  db:       {}", cfg.db_path.display());
            println!("  state:    empty — run 'asd index <dir>' to build");
        }
        return Ok(());
    }

    let count = fts.symbol_count();
    let now = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| d.as_secs() as i64)
        .unwrap_or(0);

    let (indexed_at, age_hours, fresh) = match fts.last_indexed_at() {
        Some(ts) => {
            let age_h = (now - ts).max(0) / 3600;
            (Some(ts), Some(age_h), age_h == 0)
        }
        None => (None, None, false),
    };

    let dirty_files = if args.show_dirty || args.json {
        collect_dirty_files(cfg)
    } else {
        vec![]
    };

    // Concept-gap detection: symbols with Ownership but no Concept entry.
    // Plan J t-005: while we have the by-qname tree open, also tally
    // its length so we can emit `index_consistency` alongside the
    // FTS-derived `symbols` count. The two diverge after a failed FTS
    // rebuild or an out-of-band ledger migration; surfacing the delta
    // turns a silent "why does asd status say 412 but asd health say
    // 408?" puzzle into a one-line `advice` field.
    let engine = Engine::open_sqlite(&cfg.db_path).ok();

    // Ledger entries in the cache vs the store vs the sidecar. "hydrated"
    // only ever meant a marker file exists; on its own it said nothing about
    // whether the sidecar holds what the store acknowledged. Before ASG
    // compare-and-swapped its refs, concurrent writers lost ~14% of one
    // store's ledger while this line kept reading "sidecar is current".
    let ledger = engine
        .as_ref()
        .and_then(|e| ledger_counts(&e.repo, &e.ref_name, e.fts.as_ref(), Some(project_root)).ok());
    let ledger_warning = ledger.as_ref().and_then(|l| l.warning());

    let (concept_gaps, asg_symbol_count): (Vec<serde_json::Value>, Option<usize>) = if args.json {
        if let Some(engine) = engine.as_ref() {
            let ledger_store = AsgLedgerStore::from_engine(engine);
            let tree = engine
                .repo
                .get_tree(&engine.ref_name, "/asd/v1/index/by-qname")
                .unwrap_or(serde_json::Value::Object(Default::default()));
            let asg_count = tree.as_object().map(|m| m.len());
            let gaps = tree
                .as_object()
                .map(|m| {
                    m.values()
                        .filter_map(|v| serde_json::from_value::<Symbol>(v.clone()).ok())
                        .filter_map(|sym| {
                            let entries = ledger_store
                                .list_entries(&engine.ref_name, &sym.symbol_id)
                                .unwrap_or_default();
                            let has_ownership =
                                entries.iter().any(|e| e.kind == LedgerKind::Ownership);
                            let has_concept = entries.iter().any(|e| e.kind == LedgerKind::Concept);
                            if has_ownership && !has_concept {
                                Some(json!({"qname": sym.qname, "file": sym.file}))
                            } else {
                                None
                            }
                        })
                        .collect()
                })
                .unwrap_or_default();
            (gaps, asg_count)
        } else {
            (vec![], None)
        }
    } else {
        (vec![], None)
    };

    let sidecar_key = sidecar_state_key(&sidecar_state);
    let sidecar_action = ledger_warning
        .clone()
        .unwrap_or_else(|| sidecar_action_hint(&sidecar_state).to_string());

    if args.json {
        let index_state = if fresh {
            "fresh"
        } else if age_hours.unwrap_or(0) >= 1 {
            "stale"
        } else {
            "ok"
        };

        // State Trust Score rollup.
        let trust = compute_trust_score(&cfg.db_path);

        // Plan J t-005: emit `index_consistency` when we have an
        // ASG count to compare against. Null when the engine couldn't
        // open (concept_gaps would also be empty in that case).
        let index_consistency = asg_symbol_count
            .map(|asg| compute_index_consistency(asg, count as usize))
            .unwrap_or(serde_json::Value::Null);

        let out = json!({
            "db": cfg.db_path.display().to_string(),
            "symbols": count,
            "indexed_at_unix": indexed_at,
            "age_hours": age_hours,
            "state": index_state,
            "sidecar": sidecar_key,
            "sidecar_action": sidecar_action,
            "ledger": ledger,
            "ledger_warning": ledger_warning,
            "hooks_warning": agentstatedeveloper_core::outdated_hooks(project_root),
            "dirty_files": dirty_files,
            "concept_gaps": concept_gaps,
            "index_consistency": index_consistency,
            "trust": trust.to_json(),
        });

        // Append compact snapshot to trust-history.jsonl.
        // Use the pre-strip dirty_files / concept_gaps lengths for
        // historical accuracy (the snapshot wants 0/0 explicitly,
        // not absence).
        append_trust_history(
            cfg,
            &trust,
            indexed_at,
            age_hours,
            count as u64,
            sidecar_key,
            dirty_files.len(),
            concept_gaps.len(),
        );

        // Token economy (1.0.80): drop top-level null/[]/{} fields.
        // status is mixed-use (human + machine); pretty-print stays
        // for terminal readability, but the empty arrays and the
        // null `index_consistency` (when consistent) drop out — both
        // are signal-free for any consumer.
        let out = agentstatedeveloper_core::drop_empty_top_level(out);

        println!("{}", serde_json::to_string_pretty(&out)?);
        return Ok(());
    }

    // Human-readable output.
    println!("ASD index status");
    println!("  db:       {}", cfg.db_path.display());
    println!("  symbols:  {count}");

    match indexed_at {
        Some(ts) => {
            println!("  indexed:  {} (unix {})", format_age(ts), ts);
            if age_hours.unwrap_or(0) >= 1 {
                println!(
                    "  warning:  index is {}h old — consider re-running 'asd index'",
                    age_hours.unwrap_or(0)
                );
            } else {
                println!("  state:    fresh");
            }
        }
        None => println!("  indexed:  unknown"),
    }

    let sidecar_label = match sidecar_state {
        SidecarState::Missing => "missing — run 'asd sync' to create",
        SidecarState::Present => "present — run 'asd hydrate' to load into ASG",
        SidecarState::Hydrated if ledger_warning.is_some() => {
            "hydrated — but ledger counts disagree (see warning below)"
        }
        SidecarState::Hydrated => "hydrated",
        SidecarState::FreshReset => {
            "fresh-reset (deliberate reset — re-run 'asd index' + 'asd sync')"
        }
    };
    println!("  sidecar:  {sidecar_label}");
    if let Some(l) = &ledger {
        let fmt = |n: Option<usize>| n.map_or_else(|| "—".to_string(), |n| n.to_string());
        println!(
            "  ledger:   cache {} · store {} · sidecar {}",
            fmt(l.cache),
            l.asg,
            fmt(l.sidecar)
        );
    }
    if let Some(w) = &ledger_warning {
        println!("  warning:  {w}");
    }
    if let Some(w) = agentstatedeveloper_core::outdated_hooks(project_root) {
        println!("  warning:  {w}");
    }

    if args.show_dirty {
        let files = dirty_files;
        if files.is_empty() {
            println!("  dirty:    none (all tracked source files match index)");
        } else {
            println!(
                "  dirty:    {} modified source file(s) since last commit:",
                files.len()
            );
            for f in &files {
                println!("            {}", f);
            }
        }
    }

    Ok(())
}

// ---------------------------------------------------------------------------
// Trust-history JSONL
// ---------------------------------------------------------------------------

fn trust_history_path(cfg: &Config) -> std::path::PathBuf {
    cfg.db_path
        .parent()
        .unwrap_or(std::path::Path::new("."))
        .join(".asd")
        .join("trust-history.jsonl")
}

fn append_trust_history(
    cfg: &Config,
    trust: &agentstatedeveloper_core::TrustScore,
    indexed_at: Option<i64>,
    age_hours: Option<i64>,
    symbol_count: u64,
    sidecar_state: &str,
    dirty_file_count: usize,
    concept_gap_count: usize,
) {
    let now = chrono::Utc::now().to_rfc3339();
    let record = json!({
        "timestamp": now,
        "indexed_at_unix": indexed_at,
        "age_hours": age_hours,
        "db_state": if age_hours.map(|h| h >= 1).unwrap_or(false) { "stale" } else { "fresh" },
        "symbol_count": symbol_count,
        "sidecar_state": sidecar_state,
        "dirty_file_count": dirty_file_count,
        "concept_gap_count": concept_gap_count,
        "ledger_density": trust.signals.ledger_density,
        "schema_version": trust.signals.schema_version,
        "asd_version": env!("CARGO_PKG_VERSION"),
        "trust_score": trust.score,
        "trust_level": trust.level,
    });

    let path = trust_history_path(cfg);
    if let Some(parent) = path.parent() {
        let _ = std::fs::create_dir_all(parent);
    }

    // Read existing lines, cap at 500 before appending.
    const MAX_LINES: usize = 500;
    let existing = std::fs::read_to_string(&path).unwrap_or_default();
    let mut lines: Vec<&str> = existing.lines().collect();
    let new_line = serde_json::to_string(&record).unwrap_or_default();

    // Trim old entries so total stays at MAX_LINES after append.
    if lines.len() >= MAX_LINES {
        let keep = MAX_LINES - 1;
        lines = lines[lines.len() - keep..].to_vec();
    }

    let mut content = lines.join("\n");
    if !content.is_empty() {
        content.push('\n');
    }
    content.push_str(&new_line);
    content.push('\n');

    let _ = std::fs::write(&path, content);
}

// ---------------------------------------------------------------------------
// Trust-history reader (`asd status history`)
// ---------------------------------------------------------------------------

fn show_history(cfg: &Config, args: &StatusHistoryArgs) -> Result<()> {
    let path = trust_history_path(cfg);
    let content = std::fs::read_to_string(&path).unwrap_or_default();
    let mut records: Vec<serde_json::Value> = content
        .lines()
        .filter(|l| !l.trim().is_empty())
        .filter_map(|l| serde_json::from_str(l).ok())
        .collect();

    // Newest first, then take last N.
    records.reverse();
    records.truncate(args.last);
    records.reverse(); // restore chronological for display

    if records.is_empty() {
        if args.json {
            println!("[]");
        } else {
            println!("No trust-history snapshots yet. Run `asd status --json` to record one.");
        }
        return Ok(());
    }

    if args.json {
        println!("{}", serde_json::to_string_pretty(&records)?);
        return Ok(());
    }

    // Human-readable table.
    println!(
        "{:<25} {:>6} {:>7} {:>8} {:>10} {:>8} {:>5}",
        "timestamp", "score", "level", "symbols", "sidecar", "age_hrs", "dirty"
    );
    println!("{}", "-".repeat(75));
    for r in &records {
        let ts = r.get("timestamp").and_then(|v| v.as_str()).unwrap_or("?");
        // Trim to 23 chars (ISO without offset) for table width.
        let ts_short = &ts[..ts.len().min(23)];
        let score = r.get("trust_score").and_then(|v| v.as_f64()).unwrap_or(0.0);
        let level = r.get("trust_level").and_then(|v| v.as_str()).unwrap_or("?");
        let syms = r.get("symbol_count").and_then(|v| v.as_u64()).unwrap_or(0);
        let sidecar = r
            .get("sidecar_state")
            .and_then(|v| v.as_str())
            .unwrap_or("?");
        let age = r
            .get("age_hours")
            .and_then(|v| v.as_i64())
            .map(|h| h.to_string())
            .unwrap_or_else(|| "?".to_string());
        let dirty = r
            .get("dirty_file_count")
            .and_then(|v| v.as_u64())
            .unwrap_or(0);
        println!(
            "{:<25} {:>6.2} {:>7} {:>8} {:>10} {:>8} {:>5}",
            ts_short, score, level, syms, sidecar, age, dirty
        );
    }

    Ok(())
}

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

fn sidecar_state_key(s: &SidecarState) -> &'static str {
    match s {
        SidecarState::Missing => "missing",
        SidecarState::Present => "present",
        SidecarState::Hydrated => "hydrated",
        SidecarState::FreshReset => "fresh-reset",
    }
}

fn sidecar_action_hint(s: &SidecarState) -> &'static str {
    match s {
        SidecarState::Missing => "run 'asd sync' to create sidecar",
        SidecarState::Present => "run 'asd hydrate' to load sidecar into ASG",
        SidecarState::Hydrated => "sidecar is current",
        SidecarState::FreshReset => "re-run 'asd index' then 'asd sync'",
    }
}

fn collect_dirty_files(cfg: &Config) -> Vec<String> {
    let workspace = cfg.db_path.parent().unwrap_or(std::path::Path::new("."));
    let output = std::process::Command::new("git")
        .args(["status", "--short", "--untracked-files=no"])
        .current_dir(workspace)
        .output();

    let Ok(out) = output else {
        return vec![];
    };
    if !out.status.success() {
        return vec![];
    }

    let text = String::from_utf8_lossy(&out.stdout);
    let source_exts = [
        ".swift", ".py", ".ts", ".tsx", ".js", ".rs", ".go", ".kt", ".java", ".rb", ".cs",
    ];
    text.lines()
        .filter(|l| source_exts.iter().any(|ext| l.ends_with(ext)))
        .map(|l| l.trim().to_string())
        .collect()
}