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
Adds initial lifecycle hooks for streaming
elijahbenizzy6b4d59ddf769
diff --git a/burr/core/application.py b/burr/core/application.pyindex 7b7e6d85..930eda71 100644--- a/burr/core/application.py+++ b/burr/core/application.py@@ -23,7 +23,7 @@ from typing import (cast,)-from burr import telemetry, visibility+from burr import system, telemetry, visibilityfrom burr.common import types as burr_typesfrom burr.core import persistence, validationfrom burr.core.action import (@@ -244,14 +244,23 @@ 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]],+ sequence_id: int,+ app_id: str,+ partition_key: Optional[str],+ lifecycle_adapters: LifecycleAdapterSet = LifecycleAdapterSet(),) -> Generator[Tuple[dict, Optional[State]], None, None]:"""Runs a single step streaming action. This API is internal-facing.This normalizes + validates the output."""action.validate_inputs(inputs)+ stream_initialize_time = system.now()+ first_stream_start_time = Nonegenerator = action.stream_run_and_update(state, **inputs)result = Nonestate_update = None+ count = 0for item in generator:if not isinstance(item, tuple):# TODO -- consider adding support for just returning a result.@@ -261,8 +270,23 @@ def _run_single_step_streaming_action(f"the state update must be None")result, state_update = item+ count += 1if state_update is None:+ if first_stream_start_time is None:+ first_stream_start_time = system.now()+ lifecycle_adapters.call_all_lifecycle_hooks_sync(+ "post_stream_item",+ item=result,+ item_index=count,+ stream_initialize_time=stream_initialize_time,+ first_stream_item_start_time=first_stream_start_time,+ action=action,+ app_id=app_id,+ partition_key=partition_key,+ sequence_id=sequence_id,+ )yield result, None+if state_update is None:raise ValueError(f"Action {action.name} did not return a state update. For streaming actions, the last yield "@@ -274,13 +298,22 @@ def _run_single_step_streaming_action(async def _arun_single_step_streaming_action(- action: SingleStepStreamingAction, state: State, inputs: Optional[Dict[str, Any]]+ action: SingleStepStreamingAction,+ state: State,+ inputs: Optional[Dict[str, Any]],+ sequence_id: int,+ app_id: str,+ partition_key: Optional[str],+ lifecycle_adapters: LifecycleAdapterSet = LifecycleAdapterSet(),) -> AsyncGenerator[Tuple[dict, Optional[State]], None]:"""Runs a single step streaming action in async. See the synchronous version for more details."""action.validate_inputs(inputs)+ stream_initialize_time = system.now()+ first_stream_start_time = Nonegenerator = action.stream_run_and_update(state, **inputs)result = Nonestate_update = None+ count = 0async for item in generator:if not isinstance(item, tuple):# TODO -- consider adding support for just returning a result.@@ -291,6 +324,20 @@ async def _arun_single_step_streaming_action()result, state_update = itemif state_update is None:+ if first_stream_start_time is None:+ first_stream_start_time = system.now()+ await lifecycle_adapters.call_all_lifecycle_hooks_sync_and_async(+ "post_stream_item",+ item=result,+ item_index=count,+ stream_initialize_time=stream_initialize_time,+ first_stream_item_start_time=first_stream_start_time,+ action=action,+ app_id=app_id,+ partition_key=partition_key,+ sequence_id=sequence_id,+ )+ count += 1yield result, Noneif state_update is None:raise ValueError(@@ -304,7 +351,13 @@ async def _arun_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]],+ sequence_id: int,+ app_id: str,+ partition_key: Optional[str],+ lifecycle_adapters: LifecycleAdapterSet = LifecycleAdapterSet(),) -> Generator[Tuple[dict, Optional[State]], None, None]:"""Runs a multi-step streaming action. E.G. one with a run/reduce step.This API is internal-facing. Note that this converts the shape of a@@ -314,8 +367,11 @@ def _run_multi_step_streaming_action(This peeks ahead by one so we know when this is done (and when to validate)."""action.validate_inputs(inputs)+ stream_initialize_time = system.now()generator = action.stream_run(state, **inputs)result = None+ first_stream_start_time = None+ count = 0for item in generator:# We want to peek ahead so we can return the last one# This is slightly eager, but only in the case in which we@@ -323,6 +379,20 @@ def _run_multi_step_streaming_action(next_result = resultresult = itemif next_result is not None:+ if first_stream_start_time is None:+ first_stream_start_time = system.now()+ lifecycle_adapters.call_all_lifecycle_hooks_sync(+ "post_stream_item",+ item=next_result,+ item_index=count,+ stream_initialize_time=stream_initialize_time,+ first_stream_item_start_time=first_stream_start_time,+ action=action,+ app_id=app_id,+ partition_key=partition_key,+ sequence_id=sequence_id,+ )+ count += 1yield next_result, Nonestate_update = _run_reducer(action, state, result, action.name)_validate_result(result, action.name)@@ -331,12 +401,21 @@ def _run_multi_step_streaming_action(async def _arun_multi_step_streaming_action(- action: AsyncStreamingAction, state: State, inputs: Optional[Dict[str, Any]]+ action: AsyncStreamingAction,+ state: State,+ inputs: Optional[Dict[str, Any]],+ sequence_id: int,+ app_id: str,+ partition_key: Optional[str],+ lifecycle_adapters: LifecycleAdapterSet = LifecycleAdapterSet(),) -> AsyncGenerator[Tuple[dict, Optional[State]], None]:"""Runs a multi-step streaming action in async. See the synchronous version for more details."""action.validate_inputs(inputs)+ stream_initialize_time = system.now()generator = action.stream_run(state, **inputs)result = None+ first_stream_start_time = None+ count = 0async for item in generator:# We want to peek ahead so we can return the last one# This is slightly eager, but only in the case in which we@@ -344,6 +423,20 @@ async def _arun_multi_step_streaming_action(next_result = resultresult = itemif next_result is not None:+ if first_stream_start_time is None:+ first_stream_start_time = system.now()+ await lifecycle_adapters.call_all_lifecycle_hooks_sync_and_async(+ "post_stream_item",+ item=next_result,+ stream_initialize_time=stream_initialize_time,+ item_index=count,+ first_stream_item_start_time=first_stream_start_time,+ action=action,+ app_id=app_id,+ partition_key=partition_key,+ sequence_id=sequence_id,+ )+ count += 1yield next_result, Nonestate_update = _run_reducer(action, state, result, action.name)_validate_result(result, action.name)@@ -1248,6 +1341,13 @@ class Application:exception=exc,)call_execute_method_wrapper.call_post(self, exc)+ self._adapter_set.call_all_lifecycle_hooks_sync(+ "post_end_stream",+ action=next_action.name,+ sequence_id=self.sequence_id,+ app_id=self._uid,+ partition_key=self._partition_key,+ )action_inputs = self._process_inputs(inputs, next_action)if not next_action.streaming:@@ -1269,10 +1369,24 @@ class Application:)call_execute_method_wrapper.call_post(self, None)return out+ # Both the next cases start with this+ self._adapter_set.call_all_lifecycle_hooks_sync(+ "pre_start_stream",+ action=next_action.name,+ sequence_id=self.sequence_id,+ app_id=self._uid,+ partition_key=self._partition_key,+ )if next_action.single_step:next_action = cast(SingleStepStreamingAction, next_action)generator = _run_single_step_streaming_action(- next_action, self._state, action_inputs+ action=next_action,+ state=self._state,+ inputs=action_inputs,+ sequence_id=self.sequence_id,+ app_id=self._uid,+ partition_key=self._partition_key,+ lifecycle_adapters=self._adapter_set,)return next_action, StreamingResultContainer(generator, self._state, process_result, callback@@ -1280,7 +1394,13 @@ class Application:else:next_action = cast(StreamingAction, next_action)generator = _run_multi_step_streaming_action(- next_action, self._state, action_inputs+ action=next_action,+ state=self._state,+ inputs=action_inputs,+ sequence_id=self.sequence_id,+ app_id=self._uid,+ partition_key=self._partition_key,+ lifecycle_adapters=self._adapter_set,)except Exception as e:# We only want to raise this in the case of an exception@@ -1470,6 +1590,13 @@ class Application:sequence_id=self.sequence_id,exception=exc,)+ await self._adapter_set.call_all_lifecycle_hooks_sync_and_async(+ "post_end_stream",+ action=next_action.name,+ sequence_id=self.sequence_id,+ app_id=self._uid,+ partition_key=self._partition_key,+ )await call_execute_method_wrapper.acall_post(self, exc)action_inputs = self._process_inputs(inputs, next_action)@@ -1491,6 +1618,13 @@ class Application:return action, AsyncStreamingResultContainer.pass_through(results=result, final_state=state)+ await self._adapter_set.call_all_lifecycle_hooks_sync_and_async(+ "pre_start_stream",+ action=next_action.name,+ sequence_id=self.sequence_id,+ app_id=self._uid,+ partition_key=self._partition_key,+ )if next_action.single_step:next_action = cast(SingleStepStreamingAction, next_action)if not next_action.is_async():@@ -1502,7 +1636,13 @@ class Application:"For now, convert the action to async. Please open up an issue if you hit this. ")generator = _arun_single_step_streaming_action(- next_action, self._state, action_inputs+ action=next_action,+ state=self._state,+ inputs=action_inputs,+ sequence_id=self.sequence_id,+ app_id=self._uid,+ partition_key=self._partition_key,+ lifecycle_adapters=self._adapter_set,)return next_action, AsyncStreamingResultContainer(generator, self._state, process_result, callback@@ -1518,13 +1658,19 @@ class Application:)next_action = cast(AsyncStreamingAction, next_action)generator = _arun_multi_step_streaming_action(- next_action, self._state, action_inputs+ action=next_action,+ state=self._state,+ inputs=action_inputs,+ sequence_id=self.sequence_id,+ app_id=self._uid,+ partition_key=self._partition_key,+ lifecycle_adapters=self._adapter_set,)except Exception as e:# We only want to raise this in the case of an exception# otherwise, this will get delegated to the finally# block of the streaming result container- self._adapter_set.call_all_lifecycle_hooks_sync(+ await self._adapter_set.call_all_lifecycle_hooks_sync_and_async("post_run_step",app_id=self._uid,partition_key=self._partition_key,@@ -1535,6 +1681,13 @@ class Application:exception=e,)await call_execute_method_wrapper.acall_post(self, None, e)+ await self._adapter_set.call_all_lifecycle_hooks_sync_and_async(+ "post_end_stream",+ action=next_action.name,+ sequence_id=self.sequence_id,+ app_id=self._uid,+ partition_key=self._partition_key,+ )raisereturn next_action, AsyncStreamingResultContainer(generator, self._state, process_result, callbackdiff --git a/burr/lifecycle/base.py b/burr/lifecycle/base.pyindex e5d13115..fde253ae 100644--- a/burr/lifecycle/base.py+++ b/burr/lifecycle/base.py@@ -1,4 +1,5 @@import abc+import datetimeimport enumfrom typing import TYPE_CHECKING, Any, Dict, Optional, Union@@ -358,6 +359,120 @@ class PostApplicationExecuteCallHookAsync(abc.ABC):pass[email protected]_hook("pre_start_stream")+class PreStartStreamHook(abc.ABC):+ """Hook that runs after a stream is started.+ If you have a generator, this gets run directly when the generator is called.+ """++ @abc.abstractmethod+ def pre_start_stream(+ self,+ *,+ action: str,+ sequence_id: int,+ app_id: str,+ partition_key: Optional[str],+ **future_kwargs: Any,+ ):+ pass++[email protected]_hook("pre_start_stream")+class PreStartStreamHookAsync(abc.ABC):+ """Hook that runs after a stream is started.+ If you have a generator, this gets run directly when the generator is called.+ """++ @abc.abstractmethod+ async def pre_start_stream(+ self,+ *,+ action: str,+ sequence_id: int,+ app_id: str,+ partition_key: Optional[str],+ **future_kwargs: Any,+ ):+ pass++[email protected]_hook("post_stream_item")+class PostStreamItemHook(abc.ABC):+ """Hook that runs after a stream item is yielded"""++ @abc.abstractmethod+ def post_stream_item(+ self,+ *,+ item: Any,+ item_index: int,+ stream_initialize_time: datetime.datetime,+ first_stream_item_start_time: datetime.datetime,+ action: str,+ sequence_id: int,+ app_id: str,+ partition_key: Optional[str],+ **future_kwargs: Any,+ ):+ pass++[email protected]_hook("post_stream_item")+class PostStreamItemHookAsync(abc.ABC):+ """Hook that runs after a stream item is yielded"""++ @abc.abstractmethod+ async def post_stream_item(+ self,+ *,+ item: Any,+ item_index: int,+ stream_initialize_time: datetime.datetime,+ first_stream_item_start_time: datetime.datetime,+ action: str,+ sequence_id: int,+ app_id: str,+ partition_key: Optional[str],+ **future_kwargs: Any,+ ):+ pass++[email protected]_hook("post_end_stream")+class PostEndStreamHook(abc.ABC):+ """Hook that runs after a stream is ended"""++ @abc.abstractmethod+ def post_end_stream(+ self,+ *,+ action: str,+ sequence_id: int,+ app_id: str,+ partition_key: Optional[str],+ **future_kwargs: Any,+ ):+ pass++[email protected]_hook("post_end_stream")+class PostEndStreamHookAsync(abc.ABC):+ """Hook that runs after a stream is ended"""++ @abc.abstractmethod+ async def post_end_stream(+ self,+ *,+ action: str,+ sequence_id: int,+ app_id: str,+ partition_key: Optional[str],+ **future_kwargs: Any,+ ):+ pass++# strictly for typing -- this conflicts a bit with the lifecycle decorator above, but its fine for now# This makes IDE completion/type-hinting easierLifecycleAdapter = Union[@@ -375,4 +490,10 @@ LifecycleAdapter = Union[PreStartSpanHookAsync,PostEndSpanHook,PostEndSpanHookAsync,+ PreStartStreamHook,+ PostStreamItemHook,+ PostEndStreamHook,+ PreStartStreamHookAsync,+ PostStreamItemHookAsync,+ PostEndStreamHookAsync,]diff --git a/burr/system.py b/burr/system.pyindex 4e17099e..3c34876a 100644--- a/burr/system.py+++ b/burr/system.py@@ -1,3 +1,14 @@+import datetimeimport os+import sysIS_WINDOWS = os.name == "nt"++if sys.version_info >= (3, 11):+ utc = datetime.UTC+else:+ utc = datetime.timezone.utc+++def now():+ return datetime.datetime.now(utc)diff --git a/burr/tracking/server/s3/backend.py b/burr/tracking/server/s3/backend.pyindex d5b46230..879d7ec7 100644--- a/burr/tracking/server/s3/backend.py+++ b/burr/tracking/server/s3/backend.py@@ -6,7 +6,6 @@ import jsonimport loggingimport operatorimport os.path-import sysimport uuidfrom collections import Counterfrom typing import List, Literal, Optional, Sequence, Tuple, Type, TypeVar, Union@@ -20,6 +19,7 @@ from tortoise import functions, transactionsfrom tortoise.contrib.fastapi import RegisterTortoisefrom tortoise.expressions import Q+from burr import systemfrom burr.tracking.common.models import ApplicationModelfrom burr.tracking.server import schemafrom burr.tracking.server.backend import (@@ -45,11 +45,6 @@ FileType = Literal["log", "metadata", "graph"]ContentsModel = TypeVar("ContentsModel", bound=pydantic.BaseModel)-if sys.version_info >= (3, 11):- utc = datetime.UTC-else:- utc = datetime.timezone.utc-async def _query_s3_file(bucket: str,@@ -132,7 +127,7 @@ class S3Settings(BurrSettings):def timestamp_to_reverse_alphabetical(timestamp: datetime) -> str:# Get the inverse of the timestamp- epoch = datetime.datetime(1970, 1, 1, tzinfo=datetime.timezone.utc)+ epoch = datetime.datetime(1970, 1, 1, tzinfo=system.utc)total_seconds = int((timestamp - epoch).total_seconds())# Invert the seconds (latest timestamps become smallest values)@@ -154,7 +149,7 @@ class SQLiteS3Backend(BackendBase, IndexingBackendMixin, SnapshottingBackendMixiload_snapshot_on_start: bool,prior_snapshots_to_keep: int,):- self._backend_id = datetime.datetime.now(utc).isoformat() + str(uuid.uuid4())+ self._backend_id = system.now().isoformat() + str(uuid.uuid4())self._bucket = s3_bucketself._session = session.get_session()self._update_interval_milliseconds = update_interval_milliseconds@@ -198,7 +193,7 @@ class SQLiteS3Backend(BackendBase, IndexingBackendMixin, SnapshottingBackendMixiasync def snapshot(self):path = settings.DB_PATH- timestamp = timestamp_to_reverse_alphabetical(datetime.datetime.now(datetime.UTC))+ timestamp = timestamp_to_reverse_alphabetical(system.now())# latests3_key = f"{self._snapshot_prefix}/{timestamp}/{self._backend_id}/snapshot.db"# TODO -- copy the path at snapshot_path to s3 using aiobotocore@@ -230,9 +225,7 @@ class SQLiteS3Backend(BackendBase, IndexingBackendMixin, SnapshottingBackendMixiif "Contents" in result:first_object = result["Contents"][0]return first_object["LastModified"]- return (- datetime.datetime.utcnow()- ) # This should never be hit unless someone is concurrently deleting...+ return system.now() # This should never be hit unless someone is concurrently deleting...async def _update_projects(self):current_projects = await Project.all()@@ -246,7 +239,7 @@ class SQLiteS3Backend(BackendBase, IndexingBackendMixin, SnapshottingBackendMixifor prefix in result.get("CommonPrefixes", []):project_name = prefix.get("Prefix").split("/")[-2]if project_name not in project_names:- now = datetime.datetime.utcnow()+ now = system.now()logger.info(f"Creating project: {project_name}")await Project.create(name=project_name,@@ -384,7 +377,7 @@ class SQLiteS3Backend(BackendBase, IndexingBackendMixin, SnapshottingBackendMixiname=app_id,partition_key=pk,project=project,- created_at=datetime.datetime.utcnow(),+ created_at=system.now(),)for app_id, pk in all_application_keysif (app_id, pk) not in existing_applications@@ -512,7 +505,7 @@ class SQLiteS3Backend(BackendBase, IndexingBackendMixin, SnapshottingBackendMixilogger.info(f"Scanned: {num_files} files with status stored at ID={status.id}")indexing_job.records_processed = num_files- indexing_job.end_time = datetime.datetime.utcnow()+ indexing_job.end_time = system.now()# TODO -- handle failureindexing_job.status = IndexingJobStatus.SUCCESSindexing_job.index_status = statusdiff --git a/tests/core/test_application.py b/tests/core/test_application.pyindex 3660fff7..29207eb4 100644--- a/tests/core/test_application.py+++ b/tests/core/test_application.py@@ -1,5 +1,6 @@import asyncioimport collections+import datetimeimport loggingimport typingfrom typing import Any, Awaitable, Callable, Dict, Generator, Literal, Optional, Tuple@@ -52,8 +53,13 @@ from burr.lifecycle.base import (PostApplicationCreateHook,PostApplicationExecuteCallHook,PostApplicationExecuteCallHookAsync,+ PostEndStreamHook,+ PostStreamItemHook,+ PostStreamItemHookAsync,PreApplicationExecuteCallHook,PreApplicationExecuteCallHookAsync,+ PreStartStreamHook,+ PreStartStreamHookAsync,)from burr.lifecycle.internal import LifecycleAdapterSetfrom burr.tracking.base import SyncTrackingClient@@ -128,8 +134,101 @@ base_counter_action_with_inputs = PassedInAction()-class ActionTracker(- PreRunStepHook, PostRunStepHook, PreApplicationExecuteCallHook, PostApplicationExecuteCallHook+class StreamEventCaptureTracker(PreStartStreamHook, PostStreamItemHook, PostEndStreamHook):+ def post_end_stream(+ self,+ *,+ action: str,+ sequence_id: int,+ app_id: str,+ partition_key: Optional[str],+ **future_kwargs: Any,+ ):+ self.post_end_stream_calls.append((action, locals()))++ def __init__(self):+ self.pre_start_stream_calls = []+ self.post_stream_item_calls = []+ self.post_end_stream_calls = []++ def pre_start_stream(+ self,+ *,+ action: str,+ sequence_id: int,+ app_id: str,+ partition_key: Optional[str],+ **future_kwargs: Any,+ ):+ self.pre_start_stream_calls.append((action, locals()))++ def post_stream_item(+ self,+ *,+ item: Any,+ item_index: int,+ stream_initialize_time: datetime.datetime,+ first_stream_item_start_time: datetime.datetime,+ action: str,+ sequence_id: int,+ app_id: str,+ partition_key: Optional[str],+ **future_kwargs: Any,+ ):+ self.post_stream_item_calls.append((action, locals()))+++class StreamEventCaptureTrackerAsync(+ PreStartStreamHookAsync, PostStreamItemHookAsync, PostEndStreamHook+):+ def __init__(self):+ self.pre_start_stream_calls = []+ self.post_stream_item_calls = []+ self.post_end_stream_calls = []++ async def pre_start_stream(+ self,+ *,+ action: str,+ sequence_id: int,+ app_id: str,+ partition_key: Optional[str],+ **future_kwargs: Any,+ ):+ self.pre_start_stream_calls.append((action, locals()))++ async def post_stream_item(+ self,+ *,+ item: Any,+ item_index: int,+ stream_initialize_time: datetime.datetime,+ first_stream_item_start_time: datetime.datetime,+ action: str,+ sequence_id: int,+ app_id: str,+ partition_key: Optional[str],+ **future_kwargs: Any,+ ):+ self.post_stream_item_calls.append((action, locals()))++ def post_end_stream(+ self,+ *,+ action: str,+ sequence_id: int,+ app_id: str,+ partition_key: Optional[str],+ **future_kwargs: Any,+ ):+ self.post_end_stream_calls.append((action, locals()))+++class CallCaptureTracker(+ PreRunStepHook,+ PostRunStepHook,+ PreApplicationExecuteCallHook,+ PostApplicationExecuteCallHook,):def __init__(self):self.pre_called = []@@ -459,7 +558,9 @@ def test_run_single_step_streaming_action_errors_missing_write():action = BrokenAction()state = State()with pytest.raises(ValueError, match="missing_value"):- gen = _run_single_step_streaming_action(action, state, inputs={})+ gen = _run_single_step_streaming_action(+ action, state, inputs={}, sequence_id=0, partition_key="partition_key", app_id="app_id"+ )collections.deque(gen, maxlen=0) # exhaust the generator@@ -482,7 +583,15 @@ async def test_run_single_step_streaming_action_errors_missing_write_async():action = BrokenAction()state = State()with pytest.raises(ValueError, match="missing_value"):- gen = _arun_single_step_streaming_action(action, state, inputs={})+ gen = _arun_single_step_streaming_action(+ action,+ state,+ inputs={},+ sequence_id=0,+ app_id="app_id",+ partition_key="partition_key",+ lifecycle_adapters=LifecycleAdapterSet(),+ )[result async for result in gen] # exhaust the generator@@ -506,7 +615,9 @@ def test_run_multi_step_streaming_action_errors_missing_write():action = BrokenAction()state = State()with pytest.raises(ValueError, match="missing_value"):- gen = _run_multi_step_streaming_action(action, state, inputs={})+ gen = _run_multi_step_streaming_action(+ action, state, inputs={}, sequence_id=0, partition_key="partition_key", app_id="app_id"+ )collections.deque(gen, maxlen=0) # exhaust the generator@@ -822,7 +933,9 @@ def test__run_single_step_action_deletes_state():def test__run_multistep_streaming_action():action = base_streaming_counter.with_name("counter")state = State({"count": 0, "tracker": []})- generator = _run_multi_step_streaming_action(action, state, inputs={})+ generator = _run_multi_step_streaming_action(+ action, state, inputs={}, sequence_id=0, partition_key="partition_key", app_id="app_id"+ )last_result = -1result = Nonefor result, state in generator:@@ -834,10 +947,51 @@ def test__run_multistep_streaming_action():assert state.subset("count", "tracker").get_all() == {"count": 1, "tracker": [1]}+def test__run_multistep_streaming_action_callbacks():+ class TrackingCallback(PostStreamItemHook):+ def __init__(self):+ self.items = []++ def post_stream_item(self, item: Any, **future_kwargs: Any):+ self.items.append(item)++ hook = TrackingCallback()++ action = base_streaming_counter.with_name("counter")+ state = State({"count": 0, "tracker": []})+ generator = _run_multi_step_streaming_action(+ action,+ state,+ inputs={},+ sequence_id=0,+ partition_key="partition_key",+ app_id="app_id",+ lifecycle_adapters=LifecycleAdapterSet(hook),+ )+ last_result = -1+ result = None+ for result, state in generator:+ if last_result < 1:+ # Otherwise you hit floating poit comparison problems+ assert result["count"] > last_result+ last_result = result["count"]+ assert result == {"count": 1}+ assert state.subset("count", "tracker").get_all() == {"count": 1, "tracker": [1]}+ assert len(hook.items) == 10 # one for each streaming callback++async def test__run_multistep_streaming_action_async():action = base_streaming_counter_async.with_name("counter")state = State({"count": 0, "tracker": []})- generator = _arun_multi_step_streaming_action(action, state, inputs={})+ generator = _arun_multi_step_streaming_action(+ action=action,+ state=state,+ inputs={},+ sequence_id=0,+ app_id="app_id",+ partition_key="partition_key",+ lifecycle_adapters=LifecycleAdapterSet(),+ )last_result = -1result = Noneasync for result, state in generator:@@ -849,11 +1003,45 @@ async def test__run_multistep_streaming_action_async():assert state.subset("count", "tracker").get_all() == {"count": 1, "tracker": [1]}+async def test__run_multistep_streaming_action_async_callbacks():+ class TrackingCallback(PostStreamItemHookAsync):+ def __init__(self):+ self.items = []++ async def post_stream_item(self, item: Any, **future_kwargs: Any):+ self.items.append(item)++ hook = TrackingCallback()+ action = base_streaming_counter_async.with_name("counter")+ state = State({"count": 0, "tracker": []})+ generator = _arun_multi_step_streaming_action(+ action=action,+ state=state,+ inputs={},+ sequence_id=0,+ app_id="app_id",+ partition_key="partition_key",+ lifecycle_adapters=LifecycleAdapterSet(hook),+ )+ last_result = -1+ result = None+ async for result, state in generator:+ if last_result < 1:+ # Otherwise you hit floating poit comparison problems+ assert result["count"] > last_result+ last_result = result["count"]+ assert result == {"count": 1}+ assert state.subset("count", "tracker").get_all() == {"count": 1, "tracker": [1]}+ assert len(hook.items) == 10 # one for each streaming callback++def test__run_streaming_action_incorrect_result_type():action = StreamingActionIncorrectResultType()state = State()with pytest.raises(ValueError, match="returned a non-dict"):- gen = _run_multi_step_streaming_action(action, state, inputs={})+ gen = _run_multi_step_streaming_action(+ action, state, inputs={}, sequence_id=0, partition_key="partition_key", app_id="app_id"+ )collections.deque(gen, maxlen=0) # exhaust the generator@@ -861,7 +1049,15 @@ async def test__run_streaming_action_incorrect_result_type_async():action = StreamingActionIncorrectResultTypeAsync()state = State()with pytest.raises(ValueError, match="returned a non-dict"):- gen = _arun_multi_step_streaming_action(action, state, inputs={})+ gen = _arun_multi_step_streaming_action(+ action=action,+ state=state,+ inputs={},+ sequence_id=0,+ app_id="app_id",+ partition_key="partition_key",+ lifecycle_adapters=LifecycleAdapterSet(),+ )async for _ in gen:pass@@ -870,7 +1066,14 @@ def test__run_single_step_streaming_action_incorrect_result_type():action = StreamingSingleStepActionIncorrectResultType()state = State()with pytest.raises(ValueError, match="returned a non-dict"):- gen = _run_single_step_streaming_action(action, state, inputs={})+ gen = _run_single_step_streaming_action(+ action=action,+ state=state,+ inputs={},+ sequence_id=0,+ partition_key="partition_key",+ app_id="app_id",+ )collections.deque(gen, maxlen=0) # exhaust the generator@@ -878,14 +1081,24 @@ async def test__run_single_step_streaming_action_incorrect_result_type_async():action = StreamingSingleStepActionIncorrectResultTypeAsync()state = State()with pytest.raises(ValueError, match="returned a non-dict"):- gen = _arun_single_step_streaming_action(action, state, inputs={})+ gen = _arun_single_step_streaming_action(+ action,+ state,+ inputs={},+ sequence_id=0,+ partition_key="partition_key",+ app_id="app_id",+ lifecycle_adapters=LifecycleAdapterSet(),+ )_ = [item async for item in gen]def test__run_single_step_streaming_action():action = base_streaming_single_step_counter.with_name("counter")state = State({"count": 0, "tracker": []})- generator = _run_single_step_streaming_action(action, state, inputs={})+ generator = _run_single_step_streaming_action(+ action, state, inputs={}, sequence_id=0, partition_key="partition_key", app_id="app_id"+ )last_result = -1result, state = None, Nonefor result, state in generator:@@ -898,10 +1111,53 @@ def test__run_single_step_streaming_action():assert state.subset("count", "tracker").get_all() == {"count": 1, "tracker": [1]}+def test__run_single_step_streaming_action_calls_callbacks():+ action = base_streaming_single_step_counter.with_name("counter")++ class TrackingCallback(PostStreamItemHook):+ def __init__(self):+ self.items = []++ def post_stream_item(self, item: Any, **future_kwargs: Any):+ self.items.append(item)++ hook = TrackingCallback()++ state = State({"count": 0, "tracker": []})+ generator = _run_single_step_streaming_action(+ action,+ state,+ inputs={},+ sequence_id=0,+ partition_key="partition_key",+ app_id="app_id",+ lifecycle_adapters=LifecycleAdapterSet(hook),+ )+ last_result = -1+ result, state = None, None+ for result, state in generator:+ if last_result < 1:+ # Otherwise you hit comparison issues+ # This is because we get to the last one, which is the final result+ assert result["count"] > last_result+ last_result = result["count"]+ assert result == {"count": 1}+ assert state.subset("count", "tracker").get_all() == {"count": 1, "tracker": [1]}+ assert len(hook.items) == 10 # one for each streaming callback++async def test__run_single_step_streaming_action_async():async_action = base_streaming_single_step_counter_async.with_name("counter")state = State({"count": 0, "tracker": []})- generator = _arun_single_step_streaming_action(async_action, state, inputs={})+ generator = _arun_single_step_streaming_action(+ action=async_action,+ state=state,+ inputs={},+ sequence_id=0,+ app_id="app_id",+ partition_key="partition_key",+ lifecycle_adapters=LifecycleAdapterSet(),+ )last_result = -1result, state = None, Noneasync for result, state in generator:@@ -914,6 +1170,40 @@ async def test__run_single_step_streaming_action_async():assert state.subset("count", "tracker").get_all() == {"count": 1, "tracker": [1]}+async def test__run_single_step_streaming_action_async_callbacks():+ class TrackingCallback(PostStreamItemHookAsync):+ def __init__(self):+ self.items = []++ async def post_stream_item(self, item: Any, **future_kwargs: Any):+ self.items.append(item)++ hook = TrackingCallback()++ async_action = base_streaming_single_step_counter_async.with_name("counter")+ state = State({"count": 0, "tracker": []})+ generator = _arun_single_step_streaming_action(+ action=async_action,+ state=state,+ inputs={},+ sequence_id=0,+ app_id="app_id",+ partition_key="partition_key",+ lifecycle_adapters=LifecycleAdapterSet(hook),+ )+ last_result = -1+ result, state = None, None+ async for result, state in generator:+ if last_result < 1:+ # Otherwise you hit comparison issues+ # This is because we get to the last one, which is the final result+ assert result["count"] > last_result+ last_result = result["count"]+ assert result == {"count": 1}+ assert state.subset("count", "tracker").get_all() == {"count": 1, "tracker": [1]}+ assert len(hook.items) == 10 # one for each streaming callback++class SingleStepActionWithDeletionAsync(SingleStepActionWithDeletion):async def run_and_update(self, state: State, **run_kwargs) -> Tuple[dict, State]:return {}, state.wipe(delete=["to_delete"])@@ -1533,13 +1823,14 @@ async def test_app_a_run_async_and_sync():def test_stream_result_halt_after_unique_ordered_sequence_id():- action_tracker = ActionTracker()+ action_tracker = CallCaptureTracker()+ stream_event_tracker = StreamEventCaptureTracker()counter_action = base_streaming_counter.with_name("counter")counter_action_2 = base_streaming_counter.with_name("counter_2")app = Application(state=State({"count": 0}),entrypoint="counter",- adapter_set=LifecycleAdapterSet(action_tracker),+ adapter_set=LifecycleAdapterSet(action_tracker, stream_event_tracker),partition_key="test",uid="test-123",graph=Graph(@@ -1570,16 +1861,26 @@ def test_stream_result_halt_after_unique_ordered_sequence_id():# One post call/one pre-call, as we call stream_result onceassert len(action_tracker.post_run_execute_calls) == 1assert len(action_tracker.pre_run_execute_calls) == 1+ # One call for streaming+ assert len(stream_event_tracker.pre_start_stream_calls) == 1+ # 10 streaming items+ assert len(stream_event_tracker.post_stream_item_calls) == 10+ # One call for streaming+ assert len(stream_event_tracker.post_end_stream_calls) == 1async def test_astream_result_halt_after_unique_ordered_sequence_id():- action_tracker = ActionTracker()+ action_tracker = CallCaptureTracker()+ stream_event_tracker = StreamEventCaptureTrackerAsync()+ stream_event_tracker_sync = StreamEventCaptureTracker()counter_action = base_streaming_counter_async.with_name("counter")counter_action_2 = base_streaming_counter_async.with_name("counter_2")app = Application(state=State({"count": 0}),entrypoint="counter",- adapter_set=LifecycleAdapterSet(action_tracker),+ adapter_set=LifecycleAdapterSet(+ action_tracker, stream_event_tracker, stream_event_tracker_sync+ ),partition_key="test",uid="test-123",graph=Graph(@@ -1613,17 +1914,37 @@ async def test_astream_result_halt_after_unique_ordered_sequence_id():assert len(action_tracker.post_run_execute_calls) == 1assert len(action_tracker.pre_run_execute_calls) == 1+ # One call for streaming+ assert (+ len(stream_event_tracker.pre_start_stream_calls)+ == len(stream_event_tracker_sync.pre_start_stream_calls)+ == 1+ )+ # 10 streaming items+ assert (+ len(stream_event_tracker.post_stream_item_calls)+ == len(stream_event_tracker_sync.post_stream_item_calls)+ == 10+ )+ # One call for streaming+ assert (+ len(stream_event_tracker.post_end_stream_calls)+ == len(stream_event_tracker_sync.post_end_stream_calls)+ == 1+ )+def test_stream_result_halt_after_run_through_streaming():"""Tests that we can pass through streaming results,fully realize them, then get to the streaming results at the end and return the stream"""- action_tracker = ActionTracker()+ action_tracker = CallCaptureTracker()+ stream_event_tracker = StreamEventCaptureTracker()counter_action = base_streaming_single_step_counter.with_name("counter")counter_action_2 = base_streaming_single_step_counter.with_name("counter_2")app = Application(state=State({"count": 0}),entrypoint="counter",- adapter_set=LifecycleAdapterSet(action_tracker),+ adapter_set=LifecycleAdapterSet(action_tracker, stream_event_tracker),partition_key="test",uid="test-123",graph=Graph(@@ -1653,10 +1974,19 @@ def test_stream_result_halt_after_run_through_streaming():] # ensure sequence ID is respectedassert len(action_tracker.post_run_execute_calls) == 1assert len(action_tracker.pre_run_execute_calls) == 1+ # One call for streaming+ assert len(stream_event_tracker.pre_start_stream_calls) == 1+ # 10 streaming items+ assert len(stream_event_tracker.post_stream_item_calls) == 10+ # One call for streaming+ assert len(stream_event_tracker.post_end_stream_calls) == 1async def test_astream_result_halt_after_run_through_streaming():- action_tracker = ActionTracker()+ action_tracker = CallCaptureTracker()+ stream_event_tracker = StreamEventCaptureTrackerAsync()+ sync_stream_event_tracker = StreamEventCaptureTracker()+counter_action = base_streaming_single_step_counter_async.with_name("counter")counter_action_2 = base_streaming_single_step_counter_async.with_name("counter_2")assert counter_action.is_async()@@ -1664,7 +1994,9 @@ async def test_astream_result_halt_after_run_through_streaming():app = Application(state=State({"count": 0}),entrypoint="counter",- adapter_set=LifecycleAdapterSet(action_tracker),+ adapter_set=LifecycleAdapterSet(+ action_tracker, stream_event_tracker, sync_stream_event_tracker+ ),partition_key="test",uid="test-123",graph=Graph(@@ -1697,18 +2029,38 @@ async def test_astream_result_halt_after_run_through_streaming():assert len(action_tracker.post_run_execute_calls) == 1assert len(action_tracker.pre_run_execute_calls) == 1+ # One call for streaming+ assert (+ len(stream_event_tracker.pre_start_stream_calls)+ == len(sync_stream_event_tracker.pre_start_stream_calls)+ == 1+ )+ # 10 streaming items+ assert (+ len(stream_event_tracker.post_stream_item_calls)+ == len(sync_stream_event_tracker.post_stream_item_calls)+ == 10+ )+ # One call for streaming+ assert (+ len(stream_event_tracker.post_end_stream_calls)+ == len(sync_stream_event_tracker.post_end_stream_calls)+ == 1+ )+def test_stream_result_halt_after_run_through_non_streaming():"""Tests what happens when we have an app that runs through non-streamingresults before hitting a final streaming result specified by halt_after"""- action_tracker = ActionTracker()+ action_tracker = CallCaptureTracker()+ stream_event_tracker = StreamEventCaptureTracker()counter_non_streaming = base_counter_action.with_name("counter_non_streaming")counter_streaming = base_streaming_single_step_counter.with_name("counter_streaming")app = Application(state=State({"count": 0}),entrypoint="counter_non_streaming",- adapter_set=LifecycleAdapterSet(action_tracker),+ adapter_set=LifecycleAdapterSet(action_tracker, stream_event_tracker),partition_key="test",uid="test-123",graph=Graph(@@ -1743,16 +2095,27 @@ def test_stream_result_halt_after_run_through_non_streaming():assert len(action_tracker.post_run_execute_calls) == 1assert len(action_tracker.pre_run_execute_calls) == 1+ # One call for streaming+ assert len(stream_event_tracker.pre_start_stream_calls) == 1+ # 10 streaming items+ assert len(stream_event_tracker.post_stream_item_calls) == 10+ # One call for streaming+ assert len(stream_event_tracker.post_end_stream_calls) == 1+async def test_astream_result_halt_after_run_through_non_streaming():- action_tracker = ActionTracker()+ action_tracker = CallCaptureTracker()+ stream_event_tracker = StreamEventCaptureTrackerAsync()+ stream_event_tracker_sync = StreamEventCaptureTracker()counter_non_streaming = base_counter_action_async.with_name("counter_non_streaming")counter_streaming = base_streaming_single_step_counter_async.with_name("counter_streaming")app = Application(state=State({"count": 0}),entrypoint="counter_non_streaming",- adapter_set=LifecycleAdapterSet(action_tracker),+ adapter_set=LifecycleAdapterSet(+ action_tracker, stream_event_tracker, stream_event_tracker_sync+ ),partition_key="test",uid="test-123",graph=Graph(@@ -1789,10 +2152,29 @@ async def test_astream_result_halt_after_run_through_non_streaming():assert len(action_tracker.post_run_execute_calls) == 1assert len(action_tracker.pre_run_execute_calls) == 1+ # One call for streaming+ assert (+ len(stream_event_tracker.pre_start_stream_calls)+ == len(stream_event_tracker_sync.pre_start_stream_calls)+ == 1+ )+ # 10 streaming items+ assert (+ len(stream_event_tracker.post_stream_item_calls)+ == len(stream_event_tracker_sync.post_stream_item_calls)+ == 10+ )+ # One call for streaming+ assert (+ len(stream_event_tracker.post_end_stream_calls)+ == len(stream_event_tracker_sync.post_end_stream_calls)+ == 1+ )+def test_stream_result_halt_after_run_through_final_non_streaming():"""Tests that we can pass through non-streaming results when streaming is called"""- action_tracker = ActionTracker()+ action_tracker = CallCaptureTracker()counter_non_streaming = base_counter_action.with_name("counter_non_streaming")counter_final_non_streaming = base_counter_action.with_name("counter_final_non_streaming")@@ -1837,7 +2219,8 @@ def test_stream_result_halt_after_run_through_final_non_streaming():async def test_astream_result_halt_after_run_through_final_streaming():"""Tests that we can pass through non-streaming results when streaming is called"""- action_tracker = ActionTracker()+ action_tracker = CallCaptureTracker()+counter_non_streaming = base_counter_action_async.with_name("counter_non_streaming")counter_final_non_streaming = base_counter_action_async.with_name("counter_final_non_streaming")@@ -1885,7 +2268,7 @@ async def test_astream_result_halt_after_run_through_final_streaming():def test_stream_result_halt_before():- action_tracker = ActionTracker()+ action_tracker = CallCaptureTracker()counter_non_streaming = base_counter_action.with_name("counter_non_streaming")counter_streaming = base_streaming_single_step_counter.with_name("counter_final")@@ -1921,7 +2304,7 @@ def test_stream_result_halt_before():async def test_astream_result_halt_before():- action_tracker = ActionTracker()+ action_tracker = CallCaptureTracker()counter_non_streaming = base_counter_action_async.with_name("counter_non_streaming")counter_streaming = base_streaming_single_step_counter_async.with_name("counter_final")@@ -2069,7 +2452,7 @@ def test_application_builder_unset():def test_application_run_step_hooks_sync():- action_tracker = ActionTracker()+ action_tracker = CallCaptureTracker()counter_action = base_counter_action.with_name("counter")result_action = Result("count").with_name("result")app = Application(@@ -2164,7 +2547,7 @@ async def test_application_run_step_hooks_async():async def test_application_run_step_runs_hooks():- hooks = [ActionTracker(), ActionTrackerAsync()]+ hooks = [CallCaptureTracker(), ActionTrackerAsync()]counter_action = base_counter_action.with_name("counter")app = Application(