greentic-deployer-dev 1.1.27501952916

Greentic deployer runtime for plan construction and deployment-pack dispatch
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
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
//! `ensure_local_environment` (`A4` of `plans/next-gen-deployment.md`).
//!
//! Idempotent bootstrap of the `local` [`Environment`] with the five default
//! [`greentic_deploy_spec::EnvPackBinding`]s (deployer/secrets/telemetry/sessions/state).
//! Invoked by `gtc setup` and `gtc start` before any operator-state read so
//! first-run installs always have an env to bind against.
//!
//! Heavy resolution of the descriptor strings to concrete handlers is the
//! env-pack registry's job (A9); A4 only persists the binding intent.

use chrono::Utc;
use greentic_deploy_spec::{
    CapabilitySlot, EnvId, Environment, EnvironmentRuntime, PackDescriptor, SchemaVersion,
};
use serde_json::Value;
use std::collections::BTreeMap;

use crate::defaults::{LOCAL_DEPLOYER_PACK, LOCAL_ENV_ID};
use crate::env_packs::LocalProcessDeployerHandler;
// Re-export so `cli::env` can reference `super::bootstrap::LocalEnvOutcome`
// as before (the enum moved from this module to `environment::bootstrap`).
pub use crate::environment::LocalEnvOutcome;
use crate::environment::{EnsureLocalEnvironmentPayload, LocalFsStore, Locked};

use super::{OpError, map_store_err_preserving_noun};

/// Creates the `local` Environment with default env-pack bindings if absent.
///
/// Idempotent: callers may invoke this on every `gtc setup` / `gtc start`
/// without checking first. Returns `(Environment, LocalEnvOutcome)` so
/// consumers can log a one-line note on creation and stay silent otherwise.
///
/// `public_base_url`: when `Some`, persisted on the env's `host_config` ONLY
/// during creation (so a `Created` outcome carries it). For `AlreadyExists`
/// and `Healed` outcomes the existing URL is preserved — use
/// `op env set-public-url` (or `op config set --public-url`) to overwrite it.
/// The value is NOT validated here; callers (the `op env init` dispatcher)
/// must run [`greentic_deploy_spec::validate_public_base_url`] before passing
/// it in.
///
/// Delegates to [`LocalFsStore::ensure_local_environment`] for the atomic
/// read-modify-write, then runs `refresh_local_runtime_stub` at the CLI
/// layer.
pub fn ensure_local_environment(
    store: &LocalFsStore,
    public_base_url: Option<String>,
) -> Result<(Environment, LocalEnvOutcome), OpError> {
    let env_id = EnvId::try_from(LOCAL_ENV_ID).map_err(|e| {
        OpError::InvalidArgument(format!("default env id `{}`: {}", LOCAL_ENV_ID, e))
    })?;
    let payload = EnsureLocalEnvironmentPayload { public_base_url };
    let (env, outcome) = store
        .ensure_local_environment(&env_id, payload)
        .map_err(map_store_err_preserving_noun)?;
    // Runtime stub refresh runs outside the typed verb's flock. The tiny
    // race window is acceptable — the stub is a derived projection that
    // self-heals on every bootstrap call.
    let force_bump = !matches!(outcome, LocalEnvOutcome::AlreadyExists);
    store.transact(&env_id, |locked| -> Result<(), OpError> {
        refresh_local_runtime_stub(locked, &env, force_bump)?;
        Ok(())
    })?;
    Ok((env, outcome))
}

/// Refresh (or create) the local-process deployer's `runtime.json` stub.
/// `force_bump = true` (Create/Heal): always write with a bumped generation.
/// `force_bump = false` (AlreadyExists): write only when `listen_addr` is
/// stale or the file is absent. Existing `discovered` keys are preserved;
/// only `listen_addr` is upserted.
///
/// TODO(phase-d): replace with `EnvPackHandler::report_runtime_config()` on
/// `LocalProcessDeployerHandler` once the trait method lands (see
/// `plans/next-gen-deployment.md` line 1406).
fn refresh_local_runtime_stub(
    locked: &Locked<'_>,
    env: &Environment,
    force_bump: bool,
) -> Result<(), OpError> {
    // Gate 1: env Deployer binding must be local-process — otherwise this
    // helper is not the authoritative producer.
    let is_local = env
        .pack_for_slot(CapabilitySlot::Deployer)
        .is_some_and(|b| b.kind.path() == LocalProcessDeployerHandler::DESCRIPTOR_PATH);
    if !is_local {
        return Ok(());
    }

    let existing = locked.load_runtime()?;

    // Gate 2: a foreign producer's runtime.json is left untouched.
    if let Some(ref rt) = existing
        && rt.generated_by.path() != LocalProcessDeployerHandler::DESCRIPTOR_PATH
    {
        return Ok(());
    }

    // No-op fast path for the dominant AlreadyExists case: check the one key
    // we own BEFORE cloning the discovered map.
    let new_addr_str = env.host_config.resolved_listen_addr().to_string();
    if !force_bump
        && let Some(ref rt) = existing
        && rt.discovered.get("listen_addr") == Some(&Value::String(new_addr_str.clone()))
    {
        return Ok(());
    }

    let prev_generation = existing.as_ref().map(|r| r.generation).unwrap_or(0);
    let mut desired_discovered = existing.map(|r| r.discovered).unwrap_or_default();
    desired_discovered.insert("listen_addr".to_string(), Value::String(new_addr_str));

    locked
        .save_runtime(&build_local_runtime_stub(
            &env.environment_id,
            prev_generation + 1,
            desired_discovered,
        )?)
        .map_err(Into::into)
}

fn build_local_runtime_stub(
    env_id: &EnvId,
    generation: u64,
    discovered: BTreeMap<String, Value>,
) -> Result<EnvironmentRuntime, OpError> {
    let generated_by = PackDescriptor::try_new(LOCAL_DEPLOYER_PACK).map_err(|e| {
        OpError::InvalidArgument(format!(
            "local-process descriptor `{}`: {}",
            LOCAL_DEPLOYER_PACK, e
        ))
    })?;
    Ok(EnvironmentRuntime {
        schema: SchemaVersion::new(SchemaVersion::ENVIRONMENT_RUNTIME_V1),
        environment_id: env_id.clone(),
        discovered,
        generated_at: Utc::now(),
        generated_by,
        generation,
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::defaults::{
        LOCAL_DEPLOYER_PACK, LOCAL_SECRETS_PACK, LOCAL_SESSIONS_PACK, LOCAL_STATE_PACK,
        LOCAL_TELEMETRY_PACK,
    };
    use greentic_deploy_spec::{CapabilitySlot, DEFAULT_LISTEN_ADDR, EnvironmentHostConfig};
    use tempfile::TempDir;

    fn store() -> (TempDir, LocalFsStore) {
        let tmp = TempDir::new().expect("tempdir");
        let store = LocalFsStore::new(tmp.path().to_path_buf());
        (tmp, store)
    }

    #[test]
    fn creates_local_env_when_missing() {
        let (_tmp, store) = store();
        let (env, outcome) = ensure_local_environment(&store, None).expect("bootstrap");
        assert_eq!(outcome, LocalEnvOutcome::Created);
        assert_eq!(env.environment_id.as_str(), LOCAL_ENV_ID);
        assert_eq!(env.name, LOCAL_ENV_ID);
        assert_eq!(env.packs.len(), 5);
        env.validate().expect("env is spec-valid");
    }

    #[test]
    fn returns_existing_env_on_second_call() {
        let (_tmp, store) = store();
        let (first, first_outcome) =
            ensure_local_environment(&store, None).expect("first bootstrap");
        assert_eq!(first_outcome, LocalEnvOutcome::Created);
        let (second, second_outcome) =
            ensure_local_environment(&store, None).expect("second bootstrap");
        assert_eq!(second_outcome, LocalEnvOutcome::AlreadyExists);
        assert_eq!(first, second);
    }

    #[test]
    fn default_bindings_cover_expected_slots_and_descriptors() {
        let (_tmp, store) = store();
        let (env, _) = ensure_local_environment(&store, None).expect("bootstrap");
        let by_slot: std::collections::BTreeMap<CapabilitySlot, &str> = env
            .packs
            .iter()
            .map(|b| (b.slot, b.kind.as_str()))
            .collect();
        assert_eq!(
            by_slot.get(&CapabilitySlot::Deployer).copied(),
            Some(LOCAL_DEPLOYER_PACK)
        );
        assert_eq!(
            by_slot.get(&CapabilitySlot::Secrets).copied(),
            Some(LOCAL_SECRETS_PACK)
        );
        assert_eq!(
            by_slot.get(&CapabilitySlot::Telemetry).copied(),
            Some(LOCAL_TELEMETRY_PACK)
        );
        assert_eq!(
            by_slot.get(&CapabilitySlot::Sessions).copied(),
            Some(LOCAL_SESSIONS_PACK)
        );
        assert_eq!(
            by_slot.get(&CapabilitySlot::State).copied(),
            Some(LOCAL_STATE_PACK)
        );
        assert!(!by_slot.contains_key(&CapabilitySlot::Revocation));
    }

    #[test]
    fn bootstrap_env_has_no_bundles_or_revisions_or_splits() {
        let (_tmp, store) = store();
        let (env, _) = ensure_local_environment(&store, None).expect("bootstrap");
        assert!(env.bundles.is_empty());
        assert!(env.revisions.is_empty());
        assert!(env.traffic_splits.is_empty());
        assert!(env.credentials_ref.is_none());
    }

    #[test]
    fn bootstrap_env_host_config_has_no_region_or_org() {
        let (_tmp, store) = store();
        let (env, _) = ensure_local_environment(&store, None).expect("bootstrap");
        assert_eq!(env.host_config.env_id, env.environment_id);
        assert!(env.host_config.region.is_none());
        assert!(env.host_config.tenant_org_id.is_none());
    }

    #[test]
    fn bootstrap_env_writes_default_listen_addr_so_start_can_resolve_it() {
        let (_tmp, store) = store();
        let (env, outcome) = ensure_local_environment(&store, None).expect("bootstrap");
        assert_eq!(outcome, LocalEnvOutcome::Created);
        assert_eq!(
            env.host_config.listen_addr,
            Some(DEFAULT_LISTEN_ADDR),
            "fresh env must carry the canonical loopback default so `gtc start` \
             on an empty env has a deterministic bind address",
        );
        assert_eq!(env.host_config.resolved_listen_addr(), DEFAULT_LISTEN_ADDR);
    }

    #[test]
    fn bootstrap_heal_path_preserves_user_set_listen_addr() {
        use crate::environment::EnvironmentStore;
        use std::net::{IpAddr, Ipv4Addr, SocketAddr};
        let (_tmp, store) = store();
        let (mut env, _) = ensure_local_environment(&store, None).expect("bootstrap");
        let custom = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 9090);
        env.host_config.listen_addr = Some(custom);
        store.save(&env).expect("user save");
        let (reloaded, outcome) = ensure_local_environment(&store, None).expect("second bootstrap");
        assert_eq!(outcome, LocalEnvOutcome::AlreadyExists);
        assert_eq!(
            reloaded.host_config.listen_addr,
            Some(custom),
            "user's custom bind must survive re-bootstrap",
        );
    }

    #[test]
    fn second_call_does_not_overwrite_user_mutations() {
        use crate::environment::EnvironmentStore;
        let (_tmp, store) = store();
        let (mut env, _) = ensure_local_environment(&store, None).expect("bootstrap");
        env.name = "user-renamed".to_string();
        env.host_config.region = Some("eu-west-1".to_string());
        store.save(&env).expect("user save");
        let (reloaded, outcome) = ensure_local_environment(&store, None).expect("second bootstrap");
        assert_eq!(outcome, LocalEnvOutcome::AlreadyExists);
        assert_eq!(reloaded.name, "user-renamed");
        assert_eq!(reloaded.host_config.region.as_deref(), Some("eu-west-1"));
    }

    // Helpers for the heal-path tests below.
    use crate::environment::EnvironmentStore;
    use greentic_deploy_spec::{EnvPackBinding, PackDescriptor, PackId};

    /// Seed an empty `local` env (all 5 slots missing) — mimics the state a
    /// user lands in after running `gtc op env create local` (A3), which
    /// creates an env with `packs = Vec::new()`.
    fn seed_empty_local_env(store: &LocalFsStore) -> Environment {
        let env_id = EnvId::try_from(LOCAL_ENV_ID).unwrap();
        let env = Environment {
            schema: SchemaVersion::new(SchemaVersion::ENVIRONMENT_V1),
            environment_id: env_id.clone(),
            name: LOCAL_ENV_ID.to_string(),
            host_config: EnvironmentHostConfig {
                env_id,
                region: None,
                tenant_org_id: None,
                listen_addr: None,
                public_base_url: None,
            },
            packs: Vec::new(),
            credentials_ref: None,
            bundles: Vec::new(),
            revisions: Vec::new(),
            traffic_splits: Vec::new(),
            messaging_endpoints: Vec::new(),
            extensions: Vec::new(),
            revocation: Default::default(),
            retention: Default::default(),
            health: Default::default(),
        };
        store.save(&env).expect("seed save");
        env
    }

    fn custom_binding(slot: CapabilitySlot, descriptor: &str) -> EnvPackBinding {
        EnvPackBinding {
            slot,
            kind: PackDescriptor::try_new(descriptor).expect("valid descriptor"),
            pack_ref: PackId::new(descriptor),
            answers_ref: None,
            generation: 0,
            previous_binding_ref: None,
        }
    }

    #[test]
    fn heals_existing_env_with_no_packs() {
        let (_tmp, store) = store();
        seed_empty_local_env(&store);
        let (env, outcome) = ensure_local_environment(&store, None).expect("bootstrap heal");
        match outcome {
            LocalEnvOutcome::Healed { added_slots } => {
                assert_eq!(
                    added_slots,
                    vec![
                        CapabilitySlot::Deployer,
                        CapabilitySlot::Secrets,
                        CapabilitySlot::Telemetry,
                        CapabilitySlot::Sessions,
                        CapabilitySlot::State,
                    ],
                    "all 5 default slots should be reported as added"
                );
            }
            other => panic!("expected Healed, got {other:?}"),
        }
        assert_eq!(env.packs.len(), 5);
        env.validate().expect("env is spec-valid after heal");
        // Persisted: second call sees a fully-bound env and reports AlreadyExists.
        let (_again, outcome2) = ensure_local_environment(&store, None).expect("second bootstrap");
        assert_eq!(outcome2, LocalEnvOutcome::AlreadyExists);
    }

    #[test]
    fn heals_existing_env_with_partial_packs() {
        let (_tmp, store) = store();
        let mut env = seed_empty_local_env(&store);
        // Pre-seed only the deployer slot; expect bootstrap to add the other 4.
        env.packs.push(custom_binding(
            CapabilitySlot::Deployer,
            LOCAL_DEPLOYER_PACK,
        ));
        store.save(&env).expect("partial save");

        let (env, outcome) = ensure_local_environment(&store, None).expect("bootstrap heal");
        match outcome {
            LocalEnvOutcome::Healed { added_slots } => {
                assert_eq!(
                    added_slots,
                    vec![
                        CapabilitySlot::Secrets,
                        CapabilitySlot::Telemetry,
                        CapabilitySlot::Sessions,
                        CapabilitySlot::State,
                    ],
                    "only the 4 missing slots should be reported as added"
                );
            }
            other => panic!("expected Healed, got {other:?}"),
        }
        assert_eq!(env.packs.len(), 5);
        env.validate()
            .expect("env is spec-valid after partial heal");
    }

    #[test]
    fn heal_preserves_user_bound_non_default_descriptor() {
        let (_tmp, store) = store();
        let mut env = seed_empty_local_env(&store);
        // User bound `secrets` to a custom backend; bootstrap must NOT overwrite.
        let custom_secrets = "greentic.secrets.aws-secrets-manager@1.0.0";
        env.packs
            .push(custom_binding(CapabilitySlot::Secrets, custom_secrets));
        store.save(&env).expect("custom-secrets save");

        let (env, outcome) = ensure_local_environment(&store, None).expect("bootstrap heal");
        match outcome {
            LocalEnvOutcome::Healed { added_slots } => {
                assert_eq!(
                    added_slots,
                    vec![
                        CapabilitySlot::Deployer,
                        CapabilitySlot::Telemetry,
                        CapabilitySlot::Sessions,
                        CapabilitySlot::State,
                    ],
                    "secrets must NOT be re-added; the 4 other defaults fill the gaps"
                );
            }
            other => panic!("expected Healed, got {other:?}"),
        }
        let secrets_descriptor = env
            .packs
            .iter()
            .find(|b| b.slot == CapabilitySlot::Secrets)
            .map(|b| b.kind.as_str())
            .expect("secrets slot present");
        assert_eq!(
            secrets_descriptor, custom_secrets,
            "user's custom secrets descriptor must survive bootstrap"
        );
        assert_eq!(env.packs.len(), 5);
    }

    #[test]
    fn fully_bound_env_yields_already_exists_with_no_healing() {
        let (_tmp, store) = store();
        // First call creates the env with all 5 defaults; second call must NOT
        // report any healing — env is already in the required shape.
        ensure_local_environment(&store, None).expect("first bootstrap");
        let (_env, outcome) = ensure_local_environment(&store, None).expect("second bootstrap");
        assert_eq!(outcome, LocalEnvOutcome::AlreadyExists);
    }

    #[test]
    fn create_writes_runtime_stub_with_listen_addr() {
        let (_tmp, store) = store();
        let (env, _) = ensure_local_environment(&store, None).expect("bootstrap");
        let runtime = store
            .load_runtime(&env.environment_id)
            .expect("load runtime")
            .expect("runtime.json must exist after first bootstrap");
        assert_eq!(runtime.environment_id, env.environment_id);
        assert_eq!(
            runtime.schema.as_str(),
            SchemaVersion::ENVIRONMENT_RUNTIME_V1
        );
        assert_eq!(runtime.generation, 1);
        assert_eq!(runtime.generated_by.as_str(), LOCAL_DEPLOYER_PACK);
        let listen_addr = runtime
            .discovered
            .get("listen_addr")
            .expect("discovered must seed listen_addr for runtime:// resolution");
        assert_eq!(
            listen_addr.as_str(),
            Some(env.host_config.resolved_listen_addr().to_string().as_str()),
        );
    }

    #[test]
    fn already_exists_preserves_runtime_stub_and_skips_rewrite() {
        let (_tmp, store) = store();
        ensure_local_environment(&store, None).expect("first bootstrap");
        let env_id = EnvId::try_from(LOCAL_ENV_ID).unwrap();
        let first = store
            .load_runtime(&env_id)
            .expect("load runtime")
            .expect("runtime.json exists");
        ensure_local_environment(&store, None).expect("second bootstrap");
        let second = store
            .load_runtime(&env_id)
            .expect("load runtime")
            .expect("runtime.json exists");
        // An idempotent re-bootstrap must NOT churn the runtime stub — the
        // file is identical (same generation + same generated_at). This is
        // what the C5 watcher relies on to avoid spurious reloads on every
        // `gtc start`.
        assert_eq!(
            first.generation, second.generation,
            "generation must not bump on AlreadyExists"
        );
        assert_eq!(
            first.generated_at, second.generated_at,
            "generated_at must not refresh on AlreadyExists"
        );
    }

    #[test]
    fn already_exists_writes_runtime_stub_when_absent() {
        let (_tmp, store) = store();
        let (_env, _) = ensure_local_environment(&store, None).expect("first bootstrap");
        let env_id = EnvId::try_from(LOCAL_ENV_ID).unwrap();
        // Simulate an env that was created before the C5 stub-producer landed
        // (or where an operator manually deleted runtime.json): delete the
        // file under the env-store layout and re-run bootstrap.
        let runtime_path = store
            .env_lock_path(&env_id)
            .map(|p| {
                p.parent()
                    .expect("lock path has parent")
                    .join("runtime.json")
            })
            .expect("runtime path");
        std::fs::remove_file(&runtime_path).expect("remove runtime.json");
        assert!(!runtime_path.exists());

        let (_env, outcome) = ensure_local_environment(&store, None).expect("second bootstrap");
        assert_eq!(outcome, LocalEnvOutcome::AlreadyExists);
        let runtime = store
            .load_runtime(&env_id)
            .expect("load runtime")
            .expect("runtime.json must be re-emitted by the AlreadyExists path");
        assert_eq!(runtime.generation, 1);
    }

    #[test]
    fn heal_writes_runtime_stub() {
        let (_tmp, store) = store();
        seed_empty_local_env(&store);
        let env_id = EnvId::try_from(LOCAL_ENV_ID).unwrap();
        assert!(
            store.load_runtime(&env_id).expect("load runtime").is_none(),
            "seeded env should have no runtime.json yet",
        );
        let (_env, outcome) = ensure_local_environment(&store, None).expect("bootstrap heal");
        assert!(matches!(outcome, LocalEnvOutcome::Healed { .. }));
        let runtime = store
            .load_runtime(&env_id)
            .expect("load runtime")
            .expect("heal arm must emit runtime.json");
        assert_eq!(runtime.generation, 1);
    }

    #[test]
    fn heal_bumps_runtime_stub_generation_when_present() {
        let (_tmp, store) = store();
        // First bootstrap creates env + writes generation=1.
        ensure_local_environment(&store, None).expect("first bootstrap");
        let env_id = EnvId::try_from(LOCAL_ENV_ID).unwrap();
        // Strip one of the default bindings so the next bootstrap call falls
        // into the Heal arm and re-emits the stub.
        let mut env = store.load(&env_id).expect("load");
        env.packs.retain(|b| b.slot != CapabilitySlot::Telemetry);
        store.save(&env).expect("user save");

        let (_env, outcome) = ensure_local_environment(&store, None).expect("heal bootstrap");
        assert!(matches!(outcome, LocalEnvOutcome::Healed { .. }));
        let runtime = store
            .load_runtime(&env_id)
            .expect("load runtime")
            .expect("runtime.json still present");
        assert_eq!(
            runtime.generation, 2,
            "Heal arm must bump generation against the previous stub",
        );
    }

    #[test]
    fn heal_preserves_existing_discovered_keys() {
        let (_tmp, store) = store();
        // First bootstrap to create env + runtime stub (generation=1).
        ensure_local_environment(&store, None).expect("first bootstrap");
        let env_id = EnvId::try_from(LOCAL_ENV_ID).unwrap();

        // Inject extra discovered keys into the existing runtime.json,
        // simulating a Phase-D deployer that added `alb_dns`.
        let mut runtime = store.load_runtime(&env_id).expect("load").expect("exists");
        runtime
            .discovered
            .insert("alb_dns".to_string(), Value::String("ALB".to_string()));
        store.save_runtime(&runtime).expect("save patched runtime");

        // Strip one default binding so the next bootstrap falls into Heal.
        let mut env = store.load(&env_id).expect("load env");
        env.packs.retain(|b| b.slot != CapabilitySlot::Telemetry);
        store.save(&env).expect("user save");

        let (healed_env, outcome) = ensure_local_environment(&store, None).expect("heal bootstrap");
        assert!(matches!(outcome, LocalEnvOutcome::Healed { .. }));

        let refreshed = store.load_runtime(&env_id).expect("load").expect("exists");
        assert_eq!(
            refreshed.discovered.get("alb_dns").and_then(Value::as_str),
            Some("ALB"),
            "pre-existing discovered keys must survive Heal",
        );
        assert_eq!(
            refreshed
                .discovered
                .get("listen_addr")
                .and_then(Value::as_str),
            Some(
                healed_env
                    .host_config
                    .resolved_listen_addr()
                    .to_string()
                    .as_str()
            ),
        );
        assert!(
            refreshed.generation > runtime.generation,
            "generation must bump on Heal",
        );
    }

    #[test]
    fn heal_skips_runtime_stub_when_deployer_binding_is_not_local_process() {
        let (_tmp, store) = store();
        let mut env = seed_empty_local_env(&store);
        // Bind the Deployer slot to a non-local-process descriptor.
        env.packs.push(custom_binding(
            CapabilitySlot::Deployer,
            "greentic.deployer.aws-ecs@1.0.0",
        ));
        store.save(&env).expect("save env with aws deployer");

        // Pre-seed runtime.json with the AWS deployer as generated_by.
        let aws_descriptor = PackDescriptor::try_new("greentic.deployer.aws-ecs@1.0.0").unwrap();
        let mut aws_discovered = BTreeMap::new();
        aws_discovered.insert("alb_dns".to_string(), Value::String("ALB".to_string()));
        let aws_runtime = EnvironmentRuntime {
            schema: SchemaVersion::new(SchemaVersion::ENVIRONMENT_RUNTIME_V1),
            environment_id: env.environment_id.clone(),
            discovered: aws_discovered,
            generated_at: Utc::now(),
            generated_by: aws_descriptor,
            generation: 5,
        };
        store.save_runtime(&aws_runtime).expect("save aws runtime");

        // Bootstrap — should heal missing slots but NOT touch runtime.json.
        let (_env, outcome) = ensure_local_environment(&store, None).expect("heal");
        assert!(matches!(outcome, LocalEnvOutcome::Healed { .. }));

        let after = store
            .load_runtime(&env.environment_id)
            .expect("load")
            .expect("exists");
        assert_eq!(
            after.generation, aws_runtime.generation,
            "generation must not change — foreign producer owns the file",
        );
        assert_eq!(
            after.generated_at, aws_runtime.generated_at,
            "generated_at must not change",
        );
        assert_eq!(
            after.discovered, aws_runtime.discovered,
            "discovered must be untouched",
        );
    }

    #[test]
    fn already_exists_refreshes_stale_listen_addr() {
        use std::net::{IpAddr, Ipv4Addr, SocketAddr};
        let (_tmp, store) = store();
        // First bootstrap writes env + runtime stub with default listen_addr.
        ensure_local_environment(&store, None).expect("first bootstrap");
        let env_id = EnvId::try_from(LOCAL_ENV_ID).unwrap();
        let before = store.load_runtime(&env_id).expect("load").expect("exists");

        // Mutate listen_addr on the persisted env (simulates `op config set`).
        let mut env = store.load(&env_id).expect("load env");
        let custom = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 9090);
        env.host_config.listen_addr = Some(custom);
        store.save(&env).expect("user save");

        // Second bootstrap → AlreadyExists, but listen_addr is now stale.
        let (_env, outcome) = ensure_local_environment(&store, None).expect("second bootstrap");
        assert_eq!(outcome, LocalEnvOutcome::AlreadyExists);

        let after = store.load_runtime(&env_id).expect("load").expect("exists");
        assert_eq!(
            after.discovered.get("listen_addr").and_then(Value::as_str),
            Some(custom.to_string().as_str()),
            "listen_addr must be refreshed to the new address",
        );
        assert_eq!(
            after.generation,
            before.generation + 1,
            "generation must bump by exactly 1",
        );
    }
}