Skip to content

fix(agent): preserve multi-round ReAct tool results in message history - #59

Draft
cursor[bot] wants to merge 1 commit into
mainfrom
cacritical-bug-management-4ed0
Draft

fix(agent): preserve multi-round ReAct tool results in message history#59
cursor[bot] wants to merge 1 commit into
mainfrom
cacritical-bug-management-4ed0

Conversation

@cursor

@cursor cursor Bot commented Jul 25, 2026

Copy link
Copy Markdown

Bug and impact

When the ReAct loop ran more than one tool round in a single agent_run, prior tool-result messages were corrupted before the next provider chat() call. The model received wrong tool outputs, breaking multi-step autonomy (cron + shell, chained tool use, etc.).

Root cause

agent_react_loop stored tool results as pointers into a fixed tool_result_bufs scratch slab. Each new iteration overwrote that slab while older message slots still referenced it. A premature free(prev_assistant) also freed assistant text still referenced in ctx->messages.

Fix and validation

  • strdup each tool result when appending to the message list.
  • Track base_msg_count and free heap-owned ReAct slots (assistant text, tool results, tool_calls) in agent_run_cleanup.
  • Add test_react_loop_preserves_prior_tool_results — fails on main, passes with fix.

Validation: make test_agent — all tests passed (new regression test included).

Open in Web View Automation 

Tool result messages pointed into a reusable scratch buffer that was
overwritten on each iteration, corrupting prior rounds before the next
provider chat. strdup tool outputs and free heap-owned ReAct slots on
cleanup.

Co-authored-by: esadrianno <esadrianno@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant