Commit a1bbb5e
feat(copilot): add seq ordinal to copilot_messages for order-preserving reads
copilot_messages had no column preserving message order: created_at (set
from each message's timestamp) ties at millisecond granularity in 58% of
chats, and some chats have out-of-order timestamps within their array. The
only other tiebreaker, id, is a random UUID — so ORDER BY created_at, id
renders same-timestamp user/assistant pairs swapped. This blocks the R+1
read cutover.
Add an integer seq = the message's 0-based index within the chat's JSONB
array (ground-truth order), backfilled inline in migration 0219 (no script
for self-hosters or us). Reads will use ORDER BY seq NULLS LAST, created_at,
id at cutover; reads still come from JSONB after this PR.
Design:
- seq is a tiebreaker, not the sole sort key (concurrent-append/NULL safety).
- Nullable now; defer NOT NULL so rolling-deploy old pods don't fail inserts.
- replace (update-messages snapshot) overwrites seq = array index
(re-densifies after a mid-conversation delete); append preserves existing
seq via COALESCE and assigns base+idx from a single MAX(seq) read (never
MAX+i in SQL — multi-row batches would collide).
- Dedupe message ids before insert (87 prod chats carry dup ids; a repeated
id in one INSERT...ON CONFLICT would otherwise throw).
- Backfill picks first-occurrence per (chat,id), gap-free via ROW_NUMBER;
validated on staging data (0-based, contiguous, 0 bad ranges).
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>1 parent 503432c commit a1bbb5e
7 files changed
Lines changed: 17649 additions & 20 deletions
File tree
- apps/sim/lib/copilot/chat
- packages
- db
- migrations
- meta
- testing/src/mocks
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
29 | 35 | | |
30 | 36 | | |
31 | 37 | | |
| |||
43 | 49 | | |
44 | 50 | | |
45 | 51 | | |
46 | | - | |
| 52 | + | |
47 | 53 | | |
48 | 54 | | |
49 | 55 | | |
| |||
54 | 60 | | |
55 | 61 | | |
56 | 62 | | |
57 | | - | |
58 | | - | |
| 63 | + | |
| 64 | + | |
59 | 65 | | |
60 | 66 | | |
61 | 67 | | |
62 | 68 | | |
63 | 69 | | |
64 | 70 | | |
65 | 71 | | |
66 | | - | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
67 | 80 | | |
68 | 81 | | |
69 | | - | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
70 | 86 | | |
71 | | - | |
72 | | - | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
73 | 90 | | |
74 | 91 | | |
75 | 92 | | |
| |||
78 | 95 | | |
79 | 96 | | |
80 | 97 | | |
81 | | - | |
| 98 | + | |
82 | 99 | | |
83 | 100 | | |
84 | 101 | | |
85 | 102 | | |
86 | 103 | | |
87 | 104 | | |
88 | | - | |
| 105 | + | |
89 | 106 | | |
90 | 107 | | |
91 | 108 | | |
| |||
96 | 113 | | |
97 | 114 | | |
98 | 115 | | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
99 | 125 | | |
100 | 126 | | |
101 | 127 | | |
| |||
120 | 146 | | |
121 | 147 | | |
122 | 148 | | |
123 | | - | |
| 149 | + | |
124 | 150 | | |
125 | | - | |
126 | | - | |
127 | | - | |
128 | | - | |
| 151 | + | |
129 | 152 | | |
130 | 153 | | |
131 | 154 | | |
132 | 155 | | |
133 | 156 | | |
134 | 157 | | |
135 | 158 | | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
136 | 180 | | |
137 | 181 | | |
138 | 182 | | |
139 | 183 | | |
140 | 184 | | |
141 | | - | |
| 185 | + | |
142 | 186 | | |
143 | 187 | | |
144 | 188 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
10 | 27 | | |
11 | 28 | | |
12 | 29 | | |
| 30 | + | |
13 | 31 | | |
14 | 32 | | |
15 | 33 | | |
| |||
18 | 36 | | |
19 | 37 | | |
20 | 38 | | |
| 39 | + | |
21 | 40 | | |
22 | 41 | | |
23 | 42 | | |
| |||
37 | 56 | | |
38 | 57 | | |
39 | 58 | | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
40 | 70 | | |
41 | 71 | | |
42 | | - | |
| 72 | + | |
43 | 73 | | |
44 | 74 | | |
45 | 75 | | |
46 | 76 | | |
47 | 77 | | |
48 | 78 | | |
49 | 79 | | |
| 80 | + | |
50 | 81 | | |
51 | 82 | | |
52 | 83 | | |
| |||
69 | 100 | | |
70 | 101 | | |
71 | 102 | | |
72 | | - | |
| 103 | + | |
| 104 | + | |
73 | 105 | | |
74 | 106 | | |
75 | 107 | | |
| |||
82 | 114 | | |
83 | 115 | | |
84 | 116 | | |
85 | | - | |
86 | | - | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
87 | 121 | | |
88 | 122 | | |
89 | 123 | | |
90 | | - | |
| 124 | + | |
91 | 125 | | |
92 | 126 | | |
93 | 127 | | |
94 | 128 | | |
95 | 129 | | |
96 | 130 | | |
97 | 131 | | |
| 132 | + | |
98 | 133 | | |
99 | 134 | | |
100 | 135 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
0 commit comments