Move enhanced context to the end, to help prompt caching

This commit is contained in:
Amar Sood (tekacs) 2025-04-13 13:56:22 -04:00
parent 209518b73f
commit b4141c6537

View file

@ -182,11 +182,16 @@ class NavigatorCoder(Coder):
""" """
Override parent's format_chat_chunks to include enhanced context blocks with a Override parent's format_chat_chunks to include enhanced context blocks with a
cleaner, more hierarchical structure for better organization. cleaner, more hierarchical structure for better organization.
Optimized for prompt caching: enhanced context blocks are inserted after static
chat elements (system, examples, repo, readonly_files, done) but before variable
elements (chat_files, cur, reminder) to preserve prefix caching while providing
fresh context information.
""" """
# First get the normal chat chunks from the parent method # First get the normal chat chunks from the parent method
chunks = super().format_chat_chunks() # Calls BaseCoder's format_chat_chunks chunks = super().format_chat_chunks() # Calls BaseCoder's format_chat_chunks
# If enhanced context blocks are enabled, prepend them to the current messages # If enhanced context blocks are enabled, insert them in a strategic position
if self.use_enhanced_context: if self.use_enhanced_context:
# Create environment info context block # Create environment info context block
env_context = self.get_environment_info() env_context = self.get_environment_info()
@ -216,18 +221,25 @@ class NavigatorCoder(Coder):
if symbol_outline: # Add the new block if it was generated if symbol_outline: # Add the new block if it was generated
context_blocks.append(symbol_outline) context_blocks.append(symbol_outline)
# If we have any context blocks, prepend them to the system message # Insert a fresh context update as a separate message before current messages
if context_blocks: # This preserves cacheable prefix portions (system, examples, repo, etc.)
# while still providing fresh context information
if context_blocks and chunks.cur:
context_message = "\n\n".join(context_blocks) context_message = "\n\n".join(context_blocks)
# Prepend to system context but don't overwrite existing system content # Insert fresh context as a system message right before the first user message in cur
if chunks.system: for i, msg in enumerate(chunks.cur):
# If we already have system messages, append our context to the first one if msg["role"] == "user":
original_content = chunks.system[0]["content"] # Insert context message right before the first user message
# Ensure there's separation between our blocks and the original prompt chunks.cur.insert(i, dict(role="system", content=context_message))
chunks.system[0]["content"] = context_message + "\n\n" + original_content break
else: else:
# Otherwise, create a new system message # If no user message found, append to the end of chat_files
chunks.system = [dict(role="system", content=context_message)] # (just before any existing cur messages)
chunks.chat_files.append(dict(role="system", content=context_message))
elif context_blocks:
# If there are context blocks but no cur messages, append to chat_files
context_message = "\n\n".join(context_blocks)
chunks.chat_files.append(dict(role="system", content=context_message))
return chunks return chunks