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
//! Built-in [`ProviderFactory`] implementations.
//!
//! Each factory reproduces one arm of the old
//! [`crate::serve::build_worker`] dispatch verbatim, including its
//! "missing config section → skip cleanly (`Ok(None)`)" behaviour.

use std::sync::Arc;

use anyhow::Result;
use tracing::{info, warn};

use super::ProviderFactory;
use crate::agents::config::{AgentConfig, ClaudeProviderConfig};
use crate::agents::exec_agent::ExecAgent;
use crate::agents::mcp_agent::{ClaudeAgent, McpAgent};
use crate::agents::{NsedAgent, ProposerEvaluatorAgent};
use crate::config::ProviderEntry;
use crate::llms::OpenAICompatibleModel;
use crate::prompts::defaults::DefaultPromptSet;
use crate::serve::instantiate_builtin_tools;

/// `exec` — external subprocess, no LLM.
pub struct ExecFactory;

impl ProviderFactory for ExecFactory {
    fn provider_type(&self) -> &str {
        "exec"
    }

    fn build_agent(
        &self,
        agent_config: &AgentConfig,
        _provider: &ProviderEntry,
    ) -> Result<Option<Arc<dyn NsedAgent>>> {
        let exec_cfg = match agent_config.exec.clone() {
            Some(cfg) => cfg,
            None => {
                warn!(
                    agent = %agent_config.name,
                    "provider_type=exec but no `exec` section in agent config — skipping"
                );
                return Ok(None);
            }
        };
        Ok(Some(Arc::new(ExecAgent::new(
            agent_config.name.clone(),
            exec_cfg,
        ))))
    }
}

/// `mcp` — external subprocess speaking Model Context Protocol.
pub struct McpFactory;

impl ProviderFactory for McpFactory {
    fn provider_type(&self) -> &str {
        "mcp"
    }

    fn build_agent(
        &self,
        agent_config: &AgentConfig,
        _provider: &ProviderEntry,
    ) -> Result<Option<Arc<dyn NsedAgent>>> {
        let mcp_cfg = match agent_config.mcp.clone() {
            Some(cfg) => cfg,
            None => {
                warn!(
                    agent = %agent_config.name,
                    "provider_type=mcp but no `mcp` section in agent config — skipping"
                );
                return Ok(None);
            }
        };
        Ok(Some(Arc::new(McpAgent::new(
            agent_config.name.clone(),
            mcp_cfg,
        ))))
    }
}

/// `claude` — Claude Code CLI subprocess.
///
/// Config resolution — also the reference for the generic
/// `provider_config` channel a third-party `codex` factory uses:
///
/// 1. the typed `claude:` section, if present (wins);
/// 2. else [`AgentConfig::provider_config`] deserialized into
///    [`ClaudeProviderConfig`];
/// 3. else defaults (the CLI is usable with zero config).
pub struct ClaudeFactory;

impl ProviderFactory for ClaudeFactory {
    fn provider_type(&self) -> &str {
        "claude"
    }

    fn build_agent(
        &self,
        agent_config: &AgentConfig,
        _provider: &ProviderEntry,
    ) -> Result<Option<Arc<dyn NsedAgent>>> {
        let claude_cfg = match agent_config.claude.clone() {
            Some(cfg) => cfg,
            None if !agent_config.provider_config.is_empty() => agent_config
                .provider_config_as::<ClaudeProviderConfig>()
                .map_err(|e| {
                    anyhow::anyhow!(
                        "claude agent '{}': failed to parse `provider_config`: {e}",
                        agent_config.name
                    )
                })?,
            None => ClaudeProviderConfig::default(),
        };
        Ok(Some(Arc::new(ClaudeAgent::new(
            agent_config.clone(),
            claude_cfg,
            Arc::new(DefaultPromptSet::new()),
        ))))
    }
}

/// OpenAI-wire-compatible HTTP provider (`openai`, `ollama`,
/// `simulated`). One instance per `provider_type`; `requires_api_key`
/// is set at construction.
pub struct OpenAiCompatibleFactory {
    provider_type: String,
    requires_api_key: bool,
}

impl OpenAiCompatibleFactory {
    pub fn new(provider_type: impl Into<String>, requires_api_key: bool) -> Self {
        Self {
            provider_type: provider_type.into(),
            requires_api_key,
        }
    }
}

impl ProviderFactory for OpenAiCompatibleFactory {
    fn provider_type(&self) -> &str {
        &self.provider_type
    }

    fn requires_api_key(&self) -> bool {
        self.requires_api_key
    }

    fn build_agent(
        &self,
        agent_config: &AgentConfig,
        provider: &ProviderEntry,
    ) -> Result<Option<Arc<dyn NsedAgent>>> {
        // Base-URL resolution mirrors the old dispatch: only `openai`
        // gets the api.openai.com default. Any other type with an
        // empty `base_url` is skipped so a typoed `type:` can't leak
        // the API key to api.openai.com.
        let base_url = if provider.base_url.is_empty() {
            if self.provider_type == "openai" {
                "https://api.openai.com/v1".to_string()
            } else {
                warn!(
                    agent = %agent_config.name,
                    provider_type = %self.provider_type,
                    "no `base_url` set for non-openai provider type — skipping"
                );
                return Ok(None);
            }
        } else {
            provider.base_url.clone()
        };

        let llm =
            OpenAICompatibleModel::new(base_url, provider.api_key.clone(), provider.engine.clone());

        let builtin_tools = match instantiate_builtin_tools(agent_config) {
            Ok(tools) => tools,
            Err(reason) => {
                warn!(
                    agent = %agent_config.name,
                    reason = %reason,
                    "skipping agent: failed to instantiate builtin_tools"
                );
                return Ok(None);
            }
        };
        if !builtin_tools.is_empty() {
            info!(
                agent = %agent_config.name,
                count = builtin_tools.len(),
                "attached SDK-builtin tool grants"
            );
        }

        Ok(Some(Arc::new(ProposerEvaluatorAgent::new(
            agent_config.clone(),
            Box::new(llm),
            Box::new(DefaultPromptSet::new()),
            vec![],
            builtin_tools,
        ))))
    }
}

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

    fn resolve(yaml: &str, agent: &str) -> (AgentConfig, ProviderEntry) {
        let fleet: crate::config::AgentFleetConfig =
            serde_yaml::from_str(yaml).expect("fleet yaml must parse");
        crate::config::load_agent_from_config(&fleet, agent).expect("agent must resolve")
    }

    #[test]
    fn exec_without_section_skips() {
        let (cfg, provider) = resolve(
            r#"
providers:
  exec_local:
    type: exec
agents:
  - name: broken
    provider_id: exec_local
    model_name: custom
"#,
            "broken",
        );
        let built = ExecFactory.build_agent(&cfg, &provider).unwrap();
        assert!(built.is_none(), "exec without `exec:` section must skip");
    }

    #[test]
    fn exec_with_section_builds() {
        let (cfg, provider) = resolve(
            r#"
providers:
  exec_local:
    type: exec
agents:
  - name: runner
    provider_id: exec_local
    model_name: custom
    exec:
      command: ["echo", "hi"]
"#,
            "runner",
        );
        let agent = ExecFactory
            .build_agent(&cfg, &provider)
            .unwrap()
            .expect("exec with section must build");
        assert_eq!(agent.name(), "runner");
    }

    #[test]
    fn mcp_without_section_skips() {
        let (cfg, provider) = resolve(
            r#"
providers:
  mcp_local:
    type: mcp
agents:
  - name: broken
    provider_id: mcp_local
    model_name: custom
"#,
            "broken",
        );
        assert!(McpFactory.build_agent(&cfg, &provider).unwrap().is_none());
    }

    #[test]
    fn mcp_with_section_builds() {
        let (cfg, provider) = resolve(
            r#"
providers:
  mcp_local:
    type: mcp
agents:
  - name: mcp-runner
    provider_id: mcp_local
    model_name: custom
    mcp:
      command: ["my-mcp-server"]
"#,
            "mcp-runner",
        );
        let agent = McpFactory
            .build_agent(&cfg, &provider)
            .unwrap()
            .expect("mcp with section must build");
        assert_eq!(agent.name(), "mcp-runner");
    }

    #[test]
    fn claude_builds_with_or_without_section() {
        let (cfg, provider) = resolve(
            r#"
providers:
  claude_cli:
    type: claude
agents:
  - name: claude-agent
    provider_id: claude_cli
    model_name: claude-sonnet
"#,
            "claude-agent",
        );
        let agent = ClaudeFactory
            .build_agent(&cfg, &provider)
            .unwrap()
            .expect("claude must build from defaults");
        assert_eq!(agent.name(), "claude-agent");
    }

    /// The built-in `claude` provider sources its typed config from the
    /// generic `provider_config` map (no `claude:` section) — the same
    /// pattern a third-party `codex` factory uses.
    #[test]
    fn claude_builds_from_provider_config() {
        let (cfg, provider) = resolve(
            r#"
providers:
  claude_cli:
    type: claude
agents:
  - name: claude-agent
    provider_id: claude_cli
    model_name: claude-sonnet
    provider_config:
      permission_mode: "acceptEdits"
      timeout_secs: 120
"#,
            "claude-agent",
        );
        assert!(
            cfg.claude.is_none(),
            "no typed claude: section in this fixture"
        );
        let agent = ClaudeFactory
            .build_agent(&cfg, &provider)
            .unwrap()
            .expect("claude must build from provider_config");
        assert_eq!(agent.name(), "claude-agent");
    }

    /// Precedence: a present typed `claude:` section wins, and the
    /// `provider_config` map is not even parsed (here it would fail —
    /// `timeout_secs` is a string — proving it was ignored).
    #[test]
    fn claude_section_wins_over_provider_config() {
        let (cfg, provider) = resolve(
            r#"
providers:
  claude_cli:
    type: claude
agents:
  - name: claude-agent
    provider_id: claude_cli
    model_name: claude-sonnet
    claude:
      permission_mode: "acceptEdits"
    provider_config:
      timeout_secs: "not-a-number"
"#,
            "claude-agent",
        );
        let agent = ClaudeFactory
            .build_agent(&cfg, &provider)
            .unwrap()
            .expect("typed claude: section must win, provider_config ignored");
        assert_eq!(agent.name(), "claude-agent");
    }

    /// A malformed `provider_config` (when it IS the config source) is a
    /// hard error, not a silent skip — the factory surfaces the parse
    /// failure so the operator fixes the YAML.
    #[test]
    fn claude_bad_provider_config_errors() {
        let (cfg, provider) = resolve(
            r#"
providers:
  claude_cli:
    type: claude
agents:
  - name: claude-agent
    provider_id: claude_cli
    model_name: claude-sonnet
    provider_config:
      timeout_secs: "not-a-number"
"#,
            "claude-agent",
        );
        let err = ClaudeFactory.build_agent(&cfg, &provider).unwrap_err();
        assert!(
            err.to_string()
                .contains("failed to parse `provider_config`"),
            "got: {err}"
        );
    }

    #[test]
    fn openai_empty_base_url_defaults_to_openai_com() {
        let (cfg, provider) = resolve(
            r#"
providers:
  oai:
    type: openai
    api_key: "sk-real-key"
agents:
  - name: gpt
    provider_id: oai
    model_name: gpt-4o
"#,
            "gpt",
        );
        let factory = OpenAiCompatibleFactory::new("openai", true);
        let agent = factory
            .build_agent(&cfg, &provider)
            .unwrap()
            .expect("openai with empty base_url must default and build");
        assert_eq!(agent.name(), "gpt");
    }

    #[test]
    fn non_openai_empty_base_url_skips() {
        let (cfg, provider) = resolve(
            r#"
providers:
  local_ollama:
    type: ollama
agents:
  - name: llama
    provider_id: local_ollama
    model_name: llama3
"#,
            "llama",
        );
        let factory = OpenAiCompatibleFactory::new("ollama", false);
        assert!(
            factory.build_agent(&cfg, &provider).unwrap().is_none(),
            "ollama without base_url must skip (no openai.com fallback)"
        );
    }

    #[test]
    fn ollama_with_base_url_builds() {
        let (cfg, provider) = resolve(
            r#"
providers:
  local_ollama:
    type: ollama
    base_url: "http://localhost:11434/v1"
agents:
  - name: llama
    provider_id: local_ollama
    model_name: llama3
"#,
            "llama",
        );
        let factory = OpenAiCompatibleFactory::new("ollama", false);
        let agent = factory
            .build_agent(&cfg, &provider)
            .unwrap()
            .expect("ollama with base_url must build");
        assert_eq!(agent.name(), "llama");
    }
}