mirror of
https://github.com/mastra-ai/mastra.git
synced 2026-11-17 08:44:46 +00:00
fix(core): answer once when the model already saw a background result under untilIdle (#29848)
## Description
An `untilIdle` stream sends a follow-up model request for every
background task it sees finish, so the model can report the result. When
the model already answered with that result in the same run, the
follow-up makes it answer a second time. This happened in two cases:
- an awaited background tool, whose tool call waits for the task and
hands the result to the model;
- a deferred task that finished while its run was still going, so a
later request in the run already carried the real result.
The LLM step of an `untilIdle` inner run now lists, on `step-start`, the
background tasks whose final result is in the request's prompt. Task
status is read before the prompt is built, and a result counts only if
request processors left it unchanged. Cached replies never count. The
wrapper skips the follow-up for those tasks once that step ends in a
real reply: no client tool call, no error, and a finish reason other
than `tool-calls`, `error`, `tripwire`, `retry`, `abort`, `length` or
`content-filter`. The list is internal and is removed before the caller
sees the chunk.
A task whose result the model never answered still gets its follow-up,
for example when the run hits its step limit right after the tool call.
```ts
const output = await agent.stream('Look up the invoice record.', { untilIdle: true, memory });
// The model calls a tool with `_background: { disposition: 'awaited' }`, then answers.
// Before: a second model request after the turn, and the answer streamed twice.
// After: one answer, and the stream closes once the task's completion event arrives.
```
The new scenario test runs on every engine. Known gaps, pinned in the
test:
- durable and evented still fail the whole run when an awaited task
fails, as on `main`;
- an awaited tool that suspends never ends its parked turn on durable
and evented, so that case is skipped there.
## Related issue(s)
Fixes #29679
## Type of change
- [x] Bug fix (non-breaking change that fixes an issue)
- [ ] New feature (non-breaking change that adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
- [ ] Documentation update
- [ ] Code refactoring
- [ ] Performance improvement
- [ ] Test update
## Checklist
- [x] I have linked the related issue(s) in the description above
- [ ] I have made corresponding changes to the documentation (if
applicable)
- [x] I have added tests that prove my fix is effective or that my
feature works
- [ ] I have addressed all Coderabbit comments on this PR
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
<!-- coderabbit-ai-generated:v1 -->
## ELI5
When the model has already used a background task’s result to answer,
`untilIdle` should not ask it to answer again. This change skips that
extra request only when the model received the result and gave a usable
reply.
## Summary
- Track completed and failed background-task results included in the
prompt before and after request processors run.
- Do not count results that a processor replaced or that were associated
with a cached response.
- Mark results as answered only when the model’s step produces text or
object output, has no error or client tool call, and ends with a finish
reason that does not indicate an incomplete answer.
- Keep follow-ups for results the model did not receive or did not
answer.
- Remove the internal task-ID field from caller-facing `untilIdle`
chunks.
- Add scenario tests for awaited and deferred tasks, failures, step
limits, request processing, and suspended tasks. Durable and evented
engines have known gaps for failed awaited tasks and suspended tasks.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
parent
6e682db7f5
commit
66c620c086
9 changed files with 719 additions and 21 deletions
Loading…
Add table
Add a link
Reference in a new issue