From 3f8763d9da3e505328d323ab7b9c45558e3149b3 Mon Sep 17 00:00:00 2001 From: sk-dev-ai Date: Fri, 18 Sep 2026 19:39:17 +0530 Subject: [PATCH] feat: forward native cache-affinity knobs (prompt_cache_key et al) Native opencode sends prompt_cache_key (session-sliced ID), plus safety_identifier, store, and service_tier. The translator dropped all four, so proxied turns never rode prompt-cache affinity while native did. Decode, carry, and re-emit on both Chat and Responses paths, omit-when-absent. Verified live: same-session turns hit 99% cached upstream after warm-up. --- internal/protocol/bridge.go | 8 ++++++++ internal/protocol/request.go | 18 ++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/internal/protocol/bridge.go b/internal/protocol/bridge.go index 37f0353..e61246e 100644 --- a/internal/protocol/bridge.go +++ b/internal/protocol/bridge.go @@ -73,6 +73,14 @@ type bridgeRequest struct { FrequencyPenalty any PresencePenalty any Seed any + // Native-fidelity knobs decoded from the client body and re-emitted + // upstream. prompt_cache_key is the big one: native opencode sends the + // session-sliced ID here and rides prompt-cache affinity; without it + // every proxied turn pays full attention over the whole history. + PromptCacheKey any + SafetyIdentifier any + ServiceTier any + Store any } type Usage struct { diff --git a/internal/protocol/request.go b/internal/protocol/request.go index 7787858..32a8f80 100644 --- a/internal/protocol/request.go +++ b/internal/protocol/request.go @@ -268,6 +268,10 @@ func decodeBridgeRequest(protocol Protocol, input map[string]any) (bridgeRequest request.FrequencyPenalty = input["frequency_penalty"] request.PresencePenalty = input["presence_penalty"] request.Seed = input["seed"] + request.PromptCacheKey = input["prompt_cache_key"] + request.SafetyIdentifier = input["safety_identifier"] + request.ServiceTier = input["service_tier"] + request.Store = input["store"] case Responses: request.MaxTokens = input["max_output_tokens"] @@ -340,6 +344,10 @@ func decodeBridgeRequest(protocol Protocol, input map[string]any) (bridgeRequest request.ToolChoice = decodeResponsesToolChoice(input["tool_choice"]) request.ResponseFormat = jsonutil.MapAt(input, "text", "format") request.ParallelToolCalls = input["parallel_tool_calls"] + request.PromptCacheKey = input["prompt_cache_key"] + request.SafetyIdentifier = input["safety_identifier"] + request.ServiceTier = input["service_tier"] + request.Store = input["store"] case Anthropic: request.MaxTokens = input["max_tokens"] @@ -478,6 +486,10 @@ func encodeChatRequest(request bridgeRequest) (map[string]any, error) { jsonutil.Put(output, "frequency_penalty", request.FrequencyPenalty) jsonutil.Put(output, "presence_penalty", request.PresencePenalty) jsonutil.Put(output, "seed", request.Seed) + jsonutil.Put(output, "prompt_cache_key", request.PromptCacheKey) + jsonutil.Put(output, "safety_identifier", request.SafetyIdentifier) + jsonutil.Put(output, "service_tier", request.ServiceTier) + jsonutil.Put(output, "store", request.Store) if request.Stream { output["stream_options"] = map[string]any{"include_usage": true} } @@ -667,6 +679,12 @@ func encodeResponsesRequest(request bridgeRequest) map[string]any { output["text"] = map[string]any{"format": format} } jsonutil.Put(output, "parallel_tool_calls", request.ParallelToolCalls) + // ponytail: native-fidelity knobs. Omit-when-absent: clients that never + // send them see zero behavior change; opencode turns ride cache affinity. + jsonutil.Put(output, "prompt_cache_key", request.PromptCacheKey) + jsonutil.Put(output, "safety_identifier", request.SafetyIdentifier) + jsonutil.Put(output, "service_tier", request.ServiceTier) + jsonutil.Put(output, "store", request.Store) items := make([]any, 0, len(request.Messages)+1) if len(request.Developer) > 0 {