feat(streaming): tag streamed events with agent_path - #474
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.
| # MANUAL PATCH — mirror into the upstream OpenAPI spec (see .stats.yml | ||
| # openapi_spec_url) or the next Stainless regen drops it. | ||
| agent_path: Optional[Union[str, List[str]]] = None | ||
| """Identifier of the agent that emitted this message. | ||
|
|
||
| A single agent id, or a root->emitter path (e.g. ["researcher", "subagent-abc"]) | ||
| when nested sub-agents share one task stream. Consumers group/filter events by it. | ||
| """ |
There was a problem hiding this comment.
Manual patch in Stainless-generated file
agent_path is added directly to a Stainless-generated model. The project's codegen-boundary rule requires keeping manual additions separate from generated code. The next Stainless regen (triggered by openapi_spec_url update or any other regen run) will silently drop this field — at that point self.task_message.agent_path = ... in StreamingTaskMessageContext.open() still executes (it falls back to extra="allow" attribute storage rather than a declared Pydantic field), but model_dump and SDK serialization will no longer include it in the stream envelope, breaking attribution without raising any error.
The PR description calls this out and asks for maintainer sequencing, but per the codegen boundaries rule this needs to be resolved before merge: either land the field in the OpenAPI spec first, or carry the patch in an overlay/mixin that is explicitly excluded from regen.
Context Used: Keep manual code separate from generated SDK code (source)
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/agentex/types/task_message.py
Line: 30-37
Comment:
**Manual patch in Stainless-generated file**
`agent_path` is added directly to a Stainless-generated model. The project's codegen-boundary rule requires keeping manual additions separate from generated code. The next Stainless regen (triggered by `openapi_spec_url` update or any other regen run) will silently drop this field — at that point `self.task_message.agent_path = ...` in `StreamingTaskMessageContext.open()` still executes (it falls back to `extra="allow"` attribute storage rather than a declared Pydantic field), but `model_dump` and SDK serialization will no longer include it in the stream envelope, breaking attribution without raising any error.
The PR description calls this out and asks for maintainer sequencing, but per the codegen boundaries rule this needs to be resolved before merge: either land the field in the OpenAPI spec first, or carry the patch in an overlay/mixin that is explicitly excluded from regen.
**Context Used:** Keep manual code separate from generated SDK code ([source](https://app.greptile.com/scale-ai/github/scaleapi/scale-agentex-python/-/custom-context?memory=196e336b-2593-4ea1-a710-b9849099a124))
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
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