fix(loop): persist subagent follow-up events in history

This commit is contained in:
xzq.xu
2026-04-18 13:30:22 +08:00
committed by Xubin Ren
parent c27b4d07c4
commit 1c939e8a5f
3 changed files with 170 additions and 1 deletions
+22 -1
View File
@@ -647,13 +647,17 @@ class AgentLoop:
session, pending = self.auto_compact.prepare_session(session, key)
await self.consolidator.maybe_consolidate_by_tokens(session)
if msg.sender_id == "subagent" and self._persist_subagent_followup(session, msg):
self.sessions.save(session)
self._set_tool_context(channel, chat_id, msg.metadata.get("message_id"))
history = session.get_history(max_messages=0)
current_role = "assistant" if msg.sender_id == "subagent" else "user"
messages = self.context.build_messages(
history=history,
current_message=msg.content, channel=channel, chat_id=chat_id,
current_message="" if msg.sender_id == "subagent" else msg.content,
channel=channel,
chat_id=chat_id,
session_summary=pending,
current_role=current_role,
)
@@ -870,6 +874,23 @@ class AgentLoop:
session.messages.append(entry)
session.updated_at = datetime.now()
def _persist_subagent_followup(self, session: Session, msg: InboundMessage) -> bool:
"""Persist subagent follow-ups before prompt assembly so history stays durable."""
task_id = msg.metadata.get("subagent_task_id") if isinstance(msg.metadata, dict) else None
if task_id and any(
m.get("injected_event") == "subagent_result" and m.get("subagent_task_id") == task_id
for m in session.messages
):
return False
session.add_message(
"assistant",
msg.content,
sender_id=msg.sender_id,
injected_event="subagent_result",
subagent_task_id=task_id,
)
return True
def _set_runtime_checkpoint(self, session: Session, payload: dict[str, Any]) -> None:
"""Persist the latest in-flight turn state into session metadata."""
session.metadata[self._RUNTIME_CHECKPOINT_KEY] = payload
+4
View File
@@ -246,6 +246,10 @@ class SubagentManager:
sender_id="subagent",
chat_id=f"{origin['channel']}:{origin['chat_id']}",
content=announce_content,
metadata={
"injected_event": "subagent_result",
"subagent_task_id": task_id,
},
)
await self.bus.publish_inbound(msg)