TestPackDropsOrphanToolMessage: if budget-trimming cuts the assistant whose tool_calls spawned a tool message, that orphan must be dropped, else OpenAI-compat servers 400 with "tool message without preceding tool_calls".
(t *testing.T)
| 130 | // tool_calls spawned a tool message, that orphan must be dropped, else |
| 131 | // OpenAI-compat servers 400 with "tool message without preceding tool_calls". |
| 132 | func TestPackDropsOrphanToolMessage(t *testing.T) { |
| 133 | fortyX := strings.Repeat("x", 40) |
| 134 | history := []Message{ |
| 135 | {Role: RoleAssistant, ToolCalls: []ToolCall{{ID: "c1", Name: "bash"}}}, |
| 136 | {Role: RoleTool, ToolCallID: "c1", Content: fortyX}, |
| 137 | {Role: RoleAssistant, Content: "reply"}, |
| 138 | } |
| 139 | // Tight enough to drop the first assistant, loose enough that the tool |
| 140 | // message would otherwise survive. Tuned against the +8 per-message overhead. |
| 141 | r := Pack(history, 30) |
| 142 | for _, m := range r.Messages { |
| 143 | if m.Role == RoleTool { |
| 144 | t.Fatalf("orphan tool message survived pack: %+v", r.Messages) |
| 145 | } |
| 146 | } |
| 147 | if len(r.Messages) == 0 { |
| 148 | t.Fatal("newest assistant should have survived") |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | // TestPackDropsEmptyIDToolMessages: an empty-ID tool_call must not let |
| 153 | // subsequent empty-ToolCallID tool messages ride through as "paired". An |