fix session display replay consistency

This commit is contained in:
wuyang6
2026-06-30 14:40:20 +08:00
parent 84ff22fc65
commit 0007d99763
4 changed files with 291 additions and 19 deletions
+36 -9
View File
@@ -3169,8 +3169,42 @@ def _stored_display_messages(
stored: StoredAgentSession,
) -> tuple[dict[str, Any], ...]:
if stored.display_messages:
return tuple(dict(message) for message in stored.display_messages)
return tuple(dict(message) for message in stored.messages)
messages = tuple(dict(message) for message in stored.display_messages)
else:
messages = tuple(dict(message) for message in stored.messages)
return tuple(message for message in messages if _is_visible_display_message(message))
_INTERNAL_DISPLAY_KINDS = {
'compact_boundary',
'compact_summary',
'continuation_request',
'file_history_replay',
'plugin_tool_runtime',
'runtime_context',
'snipped_message',
'system_context',
}
def _is_visible_display_message(message: dict[str, Any]) -> bool:
if message.get('role') == 'system':
return False
metadata = message.get('metadata')
if isinstance(metadata, dict):
kind = metadata.get('kind')
if isinstance(kind, str) and kind in _INTERNAL_DISPLAY_KINDS:
return False
content = message.get('content')
if isinstance(content, str):
stripped = content.strip()
if stripped.startswith('<system-reminder>'):
return False
if message.get('role') == 'user' and stripped.startswith(
'This session is being continued from a previous conversation'
):
return False
return True
def _stored_session_has_incomplete_tail(stored: StoredAgentSession) -> bool:
@@ -3572,10 +3606,7 @@ def _save_in_progress_session(
stored = load_agent_session(safe_id, directory=directory)
except (FileNotFoundError, OSError, json.JSONDecodeError):
return None
messages = list(stored.messages)
display_messages = list(_stored_display_messages(stored))
if messages and _is_pending_user_message(dict(messages[-1])):
messages.pop()
if display_messages and _is_pending_user_message(dict(display_messages[-1])):
display_messages.pop()
pending_message = {
@@ -3585,9 +3616,6 @@ def _save_in_progress_session(
'metadata': pending_metadata,
'message_id': f'user_pending_{int(time.time() * 1000)}',
}
messages.append(
dict(pending_message)
)
display_messages.append(dict(pending_message))
budget_state = (
dict(stored.budget_state)
@@ -3599,7 +3627,6 @@ def _save_in_progress_session(
save_agent_session(
replace(
stored,
messages=tuple(messages),
display_messages=tuple(display_messages),
budget_state=budget_state,
),