diff --git a/navi/core/agent.py b/navi/core/agent.py index 70f32a9..49c0b5c 100644 --- a/navi/core/agent.py +++ b/navi/core/agent.py @@ -426,6 +426,23 @@ if image_msg: session.context.append(image_msg) + # 6. If switch_profile was called this iteration, reload profile + tools. + # switch_profile updates the DB but run_stream() holds a local session + # object — without this check the final save would overwrite the change + # and the next LLM call would still use the old tool schemas. + fresh = await self._sessions.get(session_id) + if fresh and fresh.profile_id != session.profile_id: + session.profile_id = fresh.profile_id + profile = self._profiles.get(session.profile_id) + tools = self._tool_list(profile.enabled_tools) + tool_schemas = [t.schema() for t in tools] + llm = self._get_backend(profile.llm_backend) + log.info( + "agent.profile_reloaded", + session_id=session_id, + new_profile=session.profile_id, + ) + await self._sessions.save(session) raise MaxIterationsReached(profile.max_iterations)