@@ -114,6 +114,31 @@ describe("sortThreads", () => {
114114 ] ) ;
115115 } ) ;
116116
117+ it ( "falls back to createdAt when updatedAt is invalid" , ( ) => {
118+ const sorted = sortThreads (
119+ [
120+ makeThread ( {
121+ id : ThreadId . make ( "thread-1" ) ,
122+ createdAt : "2026-03-09T10:00:00.000Z" ,
123+ updatedAt : "invalid-date" as never ,
124+ messages : [ ] ,
125+ } ) ,
126+ makeThread ( {
127+ id : ThreadId . make ( "thread-2" ) ,
128+ createdAt : "2026-03-09T09:00:00.000Z" ,
129+ updatedAt : "2026-03-09T09:30:00.000Z" ,
130+ messages : [ ] ,
131+ } ) ,
132+ ] ,
133+ "updated_at" ,
134+ ) ;
135+
136+ expect ( sorted . map ( ( thread ) => thread . id ) ) . toEqual ( [
137+ ThreadId . make ( "thread-1" ) ,
138+ ThreadId . make ( "thread-2" ) ,
139+ ] ) ;
140+ } ) ;
141+
117142 it ( "falls back to id ordering when threads have no sortable timestamps" , ( ) => {
118143 const sorted = sortThreads (
119144 [
@@ -162,6 +187,29 @@ describe("sortThreads", () => {
162187 ] ) ;
163188 } ) ;
164189
190+ it ( "uses updatedAt as a fallback for created_at sorting when createdAt is invalid" , ( ) => {
191+ const sorted = sortThreads (
192+ [
193+ makeThread ( {
194+ id : ThreadId . make ( "thread-1" ) ,
195+ createdAt : "invalid-date" as never ,
196+ updatedAt : "2026-03-09T10:05:00.000Z" ,
197+ } ) ,
198+ makeThread ( {
199+ id : ThreadId . make ( "thread-2" ) ,
200+ createdAt : "2026-03-09T10:00:00.000Z" ,
201+ updatedAt : "2026-03-09T10:10:00.000Z" ,
202+ } ) ,
203+ ] ,
204+ "created_at" ,
205+ ) ;
206+
207+ expect ( sorted . map ( ( thread ) => thread . id ) ) . toEqual ( [
208+ ThreadId . make ( "thread-1" ) ,
209+ ThreadId . make ( "thread-2" ) ,
210+ ] ) ;
211+ } ) ;
212+
165213 it ( "returns the latest active thread for a project" , ( ) => {
166214 const latestThread = getLatestThreadForProject (
167215 [
0 commit comments