Skip to content

Self-hosted workflows for coding-agent delivery. Orchestrate CodeBuddy, Codex and Claude Code with scripts and human approval gates. Includes Local workflows for coding assistants.

fix(runtime): clip tool messages and executor output on rune boundaries (#191)

kevin91f3fa55b0bd
diff --git a/internal/runtime/cli_live_clip_test.go b/internal/runtime/cli_live_clip_test.go
new file mode 100644
index 00000000..01ad07e1
--- /dev/null
+++ b/internal/runtime/cli_live_clip_test.go
@@ -0,0 +1,20 @@
+//go:build clilive
+
+package runtime
+
+import (
+ "strings"
+ "testing"
+ "unicode/utf8"
+)
+
+func TestClipLiveKeepsMultiByteRunesWhole(t *testing.T) {
+ // 700 bytes is not a multiple of 3, so the byte cut lands inside a rune.
+ got := clipLive(strings.Repeat("失", 300))
+ if !utf8.ValidString(got) {
+ t.Fatalf("clipped live output is not valid UTF-8: %q", got[len(got)-8:])
+ }
+ if !strings.HasSuffix(got, "…") || len(got) > 700+len("…") {
+ t.Fatalf("expected clipped live output of at most 700 bytes, got %d bytes", len(got))
+ }
+}
diff --git a/internal/runtime/cli_live_config.go b/internal/runtime/cli_live_config.go
index e9842213..80a6e957 100644
--- a/internal/runtime/cli_live_config.go
+++ b/internal/runtime/cli_live_config.go
@@ -83,7 +83,7 @@ func redactLive(text string) string {
func clipLive(text string) string {
text = strings.TrimSpace(text)
if len(text) > 700 {
- return text[:700] + "…"
+ return clipOnRuneBoundary(text, 700) + "…"
}
return text
}
diff --git a/internal/runtime/protocol_tools.go b/internal/runtime/protocol_tools.go
index 1ea4e045..619ad67d 100644
--- a/internal/runtime/protocol_tools.go
+++ b/internal/runtime/protocol_tools.go
@@ -250,7 +250,9 @@ func clipToolMsg(s string) string {
if len(s) <= n {
return s
}
- return s[:n] + "…"
+ // A byte cut can split a multi-byte character (a Chinese label such as
+ // "失败" is 3 bytes a rune) and leave invalid UTF-8.
+ return clipOnRuneBoundary(s, n) + "…"
}
func firstNonEmpty(values ...string) string {
diff --git a/internal/runtime/runner.go b/internal/runtime/runner.go
index e7fe77c3..cd0269d5 100644
--- a/internal/runtime/runner.go
+++ b/internal/runtime/runner.go
@@ -9,6 +9,7 @@ import (
"path/filepath"
"strings"
"time"
+ "unicode/utf8"
)
type ExecutorRunner struct{}
@@ -257,12 +258,30 @@ func appendTaskPromptToSystemFile(workspace, agentRel, prompt string) error {
const maxExecutorOutputBytes = 32 << 10
+// clipOnRuneBoundary cuts s to at most n bytes without splitting a UTF-8
+// sequence. It backs off at most utf8.UTFMax-1 bytes, so malformed input
+// cannot shrink the result to nothing.
+func clipOnRuneBoundary(s string, n int) string {
+ if len(s) <= n {
+ return s
+ }
+ end := n
+ for i := 0; i < utf8.UTFMax-1 && end > 0 && !utf8.RuneStart(s[end]); i++ {
+ end--
+ }
+ if !utf8.RuneStart(s[end]) {
+ // Not valid UTF-8 here anyway; keep the byte cut.
+ end = n
+ }
+ return s[:end]
+}
+
func clipExecutorOutput(s string) string {
s = strings.TrimSpace(s)
if len(s) <= maxExecutorOutputBytes {
return s
}
- return s[:maxExecutorOutputBytes] + fmt.Sprintf("\n… [output truncated, %d bytes]", len(s))
+ return clipOnRuneBoundary(s, maxExecutorOutputBytes) + fmt.Sprintf("\n… [output truncated, %d bytes]", len(s))
}
var ErrMaxTurns = errors.New("executor reached max turns")
diff --git a/internal/runtime/runtime_test.go b/internal/runtime/runtime_test.go
index aaf5d856..11e9c5dc 100644
--- a/internal/runtime/runtime_test.go
+++ b/internal/runtime/runtime_test.go
@@ -14,6 +14,7 @@ import (
"sync/atomic"
"testing"
"time"
+ "unicode/utf8"
"agentflow/internal/domain"
)
@@ -2172,6 +2173,38 @@ func TestClipExecutorOutputBoundsRawCLIStreams(t *testing.T) {
}
}
+func TestClipExecutorOutputKeepsMultiByteRunesWhole(t *testing.T) {
+ // "失" is 3 bytes, so one leading byte puts the cut inside a rune.
+ raw := "x" + strings.Repeat("失", maxExecutorOutputBytes/3+10)
+ got := clipExecutorOutput(raw)
+ if !utf8.ValidString(got) {
+ t.Fatalf("clipped output is not valid UTF-8: %q", got[maxExecutorOutputBytes-4:maxExecutorOutputBytes+4])
+ }
+ if !strings.Contains(got, "output truncated") {
+ t.Fatalf("expected clipped output, got %d bytes", len(got))
+ }
+}
+
+func TestClipToolMsgKeepsMultiByteRunesWhole(t *testing.T) {
+ // 180 bytes is 60 three-byte runes; one leading byte moves the cut inside one.
+ got := clipToolMsg("x" + strings.Repeat("失败", 100))
+ if !utf8.ValidString(got) {
+ t.Fatalf("clipped tool message is not valid UTF-8: %q", got)
+ }
+ if !strings.HasSuffix(got, "…") || len(got) > 180+len("…") {
+ t.Fatalf("expected a clipped message of at most 180 bytes, got %d bytes", len(got))
+ }
+}
+
+func TestClipOnRuneBoundaryBoundsBackoffOnMalformedInput(t *testing.T) {
+ // A run of continuation bytes has no rune start to back off to; keep the
+ // byte cut rather than shrinking the result to nothing.
+ got := clipOnRuneBoundary("x"+strings.Repeat("\x80", 50), 20)
+ if len(got) != 20 {
+ t.Fatalf("expected a 20-byte cut, got %d bytes", len(got))
+ }
+}
+
func TestOversizedPromptMovesOffArgvIntoSystemFile(t *testing.T) {
sb := &capturingSandbox{workspace: t.TempDir(), driver: "local"}
ex := domain.ExecutorDefinition{Kind: "codebuddy", Binary: "codebuddy", Args: []string{"-p"}}