sort memory checkpoints monotonically decreasing

This commit is contained in:
vbarda
2024-07-22 12:14:33 -04:00
parent afe3905958
commit d635a22302
3 changed files with 143 additions and 141 deletions
@@ -119,7 +119,9 @@ class MemorySaver(BaseCheckpointSaver):
"""
thread_ids = (config["configurable"]["thread_id"],) if config else self.storage
for thread_id in thread_ids:
for ts, (checkpoint, metadata_b) in self.storage[thread_id].items():
for ts, (checkpoint, metadata_b) in sorted(
self.storage[thread_id].items(), key=lambda x: x[0], reverse=True
):
# filter by thread_ts
if before and ts >= before["configurable"]["thread_ts"]:
continue
+70 -70
View File
@@ -608,33 +608,7 @@ def test_invoke_two_processes_in_out_interrupt(mocker: MockerFixture) -> None:
# list history
assert [c for c in app.get_state_history({"configurable": {"thread_id": 1}})] == [
StateSnapshot(
values={"input": 2},
next=("one",),
config={
"configurable": {
"thread_id": 1,
"thread_ts": AnyStr(),
}
},
created_at=AnyStr(),
metadata={"source": "input", "step": -1, "writes": 2},
parent_config=None,
),
StateSnapshot(
values={"inbox": 3, "input": 2},
next=("two",),
config={
"configurable": {
"thread_id": 1,
"thread_ts": AnyStr(),
}
},
created_at=AnyStr(),
metadata={"source": "loop", "step": 0, "writes": None},
parent_config=None,
),
StateSnapshot(
values={"inbox": 3, "output": 4, "input": 2},
values={"inbox": 4, "output": 5, "input": 3},
next=(),
config={
"configurable": {
@@ -642,47 +616,8 @@ def test_invoke_two_processes_in_out_interrupt(mocker: MockerFixture) -> None:
"thread_ts": AnyStr(),
}
},
metadata={"source": "loop", "step": 6, "writes": 5},
created_at=AnyStr(),
metadata={"source": "loop", "step": 1, "writes": 4},
parent_config=None,
),
StateSnapshot(
values={"inbox": 3, "output": 4, "input": 20},
next=("one",),
config={
"configurable": {
"thread_id": 1,
"thread_ts": AnyStr(),
}
},
created_at=AnyStr(),
metadata={"source": "input", "step": 2, "writes": 20},
parent_config=None,
),
StateSnapshot(
values={"inbox": 21, "output": 4, "input": 20},
next=("two",),
config={
"configurable": {
"thread_id": 1,
"thread_ts": AnyStr(),
}
},
created_at=AnyStr(),
metadata={"source": "loop", "step": 3, "writes": None},
parent_config=None,
),
StateSnapshot(
values={"inbox": 21, "output": 4, "input": 3},
next=("one",),
config={
"configurable": {
"thread_id": 1,
"thread_ts": AnyStr(),
}
},
created_at=AnyStr(),
metadata={"source": "input", "step": 4, "writes": 3},
parent_config=None,
),
StateSnapshot(
@@ -694,12 +629,51 @@ def test_invoke_two_processes_in_out_interrupt(mocker: MockerFixture) -> None:
"thread_ts": AnyStr(),
}
},
created_at=AnyStr(),
metadata={"source": "loop", "step": 5, "writes": None},
created_at=AnyStr(),
parent_config=None,
),
StateSnapshot(
values={"inbox": 4, "output": 5, "input": 3},
values={"inbox": 21, "output": 4, "input": 3},
next=("one",),
config={
"configurable": {
"thread_id": 1,
"thread_ts": AnyStr(),
}
},
metadata={"source": "input", "step": 4, "writes": 3},
created_at=AnyStr(),
parent_config=None,
),
StateSnapshot(
values={"inbox": 21, "output": 4, "input": 20},
next=("two",),
config={
"configurable": {
"thread_id": 1,
"thread_ts": AnyStr(),
}
},
metadata={"source": "loop", "step": 3, "writes": None},
created_at=AnyStr(),
parent_config=None,
),
StateSnapshot(
values={"inbox": 3, "output": 4, "input": 20},
next=("one",),
config={
"configurable": {
"thread_id": 1,
"thread_ts": AnyStr(),
}
},
metadata={"source": "input", "step": 2, "writes": 20},
created_at=AnyStr(),
parent_config=None,
),
StateSnapshot(
values={"inbox": 3, "output": 4, "input": 2},
next=(),
config={
"configurable": {
@@ -707,8 +681,34 @@ def test_invoke_two_processes_in_out_interrupt(mocker: MockerFixture) -> None:
"thread_ts": AnyStr(),
}
},
metadata={"source": "loop", "step": 1, "writes": 4},
created_at=AnyStr(),
parent_config=None,
),
StateSnapshot(
values={"inbox": 3, "input": 2},
next=("two",),
config={
"configurable": {
"thread_id": 1,
"thread_ts": AnyStr(),
}
},
metadata={"source": "loop", "step": 0, "writes": None},
created_at=AnyStr(),
parent_config=None,
),
StateSnapshot(
values={"input": 2},
next=("one",),
config={
"configurable": {
"thread_id": 1,
"thread_ts": AnyStr(),
}
},
metadata={"source": "input", "step": -1, "writes": 2},
created_at=AnyStr(),
metadata={"source": "loop", "step": 6, "writes": 5},
parent_config=None,
),
]
+70 -70
View File
@@ -727,33 +727,7 @@ async def test_invoke_two_processes_in_out_interrupt(mocker: MockerFixture) -> N
c async for c in app.aget_state_history({"configurable": {"thread_id": 1}})
] == [
StateSnapshot(
values={"input": 2},
next=("one",),
config={
"configurable": {
"thread_id": 1,
"thread_ts": AnyStr(),
}
},
created_at=AnyStr(),
metadata={"source": "input", "step": -1, "writes": 2},
parent_config=None,
),
StateSnapshot(
values={"inbox": 3, "input": 2},
next=("two",),
config={
"configurable": {
"thread_id": 1,
"thread_ts": AnyStr(),
}
},
created_at=AnyStr(),
metadata={"source": "loop", "step": 0, "writes": None},
parent_config=None,
),
StateSnapshot(
values={"inbox": 3, "output": 4, "input": 2},
values={"inbox": 4, "output": 5, "input": 3},
next=(),
config={
"configurable": {
@@ -761,47 +735,8 @@ async def test_invoke_two_processes_in_out_interrupt(mocker: MockerFixture) -> N
"thread_ts": AnyStr(),
}
},
metadata={"source": "loop", "step": 6, "writes": 5},
created_at=AnyStr(),
metadata={"source": "loop", "step": 1, "writes": 4},
parent_config=None,
),
StateSnapshot(
values={"inbox": 3, "output": 4, "input": 20},
next=("one",),
config={
"configurable": {
"thread_id": 1,
"thread_ts": AnyStr(),
}
},
created_at=AnyStr(),
metadata={"source": "input", "step": 2, "writes": 20},
parent_config=None,
),
StateSnapshot(
values={"inbox": 21, "output": 4, "input": 20},
next=("two",),
config={
"configurable": {
"thread_id": 1,
"thread_ts": AnyStr(),
}
},
created_at=AnyStr(),
metadata={"source": "loop", "step": 3, "writes": None},
parent_config=None,
),
StateSnapshot(
values={"inbox": 21, "output": 4, "input": 3},
next=("one",),
config={
"configurable": {
"thread_id": 1,
"thread_ts": AnyStr(),
}
},
created_at=AnyStr(),
metadata={"source": "input", "step": 4, "writes": 3},
parent_config=None,
),
StateSnapshot(
@@ -813,12 +748,51 @@ async def test_invoke_two_processes_in_out_interrupt(mocker: MockerFixture) -> N
"thread_ts": AnyStr(),
}
},
created_at=AnyStr(),
metadata={"source": "loop", "step": 5, "writes": None},
created_at=AnyStr(),
parent_config=None,
),
StateSnapshot(
values={"inbox": 4, "output": 5, "input": 3},
values={"inbox": 21, "output": 4, "input": 3},
next=("one",),
config={
"configurable": {
"thread_id": 1,
"thread_ts": AnyStr(),
}
},
metadata={"source": "input", "step": 4, "writes": 3},
created_at=AnyStr(),
parent_config=None,
),
StateSnapshot(
values={"inbox": 21, "output": 4, "input": 20},
next=("two",),
config={
"configurable": {
"thread_id": 1,
"thread_ts": AnyStr(),
}
},
metadata={"source": "loop", "step": 3, "writes": None},
created_at=AnyStr(),
parent_config=None,
),
StateSnapshot(
values={"inbox": 3, "output": 4, "input": 20},
next=("one",),
config={
"configurable": {
"thread_id": 1,
"thread_ts": AnyStr(),
}
},
metadata={"source": "input", "step": 2, "writes": 20},
created_at=AnyStr(),
parent_config=None,
),
StateSnapshot(
values={"inbox": 3, "output": 4, "input": 2},
next=(),
config={
"configurable": {
@@ -826,8 +800,34 @@ async def test_invoke_two_processes_in_out_interrupt(mocker: MockerFixture) -> N
"thread_ts": AnyStr(),
}
},
metadata={"source": "loop", "step": 1, "writes": 4},
created_at=AnyStr(),
parent_config=None,
),
StateSnapshot(
values={"inbox": 3, "input": 2},
next=("two",),
config={
"configurable": {
"thread_id": 1,
"thread_ts": AnyStr(),
}
},
metadata={"source": "loop", "step": 0, "writes": None},
created_at=AnyStr(),
parent_config=None,
),
StateSnapshot(
values={"input": 2},
next=("one",),
config={
"configurable": {
"thread_id": 1,
"thread_ts": AnyStr(),
}
},
metadata={"source": "input", "step": -1, "writes": 2},
created_at=AnyStr(),
metadata={"source": "loop", "step": 6, "writes": 5},
parent_config=None,
),
]