Skip to content

fix(harness): preserve Docker exec commands on Windows - #2925

Closed
guslegend0510 wants to merge 30 commits into
agentscope-ai:mainfrom
guslegend0510:codex/fix-2924-docker-exec-stdin
Closed

guslegend0510 wants to merge 30 commits into
agentscope-ai:mainfrom
guslegend0510:codex/fix-2924-docker-exec-stdin

Conversation

@guslegend0510

Copy link
Copy Markdown
Contributor

AgentScope-Java Version

2.0.3-SNAPSHOT

Description

Fixes #2924

DockerSandbox.doExec() previously passed the complete shell command as the final docker.exe argument (sh -c <command>). On Windows, nested double quotes can be altered while the Win32 command line is serialized and parsed, breaking filesystem commands such as write_file and edit_file.

This PR:

  • runs docker exec -i ... sh -s and streams the command through UTF-8 stdin instead of placing it in argv;
  • wraps the streamed command so command-level stdin remains isolated while heredocs and explicit redirections continue to work;
  • preserves existing stdout/stderr capture, output truncation, non-zero exit, and timeout behavior;
  • adds regression tests for nested quotes, Unicode, multiline heredocs, non-zero exits, timeouts, and output truncation.

Testing

  • mvn -pl agentscope-harness -am -Dtest=DockerSandboxExecTest -Dsurefire.failIfNoSpecifiedTests=false test
  • mvn -pl agentscope-harness -am test
  • mvn spotless:check
  • git diff --check
  • Docker Linux-container verification covering the Issue [Bug]:DockerSandbox.doExec() strips nested double quotes on Windows #2924 reproduction, file write, Python heredoc edit, and stdin isolation

Checklist

  • Code has been formatted with mvn spotless:apply
  • All tests are passing (mvn test)
  • Javadoc comments are complete and follow project conventions
  • Related documentation is not required; the implementation Javadoc was updated
  • Code is ready for review

@codecov

codecov Bot commented Sep 1, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 85.29412% with 5 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...rness/agent/sandbox/impl/docker/DockerSandbox.java 85.29% 5 Missing ⚠️

📢 Thoughts on this report? Let us know!

@oss-maintainer oss-maintainer left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary

Re-review of PR #2925 at its current head (automated review of the changes in this diff). 2 item(s) raised below; nothing here blocks the approach itself — details inline.

Findings

  • [Warning] agentscope-harness/src/main/java/io/agentscope/harness/agent/sandbox/impl/docker/DockerSandbox.java:185 — The stdin writer thread performs blocking I/O on a real OutputStream which is not interruptible by Thread.interrupt(). If the pipe buffer fills (unlikely for typical sandbox commands, but possible for very large generated scripts) and the process stalls, stdinFuture.cancel(true) in the timeout path will not unblock the writer. Consider adding a deadline to stdinFuture.get() in the success path (e.g. stdinFuture.get(5, TimeUnit.SECONDS)) to avoid an indefinite hang if the writer is stuck, or use a socket-based stream that supports async close.
  • [Info] agentscope-harness/src/main/java/io/agentscope/harness/agent/sandbox/impl/docker/DockerSandbox.java:228 — The {...} </dev/null wrapper intentionally severs stdin for inner commands to prevent them from consuming the script text fed to sh -s. This is a behavioral change from the old sh -c approach: commands like bare cat or read that previously would hang waiting for stdin will now silently receive EOF from /dev/null. Consider documenting this constraint in the class-level Javadoc or the wrapCommandForStdin Javadoc so callers are aware.

Notes

Inline comments are anchored to the diff; if a line anchor was out of range it is listed above instead.


Automated review by github-manager-bot

ioExecutor.submit(
() -> {
try (OutputStream stdin = process.getOutputStream()) {
stdin.write(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The stdin writer thread performs blocking I/O on a real OutputStream which is not interruptible by Thread.interrupt(). If the pipe buffer fills (unlikely for typical sandbox commands, but possible for very large generated scripts) and the process stalls, stdinFuture.cancel(true) in the timeout path will not unblock the writer. Consider adding a deadline to stdinFuture.get() in the success path (e.g. stdinFuture.get(5, TimeUnit.SECONDS)) to avoid an indefinite hang if the writer is stuck, or use a socket-based stream that supports async close.

*
* <p>The shell parses the complete compound command before applying the {@code /dev/null}
* redirection. Commands such as {@code read} therefore cannot consume the remaining script,
* while heredocs and explicit stdin redirections continue to work.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The {...} </dev/null wrapper intentionally severs stdin for inner commands to prevent them from consuming the script text fed to sh -s. This is a behavioral change from the old sh -c approach: commands like bare cat or read that previously would hang waiting for stdin will now silently receive EOF from /dev/null. Consider documenting this constraint in the class-level Javadoc or the wrapCommandForStdin Javadoc so callers are aware.

@oss-maintainer oss-maintainer left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary

Streaming the script through sh -s instead of sh -c <argv> is the right fix for Windows argv mangling, and wrapping it in { : ...; } </dev/null is a neat way to stop an inner read from swallowing the rest of the script while heredocs and explicit redirects keep working. Tests are thorough (nested quotes/unicode/heredoc byte-fidelity/exit-code/timeout/truncation).

Findings

  • [Warning] stdinFuture.get() on the success path can convert a successful exec into a thrown exception on EPIPE.
  • [Info] the group redirect also denies host stdin to inner commands — document the invariant.
  • [Info] no test for "process exits while the script is still being written".

Verdict

Nothing here is architecturally wrong; the [Warning] is a small robustness fix. Not approving in this round because the PR still has a merge conflict with main — rebase and ping for a re-review.


Automated review by github-manager-bot

awaitWriterIgnoringFailure(stdinFuture);
throw new SandboxException.ExecException(exitCode, stdout, stderr);
}
stdinFuture.get();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This unconditional get() turns a writer failure into an exec failure. If the child exits before draining the script (a large command, or one that ends early), the write fails with EPIPE, the Future completes exceptionally, and a command that exited 0 is reported as an exception instead of an ExecResult. The non-zero path already calls awaitWriterIgnoringFailure(stdinFuture) for exactly this reason — consider the same treatment here, since the exit code is the authoritative signal.

*/
static String wrapCommandForStdin(String command) {
String trailingNewline = command.endsWith("\n") ? "" : "\n";
return "{\n:\n" + command + trailingNewline + "} </dev/null\n";

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worth a note in the javadoc that the wrapper also means the inner commands never see host stdin (it is /dev/null for the whole group). Today nothing pipes data in from the host so this is a feature, but it is the kind of invariant that a future execWithStdin caller would trip over.

}

@Test
void destroysProcessAndClosesStdinOnTimeout() {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice seam with startExecProcess. One gap: no case where the process exits while the writer still has bytes pending (the EPIPE scenario above). A FakeProcess whose stdin sink rejects writes after exitValue() becomes available would pin down whatever behaviour you decide on here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]:DockerSandbox.doExec() strips nested double quotes on Windows

3 participants