stackless-core 0.4.0

Definition model, state store, and lifecycle engine for stackless
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
//! The `Substrate` trait — the one provider seam (ARCHITECTURE.md §8).
//!
//! Core never names a substrate; providers implement this trait and the
//! binary registers them by name. Everything per-substrate flows
//! through here: validation, capabilities, defaults, and the resource
//! operations the lifecycle engine drives (execute / observe /
//! destroy). Adding a provider must require zero changes to the engine
//! or state modules.

use std::collections::BTreeMap;
use std::time::Duration;

use serde::{Deserialize, Serialize};

use crate::def::{Namespace, StackDef};
use crate::engine::Step;
use crate::fault::{ErrorContext, Fault};
use crate::state::Checkpoint;

/// Structured spend data for cloud `--json` envelopes (§4).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SpendInfo {
    pub provider: String,
    pub cap_usd: u32,
    pub summary: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub data: Option<serde_json::Value>,
}

/// A substrate failure, flattened at the trait boundary so the §2
/// error contract (stable code + remediation) crosses it intact
/// whatever error enum the provider uses internally.
#[derive(Debug, thiserror::Error)]
#[error("{message}")]
pub struct SubstrateFault {
    pub code: Box<str>,
    pub message: String,
    pub remediation: String,
    pub context: Box<ErrorContext>,
}

impl SubstrateFault {
    pub fn from_fault(fault: &dyn Fault) -> Self {
        Self {
            code: fault.code().into(),
            message: fault.to_string(),
            remediation: fault.remediation(),
            context: Box::new(fault.context()),
        }
    }
}

impl Fault for SubstrateFault {
    fn code(&self) -> &str {
        &self.code
    }

    fn remediation(&self) -> String {
        self.remediation.clone()
    }

    fn context(&self) -> ErrorContext {
        self.context.as_ref().clone()
    }
}

/// Steps that perform work but create no destructible resource (hooks,
/// health gates) record this kind; teardown drops their checkpoints
/// without a destroy/observe round-trip.
pub const ACTION_RESOURCE_KIND: &str = "action";

pub fn action_resource(step_id: &str) -> StepResource {
    StepResource {
        resource_kind: ACTION_RESOURCE_KIND.into(),
        resource_id: step_id.to_owned(),
        payload: "{}".into(),
    }
}

pub fn present_or_gone(present: bool) -> Observation {
    if present {
        Observation::Present
    } else {
        Observation::Gone
    }
}

/// Which env resolution path is building a namespace.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NamespacePurpose {
    /// Service runtime env (Render: internal DB URLs).
    ServiceEnv,
    /// Operator-side prepare hooks (Render: external DB URLs).
    OperatorPrepare,
    /// `stackless verify` env resolution.
    Verify,
}

/// What a recorded resource looks like when re-checked against the
/// substrate (invariant 4: the manifest says where to look, the
/// substrate says what's true).
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Observation {
    Present,
    Drifted { settings: Vec<SettingDrift> },
    Gone,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SettingDrift {
    pub setting: String,
    pub expected: String,
    pub actual: String,
}

/// One service's recent logs as a substrate retrieved them, for the `logs`
/// verb. The substrate owns where the lines come from (a cloud API, a local
/// file); the CLI only renders them.
#[derive(Debug, Clone)]
pub struct ServiceLog {
    pub service: String,
    /// Provenance tag for `--json` output (e.g. `"render_api"`, `"file"`).
    pub source: &'static str,
    /// Local log file path, when the lines were read from disk.
    pub log_path: Option<String>,
    pub lines: Vec<String>,
}

/// What `execute` hands back for the journal: the resource the step
/// created (or re-affirmed), recorded before the engine proceeds.
#[derive(Debug, Clone)]
pub struct StepResource {
    pub resource_kind: String,
    pub resource_id: String,
    /// Substrate-specific JSON needed to re-find the resource later.
    pub payload: String,
}

/// Identity and recorded handles for one instance. Names are user-facing;
/// resource namespaces belong to one birth and survive retries.
#[derive(Debug, Clone, Copy)]
pub struct InstanceContext<'a> {
    pub name: &'a str,
    pub id: &'a str,
    pub resource_namespace: &'a str,
    pub checkpoints: &'a [Checkpoint],
    /// Cross-provider URLs supplied by the routing adapter. Missing outputs stay missing.
    pub routed_origins: Option<&'a BTreeMap<String, String>>,
}

impl<'a> InstanceContext<'a> {
    pub fn from_record(
        record: &'a crate::state::InstanceRecord,
        checkpoints: &'a [Checkpoint],
    ) -> Self {
        Self {
            name: record.name.as_str(),
            id: &record.instance_id,
            resource_namespace: &record.resource_namespace,
            checkpoints,
            routed_origins: None,
        }
    }

    pub fn bind_namespace(&self, namespace: &mut Namespace, def: &StackDef) {
        if let Some(origins) = self.routed_origins {
            namespace.service_origins = origins.clone();
        }
        namespace.bind_endpoints(def);
    }

    /// Stripe resource name, bounded to 52 bytes for new identities.
    pub fn resource_name(&self, logical_name: &str) -> String {
        namespaced_resource_name(self.resource_namespace, logical_name)
    }

    /// Preserve legacy provider names until those instances are destroyed.
    pub fn provider_resource_name(&self, stack: &str, logical_name: &str) -> String {
        if self.resource_namespace == self.name {
            format!("{stack}-{}-{logical_name}", self.name)
        } else {
            self.resource_name(logical_name)
        }
    }
}

pub fn namespaced_resource_name(namespace: &str, logical_name: &str) -> String {
    use sha2::{Digest, Sha256};
    if namespace.len() == 35
        && namespace.starts_with("sl-")
        && namespace[3..].bytes().all(|b| b.is_ascii_hexdigit())
    {
        let digest = Sha256::digest(logical_name.as_bytes());
        let suffix: String = digest[..8].iter().map(|b| format!("{b:02x}")).collect();
        format!("{namespace}-{suffix}")
    } else {
        format!("{namespace}-{logical_name}")
    }
}

/// Everything a substrate gets to execute one step.
#[derive(Debug)]
pub struct StepContext<'a> {
    /// Stable across controller recovery of the same accepted operation.
    pub operation_id: &'a str,
    pub store: &'a crate::state::Store,
    pub instance: &'a InstanceContext<'a>,
    pub def: &'a StackDef,
    pub step: &'a Step,
    /// Recorded `--source` pins (service → path), local-only.
    pub source_overrides: &'a BTreeMap<String, String>,
    /// Snapshot `--source` pins into instance-owned space instead of using
    /// them in place.
    pub dirty: bool,
    /// Checkpoints recorded so far, in order — earlier steps' resources
    /// (ports, paths, connection strings) live here.
    pub prior: &'a [Checkpoint],
    pub parent_resources: &'a [&'a str],
    pub cancelled: Option<std::sync::Arc<std::sync::atomic::AtomicBool>>,
}

impl StepContext<'_> {
    pub fn is_cancelled(&self) -> bool {
        self.cancelled
            .as_ref()
            .is_some_and(|flag| flag.load(std::sync::atomic::Ordering::Acquire))
    }
}

#[async_trait::async_trait]
pub trait Substrate: Send + Sync {
    /// The name instances are bound to at creation (`--on <name>`).
    fn name(&self) -> &str;

    /// Route inventory records without granting ownership of their resources.
    fn can_manage_resource(&self, resource: &crate::state::ResourceRecord) -> bool {
        resource.provider == self.name()
    }

    fn capabilities(&self) -> crate::capabilities::Capabilities;

    fn execution_plan(
        &self,
        def: &StackDef,
    ) -> Result<crate::engine::plan::ExecutionPlan, crate::def::DefError> {
        def.execution_plan(self.name(), self.capabilities().early_origins)
    }

    fn supports_source_override_for(&self, _def: &StackDef, _service: &str) -> bool {
        self.supports_source_override()
    }

    fn validate(&self, def: &StackDef) -> Result<(), SubstrateFault> {
        if def
            .services
            .values()
            .any(|workload| workload.on.as_ref().is_some_and(|on| on != self.name()))
            || def
                .integrations
                .values()
                .any(|resource| resource.on.as_ref().is_some_and(|on| on != self.name()))
        {
            return Err(crate::capabilities::unsupported_feature(
                self.name(),
                "placement",
                "mixed provider placement",
            ));
        }
        self.capabilities().validate(self.name(), def)?;
        self.execution_plan(def)
            .map_err(|error| SubstrateFault::from_fault(&error))?;
        self.validate_definition(def)
    }

    /// Substrate-specific shape validation of the definition — core has
    /// already checked everything substrate-blind.
    fn validate_definition(&self, def: &StackDef) -> Result<(), SubstrateFault>;

    /// Whether `--source service=path` pins are allowed here. Local
    /// substrates say yes; deploy-from-ref substrates say no (§1).
    fn supports_source_override(&self) -> bool;

    /// Per-substrate lease default (§6).
    fn default_lease(&self) -> Duration;

    /// The origin `${services.X.origin}` resolves to for this substrate.
    fn service_origin(
        &self,
        def: &StackDef,
        instance: &InstanceContext<'_>,
        service: &str,
    ) -> String {
        self.build_namespace(
            def,
            instance,
            instance.checkpoints,
            &BTreeMap::new(),
            NamespacePurpose::ServiceEnv,
        )
        .service_origins
        .remove(service)
        .unwrap_or_default()
    }

    /// Build the interpolation namespace for one instance.
    fn build_namespace(
        &self,
        def: &StackDef,
        instance: &InstanceContext<'_>,
        prior: &[Checkpoint],
        secrets: &BTreeMap<String, String>,
        purpose: NamespacePurpose,
    ) -> Namespace;

    /// Execute one step, returning the resource for the journal.
    async fn execute(&self, ctx: StepContext<'_>) -> Result<StepResource, SubstrateFault>;

    fn step_revision(&self, ctx: &StepContext<'_>) -> Result<String, SubstrateFault> {
        crate::engine::revision::step_revision(ctx, self)
    }

    /// Whether a new operation must re-evaluate this step despite a matching revision.
    fn refresh_each_operation(&self, step: &Step) -> bool {
        matches!(
            step.kind,
            crate::engine::StepKind::Prepare | crate::engine::StepKind::HealthGate
        )
    }

    /// Apply changed inputs to an existing resource. Providers must preserve
    /// its ownership handle or explicitly replace and retire it in the journal.
    async fn reconcile(
        &self,
        ctx: StepContext<'_>,
        _previous: &Checkpoint,
    ) -> Result<StepResource, SubstrateFault> {
        self.execute(ctx).await
    }

    /// Re-check a recorded resource against reality.
    async fn observe(
        &self,
        instance: &InstanceContext<'_>,
        checkpoint: &Checkpoint,
    ) -> Result<Observation, SubstrateFault>;

    /// Destroy a recorded resource. Returning `Ok` is a claim the
    /// engine immediately verifies with `observe` — silence is not
    /// success (invariant 4).
    async fn destroy(
        &self,
        instance: &InstanceContext<'_>,
        checkpoint: &Checkpoint,
    ) -> Result<(), SubstrateFault>;

    /// Recover and destroy unfinished inventory without losing newly discovered handles.
    async fn destroy_record(
        &self,
        _store: &crate::state::Store,
        instance: &InstanceContext<'_>,
        resource: &crate::state::ResourceRecord,
    ) -> Result<(), SubstrateFault> {
        self.destroy(instance, &resource.checkpoint(instance.name))
            .await
    }

    async fn observe_record(
        &self,
        _store: &crate::state::Store,
        instance: &InstanceContext<'_>,
        resource: &crate::state::ResourceRecord,
    ) -> Result<Observation, SubstrateFault> {
        self.observe(instance, &resource.checkpoint(instance.name))
            .await
    }

    /// Rebuild controller routes from verified runtime handles after restart.
    async fn restore_routes(
        &self,
        _store: &crate::state::Store,
        _instance: &InstanceContext<'_>,
    ) -> Result<(), SubstrateFault> {
        Ok(())
    }

    /// Substrate-wide cleanup after verified teardown (e.g. delete a
    /// shared Stripe Projects environment).
    async fn finalize_teardown(
        &self,
        _instance: &InstanceContext<'_>,
    ) -> Result<(), SubstrateFault> {
        Ok(())
    }

    /// Structured spend for `--json` envelopes (§4). Substrates that spend
    /// nothing (local) return `None`.
    async fn spend(&self) -> Option<SpendInfo> {
        None
    }

    /// Human spend line after `up`/`down` (§4 — never silently nothing).
    async fn spend_line(&self) -> Option<String> {
        self.spend().await.map(|info| info.summary)
    }

    /// Recent logs for `services` (§2 — recent window, no streaming). `None`
    /// means this substrate has no log facility (the daemon never saw the
    /// processes and there is no remote log API); the CLI reports that rather
    /// than inventing output.
    async fn fetch_logs(
        &self,
        _store: &crate::state::Store,
        _def: &StackDef,
        _instance: &InstanceContext<'_>,
        _services: &[String],
        _tail: usize,
    ) -> Result<Option<Vec<ServiceLog>>, SubstrateFault> {
        Ok(None)
    }
}