llmman 0.1.689

Run any agent on any model, models stored as OCI images
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
//! Safe wrappers around the CGO-exported Go shim functions.
//!
//! Every Go function returns a JSON-encoded `{"ok":bool,"data":"...","error":"..."}`.
//! The wrappers decode this envelope and surface Rust `Result`s.

use std::ffi::{CStr, CString};
use std::os::raw::c_char;

use anyhow::{anyhow, Context};
use serde::{Deserialize, Serialize};

// ---------------------------------------------------------------------------
// Raw FFI declarations — symbols produced by the Go shim static archive
// ---------------------------------------------------------------------------
extern "C" {
    fn llmman_free(s: *mut c_char);
    fn llmman_login(
        server: *const c_char,
        username: *const c_char,
        password: *const c_char,
    ) -> *mut c_char;
    fn llmman_logout(server: *const c_char) -> *mut c_char;
    fn llmman_push(layout_dir: *const c_char, reference: *const c_char) -> *mut c_char;
    fn llmman_push_artifact(
        layout_dir: *const c_char,
        reference: *const c_char,
        subject_json: *const c_char,
    ) -> *mut c_char;
    fn llmman_referrers(reference: *const c_char, digest: *const c_char) -> *mut c_char;
    fn llmman_pull(reference: *const c_char, layout_dir: *const c_char) -> *mut c_char;
    fn llmman_inspect(reference: *const c_char) -> *mut c_char;
    fn llmman_transfer(source: *const c_char, destination: *const c_char) -> *mut c_char;
    fn llmman_progress(key: *const c_char) -> *mut c_char;
    fn llmman_verify(
        reference: *const c_char,
        digest: *const c_char,
        keys_json: *const c_char,
        claims: *const c_char,
        identity_json: *const c_char,
    ) -> *mut c_char;
    fn llmman_sign(
        reference: *const c_char,
        digest: *const c_char,
        key_path: *const c_char,
        password: *const c_char,
    ) -> *mut c_char;
    fn llmman_resolve_digest(reference: *const c_char) -> *mut c_char;
    fn llmman_set_registry_mirrors(mirrors_json: *const c_char) -> *mut c_char;
}

// go-shim/push_stream.go is `!podman`-only: containerd's docker.Resolver
// has a per-blob streaming push primitive, podman's copy.Image doesn't
// (see `crate::hf::transfer`'s doc comment for its podman fallback).
#[cfg(feature = "docker")]
extern "C" {
    fn llmman_push_session_open(destination: *const c_char) -> *mut c_char;
    fn llmman_push_session_close(session: i64) -> *mut c_char;
    fn llmman_push_stream_open(
        session: i64,
        media_type: *const c_char,
        digest: *const c_char,
        size: i64,
        annotations_json: *const c_char,
    ) -> *mut c_char;
    fn llmman_push_stream_wait(handle: i64) -> *mut c_char;
}

// ---------------------------------------------------------------------------
// Windows-only: manual Go runtime bootstrap
// ---------------------------------------------------------------------------
//
// On Linux (ELF) and macOS (Mach-O), `go build -buildmode=c-archive`'s
// runtime-init entry point (`_rt0_<arch>_lib`) gets registered as a real
// global constructor that the platform's own C runtime startup invokes
// automatically before `main()` ever runs (`.init_array`/`__mod_init_
// func`), exactly as cgo's docs promise — every FFI call above just works.
//
// On windows-gnu (MinGW-w64's own ld + CRT startup) this mechanism is
// emitted differently but still works: `cmd/link` (Go's own linker —
// this is unconditional, regardless of which C compiler compiled the
// shim's cgo object files) always emits an old-style GNU `.ctors` COFF
// section for this (see $GOROOT/src/cmd/link/internal/ld/pe.go's
// addInitArray), and MinGW-w64's CRT startup scans exactly that section
// for constructors to run before `main()` — confirmed locally with a
// minimal repro. But that is *not* a convention the MSVC/UCRT startup
// code that Rust's own *-pc-windows-msvc targets link against (via
// lld-link/link.exe, see build.rs) understands at all; it looks for
// `.CRT$XCU` instead. The net effect — confirmed by reproducing it
// locally against the exact same clang+lld-link toolchain this
// project's Windows CI build uses, with this crate's own real go-shim
// archive, and by reading $GOROOT's own runtime/cgo and cmd/link source
// — is that the constructor meant to kick off the Go runtime's
// background init thread (the one that unblocks every cgo call's own
// `_cgo_wait_runtime_init_done()`, which cmd/cgo generates into the
// front of every `//export`ed function — see backend_docker.go's
// llmman_pull) never runs on an MSVC target, and the very first call
// into this shim from anywhere in the process hangs forever waiting on
// an event nothing will ever signal. This was the cause of the
// Windows-only `llmman_pull` hang tracked across this repo's earlier
// trace-logging commits, which had already narrowed it down to
// "somewhere before llmman_pull's own first line ever runs" — i.e.
// squarely in this gap.
//
// The fix: call the Go runtime's own entry point ourselves. It's an
// ordinary exported symbol (`_rt0_<arch>_windows_lib`, see
// $GOROOT/src/runtime/rt0_windows_{amd64,arm64}.s) that's always present
// in the archive whether or not anything actually invokes it as a
// constructor. Calling it directly, exactly once, before any other call
// into this shim, starts the same background init thread the (non-
// functional, on this target) automatic constructor was always meant to
// start; every generated `_cgo_wait_runtime_init_done()` call proceeds
// normally after that.
//
// Scoped to `target_env = "msvc"` specifically (not all of `windows`):
// on windows-gnu the `.ctors` constructor already runs automatically as
// described above, and `_rt0_<arch>_lib`'s own body is *not* idempotent
// (it unconditionally spins up a fresh runtime-init thread every time
// it's called, with no once-guard of its own — confirmed by reading
// $GOROOT/src/runtime/asm_amd64.s) — calling it a second time here would
// race a second Go runtime bootstrap against the one already running.
// llmman doesn't currently ship a windows-gnu target (see ci.yml's
// build matrix), but scoping this to the target that actually needs it
// costs nothing and avoids relying on that.
#[cfg(all(target_os = "windows", target_env = "msvc", target_arch = "x86_64"))]
extern "C" {
    fn _rt0_amd64_windows_lib(argc: i32, argv: *mut *mut u8);
}

#[cfg(all(target_os = "windows", target_env = "msvc", target_arch = "aarch64"))]
extern "C" {
    fn _rt0_arm64_windows_lib();
}

/// Must be called once, before the first call into any other function in
/// this module — see the doc comment above. A no-op on every target
/// except windows-msvc, where the platform's own C runtime already runs
/// this automatically as a real constructor. Idempotent and cheap to
/// call more than once (guarded by a `Once`), so callers can simply
/// invoke it defensively rather than needing to prove they're first.
pub fn ensure_runtime_init() {
    #[cfg(all(target_os = "windows", target_env = "msvc"))]
    {
        static INIT: std::sync::Once = std::sync::Once::new();
        INIT.call_once(|| unsafe {
            #[cfg(target_arch = "x86_64")]
            _rt0_amd64_windows_lib(0, std::ptr::null_mut());
            #[cfg(target_arch = "aarch64")]
            _rt0_arm64_windows_lib();
        });
    }
}

// ---------------------------------------------------------------------------
// Response envelope (mirrors the Go `response` struct)
// ---------------------------------------------------------------------------

#[derive(Deserialize)]
struct GoResponse {
    ok: bool,
    #[serde(default)]
    data: String,
    #[serde(default)]
    error: String,
    /// Set by `llmman_pull` alone: the registry said the image is not there.
    #[serde(default)]
    not_found: bool,
}

/// Consume a raw C string returned by the Go shim and decode the JSON envelope.
/// Returns `Ok(data)` on success, `Err(error)` on failure.
fn consume(raw: *mut c_char) -> anyhow::Result<String> {
    assert!(!raw.is_null(), "Go shim returned a null pointer");
    let json_str = unsafe { CStr::from_ptr(raw).to_string_lossy().into_owned() };
    unsafe { llmman_free(raw) };
    decode_response(&json_str)
}

/// [`consume`]'s decoding of the shim's JSON envelope.
fn decode_response(json_str: &str) -> anyhow::Result<String> {
    let resp: GoResponse =
        serde_json::from_str(json_str).context("failed to decode Go shim response")?;
    if resp.ok {
        Ok(resp.data)
    } else if resp.not_found {
        Err(crate::storage::oci::source_not_found(anyhow!(
            "{}", resp.error
        )))
    } else {
        Err(anyhow!("{}", resp.error))
    }
}

fn cstr(s: &str) -> anyhow::Result<CString> {
    CString::new(s).context("string contains interior NUL byte")
}

// ---------------------------------------------------------------------------
// Registry configuration
// ---------------------------------------------------------------------------

/// Tells the Go shim the mirrors `llmman.conf` configures, once per
/// process. Nothing but these arguments crosses the boundary, so it has
/// to be told; a refused mirror is reported and dropped, since a pull
/// straight from the registry is the right fallback.
fn ensure_registries_configured() {
    static ONCE: std::sync::Once = std::sync::Once::new();
    ONCE.call_once(|| {
        let mirrors = crate::config::registry_mirrors();
        if mirrors.is_empty() {
            return;
        }
        let result = serde_json::to_string(&mirrors)
            .context("encode registry mirrors")
            .and_then(|json| {
                let j = cstr(&json)?;
                consume(unsafe { llmman_set_registry_mirrors(j.as_ptr()) })
            });
        if let Err(e) = result {
            eprintln!("[llmman] warning: ignoring registry mirrors: {e}");
        }
    });
}

/// [`consume`] for a call that reaches a registry: configures mirrors
/// first, so no wrapper can forget to.
fn registry_call(call: impl FnOnce() -> *mut c_char) -> anyhow::Result<String> {
    ensure_registries_configured();
    consume(call())
}

// ---------------------------------------------------------------------------
// Safe public API
// ---------------------------------------------------------------------------

/// Store registry credentials.
pub fn login(server: &str, username: &str, password: &str) -> anyhow::Result<()> {
    let s = cstr(server)?;
    let u = cstr(username)?;
    let p = cstr(password)?;
    consume(unsafe { llmman_login(s.as_ptr(), u.as_ptr(), p.as_ptr()) }).map(|_| ())
}

/// Remove stored registry credentials.
pub fn logout(server: &str) -> anyhow::Result<()> {
    let s = cstr(server)?;
    consume(unsafe { llmman_logout(s.as_ptr()) }).map(|_| ())
}

/// Push the image tagged `reference` from `layout_dir` (OCI layout) to a registry.
pub fn push(layout_dir: &str, reference: &str) -> anyhow::Result<()> {
    let l = cstr(layout_dir)?;
    let r = cstr(reference)?;
    registry_call(|| unsafe { llmman_push(l.as_ptr(), r.as_ptr()) }).map(|_| ())
}

/// An OCI descriptor crossing the registry FFI boundary.
///
/// `artifact_type` is populated for referrers so callers can distinguish a
/// signature, SBOM, or later attestation without fetching each manifest.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct RegistryDescriptor {
    pub media_type: String,
    pub digest: String,
    pub size: u64,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub artifact_type: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub annotations: Option<std::collections::HashMap<String, String>>,
}

/// Set `subject` on the locally stored artifact manifest and push it to the
/// registry. Registries without the OCI 1.1 referrers endpoint receive the
/// standard digest-derived fallback index automatically.
pub fn push_artifact(
    layout_dir: &str,
    reference: &str,
    subject: &RegistryDescriptor,
) -> anyhow::Result<()> {
    let l = cstr(layout_dir)?;
    let r = cstr(reference)?;
    let s = cstr(&serde_json::to_string(subject).context("encode subject descriptor")?)?;
    registry_call(|| unsafe { llmman_push_artifact(l.as_ptr(), r.as_ptr(), s.as_ptr()) })
        .map(|_| ())
}

/// List the OCI artifacts attached to `digest` in `reference`'s repository.
pub fn referrers(reference: &str, digest: &str) -> anyhow::Result<Vec<RegistryDescriptor>> {
    let r = cstr(reference)?;
    let d = cstr(digest)?;
    let data = registry_call(|| unsafe { llmman_referrers(r.as_ptr(), d.as_ptr()) })?;
    serde_json::from_str(&data).context("decode referrers")
}

/// Pull an image from a registry into `layout_dir` (OCI layout).
pub fn pull(reference: &str, layout_dir: &str) -> anyhow::Result<()> {
    let r = cstr(reference)?;
    let l = cstr(layout_dir)?;
    registry_call(|| unsafe { llmman_pull(r.as_ptr(), l.as_ptr()) }).map(|_| ())
}

/// Fetch and return the raw manifest JSON for a remote registry reference.
pub fn inspect_remote(reference: &str) -> anyhow::Result<String> {
    let r = cstr(reference)?;
    registry_call(|| unsafe { llmman_inspect(r.as_ptr()) })
}

/// The result of one completed transfer, whichever of the three
/// implementations (`ffi::transfer`, `crate::hf::transfer`,
/// `crate::sources::transfer`) produced it.
#[derive(Debug, Clone, Deserialize)]
pub struct TransferOutcome {
    /// Whether anything was actually pushed. Everything is
    /// content-addressed, so re-running a transfer for an unchanged
    /// source pushes nothing — this lets `cmd::transfer` say so instead
    /// of always printing "Transferred ...".
    pub changed: bool,
    /// The manifest digest now at the destination. `--sign-key` signs
    /// exactly this rather than re-resolving the tag afterwards, so
    /// nothing can slip in between the push and the signature.
    #[serde(default)]
    pub digest: Option<String>,
}

impl TransferOutcome {
    /// For the transfer paths that push a manifest they built or staged
    /// themselves, and so already know its digest.
    pub fn new(changed: bool, digest: impl Into<String>) -> Self {
        Self {
            changed,
            digest: Some(digest.into()),
        }
    }
}

/// Transfer an image directly from `source` to `destination` without
/// going through the local store. See go-shim/transfer_docker.go /
/// transfer_podman.go for how each backend implements this (streamed
/// blob-for-blob where possible, falling back to a throwaway local
/// staging directory only for source kinds that transport has no way to
/// stream).
pub fn transfer(source: &str, destination: &str) -> anyhow::Result<TransferOutcome> {
    let s = cstr(source)?;
    let d = cstr(destination)?;
    let data = registry_call(|| unsafe { llmman_transfer(s.as_ptr(), d.as_ptr()) })?;
    serde_json::from_str(&data).context("decode transfer outcome")
}

/// A keyless trust rule as the shim reads it (go-shim/sigstore.go's
/// `identityPolicy`): the OIDC issuer and the certificate SAN a signing
/// certificate must carry, and the Sigstore TrustedRoot to anchor it
/// to. Unset fields are omitted rather than sent as `null`, which the
/// Go side cannot tell from an empty string anyway.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Identity {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub issuer: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub subject: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub trusted_root: Option<String>,
}

/// One signature that verified, and the trusted key or identity that
/// accepted it.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SignatureMatch {
    /// What accepted it, as a label fit for a report: the key path for
    /// a key match, the rule's issuer and subject for an identity match.
    pub signer: String,
    /// The trusted key file that accepted it; empty for an identity
    /// match.
    #[serde(default)]
    pub key_path: String,
    /// The `docker-reference` the signer claimed to be signing.
    pub identity: String,
}

/// The outcome of checking a manifest's signatures — see
/// go-shim/sigstore.go's `verifyReport`.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct VerifyReport {
    pub reference: String,
    pub digest: String,
    pub verified: bool,
    /// Signature layers present on the registry, whether or not any
    /// verified — "nobody signed this" and "someone signed this, but not
    /// with a key you trust" are very different things to report.
    pub signatures_found: u32,
    pub matches: Vec<SignatureMatch>,
    /// Why `verified` is false. Empty when it's true.
    pub reason: String,
}

/// Check the signatures published for `reference` against the PEM public
/// keys at `keys` and, when set, the keyless `identity`. `digest` may be
/// `""` to resolve `reference` first. `claims` is the glob the
/// signature's claimed repository must match, or `""` to require it to
/// equal the pulled repository. An unset `identity` crosses as `""`, the
/// same convention `digest` and `claims` use.
///
/// "Not verified" comes back as `Ok` with `verified` false; `Err` means
/// no answer could be reached. `crate::verify` decides what each means.
pub fn verify(
    reference: &str,
    digest: &str,
    keys: &[String],
    claims: &str,
    identity: Option<&Identity>,
) -> anyhow::Result<VerifyReport> {
    let r = cstr(reference)?;
    let d = cstr(digest)?;
    let k = cstr(&serde_json::to_string(keys).context("encode trusted key list")?)?;
    let c = cstr(claims)?;
    let i = cstr(&match identity {
        Some(identity) => serde_json::to_string(identity).context("encode identity policy")?,
        None => String::new(),
    })?;
    let data = registry_call(|| unsafe {
        llmman_verify(r.as_ptr(), d.as_ptr(), k.as_ptr(), c.as_ptr(), i.as_ptr())
    })?;
    serde_json::from_str(&data).context("decode verification report")
}

/// Sign `reference` at `digest` with the PEM private key at `key_path`
/// and publish a cosign-format signature beside it in the same
/// repository. Returns the digest that was signed.
///
/// `digest` may be `""` to resolve `reference` remotely first.
/// `password` is ignored for an unencrypted key.
pub fn sign(
    reference: &str,
    digest: &str,
    key_path: &str,
    password: &str,
) -> anyhow::Result<String> {
    let r = cstr(reference)?;
    let d = cstr(digest)?;
    let k = cstr(key_path)?;
    let p = cstr(password)?;
    registry_call(|| unsafe { llmman_sign(r.as_ptr(), d.as_ptr(), k.as_ptr(), p.as_ptr()) })
}

/// The manifest digest the registry currently serves for `reference`,
/// without fetching any layer. `crate::verify::PullGuard` uses it to
/// check a signature before starting a multi-gigabyte download.
pub fn resolved_digest_of(reference: &str) -> anyhow::Result<String> {
    let r = cstr(reference)?;
    registry_call(|| unsafe { llmman_resolve_digest(r.as_ptr()) })
}

/// A registry destination resolved once via [`push_session_open`] and
/// shared by every [`push_stream_open`] call within one transfer —
/// resolving a destination negotiates auth (and, for a non-HTTPS-only
/// host, containerd's HTTPS-then-HTTP-fallback probe), neither of which
/// is free to redo per blob. Releases itself (best-effort) on `Drop`, so
/// an early `?` return partway through a transfer can't leak it.
#[cfg(feature = "docker")]
pub struct PushSession(i64);

#[cfg(feature = "docker")]
pub fn push_session_open(destination: &str) -> anyhow::Result<PushSession> {
    #[derive(Deserialize)]
    struct Result_ {
        session: i64,
    }
    let d = cstr(destination)?;
    let data = registry_call(|| unsafe { llmman_push_session_open(d.as_ptr()) })?;
    let result: Result_ =
        serde_json::from_str(&data).context("decode push_session_open response")?;
    Ok(PushSession(result.session))
}

#[cfg(feature = "docker")]
impl Drop for PushSession {
    fn drop(&mut self) {
        if let Err(e) = consume(unsafe { llmman_push_session_close(self.0) }) {
            eprintln!("[llmman] close push session: {e:#}");
        }
    }
}

/// The write end of a pipe Go created for one streamed blob/manifest
/// push (see go-shim/push_stream.go), plus the handle to collect its
/// outcome with [`push_stream_wait`]. `fd` is a raw POSIX fd on Unix or
/// a raw Windows `HANDLE` widened to `u64` — Go, not this side, creates
/// the pipe (see `crate::hf::transfer`'s doc comment for why). Once
/// wrapped in a `File` (`from_raw_fd`/`from_raw_handle`), `fd` itself is
/// a stale value — don't read it again.
#[cfg(feature = "docker")]
#[must_use = "must be passed to push_stream_wait, or the Go-side handle and its goroutine leak"]
pub struct PushStream {
    pub fd: u64,
    handle: i64,
}

#[cfg(feature = "docker")]
#[derive(Deserialize)]
struct PushStreamOpenResult {
    /// The destination already has the blob; `fd`/`handle` are unset.
    #[serde(default)]
    exists: bool,
    #[serde(default)]
    fd: u64,
    #[serde(default)]
    handle: i64,
}

/// What [`push_stream_open`] found at the destination.
#[cfg(feature = "docker")]
pub enum PushStreamOpen {
    /// The destination already has this blob: nothing to write or wait on.
    Exists,
    /// An upload is in progress: write the content to the stream's
    /// `fd`, close it, then [`push_stream_wait`].
    Stream(PushStream),
}

#[cfg(feature = "docker")]
impl PushStreamOpen {
    fn decode(data: &str) -> anyhow::Result<Self> {
        let result: PushStreamOpenResult =
            serde_json::from_str(data).context("decode push_stream_open response")?;
        Ok(if result.exists {
            Self::Exists
        } else {
            Self::Stream(PushStream {
                fd: result.fd,
                handle: result.handle,
            })
        })
    }
}

/// Starts pushing one blob (or the final manifest — the registry tells
/// them apart by `media_type`, not anything passed here) within
/// `session` (see [`push_session_open`]). `digest` is `"sha256:<hex>"`.
/// `annotations_json` is an optional JSON object (`""` for none).
///
/// Checks the destination first: [`PushStreamOpen::Exists`] if it has the
/// blob (a registry failure is an `Err` here), otherwise the write end of
/// a pipe to stream into; see [`push_stream_wait`].
#[cfg(feature = "docker")]
pub fn push_stream_open(
    session: &PushSession,
    media_type: &str,
    digest: &str,
    size: i64,
    annotations_json: &str,
) -> anyhow::Result<PushStreamOpen> {
    let m = cstr(media_type)?;
    let g = cstr(digest)?;
    let a = cstr(annotations_json)?;
    let data = consume(unsafe {
        llmman_push_stream_open(session.0, m.as_ptr(), g.as_ptr(), size, a.as_ptr())
    })?;
    PushStreamOpen::decode(&data)
}

/// Blocks until the streamed push started by [`push_stream_open`]
/// finishes — the caller must have already finished writing and closed
/// its own end of `stream.fd` before calling this, or it hangs waiting
/// for an EOF that will never come. Returns whether the destination
/// didn't already have this exact blob (by digest).
#[cfg(feature = "docker")]
pub fn push_stream_wait(stream: PushStream) -> anyhow::Result<bool> {
    #[derive(Deserialize)]
    struct WaitResult {
        changed: bool,
    }
    let data = consume(unsafe { llmman_push_stream_wait(stream.handle) })?;
    let result: WaitResult =
        serde_json::from_str(&data).context("decode push_stream_wait response")?;
    Ok(result.changed)
}

/// A byte-level snapshot of one particular pull/push (identified by
/// `key`, the exact model reference it was started with), as tracked by
/// the Go shim's `progressState` (see go-shim/progress_state.go).
/// `total`/`completed` are 0 until the shim learns a blob's size and
/// starts transferring it, or if `key` has no tracked entry (not started
/// yet, or already finished and cleaned up).
#[derive(Deserialize)]
pub struct ProgressSnapshot {
    pub status: String,
    pub total: i64,
    pub completed: i64,
}

/// Polls the Go shim's pull/push progress snapshot for `key` (the same
/// model reference passed to `pull`/`push` above). Called by `cmd::serve`
/// every ~200ms while a `/api/pull` or `/api/push` task for that same key
/// is in flight, to relay real byte counts over its NDJSON stream instead
/// of just a coarse "pulling <model>" heartbeat — see llmman_progress's
/// own doc comment for why the daemon can't just let the shim's own mpb
/// bars (go-shim/shared_oci.go) reach an interactive terminal directly.
/// Keying by reference (rather than one process-wide snapshot, as this
/// used to be) is what lets two different models' pulls/pushes run
/// concurrently in the same daemon without their progress numbers
/// interleaving — see serve.rs's per-model lock registry.
pub fn progress(key: &str) -> anyhow::Result<ProgressSnapshot> {
    let k = cstr(key)?;
    let data = consume(unsafe { llmman_progress(k.as_ptr()) })?;
    serde_json::from_str(&data).context("failed to decode progress snapshot")
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::storage::oci::is_source_not_found;

    #[test]
    fn a_successful_envelope_returns_its_data() {
        assert_eq!(decode_response(r#"{"ok":true,"data":"x"}"#).unwrap(), "x");
    }

    #[test]
    fn a_failed_envelope_is_the_message_and_nothing_more() {
        let err = decode_response(r#"{"ok":false,"error":"dial tcp: no such host"}"#).unwrap_err();
        assert_eq!(err.to_string(), "dial tcp: no such host");
        assert!(!is_source_not_found(&err));
    }

    #[test]
    fn a_not_found_envelope_is_marked_without_changing_the_message() {
        let json =
            r#"{"ok":false,"error":"resolve: docker.io/ai/nope:1b: not found","not_found":true}"#;
        let err = decode_response(json).unwrap_err();
        assert!(is_source_not_found(&err));
        assert_eq!(err.to_string(), "resolve: docker.io/ai/nope:1b: not found");
        let err = err.context("pull failed");
        assert!(is_source_not_found(&err));
        assert_eq!(
            format!("{err:#}"),
            "pull failed: resolve: docker.io/ai/nope:1b: not found"
        );
    }

    /// An envelope from a shim built before the field existed.
    #[test]
    fn an_envelope_without_not_found_is_not_marked() {
        let err = decode_response(r#"{"ok":false,"error":"x: not found"}"#).unwrap_err();
        assert!(!is_source_not_found(&err));
    }

    #[cfg(feature = "docker")]
    #[test]
    fn push_stream_open_decodes_exists_without_a_stream() {
        let open = PushStreamOpen::decode(r#"{"exists":true,"fd":0,"handle":0}"#).unwrap();
        assert!(matches!(open, PushStreamOpen::Exists));
    }

    #[cfg(feature = "docker")]
    #[test]
    fn push_stream_open_decodes_a_stream() {
        let open = PushStreamOpen::decode(r#"{"exists":false,"fd":7,"handle":3}"#).unwrap();
        match open {
            PushStreamOpen::Stream(s) => assert_eq!((s.fd, s.handle), (7, 3)),
            PushStreamOpen::Exists => panic!("expected a stream"),
        }
    }

    #[cfg(feature = "docker")]
    #[test]
    fn push_stream_open_rejects_garbage() {
        assert!(PushStreamOpen::decode("not json").is_err());
    }

    #[test]
    fn identity_crosses_as_camel_case_without_unset_fields() {
        let identity = Identity {
            issuer: Some("https://issuer".into()),
            subject: None,
            trusted_root: Some("/etc/llmman/root.json".into()),
        };
        assert_eq!(
            serde_json::to_string(&identity).unwrap(),
            r#"{"issuer":"https://issuer","trustedRoot":"/etc/llmman/root.json"}"#
        );
    }

    #[test]
    fn a_signature_match_without_a_key_path_decodes() {
        let m: SignatureMatch =
            serde_json::from_str(r#"{"signer":"https://issuer me@x","identity":"docker.io/a/b"}"#)
                .unwrap();
        assert_eq!(m.signer, "https://issuer me@x");
        assert_eq!(m.key_path, "");
    }
}