tensorlake 0.5.75

A Rust SDK for interacting with Tensorlake Cloud APIs.
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
//! Workspace client: private durable refs (`refs/workspaces/<id>`) on artifact storage.
//!
//! A workspace is the unit of ongoing work (artifact_storage issue #24): created from a base
//! commit, advanced by snapshots (ordinary commits on the workspace ref, uploaded as CDC chunks
//! through the resumable-ingest pipeline), and published by promoting a snapshot onto a real
//! branch (squash by default). Workspaces are durable scratch pads — they survive crashes,
//! timeouts, and unmounts, and die only by explicit deletion. All calls authenticate with a
//! minted git credential, like the repo/commit APIs.

use reqwest::Method;
use serde::{Deserialize, Serialize};

use crate::Traced;
use crate::error::SdkError;

use super::ingest::expect_json;
use super::merge::{MergeConflict, MergeReport, MergeStats, Signature, expect_json_or_conflict};
use super::{ArtifactStorageClient, decode_empty};

/// One workspace joined across its identity record, ref, and lease rows.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct WorkspaceInfo {
    pub id: String,
    pub ref_name: String,
    /// Principal bound at creation; empty for open-mode workspaces.
    pub principal: String,
    /// Base commit (hex) the workspace was created from.
    pub base: String,
    /// The ref the base was resolved from, when created from one; display-only.
    pub base_ref: Option<String>,
    /// Current snapshot tip (hex); equals `base` until the first snapshot.
    pub head: String,
    pub created_at_secs: u64,
    pub lease_secs: u64,
    /// Epoch-ms lease expiry. `None` means pinned: never expires.
    pub lease_due_ms: Option<u64>,
    pub pinned: bool,
    /// Shared-rw mode: every snapshot is automatically reconciled into this branch (short
    /// name), in server-assigned order.
    #[serde(default)]
    pub shared_target: Option<String>,
}

#[derive(Clone, Debug, Default, Serialize)]
pub struct CreateWorkspaceRequest {
    /// Base commit-ish (branch, full ref, tag, or commit hex). Defaults to the repo's HEAD.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub base: Option<String>,
    /// Shared-rw mode: reconcile every snapshot into this branch (short name) in server order.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub shared_target: Option<String>,
    /// Shared-rw conflict handling: `"materialize"` (default — three-way merge, diff3 markers
    /// on conflicts) or `"lww"` (legacy last-writer-wins overwrite).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reconcile_policy: Option<String>,
    /// Which product surface this session belongs to: `"filesystem"` makes the SERVER apply
    /// filesystem semantics from the repo's authoritative kind (publish-on-save defaults;
    /// kind=repository rejected with the right command). Requires a server with repo
    /// surface support (issue #103); older servers ignore the field.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub surface: Option<String>,
    /// The session will never write (a read-only mount's anchor): the surface kind check
    /// still applies, publish defaults are skipped.
    #[serde(skip_serializing_if = "std::ops::Not::not")]
    pub read_only: bool,
}

#[derive(Clone, Debug, Deserialize)]
pub struct WorkspaceHeartbeat {
    /// New lease expiry (epoch ms); absent when the workspace is pinned.
    pub lease_due_ms: Option<u64>,
    pub pinned: bool,
}

#[derive(Clone, Debug, Default, Serialize)]
pub struct PromoteWorkspaceRequest {
    /// Target branch (short name; `refs/heads/` implied).
    pub branch: String,
    /// Expected target branch head; defaults to the observed head (CAS either way).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub expect_oid: Option<String>,
    /// Land the full checkpoint chain instead of the default squash.
    #[serde(skip_serializing_if = "std::ops::Not::not")]
    pub full_history: bool,
    /// `"squash"` (default) | `"full_history"` | `"merge"`. Takes precedence over the legacy
    /// `full_history` flag. `merge` lands a two-parent merge commit when the target moved
    /// since the workspace forked; conflicts come back as `PromoteOutcome::Conflicted`
    /// (`fail` policy — the workspace is the resolution surface).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub mode: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub message: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub author: Option<Signature>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct PromoteWorkspaceResponse {
    pub commit: String,
    pub ref_name: String,
    pub created: bool,
    pub squashed: bool,
    /// Merge-mode promote landed a merge (or found nothing to do).
    #[serde(default)]
    pub merged: bool,
    /// Merge mode advanced the ref to the workspace tip with no new commit object.
    #[serde(default)]
    pub fast_forwarded: bool,
}

/// A merge-mode promote either lands or reports why it can't — conflicts are a normal outcome,
/// not a transport error (the server's `409` report body).
#[derive(Clone, Debug)]
pub enum PromoteOutcome {
    Promoted(PromoteWorkspaceResponse),
    /// `fail`-policy conflicts: nothing was published; the target branch and the workspace are
    /// both untouched. Sync the workspace, resolve, and promote again.
    Conflicted(MergeReport),
}

/// Ref-level promote preflight: did the target move since the workspace forked?
/// (Tree-level conflict prediction is `repo_merge` in preflight mode.)
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct PromotePreflight {
    pub target_ref: String,
    /// Absent when the target branch does not exist yet.
    pub target_head: Option<String>,
    pub workspace_base: String,
    pub workspace_head: String,
    pub moved: bool,
    pub fast_forward: bool,
}

#[derive(Clone, Debug, Default, Serialize)]
pub struct SyncWorkspaceRequest {
    /// Branch to pull from (short name). Defaults to the workspace's shared-rw target, else
    /// the ref it was created from.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub target: Option<String>,
    /// `"materialize"` (default — conflicts land as diff3 markers in the workspace, with the
    /// commit's structured conflict record) or `"fail"` (a conflicted sync changes nothing and
    /// returns the report).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub policy: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub message: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub author: Option<Signature>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct SyncWorkspaceResponse {
    /// The workspace ref after the sync (unchanged when `up_to_date`, or when a
    /// `fail`-policy sync hit conflicts).
    pub workspace_head: String,
    pub target_head: String,
    /// The workspace's fork point after the sync.
    pub base: String,
    pub clean: bool,
    pub up_to_date: bool,
    /// The workspace had no snapshots: its ref moved to the target head with no new commit.
    pub fast_forwarded: bool,
    pub changed_paths: u64,
    pub conflicts: Vec<MergeConflict>,
    pub stats: MergeStats,
}

/// One ref's head and movement generation — the poll target for branch/workspace following.
#[derive(Clone, Debug, Deserialize)]
pub struct RefStatus {
    pub ref_name: String,
    /// Current head (hex); absent when the ref does not exist.
    pub oid: Option<String>,
    /// Movement counter: bumps on every write or delete of the ref. 0 = never written.
    pub generation: u64,
}

/// One directory page from the paged tree listing.
#[derive(Clone, Debug, Deserialize)]
pub struct TreePage {
    pub entries: Vec<TreeEntry>,
    pub truncated: bool,
    pub next_after: Option<String>,
}

#[derive(Clone, Debug, Deserialize)]
pub struct TreeEntry {
    pub name: String,
    pub oid: String,
    /// Raw git mode (`0o100644`, `0o100755`, `0o120000`, `0o40000` for directories).
    pub mode: u32,
    /// Blob size when cheaply known server-side.
    pub size: Option<u64>,
}

/// One row of the project-scope workspace fleet listing
/// (`GET /project/{project}/workspaces`, artifact_storage issue #56).
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct WorkspaceFleetItem {
    pub id: String,
    /// Full repo id (`{project}/{repo}`).
    pub repo: String,
    /// Derived server-side at read time: `live` | `gap` | `idle` | `detached`.
    pub status: String,
    /// `default` | `shared-rw`.
    pub mode: String,
    /// Creating principal; absent for unbound (open-mode) workspaces.
    #[serde(default)]
    pub created_by: Option<WorkspaceActor>,
    /// Base commit (hex) the workspace was created from.
    pub base: String,
    #[serde(default)]
    pub base_ref: Option<String>,
    /// Current snapshot tip (hex); equals `base` until the first snapshot.
    pub head: String,
    pub created_at_secs: u64,
    /// Lease duration from the identity record; 0 for durable (modern) workspaces. Defaults
    /// to 0 against servers that predate the field.
    #[serde(default)]
    pub lease_secs: u64,
    /// Epoch-ms lease expiry. `None` means pinned: never expires. Servers that predate the
    /// field omit it, which decodes as pinned — correct for every durable workspace.
    #[serde(default)]
    pub lease_due_ms: Option<u64>,
    #[serde(default)]
    pub shared_target: Option<String>,
    #[serde(default)]
    pub snapshot_count: u64,
    #[serde(default)]
    pub last_snapshot_ms: Option<u64>,
    #[serde(default)]
    pub last_heartbeat_ms: Option<u64>,
    /// Host reported by the currently-open mount session, if any.
    #[serde(default)]
    pub mounted_on: Option<String>,
}

/// A workspace's creating principal: the authenticated `sub` and its human/agent classification.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct WorkspaceActor {
    pub name: String,
    /// `human` | `agent` | `unknown`.
    pub kind: String,
}

/// Per-status totals for the fleet page's filter set (status filter excluded).
#[derive(Clone, Debug, Default, Deserialize)]
pub struct WorkspaceFleetCounts {
    pub total: u64,
    pub live: u64,
    pub detached: u64,
    pub gap: u64,
    pub idle: u64,
    /// Counts cover only the server's bounded scan prefix when the fleet exceeds its cap.
    pub truncated: bool,
}

/// One page of the project-scope workspace fleet.
#[derive(Clone, Debug, Deserialize)]
pub struct WorkspaceFleetPage {
    pub project: String,
    pub items: Vec<WorkspaceFleetItem>,
    pub counts: WorkspaceFleetCounts,
    /// More items exist past this page; resume with `after = next_after`.
    pub truncated: bool,
    #[serde(default)]
    pub next_after: Option<String>,
}

/// Filters for one fleet page. `repo` accepts the bare repo name (the server also matches the
/// full `{project}/{repo}` id); `q` is an id substring match.
#[derive(Clone, Copy, Debug, Default)]
pub struct WorkspaceFleetQuery<'a> {
    pub repo: Option<&'a str>,
    pub q: Option<&'a str>,
    /// Ask the server for the caller's own + unbound workspaces only (`principal=self`).
    /// Servers that predate the param ignore it — callers relying on the narrowing must also
    /// filter client-side.
    pub principal_self: bool,
    pub after: Option<&'a str>,
    /// Page size; the server clamps to its own cap (200).
    pub limit: Option<usize>,
}

impl ArtifactStorageClient {
    pub async fn create_workspace(
        &self,
        project_id: &str,
        repo: &str,
        git_username: &str,
        git_token: &str,
        request: &CreateWorkspaceRequest,
    ) -> Result<Traced<WorkspaceInfo>, SdkError> {
        let (req, trace_id) = self.git_request(
            Method::POST,
            project_id,
            repo,
            Some("workspaces"),
            git_username,
            git_token,
        )?;
        let info = expect_json(req.json(request).send().await?).await?;
        Ok(Traced::new(trace_id, info))
    }

    pub async fn get_workspace(
        &self,
        project_id: &str,
        repo: &str,
        git_username: &str,
        git_token: &str,
        workspace_id: &str,
    ) -> Result<Traced<WorkspaceInfo>, SdkError> {
        let (req, trace_id) = self.git_request(
            Method::GET,
            project_id,
            repo,
            Some(&format!("workspaces/{workspace_id}")),
            git_username,
            git_token,
        )?;
        let info = expect_json(req.send().await?).await?;
        Ok(Traced::new(trace_id, info))
    }

    /// One page of the project-scope workspace fleet: every repo in one call, joined with
    /// liveness (status, `mounted_on`). Requires a project-wide credential — repo-scoped mints
    /// carry no `project:read` and are refused. Page with `next_after` until `truncated` is
    /// false.
    pub async fn workspace_fleet(
        &self,
        project_id: &str,
        git_username: &str,
        git_token: &str,
        query: &WorkspaceFleetQuery<'_>,
    ) -> Result<Traced<WorkspaceFleetPage>, SdkError> {
        let mut params = url::form_urlencoded::Serializer::new(String::new());
        if let Some(repo) = query.repo {
            params.append_pair("repo", repo);
        }
        if let Some(q) = query.q {
            params.append_pair("q", q);
        }
        if query.principal_self {
            params.append_pair("principal", "self");
        }
        if let Some(after) = query.after {
            params.append_pair("after", after);
        }
        if let Some(limit) = query.limit {
            params.append_pair("limit", &limit.to_string());
        }
        let params = params.finish();
        let suffix = if params.is_empty() {
            "workspaces".to_string()
        } else {
            format!("workspaces?{params}")
        };
        let (req, trace_id) =
            self.project_git_request(Method::GET, project_id, &suffix, git_username, git_token);
        let page = expect_json(req.send().await?).await?;
        Ok(Traced::new(trace_id, page))
    }

    /// The complete fleet for the given filters: pages internally (like every other list
    /// helper in this crate) until the server reports no more rows, starting from `query.after`
    /// if set. Returns the items and the trace id of the final page.
    pub async fn workspace_fleet_all(
        &self,
        project_id: &str,
        git_username: &str,
        git_token: &str,
        query: &WorkspaceFleetQuery<'_>,
    ) -> Result<Traced<Vec<WorkspaceFleetItem>>, SdkError> {
        let mut items = Vec::new();
        let mut after: Option<String> = query.after.map(str::to_string);
        loop {
            let page = self
                .workspace_fleet(
                    project_id,
                    git_username,
                    git_token,
                    &WorkspaceFleetQuery {
                        after: after.as_deref(),
                        ..*query
                    },
                )
                .await?;
            let trace_id = page.trace_id.clone();
            let page = page.into_inner();
            items.extend(page.items);
            if !page.truncated {
                return Ok(Traced::new(trace_id, items));
            }
            let Some(next) = page.next_after else {
                return Ok(Traced::new(trace_id, items));
            };
            after = Some(next);
        }
    }

    pub async fn list_workspaces(
        &self,
        project_id: &str,
        repo: &str,
        git_username: &str,
        git_token: &str,
    ) -> Result<Traced<Vec<WorkspaceInfo>>, SdkError> {
        let (req, trace_id) = self.git_request(
            Method::GET,
            project_id,
            repo,
            Some("workspaces"),
            git_username,
            git_token,
        )?;
        let list = expect_json(req.send().await?).await?;
        Ok(Traced::new(trace_id, list))
    }

    /// Re-arm the workspace's activity lease. Pinned workspaces heartbeat as a no-op.
    pub async fn workspace_heartbeat(
        &self,
        project_id: &str,
        repo: &str,
        git_username: &str,
        git_token: &str,
        workspace_id: &str,
    ) -> Result<Traced<WorkspaceHeartbeat>, SdkError> {
        let (req, trace_id) = self.git_request(
            Method::POST,
            project_id,
            repo,
            Some(&format!("workspaces/{workspace_id}/heartbeat")),
            git_username,
            git_token,
        )?;
        let hb = expect_json(req.send().await?).await?;
        Ok(Traced::new(trace_id, hb))
    }

    /// CAS-advance a real branch to the workspace's snapshot (squash by default). Merge mode
    /// (`mode: "merge"`) lands a two-parent merge commit when the target moved; its conflicts
    /// are returned as `PromoteOutcome::Conflicted`, not an error. A plain `409` (concurrent
    /// ref move) still surfaces as `SdkError::ServerError`.
    pub async fn workspace_promote(
        &self,
        project_id: &str,
        repo: &str,
        git_username: &str,
        git_token: &str,
        workspace_id: &str,
        request: &PromoteWorkspaceRequest,
    ) -> Result<Traced<PromoteOutcome>, SdkError> {
        let (req, trace_id) = self.git_request(
            Method::POST,
            project_id,
            repo,
            Some(&format!("workspaces/{workspace_id}/promote")),
            git_username,
            git_token,
        )?;
        let outcome = expect_json_or_conflict::<PromoteWorkspaceResponse, MergeReport>(
            req.json(request).send().await?,
        )
        .await?
        .map(PromoteOutcome::Promoted)
        .unwrap_or_else(PromoteOutcome::Conflicted);
        Ok(Traced::new(trace_id, outcome))
    }

    /// Ref-level promote preflight: whether `target` moved since the workspace forked, and
    /// whether a promote would fast-forward.
    pub async fn workspace_promote_preflight(
        &self,
        project_id: &str,
        repo: &str,
        git_username: &str,
        git_token: &str,
        workspace_id: &str,
        target: &str,
    ) -> Result<Traced<PromotePreflight>, SdkError> {
        let suffix = format!(
            "workspaces/{workspace_id}/promote/preflight?target={}",
            url::form_urlencoded::byte_serialize(target.as_bytes()).collect::<String>()
        );
        let (req, trace_id) = self.git_request(
            Method::GET,
            project_id,
            repo,
            Some(&suffix),
            git_username,
            git_token,
        )?;
        let preflight = expect_json(req.send().await?).await?;
        Ok(Traced::new(trace_id, preflight))
    }

    /// Pull the target branch into a behind workspace, rebase-style: one merge commit on the
    /// target head, workspace history kept linear, the pre-sync chain preserved under a
    /// presync ref. Under the default `materialize` policy conflicts land as diff3 markers in
    /// the workspace; under `fail` a conflicted sync changes nothing and the returned report
    /// has `clean: false` with `workspace_head` untouched. Snapshot uncommitted mount changes
    /// before syncing.
    pub async fn workspace_sync(
        &self,
        project_id: &str,
        repo: &str,
        git_username: &str,
        git_token: &str,
        workspace_id: &str,
        request: &SyncWorkspaceRequest,
    ) -> Result<Traced<SyncWorkspaceResponse>, SdkError> {
        let (req, trace_id) = self.git_request(
            Method::POST,
            project_id,
            repo,
            Some(&format!("workspaces/{workspace_id}/sync")),
            git_username,
            git_token,
        )?;
        let resp = expect_json_or_conflict::<SyncWorkspaceResponse, SyncWorkspaceResponse>(
            req.json(request).send().await?,
        )
        .await?
        .unwrap_or_else(|conflicted| conflicted);
        Ok(Traced::new(trace_id, resp))
    }

    pub async fn delete_workspace(
        &self,
        project_id: &str,
        repo: &str,
        git_username: &str,
        git_token: &str,
        workspace_id: &str,
    ) -> Result<Traced<()>, SdkError> {
        let (req, trace_id) = self.git_request(
            Method::DELETE,
            project_id,
            repo,
            Some(&format!("workspaces/{workspace_id}")),
            git_username,
            git_token,
        )?;
        decode_empty(req.send().await?, trace_id).await
    }

    /// One ref's head + movement generation (`?ref=` accepts a short branch or full ref name).
    pub async fn ref_status(
        &self,
        project_id: &str,
        repo: &str,
        git_username: &str,
        git_token: &str,
        refspec: &str,
    ) -> Result<Traced<RefStatus>, SdkError> {
        let suffix = format!(
            "ref-status?ref={}",
            url::form_urlencoded::byte_serialize(refspec.as_bytes()).collect::<String>()
        );
        let (req, trace_id) = self.git_request(
            Method::GET,
            project_id,
            repo,
            Some(&suffix),
            git_username,
            git_token,
        )?;
        let status = expect_json(req.send().await?).await?;
        Ok(Traced::new(trace_id, status))
    }

    /// One page of a directory listing at `version` (commit hex, branch, or ref).
    pub async fn list_tree_page(
        &self,
        project_id: &str,
        repo: &str,
        git_username: &str,
        git_token: &str,
        version: &str,
        dir_path: &str,
        after: Option<&str>,
        limit: usize,
    ) -> Result<Traced<TreePage>, SdkError> {
        let enc = |s: &str| url::form_urlencoded::byte_serialize(s.as_bytes()).collect::<String>();
        let mut suffix = if dir_path.is_empty() {
            format!("tree?version={}&limit={limit}", enc(version))
        } else {
            format!("tree/{}?version={}&limit={limit}", dir_path, enc(version))
        };
        if let Some(after) = after {
            suffix.push_str(&format!("&after={}", enc(after)));
        }
        let (req, trace_id) = self.git_request(
            Method::GET,
            project_id,
            repo,
            Some(&suffix),
            git_username,
            git_token,
        )?;
        let page = expect_json(req.send().await?).await?;
        Ok(Traced::new(trace_id, page))
    }

    /// Raw file bytes at `version`.
    pub async fn get_file_bytes(
        &self,
        project_id: &str,
        repo: &str,
        git_username: &str,
        git_token: &str,
        version: &str,
        file_path: &str,
    ) -> Result<Traced<Vec<u8>>, SdkError> {
        let suffix = format!(
            "files/{file_path}?version={}",
            url::form_urlencoded::byte_serialize(version.as_bytes()).collect::<String>()
        );
        let (req, trace_id) = self.git_request(
            Method::GET,
            project_id,
            repo,
            Some(&suffix),
            git_username,
            git_token,
        )?;
        let resp = req.send().await?;
        if !resp.status().is_success() {
            let status = resp.status();
            let body = resp.text().await.unwrap_or_default();
            return Err(SdkError::ServerError {
                status,
                message: body,
            });
        }
        let bytes = resp.bytes().await?.to_vec();
        Ok(Traced::new(trace_id, bytes))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    /// Promote responses from servers that predate merge mode decode with the new fields
    /// defaulting to false.
    #[test]
    fn promote_response_tolerates_old_servers() {
        let body = r#"{"commit": "aa", "ref_name": "refs/heads/main", "created": false, "squashed": true}"#;
        let resp: PromoteWorkspaceResponse = serde_json::from_str(body).unwrap();
        assert!(resp.squashed);
        assert!(!resp.merged);
        assert!(!resp.fast_forwarded);
    }

    /// The fleet page decodes gsvc-server's exact wire shape (`fleet_item_json` /
    /// `WorkspaceFleetResponse` in `crates/gsvc-server/src/http.rs`) — every field the CLI
    /// listing consumes. The server side of this contract is pinned by the fleet e2e test in
    /// artifact_storage; change either together.
    #[test]
    fn fleet_page_decodes_server_shape() {
        let body = r#"{
            "project": "p1",
            "items": [{
                "id": "aabbccdd", "repo": "p1/demo", "status": "live", "mode": "shared-rw",
                "created_by": {"name": "user:u1", "kind": "human"},
                "base": "1111111111111111111111111111111111111111",
                "base_ref": "refs/heads/main",
                "head": "2222222222222222222222222222222222222222",
                "created_at_secs": 1751000000,
                "lease_secs": 3600,
                "lease_due_ms": 1751003600000,
                "shared_target": "main",
                "snapshot_count": 3,
                "last_snapshot_ms": 1751000500000,
                "last_heartbeat_ms": 1751000600000,
                "mounted_on": "sandbox-42"
            }],
            "counts": {"total": 1, "live": 1, "detached": 0, "gap": 0, "idle": 0, "truncated": false},
            "truncated": true,
            "next_after": "aabbccdd"
        }"#;
        let page: WorkspaceFleetPage = serde_json::from_str(body).unwrap();
        assert_eq!(page.project, "p1");
        assert_eq!(page.counts.total, 1);
        assert!(page.truncated);
        assert_eq!(page.next_after.as_deref(), Some("aabbccdd"));
        let item = &page.items[0];
        assert_eq!(item.id, "aabbccdd");
        assert_eq!(item.repo, "p1/demo");
        assert_eq!(item.status, "live");
        assert_eq!(item.mode, "shared-rw");
        assert_eq!(item.created_by.as_ref().unwrap().name, "user:u1");
        assert_eq!(item.created_by.as_ref().unwrap().kind, "human");
        assert_eq!(item.base_ref.as_deref(), Some("refs/heads/main"));
        assert_eq!(item.lease_secs, 3600);
        assert_eq!(item.lease_due_ms, Some(1751003600000));
        assert_eq!(item.snapshot_count, 3);
        assert_eq!(item.mounted_on.as_deref(), Some("sandbox-42"));
    }

    /// Optional fleet fields are `skip_serializing_if` on the server: an unbound, never-mounted,
    /// never-snapshotted workspace omits them entirely and must still decode. Lease fields also
    /// default (0 / pinned) against servers that predate them.
    #[test]
    fn fleet_item_tolerates_omitted_optionals() {
        let body = r#"{
            "project": "p1",
            "items": [{
                "id": "ee", "repo": "p1/demo", "status": "detached", "mode": "default",
                "base": "1111111111111111111111111111111111111111",
                "head": "1111111111111111111111111111111111111111",
                "created_at_secs": 1751000000,
                "snapshot_count": 0
            }],
            "counts": {"total": 1, "live": 0, "detached": 1, "gap": 0, "idle": 0, "truncated": false},
            "truncated": false
        }"#;
        let page: WorkspaceFleetPage = serde_json::from_str(body).unwrap();
        let item = &page.items[0];
        assert!(item.created_by.is_none(), "unbound workspace");
        assert!(item.base_ref.is_none());
        assert_eq!(item.lease_secs, 0, "old servers omit lease_secs");
        assert!(item.lease_due_ms.is_none(), "omitted lease row = pinned");
        assert!(item.shared_target.is_none());
        assert!(item.last_snapshot_ms.is_none());
        assert!(item.last_heartbeat_ms.is_none());
        assert!(item.mounted_on.is_none());
        assert!(page.next_after.is_none());
    }

    /// The sync response decodes the full server shape, conflicts included.
    #[test]
    fn sync_response_decodes_server_shape() {
        let body = r#"{
            "workspace_head": "ws1", "target_head": "t1", "base": "t1",
            "clean": false, "up_to_date": false, "fast_forwarded": false,
            "changed_paths": 2,
            "conflicts": [{"path": "f", "kind": "content", "potential": false,
                           "ours": {"mode": 33188, "oid": "o"}, "base": {"mode": 33188, "oid": "b"},
                           "theirs": {"mode": 33188, "oid": "t"}}],
            "stats": {"trees_read": 4, "entries_compared": 9, "blobs_merged": 1, "wall_ms": 0.7}
        }"#;
        let resp: SyncWorkspaceResponse = serde_json::from_str(body).unwrap();
        assert!(!resp.clean);
        assert_eq!(resp.base, "t1");
        assert_eq!(resp.conflicts.len(), 1);
        assert_eq!(resp.conflicts[0].kind, "content");
    }
}