[Bug]: DockerSandbox corrupts complex sh -c commands on Windows
Summary
On a Windows host, DockerSandbox#doExec passes the full shell program as the last argument of:
docker exec -w <workspaceRoot> <containerId> sh -c <command>
Commands containing command substitution and embedded quotes can be corrupted while crossing Java ProcessBuilder -> docker.exe -> container sh -c. This currently makes the standard Harness write_file path fail even for a tiny new file.
This is related to #1560 (large file content embedded in the command line), but the reproduction here is only a short preflight command and fails because of quoting, not command length.
Environment
- Windows host
- Java 17
- Docker Desktop 29.2.0
- current
main/ AgentScope Java 2.0 Harness - image:
ubuntu:24.04
Minimal reproduction
Create and start an official Docker sandbox, then attach it to SandboxBackedFilesystem and call:
SandboxBackedFilesystem filesystem = new SandboxBackedFilesystem();
filesystem.setSandbox(sandbox);
WriteResult result = filesystem.write(
runtimeContext,
"filesystem-contract.txt",
"jcode-filesystem-contract");
Expected:
WriteResult.success
Actual:
Failed to write file 'filesystem-contract.txt'
Calling the exact preflight command directly exposes the hidden error:
sandbox.exec(
runtimeContext,
"if [ -e 'filesystem-contract.txt' ]; then echo 'EXISTS'; exit 1; fi; "
+ "mkdir -p \"$(dirname 'filesystem-contract.txt')\" 2>&1",
60);
Actual container shell error:
Command exited with code 2: 'filesystem-contract.txt') 2>&1: 1: Syntax error: end of file unexpected (expecting ")")
The same shell program succeeds when invoked manually inside the same ubuntu:24.04 image.
Source path
BaseSandboxFilesystem.write() creates the preflight:
String checkCmd =
"if [ -e " + escapedPath + " ]; then echo 'EXISTS'; exit 1; fi; "
+ "mkdir -p \"$(dirname " + escapedPath + ")\" 2>&1";
DockerSandbox#doExec() then appends that program directly as a Windows process argument:
cmd.add("sh");
cmd.add("-c");
cmd.add(command);
new ProcessBuilder(cmd).start();
Docker events show the sh -c command reaching the container with damaged quoting. BaseSandboxFilesystem.write() also replaces the real preflight output with the generic Failed to write file message, which initially hides the cause.
Impact
- Standard Harness
write_filefails for Docker sandbox on Windows. edit_fileand other complex shell-backed filesystem operations may be affected.- Redis/MySQL cross-replica sandbox recovery cannot complete because the writer phase cannot create the workspace file.
- The failure is unrelated to Redis, snapshot persistence, or Windows host path mapping; it also reproduces in a standalone official Docker sandbox without distributed storage.
Suggested direction
Avoid transporting a shell program as a complex docker.exe command-line argument on Windows. A platform-independent approach is to stream the program through stdin, for example:
docker exec -i -w <workspaceRoot> <containerId> sh -s
and write the program to process stdin. This direction also aligns with #1560 and avoids both quoting corruption and OS command-line length limits.
Separately, preserve checkResult.output() and exit code in the BaseSandboxFilesystem.write() preflight failure so future backend failures remain diagnosable.
I can prepare a focused PR with Windows/Linux tests if maintainers agree with the stdin execution direction.
I can work on a focused fix for this. The proposed change is to run container commands as docker exec -i -w <root> <container> sh -s and stream the shell program through stdin instead of placing it in the host process argument list.
This should address both the Windows nested-quoting corruption reported here and the command-line size failure in #1560 without adding a docker-java dependency or changing the public Sandbox API.
I will include:
- a unit contract proving the shell program is absent from ProcessBuilder arguments;
- complex command coverage for nested quotes and
$(dirname ...); - a large-script regression;
- real Docker verification on Windows;
- focused Harness tests and Spotless.
Fix proposed in #2148.
Verified on Windows with Docker Desktop:
- upstream real Docker complex command and 100KB script: 3/3;
- downstream official Docker provider lifecycle: 1/1;
- downstream Redis + Docker fresh-JVM writer/reader: 1/1 each.
JCode does not copy the Docker wrapper or pin a private AgentScope version; it will consume the fix after it is merged into an official AgentScope build.
🤖 AI Assessment: Bug Report
Classification: Bug (area/harness, docker, windows)
Analysis: This is a valid bug. DockerSandbox corrupts complex sh -c commands on Windows due to command-line length limits and escaping issues.
Related PR: #2148 (fix(harness): stream Docker exec commands through stdin) fixes this issue.
Next Steps: Review and merge #2148.
Automated assessment by github-manager