1515 get_file_paths ,
1616 calculate_uvicorn_target_for_local ,
1717)
18- from agentex .lib .constants .ports import DEFAULT_AGENTEX_BASE_URL
1918from agentex .lib .environment_variables import EnvVarKeys
2019from agentex .lib .sdk .config .agent_manifest import AgentManifest
2120from agentex .lib .cli .handlers .cleanup_handlers import cleanup_agent_workflows , should_cleanup_on_restart
@@ -95,16 +94,16 @@ async def start_temporal_worker_with_reload(
9594 worker_process = await start_temporal_worker (worker_path , env , manifest_dir )
9695 process_manager .add_process (worker_process )
9796 return asyncio .create_task (stream_process_output (worker_process , "WORKER" ))
98-
97+
9998 async def worker_runner () -> None :
10099 current_process : asyncio .subprocess .Process | None = None
101100 output_task : asyncio .Task [None ] | None = None
102-
101+
103102 console .print (f"[blue]Starting Temporal worker with auto-reload from { worker_path } ...[/blue]" )
104-
103+
105104 async def start_worker () -> asyncio .subprocess .Process :
106105 nonlocal current_process , output_task
107-
106+
108107 # PRE-RESTART CLEANUP - NEW!
109108 if current_process is not None :
110109 # Extract agent name from worker path for cleanup
@@ -113,7 +112,7 @@ async def start_worker() -> asyncio.subprocess.Process:
113112 console .print (f"FOUND AGENT_NAME FROM ENV VARS: { agent_name } { agent_name is None } " )
114113 if agent_name is None :
115114 agent_name = worker_path .parent .parent .name
116-
115+
117116 # Perform cleanup if configured
118117 if should_cleanup_on_restart ():
119118 console .print ("[yellow]Cleaning up workflows before worker restart...[/yellow]" )
@@ -122,7 +121,7 @@ async def start_worker() -> asyncio.subprocess.Process:
122121 except Exception as e :
123122 logger .warning (f"Cleanup failed: { e } " )
124123 console .print (f"[yellow]⚠ Cleanup failed: { str (e )} [/yellow]" )
125-
124+
126125 # Clean up previous process
127126 if current_process and current_process .returncode is None :
128127 current_process .terminate ()
@@ -131,41 +130,41 @@ async def start_worker() -> asyncio.subprocess.Process:
131130 except asyncio .TimeoutError :
132131 current_process .kill ()
133132 await current_process .wait ()
134-
133+
135134 # Cancel previous output task
136135 if output_task :
137136 output_task .cancel ()
138137 try :
139138 await output_task
140139 except asyncio .CancelledError :
141140 pass
142-
141+
143142 current_process = await start_temporal_worker (worker_path , env , manifest_dir )
144143 process_manager .add_process (current_process )
145144 console .print ("[green]Temporal worker started[/green]" )
146145 return current_process
147-
146+
148147 try :
149148 # Start initial worker
150149 current_process = await start_worker ()
151150 if current_process :
152151 output_task = asyncio .create_task (stream_process_output (current_process , "WORKER" ))
153-
152+
154153 # Watch for file changes
155154 async for changes in awatch (manifest_dir , recursive = True ):
156155 # Filter for Python files
157- py_changes = [(change , path ) for change , path in changes if str (path ).endswith (' .py' )]
158-
156+ py_changes = [(change , path ) for change , path in changes if str (path ).endswith (" .py" )]
157+
159158 if py_changes :
160159 changed_files = [str (Path (path ).relative_to (worker_path .parent )) for _ , path in py_changes ]
161160 console .print (f"[yellow]File changes detected: { changed_files } [/yellow]" )
162161 console .print ("[yellow]Restarting Temporal worker...[/yellow]" )
163-
162+
164163 # Restart worker (with cleanup handled in start_worker)
165164 await start_worker ()
166165 if current_process :
167166 output_task = asyncio .create_task (stream_process_output (current_process , "WORKER" ))
168-
167+
169168 except asyncio .CancelledError :
170169 # Clean shutdown
171170 if output_task :
@@ -174,7 +173,7 @@ async def start_worker() -> asyncio.subprocess.Process:
174173 await output_task
175174 except asyncio .CancelledError :
176175 pass
177-
176+
178177 if current_process and current_process .returncode is None :
179178 current_process .terminate ()
180179 try :
@@ -183,7 +182,7 @@ async def start_worker() -> asyncio.subprocess.Process:
183182 current_process .kill ()
184183 await current_process .wait ()
185184 raise
186-
185+
187186 return asyncio .create_task (worker_runner ())
188187
189188
@@ -193,7 +192,7 @@ async def start_acp_server(
193192 """Start the ACP server process"""
194193 # Use file path relative to manifest directory if possible
195194 uvicorn_target = calculate_uvicorn_target_for_local (acp_path , manifest_dir )
196-
195+
197196 cmd = [
198197 sys .executable ,
199198 "-m" ,
@@ -297,11 +296,17 @@ async def run_agent(manifest_path: str, debug_config: "DebugConfig | None" = Non
297296 manifest_dir = Path (manifest_path ).parent
298297 if debug_config and debug_config .should_debug_acp ():
299298 acp_process = await start_acp_server_debug (
300- file_paths ["acp" ], manifest .local_development .agent .port , agent_env , debug_config # type: ignore[union-attr]
299+ file_paths ["acp" ],
300+ manifest .local_development .agent .port ,
301+ agent_env ,
302+ debug_config , # type: ignore[union-attr]
301303 )
302304 else :
303305 acp_process = await start_acp_server (
304- file_paths ["acp" ], manifest .local_development .agent .port , agent_env , manifest_dir # type: ignore[union-attr]
306+ file_paths ["acp" ],
307+ manifest .local_development .agent .port ,
308+ agent_env ,
309+ manifest_dir , # type: ignore[union-attr]
305310 )
306311 process_manager .add_process (acp_process )
307312
@@ -314,14 +319,14 @@ async def run_agent(manifest_path: str, debug_config: "DebugConfig | None" = Non
314319 if is_temporal_agent (manifest ) and file_paths ["worker" ]:
315320 if debug_config and debug_config .should_debug_worker ():
316321 # In debug mode, start worker without auto-reload to prevent conflicts
317- worker_process = await start_temporal_worker_debug (
318- file_paths ["worker" ], agent_env , debug_config
319- )
322+ worker_process = await start_temporal_worker_debug (file_paths ["worker" ], agent_env , debug_config )
320323 process_manager .add_process (worker_process )
321324 worker_task = asyncio .create_task (stream_process_output (worker_process , "WORKER" ))
322325 else :
323326 # Normal mode with auto-reload
324- worker_task = await start_temporal_worker_with_reload (file_paths ["worker" ], agent_env , process_manager , manifest_dir )
327+ worker_task = await start_temporal_worker_with_reload (
328+ file_paths ["worker" ], agent_env , process_manager , manifest_dir
329+ )
325330 tasks .append (worker_task )
326331
327332 console .print (
@@ -334,7 +339,7 @@ async def run_agent(manifest_path: str, debug_config: "DebugConfig | None" = Non
334339 await process_manager .wait_for_shutdown ()
335340 except KeyboardInterrupt :
336341 console .print ("\n [yellow]Received shutdown signal...[/yellow]" )
337-
342+
338343 # Cancel output streaming tasks
339344 for task in tasks :
340345 task .cancel ()
@@ -352,9 +357,6 @@ async def run_agent(manifest_path: str, debug_config: "DebugConfig | None" = Non
352357 await process_manager .cleanup_processes ()
353358
354359
355-
356-
357-
358360def create_agent_environment (manifest : AgentManifest ) -> dict [str , str ]:
359361 """Create environment variables for agent processes without modifying os.environ"""
360362 # Start with current environment
@@ -378,6 +380,7 @@ def create_agent_environment(manifest: AgentManifest) -> dict[str, str]:
378380
379381 # Add authorization principal if set - for local development, auth is optional
380382 from agentex .lib .cli .utils .auth_utils import _encode_principal_context
383+
381384 encoded_principal = _encode_principal_context (manifest )
382385 if encoded_principal :
383386 env_vars [EnvVarKeys .AUTH_PRINCIPAL_B64 ] = encoded_principal
0 commit comments