ralph-core 2.9.3

Core orchestration loop, configuration, and state management for Ralph Orchestrator
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
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
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
//! Task definition types for benchmark harness.
//!
//! Defines the JSON schema for benchmark tasks, including setup, verification,
//! and metrics collection. Tasks run in isolated workspaces with their own
//! `.git` directories to avoid polluting the main repository.
//!
//! # Example
//!
//! ```
//! use ralph_core::task_definition::{TaskDefinition, TaskSuite, Verification};
//!
//! let task = TaskDefinition::builder("hello-world", "tasks/hello-world/PROMPT.md", "TASK_COMPLETE")
//!     .verification_command("python hello.py | grep -q 'Hello, World!'")
//!     .max_iterations(5)
//!     .expected_iterations(1)
//!     .complexity("simple")
//!     .build();
//!
//! assert_eq!(task.name, "hello-world");
//! assert!(task.verification.command.contains("Hello, World!"));
//! ```

use serde::{Deserialize, Serialize};
use std::path::Path;

/// A suite of benchmark tasks loaded from a JSON file.
///
/// The suite contains multiple tasks that can be run sequentially during
/// batch benchmarking.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TaskSuite {
    /// List of task definitions.
    pub tasks: Vec<TaskDefinition>,

    /// Optional suite-level metadata.
    #[serde(default)]
    pub metadata: SuiteMetadata,
}

impl TaskSuite {
    /// Loads a task suite from a JSON file.
    pub fn from_file(path: impl AsRef<Path>) -> Result<Self, TaskDefinitionError> {
        let path_ref = path.as_ref();
        let content = std::fs::read_to_string(path_ref)?;
        let suite: Self = serde_json::from_str(&content)?;
        suite.validate()?;
        Ok(suite)
    }

    /// Validates all tasks in the suite.
    pub fn validate(&self) -> Result<(), TaskDefinitionError> {
        if self.tasks.is_empty() {
            return Err(TaskDefinitionError::Validation(
                "Task suite must contain at least one task".to_string(),
            ));
        }

        for task in &self.tasks {
            task.validate()?;
        }

        // Check for duplicate names
        let mut names = std::collections::HashSet::new();
        for task in &self.tasks {
            if !names.insert(&task.name) {
                return Err(TaskDefinitionError::Validation(format!(
                    "Duplicate task name: '{}'",
                    task.name
                )));
            }
        }

        Ok(())
    }

    /// Returns tasks filtered by complexity level.
    pub fn filter_by_complexity(&self, complexity: &str) -> Vec<&TaskDefinition> {
        self.tasks
            .iter()
            .filter(|t| t.complexity == complexity)
            .collect()
    }

    /// Returns tasks filtered by tag.
    pub fn filter_by_tag(&self, tag: &str) -> Vec<&TaskDefinition> {
        self.tasks
            .iter()
            .filter(|t| t.tags.iter().any(|t| t == tag))
            .collect()
    }
}

/// Suite-level metadata.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct SuiteMetadata {
    /// Optional suite name.
    pub name: Option<String>,

    /// Optional description.
    pub description: Option<String>,

    /// Suite version.
    pub version: Option<String>,
}

/// A single benchmark task definition.
///
/// Tasks define what the agent should accomplish, how to verify success,
/// and optional setup requirements. Each task runs in an isolated workspace.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TaskDefinition {
    // ─────────────────────────────────────────────────────────────────────────
    // REQUIRED FIELDS
    // ─────────────────────────────────────────────────────────────────────────
    /// Unique task identifier (alphanumeric + hyphens).
    ///
    /// Used for recording filenames and result reporting.
    pub name: String,

    /// Path to the prompt markdown file.
    ///
    /// Relative to the task suite file or absolute path.
    pub prompt_file: String,

    /// String the agent outputs when task is complete.
    ///
    /// This is detected by the orchestration loop to terminate the task.
    pub completion_promise: String,

    /// Verification configuration for confirming task success.
    pub verification: Verification,

    // ─────────────────────────────────────────────────────────────────────────
    // OPTIONAL FIELDS
    // ─────────────────────────────────────────────────────────────────────────
    /// Human-readable description of the task.
    #[serde(default)]
    pub description: Option<String>,

    /// Task complexity level: "simple", "medium", or "complex".
    ///
    /// Used for filtering and baseline comparisons.
    #[serde(default = "default_complexity")]
    pub complexity: String,

    /// Maximum iterations before the task is considered failed.
    ///
    /// Safety limit to prevent runaway loops.
    #[serde(default = "default_max_iterations")]
    pub max_iterations: u32,

    /// Expected number of iterations for baseline comparison.
    ///
    /// Used to calculate `iteration_delta` in results.
    #[serde(default)]
    pub expected_iterations: Option<u32>,

    /// Timeout in seconds for the entire task.
    #[serde(default = "default_timeout_seconds")]
    pub timeout_seconds: u64,

    /// Setup configuration for the task workspace.
    #[serde(default)]
    pub setup: TaskSetup,

    /// Tags for filtering and categorization.
    #[serde(default)]
    pub tags: Vec<String>,
}

fn default_complexity() -> String {
    "medium".to_string()
}

fn default_max_iterations() -> u32 {
    100
}

fn default_timeout_seconds() -> u64 {
    300 // 5 minutes
}

impl TaskDefinition {
    /// Creates a builder for constructing task definitions.
    pub fn builder(
        name: impl Into<String>,
        prompt_file: impl Into<String>,
        completion_promise: impl Into<String>,
    ) -> TaskDefinitionBuilder {
        TaskDefinitionBuilder::new(name, prompt_file, completion_promise)
    }

    /// Validates the task definition.
    pub fn validate(&self) -> Result<(), TaskDefinitionError> {
        // Validate name format (alphanumeric + hyphens)
        if self.name.is_empty() {
            return Err(TaskDefinitionError::MissingField("name".to_string()));
        }

        if !self
            .name
            .chars()
            .all(|c| c.is_alphanumeric() || c == '-' || c == '_')
        {
            return Err(TaskDefinitionError::Validation(format!(
                "Task name '{}' contains invalid characters. Use alphanumeric, hyphens, or underscores only.",
                self.name
            )));
        }

        // Validate prompt_file is not empty
        if self.prompt_file.is_empty() {
            return Err(TaskDefinitionError::MissingField("prompt_file".to_string()));
        }

        // Validate completion_promise is not empty
        if self.completion_promise.is_empty() {
            return Err(TaskDefinitionError::MissingField(
                "completion_promise".to_string(),
            ));
        }

        // Validate verification command is not empty
        if self.verification.command.is_empty() {
            return Err(TaskDefinitionError::MissingField(
                "verification.command".to_string(),
            ));
        }

        // Validate complexity is valid
        if !["simple", "medium", "complex"].contains(&self.complexity.as_str()) {
            return Err(TaskDefinitionError::Validation(format!(
                "Invalid complexity '{}'. Must be one of: simple, medium, complex",
                self.complexity
            )));
        }

        Ok(())
    }

    /// Returns the iteration delta if expected_iterations is set.
    ///
    /// `delta = actual - expected` (positive means took more iterations)
    pub fn iteration_delta(&self, actual: u32) -> Option<i32> {
        self.expected_iterations
            .map(|expected| actual as i32 - expected as i32)
    }
}

/// Builder for constructing task definitions.
pub struct TaskDefinitionBuilder {
    name: String,
    prompt_file: String,
    completion_promise: String,
    verification: Verification,
    description: Option<String>,
    complexity: String,
    max_iterations: u32,
    expected_iterations: Option<u32>,
    timeout_seconds: u64,
    setup: TaskSetup,
    tags: Vec<String>,
}

impl TaskDefinitionBuilder {
    /// Creates a new builder with required fields.
    pub fn new(
        name: impl Into<String>,
        prompt_file: impl Into<String>,
        completion_promise: impl Into<String>,
    ) -> Self {
        Self {
            name: name.into(),
            prompt_file: prompt_file.into(),
            completion_promise: completion_promise.into(),
            verification: Verification::default(),
            description: None,
            complexity: default_complexity(),
            max_iterations: default_max_iterations(),
            expected_iterations: None,
            timeout_seconds: default_timeout_seconds(),
            setup: TaskSetup::default(),
            tags: Vec::new(),
        }
    }

    /// Sets the verification command.
    pub fn verification_command(mut self, command: impl Into<String>) -> Self {
        self.verification.command = command.into();
        self
    }

    /// Sets the verification success exit code.
    pub fn verification_exit_code(mut self, code: i32) -> Self {
        self.verification.success_exit_code = code;
        self
    }

    /// Sets the full verification configuration.
    pub fn verification(mut self, verification: Verification) -> Self {
        self.verification = verification;
        self
    }

    /// Sets the task description.
    pub fn description(mut self, description: impl Into<String>) -> Self {
        self.description = Some(description.into());
        self
    }

    /// Sets the complexity level.
    pub fn complexity(mut self, complexity: impl Into<String>) -> Self {
        self.complexity = complexity.into();
        self
    }

    /// Sets the maximum iterations.
    pub fn max_iterations(mut self, max: u32) -> Self {
        self.max_iterations = max;
        self
    }

    /// Sets the expected iterations for baseline comparison.
    pub fn expected_iterations(mut self, expected: u32) -> Self {
        self.expected_iterations = Some(expected);
        self
    }

    /// Sets the timeout in seconds.
    pub fn timeout_seconds(mut self, seconds: u64) -> Self {
        self.timeout_seconds = seconds;
        self
    }

    /// Sets the setup configuration.
    pub fn setup(mut self, setup: TaskSetup) -> Self {
        self.setup = setup;
        self
    }

    /// Sets the setup script.
    pub fn setup_script(mut self, script: impl Into<String>) -> Self {
        self.setup.script = Some(script.into());
        self
    }

    /// Sets the setup files.
    pub fn setup_files(mut self, files: Vec<String>) -> Self {
        self.setup.files = files;
        self
    }

    /// Adds tags.
    pub fn tags(mut self, tags: Vec<String>) -> Self {
        self.tags = tags;
        self
    }

    /// Adds a single tag.
    pub fn tag(mut self, tag: impl Into<String>) -> Self {
        self.tags.push(tag.into());
        self
    }

    /// Builds the task definition.
    pub fn build(self) -> TaskDefinition {
        TaskDefinition {
            name: self.name,
            prompt_file: self.prompt_file,
            completion_promise: self.completion_promise,
            verification: self.verification,
            description: self.description,
            complexity: self.complexity,
            max_iterations: self.max_iterations,
            expected_iterations: self.expected_iterations,
            timeout_seconds: self.timeout_seconds,
            setup: self.setup,
            tags: self.tags,
        }
    }
}

/// Verification configuration for a task.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct Verification {
    /// Bash command to verify task success.
    ///
    /// Runs in the task workspace after completion promise is detected.
    #[serde(default)]
    pub command: String,

    /// Exit code that indicates success (default: 0).
    #[serde(default)]
    pub success_exit_code: i32,
}

impl Verification {
    /// Creates a new verification with the given command.
    pub fn new(command: impl Into<String>) -> Self {
        Self {
            command: command.into(),
            success_exit_code: 0,
        }
    }

    /// Creates a verification that expects a non-zero exit code.
    pub fn expect_failure(command: impl Into<String>, exit_code: i32) -> Self {
        Self {
            command: command.into(),
            success_exit_code: exit_code,
        }
    }
}

/// Setup configuration for task workspace.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct TaskSetup {
    /// Script to run before the task starts.
    ///
    /// Executed in the task workspace directory.
    #[serde(default)]
    pub script: Option<String>,

    /// Files to copy to the task workspace.
    ///
    /// Paths relative to the task suite file.
    #[serde(default)]
    pub files: Vec<String>,
}

impl TaskSetup {
    /// Returns true if there is any setup to perform.
    pub fn has_setup(&self) -> bool {
        self.script.is_some() || !self.files.is_empty()
    }
}

/// Errors that can occur when working with task definitions.
#[derive(Debug, thiserror::Error)]
pub enum TaskDefinitionError {
    /// IO error reading task file.
    #[error("IO error: {0}")]
    Io(#[from] std::io::Error),

    /// JSON parse error.
    #[error("JSON parse error: {0}")]
    Json(#[from] serde_json::Error),

    /// Missing required field.
    #[error("Missing required field: {0}")]
    MissingField(String),

    /// Validation error.
    #[error("Validation error: {0}")]
    Validation(String),
}

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

    #[test]
    fn test_task_definition_builder() {
        let task = TaskDefinition::builder("hello-world", "tasks/hello.md", "TASK_COMPLETE")
            .verification_command("python hello.py | grep -q 'Hello, World!'")
            .description("Create a hello world script")
            .complexity("simple")
            .max_iterations(5)
            .expected_iterations(1)
            .tag("python")
            .build();

        assert_eq!(task.name, "hello-world");
        assert_eq!(task.prompt_file, "tasks/hello.md");
        assert_eq!(task.completion_promise, "TASK_COMPLETE");
        assert!(task.verification.command.contains("Hello, World!"));
        assert_eq!(task.complexity, "simple");
        assert_eq!(task.max_iterations, 5);
        assert_eq!(task.expected_iterations, Some(1));
        assert!(task.tags.contains(&"python".to_string()));
    }

    #[test]
    fn test_task_definition_defaults() {
        let task = TaskDefinition::builder("test", "prompt.md", "DONE")
            .verification_command("echo ok")
            .build();

        assert_eq!(task.complexity, "medium");
        assert_eq!(task.max_iterations, 100);
        assert_eq!(task.timeout_seconds, 300);
        assert!(task.expected_iterations.is_none());
        assert!(task.tags.is_empty());
    }

    #[test]
    fn test_task_validation_valid() {
        let task = TaskDefinition::builder("valid-task", "prompt.md", "DONE")
            .verification_command("echo ok")
            .build();

        assert!(task.validate().is_ok());
    }

    #[test]
    fn test_task_validation_invalid_name() {
        let task = TaskDefinition::builder("invalid task name!", "prompt.md", "DONE")
            .verification_command("echo ok")
            .build();

        let err = task.validate().unwrap_err();
        assert!(matches!(err, TaskDefinitionError::Validation(_)));
    }

    #[test]
    fn test_task_validation_empty_prompt() {
        let task = TaskDefinition::builder("test", "", "DONE")
            .verification_command("echo ok")
            .build();

        let err = task.validate().unwrap_err();
        assert!(matches!(err, TaskDefinitionError::MissingField(f) if f == "prompt_file"));
    }

    #[test]
    fn test_task_validation_empty_verification() {
        let task = TaskDefinition::builder("test", "prompt.md", "DONE").build();

        let err = task.validate().unwrap_err();
        assert!(matches!(err, TaskDefinitionError::MissingField(f) if f == "verification.command"));
    }

    #[test]
    fn test_task_validation_invalid_complexity() {
        let task = TaskDefinition::builder("test", "prompt.md", "DONE")
            .verification_command("echo ok")
            .complexity("invalid")
            .build();

        let err = task.validate().unwrap_err();
        assert!(matches!(err, TaskDefinitionError::Validation(_)));
    }

    #[test]
    fn test_iteration_delta() {
        let task = TaskDefinition::builder("test", "prompt.md", "DONE")
            .verification_command("echo ok")
            .expected_iterations(5)
            .build();

        // Took fewer iterations than expected
        assert_eq!(task.iteration_delta(3), Some(-2));

        // Took more iterations than expected
        assert_eq!(task.iteration_delta(7), Some(2));

        // Took exactly expected
        assert_eq!(task.iteration_delta(5), Some(0));
    }

    #[test]
    fn test_iteration_delta_no_expected() {
        let task = TaskDefinition::builder("test", "prompt.md", "DONE")
            .verification_command("echo ok")
            .build();

        assert!(task.iteration_delta(5).is_none());
    }

    #[test]
    fn test_task_suite_parse() {
        let json = r#"{
            "tasks": [
                {
                    "name": "hello-world",
                    "prompt_file": "tasks/hello/PROMPT.md",
                    "completion_promise": "TASK_COMPLETE",
                    "verification": {
                        "command": "python hello.py | grep -q 'Hello, World!'"
                    },
                    "complexity": "simple",
                    "max_iterations": 5,
                    "expected_iterations": 1
                },
                {
                    "name": "fizzbuzz-tdd",
                    "description": "Implement FizzBuzz with TDD",
                    "prompt_file": "tasks/fizzbuzz/PROMPT.md",
                    "completion_promise": "TESTS_PASSING",
                    "verification": {
                        "command": "pytest test_fizzbuzz.py -v"
                    },
                    "complexity": "medium",
                    "max_iterations": 15,
                    "expected_iterations": 5,
                    "setup": {
                        "files": ["test_fizzbuzz.py"]
                    },
                    "tags": ["python", "tdd"]
                }
            ],
            "metadata": {
                "name": "Ralph Benchmark Suite",
                "version": "1.0.0"
            }
        }"#;

        let suite: TaskSuite = serde_json::from_str(json).unwrap();
        assert_eq!(suite.tasks.len(), 2);

        let hello = &suite.tasks[0];
        assert_eq!(hello.name, "hello-world");
        assert_eq!(hello.complexity, "simple");
        assert_eq!(hello.max_iterations, 5);
        assert_eq!(hello.expected_iterations, Some(1));

        let fizzbuzz = &suite.tasks[1];
        assert_eq!(fizzbuzz.name, "fizzbuzz-tdd");
        assert!(fizzbuzz.description.is_some());
        assert_eq!(fizzbuzz.setup.files.len(), 1);
        assert!(fizzbuzz.tags.contains(&"tdd".to_string()));

        assert_eq!(
            suite.metadata.name,
            Some("Ralph Benchmark Suite".to_string())
        );
    }

    #[test]
    fn test_task_suite_validation_empty() {
        let suite = TaskSuite {
            tasks: vec![],
            metadata: SuiteMetadata::default(),
        };

        let err = suite.validate().unwrap_err();
        assert!(matches!(err, TaskDefinitionError::Validation(_)));
    }

    #[test]
    fn test_task_suite_validation_duplicates() {
        let task = TaskDefinition::builder("duplicate", "prompt.md", "DONE")
            .verification_command("echo ok")
            .build();

        let suite = TaskSuite {
            tasks: vec![task.clone(), task],
            metadata: SuiteMetadata::default(),
        };

        let err = suite.validate().unwrap_err();
        assert!(err.to_string().contains("Duplicate task name"));
    }

    #[test]
    fn test_filter_by_complexity() {
        let json = r#"{
            "tasks": [
                {"name": "t1", "prompt_file": "p.md", "completion_promise": "DONE", "verification": {"command": "echo ok"}, "complexity": "simple"},
                {"name": "t2", "prompt_file": "p.md", "completion_promise": "DONE", "verification": {"command": "echo ok"}, "complexity": "medium"},
                {"name": "t3", "prompt_file": "p.md", "completion_promise": "DONE", "verification": {"command": "echo ok"}, "complexity": "simple"}
            ]
        }"#;

        let suite: TaskSuite = serde_json::from_str(json).unwrap();
        let simple = suite.filter_by_complexity("simple");
        assert_eq!(simple.len(), 2);
        assert!(simple.iter().all(|t| t.complexity == "simple"));
    }

    #[test]
    fn test_filter_by_tag() {
        let json = r#"{
            "tasks": [
                {"name": "t1", "prompt_file": "p.md", "completion_promise": "DONE", "verification": {"command": "echo ok"}, "tags": ["python", "testing"]},
                {"name": "t2", "prompt_file": "p.md", "completion_promise": "DONE", "verification": {"command": "echo ok"}, "tags": ["rust"]},
                {"name": "t3", "prompt_file": "p.md", "completion_promise": "DONE", "verification": {"command": "echo ok"}, "tags": ["python"]}
            ]
        }"#;

        let suite: TaskSuite = serde_json::from_str(json).unwrap();
        let python = suite.filter_by_tag("python");
        assert_eq!(python.len(), 2);
    }

    #[test]
    fn test_setup_has_setup() {
        let empty = TaskSetup::default();
        assert!(!empty.has_setup());

        let with_script = TaskSetup {
            script: Some("setup.sh".to_string()),
            files: vec![],
        };
        assert!(with_script.has_setup());

        let with_files = TaskSetup {
            script: None,
            files: vec!["file.py".to_string()],
        };
        assert!(with_files.has_setup());
    }

    #[test]
    fn test_verification_new() {
        let v = Verification::new("pytest tests/");
        assert_eq!(v.command, "pytest tests/");
        assert_eq!(v.success_exit_code, 0);
    }

    #[test]
    fn test_verification_expect_failure() {
        let v = Verification::expect_failure("false", 1);
        assert_eq!(v.command, "false");
        assert_eq!(v.success_exit_code, 1);
    }
}