fleet-memory/hindsight-integrations/langgraph/tests/test_nodes.py
DK09876 b4320254b2
feat: add LangGraph integration (#610)
* feat: add LangGraph integration with tools, nodes, and store patterns

Add hindsight-langgraph SDK providing three integration patterns:
- Tools: retain/recall/reflect as LangChain tools for ReAct agents
- Nodes: automatic memory injection and storage as graph steps
- Store: LangGraph BaseStore implementation for checkpoint-based memory

Fix: remove `from __future__ import annotations` in nodes.py which
prevented LangGraph from passing RunnableConfig to node functions
(runtime type inspection saw string annotations instead of actual types).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* chore: register langgraph with independent versioning system

- Set version to 0.1.0 (integrations are versioned independently)
- Add langgraph to VALID_INTEGRATIONS in release-integration.sh
- Add changelog page for langgraph integration

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* chore: remove manual cookbook recipe page

The sync-cookbook script will auto-generate this from the notebook
in hindsight-cookbook once PR #17 is merged.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix: comprehensive improvements to langgraph integration

Code fixes:
- Retain node only stores latest messages instead of all history (prevents duplicates)
- Handle multimodal msg.content (list type) in nodes
- Fix store docstring separator "/" → "."
- Apply search filters before pagination in store
- Add ttl parameter to store.aput for LangGraph BaseStore compat
- Fix _ensure_bank to not cache failed bank creations
- Fix falsy value bugs (or → is not None) in tools
- Remove from __future__ import annotations from all files
- Consistent default budget="mid" across tools/nodes/store
- Bump langgraph floor to >=0.3.0, remove duplicate dev deps

Docs fixes:
- Fix broken Cloud client example (base_url is required)
- Complete API reference tables with all parameters
- Add Limitations and Notes section (async-only store, etc.)
- Add Requirements section
- Fix broken cookbook link and Cloud claim in blog post

All 61 unit tests pass. E2E tested against Hindsight Cloud:
tools, nodes, store, configure(), multimodal content.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* chore: remove blog post (lives in hindsight-marketing-content)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* chore: remove Hindsight Cloud section from langgraph docs

Keep OSS docs self-hosted-first, consistent with other integration
docs (crewai, pydantic-ai, agno). Cloud setup details live in the
cookbook notebooks.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* docs: explicitly mention LangChain compatibility in langgraph integration

The tools pattern (create_hindsight_tools) only depends on
langchain-core and works with plain LangChain via bind_tools() —
no LangGraph required. Update docs to make this clear with both
LangGraph and LangChain quick start examples.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix: address PR review findings

1. Guard manual test files with if __name__ == "__main__" so pytest
   doesn't collect and execute them during test runs
2. Remove semantic fallback in HindsightStore.aget() — only return
   exact document_id matches, not unrelated semantic search hits
3. Make langgraph an optional dependency — tools pattern only needs
   langchain-core. Install with pip install hindsight-langgraph[langgraph]
   for nodes and store patterns. Lazy imports with clear error messages.
4. Clean up README to be self-hosted-first, consistent with other
   integration docs
5. Update docs requirements section to reflect optional langgraph dep

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix: address PR review feedback for langgraph integration

- Fix #2: Add per-bank asyncio.Lock to _ensure_bank for concurrency safety
- Fix #3: Clamp search score to max(0.0, ...) to prevent negative values
- Fix #4: Implement suffix matching in _handle_list_namespaces
- Fix #5: Truncate namespaces to max_depth instead of filtering (per BaseStore contract)
- Fix #6: Remove list_namespaces/alist_namespaces overrides — let base class handle prefix=/suffix= kwargs
- Fix #7: Document ephemeral namespace tracking and get() limitations in class docstring
- Fix #8: Add stable ID to recall node SystemMessage, document ordering behavior
- Fix #9: Change budget/max_tokens/recall_tags_match defaults to None so global config fallback works
- Fix #10: Conditionally populate __all__ so import * works without langgraph installed
- Fix #11: Bump langgraph lower bound from >=0.3.0 to >=0.5.0
- Fix #12: Extract _resolve_client to shared _client.py module

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix: address remaining review gaps for langgraph integration

- Add output_key parameter to create_recall_node for prompt ordering control
- Add prefix/suffix/combined filter tests for list_namespaces
- Add output_key unit tests (memory text, none on empty, none on error)
- Remove unused imports and backward-compat alias in tools.py
- Update docs with output_key usage example and API reference

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix: relax langgraph version constraint to >=0.3.0

Research confirmed all required APIs (BaseStore, SearchItem, Result,
GetOp, PutOp, SearchOp, ListNamespacesOp) are available since
langgraph-checkpoint 2.0.7, which maps to langgraph >=0.2.63.
Using >=0.3.0 as a clean semver boundary — >=0.5.0 was unnecessarily
conservative and excluded many compatible versions.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-20 13:36:57 +01:00

269 lines
8.8 KiB
Python

"""Unit tests for Hindsight LangGraph nodes."""
from unittest.mock import AsyncMock, MagicMock
import pytest
from hindsight_langgraph import create_recall_node, create_retain_node
from langchain_core.messages import AIMessage, HumanMessage, SystemMessage
def _mock_client():
client = MagicMock()
client.aretain = AsyncMock()
client.arecall = AsyncMock()
return client
def _mock_recall_response(texts: list[str]):
response = MagicMock()
results = []
for t in texts:
r = MagicMock()
r.text = t
results.append(r)
response.results = results
return response
class TestRecallNode:
@pytest.mark.asyncio
async def test_injects_memories_as_system_message(self):
client = _mock_client()
client.arecall.return_value = _mock_recall_response(
["User likes Python", "User is in NYC"]
)
node = create_recall_node(bank_id="test-bank", client=client)
state = {"messages": [HumanMessage(content="What do you remember about me?")]}
result = await node(state)
assert len(result["messages"]) == 1
msg = result["messages"][0]
assert isinstance(msg, SystemMessage)
assert "User likes Python" in msg.content
assert "User is in NYC" in msg.content
@pytest.mark.asyncio
async def test_returns_empty_when_no_human_message(self):
client = _mock_client()
node = create_recall_node(bank_id="test-bank", client=client)
state = {"messages": [SystemMessage(content="You are a helpful assistant")]}
result = await node(state)
assert result["messages"] == []
client.arecall.assert_not_called()
@pytest.mark.asyncio
async def test_returns_empty_when_no_results(self):
client = _mock_client()
client.arecall.return_value = _mock_recall_response([])
node = create_recall_node(bank_id="test-bank", client=client)
state = {"messages": [HumanMessage(content="hello")]}
result = await node(state)
assert result["messages"] == []
@pytest.mark.asyncio
async def test_respects_max_results(self):
client = _mock_client()
client.arecall.return_value = _mock_recall_response(
["fact1", "fact2", "fact3", "fact4", "fact5"]
)
node = create_recall_node(bank_id="test-bank", client=client, max_results=2)
state = {"messages": [HumanMessage(content="query")]}
result = await node(state)
msg = result["messages"][0]
assert "1. fact1" in msg.content
assert "2. fact2" in msg.content
assert "3." not in msg.content
@pytest.mark.asyncio
async def test_resolves_bank_id_from_config(self):
client = _mock_client()
client.arecall.return_value = _mock_recall_response(["fact"])
node = create_recall_node(client=client, bank_id_from_config="user_id")
state = {"messages": [HumanMessage(content="hello")]}
config = {"configurable": {"user_id": "user-456"}}
await node(state, config=config)
call_kwargs = client.arecall.call_args[1]
assert call_kwargs["bank_id"] == "user-456"
@pytest.mark.asyncio
async def test_skips_when_no_bank_id(self):
client = _mock_client()
node = create_recall_node(client=client)
state = {"messages": [HumanMessage(content="hello")]}
result = await node(state)
assert result["messages"] == []
client.arecall.assert_not_called()
@pytest.mark.asyncio
async def test_handles_recall_error_gracefully(self):
client = _mock_client()
client.arecall.side_effect = RuntimeError("connection refused")
node = create_recall_node(bank_id="test-bank", client=client)
state = {"messages": [HumanMessage(content="hello")]}
result = await node(state)
assert result["messages"] == []
@pytest.mark.asyncio
async def test_passes_tags(self):
client = _mock_client()
client.arecall.return_value = _mock_recall_response(["fact"])
node = create_recall_node(
bank_id="test-bank",
client=client,
tags=["scope:user"],
tags_match="all",
)
state = {"messages": [HumanMessage(content="hello")]}
await node(state)
call_kwargs = client.arecall.call_args[1]
assert call_kwargs["tags"] == ["scope:user"]
assert call_kwargs["tags_match"] == "all"
class TestRecallNodeOutputKey:
@pytest.mark.asyncio
async def test_output_key_returns_memory_text(self):
client = _mock_client()
client.arecall.return_value = _mock_recall_response(
["User likes Python", "User is in NYC"]
)
node = create_recall_node(
bank_id="test-bank", client=client, output_key="memory_context"
)
state = {"messages": [HumanMessage(content="What do you remember?")]}
result = await node(state)
assert "messages" not in result
assert "memory_context" in result
assert "User likes Python" in result["memory_context"]
assert "User is in NYC" in result["memory_context"]
@pytest.mark.asyncio
async def test_output_key_returns_none_when_no_results(self):
client = _mock_client()
client.arecall.return_value = _mock_recall_response([])
node = create_recall_node(
bank_id="test-bank", client=client, output_key="memory_context"
)
state = {"messages": [HumanMessage(content="hello")]}
result = await node(state)
assert result == {"memory_context": None}
@pytest.mark.asyncio
async def test_output_key_returns_none_on_error(self):
client = _mock_client()
client.arecall.side_effect = RuntimeError("connection refused")
node = create_recall_node(
bank_id="test-bank", client=client, output_key="memory_context"
)
state = {"messages": [HumanMessage(content="hello")]}
result = await node(state)
assert result == {"memory_context": None}
class TestRetainNode:
@pytest.mark.asyncio
async def test_retains_human_messages(self):
client = _mock_client()
node = create_retain_node(bank_id="test-bank", client=client)
state = {
"messages": [
HumanMessage(content="I like pizza"),
AIMessage(content="Got it!"),
]
}
await node(state)
client.aretain.assert_called_once()
call_kwargs = client.aretain.call_args[1]
assert call_kwargs["bank_id"] == "test-bank"
assert "I like pizza" in call_kwargs["content"]
assert "Got it!" not in call_kwargs["content"]
@pytest.mark.asyncio
async def test_retains_both_when_configured(self):
client = _mock_client()
node = create_retain_node(
bank_id="test-bank", client=client, retain_human=True, retain_ai=True
)
state = {
"messages": [
HumanMessage(content="I like pizza"),
AIMessage(content="Got it!"),
]
}
await node(state)
call_kwargs = client.aretain.call_args[1]
assert "I like pizza" in call_kwargs["content"]
assert "Got it!" in call_kwargs["content"]
@pytest.mark.asyncio
async def test_skips_when_no_messages_match(self):
client = _mock_client()
node = create_retain_node(
bank_id="test-bank", client=client, retain_human=False, retain_ai=False
)
state = {"messages": [HumanMessage(content="hello")]}
await node(state)
client.aretain.assert_not_called()
@pytest.mark.asyncio
async def test_passes_tags(self):
client = _mock_client()
node = create_retain_node(
bank_id="test-bank", client=client, tags=["source:chat"]
)
state = {"messages": [HumanMessage(content="hello")]}
await node(state)
call_kwargs = client.aretain.call_args[1]
assert call_kwargs["tags"] == ["source:chat"]
@pytest.mark.asyncio
async def test_resolves_bank_id_from_config(self):
client = _mock_client()
node = create_retain_node(client=client, bank_id_from_config="user_id")
state = {"messages": [HumanMessage(content="hello")]}
config = {"configurable": {"user_id": "user-789"}}
await node(state, config=config)
call_kwargs = client.aretain.call_args[1]
assert call_kwargs["bank_id"] == "user-789"
@pytest.mark.asyncio
async def test_handles_retain_error_gracefully(self):
client = _mock_client()
client.aretain.side_effect = RuntimeError("connection refused")
node = create_retain_node(bank_id="test-bank", client=client)
state = {"messages": [HumanMessage(content="hello")]}
# Should not raise
result = await node(state)
assert result["messages"] == []