Agent Core & Cache

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
Cache Invariant Timing
📊 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=(',', ':'))
Cache Optimization Flow
📊 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 user messages are found, the system automatically merges them at the text level via "\n\n", reducing them to a single message.
  • If a tool message lacks a preceding assistant tool_calls alignment, the system automatically inserts a synthesized assistant response with an empty reply to satisfy provider specifications.