* 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>
297 lines
9.4 KiB
Python
297 lines
9.4 KiB
Python
"""Unit tests for Hindsight LangGraph BaseStore adapter."""
|
|
|
|
import json
|
|
from unittest.mock import AsyncMock, MagicMock
|
|
|
|
import pytest
|
|
from hindsight_langgraph.errors import HindsightError
|
|
from hindsight_langgraph.store import (
|
|
HindsightStore,
|
|
_namespace_to_bank_id,
|
|
_parse_value,
|
|
)
|
|
|
|
|
|
def _mock_client():
|
|
client = MagicMock()
|
|
client.aretain = AsyncMock()
|
|
client.arecall = AsyncMock()
|
|
client.acreate_bank = AsyncMock()
|
|
return client
|
|
|
|
|
|
def _mock_recall_response(texts: list[str], document_ids: list[str] | None = None):
|
|
response = MagicMock()
|
|
results = []
|
|
for i, t in enumerate(texts):
|
|
r = MagicMock()
|
|
r.text = t
|
|
r.document_id = document_ids[i] if document_ids else None
|
|
r.occurred_start = None
|
|
results.append(r)
|
|
response.results = results
|
|
return response
|
|
|
|
|
|
class TestNamespaceMapping:
|
|
def test_simple_namespace(self):
|
|
assert _namespace_to_bank_id(("user", "123")) == "user.123"
|
|
|
|
def test_single_element(self):
|
|
assert _namespace_to_bank_id(("memories",)) == "memories"
|
|
|
|
def test_empty_namespace(self):
|
|
assert _namespace_to_bank_id(()) == "default"
|
|
|
|
def test_deep_namespace(self):
|
|
assert (
|
|
_namespace_to_bank_id(("org", "team", "user", "123")) == "org.team.user.123"
|
|
)
|
|
|
|
|
|
class TestParseValue:
|
|
def test_parses_json_dict(self):
|
|
assert _parse_value('{"name": "Alice"}') == {"name": "Alice"}
|
|
|
|
def test_wraps_plain_text(self):
|
|
assert _parse_value("hello world") == {"text": "hello world"}
|
|
|
|
def test_wraps_json_non_dict(self):
|
|
assert _parse_value("[1, 2, 3]") == {"text": "[1, 2, 3]"}
|
|
|
|
|
|
class TestHindsightStorePut:
|
|
@pytest.mark.asyncio
|
|
async def test_put_calls_retain(self):
|
|
client = _mock_client()
|
|
store = HindsightStore(client=client)
|
|
|
|
await store.aput(("user", "123"), "pref-1", {"color": "blue"})
|
|
|
|
client.aretain.assert_called_once()
|
|
call_kwargs = client.aretain.call_args[1]
|
|
assert call_kwargs["bank_id"] == "user.123"
|
|
assert call_kwargs["document_id"] == "pref-1"
|
|
assert json.loads(call_kwargs["content"]) == {"color": "blue"}
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_put_passes_tags(self):
|
|
client = _mock_client()
|
|
store = HindsightStore(client=client, tags=["source:langgraph"])
|
|
|
|
await store.aput(("user", "123"), "key", {"value": 1})
|
|
|
|
call_kwargs = client.aretain.call_args[1]
|
|
assert call_kwargs["tags"] == ["source:langgraph"]
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_put_tracks_namespace(self):
|
|
client = _mock_client()
|
|
store = HindsightStore(client=client)
|
|
|
|
await store.aput(("user", "123"), "key", {"value": 1})
|
|
|
|
namespaces = await store.alist_namespaces()
|
|
assert ("user", "123") in namespaces
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_put_none_value_is_delete_noop(self):
|
|
client = _mock_client()
|
|
store = HindsightStore(client=client)
|
|
|
|
await store.adelete(("user", "123"), "key")
|
|
|
|
client.aretain.assert_not_called()
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_put_raises_on_error(self):
|
|
client = _mock_client()
|
|
client.aretain.side_effect = RuntimeError("connection refused")
|
|
store = HindsightStore(client=client)
|
|
|
|
with pytest.raises(HindsightError, match="Store put failed"):
|
|
await store.aput(("user", "123"), "key", {"value": 1})
|
|
|
|
|
|
class TestHindsightStoreGet:
|
|
@pytest.mark.asyncio
|
|
async def test_get_returns_item_by_document_id(self):
|
|
client = _mock_client()
|
|
client.arecall.return_value = _mock_recall_response(
|
|
['{"color": "blue"}'], document_ids=["pref-1"]
|
|
)
|
|
store = HindsightStore(client=client)
|
|
|
|
item = await store.aget(("user", "123"), "pref-1")
|
|
|
|
assert item is not None
|
|
assert item.namespace == ("user", "123")
|
|
assert item.key == "pref-1"
|
|
assert item.value == {"color": "blue"}
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_returns_none_when_empty(self):
|
|
client = _mock_client()
|
|
client.arecall.return_value = _mock_recall_response([])
|
|
store = HindsightStore(client=client)
|
|
|
|
item = await store.aget(("user", "123"), "nonexistent")
|
|
|
|
assert item is None
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_handles_error_gracefully(self):
|
|
client = _mock_client()
|
|
client.arecall.side_effect = RuntimeError("timeout")
|
|
store = HindsightStore(client=client)
|
|
|
|
item = await store.aget(("user", "123"), "key")
|
|
|
|
assert item is None
|
|
|
|
|
|
class TestHindsightStoreSearch:
|
|
@pytest.mark.asyncio
|
|
async def test_search_returns_results(self):
|
|
client = _mock_client()
|
|
client.arecall.return_value = _mock_recall_response(
|
|
["User likes Python", "User is in NYC"]
|
|
)
|
|
store = HindsightStore(client=client)
|
|
|
|
results = await store.asearch(("user", "123"), query="preferences")
|
|
|
|
assert len(results) == 2
|
|
assert results[0].value == {"text": "User likes Python"}
|
|
assert results[1].value == {"text": "User is in NYC"}
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_search_respects_limit(self):
|
|
client = _mock_client()
|
|
client.arecall.return_value = _mock_recall_response(
|
|
["fact1", "fact2", "fact3", "fact4", "fact5"]
|
|
)
|
|
store = HindsightStore(client=client)
|
|
|
|
results = await store.asearch(("user", "123"), query="facts", limit=2)
|
|
|
|
assert len(results) == 2
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_search_empty_results(self):
|
|
client = _mock_client()
|
|
client.arecall.return_value = _mock_recall_response([])
|
|
store = HindsightStore(client=client)
|
|
|
|
results = await store.asearch(("user", "123"), query="anything")
|
|
|
|
assert results == []
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_search_with_filter(self):
|
|
client = _mock_client()
|
|
client.arecall.return_value = _mock_recall_response(
|
|
[
|
|
'{"type": "preference", "text": "likes Python"}',
|
|
'{"type": "fact", "text": "lives in NYC"}',
|
|
]
|
|
)
|
|
store = HindsightStore(client=client)
|
|
|
|
results = await store.asearch(
|
|
("user", "123"), query="info", filter={"type": "preference"}
|
|
)
|
|
|
|
assert len(results) == 1
|
|
assert results[0].value["type"] == "preference"
|
|
|
|
|
|
class TestHindsightStoreListNamespaces:
|
|
@pytest.mark.asyncio
|
|
async def test_lists_known_namespaces(self):
|
|
client = _mock_client()
|
|
store = HindsightStore(client=client)
|
|
|
|
await store.aput(("user", "123"), "k1", {"v": 1})
|
|
await store.aput(("user", "456"), "k2", {"v": 2})
|
|
|
|
namespaces = await store.alist_namespaces()
|
|
|
|
assert len(namespaces) == 2
|
|
assert ("user", "123") in namespaces
|
|
assert ("user", "456") in namespaces
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_list_respects_max_depth(self):
|
|
"""max_depth truncates deep namespaces and deduplicates per BaseStore contract."""
|
|
client = _mock_client()
|
|
store = HindsightStore(client=client)
|
|
|
|
await store.aput(("a",), "k", {"v": 1})
|
|
await store.aput(("a", "b", "c"), "k", {"v": 2})
|
|
await store.aput(("x", "y"), "k", {"v": 3})
|
|
|
|
namespaces = await store.alist_namespaces(max_depth=1)
|
|
|
|
# ("a",) stays as-is, ("a", "b", "c") truncated to ("a",) and deduped,
|
|
# ("x", "y") truncated to ("x",)
|
|
assert ("a",) in namespaces
|
|
assert ("x",) in namespaces
|
|
assert ("a", "b", "c") not in namespaces
|
|
assert ("x", "y") not in namespaces
|
|
assert len(namespaces) == 2
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_list_filters_by_prefix(self):
|
|
client = _mock_client()
|
|
store = HindsightStore(client=client)
|
|
|
|
await store.aput(("user", "123"), "k1", {"v": 1})
|
|
await store.aput(("user", "456"), "k2", {"v": 2})
|
|
await store.aput(("org", "abc"), "k3", {"v": 3})
|
|
|
|
namespaces = await store.alist_namespaces(prefix=("user",))
|
|
|
|
assert ("user", "123") in namespaces
|
|
assert ("user", "456") in namespaces
|
|
assert ("org", "abc") not in namespaces
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_list_filters_by_suffix(self):
|
|
client = _mock_client()
|
|
store = HindsightStore(client=client)
|
|
|
|
await store.aput(("user", "prefs"), "k1", {"v": 1})
|
|
await store.aput(("org", "prefs"), "k2", {"v": 2})
|
|
await store.aput(("user", "history"), "k3", {"v": 3})
|
|
|
|
namespaces = await store.alist_namespaces(suffix=("prefs",))
|
|
|
|
assert ("user", "prefs") in namespaces
|
|
assert ("org", "prefs") in namespaces
|
|
assert ("user", "history") not in namespaces
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_list_filters_by_prefix_and_suffix(self):
|
|
client = _mock_client()
|
|
store = HindsightStore(client=client)
|
|
|
|
await store.aput(("user", "prefs"), "k1", {"v": 1})
|
|
await store.aput(("org", "prefs"), "k2", {"v": 2})
|
|
await store.aput(("user", "history"), "k3", {"v": 3})
|
|
|
|
namespaces = await store.alist_namespaces(prefix=("user",), suffix=("prefs",))
|
|
|
|
assert namespaces == [("user", "prefs")]
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_list_respects_limit(self):
|
|
client = _mock_client()
|
|
store = HindsightStore(client=client)
|
|
|
|
for i in range(5):
|
|
await store.aput((f"ns-{i}",), "k", {"v": i})
|
|
|
|
namespaces = await store.alist_namespaces(limit=2)
|
|
|
|
assert len(namespaces) == 2
|