fix(langgraph): replace the compressed ToolMessage in graph state (#4620)

## Description

`create_compress_tool_messages_node` didn't actually shrink LangGraph
state. Each compressed `ToolMessage` was rebuilt with only `content` and
`tool_call_id`, so it lost its `id`. LangGraph's `add_messages` reducer
replaces a message with a matching `id` and appends one with a new `id`.
So after the node ran, state held the original tool result and a
compressed copy for the same `tool_call_id`. The model saw both: no
tokens saved, plus a duplicate tool result.

Found while collecting LangGraph runtime evidence for #4614. Same
behaviour on langgraph 1.2.11 and 1.2.19.

## Type of Change

- [x] Bug fix (non-breaking change that fixes an issue)

## Changes Made

- `headroom/integrations/langchain/langgraph.py`: build the compressed
message with `msg.model_copy(update={"content": compressed_text})`, so
`id`, `name`, `tool_call_id` and other fields carry over.
- `tests/test_integrations/langchain/test_langgraph.py`:
`test_node_replaces_tool_message_in_graph_state` runs the node inside a
compiled `StateGraph` with `add_messages` and asserts one compressed
`ToolMessage` remains.

## Testing

- [x] Unit tests pass (`pytest
tests/test_integrations/langchain/test_langgraph.py
tests/integrations/test_langgraph.py`): 27 passed
- [x] Linting passes (`ruff check`, `ruff format --check` on the touched
paths)
- [x] Type checking passes (`mypy
headroom/integrations/langchain/langgraph.py`: no errors in this file)
- [x] New test added: fails on `main` with `assert 4 == 3`, passes here
- [x] Manual testing performed

## Real Behavior Proof

- Environment: macOS arm64, Python 3.12.13, langgraph 1.2.19,
langgraph-checkpoint 4.3.1, langchain-core 1.6.1.
- Steps: a `StateGraph` agent → tool →
`create_compress_tool_messages_node()` → model, with an `InMemorySaver`
checkpointer. The tool returns 400 JSON rows; the graph is invoked once
and the checkpointed state is read back.
- Observed on `main`: `ToolMessage 30690 chars` **and** `ToolMessage
17933 chars`, both `tool_call_id='t1'`; 5 messages in state.
- Observed here: one `ToolMessage 17933 chars`, `tool_call_id='t1'`; 4
messages in state.
- Not tested: a live model call after the node.

## Runtime Rollout Safety

- Rollout-managed feature(s): none
- Minimum rollout channel: N/A
- Stable/default behavior changed: yes, for LangGraph users of the node:
state now holds only the compressed result, as documented
- Kill switch / disable path: remove the node from the graph
- Unsafe override required: no
- Qualification impact: none
- Rollback path: revert this PR

## Review Readiness

- [x] I have performed a self-review
- [x] This PR is ready for human review

## Checklist

- [x] I did **not** edit `CHANGELOG.md`

🤖 Generated with [Claude Code](https://claude.com/claude-code)

<!-- devin-review-badge-begin -->

---

<a href="https://app.devin.ai/review/headroomlabs-ai/headroom/pull/4620"
target="_blank"><picture><source media="(prefers-color-scheme: dark)"
srcset="https://static.devin.ai/assets/gh-devin-review-dark.svg?v=4"><img
src="https://static.devin.ai/assets/gh-devin-review-light.svg?v=4"
alt="Devin Review"></picture></a>
<!-- devin-review-badge-end -->

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: JD Davis <mxjerrett@gmail.com>
This commit is contained in:
Tejas Chopra
2026-11-03 18:24:19 -06:00
committed by GitHub
co-authored by Claude Opus 5.5 JD Davis
parent 2643f34a54
commit 70a8f0979e
2 changed files with 28 additions and 5 deletions
+3 -5
View File
@@ -313,11 +313,9 @@ def compress_tool_messages(
tokens_after = _estimate_tokens(compressed_text)
if was_modified and tokens_after < tokens_before:
# Create new ToolMessage with compressed content, preserving tool_call_id
compressed_msg = ToolMessage(
content=compressed_text,
tool_call_id=msg.tool_call_id,
)
# Copy so id, name and tool_call_id survive: LangGraph's add_messages
# replaces a message by id, and appends one without it as a duplicate.
compressed_msg = msg.model_copy(update={"content": compressed_text})
result_messages.append(compressed_msg)
tokens_saved = tokens_before - tokens_after
@@ -314,6 +314,31 @@ class TestCreateCompressToolMessagesNode:
assert result_state["messages"][2].tool_call_id == "call_xyz"
def test_node_replaces_tool_message_in_graph_state(self):
"""In a real graph the compressed result replaces the original, not joins it."""
from typing import Annotated, TypedDict
langgraph_graph = pytest.importorskip("langgraph.graph")
from langgraph.graph.message import add_messages
from headroom.integrations.langchain.langgraph import create_compress_tool_messages_node
class State(TypedDict):
messages: Annotated[list, add_messages]
large_output = _make_large_tool_output(200)
graph = langgraph_graph.StateGraph(State)
graph.add_node("compress", create_compress_tool_messages_node())
graph.add_edge(langgraph_graph.START, "compress")
graph.add_edge("compress", langgraph_graph.END)
out = graph.compile().invoke({"messages": _make_messages_with_tool_output(large_output)})
tool_messages = [m for m in out["messages"] if isinstance(m, ToolMessage)]
assert len(out["messages"]) == 3
assert len(tool_messages) == 1
assert len(tool_messages[0].content) < len(large_output)
def test_node_handles_empty_state(self):
"""Node should handle empty messages gracefully."""
from headroom.integrations.langchain.langgraph import create_compress_tool_messages_node