hf-xet 1.6.0

Client library and tooling for the Hugging Face Xet data storage system.
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
use std::io::Write;
use std::path::{Path, PathBuf};
use std::process::{Command, Output, Stdio};
use std::time::Duration;

use tempfile::{TempDir, tempdir};

fn xtool_bin() -> PathBuf {
    PathBuf::from(env!("CARGO_BIN_EXE_xtool"))
}

fn xtool_cmd(cas_dir: &Path, args: &[&str]) -> Output {
    let endpoint = format!("local://{}", cas_dir.display());
    Command::new(xtool_bin())
        .arg("--endpoint")
        .arg(&endpoint)
        .args(args)
        .output()
        .expect("failed to execute xtool binary")
}

fn xtool_cmd_with_env(args: &[&str], env_vars: &[(&str, &str)]) -> Output {
    let mut cmd = Command::new(xtool_bin());
    cmd.args(args);
    for (key, value) in env_vars {
        cmd.env(key, value);
    }
    cmd.output().expect("failed to execute xtool binary")
}

fn xtool_ok(cas_dir: &Path, args: &[&str]) -> String {
    let out = xtool_cmd(cas_dir, args);
    assert!(
        out.status.success(),
        "xtool {:?} failed:\nstdout: {}\nstderr: {}",
        args,
        String::from_utf8_lossy(&out.stdout),
        String::from_utf8_lossy(&out.stderr),
    );
    String::from_utf8(out.stdout).unwrap()
}

fn xtool_ok_with_redb_lock_retry(cas_dir: &Path, args: &[&str]) -> String {
    const MAX_ATTEMPTS: u32 = 100;
    const LOCK_MSG: &str = "Database already open. Cannot acquire lock.";

    for attempt in 0..MAX_ATTEMPTS {
        let out = xtool_cmd(cas_dir, args);
        if out.status.success() {
            return String::from_utf8(out.stdout).unwrap();
        }

        let stderr = String::from_utf8_lossy(&out.stderr);
        if stderr.contains(LOCK_MSG) && attempt + 1 < MAX_ATTEMPTS {
            let delay_ms = (5u64 << attempt.min(5)).min(100);
            std::thread::sleep(Duration::from_millis(delay_ms));
            continue;
        }

        assert!(
            out.status.success(),
            "xtool {:?} failed:\nstdout: {}\nstderr: {}",
            args,
            String::from_utf8_lossy(&out.stdout),
            stderr,
        );
    }

    unreachable!("retry loop returns on success or assertion failure");
}

#[allow(dead_code)]
fn xtool_err(cas_dir: &Path, args: &[&str]) -> String {
    let out = xtool_cmd(cas_dir, args);
    assert!(
        !out.status.success(),
        "xtool {:?} unexpectedly succeeded:\nstdout: {}\nstderr: {}",
        args,
        String::from_utf8_lossy(&out.stdout),
        String::from_utf8_lossy(&out.stderr),
    );
    let stderr = String::from_utf8(out.stderr).unwrap();
    let stdout = String::from_utf8(out.stdout).unwrap();
    format!("{stdout}{stderr}")
}

/// Upload a file via CLI and parse the stdout output line to extract hash and size.
/// Output format on stdout: `<name>  hash=<hex>  size=<n>  sha256=<hex|->`
fn upload_file(cas_dir: &Path, file_path: &Path) -> (String, u64) {
    let stdout = xtool_ok(cas_dir, &["file", "upload", file_path.to_str().unwrap()]);
    parse_upload_line(&stdout)
}

fn parse_upload_line(line: &str) -> (String, u64) {
    let mut hash = String::new();
    let mut size = 0u64;
    for part in line.split_whitespace() {
        if let Some(h) = part.strip_prefix("hash=") {
            hash = h.to_string();
        }
        if let Some(s) = part.strip_prefix("size=") {
            size = s.parse().unwrap();
        }
    }
    assert!(!hash.is_empty(), "could not parse hash from: {line}");
    (hash, size)
}

fn write_test_file(dir: &TempDir, name: &str, content: &[u8]) -> PathBuf {
    let path = dir.path().join(name);
    std::fs::write(&path, content).unwrap();
    path
}

// ─── Tests ───────────────────────────────────────────────────────────────────

#[test]
fn test_cli_help() {
    let out = Command::new(xtool_bin())
        .arg("--help")
        .output()
        .expect("failed to run xtool --help");
    assert!(out.status.success());
    let stdout = String::from_utf8(out.stdout).unwrap();
    assert!(stdout.contains("file"));
    assert!(stdout.contains("dedup"));
    assert!(stdout.contains("query"));
}

#[test]
fn test_cli_file_help() {
    let out = Command::new(xtool_bin())
        .args(["file", "--help"])
        .output()
        .expect("failed to run xtool file --help");
    assert!(out.status.success());
    let stdout = String::from_utf8(out.stdout).unwrap();
    assert!(stdout.contains("upload"));
    assert!(stdout.contains("download"));
    // scan and dump-reconstruction were folded into the top-level dedup/query commands.
    assert!(!stdout.contains("scan"));
    assert!(!stdout.contains("dump-reconstruction"));
}

#[test]
fn test_cli_upload_and_download_roundtrip() {
    let cas_dir = tempdir().unwrap();
    let src_dir = tempdir().unwrap();
    let content = b"integration test roundtrip content";
    let src = write_test_file(&src_dir, "roundtrip.txt", content);

    let (hash, size) = upload_file(cas_dir.path(), &src);
    assert_eq!(size, content.len() as u64);

    let dest_dir = tempdir().unwrap();
    let dest = dest_dir.path().join("downloaded.txt");
    xtool_ok(
        cas_dir.path(),
        &[
            "file",
            "download",
            &hash,
            "-o",
            dest.to_str().unwrap(),
            "--size",
            &size.to_string(),
        ],
    );

    assert_eq!(std::fs::read(&dest).unwrap(), content);
}

#[test]
fn test_cli_download_to_stdout() {
    let cas_dir = tempdir().unwrap();
    let src_dir = tempdir().unwrap();
    let content = b"stdout download test data";
    let src = write_test_file(&src_dir, "stdout.bin", content);

    let (hash, _size) = upload_file(cas_dir.path(), &src);

    let stdout_bytes = xtool_ok(cas_dir.path(), &["file", "download", &hash]);
    assert_eq!(stdout_bytes.as_bytes(), content);
}

#[test]
fn test_cli_download_source_range() {
    let cas_dir = tempdir().unwrap();
    let src_dir = tempdir().unwrap();
    let content = b"0123456789abcdefghijklmnopqrstuvwxyz";
    let src = write_test_file(&src_dir, "range.bin", content);

    let (hash, size) = upload_file(cas_dir.path(), &src);

    let dest_dir = tempdir().unwrap();
    let dest = dest_dir.path().join("range_out.bin");
    xtool_ok(
        cas_dir.path(),
        &[
            "file",
            "download",
            &hash,
            "-o",
            dest.to_str().unwrap(),
            "--size",
            &size.to_string(),
            "--source-range",
            "5..13",
        ],
    );

    assert_eq!(std::fs::read(&dest).unwrap(), content[5..13].to_vec());
}

#[test]
fn test_cli_download_write_range() {
    let cas_dir = tempdir().unwrap();
    let src_dir = tempdir().unwrap();
    let content = b"abcdefghij";
    let src = write_test_file(&src_dir, "write_range.bin", content);
    let (hash, _size) = upload_file(cas_dir.path(), &src);

    let dest_dir = tempdir().unwrap();
    let dest = dest_dir.path().join("write_range_out.bin");
    let mut initial = b"................".to_vec();
    std::fs::write(&dest, &initial).unwrap();

    xtool_ok(
        cas_dir.path(),
        &[
            "file",
            "download",
            &hash,
            "-o",
            dest.to_str().unwrap(),
            "--source-range",
            "2..7",
            "--write-range",
            "4..9",
        ],
    );

    initial[4..9].copy_from_slice(&content[2..7]);
    assert_eq!(std::fs::read(&dest).unwrap(), initial);
}

#[test]
fn test_cli_download_write_range_requires_output() {
    let cas_dir = tempdir().unwrap();
    let src_dir = tempdir().unwrap();
    let src = write_test_file(&src_dir, "write_range_required.bin", b"abcdef");
    let (hash, _size) = upload_file(cas_dir.path(), &src);

    let out = xtool_cmd(cas_dir.path(), &["file", "download", &hash, "--write-range", "0..4"]);
    assert!(!out.status.success());
    let stderr = String::from_utf8(out.stderr).unwrap();
    assert!(stderr.contains("--output"));
}

#[test]
fn test_cli_upload_from_stdin() {
    let cas_dir = tempdir().unwrap();
    let content = b"piped stdin content for upload";

    let endpoint = format!("local://{}", cas_dir.path().display());
    let mut child = Command::new(xtool_bin())
        .arg("--endpoint")
        .arg(&endpoint)
        .args(["file", "upload", "-"])
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .expect("failed to spawn xtool");

    child.stdin.take().unwrap().write_all(content).unwrap();
    let out = child.wait_with_output().unwrap();
    assert!(
        out.status.success(),
        "xtool file upload - failed:\nstdout: {}\nstderr: {}",
        String::from_utf8_lossy(&out.stdout),
        String::from_utf8_lossy(&out.stderr),
    );

    let stdout = String::from_utf8(out.stdout).unwrap();
    let (hash, size) = parse_upload_line(&stdout);
    assert_eq!(size, content.len() as u64);

    let stdout_bytes = xtool_ok(cas_dir.path(), &["file", "download", &hash]);
    assert_eq!(stdout_bytes.as_bytes(), content);
}

#[test]
fn test_cli_upload_multiple_files() {
    let cas_dir = tempdir().unwrap();
    let src_dir = tempdir().unwrap();

    let files: Vec<PathBuf> = (0..3)
        .map(|i| write_test_file(&src_dir, &format!("multi_{i}.bin"), format!("file {i} data").as_bytes()))
        .collect();

    let file_args: Vec<&str> = files.iter().map(|p| p.to_str().unwrap()).collect();
    let mut args = vec!["file", "upload"];
    args.extend(&file_args);
    let stdout = xtool_ok(cas_dir.path(), &args);

    let lines: Vec<&str> = stdout.lines().filter(|l| l.contains("hash=")).collect();
    assert_eq!(lines.len(), 3);
}

#[test]
fn test_cli_upload_json_output() {
    let cas_dir = tempdir().unwrap();
    let src_dir = tempdir().unwrap();
    let src = write_test_file(&src_dir, "json.txt", b"json output via cli");

    let out_dir = tempdir().unwrap();
    let json_path = out_dir.path().join("results.json");

    xtool_ok(
        cas_dir.path(),
        &[
            "file",
            "upload",
            "--output",
            json_path.to_str().unwrap(),
            src.to_str().unwrap(),
        ],
    );

    let json_str = std::fs::read_to_string(&json_path).unwrap();
    let parsed: serde_json::Value = serde_json::from_str(&json_str).unwrap();
    let arr = parsed.as_array().unwrap();
    assert_eq!(arr.len(), 1);
    assert_eq!(arr[0]["xet_info"]["file_size"], 19);
    assert!(!arr[0]["xet_info"]["hash"].as_str().unwrap().is_empty());
}

#[test]
fn test_cli_download_bad_hash() {
    let cas_dir = tempdir().unwrap();

    let fake_hash = "0".repeat(64);
    let out = xtool_cmd(cas_dir.path(), &["file", "download", &fake_hash]);
    assert!(out.stdout.is_empty(), "expected no stdout for nonexistent hash");
}

#[test]
fn test_cli_dedup_dry_run_basic() {
    let cas_dir = tempdir().unwrap();
    let src_dir = tempdir().unwrap();
    let src = write_test_file(&src_dir, "dedup_test.bin", &vec![7u8; 4096]);

    // Dry-run dedup writes the JSON file reconstruction info to stdout.
    let stdout = xtool_ok(cas_dir.path(), &["dedup", src.to_str().unwrap()]);
    let parsed: serde_json::Value = serde_json::from_str(&stdout).unwrap();
    assert!(parsed.is_array());
}

#[test]
fn test_cli_query_after_upload() {
    let cas_dir = tempdir().unwrap();
    let src_dir = tempdir().unwrap();
    let src = write_test_file(&src_dir, "recon.bin", &vec![3u8; 2048]);

    let (hash, _size) = upload_file(cas_dir.path(), &src);

    let stdout = xtool_ok(cas_dir.path(), &["query", &hash]);
    let parsed: serde_json::Value = serde_json::from_str(&stdout).unwrap();
    if !parsed.is_null() {
        assert!(parsed["terms"].is_array());
    }
}

#[test]
fn test_cli_query_with_bytes_range() {
    let cas_dir = tempdir().unwrap();
    let src_dir = tempdir().unwrap();
    let src = write_test_file(&src_dir, "recon_range.bin", &vec![9u8; 4096]);

    let (hash, _size) = upload_file(cas_dir.path(), &src);
    let stdout = xtool_ok(cas_dir.path(), &["query", &hash, "0-512"]);
    let parsed: serde_json::Value = serde_json::from_str(&stdout).unwrap();
    assert!(!parsed.is_null());
    let terms = parsed["terms"].as_array().unwrap();
    assert!(!terms.is_empty());
    let total_unpacked: u64 = terms.iter().map(|t| t["unpacked_length"].as_u64().unwrap()).sum();
    assert!(total_unpacked > 0);
}

#[test]
fn test_cli_query_nonexistent_hash() {
    let cas_dir = tempdir().unwrap();
    let fake_hash = "0".repeat(64);
    let stdout = xtool_ok(cas_dir.path(), &["query", &fake_hash]);
    let parsed: serde_json::Value = serde_json::from_str(&stdout).unwrap();
    if !parsed.is_null()
        && let Some(t) = parsed["terms"].as_array()
    {
        assert!(t.is_empty());
    }
}

#[test]
fn test_cli_upload_remote_endpoint_rejected() {
    let src_dir = tempdir().unwrap();
    let src = write_test_file(&src_dir, "remote.bin", b"should not upload");

    let out = Command::new(xtool_bin())
        .args([
            "--endpoint",
            "https://cas.example.com",
            "file",
            "upload",
            src.to_str().unwrap(),
        ])
        .output()
        .expect("failed to execute xtool");
    assert!(!out.status.success(), "upload to a remote endpoint should be rejected");
    let stderr = String::from_utf8(out.stderr).unwrap();
    assert!(stderr.contains("remote CAS endpoint"), "unexpected stderr: {stderr}");
}

#[test]
fn test_cli_quiet_mode() {
    let cas_dir = tempdir().unwrap();
    let src_dir = tempdir().unwrap();
    let src = write_test_file(&src_dir, "quiet.txt", b"quiet test");

    let out = xtool_cmd(cas_dir.path(), &["--quiet", "file", "upload", src.to_str().unwrap()]);
    assert!(out.status.success());
    let stderr = String::from_utf8(out.stderr).unwrap();
    assert!(stderr.is_empty(), "expected no stderr in quiet mode, got: {stderr}");
}

#[test]
fn test_cli_config_override_accepted() {
    let cas_dir = tempdir().unwrap();
    let src_dir = tempdir().unwrap();
    let src = write_test_file(&src_dir, "config_test.txt", b"config test");

    let endpoint = format!("local://{}", cas_dir.path().display());
    let out = Command::new(xtool_bin())
        .arg("--endpoint")
        .arg(&endpoint)
        .arg("-c")
        .arg("client.enable_multirange_fetching=true")
        .args(["file", "upload", src.to_str().unwrap()])
        .output()
        .expect("failed to execute xtool");

    assert!(out.status.success(), "config override failed:\nstderr: {}", String::from_utf8_lossy(&out.stderr));
}

#[test]
fn test_cli_hf_endpoint_env_fallback() {
    let cas_dir = tempdir().unwrap();
    let endpoint = format!("local://{}", cas_dir.path().display());
    let src_dir = tempdir().unwrap();
    let content = b"hf endpoint fallback";
    let src = write_test_file(&src_dir, "env_fallback.txt", content);

    let out = xtool_cmd_with_env(&["file", "upload", src.to_str().unwrap()], &[("HF_ENDPOINT", &endpoint)]);
    assert!(out.status.success());
    let stdout = String::from_utf8(out.stdout).unwrap();
    let (hash, size) = parse_upload_line(&stdout);
    assert_eq!(size, content.len() as u64);

    let downloaded = xtool_ok(cas_dir.path(), &["file", "download", &hash]);
    assert_eq!(downloaded.as_bytes(), content);
}

#[test]
fn test_cli_endpoint_flag_overrides_hf_endpoint() {
    let env_cas_dir = tempdir().unwrap();
    let env_endpoint = format!("local://{}", env_cas_dir.path().display());
    let flag_cas_dir = tempdir().unwrap();
    let flag_endpoint = format!("local://{}", flag_cas_dir.path().display());
    let src_dir = tempdir().unwrap();
    let content = b"endpoint override";
    let src = write_test_file(&src_dir, "override.txt", content);

    let out = xtool_cmd_with_env(
        &["--endpoint", &flag_endpoint, "file", "upload", src.to_str().unwrap()],
        &[("HF_ENDPOINT", &env_endpoint)],
    );
    assert!(out.status.success());
    let stdout = String::from_utf8(out.stdout).unwrap();
    let (hash, _size) = parse_upload_line(&stdout);

    let downloaded = xtool_ok(flag_cas_dir.path(), &["file", "download", &hash]);
    assert_eq!(downloaded.as_bytes(), content);

    let env_downloaded = xtool_ok(env_cas_dir.path(), &["file", "download", &hash]);
    assert!(env_downloaded.is_empty());
}

#[test]
fn test_cli_parallel_upload_download_stress() {
    let cas_dir = tempdir().unwrap();
    let src_dir = tempdir().unwrap();

    let n = 20;
    let files: Vec<(PathBuf, Vec<u8>)> = (0..n)
        .map(|i| {
            let content: Vec<u8> = (0..256).map(|b| ((b as u16 * (i + 1) as u16) % 256) as u8).collect();
            let path = write_test_file(&src_dir, &format!("stress_{i}.bin"), &content);
            (path, content)
        })
        .collect();

    let file_args: Vec<&str> = files.iter().map(|(p, _)| p.to_str().unwrap()).collect();
    let mut args = vec!["file", "upload", "--no-sha256"];
    args.extend(&file_args);
    let stdout = xtool_ok(cas_dir.path(), &args);

    let upload_lines: Vec<&str> = stdout.lines().filter(|l| l.contains("hash=")).collect();
    assert_eq!(upload_lines.len(), n);
    let hashes: Vec<String> = upload_lines.iter().map(|line| parse_upload_line(line).0).collect();

    let dest_dir = tempdir().unwrap();
    let cas_path = cas_dir.path().to_path_buf();
    let dest_root = dest_dir.path().to_path_buf();
    let mut workers = Vec::with_capacity(hashes.len());
    for (i, hash) in hashes.into_iter().enumerate() {
        let cas_path = cas_path.clone();
        let dest_root = dest_root.clone();
        let expected = files[i].1.clone();
        workers.push(std::thread::spawn(move || {
            let dest = dest_root.join(format!("out_{i}.bin"));
            xtool_ok_with_redb_lock_retry(&cas_path, &["file", "download", &hash, "-o", dest.to_str().unwrap()]);
            let downloaded = std::fs::read(&dest).unwrap();
            assert_eq!(downloaded, expected);
        }));
    }
    for worker in workers {
        worker.join().unwrap();
    }
}