Skip to content

Commit 459fb3f

Browse files
i-vainnclaude
andcommitted
fix(opencode): optionally pass the prompt to opencode run verbatim
`opencode run` wraps every message argument that contains a space in double quotes and escapes inner quotes (anomalyco/opencode#43923), so opencode_sandboxed_agent and the local CLI path of opencode_agent deliver `"<prompt>"`, with each `"` as `\"`, instead of the task prompt. A new `verbatim_prompt` flag on both sends the prompt on stdin instead, which `opencode run` uses unchanged, as opencode_agent sandbox sessions already do. It defaults to false, leaving the launched command and every existing workflow exactly as before. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: i-vainn <[email protected]>
1 parent 2d6170d commit 459fb3f

6 files changed

Lines changed: 167 additions & 3 deletions

File tree

‎responses_api_agents/opencode_agent/README.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,13 @@ stricter, single-activation contract described above. The intentional difference
131131
| Provider name | `nemo` | `nemo_gym` (internal alias for the same Gym model server) |
132132
| `env`, `extra_args`, custom `command` | Applied | Rejected at seed, not silently ignored |
133133
| Request controls | Existing local compatibility behavior | Unsupported controls rejected explicitly |
134+
| Prompt delivery | Last `opencode run` argument; on stdin with `verbatim_prompt: true` | Always on stdin |
135+
136+
By default the local CLI passes the prompt as an argument, which `opencode run` wraps in
137+
double quotes, escaping every inner `"` as `\"`
138+
([anomalyco/opencode#43923](https://github.com/anomalyco/opencode/issues/43923)).
139+
`verbatim_prompt: true` (default false) sends it on stdin, which OpenCode uses unchanged.
140+
Enabling it changes the model's input, so results are not comparable with runs made without it.
134141

135142
Both paths honor configured `thinking` and declare interleaved `reasoning_content`.
136143
Different prompt placement can affect results; this integration does not establish

‎responses_api_agents/opencode_agent/app.py‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,15 @@ class OpenCodeAgentConfig(BaseResponsesAPIAgentConfig):
177177
context_window: int = 262144
178178
max_output_tokens: int = 131072
179179
opencode_version: str = OPENCODE_VERSION
180+
verbatim_prompt: bool = Field(
181+
default=False,
182+
description=(
183+
"Local CLI only: send the task prompt to `opencode run` on stdin instead of as a command-line "
184+
"argument. OpenCode wraps an argument containing spaces in double quotes and escapes inner quotes "
185+
"(anomalyco/opencode#43923), so the model otherwise sees an altered prompt. Off by default so "
186+
"existing results stay comparable. Sandbox sessions always send the prompt on stdin."
187+
),
188+
)
180189

181190
# Used only when Resources does not supply a borrowed sandbox.
182191
sandbox_provider: str | None = None
@@ -346,19 +355,22 @@ async def _run_opencode(
346355
if self.config.thinking:
347356
cmd.append("--thinking")
348357
cmd.extend(self.config.extra_args)
349-
cmd.append(prompt)
358+
if not self.config.verbatim_prompt:
359+
cmd.append(prompt)
350360

351361
try:
352362
timed_out = False
353363
proc = await asyncio.create_subprocess_exec(
354364
*cmd,
355365
cwd=str(project_dir),
366+
stdin=asyncio.subprocess.PIPE if self.config.verbatim_prompt else None,
356367
stdout=asyncio.subprocess.PIPE,
357368
stderr=asyncio.subprocess.PIPE,
358369
env=env,
359370
)
371+
prompt_input = prompt.encode() if self.config.verbatim_prompt else None
360372
try:
361-
_, stderr = await asyncio.wait_for(proc.communicate(), timeout=self.config.timeout)
373+
_, stderr = await asyncio.wait_for(proc.communicate(prompt_input), timeout=self.config.timeout)
362374
except asyncio.TimeoutError:
363375
proc.kill()
364376
_, stderr = await proc.communicate()

‎responses_api_agents/opencode_agent/tests/test_app.py‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import asyncio
1717
import json
18+
import sys
1819
from pathlib import Path
1920
from unittest.mock import AsyncMock, MagicMock, patch
2021
from uuid import uuid4
@@ -753,3 +754,42 @@ def test_config_yaml_parses(self, monkeypatch: pytest.MonkeyPatch, training_capt
753754
agent.base_url_for_run("http://model", {"_ng_task_index": 0, "_ng_rollout_index": 0})
754755
== f"http://model/ng-rollout/0-0{capture_path}"
755756
)
757+
758+
759+
class TestVerbatimPrompt:
760+
@pytest.mark.parametrize("verbatim_prompt", [False, True])
761+
async def test_prompt_delivery_to_opencode(self, tmp_path: Path, verbatim_prompt: bool) -> None:
762+
"""Launch a stand-in `opencode` that records its arguments and stdin."""
763+
received = tmp_path / "received.json"
764+
opencode = tmp_path / "opencode"
765+
opencode.write_text(
766+
f"#!{sys.executable}\n"
767+
"import json, os, sys\n"
768+
"stdin = sys.stdin.read() if os.environ.get('READ_STDIN') else None\n"
769+
f"open({str(received)!r}, 'w').write(json.dumps({{'argv': sys.argv[1:], 'stdin': stdin}}))\n"
770+
)
771+
opencode.chmod(0o755)
772+
agent = _make_agent(
773+
command=str(opencode),
774+
verbatim_prompt=verbatim_prompt,
775+
env={"READ_STDIN": "1"} if verbatim_prompt else {},
776+
repo_dir=str(tmp_path / "repo"),
777+
)
778+
prompt = "Run `python3 -c \"print(1)\"`, then\n-- report $HOME and 'quotes'."
779+
780+
with (
781+
patch.object(agent, "_workspace_root", return_value=tmp_path / "workspace"),
782+
patch(
783+
"responses_api_agents.opencode_agent.app.parse_opencode_session",
784+
return_value=([], {"input_tokens": 0, "output_tokens": 0}),
785+
),
786+
):
787+
await agent._run_opencode(prompt, None, collect_observations=False)
788+
789+
received_call = json.loads(received.read_text())
790+
assert received_call["argv"][:2] == ["run", "-m"]
791+
if verbatim_prompt:
792+
assert prompt not in received_call["argv"]
793+
assert received_call["stdin"] == prompt
794+
else:
795+
assert received_call["argv"][-1] == prompt

‎responses_api_agents/opencode_sandboxed_agent/README.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ checksum before use. Gym uploads it through the sandbox file API and installs it
4747
alongside OpenCode before execution; no sandbox internet access or root package
4848
installation is needed. The default is `null`, preserving existing behavior.
4949

50+
## Verbatim prompts
51+
52+
By default the prompt is the last `opencode run` argument. `opencode run` wraps an argument containing
53+
spaces in double quotes and escapes inner quotes ([anomalyco/opencode#43923](https://github.com/anomalyco/opencode/issues/43923)),
54+
so the model sees `"<prompt>"` with every `"` as `\"`. Set `verbatim_prompt: true` to upload the prompt to the
55+
sandbox and pass it on stdin, which `opencode run` uses unchanged. It is off by default because it changes the
56+
model's input: results are not comparable with runs made without it.
57+
5058
## Offline scientific evaluation with OpenCode or Pi
5159

5260
The dedicated `opencode_sandboxed_agent` and `pi_sandboxed_agent` run their native

‎responses_api_agents/opencode_sandboxed_agent/app.py‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from copy import deepcopy
2323
from pathlib import Path
2424
from shlex import quote
25+
from tempfile import TemporaryDirectory
2526
from time import time
2627
from traceback import format_exc
2728
from typing import Any, Dict, List, Literal, Optional
@@ -434,6 +435,15 @@ class OpenCodeSandboxedAgentConfig(BaseResponsesAPIAgentConfig):
434435
tool_servers: List[ResourcesServerRef] = Field(default_factory=list)
435436
artifacts_dir: Optional[str] = None
436437
opencode_model_call_timeout: Optional[int] = None
438+
verbatim_prompt: bool = Field(
439+
default=False,
440+
description=(
441+
"Send the task prompt to `opencode run` on stdin instead of as a command-line argument. "
442+
"OpenCode wraps an argument containing spaces in double quotes and escapes inner quotes "
443+
"(anomalyco/opencode#43923), so the model otherwise sees an altered prompt. Off by default "
444+
"so existing results stay comparable."
445+
),
446+
)
437447

438448
# Sandbox config
439449
sandbox_provider: str
@@ -860,6 +870,12 @@ async def responses(
860870
# and there is no way to set it to null.
861871
# Here, we set an exorbitantly high number that cannot ever be reached.
862872
# In future versions of OpenCode, this can be directly passed via maxOutputTokens in the limit config above https://github.com/anomalyco/opencode/blob/1b18a50418f730aca32630ccfcde850f2b5fc360/packages/opencode/src/provider/transform.ts#L1418
873+
prompt_arg = f"-- {quote(query)}"
874+
prompt_remote_fpath = None
875+
if self.config.verbatim_prompt:
876+
prompt_remote_fpath = f"/tmp/nemo-gym-prompt-{uuid4().hex}.txt"
877+
prompt_arg = f"< {quote(prompt_remote_fpath)}"
878+
863879
command = f"""
864880
echo "Shell: $SHELL" \
865881
&& {install_str} \
@@ -869,13 +885,18 @@ async def responses(
869885
&& echo "Installed OpenCode" \
870886
&& rm -f /tmp/nemo-gym-mcp-setup-error \
871887
&& NEMO_GYM_REQUIRED_MCP_SERVERS={quote(json.dumps([s.name for s in self.config.tool_servers]))} OPENCODE_CONFIG_CONTENT={quote(opencode_config_content)} OPENCODE_EXPERIMENTAL_OUTPUT_TOKEN_MAX=1000000000 {xdg_home_str} \
872-
opencode run --title "NG dummy title" {opencode_debug_str} {opencode_thinking_str} -- {quote(query)} \
888+
opencode run --title "NG dummy title" {opencode_debug_str} {opencode_thinking_str} {prompt_arg} \
873889
&& echo "OpenCode run finished"
874890
"""
875891

876892
if self.config.debug:
877893
print("Starting OpenCode (runtime configuration omitted to protect credentials)", file=sys.stderr)
878894

895+
if prompt_remote_fpath is not None:
896+
with TemporaryDirectory() as directory:
897+
prompt_local_fpath = Path(directory) / "prompt.txt"
898+
prompt_local_fpath.write_text(query, encoding="utf-8")
899+
await sandbox.upload(prompt_local_fpath, prompt_remote_fpath)
879900
if ripgrep_remote_path is not None:
880901
await sandbox.upload(self.config.local_ripgrep_binary_path, ripgrep_remote_path)
881902

‎responses_api_agents/opencode_sandboxed_agent/tests/test_app.py‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,3 +1346,79 @@ def test_assistant_message_plugin_hooks() -> None:
13461346
check=True,
13471347
timeout=30,
13481348
)
1349+
1350+
1351+
@mark.parametrize("verbatim_prompt", [False, True])
1352+
async def test_prompt_delivery_to_opencode(tmp_path: Path, monkeypatch: MonkeyPatch, verbatim_prompt: bool) -> None:
1353+
"""Run the generated launch command against a stand-in `opencode` that records what it receives."""
1354+
config = TestOpenCodeSandboxedAgent()._create_config()
1355+
config.preinstalled_opencode = True
1356+
config.opencode_version = "test"
1357+
config.artifacts_dir = str(tmp_path / "artifacts")
1358+
config.verbatim_prompt = verbatim_prompt
1359+
server = OpenCodeSandboxedAgent(config=config, server_client=MagicMock(spec=ServerClient))
1360+
uploads: Dict[str, bytes] = {}
1361+
1362+
async def upload(source: Any, target: str) -> None:
1363+
uploads[target] = Path(source).read_bytes()
1364+
1365+
sandbox = MagicMock(
1366+
upload=AsyncMock(side_effect=upload),
1367+
download=AsyncMock(),
1368+
exec=AsyncMock(
1369+
side_effect=[
1370+
SimpleNamespace(stdout="Shell: sh\nOpenCode run finished", stderr="", return_code=0, error_type=None),
1371+
RuntimeError("no session to export"),
1372+
]
1373+
),
1374+
)
1375+
monkeypatch.setattr(server, "_sandbox_id_to_sandbox", {"": sandbox})
1376+
monkeypatch.setattr(server, "_create_opencode_config", AsyncMock(return_value={}))
1377+
prompt = "Run `python3 -c \"print(1)\"`, then\n-- report $HOME and 'quotes'."
1378+
1379+
await server.responses(
1380+
request=MagicMock(session={SESSION_ID_KEY: "s"}, cookies={"sandbox_id": ""}, state=SimpleNamespace()),
1381+
body=NeMoGymResponseCreateParamsNonStreaming(input=[{"role": "user", "content": prompt}]),
1382+
)
1383+
1384+
command = sandbox.exec.await_args_list[0].kwargs["command"]
1385+
prompt_paths = [path for path in uploads if path.startswith("/tmp/nemo-gym-prompt-")]
1386+
home = tmp_path / "home"
1387+
bin_dir = home / ".opencode/bin"
1388+
bin_dir.mkdir(parents=True)
1389+
received = tmp_path / "received.json"
1390+
opencode = bin_dir / "opencode"
1391+
opencode.write_text(
1392+
f"#!{sys.executable}\n"
1393+
"import json, sys\n"
1394+
"if sys.argv[1:] == ['--version']:\n print('test'); sys.exit(0)\n"
1395+
f"stdin = sys.stdin.read() if {verbatim_prompt} else None\n"
1396+
f"open({str(received)!r}, 'w').write(json.dumps({{'argv': sys.argv[1:], 'stdin': stdin}}))\n"
1397+
)
1398+
opencode.chmod(0o755)
1399+
local_command = command.replace("/tmp/nemo-gym-mcp-setup-error", shlex.quote(str(tmp_path / "mcp-error")))
1400+
if verbatim_prompt:
1401+
[prompt_path] = prompt_paths
1402+
local_prompt = tmp_path / "prompt.txt"
1403+
local_prompt.write_bytes(uploads[prompt_path])
1404+
local_command = local_command.replace(prompt_path, shlex.quote(str(local_prompt)))
1405+
else:
1406+
assert prompt_paths == []
1407+
completed = subprocess.run(
1408+
["sh", "-c", local_command],
1409+
env={"HOME": str(home), "PATH": f"{bin_dir}{os.pathsep}{os.defpath}"},
1410+
stdin=subprocess.DEVNULL,
1411+
capture_output=True,
1412+
text=True,
1413+
timeout=10,
1414+
)
1415+
assert completed.returncode == 0, completed.stderr
1416+
1417+
argv = json.loads(received.read_text())
1418+
if verbatim_prompt:
1419+
# No message argument: `opencode run` uses stdin verbatim instead of re-quoting arguments.
1420+
assert argv == {"argv": ["run", "--title", "NG dummy title", "--thinking"], "stdin": prompt}
1421+
assert prompt not in command
1422+
else:
1423+
# Unchanged default: the prompt is the final argument, which `opencode run` wraps in quotes.
1424+
assert argv == {"argv": ["run", "--title", "NG dummy title", "--thinking", "--", prompt], "stdin": None}

0 commit comments

Comments
 (0)