Repository navigation
Description
Describe the bug
Any command sent through the execute tool (ShellExecuteTool) fails if its last line is a heredoc terminator. The command is valid shell and works verbatim when run by hand. The error that comes back is a shell syntax error that mentions neither the heredoc nor its delimiter, so it reads as "heredocs are unavailable in the sandbox".
Something is appended to the command's final line before it reaches the shell. When that line is the closing delimiter, the delimiter stops matching, the heredoc consumes the rest of the input, and the shell reports an unterminated construct at the last line of the content.
This is the same family as the transport/escaping bugs already fixed or reported here — #2925 (Windows docker exec), #3084 (bash -lc escaping in edit()), #2618 (base64 payload in argv) — but it needs no large payload and no Windows: it reproduces with a three-line heredoc on Linux.
To Reproduce
- Call the
executetool with a command whose last line is the terminator:
cd /workspace/runs/1/work && python3 - <<'EOF'
import json
d = {'a': 1}
for k in d:
if k in ('SRC-a','SRC-b','SRC-c'):
print(k)
print("done")
EOF
- The call fails. The same command with one trailing newline, or with any command after the terminator (
wc -c file), succeeds.
Where the appended text lands can be reproduced exactly outside the framework. Wrapping the command in ( … ) with the closing paren concatenated onto the end of the string reproduces the error string, wording and line number exactly, while two other plausible appendages do not:
import subprocess
lines = ["cd /tmp && python3 - <<'EOF'", "import json",
"with open('/dev/null') as f:", " pass", "d = {'a': 1}", "for k in d:",
" if k in ('SRC-a','SRC-b','SRC-c'):", " print(k)",
"with open('/dev/null') as f:", " pass", 'print("done")', "EOF"]
cmd = "\n".join(lines)
for label, wrapped in [
("no wrapper", cmd),
("( … ) closing paren on its own line", "( " + cmd + "\n)"),
("( … ) closing paren appended to the last line", "( " + cmd + " )"),
("`< /dev/null` appended", cmd + " < /dev/null"),
("`; echo EXIT:$?` appended", cmd + " ; echo EXIT:$?"),
]:
r = subprocess.run(["/bin/dash", "-c", wrapped], capture_output=True, text=True)
print(label, "->", r.returncode, (r.stderr or r.stdout).strip().splitlines()[:1])no wrapper -> 0 ['done']
( … ) closing paren on its own line -> 0 ['done']
( … ) closing paren appended to the last line -> 2 ['/bin/dash: 12: Syntax error: end of file unexpected (expecting ")")']
`< /dev/null` appended -> 1 [' File "<stdin>", line 11']
`; echo EXIT:$?` appended -> 1 [' File "<stdin>", line 11']
Only the third matches what the tool returns, so the appended text is on the command's last line rather than on a new one.
Expected behavior
execute advertises "Execute a shell command". A command ending in a heredoc terminator is a shell command and should run. Either the command should not be modified, or whatever is appended should go on its own line.
Error messages
What the tool returned for the command above:
Exit code: 2
/bin/sh: 12: Syntax error: end of file unexpected (expecting ")")
Line 12 is the EOF line. Nothing in the message points at the heredoc, the delimiter, or the appended text.
Environment
- AgentScope-Java Version: 2.0.2 (
agentscope-harness,agentscope-core) - Java Version: 17
- OS: Linux (sandbox container);
/bin/shis dash, judging by thesh: <line>: Syntax errorformat - Sandbox: Kubernetes (
agentscope-extensions-sandbox-kubernetes, agent-sandbox)
Additional context
Where the modification happens, as far as I could trace it: ShellExecuteTool.execute passes command through unchanged unless working_directory is set; SandboxBackedFilesystem.execute forwards it to Sandbox.exec; CommandExecutor POSTs {"command": …} to the sandbox's /execute endpoint. I found no ( … ) wrapping in agentscope-harness or in the Kubernetes client, so it looks server-side — but the tool is the surface where it is observable, and a one-line defence here would cover every sandbox backend:
// ShellExecuteTool.execute, before handing the command over
String effectiveCommand = command.endsWith("\n") ? command : command + "\n";That is the same shape of defence as #3084 (base64 + stdin rather than relying on the shell's quoting). A server-side fix (append on a new line, or do not wrap) would be the real one; the trailing newline works for the behaviour observed above and would stop being needed once that lands.
Why this is worth more than the syntax error suggests. The failure teaches the wrong lesson. The error names nothing related to heredocs and the identical command works by hand, so an agent that hits it concludes heredocs are unavailable and switches to write_file a script, then execute the script — two tool calls for every one, for the rest of the run. In one 25-minute run of ours the agent did that three times after a single failure of this kind. It also removes cat >> file <<'EOF' as a way to build a file larger than one tool call can carry, which pushes the model toward writing many small files plus a merge script instead.
Activity
oss-maintainer commentedon Sep 18, 2026
Issue Evaluation
Category:
bug| Status: Confirmed (reproduced by code inspection onmain@dc15ff42c; also present in v2.0.2)Root cause — the wrapping IS in this repo (client side), and it is Kubernetes-specific.
KubernetesSandbox.doExecconcatenates the closing parenthesis directly onto the end of the command string:When the command's last line is a heredoc terminator, the line the shell sees is
EOF)instead ofEOF, the delimiter never matches, the heredoc swallows the rest of the input, anddashfails at the last line withSyntax error: end of file unexpected (expecting ")")— the exact wording and line number reported here. Your( … )"closing paren appended to the last line" experiment matches the code path precisely.Blast radius: only the Kubernetes backend wraps. Other execution paths pass the command verbatim, which is why this is not observable with Docker/local/AgentRun:
KubernetesSandbox.java:91cd <root> && (cmd)← bugDockerSandbox.java:156(sh -c <command>)LocalFilesystemWithShell.java:338(sh -c <command>)AgentRunSandbox.java:119(mcp.exec(command, …))The
working_directorybranch inShellExecuteTool.java:64-98only prefixescd '<wd>' &&, so it is not the cause (and it does not help either: the closing)still lands on the terminator line).Suggested fix — put the subshell delimiters on their own lines, which fixes the defect at the place that creates it instead of masking it downstream:
The
command.endsWith("\n") ? … : + "\n"guard inShellExecuteTool.executewould hide the symptom for the Kubernetes path, but it changes the payload for every backend and leaves the wrapping bug in place, so it is better treated as optional hardening than as the fix.Note that
KubernetesSandboxTest.java:78currently pins the broken shape (assertEquals("cd '/workspace' && (echo hi)", …)); it needs updating together with the fix, plus a case whose command ends with a heredoc terminator asserting a newline before the closing).Severity: medium (scoped to the Kubernetes sandbox), high impact on agent reliability — as you wrote, the error message teaches the agent to abandon heredocs for the rest of the run.
An automated fix proposal will follow. Reply
/approveto proceed with PR generation,/revise <text>to change the approach, or/rejectto stop.Automated evaluation by github-manager-bot
fix(sandbox): keep Kubernetes wrap newlines around command (agentscop…