Skip to main content

acl_core/
semantics.rs

1//! Formal semantics (spec ยง17): every performative carries machine-readable
2//! feasibility preconditions (FP) and rational effects (RE), in the
3//! FIPA-ACL tradition, so agents and verifiers can process the semantics
4//! of a message rather than parse English prose.
5
6use crate::Performative;
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9pub struct PerformativeSemantics {
10    pub performative: Performative,
11    /// What must be true of the SENDER's mental state for the message to
12    /// be sincere/valid (feasibility precondition).
13    pub feasibility_precondition: &'static str,
14    /// The intended effect on the RECEIVER (rational effect).
15    pub rational_effect: &'static str,
16}
17
18/// The semantic table for all 14 performatives.
19pub fn semantics(p: Performative) -> PerformativeSemantics {
20    use Performative::*;
21    let (fp, re) = match p {
22        Request => (
23            "FP(request): sender intends that receiver perform act(a); sender believes receiver CAN perform act(a)",
24            "RE(request): receiver intends to perform act(a)",
25        ),
26        Inform => (
27            "FP(inform): sender believes proposition(p) is true; sender intends receiver to believe it",
28            "RE(inform): receiver believes proposition(p)",
29        ),
30        Query => (
31            "FP(query): sender does not know whether proposition(p) holds; sender wants to know",
32            "RE(query): receiver informs sender of the truth of proposition(p)",
33        ),
34        Propose => (
35            "FP(propose): sender intends receiver commit to act(a); act is beneficial to both under the stated terms",
36            "RE(propose): receiver evaluates and accepts/rejects the commitment",
37        ),
38        Accept => (
39            "FP(accept): a prior commitment exists with status=proposed and sender is its creditor",
40            "RE(accept): commitment becomes active; debtor owes the act",
41        ),
42        Reject => (
43            "FP(reject): a prior proposal exists and sender refuses it (reason optional)",
44            "RE(reject): proposal is void; no commitment arises",
45        ),
46        Cancel => (
47            "FP(cancel): a prior act is in flight and sender has authority to abort it",
48            "RE(cancel): receiver stops the act and acknowledges",
49        ),
50        Progress => (
51            "FP(progress): sender is executing an act and has intermediate state worth reporting",
52            "RE(progress): receiver's estimate of completion is updated",
53        ),
54        InputRequired => (
55            "FP(input_required): receiver cannot continue without X from the sender; receiver believes sender can supply X",
56            "RE(input_required): sender supplies X (via inform) or cancels",
57        ),
58        Failure => (
59            "FP(failure): receiver attempted act(a) and could not complete it; failure is terminal",
60            "RE(failure): sender knows act(a) failed (and why); no result will come",
61        ),
62        Delegate => (
63            "FP(delegate): sender has a chain of authority to sub-contract act(a) to receiver; chain recorded in body",
64            "RE(delegate): receiver acts as sub-contractor; result flows back up the chain",
65        ),
66        Error => (
67            "FP(error): a protocol/transport-level problem occurred that prevents semantic processing",
68            "RE(error): sender corrects the protocol violation",
69        ),
70        Hello => (
71            "FP(hello): sender is ready to communicate and its AgentCard (or hash) is attached",
72            "RE(hello): receiver knows sender's identity and capabilities",
73        ),
74        Bye => (
75            "FP(bye): sender has no pending obligations in this session",
76            "RE(bye): session is closed; further messages require a new session",
77        ),
78        Retract => (
79            "FP(retract): sender previously asserted proposition(p) and now believes it false or inapplicable",
80            "RE(retract): receiver no longer holds proposition(p) as asserted by sender",
81        ),
82        Subscribe => (
83            "FP(subscribe): sender wants to be informed of changes to proposition(p) until deadline",
84            "RE(subscribe): receiver will push inform messages matching the matcher",
85        ),
86        Unsubscribe => (
87            "FP(unsubscribe): sender no longer wants the referenced subscription",
88            "RE(unsubscribe): receiver stops pushing for that subscription",
89        ),
90    };
91    PerformativeSemantics { performative: p, feasibility_precondition: fp, rational_effect: re }
92}
93
94#[cfg(test)]
95mod tests {
96    use super::*;
97    use strum_like::all_performatives;
98
99    #[test]
100    fn every_performative_has_semantics() {
101        for p in all_performatives() {
102            let s = semantics(p);
103            assert!(s.feasibility_precondition.starts_with("FP("));
104            assert!(s.rational_effect.starts_with("RE("));
105            assert_eq!(s.performative, p);
106        }
107    }
108
109    #[test]
110    fn specific_spot_checks() {
111        let s = semantics(Performative::InputRequired);
112        assert!(s.feasibility_precondition.contains("cannot continue"));
113        let a = semantics(Performative::Accept);
114        assert!(a.feasibility_precondition.contains("status=proposed"));
115        let b = semantics(Performative::Bye);
116        assert!(b.feasibility_precondition.contains("no pending obligations"));
117    }
118}
119
120/// Tiny local iterator over all performatives (avoids a strum dependency).
121pub mod strum_like {
122    use crate::Performative;
123
124    pub fn all_performatives() -> impl Iterator<Item = Performative> {
125        use Performative::*;
126        [
127            Request, Inform, Query, Propose, Accept, Reject, Cancel, Progress,
128            InputRequired, Failure, Delegate, Error, Hello, Bye,
129        ]
130        .into_iter()
131    }
132}