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
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
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
//! `asd think <verb>` — capture agent thinking (Plan G t-003).
//!
//! Five verbs:
//!   - speculate <qname> --conf F --summary S    → Hypothesis
//!   - model NAME --symbols q1,q2 --summary S    → MentalModel
//!   - failed <qname> --tried T --because B       → FailedAttempt
//!   - question <qname> --q Q                    → OpenQuestion
//!   - list [--kind X] [--symbol Q]              → read-side
//!
//! Entries use deterministic blake3-derived IDs so re-running the
//! initial-read prompt overwrites instead of duplicating.

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

use agentstatedeveloper_core::{
    AsgIndexStore, AsgLedgerStore, Author, AuthorKind, ConclusionClass, Engine, IndexStore,
    LedgerEntry, LedgerKind, LedgerStore,
};

use crate::config::Config;

/// Initial-read prompt template, embedded at compile time so
/// `asd think prompt` works from any CWD (AcmeProj or any other
/// project that asd indexes — not just the AgentStateDeveloper
/// source checkout). The embedded copy lives inside the crate so the
/// published package builds; `docs/initial-read-prompt.md` is the copy
/// people read, and `initial_read_prompt_matches_docs` keeps them equal.
pub const INITIAL_READ_PROMPT: &str = include_str!("../../assets/initial-read-prompt.md");

#[derive(Debug, Subcommand)]
pub enum ThinkCmd {
    /// Record a Hypothesis: speculation with a confidence (0.0–1.0).
    Speculate(SpeculateArgs),
    /// Record a MentalModel: multi-symbol structural understanding.
    Model(ModelArgs),
    /// Record a FailedAttempt: negative evidence.
    Failed(FailedArgs),
    /// Record an OpenQuestion: known unknown blocking confident action.
    Question(QuestionArgs),
    /// List captured thinking entries.
    List(ListArgs),
    /// Print the initial-read prompt template to stdout. Embedded at
    /// compile time — works from any CWD. Pipe into your agent
    /// (e.g. `asd think prompt | claude`) or redirect to a file
    /// (`asd think prompt > .asd/onboarding-prompt.md`).
    Prompt,
    /// Print the initial-read prompt path + starter checklist.
    /// With --check, scans existing thinking entries and reports gaps.
    Bootstrap(BootstrapArgs),
}

#[derive(Debug, Args)]
pub struct BootstrapArgs {
    /// Scan existing thinking entries and report gaps instead of
    /// printing the starter checklist verbatim.
    #[arg(long)]
    pub check: bool,
    /// Emit JSON instead of human-readable text.
    #[arg(long)]
    pub json: bool,
}

#[derive(Debug, Args)]
pub struct SpeculateArgs {
    /// Fully-qualified symbol this hypothesis is about.
    pub qname: String,
    /// Confidence in [0.0, 1.0]. Below 0.3 is excluded from
    /// prior_thinking auto-surface by default.
    #[arg(long)]
    pub conf: f64,
    /// One-line claim.
    #[arg(long)]
    pub summary: String,
    /// Optional evidence body (markdown).
    #[arg(long)]
    pub body: Option<String>,
}

#[derive(Debug, Args)]
pub struct ModelArgs {
    /// Short name for the model (becomes the entry summary prefix).
    pub name: String,
    /// Comma-separated qnames the model spans.
    #[arg(long)]
    pub symbols: String,
    /// One-line description of how data/control flows.
    #[arg(long)]
    pub summary: String,
}

#[derive(Debug, Args)]
pub struct FailedArgs {
    /// Fully-qualified symbol the attempt targeted.
    pub qname: String,
    /// What was tried (one line).
    #[arg(long)]
    pub tried: String,
    /// Why it didn't work (one line).
    #[arg(long)]
    pub because: String,
}

#[derive(Debug, Args)]
pub struct QuestionArgs {
    /// Fully-qualified symbol the question is about.
    pub qname: String,
    /// The question (one line).
    #[arg(long = "q")]
    pub question: String,
}

#[derive(Debug, Args)]
pub struct ListArgs {
    /// Filter to one thinking kind.
    #[arg(long, value_enum)]
    pub kind: Option<ThinkKind>,
    /// Filter to one symbol.
    #[arg(long)]
    pub symbol: Option<String>,
}

#[derive(Debug, Clone, Copy, clap::ValueEnum)]
pub enum ThinkKind {
    Hypothesis,
    Model,
    Failed,
    Question,
}

pub fn run(cfg: &Config, cmd: ThinkCmd) -> Result<()> {
    match cmd {
        ThinkCmd::Speculate(a) => run_speculate(cfg, a),
        ThinkCmd::Model(a) => run_model(cfg, a),
        ThinkCmd::Failed(a) => run_failed(cfg, a),
        ThinkCmd::Question(a) => run_question(cfg, a),
        ThinkCmd::List(a) => run_list(cfg, a),
        ThinkCmd::Prompt => {
            // No trailing newline added — include_str! preserves
            // the file's final newline already; a second one would
            // pollute pipe consumers.
            print!("{}", INITIAL_READ_PROMPT);
            Ok(())
        }
        ThinkCmd::Bootstrap(a) => run_bootstrap(cfg, a),
    }
}

/// Counts of each thinking kind, plus optional you-vs-team
/// breakdown. Plan K t-006: `bootstrap --check` reports both.
#[derive(Debug, Default, Clone)]
struct ThinkingCounts {
    hypothesis: usize,
    mental_model: usize,
    failed_attempt: usize,
    open_question: usize,
    // "you" subset — entries whose author.id matches the local agent_id.
    hypothesis_you: usize,
    mental_model_you: usize,
    failed_attempt_you: usize,
    open_question_you: usize,
}

/// One inherited mental-model or hypothesis sample for the auto-
/// surfaced "Inherited thinking from prior session(s)" block in
/// default bootstrap output (Plan K t-006).
#[derive(Debug, Clone)]
struct InheritedSample {
    qname: String,
    summary: String,
    confidence: Option<f64>,
    author_id: String,
}

#[derive(Debug, Default, Clone)]
struct InheritanceSummary {
    counts: ThinkingCounts,
    /// Up to 3 mental-model samples (newest first).
    mental_models: Vec<InheritedSample>,
    /// Up to 3 highest-confidence hypotheses.
    top_hypotheses: Vec<InheritedSample>,
}

impl InheritanceSummary {
    /// Detection trigger: ≥1 MentalModel OR ≥3 Hypotheses → there's
    /// inherited thinking worth surfacing.
    fn has_inheritance(&self) -> bool {
        self.counts.mental_model >= 1 || self.counts.hypothesis >= 3
    }
}

/// Plan K t-006: walk every indexed symbol's ledger, bucket thinking
/// entries by kind and by author (you vs team), and collect a few
/// samples for the inheritance preview.
fn gather_thinking(cfg: &Config) -> Result<InheritanceSummary> {
    let engine = Engine::open_sqlite(&cfg.db_path)?;

    let mut counts = ThinkingCounts::default();
    let mut mm_samples: Vec<InheritedSample> = Vec::new();
    let mut hyp_samples: Vec<InheritedSample> = Vec::new();
    let me = cfg.agent_id.as_str();

    // One bulk walk (Plan T scale fix) instead of a per-qname symbol
    // fetch + per-symbol ledger read.
    for (qname, entries) in agentstatedeveloper_core::thinking::all_symbol_entries(&engine) {
        for e in entries {
            let is_me = e.author.id == me;
            match e.kind {
                LedgerKind::Hypothesis => {
                    counts.hypothesis += 1;
                    if is_me {
                        counts.hypothesis_you += 1;
                    }
                    hyp_samples.push(InheritedSample {
                        qname: qname.clone(),
                        summary: e.summary.clone(),
                        confidence: e.confidence,
                        author_id: e.author.id.clone(),
                    });
                }
                LedgerKind::MentalModel => {
                    counts.mental_model += 1;
                    if is_me {
                        counts.mental_model_you += 1;
                    }
                    mm_samples.push(InheritedSample {
                        qname: qname.clone(),
                        summary: e.summary.clone(),
                        confidence: None,
                        author_id: e.author.id.clone(),
                    });
                }
                LedgerKind::FailedAttempt => {
                    counts.failed_attempt += 1;
                    if is_me {
                        counts.failed_attempt_you += 1;
                    }
                }
                LedgerKind::OpenQuestion => {
                    counts.open_question += 1;
                    if is_me {
                        counts.open_question_you += 1;
                    }
                }
                _ => {}
            }
        }
    }

    // Top 3 hypotheses by confidence desc (None sorts last).
    hyp_samples.sort_by(|a, b| {
        b.confidence
            .unwrap_or(0.0)
            .partial_cmp(&a.confidence.unwrap_or(0.0))
            .unwrap_or(std::cmp::Ordering::Equal)
    });
    hyp_samples.truncate(3);
    mm_samples.truncate(3);

    Ok(InheritanceSummary {
        counts,
        mental_models: mm_samples,
        top_hypotheses: hyp_samples,
    })
}

/// Backward-compatible bare-counts helper (kept for any external
/// callers; internally we use `gather_thinking` for the richer
/// inheritance summary).
#[allow(dead_code)]
fn count_thinking(cfg: &Config) -> Result<(usize, usize, usize, usize)> {
    let s = gather_thinking(cfg)?;
    Ok((
        s.counts.hypothesis,
        s.counts.mental_model,
        s.counts.failed_attempt,
        s.counts.open_question,
    ))
}

fn run_bootstrap(cfg: &Config, args: BootstrapArgs) -> Result<()> {
    let prompt_path = "docs/initial-read-prompt.md";
    let summary = gather_thinking(cfg)?;

    if args.check {
        return run_bootstrap_check(prompt_path, &summary, args.json);
    }

    run_bootstrap_default(prompt_path, &summary, args.json)
}

fn run_bootstrap_check(prompt_path: &str, s: &InheritanceSummary, json_out: bool) -> Result<()> {
    let c = &s.counts;
    // Gaps are computed against the TEAM total (anything in the
    // ledger counts as "we have it"). The "you" subset is reported
    // alongside so a new dev can see what's inherited vs personal.
    let mut gaps: Vec<&str> = Vec::new();
    if c.mental_model == 0 {
        gaps.push(
            "no MentalModel yet — describe the top-level architecture with `asd think model`",
        );
    }
    if c.hypothesis == 0 {
        gaps.push("no Hypothesis yet — record at least one speculation with `asd think speculate`");
    }
    if c.open_question == 0 {
        gaps.push("no OpenQuestion yet — record known unknowns with `asd think question`");
    }
    if c.failed_attempt == 0 {
        gaps.push(
            "no FailedAttempt yet — once a dead end appears, capture with `asd think failed`",
        );
    }

    if json_out {
        println!(
            "{}",
            serde_json::to_string_pretty(&json!({
                "ok": true,
                "prompt_path": prompt_path,
                "counts_team": {
                    "hypothesis": c.hypothesis,
                    "mental_model": c.mental_model,
                    "failed_attempt": c.failed_attempt,
                    "open_question": c.open_question,
                },
                "counts_you": {
                    "hypothesis": c.hypothesis_you,
                    "mental_model": c.mental_model_you,
                    "failed_attempt": c.failed_attempt_you,
                    "open_question": c.open_question_you,
                },
                "gaps": gaps,
            }))?
        );
        return Ok(());
    }

    println!("# asd think bootstrap --check");
    println!();
    println!("prompt: {prompt_path}");
    println!();
    // Plan K t-006: print you-vs-team counts side-by-side so a new
    // dev can distinguish "what the team has captured" from
    // "what I personally have contributed."
    println!("counts (team / you):");
    println!(
        "  hypothesis     : {:>3} / {:>3}",
        c.hypothesis, c.hypothesis_you
    );
    println!(
        "  mental_model   : {:>3} / {:>3}",
        c.mental_model, c.mental_model_you
    );
    println!(
        "  failed_attempt : {:>3} / {:>3}",
        c.failed_attempt, c.failed_attempt_you
    );
    println!(
        "  open_question  : {:>3} / {:>3}",
        c.open_question, c.open_question_you
    );
    println!();
    if gaps.is_empty() {
        println!("gaps: none — every thinking bucket has at least one entry.");
    } else {
        println!("gaps:");
        for g in &gaps {
            println!("  - {g}");
        }
    }
    Ok(())
}

fn run_bootstrap_default(prompt_path: &str, s: &InheritanceSummary, json_out: bool) -> Result<()> {
    if json_out {
        // JSON output: include inheritance summary if present so MCP
        // callers can detect it programmatically.
        let mut payload = json!({
            "ok": true,
            "prompt_path": prompt_path,
            "prompt_command": "asd think prompt",
            "checklist": [
                "Read the prompt: run `asd think prompt` (embedded — works from any CWD)",
                "Run `asd reindex` so the project graph is current (defaults to CWD)",
                "Capture at least one MentalModel for the top-level architecture",
                "Capture Hypotheses for hot files with confidence in [0.0, 1.0]",
                "Record OpenQuestions for known unknowns",
                "Record FailedAttempts as dead ends emerge",
                "Re-run `asd think bootstrap --check` to confirm coverage",
            ],
            "commands": {
                "model": "asd think model <NAME> --symbols a.b,c.d --summary \"...\"",
                "speculate": "asd think speculate <QNAME> --conf 0.6 --summary \"...\"",
                "question": "asd think question <QNAME> --q \"...\"",
                "failed": "asd think failed <QNAME> --tried \"...\" --because \"...\"",
                "list": "asd think list [--kind hypothesis|model|failed|question]",
            },
        });
        if s.has_inheritance() {
            payload["inherited_thinking"] = inheritance_json(s);
        }
        println!("{}", serde_json::to_string_pretty(&payload)?);
        return Ok(());
    }

    println!("# asd think bootstrap");
    println!();
    if s.has_inheritance() {
        print_inheritance_block(s);
    }
    println!("Prompt template: `asd think prompt` (embedded — works from any CWD)");
    println!("                 source: {prompt_path} (in the AgentStateDeveloper repo)");
    println!();
    println!("Starter checklist:");
    println!("  1. Read the prompt: `asd think prompt` (pipe into your agent or read directly)");
    println!("  2. Run `asd reindex` so the project graph is current (defaults to CWD)");
    println!("  3. Capture at least one MentalModel for the top-level architecture");
    println!("  4. Capture Hypotheses for hot files with confidence in [0.0, 1.0]");
    println!("  5. Record OpenQuestions for known unknowns");
    println!("  6. Record FailedAttempts as dead ends emerge");
    println!("  7. Re-run `asd think bootstrap --check` to confirm coverage");
    println!();
    println!("Write-back commands:");
    println!("  asd think model   <NAME>  --symbols a.b,c.d --summary \"...\"");
    println!("  asd think speculate <QN>  --conf 0.6        --summary \"...\"");
    println!("  asd think question  <QN>  --q \"...\"");
    println!("  asd think failed    <QN>  --tried \"...\" --because \"...\"");
    println!("  asd think list      [--kind hypothesis|model|failed|question]");
    Ok(())
}

/// Plan K t-006: pretty-print the inherited-thinking block surfaced
/// at the top of `asd think bootstrap` when a prior session captured
/// enough thinking to be worth advertising. Goal: a new dev's agent
/// sees "you're not starting from zero — here's what the team
/// already concluded" before being pushed into the initial-read flow.
fn print_inheritance_block(s: &InheritanceSummary) {
    let c = &s.counts;
    println!("## Inherited thinking from prior session(s)");
    println!();
    println!(
        "  {} MentalModel{}, {} Hypothes{}, {} OpenQuestion{}, {} FailedAttempt{} already on file.",
        c.mental_model,
        if c.mental_model == 1 { "" } else { "s" },
        c.hypothesis,
        if c.hypothesis == 1 { "is" } else { "es" },
        c.open_question,
        if c.open_question == 1 { "" } else { "s" },
        c.failed_attempt,
        if c.failed_attempt == 1 { "" } else { "s" },
    );
    println!();
    if !s.mental_models.is_empty() {
        println!("  Mental models:");
        for mm in &s.mental_models {
            println!("    - {} ({}) — {}", mm.qname, mm.author_id, mm.summary);
        }
        println!();
    }
    if !s.top_hypotheses.is_empty() {
        println!("  Top hypotheses (by confidence):");
        for h in &s.top_hypotheses {
            let conf = h
                .confidence
                .map(|c| format!("{:.2}", c))
                .unwrap_or_else(|| "—".into());
            println!(
                "    - [{conf}] {} ({}) — {}",
                h.qname, h.author_id, h.summary
            );
        }
        println!();
    }
    println!("  Read `.asd/conclusions/thinking.jsonl` or run `asd think list` for the full set.");
    println!("  The starter checklist below still applies for anything the team hasn't covered.");
    println!();
}

fn inheritance_json(s: &InheritanceSummary) -> serde_json::Value {
    let c = &s.counts;
    json!({
        "counts_team": {
            "hypothesis": c.hypothesis,
            "mental_model": c.mental_model,
            "failed_attempt": c.failed_attempt,
            "open_question": c.open_question,
        },
        "counts_you": {
            "hypothesis": c.hypothesis_you,
            "mental_model": c.mental_model_you,
            "failed_attempt": c.failed_attempt_you,
            "open_question": c.open_question_you,
        },
        "mental_models": s.mental_models.iter().map(|m| json!({
            "qname": m.qname, "author": m.author_id, "summary": m.summary,
        })).collect::<Vec<_>>(),
        "top_hypotheses": s.top_hypotheses.iter().map(|h| json!({
            "qname": h.qname, "author": h.author_id,
            "confidence": h.confidence, "summary": h.summary,
        })).collect::<Vec<_>>(),
    })
}

fn open_with_symbol(cfg: &Config, qname: &str) -> Result<(Engine, String)> {
    let engine = Engine::open_sqlite(&cfg.db_path)?;
    let index = AsgIndexStore::from_engine(&engine);
    let sym = index
        .get_symbol_by_qname(&engine.ref_name, qname)?
        .ok_or_else(|| anyhow!("symbol not found: {}", qname))?;
    Ok((engine, sym.symbol_id))
}

fn agent_author(cfg: &Config) -> Author {
    Author {
        kind: AuthorKind::Agent,
        id: cfg.agent_id.clone(),
    }
}

/// Deterministic entry id so re-running the initial-read prompt
/// overwrites instead of duplicating. Same (intent, qname, summary)
/// → same id.
fn det_id(intent: &str, qname: &str, content: &str) -> String {
    let key = format!("think:{intent}:{qname}:{content}");
    let h = blake3::hash(key.as_bytes()).to_hex();
    let short: String = h.chars().take(24).collect();
    format!("led_think_{short}")
}

/// Plan G t-007: read the active CTX task id from `CTX_ACTIVE_TASK`
/// env var (JSON: `{"task_id": "..."}`) with a fallback to the
/// `.asd/cache/active-task.json` file under the DB parent. Returns
/// `None` when neither source is set — callers should skip the tag
/// in that case.
///
/// Mirrors the helper in `commands::map`; kept local so think.rs
/// stays self-contained.
fn read_active_ctx_task_id_from(
    env_raw: Option<&str>,
    db_parent: Option<&std::path::Path>,
) -> Option<String> {
    let raw = match env_raw {
        Some(s) if !s.is_empty() => s.to_string(),
        _ => {
            let p = db_parent?
                .join(".asd")
                .join("cache")
                .join("active-task.json");
            std::fs::read_to_string(p).ok()?
        }
    };
    let v: serde_json::Value = serde_json::from_str(&raw).ok()?;
    v.get("task_id")?.as_str().map(String::from)
}

fn active_ctx_task_tag(cfg: &Config) -> Option<String> {
    let env_raw = std::env::var("CTX_ACTIVE_TASK").ok();
    let db_parent = std::path::Path::new(&cfg.db_path).parent();
    read_active_ctx_task_id_from(env_raw.as_deref(), db_parent).map(|id| format!("ctx:task:{id}"))
}

/// Append `source:asd-think` and (when set) `ctx:task:<id>` to an
/// entry's tag list. Called by every `asd think *` writer so Plan G
/// entries inherit the same provenance trail Plan E added for map/ledger.
fn push_provenance_tags(cfg: &Config, tags: &mut Vec<String>) {
    tags.push("source:asd-think".into());
    if let Some(t) = active_ctx_task_tag(cfg) {
        tags.push(t);
    }
}

fn run_speculate(cfg: &Config, args: SpeculateArgs) -> Result<()> {
    if !(0.0..=1.0).contains(&args.conf) {
        bail!("--conf must be in [0.0, 1.0]; got {}", args.conf);
    }
    let (engine, symbol_id) = open_with_symbol(cfg, &args.qname)?;
    let ledger = AsgLedgerStore::from_engine(&engine);
    let mut entry = LedgerEntry::new(
        &symbol_id,
        LedgerKind::Hypothesis,
        &args.summary,
        agent_author(cfg),
    );
    entry.entry_id = det_id("hypothesis", &args.qname, &args.summary);
    entry.confidence = Some(args.conf);
    entry.body = args.body;
    push_provenance_tags(cfg, &mut entry.tags);
    ledger.append_entry(&engine.ref_name, &entry, &cfg.agent_id)?;
    println!(
        "{}",
        json!({
            "ok": true, "kind": "hypothesis", "qname": args.qname,
            "confidence": args.conf, "entry_id": entry.entry_id,
        })
    );
    Ok(())
}

fn run_model(cfg: &Config, args: ModelArgs) -> Result<()> {
    let symbols: Vec<String> = args
        .symbols
        .split(',')
        .map(|s| s.trim().to_string())
        .filter(|s| !s.is_empty())
        .collect();
    if symbols.is_empty() {
        bail!("--symbols must list at least one qname (comma-separated)");
    }
    // Anchor the model on the FIRST symbol — that's the lookup target
    // for prepare-change's prior_thinking surfacing.
    let (engine, symbol_id) = open_with_symbol(cfg, &symbols[0])?;
    let ledger = AsgLedgerStore::from_engine(&engine);
    let body = json!({ "symbols": symbols, "name": args.name }).to_string();
    let mut entry = LedgerEntry::new(
        &symbol_id,
        LedgerKind::MentalModel,
        format!("{}: {}", args.name, args.summary),
        agent_author(cfg),
    );
    entry.entry_id = det_id("model", &args.name, &args.summary);
    entry.body = Some(body);
    push_provenance_tags(cfg, &mut entry.tags);
    ledger.append_entry(&engine.ref_name, &entry, &cfg.agent_id)?;
    println!(
        "{}",
        json!({
            "ok": true, "kind": "mental_model", "name": args.name,
            "symbols": symbols, "entry_id": entry.entry_id,
        })
    );
    Ok(())
}

fn run_failed(cfg: &Config, args: FailedArgs) -> Result<()> {
    let (engine, symbol_id) = open_with_symbol(cfg, &args.qname)?;
    let ledger = AsgLedgerStore::from_engine(&engine);
    let body = json!({ "tried": &args.tried, "because": &args.because }).to_string();
    let mut entry = LedgerEntry::new(
        &symbol_id,
        LedgerKind::FailedAttempt,
        format!("failed: {} — because {}", args.tried, args.because),
        agent_author(cfg),
    );
    entry.entry_id = det_id("failed", &args.qname, &args.tried);
    entry.body = Some(body);
    push_provenance_tags(cfg, &mut entry.tags);
    ledger.append_entry(&engine.ref_name, &entry, &cfg.agent_id)?;
    println!(
        "{}",
        json!({
            "ok": true, "kind": "failed_attempt", "qname": args.qname,
            "entry_id": entry.entry_id,
        })
    );
    Ok(())
}

fn run_question(cfg: &Config, args: QuestionArgs) -> Result<()> {
    let (engine, symbol_id) = open_with_symbol(cfg, &args.qname)?;
    let ledger = AsgLedgerStore::from_engine(&engine);
    let mut entry = LedgerEntry::new(
        &symbol_id,
        LedgerKind::OpenQuestion,
        &args.question,
        agent_author(cfg),
    );
    entry.entry_id = det_id("question", &args.qname, &args.question);
    push_provenance_tags(cfg, &mut entry.tags);
    ledger.append_entry(&engine.ref_name, &entry, &cfg.agent_id)?;
    println!(
        "{}",
        json!({
            "ok": true, "kind": "open_question", "qname": args.qname,
            "entry_id": entry.entry_id,
        })
    );
    Ok(())
}

fn run_list(cfg: &Config, args: ListArgs) -> Result<()> {
    let engine = Engine::open_sqlite(&cfg.db_path)?;
    let index = AsgIndexStore::from_engine(&engine);
    let ledger = AsgLedgerStore::from_engine(&engine);
    let ref_name = engine.ref_name.clone();

    let kind_filter = args.kind.map(|k| match k {
        ThinkKind::Hypothesis => LedgerKind::Hypothesis,
        ThinkKind::Model => LedgerKind::MentalModel,
        ThinkKind::Failed => LedgerKind::FailedAttempt,
        ThinkKind::Question => LedgerKind::OpenQuestion,
    });

    // Resolve which symbols to scan → (qname, live entries) pairs.
    // Workspace-wide: one bulk walk (Plan T scale fix) instead of
    // per-qname symbol + ledger reads.
    let symbol_entries: Vec<(String, Vec<LedgerEntry>)> =
        if let Some(qname) = args.symbol.as_deref() {
            match index.get_symbol_by_qname(&ref_name, qname)? {
                Some(sym) => vec![(
                    sym.qname,
                    ledger
                        .list_entries(&ref_name, &sym.symbol_id)
                        .unwrap_or_default(),
                )],
                None => return Err(anyhow!("symbol not found: {qname}")),
            }
        } else {
            agentstatedeveloper_core::thinking::all_symbol_entries(&engine)
        };

    let mut entries = Vec::new();
    for (qname, les) in &symbol_entries {
        for entry in les.iter().cloned() {
            if entry.kind.conclusion_class() != ConclusionClass::Thinking {
                continue;
            }
            if let Some(filter) = kind_filter {
                if entry.kind != filter {
                    continue;
                }
            }
            entries.push(json!({
                "entry_id": entry.entry_id,
                "kind": entry.kind.as_str(),
                "qname": qname,
                "summary": entry.summary,
                "confidence": entry.confidence,
                "body": entry.body,
                "tags": entry.tags,
                "created_at": entry.created_at,
            }));
        }
    }

    println!(
        "{}",
        serde_json::to_string_pretty(&json!({
            "total": entries.len(),
            "kind_filter": kind_filter.map(|k| k.as_str()),
            "symbol_filter": args.symbol,
            "entries": entries,
        }))?
    );
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    /// The embedded prompt must match `docs/initial-read-prompt.md`. Skipped
    /// outside the source checkout (e.g. in the published package), where
    /// `docs/` does not exist.
    #[test]
    fn initial_read_prompt_matches_docs() {
        let docs = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
            .join("../../docs/initial-read-prompt.md");
        let Ok(text) = std::fs::read_to_string(&docs) else {
            return;
        };
        assert_eq!(
            INITIAL_READ_PROMPT, text,
            "assets/initial-read-prompt.md has drifted from docs/initial-read-prompt.md; copy the docs file over it"
        );
    }

    #[test]
    fn ctx_task_id_extracted_from_env_json() {
        let id = read_active_ctx_task_id_from(Some(r#"{"task_id":"plan-g-005"}"#), None);
        assert_eq!(id.as_deref(), Some("plan-g-005"));
    }

    #[test]
    fn ctx_task_id_returns_none_when_env_empty_and_no_file() {
        let tmp = tempfile::tempdir().unwrap();
        assert_eq!(read_active_ctx_task_id_from(None, Some(tmp.path())), None);
        assert_eq!(
            read_active_ctx_task_id_from(Some(""), Some(tmp.path())),
            None
        );
    }

    #[test]
    fn ctx_task_id_falls_back_to_active_task_file() {
        let tmp = tempfile::tempdir().unwrap();
        let cache = tmp.path().join(".asd").join("cache");
        std::fs::create_dir_all(&cache).unwrap();
        std::fs::write(cache.join("active-task.json"), r#"{"task_id":"file-task"}"#).unwrap();
        let id = read_active_ctx_task_id_from(None, Some(tmp.path()));
        assert_eq!(id.as_deref(), Some("file-task"));
    }

    #[test]
    fn ctx_task_id_env_wins_over_file() {
        let tmp = tempfile::tempdir().unwrap();
        let cache = tmp.path().join(".asd").join("cache");
        std::fs::create_dir_all(&cache).unwrap();
        std::fs::write(cache.join("active-task.json"), r#"{"task_id":"file-task"}"#).unwrap();
        let id = read_active_ctx_task_id_from(Some(r#"{"task_id":"env-task"}"#), Some(tmp.path()));
        assert_eq!(id.as_deref(), Some("env-task"));
    }

    // ---- Plan K t-006: inheritance detection logic ----------------------
    //
    // `gather_thinking` requires a live engine + Config, which is more
    // setup than a unit test should own. We unit-test the pure logic
    // (`has_inheritance` trigger) directly here; integration coverage
    // of the full bootstrap flow lives in tests/think_bootstrap_inheritance.rs.

    #[test]
    fn has_inheritance_triggers_on_one_mental_model() {
        let mut s = InheritanceSummary::default();
        s.counts.mental_model = 1;
        assert!(s.has_inheritance());
    }

    #[test]
    fn has_inheritance_triggers_on_three_hypotheses() {
        let mut s = InheritanceSummary::default();
        s.counts.hypothesis = 3;
        assert!(s.has_inheritance());
    }

    #[test]
    fn has_inheritance_false_when_only_one_or_two_hypotheses() {
        let mut s = InheritanceSummary::default();
        s.counts.hypothesis = 2;
        assert!(
            !s.has_inheritance(),
            "two hypotheses without a mental model should NOT trigger inheritance preview"
        );
    }

    #[test]
    fn has_inheritance_false_when_only_questions_or_failures() {
        let mut s = InheritanceSummary::default();
        s.counts.open_question = 10;
        s.counts.failed_attempt = 10;
        assert!(
            !s.has_inheritance(),
            "open questions / failed attempts alone are not the inheritance trigger"
        );
    }
}