Skip to content

fix(agent): strip orphan tool_calls in repair_message_sequence (#56980) - #800

Closed
hashbender wants to merge 1 commit into
mainfrom
mirror/pr-56990
Closed

fix(agent): strip orphan tool_calls in repair_message_sequence (#56980)#800
hashbender wants to merge 1 commit into
mainfrom
mirror/pr-56990

Conversation

@hashbender

Copy link
Copy Markdown
Owner

What does this PR do?

Adds Pass 3 to repair_message_sequence that strips orphan tool_calls from assistant messages whose tool_call_ids have no matching tool result messages in the sequence. This prevents HTTP 400 "insufficient tool messages following tool_calls message" from strict providers (DeepSeek v4, Kimi) when context compaction, partial session resume, or retry loops leave dangling tool-call references.

Related Issue

Fixes NousResearch#56980

Type of Change

  • 🐛 Bug fix (non-breaking change that fixes an issue)

Changes Made

  • agent/agent_runtime_helpers.py: Added Pass 3 in repair_message_sequence — builds a set of all tool_call_ids present in tool messages, then strips tool_calls entries from assistant messages whose ids are absent. Keeps assistant text content intact; only removes the orphan calls.
  • tests/run_agent/test_message_sequence_repair.py: Added 3 tests covering the new pass — orphan tool_calls at sequence end, parallel tool_calls with partial results missing, and no-op when all tool_calls have matching results.

How to Test

  1. Run python -m pytest tests/run_agent/test_message_sequence_repair.py -v — all 28 tests should pass, including the 3 new ones (test_repair_strips_orphan_tool_calls_at_end, test_repair_strips_orphan_parallel_tool_calls, test_repair_preserves_complete_tool_calls).
  2. The fix is verified by the new test test_repair_strips_orphan_tool_calls_at_end which simulates the exact scenario from the issue: an assistant message with tool_calls at the end of the sequence with no matching tool result.

Checklist

Code

  • I've read the Contributing Guide
  • My commit messages follow Conventional Commits (fix(scope):, feat(scope):, etc.)
  • I searched for existing PRs to make sure this isn't a duplicate
  • My PR contains only changes related to this fix/feature (no unrelated commits)
  • I've run pytest tests/ -q and all tests pass
  • I've added tests for my changes (required for bug fixes, strongly encouraged for features)
  • I've tested on my platform: macOS 26.4.1

Documentation & Housekeeping

  • I've updated relevant documentation (README, docs/, docstrings) — or N/A
  • I've updated cli-config.yaml.example if I added/changed config keys — or N/A
  • I've updated CONTRIBUTING.md or AGENTS.md if I changed architecture or workflows — or N/A
  • I've considered cross-platform impact (Windows, macOS) per the compatibility guide — or N/A
  • I've updated tool descriptions/schemas if I changed tool behavior — or N/A

Mirror-of: NousResearch#56990
NousResearch#56990

@hashbender hashbender closed this Jul 2, 2026
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.

[Bug]: repair_message_sequence fails to prevent HTTP 400 "insufficient tool messages following tool_calls" on DeepSeek v4 after long tool-call sessions

1 participant