feat(streaming): tag streamed events with agent_path - #474
Open
vkalmathscale wants to merge 8 commits into
Open
Conversation
…t in workflow templates
…ty trace (#465) Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
Streamed lifecycle/text events carried only a task_id, so events from multiple agents sharing one task stream (e.g. child-workflow sub-agents emitting into the orchestrator's stream) were indistinguishable. Thread an optional agent_path (str | list[str]) end to end: - TaskMessage envelope carries it, so every start/delta/full/done event is attributable via parent_task_message. - ContextInterceptor propagates it workflow->activity via a new ContextVar + header, so the model activity tags live text/reasoning deltas. - TemporalStreamingHooks + stream_lifecycle_content + streaming service stamp it on tool/lifecycle events. Backward compatible: agent_path defaults to None (untagged) everywhere. NOTE: task_message.py is Stainless-generated; the agent_path field is a manual stopgap and must be mirrored in the upstream OpenAPI spec (.stats.yml openapi_spec_url) or the next regen will drop it.
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.
Why
Streamed lifecycle and text events carry only a
task_id. When multiple agents share one task stream (for example, child-workflow sub-agents emitting into a parent orchestrator's stream), their events are indistinguishable at the consumer. The only identity-ish field isauthor(Literal["user","agent"]), a role, not a source.What
Thread an optional
agent_path(str | list[str]), a single agent id or a root-to-emitter path like["researcher", "subagent-abc"], end to end:TaskMessage.agent_pathon the envelope, stamped on the in-memory message aftermessages.create(), so everystart/delta/full/doneevent carries it viaparent_task_message. No change to themessages.create/updateclient contract (stream-only tag).ContextInterceptorgains astreaming_agent_pathContextVar plus acontext-agent-pathheader, threaded from workflow to activity independently of the task/trace/span gate (a child workflow can set only_agent_path). This lets the offloaded model activity tag live text and reasoning deltas.TemporalStreamingHookstakesagent_pathand forwards it on tool-request, tool-response, and handoff;stream_lifecycle_contentand the streaming service/facade pass it to the envelope;TemporalStreamingModelreads the ContextVar for its three streaming contexts.Backward compatible
agent_pathdefaults toNone(untagged) everywhere. Existing callers, including subclasses that callsuper().__init__(task_id=..., timeout=...), are unaffected.Depends on scaleapi/scale-agentex#381 (do not merge standalone)
The
TaskMessage.agent_pathwire field is authored upstream in the backend OpenAPI spec (scaleapi/scale-agentex#381). The edit tosrc/agentex/types/task_message.pyin this PR is a temporary manual patch so the branch is functional and testable now; merging a hand-edited generated type would be wiped on the next Stainless regen.Merge order:
openapi.yaml).next, addingagent_pathto the generatedtypes/task_message.py.types/task_message.pypatch, leaving only thelib/runtime.Verification
py_compileclean on all touched files;lint(ruff/FA102) fixed.TaskMessage.agent_pathserializes correctly forstr,list[str], and theNonedefault viamodel_dump(mode="json").Follow-ups (not in this PR)
parent_task_message.agent_paththrough a singletoPath(x) = Array.isArray(x) ? x : [x]normalizer (for thestr | listunion).agent_path(the parent sets each child's path) and retire any bespoke per-agent attribution workaround.Generated with Claude Code