crosslink 0.9.0-beta.1

A synced issue tracker CLI for multi-agent AI development
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
use anyhow::{bail, Context, Result};
use std::path::Path;
use std::process::Command;

use crate::identity::{AgentConfig, AgentRole};
use crate::signing;
use crate::sync;
use crate::utils::format_issue_id;
use crate::AgentCommands;

pub fn run(command: AgentCommands, crosslink_dir: &Path) -> Result<()> {
    match command {
        AgentCommands::Init {
            agent_id,
            description,
            no_key,
            force,
            role,
        } => {
            let parsed_role = match role.as_deref() {
                Some("driver") => AgentRole::Driver,
                Some("agent") | None => AgentRole::Agent,
                Some(other) => bail!("invalid role {other:?}; expected 'driver' or 'agent'"),
            };
            init(
                crosslink_dir,
                &agent_id,
                description.as_deref(),
                no_key,
                force,
                parsed_role,
            )
        }
        AgentCommands::Status => status(crosslink_dir),
        AgentCommands::Prompt {
            session,
            message,
            no_submit,
        } => prompt(&session, &message, !no_submit),
        AgentCommands::Bootstrap {
            repo,
            identity,
            branch,
            description,
            no_key,
            target,
        } => {
            let target_path = std::path::PathBuf::from(&target);
            bootstrap(
                &target_path,
                &repo,
                &identity,
                branch.as_deref(),
                description.as_deref(),
                no_key,
            )?;
            // The agent's v3 ref is created on its first hub write (heartbeat /
            // mutation), so there is no separate agent-directory bootstrap step
            // anymore (the v2 `agents/<id>/` write path was removed in 754b).
            Ok(())
        }
        AgentCommands::Request {
            target,
            kind,
            subject_issue,
            reason,
        } => request(
            crosslink_dir,
            &target,
            &kind,
            subject_issue,
            reason.as_deref(),
        ),
        AgentCommands::Requests { target, pending } => {
            list_requests(crosslink_dir, target.as_deref(), pending)
        }
        AgentCommands::PollRequests => poll_requests(crosslink_dir),
        AgentCommands::Flags { strict } => {
            show_flags(crosslink_dir, strict);
            Ok(())
        }
    }
}

/// `crosslink agent flags [--strict]`
///
/// Reports whether `paused` / `kill` / `reprioritise` flags are set
/// in `.crosslink/agent-flags/`. With `--strict`, exits 2 when either
/// `paused` or `kill` is active so `PreToolUse` hooks can block tool
/// invocation cleanly.
fn show_flags(crosslink_dir: &Path, strict: bool) {
    let paused = crate::agent_flags::is_paused(crosslink_dir);
    let kill = crate::agent_flags::should_exit(crosslink_dir);
    let hint = crate::agent_flags::read_reprioritise_hint(crosslink_dir)
        .ok()
        .flatten();

    let json = serde_json::json!({
        "paused": paused,
        "kill": kill,
        "reprioritise": hint.as_ref().map(|h| serde_json::json!({
            "issue_id": h.issue_id,
            "from_request_id": h.from_request_id,
        })),
    });
    println!("{json}");

    if strict && (paused || kill) {
        // Exit code 2 = "blocked by control flag". Distinguishes from
        // genuine errors (1) so hooks can pattern-match cleanly.
        std::process::exit(2);
    }
}

/// `crosslink agent poll-requests`
///
/// Scans the hub cache for pending requests targeted at this agent,
/// applies each one's local flag (pause / resume / kill / reprioritise),
/// and writes an ack to the hub so drivers see the outcome. Idempotent
/// — already-acked requests are skipped.
fn poll_requests(crosslink_dir: &Path) -> Result<()> {
    let writer = crate::shared_writer::SharedWriter::new(crosslink_dir)?.ok_or_else(|| {
        anyhow::anyhow!(
            "agent poll-requests requires shared-writer mode (run `crosslink agent init` first)"
        )
    })?;
    let agent = AgentConfig::load(crosslink_dir)?
        .ok_or_else(|| anyhow::anyhow!("no agent config; run `crosslink agent init`"))?;

    let result =
        crate::agent_requests::poll::process_pending(&writer, crosslink_dir, &agent.agent_id)?;

    if result.acted.is_empty() && result.skipped_existing_ack == 0 {
        println!("No pending requests for {}.", agent.agent_id);
        return Ok(());
    }
    for a in &result.acted {
        println!(
            "{}  {:?}  acted={}  {}  ({:?})",
            a.request_id, a.kind, a.acted, a.result, a.push_outcome
        );
    }
    if result.skipped_existing_ack > 0 {
        println!(
            "Skipped {} already-acked request(s).",
            result.skipped_existing_ack
        );
    }
    Ok(())
}

/// `crosslink agent request <target> <kind> [--subject-issue N] [--reason "..."]`
///
/// Writes a signed JSON file to `agents/<target>/requests/<ulid>.json`
/// on `crosslink/hub`. The target agent's polling loop picks it up and
/// executes the action (design doc §9).
fn request(
    crosslink_dir: &Path,
    target: &str,
    kind_str: &str,
    subject_issue: Option<i64>,
    reason: Option<&str>,
) -> Result<()> {
    let kind = crate::agent_requests::RequestKind::parse(kind_str)?;

    let writer = crate::shared_writer::SharedWriter::new(crosslink_dir)?.ok_or_else(|| {
        anyhow::anyhow!(
            "agent request requires hub access — run `crosslink sync` to initialize, or `crosslink agent init` for a full agent identity"
        )
    })?;

    // Drivers (operators) may not have run `crosslink agent init`;
    // they have a `user.signingkey` in git but no agent.json. Fall
    // back to that fingerprint so the dashboard can send requests
    // without forcing operators through agent setup.
    let requested_by = if let Some(driver) = AgentConfig::load(crosslink_dir)? {
        driver
            .ssh_fingerprint
            .clone()
            .unwrap_or_else(|| format!("agent:{}", driver.agent_id))
    } else {
        let workspace_root = crosslink_dir.parent().unwrap_or(crosslink_dir);
        resolve_driver_signing_key(workspace_root).unwrap_or_else(|| "driver:unknown".to_string())
    };

    let req = crate::agent_requests::AgentRequest {
        request_id: crate::agent_requests::new_request_id(),
        kind,
        subject: crate::agent_requests::RequestSubject {
            issue_id: subject_issue,
        },
        requested_by,
        requested_at: chrono::Utc::now().to_rfc3339(),
        reason: reason.map(str::to_string),
    };

    let outcome = writer.write_agent_request(target, &req)?;
    match outcome {
        crate::shared_writer::PushOutcome::Pushed => {
            println!(
                "Request {} ({}) sent to {}.",
                req.request_id, kind_str, target
            );
        }
        crate::shared_writer::PushOutcome::LocalOnly => {
            println!(
                "Request {} ({}) saved locally for {} (push deferred — offline or contested hub).",
                req.request_id, kind_str, target
            );
        }
    }
    Ok(())
}

/// Read `user.signingkey` from the workspace's git config and turn it
/// into a fingerprint. Used as a fallback identity when no agent.json
/// exists. Returns `None` if the config is unset or the key file
/// can't be fingerprinted.
fn resolve_driver_signing_key(workspace_root: &Path) -> Option<String> {
    let out = Command::new("git")
        .arg("-C")
        .arg(workspace_root)
        .args(["config", "user.signingkey"])
        .output()
        .ok()?;
    if !out.status.success() {
        return None;
    }
    let raw = String::from_utf8_lossy(&out.stdout).trim().to_string();
    if raw.is_empty() {
        return None;
    }
    // user.signingkey can be either a raw fingerprint or a path to a
    // public/private key file. If it's a path we fingerprint it; if
    // not we just record what's there as the principal.
    let path = std::path::Path::new(&raw);
    if path.exists() {
        let pub_path = if path
            .extension()
            .is_some_and(|e| e.eq_ignore_ascii_case("pub"))
        {
            path.to_path_buf()
        } else {
            std::path::PathBuf::from(format!("{raw}.pub"))
        };
        if pub_path.exists() {
            if let Ok(fp) = signing::get_key_fingerprint(&pub_path) {
                return Some(fp);
            }
        }
    }
    Some(raw)
}

/// `crosslink agent requests [--target <id>] [--pending]`
fn list_requests(crosslink_dir: &Path, target: Option<&str>, pending_only: bool) -> Result<()> {
    let sync = sync::SyncManager::new(crosslink_dir)?;
    let cache = sync.cache_path();
    if !cache.exists() {
        println!("No hub cache present (nothing synced yet).");
        return Ok(());
    }

    let agents_dir = cache.join("agents");
    if !agents_dir.exists() {
        println!("No agents on hub yet.");
        return Ok(());
    }

    let agent_ids: Vec<String> = if let Some(t) = target {
        vec![t.to_string()]
    } else {
        std::fs::read_dir(&agents_dir)
            .with_context(|| format!("read_dir {}", agents_dir.display()))?
            .filter_map(std::result::Result::ok)
            .filter(|e| e.path().is_dir())
            .filter_map(|e| e.file_name().to_str().map(str::to_string))
            .collect()
    };

    let mut total = 0;
    for agent_id in agent_ids {
        let entries = crate::agent_requests::scan(cache, &agent_id)?;
        for row in entries {
            if pending_only && row.ack.is_some() {
                continue;
            }
            total += 1;
            let status = row
                .ack
                .as_ref()
                .map_or_else(|| "pending".to_string(), |a| format!("acked: {}", a.result));
            let subject = row
                .request
                .subject
                .issue_id
                .map_or_else(|| "-".to_string(), format_issue_id);
            println!(
                "{agent_id}  {}  {:?}  subject={subject}  [{status}]  by {}",
                row.request.request_id, row.request.kind, row.request.requested_by
            );
        }
    }

    if total == 0 {
        println!("No requests found.");
    }
    Ok(())
}

/// `crosslink agent init <agent-id> [-d "description"] [--no-key] [--force] [--role driver|agent]`
pub fn init(
    crosslink_dir: &Path,
    agent_id: &str,
    description: Option<&str>,
    no_key: bool,
    force: bool,
    role: AgentRole,
) -> Result<()> {
    match AgentConfig::load(crosslink_dir) {
        Ok(Some(_)) if force => {
            println!("Warning: Overwriting existing agent configuration (--force).");
        }
        Ok(Some(_)) => {
            bail!("Agent already configured. Use --force to overwrite, or delete .crosslink/agent.json to reconfigure.");
        }
        Ok(None) => {} // No existing config, proceed normally
        Err(e) => {
            println!(
                "Warning: Existing agent.json is malformed ({e}). Overwriting with new config."
            );
        }
    }
    // Role drives downstream signing: `Driver` makes hub commits
    // sign with the main repo's `user.signingkey` (the human's
    // GitHub-registered key); `Agent` makes them sign with this
    // agent's own key (subagent worktree attribution). See #718.
    let mut config = AgentConfig::init_with_role(crosslink_dir, agent_id, description, role)?;

    // Generate SSH key unless opted out
    if !no_key {
        // GH#610: store keys under the main repo's `.crosslink/keys/` so
        // they outlive `git worktree remove` of an agent worktree. The
        // worktree-scoped `user.signingkey` on the hub-cache (which is
        // also rooted in the main repo) then references a stable path.
        let host_crosslink = signing::host_crosslink_dir(crosslink_dir);
        let keys_dir = host_crosslink.join("keys");
        match signing::generate_agent_key(&keys_dir, agent_id, &config.machine_id) {
            Ok(keypair) => {
                // Store relative path from .crosslink/
                let rel_path = format!("keys/{agent_id}_ed25519");
                config.ssh_key_path = Some(rel_path);
                config.ssh_fingerprint = Some(keypair.fingerprint.clone());
                config.ssh_public_key = Some(keypair.public_key.clone());

                // Re-write agent.json with key info
                let path = crosslink_dir.join("agent.json");
                let json = serde_json::to_string_pretty(&config)?;
                std::fs::write(&path, json)?;

                println!("  SSH key: configured (commit signing enabled)");

                // Publish public key to hub for driver approval
                if let Err(e) =
                    super::trust::publish_agent_key(crosslink_dir, agent_id, &keypair.public_key)
                {
                    println!("  Note: Could not publish key to hub: {e}");
                    println!("  Key will be auto-published on next `crosslink sync`.");
                }

                // Configure signing on the hub cache worktree
                if let Ok(sync) = crate::sync::SyncManager::new(crosslink_dir) {
                    if let Err(e) = sync.configure_signing(crosslink_dir) {
                        tracing::warn!(
                            "could not configure commit signing: {e} — commits will be unsigned"
                        );
                    }
                }
            }
            Err(e) => {
                // On --force re-runs, key-gen errors with "already exists".
                // Reuse the on-disk key; otherwise agent.json loses
                // ssh_key_path and shared_writer silently disables signing.
                // GH#610: prefer the host-side keys dir (where new agents
                // store their keys) and fall back to the worktree-local
                // path for legacy agents whose keys predate the relocation.
                let rel_path = format!("keys/{agent_id}_ed25519");
                let host_private = host_crosslink.join(&rel_path);
                let host_public = host_crosslink.join(format!("keys/{agent_id}_ed25519.pub"));
                let (private_path, public_path) = if host_private.exists() && host_public.exists() {
                    (host_private, host_public)
                } else {
                    (
                        crosslink_dir.join(&rel_path),
                        crosslink_dir.join(format!("keys/{agent_id}_ed25519.pub")),
                    )
                };
                if private_path.exists() && public_path.exists() {
                    if let (Ok(fp), Ok(pub_key)) = (
                        signing::get_key_fingerprint(&public_path),
                        signing::read_public_key(&public_path),
                    ) {
                        config.ssh_key_path = Some(rel_path);
                        config.ssh_fingerprint = Some(fp);
                        config.ssh_public_key = Some(pub_key);
                        let path = crosslink_dir.join("agent.json");
                        if let Ok(json) = serde_json::to_string_pretty(&config) {
                            let _ = std::fs::write(&path, json);
                            println!("  SSH key: reused existing key (commit signing enabled)");
                        }
                    } else {
                        println!("  Warning: Could not reuse existing SSH key: {e}");
                        println!("  Signing will be unavailable. Use --no-key to suppress.");
                    }
                } else {
                    println!("  Warning: Could not generate SSH key: {e}");
                    println!(
                        "  Signing will be unavailable. Use --no-key to suppress this warning."
                    );
                }
            }
        }
    }

    // Re-run `configure_signing` unconditionally so that even in the
    // key-already-existed path, or the `--no-key` path, the hub-cache
    // worktree picks up the role-appropriate signing key (#718). The
    // helper is idempotent and role-aware — driver workspaces get
    // the human's GitHub-registered key, agent worktrees get the
    // agent's own key.
    if let Ok(sync) = crate::sync::SyncManager::new(crosslink_dir) {
        if let Err(e) = sync.configure_signing(crosslink_dir) {
            tracing::warn!("could not (re)configure commit signing after agent init: {e}");
        }
    }

    println!("Agent initialized:");
    println!("  ID:      {}", config.agent_id);
    println!("  Machine: {}", config.machine_id);
    if let Some(desc) = &config.description {
        println!("  Description: {desc}");
    }
    if config.ssh_fingerprint.is_some() {
        println!("  Signing: enabled");
    }
    println!();
    println!("Ask your driver to approve this agent with `crosslink trust approve {agent_id}`");
    Ok(())
}

/// Send a prompt to a running tmux-based agent session.
///
/// Uses `tmux load-buffer` + `paste-buffer` instead of raw `send-keys` to
/// avoid newline interpretation, length limits, and shell escaping issues (#503).
fn prompt(session: &str, message: &str, submit: bool) -> Result<()> {
    // Verify the tmux session exists
    let check = Command::new("tmux")
        .args(["has-session", "-t", session])
        .output()
        .context("tmux not found — is it installed?")?;

    if !check.status.success() {
        bail!("tmux session '{session}' not found. Check `tmux list-sessions`.");
    }

    // Write prompt to a temp file to avoid shell escaping issues
    let tmp = std::env::temp_dir().join(format!("crosslink-prompt-{}", std::process::id()));
    std::fs::write(&tmp, message).context("Failed to write prompt to temp file")?;

    // Load the file into a tmux buffer
    let load = Command::new("tmux")
        .args([
            "load-buffer",
            "-b",
            "crosslink-prompt",
            &tmp.to_string_lossy(),
        ])
        .output()
        .context("Failed to load tmux buffer")?;

    // Clean up temp file regardless of outcome
    let _ = std::fs::remove_file(&tmp);

    if !load.status.success() {
        let stderr = String::from_utf8_lossy(&load.stderr);
        bail!("tmux load-buffer failed: {}", stderr.trim());
    }

    // Paste the buffer into the target session
    let paste = Command::new("tmux")
        .args(["paste-buffer", "-b", "crosslink-prompt", "-t", session])
        .output()
        .context("Failed to paste tmux buffer")?;

    if !paste.status.success() {
        let stderr = String::from_utf8_lossy(&paste.stderr);
        bail!("tmux paste-buffer failed: {}", stderr.trim());
    }

    // Delete the named buffer
    let _ = Command::new("tmux")
        .args(["delete-buffer", "-b", "crosslink-prompt"])
        .output();

    // Optionally press Enter to submit
    if submit {
        let enter = Command::new("tmux")
            .args(["send-keys", "-t", session, "Enter"])
            .output()
            .context("Failed to send Enter key")?;

        if !enter.status.success() {
            let stderr = String::from_utf8_lossy(&enter.stderr);
            bail!("tmux send-keys Enter failed: {}", stderr.trim());
        }
    }

    println!("Prompt sent to session '{session}'");
    Ok(())
}

/// `crosslink agent bootstrap <target-dir> <repo-url> <agent-id> [--branch <branch>] [-d "description"] [--no-key]`
///
/// Enables a container agent to join an existing crosslink coordination hub by
/// cloning a repository, initializing an agent identity, and registering on
/// the hub branch.
pub fn bootstrap(
    target_dir: &Path,
    repo_url: &str,
    agent_id: &str,
    branch: Option<&str>,
    description: Option<&str>,
    no_key: bool,
) -> Result<()> {
    // Step 1: Clone or verify repo
    if !target_dir.exists()
        || target_dir
            .read_dir()
            .map_or(true, |mut d| d.next().is_none())
    {
        let output = Command::new("git")
            .args(["clone", "--depth", "1", repo_url])
            .arg(target_dir)
            .output()
            .context("Failed to run git clone")?;
        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr);
            bail!("git clone failed: {}", stderr.trim());
        }
    } else {
        // Verify the directory is a git repo with matching remote
        let output = Command::new("git")
            .current_dir(target_dir)
            .args(["remote", "get-url", "origin"])
            .output()
            .context("Failed to check git remote")?;
        if !output.status.success() {
            bail!(
                "Directory '{}' exists but is not a git repository with an origin remote",
                target_dir.display()
            );
        }
        let existing_url = String::from_utf8_lossy(&output.stdout).trim().to_string();
        if existing_url != repo_url {
            bail!("Remote mismatch: existing origin is '{existing_url}', expected '{repo_url}'");
        }
    }

    // Step 2: Checkout branch if specified
    if let Some(br) = branch {
        let output = Command::new("git")
            .current_dir(target_dir)
            .args(["checkout", br])
            .output()
            .context("Failed to run git checkout")?;
        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr);
            bail!("git checkout '{}' failed: {}", br, stderr.trim());
        }
    }

    // Step 3: Find or create .crosslink
    let crosslink_dir = target_dir.join(".crosslink");
    std::fs::create_dir_all(&crosslink_dir).context("Failed to create .crosslink directory")?;

    // Step 4: Initialize agent identity (skip if already exists).
    // Bootstrap provisions an autonomous agent — tag role accordingly (GH #566).
    // If a driver-typed identity already exists (e.g. from a prior `crosslink
    // init` in this repo), promote it to `agent` in place so hooks treat the
    // bootstrapped workspace correctly.
    match AgentConfig::load(&crosslink_dir)? {
        Some(existing) if existing.role == AgentRole::Agent => {
            println!("Agent already configured in this repo, skipping identity init.");
        }
        Some(mut existing) => {
            println!(
                "Promoting existing identity '{}' to agent role.",
                existing.agent_id
            );
            existing.role = AgentRole::Agent;
            let path = crosslink_dir.join("agent.json");
            let json = serde_json::to_string_pretty(&existing)?;
            std::fs::write(&path, json)?;
        }
        None => {
            AgentConfig::init_with_role(&crosslink_dir, agent_id, description, AgentRole::Agent)?;
        }
    }

    let mut config = AgentConfig::load(&crosslink_dir)?
        .ok_or_else(|| anyhow::anyhow!("Failed to load agent config after init"))?;

    // Step 5: Generate SSH key unless opted out
    if !no_key && config.ssh_key_path.is_none() {
        // GH#610: see `init` above — keys live under the main repo's
        // `.crosslink/keys/` so they survive agent-worktree cleanup.
        let keys_dir = signing::host_crosslink_dir(&crosslink_dir).join("keys");
        match signing::generate_agent_key(&keys_dir, agent_id, &config.machine_id) {
            Ok(keypair) => {
                let rel_path = format!("keys/{agent_id}_ed25519");
                config.ssh_key_path = Some(rel_path);
                config.ssh_fingerprint = Some(keypair.fingerprint);
                config.ssh_public_key = Some(keypair.public_key);

                // Re-write agent.json with key info
                let path = crosslink_dir.join("agent.json");
                let json = serde_json::to_string_pretty(&config)?;
                std::fs::write(&path, json)?;
            }
            Err(e) => {
                println!("  Warning: Could not generate SSH key: {e}");
                println!("  Signing will be unavailable.");
            }
        }
    }

    // Step 6: Initialize hub cache
    let sync = crate::sync::SyncManager::new(&crosslink_dir)?;
    sync.init_cache()?;
    // INTENTIONAL: fetch is best-effort — bootstrap proceeds with local state if offline
    let _ = sync.fetch();

    // Step 7: Create agent directory on hub
    let cache = sync.cache_path();
    let agent_dir = cache.join("agents").join(agent_id);
    std::fs::create_dir_all(&agent_dir).context("Failed to create agent directory on hub")?;

    let heartbeat = serde_json::json!({
        "agent_id": agent_id,
        "timestamp": chrono::Utc::now().to_rfc3339(),
        "status": "active"
    });
    let heartbeat_path = agent_dir.join("heartbeat.json");
    std::fs::write(&heartbeat_path, serde_json::to_string_pretty(&heartbeat)?)
        .context("Failed to write heartbeat.json")?;

    let git_in_cache = |args: &[&str]| -> Result<()> {
        let output = Command::new("git").current_dir(cache).args(args).output()?;
        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr);
            if !stderr.contains("nothing to commit") {
                bail!("git {:?} failed: {}", args, stderr.trim());
            }
        }
        Ok(())
    };

    git_in_cache(&["add", &format!("agents/{agent_id}/")])?;
    git_in_cache(&[
        "commit",
        "-m",
        &format!("bootstrap: register agent '{agent_id}'"),
    ])?;

    let remote = crate::sync::read_tracker_remote(&crosslink_dir);
    match Command::new("git")
        .current_dir(cache)
        .args(["push", &remote, crate::sync::HUB_BRANCH])
        .output()
    {
        Ok(o) if !o.status.success() => {
            eprintln!(
                "Warning: could not push agent registration to hub: {} — will be pushed on next sync",
                String::from_utf8_lossy(&o.stderr).trim()
            );
        }
        Err(e) => eprintln!(
            "Warning: could not push agent registration to hub: {e} — will be pushed on next sync"
        ),
        Ok(_) => {}
    }

    // Step 8: Publish key to hub (before configuring signing to avoid
    // the chicken-and-egg where signing is required for the publish commit)
    if let Some(pub_key) = &config.ssh_public_key {
        if let Err(e) = super::trust::publish_agent_key(&crosslink_dir, agent_id, pub_key) {
            println!("  Note: Could not publish key to hub: {e}");
            println!("  Key will be auto-published on next `crosslink sync`.");
        }
    }

    // Step 9: Configure signing (after key is published)
    if let Err(e) = sync.configure_signing(&crosslink_dir) {
        tracing::warn!("could not configure commit signing: {e} — commits will be unsigned");
    }

    // Step 10: Print summary
    println!("Bootstrap complete:");
    println!("  Agent ID:  {}", config.agent_id);
    println!("  Machine:   {}", config.machine_id);
    if config.ssh_fingerprint.is_some() {
        println!("  Signing: enabled");
    }
    println!("  Repo path: {}", target_dir.display());
    println!();
    println!("Ask your driver to approve this agent with `crosslink trust approve {agent_id}`");

    Ok(())
}

/// `crosslink agent status`
pub fn status(crosslink_dir: &Path) -> Result<()> {
    match AgentConfig::load(crosslink_dir)? {
        Some(config) => {
            println!("Agent: {}", config.agent_id);
            println!("Machine: {}", config.machine_id);
            if let Some(desc) = &config.description {
                println!("Description: {desc}");
            }
            if config.ssh_fingerprint.is_some() {
                println!("Signing: enabled");
            } else {
                println!("Signing: disabled");
            }

            // Show locked issues (best-effort)
            // Locks prevent other agents from working on the same issue simultaneously.
            if let Ok(sync) = crate::sync::SyncManager::new(crosslink_dir) {
                // INTENTIONAL: init and fetch are best-effort — status display works with stale data
                let _ = sync.init_cache();
                let _ = sync.fetch();
                if let Ok(locks) = sync.read_locks_auto() {
                    let my_locks = locks.agent_locks(&config.agent_id);
                    if my_locks.is_empty() {
                        println!("Locks: none");
                    } else {
                        println!(
                            "Locks: {} (exclusively assigned to this agent)",
                            my_locks
                                .iter()
                                .map(|id| format_issue_id(*id))
                                .collect::<Vec<_>>()
                                .join(", ")
                        );
                    }
                }
            }
        }
        None => {
            println!("No agent configured. Run 'crosslink agent init <id>' first.");
        }
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::process::Command as TestCommand;
    use tempfile::tempdir;

    /// Helper: create a bare git repo that can be used as a clone source.
    fn create_bare_repo(dir: &Path) {
        let output = TestCommand::new("git")
            .args(["init", "--bare", "-q"])
            .arg(dir)
            .output()
            .expect("git init --bare failed");
        assert!(output.status.success(), "Failed to create bare repo");
    }

    /// Helper: create a non-bare git repo with an initial commit so it can be cloned.
    fn create_repo_with_commit(dir: &Path) {
        TestCommand::new("git")
            .args(["init", "-q"])
            .arg(dir)
            .output()
            .expect("git init failed");
        TestCommand::new("git")
            .current_dir(dir)
            .args(["config", "user.email", "test@test.com"])
            .output()
            .unwrap();
        TestCommand::new("git")
            .current_dir(dir)
            .args(["config", "user.name", "Test"])
            .output()
            .unwrap();
        // Disable gpg signing for test commits
        TestCommand::new("git")
            .current_dir(dir)
            .args(["config", "commit.gpgsign", "false"])
            .output()
            .unwrap();
        std::fs::write(dir.join("README"), "init").unwrap();
        TestCommand::new("git")
            .current_dir(dir)
            .args(["add", "."])
            .output()
            .unwrap();
        TestCommand::new("git")
            .current_dir(dir)
            .args(["commit", "-q", "-m", "initial"])
            .output()
            .unwrap();
    }

    #[test]
    fn test_bootstrap_creates_crosslink_dir() {
        let tmp = tempdir().unwrap();
        let bare = tmp.path().join("origin.git");
        create_repo_with_commit(&tmp.path().join("seed"));
        // Clone seed into a bare repo so we have something to clone from
        TestCommand::new("git")
            .args([
                "clone",
                "--bare",
                "-q",
                &tmp.path().join("seed").to_string_lossy(),
                &bare.to_string_lossy(),
            ])
            .output()
            .unwrap();

        let target = tmp.path().join("cloned");
        let result = bootstrap(
            &target,
            &bare.to_string_lossy(),
            "bot-001",
            None,
            Some("test bootstrap"),
            true, // skip SSH key generation in tests
        );
        assert!(result.is_ok(), "bootstrap failed: {:?}", result.err());

        // .crosslink dir should exist in the cloned repo
        assert!(target.join(".crosslink").exists());

        // agent.json should exist
        let config = AgentConfig::load(&target.join(".crosslink"))
            .unwrap()
            .unwrap();
        assert_eq!(config.agent_id, "bot-001");
        assert_eq!(config.description, Some("test bootstrap".to_string()));
    }

    #[test]
    fn test_bootstrap_rejects_invalid_agent_id() {
        let tmp = tempdir().unwrap();
        let bare = tmp.path().join("origin.git");
        create_bare_repo(&bare);

        let target = tmp.path().join("cloned");
        // "x" is too short (< 3 chars) — AgentConfig::init validates this
        let result = bootstrap(&target, &bare.to_string_lossy(), "x", None, None, true);
        assert!(result.is_err());
        let err_msg = result.unwrap_err().to_string();
        assert!(
            err_msg.contains("at least 3 characters"),
            "Unexpected error: {err_msg}"
        );
    }

    #[test]
    fn test_bootstrap_existing_agent_skips() {
        let tmp = tempdir().unwrap();
        let bare = tmp.path().join("origin.git");
        create_repo_with_commit(&tmp.path().join("seed"));
        TestCommand::new("git")
            .args([
                "clone",
                "--bare",
                "-q",
                &tmp.path().join("seed").to_string_lossy(),
                &bare.to_string_lossy(),
            ])
            .output()
            .unwrap();

        let target = tmp.path().join("cloned");

        // First bootstrap
        bootstrap(
            &target,
            &bare.to_string_lossy(),
            "bot-002",
            None,
            Some("first"),
            true,
        )
        .unwrap();

        // Second bootstrap with same target — should succeed without error
        // (the agent identity step is skipped gracefully)
        let result = bootstrap(
            &target,
            &bare.to_string_lossy(),
            "bot-002",
            None,
            Some("second"),
            true,
        );
        assert!(
            result.is_ok(),
            "second bootstrap failed: {:?}",
            result.err()
        );

        // Config should still have the first description
        let config = AgentConfig::load(&target.join(".crosslink"))
            .unwrap()
            .unwrap();
        assert_eq!(config.description, Some("first".to_string()));
    }

    #[test]
    fn test_bootstrap_verifies_remote() {
        let tmp = tempdir().unwrap();

        // Create a repo with a real remote
        let bare = tmp.path().join("origin.git");
        create_repo_with_commit(&tmp.path().join("seed"));
        TestCommand::new("git")
            .args([
                "clone",
                "--bare",
                "-q",
                &tmp.path().join("seed").to_string_lossy(),
                &bare.to_string_lossy(),
            ])
            .output()
            .unwrap();

        let target = tmp.path().join("cloned");
        // Clone it first with the real URL
        TestCommand::new("git")
            .args([
                "clone",
                "-q",
                &bare.to_string_lossy(),
                &target.to_string_lossy(),
            ])
            .output()
            .unwrap();

        // Now try to bootstrap with a *different* URL — should fail
        let result = bootstrap(
            &target,
            "https://example.com/wrong-repo.git",
            "bot-003",
            None,
            None,
            true,
        );
        assert!(result.is_err());
        let err_msg = result.unwrap_err().to_string();
        assert!(
            err_msg.contains("Remote mismatch"),
            "Unexpected error: {err_msg}"
        );
    }

    #[test]
    fn test_init_creates_config() {
        let dir = tempdir().unwrap();
        let crosslink_dir = dir.path().join(".crosslink");
        std::fs::create_dir_all(&crosslink_dir).unwrap();

        init(
            &crosslink_dir,
            "worker-1",
            Some("Test agent"),
            true,
            false,
            AgentRole::Agent,
        )
        .unwrap();

        let config = AgentConfig::load(&crosslink_dir).unwrap().unwrap();
        assert_eq!(config.agent_id, "worker-1");
        assert_eq!(config.description, Some("Test agent".to_string()));
    }

    #[test]
    fn test_init_rejects_duplicate() {
        let dir = tempdir().unwrap();
        let crosslink_dir = dir.path().join(".crosslink");
        std::fs::create_dir_all(&crosslink_dir).unwrap();

        init(
            &crosslink_dir,
            "worker-1",
            None,
            true,
            false,
            AgentRole::Agent,
        )
        .unwrap();
        let result = init(
            &crosslink_dir,
            "worker-2",
            None,
            true,
            false,
            AgentRole::Agent,
        );
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("already configured"));
    }

    #[test]
    fn test_init_force_overwrites_valid_config() {
        let dir = tempdir().unwrap();
        let crosslink_dir = dir.path().join(".crosslink");
        std::fs::create_dir_all(&crosslink_dir).unwrap();

        init(
            &crosslink_dir,
            "worker-1",
            None,
            true,
            false,
            AgentRole::Agent,
        )
        .unwrap();
        init(
            &crosslink_dir,
            "worker-2",
            Some("New agent"),
            true,
            true,
            AgentRole::Agent,
        )
        .unwrap();

        let config = AgentConfig::load(&crosslink_dir).unwrap().unwrap();
        assert_eq!(config.agent_id, "worker-2");
        assert_eq!(config.description, Some("New agent".to_string()));
    }

    #[test]
    fn test_init_overwrites_malformed_json() {
        let dir = tempdir().unwrap();
        let crosslink_dir = dir.path().join(".crosslink");
        std::fs::create_dir_all(&crosslink_dir).unwrap();

        // Write invalid JSON
        std::fs::write(crosslink_dir.join("agent.json"), "not valid json").unwrap();

        init(
            &crosslink_dir,
            "worker-1",
            None,
            true,
            false,
            AgentRole::Agent,
        )
        .unwrap();

        let config = AgentConfig::load(&crosslink_dir).unwrap().unwrap();
        assert_eq!(config.agent_id, "worker-1");
    }

    #[test]
    fn test_init_overwrites_invalid_agent_id() {
        let dir = tempdir().unwrap();
        let crosslink_dir = dir.path().join(".crosslink");
        std::fs::create_dir_all(&crosslink_dir).unwrap();

        // Write valid JSON but with agent_id that fails validation (too short)
        std::fs::write(
            crosslink_dir.join("agent.json"),
            r#"{"agent_id": "m1", "machine_id": "host"}"#,
        )
        .unwrap();

        init(
            &crosslink_dir,
            "worker-1",
            None,
            true,
            false,
            AgentRole::Agent,
        )
        .unwrap();

        let config = AgentConfig::load(&crosslink_dir).unwrap().unwrap();
        assert_eq!(config.agent_id, "worker-1");
    }

    #[test]
    fn test_status_no_config() {
        let dir = tempdir().unwrap();
        let crosslink_dir = dir.path().join(".crosslink");
        std::fs::create_dir_all(&crosslink_dir).unwrap();

        // Should not error, just print message
        status(&crosslink_dir).unwrap();
    }

    #[test]
    fn test_status_with_config() {
        let dir = tempdir().unwrap();
        let crosslink_dir = dir.path().join(".crosslink");
        std::fs::create_dir_all(&crosslink_dir).unwrap();

        init(
            &crosslink_dir,
            "my-agent",
            Some("My agent"),
            true,
            false,
            AgentRole::Agent,
        )
        .unwrap();
        status(&crosslink_dir).unwrap();
    }
}