Skip to content

armgit / burr

Public Imported from GitHub
0

Build applications that make decisions (chatbots, agents, simulations, etc...). Monitor, trace, persist, and execute on your own infrastructure.

fix: list each app once in SQL persister list_app_ids, newest save first (#942)

Sangkyoon Nam16c6e2df1fd2
diff --git a/burr/core/persistence.py b/burr/core/persistence.py
index 87281d23..86f517d3 100644
--- a/burr/core/persistence.py
+++ b/burr/core/persistence.py
@@ -449,6 +449,7 @@ class SQLitePersister(BaseStatePersister, BaseCopyable):
return self._initialized
def list_app_ids(self, partition_key: Optional[str], **kwargs) -> list[str]:
+ """Lists the app_ids for a given partition_key, each once, most recently saved first."""
partition_key = (
partition_key if partition_key is not None else SQLitePersister.PARTITION_KEY_DEFAULT
)
@@ -456,9 +457,10 @@ class SQLitePersister(BaseStatePersister, BaseCopyable):
cursor = self.connection.cursor()
try:
cursor.execute(
- f"SELECT DISTINCT app_id FROM {self.table_name} "
+ f"SELECT app_id FROM {self.table_name} "
f"WHERE partition_key = ? "
- f"ORDER BY created_at DESC",
+ f"GROUP BY app_id "
+ f"ORDER BY MAX(created_at) DESC",
(partition_key,),
)
except sqlite3.OperationalError as e:
diff --git a/burr/integrations/persisters/b_aiosqlite.py b/burr/integrations/persisters/b_aiosqlite.py
index 8439b965..e6f56d47 100644
--- a/burr/integrations/persisters/b_aiosqlite.py
+++ b/burr/integrations/persisters/b_aiosqlite.py
@@ -188,6 +188,7 @@ class AsyncSQLitePersister(AsyncBaseStatePersister, BaseCopyable):
return self._initialized
async def list_app_ids(self, partition_key: Optional[str] = None, **kwargs) -> list[str]:
+ """Lists the app_ids for a given partition_key, each once, most recently saved first."""
partition_key = (
partition_key
if partition_key is not None
@@ -196,9 +197,10 @@ class AsyncSQLitePersister(AsyncBaseStatePersister, BaseCopyable):
cursor = await self.connection.cursor()
await cursor.execute(
- f"SELECT DISTINCT app_id FROM {self.table_name} "
+ f"SELECT app_id FROM {self.table_name} "
f"WHERE partition_key = ? "
- f"ORDER BY created_at DESC",
+ f"GROUP BY app_id "
+ f"ORDER BY MAX(created_at) DESC",
(partition_key,),
)
app_ids = [row[0] for row in await cursor.fetchall()]
diff --git a/burr/integrations/persisters/b_asyncpg.py b/burr/integrations/persisters/b_asyncpg.py
index c47e34e3..468c28c2 100644
--- a/burr/integrations/persisters/b_asyncpg.py
+++ b/burr/integrations/persisters/b_asyncpg.py
@@ -285,13 +285,14 @@ class AsyncPostgreSQLPersister(persistence.AsyncBaseStatePersister, BaseCopyable
await self._release_connection(conn, acquired)
async def list_app_ids(self, partition_key: str, **kwargs) -> list[str]:
- """Lists the app_ids for a given partition_key."""
+ """Lists the app_ids for a given partition_key, each once, most recently saved first."""
conn, acquired = await self._get_connection()
try:
query = (
- f"SELECT DISTINCT app_id, created_at FROM {self.table_name} "
+ f"SELECT app_id FROM {self.table_name} "
"WHERE partition_key = $1 "
- "ORDER BY created_at DESC"
+ "GROUP BY app_id "
+ "ORDER BY MAX(created_at) DESC"
)
fetched_data = await conn.fetch(query, partition_key)
app_ids = [row[0] for row in fetched_data]
diff --git a/burr/integrations/persisters/b_psycopg2.py b/burr/integrations/persisters/b_psycopg2.py
index 7c0430a7..7f420908 100644
--- a/burr/integrations/persisters/b_psycopg2.py
+++ b/burr/integrations/persisters/b_psycopg2.py
@@ -151,12 +151,13 @@ class PostgreSQLPersister(persistence.BaseStatePersister):
return self._initialized
def list_app_ids(self, partition_key: str, **kwargs) -> list[str]:
- """Lists the app_ids for a given partition_key."""
+ """Lists the app_ids for a given partition_key, each once, most recently saved first."""
cursor = self.connection.cursor()
cursor.execute(
- f"SELECT DISTINCT app_id, created_at FROM {self.table_name} "
+ f"SELECT app_id FROM {self.table_name} "
"WHERE partition_key = %s "
- "ORDER BY created_at DESC",
+ "GROUP BY app_id "
+ "ORDER BY MAX(created_at) DESC",
(partition_key,),
)
app_ids = [row[0] for row in cursor.fetchall()]
diff --git a/tests/core/test_persistence.py b/tests/core/test_persistence.py
index 2e35617a..fe6a63f8 100644
--- a/tests/core/test_persistence.py
+++ b/tests/core/test_persistence.py
@@ -77,6 +77,25 @@ def test_persistence_lists_app_ids(persistence):
assert set(app_ids) == set(["app_id1", "app_id2"])
+def test_sqlite_persister_list_app_ids_is_unique_and_newest_first():
+ """Each app appears once, ordered by its most recent save."""
+ persister = SQLLitePersister(db_path=":memory:", table_name="test_table")
+ persister.initialize()
+ saves = [
+ ("a-app", 0, "2026-01-01 00:00:01"),
+ ("z-app", 0, "2026-01-01 00:00:02"),
+ ("a-app", 1, "2026-01-01 00:00:03"),
+ ]
+ for app_id, sequence_id, created_at in saves:
+ persister.save("pk", app_id, sequence_id, "position", State({}), "completed")
+ persister.connection.execute(
+ "UPDATE test_table SET created_at = ? WHERE app_id = ? AND sequence_id = ?",
+ (created_at, app_id, sequence_id),
+ )
+ persister.connection.commit()
+ assert persister.list_app_ids("pk") == ["a-app", "z-app"]
+
+
def test_persistence_is_initialized_false(initializing_persistence):
assert not initializing_persistence.is_initialized()
diff --git a/tests/integrations/persisters/test_b_aiosqlite.py b/tests/integrations/persisters/test_b_aiosqlite.py
index adb97532..fd2c679e 100644
--- a/tests/integrations/persisters/test_b_aiosqlite.py
+++ b/tests/integrations/persisters/test_b_aiosqlite.py
@@ -73,6 +73,24 @@ async def test_async_persistence_lists_app_ids(async_persistence):
assert set(app_ids) == set(["app_id1", "app_id2"])
+async def test_async_persistence_list_app_ids_is_unique_and_newest_first(async_persistence):
+ """Each app appears once, ordered by its most recent save."""
+ await async_persistence.initialize()
+ saves = [
+ ("a-app", 0, "2026-01-01 00:00:01"),
+ ("z-app", 0, "2026-01-01 00:00:02"),
+ ("a-app", 1, "2026-01-01 00:00:03"),
+ ]
+ for app_id, sequence_id, created_at in saves:
+ await async_persistence.save("pk", app_id, sequence_id, "position", State({}), "completed")
+ await async_persistence.connection.execute(
+ "UPDATE test_table SET created_at = ? WHERE app_id = ? AND sequence_id = ?",
+ (created_at, app_id, sequence_id),
+ )
+ await async_persistence.connection.commit()
+ assert await async_persistence.list_app_ids("pk") == ["a-app", "z-app"]
+
+
@pytest.mark.parametrize(
"method_name,kwargs",
[
diff --git a/tests/integrations/persisters/test_postgresql.py b/tests/integrations/persisters/test_postgresql.py
index 0100cd93..f7ebe13d 100644
--- a/tests/integrations/persisters/test_postgresql.py
+++ b/tests/integrations/persisters/test_postgresql.py
@@ -15,6 +15,7 @@
# specific language governing permissions and limitations
# under the License.
+import datetime
import os
import pickle
@@ -64,6 +65,26 @@ def test_list_app_ids(postgresql_persister):
assert "app_id2" in app_ids
+def test_list_app_ids_is_unique_and_newest_first(postgresql_persister):
+ saves = [
+ ("a-app", 0, "2026-01-01 00:00:01"),
+ ("z-app", 0, "2026-01-01 00:00:02"),
+ ("a-app", 1, "2026-01-01 00:00:03"),
+ ]
+ cursor = postgresql_persister.connection.cursor()
+ for app_id, sequence_id, created_at in saves:
+ postgresql_persister.save(
+ "list-order", app_id, sequence_id, "pos", state.State({}), "completed"
+ )
+ cursor.execute(
+ "UPDATE testtable SET created_at = %s "
+ "WHERE partition_key = %s AND app_id = %s AND sequence_id = %s",
+ (created_at, "list-order", app_id, sequence_id),
+ )
+ postgresql_persister.connection.commit()
+ assert postgresql_persister.list_app_ids("list-order") == ["a-app", "z-app"]
+
+
def test_load_nonexistent_key(postgresql_persister):
state_data = postgresql_persister.load("pk", "nonexistent_key")
assert state_data is None
@@ -174,6 +195,27 @@ async def test_async_list_app_ids(asyncpostgresql_persister):
assert "app_id2" in app_ids
+async def test_async_list_app_ids_is_unique_and_newest_first(asyncpostgresql_persister):
+ saves = [
+ ("a-app", 0, "2026-01-01 00:00:01"),
+ ("z-app", 0, "2026-01-01 00:00:02"),
+ ("a-app", 1, "2026-01-01 00:00:03"),
+ ]
+ for app_id, sequence_id, created_at in saves:
+ await asyncpostgresql_persister.save(
+ "list-order", app_id, sequence_id, "pos", state.State({}), "completed"
+ )
+ await asyncpostgresql_persister.connection.execute(
+ "UPDATE testtable_async SET created_at = $1 "
+ "WHERE partition_key = $2 AND app_id = $3 AND sequence_id = $4",
+ datetime.datetime.fromisoformat(created_at),
+ "list-order",
+ app_id,
+ sequence_id,
+ )
+ assert await asyncpostgresql_persister.list_app_ids("list-order") == ["a-app", "z-app"]
+
+
async def test_async_load_nonexistent_key(asyncpostgresql_persister):
state_data = await asyncpostgresql_persister.load("pk", "nonexistent_key")
assert state_data is None