Build applications that make decisions (chatbots, agents, simulations, etc...). Monitor, trace, persist, and execute on your own infrastructure.
Python aiburrchatbot-frameworkdagsgenerative-aigraphshacktoberfestllmopsllmsmlopspersistent-data-structurestate-machinestate-managementvisibility
fix: select latest SQLite checkpoint when timestamps tie (#920)
Kuang-xianxin42a2e71391eb
diff --git a/burr/core/persistence.py b/burr/core/persistence.pyindex 86f517d3..ef08f6b7 100644--- a/burr/core/persistence.py+++ b/burr/core/persistence.py@@ -496,11 +496,11 @@ class SQLitePersister(BaseStatePersister, BaseCopyable):cursor = self.connection.cursor()try:if app_id is None:- # get latest for all app_ids+ # CURRENT_TIMESTAMP has second precision; break ties by insertion order.cursor.execute(f"SELECT position, state, sequence_id, app_id, created_at, status FROM {self.table_name} "f"WHERE partition_key = ? "- f"ORDER BY CREATED_AT DESC LIMIT 1",+ f"ORDER BY created_at DESC, rowid DESC LIMIT 1",(partition_key,),)elif sequence_id is None:diff --git a/burr/integrations/persisters/b_aiosqlite.py b/burr/integrations/persisters/b_aiosqlite.pyindex e6f56d47..33f01961 100644--- a/burr/integrations/persisters/b_aiosqlite.py+++ b/burr/integrations/persisters/b_aiosqlite.py@@ -231,11 +231,11 @@ class AsyncSQLitePersister(AsyncBaseStatePersister, BaseCopyable):logger.debug("Loading %s, %s, %s", partition_key, app_id, sequence_id)cursor = await self.connection.cursor()if app_id is None:- # get latest for all app_ids+ # CURRENT_TIMESTAMP has second precision; break ties by insertion order.await cursor.execute(f"SELECT position, state, sequence_id, app_id, created_at, status FROM {self.table_name} "f"WHERE partition_key = ? "- f"ORDER BY CREATED_AT DESC LIMIT 1",+ f"ORDER BY created_at DESC, rowid DESC LIMIT 1",(partition_key,),)elif sequence_id is None:diff --git a/tests/core/test_persistence.py b/tests/core/test_persistence.pyindex fe6a63f8..0d462083 100644--- a/tests/core/test_persistence.py+++ b/tests/core/test_persistence.py@@ -178,6 +178,46 @@ def test_sqlite_persister_list_app_ids_without_initialize_raises_runtime_error()persister.cleanup()[email protected]("partition_key", [None, "partition"])[email protected]("latest_app,latest_sequence", [("a-app", 12), ("z-app", 0)])+def test_sqlite_load_latest_breaks_timestamp_ties(+ tmp_path, partition_key, latest_app, latest_sequence+):+ db_path = str(tmp_path / "checkpoints.db")+ with SQLLitePersister(db_path=db_path, table_name="test_table") as persister:+ persister.initialize()+ persister.save(partition_key, "a-app", 11, "first", State({"step": 1}), "completed")+ persister.save(+ partition_key, latest_app, latest_sequence, "second", State({"step": 2}), "completed"+ )+ persister.save("other-partition", "other-app", 999, "other", State({}), "completed")+ # Model saves within the same second without depending on wall-clock timing.+ persister.connection.execute("UPDATE test_table SET created_at = '2026-01-01 00:00:00'")+ persister.connection.commit()++ with SQLLitePersister(db_path=db_path, table_name="test_table") as persister:+ loaded = persister.load(partition_key, None)+ assert loaded["app_id"] == latest_app+ assert loaded["sequence_id"] == latest_sequence+ assert loaded["position"] == "second"+ assert loaded["state"] == State({"step": 2})+ assert loaded["status"] == "completed"+ assert persister.load(partition_key, "a-app", 11)["state"] == State({"step": 1})+++def test_sqlite_load_latest_prioritizes_timestamp(initializing_persistence):+ persister = initializing_persistence+ persister.initialize()+ persister.save("partition", "a-app", 1, "first", State({"step": 1}), "completed")+ persister.save("partition", "z-app", 0, "second", State({"step": 2}), "completed")+ persister.connection.execute(+ "UPDATE test_table SET created_at = CASE app_id "+ "WHEN 'a-app' THEN '2026-01-02 00:00:00' ELSE '2026-01-01 00:00:00' END"+ )+ persister.connection.commit()+ assert persister.load("partition", None)["state"] == State({"step": 1})++@pytest.mark.parametrize("method_name,kwargs",[diff --git a/tests/integrations/persisters/test_b_aiosqlite.py b/tests/integrations/persisters/test_b_aiosqlite.pyindex fd2c679e..201d3938 100644--- a/tests/integrations/persisters/test_b_aiosqlite.py+++ b/tests/integrations/persisters/test_b_aiosqlite.py@@ -91,6 +91,52 @@ async def test_async_persistence_list_app_ids_is_unique_and_newest_first(async_passert await async_persistence.list_app_ids("pk") == ["a-app", "z-app"][email protected]("partition_key", [None, "partition"])[email protected]("latest_app,latest_sequence", [("a-app", 12), ("z-app", 0)])+async def test_async_sqlite_load_latest_breaks_timestamp_ties(+ tmp_path, partition_key, latest_app, latest_sequence+):+ db_path = str(tmp_path / "checkpoints.db")+ async with AsyncSQLitePersister.from_values(+ db_path=db_path, table_name="test_table"+ ) as persister:+ await persister.initialize()+ await persister.save(partition_key, "a-app", 11, "first", State({"step": 1}), "completed")+ await persister.save(+ partition_key, latest_app, latest_sequence, "second", State({"step": 2}), "completed"+ )+ await persister.save("other-partition", "other-app", 999, "other", State({}), "completed")+ # Model saves within the same second without depending on wall-clock timing.+ await persister.connection.execute(+ "UPDATE test_table SET created_at = '2026-01-01 00:00:00'"+ )+ await persister.connection.commit()++ async with AsyncSQLitePersister.from_values(+ db_path=db_path, table_name="test_table"+ ) as persister:+ loaded = await persister.load(partition_key, None)+ assert loaded["app_id"] == latest_app+ assert loaded["sequence_id"] == latest_sequence+ assert loaded["position"] == "second"+ assert loaded["state"] == State({"step": 2})+ assert loaded["status"] == "completed"+ assert (await persister.load(partition_key, "a-app", 11))["state"] == State({"step": 1})+++async def test_async_sqlite_load_latest_prioritizes_timestamp(async_persistence):+ persister = async_persistence+ await persister.initialize()+ await persister.save("partition", "a-app", 1, "first", State({"step": 1}), "completed")+ await persister.save("partition", "z-app", 0, "second", State({"step": 2}), "completed")+ await persister.connection.execute(+ "UPDATE test_table SET created_at = CASE app_id "+ "WHEN 'a-app' THEN '2026-01-02 00:00:00' ELSE '2026-01-01 00:00:00' END"+ )+ await persister.connection.commit()+ assert (await persister.load("partition", None))["state"] == State({"step": 1})++@pytest.mark.parametrize("method_name,kwargs",[