agent-hub-runtime 0.6.0

AgentHub desktop and companion CLI runtime for Tauri applications
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
use super::*;

// ── redact_url ──

#[test]
fn redact_url_with_credentials() {
    assert_eq!(
        redact_url("https://user:token@github.com/acme/repo.git"),
        "https://***@github.com/acme/repo.git"
    );
}

#[test]
fn redact_url_with_token_only() {
    assert_eq!(
        redact_url("https://ghp_abc123@github.com/acme/repo.git"),
        "https://***@github.com/acme/repo.git"
    );
}

#[test]
fn redact_url_no_credentials_unchanged() {
    assert_eq!(
        redact_url("https://github.com/acme/repo.git"),
        "https://github.com/acme/repo.git"
    );
}

#[test]
fn redact_url_not_a_url_unchanged() {
    assert_eq!(redact_url("just-a-string"), "just-a-string");
}

#[test]
fn redact_url_ssh_no_scheme_unchanged() {
    assert_eq!(
        redact_url("git@github.com:acme/repo.git"),
        "git@github.com:acme/repo.git"
    );
}

// ── build_auto_commit_message ──

#[test]
fn auto_msg_single_skill() {
    let porcelain = " M skills/my-tool/SKILL.md\n";
    assert_eq!(build_auto_commit_message(porcelain), "auto backup: my-tool");
}

#[test]
fn auto_msg_multiple_skills_sorted() {
    let porcelain = " M skills/zeta/SKILL.md\n M skills/alpha/notes.md\n";
    assert_eq!(
        build_auto_commit_message(porcelain),
        "auto backup: alpha, zeta"
    );
}

#[test]
fn auto_msg_skill_plus_metadata() {
    let porcelain = " M skills/demo/SKILL.md\n M .agent-hub/state.json\n";
    assert_eq!(
        build_auto_commit_message(porcelain),
        "auto backup: demo, metadata"
    );
}

#[test]
fn auto_msg_automation_uses_task_name_instead_of_metadata() {
    let porcelain = " M automations/daily-review/config/task.json\n M automations/daily-review/scripts/run.sh\n";
    assert_eq!(
        build_auto_commit_message(porcelain),
        "auto backup: automation/daily-review"
    );
}

#[test]
fn auto_msg_automation_plus_metadata() {
    let porcelain = " M automations/daily-review/config/task.json\n M .agent-hub/state.json\n";
    assert_eq!(
        build_auto_commit_message(porcelain),
        "auto backup: automation/daily-review, metadata"
    );
}

#[test]
fn auto_msg_overflow_truncates() {
    let porcelain = (1..=7)
        .map(|i| format!(" M skills/s{i}/SKILL.md"))
        .collect::<Vec<_>>()
        .join("\n");
    assert_eq!(
        build_auto_commit_message(&porcelain),
        "auto backup: s1, s2, s3, s4, s5 (+2 more)"
    );
}

#[test]
fn auto_msg_metadata_only() {
    let porcelain = " M .agent-hub/state.json\n";
    assert_eq!(
        build_auto_commit_message(porcelain),
        "auto backup: metadata"
    );
}

#[test]
fn auto_msg_empty_porcelain_fallback() {
    assert_eq!(build_auto_commit_message(""), "auto backup");
}

#[test]
fn auto_msg_added_and_deleted() {
    let porcelain = "?? skills/new-one/SKILL.md\n D skills/old/SKILL.md\n";
    assert_eq!(
        build_auto_commit_message(porcelain),
        "auto backup: new-one, old"
    );
}

#[test]
fn auto_msg_rename_arrow() {
    let porcelain = "R  skills/old-name/SKILL.md -> skills/new-name/SKILL.md\n";
    assert_eq!(
        build_auto_commit_message(porcelain),
        "auto backup: new-name"
    );
}

#[test]
fn auto_msg_dot_prefix_skill_excluded() {
    let porcelain = " M skills/.hidden/data.json\n M skills/visible/SKILL.md\n";
    assert_eq!(
        build_auto_commit_message(porcelain),
        "auto backup: visible, metadata"
    );
}

// ── redact_urls_in_text ──

#[test]
fn redact_urls_in_text_mixed_content() {
    let input = "failed to push to https://user:pass@github.com/repo.git (error)";
    let result = redact_urls_in_text(input);
    assert!(result.contains("***@github.com/repo.git"));
    assert!(!result.contains("user:pass"));
}

#[test]
fn redact_urls_in_text_no_urls() {
    assert_eq!(redact_urls_in_text("plain text here"), "plain text here");
}

// ── device naming (§4.3: device name = commit author) ──

#[test]
fn sanitize_device_name_strips_ident_breakers_and_collapses_whitespace() {
    assert_eq!(sanitize_device_name("  MacBook   Pro  "), "MacBook Pro");
    assert_eq!(sanitize_device_name("evil<\n>name"), "evilname");
    assert_eq!(sanitize_device_name("公司 Windows"), "公司 Windows");
    assert_eq!(sanitize_device_name("<>"), "");
    assert_eq!(
        sanitize_device_name("a".repeat(100).as_str())
            .chars()
            .count(),
        64
    );
}

#[test]
fn device_email_slugs_name_with_fallback() {
    assert_eq!(device_email("MacBook Pro"), "macbook-pro@agent-hub.local");
    assert_eq!(device_email("公司 Windows"), "windows@agent-hub.local");
    assert_eq!(device_email("公司"), "device@agent-hub.local");
}

#[test]
fn default_device_name_is_never_empty() {
    assert!(!default_device_name().is_empty());
}

// ── ensure_gitignore / compiled-Python artifacts ──

/// `ensure_gitignore` is append-only, and other code leans on that: it must
/// add what is missing without reordering, rewriting, or dropping whatever
/// the user put there themselves.
#[test]
fn ensure_gitignore_appends_missing_entries_and_leaves_user_lines_alone() {
    let tmp = tempfile::tempdir().unwrap();
    let dir = tmp.path();
    let gitignore = dir.join(".gitignore");
    std::fs::write(&gitignore, "# my own notes\nsecret-notes/\n*.tmp\n").unwrap();

    ensure_gitignore(dir).unwrap();
    let lines: Vec<String> = std::fs::read_to_string(&gitignore)
        .unwrap()
        .lines()
        .map(ToOwned::to_owned)
        .collect();

    // The user's lines survive verbatim, in their original order, at the top.
    assert_eq!(&lines[..3], &["# my own notes", "secret-notes/", "*.tmp"]);
    // The already-present entry is not duplicated.
    assert_eq!(lines.iter().filter(|l| *l == "*.tmp").count(), 1);
    for required in [
        ".DS_Store",
        "Thumbs.db",
        ".agent-hub.lock",
        "__pycache__/",
        "*.pyc",
    ] {
        assert!(lines.iter().any(|l| l == required), "missing {required}");
    }

    // Running it again changes nothing at all.
    let before = std::fs::read_to_string(&gitignore).unwrap();
    ensure_gitignore(dir).unwrap();
    assert_eq!(std::fs::read_to_string(&gitignore).unwrap(), before);
}

/// Ignoring a pattern does nothing to paths already in the index, so a
/// library that has been backing up `__pycache__` needs an explicit
/// catch-up — otherwise `git add -A` keeps re-staging them forever.
#[test]
fn compiled_python_artifacts_already_committed_are_untracked() {
    let tmp = tempfile::tempdir().unwrap();
    let dir = tmp.path();
    init_repo_unlocked(dir, "Test Device").unwrap();

    // Simulate a library from before the ignore rule existed: force the
    // artifacts in past the .gitignore, exactly as history would have them.
    let cache = dir.join("docx/scripts/__pycache__");
    std::fs::create_dir_all(&cache).unwrap();
    std::fs::write(cache.join("helpers.cpython-311.pyc"), b"\x00bytecode").unwrap();
    std::fs::write(dir.join("docx/scripts/helpers.py"), "print('hi')").unwrap();
    // A real Windows extension module must NOT be swept up with them.
    std::fs::write(dir.join("docx/scripts/_native.pyd"), b"\x00ext").unwrap();
    run_git_checked(dir, &["add", "-A", "--force"]).unwrap();
    run_git_checked(dir, &["commit", "-m", "before the ignore rule"]).unwrap();
    assert!(is_tracked(
        dir,
        "docx/scripts/__pycache__/helpers.cpython-311.pyc"
    ));

    commit_all_unlocked(dir, "backup").unwrap();

    assert!(
        !is_tracked(dir, "docx/scripts/__pycache__/helpers.cpython-311.pyc"),
        "compiled artifacts must stop being tracked"
    );
    // Untracked, not deleted: they are still on disk for the interpreter.
    assert!(cache.join("helpers.cpython-311.pyc").is_file());
    // Real content is untouched — including `.pyd`, which `content_hash`
    // counts as part of the skill.
    assert!(is_tracked(dir, "docx/scripts/helpers.py"));
    assert!(is_tracked(dir, "docx/scripts/_native.pyd"));

    // Idempotent: a second round finds nothing left to do.
    std::fs::write(dir.join("note.md"), "x").unwrap();
    commit_all_unlocked(dir, "backup again").unwrap();
    assert!(is_tracked(dir, "docx/scripts/helpers.py"));
    assert!(is_tracked(dir, "docx/scripts/_native.pyd"));
}

#[test]
fn init_repo_commits_as_device_and_identity_follows_rename() {
    let tmp = tempfile::tempdir().unwrap();
    let dir = tmp.path();
    // Init must succeed and author the initial commit as the device,
    // regardless of any global git identity on this machine.
    init_repo_unlocked(dir, "MacBook Pro").unwrap();
    assert_eq!(
        run_git(dir, &["log", "-1", "--format=%an"]).unwrap(),
        "MacBook Pro"
    );
    assert_eq!(
        run_git(dir, &["config", "--local", "--get", "user.email"]).unwrap(),
        "macbook-pro@agent-hub.local"
    );

    // Renaming the device only affects commits made afterwards.
    configure_device_identity(dir, "公司 Windows").unwrap();
    std::fs::write(dir.join("note.md"), "x").unwrap();
    commit_all_unlocked(dir, "backup").unwrap();
    assert_eq!(
        run_git(dir, &["log", "-1", "--format=%an"]).unwrap(),
        "公司 Windows"
    );

    // And the snapshot history exposes the author per entry.
    let tag = create_snapshot_tag_unlocked(dir).unwrap();
    let versions = list_snapshot_versions(dir, None).unwrap();
    let entry = versions.iter().find(|v| v.tag == tag).unwrap();
    assert_eq!(entry.author, "公司 Windows");
    assert_eq!(entry.message, "backup");
    assert!(!entry.committed_at.is_empty());
}

#[test]
fn configure_device_identity_noop_without_repo_or_name() {
    let tmp = tempfile::tempdir().unwrap();
    // Not a repo: must not fail (and must not create one).
    configure_device_identity(tmp.path(), "MacBook Pro").unwrap();
    assert!(!tmp.path().join(".git").exists());
    // Empty name on a real repo: leaves identity untouched.
    init_repo_unlocked(tmp.path(), "Device A").unwrap();
    configure_device_identity(tmp.path(), "  ").unwrap();
    assert_eq!(
        run_git(tmp.path(), &["config", "--local", "--get", "user.name"]).unwrap(),
        "Device A"
    );
}

// ── oversized skill exclusion (§3.6 后半) ──

#[test]
fn oversized_untracked_skill_is_excluded_but_tracked_one_is_not() {
    let tmp = tempfile::tempdir().unwrap();
    let dir = tmp.path();
    init_repo_unlocked(dir, "Device A").unwrap();

    // A skill committed while small stays tracked even after growing
    // past the limit — untracking would propagate as a deletion.
    std::fs::create_dir_all(dir.join("grown")).unwrap();
    std::fs::write(dir.join("grown/SKILL.md"), "small at first").unwrap();
    commit_all_unlocked(dir, "seed").unwrap();
    std::fs::write(dir.join("grown/data.bin"), vec![0u8; 64]).unwrap();

    // A brand-new oversized skill (limit shrunk for the test) with its
    // metadata file.
    std::fs::create_dir_all(dir.join("huge")).unwrap();
    std::fs::write(dir.join("huge/SKILL.md"), vec![b'x'; 64]).unwrap();
    let meta_dir = dir.join(".agent-hub/skills");
    std::fs::create_dir_all(&meta_dir).unwrap();
    std::fs::write(
            meta_dir.join("skill-huge.json"),
            br#"{"schema_version":1,"skill_id":"skill-huge","path":"huge","path_key":"huge","enabled":true,"tags":[],"source":{"type":"import","ref":null,"subpath":null,"branch":null}}"#,
        )
        .unwrap();

    let excluded = apply_oversized_exclusions(dir, 32).unwrap();
    assert_eq!(excluded, vec!["huge"]);
    let gitignore = std::fs::read_to_string(dir.join(".gitignore")).unwrap();
    assert!(gitignore.contains("/huge/"), "{gitignore}");
    assert!(
        gitignore.contains("/.agent-hub/skills/skill-huge.json"),
        "{gitignore}"
    );
    assert!(
        !gitignore.contains("/grown/"),
        "tracked skill must not be excluded: {gitignore}"
    );

    // Committing keeps the oversized skill (and its metadata) out of the
    // tree while the grown-but-tracked one stays in.
    std::fs::write(dir.join("note.md"), "trigger commit").unwrap();
    // commit_all uses the real 100MB limit, so re-apply the test limit
    // before checking what got committed.
    apply_oversized_exclusions(dir, 32).unwrap();
    run_git_checked(dir, &["add", "-A"]).unwrap();
    run_git_checked(dir, &["commit", "-m", "test"]).unwrap();
    assert!(run_git(dir, &["cat-file", "-e", "HEAD:huge/SKILL.md"]).is_err());
    assert!(run_git(
        dir,
        &["cat-file", "-e", "HEAD:.agent-hub/skills/skill-huge.json"]
    )
    .is_err());
    run_git(dir, &["cat-file", "-e", "HEAD:grown/data.bin"]).unwrap();
    // Local files are untouched.
    assert!(dir.join("huge/SKILL.md").exists());

    // Idempotent, and the section self-heals once the skill shrinks.
    apply_oversized_exclusions(dir, 32).unwrap();
    let same = std::fs::read_to_string(dir.join(".gitignore")).unwrap();
    assert_eq!(gitignore, same);
    std::fs::write(dir.join("huge/SKILL.md"), "tiny").unwrap();
    apply_oversized_exclusions(dir, 32).unwrap();
    let after = std::fs::read_to_string(dir.join(".gitignore")).unwrap();
    assert!(
        !after.contains("/huge/"),
        "shrunk skill re-enters backup: {after}"
    );
    assert!(
        !after.contains("oversized"),
        "empty section is removed: {after}"
    );
}

#[test]
fn size_report_marks_untracked_oversized_as_excluded() {
    let tmp = tempfile::tempdir().unwrap();
    let dir = tmp.path();
    init_repo_unlocked(dir, "Device A").unwrap();
    std::fs::create_dir_all(dir.join("big")).unwrap();
    std::fs::write(dir.join("big/SKILL.md"), vec![b'x'; 200]).unwrap();
    // The public report uses the real 100MB limit; exercise the flag via
    // the internal helper plus a handcrafted report path instead of
    // writing 100MB in a test: tracked → not excluded, untracked → excluded.
    assert!(!is_tracked(dir, "big"));
    run_git_checked(dir, &["add", "-A"]).unwrap();
    run_git_checked(dir, &["commit", "-m", "track big"]).unwrap();
    assert!(is_tracked(dir, "big"));
}

// ── app_commit protocol markers (§6) ──

#[test]
fn app_commits_carry_protocol_marker_and_trailer() {
    let tmp = tempfile::tempdir().unwrap();
    let dir = tmp.path();

    // init: initial commit has protocol.json in tree + trailer in message.
    init_repo_unlocked(dir, "Device A").unwrap();
    let body = run_git(dir, &["log", "-1", "--format=%B"]).unwrap();
    assert!(protocol::has_protocol_trailer(&body), "init: {body}");
    run_git(dir, &["cat-file", "-e", "HEAD:.agent-hub/protocol.json"]).unwrap();

    // A pre-protocol snapshot: simulate by committing a tree with the
    // marker removed, tagging it, then restoring it.
    let snapshot_tag = create_snapshot_tag_unlocked(dir).unwrap();
    std::fs::write(dir.join("note.md"), "x").unwrap();
    commit_all_unlocked(dir, "backup").unwrap();
    let body = run_git(dir, &["log", "-1", "--format=%B"]).unwrap();
    assert!(protocol::has_protocol_trailer(&body), "commit_all: {body}");

    // Restore an old snapshot (which does carry protocol.json since init
    // wrote it) — restore commit must carry the trailer too.
    let safety = restore_snapshot_version_unlocked(dir, &snapshot_tag).unwrap();
    assert!(safety.starts_with("sm-v-"));
    let body = run_git(dir, &["log", "-1", "--format=%B"]).unwrap();
    assert!(protocol::has_protocol_trailer(&body), "restore: {body}");
    run_git(dir, &["cat-file", "-e", "HEAD:.agent-hub/protocol.json"]).unwrap();
}

#[test]
fn restore_of_pre_protocol_snapshot_self_heals_marker() {
    let tmp = tempfile::tempdir().unwrap();
    let dir = tmp.path();
    // Build a repo whose first snapshot predates the protocol marker.
    let git = |args: &[&str]| {
        let out = Command::new("git")
            .arg("-C")
            .arg(dir)
            .args(["-c", "user.email=t@e.c", "-c", "user.name=T"])
            .args(args)
            .output()
            .unwrap();
        assert!(
            out.status.success(),
            "git {args:?}: {}",
            String::from_utf8_lossy(&out.stderr)
        );
    };
    git(&["init", "-b", "main"]);
    // Persist the identity into repo-local config so the production commit
    // path (commit_all_unlocked, plain run_git) has an author on CI runners
    // that lack a global git identity — the transient `-c` flags above only
    // apply to this closure's own invocations.
    git(&["config", "user.email", "t@e.c"]);
    git(&["config", "user.name", "T"]);
    std::fs::create_dir_all(dir.join("skill-a")).unwrap();
    std::fs::write(dir.join("skill-a/SKILL.md"), "v1").unwrap();
    git(&["add", "-A"]);
    git(&["commit", "-m", "pre-protocol"]);
    let old_tag = create_snapshot_tag_unlocked(dir).unwrap();

    // A protocol-era commit follows.
    std::fs::write(dir.join("skill-a/SKILL.md"), "v2").unwrap();
    commit_all_unlocked(dir, "backup").unwrap();
    run_git(dir, &["cat-file", "-e", "HEAD:.agent-hub/protocol.json"]).unwrap();

    // Restoring the pre-protocol snapshot must not resurrect a
    // marker-less tree: the restore commit re-adds protocol.json (sticky).
    restore_snapshot_version_unlocked(dir, &old_tag).unwrap();
    assert_eq!(
        std::fs::read_to_string(dir.join("skill-a/SKILL.md")).unwrap(),
        "v1"
    );
    run_git(dir, &["cat-file", "-e", "HEAD:.agent-hub/protocol.json"]).unwrap();
    let body = run_git(dir, &["log", "-1", "--format=%B"]).unwrap();
    assert!(protocol::has_protocol_trailer(&body), "restore: {body}");
}

// ── parse_restored_from_tag_message ──

#[test]
fn parse_restored_tag_valid() {
    let msg = "restore: switch skills library to sm-v-20260318-153012-abc1234";
    assert_eq!(
        parse_restored_from_tag_message(msg).as_deref(),
        Some("sm-v-20260318-153012-abc1234")
    );
}

#[test]
fn parse_restored_tag_invalid_prefix() {
    assert_eq!(
        parse_restored_from_tag_message("some other commit message"),
        None
    );
}

#[test]
fn parse_restored_tag_non_snapshot_tag() {
    let msg = "restore: switch skills library to v1.0.0";
    assert_eq!(parse_restored_from_tag_message(msg), None);
}

#[test]
fn clone_into_unlocked_failure_includes_git_stderr() {
    let tmp = tempfile::tempdir().unwrap();
    let target = tmp.path().join("clone-target");
    // file:// URL pointing at a non-existent path -> git clone fails with
    // a deterministic stderr message we can pattern-match on.
    let bogus_src = tmp.path().join("does-not-exist.git");
    let url = format!("file://{}", bogus_src.display());

    let err = clone_into_unlocked(&target, &url).unwrap_err();
    let msg = format!("{err:#}");
    assert!(
        msg.contains("git clone failed"),
        "expected git stderr to be surfaced, got: {msg}"
    );
    assert!(
        !msg.eq("Failed to clone repository"),
        "error must not be the old generic placeholder"
    );
}

#[test]
fn clone_into_unlocked_failure_redacts_credentials_in_url() {
    let tmp = tempfile::tempdir().unwrap();
    let target = tmp.path().join("clone-target");
    // Unreachable host with a token-bearing URL. git's stderr typically
    // echoes the URL back; the error must not leak the token.
    let url = "https://ghp_supersecrettoken123@127.0.0.1:1/does-not-exist.git";

    let err = clone_into_unlocked(&target, url).unwrap_err();
    let msg = format!("{err:#}");
    assert!(
        !msg.contains("ghp_supersecrettoken123"),
        "credential must not leak into error message: {msg}"
    );
}

#[test]
fn parse_restored_tag_with_trailing_whitespace() {
    let msg = "restore: switch skills library to sm-v-20260318-153012-abc1234  ";
    assert_eq!(
        parse_restored_from_tag_message(msg).as_deref(),
        Some("sm-v-20260318-153012-abc1234")
    );
}

// ── #244 acceptance: status stable across a simulated restart ──

#[test]
fn status_reports_in_sync_after_backup_cycle_and_restart() {
    let tmp = tempfile::tempdir().unwrap();
    let remote = tmp.path().join("remote.git");
    let work = tmp.path().join("work");
    std::fs::create_dir_all(&work).unwrap();

    assert!(Command::new("git")
        .args(["init", "--bare", "--initial-branch=main"])
        .arg(&remote)
        .output()
        .unwrap()
        .status
        .success());

    let git = |args: &[&str]| {
        let out = Command::new("git")
            .arg("-C")
            .arg(&work)
            .args(args)
            .output()
            .unwrap();
        assert!(
            out.status.success(),
            "git {args:?} failed: {}",
            String::from_utf8_lossy(&out.stderr)
        );
    };
    git(&["init", "-b", "main"]);
    git(&["config", "user.email", "test@example.com"]);
    git(&["config", "user.name", "Test"]);
    git(&["config", "push.autoSetupRemote", "false"]);

    // Full backup cycle: add a skill, commit, snapshot, push.
    std::fs::create_dir_all(work.join("skill-a")).unwrap();
    std::fs::write(work.join("skill-a/SKILL.md"), "content").unwrap();
    commit_all_unlocked(&work, "backup").unwrap();
    set_remote_unlocked(&work, remote.to_str().unwrap()).unwrap();
    let tag = create_snapshot_tag_unlocked(&work).unwrap();
    push_unlocked(&work).unwrap();

    // Simulated restart: a fresh fetch + status read (what the app does
    // on launch) must still say "in sync" — not pending, not divergent.
    fetch_remote(&work).unwrap();
    let status = get_status(&work).unwrap();
    assert!(status.is_repo);
    assert_eq!(status.upstream_health, "healthy");
    assert!(!status.has_changes, "no pending changes after clean sync");
    assert_eq!(status.changed_skill_count, 0);
    assert_eq!((status.ahead, status.behind), (0, 0));
    assert_eq!(status.current_snapshot_tag.as_deref(), Some(tag.as_str()));
}

// ── restore safety point ──

#[test]
fn a_reftable_library_is_refused_with_a_migration_hint() {
    let tmp = tempfile::tempdir().unwrap();
    let out = Command::new("git")
        .args(["init", "--ref-format=reftable"])
        .arg(tmp.path())
        .output()
        .unwrap();
    if !out.status.success() {
        eprintln!("skipping: this git cannot create reftable repositories");
        return;
    }
    let err = ensure_repo(tmp.path()).unwrap_err().to_string();
    assert!(err.contains("refs migrate --ref-format=files"), "{err}");
    assert!(ensure_no_interrupted_git_operation(tmp.path()).is_err());
}

#[test]
#[ignore = "mutates GIT_DEFAULT_REF_FORMAT; run alone"]
fn init_creates_a_files_repo_even_when_git_defaults_to_reftable() {
    let tmp = tempfile::tempdir().unwrap();
    std::env::set_var("GIT_DEFAULT_REF_FORMAT", "reftable");
    let result = init_repo_unlocked(tmp.path(), "test-device");
    std::env::remove_var("GIT_DEFAULT_REF_FORMAT");
    result.unwrap();
    assert!(!tmp.path().join(".git/reftable").exists());
    ensure_repo(tmp.path()).unwrap();
}

#[test]
fn restore_creates_safety_point_capturing_dirty_tree() {
    let tmp = tempfile::tempdir().unwrap();
    let dir = tmp.path();
    let git = |args: &[&str]| {
        let out = Command::new("git")
            .arg("-C")
            .arg(dir)
            .args(args)
            .output()
            .unwrap();
        assert!(
            out.status.success(),
            "git {args:?} failed: {}",
            String::from_utf8_lossy(&out.stderr)
        );
    };
    git(&["init", "-b", "main"]);
    git(&["config", "user.email", "test@example.com"]);
    git(&["config", "user.name", "Test"]);

    std::fs::create_dir_all(dir.join("skill-a")).unwrap();
    std::fs::write(dir.join("skill-a/SKILL.md"), "v1").unwrap();
    git(&["add", "-A"]);
    git(&["commit", "-m", "v1"]);
    let old_tag = create_snapshot_tag_unlocked(dir).unwrap();

    std::fs::write(dir.join("skill-a/SKILL.md"), "v2").unwrap();
    git(&["add", "-A"]);
    git(&["commit", "-m", "v2"]);
    // Uncommitted edit on top — must survive inside the safety point.
    std::fs::write(dir.join("skill-a/SKILL.md"), "v3-dirty").unwrap();

    let safety_tag = restore_snapshot_version_unlocked(dir, &old_tag).unwrap();

    // Working tree is back at v1.
    assert_eq!(
        std::fs::read_to_string(dir.join("skill-a/SKILL.md")).unwrap(),
        "v1"
    );
    // The safety point is a persistent user-visible snapshot of the
    // pre-restore state, including the dirty edit.
    assert!(safety_tag.starts_with("sm-v-"));
    let captured = run_git(dir, &["show", &format!("{safety_tag}:skill-a/SKILL.md")]).unwrap();
    assert_eq!(captured, "v3-dirty");
    // And it shows up in the snapshot history for one-click undo.
    let versions = list_snapshot_versions(dir, None).unwrap();
    assert!(versions.iter().any(|v| v.tag == safety_tag));
}

// ── ensure_clean_clone_target ──
// Tested directly (without RepoLock) so cases run in parallel without
// serializing on the process-wide clone lock.

#[test]
fn ensure_clean_clone_target_allows_nonexistent_path() {
    let tmp = tempfile::tempdir().unwrap();
    let target = tmp.path().join("not-yet");
    ensure_clean_clone_target(&target).unwrap();
}

#[test]
fn ensure_clean_clone_target_allows_empty_directory() {
    let tmp = tempfile::tempdir().unwrap();
    let target = tmp.path().join("empty");
    std::fs::create_dir_all(&target).unwrap();
    ensure_clean_clone_target(&target).unwrap();
}

#[test]
fn ensure_clean_clone_target_allows_existing_git_repo() {
    let tmp = tempfile::tempdir().unwrap();
    let target = tmp.path().join("existing-repo");
    std::fs::create_dir_all(target.join(".git")).unwrap();
    std::fs::write(target.join("README"), b"x").unwrap();
    // Existing .git is delegated to clone_into_unlocked's own rejection.
    ensure_clean_clone_target(&target).unwrap();
}

#[test]
fn ensure_clean_clone_target_refuses_non_empty_non_git() {
    let tmp = tempfile::tempdir().unwrap();
    let target = tmp.path().join("populated");
    std::fs::create_dir_all(&target).unwrap();
    std::fs::write(target.join("user-file.txt"), b"important").unwrap();

    let err = ensure_clean_clone_target(&target).unwrap_err();
    let msg = format!("{err:#}");
    assert!(
        msg.contains("non-empty") && msg.contains("not a git repo"),
        "unexpected message: {msg}"
    );
    // Crucial: the user file must still be there.
    assert!(target.join("user-file.txt").exists());
}

// ── first push to an empty remote (the no_upstream scenario) ──

// ── remove_remote ──
// Tested via the unlocked inner so cases run in parallel without
// serializing on the process-wide repo lock.

#[test]
fn remove_remote_deletes_origin_and_is_idempotent() {
    let tmp = tempfile::tempdir().unwrap();
    let dir = tmp.path();
    assert!(Command::new("git")
        .arg("-C")
        .arg(dir)
        .arg("init")
        .output()
        .unwrap()
        .status
        .success());
    run_git_checked(
        dir,
        &[
            "remote",
            "add",
            "origin",
            "https://github.com/acme/repo.git",
        ],
    )
    .unwrap();

    remove_remote_unlocked(dir).unwrap();
    assert!(run_git(dir, &["remote", "get-url", "origin"]).is_err());

    // A second disconnect (no origin left) must still succeed.
    remove_remote_unlocked(dir).unwrap();
}

#[test]
fn remove_remote_on_non_repo_is_ok() {
    let tmp = tempfile::tempdir().unwrap();
    remove_remote_unlocked(tmp.path()).unwrap();
}

#[test]
fn push_first_time_to_empty_remote_with_no_upstream() {
    let tmp = tempfile::tempdir().unwrap();
    let remote = tmp.path().join("remote.git");
    let work = tmp.path().join("work");
    std::fs::create_dir_all(&work).unwrap();

    // Empty bare remote — the freshly-created GitHub/Gitee repo case.
    assert!(Command::new("git")
        .args(["init", "--bare"])
        .arg(&remote)
        .output()
        .unwrap()
        .status
        .success());

    // Local repo with one commit on main. Identity is injected explicitly so
    // the test does not depend on a global git identity (CI runners have none).
    let git = |args: &[&str]| {
        Command::new("git")
            .arg("-C")
            .arg(&work)
            .args(["-c", "user.email=test@example.com", "-c", "user.name=Test"])
            .args(args)
            .output()
            .unwrap()
    };
    assert!(git(&["init"]).status.success());
    assert!(git(&["checkout", "-b", "main"]).status.success());
    // Pin autoSetupRemote off in the repo's local config so the plain `git push`
    // deterministically fails and we actually exercise the `-u` fallback, even on
    // a dev box whose global config sets push.autoSetupRemote=true.
    assert!(git(&["config", "push.autoSetupRemote", "false"])
        .status
        .success());
    std::fs::write(work.join("a.txt"), "hello").unwrap();
    assert!(git(&["add", "-A"]).status.success());
    assert!(git(&["commit", "-m", "initial"]).status.success());

    // Wiring the empty remote leaves no tracking branch — exactly the state
    // that made the UI report "Up to date" while the remote stayed empty.
    set_remote_unlocked(&work, remote.to_str().unwrap()).unwrap();
    assert_eq!(detect_upstream_health(&work, true), "no_upstream");
    assert_eq!(get_ahead_behind(&work).unwrap_or((0, 0)), (0, 0));

    // The capability the frontend fix relies on: push must set upstream and
    // actually populate the remote, not no-op.
    push_unlocked(&work).unwrap();

    let remote_main = Command::new("git")
        .arg("-C")
        .arg(&remote)
        .args(["rev-parse", "main"])
        .output()
        .unwrap();
    assert!(
        remote_main.status.success(),
        "remote should have branch main after first push"
    );
    assert_eq!(detect_upstream_health(&work, true), "healthy");
}

#[test]
fn prune_hidden_refs_removes_remote_copies_and_keeps_local_refs() {
    let tmp = tempfile::tempdir().unwrap();
    let remote = tmp.path().join("remote.git");
    let work = tmp.path().join("work");
    std::fs::create_dir_all(&work).unwrap();
    assert!(Command::new("git")
        .args(["init", "--bare", "--initial-branch=main"])
        .arg(&remote)
        .output()
        .unwrap()
        .status
        .success());

    init_repo_unlocked(&work, "Device A").unwrap();
    set_remote_unlocked(&work, remote.to_str().unwrap()).unwrap();
    push_unlocked(&work).unwrap();

    // Simulate a mirror-style push leaking hidden refs to the remote,
    // plus a functional local ref that must survive.
    run_git_checked(
        &work,
        &["update-ref", "refs/agent-hub/conflict/skill-x", "HEAD"],
    )
    .unwrap();
    run_git_checked(&work, &["update-ref", "refs/agent-hub/pre-merge", "HEAD"]).unwrap();
    run_git_checked(
        &work,
        &[
            "push",
            "origin",
            "refs/agent-hub/conflict/skill-x",
            "refs/agent-hub/pre-merge",
        ],
    )
    .unwrap();
    let leaked = run_git(&work, &["ls-remote", "origin", "refs/agent-hub/*"]).unwrap();
    assert_eq!(
        leaked.lines().count(),
        2,
        "setup: refs must be on the remote"
    );

    let removed = prune_hidden_refs_on_remote(&work).unwrap();
    assert_eq!(removed, 2);
    let after = run_git(&work, &["ls-remote", "origin", "refs/agent-hub/*"]).unwrap();
    assert!(
        after.trim().is_empty(),
        "remote hidden refs must be gone: {after}"
    );
    // Branch and local functional refs are untouched.
    run_git(&work, &["rev-parse", "refs/agent-hub/conflict/skill-x"]).unwrap();
    run_git(&work, &["rev-parse", "refs/agent-hub/pre-merge"]).unwrap();
    let heads = run_git(&work, &["ls-remote", "--heads", "origin"]).unwrap();
    assert!(heads.contains("refs/heads/main"));

    // Idempotent: nothing left to remove.
    assert_eq!(prune_hidden_refs_on_remote(&work).unwrap(), 0);
}

#[test]
fn pull_conflict_aborts_merge_and_reports_sync_conflict() {
    let tmp = tempfile::tempdir().unwrap();
    let remote = tmp.path().join("remote.git");
    let a = tmp.path().join("a");
    let b = tmp.path().join("b");
    std::fs::create_dir_all(&a).unwrap();

    // Pin the bare repo's HEAD to main: without a global
    // init.defaultBranch (CI runners have none) it points at master,
    // the later clone finds a dangling HEAD and checks nothing out,
    // and machine B's `commit -am` fails on an unborn branch.
    assert!(Command::new("git")
        .args(["init", "--bare", "--initial-branch=main"])
        .arg(&remote)
        .output()
        .unwrap()
        .status
        .success());

    let git = |dir: &Path, args: &[&str]| {
        Command::new("git")
            .arg("-C")
            .arg(dir)
            .args(["-c", "user.email=test@example.com", "-c", "user.name=Test"])
            .args(args)
            .output()
            .unwrap()
    };

    // Machine A: seed one skill file and publish it.
    assert!(git(&a, &["init"]).status.success());
    assert!(git(&a, &["checkout", "-b", "main"]).status.success());
    std::fs::write(a.join("skill.md"), "base\n").unwrap();
    assert!(git(&a, &["add", "-A"]).status.success());
    assert!(git(&a, &["commit", "-m", "base"]).status.success());
    assert!(
        git(&a, &["remote", "add", "origin", remote.to_str().unwrap()])
            .status
            .success()
    );
    assert!(git(&a, &["push", "-u", "origin", "main"]).status.success());

    // Machine B: clone, then both sides edit the SAME line differently.
    assert!(Command::new("git")
        .args(["clone", remote.to_str().unwrap()])
        .arg(&b)
        .output()
        .unwrap()
        .status
        .success());
    assert!(git(&b, &["config", "user.email", "test@example.com"])
        .status
        .success());
    assert!(git(&b, &["config", "user.name", "Test"]).status.success());

    std::fs::write(a.join("skill.md"), "edited on A\n").unwrap();
    assert!(git(&a, &["commit", "-am", "edit A"]).status.success());
    assert!(git(&a, &["push", "origin", "main"]).status.success());

    std::fs::write(b.join("skill.md"), "edited on B\n").unwrap();
    assert!(git(&b, &["commit", "-am", "edit B"]).status.success());

    // B pulls A's conflicting change. The merge must fail, be aborted, and
    // surface a recognizable conflict error — not leave B wedged.
    let err = pull_unlocked(&b).unwrap_err();
    let msg = format!("{err:#}");
    assert!(
        msg.contains("SYNC_CONFLICT"),
        "expected a recognizable conflict error, got: {msg}"
    );

    // Crucial: the abort must clear the in-progress merge so future syncs
    // are not permanently blocked.
    assert!(
        !b.join(".git/MERGE_HEAD").exists(),
        "MERGE_HEAD should be gone after abort"
    );
    let porcelain = run_git(&b, &["status", "--porcelain"]).unwrap();
    assert!(
        porcelain.is_empty(),
        "working tree should be clean after abort, got: {porcelain:?}"
    );
    // And a follow-up sync is no longer blocked by an interrupted operation.
    ensure_no_interrupted_git_operation(&b).unwrap();
}

#[test]
fn ensure_clean_clone_target_refuses_file() {
    let tmp = tempfile::tempdir().unwrap();
    let target = tmp.path().join("a-file");
    std::fs::write(&target, b"x").unwrap();

    let err = ensure_clean_clone_target(&target).unwrap_err();
    let msg = format!("{err:#}");
    assert!(
        msg.contains("file, not a directory"),
        "unexpected message: {msg}"
    );
}

#[test]
fn auto_msg_user_configuration_is_distinct_from_skills_and_metadata() {
    assert_eq!(
        build_auto_commit_message(" M user-config/agent-launch.json\n"),
        "auto backup: user-config/agent-launch"
    );
    assert_eq!(
        build_auto_commit_message(
            " M skills/demo/SKILL.md\n M user-config/agent-launch.json\n M .agent-hub/state.json\n"
        ),
        "auto backup: demo, user-config/agent-launch, metadata"
    );
}