diff --git a/llm/provider.go b/llm/provider.go index 1d7854b..f7133e7 100644 --- a/llm/provider.go +++ b/llm/provider.go @@ -186,6 +186,18 @@ type NonStreamingProvider interface { ChatNonStreaming(ctx context.Context, req ChatRequest) (<-chan ChatEvent, error) } +// PromptCachingProvider is an optional capability interface: providers +// that implement Anthropic-style explicit prompt caching (cache_control +// breakpoints, CacheLastMessage) declare it here. The runtime gates +// caching on this capability rather than on the provider's configured +// NAME — a user-named provider block ("platformai", "bedrock", any +// relay) wrapping an Anthropic-shaped API must still get caching; +// matching the name string alone silently disabled it for every +// renamed provider. +type PromptCachingProvider interface { + SupportsPromptCaching() bool +} + // ParseProviderModel splits "provider/model" into (provider, model). // If no slash is present, returns ("", name) and the caller should use a default. func ParseProviderModel(name string) (provider, model string) { diff --git a/providers/anthropic/anthropic.go b/providers/anthropic/anthropic.go index f36cf50..07b8cb9 100644 --- a/providers/anthropic/anthropic.go +++ b/providers/anthropic/anthropic.go @@ -28,6 +28,11 @@ func NewAnthropicProvider(apiKey, baseURL string) *AnthropicProvider { return &AnthropicProvider{client: client} } +// SupportsPromptCaching implements llm.PromptCachingProvider: the +// Anthropic Messages API honours cache_control breakpoints regardless +// of what the provider block is named in user config. +func (p *AnthropicProvider) SupportsPromptCaching() bool { return true } + func (p *AnthropicProvider) Models() []llm.ModelInfo { return []llm.ModelInfo{ {ID: "claude-sonnet-4-5-20250514", Name: "Claude Sonnet 4.5", Provider: "anthropic"}, diff --git a/runtime/capability_test.go b/runtime/capability_test.go new file mode 100644 index 0000000..4c5a19c --- /dev/null +++ b/runtime/capability_test.go @@ -0,0 +1,57 @@ +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()) +} diff --git a/runtime/runtime.go b/runtime/runtime.go index d85b2b0..d41b358 100644 --- a/runtime/runtime.go +++ b/runtime/runtime.go @@ -181,14 +181,26 @@ func (r *Runtime) maybeKickoffAsyncCompaction(msgs []llm.Message, parts []llm.Sy } // providerSupportsCaching returns true when the runtime's provider implements -// Anthropic-style explicit prompt caching. +// Anthropic-style explicit prompt caching. The capability interface wins: +// a provider block named "platformai" (or "bedrock", or anything else) +// wrapping the Anthropic implementation still caches. The name check is +// the legacy fallback for custom LLMProvider impls that predate the +// capability interface. func (r *Runtime) providerSupportsCaching() bool { + if pc, ok := r.LLM.(llm.PromptCachingProvider); ok { + return pc.SupportsPromptCaching() + } return r.Provider == "anthropic" } // providerSupportsMidLoopCompaction returns true for hosted frontier -// providers that handle mid-loop summary injection cleanly. +// providers that handle mid-loop summary injection cleanly. Same +// capability-over-name rule as providerSupportsCaching: an +// Anthropic-shaped provider under a custom name qualifies. func (r *Runtime) providerSupportsMidLoopCompaction() bool { + if pc, ok := r.LLM.(llm.PromptCachingProvider); ok && pc.SupportsPromptCaching() { + return true + } switch r.Provider { case "anthropic", "openai", "gemini": return true