RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.
Go agent-harnessagent-native-databaseagentic-aiagentic-retrievalagentic-searchaiai-agentscontext-enginecontext-engineeringcontext-managementharness-engineeringknowledge-compilationragretrieval-augmented-generationretrieval-harnesssearch-harness
refactor(agent): bound local output capture (#20464)
Sbaaoui Idrissefd7bdff5d70
diff --git a/internal/agent/sandbox/local.go b/internal/agent/sandbox/local.goindex 7d80fd17ef..27fb10b6ce 100644--- a/internal/agent/sandbox/local.go+++ b/internal/agent/sandbox/local.go@@ -89,6 +89,24 @@ const localDefaultNoFile = 64const localDefaultPythonBin = "python3"const localDefaultNodeBin = "node"+// localOutputCapture keeps the bytes needed for a valid result while+// accepting all writes so a noisy child can finish rather than block.+type localOutputCapture struct {+ buffer bytes.Buffer+ limit int+ total int+}++func (c *localOutputCapture) Write(p []byte) (int, error) {+ n := len(p)+ c.total += n+ if c.limit > 0 {+ p = p[:min(len(p), max(0, c.limit-c.buffer.Len()))]+ }+ _, err := c.buffer.Write(p)+ return n, err+}+// LocalProvider is the Go port of// `agent/sandbox/providers/local.py::LocalProvider`.type LocalProvider struct {@@ -320,12 +338,9 @@ func (p *LocalProvider) ExecuteCode(// side-channel — out of scope for a "local" provider.}- var stdout, stderr bytes.BuffermaxOut := p.maxOutputBytes- if maxOut > 0 {- stdout = bytes.Buffer{}- stderr = bytes.Buffer{}- }+ stdout := localOutputCapture{limit: maxOut}+ stderr := localOutputCapture{limit: maxOut}cmd.Stdout = &stdoutcmd.Stderr = &stderr@@ -364,14 +379,14 @@ func (p *LocalProvider) ExecuteCode(// Validate output size: if stdout+stderr exceed the cap,// surface as a runtime error (matches the Python provider).if maxOut > 0 {- combined := stdout.Len() + stderr.Len()+ combined := stdout.total + stderr.totalif combined > maxOut {return nil, fmt.Errorf("local: output exceeds %d bytes (got %d)", maxOut, combined)}}// Extract the structured result from stdout.- cleanedStdout, structured := ExtractStructuredResult(stdout.String())+ cleanedStdout, structured := ExtractStructuredResult(stdout.buffer.String())// Collect artifacts under <instance_dir>/artifacts/. Matches// the Python provider's _collect_artifacts behavior@@ -395,7 +410,7 @@ func (p *LocalProvider) ExecuteCode(}return &ExecutionResult{Stdout: cleanedStdout,- Stderr: stderr.String(),+ Stderr: stderr.buffer.String(),ExitCode: exitCode,ExecutionTime: time.Since(start).Seconds(),Metadata: metadata,diff --git a/internal/agent/sandbox/local_test.go b/internal/agent/sandbox/local_test.goindex 04e801eb64..bcb187e4f8 100644--- a/internal/agent/sandbox/local_test.go+++ b/internal/agent/sandbox/local_test.go@@ -26,6 +26,67 @@ import ("time")+func TestLocalOutputCapture_DrainsBeyondLimit(t *testing.T) {+ var output localOutputCapture+ output.limit = 32+ for range 100 {+ if n, err := output.Write([]byte(strings.Repeat("x", 1024))); err != nil || n != 1024 {+ t.Fatalf("Write = (%d, %v), want (1024, nil)", n, err)+ }+ }+ if output.buffer.Len() > 32 {+ t.Errorf("captured %d bytes, want at most 32", output.buffer.Len())+ }+ if output.total != 102400 {+ t.Errorf("total = %d, want 102400", output.total)+ }+}++func TestLocal_ExecuteCode_OutputLimit(t *testing.T) {+ pythonPath, err := findBinary("python3")+ if err != nil {+ t.Skip("python3 not on PATH")+ }+ p := newLocalForTest(t)+ p.pythonBin = pythonPath+ p.maxOutputBytes = 1024+ inst, err := p.CreateInstance(t.Context(), "python")+ if err != nil {+ t.Fatalf("CreateInstance: %v", err)+ }+ defer p.DestroyInstance(t.Context(), inst)++ code := "import sys\n\ndef main():\n sys.stdout.write('x' * 65536)\n sys.stderr.write('y' * 65536)\n return 1"+ result, err := p.ExecuteCode(t.Context(), inst, code, "python", 10, nil)+ if result != nil || err == nil || !strings.Contains(err.Error(), "local: output exceeds 1024 bytes (got ") {+ t.Fatalf("ExecuteCode returned result=%t, err=%v; want output limit error", result != nil, err)+ }+}++func TestLocal_ExecuteCode_NonzeroExitWithinOutputLimit(t *testing.T) {+ pythonPath, err := findBinary("python3")+ if err != nil {+ t.Skip("python3 not on PATH")+ }+ p := newLocalForTest(t)+ p.pythonBin = pythonPath+ p.maxOutputBytes = 1024+ inst, err := p.CreateInstance(t.Context(), "python")+ if err != nil {+ t.Fatalf("CreateInstance: %v", err)+ }+ defer p.DestroyInstance(t.Context(), inst)++ code := "import sys\n\ndef main():\n print('hello')\n sys.stderr.write('failure\\n')\n raise SystemExit(7)"+ result, err := p.ExecuteCode(t.Context(), inst, code, "python", 10, nil)+ if err != nil {+ t.Fatalf("ExecuteCode: %v", err)+ }+ if result.ExitCode != 7 || result.Stdout != "hello\n" || result.Stderr != "failure\n" {+ t.Errorf("result = (%d, %q, %q), want (7, hello, failure)", result.ExitCode, result.Stdout, result.Stderr)+ }+}+// newLocalForTest builds a LocalProvider pointing at a temp// work_dir so tests don't pollute the operator's filesystem.func newLocalForTest(t *testing.T) *LocalProvider {