quorum-rs 0.7.0-rc.6

Rust SDK and CLI for multi-agent deliberation systems — ships the `quorum` binary (run / status / trace / tui / init) plus the underlying agent, LLM, tool, prompt, and worker library.
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
//! HITL control plane traits and types.
//!
//! Defines the [`AgentControlPlane`] trait for runtime agent control
//! (pause, config patching, buffer management) and the [`ConfigPatch`] struct
//! for partial live-updates to [`AgentConfig`](crate::AgentConfig) fields.
//!
//! Any agent implementation can satisfy [`AgentControlPlane`] to expose a
//! control plane; the reference implementation ships with the embedded
//! status server in this crate.

use crate::workers::buffer::BufferEntrySummary;
use serde::{Deserialize, Serialize};

// ---------------------------------------------------------------------------
// ConfigPatch
// ---------------------------------------------------------------------------

/// Subset of [`AgentConfig`](crate::AgentConfig) fields that can be live-patched
/// at runtime via the HITL control plane.
///
/// All fields are `Option` — only non-`None` values are applied.
/// Changes are **in-memory only** (lost on restart).
#[derive(Debug, Clone, Default, Serialize, Deserialize, utoipa::ToSchema)]
#[serde(deny_unknown_fields)]
pub struct ConfigPatch {
    #[schema(minimum = 0.0, maximum = 2.0)]
    pub temperature: Option<f32>,
    #[schema(minimum = -2.0, maximum = 2.0)]
    pub frequency_penalty: Option<f32>,
    #[schema(minimum = -2.0, maximum = 2.0)]
    pub presence_penalty: Option<f32>,
    pub persona: Option<String>,
    pub textual_feedback: Option<bool>,
    #[schema(minimum = 1)]
    pub max_react_iterations: Option<i32>,
    #[schema(minimum = 0)]
    pub max_retries: Option<i32>,
}

impl ConfigPatch {
    /// Validate all patch values before applying.
    ///
    /// Returns an error message describing the first invalid field found.
    pub fn validate(&self) -> Result<(), String> {
        if let Some(t) = self.temperature {
            if !t.is_finite() || !(0.0..=2.0).contains(&t) {
                return Err(format!(
                    "temperature must be finite and in [0.0, 2.0], got {}",
                    t
                ));
            }
        }
        if let Some(fp) = self.frequency_penalty {
            if !fp.is_finite() || !(-2.0..=2.0).contains(&fp) {
                return Err(format!(
                    "frequency_penalty must be finite and in [-2.0, 2.0], got {}",
                    fp
                ));
            }
        }
        if let Some(pp) = self.presence_penalty {
            if !pp.is_finite() || !(-2.0..=2.0).contains(&pp) {
                return Err(format!(
                    "presence_penalty must be finite and in [-2.0, 2.0], got {}",
                    pp
                ));
            }
        }
        if let Some(mri) = self.max_react_iterations {
            if mri < 1 {
                return Err(format!("max_react_iterations must be >= 1, got {}", mri));
            }
        }
        if let Some(mr) = self.max_retries {
            if mr < 0 {
                return Err(format!("max_retries must be >= 0, got {}", mr));
            }
        }
        Ok(())
    }

    /// Validate and apply this patch to an [`AgentConfig`](crate::AgentConfig).
    ///
    /// Returns `Err` if any patched value is out of range.
    pub fn apply(&self, config: &mut crate::AgentConfig) -> Result<(), String> {
        self.validate()?;
        if let Some(t) = self.temperature {
            config.temperature = t;
        }
        if let Some(fp) = self.frequency_penalty {
            config.frequency_penalty = Some(fp);
        }
        if let Some(pp) = self.presence_penalty {
            config.presence_penalty = Some(pp);
        }
        if let Some(ref p) = self.persona {
            config.persona = Some(p.clone());
        }
        if let Some(tf) = self.textual_feedback {
            config.textual_feedback = tf;
        }
        if let Some(mri) = self.max_react_iterations {
            config.max_react_iterations = Some(mri);
        }
        if let Some(mr) = self.max_retries {
            config.max_retries = Some(mr);
        }
        Ok(())
    }
}

// ---------------------------------------------------------------------------
// AgentControlPlane trait
// ---------------------------------------------------------------------------

/// Trait for controlling agents at runtime (pause, config, buffer ops).
///
/// Reference implementation: the embedded status server (feature `status-server`).
#[async_trait::async_trait]
pub trait AgentControlPlane: Send + Sync {
    /// Pause or resume an agent by name.
    async fn set_paused(&self, agent: &str, paused: bool) -> anyhow::Result<()>;

    /// Apply a partial configuration update to an agent.
    async fn update_config(&self, agent: &str, patch: ConfigPatch) -> anyhow::Result<()>;

    /// List buffered responses for an agent.
    async fn list_buffer(&self, agent: &str) -> anyhow::Result<Vec<BufferEntrySummary>>;

    /// Force-release a specific buffer entry (publish + ack).
    async fn release_buffer_entry(&self, agent: &str, entry_id: &str) -> anyhow::Result<()>;

    /// Reject (discard) a specific buffer entry (ack without publishing).
    async fn reject_buffer_entry(&self, agent: &str, entry_id: &str) -> anyhow::Result<()>;
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    #[test]
    fn test_config_patch_serde() {
        let patch = ConfigPatch {
            temperature: Some(0.9),
            frequency_penalty: Some(0.3),
            presence_penalty: Some(1.2),
            persona: Some("skeptical analyst".into()),
            textual_feedback: Some(false),
            max_react_iterations: Some(5),
            max_retries: Some(2),
        };
        let json = serde_json::to_string(&patch).unwrap();
        let roundtripped: ConfigPatch = serde_json::from_str(&json).unwrap();
        assert_eq!(roundtripped.temperature, Some(0.9));
        assert_eq!(roundtripped.persona, Some("skeptical analyst".into()));
        assert_eq!(roundtripped.max_retries, Some(2));
    }

    #[test]
    fn test_config_patch_all_none() {
        let patch = ConfigPatch::default();
        assert!(patch.temperature.is_none());
        assert!(patch.frequency_penalty.is_none());
        assert!(patch.presence_penalty.is_none());
        assert!(patch.persona.is_none());
        assert!(patch.textual_feedback.is_none());
        assert!(patch.max_react_iterations.is_none());
        assert!(patch.max_retries.is_none());

        // Empty patch serializes with all nulls
        let json = serde_json::to_string(&patch).unwrap();
        let roundtripped: ConfigPatch = serde_json::from_str(&json).unwrap();
        assert!(roundtripped.temperature.is_none());
    }

    #[test]
    fn test_config_patch_partial() {
        let json = r#"{"temperature": 0.5}"#;
        let patch: ConfigPatch = serde_json::from_str(json).unwrap();
        assert_eq!(patch.temperature, Some(0.5));
        assert!(patch.frequency_penalty.is_none());
        assert!(patch.persona.is_none());
    }

    #[test]
    fn test_config_patch_apply() {
        let mut config = crate::AgentConfig {
            name: "test".into(),
            provider_id: "p".into(),
            model_name: "m".into(),
            temperature: 0.7,
            frequency_penalty: Some(0.1),
            presence_penalty: Some(1.5),
            persona: Some("default persona".into()),
            textual_feedback: true,
            max_react_iterations: Some(10),
            max_retries: Some(3),
            ..Default::default()
        };

        let patch = ConfigPatch {
            temperature: Some(1.2),
            persona: Some("aggressive debater".into()),
            max_retries: Some(5),
            ..Default::default()
        };
        patch.apply(&mut config).expect("valid patch should apply");

        assert_eq!(config.temperature, 1.2);
        assert_eq!(config.persona, Some("aggressive debater".into()));
        assert_eq!(config.max_retries, Some(5));
        // Unpatched fields unchanged
        assert_eq!(config.frequency_penalty, Some(0.1));
        assert_eq!(config.presence_penalty, Some(1.5));
        assert!(config.textual_feedback);
        assert_eq!(config.max_react_iterations, Some(10));
    }

    // -------------------------------------------------------------------
    // ConfigPatch validation tests
    // -------------------------------------------------------------------

    #[test]
    fn test_validate_rejects_temperature_above_range() {
        let patch = ConfigPatch {
            temperature: Some(2.5),
            ..Default::default()
        };
        let err = patch.validate().unwrap_err();
        assert!(err.contains("temperature"), "err: {}", err);
    }

    #[test]
    fn test_validate_rejects_negative_temperature() {
        let patch = ConfigPatch {
            temperature: Some(-0.1),
            ..Default::default()
        };
        assert!(patch.validate().is_err());
    }

    #[test]
    fn test_validate_rejects_nan_temperature() {
        let patch = ConfigPatch {
            temperature: Some(f32::NAN),
            ..Default::default()
        };
        assert!(patch.validate().is_err());
    }

    #[test]
    fn test_validate_rejects_inf_temperature() {
        let patch = ConfigPatch {
            temperature: Some(f32::INFINITY),
            ..Default::default()
        };
        assert!(patch.validate().is_err());
    }

    #[test]
    fn test_validate_accepts_boundary_temperature() {
        let patch_zero = ConfigPatch {
            temperature: Some(0.0),
            ..Default::default()
        };
        assert!(patch_zero.validate().is_ok());
        let patch_two = ConfigPatch {
            temperature: Some(2.0),
            ..Default::default()
        };
        assert!(patch_two.validate().is_ok());
    }

    #[test]
    fn test_validate_rejects_frequency_penalty_out_of_range() {
        let patch = ConfigPatch {
            frequency_penalty: Some(3.0),
            ..Default::default()
        };
        let err = patch.validate().unwrap_err();
        assert!(err.contains("frequency_penalty"), "err: {}", err);
    }

    #[test]
    fn test_validate_rejects_presence_penalty_out_of_range() {
        let patch = ConfigPatch {
            presence_penalty: Some(-2.5),
            ..Default::default()
        };
        assert!(patch.validate().is_err());
    }

    #[test]
    fn test_validate_rejects_zero_max_react_iterations() {
        let patch = ConfigPatch {
            max_react_iterations: Some(0),
            ..Default::default()
        };
        let err = patch.validate().unwrap_err();
        assert!(err.contains("max_react_iterations"), "err: {}", err);
    }

    #[test]
    fn test_validate_rejects_negative_max_retries() {
        let patch = ConfigPatch {
            max_retries: Some(-1),
            ..Default::default()
        };
        let err = patch.validate().unwrap_err();
        assert!(err.contains("max_retries"), "err: {}", err);
    }

    #[test]
    fn test_validate_accepts_valid_patch() {
        let patch = ConfigPatch {
            temperature: Some(1.0),
            frequency_penalty: Some(-1.5),
            presence_penalty: Some(2.0),
            max_react_iterations: Some(5),
            max_retries: Some(0),
            ..Default::default()
        };
        assert!(patch.validate().is_ok());
    }

    #[test]
    fn test_validate_accepts_empty_patch() {
        let patch = ConfigPatch::default();
        assert!(patch.validate().is_ok());
    }

    #[test]
    fn test_apply_rejects_invalid_and_does_not_mutate() {
        let mut config = crate::AgentConfig {
            name: "test".into(),
            provider_id: "p".into(),
            model_name: "m".into(),
            temperature: 0.7,
            ..Default::default()
        };

        let patch = ConfigPatch {
            temperature: Some(5.0), // invalid
            persona: Some("should not be applied".into()),
            ..Default::default()
        };
        assert!(patch.apply(&mut config).is_err());
        // Config should be unchanged
        assert_eq!(config.temperature, 0.7);
        assert!(config.persona.is_none());
    }

    // -------------------------------------------------------------------
    // Boundary validation tests
    // -------------------------------------------------------------------

    /// Test that -2.0 and 2.0 are valid frequency_penalty values,
    /// but -2.01 and 2.01 are rejected.
    #[test]
    fn test_validate_frequency_penalty_exact_boundaries() {
        // -2.0 is valid (inclusive lower bound)
        let patch_lower = ConfigPatch {
            frequency_penalty: Some(-2.0),
            ..Default::default()
        };
        assert!(
            patch_lower.validate().is_ok(),
            "frequency_penalty -2.0 should be valid"
        );

        // 2.0 is valid (inclusive upper bound)
        let patch_upper = ConfigPatch {
            frequency_penalty: Some(2.0),
            ..Default::default()
        };
        assert!(
            patch_upper.validate().is_ok(),
            "frequency_penalty 2.0 should be valid"
        );

        // -2.01 is invalid (below lower bound)
        let patch_below = ConfigPatch {
            frequency_penalty: Some(-2.01),
            ..Default::default()
        };
        let err = patch_below.validate().unwrap_err();
        assert!(
            err.contains("frequency_penalty"),
            "error should mention frequency_penalty: {}",
            err
        );

        // 2.01 is invalid (above upper bound)
        let patch_above = ConfigPatch {
            frequency_penalty: Some(2.01),
            ..Default::default()
        };
        let err = patch_above.validate().unwrap_err();
        assert!(
            err.contains("frequency_penalty"),
            "error should mention frequency_penalty: {}",
            err
        );
    }

    /// Test that -2.0 and 2.0 are valid presence_penalty values,
    /// but -2.01 and 2.01 are rejected.
    #[test]
    fn test_validate_presence_penalty_exact_boundaries() {
        // -2.0 is valid (inclusive lower bound)
        let patch_lower = ConfigPatch {
            presence_penalty: Some(-2.0),
            ..Default::default()
        };
        assert!(
            patch_lower.validate().is_ok(),
            "presence_penalty -2.0 should be valid"
        );

        // 2.0 is valid (inclusive upper bound)
        let patch_upper = ConfigPatch {
            presence_penalty: Some(2.0),
            ..Default::default()
        };
        assert!(
            patch_upper.validate().is_ok(),
            "presence_penalty 2.0 should be valid"
        );

        // -2.01 is invalid (below lower bound)
        let patch_below = ConfigPatch {
            presence_penalty: Some(-2.01),
            ..Default::default()
        };
        let err = patch_below.validate().unwrap_err();
        assert!(
            err.contains("presence_penalty"),
            "error should mention presence_penalty: {}",
            err
        );

        // 2.01 is invalid (above upper bound)
        let patch_above = ConfigPatch {
            presence_penalty: Some(2.01),
            ..Default::default()
        };
        let err = patch_above.validate().unwrap_err();
        assert!(
            err.contains("presence_penalty"),
            "error should mention presence_penalty: {}",
            err
        );
    }

    /// Test that a very large value like i32::MAX is accepted for
    /// max_react_iterations (validation only requires >= 1).
    #[test]
    fn test_validate_max_react_iterations_large_value() {
        let patch = ConfigPatch {
            max_react_iterations: Some(i32::MAX),
            ..Default::default()
        };
        assert!(
            patch.validate().is_ok(),
            "max_react_iterations = i32::MAX ({}) should be valid",
            i32::MAX
        );
    }

    /// Test that persona = Some("") passes validation.
    ///
    /// The current validation does not reject empty strings for persona.
    /// This test documents this behavior: an empty persona is accepted
    /// at the ConfigPatch level; any semantic rejection (if desired)
    /// would happen at a higher layer.
    #[test]
    fn test_validate_persona_empty_string() {
        let patch = ConfigPatch {
            persona: Some("".into()),
            ..Default::default()
        };
        assert!(
            patch.validate().is_ok(),
            "empty persona string should pass validation"
        );

        // Also verify it applies correctly
        let mut config = crate::AgentConfig {
            name: "test".into(),
            provider_id: "p".into(),
            model_name: "m".into(),
            temperature: 0.7,
            persona: Some("original persona".into()),
            ..Default::default()
        };
        patch
            .apply(&mut config)
            .expect("empty persona patch should apply");
        assert_eq!(
            config.persona,
            Some("".into()),
            "persona should be set to empty string, not None"
        );
    }

    /// Exercises ALL ConfigPatch fields in apply(), ensuring every branch
    /// in the apply() method is covered — specifically frequency_penalty,
    /// presence_penalty, textual_feedback, and max_react_iterations.
    #[test]
    fn test_apply_all_config_patch_fields() {
        let mut config = crate::AgentConfig {
            name: "full-patch-test".into(),
            provider_id: "provider".into(),
            model_name: "model".into(),
            temperature: 0.5,
            frequency_penalty: None,
            presence_penalty: None,
            persona: None,
            textual_feedback: false,
            max_react_iterations: None,
            max_retries: None,
            ..Default::default()
        };

        // Patch ALL fields at once
        let patch = ConfigPatch {
            temperature: Some(1.5),
            frequency_penalty: Some(-0.5),
            presence_penalty: Some(0.8),
            persona: Some("devil's advocate".into()),
            textual_feedback: Some(true),
            max_react_iterations: Some(7),
            max_retries: Some(2),
        };
        patch.apply(&mut config).expect("full patch should apply");

        // Verify every field was applied
        assert_eq!(config.temperature, 1.5, "temperature should be updated");
        assert_eq!(
            config.frequency_penalty,
            Some(-0.5),
            "frequency_penalty should be set"
        );
        assert_eq!(
            config.presence_penalty,
            Some(0.8),
            "presence_penalty should be set"
        );
        assert_eq!(
            config.persona,
            Some("devil's advocate".into()),
            "persona should be set"
        );
        assert!(config.textual_feedback, "textual_feedback should be true");
        assert_eq!(
            config.max_react_iterations,
            Some(7),
            "max_react_iterations should be set"
        );
        assert_eq!(config.max_retries, Some(2), "max_retries should be set");

        // Now apply a second patch that only sets the previously-uncovered fields
        // to different values, verifying they overwrite correctly.
        let patch2 = ConfigPatch {
            frequency_penalty: Some(1.0),
            presence_penalty: Some(-1.0),
            textual_feedback: Some(false),
            max_react_iterations: Some(3),
            ..Default::default()
        };
        patch2
            .apply(&mut config)
            .expect("second patch should apply");

        assert_eq!(
            config.frequency_penalty,
            Some(1.0),
            "frequency_penalty should be overwritten"
        );
        assert_eq!(
            config.presence_penalty,
            Some(-1.0),
            "presence_penalty should be overwritten"
        );
        assert!(
            !config.textual_feedback,
            "textual_feedback should be toggled back to false"
        );
        assert_eq!(
            config.max_react_iterations,
            Some(3),
            "max_react_iterations should be overwritten"
        );
        // Fields not in patch2 should remain from patch1
        assert_eq!(
            config.temperature, 1.5,
            "temperature should be unchanged by patch2"
        );
        assert_eq!(
            config.persona,
            Some("devil's advocate".into()),
            "persona should be unchanged by patch2"
        );
    }
}