tetrad 0.1.15

Quadruple Consensus MCP Server for Claude Code - Validates code using Codex, Gemini and Qwen
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
//! Interactive configuration for Tetrad.
//!
//! This module implements interactive configuration using dialoguer.

use std::path::{Path, PathBuf};

use dialoguer::{theme::ColorfulTheme, Confirm, Input, Select};

use crate::types::config::{Config, ConsensusRule};
use crate::TetradResult;

/// Runs interactive configuration.
pub fn run_interactive_config(config_path: &Path) -> TetradResult<()> {
    let theme = ColorfulTheme::default();

    println!("\n🔧 Tetrad Interactive Configuration\n");

    // Load existing config or create new one
    let mut config = if config_path.exists() {
        Config::load(config_path)?
    } else {
        println!("Creating new configuration...\n");
        Config::default_config()
    };

    // Main menu
    loop {
        let options = vec![
            "General Settings",
            "Executors (Codex, Gemini, Qwen)",
            "Consensus",
            "ReasoningBank",
            "Cache",
            "Save and Exit",
            "Exit without Saving",
        ];

        let selection = Select::with_theme(&theme)
            .with_prompt("What would you like to configure?")
            .items(&options)
            .default(0)
            .interact()?;

        match selection {
            0 => configure_general(&theme, &mut config)?,
            1 => configure_executors(&theme, &mut config)?,
            2 => configure_consensus(&theme, &mut config)?,
            3 => configure_reasoning(&theme, &mut config)?,
            4 => configure_cache(&theme, &mut config)?,
            5 => {
                config.save(config_path)?;
                println!("\n✓ Configuration saved to: {}\n", config_path.display());
                break;
            }
            6 => {
                if Confirm::with_theme(&theme)
                    .with_prompt("Are you sure you want to exit without saving?")
                    .default(false)
                    .interact()?
                {
                    println!("\nExiting without saving.\n");
                    break;
                }
            }
            _ => {}
        }
    }

    Ok(())
}

/// Configures general options.
fn configure_general(theme: &ColorfulTheme, config: &mut Config) -> TetradResult<()> {
    println!("\n📋 General Settings\n");

    // Log level
    let log_levels = vec!["error", "warn", "info", "debug", "trace"];
    let current_idx = log_levels
        .iter()
        .position(|&l| l == config.general.log_level)
        .unwrap_or(2);

    let log_level_idx = Select::with_theme(theme)
        .with_prompt("Log level")
        .items(&log_levels)
        .default(current_idx)
        .interact()?;

    config.general.log_level = log_levels[log_level_idx].to_string();

    // Log format
    let log_formats = vec!["text", "json"];
    let current_format_idx = log_formats
        .iter()
        .position(|&f| f == config.general.log_format)
        .unwrap_or(0);

    let log_format_idx = Select::with_theme(theme)
        .with_prompt("Log format")
        .items(&log_formats)
        .default(current_format_idx)
        .interact()?;

    config.general.log_format = log_formats[log_format_idx].to_string();

    // Timeout
    let timeout: u64 = Input::with_theme(theme)
        .with_prompt("General timeout (seconds)")
        .default(config.general.timeout_secs)
        .interact_text()?;

    config.general.timeout_secs = timeout;

    println!("\n✓ General settings updated.\n");
    Ok(())
}

/// Configures executors.
fn configure_executors(theme: &ColorfulTheme, config: &mut Config) -> TetradResult<()> {
    println!("\n🤖 Executor Configuration\n");

    let executors = vec!["Codex", "Gemini", "Qwen", "Back"];

    loop {
        let selection = Select::with_theme(theme)
            .with_prompt("Which executor to configure?")
            .items(&executors)
            .default(0)
            .interact()?;

        match selection {
            0 => configure_single_executor(theme, "Codex", &mut config.executors.codex)?,
            1 => configure_single_executor(theme, "Gemini", &mut config.executors.gemini)?,
            2 => configure_single_executor(theme, "Qwen", &mut config.executors.qwen)?,
            3 => break,
            _ => {}
        }
    }

    Ok(())
}

/// Configures a specific executor.
fn configure_single_executor(
    theme: &ColorfulTheme,
    name: &str,
    executor: &mut crate::types::config::ExecutorConfig,
) -> TetradResult<()> {
    println!("\n⚙️  Configuring {}\n", name);

    // Enabled
    executor.enabled = Confirm::with_theme(theme)
        .with_prompt(format!("{} enabled?", name))
        .default(executor.enabled)
        .interact()?;

    if !executor.enabled {
        println!("{} disabled.\n", name);
        return Ok(());
    }

    // Command
    let command: String = Input::with_theme(theme)
        .with_prompt("Command")
        .default(executor.command.clone())
        .interact_text()?;

    executor.command = command;

    // Args
    let args_str: String = Input::with_theme(theme)
        .with_prompt("Arguments (space separated)")
        .default(executor.args.join(" "))
        .interact_text()?;

    executor.args = args_str.split_whitespace().map(String::from).collect();

    // Timeout
    let timeout: u64 = Input::with_theme(theme)
        .with_prompt("Timeout (seconds)")
        .default(executor.timeout_secs)
        .interact_text()?;

    executor.timeout_secs = timeout;

    // Weight
    let weight: u8 = Input::with_theme(theme)
        .with_prompt("Consensus weight (1-10)")
        .default(executor.weight)
        .interact_text()?;

    executor.weight = weight.clamp(1, 10);

    println!("\n{} configured.\n", name);
    Ok(())
}

/// Configures consensus.
fn configure_consensus(theme: &ColorfulTheme, config: &mut Config) -> TetradResult<()> {
    println!("\n🤝 Consensus Configuration\n");

    // Default rule
    let rules = vec![
        "Golden (unanimity)",
        "Strong (3/3 or 2/3 with high confidence)",
        "Weak (simple majority)",
    ];

    let current_idx = match config.consensus.default_rule {
        ConsensusRule::Golden => 0,
        ConsensusRule::Strong => 1,
        ConsensusRule::Weak => 2,
    };

    let rule_idx = Select::with_theme(theme)
        .with_prompt("Default consensus rule")
        .items(&rules)
        .default(current_idx)
        .interact()?;

    config.consensus.default_rule = match rule_idx {
        0 => ConsensusRule::Golden,
        1 => ConsensusRule::Strong,
        _ => ConsensusRule::Weak,
    };

    // Minimum score
    let min_score: u8 = Input::with_theme(theme)
        .with_prompt("Minimum score for approval (0-100)")
        .default(config.consensus.min_score)
        .interact_text()?;

    config.consensus.min_score = min_score.min(100);

    // Max loops
    let max_loops: u8 = Input::with_theme(theme)
        .with_prompt("Maximum number of refinement loops")
        .default(config.consensus.max_loops)
        .interact_text()?;

    config.consensus.max_loops = max_loops;

    println!("\n✓ Consensus configured.\n");
    Ok(())
}

/// Configures ReasoningBank.
fn configure_reasoning(theme: &ColorfulTheme, config: &mut Config) -> TetradResult<()> {
    println!("\n🧠 ReasoningBank Configuration\n");

    // Enabled
    config.reasoning.enabled = Confirm::with_theme(theme)
        .with_prompt("ReasoningBank enabled?")
        .default(config.reasoning.enabled)
        .interact()?;

    if !config.reasoning.enabled {
        println!("ReasoningBank disabled.\n");
        return Ok(());
    }

    // Database path
    let db_path: String = Input::with_theme(theme)
        .with_prompt("Database path")
        .default(config.reasoning.db_path.display().to_string())
        .interact_text()?;

    config.reasoning.db_path = PathBuf::from(db_path);

    // Max patterns per query
    let max_patterns: usize = Input::with_theme(theme)
        .with_prompt("Maximum patterns per query")
        .default(config.reasoning.max_patterns_per_query)
        .interact_text()?;

    config.reasoning.max_patterns_per_query = max_patterns;

    // Consolidation interval
    let consolidation_interval: usize = Input::with_theme(theme)
        .with_prompt("Consolidation interval (evaluations)")
        .default(config.reasoning.consolidation_interval)
        .interact_text()?;

    config.reasoning.consolidation_interval = consolidation_interval;

    println!("\n✓ ReasoningBank configured.\n");
    Ok(())
}

/// Configures cache.
fn configure_cache(theme: &ColorfulTheme, config: &mut Config) -> TetradResult<()> {
    println!("\n💾 Cache Configuration\n");

    // Enabled
    config.cache.enabled = Confirm::with_theme(theme)
        .with_prompt("Cache enabled?")
        .default(config.cache.enabled)
        .interact()?;

    if !config.cache.enabled {
        println!("Cache disabled.\n");
        return Ok(());
    }

    // Capacity
    let capacity: usize = Input::with_theme(theme)
        .with_prompt("Maximum capacity (number of entries)")
        .default(config.cache.capacity)
        .interact_text()?;

    config.cache.capacity = capacity;

    // TTL
    let ttl: u64 = Input::with_theme(theme)
        .with_prompt("Time to live (seconds)")
        .default(config.cache.ttl_secs)
        .interact_text()?;

    config.cache.ttl_secs = ttl;

    println!("\n✓ Cache configured.\n");
    Ok(())
}

/// Shows configuration summary.
pub fn show_config_summary(config: &Config) {
    println!("\n📊 Configuration Summary\n");
    println!("┌─────────────────────────────────────────┐");
    println!("│ General                                 │");
    println!("├─────────────────────────────────────────┤");
    println!("│ Log level: {:<28} │", config.general.log_level);
    println!("│ Timeout: {:<29}s │", config.general.timeout_secs);
    println!("├─────────────────────────────────────────┤");
    println!("│ Executors                               │");
    println!("├─────────────────────────────────────────┤");
    println!(
        "│ Codex:  {} ({:<26}) │",
        if config.executors.codex.enabled {
            ""
        } else {
            ""
        },
        config.executors.codex.command
    );
    println!(
        "│ Gemini: {} ({:<26}) │",
        if config.executors.gemini.enabled {
            ""
        } else {
            ""
        },
        config.executors.gemini.command
    );
    println!(
        "│ Qwen:   {} ({:<26}) │",
        if config.executors.qwen.enabled {
            ""
        } else {
            ""
        },
        config.executors.qwen.command
    );
    println!("├─────────────────────────────────────────┤");
    println!("│ Consensus                               │");
    println!("├─────────────────────────────────────────┤");
    println!(
        "│ Rule: {:<33} │",
        format!("{:?}", config.consensus.default_rule)
    );
    println!("│ Min score: {:<28} │", config.consensus.min_score);
    println!("│ Max loops: {:<28} │", config.consensus.max_loops);
    println!("├─────────────────────────────────────────┤");
    println!("│ ReasoningBank                           │");
    println!("├─────────────────────────────────────────┤");
    println!(
        "│ Enabled: {:<30} │",
        if config.reasoning.enabled {
            "Yes"
        } else {
            "No"
        }
    );
    if config.reasoning.enabled {
        println!(
            "│ Consolidation: every {:<17} │",
            format!("{} evaluations", config.reasoning.consolidation_interval)
        );
    }
    println!("├─────────────────────────────────────────┤");
    println!("│ Cache                                   │");
    println!("├─────────────────────────────────────────┤");
    println!(
        "│ Enabled: {:<30} │",
        if config.cache.enabled { "Yes" } else { "No" }
    );
    if config.cache.enabled {
        println!("│ Capacity: {:<29} │", config.cache.capacity);
        println!("│ TTL: {:<33}s │", config.cache.ttl_secs);
    }
    println!("└─────────────────────────────────────────┘");
    println!();
}

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

    #[test]
    fn test_show_config_summary() {
        let config = Config::default_config();
        // Just verify it doesn't panic
        show_config_summary(&config);
    }
}