codetether-agent 4.0.0

A2A-native AI coding agent for the CodeTether ecosystem
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
//! PRD Tool - Generate and validate PRD JSON
//!
//! This tool helps structure complex tasks into a PRD (Product Requirements
//! Document) that can be executed by `ralph`.

use anyhow::{Context, Result};
use async_trait::async_trait;
use serde::Deserialize;
use serde_json::{Value, json};
use std::collections::HashSet;
use std::path::PathBuf;

use super::{Tool, ToolResult};
use crate::ralph::{Prd, QualityChecks, UserStory, VerificationStep};

/// Tool for generating PRDs from requirements
pub struct PrdTool;

impl Default for PrdTool {
    fn default() -> Self {
        Self::new()
    }
}

impl PrdTool {
    pub fn new() -> Self {
        Self
    }
}

#[derive(Deserialize)]
struct Params {
    action: String,

    #[serde(default)]
    task_description: Option<String>,

    #[serde(default)]
    project: Option<String>,

    #[serde(default)]
    feature: Option<String>,

    #[serde(default)]
    stories: Option<Vec<StoryInput>>,

    #[serde(default)]
    quality_checks: Option<QualityChecksInput>,

    #[serde(default)]
    prd_path: Option<String>,

    /// Raw PRD JSON string (optional alternative to `prd_path`).
    #[serde(default)]
    prd_json: Option<String>,
}

#[derive(Deserialize)]
struct StoryInput {
    id: String,
    title: String,
    description: String,

    #[serde(default)]
    acceptance_criteria: Vec<String>,

    /// Optional explicit story verification (BDD/TDD/E2E evidence).
    #[serde(default)]
    verification_steps: Vec<VerificationStep>,

    #[serde(default)]
    priority: Option<u8>,

    #[serde(default)]
    depends_on: Vec<String>,

    #[serde(default)]
    complexity: Option<u8>,
}

#[derive(Deserialize)]
struct QualityChecksInput {
    #[serde(default)]
    typecheck: Option<String>,
    #[serde(default)]
    test: Option<String>,
    #[serde(default)]
    lint: Option<String>,
    #[serde(default)]
    build: Option<String>,
}

#[async_trait]
impl Tool for PrdTool {
    fn id(&self) -> &str {
        "prd"
    }

    fn name(&self) -> &str {
        "PRD Generator"
    }

    fn description(&self) -> &str {
        r#"Generate and validate structured PRDs (Product Requirements Documents) for complex tasks.

Actions:
- analyze: Analyze a task description and return what questions need answering
- generate: Generate a PRD JSON from provided answers
- validate: Validate a PRD (schema, dependencies, etc.)
- save: Save a PRD JSON to a file for ralph to execute
"#
    }

    fn parameters(&self) -> Value {
        // Keep `verification_steps` permissive; the enum is validated by serde when PRD JSON is parsed.
        json!({
            "type": "object",
            "properties": {
                "action": {
                    "type": "string",
                    "enum": ["analyze", "generate", "validate", "save"],
                    "description": "Action to perform"
                },
                "task_description": {
                    "type": "string",
                    "description": "Description of the complex task (for analyze)"
                },
                "project": {
                    "type": "string",
                    "description": "Project name (for generate/save)"
                },
                "feature": {
                    "type": "string",
                    "description": "Feature name (for generate/save)"
                },
                "stories": {
                    "type": "array",
                    "description": "User stories (for generate/validate/save)",
                    "items": {
                        "type": "object",
                        "properties": {
                            "id": {"type": "string"},
                            "title": {"type": "string"},
                            "description": {"type": "string"},
                            "acceptance_criteria": {"type": "array", "items": {"type": "string"}},
                            "verification_steps": {"type": "array", "items": {"type": "object"}},
                            "priority": {"type": "integer"},
                            "depends_on": {"type": "array", "items": {"type": "string"}},
                            "complexity": {"type": "integer"}
                        },
                        "required": ["id", "title", "description"]
                    }
                },
                "quality_checks": {
                    "type": "object",
                    "properties": {
                        "typecheck": {"type": "string"},
                        "test": {"type": "string"},
                        "lint": {"type": "string"},
                        "build": {"type": "string"}
                    }
                },
                "prd_path": {
                    "type": "string",
                    "description": "Path to PRD file (validate from file, or save destination)"
                },
                "prd_json": {
                    "type": "string",
                    "description": "Raw PRD JSON string to validate (alternative to prd_path)"
                }
            },
            "required": ["action"]
        })
    }

    async fn execute(&self, params: Value) -> Result<ToolResult> {
        let p: Params = serde_json::from_value(params).context("Invalid params")?;

        match p.action.as_str() {
            "analyze" => {
                let task = p.task_description.unwrap_or_default();
                if task.is_empty() {
                    return Ok(ToolResult::structured_error(
                        "missing_field",
                        self.id(),
                        "task_description is required for analyze",
                        Some(vec!["task_description"]),
                        Some(json!({"action":"analyze","task_description":"..."})),
                    ));
                }

                const VERIFICATION_EXAMPLE_JSON: &str = r#"{
  \"verification_steps\": [
    {
      \"type\": \"shell\",
      \"name\": \"cypress_e2e\",
      \"command\": \"npx cypress run\",
      \"expect_files_glob\": [\"cypress/videos/**/*.mp4\"]
    },
    {
      \"type\": \"url\",
      \"name\": \"deployment_live\",
      \"url\": \"https://example.com/health\",
      \"expect_status\": 200,
      \"expect_body_contains\": [\"version:1.2.3\"],
      \"timeout_secs\": 30
    }
  ]
}"#;

                let mut questions = String::new();
                questions.push_str("# Task Analysis\n\n");
                questions.push_str("## Task Description\n");
                questions.push_str(&task);
                questions.push_str("\n\n");
                questions.push_str("## Questions to Answer\n\n");
                questions.push_str("1. **Project Name**\n");
                questions.push_str("2. **Feature Name**\n");
                questions.push_str(
                    "3. **User Stories**: break down the task into discrete, independently verifiable stories.\n",
                );
                questions.push_str("   For each story:\n");
                questions.push_str("   - ID (e.g., US-001)\n");
                questions.push_str("   - Title\n");
                questions.push_str("   - Description\n");
                questions.push_str("   - Acceptance Criteria\n");
                questions.push_str(
                    "   - Verification Steps (machine-verifiable evidence; BDD/TDD/E2E/artifacts/URLs)\n",
                );
                questions.push_str("   - Priority (1=highest)\n");
                questions.push_str("   - Dependencies\n");
                questions.push_str("   - Complexity (1-5)\n\n");
                questions.push_str("4. **Quality Checks** (repo-level gates; optional)\n\n");

                questions.push_str("### Example story verification steps\n\n");
                questions.push_str("```json\n");
                questions.push_str(VERIFICATION_EXAMPLE_JSON);
                questions.push_str("\n```\n");

                Ok(ToolResult::success(questions))
            }

            "generate" => {
                let project = p.project.unwrap_or_else(|| "Project".to_string());
                let feature = p.feature.unwrap_or_else(|| "Feature".to_string());

                let stories: Vec<UserStory> = p
                    .stories
                    .unwrap_or_default()
                    .into_iter()
                    .map(|s| UserStory {
                        id: s.id,
                        title: s.title,
                        description: s.description,
                        acceptance_criteria: s.acceptance_criteria,
                        verification_steps: s.verification_steps,
                        passes: false,
                        priority: s.priority.unwrap_or(1),
                        depends_on: s.depends_on,
                        complexity: s.complexity.unwrap_or(3),
                    })
                    .collect();

                if stories.is_empty() {
                    return Ok(ToolResult::structured_error(
                        "missing_field",
                        self.id(),
                        "At least one story is required",
                        Some(vec!["stories"]),
                        None,
                    ));
                }

                let quality_checks = match p.quality_checks {
                    Some(qc) => QualityChecks {
                        typecheck: qc.typecheck,
                        test: qc.test,
                        lint: qc.lint,
                        build: qc.build,
                    },
                    None => QualityChecks::default(),
                };

                let prd = Prd {
                    project: project.clone(),
                    feature: feature.clone(),
                    branch_name: format!("feature/{}", feature.to_lowercase().replace(' ', "-")),
                    version: "1.0".to_string(),
                    user_stories: stories,
                    technical_requirements: Vec::new(),
                    quality_checks,
                    created_at: chrono::Utc::now().to_rfc3339(),
                    updated_at: chrono::Utc::now().to_rfc3339(),
                };

                let json_str = serde_json::to_string_pretty(&prd)?;
                Ok(
                    ToolResult::success(format!("# Generated PRD\n\n```json\n{}\n```\n", json_str))
                        .with_metadata("prd", serde_json::to_value(&prd)?),
                )
            }

            "validate" => {
                let prd: Prd = if let Some(json_str) = p.prd_json {
                    serde_json::from_str(&json_str)
                        .context("Failed to parse prd_json - invalid JSON")?
                } else if let Some(path) = &p.prd_path {
                    let prd_path = PathBuf::from(path);
                    if !prd_path.exists() {
                        return Ok(ToolResult::error(format!("PRD file not found: {path}")));
                    }
                    let content = tokio::fs::read_to_string(&prd_path).await?;
                    serde_json::from_str(&content)
                        .context("Failed to parse PRD file - invalid JSON")?
                } else if let Some(stories) = p.stories {
                    let project = p.project.clone().unwrap_or_else(|| "Project".to_string());
                    let feature = p.feature.clone().unwrap_or_else(|| "Feature".to_string());

                    let stories: Vec<UserStory> = stories
                        .into_iter()
                        .map(|s| UserStory {
                            id: s.id,
                            title: s.title,
                            description: s.description,
                            acceptance_criteria: s.acceptance_criteria,
                            verification_steps: s.verification_steps,
                            passes: false,
                            priority: s.priority.unwrap_or(1),
                            depends_on: s.depends_on,
                            complexity: s.complexity.unwrap_or(3),
                        })
                        .collect();

                    Prd {
                        project,
                        feature,
                        branch_name: String::new(),
                        version: "1.0".to_string(),
                        user_stories: stories,
                        technical_requirements: Vec::new(),
                        quality_checks: QualityChecks::default(),
                        created_at: String::new(),
                        updated_at: String::new(),
                    }
                } else {
                    return Ok(ToolResult::structured_error(
                        "missing_field",
                        self.id(),
                        "validate requires one of: prd_json, prd_path, or stories",
                        Some(vec!["prd_json|prd_path|stories"]),
                        None,
                    ));
                };

                let validation = validate_prd(&prd);

                if validation.is_valid {
                    Ok(ToolResult::success(format!(
                        "# PRD Validation: PASSED\n\nProject: {}\nFeature: {}\nStories: {}\nExecution stages: {}\n\n{}",
                        prd.project,
                        prd.feature,
                        prd.user_stories.len(),
                        prd.stages().len(),
                        validation.summary()
                    )))
                } else {
                    Ok(ToolResult::error(format!(
                        "# PRD Validation: FAILED\n\n{}",
                        validation.summary()
                    )))
                }
            }

            "save" => {
                let project = p.project.unwrap_or_else(|| "Project".to_string());
                let feature = p.feature.unwrap_or_else(|| "Feature".to_string());
                let prd_path = PathBuf::from(p.prd_path.unwrap_or_else(|| "prd.json".to_string()));

                let stories: Vec<UserStory> = p
                    .stories
                    .unwrap_or_default()
                    .into_iter()
                    .map(|s| UserStory {
                        id: s.id,
                        title: s.title,
                        description: s.description,
                        acceptance_criteria: s.acceptance_criteria,
                        verification_steps: s.verification_steps,
                        passes: false,
                        priority: s.priority.unwrap_or(1),
                        depends_on: s.depends_on,
                        complexity: s.complexity.unwrap_or(3),
                    })
                    .collect();

                if stories.is_empty() {
                    return Ok(ToolResult::structured_error(
                        "missing_field",
                        self.id(),
                        "At least one story is required for save",
                        Some(vec!["stories"]),
                        None,
                    ));
                }

                let quality_checks = match p.quality_checks {
                    Some(qc) => QualityChecks {
                        typecheck: qc.typecheck,
                        test: qc.test,
                        lint: qc.lint,
                        build: qc.build,
                    },
                    None => QualityChecks::default(),
                };

                let prd = Prd {
                    project: project.clone(),
                    feature: feature.clone(),
                    branch_name: format!("feature/{}", feature.to_lowercase().replace(' ', "-")),
                    version: "1.0".to_string(),
                    user_stories: stories,
                    technical_requirements: Vec::new(),
                    quality_checks,
                    created_at: chrono::Utc::now().to_rfc3339(),
                    updated_at: chrono::Utc::now().to_rfc3339(),
                };

                prd.save(&prd_path).await.context("Failed to save PRD")?;
                Ok(ToolResult::success(format!(
                    "PRD saved to: {}",
                    prd_path.display()
                )))
            }

            _ => Ok(ToolResult::error(format!(
                "Unknown action: {}. Use analyze/generate/validate/save",
                p.action
            ))),
        }
    }
}

struct ValidationResult {
    is_valid: bool,
    errors: Vec<String>,
    warnings: Vec<String>,
}

impl ValidationResult {
    fn summary(&self) -> String {
        let mut lines = Vec::new();

        if !self.errors.is_empty() {
            lines.push("## Errors".to_string());
            for err in &self.errors {
                lines.push(format!("- ❌ {err}"));
            }
        }

        if !self.warnings.is_empty() {
            if !lines.is_empty() {
                lines.push(String::new());
            }
            lines.push("## Warnings".to_string());
            for warn in &self.warnings {
                lines.push(format!("- ⚠️ {warn}"));
            }
        }

        if self.is_valid && self.warnings.is_empty() {
            lines.push("✅ All checks passed".to_string());
        }

        lines.join("\n")
    }
}

fn validate_prd(prd: &Prd) -> ValidationResult {
    let mut errors = Vec::new();
    let mut warnings = Vec::new();

    if prd.project.is_empty() {
        errors.push("Missing required field: project".to_string());
    }
    if prd.feature.is_empty() {
        errors.push("Missing required field: feature".to_string());
    }
    if prd.user_stories.is_empty() {
        errors.push("PRD must have at least one user story".to_string());
    }

    let story_ids: HashSet<String> = prd.user_stories.iter().map(|s| s.id.clone()).collect();

    let mut seen_ids = HashSet::new();
    for story in &prd.user_stories {
        if seen_ids.contains(&story.id) {
            errors.push(format!("Duplicate story ID: {}", story.id));
        }
        seen_ids.insert(story.id.clone());

        if story.id.is_empty() {
            errors.push("Story has empty ID".to_string());
        }
        if story.title.is_empty() {
            errors.push(format!("Story {} has empty title", story.id));
        }
        if story.description.is_empty() {
            warnings.push(format!("Story {} has empty description", story.id));
        }
        if story.acceptance_criteria.is_empty() {
            warnings.push(format!("Story {} has no acceptance criteria", story.id));
        }
        if story.verification_steps.is_empty() {
            warnings.push(format!(
                "Story {} has no verification_steps; it may pass on quality checks alone",
                story.id
            ));
        }

        if story.priority == 0 {
            warnings.push(format!("Story {} has priority 0 (should be 1+)", story.id));
        }
        if story.complexity == 0 || story.complexity > 5 {
            warnings.push(format!(
                "Story {} has complexity {} (should be 1-5)",
                story.id, story.complexity
            ));
        }

        for dep in &story.depends_on {
            if !story_ids.contains(dep) {
                errors.push(format!(
                    "Story {} depends on non-existent story: {}",
                    story.id, dep
                ));
            }
            if dep == &story.id {
                errors.push(format!("Story {} depends on itself", story.id));
            }
        }
    }

    if let Some(cycle) = detect_cycle(prd) {
        errors.push(format!(
            "Circular dependency detected: {}",
            cycle.join("")
        ));
    }

    let qc = &prd.quality_checks;
    if qc.typecheck.is_none() && qc.test.is_none() && qc.lint.is_none() && qc.build.is_none() {
        warnings.push("No quality checks defined - ralph won't run repo-level gates".to_string());
    }

    ValidationResult {
        is_valid: errors.is_empty(),
        errors,
        warnings,
    }
}

fn detect_cycle(prd: &Prd) -> Option<Vec<String>> {
    let story_map: std::collections::HashMap<&str, &UserStory> = prd
        .user_stories
        .iter()
        .map(|s| (s.id.as_str(), s))
        .collect();

    fn dfs<'a>(
        id: &'a str,
        story_map: &std::collections::HashMap<&str, &'a UserStory>,
        visiting: &mut HashSet<&'a str>,
        visited: &mut HashSet<&'a str>,
        path: &mut Vec<&'a str>,
    ) -> Option<Vec<String>> {
        if visiting.contains(id) {
            let start_idx = path.iter().position(|&x| x == id).unwrap_or(0);
            let mut cycle: Vec<String> = path[start_idx..].iter().map(|s| s.to_string()).collect();
            cycle.push(id.to_string());
            return Some(cycle);
        }

        if visited.contains(id) {
            return None;
        }

        visiting.insert(id);
        path.push(id);

        if let Some(story) = story_map.get(id) {
            for dep in &story.depends_on {
                if let Some(cycle) = dfs(dep.as_str(), story_map, visiting, visited, path) {
                    return Some(cycle);
                }
            }
        }

        path.pop();
        visiting.remove(id);
        visited.insert(id);
        None
    }

    let mut visited = HashSet::new();
    let mut visiting = HashSet::new();
    let mut path = Vec::new();

    for story in &prd.user_stories {
        if let Some(cycle) = dfs(
            story.id.as_str(),
            &story_map,
            &mut visiting,
            &mut visited,
            &mut path,
        ) {
            return Some(cycle);
        }
    }

    None
}