From d81d5a141d4436cd461596ff263858a104fb835c Mon Sep 17 00:00:00 2001 From: siri666942 Date: Thu, 13 Aug 2026 20:16:29 +0800 Subject: [PATCH] s09: fix recent_user_text to take tail slice and traverse chronologically MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Walk messages forward (chronological), join, then slice the tail — so the function name matches its behavior. Previously, the function reversed the collection and head-sliced, which dropped the latest (and most relevant) user turn whenever it was short. --- s09_memory/code.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/s09_memory/code.py b/s09_memory/code.py index 1113634d2..dcccd56ff 100644 --- a/s09_memory/code.py +++ b/s09_memory/code.py @@ -248,17 +248,16 @@ def extract_json_array(text: str) -> list: return value return [] -def recent_user_text(messages: list, max_turns: int = 3) -> str: - turns = [] - for message in reversed(messages): +def recent_user_text(messages: list, max_chars: int = 4000) -> str: + parts = [] + for message in messages: if message.get("role") != "user": continue text = message_text(message).strip() if text: - turns.append(text) - if len(turns) == max_turns: - break - return "\n".join(reversed(turns))[:4000] + parts.append(text) + joined = "\n".join(parts) + return joined[-max_chars:] if len(joined) > max_chars else joined def keyword_memory_selection( records: list[dict], query: str, max_items: int