fix(agent): move archived summary into system prompt for KV cache stability#3711
Merged
Conversation
07562a7 to
e6827ca
Compare
1e5ffcd to
7f744de
Compare
This was referenced May 10, 2026
…bility - Append [Archived Context Summary] to system prompt instead of injecting it into the user message runtime context, improving KV cache reuse across turns and avoiding consecutive same-role messages. - _last_summary persists in metadata (no pop) for restart survival; summary is re-injected every turn via the stable system prompt. - Remove dynamic "Inactive for X minutes" from _format_summary — use static last_active timestamp instead to preserve KV cache stability. - Pass session_summary through build_messages() so both normal and ask_user paths receive the archived summary in the system prompt. - estimate_session_prompt_tokens now reads _last_summary from metadata to include the summary in token budget estimation. - Remove obsolete session_summary parameter from maybe_consolidate_by_tokens and estimate_session_prompt_tokens call sites in loop.py (summary flows through build_messages instead). - Ensure /new (session.clear()) clears _last_summary from metadata.
7f744de to
e03d7b8
Compare
Collaborator
Author
|
Manual tested with langfuse |
This was referenced May 11, 2026
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
Archived conversation summaries were injected into the runtime context (user message block) via
_build_runtime_context. This has two drawbacks:Additionally,
_last_summarywaspop()'ed from session metadata on consumption, meaning it could not survive a process restart.Approach
Move summary into system prompt —
build_system_prompt()accepts an optionalsession_summaryand appends it as[Archived Context Summary].build_messages()forwardssession_summarytobuild_system_prompt()so both the normal path and theask_userpath receive the summary in the system prompt. Since the system prompt is stable across turns (only changes when a new archive is produced), this improves KV cache reuse.Freeze summary format at archive time —
_format_summarynow uses a staticlast_activetimestamp instead of computing a dynamic "Inactive for X minutes" on every turn, preserving KV cache stability._last_summarypersists in metadata — read withget()instead ofpop(), ensuring it survives process restarts. The summary is re-injected every turn via the stable system prompt.estimate_session_prompt_tokensreads summary from metadata — so the token budget estimation accounts for the archived summary accurately.Remove
session_summaryparameter from_build_runtime_context,maybe_consolidate_by_tokens, andestimate_session_prompt_tokens— these no longer need to handle the summary directly; it flows throughbuild_messages→build_system_promptinstead./newclears_last_summary—Session.clear()now pops_last_summaryfrom metadata so a fresh session starts clean.Files changed
nanobot/agent/autocompact.py— freeze_format_summaryoutput; stop popping_last_summarynanobot/agent/context.py— addsession_summarytobuild_messages(); move summary from runtime context tobuild_system_prompt()nanobot/agent/loop.py— passsession_summarytobuild_messagesin all call sitesnanobot/agent/memory.py—estimate_session_prompt_tokensreads summary from metadatananobot/session/manager.py—clear()removes_last_summarytests/agent/test_auto_compact.py— updated assertions + new lifecycle teststests/agent/test_loop_consolidation_tokens.py— updated assertionstests/agent/test_unified_session.py— updated assertions