Skip to content

fix(session): make message ordering robust to ID wrap - #39806

Open
quanzhuo wants to merge 1 commit into
anomalyco:devfrom
quanzhuo:issues/39624
Open

fix(session): make message ordering robust to ID wrap#39806
quanzhuo wants to merge 1 commit into
anomalyco:devfrom
quanzhuo:issues/39624

Conversation

@quanzhuo

@quanzhuo quanzhuo commented Jul 31, 2026

Copy link
Copy Markdown

Issue for this PR

Fixes #39624

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Message IDs currently encode Date.now() * 0x1000 into 6 bytes. That timestamp field wraps on 2026-08-14T11:19:55.136Z, so newer message IDs can sort before older ones. Some session code uses raw ID ordering to decide the latest turn or when a response loop should stop, which can make sessions repeat responses indefinitely.

This PR widens the native timestamp encoding to 8 bytes and removes the fragile ID-order dependency from session turn handling. Session ordering now uses persisted time.created where chronological ordering is needed, and the run loop uses the assistant parentID relationship to identify whether it already replied to the latest user message.

It also keeps old 6-byte ID timestamp decoding for compatibility and adds regression tests for the wrap boundary and mixed/client-supplied message IDs.

How did you verify your code works?

From packages/opencode:

bun test test/session/id.test.ts test/session/message-v2.test.ts test/session/prompt.test.ts test/tool/truncation.test.ts --timeout 30000 --only-failures

Result: 102 pass, 14 skip, 0 fail

bun typecheck

Result: passed
Also ran git diff --check; it passed with only local LF/CRLF warnings on Windows.

Screenshots / recordings

N/A. This is session ordering and ID generation behavior, not a UI change.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

@github-actions github-actions Bot added the needs:compliance This means the issue will auto-close after 2 hours. label Jul 31, 2026
@github-actions

Copy link
Copy Markdown
Contributor

The following comment was made by an LLM, it may be inaccurate:

I found two related PRs that address similar session and message ordering issues:

  1. fix(session): order messages by time so the run loop can terminate #38798 - fix(session): order messages by time so the run loop can terminate

    • This PR also addresses message ordering for proper run loop termination, which is directly related to the current PR's goal of fixing turn ordering.
  2. fix(session): end the turn loop by reply parent, not message ID order #38387 - fix(session): end the turn loop by reply parent, not message ID order

    • This PR also changes the session run loop exit logic from message ID ordering to a different mechanism (reply parent), which overlaps with the current PR's approach of using durable parentID relationships.

Both PRs appear to be earlier attempts or related work addressing the same message ordering and session turn termination issues that PR #39806 is fixing.

@github-actions github-actions Bot removed the needs:compliance This means the issue will auto-close after 2 hours. label Jul 31, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Identifier time field wraps on 2026-08-14T11:19:55Z — all sessions with history enter infinite response loops

1 participant