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
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
//! Post-commit hook dispatch.

use std::collections::{BTreeMap, BTreeSet};
use std::process::Command;

use crate::config::{Config, DEFAULT_SHELL_PREFIX, HookCommand, HookWhen, LayoutConfig};
use crate::error::{Error, Result};
use crate::sync::request::{TrustPrompt, TrustRequest};
use crate::sync::state::{ArtifactRecord, StateStore};

/// Which hook table a [`HookOutcome`] came from.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HookScope {
    PreSync,
    PreDeploy,
    OnChange,
    PostSync,
}

/// Whether a dispatched hook exited zero.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HookStatus {
    Success,
    Failure,
}

/// One dispatched hook's identity and outcome, for per-hook reporting (TPH-004).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct HookOutcome {
    pub hook_id: String,
    pub command: String,
    pub scope: HookScope,
    pub status: HookStatus,
}

/// Runs every target's `on_change` hook whose deployed digest-set gained members
/// since its last success, then every global `post_sync` hook. Files are never
/// rolled back; a non-zero exit surfaces as a [`HookStatus::Failure`] outcome.
///
/// # Errors
///
/// Returns an error if the registry cannot be read, hook state cannot be
/// recorded, or a hook process fails to spawn.
pub(super) fn dispatch_hooks(
    config: &Config,
    registry: &dyn StateStore,
    lockless: bool,
) -> Result<Vec<HookOutcome>> {
    let mut outcomes = Vec::new();

    for (target_name, target) in &config.targets {
        let Some(hooks) = &target.hooks else { continue };
        let Some(on_change) = &hooks.on_change else {
            continue;
        };

        let records = registry.target_artifacts(target_name)?;
        let current: BTreeSet<String> = records.iter().map(|r| r.digest.clone()).collect();
        let layout = target.layout();
        let target_path = target.expanded_path();

        for hook in dedupe(on_change) {
            let (body, suffix) = hook_key(hook);
            let id = format!("{target_name}#{body}#{suffix}");
            let recorded = recorded_set(registry, target_name, &id)?;
            let changed = changed_records(&records, &recorded);
            if changed.is_empty() {
                continue;
            }
            if lockless {
                let journal_root = registry.journal_root();
                let state_root = journal_root.parent().unwrap_or(&journal_root);
                return Err(crate::sync::state::readonly_root_error(state_root).into());
            }

            let names = changed_names(&changed);
            let paths = changed_paths(&changed, &target_path, &layout);
            let status = run_hook(
                hook,
                &[
                    ("PHORA_TARGET", target_name.as_str()),
                    ("PHORA_CHANGED", &paths),
                    ("PHORA_CHANGED_NAMES", &names),
                ],
            )?;
            if status == HookStatus::Success {
                registry.record_hook_success(target_name, &id, &current)?;
            }
            outcomes.push(HookOutcome {
                hook_id: id,
                command: hook.display(),
                scope: HookScope::OnChange,
                status,
            });
        }
    }

    if let Some(global) = &config.hooks
        && let Some(post_sync) = &global.post_sync
        && matches!(global.when, HookWhen::Always)
    {
        for hook in dedupe(post_sync) {
            let status = run_hook(hook, &[])?;
            let (body, suffix) = hook_key(hook);
            outcomes.push(HookOutcome {
                hook_id: format!("post_sync#{body}#{suffix}"),
                command: hook.display(),
                scope: HookScope::PostSync,
                status,
            });
        }
    }

    Ok(outcomes)
}

/// Runs every global `pre_sync` hook with `PHORA_TARGETS`. Unconditional: `when` governs
/// `post_sync` re-fire, never the per-run `pre_sync` gate.
///
/// # Errors
///
/// Returns an error if a hook process fails to spawn.
pub(super) fn dispatch_pre_sync(config: &Config, target_names: &str) -> Result<Vec<HookOutcome>> {
    let Some(pre_sync) = config.hooks.as_ref().and_then(|g| g.pre_sync.as_ref()) else {
        return Ok(Vec::new());
    };
    let mut outcomes = Vec::new();
    for hook in dedupe(pre_sync) {
        let status = run_hook(hook, &[("PHORA_TARGETS", target_names)])?;
        let (body, suffix) = hook_key(hook);
        outcomes.push(HookOutcome {
            hook_id: format!("pre_sync#{body}#{suffix}"),
            command: hook.display(),
            scope: HookScope::PreSync,
            status,
        });
    }
    Ok(outcomes)
}

/// Runs every `pre_deploy` hook for one target with `PHORA_TARGET`/`PHORA_TARGET_PATH`.
/// Ids are per-target so two targets sharing a command get distinct outcomes.
///
/// # Errors
///
/// Returns an error if a hook process fails to spawn.
pub(super) fn dispatch_pre_deploy(
    hooks: &crate::config::TargetHooks,
    target_name: &str,
    target_path: &std::path::Path,
) -> Result<Vec<HookOutcome>> {
    let Some(pre_deploy) = hooks.pre_deploy.as_ref() else {
        return Ok(Vec::new());
    };
    let path = target_path.to_string_lossy();
    let mut outcomes = Vec::new();
    for hook in dedupe(pre_deploy) {
        let status = run_hook(
            hook,
            &[("PHORA_TARGET", target_name), ("PHORA_TARGET_PATH", &path)],
        )?;
        let (body, suffix) = hook_key(hook);
        outcomes.push(HookOutcome {
            hook_id: format!("{target_name}#pre_deploy#{body}#{suffix}"),
            command: hook.display(),
            scope: HookScope::PreDeploy,
            status,
        });
    }
    Ok(outcomes)
}

/// A commit-pinned transitive `on_change` hook candidate plus the trust decision context.
pub(super) struct TransitiveHookRun<'a> {
    pub(super) dep_instance: &'a str,
    pub(super) hook_id: &'a str,
    pub(super) command: &'a HookCommand,
    pub(super) preimage: &'a str,
    pub(super) target_path: &'a std::path::Path,
    pub(super) source: &'a str,
    pub(super) commit: &'a str,
}

/// A new trust approval the producer must persist to the consumer lock's `trusted_hooks`.
pub(super) struct TransitiveApproval {
    pub(super) dep_instance: String,
    pub(super) hook_id: String,
    pub(super) preimage: String,
    pub(super) source: String,
    pub(super) commit: String,
}

/// Runs each commit-pinned transitive hook whose preimage a consumer `trusted_hooks` entry
/// already pins; an unpinned hook consults `prompt` (approval persists) and is skipped on a
/// decline. A dep can never self-approve: trust is keyed on the consumer lock.
pub(super) fn dispatch_transitive_hooks(
    candidates: &[TransitiveHookRun<'_>],
    trusted: &BTreeSet<String>,
    prompt: &dyn TrustPrompt,
) -> Result<(Vec<HookOutcome>, Vec<TransitiveApproval>)> {
    let mut outcomes = Vec::new();
    let mut approvals = Vec::new();
    // Identical preimages share one decision per run: prompt once, then reuse the answer so
    // duplicate hooks neither re-prompt nor re-run a decline.
    let mut decided: BTreeMap<&str, bool> = BTreeMap::new();
    for candidate in candidates {
        let approved = if trusted.contains(candidate.preimage) {
            true
        } else if let Some(&prior) = decided.get(candidate.preimage) {
            prior
        } else {
            let answer = prompt.confirm(&TrustRequest {
                dep_instance: candidate.dep_instance,
                hook_id: candidate.hook_id,
                command: candidate.command.display(),
            });
            decided.insert(candidate.preimage, answer);
            answer
        };
        if !approved {
            continue;
        }
        if !trusted.contains(candidate.preimage) {
            approvals.push(TransitiveApproval {
                dep_instance: candidate.dep_instance.to_owned(),
                hook_id: candidate.hook_id.to_owned(),
                preimage: candidate.preimage.to_owned(),
                source: candidate.source.to_owned(),
                commit: candidate.commit.to_owned(),
            });
        }
        let status = run_hook(
            candidate.command,
            &[("PHORA_TARGET", &candidate.target_path.to_string_lossy())],
        )?;
        outcomes.push(HookOutcome {
            hook_id: candidate.hook_id.to_owned(),
            command: candidate.command.display(),
            scope: HookScope::OnChange,
            status,
        });
    }
    Ok((outcomes, approvals))
}

fn hook_key(hook: &HookCommand) -> (String, String) {
    match hook {
        HookCommand::Shell { run, shell } => (
            run.clone(),
            shell.as_deref().unwrap_or(DEFAULT_SHELL_PREFIX).to_owned(),
        ),
        HookCommand::Exec { cmd } => (cmd.join(" "), "exec".to_owned()),
    }
}

#[derive(PartialEq, Eq, PartialOrd, Ord)]
enum DedupeKey<'a> {
    Shell(&'a str, &'a str),
    Exec(&'a [String]),
}

fn dedupe(commands: &[HookCommand]) -> Vec<&HookCommand> {
    let mut seen = BTreeSet::new();
    commands
        .iter()
        .filter(|cmd| {
            let key = match cmd {
                HookCommand::Shell { run, shell } => {
                    DedupeKey::Shell(run, shell.as_deref().unwrap_or(DEFAULT_SHELL_PREFIX))
                }
                HookCommand::Exec { cmd } => DedupeKey::Exec(cmd),
            };
            seen.insert(key)
        })
        .collect()
}

fn recorded_set(registry: &dyn StateStore, target: &str, id: &str) -> Result<BTreeSet<String>> {
    Ok(registry
        .hook_state(target)?
        .into_iter()
        .find(|h| h.hook_id == id)
        .map(|h| h.last_success)
        .unwrap_or_default())
}

/// Records carrying a digest absent from the hook's last-success set: the
/// directional changed set (additions/modifications). Pure removals yield none.
fn changed_records<'a>(
    records: &'a [ArtifactRecord],
    recorded: &BTreeSet<String>,
) -> Vec<&'a ArtifactRecord> {
    records
        .iter()
        .filter(|record| !recorded.contains(&record.digest))
        .collect()
}

fn changed_names(changed: &[&ArtifactRecord]) -> String {
    let names: BTreeSet<&str> = changed.iter().map(|r| r.key.artifact.as_str()).collect();
    names.into_iter().collect::<Vec<_>>().join("\n")
}

fn changed_paths(
    changed: &[&ArtifactRecord],
    target_path: &std::path::Path,
    layout: &LayoutConfig,
) -> String {
    let paths: BTreeSet<String> = changed
        .iter()
        .map(|r| {
            target_path
                .join(super::target::record_relative_destination(layout, r))
                .to_string_lossy()
                .into_owned()
        })
        .collect();
    paths.into_iter().collect::<Vec<_>>().join("\n")
}

/// `Success` on a zero exit; a spawn failure is a hard error, a non-zero exit is not.
///
/// # Errors
///
/// Returns an error if the hook shell is empty or the process fails to spawn.
fn run_hook(hook: &HookCommand, env: &[(&str, &str)]) -> Result<HookStatus> {
    let mut command = command(hook)?;
    for (key, value) in env {
        command.env(key, value);
    }

    let status = command
        .status()
        .map_err(|e| Error::Sync(format!("failed to run hook `{}`: {e}", hook.display())))?;
    Ok(if status.success() {
        HookStatus::Success
    } else {
        HookStatus::Failure
    })
}

/// The process for a configured command: a shell-wrapped `run` or a direct `cmd` argv.
///
/// # Errors
///
/// Returns an error if the shell or the argv is empty.
pub(super) fn command(hook: &HookCommand) -> Result<Command> {
    Ok(match hook {
        HookCommand::Shell { run, shell } => {
            let shell = shell.as_deref().unwrap_or(DEFAULT_SHELL_PREFIX);
            let mut parts = shell.split_whitespace();
            let program = parts
                .next()
                .ok_or_else(|| Error::Sync(format!("hook shell `{shell}` is empty")))?;
            let mut command = Command::new(program);
            command.args(parts).arg(run);
            command
        }
        HookCommand::Exec { cmd } => {
            let (program, args) = cmd
                .split_first()
                .ok_or_else(|| Error::Sync("hook command `cmd` must not be empty".to_owned()))?;
            let mut command = Command::new(program);
            command.args(args);
            command
        }
    })
}

#[cfg(test)]
mod transitive_trust_tests {
    use super::*;
    use crate::config::HookCommand;
    use std::path::Path;

    struct CannedPrompt(bool);

    impl TrustPrompt for CannedPrompt {
        fn confirm(&self, _request: &TrustRequest<'_>) -> bool {
            self.0
        }
    }

    struct NeverPrompt;
    impl TrustPrompt for NeverPrompt {
        fn confirm(&self, _request: &TrustRequest<'_>) -> bool {
            panic!("a hook already pinned in trusted_hooks must run without prompting");
        }
    }

    struct CountingPrompt(std::sync::atomic::AtomicUsize, bool);
    impl TrustPrompt for CountingPrompt {
        fn confirm(&self, _request: &TrustRequest<'_>) -> bool {
            self.0.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
            self.1
        }
    }

    fn noop_hook() -> HookCommand {
        HookCommand::Shell {
            run: "true".to_owned(),
            shell: Some("sh -c".to_owned()),
        }
    }

    fn run<'a>(command: &'a HookCommand, target: &'a Path) -> TransitiveHookRun<'a> {
        TransitiveHookRun {
            dep_instance: "owninginstance01",
            hook_id: "composed#on_change#deadbeef",
            command,
            preimage: "blake3:candidatepreimage",
            target_path: target,
            source: "nvim-kit",
            commit: "c0ffeecommit",
        }
    }

    #[test]
    fn answering_yes_yields_an_approval_matching_the_candidate_preimage_and_id() {
        let cmd = noop_hook();
        let target = Path::new("/tmp/phora-test-target");
        let candidate = run(&cmd, target);
        let trusted = BTreeSet::new();

        let (_outcomes, approvals) =
            dispatch_transitive_hooks(&[candidate], &trusted, &CannedPrompt(true))
                .expect("dispatch with a yes-answering prompt must succeed");

        assert_eq!(
            approvals.len(),
            1,
            "an untrusted hook approved with `y` must produce exactly one persistable approval"
        );
        let approval = &approvals[0];
        assert_eq!(
            approval.preimage, "blake3:candidatepreimage",
            "the persisted approval must carry the candidate's commit-bound preimage verbatim, so \
             trust is pinned to the resolved commit"
        );
        assert_eq!(
            approval.dep_instance, "owninginstance01",
            "the approval must address the candidate's owning dep instance"
        );
        assert_eq!(
            approval.hook_id, "composed#on_change#deadbeef",
            "the approval must address the candidate's hook id"
        );
        assert_eq!(
            (approval.source.as_str(), approval.commit.as_str()),
            ("nvim-kit", "c0ffeecommit"),
            "the approval must carry the candidate's source and commit so the persisted \
             trusted_hooks entry can be diffed by `phora trust`, never silently emptied"
        );
    }

    #[test]
    fn answering_no_yields_no_approval_and_does_not_run_the_hook() {
        let cmd = noop_hook();
        let target = Path::new("/tmp/phora-test-target");
        let candidate = run(&cmd, target);
        let trusted = BTreeSet::new();

        let (outcomes, approvals) =
            dispatch_transitive_hooks(&[candidate], &trusted, &CannedPrompt(false))
                .expect("dispatch with a no-answering prompt must succeed");

        assert!(
            approvals.is_empty(),
            "anti-TOFU: declining the prompt must write NO trusted_hooks approval"
        );
        assert!(
            outcomes.is_empty(),
            "a declined untrusted hook must NOT run, so it produces no outcome"
        );
    }

    fn run_keyed<'a>(
        command: &'a HookCommand,
        target: &'a Path,
        dep_instance: &'a str,
        hook_id: &'a str,
        preimage: &'a str,
    ) -> TransitiveHookRun<'a> {
        TransitiveHookRun {
            dep_instance,
            hook_id,
            command,
            preimage,
            target_path: target,
            source: "nvim-kit",
            commit: "c0ffeecommit",
        }
    }

    #[test]
    fn duplicate_preimages_prompt_once_but_each_records_its_own_approval() {
        let cmd = noop_hook();
        let target = Path::new("/tmp/phora-test-target");
        let a = run_keyed(
            &cmd,
            target,
            "instanceaaaa0001",
            "a#on_change#dead",
            "blake3:shared",
        );
        let b = run_keyed(
            &cmd,
            target,
            "instancebbbb0002",
            "b#on_change#dead",
            "blake3:shared",
        );
        let trusted = BTreeSet::new();
        let prompt = CountingPrompt(std::sync::atomic::AtomicUsize::new(0), true);

        let (outcomes, approvals) = dispatch_transitive_hooks(&[a, b], &trusted, &prompt)
            .expect("dispatch with two same-preimage candidates must succeed");

        assert_eq!(
            prompt.0.load(std::sync::atomic::Ordering::SeqCst),
            1,
            "two candidates sharing a preimage must prompt exactly once"
        );
        assert_eq!(
            approvals.len(),
            2,
            "each distinct (dep_instance, hook_id) must still get its own approval record"
        );
        assert_eq!(
            outcomes.len(),
            2,
            "both shared-preimage hooks must run after the single yes"
        );
    }

    #[test]
    fn declining_a_shared_preimage_skips_all_duplicates_without_re_prompting() {
        let cmd = noop_hook();
        let target = Path::new("/tmp/phora-test-target");
        let a = run_keyed(
            &cmd,
            target,
            "instanceaaaa0001",
            "a#on_change#dead",
            "blake3:shared",
        );
        let b = run_keyed(
            &cmd,
            target,
            "instancebbbb0002",
            "b#on_change#dead",
            "blake3:shared",
        );
        let trusted = BTreeSet::new();
        let prompt = CountingPrompt(std::sync::atomic::AtomicUsize::new(0), false);

        let (outcomes, approvals) = dispatch_transitive_hooks(&[a, b], &trusted, &prompt)
            .expect("dispatch declining a shared preimage must succeed");

        assert_eq!(
            prompt.0.load(std::sync::atomic::Ordering::SeqCst),
            1,
            "a declined preimage must not re-prompt for duplicates"
        );
        assert!(
            approvals.is_empty(),
            "a declined preimage records no approval"
        );
        assert!(outcomes.is_empty(), "a declined preimage runs nothing");
    }

    #[test]
    fn an_already_trusted_hook_runs_without_prompting_or_re_approving() {
        let cmd = noop_hook();
        let target = Path::new("/tmp/phora-test-target");
        let candidate = run(&cmd, target);
        let mut trusted = BTreeSet::new();
        trusted.insert("blake3:candidatepreimage".to_owned());

        let (outcomes, approvals) = dispatch_transitive_hooks(&[candidate], &trusted, &NeverPrompt)
            .expect("a pinned hook dispatches without error");

        assert_eq!(
            outcomes.len(),
            1,
            "a preimage-pinned hook must RUN without consulting the prompt"
        );
        assert!(
            approvals.is_empty(),
            "a hook already trusted needs no fresh approval to persist"
        );
    }
}