change stdout

This commit is contained in:
Pinghua
2026-06-10 15:56:43 +08:00
parent 971ee4dfff
commit 594d9b0b2d
+28 -13
View File
@@ -19,6 +19,7 @@ import shutil
import subprocess import subprocess
import sys import sys
import threading import threading
import time
import urllib.error import urllib.error
import urllib.request import urllib.request
@@ -321,13 +322,12 @@ def run_claude(claude_bin, project_root, prompt, extra_dir=None):
cmd = [ cmd = [
claude_bin, "-p", claude_bin, "-p",
"--permission-mode", "acceptEdits", "--permission-mode", "acceptEdits",
"--verbose",
] ]
if extra_dir: if extra_dir:
cmd += ["--add-dir", extra_dir] cmd += ["--add-dir", extra_dir]
cmd.append(prompt) cmd.append(prompt)
print(f"[review_pr] Running: {' '.join(cmd[:4])} ...", file=sys.stderr) print(f"[review_pr] Command: {' '.join(cmd)}", file=sys.stderr)
proc = subprocess.Popen( proc = subprocess.Popen(
cmd, cmd,
cwd=project_root, cwd=project_root,
@@ -337,22 +337,37 @@ def run_claude(claude_bin, project_root, prompt, extra_dir=None):
encoding="utf-8", encoding="utf-8",
) )
def _stream(pipe, label, dest_lines): def _stream(pipe, prefix, dest_lines):
for line in pipe: for line in pipe:
line = line.rstrip("\n") line = line.rstrip("\n")
if line:
print(f"[{label}] {line}", file=sys.stderr)
dest_lines.append(line) dest_lines.append(line)
if line.strip():
stderr_lines = [] print(f"[{prefix}] {line}", file=sys.stderr)
t = threading.Thread(target=_stream, args=(proc.stderr, "claude:stderr", stderr_lines))
t.start()
stdout_lines = [] stdout_lines = []
_stream(proc.stdout, "claude", stdout_lines) stderr_lines = []
t.join(timeout=10) # Heartbeat: print alive message every 30s so we know claude hasn't hung
proc.wait(timeout=600) heartbeat_stop = threading.Event()
def _heartbeat(start):
while not heartbeat_stop.is_set():
heartbeat_stop.wait(30)
if not heartbeat_stop.is_set():
elapsed = int(time.time() - start)
print(f"[review_pr] ... still running ({elapsed}s elapsed)", file=sys.stderr)
t_beat = threading.Thread(target=_heartbeat, args=(time.time(),))
t_beat.start()
t_out = threading.Thread(target=_stream, args=(proc.stdout, "claude", stdout_lines))
t_err = threading.Thread(target=_stream, args=(proc.stderr, "claude:stderr", stderr_lines))
t_out.start()
t_err.start()
t_out.join(timeout=600)
heartbeat_stop.set()
t_err.join(timeout=10)
t_beat.join(timeout=5)
proc.wait(timeout=10)
if proc.returncode != 0: if proc.returncode != 0:
print(f"Claude exited with code {proc.returncode}", file=sys.stderr) print(f"Claude exited with code {proc.returncode}", file=sys.stderr)
@@ -367,7 +382,7 @@ def extract_json_from_output(stdout):
match = re.search(r"```json\s*([\s\S]*)\s*```", stdout) match = re.search(r"```json\s*([\s\S]*)\s*```", stdout)
if not match: if not match:
print("ERROR: No ```json block found in Claude output.", file=sys.stderr) print("ERROR: No ```json block found in Claude output.", file=sys.stderr)
print(stdout[:3000], file=sys.stderr) print(stdout[-3000:], file=sys.stderr)
sys.exit(1) sys.exit(1)
try: try: