Fix PUT read-state monotonicity guard - #431
Merged
TheGreatAxios merged 5 commits intoAug 29, 2026
Merged
TheGreatAxios merged 5 commits into
TheGreatAxios merged 5 commits into
Conversation
Covers the in-memory store (a stale write landing after a newer one must not move the cursor back) and a real-Postgres drizzle test proving the same for createDrizzleChatStore's SQL upsert.
A slower "read up to X" write landing after a faster "read up to Y" regressed the stored cursor and flipped X..Y back to unread. Both store implementations now keep the later lastSeenCreatedAt on conflict — a conditional CASE WHEN in the Drizzle upsert, and an equivalent comparison in the in-memory store. Fixes CL-7131.
workbench_messages.created_at has millisecond precision and same-millisecond messages are expected, so the monotonicity guard must accept an equal-timestamp write, not just a strictly later one. Covers the in-memory store and the drizzle-backed store.
The guard used a strict > comparison, so a same-millisecond write to a later message in the same batch was dropped as a no-op and the route echoed back the older cursor. Only a strictly older timestamp is a no-op now; an equal timestamp lands the incoming write.
TheGreatAxios
force-pushed
the
cl-7131-put-read-state-has-no-monotonicity-guard-an-out-of-order
branch
from
August 29, 2026 04:46
99421ea to
46bb2f7
Compare
TheGreatAxios
commented
Aug 29, 2026
TheGreatAxios
left a comment
Contributor
Author
There was a problem hiding this comment.
critique · comment
Read-state GET and PATCH report through reportError and return { error, refId }.
- packages/chat/src/read-state-routes.ts:89-97 and :146-157 — the 500 JSON includes refId. Tests cover the GET and PATCH reportError calls.
Walking-skeleton red on this PR is main deleting DATABASE_URL after memory-mount tests (CL-7182 / #465), not this diff.
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.
Fixes CL-7131 — https://linear.app/abklabs/issue/CL-7131
Problem
putReadState(packages/chat/src/store.ts:303-323) unconditionallyset
lastSeenCreatedAt/lastSeenIdto whateverPUT /workbenches/:id/read-state(packages/chat/src/routes.ts:3498-3540)carried, via an unconditional
onConflictDoUpdate. A slower "read upto X" request landing after a faster "read up to Y" request regressed
the stored cursor, flipping messages X..Y back to unread for that
reader. The in-memory store (
packages/chat/src/store.ts:461) had thesame bug.
Change
putReadState'sonConflictDoUpdatenow sets bothcolumns via a
CASE WHEN excluded.last_seen_created_at >= workbench_read_state.last_seen_created_at THEN excluded... ELSE workbench_read_state... END, so a strictly older write is a no-opthat returns the still-current row, while an equal-timestamp write
still lands.
the existing row before overwriting.
>=(not strict>) matters becauseworkbench_messages.created_athas millisecond precision and same-millisecond messages are expected
— a legitimate forward move to a later same-millisecond message must
not be dropped as a no-op.
row
putReadStatereturns, so a no-op write now correctly reportsthe still-current cursor with a normal 200.
Tests
packages/chat/test/store.test.ts: in-memory cases for (a) a stalewrite landing after a newer one is a no-op, and (b) a same-millisecond
forward move to a different message still lands. Both verified red
against the old code, green after the fix.
packages/chat/test/read-state.drizzle.test.ts: new DB-gated suite(skipped without
DATABASE_URL, mirrors the existing*.drizzle.test.tspattern) proving the same three cases against thereal Postgres upsert. Ran locally against a local Postgres with both
red and green results for each case.