@@ -495,15 +495,15 @@ export function useThreadActions({
495495 threadActivityRef . current = next ;
496496 saveThreadActivity ( next ) ;
497497 }
498+ const getEffectiveTimestamp = ( thread : Record < string , unknown > ) => {
499+ const threadId = String ( thread ?. id ?? "" ) ;
500+ const baseTimestamp = getThreadTimestamp ( thread ) ;
501+ const activityTimestamp = nextActivityByThread [ threadId ] ?? 0 ;
502+ return Math . max ( baseTimestamp , activityTimestamp ) ;
503+ } ;
498504 if ( requestedSortKey === "updated_at" ) {
499505 uniqueThreads . sort ( ( a , b ) => {
500- const aId = String ( a ?. id ?? "" ) ;
501- const bId = String ( b ?. id ?? "" ) ;
502- const aCreated = getThreadTimestamp ( a ) ;
503- const bCreated = getThreadTimestamp ( b ) ;
504- const aActivity = Math . max ( nextActivityByThread [ aId ] ?? 0 , aCreated ) ;
505- const bActivity = Math . max ( nextActivityByThread [ bId ] ?? 0 , bCreated ) ;
506- return bActivity - aActivity ;
506+ return getEffectiveTimestamp ( b ) - getEffectiveTimestamp ( a ) ;
507507 } ) ;
508508 } else {
509509 uniqueThreads . sort ( ( a , b ) => {
@@ -535,7 +535,7 @@ export function useThreadActions({
535535 return {
536536 id,
537537 name,
538- updatedAt : getThreadTimestamp ( thread ) ,
538+ updatedAt : getEffectiveTimestamp ( thread ) ,
539539 } ;
540540 } )
541541 . filter ( ( entry ) => entry . id ) ;
@@ -558,11 +558,12 @@ export function useThreadActions({
558558 if ( ! threadId || ! message ) {
559559 return ;
560560 }
561+ const timestamp = getEffectiveTimestamp ( thread ) ;
561562 dispatch ( {
562563 type : "setLastAgentMessage" ,
563564 threadId,
564565 text : message ,
565- timestamp : getThreadTimestamp ( thread ) ,
566+ timestamp,
566567 } ) ;
567568 } ) ;
568569 } catch ( error ) {
@@ -615,6 +616,9 @@ export function useThreadActions({
615616 payload : { workspaceId : workspace . id , cursor : nextCursor } ,
616617 } ) ;
617618 try {
619+ const activityByThread = threadActivityRef . current [ workspace . id ] ?? { } ;
620+ const nextActivityByThread = { ...activityByThread } ;
621+ let didChangeActivity = false ;
618622 const matchingThreads : Record < string , unknown > [ ] = [ ] ;
619623 const maxPagesWithoutMatch = THREAD_LIST_MAX_PAGES_OLDER ;
620624 let pagesFetched = 0 ;
@@ -663,6 +667,11 @@ export function useThreadActions({
663667 if ( ! id || existingIds . has ( id ) ) {
664668 return ;
665669 }
670+ const timestamp = getThreadTimestamp ( thread ) ;
671+ if ( timestamp > ( nextActivityByThread [ id ] ?? 0 ) ) {
672+ nextActivityByThread [ id ] = timestamp ;
673+ didChangeActivity = true ;
674+ }
666675 const sourceParentId = getParentThreadIdFromSource ( thread . source ) ;
667676 const directParentId = asString ( thread . parentId ?? thread . parent_id ?? "" ) . trim ( ) || null ;
668677 const resolvedParentId = sourceParentId ?? directParentId ;
@@ -681,10 +690,23 @@ export function useThreadActions({
681690 ? `${ nameSeed . slice ( 0 , 38 ) } …`
682691 : nameSeed
683692 : fallbackName ;
684- additions . push ( { id, name, updatedAt : getThreadTimestamp ( thread ) } ) ;
693+ additions . push ( {
694+ id,
695+ name,
696+ updatedAt : Math . max ( timestamp , nextActivityByThread [ id ] ?? 0 ) ,
697+ } ) ;
685698 existingIds . add ( id ) ;
686699 } ) ;
687700
701+ if ( didChangeActivity ) {
702+ const next = {
703+ ...threadActivityRef . current ,
704+ [ workspace . id ] : nextActivityByThread ,
705+ } ;
706+ threadActivityRef . current = next ;
707+ saveThreadActivity ( next ) ;
708+ }
709+
688710 if ( additions . length > 0 ) {
689711 dispatch ( {
690712 type : "setThreads" ,
@@ -706,11 +728,15 @@ export function useThreadActions({
706728 if ( ! threadId || ! message ) {
707729 return ;
708730 }
731+ const timestamp = Math . max (
732+ getThreadTimestamp ( thread ) ,
733+ nextActivityByThread [ threadId ] ?? 0 ,
734+ ) ;
709735 dispatch ( {
710736 type : "setLastAgentMessage" ,
711737 threadId,
712738 text : message ,
713- timestamp : getThreadTimestamp ( thread ) ,
739+ timestamp,
714740 } ) ;
715741 } ) ;
716742 } catch ( error ) {
0 commit comments