From 18f2c91715b5e48c0f7f2e8199b7cb3a2ea5eb86 Mon Sep 17 00:00:00 2001 From: Carlos Garcia Date: Fri, 24 Apr 2026 23:24:40 -0400 Subject: [PATCH] fix(agent): persist user message on every turn, not just happy path MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- agent_service/agents/master_agent.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/agent_service/agents/master_agent.py b/agent_service/agents/master_agent.py index c8a8334..952b5d3 100644 --- a/agent_service/agents/master_agent.py +++ b/agent_service/agents/master_agent.py @@ -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: