ergo-runtime 0.1.0-alpha.1

Canonical primitive contracts and reference implementations for the Ergo graph execution engine
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
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
//! action
//!
//! Purpose:
//! - Define kernel action primitive types, manifests, validation errors, and
//!   registry helpers.
//!
//! Owns:
//! - `ActionValidationError` as the typed registration failure surface for
//!   action primitives.
//! - Action type metadata and registry-facing action declarations.
//!
//! Does not own:
//! - Catalog-level wrapper errors or host-facing diagnostics.
//! - Runtime execution/orchestration semantics outside action registration.
//!
//! Connects to:
//! - `catalog.rs`, which wraps action registration failures.
//! - Action implementations under `implementations/`.
//!
//! Safety notes:
//! - `Display` uses the `ErrorInfo` authority so action rule ids and summaries do
//!   not drift from the kernel meaning they already own.

use std::borrow::Cow;
use std::collections::HashMap;
use std::fmt;

use crate::common::{doc_anchor_for_rule, ErrorInfo, Phase, ValueType};

pub mod implementations;
pub mod registry;

#[derive(Debug, Clone, PartialEq)]
pub enum ActionKind {
    Action,
}

#[derive(Debug, Clone, PartialEq)]
pub enum ActionValueType {
    Event,
    Number,
    Series,
    Bool,
    String,
}

#[derive(Debug, Clone, PartialEq)]
pub enum ActionOutcome {
    Attempted,
    /// Action executed successfully and completed.
    /// NOTE: If serialization is introduced, add backward-compat serde alias for legacy "Filled".
    Completed,
    Rejected,
    Cancelled,
    Failed,
    /// Action was never attempted because gating trigger emitted NotEmitted.
    Skipped,
}

#[derive(Debug, Clone, PartialEq)]
pub enum ActionValue {
    Event(ActionOutcome),
    Number(f64),
    Series(Vec<f64>),
    Bool(bool),
    String(String),
}

impl ActionValue {
    pub fn value_type(&self) -> ActionValueType {
        match self {
            ActionValue::Event(_) => ActionValueType::Event,
            ActionValue::Number(_) => ActionValueType::Number,
            ActionValue::Series(_) => ActionValueType::Series,
            ActionValue::Bool(_) => ActionValueType::Bool,
            ActionValue::String(_) => ActionValueType::String,
        }
    }

    pub fn as_event(&self) -> Option<&ActionOutcome> {
        match self {
            ActionValue::Event(e) => Some(e),
            _ => None,
        }
    }

    pub fn as_series(&self) -> Option<&Vec<f64>> {
        match self {
            ActionValue::Series(series) => Some(series),
            _ => None,
        }
    }
}

#[derive(Debug, Clone, PartialEq)]
pub enum ParameterType {
    Int,
    Number,
    Bool,
    String,
    Enum,
}

#[derive(Debug, Clone, PartialEq)]
pub enum ParameterValue {
    Int(i64),
    Number(f64),
    Bool(bool),
    String(String),
    Enum(String),
}

impl ParameterValue {
    pub fn value_type(&self) -> ParameterType {
        match self {
            ParameterValue::Int(_) => ParameterType::Int,
            ParameterValue::Number(_) => ParameterType::Number,
            ParameterValue::Bool(_) => ParameterType::Bool,
            ParameterValue::String(_) => ParameterType::String,
            ParameterValue::Enum(_) => ParameterType::Enum,
        }
    }
}

#[derive(Debug, Clone, PartialEq)]
pub enum Cardinality {
    Single,
}

#[derive(Debug, Clone, PartialEq)]
pub struct InputSpec {
    pub name: String,
    pub value_type: ActionValueType,
    pub required: bool,
    pub cardinality: Cardinality,
}

#[derive(Debug, Clone, PartialEq)]
pub struct OutputSpec {
    pub name: String,
    pub value_type: ActionValueType,
}

#[derive(Debug, Clone, PartialEq)]
pub struct ParameterSpec {
    pub name: String,
    pub value_type: ParameterType,
    pub default: Option<ParameterValue>,
    pub required: bool,
    pub bounds: Option<String>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct ActionWriteSpec {
    pub name: String,
    pub value_type: ValueType,
    pub from_input: String,
}

#[derive(Debug, Clone, PartialEq)]
pub struct IntentFieldSpec {
    pub name: String,
    pub value_type: ValueType,
    pub from_input: Option<String>,
    pub from_param: Option<String>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct IntentMirrorWriteSpec {
    pub name: String,
    pub value_type: ValueType,
    pub from_field: String,
}

#[derive(Debug, Clone, PartialEq)]
pub struct IntentSpec {
    pub name: String,
    pub fields: Vec<IntentFieldSpec>,
    pub mirror_writes: Vec<IntentMirrorWriteSpec>,
}

#[derive(Debug, Clone, PartialEq, Default)]
pub struct ActionEffects {
    pub writes: Vec<ActionWriteSpec>,
    pub intents: Vec<IntentSpec>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct ExecutionSpec {
    pub deterministic: bool,
    pub retryable: bool,
}

#[derive(Debug, Clone, PartialEq)]
pub struct StateSpec {
    pub allowed: bool,
}

#[derive(Debug, Clone, PartialEq)]
pub struct ActionPrimitiveManifest {
    pub id: String,
    pub version: String,
    pub kind: ActionKind,
    pub inputs: Vec<InputSpec>,
    pub outputs: Vec<OutputSpec>,
    pub parameters: Vec<ParameterSpec>,
    pub effects: ActionEffects,
    pub execution: ExecutionSpec,
    pub state: StateSpec,
    pub side_effects: bool,
}

#[derive(Debug, Clone, Default)]
pub struct ActionState {
    pub data: HashMap<String, ActionValue>,
}

#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum ActionValidationError {
    InvalidId {
        id: String,
    },
    InvalidVersion {
        version: String,
    },
    WrongKind {
        expected: ActionKind,
        got: ActionKind,
    },
    SideEffectsRequired,
    NonDeterministicExecution,
    RetryNotAllowed,
    StateNotAllowed,
    DuplicateId(String),
    DuplicateInput {
        name: String,
        first_index: usize,
        second_index: usize,
    },
    EventInputRequired,
    DuplicateWriteName {
        name: String,
        first_index: usize,
        second_index: usize,
    },
    InvalidWriteType {
        name: String,
        got: ValueType,
    },
    InvalidInputType {
        input: String,
        expected: ActionValueType,
        got: ActionValueType,
    },
    OutputNotOutcome {
        name: String,
        index: usize,
    },
    InvalidOutputType {
        output: String,
        expected: ActionValueType,
        got: ActionValueType,
    },
    UndeclaredOutput {
        primitive: String,
        output: String,
    },
    InvalidParameterType {
        parameter: String,
        expected: ParameterType,
        got: ParameterType,
    },
    UnboundWriteKeyReference {
        name: String,
        referenced_param: String,
    },
    WriteKeyReferenceNotString {
        name: String,
        referenced_param: String,
    },
    WriteFromInputNotFound {
        write_name: String,
        from_input: String,
    },
    WriteFromInputTypeMismatch {
        write_name: String,
        from_input: String,
        expected: ValueType,
        found: ActionValueType,
    },
    DuplicateIntentName {
        name: String,
        first_index: usize,
        second_index: usize,
    },
    DuplicateIntentFieldName {
        intent_name: String,
        field_name: String,
        first_index: usize,
        second_index: usize,
    },
    IntentFieldMissingSource {
        intent_name: String,
        field_name: String,
    },
    IntentFieldMultipleSources {
        intent_name: String,
        field_name: String,
    },
    IntentFieldFromInputNotFound {
        intent_name: String,
        field_name: String,
        from_input: String,
    },
    IntentFieldFromInputTypeMismatch {
        intent_name: String,
        field_name: String,
        from_input: String,
        expected: ValueType,
        found: ActionValueType,
    },
    IntentFieldFromParamNotFound {
        intent_name: String,
        field_name: String,
        from_param: String,
    },
    IntentFieldFromParamTypeMismatch {
        intent_name: String,
        field_name: String,
        from_param: String,
        expected: ValueType,
        found: ParameterType,
    },
    MirrorWriteFromFieldNotFound {
        intent_name: String,
        write_name: String,
        from_field: String,
    },
    MirrorWriteTypeMismatch {
        intent_name: String,
        write_name: String,
        from_field: String,
        expected: ValueType,
        found: ValueType,
    },
}

impl ErrorInfo for ActionValidationError {
    fn rule_id(&self) -> &'static str {
        match self {
            Self::InvalidId { .. } => "ACT-1",
            Self::InvalidVersion { .. } => "ACT-2",
            Self::WrongKind { .. } => "ACT-3",
            Self::EventInputRequired => "ACT-4",
            Self::DuplicateInput { .. } => "ACT-5",
            Self::InvalidInputType { .. } => "ACT-6",
            Self::UndeclaredOutput { .. } => "ACT-7",
            Self::OutputNotOutcome { .. } => "ACT-8",
            Self::InvalidOutputType { .. } => "ACT-9",
            Self::StateNotAllowed => "ACT-10",
            Self::SideEffectsRequired => "ACT-11",
            Self::DuplicateWriteName { .. } => "ACT-14",
            Self::InvalidWriteType { .. } => "ACT-15",
            Self::RetryNotAllowed => "ACT-16",
            Self::NonDeterministicExecution => "ACT-17",
            Self::DuplicateId(_) => "ACT-18",
            Self::InvalidParameterType { .. } => "ACT-19",
            Self::UnboundWriteKeyReference { .. } => "ACT-20",
            Self::WriteKeyReferenceNotString { .. } => "ACT-21",
            Self::WriteFromInputNotFound { .. } => "ACT-22",
            Self::WriteFromInputTypeMismatch { .. } => "ACT-23",
            Self::DuplicateIntentName { .. } => "ACT-24",
            Self::DuplicateIntentFieldName { .. } => "ACT-25",
            Self::IntentFieldMissingSource { .. } => "ACT-26",
            Self::IntentFieldMultipleSources { .. } => "ACT-27",
            Self::IntentFieldFromInputNotFound { .. } => "ACT-28",
            Self::IntentFieldFromInputTypeMismatch { .. } => "ACT-29",
            Self::IntentFieldFromParamNotFound { .. } => "ACT-30",
            Self::IntentFieldFromParamTypeMismatch { .. } => "ACT-31",
            Self::MirrorWriteFromFieldNotFound { .. } => "ACT-32",
            Self::MirrorWriteTypeMismatch { .. } => "ACT-33",
        }
    }

    fn phase(&self) -> Phase {
        Phase::Registration
    }

    fn doc_anchor(&self) -> &'static str {
        doc_anchor_for_rule(self.rule_id())
    }

    fn summary(&self) -> Cow<'static, str> {
        match self {
            Self::InvalidId { id } => Cow::Owned(format!("Invalid action ID: '{}'", id)),
            Self::InvalidVersion { version } => {
                Cow::Owned(format!("Invalid version: '{}'", version))
            }
            Self::WrongKind { expected, got } => Cow::Owned(format!(
                "Wrong kind: expected {:?}, got {:?}",
                expected, got
            )),
            Self::SideEffectsRequired => Cow::Borrowed("Actions must declare side effects"),
            Self::NonDeterministicExecution => {
                Cow::Borrowed("Action execution must be deterministic")
            }
            Self::RetryNotAllowed => Cow::Borrowed("Action retryable must be false"),
            Self::StateNotAllowed => Cow::Borrowed("Action state is not allowed"),
            Self::DuplicateId(_) => Cow::Borrowed("Duplicate action ID: already registered"),
            Self::DuplicateInput { name, .. } => {
                Cow::Owned(format!("Duplicate input name: '{}'", name))
            }
            Self::EventInputRequired => Cow::Borrowed("Action requires at least one event input"),
            Self::DuplicateWriteName { name, .. } => {
                Cow::Owned(format!("Duplicate write name: '{}'", name))
            }
            Self::InvalidWriteType { name, got } => {
                Cow::Owned(format!("Write '{}' has invalid type {:?}", name, got))
            }
            Self::InvalidInputType {
                input,
                expected,
                got,
            } => Cow::Owned(format!(
                "Input '{}' has invalid type: expected {:?}, got {:?}",
                input, expected, got
            )),
            Self::OutputNotOutcome { name, .. } => Cow::Owned(format!(
                "Action output must be named 'outcome', got '{}'",
                name
            )),
            Self::InvalidOutputType {
                output,
                expected,
                got,
            } => Cow::Owned(format!(
                "Output '{}' has invalid type: expected {:?}, got {:?}",
                output, expected, got
            )),
            Self::UndeclaredOutput { primitive, output } => Cow::Owned(format!(
                "Undeclared output '{}' on primitive '{}'",
                output, primitive
            )),
            Self::InvalidParameterType {
                parameter,
                expected,
                got,
            } => Cow::Owned(format!(
                "Parameter '{}' has invalid type: expected {:?}, got {:?}",
                parameter, expected, got
            )),
            Self::UnboundWriteKeyReference {
                name,
                referenced_param,
            } => Cow::Owned(format!(
                "Write key '{}' references undefined parameter '{}'",
                name, referenced_param
            )),
            Self::WriteKeyReferenceNotString {
                name,
                referenced_param,
            } => Cow::Owned(format!(
                "Write key '{}' references parameter '{}' which is not String type",
                name, referenced_param
            )),
            Self::WriteFromInputNotFound {
                write_name,
                from_input,
            } => Cow::Owned(format!(
                "Write '{}' references undeclared input '{}'",
                write_name, from_input
            )),
            Self::WriteFromInputTypeMismatch {
                write_name,
                from_input,
                expected,
                found,
            } => Cow::Owned(format!(
                "Write '{}' type {:?} does not match input '{}' type {:?}",
                write_name, expected, from_input, found
            )),
            Self::DuplicateIntentName { name, .. } => {
                Cow::Owned(format!("Duplicate intent name: '{}'", name))
            }
            Self::DuplicateIntentFieldName {
                intent_name,
                field_name,
                ..
            } => Cow::Owned(format!(
                "Intent '{}' has duplicate field name '{}'",
                intent_name, field_name
            )),
            Self::IntentFieldMissingSource {
                intent_name,
                field_name,
            } => Cow::Owned(format!(
                "Intent '{}' field '{}' must set exactly one source (from_input or from_param)",
                intent_name, field_name
            )),
            Self::IntentFieldMultipleSources {
                intent_name,
                field_name,
            } => Cow::Owned(format!(
                "Intent '{}' field '{}' sets both from_input and from_param",
                intent_name, field_name
            )),
            Self::IntentFieldFromInputNotFound {
                intent_name,
                field_name,
                from_input,
            } => Cow::Owned(format!(
                "Intent '{}' field '{}' references undeclared input '{}'",
                intent_name, field_name, from_input
            )),
            Self::IntentFieldFromInputTypeMismatch {
                intent_name,
                field_name,
                from_input,
                expected,
                found,
            } => Cow::Owned(format!(
                "Intent '{}' field '{}' expects {:?} but input '{}' is {:?}",
                intent_name, field_name, expected, from_input, found
            )),
            Self::IntentFieldFromParamNotFound {
                intent_name,
                field_name,
                from_param,
            } => Cow::Owned(format!(
                "Intent '{}' field '{}' references undeclared parameter '{}'",
                intent_name, field_name, from_param
            )),
            Self::IntentFieldFromParamTypeMismatch {
                intent_name,
                field_name,
                from_param,
                expected,
                found,
            } => Cow::Owned(format!(
                "Intent '{}' field '{}' expects {:?} but parameter '{}' is {:?}",
                intent_name, field_name, expected, from_param, found
            )),
            Self::MirrorWriteFromFieldNotFound {
                intent_name,
                write_name,
                from_field,
            } => Cow::Owned(format!(
                "Intent '{}' mirror write '{}' references undeclared field '{}'",
                intent_name, write_name, from_field
            )),
            Self::MirrorWriteTypeMismatch {
                intent_name,
                write_name,
                from_field,
                expected,
                found,
            } => Cow::Owned(format!(
                "Intent '{}' mirror write '{}' type {:?} does not match field '{}' type {:?}",
                intent_name, write_name, expected, from_field, found
            )),
        }
    }

    fn path(&self) -> Option<Cow<'static, str>> {
        match self {
            Self::InvalidId { .. } => Some(Cow::Borrowed("$.id")),
            Self::InvalidVersion { .. } => Some(Cow::Borrowed("$.version")),
            Self::DuplicateId(_) => Some(Cow::Borrowed("$.id")),
            Self::WrongKind { .. } => Some(Cow::Borrowed("$.kind")),
            Self::EventInputRequired => Some(Cow::Borrowed("$.inputs")),
            Self::DuplicateInput { second_index, .. } => {
                Some(Cow::Owned(format!("$.inputs[{}].name", second_index)))
            }
            Self::InvalidInputType { .. } => Some(Cow::Borrowed("$.inputs[].type")),
            Self::UndeclaredOutput { .. } => Some(Cow::Borrowed("$.outputs")),
            Self::OutputNotOutcome { index, .. } => {
                Some(Cow::Owned(format!("$.outputs[{}].name", index)))
            }
            Self::InvalidOutputType { .. } => Some(Cow::Borrowed("$.outputs[0].type")),
            Self::StateNotAllowed => Some(Cow::Borrowed("$.state.allowed")),
            Self::SideEffectsRequired => Some(Cow::Borrowed("$.side_effects")),
            Self::DuplicateWriteName { second_index, .. } => Some(Cow::Owned(format!(
                "$.effects.writes[{}].name",
                second_index
            ))),
            Self::InvalidWriteType { .. } => Some(Cow::Borrowed("$.effects.writes[].type")),
            Self::RetryNotAllowed => Some(Cow::Borrowed("$.execution.retryable")),
            Self::NonDeterministicExecution => Some(Cow::Borrowed("$.execution.deterministic")),
            Self::InvalidParameterType { .. } => Some(Cow::Borrowed("$.parameters[].default")),
            Self::UnboundWriteKeyReference { .. } => Some(Cow::Borrowed("$.effects.writes[].name")),
            Self::WriteKeyReferenceNotString { .. } => {
                Some(Cow::Borrowed("$.effects.writes[].name"))
            }
            Self::WriteFromInputNotFound { .. } => {
                Some(Cow::Borrowed("$.effects.writes[].from_input"))
            }
            Self::WriteFromInputTypeMismatch { .. } => {
                Some(Cow::Borrowed("$.effects.writes[].from_input"))
            }
            Self::DuplicateIntentName { second_index, .. } => Some(Cow::Owned(format!(
                "$.effects.intents[{}].name",
                second_index
            ))),
            Self::DuplicateIntentFieldName {
                intent_name,
                second_index,
                ..
            } => Some(Cow::Owned(format!(
                "$.effects.intents[?(@.name==\"{}\")].fields[{}].name",
                intent_name, second_index
            ))),
            Self::IntentFieldMissingSource { intent_name, .. }
            | Self::IntentFieldMultipleSources { intent_name, .. } => Some(Cow::Owned(format!(
                "$.effects.intents[?(@.name==\"{}\")].fields[]",
                intent_name
            ))),
            Self::IntentFieldFromInputNotFound { intent_name, .. }
            | Self::IntentFieldFromInputTypeMismatch { intent_name, .. } => {
                Some(Cow::Owned(format!(
                    "$.effects.intents[?(@.name==\"{}\")].fields[].from_input",
                    intent_name
                )))
            }
            Self::IntentFieldFromParamNotFound { intent_name, .. }
            | Self::IntentFieldFromParamTypeMismatch { intent_name, .. } => {
                Some(Cow::Owned(format!(
                    "$.effects.intents[?(@.name==\"{}\")].fields[].from_param",
                    intent_name
                )))
            }
            Self::MirrorWriteFromFieldNotFound { intent_name, .. }
            | Self::MirrorWriteTypeMismatch { intent_name, .. } => Some(Cow::Owned(format!(
                "$.effects.intents[?(@.name==\"{}\")].mirror_writes[]",
                intent_name
            ))),
        }
    }

    fn fix(&self) -> Option<Cow<'static, str>> {
        match self {
            Self::InvalidId { .. } => Some(Cow::Borrowed(
                "ID must start with lowercase letter and contain only lowercase letters, digits, and underscores",
            )),
            Self::DuplicateId(_) => Some(Cow::Borrowed("Choose a unique ID not already registered")),
            Self::InvalidVersion { .. } => Some(Cow::Borrowed(
                "Version must be valid semver (e.g., '1.0.0')",
            )),
            Self::WrongKind { .. } => Some(Cow::Borrowed("Set kind: action")),
            Self::EventInputRequired => Some(Cow::Borrowed("Add at least one event input")),
            Self::DuplicateInput { name, .. } => Some(Cow::Owned(format!(
                "Rename input '{}' to a unique value",
                name
            ))),
            Self::InvalidInputType { .. } => Some(Cow::Borrowed(
                "Use a valid input type: event, number, series, bool, or string",
            )),
            Self::UndeclaredOutput { .. } => Some(Cow::Borrowed("Declare a single outcome output")),
            Self::OutputNotOutcome { .. } => Some(Cow::Borrowed("Rename output to 'outcome'")),
            Self::InvalidOutputType { .. } => Some(Cow::Borrowed("Output type must be event")),
            Self::StateNotAllowed => Some(Cow::Borrowed("Set state.allowed: false")),
            Self::SideEffectsRequired => Some(Cow::Borrowed("Set side_effects: true")),
            Self::DuplicateWriteName { name, .. } => Some(Cow::Owned(format!(
                "Rename write '{}' to a unique value",
                name
            ))),
            Self::InvalidWriteType { .. } => Some(Cow::Borrowed(
                "Write types must be Number, Series, Bool, or String",
            )),
            Self::RetryNotAllowed => Some(Cow::Borrowed("Set execution.retryable: false")),
            Self::NonDeterministicExecution => {
                Some(Cow::Borrowed("Set execution.deterministic: true"))
            }
            Self::InvalidParameterType { .. } => Some(Cow::Borrowed(
                "Change parameter default value to match the declared parameter type",
            )),
            Self::UnboundWriteKeyReference {
                referenced_param, ..
            } => Some(Cow::Owned(format!(
                "Add parameter '{}' to the action manifest",
                referenced_param
            ))),
            Self::WriteKeyReferenceNotString {
                referenced_param, ..
            } => Some(Cow::Owned(format!(
                "Change parameter '{}' type to String",
                referenced_param
            ))),
            Self::WriteFromInputNotFound { from_input, .. } => Some(Cow::Owned(format!(
                "Declare input '{}' in the action manifest inputs",
                from_input
            ))),
            Self::WriteFromInputTypeMismatch {
                from_input,
                expected,
                ..
            } => Some(Cow::Owned(format!(
                "Change input '{}' type to match write type {:?}, or use a scalar-typed input",
                from_input, expected
            ))),
            Self::DuplicateIntentName { name, .. } => Some(Cow::Owned(format!(
                "Rename intent '{}' to a unique value",
                name
            ))),
            Self::DuplicateIntentFieldName { field_name, .. } => Some(Cow::Owned(format!(
                "Rename intent field '{}' to a unique value within its intent",
                field_name
            ))),
            Self::IntentFieldMissingSource { .. } => Some(Cow::Borrowed(
                "Set exactly one source on each intent field: from_input or from_param",
            )),
            Self::IntentFieldMultipleSources { .. } => Some(Cow::Borrowed(
                "Set only one source on each intent field: from_input or from_param",
            )),
            Self::IntentFieldFromInputNotFound { from_input, .. } => Some(Cow::Owned(format!(
                "Declare input '{}' in the action manifest inputs",
                from_input
            ))),
            Self::IntentFieldFromInputTypeMismatch {
                from_input,
                expected,
                ..
            } => Some(Cow::Owned(format!(
                "Change input '{}' type to match intent field type {:?}",
                from_input, expected
            ))),
            Self::IntentFieldFromParamNotFound { from_param, .. } => Some(Cow::Owned(format!(
                "Declare parameter '{}' in the action manifest parameters",
                from_param
            ))),
            Self::IntentFieldFromParamTypeMismatch {
                from_param,
                expected,
                ..
            } => Some(Cow::Owned(format!(
                "Change parameter '{}' type to match intent field type {:?}",
                from_param, expected
            ))),
            Self::MirrorWriteFromFieldNotFound { from_field, .. } => Some(Cow::Owned(format!(
                "Declare intent field '{}' before referencing it from mirror_writes",
                from_field
            ))),
            Self::MirrorWriteTypeMismatch {
                from_field,
                expected,
                ..
            } => Some(Cow::Owned(format!(
                "Change mirror write type to match field '{}' type {:?}",
                from_field, expected
            ))),
        }
    }
}

impl fmt::Display for ActionValidationError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{} ({})", self.summary(), self.rule_id())
    }
}

impl std::error::Error for ActionValidationError {}

/// An action primitive that performs effects keyed by inputs and parameters.
///
/// Statelessness is enforced at registration by manifest validation
/// (`ACT-10`, `state.allowed == false`) and at runtime by capture/replay.
/// Structural enforcement on top (derive macros, marker traits, newtype
/// wrappers) was considered and rejected; see
/// `docs/ledger/decisions/rejected-structural-enforcement-of-statelessness.md`.
pub trait ActionPrimitive {
    fn manifest(&self) -> &ActionPrimitiveManifest;

    fn execute(
        &self,
        inputs: &HashMap<String, ActionValue>,
        parameters: &HashMap<String, ParameterValue>,
    ) -> HashMap<String, ActionValue>;
}

pub use implementations::{
    AckAction, AnnotateAction, ContextSetBoolAction, ContextSetNumberAction,
    ContextSetSeriesAction, ContextSetStringAction,
};
pub use registry::ActionRegistry;

#[cfg(test)]
mod tests;