fix(session): preserve proactive channel delivery during session retention trimming

This commit is contained in:
ziuus
2026-08-07 10:53:55 +08:00
committed by chengyongru
parent c2fd41b44d
commit 60282d1588
2 changed files with 252 additions and 2 deletions
+7 -2
View File
@@ -356,13 +356,18 @@ class Session:
(i for i in range(start_idx, -1, -1) if self.messages[i].get("role") == "user"),
start_idx,
)
if start_idx > 0 and self.messages[start_idx - 1].get("_channel_delivery"):
start_idx -= 1
retained = self.messages[start_idx:]
# Prefer starting at a user turn when one exists within the retained window.
# Prefer starting at a user turn (or its preceding _channel_delivery) when one exists within the retained window.
first_user = next((i for i, m in enumerate(retained) if m.get("role") == "user"), None)
if first_user is not None:
retained = retained[first_user:]
if first_user > 0 and retained[first_user - 1].get("_channel_delivery"):
retained = retained[first_user - 1:]
else:
retained = retained[first_user:]
elif not extend_to_user:
# If the hard-capped tail is assistant/tool-only, anchor to the
# latest user in the full session and take a capped forward window.