[BUG] Background workflow tools publish every nested-agent chunk to the shared background-tasks-result topic (60MB events, Redis OOM)
Describe the Bug
A background workflow tool publishes every chunk its nested agents write to the shared background-tasks-result topic, and there is no way to filter them. On Redis Streams pubsub, a single event reached 61.6MB. Six task runs put 379MB into the stream, which ran a 1GB Redis out of memory (allkeys-lru, 2,689 keys evicted).
In packages/core/src/background-tasks/workflow.ts, onProgress publishes each chunk as it comes:
await manager.publishLifecycleEvent('task.output', { ...task, chunk });For a workflow tool whose steps call an agent, those chunks include the agent's step-start, step-finish and finish parts. Each of those carries the full model request, including base64 file parts. In our stream, step-finish plus finish made up 91% of the bytes. publishLifecycleEvent also copies task.args into every event, so a large input is repeated once per chunk.
What makes it worse:
- One topic for everything. Every task on every pod goes to
background-tasks-result, and every pod consumes all of it. - No filter hook. Nothing lets you drop or slim a chunk before it's published.
progressThrottleMsonly rate-limits, so it can drop the one progress event a UI needs and still let a 60MB snapshot through.
Steps To Reproduce
- Configure
RedisStreamsPubSubas Mastra's pubsub, and enablebackgroundTasks. - Create a workflow with a
.foreachwhose step callsagent.generate()with a PDF file part of about 1MB. Expose it to an agent as a background workflow tool. - Have the agent call the tool with about 6 items.
- Measure the stream with
XRANGE background-tasks-result - +and sum the entry sizes.MEMORY USAGEsamples, so it undercounts badly.
Each task.output entry holding a nested agent's step-finish/finish is roughly the size of the whole request, attachment included.
Link to Minimal Reproducible Example
Not yet. Steps 1–3 above are the whole repro, and I can turn them into a repo if that helps.
Expected Behavior
A background task's internal agent chunks don't reach the pubsub, or you can filter them. Any of these would fix it:
- Don't forward a nested agent's internal parts (
step-*,finish, request snapshots) astask.output. Forward only the workflow's own events (workflow-step-progress, step start/finish), which is what a progress UI reads. - Add a hook such as
backgroundTasks.transformOutput(chunk, task) => chunk | null, so an app can slim or drop a chunk before it's published. - Stop repeating
argson everytask.outputevent. - Publish per task or per thread rather than to one global topic, and cap the event size or the stream length (
MAXLEN) in@mastra/redis-streams.
Our workaround is to subclass RedisStreamsPubSub and drop, in publish(), every task.output event whose chunk.payload.output.type isn't workflow-step-progress.
Environment Information
@mastra/core1.71.0 (still the same onmain)@mastra/redis-streams0.5.1- Node 24, Redis with 1GB and allkeys-lru
- Related: #15803 (
progressThrottleMs)
Verification
- I have searched the existing issues to make sure this is not a duplicate
- I have included sufficient information for the team to reproduce and understand the issue
Source: mastra-ai/mastra