Add a public workflow-side read API to workflow streams#1641
Closed
DABH wants to merge 1 commit into
Closed
Conversation
WorkflowStream gains read() plus base_offset / end_offset properties so workflow code can consume its own stream's entries deterministically, e.g. items a streaming activity publishes back to the workflow that scheduled it. read() mirrors the poll handler's semantics (topic filtering, global per-item offsets, from_offset=0 meaning the start of whatever is retained, TruncatedOffset for non-zero offsets that fell behind truncation) and subscribe's result_type decoding, and end_offset composes with workflow.wait_condition to consume entries as they land. Additive only; no behavior changes to the publish/poll/query paths.
Contributor
Author
|
Withdrawing this: the consuming integration now implements workflow-side reads over the public get_state() snapshot (decoding the wire entries directly), so no new API surface is needed for this use case. If more in-workflow consumers appear, the design notes here are a starting point — with the naming caveat from review that a waiting iterator should own the subscribe name and read is reserved for post-close retrieval. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What was changed
temporalio.contrib.workflow_streams.WorkflowStream(experimental) gains a public workflow-side read API:WorkflowStream.base_offset— global offset of the oldest entry still retained in the log. Starts at 0, advances whentruncate()discards a prefix, and carries across continue-as-new viaprior_state.WorkflowStream.end_offset— global offset one past the newest entry (base_offset+ retained length); the same value the offset query reports. Monotonic, so it composes withworkflow.wait_conditionpredicates.WorkflowStream.read(topics=None, from_offset=0, *, result_type=None)— a deterministic, non-blocking snapshot of the retained entries at global offsets >=from_offset, each returnedWorkflowStreamItemcarrying its globaloffset. Semantics deliberately mirror the existing public surfaces:result_typedecoding identical toWorkflowStreamClient.subscribe, including theresult_type=RawValueescape hatch and theresult_type=Payloadrejection;from_offset=0means "from the beginning of whatever is retained", while a non-zero offset belowbase_offsetraises anApplicationErrorof typeTruncatedOffset.The change is purely additive — no behavior changes to the publish/poll/query paths. Docstrings for
WorkflowStreamItemandWorkflowTopicHandlewere updated to reflect that a workflow-side read path now exists, and a changelog entry was added.Consuming entries as they land, from workflow code:
Why
The write and subscribe paths of workflow streams are public in both directions (workflow-side
topic().publish(...), client-side publish andsubscribe), but there is no public way for workflow code to read the stream's entries back. Integrations that dispatch streaming work to an activity and consume the streamed items inside the workflow — for example, agent-framework middleware that re-yields streamed activity output into the framework's own async-generator machinery — currently have to reach into the private_log/_base_offsetattributes to do that.A concrete consumer exists: a NeMo Agent Toolkit durability plugin that publishes each streamed item from an activity as an enveloped record on a topic and consumes exactly its own invocation's items, in sequence order, from workflow code, with truncation detection. Its requirements shaped this surface: read entries from an offset, learn the truncation-aware base offset, and iterate as new entries land in a
workflow.wait_condition-compatible way. That consumer's wake predicate also incorporates activity-handle completion, which is why the offset primitives are exposed (enabling arbitrary wait conditions) rather than only a packaged iterator; a convenience iterator can be layered on later if there is demand.Testing
test_workflow_reads_activity_published_items): a workflow consumes items an activity publishes to its own stream via the documentedend_offset+wait_condition+readloop, running withmax_cached_workflows=0so every activation replays the read loop from history (exercises determinism under replay).test_workflow_read_offsets_filters_decode_and_truncation): global offsets with and without topic filters, suffix and empty reads,base_offset/end_offsetbefore and aftertruncate, default andRawValuedecoding, theresult_type=Payloadrejection, and theTruncatedOffseterror path.tests/contrib/workflow_streams/suite passes (46 tests), including against the time-skipping test server.pyright,mypy,basedpyright,pydocstyle,ruffformat/import checks, andgen-docsare all clean.