Skip to content

Cap run_python iterations with a best-effort fallback message #84

@vahid-ahmadi

Description

@vahid-ahmadi

Symptom (live test 2026-05-28)

The agent ran run_python 15+ times without converging on a usable reform, never producing a chart. The user sees a long visible stream of "Working…" steps with no terminal result before either Vercel times out or they give up.

Diagnosis

chat_message (backend/routes/chatbot.py:259) has a max_iterations loop but the failure mode for hitting it is silent — the user gets no closure when the agent runs out of budget. The agent can also keep churning if max_iterations is generous.

Fix

  1. Lower the soft cap. ~10–12 tool iterations per turn is plenty for any sane question; beyond that we're almost certainly looping.
  2. When the cap is hit, emit a final assistant message summarizing what was tried and what's known so far — don't just cut off mid-stream. Something like:

    "I ran into trouble constructing this reform. Here's what I tried and what I learned: [summary of attempts]. Could you (a) rephrase, (b) enable Plan mode for clarifying questions, or (c) provide a more specific scenario?"

  3. Log iteration count + final-status reason so we can see in metrics how often this fires.

Constraints

  • Don't expose the raw tool-call transcript in the fallback message — summarize.
  • Plan mode turns may legitimately need fewer iterations (different cap).
  • Continue (stop-then-continue) should not reset the iteration counter — the budget is per logical turn, not per stream.

Related

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions