Scenario
You're reviewing an agent loop: on each step it calls decide_next_action(history) (a stand-in for the LLM call that actually picks the next move), runs whatever tool that returns, and repeats until the model says it's ready to answer.
In testing, it never gives a final answer — it just keeps calling search_docs with the same query until run_agent gives up at max_steps. (This is the real shape of a bug interviewers ask about directly: "this agent loops forever on some inputs — find out why and fix it.")
Read the loop all the way through before changing anything:
decide_next_action(history)looks at the last tool result inhistoryto decide whether it has enough to answer. This logic is already correct — don't change it.search_docs(query)is a mocked tool: the first call "misses," the second call "finds" the answer. Also already correct.run_agent(max_steps)is the loop that ties them together. Something in here is why the model never sees that the second call actually succeeded.
Your task: find the one missing line that explains why decide_next_action keeps seeing an empty history no matter how many times the loop runs, and fix run_agent so it terminates with a final answer well before max_steps. Don't change decide_next_action or search_docs — the bug isn't in either of them.
It's safe to hit Run right now — the loop is capped at max_steps, so you'll see it "give up" rather than actually hang.