alien-core 3.3.56

Deploy software into your customers' cloud accounts and keep it fully managed
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
//! Deployment state, step results, and runtime metadata.

use crate::{ObservedInventoryBatch, Platform, ResourceHeartbeat, StackState};
use alien_error::AlienError;
use bon::Builder;
use indexmap::IndexMap;
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;

use super::{DeploymentStatus, EnvironmentInfo, ReleaseInfo};

/// Actor that owns structural work during the initial setup phase.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Default)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
#[serde(rename_all = "camelCase")]
pub enum InitialSetupAuthority {
    /// Setup state was registered by an external setup engine. Alien may only
    /// continue controller states that the importer explicitly initialized.
    #[default]
    ImportedHandoff,
    /// Alien is the setup engine and is running with administrator credentials.
    DirectSetup,
}

/// One-shot authority for a setup re-import to replace setup-owned resources.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct SetupUpdateAuthorization {
    /// Unique revision used by persistence layers for compare-and-swap updates.
    pub nonce: String,
    /// Setup resources and permissions digest (`Stack::setup_update_digest`) of the last
    /// successful deployment. The field name is retained for serialized compatibility.
    pub baseline_frozen_digest: String,
    /// Setup resources and permissions digest (`Stack::setup_update_digest`) of the stack
    /// the setup re-import prepared.
    pub target_frozen_digest: String,
    /// Release whose stack was prepared by setup.
    pub release_id: String,
    /// Stable setup target recorded on the imported deployment.
    pub setup_target: String,
    /// Exact setup artifact revision that authored this authority.
    pub setup_fingerprint: String,
    /// Setup fingerprint contract version.
    pub setup_fingerprint_version: u32,
}

/// A credential outage's durable retry window, scoped to its deployment phase.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct CredentialRetry {
    /// First credential failure in this phase; subsequent attempts keep this timestamp.
    pub started_at: chrono::DateTime<chrono::Utc>,
    /// Changing phase starts a new window instead of carrying a previous outage forward.
    pub status: DeploymentStatus,
}

/// Explicit disposition of durable data when deleting a deployment.
/// Missing values from older checkpoints always preserve data.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Default)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
#[serde(rename_all = "camelCase")]
pub enum DeletionPolicy {
    #[default]
    Retain,
    Erase,
}

/// When an air-gapped site's latest status was written and received.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct AirgappedReportMetadata {
    /// Site clock time; absent for reports from older clients.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub written_at: Option<chrono::DateTime<chrono::Utc>>,
    /// Manager clock time when the report was accepted.
    pub received_at: chrono::DateTime<chrono::Utc>,
}

/// Runtime metadata for deployment
///
/// Stores deployment state that needs to persist across step calls.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct RuntimeMetadata {
    /// User-authorized data disposition, persisted before deletion starts.
    #[serde(default)]
    pub deletion_policy: DeletionPolicy,

    /// Most recent air-gapped report accepted by the manager.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub airgapped_report: Option<AirgappedReportMetadata>,

    /// Bounded credential retry state shared by all deployment drivers.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub credential_retry: Option<CredentialRetry>,

    /// Actor authorized to perform structural Frozen-resource work during
    /// InitialSetup. Missing legacy values fail closed as ImportedHandoff.
    #[serde(default)]
    pub initial_setup_authority: InitialSetupAuthority,

    /// Setup-owned grant to keep using the direct setup executor for updates.
    /// Runtime settings and release contents must never create or change this grant.
    #[serde(default, skip_serializing_if = "super::is_false")]
    pub automatic_setup: bool,

    /// Hash of the environment variables snapshot that was last synced to the vault
    /// Used to avoid redundant sync operations during incremental deployment
    #[serde(skip_serializing_if = "Option::is_none")]
    pub last_synced_env_vars_hash: Option<String>,

    /// Exact vault keys owned by the deployment secret synchronizer. This
    /// inventory lets a later snapshot delete removed keys without listing or
    /// touching unrelated values in the same vault.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub last_synced_secret_names: Vec<String>,

    /// Credential revocation completed before resource teardown. A delete retry
    /// can use this receipt after the vault or its binding is gone. Secret sync
    /// clears it before a deployment can write runtime credentials again.
    #[serde(default, skip_serializing_if = "is_false")]
    pub vault_credentials_revoked: bool,

    /// Digest of the installed data inventory whose runtime public-access and
    /// expiry configuration was protected before cleanup. The serialized field
    /// name is retained for compatibility. This is not a
    /// receipt for effective expiry propagation or privileged setup teardown.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub retained_storage_protection_hash: Option<String>,

    /// UTC API-confirmation time for the matching data protection digest.
    /// Provider propagation must be qualified separately before final retention.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub retained_storage_protection_confirmed_at: Option<chrono::DateTime<chrono::Utc>>,

    /// Start of the bounded wait for retained data's expiry controls to disable.
    /// Cleared on confirmation, explicit retry, or returning to normal execution.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub retained_data_protection_started_at: Option<chrono::DateTime<chrono::Utc>>,

    /// The prepared (mutated) stack from the last successful deployment phase
    /// This is the stack AFTER mutations have been applied (with service accounts, vault, etc.)
    /// Used for compatibility checks during updates to compare mutated stacks
    #[serde(skip_serializing_if = "Option::is_none")]
    pub prepared_stack: Option<crate::Stack>,

    /// Canonical resolved answers for inputs that gate Frozen resources,
    /// keyed by input id, recorded when the deployment is created or its
    /// setup import registers.
    ///
    /// Manual installations keep these answers fixed. An installation with an
    /// ongoing-management grant refreshes them when preparing a release. Live
    /// resources sharing a gate use the same recorded answer as Frozen resources.
    #[serde(default, skip_serializing_if = "IndexMap::is_empty")]
    pub persisted_gate_answers: GateAnswers,

    /// Prepared target for an update that has not reached Running yet. Keeping
    /// it separate preserves the last successful baseline across retries.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub pending_prepared_stack: Option<crate::Stack>,

    /// Release the in-flight stack was prepared from: `pending_prepared_stack`
    /// during updates, or `prepared_stack` during local first startup. The target can move
    /// to a newer release while that stack is still being applied; this tells
    /// the update which release actually converged.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub pending_prepared_release_id: Option<String>,

    /// One-shot setup update authority. It contains only non-secret identity
    /// and canonical resource digests, never the imported payload or tokens.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub setup_update_authorization: Option<SetupUpdateAuthorization>,

    /// Last generated CLI package revision whose direct setup was applied.
    /// This lets a newer generated CLI refresh setup-owned infrastructure once
    /// before handing runtime changes back to the hosted manager.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub direct_setup_revision: Option<String>,

    /// Whether cross-account registry access has been successfully granted.
    /// Set to true after the manager successfully sets the ECR/GAR repo policy
    /// for this deployment's target account. Prevents redundant API calls on
    /// every reconcile tick.
    #[serde(default, skip_serializing_if = "is_false")]
    pub registry_access_granted: bool,

    /// What that grant opened, compared against what the deployment needs now so a grant made
    /// before a resource existed is finished rather than skipped, and the revoke names what was
    /// granted. Absent on a grant recorded before this field existed.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub registry_access: Option<RegistryAccess>,

    /// What a direct setup created for resources it does not own, keyed by resource id. Setup
    /// teardown removes exactly what is recorded here.
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub setup_scaffolding: BTreeMap<String, SetupScaffolding>,

    /// Whether each vault-native deployer secret is in the customer's secret
    /// store, with where it goes. Checked from metadata only; no value is ever
    /// read or recorded here.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub deployer_secrets: Vec<crate::DeployerSecretReport>,
}

/// Cloud objects a direct setup created so a runtime-owned resource can run.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
#[serde(rename_all = "camelCase", tag = "type")]
pub enum SetupScaffolding {
    /// An AWS sandbox's image-build role, and for `egress: deny` its egress objects.
    #[serde(rename_all = "camelCase")]
    AwsSandbox {
        /// IAM role the image build runs as.
        build_role_name: String,
        /// What `egress: deny` needs; absent for an open sandbox.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        egress: Option<AwsSandboxEgressScaffolding>,
        /// A Frozen sandbox's MicroVM image, built during setup. A Live one's image belongs to its
        /// runtime controller.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        image_arn: Option<String>,
    },
}

/// The objects that keep an AWS deny sandbox's sessions inside the VPC. Each id is recorded as
/// soon as the object exists and cleared once it is deleted.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct AwsSandboxEgressScaffolding {
    /// IAM role Lambda assumes to place the connector's network interfaces.
    pub operator_role_name: String,
    /// Security group permitting egress to loopback only.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub security_group_id: Option<String>,
    /// `AWS::Lambda::NetworkConnector` the sessions start with.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub connector_arn: Option<String>,
    /// Cloud Control request creating or deleting the connector that AWS has not finished. Kept
    /// so the request's outcome, and AWS's reason when it fails, is read on a later call.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub connector_request: Option<String>,
}

impl SetupScaffolding {
    /// Takes each object `found` names that this record does not, and keeps every one it does:
    /// a recorded id is what setup last saw, and teardown must delete that one.
    pub fn fill_missing(&mut self, found: SetupScaffolding) {
        match (self, found) {
            (
                SetupScaffolding::AwsSandbox {
                    egress, image_arn, ..
                },
                SetupScaffolding::AwsSandbox {
                    egress: found_egress,
                    image_arn: found_image_arn,
                    ..
                },
            ) => {
                if image_arn.is_none() {
                    *image_arn = found_image_arn;
                }
                match (egress.as_mut(), found_egress) {
                    (None, found_egress) => *egress = found_egress,
                    (Some(recorded), Some(found)) => {
                        if recorded.security_group_id.is_none() {
                            recorded.security_group_id = found.security_group_id;
                        }
                        if recorded.connector_arn.is_none() {
                            recorded.connector_arn = found.connector_arn;
                        }
                    }
                    (Some(_), None) => {}
                }
            }
        }
    }
}

/// The cross-account read a manager opened on Alien's registry for one deployment.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct RegistryAccess {
    /// Repository identifiers the grant names, sorted.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub repositories: Vec<String>,

    /// Compute services the grant admits, sorted. Each pulls as its own principal, so a service
    /// added later needs the policy rewritten.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub service_types: Vec<String>,
}

/// Deployment state
///
/// Represents the current state of deployed infrastructure, including release tracking.
/// This is platform-agnostic - no backend IDs or database relationships.
///
/// The deployment engine manages releases internally: when a deployment succeeds,
/// it promotes `target_release` to `current_release` and clears `target_release`.
#[derive(Debug, Clone, Serialize, Deserialize, Builder)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct DeploymentState {
    /// Current lifecycle phase
    pub status: DeploymentStatus,
    /// Target cloud platform (AWS, GCP, Azure, Kubernetes)
    pub platform: Platform,
    /// Currently deployed release (None for first deployment)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub current_release: Option<ReleaseInfo>,
    /// Target release to deploy (None when synced with current)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub target_release: Option<ReleaseInfo>,
    /// Infrastructure resource tracking (which resources exist, their status, outputs)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub stack_state: Option<StackState>,
    /// Deployment-level error for failures not owned by a specific resource.
    ///
    /// Resource controller failures belong in `stack_state.resources[*].error`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub error: Option<AlienError>,
    /// Cloud account details (account ID, project number, region)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub environment_info: Option<EnvironmentInfo>,
    /// Deployment-specific data (prepared stacks, phase tracking, etc.)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub runtime_metadata: Option<RuntimeMetadata>,
    /// Whether a retry has been requested for a failed deployment
    /// When true and status is a failed state, the deployment system will retry failed resources
    #[serde(default, skip_serializing_if = "is_false")]
    pub retry_requested: bool,
    /// Protocol version for cross-actor compatibility.
    /// All actors (manager, push client, agent) check this before stepping.
    /// Mismatched versions produce a clear error instead of silent corruption.
    /// See docs/02-manager/10-deployment-protocol.md.
    pub protocol_version: u32,
}

impl DeploymentState {
    /// Returns whether this state carries desired infrastructure for the
    /// deployment runner to converge.
    pub fn has_desired(&self) -> bool {
        self.current_release.is_some()
            || self.target_release.is_some()
            || self.stack_state.is_some()
    }
}

/// Result of a deployment step
///
/// Contains the complete next deployment state along with hints for the platform.
/// This replaces the old delta-based `DeploymentStateUpdate` approach.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct DeploymentStepResult {
    /// The complete next deployment state
    pub state: DeploymentState,

    /// Suggested delay before next step (optimization hint)
    /// - `None`: No suggested delay, can poll immediately
    /// - `Some(ms)`: Wait this many milliseconds before next step
    #[serde(skip_serializing_if = "Option::is_none")]
    pub suggested_delay_ms: Option<u64>,

    /// Whether to update heartbeat timestamp (monitoring signal)
    /// - `false`: Don't update heartbeat (default for most steps)
    /// - `true`: Update lastHeartbeatAt (for successful health checks in Running state)
    #[serde(default, skip_serializing_if = "is_false")]
    pub update_heartbeat: bool,

    /// Managed Alien resource status samples emitted by controllers during this step.
    #[serde(
        default,
        rename = "resourceHeartbeats",
        skip_serializing_if = "Vec::is_empty"
    )]
    pub heartbeats: Vec<ResourceHeartbeat>,

    /// Observed raw-resource inventory batches read during this step.
    #[serde(
        default,
        rename = "observedInventoryBatches",
        skip_serializing_if = "Vec::is_empty"
    )]
    pub observed_inventory_batches: Vec<ObservedInventoryBatch>,
}

pub(crate) fn is_false(b: &bool) -> bool {
    !*b
}

/// Answers for inputs gating Frozen resources, keyed by input id.
pub type GateAnswers = IndexMap<String, bool>;

/// Oldest deployment protocol version this binary can read.
pub const MIN_SUPPORTED_DEPLOYMENT_PROTOCOL_VERSION: u32 = 1;

/// Deployment protocol written by actors with resumable protection, CORS and
/// database update states and retained deletion. Older readers must be fenced
/// before these states exist.
pub const CURRENT_DEPLOYMENT_PROTOCOL_VERSION: u32 = 4;

/// Latest state protocol this binary can execute.
pub const MAX_SUPPORTED_DEPLOYMENT_PROTOCOL_VERSION: u32 = 4;

/// GCP federation changes setup ownership and target credential semantics.
pub const GCP_FEDERATION_DEPLOYMENT_PROTOCOL_VERSION: u32 = 2;

pub fn deployment_protocol_version_for_platform(
    _platform: crate::Platform,
    _base_platform: Option<crate::Platform>,
) -> u32 {
    CURRENT_DEPLOYMENT_PROTOCOL_VERSION
}

/// Accept unchanged reports or the explicit upgrade to this writer protocol.
/// Never permit downgrade or unknown protocols. Provider setup prerequisites
/// remain separate from this checkpoint compatibility check.
pub fn accepts_deployment_protocol_report(registered: u32, reported: u32) -> bool {
    (MIN_SUPPORTED_DEPLOYMENT_PROTOCOL_VERSION..=MAX_SUPPORTED_DEPLOYMENT_PROTOCOL_VERSION)
        .contains(&registered)
        && (reported == registered
            || (registered < CURRENT_DEPLOYMENT_PROTOCOL_VERSION
                && reported == CURRENT_DEPLOYMENT_PROTOCOL_VERSION))
}

/// Backwards-compatible alias for older call sites.
pub const DEPLOYMENT_PROTOCOL_VERSION: u32 = CURRENT_DEPLOYMENT_PROTOCOL_VERSION;

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{Platform, ReleaseInfo, Stack, StackState};
    use indexmap::IndexMap;

    fn empty_stack() -> Stack {
        Stack {
            id: "stack_test".to_string(),
            resources: IndexMap::new(),
            inputs: vec![],
            dynamic_container_repositories: Vec::new(),
            dynamic_container_image_resources: Vec::new(),
            operations: None,
            permissions: crate::PermissionsConfig::default(),
            supported_platforms: None,
        }
    }

    fn release_info(id: &str) -> ReleaseInfo {
        ReleaseInfo {
            release_id: Some(id.to_string()),
            version: None,
            description: None,
            stack: empty_stack(),
        }
    }

    fn state() -> DeploymentState {
        DeploymentState {
            status: DeploymentStatus::Pending,
            platform: Platform::Kubernetes,
            current_release: None,
            target_release: None,
            stack_state: None,
            error: None,
            environment_info: None,
            runtime_metadata: None,
            retry_requested: false,
            protocol_version: DEPLOYMENT_PROTOCOL_VERSION,
        }
    }

    #[test]
    fn deployment_state_has_desired_when_release_or_stack_state_exists() {
        let observe_only = state();
        assert!(!observe_only.has_desired());

        let mut current = state();
        current.current_release = Some(release_info("rel_current"));
        assert!(current.has_desired());

        let mut target = state();
        target.target_release = Some(release_info("rel_target"));
        assert!(target.has_desired());

        let mut imported = state();
        imported.stack_state = Some(StackState::new(Platform::Kubernetes));
        assert!(imported.has_desired());
    }

    #[test]
    fn runtime_metadata_from_before_secret_inventory_defaults_to_empty() {
        let metadata: RuntimeMetadata = serde_json::from_value(serde_json::json!({
            "lastSyncedEnvVarsHash": "old-hash"
        }))
        .expect("old runtime metadata remains readable");

        assert_eq!(
            metadata.last_synced_env_vars_hash.as_deref(),
            Some("old-hash")
        );
        assert!(metadata.last_synced_secret_names.is_empty());
        assert!(!metadata.vault_credentials_revoked);
        assert!(metadata.retained_storage_protection_hash.is_none());
        assert!(metadata.retained_storage_protection_confirmed_at.is_none());
        assert!(metadata.pending_prepared_stack.is_none());
        assert!(metadata.setup_update_authorization.is_none());
        assert!(metadata.setup_scaffolding.is_empty());
    }

    /// Teardown reads this record back from persisted state, so its wire form is a contract.
    #[test]
    fn setup_scaffolding_round_trips_in_its_persisted_form() {
        let persisted = serde_json::json!({
            "initialSetupAuthority": "directSetup",
            "setupScaffolding": {
                "agents": { "type": "awsSandbox", "buildRoleName": "acme-agents-build" }
            }
        });
        let metadata: RuntimeMetadata =
            serde_json::from_value(persisted.clone()).expect("persisted record reads");
        assert_eq!(
            metadata.setup_scaffolding["agents"],
            SetupScaffolding::AwsSandbox {
                build_role_name: "acme-agents-build".to_string(),
                egress: None,
                image_arn: None,
            }
        );
        assert_eq!(metadata.deletion_policy, DeletionPolicy::Retain);
        let mut expected = persisted;
        expected["deletionPolicy"] = serde_json::json!("retain");
        assert_eq!(serde_json::to_value(&metadata).unwrap(), expected);
        assert!(
            serde_json::to_value(RuntimeMetadata::default()).unwrap()["setupScaffolding"].is_null(),
            "state with no scaffolding serializes as it did before the field existed"
        );
    }

    #[test]
    fn filling_a_record_takes_only_what_it_lacks() {
        let egress = |group: Option<&str>, connector: Option<&str>| AwsSandboxEgressScaffolding {
            operator_role_name: "acme-agents-egress".to_string(),
            security_group_id: group.map(str::to_string),
            connector_arn: connector.map(str::to_string),
            connector_request: None,
        };
        let record = |egress| SetupScaffolding::AwsSandbox {
            build_role_name: "acme-agents-build".to_string(),
            egress,
            image_arn: None,
        };

        let mut partial = record(Some(egress(Some("sg-recorded"), None)));
        partial.fill_missing(record(Some(egress(Some("sg-found"), Some("arn:found")))));
        assert_eq!(
            partial,
            record(Some(egress(Some("sg-recorded"), Some("arn:found")))),
            "a recorded id is the one teardown deletes"
        );

        let mut role_only = record(None);
        role_only.fill_missing(record(Some(egress(Some("sg-found"), None))));
        assert_eq!(role_only, record(Some(egress(Some("sg-found"), None))));

        let image = |arn: Option<&str>| SetupScaffolding::AwsSandbox {
            build_role_name: "acme-agents-build".to_string(),
            egress: None,
            image_arn: arn.map(str::to_string),
        };
        let mut without_image = image(None);
        without_image.fill_missing(image(Some("arn:found")));
        assert_eq!(without_image, image(Some("arn:found")));
        let mut with_image = image(Some("arn:recorded"));
        with_image.fill_missing(image(Some("arn:found")));
        assert_eq!(with_image, image(Some("arn:recorded")));
    }

    /// A deny sandbox's record is written piece by piece, so a partial one must read back as is.
    #[test]
    fn a_partial_egress_record_round_trips_in_its_persisted_form() {
        let persisted = serde_json::json!({
            "type": "awsSandbox",
            "buildRoleName": "acme-agents-build",
            "egress": {
                "operatorRoleName": "acme-agents-egress",
                "securityGroupId": "sg-0123"
            }
        });
        let record: SetupScaffolding =
            serde_json::from_value(persisted.clone()).expect("persisted record reads");
        assert_eq!(
            record,
            SetupScaffolding::AwsSandbox {
                build_role_name: "acme-agents-build".to_string(),
                egress: Some(AwsSandboxEgressScaffolding {
                    operator_role_name: "acme-agents-egress".to_string(),
                    security_group_id: Some("sg-0123".to_string()),
                    connector_arn: None,
                    connector_request: None,
                }),
                image_arn: None,
            }
        );
        assert_eq!(serde_json::to_value(&record).unwrap(), persisted);
    }
}