agentscope-ai / agentscope-ai/agentscope-java

BaseSandboxFilesystem.edit() inline Python script has multiple bugs, should use download-edit-upload

Đang mở
#2,397 1 bình luận 0 reaction 0 người được giao Xem trên GitHub
area/build area/core/tool area/extensions bug
Ngôn ngữ chính
Java
Star
5.6k
Fork
1.3k
Merge trung bình
4 ngày 12 giờ
Pull request đã merge (30 ngày)
77

Mô tả

## Problem

`BaseSandboxFilesystem.edit()` executes an inline Python script via `python3 -c` with heredoc to perform string replacement inside the sandbox. This approach has three bugs:

### Bug 1: `\n` is not recognized as newline by `python3 -c`

The Java code uses `\\n` (literal backslash+n) to represent line breaks in the Python code. However, `python3 -c` does not interpret `\n` as a newline separator — it is parsed as a line continuation character, causing `SyntaxError`.

**Actual error:**
```
Error editing file '/tmp/file.txt': unexpected server response:
File "", line 1
import sys, os, base64, json
SyntaxError: unexpected character after line continuation character
```

### Bug 2: Manual JSON concatenation is fragile

`edit()` builds the JSON payload by hand with `jsonEscape()`, which only escapes `\`, `"`, `\n`, `\r`, `\t`. It misses other JSON control characters (U+0000-U+0008, U+000B, U+000C, U+000E-U+001F). When `oldString`/`newString` contain these characters, the generated JSON is invalid and `json.loads()` fails.

### Bug 3: Hard dependency on `python3`

`edit()` is the only method in `BaseSandboxFilesystem` that requires `python3` in the sandbox image. All other methods (`ls`/`grep`/`glob`/`write`/`read`) use pure shell commands or the `uploadFiles` API. Many sandbox images do not have `python3` pre-installed.

## Fix (PR #)

Restructure `edit()` to download → Java replacement → upload:

1. `downloadFiles()` downloads the file content to the Java side
2. `FilesystemUtils.performStringReplacement()` performs the replacement (shared with `LocalFilesystem` and `RemoteFilesystem`)
3. `uploadFiles()` writes the modified content back to the sandbox

Additional improvements:
- Add `FilesystemUtils.ReplacementResult` record to replace the type-unsafe `Object[]` return
- Fix empty-file guard semantics: `contentBytes == null || contentBytes.length == 0`
- Remove `jsonEscape()` method and unused `import java.util.Base64`
- Use `import java.nio.charset.StandardCharsets` instead of fully qualified name

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.