fsmp 0.1.1

FSM Prompter — a CLI that steers AI agents through workflows by re-prompting them at each transition
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
//! Definition linter. Unlike `store::validate` (which hard-fails at the first
//! structural error so a broken guardrail can't be instantiated), the linter
//! collects *all* authoring problems in a parseable definition at once and adds
//! reachability analysis that `new` does not do. It operates on an already-parsed
//! `Definition` and never touches disk, so `lint` is a pure, unit-testable
//! function; the CLI feeds it `store::parse_definition` output.

use crate::model::Definition;
use serde::Serialize;
use std::collections::{HashSet, VecDeque};

/// A single authoring problem found in a definition. Each variant carries enough
/// location to render a precise, actionable message and to serialize under
/// `--json`. `snake_case` tagging yields stable `kind` strings for consumers.
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum Finding {
    /// `initial` names a state that isn't defined.
    UnknownInitial { initial: String },
    /// A transition `to:` targets a state that isn't defined.
    UnknownTarget {
        state: String,
        transition: String,
        target: String,
    },
    /// A state with no path from `initial` along transition targets.
    UnreachableState { state: String },
    /// A non-terminal state with no outgoing transitions — the agent arrives
    /// with no valid move and gets stuck.
    DeadEnd { state: String },
    /// A `terminal: true` state that also declares transitions; `do` refuses
    /// moves from a terminal state, so those edges are dead code.
    TerminalWithTransitions { state: String },
}

impl std::fmt::Display for Finding {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Finding::UnknownInitial { initial } => {
                write!(f, "initial: `{initial}` is not a defined state")
            }
            Finding::UnknownTarget {
                state,
                transition,
                target,
            } => write!(
                f,
                "{state}.{transition}: targets unknown state `{target}` — point it at a defined state"
            ),
            Finding::UnreachableState { state } => write!(
                f,
                "{state}: unreachable — no path to it from the initial state"
            ),
            Finding::DeadEnd { state } => write!(
                f,
                "{state}: dead end — non-terminal with no outgoing transitions (mark it `terminal: true` or add a transition)"
            ),
            Finding::TerminalWithTransitions { state } => write!(
                f,
                "{state}: terminal state declares transitions — dead code, since `do` refuses moves from a terminal state"
            ),
        }
    }
}

/// Analyze a parsed definition and return every problem found, in a
/// deterministic order: checks are emitted in a fixed sequence, and within each
/// check states/transitions are visited in definition (`IndexMap`) order.
pub fn lint(def: &Definition) -> Vec<Finding> {
    let mut findings = Vec::new();
    let initial_valid = def.states.contains_key(&def.initial);

    // 1. Unknown initial state.
    if !initial_valid {
        findings.push(Finding::UnknownInitial {
            initial: def.initial.clone(),
        });
    }

    // 2. Transitions targeting an undefined state.
    for (sname, state) in &def.states {
        for (tname, t) in &state.transitions {
            if !def.states.contains_key(&t.to) {
                findings.push(Finding::UnknownTarget {
                    state: sname.clone(),
                    transition: tname.clone(),
                    target: t.to.clone(),
                });
            }
        }
    }

    // 3. Unreachable states. Only meaningful with a valid entry point; when
    //    `initial` is unknown we skip this rather than flag every state (the
    //    UnknownInitial finding above is the actionable problem to fix first).
    if initial_valid {
        let reachable = reachable_from_initial(def);
        for sname in def.states.keys() {
            if !reachable.contains(sname) {
                findings.push(Finding::UnreachableState {
                    state: sname.clone(),
                });
            }
        }
    }

    // 4. Dead ends: a non-terminal state with no way out.
    for (sname, state) in &def.states {
        if !state.terminal && state.transitions.is_empty() {
            findings.push(Finding::DeadEnd {
                state: sname.clone(),
            });
        }
    }

    // 5. Terminal states carrying (unreachable) transitions.
    for (sname, state) in &def.states {
        if state.terminal && !state.transitions.is_empty() {
            findings.push(Finding::TerminalWithTransitions {
                state: sname.clone(),
            });
        }
    }

    findings
}

/// BFS the set of state names reachable from `initial` by following transition
/// `to:` edges (guards ignored — a guarded edge still makes its target reachable
/// in principle). Only traverses edges whose target is a defined state, so a
/// bogus `to:` never pollutes the reachable set. A terminal state's out-edges are
/// NOT traversed: `do` refuses moves from a terminal state, so those edges are
/// dead code (and separately flagged `TerminalWithTransitions`), and a state
/// reachable *only* through one is genuinely unreachable at runtime. Caller
/// ensures `initial` exists.
fn reachable_from_initial(def: &Definition) -> HashSet<String> {
    let mut visited = HashSet::new();
    let mut queue = VecDeque::new();
    visited.insert(def.initial.clone());
    queue.push_back(def.initial.clone());
    while let Some(name) = queue.pop_front() {
        if let Some(state) = def.states.get(&name) {
            if state.terminal {
                continue;
            }
            for t in state.transitions.values() {
                if def.states.contains_key(&t.to) && visited.insert(t.to.clone()) {
                    queue.push_back(t.to.clone());
                }
            }
        }
    }
    visited
}

/// Render findings as prose: one finding per line, then a summary line.
pub fn render_prose(findings: &[Finding]) -> String {
    if findings.is_empty() {
        return "clean — no problems found\n".to_string();
    }
    let mut out = String::new();
    for f in findings {
        out.push_str(&format!("{f}\n"));
    }
    let n = findings.len();
    out.push_str(&format!(
        "\n{n} problem{} found\n",
        if n == 1 { "" } else { "s" }
    ));
    out
}

/// Machine-readable equivalent of `render_prose`, for `--json` consumers.
pub fn to_json(findings: &[Finding]) -> serde_json::Value {
    serde_json::json!({
        "ok": findings.is_empty(),
        "findings": findings,
    })
}

#[cfg(test)]
mod tests {
    use super::*;

    /// Build a definition from YAML in-memory (no disk), mirroring how the
    /// real parser deserializes a definition file.
    fn def(yaml: &str) -> Definition {
        serde_yaml::from_str(yaml).expect("test definition should parse")
    }

    #[test]
    fn a_clean_definition_produces_no_findings() {
        let d = def("\
name: t
initial: a
states:
  a:
    transitions:
      go: { to: b }
  b:
    terminal: true
");
        assert_eq!(lint(&d), vec![]);
        assert!(to_json(&lint(&d))["ok"].as_bool().unwrap());
        assert!(render_prose(&lint(&d)).contains("clean"));
    }

    #[test]
    fn flags_an_unknown_initial_state() {
        let d = def("\
name: t
initial: nope
states:
  a:
    terminal: true
");
        assert_eq!(
            lint(&d),
            vec![Finding::UnknownInitial {
                initial: "nope".into()
            }]
        );
    }

    #[test]
    fn flags_a_transition_to_an_unknown_state() {
        let d = def("\
name: t
initial: a
states:
  a:
    transitions:
      go: { to: ghost }
");
        assert_eq!(
            lint(&d),
            vec![Finding::UnknownTarget {
                state: "a".into(),
                transition: "go".into(),
                target: "ghost".into()
            }]
        );
    }

    #[test]
    fn flags_an_unreachable_state() {
        // `b` is defined but nothing reaches it. `a` has a self-loop, so it is
        // not a dead end — keeping this case to exactly the one unreachable
        // finding.
        let d = def("\
name: t
initial: a
states:
  a:
    transitions:
      loop: { to: a }
  b:
    terminal: true
");
        assert_eq!(
            lint(&d),
            vec![Finding::UnreachableState { state: "b".into() }]
        );
    }

    #[test]
    fn flags_a_dead_end_but_not_a_terminal_with_no_transitions() {
        // `dead` is non-terminal with no exits → DeadEnd. `done` is terminal with
        // no exits → correct, no finding.
        let d = def("\
name: t
initial: a
states:
  a:
    transitions:
      stop: { to: done }
      stuck: { to: dead }
  dead: {}
  done:
    terminal: true
");
        assert_eq!(
            lint(&d),
            vec![Finding::DeadEnd {
                state: "dead".into()
            }]
        );
    }

    #[test]
    fn flags_a_terminal_state_that_declares_transitions() {
        let d = def("\
name: t
initial: a
states:
  a:
    transitions:
      done: { to: end }
  end:
    terminal: true
    transitions:
      back: { to: a }
");
        assert_eq!(
            lint(&d),
            vec![Finding::TerminalWithTransitions {
                state: "end".into()
            }]
        );
    }

    #[test]
    fn reports_an_unknown_target_and_an_unreachable_state_together() {
        // Proves lint collects rather than dying on the first structural error:
        // a's edge points nowhere (UnknownTarget) AND b is unreachable.
        let d = def("\
name: t
initial: a
states:
  a:
    transitions:
      go: { to: ghost }
  b:
    terminal: true
");
        let f = lint(&d);
        assert!(f.contains(&Finding::UnknownTarget {
            state: "a".into(),
            transition: "go".into(),
            target: "ghost".into()
        }));
        assert!(f.contains(&Finding::UnreachableState { state: "b".into() }));
        assert_eq!(f.len(), 2);
    }

    #[test]
    fn unknown_initial_does_not_cascade_into_unreachable_for_every_state() {
        // With no valid entry point, reachability is undefined; we emit the one
        // actionable UnknownInitial finding and do not flood every state.
        let d = def("\
name: t
initial: nope
states:
  a:
    transitions:
      go: { to: b }
  b:
    terminal: true
");
        let f = lint(&d);
        assert_eq!(
            f,
            vec![Finding::UnknownInitial {
                initial: "nope".into()
            }]
        );
    }

    #[test]
    fn a_state_reachable_only_through_a_broken_edge_is_still_unreachable() {
        // a → ghost (broken); c is only "reachable" via ghost, which isn't a real
        // node, so c stays unreachable. UnknownTarget and UnreachableState are
        // distinct findings, not a cascade.
        let d = def("\
name: t
initial: a
states:
  a:
    transitions:
      go: { to: ghost }
  c:
    terminal: true
");
        let f = lint(&d);
        assert_eq!(f.len(), 2);
        assert!(f.contains(&Finding::UnknownTarget {
            state: "a".into(),
            transition: "go".into(),
            target: "ghost".into()
        }));
        assert!(f.contains(&Finding::UnreachableState { state: "c".into() }));
    }

    #[test]
    fn a_state_reachable_only_through_a_terminal_states_dead_edge_is_unreachable() {
        // `do` never fires a transition out of a terminal state, so `t`'s edge to
        // `x` is dead code. Reachability must not traverse it: `x` is reachable
        // ONLY through that dead edge, so it is genuinely unreachable at runtime.
        // `t` is independently flagged TerminalWithTransitions.
        let d = def("\
name: t
initial: a
states:
  a:
    transitions:
      go: { to: t }
  t:
    terminal: true
    transitions:
      dead: { to: x }
  x:
    terminal: true
");
        let f = lint(&d);
        assert!(
            f.contains(&Finding::UnreachableState { state: "x".into() }),
            "x should be flagged unreachable, got: {f:?}"
        );
        assert!(f.contains(&Finding::TerminalWithTransitions { state: "t".into() }));
        assert_eq!(f.len(), 2);
    }

    #[test]
    fn prose_summary_is_singular_for_one_and_plural_for_many() {
        let one = vec![Finding::DeadEnd { state: "x".into() }];
        assert!(render_prose(&one).contains("1 problem found"));
        let two = vec![
            Finding::DeadEnd { state: "x".into() },
            Finding::DeadEnd { state: "y".into() },
        ];
        assert!(render_prose(&two).contains("2 problems found"));
    }
}