recall-worker 0.4.13

Recall's merge worker: an enrolled device that drains the sync server's merge queue with the local claude CLI
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
//! The evaluation's checks, and the properties the design pins on them
//! (`docs/design/part5-plan.md`, "PR 6" under "Tests"): a report carries
//! no note text outside `details`; the checks that read files never call
//! `claude`, counted by a stand-in; the secret check finds every planted
//! token and nothing in ordinary notes; and the contradiction check runs
//! only when asked for.
//!
//! Every token here is fake and made at run time from pieces, so no
//! string in this file is one a secret scanner would stop at.

use std::path::PathBuf;

use super::*;
use recall_wire::evaluations::KINDS;

const P: &str = "acme/app";
const G: &str = "global:eko";

fn file(project_key: &str, file_path: &str, content: &str) -> EvaluateFile {
    EvaluateFile {
        project_key: project_key.to_string(),
        file_path: file_path.to_string(),
        content: content.to_string(),
        updated_at: "2026-09-20T10:00:00.000Z".to_string(),
    }
}

fn input(files: Vec<EvaluateFile>, contradictions: bool) -> EvaluateInput {
    EvaluateInput {
        evaluation_id: "eval_test".into(),
        projects: Vec::new(),
        contradictions,
        files,
    }
}

fn settings() -> Settings {
    Settings {
        now: OffsetDateTime::parse(
            "2026-09-25T10:00:00.000Z",
            &time::format_description::well_known::Rfc3339,
        )
        .unwrap(),
        stale_after: Duration::from_secs(90 * 24 * 60 * 60),
        deadline: None,
        cli_unavailable: None,
    }
}

/// A stand-in `claude` that counts every `-p` call in a file beside it and
/// answers each with `answer` as its result. `auth status` is not counted:
/// it is a local check that costs nothing.
struct FakeClaude {
    _dir: tempfile::TempDir,
    bin: String,
    calls: PathBuf,
}

impl FakeClaude {
    fn new(answer: &str) -> Self {
        use std::io::Write;
        use std::os::unix::fs::PermissionsExt;
        let dir = tempfile::tempdir().unwrap();
        let calls = dir.path().join("calls");
        let envelope = dir.path().join("answer.json");
        std::fs::write(
            &envelope,
            serde_json::json!({"is_error": false, "result": answer}).to_string(),
        )
        .unwrap();
        let path = dir.path().join("claude");
        let mut f = std::fs::File::create(&path).unwrap();
        writeln!(f, "#!/bin/sh").unwrap();
        writeln!(
            f,
            r#"if [ "$1" = auth ]; then printf '%s' '{{"loggedIn":true}}'; exit 0; fi"#
        )
        .unwrap();
        writeln!(f, "echo call >> '{}'", calls.display()).unwrap();
        writeln!(f, "cat > /dev/null").unwrap();
        writeln!(f, "cat '{}'", envelope.display()).unwrap();
        drop(f);
        std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o755)).unwrap();
        // Wait out ETXTBSY: another test's fork may still hold the write
        // handle this process just closed (see merge.rs's `settle`).
        for _ in 0..200 {
            match std::process::Command::new(&path).arg("auth").output() {
                Err(e) if e.raw_os_error() == Some(26) => {
                    std::thread::sleep(Duration::from_millis(5))
                }
                _ => break,
            }
        }
        Self {
            bin: path.to_str().unwrap().to_string(),
            _dir: dir,
            calls,
        }
    }

    /// A stand-in that answers with one contradiction whose explanation is
    /// what it was handed on stdin, reversed and stripped to letters,
    /// digits and a few marks: `claude` rewording what it read, in a way
    /// no literal masking of its answer could catch.
    fn echoing() -> Self {
        use std::io::Write;
        use std::os::unix::fs::PermissionsExt;
        let dir = tempfile::tempdir().unwrap();
        let calls = dir.path().join("calls");
        let path = dir.path().join("claude");
        let mut f = std::fs::File::create(&path).unwrap();
        writeln!(f, "#!/bin/sh").unwrap();
        writeln!(
            f,
            r#"if [ "$1" = auth ]; then printf '%s' '{{"loggedIn":true}}'; exit 0; fi"#
        )
        .unwrap();
        writeln!(f, "echo call >> '{}'", calls.display()).unwrap();
        writeln!(f, "body=$(tr -cd 'A-Za-z0-9_ .:=/+-' | rev)").unwrap();
        writeln!(
            f,
            r#"printf '{{"is_error":false,"result":"{{\"contradictions\":[{{\"file\":\"F1\",\"lines\":[1,1],\"explanation\":\"%s\"}}]}}"}}' "$body""#
        )
        .unwrap();
        drop(f);
        std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o755)).unwrap();
        for _ in 0..200 {
            match std::process::Command::new(&path).arg("auth").output() {
                Err(e) if e.raw_os_error() == Some(26) => {
                    std::thread::sleep(Duration::from_millis(5))
                }
                _ => break,
            }
        }
        Self {
            bin: path.to_str().unwrap().to_string(),
            _dir: dir,
            calls,
        }
    }

    fn merger(&self) -> Merger {
        Merger::new(self.bin.clone(), Duration::from_secs(20))
    }

    fn calls(&self) -> usize {
        std::fs::read_to_string(&self.calls)
            .map(|s| s.lines().count())
            .unwrap_or(0)
    }
}

/// A fake token: `prefix` and `len` characters cycling through `alphabet`,
/// assembled here so the source holds no token-shaped string.
fn fake(prefix: &str, alphabet: &str, len: usize) -> String {
    let body: String = alphabet.chars().cycle().skip(3).take(len).collect();
    format!("{prefix}{body}")
}

const ALNUM: &str = "q7Rt2xKp9LmZ4vBn8WcJ3hYs6DfG5aEu";
const UPPER: &str = "Q7RT2XKP9LMZ4VBN8WCJ3HYS6DFG5AEU";
const HEX: &str = "9f2c4b1e7a3d8c60";

/// One of each kind of token the check knows, built from its parts.
fn planted() -> Vec<(&'static str, String)> {
    vec![
        ("AWS access key", fake(&["AK", "IA"].concat(), UPPER, 16)),
        ("AWS session key", fake(&["AS", "IA"].concat(), UPPER, 16)),
        ("GitHub token", fake(&["gh", "p_"].concat(), ALNUM, 36)),
        (
            "GitHub OAuth token",
            fake(&["gh", "o_"].concat(), ALNUM, 36),
        ),
        ("GitHub app token", fake(&["gh", "s_"].concat(), ALNUM, 36)),
        (
            "GitHub fine-grained token",
            fake(&["github", "_pat_"].concat(), "11AbCdEf_GhIjKl2", 82),
        ),
        ("GitLab token", fake(&["gl", "pat-"].concat(), ALNUM, 20)),
        (
            "Slack token",
            fake(&["xo", "xb-"].concat(), "1234-abcdEFGH", 40),
        ),
        ("Stripe key", fake(&["sk", "_live_"].concat(), ALNUM, 24)),
        (
            "Anthropic key",
            fake(&["sk", "-ant-"].concat(), "api03-AbCdEf_12", 90),
        ),
        (
            "OpenAI project key",
            fake(&["sk", "-proj-"].concat(), ALNUM, 48),
        ),
        ("OpenAI key", fake(&["sk", "-"].concat(), ALNUM, 48)),
        (
            "Google API key",
            fake(&["AI", "za"].concat(), "SyB-4x_Q9", 35),
        ),
        (
            "Recall authkey",
            fake(&["recall", "-ak-"].concat(), ALNUM, 52),
        ),
        (
            "Recall enrolment key",
            fake(&["recall", "-ek-"].concat(), "abc.DEF_12", 43),
        ),
        (
            "Recall recovery key",
            fake(&["recall", "-rk-"].concat(), "abc-DEF_12", 43),
        ),
        (
            "JSON Web Token",
            format!(
                "{}.{}.{}",
                fake(&["ey", "J"].concat(), "hbGciOiJIUzI1NiJ9", 30),
                fake("", "eyJzdWIiOiIxMjM0", 40),
                fake("", "SflKxwRJSMeKKF2QT4", 43)
            ),
        ),
        ("hex secret", fake("", HEX, 40)),
        (
            "OpenAI service account key",
            fake(&["sk", "-svcacct-"].concat(), "Ab3_Cd4-Ef5Gh6", 64),
        ),
        (
            "Slack app token",
            fake(&["xa", "pp-"].concat(), "1-A0123-4567-abcd", 60),
        ),
        ("npm token", fake(&["np", "m_"].concat(), ALNUM, 36)),
        ("Hugging Face token", fake(&["h", "f_"].concat(), ALNUM, 34)),
        (
            "aws_secret_access_key",
            fake("", "wJalrXUtnFEMI/K7MDENG+bPxRfiCY", 40),
        ),
        ("password", "Zq8!wLr2vXp".to_string()),
    ]
}

/// A fake private key's body, as a service account's JSON holds it, on one
/// line.
const SA_KEY_BODY: &str = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC7fakefakefake";

/// A fake key block quoted into a note: each line behind `> `.
fn quoted_key() -> (String, &'static str) {
    let body = "MIIEowIBAAKCAQEAq9quotedfakekeyfakekey0123456789";
    (
        format!(
            "> {}\n> {body}\n> {}\n",
            ["-----BEGIN RSA ", "PRIVATE KEY-----"].concat(),
            ["-----END RSA ", "PRIVATE KEY-----"].concat()
        ),
        body,
    )
}

/// Secrets that sit inside other text on their line: the text the line
/// holds, and the secret in it.
fn planted_in_text() -> Vec<(&'static str, String, String)> {
    let hex = fake("", HEX, 32);
    vec![
        (
            "deploy URL",
            "https://deploy:Tq4wLp9Rz2@db.example.com/app".to_string(),
            "Tq4wLp9Rz2".to_string(),
        ),
        // The first `token` on the line is not the one assigned a secret.
        ("second token", format!("none yet, token = {hex}"), hex),
        (
            "service account",
            format!(
                r#"{{"type": "service_account", "private_key": "{}\n{}\n{}\n"}}"#,
                ["-----BEGIN ", "PRIVATE KEY-----"].concat(),
                SA_KEY_BODY,
                ["-----END ", "PRIVATE KEY-----"].concat()
            ),
            SA_KEY_BODY.to_string(),
        ),
        (
            "config",
            format!(
                "passwd=Hj7#kq2Lx9 and {}{}",
                ["npm", "_"].concat(),
                fake("", ALNUM, 36)
            ),
            "Hj7#kq2Lx9".to_string(),
        ),
    ]
}

// ---------------------------------------------------------------------------
// the properties
// ---------------------------------------------------------------------------

/// A report made from notes holding sentinel strings, one in what each
/// check reports on, carries none of them outside `details`: the findings,
/// serialized as the server receives them, never quote a note. `details`
/// does, which shows the sentinels were where the checks looked.
#[tokio::test]
async fn a_report_carries_no_note_text_outside_details() {
    let token = fake(&["gh", "p_"].concat(), ALNUM, 36);
    let mut old = file(P, "old.md", "Deploy with `make ship SENTINEL-stale`\n");
    old.updated_at = "2025-01-01T00:00:00.000Z".into();
    let files = vec![
        file(
            P,
            "MEMORY.md",
            "- [Gone](gone-SENTINEL-link.md) SENTINEL-index\n- [Old](old.md)\n",
        ),
        file(
            P,
            "a.md",
            "- the staging database listens on 5433 SENTINEL-dup\n\nThe build SENTINEL-contra-a is green.\n",
        ),
        file(P, "b.md", "- the staging database listens on 5433 SENTINEL-dup\n"),
        file(P, "deploy.md", &format!("token for prod: {token} SENTINEL-secret\n")),
        file(
            P,
            "me.md",
            "---\nname: SENTINEL-scope\ntype: user\n---\nPrefers tabs\n",
        ),
        old,
    ];
    let claude = FakeClaude::new(
        r#"{"contradictions":[{"file":"F2","lines":[3,3],"other_file":"F3","other_lines":[1,1],"explanation":"SENTINEL-contra says otherwise"}]}"#,
    );
    let report = evaluate(&input(files, true), &settings(), &claude.merger()).await;

    let kinds: HashSet<&str> = report.findings.iter().map(|f| f.kind.as_str()).collect();
    assert_eq!(kinds, KINDS.into_iter().collect(), "{:#?}", report.findings);
    let findings = serde_json::to_string(&report.findings).unwrap();
    assert!(!findings.contains("SENTINEL"), "{findings}");
    assert!(!findings.contains(&token), "{findings}");
    let details = serde_json::to_string(&report.details).unwrap();
    for sentinel in [
        "SENTINEL-stale",
        "SENTINEL-index",
        "SENTINEL-dup",
        "SENTINEL-secret",
        "SENTINEL-scope",
        "SENTINEL-contra",
    ] {
        assert!(details.contains(sentinel), "{sentinel} is not in {details}");
    }
    // A secret is masked even in the details.
    assert!(!details.contains(&token), "{details}");
}

/// The five checks that read files never call `claude`: a stand-in counts
/// every call, and a run without the contradiction check, over notes that
/// set off all five, makes none.
#[tokio::test]
async fn the_checks_that_read_files_make_no_claude_call() {
    let claude = FakeClaude::new(r#"{"contradictions":[]}"#);
    let mut old = file(P, "old.md", "Run `./scripts/release.sh` to cut one\n");
    old.updated_at = "2024-06-01T00:00:00.000Z".into();
    let files = vec![
        file(P, "MEMORY.md", "- [Gone](gone.md)\n"),
        file(P, "a.md", "- the staging database listens on 5433\n"),
        file(G, "db.md", "- the staging database listens on 5433\n"),
        file(
            P,
            "keys.md",
            &format!("aws: {}\n", fake(&["AK", "IA"].concat(), UPPER, 16)),
        ),
        file(P, "me.md", "---\ntype: user\n---\nI like tabs\n"),
        old,
    ];
    let report = evaluate(&input(files, false), &settings(), &claude.merger()).await;
    let kinds: HashSet<&str> = report.findings.iter().map(|f| f.kind.as_str()).collect();
    assert_eq!(
        kinds,
        HashSet::from([
            KIND_SECRET,
            KIND_DUPLICATE,
            KIND_DEAD_LINK,
            KIND_WRONG_SCOPE,
            KIND_STALE
        ]),
        "{:#?}",
        report.findings
    );
    assert_eq!(claude.calls(), 0, "a check that reads files called claude");
}

/// Every planted token is found, on its own line, and a set of ordinary
/// notes, hashes, commit ids, paths and talk about tokens included, sets
/// off nothing.
#[test]
fn the_secret_check_finds_every_planted_token_and_nothing_in_ordinary_notes() {
    let mut planted = planted();
    let mut content = String::from("# Credentials, to move out of here\n\n");
    for (what, token) in &planted {
        content.push_str(&format!("- {what}: {token}\n"));
    }
    for (what, text, secret) in planted_in_text() {
        content.push_str(&format!("- {what}: {text}\n"));
        planted.push((what, secret));
    }
    content.push_str("- private_key: ");
    content.push_str(&fake("", HEX, 64));
    content.push('\n');
    let key = format!(
        "{}\nMIIEpAIBAAKCAQEA0Z3VS5JJcds3xfn\nzYy8x2Cc\n{}\n",
        ["-----BEGIN RSA ", "PRIVATE KEY-----"].concat(),
        ["-----END RSA ", "PRIVATE KEY-----"].concat()
    );
    content.push_str(&key);
    let (quoted, quoted_body) = quoted_key();
    content.push_str(&quoted);
    let f = file(P, "creds.md", &content);
    let found = secrets(&f);
    let lines: Vec<u32> = found.iter().map(|f| f.lines[0]).collect();
    let expected: Vec<u32> = (3..=planted.len() as u32 + 3).collect();
    assert_eq!(
        &lines[..expected.len()],
        expected.as_slice(),
        "{:#?}",
        found
            .iter()
            .map(|f| (&f.lines, &f.detail.excerpt))
            .collect::<Vec<_>>()
    );
    let blocks: Vec<[u32; 2]> = found[expected.len()..].iter().map(|f| f.lines).collect();
    let first = expected.len() as u32 + 3;
    assert_eq!(
        blocks,
        [[first, first + 3], [first + 4, first + 6]],
        "the key block, and the quoted one"
    );
    for f in &found {
        let detail = serde_json::to_string(&f.detail).unwrap();
        for (_, token) in &planted {
            assert!(!detail.contains(token.as_str()), "{token} kept in {detail}");
        }
        assert!(!detail.contains("MIIEpAIBAAKCAQEA"), "{detail}");
        assert!(!detail.contains(quoted_body), "{detail}");
        let edit = f.detail.suggested_edit.as_ref().unwrap();
        let fixed = edit.apply_to(&content).unwrap();
        let line = fixed.lines().nth(f.lines[0] as usize - 1).unwrap_or("");
        assert!(tokens_in(line).is_empty(), "{line}");
    }

    let ordinary = [
        "# Deploy\n\nThe release is tagged from commit 3f9a1c7e2b8d4f6a0c5e9b1d7f3a2c8e4b6d0f1a.\n",
        "- base_sha256 is e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\n",
        "- the request id looked like 550e8400-e29b-41d4-a716-446655440000\n",
        "- GitHub tokens start with ghp_ and live in 1Password, never here\n",
        "- AWS keys start with AKIA; ours are in the vault\n",
        "- token: kept in 1Password under \"prod\"\n",
        "- password = see the team vault\n",
        "- ASIANA flights are cheaper on Tuesdays\n",
        "- run task-list-refresh before sk-lint, then open https://example.com/sk-docs\n",
        "- recall-ak- keys are made with recall authkey create\n",
        "- -----BEGIN PUBLIC KEY----- blocks are fine to keep\n",
        "- the API lives at /v1/sync and the config at ~/.config/recall/config.toml\n",
        "- colors: #ff8800 and #00aaff; build 20260925.1\n",
        "- eyJ is how a JWT starts; ours are short-lived\n",
        "- the token hash is sha256 and 64 characters long\n",
        "- password: correct horse, see 1Password\n",
        "- password: ${DB_PASSWORD} from the environment\n",
        "- the password: kept in the vault, rotated monthly\n",
        "- clone https://github.com/pimlabs/recall and https://user@example.com/x\n",
        "- postgres://app:${PGPASSWORD}@db:5432/app is the URL shape\n",
        "- hf_hub_download fetches the weights; npm_config_cache is the cache\n",
        "- secret_access_key: in the vault, never here\n",
        // Prose about keys, and lists of their headers, are not keys.
        "A PEM file opens with -----BEGIN PRIVATE KEY-----\nThen comes the base64 body.\n\
         It closes with -----END PRIVATE KEY-----\n",
        "- -----BEGIN RSA PRIVATE KEY-----\n- -----BEGIN EC PRIVATE KEY-----\n",
        "Markers: -----BEGIN PRIVATE KEY----- and -----END PRIVATE KEY----- wrap a key.\n",
    ];
    for note in ordinary {
        let found = secrets(&file(P, "note.md", note));
        assert!(found.is_empty(), "{note:?} was reported: {found:#?}");
    }
}

/// The contradiction check spends the owner's Claude usage, so it runs
/// only when asked for: not at all without it, and once per project with
/// it, the global scope read beside each.
#[tokio::test]
async fn contradictions_run_only_when_asked_for() {
    let files = vec![
        file(P, "a.md", "- deploys go out on Fridays\n"),
        file("acme/web", "a.md", "- deploys never go out on Fridays\n"),
        file(G, "tools.md", "- I deploy on Fridays\n"),
    ];
    let claude = FakeClaude::new(r#"{"contradictions":[]}"#);
    let report = evaluate(&input(files.clone(), false), &settings(), &claude.merger()).await;
    assert_eq!(claude.calls(), 0);
    assert!(report.details.skipped.is_empty());

    let report = evaluate(&input(files, true), &settings(), &claude.merger()).await;
    assert_eq!(
        claude.calls(),
        2,
        "one call per project, none for the global scope alone"
    );
    assert!(
        report.details.skipped.is_empty(),
        "{:?}",
        report.details.skipped
    );
}

// ---------------------------------------------------------------------------
// each check
// ---------------------------------------------------------------------------

#[test]
fn a_duplicate_is_reported_on_the_copy_outside_the_global_scope() {
    let files = vec![
        file(
            P,
            "a.md",
            "# Notes\n\nThe staging database is on port 5433 now.\n\nMore.\n",
        ),
        file(G, "db.md", "the staging  database is on port 5433 now\n"),
        file(
            "acme/web",
            "x.md",
            "- The staging database is on port 5433 now.\n- other\n",
        ),
        file(P, "short.md", "- see above\n"),
        file(P, "short2.md", "- see above\n"),
    ];
    let found = duplicates(&files);
    assert_eq!(found.len(), 2, "{found:#?}");
    for f in &found {
        assert_eq!(f.related, vec![file_ref(&files[1])]);
    }
    let a = found.iter().find(|f| f.file.file_path == "a.md").unwrap();
    assert_eq!(a.lines, [3, 3]);
    let edit = a.detail.suggested_edit.as_ref().unwrap();
    assert_eq!(
        edit.apply_to(&files[0].content).unwrap(),
        "# Notes\n\nMore.\n",
        "the paragraph and the gap after it"
    );
    let x = found.iter().find(|f| f.file.file_path == "x.md").unwrap();
    assert_eq!(
        x.detail
            .suggested_edit
            .as_ref()
            .unwrap()
            .apply_to(&files[2].content)
            .unwrap(),
        "- other\n"
    );
}

#[test]
fn a_link_to_a_file_that_is_not_there_is_dead() {
    let files = vec![
        file(
            P,
            "MEMORY.md",
            "- [A](a.md)\n- [Gone](gone.md)\n- [Editor](global/editor.md)\n\
             - [Lost](global/lost.md)\n- [Site](https://example.com/x.md)\n\
             - [Up](../elsewhere.md)\n- [RAM](machine/ram.md)\n- [Spaced](my%20notes.md#top)\n",
        ),
        file(P, "a.md", "a\n"),
        file(P, "my notes.md", "b\n"),
        file(G, "editor.md", "vim\n"),
    ];
    let found = dead_links(&files);
    let lines: Vec<u32> = found.iter().map(|f| f.lines[0]).collect();
    assert_eq!(lines, [2, 4], "{found:#?}");
    assert!(found.iter().all(|f| f.related.is_empty()));
    let edit = found[0].detail.suggested_edit.as_ref().unwrap();
    assert!(!edit
        .apply_to(&files[0].content)
        .unwrap()
        .contains("gone.md"));
}

#[test]
fn a_project_note_about_the_user_is_in_the_wrong_scope() {
    let user = file(
        P,
        "me.md",
        "---\nname: Me\ntype: \"user\"\n---\nLikes tabs\n",
    );
    let found = wrong_scope(&user);
    assert_eq!(found.len(), 1);
    assert_eq!(found[0].lines, [3, 3]);
    assert!(found[0].detail.reasoning.contains("recall promote me.md"));
    assert!(wrong_scope(&file(G, "me.md", &user.content)).is_empty());
    assert!(wrong_scope(&file(P, "p.md", "---\ntype: project\n---\n")).is_empty());
    assert!(
        wrong_scope(&file(P, "p.md", "type: user\n")).is_empty(),
        "not front matter"
    );
}

#[test]
fn a_file_is_stale_once_old_and_naming_a_path_or_command() {
    let mut old = file(
        P,
        "run.md",
        "# How\n\nThe build is quick.\nRun `make ship`.\n",
    );
    old.updated_at = "2026-01-01T00:00:00.000Z".into();
    let found = stale(&old, &settings());
    assert_eq!(found.len(), 1);
    assert_eq!(found[0].lines, [4, 4]);
    // The report says a note is old; `recall review` says whether it is
    // also wrong, and the finding says where to ask.
    assert!(
        found[0].detail.reasoning.contains("`recall review run`"),
        "{}",
        found[0].detail.reasoning
    );

    let mut fresh = old.clone();
    fresh.updated_at = "2026-09-01T00:00:00.000Z".into();
    assert!(stale(&fresh, &settings()).is_empty(), "too recent");
    let mut prose = file(P, "p.md", "Nothing to run here, just a thought.\n");
    prose.updated_at = old.updated_at.clone();
    assert!(stale(&prose, &settings()).is_empty(), "names nothing");
    let mut path = file(P, "p.md", "Logs are in /var/log/recall\n");
    path.updated_at = old.updated_at.clone();
    assert_eq!(stale(&path, &settings()).len(), 1);
}

#[test]
fn a_report_numbers_its_findings_most_urgent_first() {
    let mut old = file(P, "z.md", "`x` and `y`\n");
    old.updated_at = "2020-01-01T00:00:00.000Z".into();
    let files = vec![
        old,
        file(
            P,
            "k.md",
            &format!("key: {}\n", fake(&["AK", "IA"].concat(), UPPER, 16)),
        ),
    ];
    let redactor = Redactor::new(&files);
    let report = assemble(
        deterministic(&input(files, false), &settings()),
        Vec::new(),
        &redactor,
    );
    let ids: Vec<(&str, &str)> = report
        .findings
        .iter()
        .map(|f| (f.id.as_str(), f.kind.as_str()))
        .collect();
    assert_eq!(ids, [("f1", KIND_SECRET), ("f2", KIND_STALE)]);
    assert_eq!(
        report.details.findings.keys().collect::<Vec<_>>(),
        ["f1", "f2"]
    );
    for f in &report.findings {
        recall_wire::evaluations::check_finding(&serde_json::to_value(f).unwrap()).unwrap();
    }
}

#[tokio::test]
async fn a_contradiction_is_read_from_claudes_answer_and_checked() {
    let files = vec![
        file(P, "a.md", "- deploys go out on Fridays\n- tests first\n"),
        file(G, "tools.md", "- never deploy on a Friday\n"),
        file(G, "other.md", "- one\n- two\n"),
    ];
    // Wrapped in a fence despite the prompt; one claim cites a file that
    // was not handed over, one lines past the end, one two global notes.
    let claude = FakeClaude::new(
        "```json\n{\"contradictions\":[\
         {\"file\":\"F2\",\"lines\":[1,1],\"other_file\":\"F1\",\"other_lines\":[1,1],\"explanation\":\"Fridays\"},\
         {\"file\":\"F9\",\"lines\":[1,1],\"explanation\":\"no such file\"},\
         {\"file\":\"F1\",\"lines\":[5,6],\"explanation\":\"past the end\"},\
         {\"file\":\"F2\",\"lines\":[1,1],\"other_file\":\"F3\",\"other_lines\":[1,2],\"explanation\":\"both global\"}\
         ]}\n```",
    );
    let (found, skipped) = contradictions(
        &input(files.clone(), true),
        &settings(),
        &claude.merger(),
        &Redactor::default(),
    )
    .await;
    assert!(skipped.is_empty(), "{skipped:?}");
    assert_eq!(found.len(), 1, "{found:#?}");
    assert_eq!(found[0].file, file_ref(&files[0]), "on the project's side");
    assert_eq!(found[0].lines, [1, 1]);
    assert_eq!(found[0].related, vec![file_ref(&files[1])]);
    assert!(found[0].detail.excerpt.contains("never deploy"));
}

#[tokio::test]
async fn contradictions_are_skipped_and_said_so_when_they_cannot_run() {
    let files = vec![file(P, "a.md", "- one\n")];
    let claude = FakeClaude::new(r#"{"contradictions":[]}"#);

    let mut s = settings();
    s.cli_unavailable = Some("not logged in".into());
    let (_, skipped) = contradictions(
        &input(files.clone(), true),
        &s,
        &claude.merger(),
        &Redactor::default(),
    )
    .await;
    assert!(skipped[0].reason.contains("not logged in"), "{skipped:?}");

    let mut s = settings();
    s.deadline = Some(Instant::now() + Duration::from_secs(5));
    let (_, skipped) = contradictions(
        &input(files.clone(), true),
        &s,
        &claude.merger(),
        &Redactor::default(),
    )
    .await;
    assert!(skipped[0].reason.contains("lease"), "{skipped:?}");

    let big = vec![file(P, "a.md", &"x".repeat(MAX_PROMPT_BYTES))];
    let (_, skipped) = contradictions(
        &input(big, true),
        &settings(),
        &claude.merger(),
        &Redactor::default(),
    )
    .await;
    assert!(skipped[0].reason.contains("bytes"), "{skipped:?}");

    let junk = FakeClaude::new("I found nothing worth mentioning.");
    let (_, skipped) = contradictions(
        &input(files, true),
        &settings(),
        &junk.merger(),
        &Redactor::default(),
    )
    .await;
    assert!(skipped[0].reason.contains("not the JSON"), "{skipped:?}");
    assert_eq!(claude.calls(), 0);
}

/// A secret is masked wherever a report quotes it, whichever check does:
/// a token or a private key that is also in a duplicated paragraph, a
/// stale note, a note in the wrong scope, a dead link's line, or what
/// `claude` says of a contradiction appears nowhere in the report, the
/// suggested edits included.
#[tokio::test]
async fn a_secret_is_masked_wherever_a_report_quotes_it() {
    let token = fake(&["gh", "p_"].concat(), ALNUM, 36);
    let aws = fake(&["AK", "IA"].concat(), UPPER, 16);
    let key_line = "MIIEpAIBAAKCAQEA0Z3VS5JJcds3xfnzYy8x2Cc";
    let key = format!(
        "{}\n{key_line}\n{}\n",
        ["-----BEGIN RSA ", "PRIVATE KEY-----"].concat(),
        ["-----END RSA ", "PRIVATE KEY-----"].concat()
    );
    let sa = format!(
        r#"{{"private_key": "{}\n{SA_KEY_BODY}\n{}\n"}}"#,
        ["-----BEGIN ", "PRIVATE KEY-----"].concat(),
        ["-----END ", "PRIVATE KEY-----"].concat()
    );
    let (quoted, quoted_body) = quoted_key();
    let mut old = file(
        P,
        "old.md",
        &format!("Deploy with `make ship TOKEN={token}`\n\n```\n{key}```\n"),
    );
    old.updated_at = "2025-01-01T00:00:00.000Z".into();
    let files = vec![
        file(P, "MEMORY.md", &format!("- [Gone](gone.md) uses {token}\n")),
        file(
            P,
            "a.md",
            &format!("- the deploy token is {token} for now\n\n{sa}\n\n{quoted}"),
        ),
        file(
            P,
            "b.md",
            &format!("- the deploy token is {token} for now\n\n{sa}\n\n{quoted}"),
        ),
        file(
            P,
            "me.md",
            &format!("---\nname: me {aws}\ntype: user\n---\nPrefers tabs\n"),
        ),
        old,
    ];
    let claude = FakeClaude::new(&format!(
        r#"{{"contradictions":[{{"file":"F2","lines":[1,1],"other_file":"F3","other_lines":[1,1],"explanation":"both say {token} and {key_line}"}}]}}"#
    ));
    let report = evaluate(&input(files, true), &settings(), &claude.merger()).await;
    let kinds: HashSet<&str> = report.findings.iter().map(|f| f.kind.as_str()).collect();
    for kind in [
        KIND_SECRET,
        KIND_DUPLICATE,
        KIND_STALE,
        KIND_WRONG_SCOPE,
        KIND_DEAD_LINK,
        KIND_CONTRADICTION,
    ] {
        assert!(kinds.contains(kind), "no {kind} in {:#?}", report.findings);
    }
    let all = format!(
        "{}{}",
        serde_json::to_string(&report.findings).unwrap(),
        serde_json::to_string(&report.details).unwrap()
    );
    for secret in [
        token.as_str(),
        aws.as_str(),
        key_line,
        SA_KEY_BODY,
        quoted_body,
    ] {
        assert!(!all.contains(secret), "{secret} is in {all}");
    }
    assert!(all.contains("masked"), "{all}");
}

/// The scan is linear: lines built to make it read the same bytes over and
/// over (a prefix repeated inside what it allows, a name repeated with no
/// value, `](` with no end) take no longer than any others of their length.
/// Before, 400 KB of `.recall-ek-` took 14 seconds.
#[test]
fn a_line_built_to_slow_the_scan_does_not() {
    let started = Instant::now();
    for unit in [
        [".recall", "-ek-"].concat(),
        ["recall", "-ak-"].concat(),
        "eyJ".to_string(),
        "token".to_string(),
        "password ".to_string(),
        "://a".to_string(),
        ["sk", "-"].concat(),
    ] {
        let line = unit.repeat(400_000 / unit.len());
        tokens_in(&line);
    }
    let index = file(P, "MEMORY.md", &"](".repeat(200_000));
    dead_links(std::slice::from_ref(&index));
    assert!(
        started.elapsed() < Duration::from_secs(5),
        "took {:?}",
        started.elapsed()
    );
}

/// However much a report finds, its details stay under
/// `MAX_DETAILS_BYTES`, each excerpt under `MAX_EXCERPT_BYTES`, and what
/// was cut or left out is said: a report always fits in a result the
/// server takes.
#[test]
fn a_report_is_held_to_a_size_the_server_takes() {
    let paragraphs: String = (0..600)
        .map(|i| {
            format!(
                "Paragraph {i}: {}\n\n",
                "a long note about the deploy ".repeat(180)
            )
        })
        .collect();
    let files = vec![file(P, "a.md", &paragraphs), file(P, "b.md", &paragraphs)];
    let redactor = Redactor::new(&files);
    let report = assemble(
        deterministic(&input(files, false), &settings()),
        Vec::new(),
        &redactor,
    );
    assert_eq!(report.findings.len(), 600);
    let details = serde_json::to_string(&report.details).unwrap();
    assert!(
        details.len() <= MAX_DETAILS_BYTES + 4096,
        "{} bytes of details",
        details.len()
    );
    assert!(report
        .details
        .findings
        .values()
        .all(|d| d.excerpt.len() <= MAX_EXCERPT_BYTES + 64));
    let skipped: Vec<&str> = report
        .details
        .skipped
        .iter()
        .map(|s| s.reason.as_str())
        .collect();
    assert!(
        skipped.iter().any(|r| r.contains("have no details")),
        "{skipped:?}"
    );
    assert!(
        skipped.iter().any(|r| r.contains("were cut")),
        "{skipped:?}"
    );
    let result = recall_wire::ResultRequest {
        lease_id: "lse".into(),
        merge: None,
        error: None,
        evaluate: Some(recall_wire::EvaluateResult {
            findings: report.findings,
            details: serde_json::to_value(&report.details).unwrap(),
        }),
    };
    assert!(serde_json::to_vec(&result).unwrap().len() < 5 << 20);
}

/// `claude` is handed no secret: the notes are masked before the prompt is
/// built, so however it words or splits what it read (here, reversed), its
/// answer carries none of them.
#[tokio::test]
async fn claude_is_handed_no_secret() {
    let token = fake(&["gh", "p_"].concat(), ALNUM, 36);
    let aws_secret = fake("", "wJalrXUtnFEMI/K7MDENG+bPxRfiCY", 40);
    let files = vec![
        file(
            P,
            "a.md",
            &format!(
                "- token {token}\n- aws_secret_access_key = {aws_secret}\n- password: Zq8!wLr2vXp\n\
                 - {{\"private_key\": \"{}\\n{SA_KEY_BODY}\\n{}\\n\"}}\n",
                ["-----BEGIN ", "PRIVATE KEY-----"].concat(),
                ["-----END ", "PRIVATE KEY-----"].concat()
            ),
        ),
        file(G, "tools.md", "- I use vim\n"),
    ];
    let claude = FakeClaude::echoing();
    let report = evaluate(&input(files, true), &settings(), &claude.merger()).await;
    assert_eq!(claude.calls(), 1);
    let all = format!(
        "{}{}",
        serde_json::to_string(&report.findings).unwrap(),
        serde_json::to_string(&report.details).unwrap()
    );
    // What it was handed did come back, reversed, in its explanation.
    assert!(
        all.contains("I use vim".chars().rev().collect::<String>().as_str()),
        "{all}"
    );
    for secret in [
        token.as_str(),
        aws_secret.as_str(),
        "Zq8!wLr2vXp",
        SA_KEY_BODY,
    ] {
        let kept: String = secret
            .chars()
            .filter(|c| c.is_ascii_alphanumeric() || "_ .:=/+-".contains(*c))
            .collect();
        let reversed: String = kept.chars().rev().collect();
        assert!(!all.contains(&reversed), "{secret}, reversed, is in {all}");
        assert!(!all.contains(secret), "{secret} is in {all}");
    }
}

/// A suggested edit is written into a note as it stands, so it is never
/// masked: one whose text holds something masked as a secret elsewhere is
/// left out, and the report says so. Here `release-2024x` is a password in
/// one note and a branch name in another.
#[test]
fn an_edit_that_would_write_a_mask_is_left_out() {
    let token = fake(&["gh", "p_"].concat(), ALNUM, 36);
    let files = vec![
        file(P, "a.md", "password: release-2024x\n"),
        file(P, "b.md", &format!("branch release-2024x uses {token}\n")),
    ];
    let report = assemble(
        deterministic(&input(files.clone(), false), &settings()),
        Vec::new(),
        &Redactor::new(&files),
    );
    let edit_of = |path: &str| {
        let f = report
            .findings
            .iter()
            .find(|f| f.file_path == path && f.kind == KIND_SECRET)
            .unwrap();
        report.details.findings[&f.id].suggested_edit.clone()
    };
    assert_eq!(
        edit_of("a.md").unwrap().replacement,
        "password: [removed: see recall eval]\n"
    );
    assert_eq!(edit_of("b.md"), None, "an edit that would write a mask");
    assert!(report
        .details
        .skipped
        .iter()
        .any(|s| s.reason.contains("holds something masked")));
    for d in report.details.findings.values() {
        if let Some(e) = &d.suggested_edit {
            assert!(!e.replacement.contains("masked"), "{e:?}");
        }
    }
}

/// A mask shows a public prefix only for a kind of token that has one;
/// a password, an AWS secret key, a hex secret, a URL's password and a
/// private key are masked from their first character.
#[test]
fn a_mask_shows_only_a_public_prefix() {
    let ghp = fake(&["gh", "p_"].concat(), ALNUM, 36);
    assert!(mask(&ghp, "GitHub token").starts_with("ghp_…"));
    let proj = fake(&["sk", "-proj-"].concat(), ALNUM, 48);
    assert!(mask(&proj, "OpenAI API key").starts_with("sk-proj-…"));
    for (value, what) in [
        (
            fake("", "wJalrXUtnFEMI/K7MDENG+bPxRfiCY", 40),
            "AWS secret access key",
        ),
        ("Zq8!wLr2vXpQq8!wLr2vXpQq8!w".to_string(), "password"),
        (fake("", HEX, 64), "secret value"),
        (
            "Tq4wLp9Rz2Tq4wLp9Rz2Tq4wLp9Rz2".to_string(),
            "password in a URL",
        ),
        (SA_KEY_BODY.to_string(), "private key"),
    ] {
        let masked = mask(&value, what);
        assert!(!masked.contains(&value[..4]), "{masked} shows {value}");
        assert!(masked.contains("masked"), "{masked}");
    }
}

/// Masking the notes for the contradiction prompts is linear in them,
/// however many secrets they hold, and each file is masked once a run,
/// though the global scope is in every project's prompt. A project too
/// large for one call is skipped before it is masked at all.
#[tokio::test]
async fn masking_the_prompts_is_linear_and_once_per_file() {
    let global: String = (0..4_000)
        .map(|i| format!("- password: Gx{i:05}!kq\n"))
        .collect();
    let small = "- deploys go out on Fridays\n";
    let big: String = (0..30_000)
        .map(|i| format!("- password: Bq{i:05}#zz\n"))
        .collect();
    let mut files = vec![file(G, "creds.md", &global)];
    for n in 0..40 {
        files.push(file(&format!("acme/p{n:02}"), "a.md", small));
    }
    files.push(file("acme/big", "a.md", &big));
    let claude = FakeClaude::new(r#"{"contradictions":[]}"#);

    let started = Instant::now();
    let redactor = Redactor::new(&files);
    let (_, skipped) = contradictions(
        &input(files.clone(), true),
        &settings(),
        &claude.merger(),
        &redactor,
    )
    .await;
    let took = started.elapsed();
    assert!(took < Duration::from_secs(10), "took {took:?}");
    assert_eq!(claude.calls(), 40);
    assert!(
        skipped
            .iter()
            .any(|s| s.project_key == "acme/big" && s.reason.contains("more than one call")),
        "{skipped:?}"
    );
    assert_eq!(
        redactor
            .prompt_bytes
            .load(std::sync::atomic::Ordering::Relaxed),
        global.len() + 40 * small.len(),
        "each file masked once, and the one too large not at all"
    );
}