Commit 7c0d868
fix(core): restore state signals without thread scans (#18182)
This fixes long-thread state signal restore by loading tracked signal
messages directly by id instead of scanning thread history.
Before:
```ts
await memoryStore.listMessages({
threadId,
resourceId,
perPage: false,
});
```
After:
```ts
await memoryStore.listMessagesById({ messageIds: trackedSignalIds });
```
If tracked ids or id-based lookup are unavailable, we now fall back to
local in-memory state instead of doing an unbounded thread scan.
Verified with:
`pnpm --filter ./packages/core test -- src/processors/runner.test.ts
--bail 1 --reporter=dot`
`pnpm --filter ./packages/core check`
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## ELI5
Imagine you have a very long list of messages and need to find a few
specific ones. The old way was to look through every single message in
the list one by one until you found what you needed. The new way is much
smarter: it says "give me these specific messages by their ID" instead,
skipping all the others. This makes it much faster when threads get
really long.
## Overview
This PR improves the performance of state signal restoration in
long-running threads by eliminating expensive full-thread message scans.
Instead of iterating through all messages in a thread's history, the
system now uses a targeted ID-based lookup to fetch only the tracked
signal messages it needs.
## Changes
**Core Logic Update** (`packages/core/src/agent/state-signals.ts`)
- Modified `resolveStateSignalHistory` to collect tracked signal message
IDs from `tracking.activeCopies` and `tracking.lastSnapshotSignalId`
upfront
- Replaced `memoryStore.listMessages({ threadId, resourceId, perPage:
false })` (full thread scan) with `memoryStore.listMessagesById({
messageIds })` (targeted lookup)
- Removed the `resourceId` parameter from the function signature since
it's no longer needed for the more efficient ID-based lookup
- Graceful degradation: if `listMessagesById` is unavailable or tracking
yields no IDs, the function returns the local in-memory state without
attempting expensive fallbacks
**Caller Update** (`packages/core/src/processors/runner.ts`)
- Removed the now-unnecessary `resourceId` argument from the
`resolveStateSignalHistory` call
**Test Updates** (`packages/core/src/processors/runner.test.ts`)
- Updated mock to provide both `listMessages` and `listMessagesById`
methods
- Changed test expectations to verify that only `listMessagesById` is
called (with the specific message IDs) and `listMessages` is not called
## Impact
This optimization prevents performance degradation as thread histories
grow longer, especially in long-running conversation scenarios where
state signals need to be restored. The changes were verified against
existing test suites in the core package.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
---------
Co-authored-by: Mastra Code (openai/gpt-5.5) <noreply@mastra.ai>1 parent f79daa2 commit 7c0d868
4 files changed
Lines changed: 20 additions & 13 deletions
File tree
- .changeset
- packages/core/src
- agent
- processors
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
150 | 150 | | |
151 | 151 | | |
152 | 152 | | |
153 | | - | |
154 | 153 | | |
155 | 154 | | |
156 | 155 | | |
157 | 156 | | |
158 | 157 | | |
159 | 158 | | |
160 | | - | |
161 | 159 | | |
162 | 160 | | |
163 | 161 | | |
| |||
172 | 170 | | |
173 | 171 | | |
174 | 172 | | |
175 | | - | |
176 | | - | |
177 | | - | |
178 | | - | |
179 | | - | |
180 | | - | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
181 | 184 | | |
182 | 185 | | |
183 | 186 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3270 | 3270 | | |
3271 | 3271 | | |
3272 | 3272 | | |
| 3273 | + | |
3273 | 3274 | | |
3274 | 3275 | | |
3275 | 3276 | | |
3276 | | - | |
| 3277 | + | |
3277 | 3278 | | |
3278 | 3279 | | |
3279 | 3280 | | |
| |||
3295 | 3296 | | |
3296 | 3297 | | |
3297 | 3298 | | |
3298 | | - | |
3299 | | - | |
3300 | | - | |
| 3299 | + | |
| 3300 | + | |
3301 | 3301 | | |
3302 | 3302 | | |
3303 | 3303 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
374 | 374 | | |
375 | 375 | | |
376 | 376 | | |
377 | | - | |
378 | 377 | | |
379 | 378 | | |
380 | 379 | | |
| |||
0 commit comments