|
| 1 | +""" |
| 2 | +Human-in-the-loop checkpoint implementations. |
| 3 | +
|
| 4 | +Provides structured HITL checkpoints between workflow stages. |
| 5 | +""" |
| 6 | + |
| 7 | +import logging |
| 8 | +from typing import Dict, Any |
| 9 | +from agents.agile_factory.state.agile_state import AgileFactoryState |
| 10 | + |
| 11 | +logger = logging.getLogger(__name__) |
| 12 | + |
| 13 | + |
| 14 | +def create_checkpoint_summary(state: AgileFactoryState, checkpoint_name: str) -> Dict[str, Any]: |
| 15 | + """ |
| 16 | + Create structured summary for human review at checkpoint. |
| 17 | + |
| 18 | + Args: |
| 19 | + state: Current workflow state |
| 20 | + checkpoint_name: Name of checkpoint |
| 21 | + |
| 22 | + Returns: |
| 23 | + Structured summary dictionary |
| 24 | + """ |
| 25 | + summary = { |
| 26 | + "checkpoint": checkpoint_name, |
| 27 | + "timestamp": None, # Will be set by caller if needed |
| 28 | + "status": state.get("status", "processing") |
| 29 | + } |
| 30 | + |
| 31 | + if checkpoint_name == "story_review": |
| 32 | + summary.update({ |
| 33 | + "what": "User Story Input", |
| 34 | + "content": state.get("user_story", ""), |
| 35 | + "project_type": state.get("project_type", "website") |
| 36 | + }) |
| 37 | + |
| 38 | + elif checkpoint_name == "requirements_review": |
| 39 | + requirements = state.get("requirements", {}) |
| 40 | + summary.update({ |
| 41 | + "what": "Requirements Analysis", |
| 42 | + "summary": requirements.get("summary", ""), |
| 43 | + "functional_count": len(requirements.get("functional_requirements", [])), |
| 44 | + "non_functional_count": len(requirements.get("non_functional_requirements", [])) |
| 45 | + }) |
| 46 | + |
| 47 | + elif checkpoint_name == "architecture_review": |
| 48 | + architecture = state.get("architecture", {}) |
| 49 | + summary.update({ |
| 50 | + "what": "Architecture Design", |
| 51 | + "system_overview": architecture.get("system_overview", "")[:500], |
| 52 | + "architecture_pattern": architecture.get("architecture_pattern", ""), |
| 53 | + "components_count": len(architecture.get("components", [])), |
| 54 | + "tech_stack": architecture.get("technology_stack", {}) |
| 55 | + }) |
| 56 | + |
| 57 | + elif checkpoint_name == "code_generation_review": |
| 58 | + code_files = state.get("code_files", {}) |
| 59 | + summary.update({ |
| 60 | + "what": "Code Generation", |
| 61 | + "files_generated": len(code_files), |
| 62 | + "file_list": list(code_files.keys())[:10], # First 10 files |
| 63 | + "total_size": sum(len(content) for content in code_files.values()) |
| 64 | + }) |
| 65 | + |
| 66 | + elif checkpoint_name == "final_review": |
| 67 | + summary.update({ |
| 68 | + "what": "Final Project Review", |
| 69 | + "requirements_complete": bool(state.get("requirements")), |
| 70 | + "architecture_complete": bool(state.get("architecture")), |
| 71 | + "code_complete": bool(state.get("code_files")), |
| 72 | + "tests_complete": bool(state.get("test_results")), |
| 73 | + "docs_complete": bool(state.get("documentation_files")) |
| 74 | + }) |
| 75 | + |
| 76 | + return summary |
| 77 | + |
| 78 | + |
| 79 | +def print_summary(summary: Dict[str, Any]) -> None: |
| 80 | + """ |
| 81 | + Print checkpoint summary to console. |
| 82 | + |
| 83 | + Args: |
| 84 | + summary: Summary dictionary |
| 85 | + """ |
| 86 | + print(f"\n{summary.get('what', 'Checkpoint')}:") |
| 87 | + print("-" * 60) |
| 88 | + |
| 89 | + for key, value in summary.items(): |
| 90 | + if key not in ["checkpoint", "what", "timestamp"]: |
| 91 | + if isinstance(value, dict): |
| 92 | + print(f"{key}:") |
| 93 | + for k, v in value.items(): |
| 94 | + print(f" {k}: {v}") |
| 95 | + elif isinstance(value, list): |
| 96 | + print(f"{key}: {len(value)} items") |
| 97 | + if value and len(value) <= 5: |
| 98 | + for item in value: |
| 99 | + print(f" - {item}") |
| 100 | + else: |
| 101 | + print(f"{key}: {value}") |
| 102 | + |
| 103 | + |
| 104 | +def hitl_checkpoint_node(state: AgileFactoryState, checkpoint_name: str) -> dict: |
| 105 | + """ |
| 106 | + Generic HITL checkpoint node. |
| 107 | + |
| 108 | + This node presents a checkpoint summary to the human reviewer and |
| 109 | + collects their decision (approve/reject/edit). |
| 110 | + |
| 111 | + Args: |
| 112 | + state: Current workflow state |
| 113 | + checkpoint_name: Name of checkpoint |
| 114 | + |
| 115 | + Returns: |
| 116 | + Updates dict with HITL feedback (LangGraph will merge with state) |
| 117 | + """ |
| 118 | + # Create structured summary |
| 119 | + summary = create_checkpoint_summary(state, checkpoint_name) |
| 120 | + |
| 121 | + # Present to human (console for MVP, Streamlit UI later) |
| 122 | + print("\n" + "="*60) |
| 123 | + print(f"HITL CHECKPOINT: {checkpoint_name.upper().replace('_', ' ')}") |
| 124 | + print("="*60) |
| 125 | + print_summary(summary) |
| 126 | + print("\nOptions:") |
| 127 | + print(" [a]pprove - Continue to next step") |
| 128 | + print(" [r]eject - Restart from beginning") |
| 129 | + print(" [e]dit - Provide feedback for revision") |
| 130 | + print(" [s]kip - Skip this checkpoint (not recommended)") |
| 131 | + |
| 132 | + # Get human input |
| 133 | + # For LangGraph Studio, this will be handled via interrupt |
| 134 | + # For now, we'll set a default and let Studio handle the interrupt |
| 135 | + feedback = "approve" # Default for automated testing |
| 136 | + |
| 137 | + # In LangGraph Studio, this will be an interrupt point |
| 138 | + # The human will provide feedback through Studio UI |
| 139 | + |
| 140 | + # Build updates dict (correct LangGraph pattern - return only updates) |
| 141 | + # Merge with existing hitl_approvals and hitl_feedback from state |
| 142 | + existing_approvals = state.get("hitl_approvals", {}).copy() |
| 143 | + existing_feedback = state.get("hitl_feedback", {}).copy() |
| 144 | + |
| 145 | + existing_approvals[checkpoint_name] = feedback == "approve" |
| 146 | + existing_feedback[checkpoint_name] = feedback |
| 147 | + |
| 148 | + updates = { |
| 149 | + "current_checkpoint": checkpoint_name, |
| 150 | + "hitl_approvals": existing_approvals, |
| 151 | + "hitl_feedback": existing_feedback |
| 152 | + } |
| 153 | + |
| 154 | + if feedback == "approve": |
| 155 | + updates["status"] = "approved" |
| 156 | + elif feedback == "reject": |
| 157 | + updates["status"] = "rejected" |
| 158 | + elif feedback == "edit": |
| 159 | + updates["status"] = "needs_revision" |
| 160 | + |
| 161 | + logger.info("HITL checkpoint %s: %s", checkpoint_name, feedback) |
| 162 | + |
| 163 | + return updates |
| 164 | + |
0 commit comments