tensorlake 0.5.140

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
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
pub mod desktop;
pub mod models;

use eventsource_stream::Eventsource;
use futures::{StreamExt, TryStreamExt};
use reqwest::Method;
use reqwest::StatusCode;
use reqwest::header::{ACCEPT, CONTENT_LENGTH};
use serde_json::Value;
use std::path::Path;
use std::time::{Duration, Instant};
use tokio::fs::File;
use tokio_util::io::ReaderStream;

use crate::{
    client::{
        Client, Traced, build_bytes_post_request_from_builder,
        build_empty_post_request_from_builder,
    },
    error::{SdkError, TransportFailure},
    retry::{RetryDecision, RetryPolicy, RetryState, is_transient},
};
pub use desktop::SandboxDesktopClient;

/// Default interval between `GET /sandboxes/{id}` polls while waiting for a
/// sandbox to run (ADR 0086).
pub const DEFAULT_WAIT_POLL_INTERVAL: Duration = Duration::from_secs(2);

/// Terminal reason a pending sandbox is given when its `max_pending_secs`
/// bound expires.
pub const TERMINATION_REASON_NO_CAPACITY: &str = "no_capacity";

/// Terminal reason of a pending sandbox that was deleted before it ran.
pub const TERMINATION_REASON_CANCELLED: &str = "cancelled";

/// Transient failures (502/503/504, a cut connection, a timed-out poll)
/// tolerated per wait loop between two successful observations. Each
/// observation resets the count, so a wait that spans a control-plane
/// rollout keeps going. A pending sandbox is not routable yet, so the
/// lifecycle gateway can answer a transient proxy error until it starts.
const WAIT_TRANSIENT_RETRIES: usize = 10;

/// Whether a sandbox `status` is one a readiness wait keeps polling through.
pub fn is_sandbox_pending(status: &str) -> bool {
    status == "pending"
}

/// Smallest HTTP timeout given to the first readiness poll of a wait, so a
/// wait with almost no budget can still observe the state once. Later polls
/// are capped at the remaining budget exactly: when it runs out the last
/// observation is returned instead of waiting for a slow answer.
pub const WAIT_POLL_MIN_REQUEST_TIMEOUT: Duration = Duration::from_secs(1);

use models::{
    ArchivedSandboxInfo, ArchivedSandboxesPaginationDirection, ClaimSandboxRequest,
    CopySandboxResponse, CreateSandboxPoolRequest, CreateSandboxPoolResponse, CreateSandboxRequest,
    CreateSandboxResponse, CreateSnapshotRequest, CreateSnapshotResponse, DaemonInfo,
    DetachFileSystemRequest, FileSystemMount, GetSandboxLogsRequest, HealthResponse,
    ListArchivedSandboxesParams, ListArchivedSandboxesResponse, ListDirectoryResponse,
    ListProcessesResponse, ListSandboxPoolsResponse, ListSandboxesResponse, ListSnapshotsResponse,
    NetworkPolicyUpdate, OutputEvent, OutputResponse, ProcessInfo, RunProcessEvent,
    SandboxAccepted, SandboxInfo, SandboxLogsResponse, SandboxPoolInfo, SandboxPoolRequest,
    SandboxProcessLogFiltersResponse, SendSignalResponse, SignBlobRequest, SnapshotInfo,
    SnapshotType, UpdateSandboxPoolRequest, UpdateSandboxRequest,
};

pub const DEFAULT_SANDBOX_PROXY_URL: &str = "https://sandbox.tensorlake.ai";
pub const SANDBOX_MANAGEMENT_PORT: u16 = 9501;

/// Wire-only response for claim-time configuration negotiation. Keep the
/// acknowledgement private so adding the protocol field does not make the
/// existing public [`CreateSandboxResponse`] source-incompatible for Rust
/// callers that construct it directly.
#[derive(Debug, serde::Deserialize)]
struct ClaimSandboxResponse {
    #[serde(flatten)]
    sandbox: CreateSandboxResponse,
    #[serde(default)]
    claim_configuration_applied: Option<bool>,
}

/// Client-side mirror of the server's HTTP 400 for invalid snapshot pins:
/// a mount that pins `snapshot_id` must also be `read_only`, so callers fail
/// fast (and offline) instead of round-tripping to the server.
fn validate_mount_snapshot_pins(mounts: &[FileSystemMount]) -> Result<(), SdkError> {
    for mount in mounts {
        if mount.snapshot_id.is_some() && !mount.read_only {
            return Err(SdkError::ClientError(format!(
                "file system mount {:?} at {:?} sets snapshot_id without read_only: \
                 snapshot-pinned mounts are read-only",
                mount.file_system_id, mount.mount_path
            )));
        }
    }
    Ok(())
}

/// Client-side mirror of the server's HTTP 400 for malformed mount owner
/// specs (`NAME`, `UID`, `NAME:GROUP`, or `UID:GID`), so callers fail fast
/// (and offline) instead of round-tripping to the server. Only the shape is
/// checked — a named user or group resolves against the sandbox image's own
/// user database when the mount attaches.
fn validate_mount_owners(mounts: &[FileSystemMount]) -> Result<(), SdkError> {
    const MAX_MOUNT_OWNER_BYTES: usize = 256;
    for mount in mounts {
        let Some(owner) = mount.owner.as_deref() else {
            continue;
        };
        let reject = |reason: &str| {
            Err(SdkError::ClientError(format!(
                "file system mount {:?} at {:?} has an invalid owner {owner:?}: {reason}",
                mount.file_system_id, mount.mount_path
            )))
        };
        if owner.is_empty() || owner.len() > MAX_MOUNT_OWNER_BYTES {
            return reject("owner must contain 1 to 256 bytes");
        }
        if !owner
            .bytes()
            .all(|byte| byte.is_ascii_graphic() && byte != b'"' && byte != b'\'')
        {
            return reject("owner must contain only non-whitespace printable ASCII characters");
        }
        let parts = owner.split(':').collect::<Vec<_>>();
        if parts.len() > 2 || parts.iter().any(|part| part.is_empty()) {
            return reject("owner must be NAME, UID, NAME:GROUP, or UID:GID");
        }
        // An all-digit part is unconditionally numeric to the guest resolver,
        // so a u32 overflow can never resolve.
        for part in parts {
            if part.bytes().all(|byte| byte.is_ascii_digit()) && part.parse::<u32>().is_err() {
                return reject("a numeric owner id must fit a 32-bit uid/gid");
            }
        }
    }
    Ok(())
}

/// A reference to a sandbox process: either its OS **pid** or a managed-process **name**
/// given at creation. This is the single place the pid/name path segment is built, reused by
/// the Rust SDK, the Python/Node bindings, and the CLI.
#[derive(Debug, Clone)]
pub enum ProcessRef {
    Pid(u64),
    Name(String),
}

impl ProcessRef {
    /// The `{pid_or_name}` path segment. A pid is a bare decimal; a name is **percent-encoded**
    /// so arbitrary characters (spaces, punctuation) survive as a single path segment. `/`
    /// can't appear in a valid name (see [`validate_managed_name`]), so encoded slashes never
    /// arise. The daemon's `Path<String>` extractor percent-decodes before matching the name.
    pub fn to_path_segment(&self) -> String {
        match self {
            ProcessRef::Pid(pid) => pid.to_string(),
            ProcessRef::Name(name) => urlencoding::encode(name).into_owned(),
        }
    }
}

impl From<u64> for ProcessRef {
    fn from(pid: u64) -> Self {
        ProcessRef::Pid(pid)
    }
}
impl From<u32> for ProcessRef {
    fn from(pid: u32) -> Self {
        ProcessRef::Pid(pid as u64)
    }
}
impl From<i64> for ProcessRef {
    fn from(pid: i64) -> Self {
        ProcessRef::Pid(pid as u64)
    }
}
impl From<&str> for ProcessRef {
    fn from(s: &str) -> Self {
        // An all-ASCII-digit string is a pid; anything else is a managed name. This mirrors
        // the daemon's route disambiguation, so a stringified pid still hits the pid branch.
        if !s.is_empty()
            && s.bytes().all(|b| b.is_ascii_digit())
            && let Ok(pid) = s.parse::<u64>()
        {
            return ProcessRef::Pid(pid);
        }
        ProcessRef::Name(s.to_string())
    }
}
impl From<String> for ProcessRef {
    fn from(s: String) -> Self {
        ProcessRef::from(s.as_str())
    }
}
impl From<&String> for ProcessRef {
    fn from(s: &String) -> Self {
        ProcessRef::from(s.as_str())
    }
}

/// Validate a user-supplied managed-process name. **Single source of truth** for the rule
/// across the Rust SDK, the Python/Node bindings, and the CLI (the daemon keeps a
/// byte-identical copy in its own repo). Permissive on purpose -- a name may contain any
/// characters (spaces, punctuation, unicode; clients percent-encode the path segment) with
/// only three rejections:
///   1. empty (no path segment),
///   2. contains `/` (an encoded slash is unreliable across the proxy/router chain), and
///   3. all ASCII digits (reserved for PID addressing on the shared `/processes/{pid_or_name}`
///      route -- this is the one residual backward-incompatibility; see release notes).
pub fn validate_managed_name(name: &str) -> Result<(), SdkError> {
    if name.is_empty() {
        return Err(SdkError::ClientError(
            "managed process name must not be empty".to_string(),
        ));
    }
    if name.contains('/') {
        return Err(SdkError::ClientError(
            "managed process name must not contain '/'".to_string(),
        ));
    }
    if name.bytes().all(|b| b.is_ascii_digit()) {
        return Err(SdkError::ClientError(
            "managed process name must not be all digits; numeric strings are reserved for PID addressing"
                .to_string(),
        ));
    }
    Ok(())
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SandboxProxyTarget {
    pub base_url: String,
    pub host_override: Option<String>,
    pub sandbox_id_header: Option<String>,
}

pub fn sandbox_url_from_ingress_endpoint(
    ingress_endpoint: &str,
    sandbox_id: &str,
    port: Option<u16>,
) -> Result<String, SdkError> {
    let parsed = reqwest::Url::parse(ingress_endpoint).map_err(|error| {
        SdkError::ClientError(format!(
            "invalid ingress endpoint `{ingress_endpoint}`: {error}"
        ))
    })?;
    if parsed.scheme() != "http" && parsed.scheme() != "https" {
        return Err(SdkError::ClientError(
            "ingress_endpoint must be an absolute http(s) URL".to_string(),
        ));
    }
    let host = parsed.host_str().ok_or_else(|| {
        SdkError::ClientError(format!(
            "ingress endpoint `{ingress_endpoint}` is missing a host"
        ))
    })?;
    let label = match port {
        Some(port) if port != SANDBOX_MANAGEMENT_PORT => format!("{port}-{sandbox_id}"),
        _ => sandbox_id.to_string(),
    };
    let host = if host.contains(':') && !host.starts_with('[') {
        format!("[{host}]")
    } else {
        host.to_string()
    };
    let port_part = parsed.port().map(|p| format!(":{p}")).unwrap_or_default();
    Ok(format!("{}://{label}.{host}{port_part}", parsed.scheme()))
}

pub fn resolve_default_sandbox_proxy_url(api_url: &str) -> String {
    if let Ok(url) = std::env::var("TENSORLAKE_SANDBOX_PROXY_URL") {
        let trimmed = url.trim();
        if !trimmed.is_empty() {
            return trimmed.to_string();
        }
    }
    if is_localhost_api_url(api_url) {
        return "http://localhost:9443".to_string();
    }
    if let Ok(parsed) = reqwest::Url::parse(api_url)
        && let Some(host) = parsed.host_str()
        && let Some(rest) = host.strip_prefix("api.")
    {
        return format!("{}://sandbox.{rest}", parsed.scheme());
    }
    DEFAULT_SANDBOX_PROXY_URL.to_string()
}

pub fn select_sandbox_proxy_url(
    _api_url: &str,
    _sandbox_id: &str,
    server_sandbox_url: Option<&str>,
    _server_ingress_endpoint: Option<&str>,
    explicit_proxy_url: Option<&str>,
) -> Result<String, SdkError> {
    if let Some(url) = non_empty(server_sandbox_url) {
        return Ok(url.to_string());
    }
    if let Some(url) = non_empty(explicit_proxy_url) {
        return Ok(url.to_string());
    }
    Err(SdkError::ClientError(
        "server response did not include sandbox_url; refusing to derive a proxy URL".to_string(),
    ))
}

pub fn resolve_sandbox_proxy_target(
    proxy_url: &str,
    sandbox_id: &str,
) -> Result<SandboxProxyTarget, SdkError> {
    let parsed = reqwest::Url::parse(proxy_url).map_err(|error| {
        SdkError::ClientError(format!("invalid proxy url `{proxy_url}`: {error}"))
    })?;
    let host = parsed.host_str().ok_or_else(|| {
        SdkError::ClientError(format!("proxy url `{proxy_url}` is missing a host"))
    })?;

    if host == "localhost" || host == "127.0.0.1" {
        return Ok(SandboxProxyTarget {
            base_url: proxy_url.trim_end_matches('/').to_string(),
            host_override: Some(format!("{sandbox_id}.local")),
            sandbox_id_header: None,
        });
    }

    let host = if host.contains(':') && !host.starts_with('[') {
        format!("[{host}]")
    } else {
        host.to_string()
    };
    let port = parsed.port().map(|p| format!(":{p}")).unwrap_or_default();
    Ok(SandboxProxyTarget {
        base_url: format!("{}://{host}{port}", parsed.scheme()),
        host_override: None,
        sandbox_id_header: Some(sandbox_id.to_string()),
    })
}

pub fn sandbox_proxy_hostname(proxy_url: &str) -> Result<String, SdkError> {
    let parsed = reqwest::Url::parse(proxy_url).map_err(|error| {
        SdkError::ClientError(format!("invalid sandbox url `{proxy_url}`: {error}"))
    })?;
    if parsed.scheme() != "http" && parsed.scheme() != "https" {
        return Err(SdkError::ClientError(
            "sandbox_url must be an absolute http(s) URL".to_string(),
        ));
    }
    parsed.host_str().map(str::to_string).ok_or_else(|| {
        SdkError::ClientError(format!("sandbox url `{proxy_url}` is missing a host"))
    })
}

fn non_empty(value: Option<&str>) -> Option<&str> {
    value.map(str::trim).filter(|value| !value.is_empty())
}

fn is_localhost_api_url(api_url: &str) -> bool {
    reqwest::Url::parse(api_url)
        .ok()
        .and_then(|url| url.host_str().map(ToString::to_string))
        .is_some_and(|host| host == "localhost" || host == "127.0.0.1")
}

/// A client for managing sandbox lifecycle, pool, and snapshot APIs.
#[derive(Clone)]
pub struct SandboxesClient {
    client: Client,
    log_client: Client,
    namespace: String,
    use_namespaced_endpoints: bool,
}

impl SandboxesClient {
    /// Create a new sandboxes client.
    ///
    /// If `use_namespaced_endpoints` is true, requests are sent to
    /// `/v1/namespaces/{namespace}/...`; otherwise to `/{endpoint}`.
    pub fn new(
        client: Client,
        namespace: impl Into<String>,
        use_namespaced_endpoints: bool,
    ) -> Self {
        Self {
            log_client: client.clone(),
            client,
            namespace: namespace.into(),
            use_namespaced_endpoints,
        }
    }

    /// Use a separate client/base URL for log-reader endpoints.
    pub fn with_log_client(mut self, log_client: Client) -> Self {
        self.log_client = log_client;
        self
    }

    pub fn http_client(&self) -> &Client {
        &self.client
    }

    fn endpoint(&self, endpoint: &str) -> String {
        if self.use_namespaced_endpoints {
            format!("/v1/namespaces/{}/{}", self.namespace, endpoint)
        } else {
            format!("/{endpoint}")
        }
    }

    fn log_endpoint(&self, endpoint: &str) -> String {
        format!("/v1/namespaces/{}/{}", self.namespace, endpoint)
    }

    /// Blocking create: the server waits for readiness for at most the
    /// client's request timeout and answers `status: timeout` (HTTP 504)
    /// when it runs out, leaving the sandbox pending. Prefer
    /// [`Self::create_no_wait`] followed by [`Self::wait_until_settled`]; this
    /// shape is kept for callers that depend on the server-side wait.
    pub async fn create(
        &self,
        request: &CreateSandboxRequest,
    ) -> Result<Traced<CreateSandboxResponse>, SdkError> {
        validate_mount_snapshot_pins(&request.file_systems)?;
        validate_mount_owners(&request.file_systems)?;
        let uri = self.endpoint("sandboxes");
        let req = self
            .client
            .build_post_json_request(Method::POST, &uri, request)?;
        self.client
            .execute_json_allow_status(req, &[StatusCode::GATEWAY_TIMEOUT])
            .await
    }

    /// Wait-free create (ADR 0086): sends `request` with `wait: false` and
    /// returns the acknowledgement (HTTP 202) as soon as the sandbox is
    /// durable. The sandbox starts whenever capacity allows; wait for it with
    /// [`Self::wait_until_settled`] or poll [`Self::get`] / [`Self::list`].
    ///
    /// The call is not idempotent: a retry that the server did receive
    /// creates a second sandbox. Callers that must not duplicate give the
    /// sandbox a `name` (unique per namespace; a repeat is a 409) and resolve
    /// it with get-or-create, and wrap this only in
    /// [`crate::retry::RetryPolicy::non_idempotent`].
    pub async fn create_no_wait(
        &self,
        request: &CreateSandboxRequest,
    ) -> Result<Traced<SandboxAccepted>, SdkError> {
        validate_mount_snapshot_pins(&request.file_systems)?;
        validate_mount_owners(&request.file_systems)?;
        let request = CreateSandboxRequest {
            wait: Some(false),
            ..request.clone()
        };
        let uri = self.endpoint("sandboxes");
        let req = self
            .client
            .build_post_json_request(Method::POST, &uri, &request)?;
        // A server that predates `wait: false` blocks and may answer its
        // legacy `status: timeout` 504; the sandbox is pending either way.
        self.client
            .execute_json_allow_status(req, &[StatusCode::GATEWAY_TIMEOUT])
            .await
    }

    /// Poll [`Self::get`] every `poll_interval` until the sandbox leaves
    /// `pending` or `timeout` elapses, and return the last observation.
    ///
    /// Never deletes the sandbox and never treats a timeout as an error: a
    /// `pending` status in the result means the caller's budget ran out
    /// while the sandbox kept its place in the queue, and it is the caller's
    /// decision to keep waiting, cancel, or raise. Any other status
    /// (`running`, `suspended`, `terminated`, ...) ends the wait. A zero
    /// `timeout` observes the state once without sleeping. Transient
    /// failures (502/503/504, a cut connection, a timed-out poll) are
    /// retried up to [`WAIT_TRANSIENT_RETRIES`] times between observations;
    /// a missing sandbox surfaces as the poll's 404.
    ///
    /// The budget is honoured closely: it is checked before every poll and
    /// every sleep, no sleep runs past the deadline, each poll's HTTP timeout
    /// is capped at the remaining budget (only the very first attempt gets
    /// [`WAIT_POLL_MIN_REQUEST_TIMEOUT`], so the state can be observed once),
    /// and once the budget is spent the last observation is returned without
    /// another request. When nothing was observed before the budget ran out
    /// (the first attempts all failed), the last error is returned instead.
    pub async fn wait_until_settled(
        &self,
        sandbox_id: &str,
        timeout: Duration,
        poll_interval: Duration,
    ) -> Result<Traced<SandboxInfo>, SdkError> {
        let started = Instant::now();
        let deadline = started + timeout;
        let mut last: Option<Traced<SandboxInfo>> = None;
        let mut last_error: Option<SdkError> = None;
        let mut last_progress = started;
        let mut retry = RetryState::new(RetryPolicy::idempotent(WAIT_TRANSIENT_RETRIES));
        loop {
            let remaining = deadline.saturating_duration_since(Instant::now());
            if remaining.is_zero() {
                if let Some(last) = last {
                    return Ok(last);
                }
                if let Some(error) = last_error {
                    return Err(error);
                }
            }
            // Only the very first attempt is allowed the floor; after a
            // failed attempt every retry is capped at what is left.
            let per_poll = if last.is_none() && last_error.is_none() {
                remaining.max(WAIT_POLL_MIN_REQUEST_TIMEOUT)
            } else {
                remaining
            };
            match self.get_within(sandbox_id, per_poll).await {
                Ok(info) => {
                    if !is_sandbox_pending(&info.status) {
                        return Ok(info);
                    }
                    last = Some(info);
                    last_progress = Instant::now();
                    retry = RetryState::new(RetryPolicy::idempotent(WAIT_TRANSIENT_RETRIES));
                    let remaining = deadline.saturating_duration_since(Instant::now());
                    if remaining.is_zero() {
                        return Ok(last.expect("observed"));
                    }
                    tokio::time::sleep(poll_interval.min(remaining)).await;
                }
                Err(error) => {
                    let cut_short = is_transient(&error)
                        || error.transport_failure() == Some(TransportFailure::Timeout);
                    match retry.decide_with_transient(&error, last_progress.elapsed(), cut_short) {
                        RetryDecision::Retry(wait) => {
                            let remaining = deadline.saturating_duration_since(Instant::now());
                            if remaining.is_zero() {
                                return match last {
                                    Some(last) => Ok(last),
                                    None => Err(error),
                                };
                            }
                            tokio::time::sleep(wait.min(remaining)).await;
                            last_error = Some(error);
                        }
                        RetryDecision::Stop => return Err(error),
                    }
                }
            }
        }
    }

    pub async fn claim(&self, pool_id: &str) -> Result<Traced<CreateSandboxResponse>, SdkError> {
        let uri = self.endpoint(&format!("sandbox-pools/{pool_id}/sandboxes"));
        let req = self.client.build_empty_post_request(&uri)?;
        self.client
            .execute_json_allow_status(req, &[StatusCode::GATEWAY_TIMEOUT])
            .await
    }

    /// Claim a sandbox from a pool and apply sandbox-specific file-system
    /// mounts before the claimed sandbox becomes ready. Fails closed when the
    /// server does not acknowledge claim-configuration support and requests
    /// termination of the incorrectly configured sandbox returned by that
    /// server.
    pub async fn claim_with_request(
        &self,
        pool_id: &str,
        request: &ClaimSandboxRequest,
    ) -> Result<Traced<CreateSandboxResponse>, SdkError> {
        validate_mount_snapshot_pins(&request.file_systems)?;
        validate_mount_owners(&request.file_systems)?;
        let uri = self.endpoint(&format!("sandbox-pools/{pool_id}/sandboxes"));
        let req = self
            .client
            .build_post_json_request(Method::POST, &uri, request)?;
        let response: Traced<ClaimSandboxResponse> = self
            .client
            .execute_json_allow_status(req, &[StatusCode::GATEWAY_TIMEOUT])
            .await?;
        let claim_configuration_applied = response.claim_configuration_applied;
        let response = response.map(|response| response.sandbox);
        if claim_configuration_applied != Some(true) {
            let cleanup = match self.delete(&response.sandbox_id).await {
                Ok(_) => format!(
                    "termination was requested for sandbox {:?}",
                    response.sandbox_id
                ),
                Err(error) => format!(
                    "cleanup of sandbox {:?} also failed: {error}",
                    response.sandbox_id
                ),
            };
            return Err(SdkError::ClientError(format!(
                "server did not acknowledge sandbox pool claim configuration for pool \
                 {pool_id:?}; upgrade the compute-engine server before requesting claim-time \
                 file systems; {cleanup}"
            )));
        }
        Ok(response)
    }

    pub async fn copy(
        &self,
        sandbox_id: &str,
        times: usize,
    ) -> Result<Traced<CopySandboxResponse>, SdkError> {
        let uri = self.endpoint(&format!("sandboxes/{sandbox_id}/copy"));
        let req = build_empty_post_request_from_builder(
            self.client
                .request(Method::POST, &uri)
                .query(&[("times", times)]),
        )?;
        self.client
            .execute_json_allow_status(
                req,
                &[
                    StatusCode::UNPROCESSABLE_ENTITY,
                    StatusCode::GATEWAY_TIMEOUT,
                ],
            )
            .await
    }

    pub async fn get(&self, sandbox_id: &str) -> Result<Traced<SandboxInfo>, SdkError> {
        let uri = self.endpoint(&format!("sandboxes/{sandbox_id}"));
        let req = self.client.request(Method::GET, &uri).build()?;
        self.client.execute_json(req).await
    }

    /// [`Self::get`] with the HTTP timeout set to `timeout` (never above the
    /// client's own timeout), so a readiness poll cannot outlive the wait it
    /// serves.
    async fn get_within(
        &self,
        sandbox_id: &str,
        timeout: Duration,
    ) -> Result<Traced<SandboxInfo>, SdkError> {
        let timeout = match self.client.request_timeout() {
            Some(client_timeout) => timeout.min(client_timeout),
            None => timeout,
        };
        let uri = self.endpoint(&format!("sandboxes/{sandbox_id}"));
        let req = self
            .client
            .request(Method::GET, &uri)
            .timeout(timeout)
            .build()?;
        self.client.execute_json(req).await
    }

    pub async fn list(&self) -> Result<Traced<Vec<SandboxInfo>>, SdkError> {
        let uri = self.endpoint("sandboxes");
        let req = self.client.request(Method::GET, &uri).build()?;
        Ok(self
            .client
            .execute_json::<ListSandboxesResponse>(req)
            .await?
            .map(|r| r.sandboxes))
    }

    /// List archived (terminated) sandboxes in the namespace. Archived sandboxes
    /// live in their own column family until the server-configured TTL expires.
    pub async fn list_archived(
        &self,
        params: &ListArchivedSandboxesParams,
    ) -> Result<Traced<ListArchivedSandboxesResponse>, SdkError> {
        let uri = self.endpoint("archived-sandboxes");
        let mut request_builder = self.client.request(Method::GET, &uri);
        let mut query: Vec<(&str, String)> = Vec::new();
        if let Some(limit) = params.limit {
            query.push(("limit", limit.to_string()));
        }
        if let Some(cursor) = params.cursor.as_deref() {
            query.push(("cursor", cursor.to_string()));
        }
        if let Some(direction) = params.direction {
            let value = match direction {
                ArchivedSandboxesPaginationDirection::Forward => "forward",
                ArchivedSandboxesPaginationDirection::Backward => "backward",
            };
            query.push(("direction", value.to_string()));
        }
        if !query.is_empty() {
            request_builder = request_builder.query(&query);
        }
        let req = request_builder.build()?;
        self.client.execute_json(req).await
    }

    pub async fn get_archived(
        &self,
        sandbox_id: &str,
    ) -> Result<Traced<ArchivedSandboxInfo>, SdkError> {
        let uri = self.endpoint(&format!("archived-sandboxes/{sandbox_id}"));
        let req = self.client.request(Method::GET, &uri).build()?;
        self.client.execute_json(req).await
    }

    pub async fn get_logs(
        &self,
        request: &GetSandboxLogsRequest,
    ) -> Result<Traced<SandboxLogsResponse>, SdkError> {
        let uri = self.log_endpoint(&format!("sandboxes/{}/logs", request.sandbox_id));
        let mut req_builder = self.log_client.request(Method::GET, &uri);

        for level in &request.levels {
            req_builder = req_builder.query(&[("level", level.as_i8())]);
        }
        for process_id in &request.process_ids {
            req_builder = req_builder.query(&[("processId", process_id)]);
        }
        if let Some(ref param_value) = request.next_token {
            req_builder = req_builder.query(&[("nextToken", param_value)]);
        }
        if let Some(param_value) = request.head {
            req_builder = req_builder.query(&[("head", param_value)]);
        }
        if let Some(param_value) = request.tail {
            req_builder = req_builder.query(&[("tail", param_value)]);
        }
        if let Some(ref param_value) = request.body {
            req_builder = req_builder.query(&[("body", param_value)]);
        }

        let req = req_builder.build()?;
        self.log_client.execute_json(req).await
    }

    pub async fn list_log_processes(
        &self,
        sandbox_id: &str,
    ) -> Result<Traced<SandboxProcessLogFiltersResponse>, SdkError> {
        let uri = self.log_endpoint(&format!("sandboxes/{sandbox_id}/processes"));
        let req = self.log_client.request(Method::GET, &uri).build()?;
        self.log_client.execute_json(req).await
    }

    pub async fn update(
        &self,
        sandbox_id: &str,
        request: &UpdateSandboxRequest,
    ) -> Result<Traced<SandboxInfo>, SdkError> {
        let uri = self.endpoint(&format!("sandboxes/{sandbox_id}"));
        let req = self
            .client
            .build_post_json_request(Method::PATCH, &uri, request)?;
        self.client.execute_json(req).await
    }

    pub async fn delete(&self, sandbox_id: &str) -> Result<Traced<()>, SdkError> {
        let uri = self.endpoint(&format!("sandboxes/{sandbox_id}"));
        let req = self.client.request(Method::DELETE, &uri).build()?;
        Ok(self.client.execute_traced(req).await?.map(|_| ()))
    }

    pub async fn suspend(&self, sandbox_id: &str) -> Result<Traced<()>, SdkError> {
        let uri = self.endpoint(&format!("sandboxes/{sandbox_id}/suspend"));
        let req = self.client.build_empty_post_request(&uri)?;
        Ok(self.client.execute_traced(req).await?.map(|_| ()))
    }

    pub async fn resume(&self, sandbox_id: &str) -> Result<Traced<()>, SdkError> {
        let uri = self.endpoint(&format!("sandboxes/{sandbox_id}/resume"));
        let req = self.client.build_empty_post_request(&uri)?;
        Ok(self.client.execute_traced(req).await?.map(|_| ()))
    }

    /// Attach a registered file system to a running sandbox at `mount_path`.
    ///
    /// The mount completes asynchronously on the dataplane; the returned
    /// [`SandboxInfo`] already reflects the new entry in `file_systems`.
    ///
    /// `read_only` and `prefetch` select optional mount modes; they are sent
    /// on the wire only when `true` so older servers (which reject unknown
    /// fields) keep accepting attach bodies.
    ///
    /// `snapshot_id` pins the mount to a specific filesystem snapshot and
    /// requires `read_only`; it is sent only when set for the same
    /// compatibility reason.
    ///
    /// `owner` presents the mounted files as owned by a guest user spec
    /// (`NAME`, `UID`, `NAME:GROUP`, or `UID:GID`), resolved against the
    /// sandbox image's own user database at attach time; unset keeps the
    /// image default. It is sent only when set for the same compatibility
    /// reason.
    pub async fn attach_file_system(
        &self,
        sandbox_id: &str,
        file_system_id: &str,
        mount_path: &str,
        read_only: bool,
        prefetch: bool,
        snapshot_id: Option<&str>,
        owner: Option<&str>,
    ) -> Result<Traced<SandboxInfo>, SdkError> {
        let uri = self.endpoint(&format!("sandboxes/{sandbox_id}/file_systems"));
        let body = FileSystemMount {
            file_system_id: file_system_id.to_string(),
            mount_path: mount_path.to_string(),
            read_only,
            prefetch,
            snapshot_id: snapshot_id.map(str::to_string),
            owner: owner.map(str::to_string),
        };
        validate_mount_snapshot_pins(std::slice::from_ref(&body))?;
        validate_mount_owners(std::slice::from_ref(&body))?;
        let req = self
            .client
            .build_post_json_request(Method::POST, &uri, &body)?;
        self.client.execute_json(req).await
    }

    /// Detach the file system mounted at `mount_path` from a running sandbox.
    ///
    /// The unmount completes asynchronously on the dataplane; the returned
    /// [`SandboxInfo`] already reflects the removed `file_systems` entry.
    pub async fn detach_file_system(
        &self,
        sandbox_id: &str,
        mount_path: &str,
    ) -> Result<Traced<SandboxInfo>, SdkError> {
        let uri = self.endpoint(&format!("sandboxes/{sandbox_id}/file_systems"));
        let body = DetachFileSystemRequest {
            mount_path: mount_path.to_string(),
        };
        let req = self
            .client
            .build_post_json_request(Method::DELETE, &uri, &body)?;
        self.client.execute_json(req).await
    }

    pub async fn snapshot(
        &self,
        sandbox_id: &str,
        snapshot_type: Option<SnapshotType>,
    ) -> Result<Traced<CreateSnapshotResponse>, SdkError> {
        let uri = self.endpoint(&format!("sandboxes/{sandbox_id}/snapshot"));
        let req = if snapshot_type.is_some() {
            let body = CreateSnapshotRequest { snapshot_type };
            self.client
                .build_post_json_request(Method::POST, &uri, &body)?
        } else {
            // Preserve today's wire shape (no body) for callers that don't set a snapshot type.
            self.client.build_empty_post_request(&uri)?
        };
        self.client.execute_json(req).await
    }

    pub async fn get_snapshot(&self, snapshot_id: &str) -> Result<Traced<SnapshotInfo>, SdkError> {
        let uri = self.endpoint(&format!("snapshots/{snapshot_id}"));
        let req = self.client.request(Method::GET, &uri).build()?;
        self.client.execute_json(req).await
    }

    pub async fn list_snapshots(&self) -> Result<Traced<Vec<SnapshotInfo>>, SdkError> {
        let uri = self.endpoint("snapshots");
        let req = self.client.request(Method::GET, &uri).build()?;
        Ok(self
            .client
            .execute_json::<ListSnapshotsResponse>(req)
            .await?
            .map(|r| r.snapshots))
    }

    pub async fn delete_snapshot(&self, snapshot_id: &str) -> Result<Traced<()>, SdkError> {
        let uri = self.endpoint(&format!("snapshots/{snapshot_id}"));
        let req = self.client.request(Method::DELETE, &uri).build()?;
        Ok(self.client.execute_traced(req).await?.map(|_| ()))
    }

    pub async fn create_pool(
        &self,
        request: &SandboxPoolRequest,
    ) -> Result<Traced<CreateSandboxPoolResponse>, SdkError> {
        self.create_pool_with_network(&CreateSandboxPoolRequest {
            pool: request.clone(),
            network: None,
        })
        .await
    }

    /// Creates a pool with an optional network policy for each container.
    pub async fn create_pool_with_network(
        &self,
        request: &CreateSandboxPoolRequest,
    ) -> Result<Traced<CreateSandboxPoolResponse>, SdkError> {
        let uri = self.endpoint("sandbox-pools");
        let req = self
            .client
            .build_post_json_request(Method::POST, &uri, request)?;
        self.client.execute_json(req).await
    }

    pub async fn get_pool(&self, pool_id: &str) -> Result<Traced<SandboxPoolInfo>, SdkError> {
        let uri = self.endpoint(&format!("sandbox-pools/{pool_id}"));
        let req = self.client.request(Method::GET, &uri).build()?;
        self.client.execute_json(req).await
    }

    pub async fn list_pools(&self) -> Result<Traced<Vec<SandboxPoolInfo>>, SdkError> {
        let uri = self.endpoint("sandbox-pools");
        let req = self.client.request(Method::GET, &uri).build()?;
        Ok(self
            .client
            .execute_json::<ListSandboxPoolsResponse>(req)
            .await?
            .map(|r| r.pools))
    }

    /// Replace a pool configuration, keeping its current network policy.
    /// Use [`Self::update_pool_with_network`] to replace the policy too.
    pub async fn update_pool(
        &self,
        pool_id: &str,
        request: &SandboxPoolRequest,
    ) -> Result<Traced<SandboxPoolInfo>, SdkError> {
        self.update_pool_with_network(
            pool_id,
            &UpdateSandboxPoolRequest {
                pool: request.clone(),
                network: NetworkPolicyUpdate::Keep,
            },
        )
        .await
    }

    /// Replace a pool configuration, optionally changing its network policy.
    ///
    /// [`NetworkPolicyUpdate::Keep`] leaves the current policy in place;
    /// [`NetworkPolicyUpdate::Clear`] removes it; [`NetworkPolicyUpdate::Set`]
    /// replaces it. On a change the service recycles the pool's unclaimed warm
    /// containers onto the new policy, while containers already claimed by
    /// sandboxes keep the policy they booted with.
    pub async fn update_pool_with_network(
        &self,
        pool_id: &str,
        request: &UpdateSandboxPoolRequest,
    ) -> Result<Traced<SandboxPoolInfo>, SdkError> {
        let mut request = request.clone();
        // Newer services keep the current policy when the field is absent, but
        // older ones clear it, so resolve `Keep` into the current policy here.
        // Sending the unchanged policy is a no-op for the service: the pool's
        // boot config does not change, so no warm containers are recycled.
        if request.network.is_keep()
            && let Some(current) = self.get_pool(pool_id).await?.network_policy.clone()
        {
            request.network = NetworkPolicyUpdate::Set(current);
        }

        let uri = self.endpoint(&format!("sandbox-pools/{pool_id}"));
        let req = self
            .client
            .build_post_json_request(Method::PUT, &uri, &request)?;
        self.client.execute_json(req).await
    }

    pub async fn delete_pool(&self, pool_id: &str) -> Result<Traced<()>, SdkError> {
        let uri = self.endpoint(&format!("sandbox-pools/{pool_id}"));
        let req = self.client.request(Method::DELETE, &uri).build()?;
        Ok(self.client.execute_traced(req).await?.map(|_| ()))
    }
}

/// A client for interacting with a running sandbox proxy (process/file/PTY APIs).
#[derive(Clone)]
pub struct SandboxProxyClient {
    client: Client,
    host_override: Option<String>,
    routing_hint: Option<String>,
    sandbox_id: Option<String>,
}

impl SandboxProxyClient {
    pub fn new(client: Client, host_override: Option<String>) -> Self {
        Self {
            client,
            host_override,
            routing_hint: None,
            sandbox_id: None,
        }
    }

    pub fn with_routing_hint(mut self, hint: Option<String>) -> Self {
        self.routing_hint = hint;
        self
    }

    pub fn with_sandbox_id(mut self, id: Option<String>) -> Self {
        self.sandbox_id = id;
        self
    }

    pub fn http_client(&self) -> &Client {
        &self.client
    }

    pub fn host_override(&self) -> Option<&str> {
        self.host_override.as_deref()
    }

    pub fn sandbox_id(&self) -> Option<&str> {
        self.sandbox_id.as_deref()
    }

    fn request(&self, method: Method, path: &str) -> reqwest_middleware::RequestBuilder {
        let mut request_builder = self.client.request(method, path);
        if let Some(host) = self.host_override.as_deref() {
            request_builder = request_builder.header("Host", host);
        }
        if let Some(id) = self.sandbox_id.as_deref() {
            request_builder = request_builder.header("X-Tensorlake-Sandbox-Id", id);
        }
        if let Some(hint) = self.routing_hint.as_deref() {
            request_builder = request_builder.header("X-Tensorlake-Route-Hint", hint);
        }
        request_builder
    }

    pub async fn start_process(&self, payload: &Value) -> Result<Traced<ProcessInfo>, SdkError> {
        // Validate a managed name up front (clear client-side error before the round-trip).
        // The daemon re-validates as the final authority for any path that bypasses this.
        if let Some(name) = payload.get("name").and_then(Value::as_str) {
            validate_managed_name(name)?;
        }
        let req = self
            .request(Method::POST, "/api/v1/processes")
            .json(payload)
            .build()?;
        self.client.execute_json(req).await
    }

    pub async fn list_processes(&self) -> Result<Traced<Vec<ProcessInfo>>, SdkError> {
        let req = self.request(Method::GET, "/api/v1/processes").build()?;
        Ok(self
            .client
            .execute_json::<ListProcessesResponse>(req)
            .await?
            .map(|r| r.processes))
    }

    pub async fn get_process(
        &self,
        process: impl Into<ProcessRef>,
    ) -> Result<Traced<ProcessInfo>, SdkError> {
        let seg = process.into().to_path_segment();
        let req = self
            .request(Method::GET, &format!("/api/v1/processes/{seg}"))
            .build()?;
        self.client.execute_json(req).await
    }

    pub async fn kill_process(
        &self,
        process: impl Into<ProcessRef>,
    ) -> Result<Traced<()>, SdkError> {
        let seg = process.into().to_path_segment();
        let req = self
            .request(Method::DELETE, &format!("/api/v1/processes/{seg}"))
            .build()?;
        Ok(self.client.execute_traced(req).await?.map(|_| ()))
    }

    pub async fn restart_process(
        &self,
        process: impl Into<ProcessRef>,
    ) -> Result<Traced<ProcessInfo>, SdkError> {
        let seg = process.into().to_path_segment();
        let req = build_empty_post_request_from_builder(
            self.request(Method::POST, &format!("/api/v1/processes/{seg}/restart")),
        )?;
        self.client.execute_json(req).await
    }

    pub async fn send_signal(
        &self,
        process: impl Into<ProcessRef>,
        signal: i64,
    ) -> Result<Traced<SendSignalResponse>, SdkError> {
        let seg = process.into().to_path_segment();
        let req = self
            .request(Method::POST, &format!("/api/v1/processes/{seg}/signal"))
            .json(&serde_json::json!({ "signal": signal }))
            .build()?;
        self.client.execute_json(req).await
    }

    pub async fn write_stdin(
        &self,
        process: impl Into<ProcessRef>,
        data: Vec<u8>,
    ) -> Result<Traced<()>, SdkError> {
        let seg = process.into().to_path_segment();
        let req = build_bytes_post_request_from_builder(
            self.request(Method::POST, &format!("/api/v1/processes/{seg}/stdin")),
            data,
        )?;
        Ok(self.client.execute_traced(req).await?.map(|_| ()))
    }

    pub async fn close_stdin(
        &self,
        process: impl Into<ProcessRef>,
    ) -> Result<Traced<()>, SdkError> {
        let seg = process.into().to_path_segment();
        let req = build_empty_post_request_from_builder(self.request(
            Method::POST,
            &format!("/api/v1/processes/{seg}/stdin/close"),
        ))?;
        Ok(self.client.execute_traced(req).await?.map(|_| ()))
    }

    pub async fn get_stdout(
        &self,
        process: impl Into<ProcessRef>,
    ) -> Result<Traced<OutputResponse>, SdkError> {
        let seg = process.into().to_path_segment();
        let req = self
            .request(Method::GET, &format!("/api/v1/processes/{seg}/stdout"))
            .build()?;
        self.client.execute_json(req).await
    }

    pub async fn get_stderr(
        &self,
        process: impl Into<ProcessRef>,
    ) -> Result<Traced<OutputResponse>, SdkError> {
        let seg = process.into().to_path_segment();
        let req = self
            .request(Method::GET, &format!("/api/v1/processes/{seg}/stderr"))
            .build()?;
        self.client.execute_json(req).await
    }

    pub async fn get_output(
        &self,
        process: impl Into<ProcessRef>,
    ) -> Result<Traced<OutputResponse>, SdkError> {
        let seg = process.into().to_path_segment();
        let req = self
            .request(Method::GET, &format!("/api/v1/processes/{seg}/output"))
            .build()?;
        self.client.execute_json(req).await
    }

    pub async fn follow_stdout(
        &self,
        process: impl Into<ProcessRef>,
    ) -> Result<Traced<Vec<OutputEvent>>, SdkError> {
        let mut events = Vec::new();
        let trace_id = self
            .follow_stdout_streaming(process, |event| events.push(event))
            .await?;
        Ok(Traced::new(trace_id, events))
    }

    pub async fn follow_stderr(
        &self,
        process: impl Into<ProcessRef>,
    ) -> Result<Traced<Vec<OutputEvent>>, SdkError> {
        let mut events = Vec::new();
        let trace_id = self
            .follow_stderr_streaming(process, |event| events.push(event))
            .await?;
        Ok(Traced::new(trace_id, events))
    }

    pub async fn follow_output(
        &self,
        process: impl Into<ProcessRef>,
    ) -> Result<Traced<Vec<OutputEvent>>, SdkError> {
        let mut events = Vec::new();
        let trace_id = self
            .follow_output_streaming(process, |event| events.push(event))
            .await?;
        Ok(Traced::new(trace_id, events))
    }

    /// Stream stdout output events to `on_event` as they arrive, without
    /// buffering. Returns the request `trace_id` once the upstream stream
    /// closes. This is the streaming counterpart to [`follow_stdout`] used by
    /// language bindings that surface a live event stream to the caller.
    pub async fn follow_stdout_streaming(
        &self,
        process: impl Into<ProcessRef>,
        mut on_event: impl FnMut(OutputEvent),
    ) -> Result<String, SdkError> {
        self.follow_stdout_streaming_async(process, |event| {
            on_event(event);
            std::future::ready(Ok(()))
        })
        .await
    }

    /// Await consumer capacity before reading the next event.
    pub async fn follow_stdout_streaming_async<F>(
        &self,
        process: impl Into<ProcessRef>,
        on_event: impl FnMut(OutputEvent) -> F,
    ) -> Result<String, SdkError>
    where
        F: std::future::Future<Output = Result<(), SdkError>>,
    {
        let seg = process.into().to_path_segment();
        self.follow_stream_cb(&format!("/api/v1/processes/{seg}/stdout/follow"), on_event)
            .await
    }

    pub async fn follow_stderr_streaming(
        &self,
        process: impl Into<ProcessRef>,
        mut on_event: impl FnMut(OutputEvent),
    ) -> Result<String, SdkError> {
        self.follow_stderr_streaming_async(process, |event| {
            on_event(event);
            std::future::ready(Ok(()))
        })
        .await
    }

    /// Await consumer capacity before reading the next event.
    pub async fn follow_stderr_streaming_async<F>(
        &self,
        process: impl Into<ProcessRef>,
        on_event: impl FnMut(OutputEvent) -> F,
    ) -> Result<String, SdkError>
    where
        F: std::future::Future<Output = Result<(), SdkError>>,
    {
        let seg = process.into().to_path_segment();
        self.follow_stream_cb(&format!("/api/v1/processes/{seg}/stderr/follow"), on_event)
            .await
    }

    pub async fn follow_output_streaming(
        &self,
        process: impl Into<ProcessRef>,
        mut on_event: impl FnMut(OutputEvent),
    ) -> Result<String, SdkError> {
        self.follow_output_streaming_async(process, |event| {
            on_event(event);
            std::future::ready(Ok(()))
        })
        .await
    }

    /// Await consumer capacity before reading the next event.
    pub async fn follow_output_streaming_async<F>(
        &self,
        process: impl Into<ProcessRef>,
        on_event: impl FnMut(OutputEvent) -> F,
    ) -> Result<String, SdkError>
    where
        F: std::future::Future<Output = Result<(), SdkError>>,
    {
        let seg = process.into().to_path_segment();
        self.follow_stream_cb(&format!("/api/v1/processes/{seg}/output/follow"), on_event)
            .await
    }

    pub async fn run_process(
        &self,
        payload: &Value,
    ) -> Result<Traced<Vec<RunProcessEvent>>, SdkError> {
        let mut events = Vec::new();
        let trace_id = self
            .run_process_streaming(payload, |event| events.push(event))
            .await?;
        Ok(Traced::new(trace_id, events))
    }

    /// Start a process and stream its lifecycle events to `on_event` as they
    /// arrive. Returns the request `trace_id` once the process exits and the
    /// stream closes. This is the streaming counterpart to [`run_process`].
    pub async fn run_process_streaming(
        &self,
        payload: &Value,
        mut on_event: impl FnMut(RunProcessEvent),
    ) -> Result<String, SdkError> {
        self.run_process_streaming_async(payload, |event| {
            on_event(event);
            std::future::ready(Ok(()))
        })
        .await
    }

    /// Await consumer capacity before reading the next lifecycle event.
    pub async fn run_process_streaming_async<F>(
        &self,
        payload: &Value,
        mut on_event: impl FnMut(RunProcessEvent) -> F,
    ) -> Result<String, SdkError>
    where
        F: std::future::Future<Output = Result<(), SdkError>>,
    {
        let path = "/api/v1/processes/run";
        let req = self
            .request(Method::POST, path)
            .header(ACCEPT, "text/event-stream")
            .json(payload)
            .build()?;
        let response = self.client.execute_traced(req).await?;
        let trace_id = response.trace_id.clone();
        let content_type = response
            .headers()
            .get(reqwest::header::CONTENT_TYPE)
            .and_then(|v| v.to_str().ok())
            .unwrap_or("")
            .to_ascii_lowercase();
        if !content_type.contains("text/event-stream") {
            return Err(SdkError::ClientError(format!(
                "expected text/event-stream response from {path}, got content-type: {content_type}"
            )));
        }
        let stream = response
            .into_inner()
            .bytes_stream()
            .eventsource()
            .filter_map(move |event| async move {
                match event {
                    Ok(msg) => {
                        // The Exited variant has all-optional fields so it acts as a
                        // catch-all for unrecognised JSON. Discard Exited{None, None}
                        // — a real exit always has at least one of exit_code or signal.
                        match serde_json::from_str::<RunProcessEvent>(&msg.data) {
                            Ok(RunProcessEvent::Exited {
                                exit_code: None,
                                signal: None,
                                ..
                            }) => None,
                            Ok(evt) => Some(Ok(evt)),
                            Err(_) => None,
                        }
                    }
                    Err(error) => Some(Err(SdkError::EventSourceError(error.to_string()))),
                }
            });
        futures::pin_mut!(stream);
        while let Some(event) = stream.next().await {
            on_event(event?).await?;
        }
        Ok(trace_id)
    }

    async fn follow_stream_cb<F>(
        &self,
        path: &str,
        mut on_event: impl FnMut(OutputEvent) -> F,
    ) -> Result<String, SdkError>
    where
        F: std::future::Future<Output = Result<(), SdkError>>,
    {
        let req = self
            .request(Method::GET, path)
            .header(ACCEPT, "text/event-stream")
            .build()?;
        let response = self.client.execute_traced(req).await?;
        let trace_id = response.trace_id.clone();
        let content_type = response
            .headers()
            .get(reqwest::header::CONTENT_TYPE)
            .and_then(|v| v.to_str().ok())
            .unwrap_or("")
            .to_ascii_lowercase();
        if !content_type.contains("text/event-stream") {
            return Err(SdkError::ClientError(format!(
                "expected text/event-stream response from {path}, got content-type: {content_type}"
            )));
        }
        let stream = response
            .into_inner()
            .bytes_stream()
            .eventsource()
            .filter_map(move |event| async move {
                match event {
                    Ok(msg) => match serde_json::from_str::<OutputEvent>(&msg.data) {
                        Ok(evt) => Some(Ok(evt)),
                        Err(_) => None, // skip heartbeats / non-output events
                    },
                    Err(error) => Some(Err(SdkError::EventSourceError(error.to_string()))),
                }
            });
        futures::pin_mut!(stream);
        while let Some(event) = stream.next().await {
            on_event(event?).await?;
        }
        Ok(trace_id)
    }

    pub async fn read_file(&self, path: &str) -> Result<Traced<Vec<u8>>, SdkError> {
        let req = self
            .request(Method::GET, "/api/v1/files")
            .query(&[("path", path)])
            .build()?;
        let resp = self.client.execute_traced(req).await?;
        let trace_id = resp.trace_id.clone();
        let bytes = resp.into_inner().bytes().await?;
        Ok(Traced::new(trace_id, bytes.to_vec()))
    }

    pub async fn write_file(&self, path: &str, content: Vec<u8>) -> Result<Traced<()>, SdkError> {
        let req = self
            .request(Method::PUT, "/api/v1/files")
            .query(&[("path", path)])
            .body(content)
            .build()?;
        Ok(self.client.execute_traced(req).await?.map(|_| ()))
    }

    pub async fn upload_file(
        &self,
        path: &str,
        local_path: impl AsRef<Path>,
    ) -> Result<Traced<()>, SdkError> {
        let file = File::open(local_path.as_ref()).await?;
        let size = file.metadata().await?.len();
        let stream = ReaderStream::new(file);
        let req = self
            .request(Method::PUT, "/api/v1/files")
            .query(&[("path", path)])
            .header(CONTENT_LENGTH, size)
            .body(reqwest::Body::wrap_stream(stream))
            .build()?;
        Ok(self.client.execute_traced(req).await?.map(|_| ()))
    }

    pub async fn upload_file_with_progress(
        &self,
        path: &str,
        local_path: impl AsRef<Path>,
        progress_tx: tokio::sync::mpsc::UnboundedSender<u64>,
    ) -> Result<Traced<()>, SdkError> {
        let file = File::open(local_path.as_ref()).await?;
        let size = file.metadata().await?.len();
        let mut uploaded = 0_u64;
        let stream = ReaderStream::new(file).map_ok(move |chunk| {
            uploaded = uploaded.saturating_add(chunk.len() as u64);
            let _ = progress_tx.send(uploaded);
            chunk
        });
        let req = self
            .request(Method::PUT, "/api/v1/files")
            .query(&[("path", path)])
            .header(CONTENT_LENGTH, size)
            .body(reqwest::Body::wrap_stream(stream))
            .build()?;
        Ok(self.client.execute_traced(req).await?.map(|_| ()))
    }

    pub async fn delete_file(&self, path: &str) -> Result<Traced<()>, SdkError> {
        let req = self
            .request(Method::DELETE, "/api/v1/files")
            .query(&[("path", path)])
            .build()?;
        Ok(self.client.execute_traced(req).await?.map(|_| ()))
    }

    pub async fn list_directory(
        &self,
        path: &str,
    ) -> Result<Traced<ListDirectoryResponse>, SdkError> {
        let req = self
            .request(Method::GET, "/api/v1/files/list")
            .query(&[("path", path)])
            .build()?;
        self.client.execute_json(req).await
    }

    pub async fn create_pty_session(&self, payload: &Value) -> Result<Traced<Value>, SdkError> {
        let req = self
            .request(Method::POST, "/api/v1/pty")
            .json(payload)
            .build()?;
        self.client.execute_json(req).await
    }

    pub async fn delete_pty_session(&self, session_id: &str) -> Result<Traced<()>, SdkError> {
        let req = self
            .request(Method::DELETE, &format!("/api/v1/pty/{session_id}"))
            .build()?;
        Ok(self.client.execute_traced(req).await?.map(|_| ()))
    }

    pub async fn health(&self) -> Result<Traced<HealthResponse>, SdkError> {
        let req = self.request(Method::GET, "/api/v1/health").build()?;
        self.client.execute_json(req).await
    }

    pub async fn info(&self) -> Result<Traced<DaemonInfo>, SdkError> {
        let req = self.request(Method::GET, "/api/v1/info").build()?;
        self.client.execute_json(req).await
    }

    /// Ask the sandbox proxy to mint a builder-compatible blob signing spec.
    ///
    /// Used during the versioned-response rollout: platform-api now returns
    /// `snapshotRelPath` instead of a pre-signed `upload` block, and the CLI
    /// calls this endpoint to resolve the artifact rel-path into a concrete
    /// upload spec. Parent snapshot downloads use the same endpoint with a
    /// full blob URI. The response is a `serde_json::Value` because most
    /// fields splice verbatim into the rootfs build spec consumed by the
    /// in-sandbox builder.
    pub async fn sign_blob(&self, request: &SignBlobRequest) -> Result<Traced<Value>, SdkError> {
        let req = self
            .request(Method::POST, "/api/v1/blob/sign")
            .json(request)
            .build()?;
        self.client.execute_json(req).await
    }
}

#[cfg(test)]
mod process_ref_tests {
    use super::{
        FileSystemMount, ProcessRef, resolve_sandbox_proxy_target, sandbox_proxy_hostname,
        sandbox_url_from_ingress_endpoint, select_sandbox_proxy_url, validate_managed_name,
        validate_mount_owners, validate_mount_snapshot_pins,
    };

    #[test]
    fn validate_managed_name_rules() {
        // Only three rejections: empty, contains '/', and all-digits (reserved for PID).
        let long_digits = "9".repeat(100);
        for bad in ["", "123", "0", "a/b", "worker/api", &long_digits] {
            assert!(
                validate_managed_name(bad).is_err(),
                "expected {bad:?} rejected"
            );
        }
        // Permissive: spaces, punctuation, leading/trailing whitespace, and unicode are all
        // allowed now (clients percent-encode the path segment).
        for ok in [
            "web",
            "web1",
            "1web",
            "my-app_v.2",
            "web 1",
            " web ",
            "a?b",
            "a%b",
            "a#b",
            "café",
        ] {
            assert!(
                validate_managed_name(ok).is_ok(),
                "expected {ok:?} accepted"
            );
        }
    }

    #[test]
    fn process_ref_path_segments() {
        assert_eq!(ProcessRef::from(1234u64).to_path_segment(), "1234");
        assert_eq!(ProcessRef::from(42i64).to_path_segment(), "42");
        // A numeric string is treated as a pid; a name is percent-encoded.
        assert_eq!(ProcessRef::from("1234").to_path_segment(), "1234");
        assert!(matches!(ProcessRef::from("1234"), ProcessRef::Pid(1234)));
        assert_eq!(ProcessRef::from("web").to_path_segment(), "web");
        assert!(matches!(ProcessRef::from("web"), ProcessRef::Name(_)));
        // Spaces and reserved characters in a name are percent-encoded into one segment.
        assert_eq!(ProcessRef::from("web 1").to_path_segment(), "web%201");
        assert_eq!(ProcessRef::from("a?b").to_path_segment(), "a%3Fb");
    }

    #[test]
    fn select_proxy_url_prefers_server_sandbox_url() {
        let selected = select_sandbox_proxy_url(
            "https://api.tensorlake.ai",
            "sbx-1",
            Some("https://sbx-1.sandbox.gcp-use4.tensorlake.ai"),
            Some("https://sandbox.us-east-1.aws.tensorlake.ai"),
            Some("https://override.example.com"),
        )
        .unwrap();

        assert_eq!(selected, "https://sbx-1.sandbox.gcp-use4.tensorlake.ai");
    }

    #[test]
    fn select_proxy_url_uses_explicit_override_when_server_url_missing() {
        let selected = select_sandbox_proxy_url(
            "https://api.tensorlake.ai",
            "sbx-1",
            None,
            Some("https://sandbox.gcp-use4.tensorlake.ai"),
            Some("https://override.example.com"),
        )
        .unwrap();

        assert_eq!(selected, "https://override.example.com");
    }

    #[test]
    fn select_proxy_url_errors_without_server_url_or_explicit_override() {
        let error = select_sandbox_proxy_url(
            "https://api.tensorlake.ai",
            "sbx-1",
            None,
            Some("https://sandbox.gcp-use4.tensorlake.ai"),
            None,
        )
        .unwrap_err();

        assert!(
            error
                .to_string()
                .contains("server response did not include sandbox_url"),
            "unexpected error: {error}"
        );
    }

    #[test]
    fn ingress_endpoint_url_builder_preserves_custom_port() {
        let selected = sandbox_url_from_ingress_endpoint(
            "https://sandbox.gcp-use4.tensorlake.ai:9443",
            "sbx-1",
            None,
        )
        .unwrap();

        assert_eq!(
            selected,
            "https://sbx-1.sandbox.gcp-use4.tensorlake.ai:9443"
        );
    }

    #[test]
    fn proxy_target_preserves_server_sandbox_host() {
        let target =
            resolve_sandbox_proxy_target("https://sbx-1.sandbox.gcp-use4.tensorlake.ai", "sbx-1")
                .unwrap();

        assert_eq!(
            target.base_url,
            "https://sbx-1.sandbox.gcp-use4.tensorlake.ai"
        );
        assert_eq!(target.host_override, None);
        assert_eq!(target.sandbox_id_header.as_deref(), Some("sbx-1"));
    }

    #[test]
    fn sandbox_proxy_hostname_uses_server_sandbox_url_host() {
        let hostname =
            sandbox_proxy_hostname("https://sbx-1.sandbox.gcp-use4.tensorlake.ai").unwrap();

        assert_eq!(hostname, "sbx-1.sandbox.gcp-use4.tensorlake.ai");
    }

    #[test]
    fn snapshot_pin_without_read_only_is_a_client_error() {
        let pinned_writable = FileSystemMount {
            file_system_id: "skills".to_string(),
            mount_path: "/mnt/skills".to_string(),
            read_only: false,
            prefetch: false,
            snapshot_id: Some("0abc123def".to_string()),
            owner: None,
        };
        let error = validate_mount_snapshot_pins(std::slice::from_ref(&pinned_writable))
            .expect_err("a writable snapshot pin must be rejected client-side");
        assert!(
            error
                .to_string()
                .contains("snapshot-pinned mounts are read-only"),
            "unexpected error: {error}"
        );

        let pinned_read_only = FileSystemMount {
            read_only: true,
            ..pinned_writable.clone()
        };
        let unpinned = FileSystemMount {
            snapshot_id: None,
            ..pinned_writable
        };
        validate_mount_snapshot_pins(&[pinned_read_only, unpinned])
            .expect("read-only pins and unpinned mounts are valid");
    }

    #[test]
    fn malformed_mount_owner_is_a_client_error() {
        let mount = |owner: Option<&str>| FileSystemMount {
            file_system_id: "skills".to_string(),
            mount_path: "/mnt/skills".to_string(),
            read_only: false,
            prefetch: false,
            snapshot_id: None,
            owner: owner.map(str::to_string),
        };

        for good in [None, Some("agent"), Some("1001:1001"), Some("agent:wheel")] {
            validate_mount_owners(std::slice::from_ref(&mount(good)))
                .expect("well-formed owners are valid");
        }

        for bad in ["", ":", "agent:", "a:b:c", "with space", "4294967296"] {
            let error = validate_mount_owners(std::slice::from_ref(&mount(Some(bad))))
                .expect_err("malformed owner specs must be rejected client-side");
            assert!(
                error.to_string().contains("invalid owner"),
                "unexpected error for {bad:?}: {error}"
            );
        }
    }
}