appcore-update 1.0.3-rc

Application artifact update and rollback lifecycle for AppCore Runtime
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
// =============================================================================
//        #######
//     ###       ###     F: coordinator.rs
//    ##   ## ##   ##    P: AppCore-Runtime
//         ## ##
//                       C: 2026/07/22 15:41:18 by dnettoRaw
//    ##   ## ##   ##    U: 2026/07/24 11:51:10 by dnettoRaw
//      ###########      S: 1.0.1-rc.8
// =============================================================================

//! Defines bounded coordinator contracts and behavior for this crate.

use crate::{
    sha256_hex, ActivationReceipt, ArtifactAuthenticityVerifier, ArtifactDescriptor, ArtifactStore,
    StagedArtifact, UpdateError, UpdateProvider, UpdateRequest, UpdateResult,
};
use semver::Version;

#[cfg(test)]
struct TestOnlyArtifactVerifier;

#[cfg(test)]
impl ArtifactAuthenticityVerifier for TestOnlyArtifactVerifier {
    fn verify(&self, _artifact: &ArtifactDescriptor) -> UpdateResult<()> {
        Ok(())
    }
}

#[cfg(test)]
// appcore-norm: allow(global-state) reason: feature-gated verifier is immutable and stateless
static TEST_ONLY_ARTIFACTS: TestOnlyArtifactVerifier = TestOnlyArtifactVerifier;

struct VerifiedArtifact {
    descriptor: ArtifactDescriptor,
    bytes: Vec<u8>,
}

struct ActivatedArtifact {
    descriptor: ArtifactDescriptor,
    receipt: ActivationReceipt,
}

/// Health gate executed after an artifact becomes active.
pub trait ActivationHealthCheck: Send + Sync {
    /// Returns success only when the activated application is healthy.
    fn check(&self, artifact: &ArtifactDescriptor) -> UpdateResult<()>;
}

/// Controlled lifecycle points available to fault-injection tests.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum UpdateFaultPoint {
    /// After candidate selection and compatibility validation.
    AfterSelection,
    /// After download and checksum verification.
    AfterVerification,
    /// After staging but before activation.
    AfterStaging,
    /// After activation but before health verification.
    AfterActivation,
    /// After health verification but before commit.
    BeforeCommit,
}

/// Fault-injection contract used by deterministic update tests.
pub trait UpdateFaultInjector: Send + Sync {
    /// Returns an error when execution should fail at `point`.
    fn check(&self, point: UpdateFaultPoint) -> UpdateResult<()>;
}

/// Fault injector that never interrupts production execution.
#[derive(Debug, Clone, Copy, Default)]
pub struct NoUpdateFaults;

impl UpdateFaultInjector for NoUpdateFaults {
    fn check(&self, _point: UpdateFaultPoint) -> UpdateResult<()> {
        Ok(())
    }
}

/// Final result of one update attempt.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum UpdateOutcome {
    /// Provider reported no eligible update.
    NoUpdate,
    /// Artifact passed activation and health verification.
    Applied(ArtifactDescriptor),
    /// Activation failed and the previous artifact was restored.
    RolledBack {
        /// Artifact whose activation failed.
        attempted: ArtifactDescriptor,
        /// Controlled failure that triggered rollback.
        reason: String,
    },
}

/// Result of staging and activating an update before process-level health verification.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum UpdatePreparation {
    /// Provider reported no eligible update.
    NoUpdate,
    /// Candidate is active and awaits supervisor health verification.
    AwaitingHealth(Box<ArtifactDescriptor>),
}

/// Result of candidate verification and staging before activation.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum UpdateStaging {
    /// Provider reported no eligible update.
    NoUpdate,
    /// Verified candidate is staged but not active.
    Staged(Box<StagedArtifact>),
}

/// Coordinates provider, store, integrity, health and rollback boundaries.
pub struct UpdateCoordinator<'a> {
    provider: &'a dyn UpdateProvider,
    store: &'a dyn ArtifactStore,
    health: Option<&'a dyn ActivationHealthCheck>,
    authenticity: &'a dyn ArtifactAuthenticityVerifier,
    max_artifact_bytes: usize,
}

impl<'a> UpdateCoordinator<'a> {
    /// Creates a coordinator with an explicit artifact byte bound.
    pub fn new(
        provider: &'a dyn UpdateProvider,
        store: &'a dyn ArtifactStore,
        health: &'a dyn ActivationHealthCheck,
        max_artifact_bytes: usize,
    ) -> UpdateResult<Self> {
        #[cfg(test)]
        {
            Self::new_with_authenticity(
                provider,
                store,
                health,
                &TEST_ONLY_ARTIFACTS,
                max_artifact_bytes,
            )
        }
        #[cfg(not(test))]
        {
            let _ = (provider, store, health, max_artifact_bytes);
            Err(UpdateError::Authenticity(
                "an explicit artifact authenticity verifier is required".to_string(),
            ))
        }
    }

    /// Creates a coordinator with an explicit artifact authenticity policy.
    pub fn new_with_authenticity(
        provider: &'a dyn UpdateProvider,
        store: &'a dyn ArtifactStore,
        health: &'a dyn ActivationHealthCheck,
        authenticity: &'a dyn ArtifactAuthenticityVerifier,
        max_artifact_bytes: usize,
    ) -> UpdateResult<Self> {
        if max_artifact_bytes == 0 {
            return Err(UpdateError::InvalidArtifact(
                "max_artifact_bytes must be greater than zero".to_string(),
            ));
        }
        Ok(Self {
            provider,
            store,
            health: Some(health),
            authenticity,
            max_artifact_bytes,
        })
    }

    /// Creates a staging coordinator for process-level health verification.
    pub fn new_for_preparation(
        provider: &'a dyn UpdateProvider,
        store: &'a dyn ArtifactStore,
        authenticity: &'a dyn ArtifactAuthenticityVerifier,
        max_artifact_bytes: usize,
    ) -> UpdateResult<Self> {
        if max_artifact_bytes == 0 {
            return Err(UpdateError::InvalidArtifact(
                "max_artifact_bytes must be greater than zero".to_string(),
            ));
        }
        Ok(Self {
            provider,
            store,
            health: None,
            authenticity,
            max_artifact_bytes,
        })
    }

    /// Applies an update using the production no-fault path.
    pub fn apply(
        &self,
        request: &UpdateRequest,
        runtime_version: &str,
        protocol_version: &str,
    ) -> UpdateResult<UpdateOutcome> {
        self.apply_with_faults(request, runtime_version, protocol_version, &NoUpdateFaults)
    }

    /// Stages and activates an update without committing it.
    ///
    /// An application parent uses this two-phase path when the candidate must be
    /// restarted and probed before [`ArtifactStore::commit`] or rollback.
    pub fn prepare(
        &self,
        request: &UpdateRequest,
        runtime_version: &str,
        protocol_version: &str,
    ) -> UpdateResult<UpdatePreparation> {
        match self.stage_candidate(request, runtime_version, protocol_version)? {
            UpdateStaging::NoUpdate => Ok(UpdatePreparation::NoUpdate),
            UpdateStaging::Staged(staged) => {
                let receipt = self.store.activate(*staged)?;
                Ok(UpdatePreparation::AwaitingHealth(Box::new(
                    receipt.activated,
                )))
            }
        }
    }

    /// Verifies and stages a candidate without changing the active artifact.
    pub fn stage_candidate(
        &self,
        request: &UpdateRequest,
        runtime_version: &str,
        protocol_version: &str,
    ) -> UpdateResult<UpdateStaging> {
        self.store.recover()?;
        let Some(candidate) = self.select_candidate(request, runtime_version, protocol_version)?
        else {
            return Ok(UpdateStaging::NoUpdate);
        };
        let verified = self.fetch_verified(candidate)?;
        let staged = self.store.stage(&verified.descriptor, &verified.bytes)?;
        Ok(UpdateStaging::Staged(Box::new(staged)))
    }

    /// Applies an update while exposing deterministic lifecycle fault points.
    pub fn apply_with_faults(
        &self,
        request: &UpdateRequest,
        runtime_version: &str,
        protocol_version: &str,
        faults: &dyn UpdateFaultInjector,
    ) -> UpdateResult<UpdateOutcome> {
        self.store.recover()?;
        let Some(candidate) = self.select_candidate(request, runtime_version, protocol_version)?
        else {
            return Ok(UpdateOutcome::NoUpdate);
        };
        faults.check(UpdateFaultPoint::AfterSelection)?;
        let verified = self.fetch_verified(candidate)?;
        faults.check(UpdateFaultPoint::AfterVerification)?;
        let activated = self.stage_and_activate(verified, faults)?;
        self.finish_activation(activated, faults)
    }

    fn select_candidate(
        &self,
        request: &UpdateRequest,
        runtime_version: &str,
        protocol_version: &str,
    ) -> UpdateResult<Option<ArtifactDescriptor>> {
        let Some(candidate) = self.provider.latest(request)? else {
            return Ok(None);
        };
        if candidate.application_id() != &request.application_id {
            return Err(UpdateError::Incompatible(
                "artifact application identity differs from the request".to_string(),
            ));
        }
        if candidate.channel() != request.channel {
            return Err(UpdateError::Incompatible(
                "artifact update channel differs from the request".to_string(),
            ));
        }
        self.ensure_upgrade(request, &candidate)?;
        candidate.ensure_compatible(runtime_version, protocol_version)?;
        Ok(Some(candidate))
    }

    fn ensure_upgrade(
        &self,
        request: &UpdateRequest,
        candidate: &ArtifactDescriptor,
    ) -> UpdateResult<()> {
        let installed = Version::parse(&request.current_version).map_err(|error| {
            UpdateError::Incompatible(format!("invalid installed application version: {error}"))
        })?;
        let candidate_version =
            Version::parse(candidate.application_version()).map_err(|error| {
                UpdateError::InvalidArtifact(format!(
                    "invalid candidate application version: {error}"
                ))
            })?;
        if candidate_version <= installed {
            return Err(UpdateError::Incompatible(format!(
                "candidate version {candidate_version} does not advance installed version {installed}"
            )));
        }
        let Some(active) = self.store.current()? else {
            return Ok(());
        };
        if active.application_id() != candidate.application_id() {
            return Err(UpdateError::Incompatible(
                "active artifact application identity differs from the candidate".to_string(),
            ));
        }
        if active.build_id() == candidate.build_id() {
            return Err(UpdateError::Incompatible(
                "candidate reuses the active build identity".to_string(),
            ));
        }
        let active_version = Version::parse(active.application_version()).map_err(|error| {
            UpdateError::Store(format!("active artifact version is invalid: {error}"))
        })?;
        if candidate_version <= active_version {
            return Err(UpdateError::Incompatible(format!(
                "candidate version {candidate_version} does not advance active version {active_version}"
            )));
        }
        Ok(())
    }

    fn fetch_verified(&self, candidate: ArtifactDescriptor) -> UpdateResult<VerifiedArtifact> {
        let declared_size =
            usize::try_from(candidate.size_bytes()).map_err(|_| UpdateError::ArtifactTooLarge {
                max_bytes: self.max_artifact_bytes,
            })?;
        if declared_size > self.max_artifact_bytes {
            return Err(UpdateError::ArtifactTooLarge {
                max_bytes: self.max_artifact_bytes,
            });
        }
        let bytes = self.provider.fetch(&candidate, self.max_artifact_bytes)?;
        if bytes.len() > self.max_artifact_bytes || bytes.len() != declared_size {
            return Err(UpdateError::ArtifactTooLarge {
                max_bytes: self.max_artifact_bytes,
            });
        }
        if sha256_hex(&bytes) != candidate.sha256() {
            return Err(UpdateError::ChecksumMismatch);
        }
        self.authenticity.verify(&candidate)?;
        Ok(VerifiedArtifact {
            descriptor: candidate,
            bytes,
        })
    }

    fn stage_and_activate(
        &self,
        verified: VerifiedArtifact,
        faults: &dyn UpdateFaultInjector,
    ) -> UpdateResult<ActivatedArtifact> {
        let staged = self.store.stage(&verified.descriptor, &verified.bytes)?;
        faults.check(UpdateFaultPoint::AfterStaging)?;
        let receipt = self.store.activate(staged)?;
        Ok(ActivatedArtifact {
            descriptor: verified.descriptor,
            receipt,
        })
    }

    fn finish_activation(
        &self,
        activated: ActivatedArtifact,
        faults: &dyn UpdateFaultInjector,
    ) -> UpdateResult<UpdateOutcome> {
        let health = self.health.ok_or_else(|| {
            UpdateError::Health("activation health check is not configured".to_string())
        })?;
        let result = faults
            .check(UpdateFaultPoint::AfterActivation)
            .and_then(|_| health.check(&activated.descriptor))
            .and_then(|_| faults.check(UpdateFaultPoint::BeforeCommit))
            .and_then(|_| self.store.commit(&activated.receipt));
        match result {
            Ok(()) => Ok(UpdateOutcome::Applied(activated.descriptor)),
            Err(cause) => self.rollback_after_failure(activated, cause),
        }
    }

    fn rollback_after_failure(
        &self,
        activated: ActivatedArtifact,
        cause: UpdateError,
    ) -> UpdateResult<UpdateOutcome> {
        match self.store.rollback(&activated.receipt) {
            Ok(()) => Ok(UpdateOutcome::RolledBack {
                attempted: activated.descriptor,
                reason: cause.to_string(),
            }),
            Err(rollback) => Err(UpdateError::RollbackFailed {
                cause: cause.to_string(),
                rollback: rollback.to_string(),
            }),
        }
    }
}