eggup-eggpack 0.1.3

Optional Eggpack ReleaseManifest adapter for Eggup deployment inputs (no release policy, authenticity, or destination selection)
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
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
#![forbid(unsafe_code)]
#![deny(missing_docs)]
#![doc = "Optional consumer-side adapter from Eggpack ReleaseManifest v1 to Eggup inputs."]

use eggpack_manifest::{ArtifactForm, ReleaseManifest, MAX_DOCUMENT_BYTES};
use eggup_acquisition::{AcquisitionRequest, FetchLimits};
use eggup_archive::{ArchiveFormat, ArchiveLimits, ArchiveMember, ArchivePlan, BoundMember};
use eggup_core::{
    ArtifactMember, ArtifactSet, BoundSources, InstallPlan, IntegrityRequirement, MemberId,
    PermissionsIntent, ProductId, ReleaseId,
};
use std::{
    collections::{HashMap, HashSet},
    fmt, fs,
    path::{Path, PathBuf},
};

/// Typed adapter failure. Details never contain URLs or file contents.
#[derive(Debug)]
#[non_exhaustive]
pub enum AdapterError {
    /// Manifest or Eggup identity is invalid.
    InvalidManifest,
    /// Exact canonical target is absent.
    TargetNotFound,
    /// Request, acquired-file, or permission maps differ from the required set.
    MapMismatch(&'static str),
    /// Caller artifact limit is lower than the manifest exact size.
    CallerLimitTooSmall(String),
    /// Acquired path is relative, missing, linked, non-regular, or the wrong size.
    InvalidAcquiredFile(String),
    /// Archive member bytes cannot be made installable without qualified extraction.
    ArchiveExtractionRequired,
    /// Caller-selected artifact name does not name a supported archive format.
    UnsupportedArchiveFormat,
    /// Construction in a lower Eggup layer failed.
    Eggup(String),
}

impl fmt::Display for AdapterError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::InvalidManifest => f.write_str("invalid manifest or identity"),
            Self::TargetNotFound => f.write_str("canonical target not found"),
            Self::MapMismatch(kind) => write!(f, "{kind} map does not exactly match manifest"),
            Self::CallerLimitTooSmall(name) => {
                write!(f, "caller byte limit is below manifest size for {name}")
            }
            Self::InvalidAcquiredFile(name) => write!(f, "acquired file is invalid for {name}"),
            Self::ArchiveExtractionRequired => f.write_str("archive extraction is required"),
            Self::UnsupportedArchiveFormat => {
                f.write_str("artifact name does not name a supported archive format")
            }
            Self::Eggup(detail) => write!(f, "Eggup input construction failed: {detail}"),
        }
    }
}
impl std::error::Error for AdapterError {}

/// Maximum accepted UTF-8 JSON document size, as defined by Eggpack's manifest schema.
pub const MAX_MANIFEST_BYTES: usize = MAX_DOCUMENT_BYTES;

/// One manifest artifact paired with its exact installed member relationship.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ArtifactRequirement {
    /// Manifest artifact name used only as an exact map key.
    pub artifact_name: String,
    /// Exact expected byte count.
    pub exact_size: u64,
    /// Manifest SHA-256 bytes.
    pub sha256: [u8; 32],
    /// Eggup member identity.
    pub member_id: MemberId,
    /// Flat relative install destination.
    pub destination: String,
}

/// Preserved archive member evidence; extraction remains a separate boundary.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ArchiveMemberRequirement {
    /// Normalized archive source path.
    pub source: String,
    /// Flat installation identity.
    pub install: String,
    /// Exact member size.
    pub exact_size: u64,
    /// Member SHA-256 bytes.
    pub sha256: [u8; 32],
}

/// Manifest projection for one exact canonical target.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ManifestProjection {
    /// Direct or bundle files can become one ArtifactSet after acquisition.
    Installable {
        /// Opaque product identity.
        product: ProductId,
        /// Opaque release identity.
        release: ReleaseId,
        /// Exact selected canonical triple.
        target: String,
        /// Ordered direct or bundle relationships.
        artifacts: Vec<ArtifactRequirement>,
    },
    /// Archive facts are retained but require separately qualified extraction.
    Archive {
        /// Opaque product identity.
        product: ProductId,
        /// Opaque release identity.
        release: ReleaseId,
        /// Exact selected canonical triple.
        target: String,
        /// Archive artifact key.
        artifact_name: String,
        /// Exact archive byte count.
        exact_size: u64,
        /// Archive SHA-256 bytes.
        sha256: [u8; 32],
        /// Expected member relationships, not verified extracted files.
        members: Vec<ArchiveMemberRequirement>,
    },
}

impl ManifestProjection {
    fn installable(
        &self,
    ) -> Result<(&ProductId, &ReleaseId, &[ArtifactRequirement]), AdapterError> {
        match self {
            Self::Installable {
                product,
                release,
                artifacts,
                ..
            } => Ok((product, release, artifacts)),
            Self::Archive { .. } => Err(AdapterError::ArchiveExtractionRequired),
        }
    }

    /// Manifest-default destinations keyed by stable member identity.
    ///
    /// Pure helper with no I/O. For installable projections each entry maps
    /// the projected `MemberId` to its manifest `install` default
    /// destination. For archive projections each entry maps
    /// `MemberId::new(install)` to the manifest `install` identity. Callers
    /// that bind their own destinations do not need this helper; the
    /// compatibility wrappers use it to preserve prior default behavior.
    pub fn default_destinations(&self) -> Result<HashMap<MemberId, String>, AdapterError> {
        match self {
            Self::Installable { artifacts, .. } => Ok(artifacts
                .iter()
                .map(|a| (a.member_id.clone(), a.destination.clone()))
                .collect()),
            Self::Archive { members, .. } => members
                .iter()
                .map(|m| {
                    let id = MemberId::new(m.install.clone())
                        .map_err(|e| AdapterError::Eggup(e.to_string()))?;
                    Ok((id, m.install.clone()))
                })
                .collect(),
        }
    }

    /// Bind every artifact to one exact caller-selected request and tightened limit.
    pub fn bind_requests(
        &self,
        requests: HashMap<String, AcquisitionRequest>,
        baseline: FetchLimits,
    ) -> Result<Vec<PlannedAcquisition>, AdapterError> {
        baseline
            .validate()
            .map_err(|e| AdapterError::Eggup(e.to_string()))?;
        let requirements = match self {
            Self::Installable { artifacts, .. } => artifacts
                .iter()
                .map(|a| {
                    (
                        a.artifact_name.clone(),
                        a.exact_size,
                        a.sha256,
                        a.member_id.clone(),
                    )
                })
                .collect::<Vec<_>>(),
            Self::Archive {
                artifact_name,
                exact_size,
                sha256,
                ..
            } => vec![(
                artifact_name.clone(),
                *exact_size,
                *sha256,
                MemberId::new(artifact_name.clone())
                    .map_err(|e| AdapterError::Eggup(e.to_string()))?,
            )],
        };
        if requests.len() != requirements.len()
            || requirements.iter().any(|(n, ..)| !requests.contains_key(n))
        {
            return Err(AdapterError::MapMismatch("acquisition request"));
        }
        requirements
            .into_iter()
            .map(|(name, size, sha, member)| {
                if baseline.max_artifact_bytes < size {
                    return Err(AdapterError::CallerLimitTooSmall(name.clone()));
                }
                Ok(PlannedAcquisition {
                    artifact_name: name.clone(),
                    request: requests.get(&name).expect("checked map").clone(),
                    limits: FetchLimits {
                        max_artifact_bytes: size,
                        ..baseline
                    },
                    exact_size: size,
                    sha256: sha,
                    member_id: member,
                })
            })
            .collect()
    }

    /// Validate acquired files and build one all-or-nothing direct/bundle
    /// `ArtifactSet` with caller-bound destinations.
    ///
    /// Manifest `install` values remain producer default identity only: the
    /// exact relative destination of every member comes from `destinations`,
    /// which must contain exactly the projected member set. Missing, extra,
    /// or colliding destinations fail closed before any set is returned, and
    /// invalid destinations fail through Eggup destination validation into a
    /// bounded adapter error. Artifact filename to acquired-path
    /// relationships, stable member identities, exact sizes, regular-file
    /// checks, SHA-256 evidence, and permission binding are unchanged.
    /// Replacement authorization stays outside this helper: it records where
    /// bytes should be installed and never proves control of a live
    /// destination. No URL or filesystem content is echoed in diagnostics.
    pub fn materialize_artifact_set_with_destinations(
        &self,
        acquired: HashMap<String, PathBuf>,
        destinations: HashMap<MemberId, String>,
        permissions: HashMap<MemberId, PermissionsIntent>,
    ) -> Result<ArtifactSet, AdapterError> {
        let (_, _, reqs) = self.installable()?;
        if acquired.len() != reqs.len()
            || reqs
                .iter()
                .any(|r| !acquired.contains_key(&r.artifact_name))
        {
            return Err(AdapterError::MapMismatch("acquired path"));
        }
        if destinations.len() != reqs.len()
            || reqs
                .iter()
                .any(|r| !destinations.contains_key(&r.member_id))
        {
            return Err(AdapterError::MapMismatch("destinations"));
        }
        if permissions.len() != reqs.len()
            || reqs.iter().any(|r| !permissions.contains_key(&r.member_id))
        {
            return Err(AdapterError::MapMismatch("permissions"));
        }
        let mut members = Vec::with_capacity(reqs.len());
        for r in reqs {
            let path = acquired.get(&r.artifact_name).expect("checked map");
            if !path.is_absolute() {
                return Err(AdapterError::InvalidAcquiredFile(r.artifact_name.clone()));
            }
            let meta = fs::symlink_metadata(path)
                .map_err(|_| AdapterError::InvalidAcquiredFile(r.artifact_name.clone()))?;
            if !meta.file_type().is_file() || meta.len() != r.exact_size {
                return Err(AdapterError::InvalidAcquiredFile(r.artifact_name.clone()));
            }
            let destination = destinations.get(&r.member_id).expect("checked map");
            members.push(
                ArtifactMember::new(r.member_id.clone(), path, destination)
                    .map_err(|e| AdapterError::Eggup(e.to_string()))?
                    .with_permissions(*permissions.get(&r.member_id).expect("checked map"))
                    .with_integrity(IntegrityRequirement::Sha256(r.sha256)),
            );
        }
        let mut seen = HashSet::with_capacity(members.len());
        for member in &members {
            if !seen.insert(member.destination().to_path_buf()) {
                return Err(AdapterError::MapMismatch("destinations"));
            }
        }
        ArtifactSet::new(members).map_err(|e| AdapterError::Eggup(e.to_string()))
    }

    /// Validate acquired files and build one all-or-nothing direct/bundle ArtifactSet.
    ///
    /// Compatibility wrapper: builds the manifest-default destination map and
    /// delegates to
    /// [`materialize_artifact_set_with_destinations`](Self::materialize_artifact_set_with_destinations).
    /// There is one materialization implementation.
    pub fn materialize_artifact_set(
        &self,
        acquired: HashMap<String, PathBuf>,
        permissions: HashMap<MemberId, PermissionsIntent>,
    ) -> Result<ArtifactSet, AdapterError> {
        let destinations = self.default_destinations()?;
        self.materialize_artifact_set_with_destinations(acquired, destinations, permissions)
    }
}

/// Exact request and manifest evidence to pass to an acquisition transport.
#[derive(Debug, Clone)]
pub struct PlannedAcquisition {
    /// Exact manifest artifact key.
    pub artifact_name: String,
    /// Exact unmodified caller request.
    pub request: AcquisitionRequest,
    /// Caller limits with artifact byte cap tightened to manifest size.
    pub limits: FetchLimits,
    /// Expected byte count.
    pub exact_size: u64,
    /// Expected SHA-256 bytes.
    pub sha256: [u8; 32],
    /// Related Eggup member identity.
    pub member_id: MemberId,
}

/// Validate and project one exact canonical target without performing I/O.
pub fn project(
    manifest: &ReleaseManifest,
    canonical_target: &str,
) -> Result<ManifestProjection, AdapterError> {
    manifest
        .validate()
        .map_err(|_| AdapterError::InvalidManifest)?;
    let target = manifest
        .target(canonical_target)
        .map_err(|_| AdapterError::TargetNotFound)?;
    let product = ProductId::new(manifest.product_id.clone())
        .map_err(|e| AdapterError::Eggup(e.to_string()))?;
    let release = ReleaseId::new(manifest.release_id.clone())
        .map_err(|e| AdapterError::Eggup(e.to_string()))?;
    let common = (product, release, target.target.clone());
    match &target.form {
        ArtifactForm::Direct { artifact, install } => Ok(ManifestProjection::Installable {
            product: common.0,
            release: common.1,
            target: common.2,
            artifacts: vec![ArtifactRequirement {
                artifact_name: artifact.name.clone(),
                exact_size: artifact.size,
                sha256: artifact
                    .sha256_bytes()
                    .map_err(|_| AdapterError::InvalidManifest)?,
                member_id: MemberId::new(install.clone())
                    .map_err(|e| AdapterError::Eggup(e.to_string()))?,
                destination: install.clone(),
            }],
        }),
        ArtifactForm::Bundle { entries } => Ok(ManifestProjection::Installable {
            product: common.0,
            release: common.1,
            target: common.2,
            artifacts: entries
                .iter()
                .map(|e| {
                    Ok(ArtifactRequirement {
                        artifact_name: e.artifact.name.clone(),
                        exact_size: e.artifact.size,
                        sha256: e
                            .artifact
                            .sha256_bytes()
                            .map_err(|_| AdapterError::InvalidManifest)?,
                        member_id: MemberId::new(e.install.clone())
                            .map_err(|x| AdapterError::Eggup(x.to_string()))?,
                        destination: e.install.clone(),
                    })
                })
                .collect::<Result<_, AdapterError>>()?,
        }),
        ArtifactForm::Archive { artifact, members } => Ok(ManifestProjection::Archive {
            product: common.0,
            release: common.1,
            target: common.2,
            artifact_name: artifact.name.clone(),
            exact_size: artifact.size,
            sha256: artifact
                .sha256_bytes()
                .map_err(|_| AdapterError::InvalidManifest)?,
            members: members
                .iter()
                .map(|m| {
                    Ok(ArchiveMemberRequirement {
                        source: m.source.clone(),
                        install: m.install.clone(),
                        exact_size: m.bytes.size,
                        sha256: m
                            .bytes
                            .sha256_bytes()
                            .map_err(|_| AdapterError::InvalidManifest)?,
                    })
                })
                .collect::<Result<_, AdapterError>>()?,
        }),
    }
}

/// Parse bounded ReleaseManifest JSON and project one exact canonical target.
///
/// Parsing and schema validation are delegated to `eggpack-manifest`. Input
/// contents and parser diagnostics are never included in returned errors.
pub fn project_json(
    input: &[u8],
    canonical_target: &str,
) -> Result<ManifestProjection, AdapterError> {
    if input.len() > MAX_MANIFEST_BYTES {
        return Err(AdapterError::InvalidManifest);
    }
    let json = std::str::from_utf8(input).map_err(|_| AdapterError::InvalidManifest)?;
    let manifest = ReleaseManifest::from_json(json).map_err(|_| AdapterError::InvalidManifest)?;
    project(&manifest, canonical_target)
}

/// Return product and release identities for an installable projection.
pub fn install_ids(
    projection: &ManifestProjection,
) -> Result<(&ProductId, &ReleaseId), AdapterError> {
    let (product, release, _) = projection.installable()?;
    Ok((product, release))
}

// ---------------------------------------------------------------------------
// Archive extraction handoff (M002).
//
// These helpers translate `ManifestProjection::Archive` into typed inputs the
// caller feeds to `eggup-archive::extract` and `eggup-core`'s
// `BoundSources`-based prepare seam. They perform no I/O, no extraction, no
// commit, and never reach outside their arguments; composition stays
// caller-owned so lifetimes and authority stay obvious.
//
// Required caller order:
//   1. validate the acquired archive against `projection` (size + SHA-256);
//   2. call `archive_plan_for` to obtain an `ArchivePlan`;
//   3. extract with `eggup_archive::extract`;
//   4. capture `extracted.root()` before persisting;
//   5. call `core_plan_for_archive` to obtain the core `InstallPlan` while
//      advisory paths are still meaningful;
//   6. call `extracted.persist().into_bound_sources()?.into_members()` to
//      obtain the bound members and deferred cleanup;
//   7. call `bind_archive_members` to obtain a `BoundSources` keyed by member
//      identity from the projection;
//   8. call `InstallPlan::prepare_with_bound_sources`.
// ---------------------------------------------------------------------------

/// Map a caller-selected archive artifact file name to a supported format.
///
/// Only `.tar.gz`, `.tgz`, and `.zip` suffixes (ASCII case-insensitive) are
/// accepted; anything else fails closed. The caller owns artifact selection:
/// this helper never fetches, lists, or ranks releases, it only classifies
/// the name the caller already chose.
pub fn archive_format_for_name(file_name: &str) -> Result<ArchiveFormat, AdapterError> {
    if file_name.is_empty() || !file_name.is_ascii() {
        return Err(AdapterError::UnsupportedArchiveFormat);
    }
    let lower = file_name.to_ascii_lowercase();
    if lower.ends_with(".tar.gz") || lower.ends_with(".tgz") {
        Ok(ArchiveFormat::TarGz)
    } else if lower.ends_with(".zip") {
        Ok(ArchiveFormat::Zip)
    } else {
        Err(AdapterError::UnsupportedArchiveFormat)
    }
}

/// Validate caller-owned acquired archive continuity against the manifest.
///
/// Checks that `acquired_archive_path` is absolute, names a regular file
/// (never a symlink, directory, or missing path), is exactly the manifest
/// archive byte count, and hashes to the manifest archive SHA-256. This is
/// the same continuity gate `materialize_artifact_set` applies to
/// direct/bundle files, moved ahead of extraction so no unverified byte is
/// ever extracted. Performs bounded streaming reads only; no network, no
/// alternate-source retry, no provenance claim.
pub fn validate_acquired_archive(
    projection: &ManifestProjection,
    acquired_archive_path: &Path,
) -> Result<(), AdapterError> {
    use sha2::{Digest, Sha256};
    use std::io::Read;
    let (artifact_name, exact_size, sha256) = match projection {
        ManifestProjection::Archive {
            artifact_name,
            exact_size,
            sha256,
            ..
        } => (artifact_name, *exact_size, *sha256),
        _ => return Err(AdapterError::ArchiveExtractionRequired),
    };
    if !acquired_archive_path.is_absolute() {
        return Err(AdapterError::InvalidAcquiredFile(artifact_name.clone()));
    }
    let meta = fs::symlink_metadata(acquired_archive_path)
        .map_err(|_| AdapterError::InvalidAcquiredFile(artifact_name.clone()))?;
    if !meta.file_type().is_file() || meta.len() != exact_size {
        return Err(AdapterError::InvalidAcquiredFile(artifact_name.clone()));
    }
    let mut file = fs::File::open(acquired_archive_path)
        .map_err(|_| AdapterError::InvalidAcquiredFile(artifact_name.clone()))?;
    let mut hasher = Sha256::new();
    let mut chunk = [0u8; 16 * 1024];
    loop {
        let read = file
            .read(&mut chunk)
            .map_err(|_| AdapterError::InvalidAcquiredFile(artifact_name.clone()))?;
        if read == 0 {
            break;
        }
        hasher.update(&chunk[..read]);
    }
    let digest: [u8; 32] = hasher.finalize().into();
    if digest != sha256 {
        return Err(AdapterError::InvalidAcquiredFile(artifact_name.clone()));
    }
    Ok(())
}

/// Translate `ManifestProjection::Archive` into a validated `ArchivePlan`.
///
/// Each declared member maps `source -> eggup_archive::ArchiveMember::source_path`
/// and `install -> eggup_archive::ArchiveMember::output_name`. Caller-supplied
/// `expected_size` and `expected_sha256` come from the projection so the
/// extractor can verify bytes against the manifest during extraction.
///
/// The caller must already have validated the acquired archive against the
/// manifest's exact size and SHA-256; this helper does not re-validate the
/// archive bytes and does not perform any I/O.
pub fn archive_plan_for(
    projection: &ManifestProjection,
    archive_format: ArchiveFormat,
    acquired_archive_path: &Path,
    limits: ArchiveLimits,
) -> Result<ArchivePlan, AdapterError> {
    let members = match projection {
        ManifestProjection::Archive { members, .. } => members,
        _ => return Err(AdapterError::ArchiveExtractionRequired),
    };
    let mut declared = Vec::with_capacity(members.len());
    for m in members {
        let member = ArchiveMember::new(
            m.source.clone(),
            m.install.clone(),
            Some(m.exact_size),
            Some(m.sha256),
        )
        .map_err(|e| AdapterError::Eggup(e.to_string()))?;
        declared.push(member);
    }
    ArchivePlan::new(acquired_archive_path, archive_format, declared, limits)
        .map_err(|e| AdapterError::Eggup(e.to_string()))
}

/// Build the core `InstallPlan` for an archive projection with caller-bound
/// destinations, recording each member's advisory path as
/// `extraction_root.join(manifest install)`.
///
/// The caller destination map must exactly match the projected member
/// identities: missing, extra, or colliding destinations fail closed before
/// any plan is returned, and invalid destinations fail through Eggup
/// destination validation into a bounded adapter error. Archive member
/// identity, advisory extraction source paths, and member size/digest facts
/// remain manifest-derived while destination strings come from caller policy.
/// `BoundSources` and the object-bound extraction handoff are unchanged: the
/// returned plan still carries empty bound sources until the caller transfers
/// them via `bind_archive_members`.
///
/// The caller is responsible for capturing `extraction_root` (typically
/// `ExtractedArchive::root()`) before any handle-bound handoff, and for
/// keeping the path reachable until staging no longer needs it.
/// Advisory paths are never opened during staging when a bound handle is
/// supplied; the path is recorded solely to satisfy
/// `ArtifactMember::new`'s validation and to expose diagnostics.
///
/// Replacement authorization stays outside this helper: it records where
/// extracted bytes should be installed and never proves control of a live
/// destination.
pub fn core_plan_for_archive_with_destinations(
    projection: &ManifestProjection,
    extraction_root: &Path,
    destination_root: &Path,
    destinations: HashMap<MemberId, String>,
    permissions: HashMap<MemberId, PermissionsIntent>,
) -> Result<InstallPlan, AdapterError> {
    let (product, release, members) = match projection {
        ManifestProjection::Archive {
            product,
            release,
            members,
            ..
        } => (product, release, members),
        _ => return Err(AdapterError::ArchiveExtractionRequired),
    };
    if destinations.len() != members.len() {
        return Err(AdapterError::MapMismatch("destinations"));
    }
    if permissions.len() != members.len() {
        return Err(AdapterError::MapMismatch("permissions"));
    }
    let mut artifacts = Vec::with_capacity(members.len());
    for m in members {
        let id =
            MemberId::new(m.install.clone()).map_err(|e| AdapterError::Eggup(e.to_string()))?;
        let destination = destinations
            .get(&id)
            .ok_or(AdapterError::MapMismatch("destinations"))?;
        let advisory = extraction_root.join(&m.install);
        let permissions_intent = permissions
            .get(&id)
            .copied()
            .ok_or(AdapterError::MapMismatch("permissions"))?;
        let member = ArtifactMember::new(id.clone(), &advisory, destination)
            .map_err(|e| AdapterError::Eggup(e.to_string()))?
            .with_permissions(permissions_intent)
            .with_integrity(IntegrityRequirement::Sha256(m.sha256));
        artifacts.push(member);
    }
    let mut seen = HashSet::with_capacity(artifacts.len());
    for member in &artifacts {
        if !seen.insert(member.destination().to_path_buf()) {
            return Err(AdapterError::MapMismatch("destinations"));
        }
    }
    let set = ArtifactSet::new(artifacts).map_err(|e| AdapterError::Eggup(e.to_string()))?;
    InstallPlan::new(product.clone(), release.clone(), destination_root, set)
        .map_err(|e| AdapterError::Eggup(e.to_string()))
}

/// Build the core `InstallPlan` for an archive projection, recording each
/// member's advisory path as `extraction_root.join(member.install)`.
///
/// Compatibility wrapper: builds the manifest-default destination map and
/// delegates to [`core_plan_for_archive_with_destinations`]. There is one
/// archive plan implementation.
///
/// The caller is responsible for capturing `extraction_root` (typically
/// `ExtractedArchive::root()`) before any handle-bound handoff, and for
/// keeping the path reachable until staging no longer needs it.
/// Advisory paths are never opened during staging when a bound handle is
/// supplied; the path is recorded solely to satisfy
/// `ArtifactMember::new`'s validation and to expose diagnostics.
///
/// The returned plan's `BoundSources` are still empty at this point; the
/// caller transfers bound members separately via `bind_archive_members`.
pub fn core_plan_for_archive(
    projection: &ManifestProjection,
    extraction_root: &Path,
    destination_root: &Path,
    permissions: HashMap<MemberId, PermissionsIntent>,
) -> Result<InstallPlan, AdapterError> {
    let destinations = projection.default_destinations()?;
    core_plan_for_archive_with_destinations(
        projection,
        extraction_root,
        destination_root,
        destinations,
        permissions,
    )
}

/// Build a `BoundSources` map from the projection's archive member list and
/// the bound members returned by `BoundExtraction::into_members()`.
///
/// Identity is the projection's `install` string converted into a `MemberId`;
/// bound members are paired to the projection's archive members in declaration
/// order. Mismatched counts fail closed, and so does a member whose own
/// recorded identity (`source_path`/`output_name`) does not match the declared
/// member it would be bound to.
///
/// The identity cross-check is what stops bytes being labelled positionally
/// rather than by proven identity: a `BoundExtraction` produced from a
/// different `ArchivePlan`, or reused after a re-projection, pairs by count
/// alone and would otherwise commit foreign bytes under a declared identity.
/// That substitution is digest-invisible whenever two declared members happen
/// to share identical content, which is exactly when it is most dangerous.
pub fn bind_archive_members(
    projection: &ManifestProjection,
    bound_members: Vec<BoundMember>,
) -> Result<BoundSources, AdapterError> {
    let members = match projection {
        ManifestProjection::Archive { members, .. } => members,
        _ => return Err(AdapterError::ArchiveExtractionRequired),
    };
    if members.len() != bound_members.len() {
        return Err(AdapterError::MapMismatch("archive bound members"));
    }
    let mut sources = BoundSources::new();
    for (declared, bound) in members.iter().zip(bound_members) {
        // `archive_plan_for` builds each `ArchiveMember` with
        // `source_path = declared.source` and `output_name = declared.install`,
        // so these are the exact identities a correctly-projected extraction
        // carries.
        if bound.source_path() != declared.source || bound.output_name() != declared.install {
            return Err(AdapterError::MapMismatch("archive bound member identity"));
        }
        let id = MemberId::new(declared.install.clone())
            .map_err(|e| AdapterError::Eggup(e.to_string()))?;
        // `insert` returns the displaced handle; declaration order and the
        // validated projection make a duplicate `install` impossible here, and
        // it is dropped with the map on the error paths above.
        sources.insert(id, bound.into_open_object());
    }
    Ok(sources)
}

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

    fn direct_json() -> Vec<u8> {
        include_bytes!("../tests/fixtures/direct-manifest.json").to_vec()
    }

    #[test]
    fn bounded_json_projection_accepts_fixture_and_exact_limit() {
        let fixture = direct_json();
        assert!(fixture.len() < MAX_MANIFEST_BYTES);
        assert!(matches!(
            project_json(&fixture, "x86_64-unknown-linux-gnu").unwrap(),
            ManifestProjection::Installable { .. }
        ));

        let mut at_limit = fixture;
        at_limit.resize(MAX_MANIFEST_BYTES, b' ');
        assert!(project_json(&at_limit, "x86_64-unknown-linux-gnu").is_ok());
    }

    #[test]
    fn bounded_json_projection_rejects_invalid_documents_without_leaking_input() {
        let oversized = vec![b' '; MAX_MANIFEST_BYTES + 1];
        let invalid_utf8 = [b'{', 0xff, b'}'];
        let malformed = br#"{"private":"https://user:super-secret@example.invalid/?token=hidden""#;
        let unsupported: Vec<u8> = String::from_utf8(direct_json())
            .unwrap()
            .replace("\"schema_version\":1", "\"schema_version\":999")
            .into_bytes();

        for input in [
            &oversized[..],
            &invalid_utf8[..],
            &malformed[..],
            &unsupported[..],
        ] {
            let error = project_json(input, "x86_64-unknown-linux-gnu").unwrap_err();
            assert!(matches!(error, AdapterError::InvalidManifest));
            let diagnostic = format!("{error} {error:?}");
            assert!(!diagnostic.contains("private"));
            assert!(!diagnostic.contains("super-secret"));
            assert!(!diagnostic.contains("token=hidden"));
        }
    }

    #[test]
    fn json_projection_keeps_exact_target_selection() {
        let error = project_json(&direct_json(), "linux-x64").unwrap_err();
        assert!(matches!(error, AdapterError::TargetNotFound));
    }

    #[test]
    fn corrected_fixtures_project_and_archive_stays_separate() {
        let direct: ReleaseManifest =
            serde_json::from_str(include_str!("../tests/fixtures/direct-manifest.json")).unwrap();
        assert!(matches!(
            project(&direct, "x86_64-unknown-linux-gnu").unwrap(),
            ManifestProjection::Installable { .. }
        ));
        let archive: ReleaseManifest =
            serde_json::from_str(include_str!("../tests/fixtures/archive-manifest.json")).unwrap();
        let p = project(&archive, "x86_64-unknown-linux-gnu").unwrap();
        assert!(matches!(p, ManifestProjection::Archive { .. }));
        assert!(matches!(
            p.bind_requests(HashMap::new(), FetchLimits::default()),
            Err(AdapterError::MapMismatch(_))
        ));
        let b: ReleaseManifest =
            serde_json::from_str(include_str!("../tests/fixtures/bundle-manifest.json")).unwrap();
        assert!(
            matches!(project(&b, "x86_64-unknown-linux-gnu").unwrap(), ManifestProjection::Installable { artifacts, .. } if artifacts.len() == 3)
        );
    }
    #[test]
    fn alias_target_is_not_resolved() {
        let m: ReleaseManifest =
            serde_json::from_str(include_str!("../tests/fixtures/direct-manifest.json")).unwrap();
        assert!(matches!(
            project(&m, "linux-x64"),
            Err(AdapterError::TargetNotFound)
        ));
    }

    #[test]
    fn exact_request_binding_preserves_url_and_tightens_byte_limit() {
        let m: ReleaseManifest =
            serde_json::from_str(include_str!("../tests/fixtures/direct-manifest.json")).unwrap();
        let p = project(&m, "x86_64-unknown-linux-gnu").unwrap();
        let name = match &p {
            ManifestProjection::Installable { artifacts, .. } => artifacts[0].artifact_name.clone(),
            _ => unreachable!(),
        };
        let request =
            AcquisitionRequest::new("https://user:secret@example.test/path?token=kept").unwrap();
        let mut requests = HashMap::new();
        requests.insert(name.clone(), request.clone());
        let bound = p.bind_requests(requests, FetchLimits::default()).unwrap();
        assert_eq!(bound[0].request, request);
        assert_eq!(bound[0].limits.max_artifact_bytes, bound[0].exact_size);
        let too_small = FetchLimits {
            max_artifact_bytes: bound[0].exact_size - 1,
            ..FetchLimits::default()
        };
        assert!(matches!(
            p.bind_requests(HashMap::from([(name, request)]), too_small),
            Err(AdapterError::CallerLimitTooSmall(_))
        ));
    }

    #[test]
    fn exact_size_and_explicit_permissions_gate_artifact_materialization() {
        let m: ReleaseManifest =
            serde_json::from_str(include_str!("../tests/fixtures/direct-manifest.json")).unwrap();
        let p = project(&m, "x86_64-unknown-linux-gnu").unwrap();
        let requirement = match &p {
            ManifestProjection::Installable { artifacts, .. } => &artifacts[0],
            _ => unreachable!(),
        };
        let dir = std::env::temp_dir().join(format!("eggup-eggpack-test-{}", std::process::id()));
        let _ = fs::remove_dir_all(&dir);
        fs::create_dir_all(&dir).unwrap();
        let path = dir.join("source");
        fs::write(&path, b"abc").unwrap();
        let acquired = HashMap::from([(requirement.artifact_name.clone(), path.clone())]);
        let permissions =
            HashMap::from([(requirement.member_id.clone(), PermissionsIntent::Executable)]);
        assert!(p
            .materialize_artifact_set(acquired.clone(), permissions.clone())
            .is_ok());
        fs::write(&path, b"no").unwrap();
        assert!(matches!(
            p.materialize_artifact_set(acquired, permissions),
            Err(AdapterError::InvalidAcquiredFile(_))
        ));
        let _ = fs::remove_dir_all(dir);
    }
}