providerSupportsCaching gated on the provider NAME being literally 'anthropic', so an Anthropic-shaped provider registered under a custom config name (platformai, bedrock, any relay) never set CacheLastMessage — every turn re-processed the full conversation history uncached (~120K input tokens and 4-6s TTFT per turn on a large session). Add llm.PromptCachingProvider capability interface; AnthropicProvider declares it; runtime gates providerSupportsCaching and providerSupportsMidLoopCompaction on the capability first, with the name check kept as legacy fallback for custom impls.
58 lines
2.2 KiB
Go
58 lines
2.2 KiB
Go
package runtime
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/sausheong/harness/llm"
|
|
"github.com/sausheong/harness/llm/llmtest"
|
|
anthropicprovider "github.com/sausheong/harness/providers/anthropic"
|
|
)
|
|
|
|
// cachingCapableFake implements llm.LLMProvider plus the
|
|
// llm.PromptCachingProvider capability.
|
|
type cachingCapableFake struct {
|
|
llmtest.Stub
|
|
}
|
|
|
|
func (*cachingCapableFake) SupportsPromptCaching() bool { return true }
|
|
|
|
// TestProviderSupportsCaching_CapabilityInterfaceWins: an Anthropic-shaped
|
|
// provider registered under a custom name (platformai, bedrock, any relay)
|
|
// must still get prompt caching. Gating on the config block's NAME meant
|
|
// every turn on a renamed provider re-processed the full history uncached.
|
|
func TestProviderSupportsCaching_CapabilityInterfaceWins(t *testing.T) {
|
|
r := &Runtime{Provider: "platformai", LLM: &cachingCapableFake{}}
|
|
assert.True(t, r.providerSupportsCaching(),
|
|
"capability interface must win over the provider name")
|
|
}
|
|
|
|
func TestProviderSupportsCaching_NameFallbackStillWorks(t *testing.T) {
|
|
r := &Runtime{Provider: "anthropic", LLM: &llmtest.Stub{}}
|
|
assert.True(t, r.providerSupportsCaching(),
|
|
"legacy name-based gate kept for providers without the capability interface")
|
|
}
|
|
|
|
func TestProviderSupportsCaching_NonCachingProviderFalse(t *testing.T) {
|
|
r := &Runtime{Provider: "local", LLM: &llmtest.Stub{}}
|
|
assert.False(t, r.providerSupportsCaching())
|
|
}
|
|
|
|
// TestAnthropicProviderDeclaresPromptCaching pins the wiring end-to-end:
|
|
// the real Anthropic provider must implement the capability interface.
|
|
func TestAnthropicProviderDeclaresPromptCaching(t *testing.T) {
|
|
p := anthropicprovider.NewAnthropicProvider("k", "")
|
|
pc, ok := any(p).(llm.PromptCachingProvider)
|
|
assert.True(t, ok, "AnthropicProvider must implement llm.PromptCachingProvider")
|
|
if ok {
|
|
assert.True(t, pc.SupportsPromptCaching())
|
|
}
|
|
}
|
|
|
|
// Mid-loop compaction was gated on the same name list; an Anthropic-shaped
|
|
// provider under a custom name must support it too.
|
|
func TestProviderSupportsMidLoopCompaction_CapabilityInterfaceWins(t *testing.T) {
|
|
r := &Runtime{Provider: "platformai", LLM: &cachingCapableFake{}}
|
|
assert.True(t, r.providerSupportsMidLoopCompaction())
|
|
}
|