fix(session): mint message ids and timestamps from one clock read - #46103
Open
iceteaSA wants to merge 1 commit into
Open
fix(session): mint message ids and timestamps from one clock read#46103iceteaSA wants to merge 1 commit into
iceteaSA wants to merge 1 commit into
Conversation
Freshly persisted messages now read Date.now() once, mint the ascending message ID from that value via Identifier.create("msg", "ascending", created), and persist the same value as time.created. The captured inversion was msg_04b012001003... with time_created 1787964760065 appearing after msg_04b012001001... with time_created 1787964760066 despite the later ID. Deriving time.created from the ID was tried and rejected because the ID's embedded timestamp can predate a legacy row's separately-read Date.now(), mis-ordering new messages against existing data.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Issue for this PR
Closes #46102
Type of change
What does this PR do?
A message's
idand itstime.createdcome from two different clocks.Identifier.ascending()readsDate.now()insideIdentifier.create; each producer then readsDate.now()again fortime.created. If the assembly path yields between the two reads, a message minted later can capture an earliertime.createdthan one minted before it.Messages sort
(time_created DESC, id DESC)(message-v2.ts:439). Whentime.createddisagrees with mint order, history sorts wrong — and sincetime.createdis the primary sort key, it sorts by the corrupted value.Read the clock once, mint the id from that same timestamp:
Identifier.createalready accepts an explicit timestamp, and its per-millisecond counter still applies on that path, so same-millisecond ids stay unique and strictly ascending. Applied at the message producers inprompt.ts,compaction.ts,plan.ts, and the debug/github CLI callers. The session-fork path is left alone — it clones an existing message'stimeon purpose — as are the ephemeral, never-persisted messages that never reach the ordering query.Two neighbouring issues this is not:
time.createdinstead of the id string. That makes ordering robust to id/time disagreement, but heretime.createditself is wrong, so preferring it doesn't help.Deriving
time.createdfrom the id (viaIdentifier.timestamp) is the obvious alternative and it's wrong: the id's embedded timestamp can predate a legacy row's separately-readDate.now(), sorting a new message ahead of existing data.How did you verify your code works?
Red-first, through the real producer — the test asserts a minted message's embedded id timestamp equals its stored
time.created, observed throughSessionPrompt.prompt, not a hand-built fixture. Reverse-applying only thesrc/hunks (test kept present) shows it fail against unmodifieddev:500 ids minted at one explicit timestamp were all unique and strictly ascending. Full suite
packages/opencode: 3396 pass / 0 fail.bun typecheckclean inopencodeandcore.Screenshots / recordings
Not a UI change.
Checklist