fix(agent): persist user message on every turn, not just happy path

User messages were only saved inside _update_memory at the end of a
successful directive. The clarification and access-denied branches
returned early without ever calling it, so when a clarification turn
asked 'what do you mean?' and the user replied, the original question
was missing from context — the bot looked at a transcript of nothing
but its own clarifying questions and asked yet another.

Save the user message at the top of handle_message so every branch
includes it. Drop the now-duplicate write from _update_memory.
This commit is contained in:
Carlos Garcia
2026-04-24 23:24:40 -04:00
parent 01adfbfb1a
commit 18f2c91715

View File

@@ -80,6 +80,11 @@ class MasterAgent:
t0 = time.monotonic()
await self._log_directive_start(directive_id, user_id, channel_id, message)
try:
# Persist the user message FIRST so it's part of context on every
# branch (clarification, access-denied, happy path). Without this,
# clarification turns lose the original question and the bot can't
# connect follow-up replies to the in-flight conversation.
await self._memory.append_message(user_id, 'user', message, directive_id)
context = await self._build_context(user_id, message)
intent = await self._classify_intent(context, message)
if intent.needs_clarification:
@@ -226,7 +231,8 @@ class MasterAgent:
return resp.content
async def _update_memory(self, user_id, message, response, reports, directive_id):
await self._memory.append_message(user_id, 'user', message, directive_id)
# User message is persisted at the top of handle_message — only save
# the assistant reply here.
await self._memory.append_message(user_id, 'assistant', response, directive_id)
for report in reports:
if report.data: