phora 0.6.0

A git-based artifact package manager and multiplexer for content-addressed file distribution
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
//! Transitive dependency graph: the dep-manifest DTO and the graph identity keys
//! (`FetchNode` for dedup, `Instance` for namespacing).

use std::collections::BTreeMap;
use std::path::{Component, Path, PathBuf};

use serde::Deserialize;

use crate::error::{Error, Result};
use crate::source::NormalizedUrl;

use super::{BuildInputs, BuildSpec, BuildTool, DEFAULT_OFFER, DeployMode, Source, Target};

/// A transitive dep's `phora.toml`, parsed EXACTLY ONCE into its declarative graph
/// fields. Trust-control fields (`trust`/`trusted_hooks`/`allow_hooks`) are tolerated
/// and dropped — never stored, so no trust state rides into admission. Hooks are
/// retained out-of-band as an uninterpreted [`toml::Value`]; a transitive global
/// `[hooks]` block is stripped (consumer-owned only).
#[derive(Debug, Clone)]
pub struct TransitiveManifest {
    pub sources: BTreeMap<String, Source>,
    pub targets: BTreeMap<String, Target>,
    pub offers: BTreeMap<String, ManifestOffer>,
    hooks: Option<toml::Value>,
    build_lines: BTreeMap<String, usize>,
}

#[derive(Debug, Deserialize)]
struct ManifestGraph {
    #[serde(default)]
    sources: BTreeMap<String, Source>,
    #[serde(default)]
    targets: BTreeMap<String, Target>,
    #[serde(default)]
    offers: BTreeMap<String, ManifestOffer>,
}

/// A named slice of a repo: own files under `root` plus the targets it selects by key.
#[derive(Debug, Clone, Default, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ManifestOffer {
    pub root: Option<PathBuf>,
    #[serde(default)]
    pub include: Vec<String>,
    #[serde(default)]
    pub exclude: Vec<String>,
    pub targets: Option<Vec<String>>,
}

/// Own files are `None` when a target covers the offer root; target paths are relative to it.
#[derive(Debug, Clone)]
pub struct ResolvedOffer {
    pub root: PathBuf,
    pub files: Option<Source>,
    pub targets: BTreeMap<String, Target>,
}

pub(crate) fn resolve_offer(
    name: &str,
    offers: &BTreeMap<String, ManifestOffer>,
    sources: &BTreeMap<String, Source>,
    targets: &BTreeMap<String, Target>,
) -> Result<ResolvedOffer> {
    Repo {
        offers,
        sources,
        targets,
    }
    .resolve(name, &mut Vec::new())
}

struct Repo<'a> {
    offers: &'a BTreeMap<String, ManifestOffer>,
    sources: &'a BTreeMap<String, Source>,
    targets: &'a BTreeMap<String, Target>,
}

impl Repo<'_> {
    fn resolve(&self, name: &str, resolving: &mut Vec<String>) -> Result<ResolvedOffer> {
        resolving.push(name.to_owned());
        let resolved = self.resolve_within(name, resolving);
        resolving.pop();
        resolved
    }

    fn resolve_within(&self, name: &str, resolving: &mut Vec<String>) -> Result<ResolvedOffer> {
        let implicit = ManifestOffer::default();
        let offer = match self.offers.get(name) {
            Some(offer) => offer,
            None if name == DEFAULT_OFFER => &implicit,
            None => {
                let declared: Vec<&str> = self.offers.keys().map(String::as_str).collect();
                return Err(Error::Config(format!(
                    "no offer `{name}`; declared offers: [{}]",
                    declared.join(", ")
                )));
            }
        };
        let fail = |detail: String| Error::Config(format!("offer `{name}`: {detail}"));
        let root = match offer.root.as_deref() {
            Some(root) if escapes(root) => {
                return Err(fail(format!(
                    "root `{}` must be a relative subpath of the repo",
                    root.display()
                )));
            }
            Some(root) => normalized(root),
            None => PathBuf::new(),
        };
        let selected: Vec<(&String, &Target)> = match &offer.targets {
            None => self
                .targets
                .iter()
                .filter(|(_, target)| {
                    placed(&target.path, &root).is_some()
                        && self.offerable(target, resolving).is_ok()
                })
                .collect(),
            Some(keys) => keys
                .iter()
                .map(|key| {
                    let (key, target) = self.targets.get_key_value(key).ok_or_else(|| {
                        let declared: Vec<&str> = self.targets.keys().map(String::as_str).collect();
                        fail(format!(
                            "selects undeclared target `{key}`; declared targets: [{}]",
                            declared.join(", ")
                        ))
                    })?;
                    self.offerable(target, resolving)
                        .map_err(|why| fail(format!("target `{key}` cannot be offered: {why}")))?;
                    if placed(&target.path, &root).is_none() {
                        return Err(fail(format!(
                            "target `{key}` at `{}` lies outside the offer root `{}`",
                            target.path.display(),
                            root.display()
                        )));
                    }
                    Ok((key, target))
                })
                .collect::<Result<_>>()?,
        };
        let placed_targets = selected
            .into_iter()
            .map(|(key, target)| {
                let mut target = target.clone();
                target.path = placed(&target.path, &root).unwrap_or_default();
                (key.clone(), target)
            })
            .collect();
        Ok(ResolvedOffer {
            files: own_files(offer, &root, self.targets)?,
            targets: placed_targets,
            root,
        })
    }

    fn offerable(
        &self,
        target: &Target,
        resolving: &mut Vec<String>,
    ) -> std::result::Result<(), String> {
        if escapes(&target.path) {
            return Err(format!(
                "its path `{}` is not a relative subpath of the repo",
                target.path.display()
            ));
        }
        for (identity, binding) in target.bindings() {
            let name = binding.effective_source(identity);
            let Some(source) = self.sources.get(name) else {
                return Err(format!("it binds undefined source `{name}`"));
            };
            if let Some(build) = &source.build {
                self.offerable_build(name, build, resolving)?;
                continue;
            }
            let kind = if source.path.is_some() {
                "a local path"
            } else if source.deploy == Some(DeployMode::Link) {
                "a linked"
            } else {
                continue;
            };
            return Err(format!("it binds `{name}`, {kind} source"));
        }
        Ok(())
    }

    fn offerable_build(
        &self,
        name: &str,
        build: &BuildSpec,
        resolving: &mut Vec<String>,
    ) -> std::result::Result<(), String> {
        let (BuildTool::Tool { .. }, BuildInputs::Repo) = (&build.tool, &build.inputs) else {
            return Err(format!(
                "it binds `{name}`, a build that runs its own command or reads named \
                 sources; an offered build names a `tool` and reads this repo's offer"
            ));
        };
        let offer = build.offer.as_deref().unwrap_or(DEFAULT_OFFER);
        if resolving.iter().any(|o| o == offer) {
            return Err(format!(
                "it binds `{name}`, a build that reads its own output through offer `{offer}`"
            ));
        }
        self.resolve(offer, resolving)
            .map(drop)
            .map_err(|e| format!("it binds `{name}`, whose input cannot resolve: {e}"))
    }
}

fn own_files(
    offer: &ManifestOffer,
    root: &Path,
    targets: &BTreeMap<String, Target>,
) -> Result<Option<Source>> {
    let mut exclude = offer.exclude.clone();
    if offer.include.is_empty() {
        exclude.extend(["/phora.toml".to_owned(), "/phora.lock".to_owned()]);
    }
    for target in targets.values().filter(|target| !escapes(&target.path)) {
        let path = normalized(&target.path);
        if path.as_os_str().is_empty() {
            continue;
        }
        match placed(&path, root) {
            Some(rest) if rest.as_os_str().is_empty() => return Ok(None),
            Some(rest) => exclude.push(format!("/{}/", rest.display())),
            None if root.starts_with(&path) => return Ok(None),
            None => {}
        }
    }
    let mut source = toml::Table::new();
    source.insert("path".into(), ".".into());
    if !root.as_os_str().is_empty() {
        source.insert("root".into(), root.to_string_lossy().into_owned().into());
    }
    if !offer.include.is_empty() {
        source.insert("include".into(), offer.include.clone().into());
    }
    if !exclude.is_empty() {
        source.insert("exclude".into(), exclude.into());
    }
    toml::Value::Table(source)
        .try_into()
        .map(Some)
        .map_err(|e: toml::de::Error| Error::Config(format!("own files: {e}")))
}

fn placed(path: &Path, root: &Path) -> Option<PathBuf> {
    normalized(path)
        .strip_prefix(root)
        .ok()
        .map(Path::to_path_buf)
}

pub const MEMBER_SEPARATOR: char = '%';

/// What a namespaced key names within one [`Instance`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Member {
    Files,
    /// The whole repo at the instance's snapshot, as a composed build reads it.
    Repo,
    Named(String),
}

impl Member {
    /// `None` for a key no [`Instance`] minted.
    #[must_use]
    pub fn of(key: &str) -> Option<Self> {
        let (_, member) = key.split_once(MEMBER_SEPARATOR)?;
        Some(match member {
            "" => Self::Files,
            "%" => Self::Repo,
            named => Self::Named(named.to_owned()),
        })
    }

    #[must_use]
    pub fn is_namespaced(key: &str) -> bool {
        key.contains(MEMBER_SEPARATOR)
    }
}

impl TransitiveManifest {
    /// Parses a transitive `phora.toml`.
    ///
    /// # Errors
    ///
    /// Returns [`Error::Config`] when the document is not valid TOML or its
    /// declarative `[sources]`/`[targets]`/`[offers]` fields do not type.
    pub fn parse(text: &str) -> Result<Self> {
        Self::parse_toml(text).map_err(|error| Error::Config(error.to_string()))
    }

    /// Crate-private structured-error parsing path for callers that need the concrete
    /// [`toml::de::Error`].
    ///
    /// # Errors
    ///
    /// Returns an error when the document has invalid TOML syntax or its declarative
    /// fields cannot be deserialized.
    pub(crate) fn parse_toml(text: &str) -> std::result::Result<Self, toml::de::Error> {
        let document: toml::Value = toml::from_str(text)?;
        let hooks = collect_opaque_hooks(&document);
        let mut graph: ManifestGraph = document.try_into()?;
        if let Some(key) = unnamespaceable_key(graph.sources.keys().chain(graph.targets.keys())) {
            return Err(<toml::de::Error as serde::de::Error>::custom(format!(
                "key `{key}` cannot contain `{MEMBER_SEPARATOR}`"
            )));
        }
        let sources = &graph.sources;
        for target in graph.targets.values_mut() {
            target.lower_transitive(|name| sources.get(name).is_some_and(Source::is_transitive));
        }
        let build_lines = graph
            .sources
            .iter()
            .filter(|(_, source)| source.build.is_some())
            .filter_map(|(name, _)| Some((name.clone(), super::link::build_line(text, name)?)))
            .collect();
        Ok(Self {
            sources: graph.sources,
            targets: graph.targets,
            offers: graph.offers,
            hooks,
            build_lines,
        })
    }

    pub(crate) fn offer(&self, name: &str) -> Result<ResolvedOffer> {
        resolve_offer(name, &self.offers, &self.sources, &self.targets)
    }

    /// The declared offers plus the implicit `default`.
    pub fn offer_names(&self) -> impl Iterator<Item = &str> {
        (!self.offers.contains_key(DEFAULT_OFFER))
            .then_some(DEFAULT_OFFER)
            .into_iter()
            .chain(self.offers.keys().map(String::as_str))
    }

    #[must_use]
    pub fn build_line(&self, source: &str) -> Option<usize> {
        self.build_lines.get(source).copied()
    }

    /// The retained per-target hooks as an uninterpreted payload; never the global `[hooks]`.
    #[must_use]
    pub fn hooks(&self) -> Option<&toml::Value> {
        self.hooks.as_ref()
    }
}

pub(crate) fn unnamespaceable_key<'k>(
    mut keys: impl Iterator<Item = &'k String>,
) -> Option<&'k String> {
    keys.find(|key| key.contains(MEMBER_SEPARATOR))
}

fn escapes(path: &Path) -> bool {
    path.is_absolute()
        || path.starts_with("~")
        || path.components().any(|c| matches!(c, Component::ParentDir))
}

fn normalized(path: &Path) -> PathBuf {
    path.components()
        .filter(|c| !matches!(c, Component::CurDir))
        .collect()
}

/// Per-target `hooks` keyed by target; the top-level `[hooks]` is consumer-owned.
fn collect_opaque_hooks(document: &toml::Value) -> Option<toml::Value> {
    let retained: toml::value::Table = document
        .get("targets")?
        .as_table()?
        .iter()
        .filter_map(|(name, target)| Some((name.clone(), target.get("hooks")?.clone())))
        .collect();
    (!retained.is_empty()).then_some(toml::Value::Table(retained))
}

/// Graph dedup key: a fetched node is `(normalized-url, ref, commit)`. A diamond
/// reaching the same triple collapses to one fetch; equivalent URL forms normalize
/// to the same node; a differing commit is a different node.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct FetchNode {
    url: String,
    r#ref: String,
    commit: String,
}

impl FetchNode {
    #[must_use]
    pub fn new(url: &str, r#ref: &str, commit: &str) -> Self {
        Self {
            url: NormalizedUrl::parse(url).as_str().to_owned(),
            r#ref: r#ref.to_owned(),
            commit: commit.to_owned(),
        }
    }

    #[must_use]
    pub fn commit(&self) -> &str {
        &self.commit
    }
}

/// Namespacing key for one binding composing a fetched node under one anchor. It omits
/// the snapshot, so ownership survives commit and pin-to-link switches, and offer switches
/// that keep the offer root.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Instance {
    parent: String,
    identity: String,
    anchor_target: String,
    fetch_node: FetchNode,
    root: PathBuf,
}

/// Hash input kept byte-identical so existing ownership keys stay valid.
const STABLE_KEY_SALT: &str = "package";

impl Instance {
    #[must_use]
    pub fn new(parent: &str, identity: &str, anchor_target: &str, fetch_node: FetchNode) -> Self {
        Self {
            parent: parent.to_owned(),
            identity: identity.to_owned(),
            anchor_target: anchor_target.to_owned(),
            fetch_node,
            root: PathBuf::new(),
        }
    }

    #[must_use]
    pub fn placed_at(mut self, root: &Path) -> Self {
        self.root = root.to_path_buf();
        self
    }

    pub(crate) fn key(&self, member: &Member) -> String {
        match member {
            Member::Files => format!("{}{MEMBER_SEPARATOR}", self.stable_key()),
            Member::Repo => format!("{0}{MEMBER_SEPARATOR}{MEMBER_SEPARATOR}", self.stable_key()),
            Member::Named(name) => format!("{}{MEMBER_SEPARATOR}{name}", self.stable_key()),
        }
    }

    #[must_use]
    pub fn fetch_node(&self) -> &FetchNode {
        &self.fetch_node
    }

    /// Length-prefixed field hash; stable across field reorders, unlike a `Debug` rendering.
    #[must_use]
    pub fn stable_key(&self) -> String {
        let mut hasher = blake3::Hasher::new();
        let root = self.root.to_string_lossy();
        let placement = (!root.is_empty()).then_some(root.as_ref());
        for field in [
            self.parent.as_str(),
            self.identity.as_str(),
            self.anchor_target.as_str(),
            STABLE_KEY_SALT,
        ]
        .into_iter()
        .chain(placement)
        {
            hasher.update(&(field.len() as u64).to_le_bytes());
            hasher.update(field.as_bytes());
        }
        hasher.finalize().to_hex()[..16].to_owned()
    }
}