Skip to content

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

Open

Description

@BeginnerCong

Describe the bug

On Windows, DockerSandbox.doExec() fails for any shell command containing nested double quotes (e.g. mkdir -p "$(dirname ...)", python3 -c "..."). This breaks write_file and edit_file tools entirely.

Root cause: ProcessBuilder on Windows goes through cmd.exe /c, which strips inner double quotes from the sh -c argument. Linux/macOS are unaffected (no cmd.exe).

To Reproduce

// Exact command from BaseSandboxFilesystem.write() line ~165
String cmd = "if [ -e 'test.txt' ]; then echo 'EXISTS'; exit 1; fi; "
    + "mkdir -p \"$(dirname 'test.txt')\" 2>&1";

// Current doExec() approach
ProcessBuilder pb = new ProcessBuilder(
    "docker", "exec", "-w", "/workspace", containerId, "sh", "-c", cmd);
Process p = pb.start();
p.waitFor(30, TimeUnit.SECONDS);
System.out.println(p.exitValue()); // Windows: 2, Linux: 0

Run on Windows 11 + Docker Desktop → exit code 2, stderr: 'test.txt') 2>&1: 1: Syntax error: end of file unexpected (expecting ")")

Expected behavior

Exit code 0 on all platforms.

Error messages

Windows exit code 2, stderr:

'test.txt') 2>&1: 1: Syntax error: end of file unexpected (expecting ")")

edit_file (requires python3 image) stderr:

File "<string>", line 1
    import
          ^
SyntaxError: invalid syntax

Environment:

  • AgentScope-Java Version: 2.0.0
  • Java Version: 21
  • OS: Windows 11
  • Docker: Docker Desktop with Linux containers

Additional context

  • doHydrateWorkspace() already uses docker exec -i (for tar stdin) and is unaffected.
  • Only doExec() is affected because it's the only method that passes a shell command via -c.
  • Suggested fix: pipe command through stdin instead of -c argument (see reproduction code above).

Activity

  1. added
    bug
    on Sep 1, 2026
  2. oss-maintainer commented on Sep 13, 2026

    @oss-maintainer
    Collaborator

    Issue Evaluation

    Category: bug | Status: Confirmed

    Verified against main @ 39cd304a.

    Root cause: DockerSandbox.doExec() passes the container command through the host process layer with no platform handling:

    • agentscope-harness/src/main/java/io/agentscope/harness/agent/sandbox/impl/docker/DockerSandbox.java:144-161 — builds ["docker","exec","-w",workspaceRoot,containerId,"sh","-c",command] and hands the List<String> straight to ProcessBuilder. A grep for os.name across the whole sandbox package tree returns zero matches.
    • On Windows the JDK joins that list into a single command line using the MSVCRT convention (inner " escaped as \"), and Docker Desktop's docker.cmd/docker.bat shim then routes it through cmd.exe /c, which does not understand \" and drops the inner quotes — so the sh -c payload arrives already mangled, producing exactly the Syntax error: end of file unexpected (expecting ")") you saw.
    • The payloads that break are generated by our own filesystem layer: BaseSandboxFilesystem.write() (:197-203) emits mkdir -p \"$(dirname …)\", and edit() (:258-281) emits python3 -c \"import sys, os, base64, json…\" — hence write_file and edit_file failing on every call.
    • Contrast: LocalFilesystemWithShell.java:334-338 already branches on os.name for cmd.exe vs sh -c; the Docker path never got the same treatment. Kubernetes / E2b / Daytona / AgentRun backends are unaffected (HTTP/API transports, no ProcessBuilder).

    Impact: with DockerSandbox on a Windows host, file creation and editing are completely broken for any command containing nested double quotes. Severity: high.

    Before we propose a fix — this issue already has three competing open PRs, and #2925 declares it is for this issue:

    PR Shape Touches Status
    #2925 fix(harness): preserve Docker exec commands on Windows (@guslegend0510) platform-specific handling in doExec DockerSandbox.java + new DockerSandboxExecTest.java OPEN, non-draft, MERGEABLE
    #2148 fix(harness): stream Docker exec commands through stdin (@richuV) option (1) below — docker exec -i … sh + write the script to stdin DockerSandbox.java + new DockerSandboxCommandTest.java OPEN, non-draft, mergeable=UNKNOWN
    #3084 fix(sandbox): use base64+stdin for edit() … (@gh-zhaoweifeng) option (2) below, applied at the BaseSandboxFilesystem call site only BaseSandboxFilesystem.java + test OPEN, non-draft, MERGEABLE

    #2925 and #2148 edit the same method and will conflict with each other; #3084 is orthogonal (call-site encoding, does not fix write()'s mkdir -p \"$(dirname …)\" payload or any other future doExec caller).

    Fix-direction recommendation for whoever consolidates: piping the script on stdin (the #2148 shape) is the only option that is correct on all hosts with no OS branch and no per-call-site encoding, since the command string then never traverses the host command-line parser at all:

    List<String> cmd = List.of("docker", "exec", "-i", "-w", workspaceRoot, containerId, "sh");
    Process p = new ProcessBuilder(cmd).start();
    try (OutputStream os = p.getOutputStream()) { os.write(command.getBytes(StandardCharsets.UTF_8)); }

    Base64-wrapping (sh -c 'echo <b64> | base64 -d | sh', the technique edit() already uses at :254-256) is a viable fallback if stdin streaming is undesirable for interactive/timeouts reasons.

    Because three open PRs already target this root cause, we are not filing a fourth — this window is for the maintainers to pick the seam. If you want us to contribute, the most useful non-duplicate action would be a host-side regression test that asserts the exact argv/stdin reaching docker exec on os.name=win, which none of the three currently proves end to end.


    Automated evaluation by github-manager-bot

  3. oss-maintainer commented on Sep 13, 2026

    @oss-maintainer
    Collaborator

    One precision fix to the table above

    The only PR that declares this issue is #2925 (Fixes #2924, with a Docker verification that reproduces the reported command). #2148 targets a different issue — Closes #2135 — and is listed here only because it changes the same doExec call path to the stdin-pipe shape, so it collides with #2925 and is the concrete implementation of the recommended direction. #2925 is currently the greenest of the three (License, Module Sync, build (ubuntu-latest), build (windows-latest), codecov, CLA all pass) and is MERGEABLE; it is waiting on review, not on CI.

    Nothing else in the evaluation changes, and we still are not filing a competing PR.

    Automated by github-manager-bot

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions