tracevault-cli 0.26.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
use serde::{Deserialize, Serialize};
use std::error::Error;
use std::path::Path;
use url::Url;

pub struct ApiClient {
    base_url: String,
    api_key: Option<String>,
    client: reqwest::Client,
}

#[derive(Serialize)]
pub struct RegisterRepoRequest {
    pub repo_name: String,
    pub github_url: Option<String>,
}

#[derive(Debug, Deserialize)]
pub struct RegisterRepoResponse {
    pub repo_id: uuid::Uuid,
}

#[derive(Debug, Deserialize)]
pub struct ResolveRemoteResponse {
    pub remote_id: uuid::Uuid,
    #[serde(default)]
    pub name: Option<String>,
    pub normalized_url: String,
    pub clone_status: String,
}

#[derive(Deserialize)]
pub struct RemoteRepoRef {
    pub id: uuid::Uuid,
    // Reserved for display (e.g. a future `repo status`/error message listing
    // a codebase's linked repos by name); not read by any caller yet.
    #[allow(dead_code)]
    pub name: String,
}

// The server's RemoteDetailResponse flattens the remote fields at top level and
// adds a `repos` array; serde ignores any other top-level fields.
#[derive(Deserialize)]
pub struct RemoteDetail {
    #[serde(default)]
    pub name: Option<String>,
    pub normalized_url: String,
    pub clone_status: String,
    pub repos: Vec<RemoteRepoRef>,
}

#[derive(Deserialize)]
pub struct DeviceAuthResponse {
    pub token: String,
}

#[derive(Deserialize)]
pub struct DeviceStatusResponse {
    pub status: String,
    pub token: Option<String>,
    pub email: Option<String>,
}

#[derive(Debug, Serialize)]
pub struct CheckPoliciesRequest {
    pub sessions: Vec<SessionCheckData>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub commit_sha: Option<String>,
}

#[derive(Debug, Serialize)]
pub struct SessionCheckData {
    pub session_id: String,
    pub tool_calls: Option<serde_json::Value>,
    pub files_modified: Option<Vec<String>>,
    pub total_tool_calls: Option<i32>,
}

#[derive(Debug, Deserialize)]
pub struct CheckPoliciesResponse {
    pub passed: bool,
    pub results: Vec<CheckResultItem>,
    pub blocked: bool,
}

#[derive(Debug, Deserialize)]
pub struct CheckResultItem {
    pub rule_name: String,
    pub result: String,
    pub action: String,
    pub severity: String,
    pub details: String,
}

#[derive(Debug, Deserialize)]
pub struct RepoListItem {
    pub id: uuid::Uuid,
    pub name: String,
    #[serde(default)]
    pub github_url: Option<String>,
    #[serde(default)]
    pub clone_status: Option<String>,
}

/// Response shape for the `policies/agent-instructions` endpoint.
#[derive(Debug, Clone, Deserialize)]
pub struct AgentInstructionsResponse {
    #[allow(dead_code)]
    pub format: String,
    pub content: String,
}

#[derive(Debug, Deserialize)]
pub struct MeResponse {
    #[allow(dead_code)]
    pub user_id: uuid::Uuid,
    pub email: String,
    pub name: Option<String>,
}

#[derive(Debug)]
pub enum GetMeError {
    /// 401 — token is missing or invalid.
    Unauthorized,
    /// Transport-level failure (DNS, TCP, TLS, timeout).
    Network(String),
    /// HTTP ≥ 400 other than 401, or malformed JSON.
    Server(String),
}

impl std::fmt::Display for GetMeError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Unauthorized => write!(f, "unauthorized (token invalid or expired)"),
            Self::Network(m) => write!(f, "network error: {m}"),
            Self::Server(m) => write!(f, "server error: {m}"),
        }
    }
}

impl std::error::Error for GetMeError {}

#[derive(Debug, Serialize)]
pub struct CiVerifyRequest {
    pub commits: Vec<String>,
}

#[derive(Debug, Deserialize)]
pub struct CiVerifyResponse {
    pub status: String,
    pub total_commits: usize,
    pub registered_commits: usize,
    pub sealed_commits: usize,
    pub policy_passed_commits: usize,
    pub results: Vec<CommitVerifyResult>,
}

#[derive(Debug, Deserialize)]
pub struct CommitVerifyResult {
    pub commit_sha: String,
    pub status: String,
    pub registered: bool,
    pub sealed: bool,
    pub signature_valid: bool,
    pub chain_valid: bool,
    pub policy_results: Vec<CiPolicyResult>,
}

#[derive(Debug, Deserialize)]
pub struct CiPolicyResult {
    pub rule_name: String,
    pub result: String,
    pub action: String,
    pub severity: String,
    pub details: String,
}

/// One project in `GET /api/v1/projects`. Server sends more
/// fields; only these two are consumed today.
#[derive(Debug, Deserialize)]
pub struct ProjectListItem {
    pub id: uuid::Uuid,
    pub name: String,
}

/// A repo linked to a project (member of `ProjectDetail::repos`).
#[derive(Debug, Deserialize)]
pub struct ProjectRepoRef {
    pub id: uuid::Uuid,
}

/// Full detail for a project. Server sends more fields; only `repos` is
/// consumed today.
#[derive(Debug, Deserialize)]
pub struct ProjectDetail {
    pub repos: Vec<ProjectRepoRef>,
}

/// Outcome of `ApiClient::resolve_project`, distinguishing "no project"
/// (404) from "ambiguous, multiple candidates" (409) — unlike
/// `resolve_remote`, which only distinguishes found/not-found.
#[derive(Debug)]
pub enum ResolveProjectOutcome {
    Resolved(uuid::Uuid),
    None,
    Ambiguous,
}

/// Wire shape of a successful `resolve_project` response.
#[derive(Deserialize)]
struct ResolveProjectResponse {
    project_id: uuid::Uuid,
}

impl ApiClient {
    pub fn new(base_url: &str, api_key: Option<&str>) -> Self {
        Self {
            base_url: base_url.trim_end_matches('/').to_string(),
            api_key: api_key.map(String::from),
            client: reqwest::Client::builder()
                .timeout(std::time::Duration::from_secs(60))
                .build()
                .unwrap_or_default(),
        }
    }

    pub async fn register_repo(
        &self,
        req: RegisterRepoRequest,
    ) -> Result<RegisterRepoResponse, Box<dyn Error>> {
        let builder = self
            .client
            .post(format!("{}/api/v1/repos", self.base_url))
            .json(&req);
        self.authed_send_json(builder, |status| format!("Server returned {status}"))
            .await
    }

    pub async fn device_start(&self) -> Result<DeviceAuthResponse, Box<dyn Error>> {
        let resp = self
            .client
            // Send an explicit `Content-Length: 0`. reqwest/hyper omit the header
            // entirely for a bodyless POST, and strict frontends (e.g. Google
            // Front End) reject such requests with `411 Length Required`.
            .post(format!("{}/api/v1/auth/device", self.base_url))
            .header(reqwest::header::CONTENT_LENGTH, "0")
            .send()
            .await?;

        if !resp.status().is_success() {
            let status = resp.status();
            let body = resp.text().await.unwrap_or_default();
            return Err(format!("Server returned {status}: {body}").into());
        }

        Ok(resp.json().await?)
    }

    pub async fn device_status(&self, token: &str) -> Result<DeviceStatusResponse, Box<dyn Error>> {
        let resp = self
            .client
            .get(format!(
                "{}/api/v1/auth/device/{token}/status",
                self.base_url
            ))
            .send()
            .await?;

        if !resp.status().is_success() {
            let status = resp.status();
            let body = resp.text().await.unwrap_or_default();
            return Err(format!("Server returned {status}: {body}").into());
        }

        Ok(resp.json().await?)
    }

    pub async fn logout(&self) -> Result<(), Box<dyn Error>> {
        let mut builder = self
            .client
            .post(format!("{}/api/v1/auth/logout", self.base_url));
        if let Some(key) = &self.api_key {
            builder = builder.header("Authorization", format!("Bearer {key}"));
        }
        // Explicit `Content-Length: 0` (see `device_start`): reqwest/hyper omit it
        // for a bodyless POST, which strict frontends reject with 411.
        let resp = builder
            .header(reqwest::header::CONTENT_LENGTH, "0")
            .send()
            .await?;
        if !resp.status().is_success() {
            let status = resp.status();
            let body = resp.text().await.unwrap_or_default();
            return Err(format!("Server returned {status}: {body}").into());
        }
        Ok(())
    }

    /// Attach the bearer token (if configured) to a request. Shared by every
    /// authenticated request builder so header attachment has exactly one
    /// implementation.
    fn attach_auth(&self, builder: reqwest::RequestBuilder) -> reqwest::RequestBuilder {
        match &self.api_key {
            Some(key) => builder.header("Authorization", format!("Bearer {key}")),
            None => builder,
        }
    }

    /// Attach the bearer token and send `builder`. The shared first step of
    /// every authenticated request; callers that need bespoke status-code
    /// handling (e.g. treating 404/409 as non-error outcomes) use this
    /// directly instead of `authed_send_json`.
    async fn send_authed(
        &self,
        builder: reqwest::RequestBuilder,
    ) -> Result<reqwest::Response, Box<dyn Error>> {
        Ok(self.attach_auth(builder).send().await?)
    }

    /// Check that `resp`'s status is a success and deserialize its JSON
    /// body; otherwise build an error as `"{err_prefix(status)}: {body}"`.
    /// `err_prefix` receives the status so each caller can format its own
    /// distinct message (parenthesized status, bare status, ...) — this
    /// helper only supplies the shared "check status, else deserialize"
    /// shape, not the message wording.
    async fn success_json<T, F>(resp: reqwest::Response, err_prefix: F) -> Result<T, Box<dyn Error>>
    where
        T: serde::de::DeserializeOwned,
        F: FnOnce(reqwest::StatusCode) -> String,
    {
        let status = resp.status();
        if !status.is_success() {
            let body = resp.text().await.unwrap_or_default();
            return Err(format!("{}: {}", err_prefix(status), body).into());
        }
        Ok(resp.json().await?)
    }

    /// Attach the bearer token, send `builder`, and deserialize a successful
    /// JSON response — the shared shape of every simple authenticated
    /// request/response method (`send_authed` + `success_json`).
    ///
    /// NOTE: callers whose `err_prefix` renders the status as `"({status})"`
    /// (e.g. `stream_event`'s `"Stream failed ({status})"`) produce error
    /// strings containing `"(404 "`/`"(403 "`/etc. —
    /// `commands::stream::is_deterministic_client_error` matches on exactly
    /// that substring shape to classify a stream-send failure. If you change
    /// how a status is rendered here, check that predicate (and its tests)
    /// still pass.
    async fn authed_send_json<T, F>(
        &self,
        builder: reqwest::RequestBuilder,
        err_prefix: F,
    ) -> Result<T, Box<dyn Error>>
    where
        T: serde::de::DeserializeOwned,
        F: FnOnce(reqwest::StatusCode) -> String,
    {
        let resp = self.send_authed(builder).await?;
        Self::success_json(resp, err_prefix).await
    }

    /// GET `{base}{path}` with the bearer token, mapping failures into
    /// `GetMeError` (401 → `Unauthorized`, transport → `Network`, other
    /// non-2xx or bad JSON → `Server`). Shared by the credential-scoped GETs
    /// so auth/error handling lives in one place.
    async fn authed_get_json<T: serde::de::DeserializeOwned>(
        &self,
        path: &str,
    ) -> Result<T, GetMeError> {
        let builder = self.attach_auth(self.client.get(format!("{}{}", self.base_url, path)));

        let resp = builder
            .send()
            .await
            .map_err(|e| GetMeError::Network(e.to_string()))?;

        let status = resp.status();
        if status == reqwest::StatusCode::UNAUTHORIZED {
            return Err(GetMeError::Unauthorized);
        }
        if !status.is_success() {
            let body = resp.text().await.unwrap_or_default();
            return Err(GetMeError::Server(format!("{status}: {body}")));
        }

        resp.json::<T>()
            .await
            .map_err(|e| GetMeError::Server(e.to_string()))
    }

    /// GET /api/v1/auth/me — validates the bearer token and returns user
    /// identity. Used by `tracevault status` to distinguish "logged out",
    /// "expired token", and "server unreachable".
    pub async fn get_me(&self) -> Result<MeResponse, GetMeError> {
        self.authed_get_json("/api/v1/auth/me").await
    }

    pub async fn list_repos(&self) -> Result<Vec<RepoListItem>, Box<dyn Error>> {
        let builder = self.client.get(format!("{}/api/v1/repos", self.base_url));
        self.authed_send_json(builder, |status| format!("Failed to list repos ({status})"))
            .await
    }

    pub async fn get_agent_instructions(
        &self,
        repo_id: &uuid::Uuid,
    ) -> Result<AgentInstructionsResponse, Box<dyn Error>> {
        let builder = self.client.get(format!(
            "{}/api/v1/repos/{}/policies/agent-instructions",
            self.base_url, repo_id
        ));
        self.authed_send_json(builder, |status| {
            format!("Failed to fetch agent instructions ({status})")
        })
        .await
    }

    pub async fn verify_commits(
        &self,
        repo_id: &uuid::Uuid,
        req: CiVerifyRequest,
    ) -> Result<CiVerifyResponse, Box<dyn Error>> {
        let builder = self
            .client
            .post(format!(
                "{}/api/v1/repos/{}/ci/verify",
                self.base_url, repo_id
            ))
            .json(&req);
        self.authed_send_json(builder, |status| format!("CI verify failed ({status})"))
            .await
    }

    pub async fn push_commit(
        &self,
        repo_id: &str,
        req: &tracevault_protocol::streaming::CommitPushRequest,
    ) -> Result<tracevault_protocol::streaming::CommitPushResponse, Box<dyn Error>> {
        let builder = self
            .client
            .post(format!(
                "{}/api/v1/repos/{}/commits",
                self.base_url, repo_id
            ))
            .json(req);
        self.authed_send_json(builder, |status| format!("Commit push failed ({status})"))
            .await
    }

    pub async fn stream_event(
        &self,
        repo_id: &str,
        req: &tracevault_protocol::streaming::StreamEventRequest,
    ) -> Result<tracevault_protocol::streaming::StreamEventResponse, Box<dyn Error>> {
        let builder = self
            .client
            .post(format!("{}/api/v1/repos/{}/stream", self.base_url, repo_id))
            .json(req);
        self.authed_send_json(builder, |status| format!("Stream failed ({status})"))
            .await
    }

    /// Project-scoped variant of `stream_event`: posts to the project's
    /// stream endpoint with `repo_id` as a query param instead of a path
    /// segment. The query is built with `Url::query_pairs_mut`, mirroring
    /// `resolve_project`'s `?git_url=`, so `repo_id` is percent-encoded
    /// rather than string-interpolated into the URL.
    ///
    /// Called from `commands::stream::send_stream_event` when a local
    /// project binding resolves for the capturing event.
    pub async fn stream_event_for_project(
        &self,
        project_id: uuid::Uuid,
        repo_id: &str,
        req: &tracevault_protocol::streaming::StreamEventRequest,
    ) -> Result<tracevault_protocol::streaming::StreamEventResponse, Box<dyn Error>> {
        let mut url = Url::parse(&format!(
            "{}/api/v1/projects/{}/stream",
            self.base_url, project_id
        ))?;
        url.query_pairs_mut().append_pair("repo_id", repo_id);
        let builder = self.client.post(url).json(req);
        self.authed_send_json(builder, |status| {
            format!("Project stream failed ({status})")
        })
        .await
    }

    pub async fn check_policies(
        &self,
        repo_id: &uuid::Uuid,
        req: CheckPoliciesRequest,
    ) -> Result<CheckPoliciesResponse, Box<dyn Error>> {
        let builder = self
            .client
            .post(format!(
                "{}/api/v1/repos/{}/policies/check",
                self.base_url, repo_id
            ))
            .json(&req);
        self.authed_send_json(builder, |status| format!("Policy check failed ({status})"))
            .await
    }

    /// Resolve a git URL to its codebase (git remote) by NORMALIZED URL —
    /// deduped, unlike an exact `github_url` match. `Ok(None)` if the
    /// codebase isn't tracked: a genuine domain 404, identified by the
    /// server's JSON error envelope (see [`is_domain_error_body`]). A bare
    /// 404 with no such body — axum's built-in "no route matched" fallback
    /// — is surfaced as `Err` instead, since it means this server doesn't
    /// have this route at all (most plausibly a CLI/server version skew,
    /// e.g. this CLI shipped ahead of a server that still uses the old
    /// org-scoped paths) rather than an absent codebase.
    pub async fn resolve_remote(
        &self,
        git_url: &str,
    ) -> Result<Option<ResolveRemoteResponse>, Box<dyn std::error::Error>> {
        let mut url = Url::parse(&format!("{}/api/v1/remotes/resolve", self.base_url))?;
        url.query_pairs_mut().append_pair("git_url", git_url);
        let resp = self.send_authed(self.client.get(url)).await?;
        if resp.status() == reqwest::StatusCode::NOT_FOUND {
            let body = resp.text().await.unwrap_or_default();
            return if is_domain_error_body(&body) {
                Ok(None)
            } else {
                Err(version_skew_404_error("resolve_remote"))
            };
        }
        let parsed: ResolveRemoteResponse =
            Self::success_json(resp, |status| format!("resolve_remote failed ({status})")).await?;
        Ok(Some(parsed))
    }

    /// Full detail for a remote (codebase): its display name, normalized URL,
    /// clone status, and linked repos.
    pub async fn get_remote_detail(
        &self,
        remote_id: uuid::Uuid,
    ) -> Result<RemoteDetail, Box<dyn std::error::Error>> {
        let builder = self
            .client
            .get(format!("{}/api/v1/remotes/{}", self.base_url, remote_id));
        self.authed_send_json(builder, |status| {
            format!("get_remote_detail failed ({status})")
        })
        .await
    }

    /// The repos linked to a remote (the codebase's members).
    pub async fn get_remote_repos(
        &self,
        remote_id: uuid::Uuid,
    ) -> Result<Vec<RemoteRepoRef>, Box<dyn std::error::Error>> {
        Ok(self.get_remote_detail(remote_id).await?.repos)
    }

    /// List all projects. `GET /api/v1/projects`.
    pub async fn list_projects(&self) -> Result<Vec<ProjectListItem>, Box<dyn Error>> {
        let builder = self
            .client
            .get(format!("{}/api/v1/projects", self.base_url));
        self.authed_send_json(builder, |status| {
            format!("Failed to list projects ({status})")
        })
        .await
    }

    /// Full detail for a project. `GET /api/v1/projects/{id}`.
    pub async fn get_project(&self, id: uuid::Uuid) -> Result<ProjectDetail, Box<dyn Error>> {
        let builder = self
            .client
            .get(format!("{}/api/v1/projects/{}", self.base_url, id));
        self.authed_send_json(builder, |status| {
            format!("Failed to get project ({status})")
        })
        .await
    }

    /// Resolve a git URL to its project, distinguishing "no project" (a
    /// genuine domain 404 — see [`is_domain_error_body`]) from "ambiguous,
    /// multiple candidate projects" (409). A bare 404 with no domain-error
    /// body is surfaced as `Err` instead of `None` (see `resolve_remote`'s
    /// doc comment for why). `GET /api/v1/projects/resolve?git_url=`.
    pub async fn resolve_project(
        &self,
        git_url: &str,
    ) -> Result<ResolveProjectOutcome, Box<dyn std::error::Error>> {
        let mut url = Url::parse(&format!("{}/api/v1/projects/resolve", self.base_url))?;
        url.query_pairs_mut().append_pair("git_url", git_url);
        let resp = self.send_authed(self.client.get(url)).await?;
        let status = resp.status();
        if status == reqwest::StatusCode::NOT_FOUND {
            let body = resp.text().await.unwrap_or_default();
            return if is_domain_error_body(&body) {
                Ok(ResolveProjectOutcome::None)
            } else {
                Err(version_skew_404_error("resolve_project"))
            };
        }
        if status == reqwest::StatusCode::CONFLICT {
            return Ok(ResolveProjectOutcome::Ambiguous);
        }
        let parsed: ResolveProjectResponse =
            Self::success_json(resp, |status| format!("resolve_project failed ({status})")).await?;
        Ok(ResolveProjectOutcome::Resolved(parsed.project_id))
    }
}

/// Whether a 404 response body matches the server's JSON error envelope —
/// `{"error": "...", ...}`, emitted by every domain-level `AppError` (see
/// `tracevault-server`'s `error.rs`) — rather than the empty/plain body
/// axum's built-in "no route matched" fallback sends when the server has no
/// handler for the path at all. Used by `resolve_remote`/`resolve_project`
/// to tell a genuine "not tracked"/"no project" 404 apart from a 404 that
/// really means "this server doesn't have this route" (most plausibly a
/// CLI/server version skew).
fn is_domain_error_body(body: &str) -> bool {
    serde_json::from_str::<serde_json::Value>(body)
        .ok()
        .and_then(|v| v.get("error").map(|_| ()))
        .is_some()
}

/// The error for a 404 whose body doesn't match the server's JSON error
/// envelope (see [`is_domain_error_body`]) — surfaced instead of silently
/// treating the call as "not tracked"/"no project", since this shape is the
/// CLI's best signal that the server doesn't recognize `what`'s route at
/// all, rather than a genuine absence of the resource.
fn version_skew_404_error(what: &str) -> Box<dyn std::error::Error> {
    format!(
        "{what} got a 404 with no recognizable error body — this endpoint may not exist on \
         this server yet. This usually means the CLI is newer than the server (a CLI/server \
         version mismatch); confirm the server has been upgraded and retry."
    )
    .into()
}

/// Resolve server URL and auth token from multiple sources.
/// Priority: env var > credentials file > project config.toml
/// Returns (server_url, auth_token).
pub fn resolve_credentials(project_root: &Path) -> (Option<String>, Option<String>) {
    use crate::credentials::Credentials;

    // 1. Env var API key
    let env_key = std::env::var("TRACEVAULT_API_KEY").ok();

    // 2. Credentials file
    let creds = Credentials::load();

    // 3. Project config
    let config_path = crate::config::TracevaultConfig::config_path(project_root);
    let config_content = std::fs::read_to_string(&config_path).unwrap_or_default();

    let config_server_url = config_content
        .lines()
        .find(|l| l.starts_with("server_url"))
        .and_then(|l| l.split('=').nth(1))
        .map(|s| s.trim().trim_matches('"').to_string());

    let config_api_key = config_content
        .lines()
        .find(|l| l.starts_with("api_key"))
        .and_then(|l| l.split('=').nth(1))
        .map(|s| s.trim().trim_matches('"').to_string());

    // Resolve server URL: env > creds > config
    let server_url = std::env::var("TRACEVAULT_SERVER_URL")
        .ok()
        .or_else(|| creds.as_ref().map(|c| c.server_url.clone()))
        .or(config_server_url);

    // Resolve token: env api key > creds token > config api key
    let token = env_key
        .or_else(|| creds.map(|c| c.token))
        .or(config_api_key);

    (server_url, token)
}

/// Resolve `project_root`'s credentials (via `resolve_credentials`) into a
/// ready `ApiClient`, or the standard "no server URL configured" error when
/// none of the credential sources yield a server URL. Shared by every
/// command that needs a client from a project root, so this
/// resolve-then-construct shape has exactly one implementation.
pub fn resolve_client(project_root: &Path) -> Result<ApiClient, Box<dyn Error>> {
    let (server_url, token) = resolve_credentials(project_root);
    let server_url = server_url
        .ok_or("no server URL configured: set TRACEVAULT_SERVER_URL or run `tracevault login`")?;
    Ok(ApiClient::new(&server_url, token.as_deref()))
}