-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathdebug_handlers.py
More file actions
179 lines (141 loc) Β· 5.7 KB
/
Copy pathdebug_handlers.py
File metadata and controls
179 lines (141 loc) Β· 5.7 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
"""
Debug process handlers for AgentEx CLI.
Provides debug-enabled versions of ACP server and temporal worker startup.
"""
import sys
import asyncio
import asyncio.subprocess
from typing import TYPE_CHECKING, Dict
from pathlib import Path
from rich.console import Console
if TYPE_CHECKING:
pass
from agentex.lib.utils.logging import make_logger
from agentex.lib.cli.utils.cli_utils import SUBPROCESS_STREAM_LIMIT
from .debug_config import DebugConfig, resolve_debug_port
logger = make_logger(__name__)
console = Console()
async def start_temporal_worker_debug(
worker_path: Path,
env: Dict[str, str],
debug_config: DebugConfig
):
"""Start temporal worker with debug support"""
if not debug_config.should_debug_worker():
raise ValueError("Debug config is not configured for worker debugging")
# Resolve the actual debug port
debug_port = resolve_debug_port(debug_config, debug_config.get_worker_port())
# Add debug environment variables
debug_env = env.copy()
debug_env.update({
"AGENTEX_DEBUG_ENABLED": "true",
"AGENTEX_DEBUG_PORT": str(debug_port),
"AGENTEX_DEBUG_WAIT_FOR_ATTACH": str(debug_config.wait_for_attach).lower(),
"AGENTEX_DEBUG_TYPE": "worker"
})
# Start the worker process
# For debugging, use absolute path to run_worker.py to run from workspace root
worker_script = worker_path.parent / "run_worker.py"
cmd = [sys.executable, str(worker_script)]
console.print(f"[blue]π Starting Temporal worker in debug mode[/blue]")
console.print(f"[yellow]π‘ Debug server will listen on port {debug_port}[/yellow]")
console.print(f"[green]β VS Code should connect to: localhost:{debug_port}[/green]")
if debug_config.wait_for_attach:
console.print(f"[yellow]β³ Worker will wait for debugger to attach[/yellow]")
console.print(f"[dim]π‘ In your IDE: Attach to localhost:{debug_port}[/dim]")
console.print(f"[dim]π§ If connection fails, check that VS Code launch.json uses port {debug_port}[/dim]")
return await asyncio.create_subprocess_exec(
*cmd,
cwd=Path.cwd(), # Run from current working directory (workspace root)
env=debug_env,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.STDOUT,
limit=SUBPROCESS_STREAM_LIMIT,
)
async def start_acp_server_debug(
acp_path: Path,
port: int,
env: Dict[str, str],
debug_config: DebugConfig
):
"""Start ACP server with debug support"""
if not debug_config.should_debug_acp():
raise ValueError("Debug config is not configured for ACP debugging")
# Resolve the actual debug port
debug_port = resolve_debug_port(debug_config, debug_config.get_acp_port())
# Add debug environment variables
debug_env = env.copy()
debug_env.update({
"AGENTEX_DEBUG_ENABLED": "true",
"AGENTEX_DEBUG_PORT": str(debug_port),
"AGENTEX_DEBUG_WAIT_FOR_ATTACH": str(debug_config.wait_for_attach).lower(),
"AGENTEX_DEBUG_TYPE": "acp"
})
# Disable uvicorn auto-reload in debug mode to prevent conflicts
cmd = [
sys.executable,
"-m",
"uvicorn",
f"{acp_path.parent.name}.acp:acp",
"--port",
str(port),
"--host",
"0.0.0.0",
# Note: No --reload flag when debugging
]
console.print(f"[blue]π Starting ACP server in debug mode[/blue]")
console.print(f"[yellow]π‘ Debug server will listen on port {debug_port}[/yellow]")
if debug_config.wait_for_attach:
console.print(f"[yellow]β³ ACP server will wait for debugger to attach[/yellow]")
console.print(f"[dim]π‘ In your IDE: Attach to localhost:{debug_port}[/dim]")
return await asyncio.create_subprocess_exec(
*cmd,
cwd=acp_path.parent.parent,
env=debug_env,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.STDOUT,
limit=SUBPROCESS_STREAM_LIMIT,
)
def create_debug_startup_script() -> str:
"""Create a Python script snippet for debug initialization"""
return '''
import os
import sys
# Debug initialization for AgentEx
if os.getenv("AGENTEX_DEBUG_ENABLED") == "true":
try:
import debugpy
debug_port = int(os.getenv("AGENTEX_DEBUG_PORT", "5678"))
debug_type = os.getenv("AGENTEX_DEBUG_TYPE", "unknown")
wait_for_attach = os.getenv("AGENTEX_DEBUG_WAIT_FOR_ATTACH", "false").lower() == "true"
# Configure debugpy
debugpy.configure(subProcess=False)
debugpy.listen(debug_port)
print(f"π [{debug_type.upper()}] Debug server listening on port {debug_port}")
if wait_for_attach:
print(f"β³ [{debug_type.upper()}] Waiting for debugger to attach...")
debugpy.wait_for_client()
print(f"β
[{debug_type.upper()}] Debugger attached!")
else:
print(f"π‘ [{debug_type.upper()}] Ready for debugger attachment")
except ImportError:
print("β debugpy not available. Install with: pip install debugpy")
sys.exit(1)
except Exception as e:
print(f"β Debug setup failed: {e}")
sys.exit(1)
'''
def inject_debug_code_to_worker_template() -> str:
"""Generate debug code to inject into worker template"""
return """
# === DEBUG SETUP (Auto-generated by AgentEx CLI) ===
""" + create_debug_startup_script() + """
# === END DEBUG SETUP ===
"""
def inject_debug_code_to_acp_template() -> str:
"""Generate debug code to inject into ACP template"""
return """
# === DEBUG SETUP (Auto-generated by AgentEx CLI) ===
""" + create_debug_startup_script() + """
# === END DEBUG SETUP ===
"""