reasonkit/thinktool/
toml_loader.rs

1//! TOML Protocol Loader
2//!
3//! Loads ThinkTool protocols from TOML files.
4
5use serde::{Deserialize, Serialize};
6use std::collections::HashMap;
7use std::path::Path;
8
9use super::protocol::{
10    CritiqueSeverity, DecisionMethod, InputSpec, OutputSpec, Protocol, ProtocolMetadata,
11    ProtocolStep, ReasoningStrategy, StepAction,
12};
13use crate::error::{Error, Result};
14
15/// TOML ThinkTool module definition
16#[derive(Debug, Clone, Deserialize, Serialize)]
17struct TomlThinkToolModule {
18    id: String,
19    name: String,
20    #[serde(default)]
21    shortcode: String,
22    category: String,
23    tier: String,
24    description: String,
25    capabilities: Vec<String>,
26    #[serde(default)]
27    output_schema: String,
28    #[serde(default)]
29    parameters: HashMap<String, serde_json::Value>,
30    #[serde(default)]
31    confidence_factors: Vec<TomlConfidenceFactor>,
32    thinking_pattern: TomlThinkingPattern,
33    #[serde(default)]
34    typical_duration: String,
35    #[serde(default)]
36    token_cost_estimate: String,
37    #[serde(default)]
38    steps: Option<Vec<ProtocolStep>>,
39}
40
41#[derive(Debug, Clone, Deserialize, Serialize)]
42struct TomlConfidenceFactor {
43    factor: String,
44    weight: f64,
45    formula: String,
46}
47
48#[derive(Debug, Clone, Deserialize, Serialize)]
49struct TomlThinkingPattern {
50    #[serde(rename = "type")]
51    pattern_type: String,
52    steps: Vec<String>,
53}
54
55/// Root TOML structure
56#[derive(Debug, Clone, Deserialize, Serialize)]
57struct TomlThinkToolsV2 {
58    version: String,
59    schema: String,
60    #[serde(default)]
61    thinktool_modules: HashMap<String, TomlThinkToolModule>,
62}
63
64/// Load protocols from a TOML file
65pub fn load_from_toml_file(path: &Path) -> Result<Vec<Protocol>> {
66    let content = std::fs::read_to_string(path).map_err(|e| Error::IoMessage {
67        message: format!("Failed to read TOML file {}: {}", path.display(), e),
68    })?;
69
70    load_from_toml_string(&content)
71}
72
73/// Load protocols from a TOML string
74pub fn load_from_toml_string(toml_content: &str) -> Result<Vec<Protocol>> {
75    let toml_data: TomlThinkToolsV2 = toml::from_str(toml_content).map_err(|e| Error::Parse {
76        message: format!("Failed to parse TOML: {}", e),
77    })?;
78
79    let mut protocols = Vec::new();
80
81    for (module_key, module) in toml_data.thinktool_modules {
82        let protocol = convert_toml_module_to_protocol(&module_key, &module)?;
83        protocols.push(protocol);
84    }
85
86    Ok(protocols)
87}
88
89/// Convert a TOML module definition to a Protocol struct
90fn convert_toml_module_to_protocol(
91    module_key: &str,
92    toml_module: &TomlThinkToolModule,
93) -> Result<Protocol> {
94    // Determine reasoning strategy from category
95    let strategy = match toml_module.category.as_str() {
96        "divergent" => ReasoningStrategy::Expansive,
97        "convergent" => ReasoningStrategy::Deductive,
98        "foundational" => ReasoningStrategy::Analytical,
99        "verification" => ReasoningStrategy::Verification,
100        "adversarial" => ReasoningStrategy::Adversarial,
101        _ => ReasoningStrategy::Analytical,
102    };
103
104    // Build input spec based on module type
105    let input = build_input_spec(module_key);
106
107    // Build steps: use custom defined steps if provided, otherwise fallback to pattern
108    let steps = if let Some(custom_steps) = &toml_module.steps {
109        custom_steps.clone()
110    } else {
111        build_steps_from_pattern(&toml_module.thinking_pattern, module_key)?
112    };
113
114    // Build output spec
115    let output = build_output_spec(&toml_module.name);
116
117    // Build metadata
118    let metadata = ProtocolMetadata {
119        category: toml_module.category.clone(),
120        composable_with: get_composable_modules(module_key),
121        typical_tokens: estimate_tokens(&toml_module.token_cost_estimate),
122        estimated_latency_ms: estimate_latency(&toml_module.typical_duration),
123        ..Default::default()
124    };
125
126    let protocol = Protocol {
127        id: module_key.to_string(),
128        name: toml_module.name.clone(),
129        version: "2.0.0".to_string(),
130        description: toml_module.description.trim().to_string(),
131        strategy,
132        input,
133        steps,
134        output,
135        validation: Vec::new(),
136        metadata,
137    };
138
139    // Validate the protocol
140    protocol.validate().map_err(|errors| {
141        Error::Validation(format!(
142            "Invalid protocol {}: {}",
143            protocol.id,
144            errors.join(", ")
145        ))
146    })?;
147
148    Ok(protocol)
149}
150
151// Helper functions (duplicated from yaml_loader for now to avoid refactoring)
152
153fn build_input_spec(module_key: &str) -> InputSpec {
154    match module_key {
155        "gigathink" => InputSpec {
156            required: vec!["query".to_string()],
157            optional: vec!["context".to_string(), "constraints".to_string()],
158        },
159        "laserlogic" => InputSpec {
160            required: vec!["argument".to_string()],
161            optional: vec!["context".to_string()],
162        },
163        "bedrock" => InputSpec {
164            required: vec!["statement".to_string()],
165            optional: vec!["domain".to_string()],
166        },
167        "proofguard" => InputSpec {
168            required: vec!["claim".to_string()],
169            optional: vec!["sources".to_string()],
170        },
171        "brutalhonesty" => InputSpec {
172            required: vec!["work".to_string()],
173            optional: vec!["criteria".to_string()],
174        },
175        _ => InputSpec::default(),
176    }
177}
178
179fn build_steps_from_pattern(
180    _pattern: &TomlThinkingPattern,
181    module_key: &str,
182) -> Result<Vec<ProtocolStep>> {
183    // Note: In a real implementation, we would parse the pattern steps.
184    // For now, we reuse the hardcoded builders from the original implementation
185    // to ensure consistency, keyed by the module ID.
186    match module_key {
187        "gigathink" => Ok(build_gigathink_steps()),
188        "laserlogic" => Ok(build_laserlogic_steps()),
189        "bedrock" => Ok(build_bedrock_steps()),
190        "proofguard" => Ok(build_proofguard_steps()),
191        "brutalhonesty" => Ok(build_brutalhonesty_steps()),
192        "powercombo" => Ok(build_gigathink_steps()),
193        _ => Err(Error::Validation(format!(
194            "Unknown module type: {}",
195            module_key
196        ))),
197    }
198}
199
200// Re-implement step builders (copy from yaml_loader.rs)
201// This duplication is acceptable for this task to avoid breaking existing code
202// by refactoring yaml_loader.rs into a shared module.
203
204use super::protocol::{AggregationType as AggType, StepOutputFormat as StepFmt};
205
206fn build_gigathink_steps() -> Vec<ProtocolStep> {
207    vec![
208        ProtocolStep {
209            id: "identify_dimensions".to_string(),
210            action: StepAction::Generate {
211                min_count: 5,
212                max_count: 10,
213            },
214            prompt_template:
215                r#"# GigaThink: Identify Analysis Dimensions
216
217Your task is to identify 5-10 distinct dimensions or angles from which to analyze this question. Think expansively and creatively - consider technical, business, ethical, social, temporal, and other perspectives.
218
219## Question
220{{query}}
221
222{{#if context}}
223## Additional Context
224{{context}}
225{{/if}}
226
227{{#if constraints}}
228## Constraints
229{{constraints}}
230{{/if}}
231
232## Instructions
2331. Generate 5-10 distinct analytical dimensions
2342. Each dimension should offer a unique perspective on the question
2353. Consider diverse angles: technical, business, ethical, social, temporal, strategic, tactical, user-focused, system-focused, etc.
2364. For each dimension, provide:
237   - A clear, descriptive label (2-5 words)
238   - A brief explanation of why this dimension matters
239
240## Output Format
241Provide a numbered list. For each dimension:
242
2431. [Dimension Label]: [Brief explanation of why this perspective matters]
2442. [Dimension Label]: [Brief explanation]
245...
246
247Be specific and actionable. Avoid generic dimensions like "cost" or "quality" - instead, think about what specific aspect of cost or quality is most relevant."#
248                    .to_string(),
249            output_format: StepFmt::List,
250            min_confidence: 0.7,
251            depends_on: vec![],
252            branch: None,
253        },
254        ProtocolStep {
255            id: "explore_perspectives".to_string(),
256            action: StepAction::Analyze {
257                criteria: vec![
258                    "novelty".to_string(),
259                    "relevance".to_string(),
260                    "depth".to_string(),
261                ],
262            },
263            prompt_template: r#"# GigaThink: Explore Perspectives
264
265Now explore each identified dimension in depth. For each dimension, provide a comprehensive analysis from that perspective.
266
267## Question
268{{query}}
269
270## Dimensions to Explore
271{{identify_dimensions}}
272
273{{#if context}}
274## Additional Context
275{{context}}
276{{/if}}
277
278## Instructions
279For each dimension identified above, provide:
280
2811. **Key Insight**: What is the most important insight from this perspective?
2822. **Supporting Evidence**: What evidence, examples, or reasoning supports this insight?
2833. **Implications**: What are the practical implications or consequences of this perspective?
2844. **Confidence Score**: Rate your confidence in this analysis (0.0-1.0) with a brief justification
285
286## Output Format
287For each dimension, provide:
288
289### [Dimension Label]
290- **Key Insight**: [Your insight]
291- **Supporting Evidence**: [Evidence or reasoning]
292- **Implications**: [Practical consequences]
293- **Confidence**: [0.0-1.0] - [Justification]
294
295Be thorough but concise. Each perspective should add unique value to the overall analysis."#
296                .to_string(),
297            output_format: StepFmt::Structured,
298            min_confidence: 0.6,
299            depends_on: vec!["identify_dimensions".to_string()],
300            branch: None,
301        },
302        ProtocolStep {
303            id: "synthesize".to_string(),
304            action: StepAction::Synthesize {
305                aggregation: AggType::ThematicClustering,
306            },
307            prompt_template:
308                r#"# GigaThink: Synthesize Insights
309
310Synthesize all perspectives into coherent themes, actionable insights, and a clear conclusion.
311
312## Question
313{{query}}
314
315## Perspectives Analyzed
316{{explore_perspectives}}
317
318## Instructions
319Synthesize the diverse perspectives into:
320
3211. **Major Themes** (2-4 themes): What patterns or themes emerge across perspectives?
3222. **Key Insights** (3-5 insights): What are the most important takeaways?
3233. **Recommended Actions** (if applicable): What should be done based on this analysis?
3244. **Areas of Uncertainty**: What remains unclear or requires further investigation?
3255. **Overall Confidence**: What is your overall confidence in this synthesis? (0.0-1.0)
326
327## Output Format
328
329### Major Themes
3301. [Theme Name]: [Description and why it matters]
3312. [Theme Name]: [Description]
332...
333
334### Key Insights
3351. [Insight]: [Explanation]
3362. [Insight]: [Explanation]
337...
338
339### Recommended Actions (if applicable)
340- [Action 1]: [Why this action is recommended]
341- [Action 2]: [Why this action is recommended]
342...
343
344### Areas of Uncertainty
345- [Uncertainty 1]: [Why this is uncertain]
346- [Uncertainty 2]: [Why this is uncertain]
347...
348
349### Overall Confidence
350[0.0-1.0] - [Justification]
351
352Ensure your synthesis is coherent, actionable, and acknowledges both strengths and limitations of the analysis."#
353                    .to_string(),
354            output_format: StepFmt::Structured,
355            min_confidence: 0.8,
356            depends_on: vec!["explore_perspectives".to_string()],
357            branch: None,
358        },
359    ]
360}
361
362fn build_laserlogic_steps() -> Vec<ProtocolStep> {
363    vec![
364        ProtocolStep {
365            id: "extract_claims".to_string(),
366            action: StepAction::Analyze {
367                criteria: vec!["clarity".to_string(), "completeness".to_string()],
368            },
369            prompt_template: r#"# LaserLogic: Extract Logical Structure
370
371Extract the logical structure from this argument. Identify all claims, premises, and assumptions.
372
373## Argument
374{{argument}}
375
376{{#if context}}
377## Additional Context
378{{context}}
379{{/if}}
380
381## Instructions
382Identify and extract:
383
3841. **Main Conclusion**: What is the primary claim being made?
3852. **Supporting Premises**: What explicit reasons or evidence support the conclusion?
3863. **Implicit Assumptions**: What unstated assumptions are necessary for the argument to work?
3874. **Causal Claims**: Are there any causal relationships claimed? (If A, then B)
3885. **Logical Structure**: What is the logical form? (e.g., modus ponens, modus tollens, syllogism, etc.)
389
390## Output Format
391
392### Main Conclusion
393[State the conclusion clearly]
394
395### Supporting Premises
3961. [Premise 1]
3972. [Premise 2]
398...
399
400### Implicit Assumptions
4011. [Assumption 1]
4022. [Assumption 2]
403...
404
405### Causal Claims (if any)
406- [Claim 1]: [Description]
407- [Claim 2]: [Description]
408...
409
410### Logical Structure
411[Identify the logical form or pattern]
412
413Be precise and explicit. Format each claim as a clear, unambiguous statement."#
414                .to_string(),
415            output_format: StepFmt::Structured,
416            min_confidence: 0.7,
417            depends_on: vec![],
418            branch: None,
419        },
420        ProtocolStep {
421            id: "check_validity".to_string(),
422            action: StepAction::Validate {
423                rules: vec![
424                    "logical_consistency".to_string(),
425                    "premise_support".to_string(),
426                ],
427            },
428            prompt_template: r#"# LaserLogic: Check Logical Validity
429
430Evaluate the logical validity of the argument structure. Determine if the premises logically lead to the conclusion.
431
432## Argument Structure
433{{extract_claims}}
434
435## Instructions
436Evaluate the logical validity:
437
4381. **Premise-to-Conclusion Flow**: Do the premises logically lead to the conclusion?
439   - Check for logical gaps
440   - Identify missing steps in the reasoning chain
441   - Determine if the conclusion follows necessarily from the premises
442
4432. **Logical Structure Assessment**:
444   - Is the argument **valid**? (If premises are true, conclusion must be true)
445   - Is the argument **sound**? (Valid + premises are actually true)
446   - What type of reasoning is used? (Deductive, inductive, abductive)
447
4483. **Reasoning Chain Integrity**:
449   - Are there any logical leaps?
450   - Are all necessary intermediate steps present?
451   - Could the conclusion be false even if premises are true?
452
4534. **Logical Strength Rating**: Rate the logical strength (0.0-1.0) with detailed justification
454
455## Output Format
456
457### Premise-to-Conclusion Flow
458[Analysis of whether premises support conclusion]
459
460### Logical Structure Assessment
461- **Validity**: [Valid/Invalid] - [Explanation]
462- **Soundness**: [Sound/Unsound] - [Explanation]
463- **Reasoning Type**: [Deductive/Inductive/Abductive] - [Explanation]
464
465### Reasoning Chain Integrity
466- **Gaps Identified**: [List any logical gaps]
467- **Missing Steps**: [List any missing intermediate steps]
468- **Logical Leaps**: [List any unjustified leaps]
469
470### Logical Strength
471[0.0-1.0] - [Detailed justification]
472
473Be rigorous. A valid argument can still be unsound if premises are false."#
474                .to_string(),
475            output_format: StepFmt::Structured,
476            min_confidence: 0.8,
477            depends_on: vec!["extract_claims".to_string()],
478            branch: None,
479        },
480        ProtocolStep {
481            id: "detect_fallacies".to_string(),
482            action: StepAction::Critique {
483                severity: CritiqueSeverity::Standard,
484            },
485            prompt_template: r#"# LaserLogic: Detect Logical Fallacies
486
487Check for logical fallacies in the argument. Identify any errors in reasoning.
488
489## Argument Structure
490{{extract_claims}}
491
492## Common Fallacies to Check
493
494**Informal Fallacies:**
495- **Ad Hominem**: Attacking the person instead of the argument
496- **Straw Man**: Misrepresenting the opponent's position
497- **False Dichotomy**: Presenting only two options when more exist
498- **Slippery Slope**: Assuming one thing will lead to extreme consequences
499- **Red Herring**: Introducing irrelevant information
500- **Appeal to Authority**: Using authority as proof without justification
501- **Appeal to Emotion**: Using emotion instead of logic
502- **Circular Reasoning**: Using the conclusion as a premise
503- **Hasty Generalization**: Drawing conclusions from insufficient evidence
504- **Post Hoc**: Assuming causation from correlation
505- **Begging the Question**: Assuming what you're trying to prove
506- **False Cause**: Incorrectly identifying cause and effect
507
508**Formal Fallacies:**
509- **Affirming the Consequent**: If P then Q, Q, therefore P (invalid)
510- **Denying the Antecedent**: If P then Q, not P, therefore not Q (invalid)
511- **Undistributed Middle**: All A are B, all C are B, therefore all A are C (invalid)
512
513## Instructions
514For each fallacy found:
5151. Identify the specific fallacy
5162. Explain where it occurs in the argument
5173. Explain why it's a fallacy
5184. Suggest how the argument could be corrected
519
520## Output Format
521
522### Fallacies Detected
523
524**1. [Fallacy Name]**
525- **Location**: [Where in the argument this occurs]
526- **Explanation**: [Why this is a fallacy]
527- **Correction**: [How to fix this]
528
529**2. [Fallacy Name]**
530- [Same format]
531...
532
533### Summary
534- **Total Fallacies Found**: [Number]
535- **Severity**: [Low/Medium/High] - [Justification]
536- **Impact on Argument**: [How do these fallacies affect the argument's validity?]
537
538If no fallacies are found, state: "No logical fallacies detected. The argument structure is logically sound.""#
539                .to_string(),
540            output_format: StepFmt::List,
541            min_confidence: 0.7,
542            depends_on: vec!["extract_claims".to_string()],
543            branch: None,
544        },
545    ]
546}
547
548fn build_bedrock_steps() -> Vec<ProtocolStep> {
549    vec![
550        ProtocolStep {
551            id: "decompose".to_string(),
552            action: StepAction::Analyze {
553                criteria: vec!["fundamentality".to_string(), "independence".to_string()],
554            },
555            prompt_template: r#"# BedRock: Decompose to First Principles
556
557Decompose this statement to its fundamental axioms or assumptions. Ask "Why?" repeatedly until you reach irreducible truths.
558
559## Statement
560{{statement}}
561
562{{#if domain}}
563## Domain Context
564{{domain}}
565{{/if}}
566
567{{#if context}}
568## Additional Context
569{{context}}
570{{/if}}
571
572## Instructions
573Use the "5 Whys" technique or similar first-principles thinking:
574
5751. Start with the statement
5762. For each component, ask: "What is this based on? Why is this true?"
5773. Continue decomposing until you reach:
578   - Fundamental axioms (self-evident truths)
579   - Empirical facts (observable, verifiable)
580   - Definitions (by definition true)
581   - Assumptions (must be accepted as given)
582
5834. Build a tree structure showing dependencies
5845. Identify what can be further decomposed vs. what is fundamental
585
586## Output Format
587
588### Decomposition Tree
589
590```
591[Statement]
592├── [Component 1]
593│   ├── [Sub-component 1.1]
594│   │   └── [Axiom/Fact/Definition/Assumption]
595│   └── [Sub-component 1.2]
596│       └── [Axiom/Fact/Definition/Assumption]
597└── [Component 2]
598    └── [Axiom/Fact/Definition/Assumption]
599```
600
601### Component Analysis
602For each major component:
603- **Component**: [Name]
604- **Decomposed From**: [What it's based on]
605- **Type**: [Axiom/Empirical Fact/Definition/Assumption/Further Decomposable]
606- **Certainty**: [0.0-1.0] - [Justification]
607
608Be thorough. Don't stop at surface-level explanations - dig deeper."#
609                .to_string(),
610            output_format: StepFmt::Structured,
611            min_confidence: 0.7,
612            depends_on: vec![],
613            branch: None,
614        },
615        ProtocolStep {
616            id: "identify_axioms".to_string(),
617            action: StepAction::Generate {
618                min_count: 3,
619                max_count: 7,
620            },
621            prompt_template: r#"# BedRock: Identify Foundational Axioms
622
623From the decomposition, identify the foundational axioms - the irreducible truths that everything else depends on.
624
625## Decomposition
626{{decompose}}
627
628## Instructions
629Identify all foundational axioms, facts, definitions, and assumptions:
630
6311. **Axioms**: Self-evident truths that cannot be proven but are accepted as true
6322. **Empirical Facts**: Observable, verifiable truths about the world
6333. **Definitions**: Truths by definition (e.g., "a triangle has three sides")
6344. **Assumptions**: Things that must be accepted as given for the statement to hold
635
636For each foundational element:
637- State it clearly
638- Explain why it's fundamental (cannot be further reduced)
639- Classify its type (axiom, empirical fact, definition, assumption)
640- Rate certainty (0.0-1.0)
641- Note any dependencies or prerequisites
642
643## Output Format
644
645### Foundational Axioms
646
647**1. [Axiom/Fact/Definition/Assumption Name]**
648- **Statement**: [Clear statement of the axiom]
649- **Type**: [Axiom/Empirical Fact/Definition/Assumption]
650- **Why Fundamental**: [Explanation of why this cannot be further reduced]
651- **Certainty**: [0.0-1.0] - [Justification]
652- **Dependencies**: [What this depends on, if anything]
653
654**2. [Next Axiom]**
655- [Same format]
656...
657
658### Summary
659- **Total Foundational Elements**: [Number]
660- **Breakdown**: [X axioms, Y facts, Z definitions, W assumptions]
661- **Overall Certainty**: [0.0-1.0] - [Based on the certainty of foundational elements]
662
663Be precise. Distinguish between what is truly fundamental vs. what could be further decomposed."#
664                .to_string(),
665            output_format: StepFmt::List,
666            min_confidence: 0.8,
667            depends_on: vec!["decompose".to_string()],
668            branch: None,
669        },
670        ProtocolStep {
671            id: "reconstruct".to_string(),
672            action: StepAction::Synthesize {
673                aggregation: AggType::WeightedMerge,
674            },
675            prompt_template: r#"# BedRock: Reconstruct from Axioms
676
677Reconstruct the original statement from the foundational axioms. Show the logical path and identify any gaps.
678
679## Foundational Axioms
680{{identify_axioms}}
681
682## Original Statement
683{{statement}}
684
685## Instructions
686Reconstruct the statement by:
687
6881. **Building the Logical Path**: Show how the axioms logically lead to the statement
6892. **Identifying Gaps**: Note any logical leaps or missing steps
6903. **Assessing Completeness**: Determine if the axioms fully support the statement
6914. **Calculating Confidence**: Compute overall confidence based on axiom certainties
692
693## Output Format
694
695### Reconstruction Path
696
697```
698[Axiom 1] + [Axiom 2] + [Axiom 3]
699700[Intermediate Conclusion 1]
701702[Intermediate Conclusion 2]
703704[Original Statement]
705```
706
707### Step-by-Step Logic
7081. **From Axioms**: [How axioms combine]
7092. **To Intermediate 1**: [First logical step]
7103. **To Intermediate 2**: [Second logical step]
7114. **To Final Statement**: [Final logical step]
712
713### Gaps and Leaps
714- **Gaps Identified**: [List any logical gaps]
715- **Missing Steps**: [What steps are missing?]
716- **Unjustified Leaps**: [Any leaps that need justification]
717
718### Completeness Assessment
719- **Axioms Support Statement**: [Yes/Partially/No] - [Explanation]
720- **Missing Axioms**: [Are additional axioms needed?]
721- **Over-Determined**: [Are there unnecessary axioms?]
722
723### Overall Confidence
724[0.0-1.0] - [Calculation based on axiom certainties and gap analysis]
725
726Be honest about gaps. Not all statements can be fully reconstructed from axioms alone."#
727                .to_string(),
728            output_format: StepFmt::Structured,
729            min_confidence: 0.75,
730            depends_on: vec!["identify_axioms".to_string()],
731            branch: None,
732        },
733    ]
734}
735
736fn build_proofguard_steps() -> Vec<ProtocolStep> {
737    vec![
738        ProtocolStep {
739            id: "identify_sources".to_string(),
740            action: StepAction::CrossReference { min_sources: 3 },
741            prompt_template: r#"# ProofGuard: Identify Verification Sources
742
743Identify 3+ independent sources that could verify or refute this claim. Prioritize high-quality, authoritative sources.
744
745## Claim to Verify
746{{claim}}
747
748{{#if sources}}
749## Known Sources (Optional)
750{{sources}}
751{{/if}}
752
753{{#if context}}
754## Additional Context
755{{context}}
756{{/if}}
757
758## Instructions
759Identify potential verification sources:
760
7611. **Source Types to Consider**:
762   - Official documentation (product docs, API docs, standards)
763   - Peer-reviewed research (academic papers, studies)
764   - Primary sources (original research, official statements)
765   - Authoritative references (expert opinions, industry standards)
766   - Reputable news/media (if applicable)
767   - Direct observation or testing (if applicable)
768
7692. **Quality Criteria**:
770   - Independence: Sources should be independent of each other
771   - Authority: Sources should be authoritative in the domain
772   - Recency: Prefer recent sources when applicable
773   - Reliability: Prefer sources with good track records
774
7753. **Diversity**: Include different types of sources (not all from the same category)
776
777## Output Format
778
779### Potential Sources
780
781**1. [Source Name/Title]**
782- **Type**: [Official Docs/Research Paper/Primary Source/Expert Opinion/etc.]
783- **URL/Reference**: [If available]
784- **Authority Level**: [High/Medium/Low] - [Justification]
785- **Relevance**: [How relevant is this source to verifying the claim?]
786
787**2. [Next Source]**
788- [Same format]
789...
790
791### Source Quality Assessment
792- **Total Sources Identified**: [Number]
793- **High Authority Sources**: [Number]
794- **Source Diversity**: [Assessment of diversity]
795- **Overall Source Quality**: [High/Medium/Low]
796
797Aim for at least 3 sources, preferably 5+. Prioritize quality over quantity."#
798                .to_string(),
799            output_format: StepFmt::List,
800            min_confidence: 0.6,
801            depends_on: vec![],
802            branch: None,
803        },
804        ProtocolStep {
805            id: "verify_each".to_string(),
806            action: StepAction::Validate {
807                rules: vec![
808                    "source_reliability".to_string(),
809                    "claim_support".to_string(),
810                ],
811            },
812            prompt_template: r#"# ProofGuard: Verify Each Source
813
814For each source, evaluate what it says about the claim. Determine support level and source reliability.
815
816## Claim
817{{claim}}
818
819## Sources to Check
820{{identify_sources}}
821
822## Instructions
823For each source identified:
824
8251. **What Does It Say?**: What does the source explicitly state about the claim?
8262. **Support Level**:
827   - **Confirms**: Source directly supports the claim
828   - **Partially Confirms**: Source supports part of the claim
829   - **Neutral**: Source doesn't address the claim directly
830   - **Contradicts**: Source contradicts the claim
831   - **Unclear**: Source is ambiguous or unclear
8323. **Source Reliability**: Rate the source's reliability (0.0-1.0) based on:
833   - Authority and expertise
834   - Track record and reputation
835   - Methodology (for research)
836   - Recency and relevance
8374. **Key Evidence**: Extract key quotes, data, or evidence from the source
838
839## Output Format
840
841### Source Evaluations
842
843**Source 1: [Source Name]**
844- **What It Says**: [Direct quote or summary of what the source states about the claim]
845- **Support Level**: [Confirms/Partially Confirms/Neutral/Contradicts/Unclear]
846- **Source Reliability**: [0.0-1.0] - [Justification]
847- **Key Evidence**: [Key quote, data point, or evidence]
848- **Notes**: [Any additional relevant information]
849
850**Source 2: [Next Source]**
851- [Same format]
852...
853
854### Summary
855- **Confirming Sources**: [Number and list]
856- **Contradicting Sources**: [Number and list]
857- **Neutral/Unclear Sources**: [Number and list]
858- **Average Source Reliability**: [0.0-1.0]
859
860Be objective. Report what sources actually say, not what you want them to say."#
861                .to_string(),
862            output_format: StepFmt::Structured,
863            min_confidence: 0.7,
864            depends_on: vec!["identify_sources".to_string()],
865            branch: None,
866        },
867        ProtocolStep {
868            id: "triangulate".to_string(),
869            action: StepAction::Synthesize {
870                aggregation: AggType::Consensus,
871            },
872            prompt_template: r#"# ProofGuard: Triangulate Claim Validity
873
874Apply triangulation to determine the overall validity of the claim based on multiple independent sources.
875
876## Claim
877{{claim}}
878
879## Source Evaluations
880{{verify_each}}
881
882## Triangulation Rules
883
884**High Confidence (0.8-1.0)**:
885- 3+ independent sources all confirm the claim
886- Sources are high reliability (0.8+)
887- No contradictions
888
889**Medium Confidence (0.6-0.8)**:
890- 2+ sources confirm, 1 neutral
891- Sources are medium-high reliability (0.6+)
892- Minor contradictions that can be resolved
893
894**Low Confidence (0.4-0.6)**:
895- Mixed results (some confirm, some contradict)
896- Sources have varying reliability
897- Significant contradictions
898
899**Very Low Confidence (<0.4)**:
900- Majority of sources contradict
901- Sources are low reliability
902- Claim cannot be verified
903
904**Flag for Review**:
905- Any direct contradiction between high-reliability sources
906- Claim contradicts established facts
907- Sources are all low reliability
908
909## Instructions
910Apply triangulation:
911
9121. **Count Confirmations**: How many sources confirm vs. contradict?
9132. **Weight by Reliability**: Give more weight to high-reliability sources
9143. **Resolve Contradictions**: Can contradictions be explained or resolved?
9154. **Determine Confidence**: Apply triangulation rules above
9165. **Note Discrepancies**: Document any contradictions or uncertainties
9176. **Final Verdict**: Is the claim verified, partially verified, unverified, or refuted?
918
919## Output Format
920
921### Triangulation Analysis
922
923**Confirmation Count**:
924- Confirming: [Number] sources
925- Contradicting: [Number] sources
926- Neutral/Unclear: [Number] sources
927
928**Weighted Assessment** (by source reliability):
929- Weighted confirmation score: [0.0-1.0]
930- Weighted contradiction score: [0.0-1.0]
931
932**Contradiction Analysis**:
933- Contradictions identified: [Yes/No]
934- Can contradictions be resolved? [Yes/No/Partially] - [Explanation]
935- Key discrepancies: [List any significant discrepancies]
936
937### Final Verdict
938- **Claim Status**: [Verified/Partially Verified/Unverified/Refuted]
939- **Confidence Score**: [0.0-1.0] - [Justification based on triangulation rules]
940- **Key Evidence**: [Summary of strongest evidence for/against]
941- **Recommendations**: [Should this claim be accepted, rejected, or require further investigation?]
942
943### Flags and Warnings
944- [List any flags for review]
945- [Note any significant uncertainties]
946- [Recommend additional sources if needed]
947
948Be rigorous. Triangulation requires multiple independent sources agreeing. One source is not enough."#
949                .to_string(),
950            output_format: StepFmt::Structured,
951            min_confidence: 0.8,
952            depends_on: vec!["verify_each".to_string()],
953            branch: None,
954        },
955    ]
956}
957
958fn build_brutalhonesty_steps() -> Vec<ProtocolStep> {
959    vec![
960        ProtocolStep {
961            id: "steelman".to_string(),
962            action: StepAction::Analyze {
963                criteria: vec!["strengths".to_string()],
964            },
965            prompt_template: r#"# BrutalHonesty: Steelman the Work
966
967First, steelman the work - present it in the strongest possible light. Identify genuine strengths and value.
968
969## Work to Critique
970{{work}}
971
972{{#if context}}
973## Additional Context
974{{context}}
975{{/if}}
976
977## Instructions
978Before critiquing, steelman the work:
979
9801. **What Does This Do Well?**: Identify genuine strengths, not just politeness
9812. **What Problems Does It Solve?**: What real problems or needs does this address?
9823. **What Is Genuinely Valuable?**: What unique value does this provide?
9834. **What Are the Best Arguments For It?**: What are the strongest arguments in favor?
9845. **What Would Supporters Say?**: How would advocates defend this work?
985
986Be generous but honest. A good steelman helps identify what's worth preserving even after critique.
987
988## Output Format
989
990### Strengths
991
992**1. [Strength Category]**
993- **What**: [What does this do well?]
994- **Why It Matters**: [Why is this valuable?]
995- **Evidence**: [What demonstrates this strength?]
996
997**2. [Next Strength]**
998- [Same format]
999...
1000
1001### Problems Solved
1002- [Problem 1]: [How this work addresses it]
1003- [Problem 2]: [How this work addresses it]
1004...
1005
1006### Genuine Value
1007- [Value 1]: [Explanation]
1008- [Value 2]: [Explanation]
1009...
1010
1011### Best Arguments For
10121. [Argument 1]: [Explanation]
10132. [Argument 2]: [Explanation]
1014...
1015
1016### Summary
1017- **Overall Assessment of Strengths**: [Brief summary]
1018- **Core Value Proposition**: [What is the core value this work provides?]
1019
1020Be thorough. A strong steelman makes the subsequent critique more credible and useful."#
1021                .to_string(),
1022            output_format: StepFmt::List,
1023            min_confidence: 0.7,
1024            depends_on: vec![],
1025            branch: None,
1026        },
1027        ProtocolStep {
1028            id: "attack".to_string(),
1029            action: StepAction::Critique {
1030                severity: CritiqueSeverity::Brutal,
1031            },
1032            prompt_template: r#"# BrutalHonesty: Attack the Work
1033
1034Now be brutally honest. Attack the work from all angles. Find every flaw, weakness, and problem.
1035
1036## Work
1037{{work}}
1038
1039## Strengths Identified (from Steelman)
1040{{steelman}}
1041
1042## Instructions
1043Attack the work from all angles. Don't hold back:
1044
10451. **Logical Flaws**: Are there errors in reasoning, logic, or argumentation?
10462. **Missing Considerations**: What important factors, perspectives, or consequences are overlooked?
10473. **Weak Assumptions**: What assumptions are questionable, unstated, or unsupported?
10484. **Implementation Problems**: What practical problems would arise in implementation?
10495. **Unintended Consequences**: What negative side effects or unintended outcomes might occur?
10506. **Competing Alternatives**: What better alternatives exist? Why isn't this the best approach?
10517. **Resource Concerns**: Are there cost, time, or resource issues?
10528. **Scalability Issues**: Will this work at scale? Under stress? Over time?
10539. **User Experience Problems**: Will users actually want or use this?
105410. **What Would Critics Say?**: What would harsh but fair critics point out?
1055
1056Be specific. Vague criticism is useless. Point to exact problems and explain why they matter.
1057
1058## Output Format
1059
1060### Logical Flaws
1061- **Flaw 1**: [Specific flaw] - [Why this is a problem]
1062- **Flaw 2**: [Specific flaw] - [Why this is a problem]
1063...
1064
1065### Missing Considerations
1066- **Missing 1**: [What's missing] - [Why this matters]
1067- **Missing 2**: [What's missing] - [Why this matters]
1068...
1069
1070### Weak Assumptions
1071- **Assumption 1**: [Questionable assumption] - [Why it's weak]
1072- **Assumption 2**: [Questionable assumption] - [Why it's weak]
1073...
1074
1075### Implementation Problems
1076- **Problem 1**: [Implementation issue] - [Impact]
1077- **Problem 2**: [Implementation issue] - [Impact]
1078...
1079
1080### Unintended Consequences
1081- **Consequence 1**: [Negative outcome] - [Likelihood and impact]
1082- **Consequence 2**: [Negative outcome] - [Likelihood and impact]
1083...
1084
1085### Competing Alternatives
1086- **Alternative 1**: [Better approach] - [Why it's better]
1087- **Alternative 2**: [Better approach] - [Why it's better]
1088...
1089
1090### Resource Concerns
1091- [Concern 1]: [Explanation]
1092- [Concern 2]: [Explanation]
1093...
1094
1095### Scalability Issues
1096- [Issue 1]: [Explanation]
1097- [Issue 2]: [Explanation]
1098...
1099
1100### User Experience Problems
1101- [Problem 1]: [Explanation]
1102- [Problem 2]: [Explanation]
1103...
1104
1105### Harsh Critic's Perspective
1106[What would a harsh but fair critic say? Be direct and unsparing.]
1107
1108### Summary
1109- **Total Flaws Identified**: [Number]
1110- **Severity**: [Critical/High/Medium/Low] - [Justification]
1111- **Most Critical Issues**: [List top 3-5 most critical problems]
1112
1113Don't hold back. The goal is to find problems before they cause real damage."#
1114                .to_string(),
1115            output_format: StepFmt::List,
1116            min_confidence: 0.6,
1117            depends_on: vec!["steelman".to_string()],
1118            branch: None,
1119        },
1120        ProtocolStep {
1121            id: "verdict".to_string(),
1122            action: StepAction::Decide {
1123                method: DecisionMethod::ProsCons,
1124            },
1125            prompt_template: r#"# BrutalHonesty: Final Verdict
1126
1127Provide a final verdict on whether this work is acceptable. Balance strengths against flaws.
1128
1129## Strengths
1130{{steelman}}
1131
1132## Flaws
1133{{attack}}
1134
1135## Instructions
1136Provide a final assessment:
1137
11381. **Overall Assessment**: Pass, Conditional Pass, or Fail?
11392. **Most Critical Issue**: What is the single most critical problem that must be fixed?
11403. **Confidence in Verdict**: How confident are you in this assessment? (0.0-1.0)
11414. **What Would Make This Excellent?**: What changes would elevate this from acceptable to excellent?
11425. **Recommendation**: Should this work proceed, be revised, or be rejected?
1143
1144Balance is key. Consider both strengths and flaws. A work with minor flaws but strong value might pass. A work with critical flaws but good intentions should fail.
1145
1146## Output Format
1147
1148### Overall Assessment
1149**Verdict**: [Pass/Conditional Pass/Fail]
1150
1151**Reasoning**: [Detailed explanation of why this verdict was reached, considering both strengths and flaws]
1152
1153### Most Critical Issue
1154**Issue**: [The single most critical problem]
1155
1156**Why Critical**: [Why this issue is the most important]
1157
1158**Impact**: [What happens if this isn't fixed?]
1159
1160### Confidence in Verdict
1161**[0.0-1.0]** - [Justification]
1162
1163### What Would Make This Excellent?
1164- [Improvement 1]: [How this would elevate the work]
1165- [Improvement 2]: [How this would elevate the work]
1166- [Improvement 3]: [How this would elevate the work]
1167...
1168
1169### Recommendation
1170**Action**: [Proceed/Revise and Resubmit/Reject]
1171
1172**Next Steps**: [What should happen next?]
1173
1174**Timeline**: [If revision is needed, what's a reasonable timeline?]
1175
1176### Balanced Summary
1177- **Strengths to Preserve**: [What should be kept even if revising?]
1178- **Flaws to Fix**: [What must be addressed?]
1179- **Nice-to-Haves**: [What would be good to improve but isn't critical?]
1180
1181Be fair but firm. The goal is improvement, not destruction. But don't sugarcoat critical problems."#
1182                .to_string(),
1183            output_format: StepFmt::Structured,
1184            min_confidence: 0.75,
1185            depends_on: vec!["steelman".to_string(), "attack".to_string()],
1186            branch: None,
1187        },
1188    ]
1189}
1190
1191fn build_output_spec(module_name: &str) -> OutputSpec {
1192    let format = format!("{}Result", module_name.replace(" ", ""));
1193    let fields = match module_name {
1194        "GigaThink" => vec![
1195            "dimensions".to_string(),
1196            "perspectives".to_string(),
1197            "themes".to_string(),
1198            "insights".to_string(),
1199            "confidence".to_string(),
1200        ],
1201        "LaserLogic" => vec![
1202            "conclusion".to_string(),
1203            "premises".to_string(),
1204            "validity".to_string(),
1205            "fallacies".to_string(),
1206            "confidence".to_string(),
1207        ],
1208        "BedRock" => vec![
1209            "axioms".to_string(),
1210            "decomposition".to_string(),
1211            "reconstruction".to_string(),
1212            "gaps".to_string(),
1213            "confidence".to_string(),
1214        ],
1215        "ProofGuard" => vec![
1216            "verdict".to_string(),
1217            "sources".to_string(),
1218            "evidence".to_string(),
1219            "discrepancies".to_string(),
1220            "confidence".to_string(),
1221        ],
1222        "BrutalHonesty" => vec![
1223            "strengths".to_string(),
1224            "flaws".to_string(),
1225            "verdict".to_string(),
1226            "critical_fix".to_string(),
1227            "confidence".to_string(),
1228        ],
1229        _ => vec!["confidence".to_string()],
1230    };
1231
1232    OutputSpec { format, fields }
1233}
1234
1235fn get_composable_modules(module_key: &str) -> Vec<String> {
1236    match module_key {
1237        "gigathink" => vec!["laserlogic".to_string(), "brutalhonesty".to_string()],
1238        "laserlogic" => vec!["gigathink".to_string(), "bedrock".to_string()],
1239        "bedrock" => vec!["laserlogic".to_string(), "proofguard".to_string()],
1240        "proofguard" => vec!["bedrock".to_string(), "brutalhonesty".to_string()],
1241        "brutalhonesty" => vec!["gigathink".to_string(), "proofguard".to_string()],
1242        _ => vec![],
1243    }
1244}
1245
1246fn estimate_tokens(cost_estimate: &str) -> u32 {
1247    match cost_estimate {
1248        "low" => 1000,
1249        "medium" => 2000,
1250        "medium-high" => 2500,
1251        "high" => 3000,
1252        _ => 2000,
1253    }
1254}
1255
1256fn estimate_latency(duration: &str) -> u32 {
1257    if let Some(range) = duration.strip_suffix('s') {
1258        if let Some((low, high)) = range.split_once('-') {
1259            if let (Ok(low_val), Ok(high_val)) = (low.parse::<u32>(), high.parse::<u32>()) {
1260                return ((low_val + high_val) / 2) * 1000;
1261            }
1262        }
1263    }
1264    5000
1265}