Skip to content

fix(harness): stream Docker exec commands through stdin - #2148

Open
richuV wants to merge 1 commit into
agentscope-ai:mainfrom
richuV:codex/docker-stdin-exec-2135
Open

richuV wants to merge 1 commit into
agentscope-ai:mainfrom
richuV:codex/docker-stdin-exec-2135

Conversation

@richuV

@richuV richuV commented Jul 12, 2026

Copy link
Copy Markdown

Summary

  • run Docker sandbox shell programs through docker exec -i ... sh -s
  • keep shell text out of the host process argument list
  • preserve UTF-8 complex commands and avoid Windows command-line length limits
  • add unit contracts plus an opt-in real Docker regression test

Why

docker exec ... sh -c <command> puts the complete shell program in the Windows CreateProcess argument list. Complex nested quoting can be corrupted, and large filesystem scripts can exceed the host command-line limit.

The public Sandbox API and command semantics remain unchanged; only the Docker CLI transport changes.

Tests

  • mvn -pl agentscope-harness spotless:check test -Dtest=DockerSandboxCommandTest,BaseSandboxFilesystemTest,HarnessAgentDistributedSandboxTest -Dsurefire.failIfNoSpecifiedTests=false
    • 16 tests, 12 passed, 4 environment-gated skipped
  • AGENTSCOPE_DOCKER_INTEGRATION=true mvn -pl agentscope-harness test -Dtest=DockerSandboxCommandTest
    • 3/3 passed on Windows with Docker Desktop
    • covers nested quotes, $(dirname ...), paths with spaces, and a 100KB shell program
  • downstream JCode acceptance against the patched 2.0.1-SNAPSHOT
    • real Docker provider lifecycle 1/1
    • Redis + Docker fresh-JVM writer 1/1
    • Redis + Docker fresh-JVM reader 1/1

Closes #2135.
Addresses #1560.

@richuV
richuV requested a review from a team July 12, 2026 01:04
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


u010143241 seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

@codecov

codecov Bot commented Jul 12, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 27.27273% with 8 lines in your changes missing coverage. Please review.

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

📢 Thoughts on this report? Let us know!

@AgentScopeJavaBot AgentScopeJavaBot 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.

🤖 AI Review

Changes Docker sandbox command execution from sh -c to sh -s (stdin pipe), solving Windows command-line length limits, UTF-8 escaping issues, and process argument exposure. Well-scoped change (2 files, +108/-12) with comprehensive tests. Clean implementation, no issues found.

@AgentScopeJavaBot AgentScopeJavaBot added bug Something isn't working area/harness agentscope-harness (test/runtime support) labels Jul 12, 2026
@oss-maintainer

This comment was marked as abuse.

@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

Streams the shell program into docker exec -i ... sh -s instead of embedding it in sh -c <command>, which is the right fix for the host argv limits and quoting problems with agent-generated scripts, and the helper extraction + integration-test gate are sensible. Findings above are about the unbounded stdin write (the one real risk), stdin re-direction semantics for nested commands, and the fact that the unit tests do not cover the wiring they appear to cover.

Note: the license/cla check is still pending, so this review is a COMMENT only — please sign the CLA (see reminder) before a maintainer looks at merging.


Automated review by github-manager-bot

drainer.shutdown();

try (OutputStream stdin = process.getOutputStream()) {
writeShellProgram(stdin, command);

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.

[Warning] writeShellProgram runs on the calling thread before process.waitFor(timeoutSeconds, ...), so the per-call timeout does not bound the stdin write. For a large shell program (this PR's own test uses ~100 KB) the pipe fills once docker exec -i stops draining it — e.g. the daemon is slow, the exec is failing/retrying, or the container is being killed — and stdin.write(...) can block long past timeoutSeconds while the output drainers just sit idle. Writing stdin from the same drainer executor (or a dedicated single-use future bounded by timeoutSeconds) keeps the documented timeout guarantee; the current shape turns a bounded doExec into a potentially unbounded one.

}

static List<String> buildExecCommand(String containerId, String workspaceRoot) {
return List.of("docker", "exec", "-i", "-w", workspaceRoot, containerId, "sh", "-s");

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.

[Info] Adding -i also re-points the container's stdin at the script, so any command inside the program that reads stdin (git, mysql, ssh, a bare read, cat with no file) now consumes the remaining script bytes instead of hitting an immediate EOF like it did with the previous sh -c form. For agent-generated tool calls that is a silent truncation risk. If the goal is only to keep argv small, docker exec -i <cid> sh -s -- </dev/null-style redirection is not possible from the host side, so either document the caveat in the class javadoc above or drain the script into a file first (sh -c 'cat > /tmp/x && sh /tmp/x') — otherwise worth a comment noting the tradeoff was considered.

}

static void writeShellProgram(OutputStream stdin, String command) throws IOException {
stdin.write(command.getBytes(StandardCharsets.UTF_8));

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.

[Info] No trailing newline is appended. sh -s executes the last line without one in practice (dash/bash both handle EOF-without-LF), but callers building a program by concatenation are one printf away from a silently dropped final command; stdin.write((command + "\n").getBytes(...)) costs nothing and makes the byte-for-byte assertion in the test explicit about what the shell actually receives.

ByteArrayOutputStream stdin = new ByteArrayOutputStream();
DockerSandbox.writeShellProgram(stdin, script);

assertEquals(8, command.size());

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.

[Warning] Both unit tests exercise the two new static helpers directly — no process is started — so they would still pass if doExec forgot to call writeShellProgram or kept the old sh -c argument list; the only test that covers the real wiring is @EnabledIfEnvironmentVariable(AGENTSCOPE_DOCKER_INTEGRATION), which CI never runs. A cheap way to close the gap without Docker: spawn sh -s locally through the same code path (or expose the write behind a small seam) and assert stdout/exit code, so the regression this PR fixes stays fixed.

This branch has not been deployed

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

Labels

area/harness agentscope-harness (test/runtime support) bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

4 participants