fix(capabilities): eliminate prefix cache invalidation from in-place system prompt mutation - #282
Merged
Merged
Conversation
Two fixes for subagent session ordering issue:
1. identifiers.py: Handle backward clock jumps in _create() — when
now_ms() returns a lower timestamp than _last_timestamp (NTP
adjustment on macOS), keep the last timestamp and increment the
counter instead of resetting. Ensures IDs are always monotonically
increasing even when the wall clock goes backward.
2. session_routes.py: Sort GET /session/{id}/children by time.created
(ascending) so OpenCode UI left/right navigation shows child
sessions in creation order.
3. time_utils.py: Fix misleading docstring — now_ms() uses
time.time_ns() (CLOCK_REALTIME) which is NOT monotonic.
…system prompt mutation MemoryCapability and SkillManagerCap modified SystemPromptPart.content in-place every turn via before_model_request, causing prefix cache misses on every model request (OpenAI auto-cache and Anthropic prompt caching). - MemoryCapability: replace before_model_request with get_instructions returning an async callable (dynamic=True, no system prompt mutation) - SkillManagerCap: replace before_model_request with get_instructions returning [static_metadata, dynamic_callable] — metadata is dynamic=False (cacheable), matched skill content is dynamic=True - CombinedToolsetCapability: fix get_instructions to preserve callables and sequences from children (previously silently dropped non-str returns) - Remove _inject_into_system_prompt utility (no longer used) - DynamicContextCapability: no change (compaction is intentional) Closes #281
|
Caution The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased. |
Replace before_model_request calls with get_instructions callable invocation to match the prefix cache fix.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
MemoryCapabilityandSkillManagerCapmodifiedSystemPromptPart.contentin-place every turn viabefore_model_request, causing prefix cache misses on every model request. This increased token costs, latency, and wasted compute for both OpenAI (auto-prefix-cache) and Anthropic (prompt caching).Closes #281.
Root Cause
Two capabilities used a shared
_inject_into_system_prompt()utility that mutatedSystemPromptPart.contentdirectly in the message history:Since the system prompt is part of the message history (not the instructions field), modifying it changes the prefix every turn → cache miss every turn.
Fix
Replace
before_model_requestin-place mutation withget_instructionsreturning callables. Pydantic-ai handles callables correctly: literal strings getdynamic=False(cacheable), callable results getdynamic=True(correct for dynamic content). No system prompt mutation occurs.Changes
memory.pybefore_model_request→get_instructionsreturning async callable. Removed_inject_into_system_prompt+_is_model_requestutilities.skill_manager_cap.pybefore_model_request→get_instructionsreturning[static_metadata, dynamic_callable]. Metadata isdynamic=False(cacheable); matched<skill_content>blocks aredynamic=True. Matcher usesRunContext.messages.combined_toolset.pyget_instructionsto preserve callables and sequences from children (previously silently dropped non-strreturns).dynamic_context.pyHow it works
Before (broken):
get_instructions()returns static metadata stringbefore_model_request()mutatesSystemPromptPart.contentin-place with dynamic contentAfter (fixed):
get_instructions()returns[static_str, async_callable]RunContext(has.messages)InstructionPart(dynamic=False)→ cacheableInstructionPart(dynamic=True)→ correctly marked dynamicTesting
ruff checkcleanruff format --checkcleanmypyclean