Skip to main content

acl_core/
dialogue.rs

1//! Dialogue rules (spec §16): normative conversation policy.
2//!
3//! A language has discourse structure: after a given performative, only
4//! certain replies are legal. `dialogue_legal(prev, reply)` is the machine-
5//! checkable form; receivers SHOULD reject off-policy replies, sessions
6//! MAY enforce it strictly.
7
8use crate::Performative;
9
10/// Replies legal immediately after `prev` (the client's turn) — the
11/// server's legal next moves.
12pub fn legal_replies(prev: Performative) -> &'static [Performative] {
13    use Performative::*;
14    match prev {
15        Request => &[Inform, InputRequired, Failure, Reject, Propose, Progress, Error, Bye],
16        Delegate => &[Inform, InputRequired, Failure, Reject, Propose, Progress, Error, Bye],
17        Query => &[Inform, Failure, Error, Bye],
18        Propose => &[Accept, Reject, Error, Bye],
19        Accept => &[Inform, Failure, Error, Bye],
20        Cancel => &[Inform, Error, Bye],
21        Inform => &[Inform, Error, Retract, Bye],
22        Hello => &[Hello, Error, Bye],
23        InputRequired => &[Inform, Cancel, Error, Bye],
24        Progress => &[Progress, Inform, Failure, InputRequired, Error, Bye],
25        Failure => &[Inform, Error, Bye],
26        Reject => &[Inform, Error, Bye],
27        Bye => &[],
28        Error => &[Inform, Error, Bye],
29        Retract => &[Inform, Error, Bye],
30        Subscribe => &[Inform, Failure, Error, Bye],
31        Unsubscribe => &[Inform, Error, Bye],
32    }
33}
34
35/// Is `reply` a legal move after `prev`?
36pub fn dialogue_legal(prev: Performative, reply: Performative) -> bool {
37    legal_replies(prev).contains(&reply)
38}
39
40/// §16 enforcement: tracks the last performative this side SENT and
41/// validates each incoming reply against the matrix. Off-policy replies
42/// are reported (receivers SHOULD reject them; strict enforcement is a
43/// session policy choice).
44#[derive(Debug, Default, Clone)]
45pub struct DialogueTracker {
46    /// last performative this side sent (the one awaiting a reply)
47    pub last_out: Option<Performative>,
48    /// session openers always reset expectations (§16: new conversation)
49    openers: bool,
50}
51
52impl DialogueTracker {
53    pub fn new() -> Self {
54        Self::default()
55    }
56
57    /// record an outgoing performative (it now awaits a legal reply)
58    pub fn on_send(&mut self, p: Performative) {
59        self.last_out = Some(p);
60    }
61
62    /// validate an incoming reply against what we last sent.
63    /// `Ok(true)` = legal reply, `Ok(false)` = no expectation open
64    /// (peer opened a new conversation — legal), `Err` = off-policy.
65    pub fn on_receive(&self, reply: Performative) -> Result<bool, (Performative, Performative)> {
66        match self.last_out {
67            None => Ok(true), // peer opened the conversation
68            Some(prev) => {
69                if dialogue_legal(prev, reply) {
70                    Ok(true)
71                } else {
72                    Err((prev, reply))
73                }
74            }
75        }
76    }
77
78    /// clear the expectation (e.g. after receiving the final answer)
79    pub fn close(&mut self) {
80        self.last_out = None;
81    }
82}
83
84#[cfg(test)]
85mod tests {
86    use super::*;
87    use Performative::*;
88
89    #[test]
90    fn tracker_rejects_off_policy_replies() {
91        let mut t = DialogueTracker::new();
92        // we sent a Propose: peer may only Accept/Reject/Error/Bye
93        t.on_send(Performative::Propose);
94        assert!(t.on_receive(Performative::Accept).is_ok());
95        assert!(t.on_receive(Performative::Inform).is_err());
96        assert!(t.on_receive(Performative::Request).is_err());
97        // no expectation: anything is legal (peer opened)
98        let t2 = DialogueTracker::new();
99        assert!(t2.on_receive(Performative::Request).is_ok());
100        // close clears
101        t.close();
102        assert!(t.on_receive(Performative::Request).is_ok());
103    }
104
105    #[test]
106    fn core_matrix() {
107        // request: answered by result/agreement-to-wait/refusal/progress
108        assert!(dialogue_legal(Request, Inform));
109        assert!(dialogue_legal(Request, InputRequired));
110        assert!(dialogue_legal(Request, Failure));
111        assert!(dialogue_legal(Request, Reject));
112        assert!(dialogue_legal(Request, Progress));
113        assert!(!dialogue_legal(Request, Hello));
114        assert!(!dialogue_legal(Request, Accept));
115        // §34: any participant may close the session — Bye is legal
116        // after anything except after Bye itself
117        assert!(dialogue_legal(Request, Bye));
118
119        // query must be answered with information or failure — never Accept
120        assert!(dialogue_legal(Query, Inform));
121        assert!(!dialogue_legal(Query, InputRequired));
122        assert!(!dialogue_legal(Query, Progress));
123
124        // propose: only accept/reject — a result would be off-policy
125        assert!(dialogue_legal(Propose, Accept));
126        assert!(dialogue_legal(Propose, Reject));
127        assert!(!dialogue_legal(Propose, Inform));
128
129        // input_required: the client answers inform or cancels
130        assert!(dialogue_legal(InputRequired, Inform));
131        assert!(dialogue_legal(InputRequired, Cancel));
132        assert!(!dialogue_legal(InputRequired, Failure));
133
134        // bye closes: nothing may follow
135        assert!(legal_replies(Bye).is_empty());
136        assert!(!dialogue_legal(Bye, Inform));
137        // session termination is always available (except after bye)
138        for p in [Request, Inform, Propose, Accept, Failure, Error, Query] {
139            assert!(dialogue_legal(p, Bye), "Bye must be legal after {p:?}");
140        }
141
142        // error: the peer may recover with inform
143        assert!(dialogue_legal(Error, Inform));
144        assert!(!dialogue_legal(Error, Hello));
145    }
146
147    #[test]
148    fn progress_chain() {
149        // a working agent keeps reporting progress until terminal
150        assert!(dialogue_legal(Progress, Progress));
151        assert!(dialogue_legal(Progress, Inform));
152        assert!(dialogue_legal(Progress, Failure));
153        assert!(dialogue_legal(Progress, InputRequired));
154    }
155}