fix: preserve original JSON bytes in normalizeCacheControlTTL#407
fix: preserve original JSON bytes in normalizeCacheControlTTL#407thebtf wants to merge 1 commit intorouter-for-me:mainfrom
Conversation
… TTL change needed normalizeCacheControlTTL unconditionally re-serializes the entire request body through json.Unmarshal/json.Marshal even when no TTL normalization is needed. Go's json.Marshal randomizes map key order and HTML-escapes <, >, & characters (to \u003c, \u003e, \u0026), producing different raw bytes on every call. Anthropic's prompt caching uses byte-prefix matching, so any byte-level difference causes a cache miss. This means the ~119K system prompt and tools are re-processed on every request when routed through CPA. The fix adds a bool return to normalizeTTLForBlock to indicate whether it actually modified anything, and skips the marshal step in normalizeCacheControlTTL when no blocks were changed.
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request optimizes the Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request correctly addresses an issue where normalizeCacheControlTTL would always re-serialize the JSON payload, causing unnecessary cache misses due to byte-level differences. The change to return the original byte slice when no modifications are made is a solid improvement for performance and correctness. The addition of a new test case to verify this behavior is also great. I have one suggestion to improve code maintainability by reducing some duplication.
| if tools, ok := asArray(root["tools"]); ok { | ||
| for _, tool := range tools { | ||
| if obj, ok := asObject(tool); ok { | ||
| normalizeTTLForBlock(obj, &seen5m) | ||
| if normalizeTTLForBlock(obj, &seen5m) { | ||
| modified = true | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if system, ok := asArray(root["system"]); ok { | ||
| for _, item := range system { | ||
| if obj, ok := asObject(item); ok { | ||
| normalizeTTLForBlock(obj, &seen5m) | ||
| if normalizeTTLForBlock(obj, &seen5m) { | ||
| modified = true | ||
| } | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
The logic for iterating over tools and system arrays is identical. This pattern is also repeated for the content array within each message. To improve maintainability and reduce code duplication, consider extracting this iteration logic into a local helper function.
For example:
processItems := func(items []any) {
for _, item := range items {
if obj, ok := asObject(item); ok {
if normalizeTTLForBlock(obj, &seen5m) {
modified = true
}
}
}
}
if tools, ok := asArray(root["tools"]); ok {
processItems(tools)
}
// ... and so on for system and messages content
Summary
normalizeCacheControlTTLunconditionally re-serializes the entire request body throughjson.Unmarshal→json.Marshal, even when no TTL normalization is neededjson.Marshal(map[string]any)randomizes map key order and HTML-escapes<,>,&(→\u003c,\u003e,\u0026), producing different raw bytes on every callChanges
normalizeTTLForBlocknow returnsboolindicating whether it modified the blocknormalizeCacheControlTTLtracks amodifiedflag and returns the originalpayloadslice unchanged when no TTL downgrade was neededTestNormalizeCacheControlTTL_PreservesOriginalBytesWhenNoChangeverifies exact byte preservation (including HTML chars thatjson.Marshalwould escape)Test plan
go build ./...— clean buildgo test ./internal/runtime/executor/... -run TestNormalizeCacheControlTTL— both tests passgo test ./internal/runtime/executor/...— all existing tests unaffected