fix(vfs): make copilot message ordering deterministic via WITH ORDINALITY#4597
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub. |
PR SummaryLow Risk Overview The Reviewed by Cursor Bugbot for commit e238490. Configure here. |
Greptile SummaryThis PR makes
Confidence Score: 5/5Safe to merge — the change is confined to one SQL expression, the PostgreSQL syntax is correct, and edge cases (NULL messages, empty arrays, missing The rewrite touches a single scalar subquery, applies a well-understood PostgreSQL feature ( No files require special attention. Important Files Changed
Sequence DiagramsequenceDiagram
participant DB as PostgreSQL
participant outer as jsonb_array_elements(messages) WITH ORDINALITY AS m(value, ord)
participant filter as WHERE m.value->>'role' IN ('user','assistant')
participant inner as jsonb_array_elements(contentBlocks) WITH ORDINALITY AS b(value, ord)
participant bfilter as WHERE b.value->>'type' = 'text'
participant agg as jsonb_agg(... ORDER BY ord)
DB->>outer: Expand messages array, assign ordinals (1,2,3...)
outer->>filter: Pass rows with original position preserved
filter->>inner: For each kept message, expand contentBlocks with ordinals
inner->>bfilter: Keep only 'text' blocks
bfilter->>agg: "jsonb_agg(block ORDER BY b.ord) -> ordered contentBlocks"
agg->>agg: "jsonb_agg(message ORDER BY m.ord) -> ordered messages array"
agg->>DB: Return deterministically ordered JSONB array
Reviews (1): Last reviewed commit: "fix(vfs): make copilot message ordering ..." | Re-trigger Greptile |
Summary
jsonb_aggwithout explicitORDER BYhas implementation-defined input ordering per PostgreSQL docs — though in practicejsonb_array_elements+jsonb_aggpreserves array order, the SQL standard doesn't guarantee itWITH ORDINALITY+ORDER BY ordon both the outer message aggregate and the innercontentBlockstext aggregate so message and block ordering is explicitly deterministic regardless of planner changesType of Change
Testing
Tested manually. Negligible CPU cost (≤5 rows per query). Message ordering is critical for chat transcripts; this aligns with PostgreSQL's documented recommendation to use
ORDER BYinside order-sensitive aggregates.Checklist