Prompt Cache Protection Invariant Rules & Role Alternation Alignment
Decrypting byte-level stability design in system_prompt.py and automatic role alignment in agent_runtime_helpers.py
📊 Figure 2-2-1: System Prompt Static Serialization & LLM Cache Impact UML Sequence Diagram
🔒 1. Cache Prefix Byte-Level Stable Code Implementation
To maximize LLM prompt cache hit rate, the system enforces strict tool definition sorting in agent/system_prompt.py:
def format_tools_for_system_message(tools_list: list) -> str:
# Force ascending sort by tool name to avoid hash-randomization ordering jitter
sorted_tools = sorted(tools_list, key=lambda x: x["name"])
# Strip whitespace and newlines to ensure each serialized character is identical
return json.dumps(sorted_tools, sort_keys=True, separators=(',', ':'))
📊 Figure 2-2-2: AIAgent Decision Flow & Cache Optimization Overall Sequence Diagram
🔄 2. Role Alternation Control
In agent/agent_runtime_helpers.py's repair_message_sequence_with_cursor(), the system performs self-healing cleanup on the message list:
- If two consecutive
usermessages are found, the system automatically merges them at the text level via"\n\n", reducing them to a single message. - If a
toolmessage lacks a precedingassistanttool_callsalignment, the system automatically inserts a synthesizedassistantresponse with an empty reply to satisfy provider specifications.