ralph.rb 1.2.4355354345 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/gem-push.yml +2 -2
  3. data/Gemfile +1 -1
  4. data/Gemfile.lock +53 -0
  5. data/lib/ralph/cli.rb +67 -186
  6. data/lib/ralph/display.rb +105 -0
  7. data/lib/ralph/events.rb +117 -0
  8. data/lib/ralph/loop.rb +113 -170
  9. data/lib/ralph/metrics.rb +88 -0
  10. data/lib/ralph/opencode.rb +66 -0
  11. data/lib/ralph/version.rb +1 -1
  12. data/lib/ralph.rb +0 -3
  13. data/plans/00-complete-implementation.md +120 -0
  14. data/plans/01-cli-implementation.md +53 -0
  15. data/plans/02-loop-implementation.md +78 -0
  16. data/plans/03-agents-implementation.md +76 -0
  17. data/plans/04-metrics-implementation.md +98 -0
  18. data/plans/README.md +63 -0
  19. data/specs/README.md +4 -15
  20. data/specs/__templates__/API_TEMPLATE.md +0 -0
  21. data/specs/__templates__/AUTOMATION_ACTION_TEMPLATE.md +0 -0
  22. data/specs/__templates__/AUTOMATION_TRIGGER_TEMPLATE.md +0 -0
  23. data/specs/__templates__/CONTROLLER_TEMPLATE.md +32 -0
  24. data/specs/__templates__/INTEGRATION_TEMPLATE.md +0 -0
  25. data/specs/__templates__/MODEL_TEMPLATE.md +0 -0
  26. data/specs/agents.md +426 -120
  27. data/specs/cli.md +11 -218
  28. data/specs/lib/todo_item.rb +144 -0
  29. data/specs/log +15 -0
  30. data/specs/loop.md +42 -0
  31. data/specs/metrics.md +51 -0
  32. metadata +23 -39
  33. data/lib/ralph/agents/base.rb +0 -132
  34. data/lib/ralph/agents/claude_code.rb +0 -24
  35. data/lib/ralph/agents/codex.rb +0 -25
  36. data/lib/ralph/agents/open_code.rb +0 -30
  37. data/lib/ralph/agents.rb +0 -24
  38. data/lib/ralph/config.rb +0 -40
  39. data/lib/ralph/git/file_snapshot.rb +0 -60
  40. data/lib/ralph/helpers.rb +0 -76
  41. data/lib/ralph/iteration.rb +0 -220
  42. data/lib/ralph/output/active_loop_error.rb +0 -13
  43. data/lib/ralph/output/banner.rb +0 -29
  44. data/lib/ralph/output/completion_deferred.rb +0 -12
  45. data/lib/ralph/output/completion_detected.rb +0 -17
  46. data/lib/ralph/output/config_summary.rb +0 -31
  47. data/lib/ralph/output/context_consumed.rb +0 -11
  48. data/lib/ralph/output/iteration.rb +0 -45
  49. data/lib/ralph/output/max_iterations_reached.rb +0 -16
  50. data/lib/ralph/output/no_plugin_warning.rb +0 -14
  51. data/lib/ralph/output/nonzero_exit_warning.rb +0 -11
  52. data/lib/ralph/output/plugin_error.rb +0 -12
  53. data/lib/ralph/output/status.rb +0 -176
  54. data/lib/ralph/output/struggle_warning.rb +0 -18
  55. data/lib/ralph/output/task_completion.rb +0 -12
  56. data/lib/ralph/output/tasks_file_created.rb +0 -11
  57. data/lib/ralph/prompt_template.rb +0 -183
  58. data/lib/ralph/storage/context.rb +0 -58
  59. data/lib/ralph/storage/history.rb +0 -117
  60. data/lib/ralph/storage/state.rb +0 -178
  61. data/lib/ralph/storage/tasks.rb +0 -244
  62. data/lib/ralph/threads/heartbeat.rb +0 -44
  63. data/lib/ralph/threads/stream_reader.rb +0 -50
  64. data/original/bin/ralph.js +0 -13
  65. data/original/ralph.ts +0 -1706
  66. data/specs/iteration.md +0 -173
  67. data/specs/output.md +0 -104
  68. data/specs/storage/local-data-structure.md +0 -246
  69. data/specs/tasks.md +0 -295
@@ -0,0 +1,78 @@
1
+ # Ralph.rb Core Loop Implementation Plan
2
+
3
+ This plan outlines the phased implementation of the core iteration loop based on the Loop specification in `specs/loop.md`.
4
+
5
+ ## Phase 1: Basic Loop Architecture
6
+ - [ ] Create `Loop` class with basic initialization
7
+ - [ ] Implement main infinite loop structure with proper termination conditions
8
+ - [ ] Create `Iteration` class for individual execution cycles
9
+ - [ ] Set up iteration cancellation mechanism (context/time guards)
10
+ - [ ] Implement basic loop state management (iterations counter, timer)
11
+
12
+ ## Phase 2: Iteration Management
13
+ - [ ] Implement iteration creation and execution workflow
14
+ - [ ] Add iteration cancellation based on context length monitoring
15
+ - [ ] Implement iteration cancellation based on duration limits
16
+ - [ ] Create iteration state tracking (running, completed, cancelled)
17
+ - [ ] Add iteration result collection and preservation
18
+
19
+ ## Phase 3: Context and Guard Implementation
20
+ - [ ] Integrate with Metrics component for real-time context monitoring
21
+ - [ ] Implement context size threshold checking and iteration restart
22
+ - [ ] Add iteration duration monitoring and cancellation
23
+ - [ ] Create context preservation between iterations (task list continuity)
24
+ - [ ] Implement task list passing and reminder system
25
+
26
+ ## Phase 4: Termination Conditions
27
+ - [ ] Implement completion string detection from agent output
28
+ - [ ] Add maximum iteration count enforcement
29
+ - [ ] Implement overall loop duration limits
30
+ - [ ] Create graceful termination and cleanup procedures
31
+ - [ ] Add termination reason reporting and statistics
32
+
33
+ ## Phase 5: Agent Integration
34
+ - [ ] Create interface to Opencode agent execution
35
+ - [ ] Implement prompt construction with task list and instructions
36
+ - [ ] Add agent output processing and completion string detection
37
+ - [ ] Integrate with Agents component for configuration and execution
38
+ - [ ] Handle agent errors and communication failures
39
+
40
+ ## Phase 6: Display and Monitoring
41
+ - [ ] Implement real-time iteration counter display
42
+ - [ ] Add current iteration status indicator
43
+ - [ ] Create duration display (current and total)
44
+ - [ ] Implement token consumption display (per iteration and total)
45
+ - [ ] Show agent output in real-time
46
+ - [ ] Display input prompt for reference
47
+
48
+ ## Phase 7: Prompt Engineering
49
+ - [ ] Create base prompt template explaining completion requirements
50
+ - [ ] Add explicit instruction to avoid user interaction
51
+ - [ ] Implement task list integration into prompts
52
+ - [ ] Add context preservation instructions
53
+ - [ ] Create prompt variation for different iteration states
54
+
55
+ ## Phase 8: Error Handling and Recovery
56
+ - [ ] Implement iteration failure recovery mechanisms
57
+ - [ ] Add context corruption detection and handling
58
+ - [ ] Create agent communication error recovery
59
+ - [ ] Implement graceful degradation on partial failures
60
+ - [ ] Add comprehensive logging for debugging
61
+
62
+ ## Verification Criteria
63
+ - [ ] Loop runs continuously until completion conditions met
64
+ - [ ] Iterations cancel properly when context limits exceeded
65
+ - [ ] Task continuity maintained across iteration restarts
66
+ - [ ] Completion string detection works reliably
67
+ - [ ] All termination conditions function correctly
68
+ - [ ] Real-time monitoring displays accurate information
69
+ - [ ] Agent integration works seamlessly
70
+ - [ ] Error recovery prevents data loss
71
+ - [ ] No Ruby style violations (run `bin/rubocop`)
72
+ - [ ] All tests pass (run `bin/test`)
73
+
74
+ ## Dependencies
75
+ - Requires Metrics component for context monitoring
76
+ - Requires Agents component for agent execution
77
+ - Requires prompt templates and task list system
78
+ - Depends on JSON stream parsing from Metrics
@@ -0,0 +1,76 @@
1
+ # Ralph.rb Agents Integration Implementation Plan
2
+
3
+ This plan outlines the phased implementation of agent integration with opencode based on the Agents specification in `specs/agents.md`.
4
+
5
+ ## Phase 1: Opencode Command Wrapper
6
+ - [ ] Create `Opencode` class for CLI interaction
7
+ - [ ] Implement basic command construction and execution
8
+ - [ ] Add subprocess management with proper error handling
9
+ - [ ] Implement JSON stream format specification (`--format json`)
10
+ - [ ] Create configuration management for opencode options
11
+
12
+ ## Phase 2: Configuration and Options
13
+ - [ ] Implement `--model` option handling and validation
14
+ - [ ] Add `--agent` option support for agent selection
15
+ - [ ] Create prompt passing mechanism (`--prompt` or direct argument)
16
+ - [ ] Implement JSON stream format enforcement
17
+ - [ ] Add opencode command path resolution and validation
18
+
19
+ ## Phase 3: Process Management
20
+ - [ ] Implement subprocess spawning with stdin/stdout handling
21
+ - [ ] Create process monitoring and timeout management
22
+ - [ ] Add signal handling for graceful termination
23
+ - [ ] Implement process cleanup and resource management
24
+ - [ ] Create error handling for opencode execution failures
25
+
26
+ ## Phase 4: JSON Stream Processing
27
+ - [ ] Create JSON stream reader for opencode output
28
+ - [ ] Implement line-by-line JSON parsing with error recovery
29
+ - [ ] Add event filtering and routing to Metrics component
30
+ - [ ] Create buffer management for high-volume output
31
+ - [ ] Implement stream error detection and handling
32
+
33
+ ## Phase 5: Event Integration
34
+ - [ ] Create event forwarding mechanism to Metrics component
35
+ - [ ] Implement event type mapping and transformation
36
+ - [ ] Add event timestamp and session ID handling
37
+ - [ ] Create event aggregation for step_finish events
38
+ - [ ] Implement real-time event processing capabilities
39
+
40
+ ## Phase 6: Agent Lifecycle Management
41
+ - [ ] Implement agent startup and initialization procedures
42
+ - [ ] Add agent state tracking (running, completed, failed)
43
+ - [ ] Create agent termination and cleanup processes
44
+ - [ ] Implement agent restart capabilities for failed iterations
45
+ - [ ] Add agent configuration validation
46
+
47
+ ## Phase 7: Communication Layer
48
+ - [ ] Create bidirectional communication interface
49
+ - [ ] Implement prompt delivery to agent process
50
+ - [ ] Add response collection and buffering
51
+ - [ ] Create interrupt mechanism for iteration cancellation
52
+ - [ ] Implement status reporting and health checks
53
+
54
+ ## Phase 8: Error Handling and Diagnostics
55
+ - [ ] Implement opencode command not found handling
56
+ - [ ] Add authentication and configuration error detection
57
+ - [ ] Create network connectivity error handling
58
+ - [ ] Implement JSON parsing error recovery
59
+ - [ ] Add comprehensive logging and debugging capabilities
60
+
61
+ ## Verification Criteria
62
+ - [ ] Opencode CLI commands execute correctly with all options
63
+ - [ ] JSON stream output is parsed reliably
64
+ - [ ] All required events are captured and forwarded
65
+ - [ ] Agent process management is robust
66
+ - [ ] Error handling covers all failure modes
67
+ - [ ] Communication with Metrics component works
68
+ - [ ] Agent lifecycle is managed properly
69
+ - [ ] No Ruby style violations (run `bin/rubocop`)
70
+ - [ ] All tests pass (run `bin/test`)
71
+
72
+ ## Dependencies
73
+ - Requires Metrics component for event processing
74
+ - Requires Loop component for agent lifecycle management
75
+ - Requires opencode CLI to be installed and available
76
+ - Depends on proper JSON stream format from opencode
@@ -0,0 +1,98 @@
1
+ # Ralph.rb Metrics Calculation Implementation Plan
2
+
3
+ This plan outlines the phased implementation of context and token usage metrics based on the Metrics specification in `specs/metrics.md`.
4
+
5
+ ## Phase 1: Event Parsing Infrastructure
6
+ - [ ] Create `Events` module with event class definitions
7
+ - [ ] Implement JSON stream parsing for opencode events
8
+ - [ ] Create `StepFinishEvent` class for token tracking
9
+ - [ ] Add `StepStartEvent` class for iteration tracking
10
+ - [ ] Implement `ToolUseEvent` class for tool execution tracking
11
+
12
+ ## Phase 2: Token Calculation Logic
13
+ - [ ] Implement context calculation formula: `input + cache.read + cache.write`
14
+ - [ ] Create token aggregation methods for each step
15
+ - [ ] Add cumulative token tracking across iterations
16
+ - [ ] Implement per-iteration token difference calculation
17
+ - [ ] Create token rate calculation (tokens per second)
18
+
19
+ ## Phase 3: Event Stream Processing
20
+ - [ ] Create `MetricsCollector` class for real-time processing
21
+ - [ ] Implement line-by-line JSON stream consumption
22
+ - [ ] Add event type detection and routing
23
+ - [ ] Create event state tracking (current step, session ID)
24
+ - [ ] Implement buffer management for partial JSON lines
25
+
26
+ ## Phase 4: Context Monitoring
27
+ - [ ] Implement real-time context size tracking
28
+ - [ ] Create context threshold alerting system
29
+ - [ ] Add context growth trend analysis
30
+ - [ ] Implement context usage percentage calculation
31
+ - [ ] Create context projection for upcoming steps
32
+
33
+ ## Phase 5: Metrics Storage and Retrieval
34
+ - [ ] Create metrics data structures for current and historical data
35
+ - [ ] Implement iteration-level metrics storage
36
+ - [ ] Add session-level metrics aggregation
37
+ - [ ] Create metrics history tracking
38
+ - [ ] Implement metrics export capabilities
39
+
40
+ ## Phase 6: Real-time Monitoring Interface
41
+ - [ ] Create `current_context()` method for immediate access
42
+ - [ ] Implement `tokens_consumed()` aggregation method
43
+ - [ ] Add `iteration_metrics()` for per-iteration data
44
+ - [ ] Create `session_metrics()` for overall statistics
45
+ - [ ] Implement metrics update notifications
46
+
47
+ ## Phase 7: Integration Points
48
+ - [ ] Create interface for Loop component context monitoring
49
+ - [ ] Implement threshold-based callback system
50
+ - [ ] Add metrics reporting for display components
51
+ - [ ] Create metrics persistence between iterations
52
+ - [ ] Implement metrics reset and cleanup procedures
53
+
54
+ ## Phase 8: Advanced Analytics
55
+ - [ ] Implement token efficiency analysis
56
+ - [ ] Add cache hit rate calculation
57
+ - [ ] Create tool usage cost analysis
58
+ - [ ] Implement performance trend analysis
59
+ - [ ] Add predictive context growth modeling
60
+
61
+ ## Phase 9: Error Handling and Validation
62
+ - [ ] Implement JSON parsing error recovery
63
+ - [ ] Add malformed event handling
64
+ - [ ] Create missing field validation
65
+ - [ ] Implement event sequence validation
66
+ - [ ] Add metrics corruption detection
67
+
68
+ ## Verification Criteria
69
+ - [ ] JSON stream parsing handles all event types correctly
70
+ - [ ] Context calculation matches specification formula exactly
71
+ - [ ] Real-time monitoring provides accurate current state
72
+ - [ ] Token tracking works across multiple iterations
73
+ - [ ] Integration with Loop component works seamlessly
74
+ - [ ] Error handling is robust and comprehensive
75
+ - [ ] Performance is suitable for real-time monitoring
76
+ - [ ] No Ruby style violations (run `bin/rubocop`)
77
+ - [ ] All tests pass (run `bin/test`)
78
+
79
+ ## Dependencies
80
+ - Requires Agents component for JSON stream input
81
+ - Requires Loop component for integration callbacks
82
+ - Depends on opencode JSON stream format consistency
83
+ - Needs proper time tracking for rate calculations
84
+
85
+ ## Example Event Classes to Implement
86
+ ```ruby
87
+ # Based on spec example - create classes for these events:
88
+ - step_start
89
+ - text
90
+ - tool_use
91
+ - step_finish (critical for token tracking)
92
+ ```
93
+
94
+ ## Critical Implementation Details
95
+ - Context formula: `input + cache.read + cache.write`
96
+ - Cache pattern: `cache.read` ≈ previous step's `cache.read + cache.write`
97
+ - Must track `step_finish` events specifically for token data
98
+ - Need real-time access for loop cancellation decisions
data/plans/README.md ADDED
@@ -0,0 +1,63 @@
1
+ # Ralph.rb Implementation Plans
2
+
3
+ This directory contains comprehensive phased implementation plans for building Ralph.rb, a Ruby CLI that runs iterative AI development loops.
4
+
5
+ ## Plans Overview
6
+
7
+ ### [00-complete-implementation.md](./00-complete-implementation.md)
8
+ **Master Plan** - Coordinates all component implementations into a cohesive system with 8 phases from foundation to optimization.
9
+
10
+ ### [01-cli-implementation.md](./01-cli-implementation.md)
11
+ **CLI Component** - Implements the command-line interface with Unix-style pipe support and all specified options.
12
+
13
+ ### [02-loop-implementation.md](./02-loop-implementation.md)
14
+ **Core Loop Component** - Implements the main iteration management with context guards and termination conditions.
15
+
16
+ ### [03-agents-implementation.md](./03-agents-implementation.md)
17
+ **Agent Integration** - Implements opencode CLI integration with JSON stream processing and process management.
18
+
19
+ ### [04-metrics-implementation.md](./04-metrics-implementation.md)
20
+ **Metrics Component** - Implements real-time token usage and context calculation from JSON streams.
21
+
22
+ ## Implementation Strategy
23
+
24
+ 1. **Foundation First** - Start with the complete implementation plan for project setup
25
+ 2. **Parallel Development** - CLI, Metrics, and Agents foundations can be developed simultaneously
26
+ 3. **Integration Focus** - Loop implementation depends on the other three components
27
+ 4. **Quality Gates** - Each phase includes verification criteria and testing requirements
28
+
29
+ ## Key Dependencies
30
+
31
+ ```
32
+ CLI → Loop → Agents → Metrics → CLI (for display)
33
+ ```
34
+
35
+ - CLI requires Loop for execution
36
+ - Loop requires Agents for iteration execution
37
+ - Loop requires Metrics for context monitoring
38
+ - Metrics requires Agents for JSON stream input
39
+ - CLI requires Metrics for progress display
40
+
41
+ ## Verification Requirements
42
+
43
+ Every plan includes:
44
+ - ✅ Phase-specific deliverables
45
+ - ✅ Integration points with other components
46
+ - ✅ Testing and quality assurance criteria
47
+ - ✅ Style compliance requirements (RuboCop)
48
+ - ✅ Final verification checklists
49
+
50
+ ## Ruby Style Requirements
51
+
52
+ All implementations must follow the guidelines in `AGENTS.md`:
53
+ - No early returns or guard clauses
54
+ - Use `.then` and `.tap` for data flow
55
+ - No abbreviated variable names
56
+ - Full descriptive naming throughout
57
+
58
+ ## Testing Commands
59
+
60
+ - **Run tests**: `bin/test`
61
+ - **Check style**: `bin/rubocop`
62
+
63
+ Both must pass before any component is considered complete.
data/specs/README.md CHANGED
@@ -29,18 +29,7 @@ Design documentation for Ralph.rb, a Ruby CLI that runs iterative AI development
29
29
 
30
30
  | Spec | Code | Purpose |
31
31
  |------|------|---------|
32
- | [agents.md](./agents.md) | [lib/ralph/agents/](../lib/ralph/agents/) | Agent abstraction: base class, subclasses, CLI resolution, subprocess argument building |
33
- | [cli.md](./cli.md) | [lib/ralph/cli.rb](../lib/ralph/cli.rb) | CLI options, subcommands, prompt resolution, error handling |
34
- ## Data Storage
35
-
36
- | Spec | Code | Purpose |
37
- |------|------|---------|
38
- | [storage/local-data-structure.md](./storage/local-data-structure.md) | [lib/ralph/storage/](../lib/ralph/storage/) | Ralph state persistence: .ralph/ directory, storage module architecture, data lifecycle |
39
- | [tasks.md](./tasks.md) | [lib/ralph/storage/tasks.rb](../lib/ralph/storage/tasks.rb) | Task management: file format, data models, storage, lifecycle in the loop, prompt integration |
40
-
41
- ## Output
42
-
43
- | Spec | Code | Purpose |
44
- |------|------|---------|
45
- | [output.md](./output.md) | [lib/ralph/output/](../lib/ralph/output/) | Terminal output structure: callable object pattern, channels, formatting conventions |
46
-
32
+ | [cli.md](./cli.md) | [exe/ralph](../exe/ralph) | Command-line interface specification for interacting with ralph |
33
+ | [loop.md](./loop.md) | | Core loop architecture and iteration management |
34
+ | [agents.md](./agents.md) | — | Integration with opencode agents and JSON streaming |
35
+ | [metrics.md](./metrics.md) | — | Context and token usage calculation from JSON streams |
File without changes
File without changes
@@ -0,0 +1,32 @@
1
+ # Spec Template Example
2
+ ...
3
+
4
+ ## Overview
5
+ ...
6
+
7
+ ### Purpose
8
+ ...
9
+
10
+ ### Purpose
11
+ ...
12
+
13
+ ### Non-Goals
14
+ ...
15
+
16
+ ### Goals
17
+ ...
18
+
19
+ ## Audit Events
20
+ ...
21
+
22
+ ## Permissions
23
+ ...
24
+
25
+ ## Security Considerations
26
+ ...
27
+
28
+ ## Future Considerations
29
+ ...
30
+
31
+ ## Future Enhancements
32
+ ...
File without changes
File without changes