fix(session): preserve literal assistant replies in history (#6717)
<87817976+master1018@users.noreply.github.com>
This commit is contained in:
1 parent
d7e30b403e
commit
f9052884ba
2 files changed
+32
-37
No files matched your search
@@ -35,9 +35,6 @@ if TYPE_CHECKING:
|
||||
|
||||
_T = TypeVar("_T")
|
||||
SESSION_CACHE_MAX_SIZE = 128
|
||||
_MESSAGE_TIME_PREFIX_RE = re.compile(r"^\[Message Time: [^\]]+\]\n?")
|
||||
_LOCAL_IMAGE_BREADCRUMB_RE = re.compile(r"^\[image: (?:/|~)[^\]]+\]\s*$")
|
||||
_TOOL_CALL_ECHO_RE = re.compile(r'^\s*(?:generate_image|message)\([^)]*\)\s*$')
|
||||
_SESSION_PREVIEW_MAX_CHARS = 120
|
||||
_FORK_VOLATILE_METADATA_KEYS = {
|
||||
"_stopped_at_iteration_limit",
|
||||
@@ -53,22 +50,6 @@ _FORK_VOLATILE_METADATA_KEYS = {
|
||||
}
|
||||
|
||||
|
||||
def _sanitize_assistant_replay_text(content: str) -> str:
|
||||
"""Remove internal replay artifacts that the model may have copied before.
|
||||
|
||||
These strings are useful as runtime/session metadata, but when they appear
|
||||
in assistant examples they become demonstrations for the model to repeat.
|
||||
"""
|
||||
content = _MESSAGE_TIME_PREFIX_RE.sub("", content, count=1)
|
||||
lines = [
|
||||
line
|
||||
for line in content.splitlines()
|
||||
if not _LOCAL_IMAGE_BREADCRUMB_RE.match(line)
|
||||
and not _TOOL_CALL_ECHO_RE.match(line)
|
||||
]
|
||||
return "\n".join(lines).strip()
|
||||
|
||||
|
||||
def _text_preview(content: object) -> str:
|
||||
if isinstance(content, str):
|
||||
text = content
|
||||
@@ -85,7 +66,6 @@ def _text_preview(content: object) -> str:
|
||||
text = " ".join(parts)
|
||||
else:
|
||||
return ""
|
||||
text = _sanitize_assistant_replay_text(text)
|
||||
text = re.sub(r"\s+", " ", text).strip()
|
||||
if len(text) > _SESSION_PREVIEW_MAX_CHARS:
|
||||
text = text[: _SESSION_PREVIEW_MAX_CHARS - 1].rstrip() + "…"
|
||||
@@ -259,8 +239,6 @@ class Session:
|
||||
message = public_history_message(message)
|
||||
content = message.get("content", "")
|
||||
role = message.get("role")
|
||||
if role == "assistant" and isinstance(content, str):
|
||||
content = _sanitize_assistant_replay_text(content)
|
||||
# Synthesize an ``[image: path]`` breadcrumb from the persisted
|
||||
# ``media`` kwarg so LLM replay still sees *something* where the
|
||||
# image used to be. Without this, an image-only user turn
|
||||
|
||||
@@ -89,6 +89,17 @@ def test_list_sessions_includes_user_preview(tmp_path):
|
||||
assert rows[0]["preview"] == "帮我总结一下 OpenAI 的最新硬件计划"
|
||||
|
||||
|
||||
def test_list_sessions_previews_literal_image_marker_as_text(tmp_path):
|
||||
manager = SessionManager(tmp_path)
|
||||
session = manager.get_or_create("websocket:chat-image-marker")
|
||||
marker = "[image: /tmp/example.png]"
|
||||
session.add_message("user", marker)
|
||||
session.add_message("assistant", "That's an image marker.")
|
||||
manager.save(session)
|
||||
|
||||
assert manager.list_sessions()[0]["preview"] == marker
|
||||
|
||||
|
||||
def test_list_sessions_bounds_preview_scan(tmp_path):
|
||||
manager = SessionManager(tmp_path)
|
||||
session = manager.get_or_create("websocket:chat-long-preview")
|
||||
@@ -676,24 +687,30 @@ def test_get_history_does_not_paste_assistant_media_paths_into_replay():
|
||||
assert history == [{"role": "assistant", "content": "来了 🎨"}]
|
||||
|
||||
|
||||
def test_get_history_sanitizes_existing_assistant_replay_artifacts():
|
||||
session = Session(key="test:polluted-assistant")
|
||||
session.messages.append(
|
||||
{
|
||||
"role": "assistant",
|
||||
"content": (
|
||||
"[Message Time: 2026-05-09 00:33:48]\n"
|
||||
"来了 🎨\n"
|
||||
"[image: /home/user/.nanobot/media/generated/img_old.png]\n\n"
|
||||
"generate_image(\"16:9\")\n"
|
||||
"message(\"来了 🎨\")"
|
||||
),
|
||||
}
|
||||
def test_get_history_preserves_assistant_text_verbatim():
|
||||
session = Session(key="test:assistant-example")
|
||||
draft = 'message("欢迎加入")'
|
||||
image_marker = "[image: /tmp/example.png]"
|
||||
example = (
|
||||
"[Message Time: 2026-05-09 00:33:48]\r\n"
|
||||
"Here is the photo delivery example:\r\n"
|
||||
' generate_image("16:9")\r\n'
|
||||
' message("来了 🎨")\r\n'
|
||||
)
|
||||
session.add_message("user", "Draft a welcome message.")
|
||||
session.add_message("assistant", draft)
|
||||
session.add_message("user", "Show a photo delivery example.")
|
||||
session.add_message("assistant", example)
|
||||
session.add_message("user", "Repeat the image marker exactly.")
|
||||
session.add_message("assistant", image_marker)
|
||||
|
||||
history = session.get_history(max_messages=500)
|
||||
history = session.get_history()
|
||||
|
||||
assert history == [{"role": "assistant", "content": "来了 🎨"}]
|
||||
assert [message["content"] for message in history if message["role"] == "assistant"] == [
|
||||
draft,
|
||||
example,
|
||||
image_marker,
|
||||
]
|
||||
|
||||
|
||||
def test_get_history_respects_max_tokens(monkeypatch):
|
||||
|
||||
Reference in new issue
Block a user