autom8-cli 0.3.0

CLI automation tool for orchestrating Claude-powered development
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
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
//! Improve command handler.
//!
//! Gathers context from git and previous autom8 sessions to enable
//! follow-up work with Claude. This command auto-detects everything
//! from the current git branch.

use crate::claude::run_improve_session;
use crate::error::{Autom8Error, Result};
use crate::gh::find_spec_for_branch;
use crate::git::{self, CommitInfo, DiffEntry};
use crate::knowledge::ProjectKnowledge;
use crate::output::improve::print_context_summary;
use crate::spec::Spec;
use crate::state::StateManager;
use std::path::PathBuf;

// ============================================================================
// US-008: Command Handler
// ============================================================================

/// Run the improve command.
///
/// This command gathers context from git and previous autom8 sessions,
/// displays a summary, and spawns an interactive Claude session for follow-up work.
///
/// The workflow is:
/// 1. Check if we're in a git repo (required)
/// 2. Gather context (git, spec, session knowledge)
/// 3. Display summary to the user (including any edge case warnings)
/// 4. Build the context prompt
/// 5. Spawn interactive Claude session
///
/// Edge cases handled:
/// - Not in a git repo: shows error with suggestion
/// - On main/master branch: shows warning but proceeds
/// - No session/spec found: shows message that only git context is available
/// - No commits vs base: shows "no commits yet" in summary
///
/// # Arguments
/// * `_verbose` - Reserved for future extensibility (currently unused)
///
/// # Returns
/// * `Ok(())` - Session completed
/// * `Err` - Not in git repo, context loading failed, or Claude spawn failed
pub fn improve_command(_verbose: bool) -> Result<()> {
    // Step 0: Check if we're in a git repo (fatal error if not)
    if !git::is_git_repo() {
        return Err(Autom8Error::NotInGitRepo);
    }

    // Step 1: Gather all context layers
    let context = load_follow_up_context()?;

    // Step 2: Display summary to the user
    print_context_summary(&context);

    // Step 3: Build the context prompt
    let prompt = build_improve_prompt(&context);

    // Step 4: Spawn interactive Claude session
    run_improve_session(&prompt)?;

    Ok(())
}

// ============================================================================
// US-001: Git Context Gathering
// ============================================================================

/// Git context for the current branch.
///
/// Contains all git-related context needed for the improve command.
/// This is Layer 1 context - always available when in a git repo.
#[derive(Debug, Clone)]
pub struct GitContext {
    /// Current branch name
    pub branch_name: String,
    /// Base branch name (main/master)
    pub base_branch: String,
    /// All commits on this branch since diverging from base
    pub commits: Vec<CommitInfo>,
    /// All file changes since diverging from base
    pub diff_entries: Vec<DiffEntry>,
    /// The merge-base commit hash (common ancestor with base branch)
    pub merge_base_commit: Option<String>,
}

impl GitContext {
    /// Check if this is a feature branch (not main/master).
    pub fn is_feature_branch(&self) -> bool {
        self.branch_name != "main" && self.branch_name != "master"
    }

    /// Get the number of commits on this branch.
    pub fn commit_count(&self) -> usize {
        self.commits.len()
    }

    /// Get the total number of files changed.
    pub fn files_changed_count(&self) -> usize {
        self.diff_entries.len()
    }

    /// Get total additions across all changed files.
    pub fn total_additions(&self) -> u32 {
        self.diff_entries.iter().map(|e| e.additions).sum()
    }

    /// Get total deletions across all changed files.
    pub fn total_deletions(&self) -> u32 {
        self.diff_entries.iter().map(|e| e.deletions).sum()
    }
}

/// Gather git context for the current branch.
///
/// This function collects:
/// - Current branch name
/// - Base branch (main/master)
/// - All commits since diverging from base
/// - All file changes since merge-base
///
/// Errors are handled gracefully - if certain operations fail,
/// the function will return partial context with empty collections.
///
/// # Returns
/// * `Ok(GitContext)` - Git context for the current branch
/// * `Err` - Only if we cannot get the current branch (fatal)
pub fn gather_git_context() -> Result<GitContext> {
    // Get current branch - this is required
    let branch_name = git::current_branch()?;

    // Detect base branch (main/master) - defaults to "main" if detection fails
    let base_branch = git::detect_base_branch().unwrap_or_else(|_| "main".to_string());

    // Get merge-base commit for accurate diff calculation
    let merge_base_commit = git::get_merge_base(&base_branch).ok();

    // Get commits since base branch
    // If this fails (e.g., branch doesn't exist), return empty vec
    let commits = git::get_branch_commits(&base_branch).unwrap_or_default();

    // Get diff entries since merge-base
    // Use merge-base if available, otherwise compare against base branch directly
    let diff_entries = if let Some(ref merge_base) = merge_base_commit {
        git::get_diff_since(merge_base).unwrap_or_default()
    } else {
        // Fallback: try to diff against base branch directly
        git::get_diff_since(&base_branch).unwrap_or_default()
    };

    Ok(GitContext {
        branch_name,
        base_branch,
        commits,
        diff_entries,
        merge_base_commit,
    })
}

// ============================================================================
// US-005: Build Context Prompt
// ============================================================================

/// Build a conversational, concise prompt summarizing the loaded context.
///
/// This prompt is shown to Claude at the start of an interactive improve session.
/// It acknowledges what context is available and ends with an open question.
///
/// # Arguments
/// * `context` - The follow-up context containing git, spec, and knowledge info
///
/// # Returns
/// A formatted prompt string (target: under ~500 words)
pub fn build_improve_prompt(context: &FollowUpContext) -> String {
    let mut sections: Vec<String> = Vec::new();

    // Opening statement acknowledging branch and what's loaded
    let opening = build_opening_statement(context);
    sections.push(opening);

    // Spec summary if available
    if let Some(ref spec) = context.spec {
        sections.push(build_spec_summary(spec));
    }

    // Key decisions if available
    if let Some(ref knowledge) = context.knowledge {
        if !knowledge.decisions.is_empty() {
            sections.push(build_decisions_summary(&knowledge.decisions));
        }
    }

    // Files touched if available
    if let Some(ref knowledge) = context.knowledge {
        if !knowledge.story_changes.is_empty() {
            if let Some(files_section) = build_files_summary(&knowledge.story_changes) {
                sections.push(files_section);
            }
        }
    }

    // Work summaries if available
    if !context.work_summaries.is_empty() {
        sections.push(build_work_summaries(&context.work_summaries));
    }

    // Closing question
    sections.push("What would you like to work on?".to_string());

    sections.join("\n\n")
}

/// Build the opening statement based on available context.
fn build_opening_statement(context: &FollowUpContext) -> String {
    let branch = &context.git.branch_name;
    let level = context.richness_level();

    match level {
        3 => format!(
            "You're on branch `{}`. I've loaded the spec, session knowledge, and git history.",
            branch
        ),
        2 => {
            if context.has_spec() {
                format!(
                    "You're on branch `{}`. I've loaded the spec and git history.",
                    branch
                )
            } else {
                format!(
                    "You're on branch `{}`. I've loaded session knowledge and git history.",
                    branch
                )
            }
        }
        _ => format!(
            "You're on branch `{}`. I've loaded the git history.",
            branch
        ),
    }
}

/// Build a summary of the spec.
fn build_spec_summary(spec: &Spec) -> String {
    let (completed, total) = spec.progress();
    let status = if spec.all_complete() {
        "all complete".to_string()
    } else {
        format!("{}/{} stories complete", completed, total)
    };

    format!("**Feature:** {} ({})", spec.project, status)
}

/// Build a summary of key decisions (topic and choice only).
fn build_decisions_summary(decisions: &[crate::knowledge::Decision]) -> String {
    let mut lines = vec!["**Key decisions:**".to_string()];

    // Limit to 5 most recent decisions to keep prompt concise
    for decision in decisions.iter().take(5) {
        lines.push(format!("- {}: {}", decision.topic, decision.choice));
    }

    if decisions.len() > 5 {
        lines.push(format!("- ...and {} more", decisions.len() - 5));
    }

    lines.join("\n")
}

/// Build a summary of files touched, grouped by created/modified.
fn build_files_summary(story_changes: &[crate::knowledge::StoryChanges]) -> Option<String> {
    use std::collections::HashSet;

    let mut created: HashSet<&std::path::Path> = HashSet::new();
    let mut modified: HashSet<&std::path::Path> = HashSet::new();

    for changes in story_changes {
        for file in &changes.files_created {
            created.insert(&file.path);
        }
        for file in &changes.files_modified {
            // Don't include in modified if already in created
            if !created.contains(file.path.as_path()) {
                modified.insert(&file.path);
            }
        }
    }

    if created.is_empty() && modified.is_empty() {
        return None;
    }

    let mut lines = vec!["**Files touched:**".to_string()];

    // Sort and limit files for readability
    let mut created_vec: Vec<_> = created.iter().collect();
    created_vec.sort();

    let mut modified_vec: Vec<_> = modified.iter().collect();
    modified_vec.sort();

    // Show created files (limit to 8)
    if !created_vec.is_empty() {
        lines.push("Created:".to_string());
        for path in created_vec.iter().take(8) {
            lines.push(format!("- {}", path.display()));
        }
        if created_vec.len() > 8 {
            lines.push(format!("- ...and {} more", created_vec.len() - 8));
        }
    }

    // Show modified files (limit to 8)
    if !modified_vec.is_empty() {
        lines.push("Modified:".to_string());
        for path in modified_vec.iter().take(8) {
            lines.push(format!("- {}", path.display()));
        }
        if modified_vec.len() > 8 {
            lines.push(format!("- ...and {} more", modified_vec.len() - 8));
        }
    }

    Some(lines.join("\n"))
}

/// Build a summary of work completed in previous iterations.
fn build_work_summaries(summaries: &[String]) -> String {
    let mut lines = vec!["**Work completed:**".to_string()];

    // Limit to 5 most recent summaries
    for summary in summaries.iter().take(5) {
        // Truncate long summaries to first 100 chars
        let truncated = if summary.len() > 100 {
            format!("{}...", &summary[..97])
        } else {
            summary.clone()
        };
        lines.push(format!("- {}", truncated));
    }

    if summaries.len() > 5 {
        lines.push(format!("- ...and {} more iterations", summaries.len() - 5));
    }

    lines.join("\n")
}

// ============================================================================
// US-004: Follow-Up Context (Combined Layers)
// ============================================================================

/// Combined context for follow-up work with Claude.
///
/// This struct combines all three context layers:
/// - Layer 1 (Git): Always present - branch, commits, diff entries
/// - Layer 2 (Spec): Optional - loaded from session or by branch name
/// - Layer 3 (Knowledge): Optional - decisions, patterns, files, work summaries
///
/// All layers except git are optional and degrade gracefully when unavailable.
#[derive(Debug, Clone)]
pub struct FollowUpContext {
    /// Git context (Layer 1) - always present
    pub git: GitContext,

    /// Spec loaded from session or by branch name (Layer 2) - optional
    pub spec: Option<Spec>,

    /// Path to the spec file (if spec was loaded)
    pub spec_path: Option<PathBuf>,

    /// Project knowledge from the session (Layer 3) - optional
    pub knowledge: Option<ProjectKnowledge>,

    /// Work summaries collected from all iterations (Layer 3) - optional
    /// Each summary describes what was accomplished in an iteration.
    pub work_summaries: Vec<String>,

    /// Session ID if a matching session was found
    pub session_id: Option<String>,
}

impl FollowUpContext {
    /// Check if spec context is available.
    pub fn has_spec(&self) -> bool {
        self.spec.is_some()
    }

    /// Check if session knowledge is available.
    pub fn has_knowledge(&self) -> bool {
        self.knowledge.is_some()
    }

    /// Check if any work summaries were collected.
    pub fn has_work_summaries(&self) -> bool {
        !self.work_summaries.is_empty()
    }

    /// Get the total number of work summaries.
    pub fn work_summary_count(&self) -> usize {
        self.work_summaries.len()
    }

    /// Get context richness level (1-3) based on available layers.
    ///
    /// - Level 1: Git only
    /// - Level 2: Git + Spec
    /// - Level 3: Git + Spec + Knowledge
    pub fn richness_level(&self) -> u8 {
        let mut level = 1; // Git is always present
        if self.has_spec() {
            level += 1;
        }
        if self.has_knowledge() {
            level += 1;
        }
        level
    }
}

/// Load all context layers for follow-up work.
///
/// This function gathers context additively:
/// 1. Git context is always gathered (required)
/// 2. Spec is loaded from session's spec_json_path if available,
///    otherwise by matching branch name
/// 3. If a session is found, project knowledge and work summaries
///    are extracted from the RunState
///
/// # Returns
/// * `Ok(FollowUpContext)` - Combined context from all available layers
/// * `Err` - Only if git context gathering fails (fatal)
pub fn load_follow_up_context() -> Result<FollowUpContext> {
    // Layer 1: Git context (always required)
    let git = gather_git_context()?;

    // Try to find a matching session for this branch
    let state_manager = StateManager::new()?;
    let session_metadata = state_manager
        .find_session_for_branch(&git.branch_name)
        .ok()
        .flatten();

    let mut spec: Option<Spec> = None;
    let mut spec_path: Option<PathBuf> = None;
    let mut knowledge: Option<ProjectKnowledge> = None;
    let mut work_summaries: Vec<String> = Vec::new();
    let mut session_id: Option<String> = None;

    if let Some(ref metadata) = session_metadata {
        session_id = Some(metadata.session_id.clone());

        // Layer 2: Try to load spec from session's spec_json_path first
        if let Some(ref path) = metadata.spec_json_path {
            if path.exists() {
                if let Ok(loaded_spec) = Spec::load(path) {
                    spec = Some(loaded_spec);
                    spec_path = Some(path.clone());
                }
            }
        }

        // Layer 3: Load RunState to extract knowledge and work summaries
        let session_state_manager = StateManager::with_session(metadata.session_id.clone())?;
        if let Ok(Some(run_state)) = session_state_manager.load_current() {
            // Extract project knowledge
            knowledge = Some(run_state.knowledge.clone());

            // Extract work summaries from iterations
            work_summaries = run_state
                .iterations
                .iter()
                .filter_map(|iter| iter.work_summary.clone())
                .collect();
        }
    }

    // Layer 2 fallback: If spec wasn't loaded from session, try find_spec_for_branch
    if spec.is_none() {
        if let Ok(Some((found_spec, found_path))) = find_spec_for_branch(&git.branch_name) {
            spec = Some(found_spec);
            spec_path = Some(found_path);
        }
    }

    Ok(FollowUpContext {
        git,
        spec,
        spec_path,
        knowledge,
        work_summaries,
        session_id,
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::git::{DiffEntry, DiffStatus};
    use crate::knowledge::{Decision, Pattern};
    use crate::spec::UserStory;
    use std::path::PathBuf;

    // ========================================================================
    // GitContext struct tests
    // ========================================================================

    #[test]
    fn test_git_context_is_feature_branch_true() {
        let context = GitContext {
            branch_name: "feature/improve-command".to_string(),
            base_branch: "main".to_string(),
            commits: vec![],
            diff_entries: vec![],
            merge_base_commit: None,
        };

        assert!(context.is_feature_branch());
    }

    #[test]
    fn test_git_context_is_feature_branch_false_main() {
        let context = GitContext {
            branch_name: "main".to_string(),
            base_branch: "main".to_string(),
            commits: vec![],
            diff_entries: vec![],
            merge_base_commit: None,
        };

        assert!(!context.is_feature_branch());
    }

    #[test]
    fn test_git_context_is_feature_branch_false_master() {
        let context = GitContext {
            branch_name: "master".to_string(),
            base_branch: "master".to_string(),
            commits: vec![],
            diff_entries: vec![],
            merge_base_commit: None,
        };

        assert!(!context.is_feature_branch());
    }

    #[test]
    fn test_git_context_commit_count() {
        let context = GitContext {
            branch_name: "feature/test".to_string(),
            base_branch: "main".to_string(),
            commits: vec![
                crate::git::CommitInfo {
                    short_hash: "abc1234".to_string(),
                    full_hash: "abc1234567890".to_string(),
                    message: "First commit".to_string(),
                    author: "Test".to_string(),
                    date: "2024-01-15".to_string(),
                },
                crate::git::CommitInfo {
                    short_hash: "def5678".to_string(),
                    full_hash: "def5678901234".to_string(),
                    message: "Second commit".to_string(),
                    author: "Test".to_string(),
                    date: "2024-01-16".to_string(),
                },
            ],
            diff_entries: vec![],
            merge_base_commit: Some("basehash".to_string()),
        };

        assert_eq!(context.commit_count(), 2);
    }

    #[test]
    fn test_git_context_files_changed_count() {
        let context = GitContext {
            branch_name: "feature/test".to_string(),
            base_branch: "main".to_string(),
            commits: vec![],
            diff_entries: vec![
                DiffEntry {
                    path: PathBuf::from("src/lib.rs"),
                    additions: 10,
                    deletions: 5,
                    status: DiffStatus::Modified,
                },
                DiffEntry {
                    path: PathBuf::from("src/main.rs"),
                    additions: 20,
                    deletions: 0,
                    status: DiffStatus::Added,
                },
            ],
            merge_base_commit: None,
        };

        assert_eq!(context.files_changed_count(), 2);
    }

    #[test]
    fn test_git_context_total_additions() {
        let context = GitContext {
            branch_name: "feature/test".to_string(),
            base_branch: "main".to_string(),
            commits: vec![],
            diff_entries: vec![
                DiffEntry {
                    path: PathBuf::from("src/lib.rs"),
                    additions: 10,
                    deletions: 5,
                    status: DiffStatus::Modified,
                },
                DiffEntry {
                    path: PathBuf::from("src/main.rs"),
                    additions: 20,
                    deletions: 3,
                    status: DiffStatus::Modified,
                },
            ],
            merge_base_commit: None,
        };

        assert_eq!(context.total_additions(), 30);
    }

    #[test]
    fn test_git_context_total_deletions() {
        let context = GitContext {
            branch_name: "feature/test".to_string(),
            base_branch: "main".to_string(),
            commits: vec![],
            diff_entries: vec![
                DiffEntry {
                    path: PathBuf::from("src/lib.rs"),
                    additions: 10,
                    deletions: 5,
                    status: DiffStatus::Modified,
                },
                DiffEntry {
                    path: PathBuf::from("src/main.rs"),
                    additions: 20,
                    deletions: 3,
                    status: DiffStatus::Modified,
                },
            ],
            merge_base_commit: None,
        };

        assert_eq!(context.total_deletions(), 8);
    }

    #[test]
    fn test_git_context_empty() {
        let context = GitContext {
            branch_name: "feature/empty".to_string(),
            base_branch: "main".to_string(),
            commits: vec![],
            diff_entries: vec![],
            merge_base_commit: None,
        };

        assert_eq!(context.commit_count(), 0);
        assert_eq!(context.files_changed_count(), 0);
        assert_eq!(context.total_additions(), 0);
        assert_eq!(context.total_deletions(), 0);
    }

    #[test]
    fn test_git_context_clone() {
        let context = GitContext {
            branch_name: "feature/test".to_string(),
            base_branch: "main".to_string(),
            commits: vec![],
            diff_entries: vec![],
            merge_base_commit: Some("abc123".to_string()),
        };

        let cloned = context.clone();
        assert_eq!(cloned.branch_name, context.branch_name);
        assert_eq!(cloned.merge_base_commit, context.merge_base_commit);
    }

    #[test]
    fn test_git_context_debug() {
        let context = GitContext {
            branch_name: "feature/test".to_string(),
            base_branch: "main".to_string(),
            commits: vec![],
            diff_entries: vec![],
            merge_base_commit: None,
        };

        let debug = format!("{:?}", context);
        assert!(debug.contains("GitContext"));
        assert!(debug.contains("feature/test"));
    }

    // ========================================================================
    // gather_git_context tests (integration-style, run in actual git repo)
    // ========================================================================

    #[test]
    fn test_gather_git_context_returns_valid_context() {
        // This test runs in the autom8 repo, so should succeed
        let result = gather_git_context();
        assert!(result.is_ok());

        let context = result.unwrap();
        // Should have a branch name
        assert!(!context.branch_name.is_empty());
        // Should have detected a base branch
        assert!(!context.base_branch.is_empty());
    }

    #[test]
    fn test_gather_git_context_has_base_branch() {
        let result = gather_git_context();
        assert!(result.is_ok());

        let context = result.unwrap();
        // Base branch should be main or master
        assert!(
            context.base_branch == "main" || context.base_branch == "master",
            "Expected 'main' or 'master', got '{}'",
            context.base_branch
        );
    }

    // ========================================================================
    // FollowUpContext struct tests (US-004)
    // ========================================================================

    fn make_git_context() -> GitContext {
        GitContext {
            branch_name: "feature/test".to_string(),
            base_branch: "main".to_string(),
            commits: vec![],
            diff_entries: vec![],
            merge_base_commit: None,
        }
    }

    fn make_spec() -> Spec {
        Spec {
            project: "TestProject".to_string(),
            branch_name: "feature/test".to_string(),
            description: "Test description".to_string(),
            user_stories: vec![UserStory {
                id: "US-001".to_string(),
                title: "Test Story".to_string(),
                description: "Test".to_string(),
                acceptance_criteria: vec![],
                priority: 1,
                passes: false,
                notes: String::new(),
            }],
        }
    }

    fn make_knowledge() -> ProjectKnowledge {
        let mut knowledge = ProjectKnowledge::default();
        knowledge.decisions.push(Decision {
            story_id: "US-001".to_string(),
            topic: "Architecture".to_string(),
            choice: "Use modules".to_string(),
            rationale: "Better organization".to_string(),
        });
        knowledge.patterns.push(Pattern {
            story_id: "US-001".to_string(),
            description: "Use Result for errors".to_string(),
            example_file: None,
        });
        knowledge
    }

    #[test]
    fn test_follow_up_context_git_only() {
        let context = FollowUpContext {
            git: make_git_context(),
            spec: None,
            spec_path: None,
            knowledge: None,
            work_summaries: vec![],
            session_id: None,
        };

        assert!(!context.has_spec());
        assert!(!context.has_knowledge());
        assert!(!context.has_work_summaries());
        assert_eq!(context.richness_level(), 1);
    }

    #[test]
    fn test_follow_up_context_with_spec() {
        let context = FollowUpContext {
            git: make_git_context(),
            spec: Some(make_spec()),
            spec_path: Some(PathBuf::from("/path/to/spec.json")),
            knowledge: None,
            work_summaries: vec![],
            session_id: None,
        };

        assert!(context.has_spec());
        assert!(!context.has_knowledge());
        assert_eq!(context.richness_level(), 2);
    }

    #[test]
    fn test_follow_up_context_full() {
        let context = FollowUpContext {
            git: make_git_context(),
            spec: Some(make_spec()),
            spec_path: Some(PathBuf::from("/path/to/spec.json")),
            knowledge: Some(make_knowledge()),
            work_summaries: vec![
                "Implemented feature A".to_string(),
                "Fixed bug in module B".to_string(),
            ],
            session_id: Some("session-123".to_string()),
        };

        assert!(context.has_spec());
        assert!(context.has_knowledge());
        assert!(context.has_work_summaries());
        assert_eq!(context.work_summary_count(), 2);
        assert_eq!(context.richness_level(), 3);
    }

    #[test]
    fn test_follow_up_context_richness_level_spec_only() {
        // Git + Spec = Level 2
        let context = FollowUpContext {
            git: make_git_context(),
            spec: Some(make_spec()),
            spec_path: None,
            knowledge: None,
            work_summaries: vec![],
            session_id: None,
        };

        assert_eq!(context.richness_level(), 2);
    }

    #[test]
    fn test_follow_up_context_richness_level_knowledge_only() {
        // Git + Knowledge (no spec) = Level 2
        let context = FollowUpContext {
            git: make_git_context(),
            spec: None,
            spec_path: None,
            knowledge: Some(make_knowledge()),
            work_summaries: vec![],
            session_id: None,
        };

        assert_eq!(context.richness_level(), 2);
    }

    #[test]
    fn test_follow_up_context_work_summaries_empty() {
        let context = FollowUpContext {
            git: make_git_context(),
            spec: None,
            spec_path: None,
            knowledge: None,
            work_summaries: vec![],
            session_id: None,
        };

        assert!(!context.has_work_summaries());
        assert_eq!(context.work_summary_count(), 0);
    }

    #[test]
    fn test_follow_up_context_work_summaries_with_entries() {
        let context = FollowUpContext {
            git: make_git_context(),
            spec: None,
            spec_path: None,
            knowledge: None,
            work_summaries: vec![
                "Summary 1".to_string(),
                "Summary 2".to_string(),
                "Summary 3".to_string(),
            ],
            session_id: None,
        };

        assert!(context.has_work_summaries());
        assert_eq!(context.work_summary_count(), 3);
    }

    #[test]
    fn test_follow_up_context_clone() {
        let context = FollowUpContext {
            git: make_git_context(),
            spec: Some(make_spec()),
            spec_path: Some(PathBuf::from("/path/to/spec.json")),
            knowledge: Some(make_knowledge()),
            work_summaries: vec!["Summary".to_string()],
            session_id: Some("session-id".to_string()),
        };

        let cloned = context.clone();
        assert_eq!(cloned.git.branch_name, context.git.branch_name);
        assert_eq!(cloned.spec_path, context.spec_path);
        assert_eq!(cloned.work_summaries.len(), context.work_summaries.len());
        assert_eq!(cloned.session_id, context.session_id);
    }

    #[test]
    fn test_follow_up_context_debug() {
        let context = FollowUpContext {
            git: make_git_context(),
            spec: None,
            spec_path: None,
            knowledge: None,
            work_summaries: vec![],
            session_id: None,
        };

        let debug = format!("{:?}", context);
        assert!(debug.contains("FollowUpContext"));
        assert!(debug.contains("git"));
    }

    // ========================================================================
    // build_improve_prompt tests (US-005)
    // ========================================================================

    use crate::knowledge::{FileChange, StoryChanges};

    fn make_knowledge_with_files() -> ProjectKnowledge {
        let mut knowledge = ProjectKnowledge::default();
        knowledge.decisions.push(Decision {
            story_id: "US-001".to_string(),
            topic: "Architecture".to_string(),
            choice: "Use modules".to_string(),
            rationale: "Better organization".to_string(),
        });
        knowledge.decisions.push(Decision {
            story_id: "US-002".to_string(),
            topic: "Error handling".to_string(),
            choice: "Use thiserror".to_string(),
            rationale: "Clean error types".to_string(),
        });
        knowledge.story_changes.push(StoryChanges {
            story_id: "US-001".to_string(),
            files_created: vec![FileChange {
                path: PathBuf::from("src/new_module.rs"),
                additions: 100,
                deletions: 0,
                purpose: Some("New module".to_string()),
                key_symbols: vec![],
            }],
            files_modified: vec![FileChange {
                path: PathBuf::from("src/lib.rs"),
                additions: 5,
                deletions: 0,
                purpose: None,
                key_symbols: vec![],
            }],
            files_deleted: vec![],
            commit_hash: None,
        });
        knowledge
    }

    fn make_complete_spec() -> Spec {
        Spec {
            project: "TestProject".to_string(),
            branch_name: "feature/test".to_string(),
            description: "Test description".to_string(),
            user_stories: vec![
                UserStory {
                    id: "US-001".to_string(),
                    title: "Test Story".to_string(),
                    description: "Test".to_string(),
                    acceptance_criteria: vec![],
                    priority: 1,
                    passes: true,
                    notes: String::new(),
                },
                UserStory {
                    id: "US-002".to_string(),
                    title: "Test Story 2".to_string(),
                    description: "Test 2".to_string(),
                    acceptance_criteria: vec![],
                    priority: 2,
                    passes: true,
                    notes: String::new(),
                },
            ],
        }
    }

    #[test]
    fn test_build_improve_prompt_git_only() {
        let context = FollowUpContext {
            git: make_git_context(),
            spec: None,
            spec_path: None,
            knowledge: None,
            work_summaries: vec![],
            session_id: None,
        };

        let prompt = build_improve_prompt(&context);

        // Should contain branch name
        assert!(prompt.contains("feature/test"));
        // Should mention git history
        assert!(prompt.contains("git history"));
        // Should end with question
        assert!(prompt.contains("What would you like to work on?"));
        // Should NOT contain spec or knowledge sections
        assert!(!prompt.contains("**Feature:**"));
        assert!(!prompt.contains("**Key decisions:**"));
    }

    #[test]
    fn test_build_improve_prompt_with_spec() {
        let context = FollowUpContext {
            git: make_git_context(),
            spec: Some(make_spec()),
            spec_path: None,
            knowledge: None,
            work_summaries: vec![],
            session_id: None,
        };

        let prompt = build_improve_prompt(&context);

        // Should contain branch and spec info
        assert!(prompt.contains("feature/test"));
        assert!(prompt.contains("**Feature:**"));
        assert!(prompt.contains("TestProject"));
        // Should show story count (0/1 complete)
        assert!(prompt.contains("0/1 stories complete"));
        // Should end with question
        assert!(prompt.contains("What would you like to work on?"));
    }

    #[test]
    fn test_build_improve_prompt_with_complete_spec() {
        let context = FollowUpContext {
            git: make_git_context(),
            spec: Some(make_complete_spec()),
            spec_path: None,
            knowledge: None,
            work_summaries: vec![],
            session_id: None,
        };

        let prompt = build_improve_prompt(&context);

        // Should show "all complete" status
        assert!(prompt.contains("all complete"));
    }

    #[test]
    fn test_build_improve_prompt_with_decisions() {
        let context = FollowUpContext {
            git: make_git_context(),
            spec: None,
            spec_path: None,
            knowledge: Some(make_knowledge_with_files()),
            work_summaries: vec![],
            session_id: None,
        };

        let prompt = build_improve_prompt(&context);

        // Should contain decisions section
        assert!(prompt.contains("**Key decisions:**"));
        // Should contain topic and choice
        assert!(prompt.contains("Architecture: Use modules"));
        assert!(prompt.contains("Error handling: Use thiserror"));
    }

    #[test]
    fn test_build_improve_prompt_with_files() {
        let context = FollowUpContext {
            git: make_git_context(),
            spec: None,
            spec_path: None,
            knowledge: Some(make_knowledge_with_files()),
            work_summaries: vec![],
            session_id: None,
        };

        let prompt = build_improve_prompt(&context);

        // Should contain files section
        assert!(prompt.contains("**Files touched:**"));
        // Should show created and modified separately
        assert!(prompt.contains("Created:"));
        assert!(prompt.contains("src/new_module.rs"));
        assert!(prompt.contains("Modified:"));
        assert!(prompt.contains("src/lib.rs"));
    }

    #[test]
    fn test_build_improve_prompt_with_work_summaries() {
        let context = FollowUpContext {
            git: make_git_context(),
            spec: None,
            spec_path: None,
            knowledge: None,
            work_summaries: vec![
                "Implemented user authentication".to_string(),
                "Fixed login validation bug".to_string(),
            ],
            session_id: None,
        };

        let prompt = build_improve_prompt(&context);

        // Should contain work summaries section
        assert!(prompt.contains("**Work completed:**"));
        assert!(prompt.contains("Implemented user authentication"));
        assert!(prompt.contains("Fixed login validation bug"));
    }

    #[test]
    fn test_build_improve_prompt_full_context() {
        let context = FollowUpContext {
            git: make_git_context(),
            spec: Some(make_spec()),
            spec_path: None,
            knowledge: Some(make_knowledge_with_files()),
            work_summaries: vec!["Completed initial setup".to_string()],
            session_id: Some("session-123".to_string()),
        };

        let prompt = build_improve_prompt(&context);

        // Should mention all layers loaded
        assert!(prompt.contains("spec, session knowledge, and git history"));
        // Should have all sections
        assert!(prompt.contains("**Feature:**"));
        assert!(prompt.contains("**Key decisions:**"));
        assert!(prompt.contains("**Files touched:**"));
        assert!(prompt.contains("**Work completed:**"));
        // Should end with question
        assert!(prompt.ends_with("What would you like to work on?"));
    }

    #[test]
    fn test_build_improve_prompt_limits_decisions() {
        let mut knowledge = ProjectKnowledge::default();
        // Add 7 decisions
        for i in 1..=7 {
            knowledge.decisions.push(Decision {
                story_id: format!("US-{:03}", i),
                topic: format!("Topic {}", i),
                choice: format!("Choice {}", i),
                rationale: "Rationale".to_string(),
            });
        }

        let context = FollowUpContext {
            git: make_git_context(),
            spec: None,
            spec_path: None,
            knowledge: Some(knowledge),
            work_summaries: vec![],
            session_id: None,
        };

        let prompt = build_improve_prompt(&context);

        // Should show first 5 and indicate more
        assert!(prompt.contains("Topic 1: Choice 1"));
        assert!(prompt.contains("Topic 5: Choice 5"));
        assert!(prompt.contains("...and 2 more"));
        // Should NOT show Topic 6 or 7 directly
        assert!(!prompt.contains("Topic 6: Choice 6"));
    }

    #[test]
    fn test_build_improve_prompt_truncates_long_summaries() {
        let long_summary = "A".repeat(150);
        let context = FollowUpContext {
            git: make_git_context(),
            spec: None,
            spec_path: None,
            knowledge: None,
            work_summaries: vec![long_summary],
            session_id: None,
        };

        let prompt = build_improve_prompt(&context);

        // Should contain truncated summary with ellipsis
        assert!(prompt.contains("..."));
        // Should not contain the full 150-char string
        assert!(!prompt.contains(&"A".repeat(150)));
    }

    #[test]
    fn test_build_improve_prompt_limits_work_summaries() {
        let summaries: Vec<String> = (1..=8).map(|i| format!("Summary {}", i)).collect();

        let context = FollowUpContext {
            git: make_git_context(),
            spec: None,
            spec_path: None,
            knowledge: None,
            work_summaries: summaries,
            session_id: None,
        };

        let prompt = build_improve_prompt(&context);

        // Should show first 5 and indicate more
        assert!(prompt.contains("Summary 1"));
        assert!(prompt.contains("Summary 5"));
        assert!(prompt.contains("...and 3 more iterations"));
    }

    #[test]
    fn test_build_improve_prompt_empty_knowledge_no_files_section() {
        let mut knowledge = ProjectKnowledge::default();
        knowledge.decisions.push(Decision {
            story_id: "US-001".to_string(),
            topic: "Test".to_string(),
            choice: "Test".to_string(),
            rationale: "Test".to_string(),
        });
        // No story_changes

        let context = FollowUpContext {
            git: make_git_context(),
            spec: None,
            spec_path: None,
            knowledge: Some(knowledge),
            work_summaries: vec![],
            session_id: None,
        };

        let prompt = build_improve_prompt(&context);

        // Should have decisions but no files section
        assert!(prompt.contains("**Key decisions:**"));
        assert!(!prompt.contains("**Files touched:**"));
    }

    #[test]
    fn test_build_improve_prompt_opening_level_1() {
        let context = FollowUpContext {
            git: make_git_context(),
            spec: None,
            spec_path: None,
            knowledge: None,
            work_summaries: vec![],
            session_id: None,
        };

        let prompt = build_improve_prompt(&context);
        assert!(prompt.contains("I've loaded the git history"));
    }

    #[test]
    fn test_build_improve_prompt_opening_level_2_with_spec() {
        let context = FollowUpContext {
            git: make_git_context(),
            spec: Some(make_spec()),
            spec_path: None,
            knowledge: None,
            work_summaries: vec![],
            session_id: None,
        };

        let prompt = build_improve_prompt(&context);
        assert!(prompt.contains("I've loaded the spec and git history"));
    }

    #[test]
    fn test_build_improve_prompt_opening_level_2_with_knowledge() {
        let context = FollowUpContext {
            git: make_git_context(),
            spec: None,
            spec_path: None,
            knowledge: Some(make_knowledge()),
            work_summaries: vec![],
            session_id: None,
        };

        let prompt = build_improve_prompt(&context);
        assert!(prompt.contains("I've loaded session knowledge and git history"));
    }

    #[test]
    fn test_build_improve_prompt_opening_level_3() {
        let context = FollowUpContext {
            git: make_git_context(),
            spec: Some(make_spec()),
            spec_path: None,
            knowledge: Some(make_knowledge()),
            work_summaries: vec![],
            session_id: None,
        };

        let prompt = build_improve_prompt(&context);
        assert!(prompt.contains("I've loaded the spec, session knowledge, and git history"));
    }

    // ========================================================================
    // load_follow_up_context tests (integration-style)
    // ========================================================================

    #[test]
    fn test_load_follow_up_context_succeeds() {
        // This runs in the autom8 repo, so should succeed
        let result = load_follow_up_context();
        assert!(result.is_ok());

        let context = result.unwrap();
        // Git context should always be present
        assert!(!context.git.branch_name.is_empty());
        // Richness level should be at least 1 (git)
        assert!(context.richness_level() >= 1);
    }

    #[test]
    fn test_load_follow_up_context_git_always_present() {
        let result = load_follow_up_context();
        assert!(result.is_ok());

        let context = result.unwrap();
        // All required git fields should be populated
        assert!(!context.git.branch_name.is_empty());
        assert!(!context.git.base_branch.is_empty());
        // commits and diff_entries may be empty but should not panic
        let _ = context.git.commit_count();
        let _ = context.git.files_changed_count();
    }
}