agentscope-java icon indicating copy to clipboard operation
agentscope-java copied to clipboard

edit_file: the heredoc failure claimed in #3935 does not reproduce on cube-sandbox (PROTO and JSON)

Open chcodex opened this issue 1 month ago • 2 comments

Summary

#3935 replaced the heredoc Python script in BaseSandboxFilesystem.edit() with python3 -c "exec(base64...)", on the premise that E2B-compatible sandboxes (CubeSandbox) cannot carry a multi-line command: bash then exits 2 and edit_file never reaches Python. It also widened the fallback so that any result without a __RESULT__ marker retries through editToTransfer().

On a real CubeSandbox deployment we could not reproduce the failure. Multi-line commands, the exact pre-#3935 heredoc command, and heredocs up to 32 KiB (command length 32,903 bytes) all succeed under both codecs. This issue records the measurements, so the premise can be corrected rather than carried forward as fact.

Related: #3935 (merged as adf2dea27), #3933, #3934.

Environment

Item Value
Deployment a private cube-sandbox deployment (endpoint redacted; available to maintainers on request)
Template private template (ID redacted; available to maintainers on request)
Codec PROTO and JSON (both tested)
Repo state upstream/main @ adf2dea27 (the #3935 merge)
Driver E2bSandboxClient + SandboxBackedFilesystem (same path edit_file uses)
Probe temporary live test, run and deleted; not committed

Measurements

Identical results for PROTO and JSON:

Probe What it does Result
P1 cat <<'EOF' + line1 / line2 exit=0, stdout line1\nline2 → newlines preserved
P2 The exact pre-#3935 edit command (python3 - '<path>' '<b64old>' '<b64new>' false <<'__AGENTSCOPE_EDIT_PY__' …) exit=0, stdout __RESULT__{"argv1": "/home/user/p2.txt"} → old form works
P8 Heredoc 4 KiB / 32 KiB (command 4,229 / 32,903 bytes) exit=0, __RESULT__{"kib": 4} / {"kib": 32} → no truncation
P3 edit() on alpha\nbeta\ngamma\n, replace beta→BETA ok, occ=1, content alpha\nBETA\ngamma\n
P4 edit() with replaceAll=true on x x x\n ok, occ=3, content y y y\n
P5 edit() on a CRLF file ok, occ=1, od shows one\n2\n (normalized to LF, as designed)
P6 old/new containing newlines ok, occ=1
P7 Path containing a single quote (it's a file.txt) ok, occ=1

Raw output (PROTO; JSON identical):

PROBE STARTED codec=PROTO root=/home/user
PROBE P1-heredoc exit=0 stdout=line1\nline2\n stderr=
PROBE P2-old-heredoc exit=0 stdout=__RESULT__{"argv1": "/home/user/p2.txt"}\n stderr=
PROBE P3-edit-3935 ok=true occ=1 err=null content=alpha\nBETA\ngamma\n
PROBE P4-replaceAll ok=true occ=3 err=null content=y y y\n
PROBE P5-crlf ok=true occ=1 err=null od=0000000   o   n   e  \n   2  \n\n0000006\n
PROBE P6-multiline-param ok=true occ=1 err=null
PROBE P7-quoted-path ok=true occ=1 err=null
PROBE P8-heredoc-4KiB exit=0 stdout=__RESULT__{"kib": 4}\n cmdLen=4229
PROBE P8-heredoc-32KiB exit=0 stdout=__RESULT__{"kib": 32}\n cmdLen=32903

Why this matters

Heredocs are not an exotic technique: POSIX.1 defines here-documents (§2.7.4), and every common shell (bash, dash, ksh, zsh, BusyBox ash) supports them. The repository's own sandbox image contract requires them:

| Shell | POSIX-compatible sh ([ ], &&, pipes, redirection, heredocs) | every file tool, the execute tool |

So the heredoc form is inside the documented contract, and a transport that cannot carry newlines is outside it — and would break far more than edit_file: any multi-line command sent by the execute tool (heredoc, for, if) would fail the same way. Changing edit_file's script shape therefore works around the symptom for one call site without fixing the class of failure.

#3935's own test plan still has "在 CubeSandbox 环境中验证 edit_file 端到端" unchecked, i.e. the premise was never verified against the environment it describes.

Concerns with #3935 that hold regardless of the root cause

  1. Diagnostics are lost. A transport failure (HTTP 504) or a timeout (exit 124) previously surfaced as Error editing '<path>': Internal sandbox error: Execute failed (status=504). It is now reported as Could not read file '<path>' for editing: no response. The PR rewrote edit_executeFailure_shouldFailWithCause and edit_executeFailure_nullOutput_shouldFailWithExitCodeFallback to assert the new, weaker message, so the regression is now covered by tests as expected behaviour.
  2. The retry is not free and can be wrong. Timeouts are retried through a full download/upload round trip. If the native command did finish after the client-side timeout, the transfer path re-reads the already-edited file and reports String not found — a successful edit reported as a failure.
  3. replaceAll becomes sensitive to a trailing CR. The flag is compared literally: replace_all = sys.argv[4] == "true". A transport that appends \r yields 'true\r', which is not equal, so replaceAll=true silently degrades to a single replacement (or errors with "appears N times"). Verified locally: 'true\r' ≠ "true".
  4. The "single line" invariant does not hold. assertFalse(cmd.contains("\n")) passes only because test paths have no newlines; FilesystemUtils.shellQuote() preserves newlines inside the single-quoted path, so a path containing \n still produces a multi-line command.
  5. The command is no longer readable. Logs now show python3 -c "exec(__import__('base64').b64decode(b'…'))", an opaque blob, and the command grows by ~33% for the base64 plus a wrapper.

Suggested actions

  • Revert the widened fallback: keep executeFailureMessage(...) for timeouts/transport errors, and fall back only when the script provably did not run (126/127, or a shell parse failure with truncated == false).
  • Keep the heredoc form unless a backend is demonstrated to mangle newlines. If one is, fix that transport (it affects execute too) or gate a single-line mode per backend, rather than imposing it on every backend.
  • Regardless of the above, compare the flag as sys.argv[4].strip() == "true" (or pass 0/1 and parse as int).

What would settle it

The original report (#3933/#3934) does not include the failing command or its stderr. Please provide:

  • the exact command text and the stderr/exit code observed,
  • the deployment/version and codec in use at the time,
  • whether the failing call was edit_file or the execute tool.

With that we can decide whether this is a real transport defect (fix it there), a different failure mode, or a non-reproducible report.


中文摘要

在真实 CubeSandbox(<cube-sandbox-endpoint>,PROTO 与 JSON 两种 codec)上,按 #3935 合并后的代码(adf2dea27)实测:多行 heredoc 正常、旧版 heredoc 编辑命令直接成功、4 KiB/32 KiB 命令不截断、edit() 的各项功能均正常。也就是说 #3935 声称的"envd 破坏 heredoc 导致 bash exit 2"在本环境无法复现。

heredoc 是 POSIX 标准(§2.7.4),且本仓库的沙箱镜像契约明确要求支持 heredoc,因此问题更可能出在传输层而非脚本写法;若真存在破坏换行的后端,它同样会毁掉 execute 工具的多行命令,应在那里修。

另外,无论根因如何,#3935 有三点需要修正:兜底范围过宽导致诊断丢失(504/超时被报成 "Could not read file")、超时后重试可能把已成功的编辑报成失败、以及 sys.argv[4] == "true" 对尾部 \r 敏感会让 replaceAll 静默退化。

chcodex avatar Nov 02 '26 10:11 chcodex

I independently reproduced the unsafe retry concern in this report against current main 1d8f9dc04e0e6c7e4d54074331cd5059a87cfa1d (2.1.0-SNAPSHOT): a native edit that commits limit = 1; -> limit = 11; before its response is lost can be replayed by the transfer fallback, producing limit = 1111;. A local fault-injection regression currently fails all five timeout/transport/truncated/missing-result/exception cases. This is an offline execute-boundary reproduction, not a claim of a live CubeSandbox failure.

I am implementing and will submit a focused PR for this part: preserve uncertain execute outcomes and their diagnostics without automatically replaying the edit; retain fallback when Python is unavailable or the helper explicitly reports an untouched target (write_failed). I will keep the current script transport format, and leave the separate heredoc/CR questions outside this PR. Could you assign this focused fix to @pengmoubuaixuexi? I am continuing with regression tests and module validation now.

AI assistance is being used for investigation and implementation; I will review the diff and report the checks and their limits in the PR. Thanks @chcodex for documenting the risk.

pengmoubuaixuexi avatar Nov 03 '26 00:11 pengmoubuaixuexi

Fixed in: https://github.com/agentscope-ai/agentscope-java/pull/3989

jujn avatar Nov 05 '26 06:11 jujn