[P0] ZARA-008 — Preserve valid multi-tool histories across conversation turns #9

Closed
opened 2026-07-18 19:19:23 +00:00 by lost-rob0t · 0 comments
lost-rob0t commented 2026-07-18 19:19:23 +00:00 (Migrated from github.com)

Problem

zara/agent/graph.py::validate_and_clean_messages() only keeps a ToolMessage when the immediately preceding kept message is the matching AIMessage. For one AI response containing multiple tool calls, the first result is kept and every later valid tool result is dropped because its predecessor is another ToolMessage.

The current graph can complete the same turn, but the persisted history is corrupted on the next turn and can trigger provider protocol errors or lose tool evidence.

Required fix

  • Validate tool-result groups against the preceding AI tool-call set.
  • Preserve all matching tool results in provider-compatible order.
  • Reject true orphans, duplicates, unknown IDs, and results that cross turn boundaries.
  • Keep the LangGraph add_messages reducer.

Required tests

  • Unit tests for zero, one, and multiple tool calls; out-of-order results; duplicate IDs; orphaned results; partial results; and mixed historical turns.
  • Provider-format regression tests for Anthropic/OpenAI-compatible message sequences without making network calls.
  • Add scripts/test-agent-history.sh to run the focused history and graph tests.

Acceptance

  • Two or more tool calls survive cleanup and the next conversation turn.
  • Invalid tool results are removed with precise logs.
  • Existing single-tool behavior remains intact.

Branch

fix/zara-008-multi-tool-history

Dependencies

ZARA-001, ZARA-003.

## Problem `zara/agent/graph.py::validate_and_clean_messages()` only keeps a `ToolMessage` when the immediately preceding kept message is the matching `AIMessage`. For one AI response containing multiple tool calls, the first result is kept and every later valid tool result is dropped because its predecessor is another `ToolMessage`. The current graph can complete the same turn, but the persisted history is corrupted on the next turn and can trigger provider protocol errors or lose tool evidence. ## Required fix - Validate tool-result groups against the preceding AI tool-call set. - Preserve all matching tool results in provider-compatible order. - Reject true orphans, duplicates, unknown IDs, and results that cross turn boundaries. - Keep the LangGraph `add_messages` reducer. ## Required tests - Unit tests for zero, one, and multiple tool calls; out-of-order results; duplicate IDs; orphaned results; partial results; and mixed historical turns. - Provider-format regression tests for Anthropic/OpenAI-compatible message sequences without making network calls. - Add `scripts/test-agent-history.sh` to run the focused history and graph tests. ## Acceptance - Two or more tool calls survive cleanup and the next conversation turn. - Invalid tool results are removed with precise logs. - Existing single-tool behavior remains intact. ## Branch `fix/zara-008-multi-tool-history` ## Dependencies ZARA-001, ZARA-003.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
nsaspy/zara#9
No description provided.