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
Fixes up tests for state and application
Stefan Krawczyk64aaa01b2a85
diff --git a/burr/core/application.py b/burr/core/application.pyindex 5f56d4ff..71173e41 100644--- a/burr/core/application.py+++ b/burr/core/application.py@@ -171,7 +171,7 @@ def _format_error_message(action: Action, input_state: State, inputs: dict) -> sdef _run_single_step_action(- action: SingleStepAction, state: State, inputs: Optional[Dict[str, Any]]+ action: SingleStepAction, state: State, inputs: Optional[Dict[str, Any]]) -> Tuple[Dict[str, Any], State]:"""Runs a single step action. This API is internal-facing and a bit in flux, butit corresponds to the SingleStepAction class.@@ -189,7 +189,7 @@ def _run_single_step_action(def _run_single_step_streaming_action(- action: SingleStepStreamingAction, state: State, inputs: Optional[Dict[str, Any]]+ action: SingleStepStreamingAction, state: State, inputs: Optional[Dict[str, Any]]) -> Generator[dict, None, Tuple[dict, State]]:action.validate_inputs(inputs)generator = action.stream_run_and_update(state, **inputs)@@ -197,7 +197,7 @@ def _run_single_step_streaming_action(def _run_multi_step_streaming_action(- action: StreamingAction, state: State, inputs: Optional[Dict[str, Any]]+ action: StreamingAction, state: State, inputs: Optional[Dict[str, Any]]) -> Generator[dict, None, Tuple[dict, State]]:action.validate_inputs(inputs)generator = action.stream_run(state, **inputs)@@ -207,7 +207,7 @@ def _run_multi_step_streaming_action(async def _arun_single_step_action(- action: SingleStepAction, state: State, inputs: Optional[Dict[str, Any]]+ action: SingleStepAction, state: State, inputs: Optional[Dict[str, Any]]) -> Tuple[dict, State]:"""Runs a single step action in async. See the synchronous version for more details."""state_to_use = state@@ -232,15 +232,15 @@ class ApplicationGraph:class Application:def __init__(- self,- actions: List[Action],- transitions: List[Transition],- state: State,- initial_step: str,- partition_key: str,- id: str,- sequence_id: int,- adapter_set: Optional[LifecycleAdapterSet] = None,+ self,+ actions: List[Action],+ transitions: List[Transition],+ state: State,+ initial_step: str,+ partition_key: str,+ id: str,+ sequence_id: int,+ adapter_set: Optional[LifecycleAdapterSet] = None,):self._partition_key = partition_keyself._id = id@@ -285,7 +285,7 @@ class Application:return outdef _step(- self, inputs: Optional[Dict[str, Any]], _run_hooks: bool = True+ self, inputs: Optional[Dict[str, Any]], _run_hooks: bool = True) -> Optional[Tuple[Action, dict, State]]:"""Internal-facing version of step. This is the same as step, but with an additionalparameter to hide hook execution so async can leverage it."""@@ -302,6 +302,8 @@ class Application:state=self._state,inputs=inputs,sequence_id=self.sequence_id,+ app_id=self._id,+ partition_key=self._partition_key,)exc = Noneresult = None@@ -393,6 +395,8 @@ class Application:state=self._state,inputs=inputs,sequence_id=self.sequence_id,+ app_id=self._id,+ partition_key=self._partition_key,)exc = Noneresult = None@@ -428,15 +432,17 @@ class Application:result=result,sequence_id=self.sequence_id,exception=exc,+ app_id=self._id,+ partition_key=self._partition_key,)return next_action, result, new_statedef _clean_iterate_params(- self,- halt_before: list[str] = None,- halt_after: list[str] = None,- inputs: Optional[Dict[str, Any]] = None,+ self,+ halt_before: list[str] = None,+ halt_after: list[str] = None,+ inputs: Optional[Dict[str, Any]] = None,) -> Tuple[list[str], list[str], Dict[str, Any]]:"""Utility function to clean out iterate params so we have less duplication between iterate/aiterateand the logic is cleaner later.@@ -461,7 +467,7 @@ class Application:return self.get_next_action() is not Nonedef _should_halt_iterate(- self, halt_before: list[str], halt_after: list[str], prior_action: Action+ self, halt_before: list[str], halt_after: list[str], prior_action: Action) -> bool:"""Internal utility function to determine whether or not to halt during iteration"""if self.has_next_action() and self.get_next_action().name in halt_before:@@ -473,12 +479,12 @@ class Application:return Falsedef _return_value_iterate(- self,- halt_before: list[str],- halt_after: list[str],- prior_action: Optional[Action],- result: Optional[dict],- ) -> Tuple[Action, Optional[dict], State]:+ self,+ halt_before: list[str],+ halt_after: list[str],+ prior_action: Optional[Action],+ result: Optional[dict],+ ) -> Tuple[Optional[Action], Optional[dict], State]:"""Utility function to decide what to return for iterate/arun. Note that run() will delegate to the return value ofiterate, whereas arun cannot delegate to the return value of aiterate (as async generators cannot return a value).We put the code centrally to clean up the logic.@@ -507,11 +513,11 @@ class Application:@telemetry.capture_function_usagedef iterate(- self,- *,- halt_before: list[str] = None,- halt_after: list[str] = None,- inputs: Optional[Dict[str, Any]] = None,+ self,+ *,+ halt_before: list[str] = None,+ halt_after: list[str] = None,+ inputs: Optional[Dict[str, Any]] = None,) -> Generator[Tuple[Action, dict, State], None, Tuple[Action, Optional[dict], State]]:"""Returns a generator that calls step() in a row, enabling you to see the stateof the system as it updates. Note this returns a generator, and also the final result@@ -544,11 +550,11 @@ class Application:@telemetry.capture_function_usageasync def aiterate(- self,- *,- halt_before: list[str] = None,- halt_after: list[str] = None,- inputs: Optional[Dict[str, Any]] = None,+ self,+ *,+ halt_before: list[str] = None,+ halt_after: list[str] = None,+ inputs: Optional[Dict[str, Any]] = None,) -> AsyncGenerator[Tuple[Action, dict, State], None]:"""Returns a generator that calls step() in a row, enabling you to see the stateof the system as it updates. This is the asynchronous version so it has no capability of t@@ -574,11 +580,11 @@ class Application:@telemetry.capture_function_usagedef run(- self,- *,- halt_before: list[str] = None,- halt_after: list[str] = None,- inputs: Optional[Dict[str, Any]] = None,+ self,+ *,+ halt_before: list[str] = None,+ halt_after: list[str] = None,+ inputs: Optional[Dict[str, Any]] = None,) -> Tuple[Action, Optional[dict], State]:"""Runs your application through until completion. Doesnot give access to the state along the way -- if you want that, use iterate().@@ -598,11 +604,11 @@ class Application:@telemetry.capture_function_usageasync def arun(- self,- *,- halt_before: list[str] = None,- halt_after: list[str] = None,- inputs: Optional[Dict[str, Any]] = None,+ self,+ *,+ halt_before: list[str] = None,+ halt_after: list[str] = None,+ inputs: Optional[Dict[str, Any]] = None,) -> Tuple[Action, Optional[dict], State]:"""Runs your application through until completion, using async. Doesnot give access to the state along the way -- if you want that, use iterate().@@ -618,7 +624,7 @@ class Application:halt_before, halt_after, inputs)async for prior_action, result, state in self.aiterate(- halt_before=halt_before, halt_after=halt_after, inputs=inputs+ halt_before=halt_before, halt_after=halt_after, inputs=inputs):passreturn self._return_value_iterate(halt_before, halt_after, prior_action, result)@@ -634,10 +640,10 @@ class Application:@telemetry.capture_function_usagedef stream_result(- self,- halt_after: list[str],- halt_before: list[str] = None,- inputs: Optional[Dict[str, Any]] = None,+ self,+ halt_after: list[str],+ halt_before: list[str] = None,+ inputs: Optional[Dict[str, Any]] = None,) -> Tuple[Action, StreamingResultContainer]:"""Streams a result out.@@ -788,10 +794,10 @@ class Application:return result, new_statedef callback(- result: Optional[dict],- state: State,- exc: Optional[Exception] = None,- seq_id=self.sequence_id,+ result: Optional[dict],+ state: State,+ exc: Optional[Exception] = None,+ seq_id=self.sequence_id,):self._adapter_set.call_all_lifecycle_hooks_sync("post_run_step",@@ -849,10 +855,10 @@ class Application:@telemetry.capture_function_usageasync def astream_result(- self,- halt_after: list[str],- halt_before: list[str] = None,- inputs: Optional[Dict[str, Any]] = None,+ self,+ halt_after: list[str],+ halt_before: list[str] = None,+ inputs: Optional[Dict[str, Any]] = None,) -> Tuple[Action, ...]:"""Placeholder for the async version of stream_result. This is not yet implemented."""raise NotImplementedError(@@ -862,13 +868,13 @@ class Application:@telemetry.capture_function_usagedef visualize(- self,- output_file_path: Optional[str],- include_conditions: bool = False,- include_state: bool = False,- view: bool = False,- engine: Literal["graphviz"] = "graphviz",- **engine_kwargs: Any,+ self,+ output_file_path: Optional[str],+ include_conditions: bool = False,+ include_state: bool = False,+ view: bool = False,+ engine: Literal["graphviz"] = "graphviz",+ **engine_kwargs: Any,):"""Visualizes the application graph using graphviz. This will render the graph.@@ -1022,7 +1028,7 @@ def _assert_set(value: Optional[Any], field: str, method: str):def _validate_transitions(- transitions: Optional[List[Tuple[str, str, Condition]]], actions: Set[str]+ transitions: Optional[List[Tuple[str, str, Condition]]], actions: Set[str]):_assert_set(transitions, "_transitions", "with_transitions")exhausted = {} # items for which we have seen a default transition@@ -1085,7 +1091,7 @@ class ApplicationBuilder:self.default_state: dict = Nonedef with_identifiers(- self, app_id: str = None, partition_key: str = None, sequence_id: int = None+ self, app_id: str = None, partition_key: str = None, sequence_id: int = None) -> "ApplicationBuilder":if app_id is not None:self.app_id = app_id@@ -1135,10 +1141,10 @@ class ApplicationBuilder:return selfdef with_transitions(- self,- *transitions: Union[- Tuple[Union[str, list[str]], str], Tuple[Union[str, list[str]], str, Condition]- ],+ self,+ *transitions: Union[+ Tuple[Union[str, list[str]], str], Tuple[Union[str, list[str]], str, Condition]+ ],) -> "ApplicationBuilder":"""Adds transitions to the application. Transitions are specified as tuples of either:1. (from, to, condition)@@ -1180,7 +1186,7 @@ class ApplicationBuilder:return selfdef with_tracker(- self, project: str, tracker: Literal["local"] = "local", params: Dict[str, Any] = None+ self, project: str, tracker: Literal["local"] = "local", params: Dict[str, Any] = None):"""Adds a "tracker" to the application. The tracker specifiesa project name (used for disambiguating groups of tracers), and plugs into the@@ -1204,13 +1210,12 @@ class ApplicationBuilder:raise ValueError(f"Tracker {tracker} not supported")return self-def initialize_from(- self,- persister: BasicStatePersistence,- resume_at_next_action: bool,- default_state: dict,- default_entry_point: str,+ self,+ persister: BasicStatePersistence,+ resume_at_next_action: bool,+ default_state: dict,+ default_entry_point: str,) -> "ApplicationBuilder":self.persister = persisterself.resume_at_next_action = resume_at_next_action@@ -1219,7 +1224,7 @@ class ApplicationBuilder:return selfdef with_state_persister(- self, persister: BasicStatePersistence, on_every: str = "step"+ self, persister: BasicStatePersistence, on_every: str = "step") -> "ApplicationBuilder":"""Adds a state persister to the application. This is a way to persist state out to a database, file, etc...at the specified interval.@@ -1238,15 +1243,15 @@ class ApplicationBuilder:# TODO: pull this out more formallyclass persisterWrapper(PostRunStepHook):def post_run_step(- self,- app_id: str,- partition_key: str,- sequence_id: int,- state: "State",- action: "Action",- result: Optional[Dict[str, Any]],- exception: Exception,- **future_kwargs: Any,+ self,+ app_id: str,+ partition_key: str,+ sequence_id: int,+ state: "State",+ action: "Action",+ result: Optional[Dict[str, Any]],+ exception: Exception,+ **future_kwargs: Any,):if exception is None:persister.save(@@ -1260,6 +1265,40 @@ class ApplicationBuilder:self.lifecycle_adapters.append(persisterWrapper())return self+ def _load_from_persister(self):+ """Loads from the set persister and into this current object.++ Mutates:+ - self.state+ - self.sequence_id+ - maybe self.start++ """+ # load state from persister+ load_result = self.persister.load(self.partition_key, self.app_id, self.sequence_id)+ if load_result is None:+ # there was nothing to load -- use default state+ self.state = self.state.update(**self.default_state)+ self.sequence_id = 0+ else:+ # there was something+ last_position = load_result["position"]+ self.state = load_result["state"]+ self.sequence_id = load_result["sequence_id"]+ status = load_result["status"]+ if self.resume_at_next_action:+ # if we're supposed to resume where we saved from+ if status == "completed":+ # completed means we set prior step to current to go to next action+ self.state = self.state.update(**{PRIOR_STEP: last_position})+ else:+ # else we failed we just start at that node+ self.start = last_position+ self.state = self.state.wipe(delete=[PRIOR_STEP])+ else:+ # self.start is already set to the default. We don't need to do anything.+ pass+@telemetry.capture_function_usagedef build(self) -> Application:"""Builds the application.@@ -1274,31 +1313,11 @@ class ApplicationBuilder:_validate_transitions(self.transitions, all_actions)_validate_app_id(self.app_id)- loaded_sequence_id = Noneif self.persister:- # load state from persister- load_result = self.persister.load(self.partition_key, self.app_id, self.sequence_id)- if load_result is None:- self.state = self.state.update(**self.default_state)- else:- last_position = load_result["position"]- self.state = load_result["state"]- loaded_sequence_id = load_result["sequence_id"]- status = load_result["status"]- if self.resume_at_next_action:- if status == "completed":- self.state = self.state.update(**{PRIOR_STEP: last_position})- else:- # if failed we just start at that node- self.start = last_position- self.state = self.state.wipe(delete=[PRIOR_STEP])- else:- # self.start is already set to the default. We don't need to do anything.- pass+ # sets state, sequence_id, and maybe start+ self._load_from_persister()_validate_start(self.start, all_actions)- if self.sequence_id is None:- self.sequence_id = loaded_sequence_id if loaded_sequence_id else 0return Application(actions=self.actions,transitions=[diff --git a/burr/core/state.py b/burr/core/state.pyindex 6aab1e1a..777b0e4b 100644--- a/burr/core/state.py+++ b/burr/core/state.py@@ -121,7 +121,6 @@ class DeleteField(StateDelta):def apply_mutate(self, inputs: dict):for key in self.keys:inputs.pop(key, None)- print(inputs)class State(Mapping):@@ -279,11 +278,11 @@ class SQLLitePersistence(BasicStatePersistence):"""self.db_path = db_pathself.table_name = table_name+ self.connection = sqlite3.connect(db_path)@staticmethod- def create_table_if_not_exists(db_path: str, table_name: str):+ def create_table_if_not_exists(db_path: str, table_name: str, conn: sqlite3.Connection):"""Helper function to create the table where things are stored if it doesn't exist."""- conn = sqlite3.connect(db_path)cursor = conn.cursor()cursor.execute(@@ -305,16 +304,14 @@ class SQLLitePersistence(BasicStatePersistence):""")conn.commit()- conn.close()def initialize(self):"""Creates the table if it doesn't exist"""# Usage- self.create_table_if_not_exists(self.db_path, self.table_name)+ self.create_table_if_not_exists(self.db_path, self.table_name, self.connection)def list_app_ids(self, partition_key: str) -> list[str]:- conn = sqlite3.connect(self.db_path)- cursor = conn.cursor()+ cursor = self.connection.cursor()cursor.execute(f"SELECT DISTINCT app_id FROM {self.table_name} "f"WHERE partition_key = ? "@@ -322,7 +319,6 @@ class SQLLitePersistence(BasicStatePersistence):(partition_key,),)app_ids = [row[0] for row in cursor.fetchall()]- conn.close()return app_idsdef load(@@ -339,8 +335,7 @@ class SQLLitePersistence(BasicStatePersistence)::return:"""logger.debug("Loading %s, %s, %s", partition_key, app_id, sequence_id)- conn = sqlite3.connect(self.db_path)- cursor = conn.cursor()+ cursor = self.connection.cursor()if app_id is None:# get latest for all app_idscursor.execute(@@ -363,7 +358,6 @@ class SQLLitePersistence(BasicStatePersistence):(partition_key, app_id, sequence_id),)row = cursor.fetchone()- conn.close()if row is None:return None_state = State(json.loads(row[1])["_state"])@@ -412,21 +406,23 @@ class SQLLitePersistence(BasicStatePersistence):state,status,)- conn = sqlite3.connect(self.db_path)- cursor = conn.cursor()+ cursor = self.connection.cursor()json_state = json.dumps(state.__dict__)cursor.execute(f"INSERT INTO {self.table_name} (partition_key, app_id, sequence_id, position, state, status) "f"VALUES (?, ?, ?, ?, ?, ?)",(partition_key, app_id, sequence_id, position, json_state, status),)- conn.commit()- conn.close()+ self.connection.commit()++ def __del__(self):+ # closes connection at end when things are being shutdown.+ self.connection.close()if __name__ == "__main__":s = SQLLitePersistence(db_path=".sqllite.db", table_name="test1")s.initialize()- s.save("pk", "app_id", 1, "pos", State({"a": 1, "b": 2}))+ s.save("pk", "app_id", 1, "pos", State({"a": 1, "b": 2}), "completed")print(s.list_app_ids("pk"))print(s.load("pk", "app_id"))diff --git a/tests/core/test_application.py b/tests/core/test_application.pyindex cd11c451..abd7b4ea 100644--- a/tests/core/test_application.py+++ b/tests/core/test_application.py@@ -118,10 +118,10 @@ class ActionTracker(PreRunStepHook, PostRunStepHook):self.post_called = []def pre_run_step(self, action: Action, **future_kwargs):- self.pre_called.append(action.name)+ self.pre_called.append((action.name, future_kwargs))def post_run_step(self, action: Action, **future_kwargs):- self.post_called.append(action.name)+ self.post_called.append((action.name, future_kwargs))class ActionTrackerAsync(PreRunStepHookAsync, PostRunStepHookAsync):@@ -131,11 +131,11 @@ class ActionTrackerAsync(PreRunStepHookAsync, PostRunStepHookAsync):async def pre_run_step(self, *, action: Action, **future_kwargs):await asyncio.sleep(0.0001)- self.pre_called.append(action.name)+ self.pre_called.append((action.name, future_kwargs))async def post_run_step(self, *, action: Action, **future_kwargs):await asyncio.sleep(0.0001)- self.post_called.append(action.name)+ self.post_called.append((action.name, future_kwargs))async def _counter_update_async(state: State, additional_increment: int = 0) -> dict:@@ -463,6 +463,9 @@ def test_app_step():transitions=[Transition(counter_action, counter_action, default)],state=State({}),initial_step="counter",+ partition_key="test",+ id="test-123",+ sequence_id=0,)assert app.sequence_id == 0action, result, state = app.step()@@ -480,6 +483,9 @@ def test_app_step_with_inputs():transitions=[Transition(counter_action, counter_action, default)],state=State({"count": 0, "tracker": []}),initial_step="counter",+ partition_key="test",+ id="test-123",+ sequence_id=0,)action, result, state = app.step(inputs={"additional_increment": 1})assert action.name == "counter"@@ -495,6 +501,9 @@ def test_app_step_with_inputs_missing():transitions=[Transition(counter_action, counter_action, default)],state=State({"count": 0, "tracker": []}),initial_step="counter",+ partition_key="test",+ id="test-123",+ sequence_id=0,)with pytest.raises(ValueError, match="missing required inputs"):app.step(inputs={})@@ -508,6 +517,9 @@ def test_app_step_broken(caplog):transitions=[Transition(broken_action, broken_action, default)],state=State({}),initial_step="broken_action_unique_name",+ partition_key="test",+ id="test-123",+ sequence_id=0,)with caplog.at_level(logging.ERROR): # it should say the name, that's the only contract for nowwith pytest.raises(BrokenStepException):@@ -519,7 +531,13 @@ def test_app_step_done():"""Tests that when we cannot run a step, we return None"""counter_action = base_counter_action.with_name("counter")app = Application(- actions=[counter_action], transitions=[], state=State({}), initial_step="counter"+ actions=[counter_action],+ transitions=[],+ state=State({}),+ initial_step="counter",+ partition_key="test",+ id="test-123",+ sequence_id=0,)app.step()assert app.step() is None@@ -533,6 +551,9 @@ async def test_app_astep():transitions=[Transition(counter_action, counter_action, default)],state=State({}),initial_step="counter_async",+ partition_key="test",+ id="test-123",+ sequence_id=0,)assert app.sequence_id == 0action, result, state = await app.astep()@@ -550,6 +571,9 @@ async def test_app_astep_with_inputs():transitions=[Transition(counter_action, counter_action, default)],state=State({"count": 0, "tracker": []}),initial_step="counter_async",+ partition_key="test",+ id="test-123",+ sequence_id=0,)action, result, state = await app.astep(inputs={"additional_increment": 1})assert action.name == "counter_async"@@ -565,6 +589,9 @@ async def test_app_astep_with_inputs_missing():transitions=[Transition(counter_action, counter_action, default)],state=State({"count": 0, "tracker": []}),initial_step="counter_async",+ partition_key="test",+ id="test-123",+ sequence_id=0,)with pytest.raises(ValueError, match="missing required inputs"):await app.astep(inputs={})@@ -578,6 +605,9 @@ async def test_app_astep_broken(caplog):transitions=[Transition(broken_action, broken_action, default)],state=State({}),initial_step="broken_action_unique_name",+ partition_key="test",+ id="test-123",+ sequence_id=0,)with caplog.at_level(logging.ERROR): # it should say the name, that's the only contract for nowwith pytest.raises(BrokenStepException):@@ -589,7 +619,13 @@ async def test_app_astep_done():"""Tests that when we cannot run a step, we return None"""counter_action = base_counter_action_async.with_name("counter_async")app = Application(- actions=[counter_action], transitions=[], state=State({}), initial_step="counter_async"+ actions=[counter_action],+ transitions=[],+ state=State({}),+ initial_step="counter_async",+ partition_key="test",+ id="test-123",+ sequence_id=0,)await app.astep()assert await app.astep() is None@@ -603,6 +639,9 @@ def test_app_many_steps():transitions=[Transition(counter_action, counter_action, default)],state=State({}),initial_step="counter",+ partition_key="test",+ id="test-123",+ sequence_id=0,)action, result = None, Nonefor i in range(100):@@ -618,6 +657,9 @@ async def test_app_many_a_steps():transitions=[Transition(counter_action, counter_action, default)],state=State({}),initial_step="counter_async",+ partition_key="test",+ id="test-123",+ sequence_id=0,)action, result = None, Nonefor i in range(100):@@ -637,6 +679,9 @@ def test_iterate():],state=State({}),initial_step="counter",+ partition_key="test",+ id="test-123",+ sequence_id=0,)res = []gen = app.iterate(halt_after=["result"])@@ -672,6 +717,9 @@ def test_iterate_with_inputs():],state=State({}),initial_step="counter",+ partition_key="test",+ id="test-123",+ sequence_id=0,)gen = app.iterate(halt_after=["result"], inputs={"additional_increment": 10}@@ -696,6 +744,9 @@ async def test_aiterate():],state=State({}),initial_step="counter",+ partition_key="test",+ id="test-123",+ sequence_id=0,)assert app.sequence_id == 0gen = app.aiterate(halt_after=["result"])@@ -724,6 +775,9 @@ async def test_aiterate_halt_before():],state=State({}),initial_step="counter",+ partition_key="test",+ id="test-123",+ sequence_id=0,)gen = app.aiterate(halt_before=["result"])counter = 0@@ -749,6 +803,9 @@ async def test_app_aiterate_with_inputs():],state=State({}),initial_step="counter",+ partition_key="test",+ id="test-123",+ sequence_id=0,)gen = app.aiterate(halt_after=["result"], inputs={"additional_increment": 10})async for action, result, state in gen:@@ -769,6 +826,9 @@ def test_run():],state=State({}),initial_step="counter",+ partition_key="test",+ id="test-123",+ sequence_id=0,)action, result, state = app.run(halt_after=["result"])assert state["count"] == 10@@ -786,6 +846,9 @@ def test_run_halt_before():],state=State({}),initial_step="counter",+ partition_key="test",+ id="test-123",+ sequence_id=0,)action, result, state = app.run(halt_before=["result"])assert state["count"] == 10@@ -804,6 +867,9 @@ def test_run_with_inputs():],state=State({}),initial_step="counter",+ partition_key="test",+ id="test-123",+ sequence_id=0,)action, result, state = app.run(halt_after=["result"], inputs={"additional_increment": 10})assert action.name == "result"@@ -821,6 +887,9 @@ async def test_arun():],state=State({}),initial_step="counter",+ partition_key="test",+ id="test-123",+ sequence_id=0,)action, result, state = await app.arun(halt_after=["result"])assert state["count"] == result["count"] == 10@@ -838,6 +907,9 @@ async def test_arun_halt_before():],state=State({}),initial_step="counter",+ partition_key="test",+ id="test-123",+ sequence_id=0,)action, result, state = await app.arun(halt_before=["result"])assert state["count"] == 10@@ -856,6 +928,9 @@ async def test_arun_with_inputs():],state=State({}),initial_step="counter",+ partition_key="test",+ id="test-123",+ sequence_id=0,)action, result, state = await app.arun(halt_after=["result"], inputs={"additional_increment": 10}@@ -877,6 +952,9 @@ async def test_app_a_run_async_and_sync():],state=State({}),initial_step="counter_sync",+ partition_key="test",+ id="test-123",+ sequence_id=0,)action, result, state = await app.arun(halt_after=["result"])assert state["count"] > 20@@ -895,6 +973,9 @@ def test_stream_result_halt_after():state=State({"count": 0}),initial_step="counter",adapter_set=LifecycleAdapterSet(action_tracker),+ partition_key="test",+ id="test-123",+ sequence_id=0,)action, streaming_container = app.stream_result(halt_after=["counter_2"])results = list(streaming_container)@@ -904,8 +985,8 @@ def test_stream_result_halt_after():assert state["tracker"] == [1, 2]assert len(action_tracker.pre_called) == 2assert len(action_tracker.post_called) == 2- assert set(action_tracker.pre_called) == {"counter", "counter_2"}- assert set(action_tracker.post_called) == {"counter", "counter_2"}+ assert set(dict(action_tracker.pre_called).keys()) == {"counter", "counter_2"}+ assert set(dict(action_tracker.post_called).keys()) == {"counter", "counter_2"}def test_stream_result_halt_after_single_step():@@ -920,6 +1001,9 @@ def test_stream_result_halt_after_single_step():state=State({"count": 0}),initial_step="counter",adapter_set=LifecycleAdapterSet(action_tracker),+ partition_key="test",+ id="test-123",+ sequence_id=0,)action, streaming_container = app.stream_result(halt_after=["counter_2"])results = list(streaming_container)@@ -929,8 +1013,8 @@ def test_stream_result_halt_after_single_step():assert state["tracker"] == [1, 2]assert len(action_tracker.pre_called) == 2assert len(action_tracker.post_called) == 2- assert set(action_tracker.pre_called) == {"counter", "counter_2"}- assert set(action_tracker.post_called) == {"counter", "counter_2"}+ assert set(dict(action_tracker.pre_called).keys()) == {"counter", "counter_2"}+ assert set(dict(action_tracker.post_called).keys()) == {"counter", "counter_2"}def test_stream_result_halt_after_run_through_final_streaming():@@ -949,6 +1033,9 @@ def test_stream_result_halt_after_run_through_final_streaming():state=State({"count": 0}),initial_step="counter_non_streaming",adapter_set=LifecycleAdapterSet(action_tracker),+ partition_key="test",+ id="test-123",+ sequence_id=0,)action, streaming_container = app.stream_result(halt_after=["counter_streaming"])results = list(streaming_container)@@ -957,8 +1044,14 @@ def test_stream_result_halt_after_run_through_final_streaming():assert result["count"] == state["count"] == 11assert len(action_tracker.pre_called) == 11assert len(action_tracker.post_called) == 11- assert set(action_tracker.pre_called) == {"counter_streaming", "counter_non_streaming"}- assert set(action_tracker.post_called) == {"counter_streaming", "counter_non_streaming"}+ assert set(dict(action_tracker.pre_called).keys()) == {+ "counter_streaming",+ "counter_non_streaming",+ }+ assert set(dict(action_tracker.post_called).keys()) == {+ "counter_streaming",+ "counter_non_streaming",+ }def test_stream_result_halt_after_run_through_final_non_streaming():@@ -975,6 +1068,9 @@ def test_stream_result_halt_after_run_through_final_non_streaming():state=State({"count": 0}),initial_step="counter_non_streaming",adapter_set=LifecycleAdapterSet(action_tracker),+ partition_key="test",+ id="test-123",+ sequence_id=0,)action, streaming_container = app.stream_result(halt_after=["counter_final_non_streaming"])results = list(streaming_container)@@ -983,11 +1079,11 @@ def test_stream_result_halt_after_run_through_final_non_streaming():assert result["count"] == state["count"] == 11assert len(action_tracker.pre_called) == 11assert len(action_tracker.post_called) == 11- assert set(action_tracker.pre_called) == {+ assert set(dict(action_tracker.pre_called).keys()) == {"counter_non_streaming","counter_final_non_streaming",}- assert set(action_tracker.post_called) == {+ assert set(dict(action_tracker.post_called).keys()) == {"counter_non_streaming","counter_final_non_streaming",}@@ -1005,6 +1101,9 @@ def test_stream_result_halt_before():],state=State({"count": 0}),initial_step="counter_non_streaming",+ partition_key="test",+ id="test-123",+ sequence_id=0,)action, streaming_container = app.stream_result(halt_after=[], halt_before=["counter_final"])results = list(streaming_container)@@ -1022,6 +1121,9 @@ def test_app_set_state():transitions=[Transition(counter_action, counter_action, default)],state=State(),initial_step="counter",+ partition_key="test",+ id="test-123",+ sequence_id=0,)assert "counter" not in app.state # initial valueapp.step()@@ -1044,6 +1146,9 @@ def test_app_get_next_step():],state=State(),initial_step="counter_1",+ partition_key="test",+ id="test-123",+ sequence_id=0,)# uninitialized -- counter_1assert app.get_next_action().name == "counter_1"@@ -1149,10 +1254,31 @@ def test_application_run_step_hooks_sync():state=State({}),initial_step="counter",adapter_set=internal.LifecycleAdapterSet(tracker),+ partition_key="test",+ id="test-123",+ sequence_id=0,)app.run(halt_after=["result"])- assert set(tracker.pre_called) == {"counter", "result"}- assert set(tracker.post_called) == {"counter", "result"}+ assert set(dict(tracker.pre_called).keys()) == {"counter", "result"}+ assert set(dict(tracker.post_called).keys()) == {"counter", "result"}+ # assert sequence id is incremented+ assert tracker.pre_called[0][1]["sequence_id"] == 1+ assert tracker.post_called[0][1]["sequence_id"] == 1+ assert set(tracker.pre_called[0][1].keys()) == {+ "sequence_id",+ "state",+ "inputs",+ "app_id",+ "partition_key",+ }+ assert set(tracker.post_called[0][1].keys()) == {+ "sequence_id",+ "result",+ "state",+ "exception",+ "app_id",+ "partition_key",+ }assert len(tracker.pre_called) == 11assert len(tracker.post_called) == 11# quick inclusion to ensure that the action is not called when we're done running@@ -1172,10 +1298,31 @@ async def test_application_run_step_hooks_async():state=State({}),initial_step="counter",adapter_set=internal.LifecycleAdapterSet(tracker),+ partition_key="test",+ id="test-123",+ sequence_id=0,)await app.arun(halt_after=["result"])- assert set(tracker.pre_called) == {"counter", "result"}- assert set(tracker.post_called) == {"counter", "result"}+ assert set(dict(tracker.pre_called).keys()) == {"counter", "result"}+ assert set(dict(tracker.post_called).keys()) == {"counter", "result"}+ # assert sequence id is incremented+ assert tracker.pre_called[0][1]["sequence_id"] == 1+ assert tracker.post_called[0][1]["sequence_id"] == 1+ assert set(tracker.pre_called[0][1].keys()) == {+ "sequence_id",+ "state",+ "inputs",+ "app_id",+ "partition_key",+ }+ assert set(tracker.post_called[0][1].keys()) == {+ "sequence_id",+ "result",+ "state",+ "exception",+ "app_id",+ "partition_key",+ }assert len(tracker.pre_called) == 11assert len(tracker.post_called) == 11@@ -1192,10 +1339,46 @@ async def test_application_run_step_runs_hooks():state=State({}),initial_step="counter",adapter_set=internal.LifecycleAdapterSet(*hooks),+ partition_key="test",+ id="test-123",+ sequence_id=0,)await app.astep()assert len(hooks[0].pre_called) == 1assert len(hooks[0].post_called) == 1+ # assert sequence id is incremented+ assert hooks[0].pre_called[0][1]["sequence_id"] == 1+ assert hooks[0].post_called[0][1]["sequence_id"] == 1+ assert set(hooks[0].pre_called[0][1].keys()) == {+ "sequence_id",+ "state",+ "inputs",+ "app_id",+ "partition_key",+ }+ assert set(hooks[1].pre_called[0][1].keys()) == {+ "sequence_id",+ "state",+ "inputs",+ "app_id",+ "partition_key",+ }+ assert set(hooks[0].post_called[0][1].keys()) == {+ "sequence_id",+ "result",+ "state",+ "exception",+ "app_id",+ "partition_key",+ }+ assert set(hooks[1].post_called[0][1].keys()) == {+ "sequence_id",+ "result",+ "state",+ "exception",+ "app_id",+ "partition_key",+ }assert len(hooks[1].pre_called) == 1assert len(hooks[1].post_called) == 1@@ -1222,6 +1405,9 @@ def test_application_post_application_create_hook():state=State({}),initial_step="counter",adapter_set=internal.LifecycleAdapterSet(tracker),+ partition_key="test",+ id="test-123",+ sequence_id=0,)assert "state" in tracker.called_argsassert "application_graph" in tracker.called_args@@ -1239,6 +1425,9 @@ async def test_application_gives_graph():],state=State({}),initial_step="counter",+ partition_key="test",+ id="test-123",+ sequence_id=0,)graph = app.graphassert len(graph.actions) == 2diff --git a/tests/core/test_state.py b/tests/core/test_state.pyindex fa813051..71046339 100644--- a/tests/core/test_state.py+++ b/tests/core/test_state.py@@ -1,6 +1,6 @@import pytest-from burr.core.state import State+from burr.core.state import SQLLitePersistence, Statedef test_state_access():@@ -80,3 +80,34 @@ def test_state_wipe_keep():state = State({"foo": "bar", "baz": "qux"})wiped = state.wipe(keep=["foo"])assert wiped.get_all() == {"foo": "bar"}+++def persistence():+ return SQLLitePersistence(db_path=":memory:", table_name="test_table")+++def test_persistence_initialization_creates_table(persistence):+ persistence.initialize()+ assert persistence.list_app_ids("partition_key") == []+++def test_persistence_saves_and_loads_state(persistence):+ persistence.initialize()+ persistence.save("partition_key", "app_id", 1, "position", State({"key": "value"}), "status")+ loaded_state = persistence.load("partition_key", "app_id")+ assert loaded_state["state"] == State({"key": "value"})+++def test_persistence_returns_none_when_no_state(persistence):+ persistence.initialize()+ loaded_state = persistence.load("partition_key", "app_id")+ assert loaded_state is None+++def test_persistence_lists_app_ids(persistence):+ persistence.initialize()+ persistence.save("partition_key", "app_id1", 1, "position", State({"key": "value"}), "status")+ persistence.save("partition_key", "app_id2", 1, "position", State({"key": "value"}), "status")+ app_ids = persistence.list_app_ids("partition_key")+ assert set(app_ids) == set(["app_id1", "app_id2"])