|
| 1 | +package agent |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +func toolMsg(id, name, content string) ChatMessage { |
| 10 | + return ChatMessage{Role: "tool", ToolCallID: id, Name: name, Content: content} |
| 11 | +} |
| 12 | + |
| 13 | +func asstCall(id, name, argsJSON string) ChatMessage { |
| 14 | + return ChatMessage{Role: "assistant", ToolCalls: []ToolCall{ |
| 15 | + {ID: id, Type: "function", Function: ToolCallFunc{Name: name, Arguments: argsJSON}}, |
| 16 | + }} |
| 17 | +} |
| 18 | + |
| 19 | +// buildConvo 造 1 system + 1 user + n 轮 (assistant call + tool result),每条工具结果内容为 content。 |
| 20 | +func buildConvo(n int, name, content string) []ChatMessage { |
| 21 | + convo := []ChatMessage{{Role: "system", Content: "sys"}, {Role: "user", Content: "任务"}} |
| 22 | + for k := 0; k < n; k++ { |
| 23 | + id := fmt.Sprintf("c%d", k) |
| 24 | + convo = append(convo, asstCall(id, name, fmt.Sprintf(`{"path":"src/f%d.go"}`, k))) |
| 25 | + convo = append(convo, toolMsg(id, name, content)) |
| 26 | + } |
| 27 | + return convo |
| 28 | +} |
| 29 | + |
| 30 | +func countTool(convo []ChatMessage) (reclaimed, kept int) { |
| 31 | + for _, m := range convo { |
| 32 | + if m.Role != "tool" { |
| 33 | + continue |
| 34 | + } |
| 35 | + if strings.HasPrefix(m.Content, reclaimMarkerPrefix) { |
| 36 | + reclaimed++ |
| 37 | + } else { |
| 38 | + kept++ |
| 39 | + } |
| 40 | + } |
| 41 | + return |
| 42 | +} |
| 43 | + |
| 44 | +func TestReclaim_OldReplacedRecentKept(t *testing.T) { |
| 45 | + big := strings.Repeat("这是一大段读取内容。", 500) // 每条远超小预算 |
| 46 | + convo := buildConvo(10, "Read", big) |
| 47 | + before := len(convo) |
| 48 | + |
| 49 | + if !reclaimToolOutputs(convo, 4096) { // 预算 = 4096*20% ≈ 819 token |
| 50 | + t.Fatal("应发生回收") |
| 51 | + } |
| 52 | + reclaimed, kept := countTool(convo) |
| 53 | + if reclaimed == 0 { |
| 54 | + t.Fatal("较旧工具结果应被回收") |
| 55 | + } |
| 56 | + if kept < reclaimMinTailToolMsgs { |
| 57 | + t.Fatalf("最近 %d 条应保留,实际保留 %d", reclaimMinTailToolMsgs, kept) |
| 58 | + } |
| 59 | + if len(convo) != before { |
| 60 | + t.Fatalf("reclaim 不应增删消息: %d -> %d", before, len(convo)) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +func TestReclaim_TailProtected(t *testing.T) { |
| 65 | + convo := buildConvo(reclaimMinTailToolMsgs+3, "Bash", strings.Repeat("x", 5000)) |
| 66 | + reclaimToolOutputs(convo, 1024) // 预算极小 |
| 67 | + |
| 68 | + seen := 0 |
| 69 | + for i := len(convo) - 1; i >= 0; i-- { |
| 70 | + if convo[i].Role != "tool" { |
| 71 | + continue |
| 72 | + } |
| 73 | + seen++ |
| 74 | + if seen <= reclaimMinTailToolMsgs && strings.HasPrefix(convo[i].Content, reclaimMarkerPrefix) { |
| 75 | + t.Fatalf("最近第 %d 条工具结果被误回收", seen) |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +func TestReclaim_ReferenceHasNameAndPath(t *testing.T) { |
| 81 | + convo := buildConvo(8, "Read", strings.Repeat("y", 8000)) |
| 82 | + reclaimToolOutputs(convo, 2048) |
| 83 | + |
| 84 | + found := false |
| 85 | + for _, m := range convo { |
| 86 | + if m.Role == "tool" && strings.HasPrefix(m.Content, reclaimMarkerPrefix) { |
| 87 | + if !strings.Contains(m.Content, "Read") || !strings.Contains(m.Content, "src/f") { |
| 88 | + t.Fatalf("引用应含工具名和 path, got=%q", m.Content) |
| 89 | + } |
| 90 | + found = true |
| 91 | + break |
| 92 | + } |
| 93 | + } |
| 94 | + if !found { |
| 95 | + t.Fatal("应有被回收的工具结果") |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +func TestReclaim_Idempotent(t *testing.T) { |
| 100 | + convo := buildConvo(8, "Read", strings.Repeat("z", 6000)) |
| 101 | + if !reclaimToolOutputs(convo, 2048) { |
| 102 | + t.Fatal("首次应有改动") |
| 103 | + } |
| 104 | + if reclaimToolOutputs(convo, 2048) { |
| 105 | + t.Fatal("再次 reclaim 不应有新改动(幂等)") |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +func TestReclaim_NonToolUntouched(t *testing.T) { |
| 110 | + userBig := strings.Repeat("u", 9000) |
| 111 | + asstBig := strings.Repeat("a", 9000) |
| 112 | + convo := []ChatMessage{ |
| 113 | + {Role: "system", Content: "s"}, |
| 114 | + {Role: "user", Content: userBig}, |
| 115 | + asstCall("c1", "Read", `{"path":"a.go"}`), |
| 116 | + {Role: "assistant", Content: asstBig}, |
| 117 | + } |
| 118 | + before := len(convo) |
| 119 | + reclaimToolOutputs(convo, 1024) |
| 120 | + if convo[1].Content != userBig { |
| 121 | + t.Fatal("user 消息不应被改") |
| 122 | + } |
| 123 | + if convo[3].Content != asstBig { |
| 124 | + t.Fatal("assistant 文本不应被改") |
| 125 | + } |
| 126 | + if len(convo) != before { |
| 127 | + t.Fatal("消息数不应变") |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +func TestReclaim_UnderBudgetNoChange(t *testing.T) { |
| 132 | + convo := buildConvo(6, "Read", "tiny") // 工具输出很小 |
| 133 | + if reclaimToolOutputs(convo, 1_000_000) { |
| 134 | + t.Fatal("预算充足时不应回收") |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +func TestReclaim_PairingPreserved(t *testing.T) { |
| 139 | + // 回收后每条 tool 消息的 ToolCallID / Name 不变,配对完整。 |
| 140 | + convo := buildConvo(10, "Read", strings.Repeat("w", 6000)) |
| 141 | + ids := map[int]string{} |
| 142 | + for i, m := range convo { |
| 143 | + if m.Role == "tool" { |
| 144 | + ids[i] = m.ToolCallID |
| 145 | + } |
| 146 | + } |
| 147 | + reclaimToolOutputs(convo, 2048) |
| 148 | + for i, m := range convo { |
| 149 | + if m.Role == "tool" { |
| 150 | + if m.ToolCallID != ids[i] { |
| 151 | + t.Fatalf("第 %d 条 ToolCallID 被改动", i) |
| 152 | + } |
| 153 | + if m.Name == "" { |
| 154 | + t.Fatalf("第 %d 条 Name 丢失", i) |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | +} |
0 commit comments