|
| 1 | +"""Integration tests for CrossSlot error fix in checkpoint operations.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | +from langchain_core.runnables import RunnableConfig |
| 5 | +from langgraph.checkpoint.base import ( |
| 6 | + Checkpoint, |
| 7 | + CheckpointMetadata, |
| 8 | + create_checkpoint, |
| 9 | + empty_checkpoint, |
| 10 | +) |
| 11 | + |
| 12 | +from langgraph.checkpoint.redis import RedisSaver |
| 13 | + |
| 14 | + |
| 15 | +def test_checkpoint_operations_no_crossslot_errors(redis_url: str) -> None: |
| 16 | + """Test that checkpoint operations work without CrossSlot errors. |
| 17 | +
|
| 18 | + This test verifies that the fix for using search indexes instead of keys() |
| 19 | + works correctly in a real Redis environment. |
| 20 | + """ |
| 21 | + # Create a saver |
| 22 | + saver = RedisSaver(redis_url) |
| 23 | + saver.setup() |
| 24 | + |
| 25 | + # Create test data |
| 26 | + thread_id = "test-thread-crossslot" |
| 27 | + checkpoint_ns = "test-ns" |
| 28 | + |
| 29 | + # Create checkpoints with unique IDs |
| 30 | + checkpoint1 = create_checkpoint(empty_checkpoint(), {}, 1) |
| 31 | + checkpoint2 = create_checkpoint(checkpoint1, {"messages": ["hello"]}, 2) |
| 32 | + checkpoint3 = create_checkpoint(checkpoint2, {"messages": ["hello", "world"]}, 3) |
| 33 | + |
| 34 | + # Create metadata |
| 35 | + metadata1 = {"source": "input", "step": 1, "writes": {"task1": "value1"}} |
| 36 | + metadata2 = {"source": "loop", "step": 2, "writes": {"task2": "value2"}} |
| 37 | + metadata3 = {"source": "loop", "step": 3, "writes": {"task3": "value3"}} |
| 38 | + |
| 39 | + # Put checkpoints with writes |
| 40 | + config1 = {"configurable": {"thread_id": thread_id, "checkpoint_ns": checkpoint_ns}} |
| 41 | + config2 = {"configurable": {"thread_id": thread_id, "checkpoint_ns": checkpoint_ns}} |
| 42 | + config3 = {"configurable": {"thread_id": thread_id, "checkpoint_ns": checkpoint_ns}} |
| 43 | + |
| 44 | + # Put checkpoints first to get configs with checkpoint_ids |
| 45 | + saved_config1 = saver.put(config1, checkpoint1, metadata1, {}) |
| 46 | + saved_config2 = saver.put(config2, checkpoint2, metadata2, {}) |
| 47 | + saved_config3 = saver.put(config3, checkpoint3, metadata3, {}) |
| 48 | + |
| 49 | + # Add some pending writes using saved configs |
| 50 | + saver.put_writes( |
| 51 | + saved_config1, |
| 52 | + [ |
| 53 | + ("channel1", {"value": "data1"}), |
| 54 | + ("channel2", {"value": "data2"}), |
| 55 | + ], |
| 56 | + "task-1", |
| 57 | + ) |
| 58 | + |
| 59 | + # Now test operations that previously used keys() and would fail in cluster mode |
| 60 | + |
| 61 | + # Test 1: Load pending writes (uses _load_pending_writes) |
| 62 | + # This should work without CrossSlot errors |
| 63 | + tuple1 = saver.get_tuple(saved_config1) |
| 64 | + assert tuple1 is not None |
| 65 | + # Verify pending writes were loaded |
| 66 | + assert len(tuple1.pending_writes) == 2 |
| 67 | + pending_channels = [w[1] for w in tuple1.pending_writes] |
| 68 | + assert "channel1" in pending_channels |
| 69 | + assert "channel2" in pending_channels |
| 70 | + |
| 71 | + # Test 2: Get tuple with TTL (uses get_tuple which searches for blob and write keys) |
| 72 | + saver_with_ttl = RedisSaver(redis_url, ttl={"checkpoint": 3600}) |
| 73 | + saver_with_ttl.setup() |
| 74 | + |
| 75 | + # Put a checkpoint with TTL |
| 76 | + config_ttl = { |
| 77 | + "configurable": {"thread_id": "ttl-thread", "checkpoint_ns": "ttl-ns"} |
| 78 | + } |
| 79 | + saver_with_ttl.put(config_ttl, checkpoint1, metadata1, {}) |
| 80 | + |
| 81 | + # Get the checkpoint - this triggers TTL application which uses key searches |
| 82 | + tuple_ttl = saver_with_ttl.get_tuple(config_ttl) |
| 83 | + assert tuple_ttl is not None |
| 84 | + |
| 85 | + # Test 3: List checkpoints - this should work without CrossSlot errors |
| 86 | + # List returns only the latest checkpoint by default |
| 87 | + checkpoints = list(saver.list(config1)) |
| 88 | + assert len(checkpoints) >= 1 |
| 89 | + |
| 90 | + # The latest checkpoint should have the pending writes from checkpoint1 |
| 91 | + latest_checkpoint = checkpoints[0] |
| 92 | + assert len(latest_checkpoint.pending_writes) == 2 |
| 93 | + |
| 94 | + # The important part is that all these operations work without CrossSlot errors |
| 95 | + # In a Redis cluster, the old keys() based approach would have failed by now |
| 96 | + |
| 97 | + |
| 98 | +def test_subgraph_checkpoint_operations(redis_url: str) -> None: |
| 99 | + """Test checkpoint operations with subgraphs work without CrossSlot errors.""" |
| 100 | + saver = RedisSaver(redis_url) |
| 101 | + saver.setup() |
| 102 | + |
| 103 | + # Create nested namespace checkpoints |
| 104 | + thread_id = "test-thread-subgraph" |
| 105 | + |
| 106 | + # Parent checkpoint |
| 107 | + parent_config = { |
| 108 | + "configurable": { |
| 109 | + "thread_id": thread_id, |
| 110 | + "checkpoint_ns": "", |
| 111 | + } |
| 112 | + } |
| 113 | + parent_checkpoint = empty_checkpoint() |
| 114 | + parent_metadata = {"source": "input", "step": 1} |
| 115 | + |
| 116 | + # Child checkpoint in subgraph |
| 117 | + child_config = { |
| 118 | + "configurable": { |
| 119 | + "thread_id": thread_id, |
| 120 | + "checkpoint_ns": "subgraph1", |
| 121 | + } |
| 122 | + } |
| 123 | + child_checkpoint = create_checkpoint(parent_checkpoint, {"subgraph": "data"}, 1) |
| 124 | + child_metadata = {"source": "loop", "step": 1} |
| 125 | + |
| 126 | + # Grandchild checkpoint in nested subgraph |
| 127 | + grandchild_config = { |
| 128 | + "configurable": { |
| 129 | + "thread_id": thread_id, |
| 130 | + "checkpoint_ns": "subgraph1:subgraph2", |
| 131 | + } |
| 132 | + } |
| 133 | + grandchild_checkpoint = create_checkpoint(child_checkpoint, {"nested": "data"}, 2) |
| 134 | + grandchild_metadata = {"source": "loop", "step": 2} |
| 135 | + |
| 136 | + # Put all checkpoints first to get saved configs |
| 137 | + saved_parent_config = saver.put( |
| 138 | + parent_config, parent_checkpoint, parent_metadata, {} |
| 139 | + ) |
| 140 | + saved_child_config = saver.put(child_config, child_checkpoint, child_metadata, {}) |
| 141 | + saved_grandchild_config = saver.put( |
| 142 | + grandchild_config, grandchild_checkpoint, grandchild_metadata, {} |
| 143 | + ) |
| 144 | + |
| 145 | + # Put checkpoints with writes using saved configs |
| 146 | + saver.put_writes( |
| 147 | + saved_parent_config, [("parent_channel", {"parent": "data"})], "parent-task" |
| 148 | + ) |
| 149 | + saver.put_writes( |
| 150 | + saved_child_config, [("child_channel", {"child": "data"})], "child-task" |
| 151 | + ) |
| 152 | + saver.put_writes( |
| 153 | + saved_grandchild_config, |
| 154 | + [("grandchild_channel", {"grandchild": "data"})], |
| 155 | + "grandchild-task", |
| 156 | + ) |
| 157 | + |
| 158 | + # Test loading checkpoints with pending writes from different namespaces |
| 159 | + parent_tuple = saver.get_tuple(parent_config) |
| 160 | + assert parent_tuple is not None |
| 161 | + |
| 162 | + child_tuple = saver.get_tuple(child_config) |
| 163 | + assert child_tuple is not None |
| 164 | + |
| 165 | + grandchild_tuple = saver.get_tuple(grandchild_config) |
| 166 | + assert grandchild_tuple is not None |
| 167 | + |
| 168 | + # List all checkpoints - should work without CrossSlot errors |
| 169 | + all_checkpoints = list(saver.list({"configurable": {"thread_id": thread_id}})) |
| 170 | + assert len(all_checkpoints) >= 3 |
0 commit comments