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: