TestCache_EmptyResponseIsNotCached documents that the cache_response stop-hook deliberately drops empty (whitespace-only) assistant responses on the floor: replaying nothing on a future turn would leave the user staring at a blank reply with no recourse but to ask again, so the model is called every
(t *testing.T)
| 213 | // the user staring at a blank reply with no recourse but to ask again, |
| 214 | // so the model is called every time. |
| 215 | func TestCache_EmptyResponseIsNotCached(t *testing.T) { |
| 216 | t.Parallel() |
| 217 | |
| 218 | c, err := cache.New(cache.Config{Enabled: true}) |
| 219 | require.NoError(t, err) |
| 220 | |
| 221 | // Two empty-response streams; we expect the model to be called both |
| 222 | // times because the empty answer must never reach the cache. |
| 223 | stream1 := newStreamBuilder().AddContent("").AddStopWithUsage(5, 0).Build() |
| 224 | stream2 := newStreamBuilder().AddContent("").AddStopWithUsage(5, 0).Build() |
| 225 | prov := &messageRecordingProvider{ |
| 226 | id: "test/mock-model", |
| 227 | streams: []*mockStream{stream1, stream2}, |
| 228 | } |
| 229 | |
| 230 | sess1 := session.New(session.WithUserMessage("silent treatment")) |
| 231 | runWithCache(t, c, prov, sess1) |
| 232 | sess2 := session.New(session.WithUserMessage("silent treatment")) |
| 233 | runWithCache(t, c, prov, sess2) |
| 234 | |
| 235 | prov.mu.Lock() |
| 236 | defer prov.mu.Unlock() |
| 237 | assert.Len(t, prov.recordedMessages, 2, |
| 238 | "empty responses must not be cached; the model must be called every time") |
| 239 | |
| 240 | _, found := c.Lookup("silent treatment") |
| 241 | assert.False(t, found, "empty assistant response must not appear in the cache") |
| 242 | } |
| 243 | |
| 244 | func hasAgentChoice(events []Event, content string) bool { |
| 245 | for _, ev := range events { |
nothing calls this directly
no test coverage detected