Skip to content

Commit 839368a

Browse files
fix: use stdbuf -oL and StreamType.PIPE for proper streaming
1 parent 381fb46 commit 839368a

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

src/policyengine_api/agent_sandbox.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,9 @@ async def run_claude_code_in_sandbox_async(
8585
escaped_mcp_config = mcp_config_json.replace("'", "'\"'\"'")
8686
# CRITICAL: < /dev/null closes stdin (otherwise Claude hangs waiting for input)
8787
# 2>&1 merges stderr into stdout for unified streaming
88+
# stdbuf -oL forces line-buffered stdout to prevent libc buffering
8889
cmd = (
89-
f"claude -p '{escaped_question}' "
90+
f"stdbuf -oL claude -p '{escaped_question}' "
9091
f"--mcp-config '{escaped_mcp_config}' "
9192
"--output-format stream-json --verbose --max-turns 10 "
9293
"--allowedTools 'mcp__policyengine__*,Bash,Read,Grep,Glob,Write,Edit' "
@@ -99,14 +100,14 @@ async def run_claude_code_in_sandbox_async(
99100
escaped_question_len=len(escaped_question),
100101
)
101102
# Use async exec for proper streaming
102-
# stdout=StreamType.STDOUT prints to Modal logs as it comes in
103+
# stdout=StreamType.PIPE allows us to consume the stream (default but explicit)
103104
process = await sb.exec.aio(
104105
"sh",
105106
"-c",
106107
cmd,
107108
text=True,
108109
bufsize=1,
109-
stdout=StreamType.STDOUT, # Print to Modal logs for debugging
110+
stdout=StreamType.PIPE,
110111
)
111112
logfire.info("run_claude_code_in_sandbox: claude CLI process started, returning")
112113

0 commit comments

Comments
 (0)