hf2q 0.1.17

Pure Rust CLI for converting HuggingFace models to hardware-optimized formats and serving them over an OpenAI-compatible API on Apple Silicon
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
use super::*;

pub(super) enum PreparedLocalArtifact {
    Existing {
        retained: crate::core::bounded_file::StableRegularFile,
        destination: PathBuf,
    },
    Adoption {
        source: crate::core::bounded_file::StableRegularFile,
        destination_parent: crate::core::bounded_file::StableDirectory,
        destination_name: std::ffi::OsString,
        destination: PathBuf,
    },
}

impl PreparedLocalArtifact {
    pub(super) fn prepare(
        source: &Path,
        destination: &Path,
        bytes: u64,
        sha256: &str,
    ) -> Result<Self> {
        if verify_or_refuse_existing_destination(destination, bytes, sha256)? {
            let retained = retain_exact_digest(destination, bytes, sha256)?
                .context("exact local destination changed while retaining its authority")?;
            return Ok(Self::Existing {
                retained,
                destination: destination.to_path_buf(),
            });
        }
        let source = source
            .canonicalize()
            .with_context(|| format!("resolve materialization source {}", source.display()))?;
        let source = retain_exact_digest(&source, bytes, sha256)?
            .context("materialization source changed after exact verification")?;
        let parent = destination
            .parent()
            .context("managed artifact has no parent")?;
        let destination_parent =
            crate::core::bounded_file::StableDirectory::create_and_open(parent)
                .context("retain managed artifact destination parent")?;
        let destination_name = destination
            .file_name()
            .context("managed artifact destination has no filename")?
            .to_os_string();
        Ok(Self::Adoption {
            source,
            destination_parent,
            destination_name,
            destination: destination.to_path_buf(),
        })
    }

    pub(super) fn prepare_retained(
        source: crate::core::bounded_file::StableRegularFile,
        destination: &Path,
        bytes: u64,
        sha256: &str,
    ) -> Result<Self> {
        if verify_or_refuse_existing_destination(destination, bytes, sha256)? {
            let retained = retain_exact_digest(destination, bytes, sha256)?
                .context("exact local destination changed while retaining its authority")?;
            return Ok(Self::Existing {
                retained,
                destination: destination.to_path_buf(),
            });
        }
        if !source.is_stable()? {
            bail!("retained materialization source changed during planning");
        }
        let parent = destination
            .parent()
            .context("managed artifact has no parent")?;
        let destination_parent =
            crate::core::bounded_file::StableDirectory::create_and_open(parent)
                .context("retain managed artifact destination parent")?;
        let destination_name = destination
            .file_name()
            .context("managed artifact destination has no filename")?
            .to_os_string();
        Ok(Self::Adoption {
            source,
            destination_parent,
            destination_name,
            destination: destination.to_path_buf(),
        })
    }

    pub(super) fn destination(&self) -> &Path {
        match self {
            Self::Existing { destination, .. } | Self::Adoption { destination, .. } => destination,
        }
    }

    pub(super) fn needs_copy(&self) -> bool {
        matches!(self, Self::Adoption { .. })
    }

    pub(super) fn source_device_id(&self) -> Option<u64> {
        match self {
            Self::Existing { .. } => None,
            Self::Adoption { source, .. } => Some(source.device_id()),
        }
    }

    pub(super) fn destination_device_id(&self) -> Option<u64> {
        match self {
            Self::Existing { retained, .. } => Some(retained.device_id()),
            Self::Adoption {
                destination_parent, ..
            } => Some(destination_parent.device_id()),
        }
    }

    pub(super) fn destination_available_bytes(&self) -> Option<u64> {
        match self {
            Self::Existing { .. } => None,
            Self::Adoption {
                destination_parent, ..
            } => destination_parent.available_bytes(),
        }
    }

    pub(super) fn is_current(&self) -> Result<bool> {
        match self {
            Self::Existing { retained, .. } => Ok(retained.is_stable()?),
            Self::Adoption {
                source,
                destination_parent,
                ..
            } => Ok(source.is_stable()? && destination_parent.is_current()?),
        }
    }

    pub(super) fn materialize(self, repository: &str, bytes: u64, sha256: &str) -> Result<()> {
        match self {
            Self::Existing { retained, .. } => {
                if !retained.is_stable()? {
                    bail!("exact local destination changed before activation");
                }
                Ok(())
            }
            Self::Adoption {
                source,
                destination_parent,
                destination_name,
                ..
            } => materialize_retained_exact_at(
                source,
                destination_parent,
                &destination_name,
                repository,
                bytes,
                sha256,
            ),
        }
    }
}

fn retain_exact_digest(
    path: &Path,
    bytes: u64,
    sha256: &str,
) -> Result<Option<crate::core::bounded_file::StableRegularFile>> {
    let Some(mut retained) = crate::core::bounded_file::StableRegularFile::open_exact(path, bytes)?
    else {
        return Ok(None);
    };
    Ok(retained
        .sha256()?
        .is_some_and(|digest| digest.eq_ignore_ascii_case(sha256))
        .then_some(retained))
}

/// Check a final destination before any payload transfer. Exact bytes are an
/// idempotent hit; any other existing entry is a fail-closed conflict.
pub(super) fn verify_or_refuse_existing_destination(
    destination: &Path,
    bytes: u64,
    sha256: &str,
) -> Result<bool> {
    let metadata = match fs::symlink_metadata(destination) {
        Ok(metadata) => metadata,
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(false),
        Err(error) => return Err(error.into()),
    };
    if metadata.file_type().is_symlink() || !metadata.is_file() || metadata.len() != bytes {
        bail!(
            "destination conflicts with the selected immutable artifact: {}",
            destination.display()
        );
    }
    if !crate::core::bounded_file::sha256_regular_nofollow_exact(destination, bytes)?
        .is_some_and(|digest| digest.eq_ignore_ascii_case(sha256))
    {
        bail!(
            "destination conflicts with the selected immutable artifact: {}",
            destination.display()
        );
    }
    Ok(true)
}

/// Materialize bytes whose complete SHA-256 was verified by the immediately
/// preceding selection/download step. On Apple filesystems a clone avoids a
/// second model-sized allocation; cross-filesystem fallback copies and hashes.
pub(super) fn materialize_preverified_exact(
    source: &Path,
    destination: &Path,
    repository: &str,
    bytes: u64,
    sha256: &str,
) -> Result<()> {
    let canonical = source
        .canonicalize()
        .with_context(|| format!("resolve materialization source {}", source.display()))?;
    let retained = crate::core::bounded_file::StableRegularFile::open_exact(&canonical, bytes)?
        .context("materialization source is not the expected regular file")?;
    materialize_retained_exact_with_proof(
        retained,
        destination,
        repository,
        bytes,
        sha256,
        RetainedProof::VerifyStagedClone,
    )
}

#[derive(Clone, Copy)]
enum RetainedProof {
    AlreadyVerified,
    VerifyStagedClone,
}

/// Adopt a source that was hashed and admitted through a retained descriptor.
/// The staged clone/copy is published create-if-absent, and the retained
/// source plus staged inode identities replace a second model-sized hash.
pub(super) fn materialize_retained_exact(
    source: crate::core::bounded_file::StableRegularFile,
    destination: &Path,
    repository: &str,
    bytes: u64,
    sha256: &str,
) -> Result<()> {
    materialize_retained_exact_with_proof(
        source,
        destination,
        repository,
        bytes,
        sha256,
        RetainedProof::AlreadyVerified,
    )
}

fn materialize_retained_exact_with_proof(
    source: crate::core::bounded_file::StableRegularFile,
    destination: &Path,
    repository: &str,
    bytes: u64,
    sha256: &str,
    proof: RetainedProof,
) -> Result<()> {
    let parent = destination
        .parent()
        .context("managed artifact has no parent")?;
    let destination_parent = crate::core::bounded_file::StableDirectory::create_and_open(parent)
        .context("retain managed artifact destination parent")?;
    let destination_name = destination
        .file_name()
        .context("managed artifact destination has no filename")?;
    materialize_retained_exact_at_with_proof(
        source,
        destination_parent,
        destination_name,
        repository,
        bytes,
        sha256,
        proof,
    )
}

pub(super) fn materialize_retained_exact_at(
    source: crate::core::bounded_file::StableRegularFile,
    destination_parent: crate::core::bounded_file::StableDirectory,
    destination_name: &std::ffi::OsStr,
    repository: &str,
    bytes: u64,
    sha256: &str,
) -> Result<()> {
    materialize_retained_exact_at_with_proof(
        source,
        destination_parent,
        destination_name,
        repository,
        bytes,
        sha256,
        RetainedProof::AlreadyVerified,
    )
}

pub(super) fn materialize_preverified_exact_at(
    source: &Path,
    destination_parent: crate::core::bounded_file::StableDirectory,
    destination_name: &std::ffi::OsStr,
    repository: &str,
    bytes: u64,
    sha256: &str,
) -> Result<()> {
    let canonical = source
        .canonicalize()
        .with_context(|| format!("resolve materialization source {}", source.display()))?;
    let retained = crate::core::bounded_file::StableRegularFile::open_exact(&canonical, bytes)?
        .context("materialization source is not the expected regular file")?;
    materialize_retained_exact_at_with_proof(
        retained,
        destination_parent,
        destination_name,
        repository,
        bytes,
        sha256,
        RetainedProof::VerifyStagedClone,
    )
}

fn materialize_retained_exact_at_with_proof(
    mut source: crate::core::bounded_file::StableRegularFile,
    destination_parent: crate::core::bounded_file::StableDirectory,
    destination_name: &std::ffi::OsStr,
    repository: &str,
    bytes: u64,
    sha256: &str,
    proof: RetainedProof,
) -> Result<()> {
    use rustix::fs::{AtFlags, Mode, OFlags, RenameFlags};

    if !source.is_stable()? || !destination_parent.is_current()? {
        bail!("materialization authority changed before retained adoption");
    }
    let canonical_destination = destination_parent.canonical_path().join(destination_name);
    if verify_or_refuse_existing_destination(&canonical_destination, bytes, sha256)? {
        return Ok(());
    }
    let parent_fd = destination_parent.try_clone()?;
    let staged_name =
        std::ffi::OsString::from(format!(".hf2q-adopt-{}.partial", uuid::Uuid::new_v4()));
    let mut guard = StagedAtGuard::new(parent_fd.try_clone()?, staged_name.clone());
    let (staged_file, cloned) = match source.clone_to_at(&parent_fd, &staged_name) {
        Ok(()) => {
            guard.arm();
            (
                fs::File::from(rustix::fs::openat(
                    &parent_fd,
                    &staged_name,
                    OFlags::RDONLY | OFlags::NOFOLLOW | OFlags::NONBLOCK | OFlags::CLOEXEC,
                    Mode::empty(),
                )?),
                true,
            )
        }
        Err(error) if clone_requires_copy(&error) => {
            let _ = rustix::fs::unlinkat(&parent_fd, &staged_name, AtFlags::empty());
            check_hub_artifact_destination(repository, &canonical_destination, bytes)?;
            let mut output = fs::File::from(rustix::fs::openat(
                &parent_fd,
                &staged_name,
                OFlags::WRONLY | OFlags::CREATE | OFlags::EXCL | OFlags::NOFOLLOW | OFlags::CLOEXEC,
                Mode::from_bits_truncate(0o600),
            )?);
            guard.arm();
            let (copied_bytes, copied_sha256) = source
                .copy_and_hash(&mut output)?
                .context("materialization source changed during retained copy")?;
            if copied_bytes != bytes || !copied_sha256.eq_ignore_ascii_case(sha256) {
                bail!("retained copy failed exact size/SHA-256 verification");
            }
            output.sync_all()?;
            (output, false)
        }
        Err(error) => {
            let _ = rustix::fs::unlinkat(&parent_fd, &staged_name, AtFlags::empty());
            return Err(error.into());
        }
    };
    let staged_path = destination_parent.canonical_path().join(&staged_name);
    let mut staged = crate::core::bounded_file::StableRegularFile::from_open_file(
        staged_file,
        &staged_path,
        bytes,
    )?
    .context("retained adoption did not produce the expected regular file")?;
    if cloned
        && matches!(proof, RetainedProof::VerifyStagedClone)
        && !staged
            .sha256()?
            .is_some_and(|digest| digest.eq_ignore_ascii_case(sha256))
    {
        bail!("staged clone failed exact SHA-256 verification");
    }
    if !source.is_stable()? || !staged.descriptor_is_stable()? {
        bail!("materialization source or staged clone changed before publication");
    }
    match rustix::fs::renameat_with(
        &parent_fd,
        &staged_name,
        &parent_fd,
        destination_name,
        RenameFlags::NOREPLACE,
    ) {
        Ok(()) => {}
        Err(error) if error == rustix::io::Errno::EXIST => {
            if verify_or_refuse_existing_destination(&canonical_destination, bytes, sha256)? {
                return Ok(());
            }
            return Err(std::io::Error::from_raw_os_error(error.raw_os_error()).into());
        }
        Err(error) => return Err(std::io::Error::from_raw_os_error(error.raw_os_error()).into()),
    }
    guard.disarm();
    let published = staged.current_descriptor_matches_path(&canonical_destination)?;
    let source_stable = source.is_stable()?;
    let public_parent_stable = destination_parent.is_current()?;
    if !published || !source_stable || !public_parent_stable {
        bail!("retained artifact publication changed before authority binding");
    }
    Ok(())
}

struct StagedAtGuard {
    directory: fs::File,
    name: std::ffi::OsString,
    armed: bool,
}

impl StagedAtGuard {
    fn new(directory: fs::File, name: std::ffi::OsString) -> Self {
        Self {
            directory,
            name,
            armed: false,
        }
    }

    fn arm(&mut self) {
        self.armed = true;
    }

    fn disarm(&mut self) {
        self.armed = false;
    }
}

impl Drop for StagedAtGuard {
    fn drop(&mut self) {
        if self.armed {
            let _ = rustix::fs::unlinkat(&self.directory, &self.name, rustix::fs::AtFlags::empty());
        }
    }
}

pub(super) fn clone_requires_copy(error: &std::io::Error) -> bool {
    error
        .raw_os_error()
        .is_some_and(|code| [libc::EXDEV, libc::ENOTSUP, libc::EOPNOTSUPP].contains(&code))
}