-
Notifications
You must be signed in to change notification settings - Fork 221
fix(server): strip volatile billing header on all cache paths (OpenAI + codex) #360
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+228
−23
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| // Prompt normalization — volatile-header stripping for stable cache keys. | ||
|
|
||
| #include "prompt_normalize.h" | ||
| #include <algorithm> | ||
|
|
||
| namespace dflash::common { | ||
|
|
||
| static constexpr std::string_view kBillingHeader = "x-anthropic-billing-header:"; | ||
|
|
||
| // Returns true if `s`, after skipping leading whitespace, starts with kBillingHeader. | ||
| static bool is_billing_header_block(const std::string & s) { | ||
| auto pos = s.find_first_not_of(" \t\r\n"); | ||
| if (pos == std::string::npos) return false; | ||
| return s.compare(pos, kBillingHeader.size(), kBillingHeader) == 0; | ||
| } | ||
|
|
||
| // Strip any line whose ltrimmed text starts with kBillingHeader from a multi-line string. | ||
| static std::string strip_billing_header_lines(const std::string & s) { | ||
| std::string out; | ||
| out.reserve(s.size()); | ||
| std::string::size_type start = 0; | ||
| while (start <= s.size()) { | ||
| auto end = s.find('\n', start); | ||
| std::string_view line = (end == std::string::npos) | ||
| ? std::string_view(s).substr(start) | ||
| : std::string_view(s).substr(start, end - start); | ||
| // ltrim check | ||
| auto nws = line.find_first_not_of(" \t\r"); | ||
| bool is_header = (nws != std::string_view::npos) && | ||
| (line.substr(nws, kBillingHeader.size()) == kBillingHeader); | ||
| if (!is_header) { | ||
| out.append(line); | ||
| if (end != std::string::npos) out += '\n'; | ||
| } | ||
| if (end == std::string::npos) break; | ||
| start = end + 1; | ||
| } | ||
| return out; | ||
| } | ||
|
|
||
| std::string normalize_system_for_cache(const json & system_or_messages) { | ||
| if (system_or_messages.is_array()) { | ||
| if (system_or_messages.empty()) return ""; | ||
| const auto & first = system_or_messages[0]; | ||
| if (first.is_object() && first.contains("role")) { | ||
| // OpenAI messages array: strip billing-header lines from messages[0]. | ||
| if (first.value("role", "") == "system") { | ||
| const auto & content = first["content"]; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P1: (Based on your team's feedback about guarding JSON string reads and key/type checks.) . Prompt for AI agents |
||
| if (content.is_string()) { | ||
| return strip_billing_header_lines(content.get<std::string>()); | ||
| } | ||
| if (content.is_array()) { | ||
| std::string out; | ||
| for (const auto & block : content) { | ||
| if (block.is_object() && block.value("type", "") == "text") { | ||
| out += block.value("text", ""); | ||
|
cubic-dev-ai[bot] marked this conversation as resolved.
|
||
| } | ||
| } | ||
| return strip_billing_header_lines(out); | ||
| } | ||
| } | ||
| return ""; | ||
| } | ||
| // Anthropic content-block array: skip billing-header blocks entirely. | ||
| std::string out; | ||
| for (const auto & block : system_or_messages) { | ||
| if (block.is_object() && block.value("type", "") == "text") { | ||
| std::string text = block.value("text", ""); | ||
| if (!is_billing_header_block(text)) out += text; | ||
| } | ||
| } | ||
| return out; | ||
| } | ||
|
|
||
| if (system_or_messages.is_string()) { | ||
| return strip_billing_header_lines(system_or_messages.get<std::string>()); | ||
| } | ||
|
|
||
| return ""; | ||
| } | ||
|
|
||
| } // namespace dflash::common | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| // Prompt normalization — volatile-header stripping for stable cache keys. | ||
| // | ||
| // Pure functions: no IO, no globals, no CUDA deps. Tested standalone. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <nlohmann/json.hpp> | ||
| #include <string> | ||
|
|
||
| namespace dflash::common { | ||
|
|
||
| using json = nlohmann::json; | ||
|
|
||
| // Normalize the effective system/messages content for cache-key hashing. | ||
| // | ||
| // Accepts either: | ||
| // - Anthropic-format: the `system` field from a /v1/messages body | ||
| // (string or array-of-content-blocks) | ||
| // - OpenAI-format: the full `messages` array from a /v1/chat/completions | ||
| // body (the function inspects messages[0] when role=="system") | ||
| // | ||
| // Returns the normalized text string that represents the system content | ||
| // for the purposes of cache-key construction. Volatile claude-code headers | ||
| // (blocks or lines starting with "x-anthropic-billing-header:") are REMOVED | ||
| // so that two requests differing only in the header value hash identically. | ||
| std::string normalize_system_for_cache(const json & system_or_messages); | ||
|
|
||
| } // namespace dflash::common |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2: OpenAI system header normalization is skipped when
messages[0].contentis an array, leaving a cache-miss path unnormalized.Prompt for AI agents