Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions tests/test_live_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -643,14 +643,36 @@ def fake_run(cmd, **kw):
assert text == "pong" and eff
run = next(c for c, _ in seen if "--format" in c)
assert run[:2] == ["oc", "run"] and "--pure" in run and "--standalone" in run
assert run[-3:] == ["--model", "p/m", "is it stuck?"]
assert run[-2:] == ["--model", "p/m"]
kw = next(k for c, k in seen if "--format" in c)
assert kw["input"] == "is it stuck?"
assert kw["env"]["TE_SIDECALL"] == "1"
perm = json.loads(kw["env"]["OPENCODE_CONFIG_CONTENT"])["permission"]
assert perm["bash"] == "deny" and perm["edit"] == "deny"
assert any(c[:3] == ["oc", "session", "delete"] and c[3] == "ses_x" for c, _ in seen)


def test_a_long_opencode_prompt_goes_on_stdin_not_the_command_line(monkeypatch):
"""Windows stops a command line at 32,767 characters, and the proposer's prompt (eight task
digests plus the facts) outgrows it: the call died with WinError 206 before a session
existed. `opencode run` reads the message from stdin when it is given none, so the prompt
must go there, never on argv."""
from tokeneconomizer.plugin import sidecall
seen = {}

def fake_run(cmd, **kw):
seen["cmd"], seen["kw"] = cmd, kw
return subprocess.CompletedProcess(cmd, 0, "", "")

monkeypatch.setattr(sidecall.subprocess, "run", fake_run)
sidecall._OC_FLAGS.clear()
prompt = "task digest " * 4_000 # 48,000 characters: past the limit
sidecall.opencode_call(prompt, model="", cli="oc")
assert prompt not in seen["cmd"]
assert max(len(part) for part in seen["cmd"]) < 200
assert seen["kw"]["input"] == prompt


def test_the_opencode_side_call_adds_only_the_flags_its_cli_lists(monkeypatch):
from tokeneconomizer.plugin import sidecall
monkeypatch.setattr(sidecall.subprocess, "run", lambda cmd, **kw: subprocess.CompletedProcess(
Expand Down Expand Up @@ -1247,7 +1269,12 @@ def fake_run(cmd, **kw):
seen.clear()
call(prompt)
assert seen and all(c[:2] == ["/n/node", f"/n/{cli}.js"] for c in seen), seen
assert any(prompt in c for c in seen)
if cli == "opencode":
# opencode is handed the prompt on stdin: Windows stops a command line at 32,767
# characters and the proposer's prompt outgrows it (see the long-prompt test).
assert prompt not in seen[0], seen[0]
else:
assert any(prompt in c for c in seen)


# `pi --print --mode json` on Pi 1.2.0 through OpenRouter, trimmed: the header, the system and
Expand Down
10 changes: 7 additions & 3 deletions tokeneconomizer/plugin/sidecall.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,10 +428,14 @@ def opencode_call(prompt, model="", cli="opencode", timeout=TIMEOUT_S):
env = dict(_env(), OPENCODE_CONFIG_CONTENT=OPENCODE_CONFIG)
exe = program(cli)
with tempfile.TemporaryDirectory(prefix="te-side-", ignore_cleanup_errors=True) as cwd:
# The message goes on stdin, never on argv: Windows stops a command line at 32,767
# characters, and the proposer's prompt (eight task digests plus the facts) outgrows
# that -- CreateProcess failed with WinError 206 before a session existed. `opencode
# run` reads the message from stdin when it is given none (opencode 2.3.0).
out = subprocess.run([*exe, "run", *opencode_flags(cli), "--format", "json",
*model_args, prompt],
capture_output=True, encoding="utf-8", errors="replace",
timeout=timeout, env=env, cwd=cwd, stdin=subprocess.DEVNULL)
*model_args],
input=prompt, capture_output=True, encoding="utf-8",
errors="replace", timeout=timeout, env=env, cwd=cwd)
text, usage, sid = trace.opencode_run_reply(out.stdout)
if sid:
try:
Expand Down