openlatch-client 0.1.6

The open-source security layer for AI agents — client forwarder
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
//! `openlatch doctor --rescue` — bundle redacted diagnostics into a ZIP.
//!
//! Collection pipeline (brainstorm §5.5):
//! 1. Parse `--since` (default `24h`).
//! 2. Probe daemon `/health` + `/metrics` (no auth) with 500ms timeout.
//! 3. Walk `<ol_dir>` excluding `*.bak` / `*.tmp`.
//! 4. Each text file: read → `privacy::filter_event_with_hits`.
//! 5. Hard-redact `daemon.token` and `Authorization: Bearer ...`.
//! 6. Redact `$HOME` → `~` in `fix-journal.json` paths.
//! 7. Claude Code's `settings.json` (other 7 agents marked
//!    `not_collected: not_supported_in_v1` — matches Day-1 install scope).
//! 8. Binary metadata (SHA256 + size + path + `--version`); no bytes.
//! 9. Inventory + y/N prompt (skip with `--yes`).
//! 10. Write ZIP to `$PWD/openlatch-rescue-<ts>-<agid4>.zip` (or `--output`).
//! 11. MANIFEST.json describes everything.

use std::collections::BTreeMap;
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
use std::time::{Duration, Instant};

use chrono::{DateTime, Utc};
use serde::Serialize;
use sha2::{Digest, Sha256};
use zip::write::SimpleFileOptions;
use zip::CompressionMethod;

use crate::cli::output::{OutputConfig, OutputFormat};
use crate::cli::DoctorArgs;
use crate::config;
use crate::error::OlError;
use crate::privacy::{HitCounter, PrivacyFilter};
use crate::telemetry::{self, Event};

/// Hard-redaction marker substituted for `daemon.token` content.
const TOKEN_REDACTED_MARKER: &str = "[REDACTED:TOKEN]";
/// Soft cap printed as a warning (not a hard error) per brainstorm §5.5.
const ARCHIVE_SOFT_CAP_BYTES: u64 = 50 * 1024 * 1024;
/// Per-event-log file cap before head-truncation (brainstorm §5.5).
const EVENT_LOG_FILE_CAP_BYTES: usize = 5 * 1024 * 1024;
/// daemon.log tail cap.
const DAEMON_LOG_TAIL_CAP_BYTES: usize = 1024 * 1024;

/// Entry point for `openlatch doctor --rescue`.
///
/// `fix_applied_after` is recorded in MANIFEST so support engineers can
/// distinguish a pre-fix snapshot (the standalone `--rescue` case) from
/// a snapshot taken inside `--fix --rescue` (which then ran fix after).
pub fn run(
    args: &DoctorArgs,
    output: &OutputConfig,
    fix_applied_after: bool,
) -> Result<(), OlError> {
    let started_at = Utc::now();
    let started_instant = Instant::now();
    let ol_dir = config::openlatch_dir();

    crate::cli::header::print(output, &["doctor", "--rescue"]);
    if output.format == OutputFormat::Human && !output.quiet {
        eprintln!();
    }

    let since = parse_since(args.since.as_deref())?;
    let logs_since = started_at - since;

    // 1. Probe daemon (best-effort, 500ms timeout each).
    let port = config::Config::load(None, None, false)
        .map(|c| c.port)
        .unwrap_or(config::PORT_RANGE_START);
    let daemon_health = probe_endpoint(port, "/health");
    let daemon_metrics = probe_endpoint(port, "/metrics");
    let daemon_reachable = daemon_health.body.is_some() || daemon_metrics.body.is_some();

    // 2. Plan the collection — gather (live_path, archive_path, redaction_strategy).
    let plan = build_collection_plan(&ol_dir, logs_since);

    // 3. Inventory + consent prompt unless --yes.
    if !args.yes && !confirm_inventory(&plan, output)? {
        if output.format == OutputFormat::Json {
            output.print_json(&serde_json::json!({
                "command": "doctor_rescue",
                "status": "cancelled",
                "message": "user declined inventory prompt",
            }));
        } else if !output.quiet {
            eprintln!("Cancelled.");
        }
        return Ok(());
    }

    // 4. Open ZIP.
    let agid = read_agent_id_short(&ol_dir);
    let ts_label = started_at.format("%Y%m%dT%H%M%SZ").to_string();
    let archive_name = format!("openlatch-rescue-{ts_label}-{agid}.zip");
    let archive_path = match args.output.as_ref() {
        Some(p) => p.clone(),
        None => std::env::current_dir()
            .unwrap_or_else(|_| PathBuf::from("."))
            .join(&archive_name),
    };

    let archive_file = std::fs::File::create(&archive_path).map_err(|e| {
        OlError::new(
            crate::error::ERR_DOCTOR_RESCUE_WRITE_FAILED,
            format!(
                "cannot create rescue archive '{}': {e}",
                archive_path.display()
            ),
        )
    })?;
    let mut zw = zip::ZipWriter::new(archive_file);
    let zip_opts = SimpleFileOptions::default().compression_method(CompressionMethod::Deflated);

    // 5. Build the privacy filter once and aggregate hits across the run.
    let cfg = config::Config::load(None, None, false).ok();
    let extra = cfg.map(|c| c.extra_patterns).unwrap_or_default();
    let filter = PrivacyFilter::new(&extra);
    let mut total_hits = HitCounter::new();
    let mut file_records: Vec<FileRecord> = Vec::new();
    let mut had_partial_failure = false;

    for entry in &plan.entries {
        match collect_file(entry, &filter, &mut total_hits, &mut zw, zip_opts) {
            Ok(record) => file_records.push(record),
            Err(e) => {
                had_partial_failure = true;
                tracing::warn!(
                    error = %e.message,
                    code = e.code,
                    path = %entry.live_path.display(),
                    "doctor --rescue: collector failed for entry"
                );
                file_records.push(FileRecord {
                    path: entry.archive_path.clone(),
                    size_bytes: 0,
                    sha256: String::new(),
                    redactors_applied: Vec::new(),
                    redaction_failed: true,
                    note: Some(format!("collector errored: {}", e.code)),
                    truncated: false,
                });
            }
        }
    }

    // 6. Daemon snapshots (system/).
    if let Err(e) = write_zip_entry(
        &mut zw,
        zip_opts,
        "system/daemon-health.json",
        &daemon_health.to_json_bytes(),
    ) {
        had_partial_failure = true;
        tracing::warn!(error = %e.message, "doctor --rescue: cannot write daemon-health.json");
    } else {
        file_records.push(FileRecord {
            path: "system/daemon-health.json".to_string(),
            size_bytes: daemon_health.to_json_bytes().len() as u64,
            sha256: sha256_hex(&daemon_health.to_json_bytes()),
            redactors_applied: Vec::new(),
            redaction_failed: false,
            note: Some(format!("status={}", daemon_health.status_label())),
            truncated: false,
        });
    }
    if let Err(e) = write_zip_entry(
        &mut zw,
        zip_opts,
        "system/daemon-metrics.json",
        &daemon_metrics.to_json_bytes(),
    ) {
        had_partial_failure = true;
        tracing::warn!(error = %e.message, "doctor --rescue: cannot write daemon-metrics.json");
    } else {
        file_records.push(FileRecord {
            path: "system/daemon-metrics.json".to_string(),
            size_bytes: daemon_metrics.to_json_bytes().len() as u64,
            sha256: sha256_hex(&daemon_metrics.to_json_bytes()),
            redactors_applied: Vec::new(),
            redaction_failed: false,
            note: Some(format!("status={}", daemon_metrics.status_label())),
            truncated: false,
        });
    }

    // 7. OS info.
    let os_info = serde_json::json!({
        "os": std::env::consts::OS,
        "arch": std::env::consts::ARCH,
        "family": std::env::consts::FAMILY,
        // No hostname, no username, no working directory (telemetry no-PII rule).
    });
    let os_bytes = serde_json::to_vec_pretty(&os_info).unwrap_or_default();
    let _ = write_zip_entry(&mut zw, zip_opts, "system/os-info.json", &os_bytes);
    file_records.push(FileRecord {
        path: "system/os-info.json".to_string(),
        size_bytes: os_bytes.len() as u64,
        sha256: sha256_hex(&os_bytes),
        redactors_applied: Vec::new(),
        redaction_failed: false,
        note: None,
        truncated: false,
    });

    // 8. Binary metadata (SHA256 + size + path + version) — never bytes.
    let bin_info = collect_binary_info();
    let bin_bytes = serde_json::to_vec_pretty(&bin_info).unwrap_or_default();
    let _ = write_zip_entry(&mut zw, zip_opts, "system/binary-info.json", &bin_bytes);
    file_records.push(FileRecord {
        path: "system/binary-info.json".to_string(),
        size_bytes: bin_bytes.len() as u64,
        sha256: sha256_hex(&bin_bytes),
        redactors_applied: Vec::new(),
        redaction_failed: false,
        note: None,
        truncated: false,
    });

    // 9. Per-pattern privacy hit summary.
    let hits_value = serde_json::json!({
        "total_hits": total_hits.total(),
        "by_pattern": total_hits.as_map(),
    });
    let hits_bytes = serde_json::to_vec_pretty(&hits_value).unwrap_or_default();
    let _ = write_zip_entry(&mut zw, zip_opts, "privacy/redactor-hits.json", &hits_bytes);

    // 10. Agents checked. Day 1 = Claude Code only.
    let claude_present = match crate::hooks::detect_agent() {
        Ok(crate::hooks::DetectedAgent::ClaudeCode { settings_path, .. }) => settings_path.exists(),
        _ => false,
    };
    let agents_checked = serde_json::json!({
        "claude-code": {
            "found": claude_present,
            "config_path": match crate::hooks::detect_agent() {
                Ok(crate::hooks::DetectedAgent::ClaudeCode { settings_path, .. }) =>
                    redact_home(&settings_path.display().to_string()),
                _ => String::new(),
            },
            "openlatch_hooks_present": claude_present,
        },
        "cursor":         {"found": false, "reason": "not_supported_in_v1"},
        "windsurf":       {"found": false, "reason": "not_supported_in_v1"},
        "github-copilot": {"found": false, "reason": "not_supported_in_v1"},
        "codex-cli":      {"found": false, "reason": "not_supported_in_v1"},
        "gemini-cli":     {"found": false, "reason": "not_supported_in_v1"},
        "cline":          {"found": false, "reason": "not_supported_in_v1"},
        "openclaw":       {"found": false, "reason": "not_supported_in_v1"},
    });

    // 11. MANIFEST.json (last so file_records is fully populated).
    let completed_at = Utc::now();
    let duration_ms = started_instant.elapsed().as_millis() as u64;
    let manifest = serde_json::json!({
        "manifest_version": "1",
        "generated_at": completed_at,
        "tool": {
            "name": "openlatch-client",
            "version": env!("CARGO_PKG_VERSION"),
            "build_profile": if cfg!(debug_assertions) { "debug" } else { "release" },
        },
        "host": {
            "os": std::env::consts::OS,
            "arch": std::env::consts::ARCH,
        },
        "collection": {
            "started_at": started_at,
            "completed_at": completed_at,
            "duration_ms": duration_ms,
            "window": { "logs_since": logs_since },
            "daemon_reachable": daemon_reachable,
            "fix_applied_after": fix_applied_after,
            "partial_failure": had_partial_failure,
        },
        "files": file_records,
        "agents_checked": agents_checked,
        "redactors": {
            "total_hits": total_hits.total(),
            "by_pattern": total_hits.as_map(),
        },
    });
    let manifest_bytes = serde_json::to_vec_pretty(&manifest).map_err(|e| {
        OlError::new(
            crate::error::ERR_DOCTOR_RESCUE_WRITE_FAILED,
            format!("cannot serialize MANIFEST.json: {e}"),
        )
    })?;
    write_zip_entry(&mut zw, zip_opts, "MANIFEST.json", &manifest_bytes)?;

    let archive_handle = zw.finish().map_err(|e| {
        OlError::new(
            crate::error::ERR_DOCTOR_RESCUE_WRITE_FAILED,
            format!("cannot finalize archive: {e}"),
        )
    })?;
    drop(archive_handle);

    let archive_size = std::fs::metadata(&archive_path)
        .map(|m| m.len())
        .unwrap_or(0);

    print_rescue_results(
        &archive_path,
        archive_size,
        file_records.len(),
        daemon_reachable,
        claude_present,
        total_hits.total(),
        duration_ms,
        fix_applied_after,
        had_partial_failure,
        output,
    );

    if had_partial_failure {
        tracing::warn!(
            code = crate::error::ERR_DOCTOR_RESCUE_PARTIAL,
            "doctor --rescue: archive produced with one or more collector failures"
        );
    }

    let agents_found_for_telemetry: Vec<&str> = if claude_present {
        vec!["claude-code"]
    } else {
        vec![]
    };
    telemetry::capture_global(Event::doctor_rescue_run(
        archive_size,
        file_records.len(),
        daemon_reachable,
        agents_found_for_telemetry,
        total_hits.total(),
        duration_ms,
        fix_applied_after,
    ));

    Ok(())
}

// ---------------------------------------------------------------------------
// Plan + collection
// ---------------------------------------------------------------------------

#[derive(Debug, Clone)]
struct CollectionPlan {
    entries: Vec<CollectionEntry>,
}

#[derive(Debug, Clone)]
struct CollectionEntry {
    /// Source path on disk.
    live_path: PathBuf,
    /// Path inside the ZIP archive (forward-slash).
    archive_path: String,
    /// How to redact this file's contents.
    strategy: RedactionStrategy,
}

#[derive(Debug, Clone, PartialEq, Eq)]
enum RedactionStrategy {
    /// Run privacy filter (regex masks) on JSON-shaped content.
    PrivacyFilter,
    /// Same as PrivacyFilter, plus head-truncate if over the per-file cap.
    PrivacyFilterTruncating { cap_bytes: usize, keep_tail: bool },
    /// Hard-replace the whole file body with TOKEN_REDACTED_MARKER.
    HardRedactToken,
    /// Privacy filter + path redaction ($HOME → ~) for fix-journal.json.
    PrivacyFilterAndPathRedact,
    /// Pass through unmodified (e.g. daemon.port — just a port number).
    Passthrough,
}

fn build_collection_plan(ol_dir: &Path, _logs_since: DateTime<Utc>) -> CollectionPlan {
    let mut entries = Vec::new();

    // Top-level state files.
    let candidates = [
        (
            ol_dir.join("config.toml"),
            "config/config.toml",
            RedactionStrategy::PrivacyFilter,
        ),
        (
            ol_dir.join("telemetry.json"),
            "config/telemetry.json",
            RedactionStrategy::PrivacyFilter,
        ),
        (
            ol_dir.join("daemon.token"),
            "config/daemon.token",
            RedactionStrategy::HardRedactToken,
        ),
        (
            ol_dir.join("daemon.port"),
            "config/daemon.port",
            RedactionStrategy::Passthrough,
        ),
        (
            ol_dir.join(crate::cli::commands::doctor_fix::JOURNAL_FILENAME),
            "config/fix-journal.json",
            RedactionStrategy::PrivacyFilterAndPathRedact,
        ),
    ];
    for (live, archive, strategy) in candidates {
        if live.exists() && !is_excluded_extension(&live) {
            entries.push(CollectionEntry {
                live_path: live,
                archive_path: archive.to_string(),
                strategy,
            });
        }
    }

    // Logs (events-*.jsonl + daemon.log) — capped per file.
    let logs_dir = ol_dir.join("logs");
    if logs_dir.exists() {
        if let Ok(read) = std::fs::read_dir(&logs_dir) {
            for ent in read.flatten() {
                let p = ent.path();
                if !p.is_file() || is_excluded_extension(&p) {
                    continue;
                }
                let name = match p.file_name().and_then(|n| n.to_str()) {
                    Some(s) => s.to_string(),
                    None => continue,
                };
                let archive_path = format!("logs/{name}");
                let strategy = if name == "daemon.log" {
                    RedactionStrategy::PrivacyFilterTruncating {
                        cap_bytes: DAEMON_LOG_TAIL_CAP_BYTES,
                        keep_tail: true,
                    }
                } else if name.starts_with("events-") && name.ends_with(".jsonl") {
                    RedactionStrategy::PrivacyFilterTruncating {
                        cap_bytes: EVENT_LOG_FILE_CAP_BYTES,
                        keep_tail: true,
                    }
                } else if name == "fallback.jsonl" {
                    RedactionStrategy::PrivacyFilter
                } else {
                    continue;
                };
                entries.push(CollectionEntry {
                    live_path: p,
                    archive_path,
                    strategy,
                });
            }
        }
    }

    // Claude Code's settings.json (only if present). Hard-redact bearer
    // tokens that may appear in arbitrary user keys, then privacy filter.
    if let Ok(crate::hooks::DetectedAgent::ClaudeCode { settings_path, .. }) =
        crate::hooks::detect_agent()
    {
        if settings_path.exists() && !is_excluded_extension(&settings_path) {
            entries.push(CollectionEntry {
                live_path: settings_path,
                archive_path: "agents/claude-code/settings.json".to_string(),
                strategy: RedactionStrategy::PrivacyFilter,
            });
        }
    }

    CollectionPlan { entries }
}

#[derive(Debug, Clone, Serialize)]
struct FileRecord {
    path: String,
    size_bytes: u64,
    sha256: String,
    redactors_applied: Vec<&'static str>,
    redaction_failed: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    note: Option<String>,
    truncated: bool,
}

fn collect_file(
    entry: &CollectionEntry,
    filter: &PrivacyFilter,
    total_hits: &mut HitCounter,
    zw: &mut zip::ZipWriter<std::fs::File>,
    zip_opts: SimpleFileOptions,
) -> Result<FileRecord, OlError> {
    let raw = std::fs::read_to_string(&entry.live_path).map_err(|e| {
        OlError::new(
            crate::error::ERR_DOCTOR_RESCUE_PARTIAL,
            format!(
                "cannot read '{}' for rescue bundle: {e}",
                entry.live_path.display()
            ),
        )
    })?;

    let mut local_hits = HitCounter::new();
    let mut truncated = false;

    let body = match &entry.strategy {
        RedactionStrategy::HardRedactToken => TOKEN_REDACTED_MARKER.to_string(),
        RedactionStrategy::Passthrough => raw,
        RedactionStrategy::PrivacyFilter => filter_text(&raw, filter, &mut local_hits),
        RedactionStrategy::PrivacyFilterAndPathRedact => {
            let filtered = filter_text(&raw, filter, &mut local_hits);
            redact_home(&filtered)
        }
        RedactionStrategy::PrivacyFilterTruncating {
            cap_bytes,
            keep_tail,
        } => {
            let mut text = raw;
            if text.len() > *cap_bytes {
                truncated = true;
                if *keep_tail {
                    let start = text.len() - cap_bytes;
                    let mut start = start;
                    while start < text.len() && !text.is_char_boundary(start) {
                        start += 1;
                    }
                    text = text[start..].to_string();
                } else {
                    let mut end = *cap_bytes;
                    while end > 0 && !text.is_char_boundary(end) {
                        end -= 1;
                    }
                    text.truncate(end);
                }
            }
            filter_text(&text, filter, &mut local_hits)
        }
    };

    let bytes = body.as_bytes();
    write_zip_entry(zw, zip_opts, &entry.archive_path, bytes)?;

    let mut redactors_applied: Vec<&'static str> = local_hits
        .iter()
        .filter(|(_, n)| *n > 0)
        .map(|(label, _)| label)
        .collect();
    redactors_applied.sort();

    if matches!(entry.strategy, RedactionStrategy::HardRedactToken) {
        redactors_applied.push("HARD_TOKEN");
    }

    // Merge local hits into the run-wide counter.
    for (label, n) in local_hits.iter() {
        total_hits.add(label, n);
    }

    Ok(FileRecord {
        path: entry.archive_path.clone(),
        size_bytes: bytes.len() as u64,
        sha256: sha256_hex(bytes),
        redactors_applied,
        redaction_failed: false,
        note: None,
        truncated,
    })
}

fn filter_text(raw: &str, filter: &PrivacyFilter, hits: &mut HitCounter) -> String {
    // Process line-by-line so we don't trip the privacy filter's
    // per-string-leaf 4 KB truncation (MAX_STRING_BYTES). Multi-MB log
    // files would otherwise lose content. Joining back with `\n`
    // preserves the original line structure; the trailing newline (or
    // absence thereof) is restored to match the input.
    let trailing_newline = raw.ends_with('\n');
    let mut out: Vec<String> = Vec::new();
    for line in raw.lines() {
        let mut value = serde_json::Value::String(line.to_string());
        crate::privacy::filter_event_with_hits(&mut value, filter, hits);
        out.push(value.as_str().map(|s| s.to_string()).unwrap_or_default());
    }
    let mut joined = out.join("\n");
    if trailing_newline {
        joined.push('\n');
    }
    joined
}

fn write_zip_entry(
    zw: &mut zip::ZipWriter<std::fs::File>,
    opts: SimpleFileOptions,
    name: &str,
    bytes: &[u8],
) -> Result<(), OlError> {
    zw.start_file(name, opts).map_err(|e| {
        OlError::new(
            crate::error::ERR_DOCTOR_RESCUE_WRITE_FAILED,
            format!("cannot start zip entry '{name}': {e}"),
        )
    })?;
    zw.write_all(bytes).map_err(|e| {
        OlError::new(
            crate::error::ERR_DOCTOR_RESCUE_WRITE_FAILED,
            format!("cannot write zip entry '{name}': {e}"),
        )
    })?;
    Ok(())
}

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

fn is_excluded_extension(p: &Path) -> bool {
    matches!(
        p.extension().and_then(|s| s.to_str()),
        Some("bak") | Some("tmp")
    )
}

/// Replace any occurrence of the user's home directory with `~`.
///
/// Applied to file contents that contain absolute paths (e.g. the fix
/// journal). Telemetry-grade redaction: we never want a username to
/// leak through a support bundle.
fn redact_home(text: &str) -> String {
    if let Some(home) = dirs::home_dir() {
        let home_str = home.display().to_string();
        if !home_str.is_empty() {
            // Replace both the platform-native separator and forward slashes
            // (Windows journals may serialize with either).
            let alt = home_str.replace('\\', "/");
            let replaced = text.replace(&home_str, "~");
            if replaced != text {
                return replaced.replace(&alt, "~");
            }
            return text.replace(&alt, "~");
        }
    }
    text.to_string()
}

fn sha256_hex(bytes: &[u8]) -> String {
    let mut h = Sha256::new();
    h.update(bytes);
    let out = h.finalize();
    out.iter().map(|b| format!("{b:02x}")).collect()
}

fn read_agent_id_short(ol_dir: &Path) -> String {
    let cfg = match config::Config::load(None, None, false) {
        Ok(c) => c,
        Err(_) => {
            // Fall back to reading raw config.toml for the agent_id.
            let raw = std::fs::read_to_string(ol_dir.join("config.toml")).unwrap_or_default();
            for line in raw.lines() {
                if let Some(rest) = line.trim().strip_prefix("agent_id") {
                    if let Some(eq) = rest.find('=') {
                        let val = rest[eq + 1..].trim().trim_matches('"');
                        return short_agid(val);
                    }
                }
            }
            return "unknown".to_string();
        }
    };
    cfg.agent_id
        .as_deref()
        .map(short_agid)
        .unwrap_or_else(|| "unknown".to_string())
}

fn short_agid(full: &str) -> String {
    // Format: agt_<32 hex chars>. We want the 4-char prefix after the
    // underscore for filename uniqueness.
    full.strip_prefix("agt_")
        .map(|hex| hex.chars().take(4).collect::<String>())
        .unwrap_or_else(|| full.chars().take(4).collect::<String>())
}

#[derive(Debug, Clone, Serialize)]
struct ProbeResult {
    endpoint: String,
    body: Option<serde_json::Value>,
    error: Option<String>,
}

impl ProbeResult {
    fn status_label(&self) -> &'static str {
        if self.body.is_some() {
            "ok"
        } else {
            "unreachable"
        }
    }

    fn to_json_bytes(&self) -> Vec<u8> {
        serde_json::to_vec_pretty(self).unwrap_or_default()
    }
}

fn probe_endpoint(port: u16, path: &str) -> ProbeResult {
    let url = format!("http://127.0.0.1:{port}{path}");
    let endpoint = path.to_string();
    let client = match reqwest::blocking::Client::builder()
        .timeout(Duration::from_millis(500))
        .build()
    {
        Ok(c) => c,
        Err(e) => {
            return ProbeResult {
                endpoint,
                body: None,
                error: Some(format!("client build failed: {e}")),
            };
        }
    };
    match client.get(&url).send() {
        Ok(resp) => match resp.json::<serde_json::Value>() {
            Ok(v) => ProbeResult {
                endpoint,
                body: Some(v),
                error: None,
            },
            Err(e) => ProbeResult {
                endpoint,
                body: None,
                error: Some(format!("body parse failed: {e}")),
            },
        },
        Err(e) => ProbeResult {
            endpoint,
            body: None,
            error: Some(format!("request failed: {e}")),
        },
    }
}

#[derive(Debug, Clone, Serialize)]
struct BinaryInfo {
    binaries: BTreeMap<String, BinaryRecord>,
}

#[derive(Debug, Clone, Serialize)]
struct BinaryRecord {
    path: String,
    size_bytes: Option<u64>,
    sha256: Option<String>,
    version: String,
    locatable: bool,
}

fn collect_binary_info() -> BinaryInfo {
    let mut binaries = BTreeMap::new();

    let openlatch_path = std::env::current_exe()
        .ok()
        .map(|p| redact_home(&p.display().to_string()))
        .unwrap_or_default();
    let (size, sha) = std::env::current_exe()
        .ok()
        .map(|p| binary_size_and_sha(&p))
        .unwrap_or((None, None));
    binaries.insert(
        "openlatch".to_string(),
        BinaryRecord {
            path: openlatch_path,
            size_bytes: size,
            sha256: sha,
            version: env!("CARGO_PKG_VERSION").to_string(),
            locatable: true,
        },
    );

    let hook_path = crate::hooks::resolve_hook_binary_path();
    let (hook_size, hook_sha) = if hook_path.exists() {
        binary_size_and_sha(&hook_path)
    } else {
        (None, None)
    };
    binaries.insert(
        "openlatch-hook".to_string(),
        BinaryRecord {
            path: redact_home(&hook_path.display().to_string()),
            size_bytes: hook_size,
            sha256: hook_sha,
            version: env!("CARGO_PKG_VERSION").to_string(),
            locatable: hook_path.exists(),
        },
    );

    BinaryInfo { binaries }
}

fn binary_size_and_sha(path: &Path) -> (Option<u64>, Option<String>) {
    let size = std::fs::metadata(path).ok().map(|m| m.len());
    // Hash the file bytes by streaming — binaries can be tens of MB.
    let sha = std::fs::File::open(path).ok().and_then(|mut f| {
        let mut hasher = Sha256::new();
        let mut buf = [0u8; 64 * 1024];
        loop {
            match f.read(&mut buf) {
                Ok(0) => break,
                Ok(n) => hasher.update(&buf[..n]),
                Err(_) => return None,
            }
        }
        let out = hasher.finalize();
        Some(out.iter().map(|b| format!("{b:02x}")).collect())
    });
    (size, sha)
}

// ---------------------------------------------------------------------------
// --since parsing
// ---------------------------------------------------------------------------

/// Parse the `--since` argument into a `chrono::Duration`.
///
/// Accepts: `<n>m` (minutes), `<n>h` (hours), `<n>d` (days). Default 24h.
/// ISO8601 absolute timestamps are not supported in v1 — duration-only
/// keeps the CLI surface tight; users with a specific time can compute
/// the duration from "now".
pub(crate) fn parse_since(input: Option<&str>) -> Result<chrono::Duration, OlError> {
    let raw = input.unwrap_or("24h").trim();
    if raw.is_empty() {
        return Ok(chrono::Duration::hours(24));
    }
    let (num_part, unit) = match raw.chars().last() {
        Some(c) if c.is_alphabetic() => (&raw[..raw.len() - c.len_utf8()], c),
        _ => {
            return Err(OlError::new(
                crate::error::ERR_DOCTOR_RESCUE_PARTIAL,
                format!("--since '{raw}' is missing a unit suffix (e.g. 24h, 7d, 30m)"),
            ));
        }
    };
    let n: i64 = num_part.parse().map_err(|_| {
        OlError::new(
            crate::error::ERR_DOCTOR_RESCUE_PARTIAL,
            format!("--since '{raw}' is not a valid number+unit (e.g. 24h, 7d, 30m)"),
        )
    })?;
    let dur = match unit {
        'm' | 'M' => chrono::Duration::minutes(n),
        'h' | 'H' => chrono::Duration::hours(n),
        'd' | 'D' => chrono::Duration::days(n),
        _ => {
            return Err(OlError::new(
                crate::error::ERR_DOCTOR_RESCUE_PARTIAL,
                format!("--since '{raw}' has unknown unit '{unit}' (use m, h, or d)"),
            ));
        }
    };
    if dur < chrono::Duration::zero() {
        return Err(OlError::new(
            crate::error::ERR_DOCTOR_RESCUE_PARTIAL,
            format!("--since '{raw}' must be a positive duration"),
        ));
    }
    Ok(dur)
}

// ---------------------------------------------------------------------------
// Inventory + output
// ---------------------------------------------------------------------------

fn confirm_inventory(plan: &CollectionPlan, output: &OutputConfig) -> Result<bool, OlError> {
    if output.format == OutputFormat::Json {
        // Non-interactive callers using --json must pass --yes; otherwise
        // we cannot prompt and we treat that as a refusal so we never
        // bundle without consent.
        return Ok(false);
    }
    if !std::io::IsTerminal::is_terminal(&std::io::stdin()) {
        // No TTY: refuse to bundle without explicit --yes (matches the
        // "consent must be explicit" rule from the brainstorm).
        return Ok(false);
    }
    if output.quiet {
        return Ok(false);
    }

    eprintln!("openlatch doctor --rescue will bundle the following:");
    for entry in &plan.entries {
        let live_size = std::fs::metadata(&entry.live_path)
            .map(|m| m.len())
            .unwrap_or(0);
        let strategy_label = match entry.strategy {
            RedactionStrategy::HardRedactToken => "fully redacted",
            RedactionStrategy::Passthrough => "passthrough",
            RedactionStrategy::PrivacyFilter => "redacted",
            RedactionStrategy::PrivacyFilterAndPathRedact => "redacted (paths anonymised)",
            RedactionStrategy::PrivacyFilterTruncating { .. } => {
                "redacted, head-truncated if large"
            }
        };
        eprintln!(
            "  - {} ({}, {})",
            redact_home(&entry.live_path.display().to_string()),
            human_size(live_size),
            strategy_label,
        );
    }
    eprintln!("  - Daemon /health and /metrics snapshots");
    eprintln!("  - Binary metadata (SHA256 + size + version) — no binary bytes");
    eprintln!("  - Per-pattern privacy redactor hit counts (no matched text)");
    eprintln!();
    eprintln!("Continue? [y/N] ");

    let mut buf = String::new();
    std::io::stdin().read_line(&mut buf).map_err(|e| {
        OlError::new(
            crate::error::ERR_DOCTOR_RESCUE_PARTIAL,
            format!("cannot read confirmation: {e}"),
        )
    })?;
    let answer = buf.trim().to_ascii_lowercase();
    Ok(answer == "y" || answer == "yes")
}

fn human_size(n: u64) -> String {
    const KB: u64 = 1024;
    const MB: u64 = 1024 * KB;
    if n < KB {
        format!("{n} B")
    } else if n < MB {
        format!("{:.1} KB", n as f64 / KB as f64)
    } else {
        format!("{:.1} MB", n as f64 / MB as f64)
    }
}

#[allow(clippy::too_many_arguments)] // every field is intentional output context
fn print_rescue_results(
    archive_path: &Path,
    archive_size: u64,
    files_collected: usize,
    daemon_reachable: bool,
    claude_present: bool,
    redactor_hits_total: u64,
    duration_ms: u64,
    fix_applied_after: bool,
    partial_failure: bool,
    output: &OutputConfig,
) {
    let agents: Vec<&str> = if claude_present {
        vec!["claude-code"]
    } else {
        vec![]
    };

    if output.format == OutputFormat::Json {
        output.print_json(&serde_json::json!({
            "command": "doctor_rescue",
            "archive_path": archive_path.display().to_string(),
            "archive_size_bytes": archive_size,
            "files_collected_count": files_collected,
            "daemon_reachable": daemon_reachable,
            "agents_found": agents,
            "redactor_hits_total": redactor_hits_total,
            "duration_ms": duration_ms,
            "fix_applied_after": fix_applied_after,
            "partial_failure": partial_failure,
            "soft_cap_exceeded": archive_size > ARCHIVE_SOFT_CAP_BYTES,
        }));
        return;
    }

    if output.quiet {
        return;
    }

    eprintln!("Rescue archive created:");
    eprintln!(
        "  {} ({})",
        archive_path.display(),
        human_size(archive_size)
    );
    if archive_size > ARCHIVE_SOFT_CAP_BYTES {
        eprintln!(
            "  Warning: archive exceeds {} soft cap.",
            human_size(ARCHIVE_SOFT_CAP_BYTES)
        );
    }
    eprintln!();
    eprintln!("Share this with OpenLatch support:");
    eprintln!("  support@openlatch.ai");
    eprintln!(
        "  or attach to a GitHub issue: https://github.com/OpenLatch/openlatch-client/issues"
    );
    eprintln!();
    eprintln!("Review contents before sharing: unzip -l <archive>");
}

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

    #[test]
    fn test_parse_since_default_24h() {
        let d = parse_since(None).unwrap();
        assert_eq!(d, chrono::Duration::hours(24));
    }

    #[test]
    fn test_parse_since_accepts_minutes_hours_days() {
        assert_eq!(
            parse_since(Some("30m")).unwrap(),
            chrono::Duration::minutes(30)
        );
        assert_eq!(parse_since(Some("4h")).unwrap(), chrono::Duration::hours(4));
        assert_eq!(parse_since(Some("7d")).unwrap(), chrono::Duration::days(7));
    }

    #[test]
    fn test_parse_since_rejects_missing_unit() {
        let err = parse_since(Some("100")).expect_err("must reject");
        assert_eq!(err.code, crate::error::ERR_DOCTOR_RESCUE_PARTIAL);
    }

    #[test]
    fn test_parse_since_rejects_unknown_unit() {
        let err = parse_since(Some("3y")).expect_err("must reject");
        assert_eq!(err.code, crate::error::ERR_DOCTOR_RESCUE_PARTIAL);
    }

    #[test]
    fn test_parse_since_rejects_negative() {
        let err = parse_since(Some("-1h")).expect_err("must reject negative");
        assert_eq!(err.code, crate::error::ERR_DOCTOR_RESCUE_PARTIAL);
    }

    #[test]
    fn test_is_excluded_extension_skips_bak_and_tmp() {
        assert!(is_excluded_extension(Path::new("foo.bak")));
        assert!(is_excluded_extension(Path::new("foo.tmp")));
        assert!(!is_excluded_extension(Path::new("foo.toml")));
        assert!(!is_excluded_extension(Path::new("foo.json")));
    }

    #[test]
    fn test_short_agid_takes_first_4_hex_chars() {
        assert_eq!(short_agid("agt_4f2ab1c2d3e4f567"), "4f2a");
        assert_eq!(short_agid("nopfx"), "nopf");
    }

    #[test]
    fn test_human_size_formats_kb_mb_b() {
        assert_eq!(human_size(512), "512 B");
        assert_eq!(human_size(1536), "1.5 KB");
        assert_eq!(human_size(2_097_152), "2.0 MB");
    }

    #[test]
    fn test_sha256_hex_matches_known_value() {
        // SHA-256("abc") = ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad
        assert_eq!(
            sha256_hex(b"abc"),
            "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
        );
    }

    #[test]
    fn test_filter_text_redacts_aws_key_and_records_hits() {
        let f = PrivacyFilter::new(&[]);
        let mut hits = HitCounter::new();
        let out = filter_text("export KEY=AKIA1234567890ABCDEF", &f, &mut hits);
        assert!(out.contains("[AWS_KEY:AKIA***]"), "got: {out}");
        assert_eq!(hits.as_map().get("AWS_KEY"), Some(&1));
    }

    #[test]
    fn test_collect_file_hard_redacts_token() {
        let tmp = TempDir::new().unwrap();
        let live = tmp.path().join("daemon.token");
        std::fs::write(&live, "deadbeef".repeat(8)).unwrap();
        let entry = CollectionEntry {
            live_path: live,
            archive_path: "config/daemon.token".to_string(),
            strategy: RedactionStrategy::HardRedactToken,
        };
        let f = PrivacyFilter::new(&[]);
        let mut hits = HitCounter::new();
        let zip_path = tmp.path().join("test.zip");
        let zip_file = std::fs::File::create(&zip_path).unwrap();
        let mut zw = zip::ZipWriter::new(zip_file);
        let opts = SimpleFileOptions::default().compression_method(CompressionMethod::Stored);

        let record = collect_file(&entry, &f, &mut hits, &mut zw, opts).expect("collect ok");
        zw.finish().unwrap();

        // The recorded body inside the ZIP must be the marker, not the token bytes.
        let zip_file = std::fs::File::open(&zip_path).unwrap();
        let mut zr = zip::ZipArchive::new(zip_file).unwrap();
        let mut entry = zr.by_name("config/daemon.token").unwrap();
        let mut body = String::new();
        entry.read_to_string(&mut body).unwrap();
        assert_eq!(body, TOKEN_REDACTED_MARKER);
        assert!(record.redactors_applied.contains(&"HARD_TOKEN"));
    }

    #[test]
    fn test_collect_file_truncates_oversized_event_log_keeping_tail() {
        let tmp = TempDir::new().unwrap();
        let live = tmp.path().join("events-2026-04-16.jsonl");
        // Write 6 MB of content; cap is 5 MB → tail-keep.
        let line = "{\"type\":\"x\"}\n";
        let mut content = String::with_capacity(6 * 1024 * 1024);
        while content.len() < 6 * 1024 * 1024 {
            content.push_str(line);
        }
        let unique_tail = "{\"type\":\"final-tail-marker\"}\n";
        content.push_str(unique_tail);
        std::fs::write(&live, &content).unwrap();

        let entry = CollectionEntry {
            live_path: live,
            archive_path: "logs/events-2026-04-16.jsonl".to_string(),
            strategy: RedactionStrategy::PrivacyFilterTruncating {
                cap_bytes: EVENT_LOG_FILE_CAP_BYTES,
                keep_tail: true,
            },
        };
        let f = PrivacyFilter::new(&[]);
        let mut hits = HitCounter::new();
        let zip_path = tmp.path().join("trunc.zip");
        let zip_file = std::fs::File::create(&zip_path).unwrap();
        let mut zw = zip::ZipWriter::new(zip_file);
        let opts = SimpleFileOptions::default().compression_method(CompressionMethod::Stored);

        let record = collect_file(&entry, &f, &mut hits, &mut zw, opts).unwrap();
        zw.finish().unwrap();

        assert!(record.truncated);
        assert!(record.size_bytes <= EVENT_LOG_FILE_CAP_BYTES as u64);
        let zip_file = std::fs::File::open(&zip_path).unwrap();
        let mut zr = zip::ZipArchive::new(zip_file).unwrap();
        let mut e = zr.by_name("logs/events-2026-04-16.jsonl").unwrap();
        let mut body = String::new();
        e.read_to_string(&mut body).unwrap();
        // Tail marker must survive — the truncation kept the latest bytes.
        assert!(
            body.contains("final-tail-marker"),
            "truncation dropped the tail"
        );
    }
}