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(agent): account for file paths in read pagination budgets
icearia0219b1d92048cf61
diff --git a/internal/agent/tools/read_file_path_budget_test.go b/internal/agent/tools/read_file_path_budget_test.gonew file mode 100644index 000000000..548b2199f--- /dev/null+++ b/internal/agent/tools/read_file_path_budget_test.go@@ -0,0 +1,68 @@+package tools++import (+ "encoding/json"+ "fmt"+ "strings"+ "testing"+ "unicode/utf8"++ "github.com/Tencent/WeKnora/internal/sandbox"+ "github.com/stretchr/testify/require"+)++func TestReadFileLongPathPagesDoNotLoseContent(t *testing.T) {+ for _, dir := range []string{"short/", strings.Repeat("nested/", 100), strings.Repeat("目录/", 220)} {+ t.Run(fmt.Sprintf("path-runes-%d", utf8.RuneCountInString(dir)), func(t *testing.T) {+ filePath := "/workspace/" + dir + "file.txt"+ var content strings.Builder+ for i := 0; i < 100; i++ {+ fmt.Fprintf(&content, "line %03d %s\n", i, strings.Repeat("x", 80))+ }+ source := &fakeSandboxFileSource{+ stat: &sandbox.RemoteStatEntry{+ Path: filePath, Type: sandbox.RemoteEntryFile, Size: int64(content.Len()),+ },+ data: []byte(content.String()),+ }+ registry := NewToolRegistry()+ registry.SetMaxToolOutputSize(2048)+ registry.RegisterTool(NewReadFileTool(source))+ offset := 1+ var restored strings.Builder+ for calls := 0; ; calls++ {+ require.Less(t, calls, 100, "continuation must make progress")+ args, err := json.Marshal(ReadFileInput{Path: filePath, Offset: offset})+ require.NoError(t, err)+ result, err := registry.ExecuteTool(sandboxFileTestContext(), ToolReadFile, args)+ require.NoError(t, err)+ require.True(t, result.Success, result.Error)+ require.NotContains(t, result.Output, "output truncated")+ require.LessOrEqual(t, utf8.RuneCountInString(result.Output), 2048)+ _, text, ok := strings.Cut(result.Output, "```\n")+ require.True(t, ok)+ text, _, ok = strings.Cut(text, "```\n")+ require.True(t, ok)+ require.Equal(t, len(text), result.Data["returned_bytes"])+ restored.WriteString(text)+ next, more := result.Data["next_offset"].(int)+ if !more {+ break+ }+ require.Greater(t, next, offset)+ offset = next+ }+ require.Equal(t, content.String(), restored.String())+ })+ }+}++func TestReadFileRejectsBudgetTooSmallForPath(t *testing.T) {+ filePath := "/workspace/" + strings.Repeat("nested/", 100) + "file.txt"+ result := renderFilePage(WithOutputBudget(t.Context(), 512), ReadFileInput{},+ []byte("hello\n"), "session-1", filePath, "/workspace")+ require.False(t, result.Success)+ require.Contains(t, result.Error, "output budget")+ require.Empty(t, result.Output)+ require.NotContains(t, result.Data, "next_offset")+}diff --git a/internal/agent/tools/workspace_reader.go b/internal/agent/tools/workspace_reader.goindex 4d89f55b6..3d7a82c50 100644--- a/internal/agent/tools/workspace_reader.go+++ b/internal/agent/tools/workspace_reader.go@@ -42,8 +42,8 @@ const (// arrive as thousands of nearly empty rows.defaultReadSandboxMaxLines = 2000- // Room left for the header, the fences, and the continuation hint when- // sizing a page against the registry's output budget.+ // Room left for metadata, fences, and the continuation hint when sizing+ // a page against the registry's output budget. The path is counted separately.readSandboxPageOverhead = 512// How large a file may be and still be downloaded to serve a page. This is@@ -262,7 +262,14 @@ func renderFilePage(ctx context.Context, input ReadFileInput, data []byte, sessi// The two budgets are also in different units, and the mismatch runs// opposite to intuition: 64 KiB of CJK is ~22k runes and fits, while 64 KiB// of ASCII is 65k runes and does not.- maxRunes := max(OutputBudget(ctx)-readSandboxPageOverhead, 1)+ maxRunes := OutputBudget(ctx) - readSandboxPageOverhead - utf8.RuneCountInString(clean)+ if maxRunes <= 0 {+ return &types.ToolResult{+ Success: false,+ Error: "file path and metadata exceed this read's output budget; increase the tool output limit",+ Data: resultData,+ }+ }page := paginateSandboxFile(string(data), input.Offset, input.Limit, maxBytes, maxRunes)// A line wider than the whole byte budget cannot be paged around: every