syncable-cli 0.37.1

A Rust-based CLI that analyzes code repositories and generates Infrastructure as Code configurations
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
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
//! RAG Storage Layer for Tool Outputs
//!
//! Stores full tool outputs to disk for later retrieval by the agent.
//! Implements the storage part of the RAG (Retrieval-Augmented Generation) pattern.
//!
//! ## Session Tracking
//!
//! All stored outputs are tracked in a session registry, so the agent always knows
//! what data is available for retrieval. Every compressed output includes the full
//! list of available refs.

use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::fs;
use std::path::PathBuf;
use std::sync::Mutex;
use std::time::{SystemTime, UNIX_EPOCH};

/// Directory where outputs are stored
const OUTPUT_DIR: &str = "/tmp/syncable-cli/outputs";

/// Maximum age of stored outputs in seconds (1 hour)
const MAX_AGE_SECS: u64 = 3600;

/// Session registry entry - tracks what's available for retrieval
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SessionRef {
    /// Reference ID for retrieval
    pub ref_id: String,
    /// Tool that generated this output
    pub tool: String,
    /// What this output contains (brief description)
    pub contains: String,
    /// Summary counts (e.g., "47 issues: 3 critical, 12 high")
    pub summary: String,
    /// Timestamp when stored
    pub timestamp: u64,
    /// Size in bytes
    pub size_bytes: usize,
}

/// Global session registry - tracks all stored outputs in current session
static SESSION_REGISTRY: Mutex<Vec<SessionRef>> = Mutex::new(Vec::new());

/// Register a new output in the session registry
pub fn register_session_ref(
    ref_id: &str,
    tool: &str,
    contains: &str,
    summary: &str,
    size_bytes: usize,
) {
    if let Ok(mut registry) = SESSION_REGISTRY.lock() {
        // Remove any existing entry for this ref_id (in case of re-runs)
        registry.retain(|r| r.ref_id != ref_id);

        registry.push(SessionRef {
            ref_id: ref_id.to_string(),
            tool: tool.to_string(),
            contains: contains.to_string(),
            summary: summary.to_string(),
            timestamp: SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .map(|d| d.as_secs())
                .unwrap_or(0),
            size_bytes,
        });
    }
}

/// Get all session refs for inclusion in compressed outputs
pub fn get_session_refs() -> Vec<SessionRef> {
    SESSION_REGISTRY
        .lock()
        .map(|r| r.clone())
        .unwrap_or_default()
}

/// Clear old entries from session registry (called periodically)
pub fn cleanup_session_registry() {
    let now = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.as_secs())
        .unwrap_or(0);

    if let Ok(mut registry) = SESSION_REGISTRY.lock() {
        registry.retain(|r| now - r.timestamp < MAX_AGE_SECS);
    }
}

/// Format session refs as a user-friendly string for the agent
pub fn format_session_refs_for_agent() -> String {
    let refs = get_session_refs();

    if refs.is_empty() {
        return String::new();
    }

    let mut output = String::from("\n📦 AVAILABLE DATA FOR RETRIEVAL:\n");
    output.push_str("─────────────────────────────────\n");

    for r in &refs {
        let age = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .map(|d| d.as_secs())
            .unwrap_or(0)
            .saturating_sub(r.timestamp);

        let age_str = if age < 60 {
            format!("{}s ago", age)
        } else {
            format!("{}m ago", age / 60)
        };

        output.push_str(&format!(
            "\n• {} [{}]\n  Contains: {}\n  Summary: {}\n  Retrieve: retrieve_output(\"{}\") or with query\n",
            r.ref_id, age_str, r.contains, r.summary, r.ref_id
        ));
    }

    output.push_str("\n─────────────────────────────────\n");
    output.push_str(
        "Query examples: \"severity:critical\", \"file:deployment.yaml\", \"code:DL3008\"\n",
    );

    output
}

/// Generate a short unique reference ID
fn generate_ref_id() -> String {
    let timestamp = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.as_millis())
        .unwrap_or(0);

    // Use last 8 chars of timestamp + random suffix
    let ts_part = format!("{:x}", timestamp)
        .chars()
        .rev()
        .take(6)
        .collect::<String>();
    let rand_part: String = (0..4)
        .map(|_| {
            let idx = (timestamp as usize + rand_simple()) % 36;
            "abcdefghijklmnopqrstuvwxyz0123456789"
                .chars()
                .nth(idx)
                .unwrap()
        })
        .collect();

    format!("{}_{}", ts_part, rand_part)
}

/// Simple pseudo-random number (no external deps)
fn rand_simple() -> usize {
    let ptr = Box::into_raw(Box::new(0u8));
    let addr = ptr as usize;
    unsafe { drop(Box::from_raw(ptr)) };
    addr.wrapping_mul(1103515245).wrapping_add(12345) % (1 << 31)
}

/// Ensure output directory exists
fn ensure_output_dir() -> std::io::Result<PathBuf> {
    let path = PathBuf::from(OUTPUT_DIR);
    if !path.exists() {
        fs::create_dir_all(&path)?;
    }
    Ok(path)
}

/// Store output to disk and return reference ID
///
/// # Arguments
/// * `output` - The JSON value to store
/// * `tool_name` - Name of the tool (used as prefix in ref_id)
///
/// # Returns
/// Reference ID that can be used to retrieve the output later
pub fn store_output(output: &Value, tool_name: &str) -> String {
    let ref_id = format!("{}_{}", tool_name, generate_ref_id());

    if let Ok(dir) = ensure_output_dir() {
        let path = dir.join(format!("{}.json", ref_id));

        // Store with metadata
        let stored = serde_json::json!({
            "ref_id": ref_id,
            "tool": tool_name,
            "timestamp": SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .map(|d| d.as_secs())
                .unwrap_or(0),
            "data": output
        });

        if let Ok(json_str) = serde_json::to_string(&stored) {
            let _ = fs::write(&path, json_str);
        }
    }

    ref_id
}

/// Retrieve stored output by reference ID
///
/// # Arguments
/// * `ref_id` - The reference ID returned from `store_output`
///
/// # Returns
/// The stored JSON value, or None if not found
pub fn retrieve_output(ref_id: &str) -> Option<Value> {
    let path = PathBuf::from(OUTPUT_DIR).join(format!("{}.json", ref_id));

    if !path.exists() {
        return None;
    }

    let content = fs::read_to_string(&path).ok()?;
    let stored: Value = serde_json::from_str(&content).ok()?;

    // Return just the data portion
    stored.get("data").cloned()
}

/// Retrieve and filter output by query
///
/// # Arguments
/// * `ref_id` - The reference ID
/// * `query` - Optional filter query (e.g., "severity:critical", "file:path", "code:DL3008")
///
/// For analyze_project outputs, supports:
/// - section:summary - Top-level info
/// - section:projects - List projects
/// - section:frameworks - All frameworks
/// - section:languages - All languages
/// - section:services - All services
/// - project:name - Specific project details
/// - service:name - Specific service
/// - language:Go - Language details
/// - framework:* - Framework details
/// - compact:true - Compacted output (default for analyze_project)
///
/// # Returns
/// Filtered JSON value, or None if not found
pub fn retrieve_filtered(
    ref_id: &str,
    query: Option<&str>,
    limit: usize,
    offset: usize,
) -> Option<Value> {
    let data = retrieve_output(ref_id)?;

    // Check if this is an analyze_project output
    if is_analyze_project_output(&data) {
        return retrieve_analyze_project(&data, query);
    }

    let query = match query {
        Some(q) if !q.is_empty() => q,
        _ => return Some(data),
    };

    // Parse query
    let (filter_type, filter_value) = parse_query(query);

    // Find issues/findings array in data
    let issues = find_issues_array(&data).unwrap_or_default();

    // Filter issues
    let filtered: Vec<Value> = issues
        .iter()
        .filter(|issue| matches_filter(issue, &filter_type, &filter_value))
        .cloned()
        .collect();

    let total_matches = filtered.len();

    // Apply pagination
    let page: Vec<Value> = filtered
        .into_iter()
        .skip(offset)
        .take(limit)
        .map(|v| truncate_result_value(v))
        .collect();

    let showing = page.len();
    let has_more = offset + showing < total_matches;

    let mut result = serde_json::json!({
        "query": query,
        "total_matches": total_matches,
        "showing": showing,
        "offset": offset,
        "has_more": has_more,
        "results": page
    });

    if has_more {
        result.as_object_mut().unwrap().insert(
            "next_command".to_string(),
            Value::String(format!(
                "sync-ctl retrieve '{}' --query '{}' --offset {} --limit {}",
                ref_id,
                query,
                offset + limit,
                limit
            )),
        );
    }

    Some(result)
}

/// Truncate large fields in a single result to keep output compact.
fn truncate_result_value(mut value: Value) -> Value {
    if let Some(obj) = value.as_object_mut() {
        // Truncate long description fields
        for field in ["description", "message", "details"] {
            if let Some(s) = obj.get(field).and_then(|v| v.as_str()) {
                if s.len() > 200 {
                    let truncated = format!("{}...", &s[..200]);
                    obj.insert(field.to_string(), Value::String(truncated));
                }
            }
        }

        // Cap references/urls arrays
        if let Some(refs) = obj.get("references").and_then(|v| v.as_array()) {
            if refs.len() > 3 {
                let truncated: Vec<Value> = refs.iter().take(3).cloned().collect();
                let remaining = refs.len() - 3;
                obj.insert("references".to_string(), Value::Array(truncated));
                obj.insert(
                    "references_truncated".to_string(),
                    Value::Number(remaining.into()),
                );
            }
        }
    }
    value
}

/// Parse a query string into type and value
fn parse_query(query: &str) -> (String, String) {
    if let Some(idx) = query.find(':') {
        let (t, v) = query.split_at(idx);
        (t.to_lowercase(), v[1..].to_string())
    } else {
        // Treat as general search term
        ("any".to_string(), query.to_string())
    }
}

/// Find issues/findings array in a JSON value
fn find_issues_array(data: &Value) -> Option<Vec<Value>> {
    let issue_fields = [
        "issues",
        "findings",
        "violations",
        "warnings",
        "errors",
        "recommendations",
        "results",
        "failures",
        "diagnostics",
        "vulnerable_dependencies",
        "dependencies",
    ];

    for field in &issue_fields {
        if let Some(arr) = data.get(field).and_then(|v| v.as_array()) {
            // Flatten vulnerable_dependencies: each dep has inner vulnerabilities[]
            if *field == "vulnerable_dependencies" && !arr.is_empty() {
                let mut flat = Vec::new();
                for dep in arr {
                    let dep_name = dep
                        .get("name")
                        .and_then(|v| v.as_str())
                        .unwrap_or("unknown");
                    let dep_version = dep.get("version").and_then(|v| v.as_str()).unwrap_or("?");
                    let source_dir = dep.get("source_dir").cloned();
                    let language = dep.get("language").cloned();
                    if let Some(vulns) = dep.get("vulnerabilities").and_then(|v| v.as_array()) {
                        for vuln in vulns {
                            let mut entry = vuln.clone();
                            if let Some(obj) = entry.as_object_mut() {
                                obj.insert(
                                    "package".to_string(),
                                    Value::String(dep_name.to_string()),
                                );
                                obj.insert(
                                    "package_version".to_string(),
                                    Value::String(dep_version.to_string()),
                                );
                                if let Some(sd) = &source_dir {
                                    obj.insert("source_dir".to_string(), sd.clone());
                                }
                                if let Some(lang) = &language {
                                    obj.insert("language".to_string(), lang.clone());
                                }
                            }
                            flat.push(entry);
                        }
                    }
                }
                return Some(flat);
            }
            return Some(arr.clone());
        }
    }

    // Check if data itself is an array
    if let Some(arr) = data.as_array() {
        return Some(arr.clone());
    }

    None
}

/// Check if an issue matches a filter
fn matches_filter(issue: &Value, filter_type: &str, filter_value: &str) -> bool {
    match filter_type {
        "severity" | "level" => {
            let sev = issue
                .get("severity")
                .or_else(|| issue.get("level"))
                .and_then(|v| v.as_str())
                .unwrap_or("");
            sev.to_lowercase().contains(&filter_value.to_lowercase())
        }
        "file" | "path" => {
            let file = issue
                .get("file")
                .or_else(|| issue.get("path"))
                .or_else(|| issue.get("filename"))
                .and_then(|v| v.as_str())
                .unwrap_or("");
            file.to_lowercase().contains(&filter_value.to_lowercase())
        }
        "code" | "rule" => {
            let code = issue
                .get("code")
                .or_else(|| issue.get("rule"))
                .or_else(|| issue.get("rule_id"))
                .and_then(|v| v.as_str())
                .unwrap_or("");
            code.to_lowercase().contains(&filter_value.to_lowercase())
        }
        "container" | "resource" => {
            let container = issue
                .get("container")
                .or_else(|| issue.get("resource"))
                .or_else(|| issue.get("name"))
                .and_then(|v| v.as_str())
                .unwrap_or("");
            container
                .to_lowercase()
                .contains(&filter_value.to_lowercase())
        }
        _ => {
            // Search in all string values
            let issue_str = serde_json::to_string(issue).unwrap_or_default();
            issue_str
                .to_lowercase()
                .contains(&filter_value.to_lowercase())
        }
    }
}

// ============================================================================
// Smart Retrieval for different output types
// ============================================================================

/// Output type detection for smart retrieval
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OutputType {
    /// MonorepoAnalysis - has "projects" array and/or "is_monorepo"
    MonorepoAnalysis,
    /// ProjectAnalysis - flat structure with "languages" + "analysis_metadata"
    ProjectAnalysis,
    /// LintResult - has "failures" array (kubelint, hadolint, dclint, helmlint)
    LintResult,
    /// OptimizationResult - has "recommendations" array (k8s_optimize)
    OptimizationResult,
    /// Generic - fallback for unknown structures
    Generic,
}

/// Detect the output type for smart retrieval routing
pub fn detect_output_type(data: &Value) -> OutputType {
    // MonorepoAnalysis: has projects array or is_monorepo flag
    if data.get("projects").is_some() || data.get("is_monorepo").is_some() {
        return OutputType::MonorepoAnalysis;
    }

    // ProjectAnalysis: has languages array + analysis_metadata (flat structure)
    if data.get("languages").is_some() && data.get("analysis_metadata").is_some() {
        return OutputType::ProjectAnalysis;
    }

    // LintResult: has failures array
    if data.get("failures").is_some() {
        return OutputType::LintResult;
    }

    // OptimizationResult: has recommendations array
    if data.get("recommendations").is_some() {
        return OutputType::OptimizationResult;
    }

    OutputType::Generic
}

/// Check if data is an analyze_project output (either type)
fn is_analyze_project_output(data: &Value) -> bool {
    matches!(
        detect_output_type(data),
        OutputType::MonorepoAnalysis | OutputType::ProjectAnalysis
    )
}

/// Smart retrieval for analyze_project outputs
/// Supports queries like:
/// - section:summary - Top-level info without nested data
/// - section:projects - List project names and categories
/// - project:name - Get specific project details (compacted)
/// - service:name - Get specific service details
/// - language:Go - Get language details for a specific language
/// - framework:* - List all detected frameworks
/// - compact:true - Strip file arrays, return counts
pub fn retrieve_analyze_project(data: &Value, query: Option<&str>) -> Option<Value> {
    let query = query.unwrap_or("compact:true");
    let (query_type, query_value) = parse_query(query);

    match query_type.as_str() {
        "section" => match query_value.as_str() {
            "summary" => Some(extract_summary(data)),
            "projects" => Some(extract_projects_list(data)),
            "frameworks" => Some(extract_all_frameworks(data)),
            "languages" => Some(extract_all_languages(data)),
            "services" => Some(extract_all_services(data)),
            _ => Some(compact_analyze_output(data)),
        },
        "project" => extract_project_by_name(data, &query_value),
        "service" => extract_service_by_name(data, &query_value),
        "language" => extract_language_details(data, &query_value),
        "framework" => extract_framework_details(data, &query_value),
        "compact" => Some(compact_analyze_output(data)),
        _ => {
            // Default: return compacted output
            Some(compact_analyze_output(data))
        }
    }
}

/// Extract top-level summary without nested data
fn extract_summary(data: &Value) -> Value {
    let mut summary = serde_json::Map::new();

    // Handle MonorepoAnalysis structure
    if let Some(root) = data.get("root_path").and_then(|v| v.as_str()) {
        summary.insert("root_path".to_string(), Value::String(root.to_string()));
    }
    if let Some(mono) = data.get("is_monorepo").and_then(|v| v.as_bool()) {
        summary.insert("is_monorepo".to_string(), Value::Bool(mono));
    }

    // Handle ProjectAnalysis structure (flat)
    if let Some(root) = data.get("project_root").and_then(|v| v.as_str()) {
        summary.insert("project_root".to_string(), Value::String(root.to_string()));
    }
    if let Some(arch) = data.get("architecture_type").and_then(|v| v.as_str()) {
        summary.insert(
            "architecture_type".to_string(),
            Value::String(arch.to_string()),
        );
    }

    // Count projects (MonorepoAnalysis)
    if let Some(projects) = data.get("projects").and_then(|v| v.as_array()) {
        summary.insert(
            "project_count".to_string(),
            Value::Number(projects.len().into()),
        );

        // Extract project names
        let names: Vec<Value> = projects
            .iter()
            .filter_map(|p| p.get("name").and_then(|n| n.as_str()))
            .map(|n| Value::String(n.to_string()))
            .collect();
        summary.insert("project_names".to_string(), Value::Array(names));
    }

    // Extract languages (ProjectAnalysis flat structure)
    if let Some(languages) = data.get("languages").and_then(|v| v.as_array()) {
        let names: Vec<Value> = languages
            .iter()
            .filter_map(|l| l.get("name").and_then(|n| n.as_str()))
            .map(|n| Value::String(n.to_string()))
            .collect();
        summary.insert("languages".to_string(), Value::Array(names));
    }

    // Extract technologies (ProjectAnalysis flat structure)
    if let Some(techs) = data.get("technologies").and_then(|v| v.as_array()) {
        let names: Vec<Value> = techs
            .iter()
            .filter_map(|t| t.get("name").and_then(|n| n.as_str()))
            .map(|n| Value::String(n.to_string()))
            .collect();
        summary.insert("technologies".to_string(), Value::Array(names));
    }

    // Extract services (ProjectAnalysis flat structure) - include names, not just count
    if let Some(services) = data.get("services").and_then(|v| v.as_array()) {
        summary.insert(
            "services_count".to_string(),
            Value::Number(services.len().into()),
        );
        // Include service names so agent knows what microservices exist
        let service_names: Vec<Value> = services
            .iter()
            .filter_map(|s| s.get("name").and_then(|n| n.as_str()))
            .map(|n| Value::String(n.to_string()))
            .collect();
        if !service_names.is_empty() {
            summary.insert("services".to_string(), Value::Array(service_names));
        }
    }

    Value::Object(summary)
}

/// Extract list of projects with basic info (no file arrays)
fn extract_projects_list(data: &Value) -> Value {
    let projects = data.get("projects").and_then(|v| v.as_array());

    let list: Vec<Value> = projects
        .map(|arr| {
            arr.iter()
                .map(|p| {
                    let mut proj = serde_json::Map::new();
                    if let Some(name) = p.get("name") {
                        proj.insert("name".to_string(), name.clone());
                    }
                    if let Some(path) = p.get("path") {
                        proj.insert("path".to_string(), path.clone());
                    }
                    if let Some(cat) = p.get("project_category") {
                        proj.insert("category".to_string(), cat.clone());
                    }
                    // Add language/framework counts
                    if let Some(analysis) = p.get("analysis") {
                        if let Some(langs) = analysis.get("languages").and_then(|v| v.as_array()) {
                            let lang_names: Vec<Value> = langs
                                .iter()
                                .filter_map(|l| l.get("name").and_then(|n| n.as_str()))
                                .map(|n| Value::String(n.to_string()))
                                .collect();
                            proj.insert("languages".to_string(), Value::Array(lang_names));
                        }
                        if let Some(fws) = analysis.get("frameworks").and_then(|v| v.as_array()) {
                            let fw_names: Vec<Value> = fws
                                .iter()
                                .filter_map(|f| f.get("name").and_then(|n| n.as_str()))
                                .map(|n| Value::String(n.to_string()))
                                .collect();
                            proj.insert("frameworks".to_string(), Value::Array(fw_names));
                        }
                    }
                    Value::Object(proj)
                })
                .collect()
        })
        .unwrap_or_default();

    serde_json::json!({
        "total_projects": list.len(),
        "projects": list
    })
}

/// Extract specific project by name
fn extract_project_by_name(data: &Value, name: &str) -> Option<Value> {
    let projects = data.get("projects").and_then(|v| v.as_array())?;

    let project = projects.iter().find(|p| {
        p.get("name")
            .and_then(|n| n.as_str())
            .map(|n| n.to_lowercase().contains(&name.to_lowercase()))
            .unwrap_or(false)
    })?;

    Some(compact_project(project))
}

/// Extract specific service by name
fn extract_service_by_name(data: &Value, name: &str) -> Option<Value> {
    let projects = data.get("projects").and_then(|v| v.as_array())?;

    for project in projects {
        if let Some(services) = project
            .get("analysis")
            .and_then(|a| a.get("services"))
            .and_then(|s| s.as_array())
            && let Some(service) = services.iter().find(|s| {
                s.get("name")
                    .and_then(|n| n.as_str())
                    .map(|n| n.to_lowercase().contains(&name.to_lowercase()))
                    .unwrap_or(false)
            })
        {
            return Some(service.clone());
        }
    }
    None
}

/// Extract language detection details (with file count instead of file list)
fn extract_language_details(data: &Value, lang_name: &str) -> Option<Value> {
    let mut results = Vec::new();

    // Helper to process a languages array
    let process_languages = |languages: &[Value], proj_name: &str, results: &mut Vec<Value>| {
        for lang in languages {
            let name = lang.get("name").and_then(|n| n.as_str()).unwrap_or("");
            if lang_name == "*" || name.to_lowercase().contains(&lang_name.to_lowercase()) {
                let mut compact_lang = serde_json::Map::new();
                if !proj_name.is_empty() {
                    compact_lang
                        .insert("project".to_string(), Value::String(proj_name.to_string()));
                }
                compact_lang.insert(
                    "name".to_string(),
                    lang.get("name").cloned().unwrap_or(Value::Null),
                );
                compact_lang.insert(
                    "version".to_string(),
                    lang.get("version").cloned().unwrap_or(Value::Null),
                );
                compact_lang.insert(
                    "confidence".to_string(),
                    lang.get("confidence").cloned().unwrap_or(Value::Null),
                );

                // Replace file array with count
                if let Some(files) = lang.get("files").and_then(|f| f.as_array()) {
                    compact_lang
                        .insert("file_count".to_string(), Value::Number(files.len().into()));
                }

                results.push(Value::Object(compact_lang));
            }
        }
    };

    // Handle ProjectAnalysis flat structure (languages at top level)
    if let Some(languages) = data.get("languages").and_then(|v| v.as_array()) {
        process_languages(languages, "", &mut results);
    }

    // Handle MonorepoAnalysis structure (languages nested in projects)
    if let Some(projects) = data.get("projects").and_then(|v| v.as_array()) {
        for project in projects {
            let proj_name = project
                .get("name")
                .and_then(|n| n.as_str())
                .unwrap_or("unknown");

            if let Some(languages) = project
                .get("analysis")
                .and_then(|a| a.get("languages"))
                .and_then(|l| l.as_array())
            {
                process_languages(languages, proj_name, &mut results);
            }
        }
    }

    Some(serde_json::json!({
        "query": format!("language:{}", lang_name),
        "total_matches": results.len(),
        "results": results
    }))
}

/// Extract framework/technology details
fn extract_framework_details(data: &Value, fw_name: &str) -> Option<Value> {
    let mut results = Vec::new();

    // Helper to process a frameworks/technologies array
    let process_techs = |techs: &[Value], proj_name: &str, results: &mut Vec<Value>| {
        for tech in techs {
            let name = tech.get("name").and_then(|n| n.as_str()).unwrap_or("");
            if fw_name == "*" || name.to_lowercase().contains(&fw_name.to_lowercase()) {
                let mut compact_fw = serde_json::Map::new();
                if !proj_name.is_empty() {
                    compact_fw.insert("project".to_string(), Value::String(proj_name.to_string()));
                }
                if let Some(v) = tech.get("name") {
                    compact_fw.insert("name".to_string(), v.clone());
                }
                if let Some(v) = tech.get("version") {
                    compact_fw.insert("version".to_string(), v.clone());
                }
                if let Some(v) = tech.get("category") {
                    compact_fw.insert("category".to_string(), v.clone());
                }
                results.push(Value::Object(compact_fw));
            }
        }
    };

    // Handle ProjectAnalysis flat structure (technologies at top level)
    if let Some(techs) = data.get("technologies").and_then(|v| v.as_array()) {
        process_techs(techs, "", &mut results);
    }

    // Also check frameworks field (deprecated but may exist)
    if let Some(fws) = data.get("frameworks").and_then(|v| v.as_array()) {
        process_techs(fws, "", &mut results);
    }

    // Handle MonorepoAnalysis structure (frameworks nested in projects)
    if let Some(projects) = data.get("projects").and_then(|v| v.as_array()) {
        for project in projects {
            let proj_name = project
                .get("name")
                .and_then(|n| n.as_str())
                .unwrap_or("unknown");

            if let Some(frameworks) = project
                .get("analysis")
                .and_then(|a| a.get("frameworks"))
                .and_then(|f| f.as_array())
            {
                process_techs(frameworks, proj_name, &mut results);
            }
        }
    }

    Some(serde_json::json!({
        "query": format!("framework:{}", fw_name),
        "total_matches": results.len(),
        "results": results
    }))
}

/// Extract all frameworks across all projects
fn extract_all_frameworks(data: &Value) -> Value {
    extract_framework_details(data, "*").unwrap_or(serde_json::json!({"results": []}))
}

/// Extract all languages across all projects
fn extract_all_languages(data: &Value) -> Value {
    extract_language_details(data, "*").unwrap_or(serde_json::json!({"results": []}))
}

/// Extract all services across all projects
/// In a monorepo, projects ARE services - so we return projects data
fn extract_all_services(data: &Value) -> Value {
    // In monorepos, projects = services. Return projects list as services.
    // This is because the `services` field in ProjectAnalysis was never implemented.
    extract_projects_list(data)
}

/// Compact entire analyze_project output (strip file arrays)
fn compact_analyze_output(data: &Value) -> Value {
    let mut result = serde_json::Map::new();

    // Handle MonorepoAnalysis structure
    if let Some(v) = data.get("root_path") {
        result.insert("root_path".to_string(), v.clone());
    }
    if let Some(v) = data.get("is_monorepo") {
        result.insert("is_monorepo".to_string(), v.clone());
    }

    // Compact projects (MonorepoAnalysis)
    if let Some(projects) = data.get("projects").and_then(|v| v.as_array()) {
        let compacted: Vec<Value> = projects.iter().map(compact_project).collect();
        result.insert("projects".to_string(), Value::Array(compacted));
        return Value::Object(result);
    }

    // Handle ProjectAnalysis flat structure
    if let Some(v) = data.get("project_root") {
        result.insert("project_root".to_string(), v.clone());
    }
    if let Some(v) = data.get("architecture_type") {
        result.insert("architecture_type".to_string(), v.clone());
    }
    if let Some(v) = data.get("project_type") {
        result.insert("project_type".to_string(), v.clone());
    }

    // Compact languages (replace files array with count)
    if let Some(languages) = data.get("languages").and_then(|v| v.as_array()) {
        let compacted: Vec<Value> = languages
            .iter()
            .map(|lang| {
                let mut compact_lang = serde_json::Map::new();
                for key in &["name", "version", "confidence"] {
                    if let Some(v) = lang.get(*key) {
                        compact_lang.insert(key.to_string(), v.clone());
                    }
                }
                // Replace files array with count
                if let Some(files) = lang.get("files").and_then(|f| f.as_array()) {
                    compact_lang
                        .insert("file_count".to_string(), Value::Number(files.len().into()));
                }
                Value::Object(compact_lang)
            })
            .collect();
        result.insert("languages".to_string(), Value::Array(compacted));
    }

    // Include technologies (usually not huge)
    if let Some(techs) = data.get("technologies").and_then(|v| v.as_array()) {
        let compacted: Vec<Value> = techs
            .iter()
            .map(|tech| {
                let mut compact_tech = serde_json::Map::new();
                for key in &["name", "version", "category", "confidence"] {
                    if let Some(v) = tech.get(*key) {
                        compact_tech.insert(key.to_string(), v.clone());
                    }
                }
                Value::Object(compact_tech)
            })
            .collect();
        result.insert("technologies".to_string(), Value::Array(compacted));
    }

    // Include services (usually small)
    if let Some(services) = data.get("services").and_then(|v| v.as_array()) {
        result.insert("services".to_string(), Value::Array(services.clone()));
    }

    // Include analysis_metadata
    if let Some(meta) = data.get("analysis_metadata") {
        result.insert("analysis_metadata".to_string(), meta.clone());
    }

    Value::Object(result)
}

/// Compact a single project (strip file arrays, replace with counts)
fn compact_project(project: &Value) -> Value {
    let mut compact = serde_json::Map::new();

    // Copy basic fields
    for key in &["name", "path", "project_category"] {
        if let Some(v) = project.get(*key) {
            compact.insert(key.to_string(), v.clone());
        }
    }

    // Compact analysis
    if let Some(analysis) = project.get("analysis") {
        let mut compact_analysis = serde_json::Map::new();

        // Copy project_root
        if let Some(v) = analysis.get("project_root") {
            compact_analysis.insert("project_root".to_string(), v.clone());
        }

        // Compact languages (strip files, add file_count)
        if let Some(languages) = analysis.get("languages").and_then(|v| v.as_array()) {
            let compacted: Vec<Value> = languages
                .iter()
                .map(|lang| {
                    let mut compact_lang = serde_json::Map::new();
                    for key in &["name", "version", "confidence"] {
                        if let Some(v) = lang.get(*key) {
                            compact_lang.insert(key.to_string(), v.clone());
                        }
                    }
                    // Replace files array with count
                    if let Some(files) = lang.get("files").and_then(|f| f.as_array()) {
                        compact_lang
                            .insert("file_count".to_string(), Value::Number(files.len().into()));
                    }
                    Value::Object(compact_lang)
                })
                .collect();
            compact_analysis.insert("languages".to_string(), Value::Array(compacted));
        }

        // Copy frameworks, databases, services as-is (usually not huge)
        for key in &[
            "frameworks",
            "databases",
            "services",
            "build_tools",
            "package_managers",
        ] {
            if let Some(v) = analysis.get(*key) {
                compact_analysis.insert(key.to_string(), v.clone());
            }
        }

        compact.insert("analysis".to_string(), Value::Object(compact_analysis));
    }

    Value::Object(compact)
}

/// List all stored outputs
pub fn list_outputs() -> Vec<OutputInfo> {
    let dir = match ensure_output_dir() {
        Ok(d) => d,
        Err(_) => return Vec::new(),
    };

    let mut outputs = Vec::new();

    if let Ok(entries) = fs::read_dir(&dir) {
        for entry in entries.flatten() {
            if let Some(filename) = entry.file_name().to_str()
                && filename.ends_with(".json")
            {
                let ref_id = filename.trim_end_matches(".json").to_string();

                // Read metadata
                if let Ok(content) = fs::read_to_string(entry.path())
                    && let Ok(stored) = serde_json::from_str::<Value>(&content)
                {
                    let tool = stored
                        .get("tool")
                        .and_then(|v| v.as_str())
                        .unwrap_or("unknown")
                        .to_string();
                    let timestamp = stored
                        .get("timestamp")
                        .and_then(|v| v.as_u64())
                        .unwrap_or(0);
                    let size = content.len();

                    outputs.push(OutputInfo {
                        ref_id,
                        tool,
                        timestamp,
                        size_bytes: size,
                    });
                }
            }
        }
    }

    // Sort by timestamp (newest first)
    outputs.sort_by(|a, b| b.timestamp.cmp(&a.timestamp));
    outputs
}

/// Resolve "latest" to the most recent ref_id by scanning disk files.
/// Works across separate CLI invocations (no in-memory state dependency).
pub fn resolve_latest() -> Option<String> {
    let output_dir = std::path::Path::new("/tmp/syncable-cli/outputs");
    if !output_dir.exists() {
        return None;
    }

    let mut newest: Option<(u64, String)> = None;

    if let Ok(entries) = std::fs::read_dir(output_dir) {
        for entry in entries.flatten() {
            let path = entry.path();
            if path.extension().map_or(true, |e| e != "json") {
                continue;
            }

            if let Ok(contents) = std::fs::read_to_string(&path) {
                if let Ok(data) = serde_json::from_str::<Value>(&contents) {
                    if let Some(ts) = data.get("timestamp").and_then(|v| v.as_u64()) {
                        if let Some(ref_id) = data.get("ref_id").and_then(|v| v.as_str()) {
                            match &newest {
                                Some((best_ts, _)) if ts > *best_ts => {
                                    newest = Some((ts, ref_id.to_string()));
                                }
                                None => {
                                    newest = Some((ts, ref_id.to_string()));
                                }
                                _ => {}
                            }
                        }
                    }
                }
            }
        }
    }

    newest.map(|(_, ref_id)| ref_id)
}

/// Information about a stored output
#[derive(Debug, Clone)]
pub struct OutputInfo {
    pub ref_id: String,
    pub tool: String,
    pub timestamp: u64,
    pub size_bytes: usize,
}

/// Clean up old stored outputs
pub fn cleanup_old_outputs() {
    let dir = match ensure_output_dir() {
        Ok(d) => d,
        Err(_) => return,
    };

    let now = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.as_secs())
        .unwrap_or(0);

    if let Ok(entries) = fs::read_dir(&dir) {
        for entry in entries.flatten() {
            if let Ok(content) = fs::read_to_string(entry.path())
                && let Ok(stored) = serde_json::from_str::<Value>(&content)
            {
                let timestamp = stored
                    .get("timestamp")
                    .and_then(|v| v.as_u64())
                    .unwrap_or(0);

                if now - timestamp > MAX_AGE_SECS {
                    let _ = fs::remove_file(entry.path());
                }
            }
        }
    }
}

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

    #[test]
    fn test_store_and_retrieve() {
        let data = serde_json::json!({
            "issues": [
                { "code": "test1", "severity": "high", "file": "test.yaml" }
            ]
        });

        let ref_id = store_output(&data, "test_tool");
        assert!(ref_id.starts_with("test_tool_"));

        let retrieved = retrieve_output(&ref_id);
        assert!(retrieved.is_some());
        assert_eq!(retrieved.unwrap(), data);
    }

    #[test]
    fn test_filtered_retrieval() {
        let data = serde_json::json!({
            "issues": [
                { "code": "DL3008", "severity": "warning", "file": "Dockerfile1" },
                { "code": "DL3009", "severity": "info", "file": "Dockerfile2" },
                { "code": "DL3008", "severity": "warning", "file": "Dockerfile3" }
            ]
        });

        let ref_id = store_output(&data, "filter_test");

        // Filter by code
        let filtered = retrieve_filtered(&ref_id, Some("code:DL3008"), 100, 0);
        assert!(filtered.is_some());
        let results = filtered.unwrap();
        assert_eq!(results["total_matches"], 2);

        // Filter by severity
        let filtered = retrieve_filtered(&ref_id, Some("severity:info"), 100, 0);
        assert!(filtered.is_some());
        let results = filtered.unwrap();
        assert_eq!(results["total_matches"], 1);
    }

    #[test]
    fn test_parse_query() {
        assert_eq!(
            parse_query("severity:critical"),
            ("severity".to_string(), "critical".to_string())
        );
        assert_eq!(
            parse_query("searchterm"),
            ("any".to_string(), "searchterm".to_string())
        );
    }

    #[test]
    fn test_analyze_project_detection() {
        let analyze_data = serde_json::json!({
            "root_path": "/test",
            "is_monorepo": true,
            "projects": []
        });
        assert!(is_analyze_project_output(&analyze_data));

        let lint_data = serde_json::json!({
            "issues": [{ "code": "DL3008" }]
        });
        assert!(!is_analyze_project_output(&lint_data));
    }

    #[test]
    fn test_analyze_project_summary() {
        let data = serde_json::json!({
            "root_path": "/test/monorepo",
            "is_monorepo": true,
            "projects": [
                { "name": "api-gateway", "path": "services/api" },
                { "name": "web-app", "path": "apps/web" }
            ]
        });

        let summary = extract_summary(&data);
        assert_eq!(summary["root_path"], "/test/monorepo");
        assert_eq!(summary["is_monorepo"], true);
        assert_eq!(summary["project_count"], 2);
    }

    #[test]
    fn test_analyze_project_compact() {
        // Simulates massive analyze_project output with 1000s of files
        let files: Vec<String> = (0..1000).map(|i| format!("/src/file{}.ts", i)).collect();

        let data = serde_json::json!({
            "root_path": "/test",
            "is_monorepo": false,
            "projects": [{
                "name": "test-project",
                "path": "",
                "project_category": "Api",
                "analysis": {
                    "project_root": "/test",
                    "languages": [{
                        "name": "TypeScript",
                        "version": "5.0",
                        "confidence": 0.95,
                        "files": files
                    }],
                    "frameworks": [{
                        "name": "React",
                        "version": "18.0"
                    }]
                }
            }]
        });

        let ref_id = store_output(&data, "analyze_project_test");

        // Default retrieval should return compacted output
        let result = retrieve_filtered(&ref_id, None, 100, 0);
        assert!(result.is_some());

        let compacted = result.unwrap();

        // Verify files array was replaced with file_count
        let project = &compacted["projects"][0];
        let lang = &project["analysis"]["languages"][0];
        assert_eq!(lang["name"], "TypeScript");
        assert_eq!(lang["file_count"], 1000);
        assert!(lang.get("files").is_none()); // No files array

        // The compacted JSON should be much smaller
        let compacted_str = serde_json::to_string(&compacted).unwrap();
        let original_str = serde_json::to_string(&data).unwrap();
        assert!(compacted_str.len() < original_str.len() / 10); // At least 10x smaller
    }

    #[test]
    fn test_analyze_project_section_queries() {
        let data = serde_json::json!({
            "root_path": "/test",
            "is_monorepo": true,
            "projects": [{
                "name": "api-service",
                "path": "services/api",
                "project_category": "Api",
                "analysis": {
                    "languages": [{
                        "name": "Go",
                        "version": "1.21",
                        "confidence": 0.9,
                        "files": ["/main.go", "/handler.go"]
                    }],
                    "frameworks": [{
                        "name": "Gin",
                        "version": "1.9",
                        "category": "Web"
                    }],
                    "services": [{
                        "name": "api-http",
                        "type": "http",
                        "port": 8080
                    }]
                }
            }]
        });

        let ref_id = store_output(&data, "analyze_query_test");

        // Test section:projects
        let projects = retrieve_filtered(&ref_id, Some("section:projects"), 100, 0);
        assert!(projects.is_some());
        assert_eq!(projects.as_ref().unwrap()["total_projects"], 1);

        // Test section:frameworks
        let frameworks = retrieve_filtered(&ref_id, Some("section:frameworks"), 100, 0);
        assert!(frameworks.is_some());
        assert_eq!(frameworks.as_ref().unwrap()["total_matches"], 1);
        assert_eq!(frameworks.as_ref().unwrap()["results"][0]["name"], "Gin");

        // Test section:languages
        let languages = retrieve_filtered(&ref_id, Some("section:languages"), 100, 0);
        assert!(languages.is_some());
        assert_eq!(languages.as_ref().unwrap()["total_matches"], 1);
        assert_eq!(languages.as_ref().unwrap()["results"][0]["name"], "Go");
        // Files should be replaced with count
        assert_eq!(languages.as_ref().unwrap()["results"][0]["file_count"], 2);

        // Test language:Go specific query
        let go = retrieve_filtered(&ref_id, Some("language:Go"), 100, 0);
        assert!(go.is_some());
        assert_eq!(go.as_ref().unwrap()["total_matches"], 1);

        // Test framework:Gin specific query
        let gin = retrieve_filtered(&ref_id, Some("framework:Gin"), 100, 0);
        assert!(gin.is_some());
        assert_eq!(gin.as_ref().unwrap()["total_matches"], 1);
    }

    #[test]
    fn test_find_issues_array_failures_field() {
        let data = serde_json::json!({
            "failures": [
                {"code": "DL3008", "severity": "warning", "message": "Pin versions"},
                {"code": "DL3009", "severity": "info", "message": "Delete apt cache"}
            ]
        });
        let result = find_issues_array(&data);
        assert!(result.is_some());
        assert_eq!(result.unwrap().len(), 2);
    }

    #[test]
    fn test_find_issues_array_diagnostics_field() {
        let data = serde_json::json!({
            "diagnostics": [
                {"code": "DC001", "severity": "error", "message": "Invalid compose version"}
            ]
        });
        let result = find_issues_array(&data);
        assert!(result.is_some());
        assert_eq!(result.unwrap().len(), 1);
    }

    #[test]
    fn test_resolve_latest_returns_most_recent() {
        use std::fs;
        use std::path::Path;

        let output_dir = Path::new("/tmp/syncable-cli/outputs");
        fs::create_dir_all(output_dir).unwrap();

        // Clean up any existing test files
        let _ = fs::remove_file(output_dir.join("test_old_aaa111.json"));
        let _ = fs::remove_file(output_dir.join("test_new_bbb222.json"));

        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_secs();

        // Write two files with different timestamps.
        // Use a far-future timestamp for the "new" file so it's always
        // the most recent, even when other tests run concurrently and
        // call store_output() with the current time.
        let old_data = serde_json::json!({
            "ref_id": "test_old_aaa111",
            "tool": "test_old",
            "timestamp": now - 60,
            "data": {}
        });
        let new_data = serde_json::json!({
            "ref_id": "test_new_bbb222",
            "tool": "test_new",
            "timestamp": now + 9_999_999,
            "data": {}
        });

        fs::write(
            output_dir.join("test_old_aaa111.json"),
            serde_json::to_string(&old_data).unwrap(),
        )
        .unwrap();
        fs::write(
            output_dir.join("test_new_bbb222.json"),
            serde_json::to_string(&new_data).unwrap(),
        )
        .unwrap();

        let latest = resolve_latest();
        assert!(latest.is_some());
        assert_eq!(latest.unwrap(), "test_new_bbb222");

        // Cleanup
        let _ = fs::remove_file(output_dir.join("test_old_aaa111.json"));
        let _ = fs::remove_file(output_dir.join("test_new_bbb222.json"));
    }
}