Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions s09_memory/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Keep recall scoped to recent turns

When a session has more than a few short user turns, iterating over every historical user message means the text passed as Current request can still include old topics as long as the total is under 4 KB. That makes both the model selector and the deterministic keyword fallback consider stale words from earlier tasks, so an unrelated later request can recall memories because of something the user asked many turns ago; retaining a small recent-turn window while tail-slicing would fix the truncation issue without letting old topics keep influencing recall.

Useful? React with 👍 / 👎.

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
Expand Down