fix: resolve review budget feedback

This commit is contained in:
Claude
2026-09-01 12:14:18 +00:00
parent 4d51e14a1a
commit 592747d98d
3 changed files with 49 additions and 13 deletions
+11 -11
View File
@@ -163,15 +163,15 @@ def equivalent_cost(usage: dict, model: str, price_target: str = "") -> float:
"""Estimate comparison cost for one completed iteration."""
try:
from cost_model import PRICES, Usage, cost
target = price_target or os.environ.get("PRAGENT_PRICE_TARGET", "claude-sonnet-5")
price = PRICES.get(target)
if price is None:
return 0.0
return cost(Usage(
uncached_input=max(0, int(usage.get("input") or 0) - int(usage.get("cache_read") or 0)),
cached_input=int(usage.get("cache_read") or 0),
cache_writes=int(usage.get("cache_write") or 0),
output=int(usage.get("output") or 0),
), price)
except Exception:
except (ImportError, ModuleNotFoundError):
return 0.0
target = price_target or os.environ.get("PRAGENT_PRICE_TARGET", "claude-sonnet-5")
price = PRICES.get(target)
if price is None:
return 0.0
return cost(Usage(
uncached_input=max(0, int(usage.get("input") or 0) - int(usage.get("cache_read") or 0)),
cached_input=int(usage.get("cache_read") or 0),
cache_writes=int(usage.get("cache_write") or 0),
output=int(usage.get("output") or 0),
), price)
+23 -1
View File
@@ -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 ""