Skip to content

fix(core): keep part timestamps out of message ordering#17598

Merged
TylerBarnes merged 3 commits into
mainfrom
fix/prompt-cache-timestamp-drift
Jun 5, 2026
Merged

fix(core): keep part timestamps out of message ordering#17598
TylerBarnes merged 3 commits into
mainfrom
fix/prompt-cache-timestamp-drift

Conversation

@TylerBarnes

@TylerBarnes TylerBarnes commented Jun 4, 2026

Copy link
Copy Markdown
Member

This keeps message part timestamps from affecting transcript ordering. Part timestamps are still preserved as event metadata, but only the message-level createdAt advances the ordering watermark used for later messages/signals.

Before, a late part timestamp could push a later signal forward:

message.createdAt = 10:00:00
part.createdAt = 10:00:30
nextSignal.createdAt = 10:00:30.001

After, later messages/signals are ordered from the message-level timestamp:

message.createdAt = 10:00:00
part.createdAt = 10:00:30
nextSignal.createdAt = 10:00:03

Also preserves the existing message-level createdAt when merging response content into a memory-loaded assistant message, since OM uses that timestamp to decide whether a message has already been observed.

Verified with the focused message-list regression test, pnpm build:core, and pnpm --filter ./packages/core check.

ELI5

Imagine you're organizing messages by time. This PR fixes a bug where a late timestamp on a piece of a message (a "part") could reorder later messages — now only the main message timestamp controls ordering, while part timestamps are preserved as metadata and no longer affect transcript ordering.

Changes

Changeset Entry (.changeset/old-news-press.md)

  • Adds a patch release note for @mastra/core describing the fix: message-part timestamps no longer advance the transcript ordering watermark.

Message Merging (packages/core/src/agent/message-list/merge/MessageMerger.ts)

  • Preserve an existing message's createdAt when merging incoming content; removed logic that would update/advance createdAt from the incoming message.
  • Documentation updated to clarify that createdAt must remain unchanged so previously observed messages are not reprocessed.

Message List Ordering (packages/core/src/agent/message-list/message-list.ts)

  • MessageList.updateLastCreatedAt now advances the ordering watermark using only message-level createdAt.
  • Removed prior logic that inspected part-level timestamps when updating the watermark.
  • Comments updated to reflect that lastCreatedAt is the message-level ordering timestamp.

Tests (packages/core/src/agent/message-list/tests)

  • message-list-om-multistep.test.ts:
    • Updated OM multi-step test to ensure promoted (memory-loaded) assistant responses retain the original createdAt.
    • Added a regression test asserting that a tool-part with a later timestamp does not advance subsequent signal timestamps.
  • message-list-ordering.test.ts:
    • Adjusted assertions so signals are normalized using the response (message-level) timestamp rather than a later part timestamp.

Verification

  • Focused regression tests added/updated.
  • Build and check commands referenced: pnpm build:core (runs turbo build --filter ./packages/core) and pnpm --filter ./packages/core check (typecheck/check step used in CI).

@vercel

vercel Bot commented Jun 4, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
mastra-docs-1.x Ready Ready Preview, Comment Jun 5, 2026 12:57am
mastra-playground-ui Ready Ready Preview, Comment Jun 5, 2026 12:57am

Request Review

@changeset-bot

changeset-bot Bot commented Jun 4, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 50bbcda

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 23 packages
Name Type
@mastra/core Patch
mastracode Patch
@mastra/mcp-docs-server Patch
@internal/playground Patch
@mastra/client-js Patch
@mastra/opencode Patch
@mastra/longmemeval Patch
mastra Patch
@mastra/deployer-cloud Patch
@mastra/deployer-vercel Patch
@mastra/playground-ui Patch
@mastra/react Patch
@mastra/server Patch
@mastra/deployer Patch
create-mastra Patch
@mastra/express Patch
@mastra/fastify Patch
@mastra/hono Patch
@mastra/koa Patch
@mastra/nestjs Patch
@mastra/deployer-cloudflare Patch
@mastra/deployer-netlify Patch
@mastra/temporal Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@coderabbitai

coderabbitai Bot commented Jun 4, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: b74ae146-b99d-402b-914d-ca107d200f3f

📥 Commits

Reviewing files that changed from the base of the PR and between 96a9306 and 50bbcda.

📒 Files selected for processing (5)
  • .changeset/old-news-press.md
  • packages/core/src/agent/message-list/merge/MessageMerger.ts
  • packages/core/src/agent/message-list/message-list.ts
  • packages/core/src/agent/message-list/tests/message-list-om-multistep.test.ts
  • packages/core/src/agent/message-list/tests/message-list-ordering.test.ts
✅ Files skipped from review due to trivial changes (1)
  • .changeset/old-news-press.md
🚧 Files skipped from review as they are similar to previous changes (4)
  • packages/core/src/agent/message-list/tests/message-list-ordering.test.ts
  • packages/core/src/agent/message-list/tests/message-list-om-multistep.test.ts
  • packages/core/src/agent/message-list/merge/MessageMerger.ts
  • packages/core/src/agent/message-list/message-list.ts

Walkthrough

Fixes transcript ordering so only message-level createdAt advances the ordering watermark; merge logic no longer updates message createdAt, and tests plus a changeset validate the behavior.

Changes

Message watermark and merge timestamp semantics

Layer / File(s) Summary
Message merge and watermark timestamp preservation
packages/core/src/agent/message-list/merge/MessageMerger.ts, packages/core/src/agent/message-list/message-list.ts
MessageMerger.merge preserves existing latestMessage.createdAt (no assignment from incoming parts). MessageList.updateLastCreatedAt and generateCreatedAt use only message-level createdAt for the ordering watermark; comments updated to reflect ordering vs part metadata semantics.
Transcript ordering test coverage and release notes
packages/core/src/agent/message-list/tests/message-list-om-multistep.test.ts, packages/core/src/agent/message-list/tests/message-list-ordering.test.ts, .changeset/old-news-press.md
OM multi-step test uses a shared originalCreatedAt and asserts promoted responses retain it. New test verifies late-added tool-part timestamps do not advance signal timestamps. Ordering test assertions adjusted accordingly. Changeset records the fix.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

  • mastra-ai/mastra#17338: Both PRs modify message list and merge logic around createdAt and ordering-watermark behavior during observational-memory replay, specifically preserving historical response timestamps to prevent part-timestamps from causing incorrect reprocessing.

Suggested labels

tests: green ✅, complexity: low

Suggested reviewers

  • abhiaiyer91
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: preventing part timestamps from affecting message ordering, using imperative mood and proper formatting.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/prompt-cache-timestamp-drift

Comment @coderabbitai help to get the list of available commands and usage tips.

@dane-ai-mastra dane-ai-mastra Bot added the complexity: low Low-complexity PR label Jun 4, 2026
@dane-ai-mastra

dane-ai-mastra Bot commented Jun 4, 2026

Copy link
Copy Markdown
Contributor

PR triage

Linked issue check skipped for core contributor @TylerBarnes.


PR complexity score

Factor Value Score impact
Files changed 5 +10
Lines changed 115 +6
Author merged PRs 543 -20
Test files changed Yes -10
Final score -14

Applied label: complexity: low


Changed test gate

Changed tests failed against the base branch as expected.

Label: tests: green ✅

@dane-ai-mastra dane-ai-mastra Bot added the tests: green ✅ Changed tests failed against base as expected label Jun 4, 2026
@vercel vercel Bot temporarily deployed to Preview – mastra-docs-1.x June 5, 2026 00:31 Inactive
TylerBarnes and others added 3 commits June 4, 2026 17:52
Avoid advancing message ordering watermarks from recalled memory part timestamps, and keep merged assistant messages anchored to their original createdAt. This prevents saved memory messages from drifting across reloads and disrupting prompt cache stability.

Co-Authored-By: Mastra Code (openai/gpt-5.5) <noreply@mastra.ai>
Use message-level createdAt as the only source for the MessageList ordering watermark so part-level event timestamps cannot advance later transcript messages or signals. This preserves OM observation boundaries while keeping part timestamps as intra-message metadata.

Adds regression coverage for a memory-loaded assistant message promoted back to response with a late part timestamp, followed by an active signal.

Co-Authored-By: Mastra Code (openai/gpt-5.5) <noreply@mastra.ai>
Update the signal ordering regression test to match the message-level ordering watermark behavior. Signals now advance from the containing message timestamp, not from later part-level event timestamps.

Co-Authored-By: Mastra Code (openai/gpt-5.5) <noreply@mastra.ai>
@TylerBarnes TylerBarnes force-pushed the fix/prompt-cache-timestamp-drift branch from 96a9306 to 50bbcda Compare June 5, 2026 00:55
@TylerBarnes TylerBarnes merged commit 0c72f03 into main Jun 5, 2026
98 checks passed
@TylerBarnes TylerBarnes deleted the fix/prompt-cache-timestamp-drift branch June 5, 2026 02:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

complexity: low Low-complexity PR tests: green ✅ Changed tests failed against base as expected

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants