|
10 | 10 | import os |
11 | 11 | from uuid import uuid4 |
12 | 12 |
|
| 13 | +import logfire |
13 | 14 | from fastapi import APIRouter, HTTPException |
14 | 15 | from fastapi.responses import StreamingResponse |
15 | 16 | from pydantic import BaseModel |
@@ -88,32 +89,24 @@ async def _stream_modal_sandbox(question: str, api_base_url: str): |
88 | 89 | """Stream output from Claude Code running in Modal Sandbox.""" |
89 | 90 | from concurrent.futures import ThreadPoolExecutor |
90 | 91 |
|
91 | | - import logfire |
92 | | - |
93 | | - # Immediate yield to confirm generator is running |
94 | | - print(f"[AGENT] _stream_modal_sandbox called with question: {question[:50]}") |
95 | | - yield f"data: {json.dumps({'type': 'output', 'content': '[DEBUG] Generator started\\n'})}\n\n" |
| 92 | + # Immediate log and yield |
| 93 | + logfire.info("_stream_modal_sandbox: started", question=question[:100]) |
96 | 94 |
|
97 | 95 | sb = None |
98 | 96 | executor = ThreadPoolExecutor(max_workers=1) |
99 | 97 | try: |
100 | 98 | from policyengine_api.agent_sandbox import run_claude_code_in_sandbox |
101 | 99 |
|
102 | | - print("[AGENT] About to call run_claude_code_in_sandbox") |
103 | 100 | logfire.info( |
104 | | - "Creating Modal sandbox", question=question[:100], api_base_url=api_base_url |
| 101 | + "_stream_modal_sandbox: creating sandbox", api_base_url=api_base_url |
105 | 102 | ) |
106 | | - yield f"data: {json.dumps({'type': 'output', 'content': '[DEBUG] Creating Modal sandbox...\\n'})}\n\n" |
107 | 103 |
|
108 | 104 | # Run blocking Modal SDK calls in thread pool to avoid blocking event loop |
109 | 105 | loop = asyncio.get_event_loop() |
110 | | - print("[AGENT] Calling run_in_executor for sandbox creation") |
111 | 106 | sb, process = await loop.run_in_executor( |
112 | 107 | executor, run_claude_code_in_sandbox, question, api_base_url |
113 | 108 | ) |
114 | | - print("[AGENT] Sandbox created successfully") |
115 | | - logfire.info("Modal sandbox created, streaming output") |
116 | | - yield f"data: {json.dumps({'type': 'output', 'content': '[DEBUG] Sandbox created, starting stream...\\n'})}\n\n" |
| 109 | + logfire.info("_stream_modal_sandbox: sandbox created") |
117 | 110 |
|
118 | 111 | # Poll for lines with timeout to allow other async tasks |
119 | 112 | import queue |
@@ -190,11 +183,7 @@ def stream_reader(): |
190 | 183 | raise |
191 | 184 |
|
192 | 185 | except Exception as e: |
193 | | - import traceback |
194 | | - |
195 | | - tb = traceback.format_exc() |
196 | | - print(f"[AGENT] Exception in _stream_modal_sandbox: {e}\n{tb}") |
197 | | - logfire.exception("Modal sandbox failed", error=str(e)) |
| 186 | + logfire.exception("_stream_modal_sandbox: failed", error=str(e)) |
198 | 187 | yield f"data: {json.dumps({'type': 'error', 'content': f'Sandbox error: {str(e)}'})}\n\n" |
199 | 188 | yield f"data: {json.dumps({'type': 'done', 'returncode': 1})}\n\n" |
200 | 189 | finally: |
@@ -226,19 +215,20 @@ async def stream_analysis(request: AskRequest): |
226 | 215 | data: {"type": "done", "returncode": 0} |
227 | 216 | ``` |
228 | 217 | """ |
229 | | - print(f"[AGENT] /stream endpoint called with question: {request.question[:50]}") |
230 | | - print(f"[AGENT] agent_use_modal = {settings.agent_use_modal}") |
231 | 218 | api_base_url = settings.policyengine_api_url |
232 | | - print(f"[AGENT] api_base_url = {api_base_url}") |
| 219 | + logfire.info( |
| 220 | + "stream_analysis: called", |
| 221 | + question=request.question[:100], |
| 222 | + agent_use_modal=settings.agent_use_modal, |
| 223 | + api_base_url=api_base_url, |
| 224 | + ) |
233 | 225 |
|
234 | 226 | if settings.agent_use_modal: |
235 | | - print("[AGENT] Using Modal sandbox path") |
236 | 227 | return StreamingResponse( |
237 | 228 | _stream_modal_sandbox(request.question, api_base_url), |
238 | 229 | media_type="text/event-stream", |
239 | 230 | ) |
240 | 231 | else: |
241 | | - print("[AGENT] Using local claude path") |
242 | 232 | return StreamingResponse( |
243 | 233 | _stream_claude_code(request.question, api_base_url), |
244 | 234 | media_type="text/event-stream", |
|
0 commit comments