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
//! Smart Context Compression for Tool Outputs
//!
//! Implements multi-layer semantic compression with RAG retrieval pattern:
//! 1. Semantic Deduplication - Group identical patterns
//! 2. Importance-Weighted Output - Critical=full, Low=counts
//! 3. Hierarchical Summaries - Multi-level detail
//! 4. RAG Pattern - Store full data, return summary with retrieval reference

use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
use std::collections::HashMap;

use super::output_store;

/// Severity levels for importance-weighted filtering
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Severity {
    Info,
    Low,
    Medium,
    High,
    Critical,
}

impl Severity {
    pub fn from_str(s: &str) -> Self {
        match s.to_lowercase().as_str() {
            "critical" | "error" => Severity::Critical,
            "high" | "warning" => Severity::High,
            "medium" => Severity::Medium,
            "low" | "hint" => Severity::Low,
            _ => Severity::Info,
        }
    }
}

/// A deduplicated pattern representing multiple similar issues
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DeduplicatedPattern {
    /// The issue code/type (e.g., "no-resource-limits", "DL3008")
    pub code: String,
    /// Number of occurrences
    pub count: usize,
    /// Severity level
    pub severity: Severity,
    /// Brief description of the issue
    pub message: String,
    /// List of affected files (truncated if too many)
    pub affected_files: Vec<String>,
    /// One full example for context
    pub example: Option<Value>,
    /// Suggested fix template
    pub fix_template: Option<String>,
}

/// Compressed output ready for LLM context
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CompressedOutput {
    /// Tool that generated this output
    pub tool: String,
    /// Overall status
    pub status: String,
    /// Summary counts by severity
    pub summary: SeveritySummary,
    /// Critical issues - always shown in full
    pub critical_issues: Vec<Value>,
    /// High severity issues - shown in full if few, otherwise patterns
    pub high_issues: Vec<Value>,
    /// Deduplicated patterns for medium/low issues
    pub patterns: Vec<DeduplicatedPattern>,
    /// Reference ID for retrieving full data
    pub full_data_ref: String,
    /// Hint for agent on how to retrieve more details
    pub retrieval_hint: String,
}

/// Summary counts by severity level
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct SeveritySummary {
    pub total: usize,
    pub critical: usize,
    pub high: usize,
    pub medium: usize,
    pub low: usize,
    pub info: usize,
}

/// Configuration for compression behavior
#[derive(Debug, Clone)]
pub struct CompressionConfig {
    /// Maximum high-severity issues to show in full (default: 10)
    pub max_high_full: usize,
    /// Maximum files to list per pattern (default: 5)
    pub max_files_per_pattern: usize,
    /// Target output size in bytes (default: 15KB)
    pub target_size_bytes: usize,
}

impl Default for CompressionConfig {
    fn default() -> Self {
        Self {
            max_high_full: 10,
            max_files_per_pattern: 5,
            target_size_bytes: 15_000,
        }
    }
}

/// Main compression function - compresses tool output and stores full data for retrieval
///
/// # Arguments
/// * `output` - The raw JSON output from a tool
/// * `tool_name` - Name of the tool (e.g., "kubelint", "k8s_optimize")
/// * `config` - Compression configuration
///
/// # Returns
/// JSON string of compressed output, or original if compression not applicable
pub fn compress_tool_output(output: &Value, tool_name: &str, config: &CompressionConfig) -> String {
    // Check if output is small enough - no compression needed
    let raw_str = serde_json::to_string(output).unwrap_or_default();
    if raw_str.len() <= config.target_size_bytes {
        return raw_str;
    }

    // Store full output for later retrieval
    let ref_id = output_store::store_output(output, tool_name);

    // Extract issues/findings array from the output
    let issues = extract_issues(output);

    if issues.is_empty() {
        // Register in session with description
        let contains = format!("{} analysis data (no issues)", tool_name);
        output_store::register_session_ref(
            &ref_id,
            tool_name,
            &contains,
            "0 issues",
            raw_str.len(),
        );

        // No issues to compress, just store and return summary
        let mut result = serde_json::to_string_pretty(&json!({
            "tool": tool_name,
            "status": "NO_ISSUES",
            "summary": { "total": 0 },
            "full_data_ref": ref_id,
            "retrieval_hint": format!("Use retrieve_output('{}') for full analysis data", ref_id)
        }))
        .unwrap_or(raw_str.clone());

        // Append ALL session refs so agent always knows what's available
        result.push_str(&output_store::format_session_refs_for_agent());
        return result;
    }

    // Classify issues by severity
    let (critical, high, medium, low, info) = classify_by_severity(&issues);

    // Build summary
    let summary = SeveritySummary {
        total: issues.len(),
        critical: critical.len(),
        high: high.len(),
        medium: medium.len(),
        low: low.len(),
        info: info.len(),
    };

    // Critical issues: always full detail
    let critical_issues: Vec<Value> = critical.clone();

    // High issues: full detail if few, otherwise deduplicate
    let high_issues: Vec<Value> = if high.len() <= config.max_high_full {
        high.clone()
    } else {
        // Show first few + pattern for rest
        high.iter().take(config.max_high_full).cloned().collect()
    };

    // Deduplicate medium/low/info issues into patterns
    let mut all_lower: Vec<Value> = Vec::new();
    all_lower.extend(medium.clone());
    all_lower.extend(low.clone());
    all_lower.extend(info.clone());

    // Also add remaining high issues if there were too many
    if high.len() > config.max_high_full {
        all_lower.extend(high.iter().skip(config.max_high_full).cloned());
    }

    let patterns = deduplicate_to_patterns(&all_lower, config);

    // Determine status
    let status = if summary.critical > 0 {
        "CRITICAL_ISSUES_FOUND"
    } else if summary.high > 0 {
        "HIGH_ISSUES_FOUND"
    } else if summary.total > 0 {
        "ISSUES_FOUND"
    } else {
        "CLEAN"
    };

    // Register in session registry with meaningful description
    let contains = match tool_name {
        "kubelint" => "Kubernetes manifest lint issues (security, best practices)",
        "k8s_optimize" => "K8s resource optimization recommendations",
        "analyze" => "Project analysis (languages, frameworks, dependencies)",
        _ => "Tool analysis results",
    };
    let summary_str = format!(
        "{} issues: {} critical, {} high, {} medium",
        summary.total, summary.critical, summary.high, summary.medium
    );
    output_store::register_session_ref(&ref_id, tool_name, contains, &summary_str, raw_str.len());

    let compressed = CompressedOutput {
        tool: tool_name.to_string(),
        status: status.to_string(),
        summary,
        critical_issues,
        high_issues,
        patterns,
        full_data_ref: ref_id.clone(),
        retrieval_hint: format!(
            "Use retrieve_output('{}', query) to get full details. Query options: 'severity:critical', 'file:path', 'code:DL3008'",
            ref_id
        ),
    };

    let mut result = serde_json::to_string_pretty(&compressed).unwrap_or(raw_str);

    // Append ALL session refs so agent always knows what's available
    result.push_str(&output_store::format_session_refs_for_agent());
    result
}

/// Extract issues/findings array from various output formats
fn extract_issues(output: &Value) -> Vec<Value> {
    // Try common field names for issues/findings
    let issue_fields = [
        "issues",
        "findings",
        "violations",
        "warnings",
        "errors",
        "recommendations",
        "results",
        "diagnostics",
        "failures", // LintResult from kubelint, hadolint, dclint, helmlint
        "vulnerable_dependencies",
    ];

    for field in &issue_fields {
        if let Some(arr) = output.get(field).and_then(|v| v.as_array()) {
            // For vulnerable_dependencies, flatten inner vulnerabilities
            // so each vuln has a severity field the compressor can classify
            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 language = dep
                        .get("language")
                        .cloned()
                        .unwrap_or(serde_json::Value::Null);
                    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(), serde_json::json!(dep_name));
                                obj.insert(
                                    "package_version".to_string(),
                                    serde_json::json!(dep_version),
                                );
                                obj.insert("language".to_string(), language.clone());
                            }
                            flat.push(entry);
                        }
                    }
                }
                return flat;
            }
            return arr.clone();
        }
    }

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

    // Try nested structures
    if let Some(obj) = output.as_object() {
        for (_, v) in obj {
            if let Some(arr) = v.as_array()
                && !arr.is_empty()
                && is_issue_like(&arr[0])
            {
                return arr.clone();
            }
        }
    }

    Vec::new()
}

/// Check if a value looks like an issue/finding
fn is_issue_like(value: &Value) -> bool {
    if let Some(obj) = value.as_object() {
        // Issues typically have severity, code, message, or file fields
        obj.contains_key("severity")
            || obj.contains_key("code")
            || obj.contains_key("message")
            || obj.contains_key("rule")
            || obj.contains_key("level")
    } else {
        false
    }
}

/// Classify issues by severity level
fn classify_by_severity(
    issues: &[Value],
) -> (Vec<Value>, Vec<Value>, Vec<Value>, Vec<Value>, Vec<Value>) {
    let mut critical = Vec::new();
    let mut high = Vec::new();
    let mut medium = Vec::new();
    let mut low = Vec::new();
    let mut info = Vec::new();

    for issue in issues {
        let severity = get_severity(issue);
        match severity {
            Severity::Critical => critical.push(issue.clone()),
            Severity::High => high.push(issue.clone()),
            Severity::Medium => medium.push(issue.clone()),
            Severity::Low => low.push(issue.clone()),
            Severity::Info => info.push(issue.clone()),
        }
    }

    (critical, high, medium, low, info)
}

/// Extract severity from an issue value
fn get_severity(issue: &Value) -> Severity {
    // Try common severity field names
    let severity_fields = ["severity", "level", "priority", "type"];

    for field in &severity_fields {
        if let Some(s) = issue.get(field).and_then(|v| v.as_str()) {
            return Severity::from_str(s);
        }
    }

    // Check for error/warning in code field
    if let Some(code) = issue.get("code").and_then(|v| v.as_str()) {
        if code.to_lowercase().contains("error") {
            return Severity::Critical;
        }
        if code.to_lowercase().contains("warn") {
            return Severity::High;
        }
    }

    Severity::Medium // Default
}

/// Get issue code/type for deduplication grouping
fn get_issue_code(issue: &Value) -> String {
    // Try common code field names
    let code_fields = ["code", "rule", "rule_id", "type", "check", "id"];

    for field in &code_fields {
        if let Some(s) = issue.get(field).and_then(|v| v.as_str()) {
            return s.to_string();
        }
    }

    // Fall back to message hash
    if let Some(msg) = issue.get("message").and_then(|v| v.as_str()) {
        return format!("msg:{}", &msg[..msg.len().min(30)]);
    }

    "unknown".to_string()
}

/// Get file path from an issue
fn get_issue_file(issue: &Value) -> Option<String> {
    let file_fields = ["file", "path", "filename", "location", "source"];

    for field in &file_fields {
        if let Some(s) = issue.get(field).and_then(|v| v.as_str()) {
            return Some(s.to_string());
        }
        // Handle nested location objects
        if let Some(loc) = issue.get(field).and_then(|v| v.as_object())
            && let Some(f) = loc.get("file").and_then(|v| v.as_str())
        {
            return Some(f.to_string());
        }
    }

    None
}

/// Get message from an issue
fn get_issue_message(issue: &Value) -> String {
    let msg_fields = ["message", "msg", "description", "text", "detail"];

    for field in &msg_fields {
        if let Some(s) = issue.get(field).and_then(|v| v.as_str()) {
            return s.to_string();
        }
    }

    "No message".to_string()
}

/// Deduplicate issues into patterns
fn deduplicate_to_patterns(
    issues: &[Value],
    config: &CompressionConfig,
) -> Vec<DeduplicatedPattern> {
    // Group by issue code
    let mut groups: HashMap<String, Vec<&Value>> = HashMap::new();

    for issue in issues {
        let code = get_issue_code(issue);
        groups.entry(code).or_default().push(issue);
    }

    // Convert groups to patterns
    let mut patterns: Vec<DeduplicatedPattern> = groups
        .into_iter()
        .map(|(code, group)| {
            let first = group[0];
            let severity = get_severity(first);
            let message = get_issue_message(first);

            // Collect affected files
            let mut files: Vec<String> = group.iter().filter_map(|i| get_issue_file(i)).collect();
            files.dedup();

            let total_files = files.len();
            let truncated_files: Vec<String> = if files.len() > config.max_files_per_pattern {
                let mut truncated: Vec<String> = files
                    .iter()
                    .take(config.max_files_per_pattern)
                    .cloned()
                    .collect();
                truncated.push(format!(
                    "...+{} more",
                    total_files - config.max_files_per_pattern
                ));
                truncated
            } else {
                files
            };

            // Extract fix template if available
            let fix_template = first
                .get("fix")
                .or_else(|| first.get("suggestion"))
                .or_else(|| first.get("recommendation"))
                .and_then(|v| v.as_str())
                .map(|s| s.to_string());

            DeduplicatedPattern {
                code,
                count: group.len(),
                severity,
                message,
                affected_files: truncated_files,
                example: if group.len() > 1 {
                    Some(first.clone())
                } else {
                    None
                },
                fix_template,
            }
        })
        .collect();

    // Sort by severity (critical first) then by count
    patterns.sort_by(|a, b| {
        b.severity
            .cmp(&a.severity)
            .then_with(|| b.count.cmp(&a.count))
    });

    patterns
}

/// Compress analyze_project output specifically
///
/// Handles both:
/// - MonorepoAnalysis: has "projects" array, "is_monorepo", "root_path"
/// - ProjectAnalysis: flat structure with "languages", "technologies" at top level
///
/// For large analysis, returns a minimal summary and stores full data for retrieval.
pub fn compress_analysis_output(output: &Value, config: &CompressionConfig) -> String {
    let raw_str = serde_json::to_string(output).unwrap_or_default();
    if raw_str.len() <= config.target_size_bytes {
        return raw_str;
    }

    // Store full output for later retrieval
    let ref_id = output_store::store_output(output, "analyze_project");

    // Build a MINIMAL summary - just enough to understand the project
    let mut summary = json!({
        "tool": "analyze_project",
        "status": "ANALYSIS_COMPLETE",
        "full_data_ref": ref_id.clone()
    });

    let summary_obj = summary.as_object_mut().unwrap();

    // Detect output type and extract accordingly
    let is_monorepo = output.get("projects").is_some() || output.get("is_monorepo").is_some();
    let is_project_analysis =
        output.get("languages").is_some() && output.get("analysis_metadata").is_some();

    if is_monorepo {
        // MonorepoAnalysis structure
        if let Some(mono) = output.get("is_monorepo").and_then(|v| v.as_bool()) {
            summary_obj.insert("is_monorepo".to_string(), json!(mono));
        }
        if let Some(root) = output.get("root_path").and_then(|v| v.as_str()) {
            summary_obj.insert("root_path".to_string(), json!(root));
        }

        if let Some(projects) = output.get("projects").and_then(|v| v.as_array()) {
            summary_obj.insert("project_count".to_string(), json!(projects.len()));

            let mut all_languages: Vec<String> = Vec::new();
            let mut all_frameworks: Vec<String> = Vec::new();
            let mut project_names: Vec<String> = Vec::new();

            for project in projects.iter().take(20) {
                if let Some(name) = project.get("name").and_then(|v| v.as_str()) {
                    project_names.push(name.to_string());
                }
                if let Some(analysis) = project.get("analysis") {
                    if let Some(langs) = analysis.get("languages").and_then(|v| v.as_array()) {
                        for lang in langs {
                            if let Some(name) = lang.get("name").and_then(|v| v.as_str())
                                && !all_languages.contains(&name.to_string())
                            {
                                all_languages.push(name.to_string());
                            }
                        }
                    }
                    if let Some(fws) = analysis.get("frameworks").and_then(|v| v.as_array()) {
                        for fw in fws {
                            if let Some(name) = fw.get("name").and_then(|v| v.as_str())
                                && !all_frameworks.contains(&name.to_string())
                            {
                                all_frameworks.push(name.to_string());
                            }
                        }
                    }
                }
            }

            summary_obj.insert("project_names".to_string(), json!(project_names));
            summary_obj.insert("languages_detected".to_string(), json!(all_languages));
            summary_obj.insert("frameworks_detected".to_string(), json!(all_frameworks));
        }
    } else if is_project_analysis {
        // ProjectAnalysis flat structure - languages/technologies at top level
        if let Some(root) = output.get("project_root").and_then(|v| v.as_str()) {
            summary_obj.insert("project_root".to_string(), json!(root));
        }
        if let Some(arch) = output.get("architecture_type").and_then(|v| v.as_str()) {
            summary_obj.insert("architecture_type".to_string(), json!(arch));
        }
        if let Some(proj_type) = output.get("project_type").and_then(|v| v.as_str()) {
            summary_obj.insert("project_type".to_string(), json!(proj_type));
        }

        // Extract languages (at top level)
        if let Some(langs) = output.get("languages").and_then(|v| v.as_array()) {
            let names: Vec<&str> = langs
                .iter()
                .filter_map(|l| l.get("name").and_then(|n| n.as_str()))
                .collect();
            summary_obj.insert("languages_detected".to_string(), json!(names));
        }

        // Extract technologies (at top level)
        if let Some(techs) = output.get("technologies").and_then(|v| v.as_array()) {
            let names: Vec<&str> = techs
                .iter()
                .filter_map(|t| t.get("name").and_then(|n| n.as_str()))
                .collect();
            summary_obj.insert("technologies_detected".to_string(), json!(names));
        }

        // Extract services (include names, not just count)
        if let Some(services) = output.get("services").and_then(|v| v.as_array()) {
            summary_obj.insert("services_count".to_string(), json!(services.len()));
            // Include service names so agent knows what microservices exist
            let service_names: Vec<&str> = services
                .iter()
                .filter_map(|s| s.get("name").and_then(|n| n.as_str()))
                .collect();
            if !service_names.is_empty() {
                summary_obj.insert("services_detected".to_string(), json!(service_names));
            }
        }
    }

    // CRITICAL: Include retrieval instructions prominently
    summary_obj.insert(
        "retrieval_instructions".to_string(),
        json!({
            "message": "Full analysis stored. Use retrieve_output with queries to get specific sections.",
            "ref_id": ref_id,
            "available_queries": [
                "section:summary - Project overview",
                "section:languages - All detected languages",
                "section:frameworks - All detected frameworks/technologies",
                "section:services - All detected services",
                "language:<name> - Details for specific language (e.g., language:Rust)",
                "framework:<name> - Details for specific framework"
            ],
            "example": format!("retrieve_output('{}', 'section:summary')", ref_id)
        }),
    );

    // Build session summary
    let project_count = output
        .get("projects")
        .and_then(|v| v.as_array())
        .map(|a| a.len())
        .unwrap_or(1);
    let summary_str = format!(
        "{} project(s), {} bytes stored",
        project_count,
        raw_str.len()
    );

    // Register in session registry
    output_store::register_session_ref(
        &ref_id,
        "analyze_project",
        "Full project analysis (use section queries to retrieve specific data)",
        &summary_str,
        raw_str.len(),
    );

    // Return minimal JSON
    serde_json::to_string_pretty(&summary).unwrap_or_else(|_| {
        format!(
            r#"{{"tool":"analyze_project","status":"STORED","full_data_ref":"{}","message":"Analysis complete. Use retrieve_output('{}', 'section:summary') to view."}}"#,
            ref_id, ref_id
        )
    })
}

/// CLI variant of compress_tool_output - produces strict valid JSON with CLI-syntax retrieval hints.
///
/// Differences from compress_tool_output():
/// - retrieval_hint uses CLI syntax (`sync-ctl retrieve '<ref_id>' --query '...'`)
/// - Does NOT append format_session_refs_for_agent() plaintext footer
/// - Output is guaranteed valid JSON
pub fn compress_tool_output_cli(
    output: &Value,
    tool_name: &str,
    config: &CompressionConfig,
) -> String {
    // Check if output is small enough - no compression needed
    let raw_str = serde_json::to_string(output).unwrap_or_default();
    if raw_str.len() <= config.target_size_bytes {
        // Still store and add retrieval fields for consistency
        let ref_id = output_store::store_output(output, tool_name);
        let mut obj = match output.clone() {
            Value::Object(m) => m,
            other => {
                let mut m = serde_json::Map::new();
                m.insert("data".to_string(), other);
                m
            }
        };
        obj.insert("full_data_ref".to_string(), json!(ref_id));
        obj.insert(
            "retrieval_hint".to_string(),
            json!(format!(
                "Use `sync-ctl retrieve '{}' --query 'severity:critical'` for details. Paginate with --limit N --offset M. Other queries: 'file:<path>', 'code:<id>'",
                ref_id
            )),
        );
        return serde_json::to_string_pretty(&Value::Object(obj)).unwrap_or(raw_str);
    }

    // Store full output for later retrieval
    let ref_id = output_store::store_output(output, tool_name);

    // Handle dependency-map outputs (e.g. {"dependencies": {...}, "total": N})
    // These aren't issues/findings — compress by summarizing the dep map
    if let Some(deps_obj) = output.get("dependencies").and_then(|v| v.as_object()) {
        let total = output
            .get("total")
            .and_then(|v| v.as_u64())
            .unwrap_or(deps_obj.len() as u64);

        // Build a compact summary: counts by source, license distribution
        let mut by_source: std::collections::HashMap<String, usize> =
            std::collections::HashMap::new();
        let mut by_license: std::collections::HashMap<String, usize> =
            std::collections::HashMap::new();
        let mut dev_count = 0usize;
        let mut prod_count = 0usize;

        for dep in deps_obj.values() {
            let source = dep
                .get("source")
                .and_then(|v| v.as_str())
                .unwrap_or("unknown");
            *by_source.entry(source.to_string()).or_default() += 1;
            let license = dep
                .get("license")
                .and_then(|v| v.as_str())
                .unwrap_or("Unknown");
            *by_license.entry(license.to_string()).or_default() += 1;
            if dep.get("is_dev").and_then(|v| v.as_bool()).unwrap_or(false) {
                dev_count += 1;
            } else {
                prod_count += 1;
            }
        }

        return serde_json::to_string_pretty(&json!({
            "tool": tool_name,
            "total": total,
            "production": prod_count,
            "development": dev_count,
            "by_source": by_source,
            "by_license": by_license,
            "full_data_ref": ref_id,
            "retrieval_hint": format!(
                "Use `sync-ctl retrieve '{}' --query 'file:<path>'` for details. Paginate with --limit N --offset M.",
                ref_id
            )
        }))
        .unwrap_or(raw_str);
    }

    // Extract issues/findings array from the output
    let issues = extract_issues(output);

    if issues.is_empty() {
        // No issues to compress, just store and return summary as strict JSON
        return serde_json::to_string_pretty(&json!({
            "tool": tool_name,
            "status": "NO_ISSUES",
            "summary": { "total": 0 },
            "full_data_ref": ref_id,
            "retrieval_hint": format!(
                "Use `sync-ctl retrieve '{}' --query 'severity:critical'` for details. Paginate with --limit N --offset M.",
                ref_id
            )
        }))
        .unwrap_or(raw_str);
    }

    // Classify issues by severity
    let (critical, high, medium, low, info) = classify_by_severity(&issues);

    // Build summary
    let summary = SeveritySummary {
        total: issues.len(),
        critical: critical.len(),
        high: high.len(),
        medium: medium.len(),
        low: low.len(),
        info: info.len(),
    };

    // Critical issues: always full detail
    let critical_issues: Vec<Value> = critical.clone();

    // High issues: full detail if few, otherwise show first max_high_full
    let high_issues: Vec<Value> = if high.len() <= config.max_high_full {
        high.clone()
    } else {
        high.iter().take(config.max_high_full).cloned().collect()
    };

    // Deduplicate medium/low/info issues into patterns
    let mut all_lower: Vec<Value> = Vec::new();
    all_lower.extend(medium.clone());
    all_lower.extend(low.clone());
    all_lower.extend(info.clone());

    // Also add remaining high issues if there were too many
    if high.len() > config.max_high_full {
        all_lower.extend(high.iter().skip(config.max_high_full).cloned());
    }

    let patterns = deduplicate_to_patterns(&all_lower, config);

    // Determine status
    let status = if summary.critical > 0 {
        "CRITICAL_ISSUES_FOUND"
    } else if summary.high > 0 {
        "HIGH_ISSUES_FOUND"
    } else if summary.total > 0 {
        "ISSUES_FOUND"
    } else {
        "CLEAN"
    };

    let compressed = CompressedOutput {
        tool: tool_name.to_string(),
        status: status.to_string(),
        summary,
        critical_issues,
        high_issues,
        patterns,
        full_data_ref: ref_id.clone(),
        retrieval_hint: format!(
            "Use `sync-ctl retrieve '{}' --query 'severity:critical'` for details. Paginate with --limit N --offset M. Other queries: 'file:<path>', 'code:<id>'",
            ref_id
        ),
    };

    // Return strict JSON - no plaintext footer appended
    serde_json::to_string_pretty(&compressed).unwrap_or(raw_str)
}

/// CLI variant of compress_analysis_output - produces strict valid JSON with CLI-syntax retrieval hints.
///
/// Differences from compress_analysis_output():
/// - retrieval_hint uses CLI syntax (`sync-ctl retrieve '<ref_id>' --query '...'`)
/// - Does NOT append format_session_refs_for_agent() plaintext footer
/// - Output is guaranteed valid JSON
pub fn compress_analysis_output_cli(output: &Value, _config: &CompressionConfig) -> String {
    // Store full output for later retrieval
    let ref_id = output_store::store_output(output, "analyze_project");

    // Build a MINIMAL summary - just enough to understand the project
    let mut summary = json!({
        "tool": "analyze_project",
        "status": "ANALYSIS_COMPLETE",
        "full_data_ref": ref_id.clone()
    });

    let summary_obj = summary.as_object_mut().unwrap();

    // Detect output type and extract accordingly
    let is_monorepo = output.get("projects").is_some() || output.get("is_monorepo").is_some();
    let is_project_analysis =
        output.get("languages").is_some() && output.get("analysis_metadata").is_some();

    if is_monorepo {
        // MonorepoAnalysis structure
        if let Some(mono) = output.get("is_monorepo").and_then(|v| v.as_bool()) {
            summary_obj.insert("is_monorepo".to_string(), json!(mono));
        }
        if let Some(root) = output.get("root_path").and_then(|v| v.as_str()) {
            summary_obj.insert("root_path".to_string(), json!(root));
        }

        if let Some(projects) = output.get("projects").and_then(|v| v.as_array()) {
            summary_obj.insert("project_count".to_string(), json!(projects.len()));

            let mut all_languages: Vec<String> = Vec::new();
            let mut all_frameworks: Vec<String> = Vec::new();
            let mut project_names: Vec<String> = Vec::new();

            for project in projects.iter().take(20) {
                if let Some(name) = project.get("name").and_then(|v| v.as_str()) {
                    project_names.push(name.to_string());
                }
                if let Some(analysis) = project.get("analysis") {
                    if let Some(langs) = analysis.get("languages").and_then(|v| v.as_array()) {
                        for lang in langs {
                            if let Some(name) = lang.get("name").and_then(|v| v.as_str())
                                && !all_languages.contains(&name.to_string())
                            {
                                all_languages.push(name.to_string());
                            }
                        }
                    }
                    if let Some(fws) = analysis.get("frameworks").and_then(|v| v.as_array()) {
                        for fw in fws {
                            if let Some(name) = fw.get("name").and_then(|v| v.as_str())
                                && !all_frameworks.contains(&name.to_string())
                            {
                                all_frameworks.push(name.to_string());
                            }
                        }
                    }
                }
            }

            summary_obj.insert("project_names".to_string(), json!(project_names));
            summary_obj.insert("languages_detected".to_string(), json!(all_languages));
            summary_obj.insert("frameworks_detected".to_string(), json!(all_frameworks));
        }
    } else if is_project_analysis {
        // ProjectAnalysis flat structure - languages/technologies at top level
        if let Some(root) = output.get("project_root").and_then(|v| v.as_str()) {
            summary_obj.insert("project_root".to_string(), json!(root));
        }
        if let Some(arch) = output.get("architecture_type").and_then(|v| v.as_str()) {
            summary_obj.insert("architecture_type".to_string(), json!(arch));
        }
        if let Some(proj_type) = output.get("project_type").and_then(|v| v.as_str()) {
            summary_obj.insert("project_type".to_string(), json!(proj_type));
        }

        // Extract languages (at top level)
        if let Some(langs) = output.get("languages").and_then(|v| v.as_array()) {
            let names: Vec<&str> = langs
                .iter()
                .filter_map(|l| l.get("name").and_then(|n| n.as_str()))
                .collect();
            summary_obj.insert("languages_detected".to_string(), json!(names));
        }

        // Extract technologies (at top level)
        if let Some(techs) = output.get("technologies").and_then(|v| v.as_array()) {
            let names: Vec<&str> = techs
                .iter()
                .filter_map(|t| t.get("name").and_then(|n| n.as_str()))
                .collect();
            summary_obj.insert("technologies_detected".to_string(), json!(names));
        }

        // Extract services (include names, not just count)
        if let Some(services) = output.get("services").and_then(|v| v.as_array()) {
            summary_obj.insert("services_count".to_string(), json!(services.len()));
            let service_names: Vec<&str> = services
                .iter()
                .filter_map(|s| s.get("name").and_then(|n| n.as_str()))
                .collect();
            if !service_names.is_empty() {
                summary_obj.insert("services_detected".to_string(), json!(service_names));
            }
        }
    }

    // CLI-syntax retrieval hint
    summary_obj.insert(
        "retrieval_hint".to_string(),
        json!(format!(
            "Use `sync-ctl retrieve '{}' --query 'section:summary'` for full details. Other queries: 'section:languages', 'section:frameworks', 'section:services'",
            ref_id
        )),
    );

    // Return strict JSON - no plaintext footer appended
    serde_json::to_string_pretty(&summary).unwrap_or_else(|_| {
        format!(
            r#"{{"tool":"analyze_project","status":"STORED","full_data_ref":"{}","retrieval_hint":"Use `sync-ctl retrieve '{}' --query 'section:summary'` for full details."}}"#,
            ref_id, ref_id
        )
    })
}

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

    #[test]
    fn test_severity_ordering() {
        assert!(Severity::Critical > Severity::High);
        assert!(Severity::High > Severity::Medium);
        assert!(Severity::Medium > Severity::Low);
        assert!(Severity::Low > Severity::Info);
    }

    #[test]
    fn test_extract_issues_from_array_field() {
        let output = json!({
            "issues": [
                { "code": "DL3008", "severity": "warning", "message": "Pin versions" },
                { "code": "DL3009", "severity": "info", "message": "Delete apt lists" }
            ]
        });

        let issues = extract_issues(&output);
        assert_eq!(issues.len(), 2);
    }

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

        let config = CompressionConfig::default();
        let patterns = deduplicate_to_patterns(&issues, &config);

        assert_eq!(patterns.len(), 2);

        let dl3008 = patterns.iter().find(|p| p.code == "DL3008").unwrap();
        assert_eq!(dl3008.count, 3);
        assert_eq!(dl3008.affected_files.len(), 3);
    }

    #[test]
    fn test_small_output_not_compressed() {
        let small_output = json!({
            "issues": [
                { "code": "test", "severity": "low" }
            ]
        });

        let config = CompressionConfig {
            target_size_bytes: 10000,
            ..Default::default()
        };

        let result = compress_tool_output(&small_output, "test", &config);
        // Should return original (not compressed) since it's small
        assert!(!result.contains("full_data_ref"));
    }

    #[test]
    fn test_compress_tool_output_cli_produces_valid_json() {
        let output = serde_json::json!({
            "findings": (0..100).map(|i| serde_json::json!({
                "code": format!("SEC{:03}", i),
                "severity": if i < 3 { "critical" } else if i < 15 { "high" } else { "medium" },
                "message": format!("Finding {} with enough text to exceed compression threshold when multiplied", i),
                "file": format!("src/file_{}.rs", i),
            })).collect::<Vec<_>>()
        });
        let config = CompressionConfig::default();
        let result = compress_tool_output_cli(&output, "security", &config);

        // Must be valid JSON
        let parsed: Result<serde_json::Value, _> = serde_json::from_str(&result);
        assert!(
            parsed.is_ok(),
            "CLI output must be valid JSON, got: {}",
            &result[..200.min(result.len())]
        );

        let json = parsed.unwrap();
        // Must contain CLI-syntax retrieval hint
        let hint = json.get("retrieval_hint").and_then(|v| v.as_str()).unwrap();
        assert!(
            hint.contains("sync-ctl retrieve"),
            "Hint should use CLI syntax, got: {}",
            hint
        );
        assert!(
            !hint.contains("retrieve_output("),
            "Hint should NOT use internal tool call syntax"
        );
    }
}