Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions pr_agent/algo/ai_handlers/litellm_ai_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,18 @@ async def chat_completion(self, model: str, system: str, user: str, temperature:
# Support for custom OpenAI body fields (e.g., Flex Processing)
kwargs = _process_litellm_extra_body(kwargs)

# Support for Anthropic prompt caching via LiteLLM's cache_control_injection_points
# (https://docs.litellm.ai/docs/tutorials/prompt_caching). Configurable as a JSON
# array in [litellm] section of configuration.toml or .pr_agent.toml.
if get_settings().get("LITELLM.CACHE_CONTROL_INJECTION_POINTS", None):
try:
cache_points = json.loads(get_settings().litellm.cache_control_injection_points)
if not isinstance(cache_points, list):
raise ValueError("LITELLM.CACHE_CONTROL_INJECTION_POINTS must be a JSON array")
kwargs["cache_control_injection_points"] = cache_points
Comment on lines +538 to +543

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Action required

1. cache_control_injection_points overwrites kwargs 📘 Rule violation ⛨ Security

The new config pass-through sets kwargs["cache_control_injection_points"] without checking whether
that key is already present. This can silently override caller-supplied/provider-supplied values and
violates the requirement to guard against parameter collisions when merging request kwargs.
Agent Prompt
## Issue description
`kwargs["cache_control_injection_points"]` is written unconditionally once the setting exists, which can silently overwrite an already-populated value coming from upstream kwargs processing.

## Issue Context
PR Compliance requires explicit collision guards for merged request parameters so critical/behavior-changing kwargs cannot be overwritten without detection.

## Fix Focus Areas
- pr_agent/algo/ai_handlers/litellm_ai_handler.py[538-545]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools

except json.JSONDecodeError as e:
raise ValueError(f"LITELLM.CACHE_CONTROL_INJECTION_POINTS contains invalid JSON: {str(e)}")
Comment on lines +539 to +545

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Action required

2. cache_points parsing misses typeerror 📘 Rule violation ☼ Reliability

The new JSON parsing for cache_control_injection_points only handles json.JSONDecodeError, but
json.loads(...) can also raise TypeError when the setting is not a string/bytes (e.g., a TOML
value parsed into a native Python list). This violates the compliance expectation to
validate/normalize configuration at the boundary and surface targeted configuration errors instead
of leaking unexpected runtime exceptions that can abort a request.
Agent Prompt
## Issue description
`cache_control_injection_points` is currently passed through `json.loads(...)` with handling only for `json.JSONDecodeError`, but the value can legitimately arrive from configuration as a native Python type (e.g., a list from TOML parsing), in which case `json.loads` raises `TypeError` that is unhandled and can abort the request. Update the boundary handling to validate/normalize the type and raise a targeted configuration error (or accept the already-parsed type) rather than leaking unexpected exceptions.

## Issue Context
This setting is configured via TOML (e.g., `configuration.toml` / `.pr_agent.toml`) and the settings loader uses `tomllib.load`, which yields native Python types such as lists and dicts for TOML arrays/tables. The current implementation always attempts JSON parsing regardless of the incoming type and only catches JSON decode failures, so mis-typed or naturally-typed TOML values can cause an unhandled `TypeError` instead of a clear, configuration-focused error message.

## Fix Focus Areas
- pr_agent/algo/ai_handlers/litellm_ai_handler.py[535-546]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


# Support for Bedrock custom inference profile via model_id
model_id = get_settings().get("litellm.model_id")
if model_id and 'bedrock/' in model:
Expand Down
3 changes: 3 additions & 0 deletions pr_agent/settings/configuration.toml
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,9 @@ success_callback = []
failure_callback = []
service_callback = []
# model_id = "" # Optional: Custom inference profile ID for Amazon Bedrock
# cache_control_injection_points = "" # Optional: JSON array enabling Anthropic prompt caching via LiteLLM
# Example: cache_control_injection_points = '[{"location": "message", "role": "system"}]'
# See https://docs.litellm.ai/docs/tutorials/prompt_caching

[pr_similar_issue]
skip_comments = false
Expand Down
Loading