liboxen 0.65.0

Oxen is a fast data version control system, built with machine learning training data in mind. Designed to handle terabytes of data with ease, using a workflow similar to git. Version both structured and unstructured data of any modality: text, images, video, audio, CSV, Parquet, JSONL, model checkpoints, and more. liboxen is the embeddable core library behind the oxen CLI and server, which power fine tuning and inference pipelines for multimodal LLMs, image models, and video models on Oxen.ai.
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
//! # oxen schemas
//!
//! Interact with schemas
//!

use crate::error::OxenError;
use crate::model::{Commit, LocalRepository, Schema};
use crate::repositories;

use std::path::Path;
use tokio::task::spawn_blocking;

pub use crate::core::v_latest::data_frames::schemas::list;

pub use crate::core::v_latest::data_frames::schemas::get_by_path;

/// [`get_by_path`], off the async worker.
pub async fn get_by_path_async(
    repo: &LocalRepository,
    commit: &Commit,
    path: &Path,
) -> Result<Option<Schema>, OxenError> {
    let repo = repo.clone();
    let commit = commit.clone();
    let path = path.to_path_buf();
    spawn_blocking(move || get_by_path(&repo, &commit, path)).await?
}

/// Get a staged schema
pub use crate::core::v_latest::data_frames::schemas::get_staged;

/// Get staged schema for workspace
pub use crate::core::v_latest::data_frames::schemas::get_staged_schema_with_staged_db_manager;

/// [`get_staged_schema_with_staged_db_manager`], off the async worker.
pub async fn get_staged_schema_with_staged_db_manager_async(
    repo: &LocalRepository,
    path: &Path,
) -> Result<Option<Schema>, OxenError> {
    let repo = repo.clone();
    let path = path.to_path_buf();
    spawn_blocking(move || get_staged_schema_with_staged_db_manager(&repo, path)).await?
}

/// List all the staged schemas
pub use crate::core::v_latest::data_frames::schemas::list_staged;

/// Get a string representation of the schema given a schema ref
pub fn show(
    repo: &LocalRepository,
    path: impl AsRef<Path>,
    staged: bool,
    verbose: bool,
) -> Result<String, OxenError> {
    let path = path.as_ref();
    let schema = if staged {
        get_staged(repo, path)?
    } else {
        match repositories::commits::head_commit_maybe(repo)? {
            Some(commit) => repositories::data_frames::schemas::get_by_path(repo, &commit, path)?,
            None => None,
        }
    };

    log::debug!("show: {schema:?}");
    let Some(schema) = schema else {
        return Err(OxenError::schema_does_not_exist(path));
    };

    let mut results = String::new();
    if verbose {
        let verbose_str = schema.verbose_str();
        results.push_str(&format!(
            "{} {}\n{}\n",
            path.to_string_lossy(),
            schema.hash,
            verbose_str
        ));
    } else {
        results.push_str(&format!(
            "{}\t{}\t{}",
            path.to_string_lossy(),
            schema.hash,
            schema
        ))
    }
    Ok(results)
}

/// Remove a schema override from the staging area, TODO: Currently undefined behavior for non-staged schemas
pub use crate::core::v_latest::data_frames::schemas::rm;

/// Add metadata to the schema
pub use crate::core::v_latest::data_frames::schemas::add_schema_metadata;

/// Add metadata to a specific column
pub use crate::core::v_latest::data_frames::schemas::add_column_metadata;

// unit tests
#[cfg(test)]
mod tests {
    use crate::error::OxenError;
    use crate::model::Schema;
    use crate::test;
    use crate::util;
    use crate::{command, repositories};

    use serde_json::json;
    use std::path::{Path, PathBuf};
    use std::sync::Barrier;

    #[tokio::test]
    async fn test_list_staged_serves_simultaneous_callers() -> Result<(), OxenError> {
        // Listing staged schemas is a read. Concurrent readers all have to get an answer rather
        // than one of them failing on RocksDB's exclusive per-path lock.
        test::run_empty_local_repo_test_async(|repo| async move {
            const CALLERS: usize = 8;
            let barrier = Barrier::new(CALLERS);
            let results = std::thread::scope(|scope| {
                let threads: Vec<_> = (0..CALLERS)
                    .map(|_| {
                        scope.spawn(|| {
                            barrier.wait();
                            repositories::data_frames::schemas::list_staged(&repo)
                        })
                    })
                    .collect();
                threads
                    .into_iter()
                    .map(|thread| thread.join().expect("listing thread panicked"))
                    .collect::<Vec<_>>()
            });

            for result in results {
                assert!(result?.is_empty(), "nothing is staged in an empty repo");
            }

            Ok(())
        })
        .await
    }

    #[tokio::test]
    async fn test_command_schema_list() -> Result<(), OxenError> {
        test::run_training_data_repo_test_fully_committed_async(|repo| async move {
            let commit = repositories::commits::head_commit(&repo)?;
            let schemas = repositories::data_frames::schemas::list(&repo, &commit)?;
            assert_eq!(schemas.len(), 8);
            let path = PathBuf::from("annotations")
                .join("train")
                .join("bounding_box.csv");

            let schema = repositories::data_frames::schemas::get_by_path(&repo, &commit, &path)?
                .expect("bounding_box.csv carries a schema");
            assert_eq!(schema.hash, "b821946753334c083124fd563377d795");
            assert_bounding_box_fields(&schema);

            Ok(())
        })
        .await
    }

    #[tokio::test]
    async fn test_stage_and_commit_schema() -> Result<(), OxenError> {
        test::run_training_data_repo_test_no_commits_async(|repo| async move {
            // Make sure no schemas are staged
            let status = repositories::status(&repo).await?;
            assert_eq!(status.staged_schemas.len(), 0);

            // Schema should be staged when added
            let bbox_filename = Path::new("annotations")
                .join("train")
                .join("bounding_box.csv");
            let bbox_file = repo.path.join(&bbox_filename);
            repositories::add(&repo, bbox_file).await?;

            // Make sure it is staged
            let status = repositories::status(&repo).await?;
            assert_eq!(status.staged_schemas.len(), 1);

            // Schema should be committed after commit
            let commit = repositories::commit(&repo, "Adding bounding box schema")?;

            // Make sure no schemas are staged after commit
            let status = repositories::status(&repo).await?;
            assert_eq!(status.staged_schemas.len(), 0);

            // Fetch schema from HEAD commit
            let schema =
                repositories::data_frames::schemas::get_by_path(&repo, &commit, &bbox_filename)?
                    .expect("the committed csv carries a schema");
            assert_eq!(schema.hash, "b821946753334c083124fd563377d795");
            assert_bounding_box_fields(&schema);

            // Write a new commit that is modifies any file
            let readme_file = repo.path.join("README.md");
            util::fs::write(&readme_file, "Changing the README")?;
            repositories::add(&repo, readme_file).await?;
            let commit = repositories::commit(&repo, "Changing the README")?;

            // Fetch schema from HEAD commit, it should still be there in all it's glory
            let schema =
                repositories::data_frames::schemas::get_by_path(&repo, &commit, &bbox_filename)?
                    .expect("a commit that leaves the csv alone keeps its parent's schema");
            assert_eq!(schema.hash, "b821946753334c083124fd563377d795");
            assert_bounding_box_fields(&schema);

            Ok(())
        })
        .await
    }

    /// Asserts the columns of `annotations/train/bounding_box.csv`.
    fn assert_bounding_box_fields(schema: &Schema) {
        let fields: Vec<(&str, &str)> = schema
            .fields
            .iter()
            .map(|field| (field.name.as_str(), field.dtype.as_str()))
            .collect();
        assert_eq!(
            fields,
            [
                ("file", "str"),
                ("label", "str"),
                ("min_x", "f64"),
                ("min_y", "f64"),
                ("width", "i64"),
                ("height", "i64"),
            ]
        );
    }

    // Each step starts from the schema the step before it left, staged or committed.
    #[tokio::test]
    async fn test_schemas_column_and_schema_metadata() -> Result<(), OxenError> {
        test::run_select_data_repo_test_no_commits_async("annotations", |repo| async move {
            let bbox_file = Path::new("annotations")
                .join("train")
                .join("bounding_box.csv");
            let bbox_path = repo.path.join(&bbox_file);
            let get_staged = || -> Result<Schema, OxenError> {
                Ok(
                    repositories::data_frames::schemas::get_staged(&repo, &bbox_file)?
                        .expect("the csv has a staged schema"),
                )
            };

            // Stage the file
            repositories::add(&repo, &bbox_path).await?;
            assert_bounding_box_fields(&get_staged()?);

            let file_metadata = json!({
                "root": "images"
            });
            repositories::data_frames::schemas::add_column_metadata(
                &repo,
                &bbox_file,
                "file",
                &file_metadata,
            )?;
            let schema = get_staged()?;
            assert_bounding_box_fields(&schema);
            assert_eq!(schema.fields[0].metadata, Some(file_metadata.to_owned()));

            // Commit the schema
            let commit = repositories::commit(&repo, "Adding bounding box schema")?;
            let schemas = repositories::data_frames::schemas::list(&repo, &commit)?;
            assert_eq!(schemas.len(), 1);
            let schema = schemas.values().next().unwrap();
            assert_bounding_box_fields(schema);
            assert_eq!(schema.fields[0].metadata, Some(file_metadata.to_owned()));

            // Add schema metadata to the committed schema
            let schema_metadata = json!({
                "task": "bounding_box",
                "description": "detect some bounding boxes"
            });
            repositories::data_frames::schemas::add_schema_metadata(
                &repo,
                &bbox_file,
                &schema_metadata,
            )?;
            assert_eq!(get_staged()?.metadata, Some(schema_metadata.to_owned()));
            // Make sure to do this last for this test, because then we get str instead of path as the dtype_override
            repositories::data_frames::schemas::add_column_metadata(
                &repo,
                &bbox_file,
                "file",
                &file_metadata,
            )?;
            let schema = get_staged()?;
            assert_eq!(schema.metadata, Some(schema_metadata.to_owned()));
            assert_eq!(schema.fields[0].metadata, Some(file_metadata.to_owned()));

            // Adding the unchanged file again keeps its staged schema change
            repositories::add(&repo, &bbox_path).await?;
            assert_eq!(get_staged()?.metadata, Some(schema_metadata));

            // Update the schema
            let min_x_metadata = json!({
                "key": "val"
            });
            let updated_schemas = repositories::data_frames::schemas::add_column_metadata(
                &repo,
                &bbox_file,
                "min_x",
                &min_x_metadata,
            )?;
            let updated_schema = updated_schemas
                .get(&bbox_file)
                .expect("Expected to find updated schema");
            let schema = get_staged()?;
            assert_eq!(updated_schema.hash, schema.hash);
            assert_bounding_box_fields(&schema);
            // this was added in the previous commit, so it should still be there
            assert_eq!(schema.fields[0].metadata, Some(file_metadata.to_owned()));
            assert_eq!(schema.fields[2].metadata, Some(min_x_metadata.to_owned()));

            // Commit the schema again
            repositories::commit(&repo, "Updating the bounding box schema")?;

            // Update the schema
            let width_metadata = json!({
                "metric": "meters"
            });
            let updated_schemas = repositories::data_frames::schemas::add_column_metadata(
                &repo,
                &bbox_file,
                "width",
                &width_metadata,
            )?;
            let updated_schema = updated_schemas
                .get(&bbox_file)
                .expect("Expected to find updated schema");
            let schema = get_staged()?;
            assert_eq!(updated_schema.hash, schema.hash);
            assert_bounding_box_fields(&schema);
            // these were added in the previous commits, so they should still be there
            assert_eq!(schema.fields[0].metadata, Some(file_metadata.to_owned()));
            assert_eq!(schema.fields[2].metadata, Some(min_x_metadata));
            assert_eq!(schema.fields[4].metadata, Some(width_metadata));

            // Commit the schema, then add a new column to the data frame
            repositories::commit(&repo, "Adding metadata to width column")?;
            command::df::add_column(&bbox_path, "new_column:0:i32").await?;
            repositories::add(&repo, &bbox_path).await?;

            // Make sure the metadata persisted
            let schema = get_staged()?;
            assert_eq!(schema.fields.len(), 7);
            assert_eq!(schema.fields[0].name, "file");
            assert_eq!(schema.fields[0].dtype, "str");
            assert_eq!(schema.fields[0].metadata, Some(file_metadata));

            // Remove the staged schema
            repositories::data_frames::schemas::rm(&repo, &bbox_file, true)?;
            assert!(
                repositories::data_frames::schemas::get_staged(&repo, &bbox_file)?.is_none(),
                "no staged schema is left after removing it"
            );

            Ok(())
        })
        .await
    }

    /// The staged db holds directory markers and non-tabular files next to data
    /// frames, so listing staged schemas has to skip them rather than fail.
    #[tokio::test]
    async fn test_schemas_list_staged_skips_entries_without_a_schema() -> Result<(), OxenError> {
        test::run_empty_local_repo_test_async(|repo| async move {
            let csv_path = repo.path.join("nested").join("data.csv");
            util::fs::create_dir_all(csv_path.parent().unwrap())?;
            test::write_txt_file_to_path(&csv_path, "a,b\n1,2\n")?;
            let txt_path = repo.path.join("nested").join("readme.txt");
            test::write_txt_file_to_path(&txt_path, "not a data frame\n")?;

            // Staging the csv also stages `nested` and the repo root as directory markers.
            repositories::add(&repo, &csv_path).await?;
            repositories::add(&repo, &txt_path).await?;

            let staged = repositories::data_frames::schemas::list_staged(&repo)?;
            let paths: Vec<&PathBuf> = staged.keys().collect();
            assert_eq!(
                paths.len(),
                1,
                "only the data frame has a staged schema, got {paths:?}"
            );
            let schema = staged
                .get(&PathBuf::from("nested").join("data.csv"))
                .expect("the csv's schema is listed under its repo-relative path");
            let names: Vec<&str> = schema.fields.iter().map(|f| f.name.as_str()).collect();
            assert_eq!(names, vec!["a", "b"]);

            Ok(())
        })
        .await
    }

    #[cfg_attr(windows, ignore = "oxen-server is not supported on Windows")]
    #[tokio::test]
    async fn test_schemas_merge_fast_forward_pull() -> Result<(), OxenError> {
        test::run_select_data_sync_remote("README.md", |_local_repo, remote_repo| async move {
            let remote_repo_copy = remote_repo.clone();
            test::run_empty_dir_test_async(|repo_dir_a| async move {
                let repo_dir_a = repo_dir_a.join("repo_a");
                let cloned_repo_a =
                    repositories::clone_url(&remote_repo.remote.url, &repo_dir_a).await?;

                test::run_empty_dir_test_async(|repo_dir_b| async move {
                    println!("=== Starting test: Two users pushing to remote ===");
                    println!("User A: Will add schema metadata to dataframe");
                    println!("User B: Will edit README");

                    // Write a data frame to data.csv
                    let data_filename = Path::new("data.csv");
                    let data_path = cloned_repo_a.path.join(data_filename);
                    let column_name = "name";
                    let data_content = "name,age\nJohn,30\nJane,25";
                    util::fs::write_to_path(&data_path, data_content)?;
                    repositories::add(&cloned_repo_a, &data_path).await?;
                    repositories::commit(&cloned_repo_a, "User A adding data.csv")?;
                    // Push to the remote so User B can pull
                    repositories::push(&cloned_repo_a).await?;

                    // Clone repo to User B before we edit the schema metadata
                    let repo_dir_b = repo_dir_b.join("repo_b");
                    let cloned_repo_b =
                        repositories::clone_url(&remote_repo.remote.url, &repo_dir_b).await?;

                    // User A: Add schema metadata to the data.csv dataframe
                    println!("User A: Adding schema metadata...");
                    // Add schema metadata
                    let column_metadata = json!({
                        "my_custom": "name_column_metadata"
                    });
                    repositories::data_frames::schemas::add_column_metadata(
                        &cloned_repo_a,
                        data_filename,
                        column_name,
                        &column_metadata,
                    )?;
                    repositories::commit(&cloned_repo_a, "User A adding schema metadata")?;
                    println!("User A: Pushing schema metadata...");
                    repositories::push(&cloned_repo_a).await?;
                    println!("User A pushed ✅ 1");

                    // Make sure the schema metadata is in the schema
                    let commit = repositories::commits::head_commit(&cloned_repo_a)?;
                    let schema = repositories::data_frames::schemas::get_by_path(
                        &cloned_repo_a,
                        &commit,
                        data_filename,
                    )?
                    .expect("Schema should exist");
                    println!("User A: Schema: {schema:?}");

                    // Save the number of commits in User A
                    let num_commits_a = repositories::commits::list(&cloned_repo_a)?.len();
                    println!("User A: Number of commits: {num_commits_a}");
                    let commits = repositories::commits::list(&cloned_repo_a)?;
                    for commit in &commits {
                        println!("User A: GOT COMMIT {commit}");
                    }
                    println!("================================================");

                    // User B: Edit the README (but don't push yet)
                    println!("User B: Editing README...");
                    let readme_path = cloned_repo_b.path.join("README.md");
                    let readme_content = "Updated README by User B";
                    util::fs::write_to_path(&readme_path, readme_content)?;
                    repositories::add(&cloned_repo_b, &readme_path).await?;
                    repositories::commit(&cloned_repo_b, "User B editing README")?;

                    // User B should have all the commits from User A
                    let commits = repositories::commits::list(&cloned_repo_b)?;
                    for commit in &commits {
                        println!("User B: GOT COMMIT BEFORE {commit}");
                    }
                    println!("================================================");

                    // User B tries to push - should fail because remote is ahead
                    println!("User B: Attempting to push README changes (should fail because remote is ahead)...");
                    let push_result = repositories::push(&cloned_repo_b).await;
                    assert!(push_result.is_err(), "Push should fail when remote is ahead");
                    println!("User B push failed ✅ 3 (as expected - remote is ahead)");

                    // User B pulls - should succeed with fast-forward merge (no conflicts)
                    println!("User B: Pulling changes...");
                    repositories::pull(&cloned_repo_b).await?;
                    println!("User B pulled ✅ 4");

                    // User B should have all the commits from User A
                    let commits = repositories::commits::list(&cloned_repo_b)?;
                    for commit in &commits {
                        println!("User B: GOT COMMIT AFTER {commit}");
                    }
                    println!("================================================");
                    // assert_eq!(commits.len(), num_commits_a);

                    // Verify User B has both changes locally:
                    // 1. The README should still have User B's changes
                    let readme_content_after_pull = util::fs::read_from_path(&readme_path)?;
                    assert_eq!(
                        readme_content_after_pull, readme_content,
                        "README should still have User B's changes"
                    );

                    // 2. The schema should have User A's metadata
                    let commit = repositories::commits::head_commit(&cloned_repo_b)?;
                    println!("User B: CHECKING COMMIT {commit}");
                    let schema = repositories::data_frames::schemas::get_by_path(
                        &cloned_repo_b,
                        &commit,
                        data_filename,
                    )?
                    .expect("Schema should exist");
                    println!("User B: Schema: {schema:?}");
                    let name_field = schema.fields.iter()
                        .find(|field| field.name == column_name)
                        .expect("Schema should have a 'name' field");
                    assert_eq!(
                        name_field.metadata, Some(column_metadata.clone()),
                        "Schema should have User A's metadata on the 'name' field"
                    );

                    // User B pushes - should succeed now after pulling
                    println!("User B: Pushing README changes (should succeed after pull)...");
                    repositories::push(&cloned_repo_b).await?;
                    println!("User B pushed ✅ 5");

                    Ok(())
                })
                .await?;
                Ok(())
            })
            .await?;
            Ok(remote_repo_copy)
        })
        .await
    }
}