Skip to content

fix(sandbox): keep Kubernetes wrap newlines around command - #3204

Merged
Buktal merged 3 commits into
agentscope-ai:mainfrom
tengjiaozhai:fix/k8s-sandbox-exec-heredoc-wrap
Sep 20, 2026
Merged

Buktal merged 3 commits into
agentscope-ai:mainfrom
tengjiaozhai:fix/k8s-sandbox-exec-heredoc-wrap

Conversation

@tengjiaozhai

@tengjiaozhai tengjiaozhai commented Sep 18, 2026 •

Copy link
Copy Markdown
Contributor

AgentScope-Java Version

2.0.3-SNAPSHOT (based on upstream/main at 6c8ccc5)

Description

KubernetesSandbox.doExec wrapped commands as cd <root> && (command) with the closing ) concatenated onto the last line of the command. When that last line was a heredoc terminator (e.g. EOF), the shell saw EOF) instead of EOF, so the delimiter never matched and dash reported Syntax error: end of file unexpected (expecting ")").

This change puts the subshell delimiters on their own lines:

cd <root> && (
<command>
)

Only the Kubernetes backend wraps this way; Docker / local / AgentRun paths are unchanged.

Closes #3198

Verification

  • mvn spotless:apply / mvn spotless:check for the changed sandbox-kubernetes module: passed
  • mvn -pl agentscope-extensions/agentscope-extensions-sandbox/agentscope-extensions-sandbox-kubernetes -am test -Dtest=KubernetesSandboxTest: 11 tests, 0 failures
  • Updated the existing wrap-shape assertion and added doExecKeepsClosingParenOffHeredocTerminatorLine

Checklist

  • Code has been formatted with mvn spotless:apply for the changed module.
  • Targeted tests are passing (KubernetesSandboxTest).
  • Related documentation has been updated (not applicable; no public API change).
  • Code is ready for review.

@codecov

codecov Bot commented Sep 19, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 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

Minimal, correct fix for the heredoc/#-comment breakage in the Kubernetes sandbox exec path: the closing ) is moved onto its own line, so a command whose last line is a heredoc terminator (or ends in a trailing comment / line continuation) is no longer glued to ). Verdict: approve — the two items below are non-blocking.

I replayed both wrap shapes against dash and bash with the same grammar the runtime uses (sh -c '<cd> && (<command>)'). The new shape fixes exactly the class reported in #3198 and also repairs two shapes the old wrap broke — a command ending in # comment and one ending in a trailing \ — with no behaviour change for single-line commands, trailing newlines, multi-line if/fi blocks, background &, or commands containing quoted parens. So this is a strict improvement, not a swap of one broken case for another.

Scope is right too: the wrap only exists in KubernetesSandbox.doExec — Docker passes sh -c <command> with -w <workspaceRoot>, AgentRun hands the command to mcp.exec, Daytona and E2b take a cwd — so the "only the Kubernetes backend changes" claim in the description holds. Repo-wide grep finds no remaining consumer of the old single-line shape outside the two places this PR updates. CI is green (build ubuntu/windows, Check Module Sync, Check License, codecov patch) and license/cla is signed.

Findings

  • [Warning] KubernetesSandbox.java:92 — a null/blank command still reaches the shell and fails as an opaque syntax error under both the old and the new shape; not a regression, just an opportunity to make the contract explicit while this line is open.
  • [Info] KubernetesSandboxTest.java:96 — assertTrue(!…) → assertFalse(…), and consider pinning the two additional shapes the newline wrap repairs (trailing # comment, trailing \) so a future refactor of the wrap cannot silently re-break them.

Suggestions

If you want the blank-command guard, the narrow version keeps the exception type the harness already maps:

String wrapped =
        "cd " + shellQuote(k8sState.getWorkspaceRoot()) + " && (\n" + command + "\n)";

→

if (command == null || command.isBlank()) {
    throw new SandboxException.ExecException(2, "", "empty command rejected by KubernetesSandbox");
}
String wrapped =
        "cd " + shellQuote(k8sState.getWorkspaceRoot()) + " && (\n" + command + "\n)";

A @ParameterizedTest over the last line of the payload (heredoc terminator, # comment, trailing \, plain command) would cover the whole class in one place.


Automated review by github-manager-bot

throws Exception {
String wrapped = "cd " + shellQuote(k8sState.getWorkspaceRoot()) + " && (" + command + ")";
String wrapped =
"cd " + shellQuote(k8sState.getWorkspaceRoot()) + " && (\n" + command + "\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.

[Warning] Non-blocking, but worth deciding on while this line is open. The new shape still produces a shell syntax error for a null/blank command — cd '/workspace' && (\n\n) fails with syntax error near unexpected token ')' in both dash and bash. That is unchanged from the old wrap, so it is not a regression; it just means an empty command surfaces as an opaque sh parse error instead of a clear failure.

Since this PR is the one that owns the wrap shape, a cheap guard would make the contract explicit:

if (command == null || command.isBlank()) {
    throw new SandboxException.ExecException(2, "", "empty command rejected before exec");
}

(Or simply skip the subshell for blank input.) Happy to see this handled in a follow-up — it does not affect the fix here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Applied: null/blank command now throws SandboxException.ExecException(2, "", "empty command rejected by KubernetesSandbox") before the wrap.

assertEquals("cd '/workspace' && (\n" + command + "\n)", wrapped);
// Closing ')' must not glue onto the heredoc terminator.
assertTrue(wrapped.endsWith("\nEOF\n)"));
assertTrue(!wrapped.contains("EOF)"));

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] Two small test nits:

  1. assertTrue(!wrapped.contains("EOF)")) reads better as assertFalse(...) — the failure message then states the positive condition that broke.
  2. The newline wrap actually repairs more than the heredoc case: echo hi # note (the old form let the # comment swallow the closing )) and a command ending in a trailing \ continuation both failed before and pass now. Those two shapes are the reason I would not switch to a ;-appending variant instead. Consider folding them into this test (a @ParameterizedTest over last-line payloads would keep it compact) so the broader guarantee is pinned and a later refactor of the wrap cannot silently re-break them.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Applied: switched to assertFalse, and expanded into a @ParameterizedTest over last-line payloads (heredoc terminator / # comment / trailing \ / plain) asserting the closing ) stays on its own line. Also added doExecRejectsNullOrBlankCommand.

Address oss-maintainer review on agentscope-ai#3204: guard null/blank command before wrap,
use assertFalse, and parameterize last-line wrap shapes (heredoc / comment / trailing \ / plain).
@tengjiaozhai

Copy link
Copy Markdown
Contributor Author

Applied review feedback from @oss-maintainer on fee113e:

  1. Blank-command guard in KubernetesSandbox.doExec — null/blank command throws SandboxException.ExecException with "empty command rejected by KubernetesSandbox" before wrapping.
  2. Tests — assertFalse(wrapped.contains(...)); @ParameterizedTest over last-line shapes (heredoc terminator / # comment / trailing \ / plain); plus doExecRejectsNullOrBlankCommand.

KubernetesSandboxTest: 15 tests, 0 failures. spotless:check clean.

@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-reviewed the new commit since my approval (0d96e96 → fee113e): the incremental change rejects null/blank commands in KubernetesSandbox.doExec with a clear ExecException (stderr message) and converts the newline-wrapping assertions into a parameterized test covering heredoc, trailing-comment, trailing-backslash and plain commands. The wrap contract (closing paren on its own line) is now asserted generically instead of the heredoc-only case. All 6 checks pass on this head. LGTM — the added guard makes failed-exec surfacing unambiguous and the test matrix covers the previously fragile edge cases.


Automated review by github-manager-bot

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

LGTM

@Buktal
Buktal merged commit c767160 into agentscope-ai:main Sep 20, 2026
6 checks passed
@tengjiaozhai

Copy link
Copy Markdown
Contributor Author

Thanks for the review! I didn’t realize you had merge access 😄

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]: execute tool fails on any command whose last line is a heredoc terminator

3 participants