Skip to content

[Bug]: DockerSandbox corrupts complex sh -c commands on Windows #2135

Open

Description

@richuV

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_file fails for Docker sandbox on Windows.
  • edit_file and 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.

Activity

  1. richuV commented on Jul 12, 2026

    @richuV
    Author

    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.
  2. added
    bug
    area/harness
    on Jul 12, 2026
  3. richuV commented on Jul 12, 2026

    @richuV
    Author

    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.

  4. moved this from Backlog to In progress in AgentScope Javaon Jul 12, 2026
  5. oss-maintainer commented on Jul 14, 2026

    @oss-maintainer
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

    area/harnessbug

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions