Skip to content

Commit 4ffb918

Browse files
fix: add --dangerously-skip-permissions + clean up logging (#31)
- Add --dangerously-skip-permissions to Claude CLI (fixes permission prompt hang) - Clean up logfire calls with proper kwargs - Remove duplicate/broken print statements
1 parent d7adf38 commit 4ffb918

2 files changed

Lines changed: 14 additions & 22 deletions

File tree

src/policyengine_api/agent_sandbox.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ def run_claude_code_in_sandbox(
136136
)
137137

138138
# Run Claude Code with the question
139+
# --dangerously-skip-permissions: auto-accept permission prompts (required for non-interactive)
139140
logfire.info("run_claude_code_in_sandbox: starting claude CLI")
140141
process = sb.exec(
141142
"claude",
@@ -144,6 +145,7 @@ def run_claude_code_in_sandbox(
144145
"--output-format",
145146
"stream-json",
146147
"--verbose",
148+
"--dangerously-skip-permissions",
147149
"--allowedTools",
148150
"mcp__policyengine__*,Bash,Read,Grep,Glob,Write,Edit",
149151
)

src/policyengine_api/api/agent.py

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import os
1111
from uuid import uuid4
1212

13+
import logfire
1314
from fastapi import APIRouter, HTTPException
1415
from fastapi.responses import StreamingResponse
1516
from pydantic import BaseModel
@@ -88,32 +89,24 @@ async def _stream_modal_sandbox(question: str, api_base_url: str):
8889
"""Stream output from Claude Code running in Modal Sandbox."""
8990
from concurrent.futures import ThreadPoolExecutor
9091

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])
9694

9795
sb = None
9896
executor = ThreadPoolExecutor(max_workers=1)
9997
try:
10098
from policyengine_api.agent_sandbox import run_claude_code_in_sandbox
10199

102-
print("[AGENT] About to call run_claude_code_in_sandbox")
103100
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
105102
)
106-
yield f"data: {json.dumps({'type': 'output', 'content': '[DEBUG] Creating Modal sandbox...\\n'})}\n\n"
107103

108104
# Run blocking Modal SDK calls in thread pool to avoid blocking event loop
109105
loop = asyncio.get_event_loop()
110-
print("[AGENT] Calling run_in_executor for sandbox creation")
111106
sb, process = await loop.run_in_executor(
112107
executor, run_claude_code_in_sandbox, question, api_base_url
113108
)
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")
117110

118111
# Poll for lines with timeout to allow other async tasks
119112
import queue
@@ -190,11 +183,7 @@ def stream_reader():
190183
raise
191184

192185
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))
198187
yield f"data: {json.dumps({'type': 'error', 'content': f'Sandbox error: {str(e)}'})}\n\n"
199188
yield f"data: {json.dumps({'type': 'done', 'returncode': 1})}\n\n"
200189
finally:
@@ -226,19 +215,20 @@ async def stream_analysis(request: AskRequest):
226215
data: {"type": "done", "returncode": 0}
227216
```
228217
"""
229-
print(f"[AGENT] /stream endpoint called with question: {request.question[:50]}")
230-
print(f"[AGENT] agent_use_modal = {settings.agent_use_modal}")
231218
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+
)
233225

234226
if settings.agent_use_modal:
235-
print("[AGENT] Using Modal sandbox path")
236227
return StreamingResponse(
237228
_stream_modal_sandbox(request.question, api_base_url),
238229
media_type="text/event-stream",
239230
)
240231
else:
241-
print("[AGENT] Using local claude path")
242232
return StreamingResponse(
243233
_stream_claude_code(request.question, api_base_url),
244234
media_type="text/event-stream",

0 commit comments

Comments
 (0)