fix: resolve review budget feedback
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
"""Isolated opencode process runtime."""
|
||||
|
||||
import os
|
||||
import selectors
|
||||
import subprocess
|
||||
import time
|
||||
|
||||
@@ -130,6 +131,8 @@ def _run_process(
|
||||
cmd, cwd=cwd, env=env, capture_output=True, text=True,
|
||||
stdin=subprocess.DEVNULL, timeout=timeout,
|
||||
)
|
||||
if runner not in (None, subprocess.run):
|
||||
raise ValueError("custom runners are unsupported for budgeted streaming")
|
||||
existing_reason = budget_state.reason()
|
||||
if existing_reason:
|
||||
budget_state.cap_reason = existing_reason
|
||||
@@ -142,9 +145,27 @@ def _run_process(
|
||||
previous = {"steps": 0, "total": 0, "output": 0, "cost": 0.0}
|
||||
cap_reason = ""
|
||||
started = time.monotonic()
|
||||
selector = selectors.DefaultSelector()
|
||||
try:
|
||||
assert proc.stdout is not None
|
||||
for line in proc.stdout:
|
||||
selector.register(proc.stdout, selectors.EVENT_READ)
|
||||
while selector.get_map():
|
||||
remaining = budget.max_duration_seconds - (time.monotonic() - started)
|
||||
if remaining <= 0:
|
||||
cap_reason = "max_duration_seconds"
|
||||
budget_state.cap_reason = cap_reason
|
||||
proc.terminate()
|
||||
break
|
||||
events = selector.select(timeout=remaining)
|
||||
if not events:
|
||||
cap_reason = "max_duration_seconds"
|
||||
budget_state.cap_reason = cap_reason
|
||||
proc.terminate()
|
||||
break
|
||||
line = proc.stdout.readline()
|
||||
if not line:
|
||||
selector.unregister(proc.stdout)
|
||||
break
|
||||
output.append(line)
|
||||
_, usage = parse_events("".join(output))
|
||||
if usage:
|
||||
@@ -180,6 +201,7 @@ def _run_process(
|
||||
proc.kill()
|
||||
proc.wait()
|
||||
finally:
|
||||
selector.close()
|
||||
if proc.stdout:
|
||||
proc.stdout.close()
|
||||
stderr = proc.stderr.read() if proc.stderr else ""
|
||||
|
||||
Reference in New Issue
Block a user