frame-core 0.3.0

Component model, lifecycle, process isolation — hosts components as supervised BEAM process trees
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
//! Typed registration and lifecycle failures.

use beamr::scheduler::MailboxSendError;
use serde::{Deserialize, Serialize};
use thiserror::Error;

use crate::capability::{Capability, CapabilityRequest, ScopeValidationError};
use crate::component::ComponentId;
use crate::event::LifecycleState;

/// Stable classification of a beamr process tombstone.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub enum TombstoneKind {
    /// Process completed normally.
    Normal,
    /// Host requested an untrappable kill.
    Kill,
    /// Process observed a kill and terminated.
    Killed,
    /// Runtime execution failed.
    Error,
    /// A linked distributed node disconnected.
    NoConnection,
    /// A link targeted a process that was already absent.
    NoProcess,
}

/// Observable reason a component entered [`LifecycleState::Failed`].
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct FailureReason {
    /// Child associated with the failure, if a child had been identified.
    pub child: Option<String>,
    /// Tombstone observed for the failing process, when one existed.
    pub tombstone: Option<TombstoneKind>,
    /// Runtime detail; rich exception information is intentionally optional.
    pub detail: String,
}

/// A typed refusal or runtime failure from the component registry.
#[derive(Debug, Error)]
pub enum RegistryError {
    /// Registration attempted to reuse a stable identity.
    #[error("component {id} is already registered")]
    DuplicateComponent {
        /// Conflicting stable identity.
        id: ComponentId,
    },
    /// A declared dependency was not in the registration table.
    #[error("component {component} requires missing component {required}")]
    MissingDependency {
        /// Component being registered.
        component: ComponentId,
        /// Missing edge target.
        required: ComponentId,
    },
    /// A dependency edge would make the graph cyclic.
    #[error("dependency edge {component} -> {required} creates a cycle")]
    CyclicDependency {
        /// Source of the cyclic edge.
        component: ComponentId,
        /// Target of the cyclic edge.
        required: ComponentId,
    },
    /// Two registered components claimed the same capability identifier.
    #[error("capability {capability} is already provided by component {provider}")]
    CapabilityConflict {
        /// Colliding service capability identifier.
        capability: String,
        /// Existing provider.
        provider: ComponentId,
    },
    /// A host-authority declaration used a malformed typed scope.
    #[error("component {component} declares invalid capability need {request:?}: {reason}")]
    InvalidCapabilityScope {
        /// Component being registered.
        component: ComponentId,
        /// Offending declaration.
        request: CapabilityRequest,
        /// Exact scope validation refusal.
        reason: ScopeValidationError,
    },
    /// An action declaration failed field validation (empty id, description,
    /// or schema source).
    #[error("component {component} action '{action}' declaration is invalid: {detail}")]
    InvalidActionDeclaration {
        /// Component being registered.
        component: ComponentId,
        /// Offending action id text (possibly empty — the offense itself).
        action: String,
        /// Exact field refusal.
        detail: String,
    },
    /// A component declared the same local action id twice.
    #[error("component {component} declares action id '{action}' more than once")]
    DuplicateActionId {
        /// Component being registered.
        component: ComponentId,
        /// Duplicate local id.
        action: String,
    },
    /// One action listed an exact capability requirement twice.
    #[error(
        "component {component} action '{action}' lists requirement {requirement:?} more than once"
    )]
    DuplicateActionRequirement {
        /// Component being registered.
        component: ComponentId,
        /// Action with the duplicate requirement.
        action: String,
        /// Duplicate requirement.
        requirement: Capability,
    },
    /// An action requires a capability its component never declared as a
    /// need — a permanently dead tool, refused at registration (F-6a R1).
    #[error(
        "component {component} action '{action}' requires {requirement:?} which is not among its declared needs"
    )]
    ActionRequirementUndeclared {
        /// Component being registered.
        component: ComponentId,
        /// Action naming the undeclared requirement.
        action: String,
        /// The requirement no declared need covers.
        requirement: Capability,
    },
    /// A fragment declaration failed field validation (empty id or kind).
    #[error("component {component} fragment '{fragment}' declaration is invalid: {detail}")]
    InvalidFragmentDeclaration {
        /// Component being registered.
        component: ComponentId,
        /// Offending fragment id text (possibly empty — the offense itself).
        fragment: String,
        /// Exact field refusal.
        detail: String,
    },
    /// A component declared the same local fragment id twice (F-5a R1's
    /// typed refusal; the whole batch is refused atomically).
    #[error("component {component} declares fragment id '{fragment}' more than once")]
    DuplicateFragmentId {
        /// Component being registered.
        component: ComponentId,
        /// Duplicate local id.
        fragment: String,
    },
    /// Fragment content exceeded the registry's caller-supplied byte limit
    /// (registration and content update validate identically — F-5a R2).
    #[error(
        "component {component} fragment '{fragment}' content is {size} bytes, over the configured max_fragment_bytes limit of {limit}"
    )]
    FragmentContentOversize {
        /// Declaring or updating component.
        component: ComponentId,
        /// Offending fragment id.
        fragment: String,
        /// Offered content size in bytes.
        size: usize,
        /// The caller-supplied limit in force.
        limit: usize,
    },
    /// Fragments were declared but the embedding host supplied no
    /// `max_fragment_bytes` — there is no hidden default limit (F-5a R1);
    /// the host states one in its lifecycle configuration or fragments are
    /// refused loudly.
    #[error(
        "component {component} declares fragments but the registry's lifecycle configuration carries no max_fragment_bytes limit"
    )]
    FragmentLimitUnconfigured {
        /// Component being registered.
        component: ComponentId,
    },
    /// A fragment act named a fragment id with no live entry in the
    /// authority — an undeclared local id (the ID set is static per
    /// incarnation, F-5a R2), or an internal invariant breach surfaced
    /// loudly rather than papered over.
    #[error("component {component} carries no live fragment '{fragment}'")]
    UnknownFragment {
        /// Component named by the act.
        component: ComponentId,
        /// The fragment id with no live entry.
        fragment: String,
    },
    /// A fragment act carried an incarnation that is not the live Running
    /// incarnation — the typed held-vs-current refusal (F-2a's rule applied
    /// to fragments, F-5a R2). `current` is absent when no incarnation is
    /// live (the component is not Running).
    #[error(
        "component {component} fragment '{fragment}' act holds incarnation {held:?} but the live incarnation is {current:?}"
    )]
    FragmentIncarnationStale {
        /// Component named by the act.
        component: ComponentId,
        /// Fragment named by the act.
        fragment: String,
        /// The incarnation the caller holds.
        held: crate::fragment::ComponentIncarnation,
        /// The live Running incarnation, absent when not Running.
        current: Option<crate::fragment::ComponentIncarnation>,
    },
    /// A manifest repeated an exact kind-and-scope pair.
    #[error("component {component} declares capability need {request:?} more than once")]
    DuplicateCapabilityNeed {
        /// Component being registered.
        component: ComponentId,
        /// Duplicate declaration.
        request: CapabilityRequest,
    },
    /// Registration omitted the mandatory restart intensity declaration.
    #[error("component {id} has no declared supervision intensity")]
    UndeclaredSupervision {
        /// Component missing the declaration.
        id: ComponentId,
    },
    /// A restart window of zero cannot define an intensity interval.
    #[error("component {id} declares a zero-length supervision window")]
    InvalidSupervisionWindow {
        /// Component with the invalid policy.
        id: ComponentId,
    },
    /// Child names are component-local unique keys.
    #[error("component {id} declares child name {child} more than once")]
    DuplicateChild {
        /// Component with the duplicate declaration.
        id: ComponentId,
        /// Duplicate child key.
        child: String,
    },
    /// The requested component is not registered.
    #[error("component {id} is not registered")]
    NotRegistered {
        /// Requested identity.
        id: ComponentId,
    },
    /// A start is already in flight for this identity.
    #[error("component {id} is already starting")]
    AlreadyStarting {
        /// Busy identity.
        id: ComponentId,
    },
    /// The component is already running.
    #[error("component {id} is already running")]
    AlreadyRunning {
        /// Running identity.
        id: ComponentId,
    },
    /// A stop is already in flight for this identity.
    #[error("component {id} is already stopping")]
    AlreadyStopping {
        /// Busy identity.
        id: ComponentId,
    },
    /// A remove is already in flight for this identity.
    #[error("component {id} is already being removed")]
    AlreadyRemoving {
        /// Busy identity.
        id: ComponentId,
    },
    /// A bytecode upgrade is already in flight for this identity.
    #[error("component {id} is already upgrading")]
    AlreadyUpgrading {
        /// Busy identity.
        id: ComponentId,
    },
    /// A bytecode upgrade was requested outside the stopped states.
    #[error(
        "component {id} bytecode upgrade requires Registered or Stopped, found {state:?}: \
         the upgrade barrier drains and stops the old incarnation before new bytes stage"
    )]
    UpgradeRequiresStopped {
        /// Requested identity.
        id: ComponentId,
        /// The state that refused the upgrade.
        state: LifecycleState,
    },
    /// A dependency exists but has not reached Running.
    #[error("component {component} requires {required} to be Running, found {state:?}")]
    DependencyNotRunning {
        /// Component whose start was refused.
        component: ComponentId,
        /// Required component.
        required: ComponentId,
        /// Observed dependency state.
        state: LifecycleState,
    },
    /// A running component still depends on the removal target.
    #[error("running component {dependent} depends on {required}")]
    RunningDependent {
        /// Running dependent preventing removal.
        dependent: ComponentId,
        /// Requested removal target.
        required: ComponentId,
    },
    /// Hot-loaded bytecode carries `erlang:*` imports the scheduler composition
    /// left Deferred — dispatch would kill the process at first use.
    #[error(
        "component {id} module {module} defers built-in imports [{imports}]: the scheduler was \
         composed without a populated BIF registry; compose through \
         frame_core::composition::compose_scheduler, never bare Scheduler::with_services"
    )]
    DeferredBifImports {
        /// Component whose start was refused.
        id: ComponentId,
        /// Module whose committed import table carries the deferrals.
        module: String,
        /// Deferred imports in `erlang:name/arity` form.
        imports: String,
    },
    /// beamr refused or could not parse component bytecode.
    #[error("failed to load bytecode for component {id}: {detail}")]
    ModuleLoad {
        /// Component being started.
        id: ComponentId,
        /// beamr loader detail.
        detail: String,
    },
    /// A native supervisor or linked child could not be spawned.
    #[error("failed to spawn {process} for component {id}: {detail}")]
    Spawn {
        /// Component being started or restarted.
        id: ComponentId,
        /// Supervisor or child name.
        process: String,
        /// beamr spawn detail.
        detail: String,
    },
    /// A child did not answer its declared mailbox probe in time.
    #[error("component {id} child {child} liveness probe timed out")]
    LivenessTimeout {
        /// Component being started or probed.
        id: ComponentId,
        /// Child that did not answer.
        child: String,
    },
    /// Start failed after entering Starting; status remains Failed.
    #[error("component {id} failed during start: {reason:?}")]
    StartFailed {
        /// Failed identity.
        id: ComponentId,
        /// Observable reason retained in status.
        reason: FailureReason,
    },
    /// Ordered stop observed a non-normal or missing tombstone.
    #[error("component {id} failed during ordered stop: {reason:?}")]
    StopFailed {
        /// Component being stopped.
        id: ComponentId,
        /// Exact observed failure.
        reason: FailureReason,
    },
    /// A retained old module generation could not be safely purged.
    #[error("failed to purge module {module} for component {id}: {detail}")]
    ModulePurge {
        /// Component being removed.
        id: ComponentId,
        /// Module display name.
        module: String,
        /// beamr purge detail.
        detail: String,
    },
    /// The current module generation was absent when removal expected it.
    #[error("module {module} for component {id} was not present during delete")]
    ModuleDelete {
        /// Component being removed.
        id: ComponentId,
        /// Module display name.
        module: String,
    },
    /// Lookup still found code after deletion.
    #[error("module {module} for component {id} remained visible after delete")]
    ModuleStillLoaded {
        /// Component being removed.
        id: ComponentId,
        /// Module display name.
        module: String,
    },
    /// beamr refused a typed host command at supervisor mailbox admission.
    #[error("component {id} supervisor {command} command delivery failed: {source}")]
    CommandDelivery {
        /// Affected component.
        id: ComponentId,
        /// Stable command kind whose delivery was attempted.
        command: &'static str,
        /// Exact beamr mailbox admission refusal.
        #[source]
        source: MailboxSendError,
    },
    /// Host/supervisor mailbox protocol was violated.
    #[error("component {id} supervisor protocol failed: {detail}")]
    SupervisorProtocol {
        /// Affected component.
        id: ComponentId,
        /// Protocol detail.
        detail: String,
    },
    /// Internal synchronization was poisoned by a panic.
    #[error("component registry synchronization is poisoned")]
    SynchronizationPoisoned,
    /// A component monitor thread terminated without returning its result.
    #[error("component {id} monitor thread terminated unexpectedly")]
    MonitorTerminated {
        /// Component whose monitor was lost.
        id: ComponentId,
    },
}