supercode-interchange 0.5.79

Canonical, provider-neutral session interchange primitives for Volter Harness
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
//! How a board is worked (docs/architecture/orchestrator.md §2.9): the whole dispatch behaviour as data, a
//! statechart in the manner of Amazon States Language and XState. A card is always in one of the workflow's
//! statuses; a status may run an actor for as long as the card is in it (XState's `invoke`, ASL's `Task`); events
//! move cards between statuses through transitions that say who may send them, what must hold, and what happens
//! (Jira's conditions, validators and post functions); the dispatcher's scheduling is data too.
//!
//! Nothing here is a mode or a switch: a behaviour exists because a transition, an action or an expression says
//! so. Names (statuses, events, roles, slots, actions) are open strings; conditions, targets, limits and
//! durations are expressions over the card and the workflow's `params`, evaluated by the board engine. Hermes's
//! dispatcher is one instance of this model, [`hermes_instance`], used by a home that declares none.

use std::collections::BTreeMap;

use schemars::JsonSchema;
use serde::de::Error as _;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde_json::{Map, Value};

/// An expression over the card, its relations, the event and `params` (a small CEL-like language: literals,
/// `card.*`, `event.*`, `params.*`, comparisons, `&&`/`||`/`!`, `in`, and the engine's functions such as
/// `parents.all(p, …)`). Written as a string; a bare number, boolean or null is the literal it spells.
#[derive(Debug, Clone, PartialEq, Eq, JsonSchema)]
#[schemars(transparent)]
pub struct Expr(pub String);

impl Serialize for Expr {
    fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
        self.0.serialize(s)
    }
}

impl<'de> Deserialize<'de> for Expr {
    fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
        match Value::deserialize(d)? {
            Value::String(s) => Ok(Self(s)),
            v @ (Value::Number(_) | Value::Bool(_) | Value::Null) => Ok(Self(v.to_string())),
            other => Err(D::Error::custom(format!(
                "an expression is a string or a literal, not {other}"
            ))),
        }
    }
}

/// One step a transition, an entry or an exit takes: a primitive the engine provides, named, with its arguments
/// (`notify: {roles: [creator], template: …}`). Written as a one-key map.
#[derive(Debug, Clone, PartialEq, Eq, JsonSchema)]
pub struct Action {
    /// The primitive (`assign`, `count`, `reset`, `notify`, `comment`, `send_to_actor`, `close_session`, …).
    pub name: String,
    /// Its arguments, as written.
    pub args: Value,
}

impl Serialize for Action {
    fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
        let mut m = Map::new();
        m.insert(self.name.clone(), self.args.clone());
        Value::Object(m).serialize(s)
    }
}

impl<'de> Deserialize<'de> for Action {
    fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
        match Value::deserialize(d)? {
            Value::String(name) => Ok(Self {
                name,
                args: Value::Null,
            }),
            Value::Object(m) if m.len() == 1 => {
                let (name, args) = m.into_iter().next().unwrap();
                Ok(Self { name, args })
            }
            other => Err(D::Error::custom(format!(
                "an action is a name or a one-key map, not {other}"
            ))),
        }
    }
}

/// A move from the card's status on an event (or on none: `always`, `after`).
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct Transition {
    /// Source statuses; empty applies from every status.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub from: Vec<String>,
    /// Who may send the event: role names (the workflow's `roles`, and the engine's `platform` and `manager`).
    /// Empty: anyone who may write the card.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub by: Vec<String>,
    /// The transition is taken only when this holds (Jira's condition); the first one that holds is taken.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub guard: Option<Expr>,
    /// Must hold, else the event is refused with the message (Jira's validator).
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub validate: Vec<Validation>,
    /// The status the card moves to: a status name, or `{{expr}}`; none stays where it is (runs the actions only).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub target: Option<String>,
    /// What happens, in order, after the move (Jira's post functions).
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub actions: Vec<Action>,
    /// What a person reads about it (the event's door text, the log).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
}

/// A validator: the expression must hold, else the event is refused with the message.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct Validation {
    /// What must hold.
    pub expr: Expr,
    /// The refusal.
    pub message: String,
}

/// A transition taken once a duration has passed in the status (XState's `after`, ASL's `Wait`).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct Delayed {
    /// How long, in seconds (an expression).
    pub after: Expr,
    /// The move.
    #[serde(flatten)]
    pub transition: Transition,
}

/// What runs while a card is in a status: an actor of a role's lane in a named session slot of the card.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct Run {
    /// The role whose lane (the profile it names) runs it.
    pub role: String,
    /// The card's session slot it runs in: a live session in the slot is kept, a lost one resumed, an empty slot
    /// started fresh. Different slots are different sessions (a reviewer never works in the implementer's).
    pub session: String,
    /// What the actor is told when it starts: a template over the card (`{{card.id}}`, `{{context}}`) and
    /// `{{doors}}`, the events the actor may send in this status, generated from its transitions.
    #[serde(default)]
    pub prompt: String,
    /// Prompt file relative to the orchestration home.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub prompt_file: Option<String>,
    /// Skills the actor's session is given.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub skills: Vec<String>,
    /// Limits on the run, each an expression in seconds or a count (`runtime`, `heartbeat`, …); the engine emits
    /// an event when one passes (`timed_out`, `stale`, …).
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub limits: BTreeMap<String, Expr>,
}

/// One status a card can be in.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct Status {
    /// What `kanban.db`'s `tasks.status` holds for a card here, so Hermes reads the board (Hermes's own nine
    /// statuses store as themselves); the exact name rides beside it where it differs.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub stored_as: Option<String>,
    /// What a person reads about it.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// What runs while a card is here.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub run: Option<Run>,
    /// A specification in the run machine's roles, without repeating supervision.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub run_role: Option<String>,
    /// Status used when a failed or blocked run is later resumed.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub resume_as: Option<String>,
    /// Done on entering (after the move's own actions).
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub entry: Vec<Action>,
    /// Done on leaving (before the move's own actions): e.g. `close_session: reviewer`.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub exit: Vec<Action>,
    /// Events and their transitions, the first whose guard holds taken.
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub on: BTreeMap<String, Vec<Transition>>,
    /// Transitions taken as soon as their guard holds (XState's `always`).
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub always: Vec<Transition>,
    /// Transitions taken after a time in the status.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub after: Vec<Delayed>,
}

/// A cap on what the dispatcher starts: cards counted per group, at most `max` in each.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct Limit {
    /// What the cap is about, for its message.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// The group a card falls in (GitHub's `concurrency.group`): `'home'`, `card.board`, `card.assignee`, …
    pub group: Expr,
    /// Which cards count against it.
    pub counts: Expr,
    /// The cap; a cap that evaluates to nothing caps nothing.
    pub max: Expr,
    /// Slots held back for the cards this matches while any waits (Hermes's review reservation).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub reserve: Option<Reserve>,
}

/// Slots of a limit held for some cards.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct Reserve {
    /// The cards the slots are held for.
    #[serde(rename = "for")]
    pub for_cards: Expr,
    /// How many.
    pub slots: Expr,
}

/// The dispatcher: when it runs, what it may start, in what order, under which caps.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct Dispatch {
    /// Whether the served home dispatches its boards (an expression; Hermes's `dispatch_in_gateway`).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub serve: Option<Expr>,
    /// Seconds between ticks.
    pub tick: Expr,
    /// The event the dispatcher sends a card it starts (to cards whose status has a transition for it).
    pub start: String,
    /// Which cards it may start this tick.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub eligible: Option<Expr>,
    /// The order it starts them in: sort keys, `-` before one for descending.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub order: Vec<Expr>,
    /// The caps.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub limits: Vec<Limit>,
}

/// The independently supervised run machine. Its events never become card notices.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct RunMachine {
    /// Run specification selected for a card with an Every interval.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub recurring_role: Option<String>,
    /// Shared supervision limits, overridden by individual role specifications.
    #[serde(default)]
    pub limits: BTreeMap<String, Expr>,
    /// Named run specifications, selected by a card status or a recurring role.
    #[serde(default)]
    pub roles: BTreeMap<String, Run>,
    /// Run states and their entry, exit and event rules.
    #[serde(default)]
    pub states: BTreeMap<String, Status>,
    /// Initial run state.
    #[serde(default)]
    pub initial: String,
    /// Supervision shared by all run states.
    #[serde(default)]
    pub on: BTreeMap<String, Vec<Transition>>,
    /// A run outcome emits this card verb (the sole link between machines).
    #[serde(default)]
    pub outcomes: BTreeMap<String, Vec<Transition>>,
}

/// Ordered lowering rule for Hermes's fixed dispatcher.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct StorageRule {
    pub guard: Expr,
    pub status: String,
}

/// A board's workflow.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct Workflow {
    /// The run machine; absent for legacy homes during migration.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub run: Option<RunMachine>,
    /// Card verbs declared once, with source statuses on each transition.
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub verbs: BTreeMap<String, Vec<Transition>>,
    /// Derived card conditions, such as ready.
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub derived: BTreeMap<String, Expr>,
    /// Relation policies, interpreted by the engine's relation primitives.
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub relations: BTreeMap<String, Value>,
    /// Flag policies, interpreted by the engine's flag primitives.
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub flags: BTreeMap<String, Value>,
    /// Ordered Hermes projection and its explicitly accepted losses.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub stored_as: Vec<StorageRule>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub losses: Vec<String>,
    /// What a person reads about it.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// Named values the expressions read (`params.failure_limit`); a home's config.yaml `kanban:` keys override
    /// them by name, so Hermes's settings are this workflow's parameters.
    #[serde(default, skip_serializing_if = "Map::is_empty")]
    pub params: Map<String, Value>,
    /// Who acts on a card, each an expression naming a profile (`implementer: card.assignee`) or a person.
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub roles: BTreeMap<String, Expr>,
    /// Where a new card starts: the first transition whose guard holds.
    pub initial: Vec<Transition>,
    /// Every status, by name.
    pub statuses: BTreeMap<String, Status>,
    /// Events that apply in every status (a status's own `on` for the same event is tried first).
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub on: BTreeMap<String, Vec<Transition>>,
    /// The dispatcher.
    pub dispatch: Dispatch,
    /// Who is told what: for each audience (`creator`, `worker`, `dependency`, `chat`: who a card's subscriptions
    /// are), the event kinds it hears and the template each is told in. A kind not named is not sent.
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub notify: BTreeMap<String, BTreeMap<String, String>>,
}

/// Hermes's dispatcher, transcribed as data (`hermes_cli/kanban_db.py`): the workflow a home runs when it
/// declares none.
pub fn hermes_instance() -> Workflow {
    let mut workflow: Workflow =
        serde_yaml::from_str(HERMES_WORKFLOW).expect("the built-in Hermes workflow parses");
    if let Some(run) = &mut workflow.run {
        for (role, prompt) in [
            ("triager", include_str!("prompts/triager.md")),
            ("implementer", include_str!("prompts/implementer.md")),
            ("reviewer", include_str!("prompts/reviewer.md")),
        ] {
            if let Some(spec) = run.roles.get_mut(role) {
                spec.prompt = prompt.to_owned();
            }
        }
    }
    workflow
}

/// The built-in Hermes workflow's source.
pub const HERMES_WORKFLOW: &str = include_str!("hermes.workflow.yaml");

impl Workflow {
    /// Every name a transition targets or a run names must be declared; answers what is not.
    pub fn check(&self) -> Result<(), String> {
        let known = |t: &Option<String>| match t {
            Some(name) if !name.starts_with("{{") && !self.statuses.contains_key(name) => {
                Err(format!("no status {name}"))
            }
            _ => Ok(()),
        };
        let all = |ts: &[Transition]| ts.iter().try_for_each(|t| known(&t.target));
        all(&self.initial)?;
        for ts in self.verbs.values() {
            all(ts)?;
            for t in ts {
                for source in &t.from {
                    if !self.statuses.contains_key(source) {
                        return Err(format!("no source status {source}"));
                    }
                }
            }
        }
        if let Some(run) = &self.run {
            if !run.states.contains_key(&run.initial) {
                return Err(format!("no initial run state {}", run.initial));
            }
            for ts in run.outcomes.values() {
                all(ts)?;
            }
            for ts in run
                .on
                .values()
                .chain(run.states.values().flat_map(|s| s.on.values()))
            {
                for t in ts {
                    if let Some(target) = &t.target {
                        if !target.starts_with("{{") && !run.states.contains_key(target) {
                            return Err(format!("no run state {target}"));
                        }
                    }
                }
            }
            for (name, spec) in &run.roles {
                if !self.roles.contains_key(&spec.role) {
                    return Err(format!("run {name}: no role {}", spec.role));
                }
            }
        }
        for ts in self.on.values() {
            all(ts)?;
        }
        for (name, s) in &self.statuses {
            if let Some(role) = &s.run_role {
                if !self
                    .run
                    .as_ref()
                    .is_some_and(|r| r.roles.contains_key(role))
                {
                    return Err(format!("{name}: no run specification {role}"));
                }
            }
            for ts in s.on.values() {
                all(ts).map_err(|e| format!("{name}: {e}"))?;
            }
            all(&s.always).map_err(|e| format!("{name}: {e}"))?;
            s.after
                .iter()
                .try_for_each(|d| known(&d.transition.target))
                .map_err(|e| format!("{name}: {e}"))?;
            if let Some(run) = &s.run {
                if !self.roles.contains_key(&run.role) {
                    return Err(format!("{name}: its run names no role {}", run.role));
                }
            }
        }
        Ok(())
    }
}