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(agent): retain new file paths when compaction tracking is full (#4003)

澍6d8adb7c8aa8
diff --git a/internal/agent/compaction/checkpoint_test.go b/internal/agent/compaction/checkpoint_test.go
index f2a40da3c..c02eadc15 100644
--- a/internal/agent/compaction/checkpoint_test.go
+++ b/internal/agent/compaction/checkpoint_test.go
@@ -210,7 +210,13 @@ func TestSummarizerInputIsFittedToTheWindow(t *testing.T) {
msgs := []chat.Message{{Role: "system", Content: "you are an agent"}}
for i := 0; i < 150; i++ {
id := fmt.Sprintf("turn-%03d", i)
- msgs = append(msgs, storedTurn(id, 1, "/workspace/"+id+".txt")...)
+ // Keep the path count below the retention cap so this tests input
+ // truncation independently of oldest-path eviction.
+ path := "/workspace/shared.txt"
+ if i == 0 {
+ path = "/workspace/turn-000.txt"
+ }
+ msgs = append(msgs, storedTurn(id, 1, path)...)
}
msgs = append(msgs, chat.Message{Role: "user", Content: "next"})
diff --git a/internal/agent/compaction/fileops.go b/internal/agent/compaction/fileops.go
index 295aaf75b..919151fec 100644
--- a/internal/agent/compaction/fileops.go
+++ b/internal/agent/compaction/fileops.go
@@ -43,7 +43,7 @@ type fileOps struct {
func appendUnique(list []string, path string) []string {
path = strings.TrimSpace(path)
- if path == "" || len(list) >= maxTrackedFilePaths {
+ if path == "" {
return list
}
for _, existing := range list {
@@ -51,6 +51,9 @@ func appendUnique(list []string, path string) []string {
return list
}
}
+ if len(list) >= maxTrackedFilePaths {
+ list = list[len(list)-maxTrackedFilePaths+1:]
+ }
return append(list, path)
}
diff --git a/internal/agent/compaction/fileops_retention_test.go b/internal/agent/compaction/fileops_retention_test.go
new file mode 100644
index 000000000..e1edb8889
--- /dev/null
+++ b/internal/agent/compaction/fileops_retention_test.go
@@ -0,0 +1,43 @@
+package compaction
+
+import (
+ "fmt"
+ "testing"
+
+ "github.com/Tencent/WeKnora/internal/models/chat"
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+)
+
+func TestFileOpsKeepsNewPathsAfterReachingLimit(t *testing.T) {
+ for _, tool := range []string{"read_file", "write_sandbox_file", "edit_sandbox_file"} {
+ t.Run(tool, func(t *testing.T) {
+ var history []chat.Message
+ for i := 0; i < maxTrackedFilePaths; i++ {
+ history = append(history, toolCallMsg(tool, fmt.Sprintf(`{"path":"/workspace/%02d.txt"}`, i)))
+ }
+ previous := extractFileOps("", history).format()
+ current := extractFileOps(previous, []chat.Message{
+ toolCallMsg(tool, `{"path":"/workspace/newest.txt"}`),
+ })
+ read, modified := current.resolve()
+ paths := append(read, modified...)
+ require.Len(t, paths, maxTrackedFilePaths)
+ assert.Contains(t, paths, "/workspace/newest.txt")
+ assert.NotContains(t, paths, "/workspace/00.txt")
+ assert.Contains(t, paths, "/workspace/49.txt")
+ // The new artifact must survive a further compaction as well.
+ assert.Contains(t, extractFileOps(current.format()).format(), "/workspace/newest.txt")
+ })
+ }
+}
+
+func TestFileOpsDuplicateAtLimitDoesNotEvictAnotherPath(t *testing.T) {
+ var paths []string
+ for i := 0; i < maxTrackedFilePaths; i++ {
+ paths = append(paths, fmt.Sprintf("/workspace/%02d.txt", i))
+ }
+ before := append([]string(nil), paths...)
+ assert.Equal(t, before, appendUnique(paths, " /workspace/00.txt "))
+ assert.Equal(t, before, appendUnique(paths, " "))
+}