fix(agent): move archived summary into system prompt for KV cache stability
- 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.
This commit is contained in:
1 parent
73a8d8a875
commit
a6e993df25
8 files changed
+119
-41
No files matched your search
@@ -706,12 +706,16 @@ class AgentLoop:
|
||||
session: Session,
|
||||
history: list[dict[str, Any]],
|
||||
pending_ask_id: str | None,
|
||||
pending_summary: Any,
|
||||
pending_summary: str | None,
|
||||
) -> list[dict[str, Any]]:
|
||||
"""Build the initial message list for the LLM turn."""
|
||||
if pending_ask_id:
|
||||
system_prompt = self.context.build_system_prompt(
|
||||
channel=msg.channel,
|
||||
session_summary=pending_summary,
|
||||
)
|
||||
return ask_user_tool_result_messages(
|
||||
self.context.build_system_prompt(channel=msg.channel),
|
||||
system_prompt,
|
||||
history,
|
||||
pending_ask_id,
|
||||
image_generation_prompt(msg.content, msg.metadata),
|
||||
@@ -719,11 +723,11 @@ class AgentLoop:
|
||||
return self.context.build_messages(
|
||||
history=history,
|
||||
current_message=image_generation_prompt(msg.content, msg.metadata),
|
||||
session_summary=pending_summary,
|
||||
media=msg.media if msg.media else None,
|
||||
channel=msg.channel,
|
||||
chat_id=self._runtime_chat_id(msg),
|
||||
sender_id=msg.sender_id,
|
||||
session_summary=pending_summary,
|
||||
)
|
||||
|
||||
async def _dispatch_command_inline(
|
||||
@@ -1179,7 +1183,6 @@ class AgentLoop:
|
||||
|
||||
await self.consolidator.maybe_consolidate_by_tokens(
|
||||
session,
|
||||
session_summary=pending,
|
||||
replay_max_messages=self._max_messages,
|
||||
)
|
||||
is_subagent = msg.sender_id == "subagent"
|
||||
@@ -1203,9 +1206,9 @@ class AgentLoop:
|
||||
current_message="" if is_subagent else msg.content,
|
||||
channel=channel,
|
||||
chat_id=chat_id,
|
||||
session_summary=pending,
|
||||
current_role=current_role,
|
||||
sender_id=msg.sender_id,
|
||||
session_summary=pending,
|
||||
)
|
||||
final_content, _, all_msgs, stop_reason, _ = await self._run_agent_loop(
|
||||
messages, session=session, channel=channel, chat_id=chat_id,
|
||||
@@ -1413,7 +1416,6 @@ class AgentLoop:
|
||||
async def _state_build(self, ctx: TurnContext) -> str:
|
||||
await self.consolidator.maybe_consolidate_by_tokens(
|
||||
ctx.session,
|
||||
session_summary=ctx.pending_summary,
|
||||
replay_max_messages=self._max_messages,
|
||||
)
|
||||
self._set_tool_context(
|
||||
|
||||
Reference in new issue
Block a user