fix(agent): persist shortcut commands without polluting LLM context

Shortcut commands (e.g. /help, /pairing) skip BUILD and SAVE states,
so their turns were never persisted to the session.  This caused WebUI
chats to appear empty after _turn_end because history hydration reads
from the session file.

Fix by persisting the user message and assistant response inside
_state_command, but tag them with _command=True so Session.get_history
filters them out of LLM context.  /new is excluded because it
intentionally clears the session.

- AgentLoop._persist_user_message_early now accepts **kwargs so
  _state_command can pass _command=True for the user turn.
- Session.get_history skips messages with _command=True.
This commit is contained in:
chengyongru authored and Xubin Ren committed 2026-05-14 23:51:58 +08:00
1 parent 8b724d510e
commit 26665823e3
3 files changed
+51

No files matched your search

+15
View File
@@ -564,6 +564,7 @@ class AgentLoop:
self,
msg: InboundMessage,
session: Session,
**kwargs: Any,
) -> bool:
"""Persist the triggering user message before the turn starts.
@@ -573,6 +574,7 @@ class AgentLoop:
has_text = isinstance(msg.content, str) and msg.content.strip()
if has_text or media_paths:
extra: dict[str, Any] = {"media": list(media_paths)} if media_paths else {}
extra.update(kwargs)
text = msg.content if isinstance(msg.content, str) else ""
session.add_message("user", text, **extra)
self._mark_pending_user_turn(session)
@@ -1268,6 +1270,19 @@ class AgentLoop:
result = await self.commands.dispatch(cmd_ctx)
if result is not None:
ctx.outbound = result
# Shortcut commands skip BUILD and SAVE, so we must persist the
# turn here so WebUI history hydration after _turn_end sees the
# message. Mark messages with _command so get_history can filter
# them out of LLM context. /new is excluded because it
# intentionally clears the session.
if raw.lower() != "/new":
ctx.user_persisted_early = self._persist_user_message_early(
ctx.msg, ctx.session, _command=True
)
ctx.session.add_message(
"assistant", result.content, _command=True
)
self.sessions.save(ctx.session)
return "shortcut"
return "dispatch"