@@ -186,15 +186,19 @@ function settledOverrideFor(value: string | null): OrchestrationV2AppThread["set
186186 return value === "settled" || value === "active" ? value : null ;
187187}
188188
189- function dateTime ( value : string ) : DateTime . Utc {
190- return DateTime . makeUnsafe ( value ) ;
189+ // V1 rows can hold unparseable timestamps; fall back to another time rather than fail the import.
190+ function dateTimeOr ( value : string | null , fallback : DateTime . Utc ) : DateTime . Utc {
191+ return value === null ? fallback : Option . getOrElse ( DateTime . make ( value ) , ( ) => fallback ) ;
191192}
192193
193- function nullableDateTime ( value : string | null ) : DateTime . Utc | null {
194- return value === null ? null : dateTime ( value ) ;
194+ // A malformed marker stays set so archived or deleted threads keep that state.
195+ function nullableDateTime ( value : string | null , fallback : DateTime . Utc ) : DateTime . Utc | null {
196+ return value === null ? null : dateTimeOr ( value , fallback ) ;
195197}
196198
197- function importedThread ( row : LegacyThreadRow ) : OrchestrationV2AppThread {
199+ function importedThread ( row : LegacyThreadRow , importedAt : DateTime . Utc ) : OrchestrationV2AppThread {
200+ const createdAt = dateTimeOr ( row . created_at , dateTimeOr ( row . updated_at , importedAt ) ) ;
201+ const updatedAt = dateTimeOr ( row . updated_at , createdAt ) ;
198202 const threadId = ThreadId . make ( row . thread_id ) ;
199203 const modelSelection = modelSelectionFor ( row ) ;
200204 const branch = row . branch ?. trim ( ) || null ;
@@ -234,27 +238,30 @@ function importedThread(row: LegacyThreadRow): OrchestrationV2AppThread {
234238 rootThreadId : threadId ,
235239 } ,
236240 forkedFrom : null ,
237- createdAt : dateTime ( row . created_at ) ,
238- updatedAt : dateTime ( row . updated_at ) ,
239- archivedAt : nullableDateTime ( row . archived_at ) ,
241+ createdAt,
242+ updatedAt,
243+ archivedAt : nullableDateTime ( row . archived_at , updatedAt ) ,
240244 settledOverride : settledOverrideFor ( row . settled_override ) ,
241- settledAt : nullableDateTime ( row . settled_at ) ,
242- unsettledAt : nullableDateTime ( row . unsettled_at ) ,
243- snoozedUntil : nullableDateTime ( row . snoozed_until ) ,
244- snoozedAt : nullableDateTime ( row . snoozed_at ) ,
245- pinnedAt : nullableDateTime ( row . pinned_at ) ,
246- autoSettleDisabledAt : nullableDateTime ( row . auto_settle_disabled_at ) ,
245+ settledAt : nullableDateTime ( row . settled_at , updatedAt ) ,
246+ unsettledAt : nullableDateTime ( row . unsettled_at , updatedAt ) ,
247+ snoozedUntil : nullableDateTime ( row . snoozed_until , updatedAt ) ,
248+ snoozedAt : nullableDateTime ( row . snoozed_at , updatedAt ) ,
249+ pinnedAt : nullableDateTime ( row . pinned_at , updatedAt ) ,
250+ autoSettleDisabledAt : nullableDateTime ( row . auto_settle_disabled_at , updatedAt ) ,
247251 pinOrderKey : row . pin_order_key ?. trim ( ) || null ,
248252 lastVisitedAt : null ,
249- deletedAt : nullableDateTime ( row . deleted_at ) ,
253+ deletedAt : nullableDateTime ( row . deleted_at , updatedAt ) ,
250254 } ;
251255}
252256
253- function messageEvents ( row : LegacyMessageRow ) : ReadonlyArray < OrchestrationV2DomainEvent > {
257+ function messageEvents (
258+ row : LegacyMessageRow ,
259+ importedAt : DateTime . Utc ,
260+ ) : ReadonlyArray < OrchestrationV2DomainEvent > {
254261 const threadId = ThreadId . make ( row . thread_id ) ;
255262 const messageId = MessageId . make ( row . message_id ) ;
256- const createdAt = dateTime ( row . created_at ) ;
257- const updatedAt = dateTime ( row . updated_at ) ;
263+ const createdAt = dateTimeOr ( row . created_at , dateTimeOr ( row . updated_at , importedAt ) ) ;
264+ const updatedAt = dateTimeOr ( row . updated_at , createdAt ) ;
258265 const attachments = attachmentsFor ( row ) ;
259266 const context = contextFor ( row ) ;
260267 const message : OrchestrationV2ConversationMessage = {
@@ -433,7 +440,7 @@ const make = Effect.gen(function* () {
433440 } ) ;
434441
435442 const reconcileShellsBase = Effect . gen ( function * ( ) {
436- const now = DateTime . formatIso ( yield * DateTime . now ) ;
443+ const now = yield * DateTime . now ;
437444 const repairRows = yield * sql < LegacyRepairRow > `
438445 SELECT
439446 thread.thread_id,
@@ -482,7 +489,7 @@ const make = Effect.gen(function* () {
482489 const decoded = decodeStoredThread ( row . payload_json ) ;
483490 if ( Option . isNone ( decoded ) ) continue ;
484491 const current = decoded . value ;
485- const legacy = importedThread ( row ) ;
492+ const legacy = importedThread ( row , now ) ;
486493 const legacyPullRequests = legacy . pullRequests ?? [ ] ;
487494 const repaired : OrchestrationV2AppThread = {
488495 ...current ,
@@ -531,7 +538,7 @@ const make = Effect.gen(function* () {
531538 type : "thread.metadata-updated" ,
532539 threadId : repaired . id ,
533540 providerInstanceId : repaired . providerInstanceId ,
534- occurredAt : dateTime ( now ) ,
541+ occurredAt : now ,
535542 payload : repaired ,
536543 } ,
537544 ] ,
@@ -578,7 +585,7 @@ const make = Effect.gen(function* () {
578585 let importedThreadCount = repairedThreadCount ;
579586 let importedMessageCount = 0 ;
580587 for ( const row of rows ) {
581- const thread = importedThread ( row ) ;
588+ const thread = importedThread ( row , now ) ;
582589 const previews = yield * listShellMessages ( thread . id ) ;
583590 const events : Array < OrchestrationV2DomainEvent > = [
584591 {
@@ -589,7 +596,7 @@ const make = Effect.gen(function* () {
589596 occurredAt : thread . createdAt ,
590597 payload : thread ,
591598 } ,
592- ...previews . flatMap ( messageEvents ) ,
599+ ...previews . flatMap ( ( message ) => messageEvents ( message , now ) ) ,
593600 {
594601 id : EventId . make ( `${ IMPORT_EVENT_PREFIX } :thread:${ row . thread_id } :shell` ) ,
595602 type : "thread.metadata-updated" ,
@@ -632,7 +639,7 @@ const make = Effect.gen(function* () {
632639 VALUES (
633640 ${ thread . id } ,
634641 ${ row . updated_at } ,
635- ${ now } ,
642+ ${ DateTime . formatIso ( now ) } ,
636643 NULL,
637644 ${ previews . length } ,
638645 NULL
@@ -695,6 +702,7 @@ const make = Effect.gen(function* () {
695702 confirmedTranscriptThreadIds . add ( threadId ) ;
696703 return { importedThreadCount : 0 , importedMessageCount : 0 } ;
697704 }
705+ const importedAt = yield * DateTime . now ;
698706 const messages = yield * listMessages ( threadId ) ;
699707 // Hint the index to skip V1 events without scanning their payloads.
700708 const existingRows = yield * sql < { readonly event_id : string } > `
@@ -728,7 +736,9 @@ const make = Effect.gen(function* () {
728736 ` ,
729737 { discard : true } ,
730738 ) ;
731- yield * eventSink . write ( { events : batch . flatMap ( messageEvents ) } ) ;
739+ yield * eventSink . write ( {
740+ events : batch . flatMap ( ( message ) => messageEvents ( message , importedAt ) ) ,
741+ } ) ;
732742 yield * Effect . yieldNow ;
733743 }
734744 const now = DateTime . formatIso ( yield * DateTime . now ) ;
0 commit comments