ralphloop 0.1.0

A CLI tool for creating and running Ralphloops with LLM integration
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
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
# Ralphloop - Technical Specification

## 1. Overview

Ralphloop is a command-line tool for creating and executing Ralphiloops - structured, iterative workflows powered by Large Language Models (LLMs).

## ๐Ÿ“‹ Table of Contents

1. [System Requirements]#system-requirements
2. [Functional Requirements]#functional-requirements
3. [Non-Functional Requirements]#non-functional-requirements
4. [Data Specifications]#data-specifications
5. [API Specifications]#api-specifications
6. [Error Specifications]#error-specifications
7. [Security Specifications]#security-specifications
8. [Performance Specifications]#performance-specifications
9. [Testing Specifications]#testing-specifications

## ๐Ÿ”ง System Requirements

### Supported Platforms

| Platform | Architecture          | Minimum Version | Test Status |
| -------- | --------------------- | --------------- | ----------- |
| Linux    | x86_64, ARM64         | Kernel 3.10+    | โœ… Tested   |
| macOS    | x86_64, Apple Silicon | macOS 10.15+    | โœ… Tested   |
| Windows  | x86_64                | Windows 10+     | โœ… Tested   |

### Dependencies

**Runtime Dependencies:**

- None (single binary distribution)

**Build Dependencies:**

- Rust 1.70+ (edition 2021)
- Cargo package manager
- Optional: OpenSSL for local builds (for TLS support)

**System Resources:**

- **Minimum RAM**: 64MB
- **Recommended RAM**: 256MB
- **Disk Space**: 10MB for binary, 1-100MB for loops
- **Network**: Internet connection for cloud LLM providers

## ๐ŸŽฏ Functional Requirements

### FR-001: Loop Management

**Requirement:** The system shall allow users to create, modify, and execute Ralphiloops.

**Acceptance Criteria:**

- User can create a loop from scratch
- User can create a loop from a template
- User can modify existing loops
- User can execute loops with specified iteration count
- System validates loop syntax before execution

**Priority:** Critical

### FR-002: Multi-Provider Support

**Requirement:** The system shall support multiple LLM providers with unified interface.

**Provider Matrix:**
| Provider | Models Supported | Authentication | Features |
|----------|----------------|----------------|----------|
| OpenAI | GPT-3.5, GPT-4, GPT-4-Turbo | API Key | Streaming, Functions |
| Anthropic | Claude-3 family | API Key | Streaming, Tool Use |
| Google | Gemini Pro | API Key | Streaming |
| Local | Ollama, Llama.cpp | None | Custom endpoints |

**Priority:** Critical

### FR-003: Variable System

**Requirement:** The system shall support dynamic variable substitution and context management.

**Variable Types:**

- **Input Variables**: User-defined constants
- **Context Variables**: Outputs from previous steps
- **System Variables**: Iteration count, timestamp
- **Environment Variables**: OS environment variables

**Priority:** High

### FR-004: Template System

**Requirement:** The system shall provide a template system for common workflows.

**Built-in Templates:**

- Code Review
- Content Creation
- Research
- Creative Writing
- Problem Solving

**Priority:** High

### FR-005: Configuration Management

**Requirement:** The system shall provide secure configuration management with multiple profiles.

**Configuration Features:**

- Multiple named profiles
- Environment variable overrides
- Secure API key storage
- Provider-specific settings

**Priority:** Critical

### FR-006: Dependency Resolution

**Requirement:** The system shall resolve step dependencies and execute in correct order.

**Dependency Rules:**

- Circular dependency detection
- Parallel execution where possible
- Dependency graph validation
- Failure propagation handling

**Priority:** High

## ๐Ÿ›ก๏ธ Non-Functional Requirements

### NFR-001: Performance

**Requirements:**

- **Startup Time**: < 500ms cold start
- **Loop Execution**: < 2s overhead per step (excluding LLM response time)
- **Memory Usage**: < 256MB for typical loops
- **Concurrent Execution**: Support 10+ simultaneous loops

### NFR-002: Reliability

**Requirements:**

- **Uptime**: 99.9% for local operations
- **Error Recovery**: Graceful handling of network failures
- **Data Integrity**: No data loss during execution
- **Crash Recovery**: Ability to resume interrupted loops

### NFR-003: Security

**Requirements:**

- **API Key Protection**: Encryption at rest
- **Data Privacy**: No sensitive data in logs
- **Input Validation**: Protection against injection attacks
- **Secure Defaults**: Safe default configurations

### NFR-004: Usability

**Requirements:**

- **Learning Curve**: < 30 minutes for basic usage
- **Error Messages**: Clear, actionable error descriptions
- **Documentation**: Complete coverage of all features
- **CLI Consistency**: Follow standard CLI conventions

### NFR-005: Maintainability

**Requirements:**

- **Code Coverage**: > 90% test coverage
- **Documentation**: All public APIs documented
- **Modularity**: Clear separation of concerns
- **Dependencies**: Minimal external dependencies

## ๐Ÿ“Š Data Specifications

### Loop Definition Schema

```json
{
  "$schema": "https://ralph-cli.org/schema/v1.json",
  "type": "object",
  "properties": {
    "name": {
      "type": "string",
      "pattern": "^[a-zA-Z][a-zA-Z0-9_-]*$",
      "maxLength": 100
    },
    "description": {
      "type": "string",
      "maxLength": 500
    },
    "version": {
      "type": "string",
      "pattern": "^\\d+\\.\\d+\\.\\d+$"
    },
    "steps": {
      "type": "array",
      "items": {
        "$ref": "#/definitions/Step"
      },
      "minItems": 1
    },
    "variables": {
      "type": "object",
      "patternProperties": {
        "^[a-zA-Z][a-zA-Z0-9_]*$": {
          "type": "string"
        }
      }
    },
    "context": {
      "type": "object",
      "additionalProperties": true
    },
    "metadata": {
      "type": "object",
      "properties": {
        "author": { "type": "string" },
        "created": { "type": "string", "format": "date-time" },
        "modified": { "type": "string", "format": "date-time" },
        "tags": {
          "type": "array",
          "items": { "type": "string" }
        }
      }
    }
  },
  "required": ["name", "description", "steps"],
  "additionalProperties": false,
  "definitions": {
    "Step": {
      "type": "object",
      "properties": {
        "name": {
          "type": "string",
          "pattern": "^[a-zA-Z][a-zA-Z0-9_-]*$"
        },
        "prompt_template": {
          "type": "string",
          "minLength": 1
        },
        "output_variable": {
          "type": "string",
          "pattern": "^[a-zA-Z][a-zA-Z0-9_]*$"
        },
        "depends_on": {
          "type": "array",
          "items": { "type": "string" },
          "uniqueItems": true
        },
        "retry_count": {
          "type": "integer",
          "minimum": 0,
          "maximum": 5,
          "default": 0
        },
        "timeout": {
          "type": "integer",
          "minimum": 1,
          "maximum": 600,
          "default": 120
        }
      },
      "required": ["name", "prompt_template"],
      "additionalProperties": false
    }
  }
}
```

### Configuration Schema

```json
{
  "$schema": "https://ralph-cli.org/config/v1.json",
  "type": "object",
  "properties": {
    "version": { "type": "string" },
    "default_profile": { "type": "string" },
    "profiles": {
      "type": "object",
      "patternProperties": {
        "^[a-zA-Z][a-zA-Z0-9_-]*$": {
          "$ref": "#/definitions/Profile"
        }
      }
    }
  },
  "definitions": {
    "Profile": {
      "type": "object",
      "properties": {
        "llm_provider": {
          "type": "string",
          "enum": ["openai", "anthropic", "gemini", "local"]
        },
        "model": { "type": "string" },
        "api_key": { "type": "string" },
        "local_endpoint": { "type": "string", "format": "uri" },
        "max_tokens": {
          "type": "integer",
          "minimum": 1,
          "maximum": 100000
        },
        "temperature": {
          "type": "number",
          "minimum": 0.0,
          "maximum": 2.0
        },
        "timeout": {
          "type": "integer",
          "minimum": 1,
          "maximum": 600
        },
        "retry_count": {
          "type": "integer",
          "minimum": 0,
          "maximum": 5
        }
      },
      "required": ["llm_provider"]
    }
  }
}
```

## ๐Ÿ”Œ API Specifications

### CLI Command API

#### Create Command

```bash
ralph create <name> [options]

Options:
  -t, --template <template>    Use a predefined template
  -d, --description <desc>     Loop description
  -o, --output <path>         Output file path
  -f, --force                 Overwrite existing file
```

**Return Codes:**

- `0`: Success
- `1`: General error
- `2`: Invalid arguments
- `3`: Template not found
- `4`: File already exists

#### Run Command

```bash
ralph run <path> [options]

Options:
  -i, --iterations <count>     Number of iterations (default: 1)
  -p, --profile <name>         Configuration profile
  -v, --variables <key=value>  Override variables (can be repeated)
  -d, --dry-run              Show prompts without execution
  -q, --quiet                 Minimal output
```

#### Configure Command

```bash
ralph configure [options]

Options:
  -p, --profile <name>        Profile name (default: default)
  --provider <provider>        LLM provider
  --model <model>             Model name
  --api-key <key>             API key
  --local-endpoint <url>      Local endpoint URL
  --list-profiles             List all profiles
```

### LLM Provider Interface

```rust
#[async_trait]
pub trait LLMProvider: Send + Sync {
    /// Complete a prompt and return response
    async fn complete(&self, prompt: &str) -> Result<LLMResponse>;

    /// Stream completion (optional)
    async fn complete_stream(&self, prompt: &str) -> Result<StreamResponse>;

    /// Get provider information
    fn provider_info(&self) -> ProviderInfo;

    /// Validate configuration
    fn validate_config(&self) -> Result<()>;
}

pub struct LLMResponse {
    pub content: String,
    pub model: String,
    pub usage: Usage,
    pub metadata: HashMap<String, Value>,
}

pub struct Usage {
    pub prompt_tokens: u32,
    pub completion_tokens: u32,
    pub total_tokens: u32,
    pub cost: Option<f64>,
}
```

## โŒ Error Specifications

### Error Hierarchy

```
Error
โ”œโ”€โ”€ ConfigError
โ”‚   โ”œโ”€โ”€ MissingConfig
โ”‚   โ”œโ”€โ”€ InvalidConfig
โ”‚   โ””โ”€โ”€ ProfileNotFound
โ”œโ”€โ”€ LoopError
โ”‚   โ”œโ”€โ”€ InvalidSyntax
โ”‚   โ”œโ”€โ”€ CircularDependency
โ”‚   โ”œโ”€โ”€ MissingVariable
โ”‚   โ””โ”€โ”€ StepExecutionFailed
โ”œโ”€โ”€ ProviderError
โ”‚   โ”œโ”€โ”€ AuthenticationFailed
โ”‚   โ”œโ”€โ”€ RateLimited
โ”‚   โ”œโ”€โ”€ ModelNotFound
โ”‚   โ””โ”€โ”€ NetworkError
โ”œโ”€โ”€ TemplateError
โ”‚   โ”œโ”€โ”€ TemplateNotFound
โ”‚   โ”œโ”€โ”€ InvalidTemplate
โ”‚   โ””โ”€โ”€ TemplateSyntaxError
โ””โ”€โ”€ IOError
    โ”œโ”€โ”€ FileNotFound
    โ”œโ”€โ”€ PermissionDenied
    โ””โ”€โ”€ DiskFull
```

### Error Format

```json
{
  "error": {
    "code": "CIRCULAR_DEPENDENCY",
    "message": "Circular dependency detected between steps: analyze -> refine -> analyze",
    "details": {
      "cycle": ["analyze", "refine", "analyze"],
      "loop_file": "my-loop.json"
    },
    "suggestions": [
      "Remove the circular reference",
      "Split the loop into separate loops"
    ],
    "timestamp": "2024-03-01T12:00:00Z",
    "request_id": "req_123456789"
  }
}
```

### Error Recovery Strategies

| Error Type          | Recovery Strategy              | User Action Required |
| ------------------- | ------------------------------ | -------------------- |
| Network Timeout     | Retry with exponential backoff | None                 |
| Rate Limit          | Wait and retry                 | None                 |
| Invalid API Key     | Prompt for new key             | Yes                  |
| Loop Syntax Error   | Show error location            | Yes                  |
| Circular Dependency | Report cycle                   | Yes                  |

## ๐Ÿ” Security Specifications

### Threat Model

#### Potential Threats

1. **API Key Exposure**
   - **Risk**: Unauthorized access to LLM services
   - **Mitigation**: Encryption at rest, no logging

2. **Prompt Injection**
   - **Risk**: Malicious input affecting execution
   - **Mitigation**: Input validation, sandboxing

3. **Data Leakage**
   - **Risk**: Sensitive data in logs or cache
   - **Mitigation**: Data filtering, secure cleanup

4. **Code Execution**
   - **Risk**: Arbitrary code from untrusted sources
   - **Mitigation**: No code execution, prompt-only

#### Security Controls

**Authentication:**

- API keys stored encrypted with system keyring
- Environment variable support for temporary keys
- Key rotation support

**Authorization:**

- Scope-limited API tokens where supported
- Profile-based access control
- Audit logging of sensitive operations

**Data Protection:**

- Zero-knowledge architecture
- Memory sanitization on exit
- No persistent storage of sensitive data

**Network Security:**

- TLS 1.3 for all HTTPS connections
- Certificate validation
- Request timeout enforcement

### Compliance Considerations

**Data Privacy:**

- GDPR compliance for EU users
- CCPA compliance for California users
- Data localization options

**Security Standards:**

- OWASP guidelines for CLI applications
- Secure coding practices
- Regular security audits

## โšก Performance Specifications

### Performance Benchmarks

**Startup Performance:**

- Cold start: < 500ms
- Warm start: < 100ms
- Config loading: < 50ms

**Execution Performance:**

- Step overhead: < 100ms
- Variable resolution: < 10ms
- Template rendering: < 5ms

**Memory Usage:**

- Base memory: 64MB
- Per loop: +1-10MB
- Per step: +100KB-1MB

### Resource Limits

**Loop Limits:**

- Maximum steps per loop: 50
- Maximum loop size: 10MB
- Maximum variable size: 1MB

**API Limits:**

- Concurrent requests: 10 per provider
- Request timeout: 120s
- Retry attempts: 3

### Optimization Strategies

**Caching:**

- Template compilation cache
- API response cache (same prompts)
- Configuration cache

**Lazy Loading:**

- Templates loaded on demand
- Providers initialized when used
- Configuration validated when accessed

**Parallel Execution:**

- Independent steps executed concurrently
- Async I/O for all network operations
- Worker pool for CPU-bound tasks

## ๐Ÿงช Testing Specifications

### Test Coverage Requirements

**Unit Tests:**

- All public functions: 100% coverage
- Error handling paths: 95% coverage
- Edge cases: 90% coverage

**Integration Tests:**

- CLI commands: 100% coverage
- Provider interfaces: 100% coverage
- File operations: 100% coverage

**End-to-End Tests:**

- Critical workflows: 100% coverage
- Error scenarios: 90% coverage
- Performance tests: 80% coverage

### Test Categories

**Functional Tests:**

- Loop creation and execution
- Template system
- Configuration management
- Provider interactions

**Performance Tests:**

- Load testing
- Memory usage profiling
- Concurrent execution
- Resource cleanup

**Security Tests:**

- API key protection
- Input validation
- Data privacy
- Network security

**Compatibility Tests:**

- Cross-platform compatibility
- Version compatibility
- Provider API compatibility
- Configuration migration

### Test Data

**Mock Data:**

- Sample loop definitions
- Template examples
- Configuration scenarios
- Error responses

**Test Harness:**

- Mock LLM providers
- File system fixtures
- Network simulation
- Time manipulation

### Continuous Testing

**Pre-commit Hooks:**

- Format checking (`cargo fmt`)
- Linting (`cargo clippy`)
- Unit tests (`cargo test`)

**CI Pipeline:**

- All test categories
- Multiple platforms
- Multiple Rust versions
- Security scanning

**Release Testing:**

- Integration tests
- Performance benchmarks
- Security audit
- Documentation validation

---

## ๐Ÿ“ Validation Criteria

### Acceptance Testing

**Critical Path Tests:**

1. Install and configure the CLI
2. Create a loop from template
3. Execute the loop successfully
4. Verify output correctness
5. Handle errors gracefully

**Performance Tests:**

1. Startup time under 500ms
2. Memory usage under 256MB
3. Support 10+ concurrent loops
4. Handle large loops (>10MB)

**Security Tests:**

1. API key encryption verification
2. No sensitive data in logs
3. Input validation effectiveness
4. Network security compliance

### Release Criteria

**Must Have:**

- All tests passing
- Security audit passed
- Performance benchmarks met
- Documentation complete

**Should Have:**

- Integration tests passing
- Cross-platform compatibility
- User acceptance testing
- Performance regression testing

**Nice to Have:**

- Load testing results
- Community feedback
- Third-party security review
- Performance profiling