-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit_workflow_orchestrator.py
More file actions
executable file
Β·248 lines (208 loc) Β· 9.88 KB
/
git_workflow_orchestrator.py
File metadata and controls
executable file
Β·248 lines (208 loc) Β· 9.88 KB
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
#!/usr/bin/env python3
"""
Agent-Driven Git Development Workflow Orchestrator
Reads workflow configuration from CLAUDE.md and executes it.
"""
import subprocess
import sys
import time
import re
from dataclasses import dataclass, field
from typing import List, Dict, Tuple, Callable
try:
import yaml
except ImportError:
print("Error: PyYAML package not found.", file=sys.stderr)
print("Please install it by running: pip install PyYAML", file=sys.stderr)
sys.exit(1)
from enum import Enum
class WorkflowState(Enum):
INIT = "init"
GIT_STATUS_CHECK = "git_status_check"
TYPECHECK = "typecheck"
LINT = "lint"
TEST = "test"
FINAL_STATUS = "final_status"
COMMIT_MESSAGE = "commit_message"
COMPLETED = "completed"
FAILED = "failed"
@dataclass
class WorkflowContext:
current_state: WorkflowState
request_type: str
initial_git_status: str = ""
last_commit: str = ""
attempts: Dict[str, int] = field(default_factory=lambda: {"typecheck": 0, "lint": 0, "test": 0})
max_attempts: int = 3
errors: List[str] = field(default_factory=list)
class AgentWorkflowOrchestrator:
def __init__(self, request_type: str, config_file: str = "CLAUDE.md", max_fix_attempts: int = 3):
self.context = WorkflowContext(
current_state=WorkflowState.INIT,
request_type=request_type,
max_attempts=max_fix_attempts
)
self.workflow_steps = self._load_workflow_from_config(config_file)
def _load_workflow_from_config(self, config_file: str) -> List[Tuple[Callable, str]]:
print(f"π Loading workflows from {config_file}...")
try:
with open(config_file, 'r', encoding='utf-8') as f:
content = f.read()
match = re.search(r'<!--AGENT_WORKFLOWS_START-->(.*)<!--AGENT_WORKFLOWS_END-->', content, re.DOTALL)
if not match:
print(f"Error: Could not find workflow block in {config_file}", file=sys.stderr)
sys.exit(1)
yaml_content = re.sub(r'```yaml|```', '', match.group(1)).strip()
config = yaml.safe_load(yaml_content)
agent_config = next((agent for agent in config.get('agents', []) if agent['name'] == self.context.request_type or agent.get('alias') == self.context.request_type), None)
if not agent_config:
print(f"β οΈ Agent '{self.context.request_type}' not found. Defaulting to 'review'.")
self.context.request_type = 'review'
return self._load_workflow_from_config(config_file)
print(f"π€ Initializing '{agent_config['name']}' agent: {agent_config['description']}")
steps = []
for step in agent_config.get('steps', []):
func = getattr(self, step['function'], None)
if callable(func):
steps.append((func, step['name']))
else:
print(f"Error: Function '{step['function']}' not found.", file=sys.stderr)
sys.exit(1)
return steps
except FileNotFoundError:
print(f"Error: Config file '{config_file}' not found.", file=sys.stderr)
sys.exit(1)
except Exception as e:
print(f"Error parsing workflow config: {e}", file=sys.stderr)
sys.exit(1)
def run_command(self, cmd: List[str]) -> Tuple[int, str, str]:
try:
process = subprocess.run(cmd, capture_output=True, text=True, check=False, timeout=600)
return process.returncode, process.stdout, process.stderr
except FileNotFoundError:
return -2, "", f"Command not found: {cmd[0]}"
except Exception as e:
return -1, "", str(e)
def check_git_status(self) -> bool:
self.context.current_state = WorkflowState.GIT_STATUS_CHECK
print("\nπ Checking Git Status...")
code, status, err = self.run_command(["git", "status", "--short"])
if code != 0:
self.context.errors.append(f"Git status failed: {err}")
return False
self.context.initial_git_status = status
print(f"π Git Status:\n{status or 'Working tree clean'}")
return True
def run_typecheck_readonly(self) -> bool:
self.context.current_state = WorkflowState.TYPECHECK
print("\nπ Running Type Check (Read-only)...")
self.context.attempts['typecheck'] += 1
code, stdout, stderr = self.run_command(["pnpm", "run", "typecheck"])
if code == 0:
print("β
Type check passed.")
return True
print(f"β Type errors found:\n{(stdout + stderr)[:1000]}")
self.context.errors.append("Type checking failed.")
return True # In review mode, we report errors but don't fail the workflow
def run_typecheck_fix(self) -> bool:
# Most type checkers don't auto-fix, so this is an alias for the readonly check
return self.run_typecheck_readonly()
def run_lint_readonly(self) -> bool:
self.context.current_state = WorkflowState.LINT
print("\nπ§Ή Running Linter (Read-only)...")
self.context.attempts['lint'] += 1
# Create a non-fixing version of the command if one doesn't exist
lint_cmd = ["pnpm", "run", "lint", "--", "--no-fix"] if "lint:fix" in self.context.initial_git_status else ["pnpm", "run", "lint"]
code, stdout, stderr = self.run_command(lint_cmd)
if code == 0:
print("β
Linting passed.")
return True
print(f"β Lint issues found:\n{(stdout + stderr)[:1000]}")
self.context.errors.append("Linting issues found.")
return True
def run_lint_fix(self) -> bool:
self.context.current_state = WorkflowState.LINT
print("\n- π§Ή Running Linter (with Fixes)...")
self.context.attempts['lint'] += 1
code, stdout, stderr = self.run_command(["pnpm", "run", "lint"]) # Your script is already aliased to lint:fix
if code == 0 and not stderr:
print("β
Linting passed and/or auto-fixed.")
return True
if self.context.attempts['lint'] >= self.context.max_attempts:
self.context.errors.append("Linting failed after max attempts.")
return False
print("- β οΈ Lint issues remain, retrying...")
time.sleep(1)
return self.run_lint_fix()
def run_tests_readonly(self) -> bool:
return self._run_tests(retry=False)
def run_tests_with_retry(self) -> bool:
return self._run_tests(retry=True)
def _run_tests(self, retry: bool) -> bool:
self.context.current_state = WorkflowState.TEST
self.context.attempts['test'] += 1
print(f"\n- π§ͺ Running Tests (Attempt {self.context.attempts['test']})...")
code, stdout, stderr = self.run_command(["pnpm", "run", "test"])
if code == 0:
print("β
All tests passed.")
return True
print(f"β Tests failed:\n{(stdout + stderr)[:2000]}")
if retry and self.context.attempts['test'] < self.context.max_attempts:
print(f"\nπ Retrying tests...")
time.sleep(1)
return self._run_tests(retry=True)
self.context.errors.append("Tests failed.")
return not retry
def check_final_status(self) -> bool:
self.context.current_state = WorkflowState.FINAL_STATUS
print("\nπ Checking Final Git Status...")
code, status, _ = self.run_command(["git", "status", "--short"])
if not status:
print("β
No changes to commit after fixes.")
self.context.current_state = WorkflowState.COMPLETED
return "SKIP_WORKFLOW"
print(f"π Final Git Status:\n{status}")
return True
def suggest_commit_message(self) -> bool:
self.context.current_state = WorkflowState.COMMIT_MESSAGE
print("\n㪠Generating Commit Message...")
_, diff_summary, _ = self.run_command(["git", "diff", "--shortstat"])
prefix = "feat" if self.context.request_type == 'feat' else 'fix'
subject = f"{prefix}: apply automated fixes and pass checks"
body = f"{diff_summary.strip()}\n\nAutomated workflow run for '{self.context.request_type}' request."
print("\n" + "="*60 + "\nπ Suggested Commit Message:\n" + f"{subject}\n\n{body}" + "\n" + "="*60)
return True
def execute(self) -> bool:
if not self.workflow_steps:
print("No workflow steps loaded. Exiting.", file=sys.stderr)
return False
print("=" * 60)
start_time = time.time()
for step_func, step_name in self.workflow_steps:
print(f"\nβΆοΈ Executing Step: {step_name}")
result = step_func()
if result is False:
self.context.current_state = WorkflowState.FAILED
break
if result == "SKIP_WORKFLOW":
break
duration = time.time() - start_time
print("\n" + "=" * 60)
final_status = WorkflowState.COMPLETED if not self.context.errors else WorkflowState.FAILED
if final_status == WorkflowState.COMPLETED:
print(f"β
Workflow Completed Successfully in {duration:.2f}s")
else:
print(f"β Workflow Failed in {duration:.2f}s")
print(f"Errors: {', '.join(set(self.context.errors))}")
return final_status == WorkflowState.COMPLETED
def main():
if len(sys.argv) < 2:
print("Usage: ./git_workflow_orchestrator.py <request_type>", file=sys.stderr)
print("Available types defined in CLAUDE.md: feat, bug, fix, review", file=sys.stderr)
sys.exit(1)
orchestrator = AgentWorkflowOrchestrator(request_type=sys.argv[1].lower())
if not orchestrator.execute():
sys.exit(1)
sys.exit(0)
if __name__ == "__main__":
main()