polyc-runtime 2026.8.3

Shared Unix-coherence runtime for polychrome binaries: logging, health/metrics side-server, signals.
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
//! Update stager — the apply substrate the hot and warm update paths build on.
//!
//! The stager runs one job: land a staged update safely. It downloads the
//! update to a *side* location that never touches the live path, verifies its
//! signature *before* anything is applied, flips the live pointer to the new
//! version, and — if a post-apply health check fails — flips the pointer back
//! to the previous version. The previous version stays staged the whole time,
//! so a rollback is a pointer flip rather than a re-download.
//!
//! # The seams
//!
//! The stager owns the *state machine*, not the I/O. Everything that touches
//! the outside world is an injectable seam, so the machine can be exercised
//! without a real download, a real key, or a real process boot:
//!
//! - [`UpdateSource`] downloads the release to a side location.
//! - [`SignatureVerifier`] checks the ed25519 signature over the staged bytes.
//!   In production this wraps the project's ed25519 key custody (the same
//!   `verify` the event log and approval flow use); the stager only ever sees a
//!   yes/no answer, so a foundation crate needs no signing dependency to run it.
//! - [`Activator`] materializes the pointer flip — a symlink swap, an image
//!   tag, or a process swap. It is called once to apply and, in reverse, once
//!   to roll back.
//! - [`HealthCheck`] probes the freshly-applied version. [`Health::Healthy`]
//!   commits; anything else triggers the auto-rollback.
//!
//! Each seam has a blanket implementation for the matching closure, so a caller
//! can pass a closure where a full type would be overkill.
//!
//! # The state machine
//!
//! [`Stager::stage_and_apply`] walks a fixed sequence:
//!
//! ```text
//!   download ──▶ verify ──▶ apply (pointer flip) ──▶ health check
//!                  │                                     │
//!                  │ (unverified)                        ├─ healthy  ──▶ Committed
//!                  ▼                                     │
//!               refused                                  └─ unhealthy ──▶ auto-rollback ──▶ RolledBack
//!               (nothing applied)                            (pointer flips back)
//! ```
//!
//! The invariant the tests pin: an unverified bundle is never applied (the live
//! pointer is untouched), the previous version stays staged, and a failed
//! health check leaves the system honestly back on the previous version.
//!
//! The staged artifact carries the [`StagedBundle`] it delivers, so a caller
//! that already classified the release (see
//! [`crate::compat`]) hands the same value straight through to apply.

use std::path::PathBuf;

use thiserror::Error;

use crate::compat::StagedBundle;

/// A release identity — the pointer the live slot resolves to and flips between.
///
/// Opaque and cheap to clone; in production it is a content address, a version
/// tag, or an image digest, and the [`Activator`] decides how a pointer flip to
/// it materializes.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ReleaseId(String);

impl ReleaseId {
    /// Wrap a release identity string.
    #[must_use]
    pub fn new(id: impl Into<String>) -> Self {
        Self(id.into())
    }

    /// The underlying identity string.
    #[must_use]
    pub const fn as_str(&self) -> &str {
        self.0.as_str()
    }
}

impl std::fmt::Display for ReleaseId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&self.0)
    }
}

/// An update downloaded to a side location, before verification.
///
/// Holds everything the stager needs to verify and apply the release without
/// re-reading the source: the release identity, the side path it landed at
/// (never the live path), the [`StagedBundle`] it delivers, and the detached
/// ed25519 signature over `signed_bytes` under `signer_public_key`.
#[derive(Debug, Clone)]
pub struct StagedArtifact {
    /// Which release this artifact is.
    pub release: ReleaseId,
    /// The side location the update was downloaded to — never the live path.
    pub staged_path: PathBuf,
    /// The config-as-data bundle this artifact delivers, already classified
    /// against the running runtime by [`crate::compat`].
    pub bundle: StagedBundle,
    /// The canonical bytes the signature commits to.
    pub signed_bytes: Vec<u8>,
    /// The detached ed25519 signature over [`Self::signed_bytes`].
    pub signature: Vec<u8>,
    /// The encoded public key the signature is expected to verify against.
    pub signer_public_key: Vec<u8>,
}

/// The result of a post-apply health check.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Health {
    /// The applied version is serving; commit it.
    Healthy,
    /// The applied version failed to come up; the enclosed reason is recorded
    /// and surfaced in the rollback outcome.
    Unhealthy(String),
}

/// What happened to a `stage_and_apply` call that got as far as a health check.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Outcome {
    /// The new version passed its health check and is now the live version.
    Committed {
        /// The version now serving.
        version: ReleaseId,
    },
    /// The new version failed its health check; the pointer flipped back and the
    /// previous version is live again.
    RolledBack {
        /// The version still serving after the rollback — the one that was live
        /// before the attempt.
        stayed_on: ReleaseId,
        /// The health-check failure that triggered the rollback.
        reason: String,
    },
}

impl Outcome {
    /// A one-line, user-facing summary of the outcome.
    ///
    /// Honest about a rollback — it names the version still serving and why the
    /// update did not stick — so the surface presenting it never has to word the
    /// state twice.
    #[must_use]
    pub fn summary(&self) -> String {
        match self {
            Self::Committed { version } => format!("Now running {version}."),
            Self::RolledBack { stayed_on, reason } => {
                format!(
                    "Stayed on {stayed_on} — the new version failed its health check ({reason})."
                )
            }
        }
    }
}

/// Why a `stage_and_apply` call could not reach a health check.
#[derive(Debug, Error)]
pub enum StageError {
    /// The source could not download the release to its side location.
    #[error("staging the update failed: {0}")]
    Download(String),
    /// The staged bundle's signature did not verify against a trusted key, so it
    /// was refused before anything was applied.
    #[error("the update's signature did not verify against a trusted key")]
    Unverified,
    /// The pointer flip itself failed — either applying the new version or
    /// flipping back to the previous one.
    #[error("activating the staged update failed: {0}")]
    Activate(String),
}

/// Downloads a release to a side location, never the live path.
pub trait UpdateSource {
    /// Fetch `release` into a side location and return the staged artifact.
    ///
    /// # Errors
    ///
    /// Returns [`StageError::Download`] when the release cannot be fetched or
    /// written to its side location.
    fn fetch(&self, release: &ReleaseId) -> Result<StagedArtifact, StageError>;
}

impl<F> UpdateSource for F
where
    F: Fn(&ReleaseId) -> Result<StagedArtifact, StageError>,
{
    fn fetch(&self, release: &ReleaseId) -> Result<StagedArtifact, StageError> {
        self(release)
    }
}

/// Verifies the ed25519 signature over a staged artifact.
///
/// The contract is a plain yes/no: `true` iff the signature over
/// [`StagedArtifact::signed_bytes`] verifies against a trusted key. A `false`
/// return means the bundle is refused and never applied. Keeping the answer a
/// boolean lets a foundation crate run the interlock without depending on the
/// signing crate — production wraps the project's ed25519 `verify` here.
pub trait SignatureVerifier {
    /// Whether `artifact`'s signature verifies against a trusted key.
    fn verify(&self, artifact: &StagedArtifact) -> bool;
}

impl<F> SignatureVerifier for F
where
    F: Fn(&StagedArtifact) -> bool,
{
    fn verify(&self, artifact: &StagedArtifact) -> bool {
        self(artifact)
    }
}

/// Materializes a pointer flip to a release — a symlink swap, an image tag, or a
/// process swap.
///
/// Called once to apply the new version and, in reverse, once to roll back to
/// the previous one. The previous version stays staged, so a rollback flip never
/// re-downloads.
pub trait Activator {
    /// Flip the live pointer to `release`.
    ///
    /// # Errors
    ///
    /// Returns [`StageError::Activate`] when the pointer cannot be flipped.
    fn activate(&self, release: &ReleaseId) -> Result<(), StageError>;
}

impl<F> Activator for F
where
    F: Fn(&ReleaseId) -> Result<(), StageError>,
{
    fn activate(&self, release: &ReleaseId) -> Result<(), StageError> {
        self(release)
    }
}

/// Probes a freshly-applied version to decide whether it commits or rolls back.
pub trait HealthCheck {
    /// Probe the applied version. [`Health::Healthy`] commits; anything else
    /// triggers the auto-rollback.
    fn check(&self) -> Health;
}

impl<F> HealthCheck for F
where
    F: Fn() -> Health,
{
    fn check(&self) -> Health {
        self()
    }
}

/// The apply substrate: stage → verify → apply → (commit | auto-rollback).
///
/// Holds the live version and the previous version behind it, plus the four
/// seams that touch the outside world. [`Stager::stage_and_apply`] drives the
/// whole sequence; [`Stager::live`] and [`Stager::previous`] expose the current
/// pointer state.
pub struct Stager<S, V, A, H> {
    source: S,
    verifier: V,
    activator: A,
    health: H,
    live: ReleaseId,
    previous: Option<ReleaseId>,
}

impl<S, V, A, H> Stager<S, V, A, H>
where
    S: UpdateSource,
    V: SignatureVerifier,
    A: Activator,
    H: HealthCheck,
{
    /// Build a stager over its four seams, starting from `initial_live` as the
    /// version already serving.
    pub const fn new(
        source: S,
        verifier: V,
        activator: A,
        health: H,
        initial_live: ReleaseId,
    ) -> Self {
        Self {
            source,
            verifier,
            activator,
            health,
            live: initial_live,
            previous: None,
        }
    }

    /// The version currently serving — the committed live pointer.
    #[must_use]
    pub const fn live(&self) -> &ReleaseId {
        &self.live
    }

    /// The previous version, kept staged so a rollback is a pointer flip. `None`
    /// until the first update commits.
    #[must_use]
    pub const fn previous(&self) -> Option<&ReleaseId> {
        self.previous.as_ref()
    }

    /// Stage `release`, verify it, apply it, and commit or auto-roll-back on the
    /// health check.
    ///
    /// The sequence is fixed:
    ///
    /// 1. download the release to a side location;
    /// 2. verify its signature — an unverified bundle returns
    ///    [`StageError::Unverified`] with the live pointer untouched;
    /// 3. flip the live pointer to the new version;
    /// 4. run the health check — on [`Health::Healthy`] commit and return
    ///    [`Outcome::Committed`]; on [`Health::Unhealthy`] flip the pointer back
    ///    to the previous version and return [`Outcome::RolledBack`].
    ///
    /// The committed live pointer ([`Stager::live`]) only advances in step 4 on
    /// a passed health check, so a refused or rolled-back attempt leaves the
    /// stager exactly where it started.
    ///
    /// # Errors
    ///
    /// Returns [`StageError::Download`] if the source cannot stage the release,
    /// [`StageError::Unverified`] if the signature does not verify, or
    /// [`StageError::Activate`] if a pointer flip fails (applying the new
    /// version, or — worse — flipping back during a rollback).
    pub fn stage_and_apply(&mut self, release: &ReleaseId) -> Result<Outcome, StageError> {
        // 1. Download to a side location — never the live path.
        let artifact = self.source.fetch(release)?;

        // 2. Verify BEFORE anything is applied. An unverified bundle is refused
        //    here, with the live pointer and staged previous both untouched.
        if !self.verifier.verify(&artifact) {
            return Err(StageError::Unverified);
        }

        // 3. Apply: flip the live pointer to the new version. The version that
        //    was live stays staged as the rollback target. `self.live` is not
        //    advanced yet — it only commits once the health check passes.
        let rollback_to = self.live.clone();
        self.activator.activate(&artifact.release)?;

        // 4. Health check decides commit vs. auto-rollback.
        match self.health.check() {
            Health::Healthy => {
                self.previous = Some(rollback_to);
                self.live = artifact.release.clone();
                Ok(Outcome::Committed {
                    version: artifact.release,
                })
            }
            Health::Unhealthy(reason) => {
                // Auto-rollback: flip the pointer back to the previous version.
                // The committed `self.live` never moved, so we are honestly back
                // where we started.
                self.activator.activate(&rollback_to)?;
                Ok(Outcome::RolledBack {
                    stayed_on: rollback_to,
                    reason,
                })
            }
        }
    }
}

#[cfg(test)]
mod tests {
    #![allow(clippy::pedantic, clippy::nursery, missing_docs)]

    use std::cell::RefCell;
    use std::rc::Rc;

    use super::*;
    use crate::compat::RuntimeTarget;

    /// A recording activator whose flip log is shared with the test after it is
    /// moved into the stager, so the sequence of pointer flips is observable.
    #[derive(Clone)]
    struct RecordingActivator {
        flips: Rc<RefCell<Vec<ReleaseId>>>,
        fail_on: Option<ReleaseId>,
    }

    impl RecordingActivator {
        fn new() -> Self {
            Self {
                flips: Rc::new(RefCell::new(Vec::new())),
                fail_on: None,
            }
        }

        fn failing_on(release: ReleaseId) -> Self {
            Self {
                flips: Rc::new(RefCell::new(Vec::new())),
                fail_on: Some(release),
            }
        }

        fn flips(&self) -> Vec<ReleaseId> {
            self.flips.borrow().clone()
        }
    }

    impl Activator for RecordingActivator {
        fn activate(&self, release: &ReleaseId) -> Result<(), StageError> {
            if self.fail_on.as_ref() == Some(release) {
                return Err(StageError::Activate(format!("cannot flip to {release}")));
            }
            self.flips.borrow_mut().push(release.clone());
            Ok(())
        }
    }

    fn artifact_for(release: &ReleaseId) -> StagedArtifact {
        StagedArtifact {
            release: release.clone(),
            // A side path — the download never touches the live path.
            staged_path: PathBuf::from(format!("/var/lib/polychrome/staged/{release}")),
            bundle: StagedBundle::new(
                RuntimeTarget::new(3, 7, "polychrome.uno/v1"),
                format!("catalog-{release}"),
            ),
            signed_bytes: format!("bytes-of-{release}").into_bytes(),
            signature: vec![0xAB; 4],
            signer_public_key: vec![0xCD; 4],
        }
    }

    /// A source that always stages the requested release from a side location.
    fn ok_source(release: &ReleaseId) -> Result<StagedArtifact, StageError> {
        Ok(artifact_for(release))
    }

    #[test]
    fn verified_healthy_update_commits_and_keeps_previous_staged() {
        let activator = RecordingActivator::new();
        let mut stager = Stager::new(
            ok_source,
            |_: &StagedArtifact| true,
            activator.clone(),
            || Health::Healthy,
            ReleaseId::new("v1"),
        );

        let outcome = stager.stage_and_apply(&ReleaseId::new("v2")).unwrap();

        assert_eq!(
            outcome,
            Outcome::Committed {
                version: ReleaseId::new("v2")
            }
        );
        // The live pointer advanced to the new version...
        assert_eq!(stager.live(), &ReleaseId::new("v2"));
        // ...and the version that was live stays staged as the rollback target.
        assert_eq!(stager.previous(), Some(&ReleaseId::new("v1")));
        // A healthy apply flips the pointer exactly once, forward.
        assert_eq!(activator.flips(), vec![ReleaseId::new("v2")]);
    }

    #[test]
    fn unverified_bundle_is_never_applied() {
        let activator = RecordingActivator::new();
        let mut stager = Stager::new(
            ok_source,
            // Signature does not verify.
            |_: &StagedArtifact| false,
            activator.clone(),
            // A health check that would panic proves it is never reached.
            || panic!("health check must not run for an unverified bundle"),
            ReleaseId::new("v1"),
        );

        let err = stager.stage_and_apply(&ReleaseId::new("v2")).unwrap_err();

        assert!(matches!(err, StageError::Unverified));
        // Nothing was applied: the pointer never flipped and the live version is
        // unchanged.
        assert!(activator.flips().is_empty());
        assert_eq!(stager.live(), &ReleaseId::new("v1"));
        assert_eq!(stager.previous(), None);
    }

    #[test]
    fn failed_health_check_auto_rolls_back_to_previous() {
        let activator = RecordingActivator::new();
        let mut stager = Stager::new(
            ok_source,
            |_: &StagedArtifact| true,
            activator.clone(),
            || Health::Unhealthy("readiness probe timed out".to_owned()),
            ReleaseId::new("v1"),
        );

        let outcome = stager.stage_and_apply(&ReleaseId::new("v2")).unwrap();

        assert_eq!(
            outcome,
            Outcome::RolledBack {
                stayed_on: ReleaseId::new("v1"),
                reason: "readiness probe timed out".to_owned(),
            }
        );
        // The committed live pointer never moved off the previous version.
        assert_eq!(stager.live(), &ReleaseId::new("v1"));
        assert_eq!(stager.previous(), None);
        // The pointer flipped to the new version, then back — a rollback is a
        // pointer flip, not a re-download.
        assert_eq!(
            activator.flips(),
            vec![ReleaseId::new("v2"), ReleaseId::new("v1")]
        );
    }

    #[test]
    fn rollback_outcome_surfaces_an_honest_summary() {
        let outcome = Outcome::RolledBack {
            stayed_on: ReleaseId::new("v1"),
            reason: "readiness probe timed out".to_owned(),
        };
        let summary = outcome.summary();

        assert_eq!(
            summary,
            "Stayed on v1 — the new version failed its health check (readiness probe timed out).",
        );
        // Copy rules: no apology or filler words.
        for banned in ["sorry", "please", "unfortunately"] {
            assert!(
                !summary.to_lowercase().contains(banned),
                "rollback summary must not contain {banned:?}",
            );
        }
    }

    #[test]
    fn committed_outcome_summary_names_the_new_version() {
        let summary = Outcome::Committed {
            version: ReleaseId::new("v2"),
        }
        .summary();
        assert_eq!(summary, "Now running v2.");
    }

    #[test]
    fn download_failure_applies_nothing() {
        let activator = RecordingActivator::new();
        let mut stager = Stager::new(
            |_: &ReleaseId| Err(StageError::Download("side location is full".to_owned())),
            |_: &StagedArtifact| panic!("verify must not run when the download fails"),
            activator.clone(),
            || panic!("health check must not run when the download fails"),
            ReleaseId::new("v1"),
        );

        let err = stager.stage_and_apply(&ReleaseId::new("v2")).unwrap_err();

        assert!(matches!(err, StageError::Download(_)));
        assert!(activator.flips().is_empty());
        assert_eq!(stager.live(), &ReleaseId::new("v1"));
    }

    #[test]
    fn apply_flip_failure_leaves_live_untouched() {
        // The activator refuses to flip to the new version; the live pointer
        // must stay on the previous one and nothing commits.
        let activator = RecordingActivator::failing_on(ReleaseId::new("v2"));
        let mut stager = Stager::new(
            ok_source,
            |_: &StagedArtifact| true,
            activator.clone(),
            || Health::Healthy,
            ReleaseId::new("v1"),
        );

        let err = stager.stage_and_apply(&ReleaseId::new("v2")).unwrap_err();

        assert!(matches!(err, StageError::Activate(_)));
        assert!(activator.flips().is_empty());
        assert_eq!(stager.live(), &ReleaseId::new("v1"));
        assert_eq!(stager.previous(), None);
    }

    #[test]
    fn a_second_update_restages_the_prior_live_as_previous() {
        let activator = RecordingActivator::new();
        let mut stager = Stager::new(
            ok_source,
            |_: &StagedArtifact| true,
            activator.clone(),
            || Health::Healthy,
            ReleaseId::new("v1"),
        );

        stager.stage_and_apply(&ReleaseId::new("v2")).unwrap();
        stager.stage_and_apply(&ReleaseId::new("v3")).unwrap();

        assert_eq!(stager.live(), &ReleaseId::new("v3"));
        // The rollback target tracks the most recent committed version.
        assert_eq!(stager.previous(), Some(&ReleaseId::new("v2")));
        assert_eq!(
            activator.flips(),
            vec![ReleaseId::new("v2"), ReleaseId::new("v3")]
        );
    }

    #[test]
    fn staged_artifact_carries_its_classified_bundle() {
        // The download seam lands the artifact on a side path and carries the
        // StagedBundle from the compat classifier straight through to apply.
        let artifact = artifact_for(&ReleaseId::new("v2"));
        assert!(
            artifact
                .staged_path
                .starts_with("/var/lib/polychrome/staged")
        );
        assert_eq!(artifact.bundle.catalog_hash, "catalog-v2");
    }
}