koi-common 0.2.202603241449

Shared types, traits, and utilities for the Koi local network toolkit
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
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
//! Generic server-driven ceremony framework.
//!
//! A **ceremony** is a server-controlled dialogue between a server and a
//! client (CLI, web UI, SDK). The server owns validation, branching, and
//! all domain logic. Clients are dumb render loops - they display
//! whatever the server sends, collect input, and post it back.
//!
//! # Core model: bag of key-value + rules
//!
//! A ceremony is **not** a linear pipeline of stages. It is:
//!
//! - A **bag** of key-value pairs (the session state), and
//! - A **rules function** that inspects the bag and decides what to do next.
//!
//! ```text
//! evaluate(bag, render_hints) → { prompts[] + messages[] | complete | fatal }
//! ```
//!
//! There is no stage index, no forward/backward cursor. The session is
//! just a `Map<String, Value>` and the rules are a pure function over it.
//! Every time the client submits data, it is merged into the bag, and the
//! rules are re-evaluated.
//!
//! # Architecture
//!
//! ```text
//! ┌──────────┐        ┌──────────────┐        ┌────────────────┐
//! │  Client   │ ←────→ │ CeremonyHost │ ←────→ │ CeremonyRules  │
//! │ (render   │ step() │ (sessions,   │ eval() │ (domain-       │
//! │  loop)    │        │  lifecycle)  │        │  specific)     │
//! └──────────┘        └──────────────┘        └────────────────┘
//! ```
//!
//! The [`CeremonyHost`] manages sessions and delegates evaluation to a
//! [`CeremonyRules`] implementation. Each domain (certmesh, storage,
//! companions, etc.) provides its own `CeremonyRules`.
//!
//! # Usage
//!
//! ```ignore
//! // 1. Implement CeremonyRules for your domain
//! impl CeremonyRules for PondRules {
//!     fn validate_ceremony_type(&self, ceremony: &str) -> Result<(), String> { ... }
//!     fn evaluate(&self, ceremony_type: &str, bag: &mut Map<String, Value>,
//!                 render: &RenderHints) -> EvalResult { ... }
//! }
//!
//! // 2. Create a host and call step()
//! let host = CeremonyHost::new(rules);
//! let response = host.step(CeremonyRequest {
//!     ceremony: Some("init".into()),
//!     data: serde_json::Map::new(),
//!     ..Default::default()
//! });
//! ```

use std::collections::HashMap;
use std::sync::Mutex;
use std::time::{Duration, Instant};

use serde::{Deserialize, Serialize};
use uuid::Uuid;

// ── Configuration ───────────────────────────────────────────────────

/// Default session time-to-live (5 minutes).
const DEFAULT_SESSION_TTL: Duration = Duration::from_secs(300);

/// Default sweep interval for expired sessions (60 seconds).
/// Consumers spawn a background task at this interval calling
/// [`CeremonyHost::sweep_expired`].
pub const SESSION_SWEEP_INTERVAL: Duration = Duration::from_secs(60);

// ── Render hints ────────────────────────────────────────────────────

/// Client-provided hints for how the server should render rich content.
///
/// Sent per-request so different clients (CLI vs browser) get appropriate
/// output without the server needing to know who's calling.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct RenderHints {
    /// Preferred QR code format. Absent = server's default.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub qr: Option<QrFormat>,
}

/// QR code rendering format.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum QrFormat {
    /// Unicode block characters for terminal display.
    #[default]
    Utf8,
    /// Base64-encoded PNG for `<img src="data:image/png;base64,...">`.
    PngBase64,
    /// Raw URI only - no visual rendering.
    UriOnly,
}

// ── Protocol types (wire format) ────────────────────────────────────

/// Inbound ceremony request from the client.
///
/// This is the universal request shape for every ceremony step.
/// The client sends key-value data which is merged into the session bag.
#[derive(Debug, Default, Serialize, Deserialize)]
pub struct CeremonyRequest {
    /// Session ID from a previous response. `None` to start a new ceremony.
    #[serde(default)]
    pub session_id: Option<Uuid>,

    /// Ceremony type identifier (e.g. "init", "join").
    /// Required on the first call; ignored on subsequent calls.
    #[serde(default)]
    pub ceremony: Option<String>,

    /// Key-value pairs to merge into the session bag.
    /// On the first call this can carry prefill data from CLI flags.
    /// On subsequent calls this carries the user's answers to prompts.
    #[serde(default)]
    pub data: serde_json::Map<String, serde_json::Value>,

    /// Client render preferences.
    #[serde(default)]
    pub render: Option<RenderHints>,
}

/// Outbound ceremony response to the client.
///
/// Contains prompts (what to ask the user), messages (what to show),
/// completion status, and any errors.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CeremonyResponse {
    /// Session ID - include in the next request.
    pub session_id: Uuid,

    /// Data the server wants the client to collect.
    /// Empty only when `complete` is true or a fatal error occurred.
    pub prompts: Vec<Prompt>,

    /// Informational content to display (instructions, QR codes, summaries).
    /// Can appear alongside prompts.
    pub messages: Vec<Message>,

    /// True when the ceremony is finished (success or fatal error).
    pub complete: bool,

    /// Validation or fatal error detail.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub error: Option<String>,

    /// The final bag state when the ceremony completes.
    /// Only present when `complete` is true and no fatal error occurred.
    /// Contains all collected data including internal keys (prefixed `_`).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub result_data: Option<serde_json::Map<String, serde_json::Value>>,
}

// ── Prompts ─────────────────────────────────────────────────────────

/// A single data request - tells the client exactly one thing to collect.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Prompt {
    /// The bag key this prompt populates.
    pub key: String,

    /// Human-readable question or instruction.
    pub prompt: String,

    /// What kind of input widget the client should render.
    pub input_type: InputType,

    /// Options for `SelectOne` or `SelectMany` input types.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub options: Vec<SelectOption>,

    /// Whether the user must provide a value.
    #[serde(default = "default_true")]
    pub required: bool,
}

fn default_true() -> bool {
    true
}

/// A selectable option within a `SelectOne` or `SelectMany` prompt.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SelectOption {
    /// The value stored in the bag when selected.
    pub value: String,
    /// Display label.
    pub label: String,
    /// Optional description shown below the label.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
}

/// The kind of input widget a prompt requires.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum InputType {
    /// Pick exactly one from `options`.
    SelectOne,
    /// Pick one or more from `options`.
    SelectMany,
    /// Free text input.
    Text,
    /// Masked text input (passphrases).
    Secret,
    /// Two masked inputs that must match (passphrase + confirmation).
    SecretConfirm,
    /// Short numeric/alphanumeric code (TOTP verification).
    Code,
    /// Raw entropy input (keyboard mashing, mouse movement).
    Entropy,
    /// Hardware key interaction (WebAuthn).
    Fido2,
}

// ── Messages ────────────────────────────────────────────────────────

/// An informational display item - not an input.
///
/// Messages carry content to show the user without requiring input.
/// They can appear alongside prompts (e.g., QR code + code input).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Message {
    /// What kind of content this is.
    pub kind: MessageKind,

    /// Short title or heading.
    pub title: String,

    /// The content body (plain text, base64 image, JSON summary, etc.).
    pub content: String,
}

/// Message content type.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum MessageKind {
    /// Plain text instruction or guidance.
    Info,
    /// QR code image (format per `RenderHints::qr`).
    QrCode,
    /// Key-value summary of completed ceremony data.
    Summary,
    /// Error detail with context (non-fatal).
    Error,
}

// ── Session ─────────────────────────────────────────────────────────

/// A live ceremony session - just a bag of key-value pairs plus metadata.
///
/// There is no stage index, no stage name, no progress counter.
/// The [`CeremonyRules`] derive everything from the bag contents.
pub struct Session {
    /// Unique session identifier (UUIDv7).
    pub id: Uuid,

    /// Ceremony type identifier string (e.g. "init", "join").
    pub ceremony_type: String,

    /// The accumulated key-value data. Rules read and write this.
    pub bag: serde_json::Map<String, serde_json::Value>,

    /// Client render hints (from the most recent request).
    pub render: RenderHints,

    /// Monotonic timestamp of creation.
    pub created_at: Instant,

    /// Monotonic timestamp of last activity.
    pub last_active: Instant,

    /// Whether this ceremony has completed.
    pub complete: bool,
}

impl Session {
    /// Store a value in the bag.
    pub fn set(&mut self, key: impl Into<String>, value: serde_json::Value) {
        self.bag.insert(key.into(), value);
    }

    /// Get a value from the bag.
    pub fn get(&self, key: &str) -> Option<&serde_json::Value> {
        self.bag.get(key)
    }

    /// Get a string value from the bag.
    pub fn get_str(&self, key: &str) -> Option<&str> {
        self.bag.get(key).and_then(|v| v.as_str())
    }

    /// Check whether a key exists in the bag.
    pub fn has(&self, key: &str) -> bool {
        self.bag.contains_key(key)
    }

    /// Remove a key from the bag (e.g. to force re-collection on conflict).
    pub fn remove(&mut self, key: &str) -> Option<serde_json::Value> {
        self.bag.remove(key)
    }
}

// ── Eval result ─────────────────────────────────────────────────────

/// Result of evaluating the ceremony rules against the current bag.
///
/// Returned by [`CeremonyRules::evaluate`] to tell the host what to
/// present to the client next.
pub enum EvalResult {
    /// The ceremony needs more data. Return prompts and optional messages.
    NeedInput {
        /// Data the client should collect.
        prompts: Vec<Prompt>,
        /// Informational content to display alongside prompts.
        messages: Vec<Message>,
    },

    /// Re-prompt with a validation error. The client shows the error
    /// and re-renders the prompts for the user to correct.
    ValidationError {
        /// Prompts to re-display (typically the offending fields).
        prompts: Vec<Prompt>,
        /// Informational messages.
        messages: Vec<Message>,
        /// Human-readable error description.
        error: String,
    },

    /// The bag is complete and consistent. The ceremony is done.
    Complete {
        /// Final messages (summary, results, etc.).
        messages: Vec<Message>,
    },

    /// Something is terminally wrong (I/O failure, impossible state).
    Fatal(String),
}

// ── Ceremony rules trait ────────────────────────────────────────────

/// Domain-specific ceremony rules.
///
/// Each domain (certmesh, storage, companions, etc.) implements this
/// trait to define its ceremony types and evaluation logic.
///
/// The rules function is essentially:
/// ```text
/// evaluate(ceremony_type, bag, render_hints) → EvalResult
/// ```
///
/// Rules inspect the bag and decide what data is still needed, whether
/// existing data conflicts, or whether the ceremony is complete.
///
/// # Thread safety
///
/// The host calls `evaluate` while holding a session lock. Keep
/// implementations fast - do heavy I/O before returning, or collect
/// parameters here and execute in a post-step hook.
pub trait CeremonyRules: Send + Sync {
    /// Validate a ceremony type string.
    ///
    /// Return `Ok(())` if the string is a known ceremony type,
    /// or `Err("message")` if it isn't.
    fn validate_ceremony_type(&self, ceremony: &str) -> Result<(), String>;

    /// Evaluate the bag and determine what happens next.
    ///
    /// The rules may read and write the bag (e.g. to inject derived keys
    /// like `_totp_secret`, or to remove conflicting keys). The bag
    /// already contains any data the client sent in this request -
    /// it was merged before `evaluate` is called.
    fn evaluate(
        &self,
        ceremony_type: &str,
        bag: &mut serde_json::Map<String, serde_json::Value>,
        render: &RenderHints,
    ) -> EvalResult;
}

// ── Ceremony host ───────────────────────────────────────────────────

/// Generic ceremony host - manages sessions and delegates evaluation
/// to a [`CeremonyRules`] implementation.
///
/// Thread-safe. One host per domain, shared across HTTP handlers.
pub struct CeremonyHost<R: CeremonyRules> {
    rules: R,
    sessions: Mutex<HashMap<Uuid, Session>>,
    session_ttl: Duration,
}

impl<R: CeremonyRules> CeremonyHost<R> {
    /// Create a new ceremony host with the given domain rules.
    pub fn new(rules: R) -> Self {
        Self {
            rules,
            sessions: Mutex::new(HashMap::new()),
            session_ttl: DEFAULT_SESSION_TTL,
        }
    }

    /// Create a ceremony host with a custom session TTL.
    pub fn with_ttl(rules: R, ttl: Duration) -> Self {
        Self {
            rules,
            sessions: Mutex::new(HashMap::new()),
            session_ttl: ttl,
        }
    }

    /// Access the domain rules (e.g. for diagnostics or testing).
    pub fn rules(&self) -> &R {
        &self.rules
    }

    /// Process a ceremony step.
    ///
    /// - If `session_id` is `None`, creates a new session, merges
    ///   `data` into the bag, evaluates the rules, and returns prompts.
    /// - If `session_id` is `Some`, merges `data` into the existing
    ///   session bag, re-evaluates the rules, and returns prompts.
    pub fn step(&self, request: CeremonyRequest) -> Result<CeremonyResponse, CeremonyError> {
        match request.session_id {
            None => self.start_new(request),
            Some(id) => self.continue_existing(id, request),
        }
    }

    /// Remove expired sessions. Call periodically from a background task.
    /// Returns the number of sessions removed.
    pub fn sweep_expired(&self) -> usize {
        let mut sessions = self.sessions.lock().unwrap_or_else(|e| {
            tracing::warn!("ceremony session lock was poisoned, recovering");
            e.into_inner()
        });
        let now = Instant::now();
        let before = sessions.len();
        sessions.retain(|_id, session| now.duration_since(session.last_active) < self.session_ttl);
        let removed = before - sessions.len();
        if removed > 0 {
            tracing::debug!(
                removed,
                remaining = sessions.len(),
                "Swept expired ceremony sessions"
            );
        }
        removed
    }

    /// Number of active sessions (for diagnostics).
    pub fn active_session_count(&self) -> usize {
        self.sessions
            .lock()
            .unwrap_or_else(|e| {
                tracing::warn!("ceremony session lock was poisoned, recovering");
                e.into_inner()
            })
            .len()
    }

    // ── Internal ────────────────────────────────────────────────────

    fn start_new(&self, request: CeremonyRequest) -> Result<CeremonyResponse, CeremonyError> {
        let ceremony = request
            .ceremony
            .as_deref()
            .ok_or_else(|| CeremonyError::MissingField("ceremony".into()))?;

        self.rules
            .validate_ceremony_type(ceremony)
            .map_err(CeremonyError::InvalidCeremony)?;

        let render = request.render.unwrap_or_default();
        let now = Instant::now();

        let mut session = Session {
            id: Uuid::now_v7(),
            ceremony_type: ceremony.to_string(),
            bag: request.data,
            render: render.clone(),
            created_at: now,
            last_active: now,
            complete: false,
        };

        let result = self.rules.evaluate(ceremony, &mut session.bag, &render);
        self.finalize(session, result)
    }

    fn continue_existing(
        &self,
        session_id: Uuid,
        request: CeremonyRequest,
    ) -> Result<CeremonyResponse, CeremonyError> {
        let mut sessions = self.sessions.lock().unwrap_or_else(|e| {
            tracing::warn!("ceremony session lock was poisoned, recovering");
            e.into_inner()
        });

        let session = sessions
            .get_mut(&session_id)
            .ok_or(CeremonyError::SessionNotFound(session_id))?;

        // Check expiry
        let now = Instant::now();
        if now.duration_since(session.last_active) >= self.session_ttl {
            sessions.remove(&session_id);
            return Err(CeremonyError::SessionExpired);
        }

        if session.complete {
            return Err(CeremonyError::AlreadyComplete);
        }

        // Update activity + render hints
        session.last_active = now;
        if let Some(render) = &request.render {
            session.render = render.clone();
        }

        // Merge new data into the bag
        for (key, value) in request.data {
            session.bag.insert(key, value);
        }

        let render = session.render.clone();
        let ceremony_type = session.ceremony_type.clone();
        let result = self
            .rules
            .evaluate(&ceremony_type, &mut session.bag, &render);

        // Extract session to finalize outside the lock
        let Some(session) = sessions.remove(&session_id) else {
            return Err(CeremonyError::SessionNotFound(session_id));
        };
        drop(sessions);

        self.finalize(session, result)
    }

    /// Convert an `EvalResult` into a `CeremonyResponse` and (re-)store
    /// the session if it isn't complete.
    fn finalize(
        &self,
        mut session: Session,
        result: EvalResult,
    ) -> Result<CeremonyResponse, CeremonyError> {
        let session_id = session.id;

        let (prompts, messages, complete, error) = match result {
            EvalResult::NeedInput { prompts, messages } => (prompts, messages, false, None),
            EvalResult::ValidationError {
                prompts,
                messages,
                error,
            } => (prompts, messages, false, Some(error)),
            EvalResult::Complete { messages } => (Vec::new(), messages, true, None),
            EvalResult::Fatal(msg) => {
                let messages = vec![Message {
                    kind: MessageKind::Error,
                    title: "Ceremony failed".into(),
                    content: msg.clone(),
                }];
                (Vec::new(), messages, true, Some(msg))
            }
        };

        session.complete = complete;

        // Capture the final bag before the session is dropped.
        let result_data = if complete && error.is_none() {
            Some(session.bag.clone())
        } else {
            None
        };

        // Only store if not complete
        if !complete {
            let mut sessions = self.sessions.lock().unwrap_or_else(|e| {
                tracing::warn!("ceremony session lock was poisoned, recovering");
                e.into_inner()
            });
            sessions.insert(session_id, session);
        }

        Ok(CeremonyResponse {
            session_id,
            prompts,
            messages,
            complete,
            error,
            result_data,
        })
    }
}

// ── Errors ──────────────────────────────────────────────────────────

/// Ceremony framework errors.
#[derive(Debug, thiserror::Error)]
pub enum CeremonyError {
    #[error("session not found: {0}")]
    SessionNotFound(Uuid),

    #[error("session expired")]
    SessionExpired,

    #[error("missing required field: {0}")]
    MissingField(String),

    #[error("invalid ceremony type: {0}")]
    InvalidCeremony(String),

    #[error("ceremony already complete")]
    AlreadyComplete,

    #[error("internal error: {0}")]
    Internal(String),
}

impl CeremonyError {
    /// Map to an HTTP status code.
    pub fn http_status(&self) -> u16 {
        match self {
            Self::SessionNotFound(_) => 404,
            Self::SessionExpired => 410,
            Self::MissingField(_) => 400,
            Self::InvalidCeremony(_) => 400,
            Self::AlreadyComplete => 409,
            Self::Internal(_) => 500,
        }
    }
}

// ── Builder helpers ─────────────────────────────────────────────────

impl Prompt {
    /// Create a `SelectOne` prompt.
    pub fn select_one(
        key: impl Into<String>,
        prompt: impl Into<String>,
        options: Vec<SelectOption>,
    ) -> Self {
        Self {
            key: key.into(),
            prompt: prompt.into(),
            input_type: InputType::SelectOne,
            options,
            required: true,
        }
    }

    /// Create a `Secret` prompt (masked input).
    pub fn secret(key: impl Into<String>, prompt: impl Into<String>) -> Self {
        Self {
            key: key.into(),
            prompt: prompt.into(),
            input_type: InputType::Secret,
            options: Vec::new(),
            required: true,
        }
    }

    /// Create a `SecretConfirm` prompt (passphrase + confirmation).
    pub fn secret_confirm(key: impl Into<String>, prompt: impl Into<String>) -> Self {
        Self {
            key: key.into(),
            prompt: prompt.into(),
            input_type: InputType::SecretConfirm,
            options: Vec::new(),
            required: true,
        }
    }

    /// Create a `Code` prompt (short verification code).
    pub fn code(key: impl Into<String>, prompt: impl Into<String>) -> Self {
        Self {
            key: key.into(),
            prompt: prompt.into(),
            input_type: InputType::Code,
            options: Vec::new(),
            required: true,
        }
    }

    /// Create a `Text` prompt (free text).
    pub fn text(key: impl Into<String>, prompt: impl Into<String>) -> Self {
        Self {
            key: key.into(),
            prompt: prompt.into(),
            input_type: InputType::Text,
            options: Vec::new(),
            required: true,
        }
    }

    /// Create an `Entropy` prompt.
    pub fn entropy(key: impl Into<String>, prompt: impl Into<String>) -> Self {
        Self {
            key: key.into(),
            prompt: prompt.into(),
            input_type: InputType::Entropy,
            options: Vec::new(),
            required: true,
        }
    }

    /// Create a `Fido2` prompt (WebAuthn hardware key interaction).
    pub fn fido2(key: impl Into<String>, prompt: impl Into<String>) -> Self {
        Self {
            key: key.into(),
            prompt: prompt.into(),
            input_type: InputType::Fido2,
            options: Vec::new(),
            required: true,
        }
    }
}

impl SelectOption {
    /// Create a select option.
    pub fn new(value: impl Into<String>, label: impl Into<String>) -> Self {
        Self {
            value: value.into(),
            label: label.into(),
            description: None,
        }
    }

    /// Create a select option with a description.
    pub fn with_description(
        value: impl Into<String>,
        label: impl Into<String>,
        description: impl Into<String>,
    ) -> Self {
        Self {
            value: value.into(),
            label: label.into(),
            description: Some(description.into()),
        }
    }
}

impl Message {
    /// Create an `Info` message.
    pub fn info(title: impl Into<String>, content: impl Into<String>) -> Self {
        Self {
            kind: MessageKind::Info,
            title: title.into(),
            content: content.into(),
        }
    }

    /// Create a `QrCode` message.
    pub fn qr_code(title: impl Into<String>, content: impl Into<String>) -> Self {
        Self {
            kind: MessageKind::QrCode,
            title: title.into(),
            content: content.into(),
        }
    }

    /// Create a `Summary` message.
    pub fn summary(title: impl Into<String>, content: impl Into<String>) -> Self {
        Self {
            kind: MessageKind::Summary,
            title: title.into(),
            content: content.into(),
        }
    }

    /// Create an `Error` message.
    pub fn error(title: impl Into<String>, content: impl Into<String>) -> Self {
        Self {
            kind: MessageKind::Error,
            title: title.into(),
            content: content.into(),
        }
    }
}

// ── Tests ───────────────────────────────────────────────────────────

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

    // ── Test rules ──────────────────────────────────────────────────
    //
    // A simple "greeting" ceremony:
    //   - Needs "name" key in the bag
    //   - Validates name is non-empty
    //   - Returns Complete with a summary message when name is present

    struct GreetRules;

    impl CeremonyRules for GreetRules {
        fn validate_ceremony_type(&self, ceremony: &str) -> Result<(), String> {
            match ceremony {
                "greet" => Ok(()),
                other => Err(format!("unknown ceremony: {other}")),
            }
        }

        fn evaluate(
            &self,
            _ceremony_type: &str,
            bag: &mut serde_json::Map<String, serde_json::Value>,
            _render: &RenderHints,
        ) -> EvalResult {
            // Check if name is in the bag
            match bag.get("name").and_then(|v| v.as_str()) {
                None => {
                    // No name yet - ask for it
                    EvalResult::NeedInput {
                        prompts: vec![Prompt::text("name", "What is your name?")],
                        messages: vec![Message::info("Welcome", "Please introduce yourself.")],
                    }
                }
                Some("") => {
                    // Empty name - validation error
                    bag.remove("name");
                    EvalResult::ValidationError {
                        prompts: vec![Prompt::text("name", "What is your name?")],
                        messages: Vec::new(),
                        error: "Name cannot be empty".into(),
                    }
                }
                Some(name) => {
                    // Name present and valid - done
                    let summary = format!("Hello, {name}!");
                    EvalResult::Complete {
                        messages: vec![Message::summary("Greeting complete", &summary)],
                    }
                }
            }
        }
    }

    fn make_host() -> CeremonyHost<GreetRules> {
        CeremonyHost::new(GreetRules)
    }

    // ── Tests ───────────────────────────────────────────────────────

    #[test]
    fn start_new_ceremony_returns_prompts() {
        let host = make_host();
        let resp = host
            .step(CeremonyRequest {
                session_id: None,
                ceremony: Some("greet".into()),
                data: serde_json::Map::new(),
                render: None,
            })
            .unwrap();

        assert!(!resp.complete);
        assert_eq!(resp.prompts.len(), 1);
        assert_eq!(resp.prompts[0].key, "name");
        assert_eq!(resp.prompts[0].input_type, InputType::Text);
        assert_eq!(resp.messages.len(), 1);
        assert_eq!(resp.messages[0].kind, MessageKind::Info);
        assert_eq!(host.active_session_count(), 1);
    }

    #[test]
    fn complete_ceremony_with_data() {
        let host = make_host();

        // Start
        let r1 = host
            .step(CeremonyRequest {
                session_id: None,
                ceremony: Some("greet".into()),
                data: serde_json::Map::new(),
                render: None,
            })
            .unwrap();
        assert!(!r1.complete);

        // Submit name
        let mut data = serde_json::Map::new();
        data.insert("name".into(), serde_json::json!("Alice"));
        let r2 = host
            .step(CeremonyRequest {
                session_id: Some(r1.session_id),
                ceremony: None,
                data,
                render: None,
            })
            .unwrap();
        assert!(r2.complete);
        assert!(r2.prompts.is_empty());
        assert_eq!(r2.messages.len(), 1);
        assert_eq!(r2.messages[0].kind, MessageKind::Summary);
        assert!(r2.messages[0].content.contains("Alice"));

        // Session cleaned up
        assert_eq!(host.active_session_count(), 0);
    }

    #[test]
    fn prefill_completes_in_one_step() {
        let host = make_host();

        let mut data = serde_json::Map::new();
        data.insert("name".into(), serde_json::json!("Bob"));

        let resp = host
            .step(CeremonyRequest {
                session_id: None,
                ceremony: Some("greet".into()),
                data,
                render: None,
            })
            .unwrap();

        assert!(resp.complete);
        assert!(resp.prompts.is_empty());
        assert!(resp.messages[0].content.contains("Bob"));
        assert_eq!(host.active_session_count(), 0);
    }

    #[test]
    fn validation_error_re_prompts() {
        let host = make_host();

        // Start
        let r1 = host
            .step(CeremonyRequest {
                session_id: None,
                ceremony: Some("greet".into()),
                data: serde_json::Map::new(),
                render: None,
            })
            .unwrap();

        // Submit empty name
        let mut data = serde_json::Map::new();
        data.insert("name".into(), serde_json::json!(""));
        let r2 = host
            .step(CeremonyRequest {
                session_id: Some(r1.session_id),
                ceremony: None,
                data,
                render: None,
            })
            .unwrap();

        assert!(!r2.complete);
        assert_eq!(r2.error.as_deref(), Some("Name cannot be empty"));
        assert_eq!(r2.prompts.len(), 1);
        assert_eq!(r2.prompts[0].key, "name");
        assert_eq!(host.active_session_count(), 1);

        // Retry with valid name
        let mut data = serde_json::Map::new();
        data.insert("name".into(), serde_json::json!("Charlie"));
        let r3 = host
            .step(CeremonyRequest {
                session_id: Some(r2.session_id),
                ceremony: None,
                data,
                render: None,
            })
            .unwrap();
        assert!(r3.complete);
        assert!(r3.messages[0].content.contains("Charlie"));
    }

    #[test]
    fn invalid_ceremony_type() {
        let host = make_host();
        let err = host
            .step(CeremonyRequest {
                session_id: None,
                ceremony: Some("bogus".into()),
                data: serde_json::Map::new(),
                render: None,
            })
            .unwrap_err();

        assert!(matches!(err, CeremonyError::InvalidCeremony(_)));
        assert_eq!(err.http_status(), 400);
    }

    #[test]
    fn missing_ceremony_field() {
        let host = make_host();
        let err = host
            .step(CeremonyRequest {
                session_id: None,
                ceremony: None,
                data: serde_json::Map::new(),
                render: None,
            })
            .unwrap_err();

        assert!(matches!(err, CeremonyError::MissingField(_)));
    }

    #[test]
    fn unknown_session_returns_not_found() {
        let host = make_host();
        let err = host
            .step(CeremonyRequest {
                session_id: Some(Uuid::now_v7()),
                ceremony: None,
                data: serde_json::Map::new(),
                render: None,
            })
            .unwrap_err();

        assert!(matches!(err, CeremonyError::SessionNotFound(_)));
        assert_eq!(err.http_status(), 404);
    }

    #[test]
    fn sweep_removes_expired() {
        let host = CeremonyHost::with_ttl(GreetRules, Duration::from_millis(1));

        let _ = host
            .step(CeremonyRequest {
                session_id: None,
                ceremony: Some("greet".into()),
                data: serde_json::Map::new(),
                render: None,
            })
            .unwrap();

        assert_eq!(host.active_session_count(), 1);

        // Wait for TTL
        std::thread::sleep(Duration::from_millis(10));

        let removed = host.sweep_expired();
        assert_eq!(removed, 1);
        assert_eq!(host.active_session_count(), 0);
    }

    #[test]
    fn render_hints_propagate() {
        let host = make_host();
        let resp = host
            .step(CeremonyRequest {
                session_id: None,
                ceremony: Some("greet".into()),
                data: serde_json::Map::new(),
                render: Some(RenderHints {
                    qr: Some(QrFormat::PngBase64),
                }),
            })
            .unwrap();

        let sessions = host.sessions.lock().unwrap();
        let session = sessions.get(&resp.session_id).unwrap();
        assert_eq!(session.render.qr, Some(QrFormat::PngBase64));
    }

    #[test]
    fn qr_format_serde_round_trip() {
        let hints = RenderHints {
            qr: Some(QrFormat::PngBase64),
        };
        let json = serde_json::to_string(&hints).unwrap();
        assert!(json.contains("png_base64"));
        let parsed: RenderHints = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed.qr, Some(QrFormat::PngBase64));
    }

    #[test]
    fn prompt_and_message_serde() {
        let prompt = Prompt::select_one(
            "color",
            "Pick a color",
            vec![
                SelectOption::new("red", "Red"),
                SelectOption::with_description("blue", "Blue", "The color of the sky"),
            ],
        );
        let json = serde_json::to_value(&prompt).unwrap();
        assert_eq!(json["key"], "color");
        assert_eq!(json["input_type"], "select_one");
        assert_eq!(json["options"].as_array().unwrap().len(), 2);

        let msg = Message::qr_code("Scan me", "data:image/png;base64,abc123");
        let json = serde_json::to_value(&msg).unwrap();
        assert_eq!(json["kind"], "qr_code");
    }

    #[test]
    fn complete_response_serde() {
        let resp = CeremonyResponse {
            session_id: Uuid::now_v7(),
            prompts: vec![Prompt::text("foo", "Enter foo")],
            messages: vec![Message::info("Note", "Something")],
            complete: false,
            error: None,
            result_data: None,
        };
        let json = serde_json::to_string(&resp).unwrap();
        let parsed: serde_json::Value = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed["complete"], false);
        assert!(parsed["prompts"].is_array());
        assert!(parsed["messages"].is_array());
        // error should be absent (skip_serializing_if)
        assert!(parsed.get("error").is_none());
    }

    // ── Multi-prompt / multi-message test ───────────────────────────

    /// Rules that ask for two things at once and return a message + prompt together.
    struct MultiRules;

    impl CeremonyRules for MultiRules {
        fn validate_ceremony_type(&self, ceremony: &str) -> Result<(), String> {
            match ceremony {
                "multi" => Ok(()),
                other => Err(format!("unknown: {other}")),
            }
        }

        fn evaluate(
            &self,
            _ceremony_type: &str,
            bag: &mut serde_json::Map<String, serde_json::Value>,
            _render: &RenderHints,
        ) -> EvalResult {
            let has_color = bag.get("color").and_then(|v| v.as_str()).is_some();
            let has_size = bag.get("size").and_then(|v| v.as_str()).is_some();
            let has_confirm = bag.get("confirm").and_then(|v| v.as_str()).is_some();

            if !has_color || !has_size {
                // Ask for both at once
                let mut prompts = Vec::new();
                if !has_color {
                    prompts.push(Prompt::select_one(
                        "color",
                        "Pick a color",
                        vec![
                            SelectOption::new("red", "Red"),
                            SelectOption::new("blue", "Blue"),
                        ],
                    ));
                }
                if !has_size {
                    prompts.push(Prompt::select_one(
                        "size",
                        "Pick a size",
                        vec![
                            SelectOption::new("s", "Small"),
                            SelectOption::new("l", "Large"),
                        ],
                    ));
                }
                return EvalResult::NeedInput {
                    prompts,
                    messages: vec![Message::info("Setup", "Choose your preferences.")],
                };
            }

            if !has_confirm {
                // Show summary message + ask for confirmation
                let summary = format!(
                    "Color: {}, Size: {}",
                    bag["color"].as_str().unwrap(),
                    bag["size"].as_str().unwrap()
                );
                return EvalResult::NeedInput {
                    prompts: vec![Prompt::text("confirm", "Type 'yes' to confirm")],
                    messages: vec![Message::summary("Review", &summary)],
                };
            }

            EvalResult::Complete {
                messages: vec![Message::summary("Done", "Order placed.")],
            }
        }
    }

    #[test]
    fn multi_prompt_returns_multiple_fields() {
        let host = CeremonyHost::new(MultiRules);

        // Start with empty bag - should get 2 prompts
        let r1 = host
            .step(CeremonyRequest {
                session_id: None,
                ceremony: Some("multi".into()),
                data: serde_json::Map::new(),
                render: None,
            })
            .unwrap();
        assert!(!r1.complete);
        assert_eq!(r1.prompts.len(), 2);
        assert_eq!(r1.prompts[0].key, "color");
        assert_eq!(r1.prompts[1].key, "size");
        assert_eq!(r1.messages.len(), 1);

        // Submit both answers
        let mut data = serde_json::Map::new();
        data.insert("color".into(), serde_json::json!("red"));
        data.insert("size".into(), serde_json::json!("l"));
        let r2 = host
            .step(CeremonyRequest {
                session_id: Some(r1.session_id),
                ceremony: None,
                data,
                render: None,
            })
            .unwrap();
        assert!(!r2.complete);
        assert_eq!(r2.prompts.len(), 1);
        assert_eq!(r2.prompts[0].key, "confirm");
        // Summary message alongside prompt
        assert_eq!(r2.messages.len(), 1);
        assert_eq!(r2.messages[0].kind, MessageKind::Summary);

        // Confirm
        let mut data = serde_json::Map::new();
        data.insert("confirm".into(), serde_json::json!("yes"));
        let r3 = host
            .step(CeremonyRequest {
                session_id: Some(r2.session_id),
                ceremony: None,
                data,
                render: None,
            })
            .unwrap();
        assert!(r3.complete);
    }

    #[test]
    fn partial_prefill_asks_only_for_missing() {
        let host = CeremonyHost::new(MultiRules);

        // Start with color already known
        let mut data = serde_json::Map::new();
        data.insert("color".into(), serde_json::json!("blue"));

        let resp = host
            .step(CeremonyRequest {
                session_id: None,
                ceremony: Some("multi".into()),
                data,
                render: None,
            })
            .unwrap();

        assert!(!resp.complete);
        // Only size should be prompted
        assert_eq!(resp.prompts.len(), 1);
        assert_eq!(resp.prompts[0].key, "size");
    }

    #[test]
    fn fatal_error_completes_with_error() {
        struct FatalRules;

        impl CeremonyRules for FatalRules {
            fn validate_ceremony_type(&self, _: &str) -> Result<(), String> {
                Ok(())
            }
            fn evaluate(
                &self,
                _: &str,
                _: &mut serde_json::Map<String, serde_json::Value>,
                _: &RenderHints,
            ) -> EvalResult {
                EvalResult::Fatal("disk full".into())
            }
        }

        let host = CeremonyHost::new(FatalRules);
        let resp = host
            .step(CeremonyRequest {
                session_id: None,
                ceremony: Some("boom".into()),
                data: serde_json::Map::new(),
                render: None,
            })
            .unwrap();

        assert!(resp.complete);
        assert_eq!(resp.error.as_deref(), Some("disk full"));
        assert_eq!(resp.messages.len(), 1);
        assert_eq!(resp.messages[0].kind, MessageKind::Error);
        assert_eq!(host.active_session_count(), 0);
    }
}