Skip to content

Commit 4f74db7

Browse files
committed
fix: Prioritize foundational knowledge when truncating context
Critical fix: When context exceeds token limit, intelligent truncation now preserves foundational warning and foundational knowledge documents. Problem: - Context truncation was cutting foundational warning and foundational knowledge, causing LLM to ignore StillMe's self-tracking capabilities - Simple truncation from end could remove critical instructions Solution: - Detect foundational warning and foundational knowledge in context - Always preserve: header + foundational warning + foundational knowledge docs - Only truncate: conversation docs and non-foundational knowledge (from end) - Fallback to simple truncation if no foundational knowledge found This ensures foundational knowledge about StillMe's capabilities is never lost during context truncation, fixing incorrect responses about self-tracking.
1 parent 089ac1e commit 4f74db7

1 file changed

Lines changed: 50 additions & 3 deletions

File tree

stillme_core/rag/rag_retrieval.py

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -939,11 +939,58 @@ def build_prompt_context(self, context: Dict[str, Any], max_context_tokens: int
939939

940940
result = "\n".join(context_parts) if context_parts else "No relevant context found."
941941

942-
# Final check: if result is still too long, truncate the entire result
942+
# Final check: if result is still too long, truncate intelligently
943+
# CRITICAL: Prioritize keeping foundational warning and foundational knowledge
943944
result_tokens = self._estimate_tokens(result)
944945
if result_tokens > max_context_tokens:
945-
logger.warning(f"Context still too long ({result_tokens} tokens), truncating entire result")
946-
result = self._truncate_text_by_tokens(result, max_context_tokens)
946+
logger.warning(f"Context still too long ({result_tokens} tokens), truncating intelligently to preserve foundational knowledge")
947+
948+
# Strategy: Truncate from the END (conversation docs first, then non-foundational knowledge)
949+
# But ALWAYS keep foundational warning and foundational knowledge documents
950+
951+
# Find foundational warning position
952+
foundational_warning_start = result.find("🚨🚨🚨 CRITICAL INSTRUCTION")
953+
foundational_warning_end = result.find("🚨🚨🚨 END CRITICAL INSTRUCTION")
954+
955+
if foundational_warning_start != -1 and foundational_warning_end != -1:
956+
# Found foundational warning - preserve header, warning, and foundational knowledge
957+
# Get everything before foundational warning (header)
958+
header_and_before = result[:foundational_warning_start]
959+
foundational_warning = result[foundational_warning_start:foundational_warning_end + len("🚨🚨🚨 END CRITICAL INSTRUCTION")]
960+
remaining_text = result[foundational_warning_end + len("🚨🚨🚨 END CRITICAL INSTRUCTION"):]
961+
962+
# Extract foundational knowledge documents (lines with [foundational knowledge] or CRITICAL_FOUNDATION)
963+
lines = remaining_text.split('\n')
964+
foundational_lines = []
965+
other_lines = []
966+
967+
for line in lines:
968+
if "[foundational knowledge]" in line or "CRITICAL_FOUNDATION" in line:
969+
foundational_lines.append(line)
970+
else:
971+
other_lines.append(line)
972+
973+
# Rebuild: header + foundational warning + foundational knowledge + truncate others
974+
foundational_text = '\n'.join(foundational_lines)
975+
other_text = '\n'.join(other_lines)
976+
977+
# Calculate tokens for foundational parts (header + warning + foundational docs)
978+
foundational_content = header_and_before + foundational_warning + '\n' + foundational_text
979+
foundational_tokens = self._estimate_tokens(foundational_content)
980+
remaining_for_others = max_context_tokens - foundational_tokens - 500 # Reserve 500 tokens buffer
981+
982+
if remaining_for_others > 0:
983+
# Truncate other text (conversation docs, non-foundational knowledge) from the END
984+
truncated_other = self._truncate_text_by_tokens(other_text, remaining_for_others)
985+
result = foundational_content + '\n' + truncated_other
986+
else:
987+
# Not enough space - keep only foundational parts
988+
logger.warning("⚠️ Context too long - keeping only foundational warning and foundational knowledge, truncating all other content")
989+
result = foundational_content
990+
else:
991+
# No foundational warning found - use simple truncation (shouldn't happen, but fallback)
992+
logger.warning("⚠️ No foundational warning found in context - using simple truncation")
993+
result = self._truncate_text_by_tokens(result, max_context_tokens)
947994

948995
# Log if foundational knowledge is present in final result
949996
if "[foundational knowledge]" in result or "CRITICAL INSTRUCTION" in result:

0 commit comments

Comments
 (0)