Skip to content

Commit d7adf38

Browse files
debug: add print statements and immediate yields to agent (#29)
- Print statements will show in Cloud Run logs - Immediate yields confirm generator is running - Full traceback on exceptions
1 parent 37b944d commit d7adf38

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

src/policyengine_api/api/agent.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,21 +90,30 @@ async def _stream_modal_sandbox(question: str, api_base_url: str):
9090

9191
import logfire
9292

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"
96+
9397
sb = None
9498
executor = ThreadPoolExecutor(max_workers=1)
9599
try:
96100
from policyengine_api.agent_sandbox import run_claude_code_in_sandbox
97101

102+
print("[AGENT] About to call run_claude_code_in_sandbox")
98103
logfire.info(
99104
"Creating Modal sandbox", question=question[:100], api_base_url=api_base_url
100105
)
106+
yield f"data: {json.dumps({'type': 'output', 'content': '[DEBUG] Creating Modal sandbox...\\n'})}\n\n"
101107

102108
# Run blocking Modal SDK calls in thread pool to avoid blocking event loop
103109
loop = asyncio.get_event_loop()
110+
print("[AGENT] Calling run_in_executor for sandbox creation")
104111
sb, process = await loop.run_in_executor(
105112
executor, run_claude_code_in_sandbox, question, api_base_url
106113
)
114+
print("[AGENT] Sandbox created successfully")
107115
logfire.info("Modal sandbox created, streaming output")
116+
yield f"data: {json.dumps({'type': 'output', 'content': '[DEBUG] Sandbox created, starting stream...\\n'})}\n\n"
108117

109118
# Poll for lines with timeout to allow other async tasks
110119
import queue
@@ -181,6 +190,10 @@ def stream_reader():
181190
raise
182191

183192
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}")
184197
logfire.exception("Modal sandbox failed", error=str(e))
185198
yield f"data: {json.dumps({'type': 'error', 'content': f'Sandbox error: {str(e)}'})}\n\n"
186199
yield f"data: {json.dumps({'type': 'done', 'returncode': 1})}\n\n"
@@ -213,14 +226,19 @@ async def stream_analysis(request: AskRequest):
213226
data: {"type": "done", "returncode": 0}
214227
```
215228
"""
229+
print(f"[AGENT] /stream endpoint called with question: {request.question[:50]}")
230+
print(f"[AGENT] agent_use_modal = {settings.agent_use_modal}")
216231
api_base_url = settings.policyengine_api_url
232+
print(f"[AGENT] api_base_url = {api_base_url}")
217233

218234
if settings.agent_use_modal:
235+
print("[AGENT] Using Modal sandbox path")
219236
return StreamingResponse(
220237
_stream_modal_sandbox(request.question, api_base_url),
221238
media_type="text/event-stream",
222239
)
223240
else:
241+
print("[AGENT] Using local claude path")
224242
return StreamingResponse(
225243
_stream_claude_code(request.question, api_base_url),
226244
media_type="text/event-stream",

0 commit comments

Comments
 (0)