quilt-cli 0.37.0

Local-first data versioning for AI agent workflows: commit immutable data packages on your own machine, then sync them with S3 to share.
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
use std::path::PathBuf;

use quilt_uri::Namespace;

use crate::cli::Error;
use crate::cli::model::Commands;
use crate::cli::output::Render;
use crate::cli::output::Std;

#[derive(Debug)]
pub struct Input {
    pub namespace: Option<Namespace>,
    pub paths: Option<Vec<PathBuf>>,
    pub uri: String,
}

#[derive(Debug)]
pub struct Output {
    installed_package: quilt_rs::InstalledPackage,
    paths: Vec<std::path::PathBuf>,
    /// Requested paths not installed because the remote no longer holds their
    /// bytes (an unversioned bucket, overwritten since).
    skipped: Vec<std::path::PathBuf>,
}

#[cfg(test)]
impl Output {
    pub fn get_installed_package(self) -> quilt_rs::InstalledPackage {
        self.installed_package
    }
}

impl Output {
    /// The requested paths that were installed: every one but the skipped.
    fn installed(&self) -> impl Iterator<Item = &PathBuf> {
        self.paths
            .iter()
            .filter(|path| !self.skipped.contains(path))
    }
}

impl std::fmt::Display for Output {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut output = vec![format!("{}", self.installed_package)];
        let installed: Vec<_> = self.installed().collect();
        if installed.is_empty() {
            output.push("No paths installed".to_string());
        } else {
            for path in installed {
                output.push(format!("Path: \"{}\"", path.display()));
            }
        }
        for path in &self.skipped {
            output.push(format!(
                "Skipped: \"{}\" (no longer on the remote)",
                path.display()
            ));
        }
        write!(f, "{}", output.join("\n"))
    }
}

impl Render for Output {
    fn to_json(&self) -> serde_json::Value {
        serde_json::json!({
            "namespace": self.installed_package.namespace.to_string(),
            "paths": self
                .installed()
                // Unlike every other payload's keys, these are the caller's
                // own `&path=` and `--path` values echoed back, less the
                // skipped ones — never matched against the manifest. A non-UTF-8 argument
                // renders lossily here; it also matches no manifest key, so
                // it installs nothing.
                .map(|path| path.display().to_string())
                .collect::<Vec<_>>(),
            "skipped": self
                .skipped
                .iter()
                .map(|path| path.display().to_string())
                .collect::<Vec<_>>(),
        })
    }
}

pub async fn command(m: impl Commands, args: Input) -> Std {
    Std::from_result(m.install(args).await)
}

async fn install_package(
    local_domain: &quilt_rs::LocalDomain,
    uri: &quilt_uri::S3PackageUri,
    namespace: Namespace,
) -> Result<quilt_rs::InstalledPackage, Error> {
    let remote = local_domain.get_remote();
    if let Some(installed_package) = local_domain.get_installed_package(&namespace).await? {
        // TODO: check the actual remote_manifest
        return Ok(installed_package);
    }
    let manifest_uri =
        quilt_rs::io::manifest::resolve_manifest_uri(remote, uri.catalog.as_ref(), uri).await?;
    Ok(local_domain.install_package(&manifest_uri).await?)
}

async fn install_paths(
    installed_package: &quilt_rs::InstalledPackage,
    paths: &[PathBuf],
) -> Result<Vec<PathBuf>, Error> {
    Ok(installed_package.install_paths(paths).await?.skipped)
}

fn get_entries(
    uri_path: Option<PathBuf>,
    arg_paths: Option<Vec<PathBuf>>,
) -> Vec<std::path::PathBuf> {
    let mut paths = Vec::new();
    if let Some(logical_key) = uri_path {
        paths.push(logical_key);
    }
    if let Some(logical_keys) = arg_paths {
        for logical_key in logical_keys {
            paths.push(logical_key);
        }
    }
    paths
}

pub async fn model(
    local_domain: &quilt_rs::LocalDomain,
    Input {
        namespace,
        paths,
        uri,
    }: Input,
) -> Result<Output, Error> {
    let uri: quilt_uri::S3PackageUri = uri.parse()?;
    let path = uri.path.clone();

    let namespace = namespace.unwrap_or(uri.namespace.clone());
    let installed_package = install_package(local_domain, &uri, namespace).await?;
    let paths = get_entries(path, paths);

    let skipped = if paths.is_empty() {
        Vec::new()
    } else {
        install_paths(&installed_package, &paths).await?
    };

    Ok(Output {
        installed_package,
        paths,
        skipped,
    })
}

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

    use std::path::PathBuf;

    use test_log::test;

    use quilt_rs::io::storage::LocalStorage;
    use quilt_rs::io::storage::Storage;
    use quilt_rs::paths::DomainPaths;

    use crate::cli::fixtures::packages::default as pkg;
    use crate::cli::model::create_model_in_temp_dir;

    /// Verifies the installation process in CLI with valid data:
    ///   * lineage is updated with the installed package and tracked paths
    ///   * the working directory contains tracked mutable files
    ///   * `.quilt/objects` contains immutable files from the package
    ///   * `.quilt/installed` contains the installed manifest under the namespace directory
    ///   * `.quilt/packages` contains the cached manifest under the bucket directory
    /// Uses an actual manifest from Quilt without mocks.
    #[allow(
        clippy::too_many_lines,
        reason = "large integration test; allowed per-test so new large tests stay flagged"
    )]
    #[test(tokio::test)]
    async fn live_model() -> Result<(), Error> {
        let uri = format!("{}&path={}", pkg::URI, pkg::README_LK_ESCAPED);

        let readme_logical_key = PathBuf::from(pkg::README_LK);
        let timestamp_logical_key = PathBuf::from(pkg::TIMESTAMP_LK);

        let (m, temp_dir) = create_model_in_temp_dir().await?;
        let working_dir = temp_dir.path().join(pkg::NAMESPACE_STR);
        {
            let local_domain = m.get_local_domain();

            let output = model(
                local_domain,
                Input {
                    namespace: None,
                    paths: Some(vec![timestamp_logical_key.clone()]),
                    uri,
                },
            )
            .await?;

            assert_eq!(
                format!("{output}"),
                format!(
                    "Installed package \"{}\"\nPath: \"{}\"\nPath: \"{}\"",
                    pkg::NAMESPACE_STR,
                    pkg::README_LK,
                    pkg::TIMESTAMP_LK,
                )
            );

            let installed_package = output.installed_package;
            assert_eq!(installed_package.namespace, (pkg::NAMESPACE).into());
            assert!(
                installed_package
                    .lineage()
                    .await?
                    .paths
                    .contains_key(&readme_logical_key)
            );

            assert_eq!(
                installed_package.package_home().await?,
                PathBuf::from(temp_dir.as_ref()).join(pkg::NAMESPACE_STR)
            );
            assert_eq!(
                output.paths,
                vec![readme_logical_key.clone(), timestamp_logical_key.clone()]
            );
        }

        let storage = LocalStorage::new();
        assert!(storage.exists(working_dir.join(&readme_logical_key)).await);
        assert!(
            storage
                .exists(working_dir.join(&timestamp_logical_key))
                .await
        );

        let domain_paths = DomainPaths::new(temp_dir.path().to_path_buf());

        assert!(
            storage
                .exists(domain_paths.installed_manifest(&pkg::NAMESPACE.into(), pkg::TOP_HASH))
                .await
        );

        assert!(
            storage
                .exists(
                    domain_paths
                        .cached_manifests_dir("data-yaml-spec-tests")
                        .join(pkg::TOP_HASH)
                )
                .await
        );

        // README.md
        assert!(
            storage
                .exists(
                    domain_paths
                        .objects_dir()
                        .join("3e5e75033079a0b5bfaeff79c8f10dbc3f461e283ad8126c333cd74792e62ea7")
                )
                .await
        );
        // timestamp.txt
        assert!(
            storage
                .exists(
                    domain_paths
                        .objects_dir()
                        .join("dc3ea61d9a4aaf7d822eed1de089db83d46aa29f3fbdd99466f7e5e216c91c8a")
                )
                .await
        );

        {
            let local_domain = m.get_local_domain();
            let install_once_more = model(
                local_domain,
                Input {
                    namespace: None,
                    paths: None,
                    uri: pkg::URI.to_string(),
                },
            )
            .await?;

            // No paths installed during this call
            assert_eq!(
                format!("{install_once_more}"),
                format!(
                    "Installed package \"{}\"\nNo paths installed",
                    pkg::NAMESPACE_STR,
                )
            );

            assert_eq!(
                install_once_more.installed_package.namespace,
                pkg::NAMESPACE.into(),
            );

            // However paths are still tracked, because we didn't install a package anew,
            // but re-use installed package from the previous call.
            let paths = install_once_more.installed_package.lineage().await?.paths;
            assert!(paths.contains_key(&readme_logical_key));
            assert!(paths.contains_key(&timestamp_logical_key));
        }

        Ok(())
    }

    /// On a bucket without versioning, an older revision's key may name bytes a
    /// later revision wrote. Installing r1 must not put r2's `b.txt` under r1's
    /// row, where it would read as Modified. It skips `b.txt`, reports it,
    /// installs `a.txt`, and leaves status clean. Asking again still skips it,
    /// because the wrong bytes never entered the object store.
    #[test(tokio::test)]
    async fn live_unversioned_install_skips_a_replaced_file_and_installs_the_rest()
    -> Result<(), Error> {
        use crate::cli::fixtures::packages::unversioned as pkg;

        let (m, temp_dir) = create_model_in_temp_dir().await?;
        let working_dir = temp_dir.path().join(pkg::NAMESPACE_STR);
        let requested = vec![PathBuf::from(pkg::KEPT), PathBuf::from(pkg::REPLACED)];

        let output = model(
            m.get_local_domain(),
            Input {
                namespace: None,
                paths: Some(requested.clone()),
                uri: pkg::R1_URI.to_string(),
            },
        )
        .await?;

        assert_eq!(output.to_json()["paths"], serde_json::json!([pkg::KEPT]));
        assert_eq!(
            output.to_json()["skipped"],
            serde_json::json!([pkg::REPLACED])
        );
        assert!(
            format!("{output}").ends_with(&format!(
                "Skipped: \"{}\" (no longer on the remote)",
                pkg::REPLACED
            )),
            "{output}"
        );
        assert_eq!(
            tokio::fs::read(working_dir.join(pkg::KEPT)).await?,
            pkg::KEPT_R1_BODY
        );
        assert!(
            !working_dir.join(pkg::REPLACED).exists(),
            "r2's bytes were placed under r1's row"
        );

        let installed = output.get_installed_package();
        let lineage = installed.lineage().await?;
        assert_eq!(
            lineage.paths.keys().collect::<Vec<_>>(),
            vec![&PathBuf::from(pkg::KEPT)]
        );
        let status = installed.status(None).await?;
        assert!(
            status.changes.is_empty(),
            "untouched files read as changed: {:?}",
            status.changes.keys().collect::<Vec<_>>()
        );

        let retry = model(
            m.get_local_domain(),
            Input {
                namespace: None,
                paths: Some(vec![PathBuf::from(pkg::REPLACED)]),
                uri: pkg::R1_URI.to_string(),
            },
        )
        .await?;
        assert_eq!(
            retry.to_json()["skipped"],
            serde_json::json!([pkg::REPLACED])
        );
        assert!(!working_dir.join(pkg::REPLACED).exists());
        Ok(())
    }

    #[test(tokio::test)]
    async fn json_carries_namespace_and_paths() -> Result<(), Error> {
        use crate::cli::create;
        use crate::cli::model::create_model_in_temp_dir;

        let (m, _temp_dir) = create_model_in_temp_dir().await?;
        let created = m
            .create(create::Input {
                namespace: ("test", "pkg").into(),
                source: None,
                message: None,
            })
            .await?;

        let output = Output {
            installed_package: created.installed_package,
            paths: vec![
                std::path::PathBuf::from("data/one.csv"),
                std::path::PathBuf::from("data/two.csv"),
            ],
            skipped: vec![std::path::PathBuf::from("data/two.csv")],
        };

        assert_eq!(
            output.to_json().to_string(),
            r#"{"namespace":"test/pkg","paths":["data/one.csv"],"skipped":["data/two.csv"]}"#
        );
        Ok(())
    }

    /// A skipped path is listed once, as skipped, never as a `Path:` line: that
    /// list reads as the files now on disk.
    #[test(tokio::test)]
    async fn text_lists_a_skipped_path_only_as_skipped() -> Result<(), Error> {
        use crate::cli::create;
        use crate::cli::model::create_model_in_temp_dir;

        let (m, _temp_dir) = create_model_in_temp_dir().await?;
        let created = m
            .create(create::Input {
                namespace: ("test", "pkg").into(),
                source: None,
                message: None,
            })
            .await?;

        let output = Output {
            installed_package: created.installed_package,
            paths: vec![PathBuf::from("a.txt"), PathBuf::from("b.txt")],
            skipped: vec![PathBuf::from("b.txt")],
        };

        assert!(
            format!("{output}").ends_with(concat!(
                "\nPath: \"a.txt\"",
                "\nSkipped: \"b.txt\" (no longer on the remote)"
            )),
            "{output}"
        );
        Ok(())
    }

    /// A copy that installed no paths reports `[]`, not a missing key.
    #[test(tokio::test)]
    async fn json_paths_are_an_empty_list_when_none_installed() -> Result<(), Error> {
        use crate::cli::create;
        use crate::cli::model::create_model_in_temp_dir;

        let (m, _temp_dir) = create_model_in_temp_dir().await?;
        let created = m
            .create(create::Input {
                namespace: ("test", "bare").into(),
                source: None,
                message: None,
            })
            .await?;

        let output = Output {
            installed_package: created.installed_package,
            paths: Vec::new(),
            skipped: Vec::new(),
        };

        assert_eq!(
            output.to_json().to_string(),
            r#"{"namespace":"test/bare","paths":[],"skipped":[]}"#
        );
        Ok(())
    }
}