Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

fix(system): keep sandbox deep-check probe previews valid UTF-8 (#4675)

ydflowf64f3d95dd42
diff --git a/internal/handler/sandbox_check.go b/internal/handler/sandbox_check.go
index e6ea525ae..5a91a80b6 100644
--- a/internal/handler/sandbox_check.go
+++ b/internal/handler/sandbox_check.go
@@ -7,6 +7,7 @@ import (
"net/http"
"strings"
"time"
+ "unicode/utf8"
"github.com/gin-gonic/gin"
@@ -242,7 +243,9 @@ func describeProbeMismatch(exitCode int, killed bool, stdout, stderr, execErr st
}
// firstProbeLine picks the first non-empty line and caps it, so one runaway log
-// line cannot flood the check panel.
+// line cannot flood the check panel. The trailing partial rune (if the cap
+// lands inside a multi-byte character) is dropped so the panel detail stays
+// valid UTF-8.
func firstProbeLine(output string) string {
for _, line := range strings.Split(output, "\n") {
trimmed := strings.TrimSpace(line)
@@ -250,7 +253,11 @@ func firstProbeLine(output string) string {
continue
}
if len(trimmed) > 300 {
- return trimmed[:300] + "…"
+ cut := trimmed[:300]
+ for len(cut) > 0 && !utf8.ValidString(cut) {
+ cut = cut[:len(cut)-1]
+ }
+ return cut + "…"
}
return trimmed
}
diff --git a/internal/handler/sandbox_check_probe_line_test.go b/internal/handler/sandbox_check_probe_line_test.go
new file mode 100644
index 000000000..2bcd62ad9
--- /dev/null
+++ b/internal/handler/sandbox_check_probe_line_test.go
@@ -0,0 +1,30 @@
+package handler
+
+import (
+ "strings"
+ "testing"
+ "unicode/utf8"
+
+ "github.com/stretchr/testify/require"
+)
+
+// firstProbeLine output lands in the sandbox deep-check panel detail
+// (SandboxCheckItem.Message via describeProbeMismatch). Capping a probe's
+// stderr line by bytes must not split a multi-byte rune, or the panel shows
+// U+FFFD garbage for CJK container errors.
+func TestFirstProbeLineKeepsValidUTF8(t *testing.T) {
+ // 120 CJK runes = 360 bytes; the 300-byte cut lands inside rune 100.
+ line := strings.Repeat("错", 120)
+ got := firstProbeLine(line + "\nsecond line")
+ require.True(t, utf8.ValidString(got), "probe preview must stay valid UTF-8, got %q", got)
+ require.True(t, strings.HasSuffix(got, "…"), "expected truncation ellipsis: %q", got)
+
+ // Mixed widths: 99 CJK (297 bytes) + two-byte runes; the cut lands
+ // exactly on a two-byte rune boundary here — both boundary and mid-rune
+ // shapes must stay valid.
+ mixed := firstProbeLine(strings.Repeat("错", 99) + strings.Repeat("é", 51))
+ require.True(t, utf8.ValidString(mixed), "probe preview must stay valid UTF-8, got %q", mixed)
+
+ require.Equal(t, "short", firstProbeLine(" short \n"), "short lines are trimmed, not cut")
+ require.Empty(t, firstProbeLine("\n \n"), "blank output yields an empty preview")
+}