tracevault-cli 0.27.0

CLI tool for Visdom Trace - AI code tracing and attribution
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
//! `tracevault repo` — workspace/detached-mode repo binding commands.

use std::path::Path;

use crate::api_client::{resolve_client, ApiClient};
use crate::credentials::resolve_credentials;
use crate::resolution::{
    binding_from_config, effective_binding, resolve_path_to_binding, BindingSource, ResolveInputs,
};
use crate::session_state::{self, RepoBinding, SessionState};

/// Sub-actions for `tracevault repo` (workspace/detached mode).
#[derive(clap::Subcommand)]
pub enum RepoCmd {
    /// Bind tracing to a registered repo for the current session and print its
    /// policies. Give a checkout <path> — `--name` is deprecated and always
    /// errors, telling you to bind by path instead.
    #[command(group(clap::ArgGroup::new("target").required(true).multiple(false)))]
    Switch {
        /// Path to a checkout of a registered repo (resolves via its git origin remote).
        #[arg(group = "target")]
        path: Option<String>,
        /// Deprecated: name-only binding is no longer supported and always
        /// errors; bind by checkout path instead (`tracevault repo switch <path>`).
        #[arg(long, group = "target")]
        name: Option<String>,
        /// Write a session-independent user-level default instead of a session
        /// binding. Implied when no session id is available.
        #[arg(long)]
        user: bool,
        /// Session to target; defaults to $TRACEVAULT_SESSION_ID.
        #[arg(long)]
        session_id: Option<String>,
    },
    /// Show the repo the current session is bound to.
    Status {
        /// Session to target; defaults to $TRACEVAULT_SESSION_ID.
        #[arg(long)]
        session_id: Option<String>,
        /// One-off: resolve this checkout path instead of the session binding.
        #[arg(long)]
        path: Option<String>,
    },
    /// Clear the current session's binding, or (with --user) the user-level default.
    Reset {
        /// Clear the user-level default instead of the session binding.
        #[arg(long)]
        user: bool,
        /// Session to target; defaults to $TRACEVAULT_SESSION_ID.
        #[arg(long)]
        session_id: Option<String>,
    },
}

/// Resolve the current session id: the explicit flag wins, else
/// `$TRACEVAULT_SESSION_ID` (set by the SessionStart hook of `init --global`).
pub fn resolve_session_id(explicit: Option<&str>) -> Result<String, Box<dyn std::error::Error>> {
    if let Some(s) = explicit {
        return Ok(s.to_string());
    }
    std::env::var("TRACEVAULT_SESSION_ID")
        .ok()
        .filter(|s| !s.is_empty())
        .ok_or_else(|| {
            "no session id available: pass --session-id or set TRACEVAULT_SESSION_ID \
             (installed by `tracevault init --global`)"
                .into()
        })
}

pub async fn run(
    cmd: RepoCmd,
    project_root: &Path,
    cwd: &Path,
) -> Result<(), Box<dyn std::error::Error>> {
    match cmd {
        RepoCmd::Status { session_id, path } => {
            status(session_id.as_deref(), path.as_deref(), project_root, cwd).await
        }
        RepoCmd::Switch {
            path,
            name,
            user,
            session_id,
        } => {
            switch(
                path.as_deref(),
                name.as_deref(),
                user,
                session_id.as_deref(),
                project_root,
            )
            .await
        }
        RepoCmd::Reset { user, session_id } => reset(user, session_id.as_deref()),
    }
}

/// Resolve a path to a binding for `repo switch`, turning "unregistered" into
/// a clear error (switch should fail loudly, unlike the best-effort `--path`
/// status override).
async fn resolve_switch_binding(
    path: &Path,
    client: &ApiClient,
) -> Result<RepoBinding, Box<dyn std::error::Error>> {
    resolve_path_to_binding(path, client).await?.ok_or_else(|| {
        format!(
            "repo at {} is not registered with TraceVault; nothing to bind",
            path.display()
        )
        .into()
    })
}

/// Apply a switch to session state (session-level active binding). Pure.
fn apply_switch(state: &mut SessionState, binding: RepoBinding) {
    state.active = Some(binding);
}

/// Clear the session's workspace binding (session-level reset). Pure.
fn apply_reset(state: &mut SessionState) {
    *state = SessionState::default();
}

fn reset(user: bool, session_id: Option<&str>) -> Result<(), Box<dyn std::error::Error>> {
    if user {
        crate::user_default::clear()?;
        println!("cleared user-level default repo binding");
        return Ok(());
    }
    let id = resolve_session_id(session_id)?;
    let mut state = session_state::load(&id);
    apply_reset(&mut state);
    session_state::save(&id, &state)?;
    // NOTE: subagent worktree-scoped reset-to-parent is handled in sub-plan C.
    println!("cleared workspace repo binding for session {id}");
    Ok(())
}

/// Which target `repo switch` should resolve to a binding: a checkout path.
/// `--name` binding is deprecated (see `switch_target`) and no longer
/// produces a variant here.
#[derive(Debug, PartialEq, Eq)]
enum SwitchTarget<'a> {
    Path(&'a str),
}

/// Pure guard for `repo switch`'s `<path>` / `--name` arguments. Kept
/// separate from `switch` so it can be unit-tested without spinning up a
/// server or session state (also a safety net if the clap `ArgGroup` on the
/// `Switch` variant is ever loosened). `--name` binding is hard-deprecated:
/// a name-only invocation errors rather than resolving by name, directing
/// callers to bind by checkout path so the codebase is resolved from its git
/// remote.
fn switch_target<'a>(
    path: Option<&'a str>,
    name: Option<&'a str>,
) -> Result<SwitchTarget<'a>, Box<dyn std::error::Error>> {
    match (path, name) {
        (Some(p), _) if !p.trim().is_empty() => Ok(SwitchTarget::Path(p)),
        (Some(_), _) => Err("repo path must not be empty".into()),
        (None, Some(_)) => Err(
            "`--name` binding is deprecated and no longer supported; bind by checkout path \
             (`tracevault repo switch <path>`) so the codebase is resolved from its git remote"
                .into(),
        ),
        (None, None) => {
            Err("provide a checkout path to bind (`tracevault repo switch <path>`)".into())
        }
    }
}

/// Where a `repo switch` should persist its binding: a specific session, or the
/// session-independent user-level default.
#[derive(Debug)]
enum SwitchDest {
    Session(String),
    UserDefault,
}

/// Pure choice of write target for `repo switch`: `--user` (or the absence of
/// any session id) selects the user-level default; otherwise the resolved
/// session id selects a session binding. Kept pure so it's unit-testable.
fn switch_destination(user: bool, session_id: Option<String>) -> SwitchDest {
    if user {
        return SwitchDest::UserDefault;
    }
    match session_id {
        Some(id) => SwitchDest::Session(id),
        None => SwitchDest::UserDefault,
    }
}

async fn switch(
    path: Option<&str>,
    name: Option<&str>,
    user: bool,
    session_id: Option<&str>,
    project_root: &Path,
) -> Result<(), Box<dyn std::error::Error>> {
    // Validate the target argument before any network work, so a bad/empty
    // path or name surfaces as an argument error rather than an unrelated
    // network error from the resolution step below.
    let target = switch_target(path, name)?;

    let client = resolve_client(project_root)?;

    let binding = match target {
        SwitchTarget::Path(p) => resolve_switch_binding(Path::new(p), &client).await?,
    };

    // Validate the repo id BEFORE persisting anything: a malformed repo_id must
    // not leave a persisted binding behind.
    let repo_uuid = binding.repo_id.parse::<uuid::Uuid>().map_err(|e| {
        format!(
            "server returned an invalid repo id {:?}: {e}",
            binding.repo_id
        )
    })?;

    // A session id is optional now: with one (and no --user) we bind that
    // session; without one (or with --user) we set the user-level default,
    // which any new session inherits. This is what lets a container bind its
    // repo before Claude — and its session — exists.
    let session = resolve_session_id(session_id).ok();
    match switch_destination(user, session) {
        SwitchDest::Session(id) => {
            let mut state = session_state::load(&id);
            apply_switch(&mut state, binding.clone());
            session_state::save(&id, &state)?;
            println!("bound session {id} to repo {}", binding.repo_id);
        }
        SwitchDest::UserDefault => {
            crate::user_default::save(&binding)?;
            println!(
                "set user-level default repo {}; applies to new sessions without their own binding (the current session, if any, is unchanged — omit --user to bind this session)",
                binding.repo_id
            );
        }
    }

    // Best-effort policy fetch (applies to either destination). A failure here
    // must not make the whole command report as an error.
    match client.get_agent_instructions(&repo_uuid).await {
        Ok(policies) => println!("{}", policies.content),
        Err(e) => eprintln!("warning: bound, but could not fetch policies: {e}"),
    }
    Ok(())
}

/// Resolve a `--path <path>` override into a live `RepoBinding`, if possible.
/// Best-effort: prints a clear message and returns `None` on any failure
/// (missing credentials, or a server error) rather than failing the whole
/// `status` inspector.
async fn resolve_repo_flag(
    repo_flag_path: Option<&str>,
    project_root: &Path,
) -> Option<RepoBinding> {
    let path = repo_flag_path?;

    // Best-effort, per this function's contract: a credential/URL mismatch
    // prints the reason and drops the `--path` override rather than failing the
    // whole `status` inspector.
    let (server_url, credential) = match resolve_credentials(project_root) {
        Ok(resolved) => resolved,
        Err(e) => {
            eprintln!("--path {path}: {e}");
            return None;
        }
    };
    let Some(server_url) = server_url else {
        eprintln!(
            "--path {path}: no server URL configured (run `tracevault login`); showing binding \
             without the --path override"
        );
        return None;
    };

    let client = ApiClient::with_credential(&server_url, credential);
    match resolve_path_to_binding(Path::new(path), &client).await {
        Ok(Some(binding)) => Some(binding),
        Ok(None) => {
            eprintln!(
                "--path {path}: no registered repo found for this path's git remote; showing \
                 binding without the --path override"
            );
            None
        }
        Err(e) => {
            eprintln!(
                "--path {path}: failed to resolve ({e}); showing binding without the --path \
                 override"
            );
            None
        }
    }
}

/// Pure formatter for `repo status`'s output: which repo is bound, and via
/// which precedence tier. Kept separate from I/O so it can be unit-tested.
fn format_status(binding: Option<(&RepoBinding, BindingSource)>) -> String {
    match binding {
        Some((b, source)) => format!("bound to repo {} via {source}", b.repo_id),
        None => {
            "not bound to any repo (workspace mode; run `tracevault repo switch <path>`)".into()
        }
    }
}

async fn status(
    session_id: Option<&str>,
    repo_flag_path: Option<&str>,
    project_root: &Path,
    cwd: &Path,
) -> Result<(), Box<dyn std::error::Error>> {
    // Session state is best-effort: if a session id resolves, load it; else
    // warn and fall back to an empty SessionState rather than silently
    // reporting "not bound" without the user knowing why.
    let session = match resolve_session_id(session_id) {
        Ok(id) => session_state::load(&id),
        Err(_) => {
            eprintln!(
                "warning: no session id (pass --session-id or set TRACEVAULT_SESSION_ID); \
                 showing binding without session context"
            );
            SessionState::default()
        }
    };
    let worktree = crate::paths::worktree_toplevel(cwd);
    let bound = crate::config::TracevaultConfig::load(project_root)
        .as_ref()
        .and_then(binding_from_config);

    // A --path override on status resolves live (needs the server).
    let repo_flag = resolve_repo_flag(repo_flag_path, project_root).await;

    let user_default = crate::user_default::load();

    let effective = effective_binding(ResolveInputs {
        repo_flag,
        session: &session,
        worktree_path: Some(&worktree),
        bound,
        user_default: user_default.clone(),
    });
    println!(
        "{}",
        format_status(effective.as_ref().map(|(b, s)| (b, *s)))
    );

    // Surface which codebase the effective binding belongs to, with live clone
    // status when reachable. Best-effort — a failure must not fail `status`:
    //   1. `remote_id` (persisted at init / set by switch) → fetch the remote
    //      detail for name + clone status (works without a checkout);
    //   2. else a `git_url` → live `resolve_remote` for name + clone status;
    //   3. else fall back to the cached `codebase_name` (name only, offline).
    if let Some((binding, _)) = &effective {
        // Best-effort codebase line: a credential/URL mismatch degrades to the
        // offline fallback (cached name only) with the reason printed, rather
        // than failing `repo status` — whose job is to explain local state.
        let (server_url, credential) = match resolve_credentials(project_root) {
            Ok(resolved) => resolved,
            Err(e) => {
                eprintln!("Warning: {e}");
                (None, None)
            }
        };
        let client = server_url
            .as_ref()
            .map(|su| ApiClient::with_credential(su, credential));
        let detail_line = if let (Some(client), Some(remote_id)) = (&client, binding.remote_id) {
            client.get_remote_detail(remote_id).await.ok().map(|d| {
                crate::resolution::codebase_line(
                    d.name.as_deref(),
                    &d.normalized_url,
                    &d.clone_status,
                )
            })
        } else {
            None
        };
        let resolved_line = if detail_line.is_none() {
            if let (Some(client), Some(git_url)) = (&client, binding.git_url.as_deref()) {
                client
                    .resolve_remote(git_url)
                    .await
                    .ok()
                    .flatten()
                    .map(|r| {
                        crate::resolution::codebase_line(
                            r.name.as_deref(),
                            &r.normalized_url,
                            &r.clone_status,
                        )
                    })
            } else {
                None
            }
        } else {
            None
        };
        if let Some(line) = crate::resolution::pick_status_line(
            detail_line,
            resolved_line,
            binding.codebase_name.as_deref(),
        ) {
            println!("{line}");
        }
    }

    // Surface a configured user default even when a more specific tier won, so
    // it's discoverable (avoids double-printing when it IS the winning tier).
    let default_is_effective = matches!(
        effective.as_ref().map(|(_, s)| *s),
        Some(BindingSource::UserDefault)
    );
    if let Some(ud) = &user_default {
        if !default_is_effective {
            println!("user default: repo {}", ud.repo_id);
        }
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::{BufRead, BufReader, Write};
    use std::net::TcpListener;
    use std::thread;

    #[test]
    fn resolve_session_id_prefers_explicit() {
        assert_eq!(resolve_session_id(Some("sess-x")).unwrap(), "sess-x");
    }

    #[test]
    fn resolve_session_id_errors_when_absent() {
        // No explicit id and (in this test process) no env var set → error.
        // Guard against a leaked env var from another test by checking the
        // message only when the var is genuinely absent.
        if std::env::var("TRACEVAULT_SESSION_ID").is_err() {
            assert!(resolve_session_id(None).is_err());
        }
    }

    #[test]
    fn switch_destination_user_flag_forces_user_default() {
        assert!(matches!(
            switch_destination(true, Some("sess-1".to_string())),
            SwitchDest::UserDefault
        ));
        assert!(matches!(
            switch_destination(true, None),
            SwitchDest::UserDefault
        ));
    }

    #[test]
    fn switch_destination_session_when_present_and_no_user_flag() {
        assert!(matches!(
            switch_destination(false, Some("sess-1".to_string())),
            SwitchDest::Session(id) if id == "sess-1"
        ));
    }

    #[test]
    fn switch_destination_user_default_when_no_session() {
        assert!(matches!(
            switch_destination(false, None),
            SwitchDest::UserDefault
        ));
    }

    #[test]
    fn switch_target_rejects_neither() {
        assert!(switch_target(None, None).is_err());
    }

    // The clap `ArgGroup` on `Switch` already prevents both being given
    // together, but as a safety net at this level path wins: a non-empty
    // path resolves the target regardless of a (deprecated) --name.
    #[test]
    fn switch_target_path_wins_when_both_given() {
        assert!(matches!(
            switch_target(Some("/p"), Some("x")),
            Ok(SwitchTarget::Path("/p"))
        ));
    }

    #[test]
    fn switch_target_accepts_path_only() {
        assert!(matches!(
            switch_target(Some("/p"), None),
            Ok(SwitchTarget::Path("/p"))
        ));
    }

    // --name now warns and does NOT resolve by name: switch_target maps a name-only
    // invocation to a deprecation error directing to path binding (no list_repos call).
    #[test]
    fn switch_target_name_is_deprecated() {
        let err = switch_target(None, Some("some-repo"))
            .unwrap_err()
            .to_string();
        assert!(err.contains("deprecated"), "got: {err}");
        assert!(err.contains("path"), "should direct to path binding: {err}");
    }

    fn binding(id: &str) -> RepoBinding {
        RepoBinding {
            repo_id: id.into(),
            git_url: None,
            remote_id: None,
            codebase_name: None,
            updated_at: "t".into(),
        }
    }

    #[test]
    fn apply_switch_sets_session_active() {
        let mut state = SessionState::default();
        apply_switch(&mut state, binding("new-repo"));
        assert_eq!(state.active.unwrap().repo_id, "new-repo");
    }

    #[test]
    fn apply_reset_clears_active_and_subagents() {
        let mut state = SessionState {
            active: Some(binding("r")),
            subagents: std::collections::HashMap::from([("/wt/a".to_string(), binding("r2"))]),
            ..Default::default()
        };
        apply_reset(&mut state);
        assert_eq!(state, SessionState::default());
    }

    #[test]
    fn apply_switch_overwrites_existing_active() {
        let mut state = SessionState {
            active: Some(binding("old-repo")),
            subagents: Default::default(),
            ..Default::default()
        };
        apply_switch(&mut state, binding("new-repo"));
        assert_eq!(state.active.unwrap().repo_id, "new-repo");
    }

    #[test]
    fn format_status_none() {
        assert_eq!(
            format_status(None),
            "not bound to any repo (workspace mode; run `tracevault repo switch <path>`)"
        );
    }

    #[test]
    fn format_status_repo_flag() {
        let b = binding("r1");
        assert_eq!(
            format_status(Some((&b, BindingSource::RepoFlag))),
            "bound to repo r1 via --path override"
        );
    }

    #[test]
    fn format_status_subagent() {
        let b = binding("r2");
        assert_eq!(
            format_status(Some((&b, BindingSource::Subagent))),
            "bound to repo r2 via subagent worktree override"
        );
    }

    #[test]
    fn format_status_session_active() {
        let b = binding("r3");
        assert_eq!(
            format_status(Some((&b, BindingSource::SessionActive))),
            "bound to repo r3 via session (repo switch)"
        );
    }

    #[test]
    fn format_status_bound() {
        let b = binding("r4");
        assert_eq!(
            format_status(Some((&b, BindingSource::Bound))),
            "bound to repo r4 via bound .tracevault/config.toml"
        );
    }

    #[test]
    fn format_status_user_default() {
        let b = binding("r5");
        assert_eq!(
            format_status(Some((&b, BindingSource::UserDefault))),
            "bound to repo r5 via user default (repo switch --user)"
        );
    }

    fn add_origin_remote(dir: &std::path::Path, url: &str) {
        let ok = std::process::Command::new("git")
            .args(["-C", &dir.to_string_lossy(), "remote", "add", "origin", url])
            .status()
            .expect("git remote add failed")
            .success();
        assert!(ok, "git remote add must succeed");
    }

    /// Spawn a one-shot raw-HTTP server that returns `response` to the first
    /// request it accepts (mirrors `tests/resolve_repo_test.rs`).
    fn spawn_once(response: &'static str) -> String {
        let listener = TcpListener::bind("127.0.0.1:0").unwrap();
        let addr = listener.local_addr().unwrap();
        thread::spawn(move || {
            if let Ok((stream, _)) = listener.accept() {
                let mut reader = BufReader::new(stream);
                let mut request_line = String::new();
                let _ = reader.read_line(&mut request_line);
                let mut stream = reader.into_inner();
                let _ = stream.write_all(response.as_bytes());
                let _ = stream.flush();
            }
        });
        format!("http://{addr}")
    }

    /// Format a raw HTTP/1.1 200 response with a correct `Content-Length` for
    /// a JSON `body`.
    fn http_200(body: &str) -> String {
        format!(
            "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{}",
            body.len(),
            body
        )
    }

    /// Generalizes `spawn_once` to a listener that answers each of `responses`
    /// in order, one per accepted connection (the resolve-by-remote flow makes
    /// two requests: `resolve_remote` then `get_remote_repos`).
    fn spawn_n(responses: Vec<String>) -> String {
        let listener = TcpListener::bind("127.0.0.1:0").unwrap();
        let addr = listener.local_addr().unwrap();
        thread::spawn(move || {
            for response in responses {
                let response: &'static str = Box::leak(response.into_boxed_str());
                if let Ok((stream, _)) = listener.accept() {
                    let mut reader = BufReader::new(stream);
                    let mut request_line = String::new();
                    let _ = reader.read_line(&mut request_line);
                    let mut stream = reader.into_inner();
                    let _ = stream.write_all(response.as_bytes());
                    let _ = stream.flush();
                }
            }
        });
        format!("http://{addr}")
    }

    #[tokio::test]
    async fn resolve_switch_binding_ok_for_registered_repo() {
        // Path resolution now goes through the codebase (remote) resolver:
        // resolve_remote finds the codebase, then get_remote_repos returns its
        // linked repos — the switch binds to the first one.
        let tmp = tempfile::tempdir().unwrap();
        crate::test_helpers::init_git_repo(tmp.path());
        add_origin_remote(tmp.path(), "git@github.com:org/repo.git");

        let remote_id = "44000761-8d22-4256-bd2c-27a0ba278c6f";
        let repo_id = "11111111-1111-4111-8111-111111111111";
        let remote = format!(
            r#"{{"remote_id":"{remote_id}","name":"repo","normalized_url":"github.com/org/repo","clone_status":"ready"}}"#
        );
        let detail = format!(
            r#"{{"id":"{remote_id}","name":"repo","normalized_url":"github.com/org/repo","clone_url":"https://github.com/org/repo.git","clone_status":"ready","clone_error":null,"last_fetched_at":null,"repo_count":1,"created_at":"2026-01-01T00:00:00Z","repos":[{{"id":"{repo_id}","name":"repo"}}]}}"#
        );
        let base = spawn_n(vec![http_200(&remote), http_200(&detail)]);
        let client = ApiClient::new(&base, Some("tok"));

        let got = resolve_switch_binding(tmp.path(), &client)
            .await
            .expect("expected Ok binding");
        assert_eq!(got.repo_id, repo_id);
        assert_eq!(got.remote_id, Some(remote_id.parse().unwrap()));
        assert_eq!(got.codebase_name.as_deref(), Some("repo"));
    }

    #[tokio::test]
    async fn resolve_switch_binding_errors_for_unregistered_repo() {
        let tmp = tempfile::tempdir().unwrap();
        crate::test_helpers::init_git_repo(tmp.path());
        add_origin_remote(tmp.path(), "git@github.com:org/unregistered.git");

        // A genuine domain 404 carries the server's JSON error envelope
        // (`{"error": "..."}` — what every `AppError` renders), which is
        // what distinguishes "not tracked" from a bare route-404 (see
        // `ApiClient::resolve_remote`).
        let body = r#"{"error":"No remote for that URL"}"#;
        let resp = format!(
            "HTTP/1.1 404 Not Found\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{}",
            body.len(), body
        );
        let base = spawn_once(Box::leak(resp.into_boxed_str()));
        let client = ApiClient::new(&base, Some("tok"));

        let err = resolve_switch_binding(tmp.path(), &client)
            .await
            .expect_err("expected Err for unregistered repo");
        assert!(
            err.to_string().contains("not registered"),
            "unexpected error message: {err}"
        );
    }

    #[tokio::test]
    async fn resolve_switch_binding_flags_bare_404_as_version_skew() {
        // A bare 404 (no JSON error body) is axum's "no route matched"
        // fallback — the shape an old server sends for a route it doesn't
        // have yet. `repo switch` must not misreport this as "not
        // registered"; it should surface a diagnosable version-mismatch
        // error instead.
        let tmp = tempfile::tempdir().unwrap();
        crate::test_helpers::init_git_repo(tmp.path());
        add_origin_remote(tmp.path(), "git@github.com:org/unregistered.git");

        let resp = "HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\nConnection: close\r\n\r\n";
        let base = spawn_once(resp);
        let client = ApiClient::new(&base, Some("tok"));

        let err = resolve_switch_binding(tmp.path(), &client)
            .await
            .expect_err("expected Err for a bare route-404");
        assert!(
            !err.to_string().contains("not registered"),
            "must not be misreported as 'not registered'; got: {err}"
        );
        assert!(
            err.to_string().to_lowercase().contains("version mismatch"),
            "expected a version-mismatch hint; got: {err}"
        );
    }

    #[test]
    fn switch_target_rejects_empty_name() {
        // `--name` is deprecated outright, so there's no separate empty-name
        // branch anymore — an empty name still hits the deprecation error.
        assert!(switch_target(None, Some("")).is_err());
        assert!(switch_target(None, Some("   ")).is_err());
        let err = switch_target(None, Some(" ")).unwrap_err().to_string();
        assert!(err.contains("deprecated"), "got: {err}");
    }

    #[test]
    fn switch_target_rejects_empty_path() {
        assert!(switch_target(Some(""), None).is_err());
        assert!(switch_target(Some("   "), None).is_err());
        let err = switch_target(Some(" "), None).unwrap_err().to_string();
        assert!(err.contains("must not be empty"), "got: {err}");
    }

    // repo switch resolves by NORMALIZED url: origin spelling differs from what
    // was registered, but resolve_remote matches, and we bind to a linked repo.
    #[tokio::test]
    async fn resolve_path_binds_via_remote_across_url_spelling() {
        let tmp = tempfile::tempdir().unwrap();
        crate::test_helpers::init_git_repo(tmp.path());
        add_origin_remote(tmp.path(), "git@github.com:o/x.git"); // scp spelling
        let remote = r#"{"remote_id":"44000761-8d22-4256-bd2c-27a0ba278c6f","name":"x","normalized_url":"github.com/o/x","clone_status":"ready"}"#;
        let detail = r#"{"id":"44000761-8d22-4256-bd2c-27a0ba278c6f","name":"x","normalized_url":"github.com/o/x","clone_url":"https://github.com/o/x.git","clone_status":"ready","clone_error":null,"last_fetched_at":null,"repo_count":1,"created_at":"2026-01-01T00:00:00Z","repos":[{"id":"11111111-1111-4111-8111-111111111111","name":"x"}]}"#;
        let base = spawn_n(vec![http_200(remote), http_200(detail)]);
        let client = ApiClient::new(&base, Some("tok"));
        let b = resolve_path_to_binding(tmp.path(), &client)
            .await
            .unwrap()
            .expect("Some binding");
        assert_eq!(b.repo_id, "11111111-1111-4111-8111-111111111111");
        assert_eq!(
            b.remote_id,
            Some("44000761-8d22-4256-bd2c-27a0ba278c6f".parse().unwrap())
        );
        assert_eq!(b.codebase_name.as_deref(), Some("x"));
    }

    // A tracked codebase with no linked repos (bare remote) → explicit error.
    #[tokio::test]
    async fn resolve_path_bare_remote_errors() {
        let tmp = tempfile::tempdir().unwrap();
        crate::test_helpers::init_git_repo(tmp.path());
        add_origin_remote(tmp.path(), "git@github.com:o/x.git");
        let remote = r#"{"remote_id":"44000761-8d22-4256-bd2c-27a0ba278c6f","name":"x","normalized_url":"github.com/o/x","clone_status":"pending"}"#;
        let detail = r#"{"id":"44000761-8d22-4256-bd2c-27a0ba278c6f","name":"x","normalized_url":"github.com/o/x","clone_url":"https://github.com/o/x.git","clone_status":"pending","clone_error":null,"last_fetched_at":null,"repo_count":0,"created_at":"2026-01-01T00:00:00Z","repos":[]}"#;
        let base = spawn_n(vec![http_200(remote), http_200(detail)]);
        let client = ApiClient::new(&base, Some("tok"));
        let err = resolve_path_to_binding(tmp.path(), &client)
            .await
            .unwrap_err()
            .to_string();
        assert!(err.contains("no tracked repo"), "got: {err}");
    }
}