Skip to content

Secure, Fast, and Extensible Sandbox runtime for AI agents.

fix(sdk-go): keep line terminators when joining stdout events

Jackmeson1491e16b018e5
diff --git a/docs/sdks/go.md b/docs/sdks/go.md
index 152d54a86..281209c30 100644
--- a/docs/sdks/go.md
+++ b/docs/sdks/go.md
@@ -75,6 +75,8 @@ The following snippets go inside a function returning `error`, with the live `sa
### Run a command with streaming output
+`Execution.Text()` combines stdout events while preserving their line-ending bytes and any unterminated final fragment. It inserts a newline between events only when the preceding event ends in neither `\n` nor `\r`, preserving ordinary legacy stripped-line output without appending a final terminator. A trailing `\r` is always treated as a line ending, including when an older producer retained it as content while stripping another terminator.
+
Use structured callbacks for normal command output:
```go
diff --git a/sdks/sandbox/go/execution.go b/sdks/sandbox/go/execution.go
index f67206b5c..b3873be7c 100644
--- a/sdks/sandbox/go/execution.go
+++ b/sdks/sandbox/go/execution.go
@@ -99,7 +99,10 @@ func (e *Execution) Text() string {
var b strings.Builder
for i, m := range e.Stdout {
if i > 0 {
- b.WriteByte('\n')
+ previous := e.Stdout[i-1].Text
+ if !strings.HasSuffix(previous, "\n") && !strings.HasSuffix(previous, "\r") {
+ b.WriteByte('\n')
+ }
}
b.WriteString(m.Text)
}
diff --git a/sdks/sandbox/go/opensandbox_test.go b/sdks/sandbox/go/opensandbox_test.go
index 4c1cc439a..645d9890b 100644
--- a/sdks/sandbox/go/opensandbox_test.go
+++ b/sdks/sandbox/go/opensandbox_test.go
@@ -2431,6 +2431,39 @@ func TestWatchMetrics_ContextCancel(t *testing.T) {
}
}
+func TestExecution_Text(t *testing.T) {
+ tests := []struct {
+ name string
+ stdout []string
+ want string
+ }{
+ {name: "no events"},
+ {name: "terminated lines", stdout: []string{"a\n", "b\n"}, want: "a\nb\n"},
+ {name: "CRLF lines", stdout: []string{"a\r\n", "b\r\n"}, want: "a\r\nb\r\n"},
+ {name: "split CRLF boundaries", stdout: []string{"a\r", "\n", "b\r", "\n"}, want: "a\r\nb\r\n"},
+ {name: "CR lines", stdout: []string{"a\r", "b\r"}, want: "a\rb\r"},
+ {name: "mixed line endings", stdout: []string{"a\n", "b\r\n", "c\r", "d"}, want: "a\nb\r\nc\rd"},
+ {name: "blank lines", stdout: []string{"\n", "a\n", "\n", "b\n", "\n"}, want: "\na\n\nb\n\n"},
+ {name: "unterminated final fragment", stdout: []string{"a\n", "b"}, want: "a\nb"},
+ {name: "single terminated event", stdout: []string{"hello\n"}, want: "hello\n"},
+ {name: "single unterminated event", stdout: []string{"hello"}, want: "hello"},
+ {name: "single empty event", stdout: []string{""}},
+ {name: "legacy stripped lines", stdout: []string{"a", "b"}, want: "a\nb"},
+ {name: "legacy blank lines", stdout: []string{"", "a", "", "b", ""}, want: "\na\n\nb\n"},
+ }
+ for _, tc := range tests {
+ t.Run(tc.name, func(t *testing.T) {
+ exec := &Execution{}
+ for _, text := range tc.stdout {
+ exec.Stdout = append(exec.Stdout, OutputMessage{Text: text})
+ }
+ if got := exec.Text(); got != tc.want {
+ t.Errorf("Text() = %q, want %q", got, tc.want)
+ }
+ })
+ }
+}
+
func TestExecution_ProcessStreamEvents(t *testing.T) {
// Test the full Execution aggregation pipeline with all event types
exec := &Execution{}