Skip to content

fix(vfs): make copilot message ordering deterministic via WITH ORDINALITY#4597

Merged
waleedlatif1 merged 1 commit into
stagingfrom
waleedlatif1/copilot-vfs-message-ordering
May 14, 2026
Merged

fix(vfs): make copilot message ordering deterministic via WITH ORDINALITY#4597
waleedlatif1 merged 1 commit into
stagingfrom
waleedlatif1/copilot-vfs-message-ordering

Conversation

@waleedlatif1
Copy link
Copy Markdown
Collaborator

Summary

  • Follow-up to improvement(db): reduce connection saturation and egress hotspots #4594 addressing Greptile inline feedback
  • jsonb_agg without explicit ORDER BY has implementation-defined input ordering per PostgreSQL docs — though in practice jsonb_array_elements + jsonb_agg preserves array order, the SQL standard doesn't guarantee it
  • Switched to WITH ORDINALITY + ORDER BY ord on both the outer message aggregate and the inner contentBlocks text aggregate so message and block ordering is explicitly deterministic regardless of planner changes

Type of Change

  • Bug fix

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 BY inside order-sensitive aggregates.

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

@vercel
Copy link
Copy Markdown

vercel Bot commented May 14, 2026

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

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
docs Skipped Skipped May 14, 2026 7:00am

Request Review

@cursor
Copy link
Copy Markdown

cursor Bot commented May 14, 2026

PR Summary

Low Risk
Low risk, localized SQL query change that only affects how copilotChats.messages and nested contentBlocks are aggregated; main concern is any subtle ordering/regression in task transcript output.

Overview
Ensures mothership task chat transcripts in WorkspaceVFS.materializeTasks are aggregated in a deterministic order.

The jsonb_agg queries for both outer messages and inner text contentBlocks now use WITH ORDINALITY plus ORDER BY ord, and update JSON field access accordingly (m.value, b.value), eliminating reliance on planner-dependent input ordering.

Reviewed by Cursor Bugbot for commit e238490. Configure here.

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented May 14, 2026

Greptile Summary

This PR makes jsonb_agg ordering deterministic in the materializeTasks VFS query by replacing implicit jsonb_array_elements(...) AS m with WITH ORDINALITY AS m(value, ord) on both the outer message loop and the inner contentBlocks loop, then adding ORDER BY m.ord / ORDER BY b.ord inside the respective aggregates.

  • Outer messages aggregate: jsonb_array_elements(messages) WITH ORDINALITY AS m(value, ord) assigns each message its original 1-based position; jsonb_agg(... ORDER BY m.ord) then rebuilds the array in that order, unaffected by which role rows the WHERE filter removes.
  • Inner contentBlocks aggregate: the same pattern is applied to the nested jsonb_array_elements(contentBlocks) call so block text ordering is also explicit.
  • All column references are updated from the bare m->> / b-> form to the qualified m.value->> / b.value-> form required when WITH ORDINALITY introduces the named column aliases.

Confidence Score: 5/5

Safe to merge — the change is confined to one SQL expression, the PostgreSQL syntax is correct, and edge cases (NULL messages, empty arrays, missing contentBlocks key) are all handled by the existing COALESCE and CASE WHEN jsonb_typeof … = 'array' guards that were not touched.

The rewrite touches a single scalar subquery, applies a well-understood PostgreSQL feature (WITH ORDINALITY) correctly to both nesting levels, and does not alter any filtering logic or return shape. All pre-existing null guards remain intact, and the ordinality is captured before the WHERE filter so the original array position is faithfully reflected in the output order even when some messages are excluded.

No files require special attention.

Important Files Changed

Filename Overview
apps/sim/lib/copilot/vfs/workspace-vfs.ts Refactors a raw SQL aggregate in materializeTasks to use WITH ORDINALITY on both jsonb_array_elements calls, then passes the ordinality column to ORDER BY inside jsonb_agg, making message and content-block ordering deterministic.

Sequence Diagram

sequenceDiagram
    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
Loading

Reviews (1): Last reviewed commit: "fix(vfs): make copilot message ordering ..." | Re-trigger Greptile

@waleedlatif1 waleedlatif1 merged commit c3ac54e into staging May 14, 2026
10 checks passed
@waleedlatif1 waleedlatif1 deleted the waleedlatif1/copilot-vfs-message-ordering branch May 14, 2026 07:03
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.

1 participant