Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.
Go agentagenticaichatbotdsh-pluginembeddingsevaluationgenerative-aigolangknowledge-basellmmulti-tenantollamaopenaiquestion-answeringragrerankingsemantic-searchvector-searchwiki
fix(sandbox): keep stderr previews in user-facing errors valid UTF-8 (#4674)
ydflow9e54a69e69e6
diff --git a/internal/sandbox/docker_remote_client.go b/internal/sandbox/docker_remote_client.goindex 30d303b79..7b6bf1164 100644--- a/internal/sandbox/docker_remote_client.go+++ b/internal/sandbox/docker_remote_client.go@@ -47,6 +47,7 @@ import ("strings""sync""time"+ "unicode/utf8""github.com/moby/moby/api/pkg/stdcopy""github.com/moby/moby/api/types/container"@@ -1334,12 +1335,18 @@ func dockerEnvSlice(env map[string]string) []string {}// firstNonEmptyLine trims a command's stderr down to something a user-facing-// error can carry.+// error can carry. The byte cap keeps one runaway line out of the error, and+// the trailing partial rune (if the cap lands inside a multi-byte character)+// is dropped so the surfaced message stays valid UTF-8.func firstNonEmptyLine(output string) string {for _, line := range strings.Split(output, "\n") {if trimmed := strings.TrimSpace(line); trimmed != "" {if len(trimmed) > 200 {- return trimmed[:200] + "…"+ cut := trimmed[:200]+ for len(cut) > 0 && !utf8.ValidString(cut) {+ cut = cut[:len(cut)-1]+ }+ return cut + "…"}return trimmed}diff --git a/internal/sandbox/docker_remote_client_utf8_test.go b/internal/sandbox/docker_remote_client_utf8_test.gonew file mode 100644index 000000000..9aa8c9608--- /dev/null+++ b/internal/sandbox/docker_remote_client_utf8_test.go@@ -0,0 +1,34 @@+package sandbox++import (+ "strings"+ "testing"+ "unicode/utf8"+)++// firstNonEmptyLine output is embedded into SandboxError messages that reach+// agent tool results and API responses (see #4336 for the format in the+// wild). Trimming a stderr line by bytes must not split a multi-byte rune,+// or the surfaced error carries invalid UTF-8.+func TestFirstNonEmptyLineKeepsValidUTF8(t *testing.T) {+ // 80 CJK runes = 240 bytes; the 200-byte cut lands inside rune 67.+ cjk := firstNonEmptyLine(strings.Repeat("错", 80) + "\nsecond line")+ if !utf8.ValidString(cjk) {+ t.Fatalf("firstNonEmptyLine produced invalid UTF-8: %q", cjk)+ }+ if !strings.HasSuffix(cjk, "…") {+ t.Fatalf("expected truncation ellipsis: %q", cjk)+ }++ // Mixed widths: 99 CJK (297 bytes) + two-byte runes; the 200-byte cut+ // lands exactly on a two-byte rune boundary here, so both boundary and+ // mid-rune shapes stay valid.+ mixed := firstNonEmptyLine(strings.Repeat("错", 99) + strings.Repeat("é", 51))+ if !utf8.ValidString(mixed) {+ t.Fatalf("firstNonEmptyLine produced invalid UTF-8: %q", mixed)+ }++ if got := firstNonEmptyLine("short\nlines"); got != "short" {+ t.Fatalf("short lines must pass through unchanged, got %q", got)+ }+}