forked from AutoForgeAI/autoforge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautonomous_agent_demo.py
More file actions
294 lines (243 loc) · 9.82 KB
/
autonomous_agent_demo.py
File metadata and controls
294 lines (243 loc) · 9.82 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
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
#!/usr/bin/env python3
"""
Autonomous Coding Agent Demo
============================
A minimal harness demonstrating long-running autonomous coding with Claude.
This script implements a unified orchestrator pattern that handles:
- Initialization (creating features from app_spec)
- Coding agents (implementing features)
- Testing agents (regression testing)
Example Usage:
# Using absolute path directly
python autonomous_agent_demo.py --project-dir C:/Projects/my-app
# Using registered project name (looked up from registry)
python autonomous_agent_demo.py --project-dir my-app
# Limit iterations for testing (when running as subprocess)
python autonomous_agent_demo.py --project-dir my-app --max-iterations 5
# YOLO mode: rapid prototyping without testing agents
python autonomous_agent_demo.py --project-dir my-app --yolo
# Parallel execution with 3 concurrent coding agents
python autonomous_agent_demo.py --project-dir my-app --concurrency 3
# Single agent mode (orchestrator with concurrency=1, the default)
python autonomous_agent_demo.py --project-dir my-app
# Run as specific agent type (used by orchestrator to spawn subprocesses)
python autonomous_agent_demo.py --project-dir my-app --agent-type initializer
python autonomous_agent_demo.py --project-dir my-app --agent-type coding --feature-id 42
python autonomous_agent_demo.py --project-dir my-app --agent-type testing
"""
import argparse
import asyncio
import sys
from pathlib import Path
# Windows-specific: Set ProactorEventLoop policy for subprocess support
# This MUST be set before any other asyncio operations
if sys.platform == "win32":
asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy())
from dotenv import load_dotenv
# Load environment variables from .env file (if it exists)
# IMPORTANT: Must be called BEFORE importing other modules that read env vars at load time
load_dotenv()
from agent import run_autonomous_agent
from registry import DEFAULT_MODEL, get_project_path
from structured_logging import get_logger
def safe_asyncio_run(coro):
"""
Run an async coroutine with proper cleanup to avoid Windows subprocess errors.
On Windows, subprocess transports may raise 'Event loop is closed' errors
during garbage collection if not properly cleaned up.
"""
if sys.platform == "win32":
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
try:
return loop.run_until_complete(coro)
finally:
# Cancel all pending tasks
pending = asyncio.all_tasks(loop)
for task in pending:
task.cancel()
# Allow cancelled tasks to complete
if pending:
loop.run_until_complete(asyncio.gather(*pending, return_exceptions=True))
# Shutdown async generators and executors
loop.run_until_complete(loop.shutdown_asyncgens())
if hasattr(loop, 'shutdown_default_executor'):
loop.run_until_complete(loop.shutdown_default_executor())
loop.close()
else:
return asyncio.run(coro)
def parse_args() -> argparse.Namespace:
"""Parse command line arguments."""
parser = argparse.ArgumentParser(
description="Autonomous Coding Agent Demo - Unified orchestrator pattern",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
# Use absolute path directly (single agent, default)
python autonomous_agent_demo.py --project-dir C:/Projects/my-app
# Use registered project name (looked up from registry)
python autonomous_agent_demo.py --project-dir my-app
# Parallel execution with 3 concurrent agents
python autonomous_agent_demo.py --project-dir my-app --concurrency 3
# YOLO mode: rapid prototyping without testing agents
python autonomous_agent_demo.py --project-dir my-app --yolo
# Configure testing agent ratio (2 testing agents per coding agent)
python autonomous_agent_demo.py --project-dir my-app --testing-ratio 2
# Disable testing agents (similar to YOLO but with verification)
python autonomous_agent_demo.py --project-dir my-app --testing-ratio 0
Authentication:
Uses Claude CLI authentication (run 'claude login' if not logged in)
Authentication is handled by start.bat/start.sh before this runs
""",
)
parser.add_argument(
"--project-dir",
type=str,
required=True,
help="Project directory path (absolute) or registered project name",
)
parser.add_argument(
"--max-iterations",
type=int,
default=None,
help="Maximum number of agent iterations (default: unlimited, typically 1 for subprocesses)",
)
parser.add_argument(
"--model",
type=str,
default=DEFAULT_MODEL,
help=f"Claude model to use (default: {DEFAULT_MODEL})",
)
parser.add_argument(
"--yolo",
action="store_true",
default=False,
help="Enable YOLO mode: skip testing agents for rapid prototyping",
)
# Unified orchestrator mode (replaces --parallel)
parser.add_argument(
"--concurrency", "-c",
type=int,
default=1,
help="Number of concurrent coding agents (default: 1, max: 5)",
)
# Backward compatibility: --parallel is deprecated alias for --concurrency
parser.add_argument(
"--parallel", "-p",
type=int,
nargs="?",
const=3,
default=None,
metavar="N",
help="DEPRECATED: Use --concurrency instead. Alias for --concurrency.",
)
parser.add_argument(
"--feature-id",
type=int,
default=None,
help="Work on a specific feature ID only (used by orchestrator for coding agents)",
)
# Agent type for subprocess mode
parser.add_argument(
"--agent-type",
choices=["initializer", "coding", "testing"],
default=None,
help="Agent type (used by orchestrator to spawn specialized subprocesses)",
)
parser.add_argument(
"--testing-feature-id",
type=int,
default=None,
help="Feature ID to regression test (used by orchestrator for testing agents)",
)
# Testing agent configuration
parser.add_argument(
"--testing-ratio",
type=int,
default=1,
help="Testing agents per coding agent (0-3, default: 1). Set to 0 to disable testing agents.",
)
return parser.parse_args()
def main() -> None:
"""Main entry point."""
print("[ENTRY] autonomous_agent_demo.py starting...", flush=True)
args = parse_args()
# Note: Authentication is handled by start.bat/start.sh before this script runs.
# The Claude SDK auto-detects credentials from ~/.claude/.credentials.json
# Handle deprecated --parallel flag
if args.parallel is not None:
print("WARNING: --parallel is deprecated. Use --concurrency instead.", flush=True)
args.concurrency = args.parallel
# Resolve project directory:
# 1. If absolute path, use as-is
# 2. Otherwise, look up from registry by name
project_dir_input = args.project_dir
project_dir = Path(project_dir_input)
# Logger will be initialized after project_dir is resolved
logger = None
if project_dir.is_absolute():
# Absolute path provided - use directly
if not project_dir.exists():
print(f"Error: Project directory does not exist: {project_dir}")
return
else:
# Treat as a project name - look up from registry
registered_path = get_project_path(project_dir_input)
if registered_path:
project_dir = registered_path
else:
print(f"Error: Project '{project_dir_input}' not found in registry")
print("Use an absolute path or register the project first.")
return
# Initialize logger now that project_dir is resolved
logger = get_logger(project_dir, agent_id="entry-point", console_output=False)
logger.info(
"Script started",
input_path=project_dir_input,
resolved_path=str(project_dir),
agent_type=args.agent_type,
concurrency=args.concurrency,
yolo_mode=args.yolo,
)
try:
if args.agent_type:
# Subprocess mode - spawned by orchestrator for a specific role
safe_asyncio_run(
run_autonomous_agent(
project_dir=project_dir,
model=args.model,
max_iterations=args.max_iterations or 1,
yolo_mode=args.yolo,
feature_id=args.feature_id,
agent_type=args.agent_type,
testing_feature_id=args.testing_feature_id,
)
)
else:
# Entry point mode - always use unified orchestrator
from parallel_orchestrator import run_parallel_orchestrator
# Clamp concurrency to valid range (1-5)
concurrency = max(1, min(args.concurrency, 5))
if concurrency != args.concurrency:
print(f"Clamping concurrency to valid range: {concurrency}", flush=True)
safe_asyncio_run(
run_parallel_orchestrator(
project_dir=project_dir,
max_concurrency=concurrency,
model=args.model,
yolo_mode=args.yolo,
testing_agent_ratio=args.testing_ratio,
)
)
except KeyboardInterrupt:
print("\n\nInterrupted by user")
print("To resume, run the same command again")
if logger:
logger.info("Interrupted by user")
except Exception as e:
print(f"\nFatal error: {e}")
if logger:
logger.error("Fatal error", error_type=type(e).__name__, message=str(e)[:200])
raise
if __name__ == "__main__":
main()