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
//! Builder for the synchronous runtime composition root.
use std::collections::{BTreeMap, BTreeSet};
use std::sync::Arc;
use crate::admission::AdmissionGuard;
use crate::core::Core;
use crate::effect_backend::EffectBackend;
use crate::error::BuildError;
use crate::operation_status_sink::OperationStatusSink;
use crate::receipt::ReceiptHashPolicy;
use crate::{handler, module, operation, receipt, register};
type BoxedHandler = Box<dyn handler::Handler + 'static>;
type BoxedReceiptSink = Box<dyn receipt::ReceiptSink + 'static>;
type BoxedStatusSink = Arc<dyn OperationStatusSink + Send + Sync>;
type BoxedAdmissionGuard = Box<dyn AdmissionGuard + 'static>;
type BoxedEffectBackend = Box<dyn EffectBackend + 'static>;
/// Builder for [`Core`].
///
/// The builder separates operation descriptors from handlers so modules can
/// mount descriptors first and callers can register concrete behavior later.
/// [`CoreBuilder::build`] validates that both sides are present before it
/// returns a runnable runtime.
#[derive(Default)]
pub struct CoreBuilder {
descriptors: BTreeMap<String, operation::OperationDescriptor>,
handlers: BTreeMap<String, BoxedHandler>,
admission_guard: Option<BoxedAdmissionGuard>,
receipt_sink: Option<BoxedReceiptSink>,
/// When true, the caller explicitly accepted a sinkless build: no receipt
/// sink is required and the built core records no receipts. Without this
/// opt-out, [`Self::build`] fails closed rather than silently dropping
/// receipts.
receipts_opted_out: bool,
status_sink: Option<BoxedStatusSink>,
receipt_hash_policy: ReceiptHashPolicy,
effect_backend: Option<BoxedEffectBackend>,
/// Capability tokens this Core is granted. At checkout the runtime fails
/// closed when a dispatched operation declares a required capability token
/// that is not in this set (effect-axis tokens are ambient and excluded).
granted_capabilities: BTreeSet<String>,
}
impl CoreBuilder {
/// Create an empty builder.
#[must_use]
pub fn new() -> Self {
Self::default()
}
/// Mount a data-oriented module descriptor into this builder.
///
/// # Errors
/// Returns a build error when the module is invalid or when any descriptor
/// duplicates an operation already registered in this builder.
pub fn mount(&mut self, module: module::Module) -> Result<&mut Self, BuildError> {
let module_name = module.name().to_owned();
let register = module
.into_register()
.map_err(|error| BuildError::invalid_module(&module_name, error.to_string()))?;
for (_, descriptor) in register.into_map() {
self.register_operation(descriptor)?;
}
Ok(self)
}
/// Register an operation descriptor without a handler.
///
/// # Errors
/// Returns [`BuildError::DuplicateOperation`] when the descriptor name is
/// already present.
pub fn register_operation(
&mut self,
descriptor: operation::OperationDescriptor,
) -> Result<&mut Self, BuildError> {
let name = descriptor.name().to_owned();
descriptor
.validate()
.map_err(|error| BuildError::invalid_operation(&name, error.to_string()))?;
if self.descriptors.contains_key(&name) {
return Err(BuildError::duplicate_operation(name));
}
self.descriptors.insert(name, descriptor);
Ok(self)
}
/// Register a handler for an operation name.
///
/// # Errors
/// Returns [`BuildError::DuplicateHandler`] when a handler for `name` is
/// already present.
pub fn register_handler<H>(
&mut self,
name: impl Into<String>,
handler: H,
) -> Result<&mut Self, BuildError>
where
H: handler::Handler + 'static,
{
let name = name.into();
register::validate_module_name(&name)
.map_err(|error| BuildError::invalid_handler(&name, error.to_string()))?;
if self.handlers.contains_key(&name) {
return Err(BuildError::duplicate_handler(name));
}
self.handlers.insert(name, Box::new(handler));
Ok(self)
}
/// Register a descriptor and handler together.
///
/// # Errors
/// Returns a duplicate descriptor or duplicate handler error if either side
/// is already present.
pub fn register<H>(
&mut self,
descriptor: operation::OperationDescriptor,
handler: H,
) -> Result<&mut Self, BuildError>
where
H: handler::Handler + 'static,
{
let name = descriptor.name().to_owned();
descriptor
.validate()
.map_err(|error| BuildError::invalid_operation(&name, error.to_string()))?;
if self.descriptors.contains_key(&name) {
return Err(BuildError::duplicate_operation(name));
}
if self.handlers.contains_key(&name) {
return Err(BuildError::duplicate_handler(name));
}
self.descriptors.insert(name.clone(), descriptor);
self.handlers.insert(name, Box::new(handler));
Ok(self)
}
/// Register a macro-generated operation item.
///
/// # Errors
/// Returns the same duplicate or validation errors as [`Self::register`].
pub fn register_item(
&mut self,
item: operation::OperationRegisterItem,
) -> Result<&mut Self, BuildError> {
let (descriptor, handler) = item.into_parts();
self.register(descriptor, handler)
}
/// Register a descriptor and an already-boxed handler together.
///
/// A composition layer above this builder (such as a module host) holds its
/// handlers as trait objects; this admits them without re-boxing. Validation
/// and duplicate detection match [`Self::register`].
///
/// # Errors
/// Returns a duplicate descriptor or duplicate handler error if either side
/// is already present, or an invalid-operation error if the descriptor fails
/// validation.
pub fn register_boxed(
&mut self,
descriptor: operation::OperationDescriptor,
handler: BoxedHandler,
) -> Result<&mut Self, BuildError> {
let name = descriptor.name().to_owned();
descriptor
.validate()
.map_err(|error| BuildError::invalid_operation(&name, error.to_string()))?;
if self.descriptors.contains_key(&name) {
return Err(BuildError::duplicate_operation(name));
}
if self.handlers.contains_key(&name) {
return Err(BuildError::duplicate_handler(name));
}
self.descriptors.insert(name.clone(), descriptor);
self.handlers.insert(name, handler);
Ok(self)
}
/// Configure the optional pre-handler admission guard.
///
/// The guard runs before every handler dispatch and may deny the call (the
/// handler never runs, and the runtime records a `Denied` receipt). At most
/// one guard is held; compose multiple policies inside a single guard.
pub fn admission_guard<G>(&mut self, guard: G) -> &mut Self
where
G: AdmissionGuard + 'static,
{
self.admission_guard = Some(Box::new(guard));
self
}
/// Clear any configured admission guard.
pub fn clear_admission_guard(&mut self) -> &mut Self {
self.admission_guard = None;
self
}
/// Configure the optional receipt sink made available to invocation
/// contexts.
pub fn receipt_sink<S>(&mut self, sink: S) -> &mut Self
where
S: receipt::ReceiptSink + 'static,
{
self.receipt_sink = Some(Box::new(sink));
self
}
/// Configure the optional receipt sink from an already-boxed trait object.
///
/// Equivalent to [`Self::receipt_sink`] for a composition layer that holds
/// its sink as a trait object.
pub fn receipt_sink_boxed(&mut self, sink: BoxedReceiptSink) -> &mut Self {
self.receipt_sink = Some(sink);
self
}
/// Explicitly build a core that records no receipts.
///
/// [`Self::build`] fails closed with [`BuildError::MissingReceiptSink`] when
/// no receipt sink is configured, because a sinkless core silently drops
/// every runtime receipt. Call this to state on purpose that this core
/// persists no receipts. A receipt sink configured with
/// [`Self::receipt_sink`] takes precedence and still persists receipts.
pub fn without_receipts(&mut self) -> &mut Self {
self.receipts_opted_out = true;
self
}
/// Configure the optional operation-status sink used during checkout.
pub fn status_sink<S>(&mut self, sink: S) -> &mut Self
where
S: OperationStatusSink + Send + Sync + 'static,
{
self.status_sink = Some(Arc::new(sink));
self
}
/// Configure the operation-status sink from an already-boxed trait object.
pub fn status_sink_boxed(&mut self, sink: BoxedStatusSink) -> &mut Self {
self.status_sink = Some(sink);
self
}
/// Clear any configured operation-status sink.
pub fn clear_status_sink(&mut self) -> &mut Self {
self.status_sink = None;
self
}
/// Configure runtime receipt hash population.
pub fn receipt_hash_policy(&mut self, policy: ReceiptHashPolicy) -> &mut Self {
self.receipt_hash_policy = policy;
self
}
/// Grant one runtime capability token to the built [`Core`].
///
/// At checkout the runtime fails closed when a dispatched operation declares
/// a required capability token it was not granted. Tokens are declared with
/// `OperationEffectRow::requires_capability` or the `#[operation]` macro's
/// `requires_capabilities`. Effect-axis tokens auto-declared by the effect
/// builders (event read/append, projection query, receipt emit, host
/// control) are ambient — they are mediated by the observed-effect subset
/// check and never need an explicit grant. A Core granted nothing therefore
/// runs only operations that declare no extra capability tokens.
pub fn grant_capability(&mut self, capability: impl Into<String>) -> &mut Self {
self.granted_capabilities.insert(capability.into());
self
}
/// Grant several runtime capability tokens to the built [`Core`].
///
/// Convenience over repeated [`Self::grant_capability`] calls; see it for
/// the gate semantics.
pub fn grant_capabilities<I, S>(&mut self, capabilities: I) -> &mut Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
self.granted_capabilities
.extend(capabilities.into_iter().map(Into::into));
self
}
/// Configure the runtime-owned effect backend.
///
/// Operations append events only through `Ctx`, which performs the append
/// through this backend. Without a backend bound, an `append_event` call from
/// a handler fails closed rather than reaching a store the runtime did not
/// mediate.
pub fn effect_backend<B>(&mut self, backend: B) -> &mut Self
where
B: EffectBackend + 'static,
{
self.effect_backend = Some(Box::new(backend));
self
}
/// Configure the effect backend from an already-boxed trait object.
///
/// Equivalent to [`Self::effect_backend`] for a composition layer that holds
/// its backend as a trait object.
pub fn effect_backend_boxed(&mut self, backend: BoxedEffectBackend) -> &mut Self {
self.effect_backend = Some(backend);
self
}
/// Build a runnable [`Core`].
///
/// # Errors
/// Returns a missing-descriptor or missing-handler error when the mounted
/// operation table and handler table do not line up by name, or
/// [`BuildError::MissingReceiptSink`] when no receipt sink is configured and
/// the caller did not opt out with [`Self::without_receipts`] (a sinkless
/// core silently drops every runtime receipt, so the build fails closed).
pub fn build(self) -> Result<Core, BuildError> {
for name in self.descriptors.keys() {
if !self.handlers.contains_key(name) {
return Err(BuildError::missing_handler(name.clone()));
}
}
for name in self.handlers.keys() {
if !self.descriptors.contains_key(name) {
return Err(BuildError::missing_descriptor(name.clone()));
}
}
if self.receipt_sink.is_none() && !self.receipts_opted_out {
return Err(BuildError::MissingReceiptSink);
}
Ok(Core {
descriptors: self.descriptors,
handlers: self.handlers,
admission_guard: self.admission_guard,
receipt_sink: self.receipt_sink,
status_sink: self.status_sink,
receipt_hash_policy: self.receipt_hash_policy,
effect_backend: self.effect_backend,
granted_capabilities: self.granted_capabilities,
})
}
}
#[cfg(test)]
mod builder_mutation_tests {
//! Pins the boxed-registration and sink/guard/backend setters: each setter
//! mutates builder state that `build()` lifts into `Core`'s `pub(crate)`
//! fields, so the leaked-`Default` setter mutants (which drop the mutation)
//! and the `register_boxed -> Ok(Default)` mutant are observable here.
use std::sync::Arc;
use crate::admission::{AdmissionDecision, AdmissionGuard};
use crate::core::Ctx;
use crate::effect_backend::{EffectBackend, EffectError};
use crate::handler::{Handler, HandlerResult};
use crate::operation::{EffectClass, OperationDescriptor};
use crate::operation_status::OperationStatusFactV1;
use crate::operation_status_sink::{OperationStatusSink, OperationStatusSinkError};
use crate::receipt::{ReceiptEnvelope, ReceiptSink, ReceiptSinkError, RecordedReceipt};
use super::CoreBuilder;
const PING: OperationDescriptor = OperationDescriptor::new(
"ping",
EffectClass::Inspect,
"schema.ping.input.v1",
"schema.ping.output.v1",
"receipt.ping.v1",
);
struct Echo;
impl Handler for Echo {
fn handle(&mut self, input: &[u8], _cx: &mut Ctx<'_>) -> HandlerResult {
Ok(input.to_vec())
}
}
fn boxed_handler() -> Box<dyn Handler + 'static> {
Box::new(Echo)
}
struct NoopReceiptSink;
impl ReceiptSink for NoopReceiptSink {
fn record_receipt(
&self,
envelope: &ReceiptEnvelope,
) -> Result<RecordedReceipt, ReceiptSinkError> {
Ok(RecordedReceipt::new(envelope.clone()))
}
}
struct NoopStatusSink;
impl OperationStatusSink for NoopStatusSink {
fn record_fact(
&self,
_fact: &OperationStatusFactV1,
) -> Result<(), OperationStatusSinkError> {
Ok(())
}
}
struct NoopBackend;
impl EffectBackend for NoopBackend {
fn append_event(
&mut self,
_kind: batpak::event::EventKind,
_payload: &[u8],
) -> Result<(), EffectError> {
Ok(())
}
}
struct DenyGuard;
impl AdmissionGuard for DenyGuard {
fn admit(
&self,
_descriptor: &OperationDescriptor,
_input: &[u8],
_cx: &mut Ctx<'_>,
) -> AdmissionDecision {
AdmissionDecision::deny("denied", "blocked in test")
}
}
#[test]
fn register_boxed_inserts_descriptor_and_handler_and_detects_duplicates() {
let mut builder = CoreBuilder::new();
builder
.register_boxed(PING, boxed_handler())
.expect("first register_boxed should succeed");
// The second registration only errors if the first insert actually took
// hold; the `Ok(Default)` mutant never inserts, so it cannot detect the
// duplicate and returns Ok.
let duplicate = builder.register_boxed(PING, boxed_handler());
assert!(
duplicate.is_err(),
"registering the same name twice must be a duplicate error"
);
builder.without_receipts();
let core = builder.build().expect("build should succeed");
assert!(
core.contains_operation("ping"),
"register_boxed must leave the descriptor in the built Core"
);
// Descriptor presence alone is insufficient: a regression that kept the
// descriptor but dropped the boxed handler would still pass the check
// above while breaking dispatch. Assert the handler map retained it too.
assert!(
core.handlers.contains_key("ping"),
"register_boxed must also retain the boxed handler in the built Core"
);
}
#[test]
fn admission_guard_is_set_then_cleared() {
let mut with_guard = CoreBuilder::new();
with_guard
.register_boxed(PING, boxed_handler())
.expect("register");
with_guard.admission_guard(DenyGuard);
with_guard.without_receipts();
let core = with_guard.build().expect("build");
assert!(
core.admission_guard.is_some(),
"admission_guard setter must bind a guard"
);
let mut cleared = CoreBuilder::new();
cleared
.register_boxed(PING, boxed_handler())
.expect("register");
cleared.admission_guard(DenyGuard);
cleared.clear_admission_guard();
cleared.without_receipts();
let core = cleared.build().expect("build");
assert!(
core.admission_guard.is_none(),
"clear_admission_guard must drop the bound guard"
);
}
#[test]
fn receipt_sink_boxed_binds_the_sink() {
let mut builder = CoreBuilder::new();
builder
.register_boxed(PING, boxed_handler())
.expect("register");
builder.receipt_sink_boxed(Box::new(NoopReceiptSink));
let core = builder.build().expect("build");
assert!(
core.receipt_sink.is_some(),
"receipt_sink_boxed must bind the sink"
);
}
#[test]
fn status_sink_boxed_is_set_then_cleared() {
let mut builder = CoreBuilder::new();
builder
.register_boxed(PING, boxed_handler())
.expect("register");
builder.status_sink_boxed(Arc::new(NoopStatusSink));
builder.without_receipts();
let core = builder.build().expect("build");
assert!(
core.status_sink.is_some(),
"status_sink_boxed must bind the sink"
);
let mut cleared = CoreBuilder::new();
cleared
.register_boxed(PING, boxed_handler())
.expect("register");
cleared.status_sink_boxed(Arc::new(NoopStatusSink));
cleared.clear_status_sink();
cleared.without_receipts();
let core = cleared.build().expect("build");
assert!(
core.status_sink.is_none(),
"clear_status_sink must drop the bound sink"
);
}
#[test]
fn effect_backend_boxed_binds_the_backend() {
let mut builder = CoreBuilder::new();
builder
.register_boxed(PING, boxed_handler())
.expect("register");
builder.effect_backend_boxed(Box::new(NoopBackend));
builder.without_receipts();
let core = builder.build().expect("build");
assert!(
core.effect_backend.is_some(),
"effect_backend_boxed must bind the backend"
);
}
}