codetether_rlm/oracle/
mod.rs1mod ast_validation;
29mod batch;
30mod batch_write;
31mod consensus;
32mod consensus_helpers;
33mod grep_oracle;
34mod grep_validation;
35mod query_type;
36mod record;
37mod schema;
38#[path = "storage/mod.rs"]
39mod storage;
40mod templates;
41mod trace_types;
42mod tree_sitter_oracle;
43mod types;
44#[path = "validator/mod.rs"]
45mod validator;
46
47pub use grep_oracle::{GrepOracle, GrepVerification};
48pub use query_type::QueryType;
49pub use record::OracleTraceRecord;
50pub use schema::{AstPayload, AstResult, FinalPayload, GrepMatch, GrepPayload, SemanticPayload};
51pub use storage::{
52 OracleTracePersistResult, OracleTraceStorage, OracleTraceSyncStats, default_spool_dir,
53};
54pub use templates::{GeneratedQuery, QueryTemplate, TemplateKind};
55pub use trace_types::{OracleResult, ValidatedTrace};
56pub use tree_sitter_oracle::{TreeSitterOracle, TreeSitterVerification};
57pub use types::{TraceStep, VerificationMethod};
58pub use validator::{BatchValidationStats, SplitWriteStats, TraceValidator};
59
60#[derive(Debug, Clone, PartialEq)]
62pub enum FinalAnswerFormat {
63 LineNumberedMatches { matches: Vec<(usize, String)> },
65 CountResult { count: usize },
67 StructuredData { data: serde_json::Value },
69 FreeFormText { text: String },
71}
72
73impl FinalAnswerFormat {
74 pub fn parse(answer: &str) -> Self {
76 let lines: Vec<&str> = answer.lines().collect();
78 let mut numbered_matches = Vec::new();
79 let mut all_valid = true;
80
81 for line in &lines {
82 let trimmed = line.trim();
84 if let Some(colon_pos) = trimmed.find(':') {
85 let num_part = trimmed[..colon_pos].trim().trim_start_matches('L').trim();
86 if let Ok(line_num) = num_part.parse::<usize>() {
87 let text_part = trimmed[colon_pos + 1..].trim().to_string();
88 numbered_matches.push((line_num, text_part));
89 } else {
90 all_valid = false;
91 break;
92 }
93 } else if !trimmed.is_empty() {
94 all_valid = false;
96 break;
97 }
98 }
99
100 if all_valid && !numbered_matches.is_empty() {
101 return Self::LineNumberedMatches {
102 matches: numbered_matches,
103 };
104 }
105
106 let lower = answer.to_lowercase();
108 if lower.contains("found") || lower.contains("count:") || lower.contains("occurrences") {
109 if let Some(count) = extract_count_from_text(answer) {
111 return Self::CountResult { count };
112 }
113 }
114
115 if (answer.trim().starts_with('{') || answer.trim().starts_with('['))
117 && let Ok(data) = serde_json::from_str::<serde_json::Value>(answer)
118 {
119 return Self::StructuredData { data };
120 }
121
122 Self::FreeFormText {
124 text: answer.to_string(),
125 }
126 }
127}
128
129fn extract_count_from_text(text: &str) -> Option<usize> {
131 let re = regex::Regex::new(r"(?i)(?:found|count:?\s*)\s*(\d+)|(\d+)\s+(?:functions?|matches?|occurrences?|items?|results?)").ok()?;
133
134 for cap in re.captures_iter(text) {
135 if let Some(m) = cap.get(1)
137 && let Ok(n) = m.as_str().parse()
138 {
139 return Some(n);
140 }
141 if let Some(m) = cap.get(2)
143 && let Ok(n) = m.as_str().parse()
144 {
145 return Some(n);
146 }
147 }
148
149 None
150}
151
152#[cfg(test)]
153mod tests {
154 use super::*;
155
156 #[test]
157 fn parse_line_numbered_matches() {
158 let answer = "42:async fn foo()\n100:pub struct Bar\n";
159 let format = FinalAnswerFormat::parse(answer);
160 match format {
161 FinalAnswerFormat::LineNumberedMatches { matches } => {
162 assert_eq!(matches.len(), 2);
163 assert_eq!(matches[0], (42, "async fn foo()".to_string()));
164 assert_eq!(matches[1], (100, "pub struct Bar".to_string()));
165 }
166 _ => panic!("Expected LineNumberedMatches"),
167 }
168 }
169
170 #[test]
171 fn parse_count_result() {
172 let answer = "Found 15 async functions";
173 let format = FinalAnswerFormat::parse(answer);
174 match format {
175 FinalAnswerFormat::CountResult { count } => assert_eq!(count, 15),
176 _ => panic!("Expected CountResult"),
177 }
178 }
179
180 #[test]
181 fn parse_structured_data() {
182 let answer = r#"{"name": "foo", "args": ["x", "y"]}"#;
183 let format = FinalAnswerFormat::parse(answer);
184 match format {
185 FinalAnswerFormat::StructuredData { data } => {
186 assert_eq!(data["name"], "foo");
187 }
188 _ => panic!("Expected StructuredData"),
189 }
190 }
191
192 #[test]
193 fn parse_free_form_text() {
194 let answer = "This function handles error cases by using the ? operator";
195 let format = FinalAnswerFormat::parse(answer);
196 match format {
197 FinalAnswerFormat::FreeFormText { text } => {
198 assert!(text.contains("error cases"));
199 }
200 _ => panic!("Expected FreeFormText"),
201 }
202 }
203}