@@ -20,9 +20,12 @@ import {
2020 resolveThreadRowClassName ,
2121 resolveSidebarV2Status ,
2222 resolveThreadStatusPill ,
23+ resolveWorkingStartedAt ,
24+ formatWorkingDurationLabel ,
2325 shouldNavigateAfterProjectRemoval ,
2426 shouldClearThreadSelectionOnMouseDown ,
2527 sortLogicalProjectsForSidebar ,
28+ sortSettledThreadsForSidebarV2 ,
2629 sortThreadsForSidebarV2 ,
2730 sortProjectsForSidebar ,
2831 sortScopedProjectsForSidebar ,
@@ -209,8 +212,10 @@ function makeLatestTurn(overrides?: {
209212 state : "completed" ,
210213 assistantMessageId : null ,
211214 requestedAt : "2026-03-09T10:00:00.000Z" ,
212- startedAt : overrides ?. startedAt ?? "2026-03-09T10:00:00.000Z" ,
213- completedAt : overrides ?. completedAt ?? "2026-03-09T10:05:00.000Z" ,
215+ startedAt :
216+ overrides ?. startedAt !== undefined ? overrides . startedAt : "2026-03-09T10:00:00.000Z" ,
217+ completedAt :
218+ overrides ?. completedAt !== undefined ? overrides . completedAt : "2026-03-09T10:05:00.000Z" ,
214219 } ;
215220}
216221
@@ -783,6 +788,141 @@ describe("sortThreadsForSidebarV2", () => {
783788 } ) ;
784789} ) ;
785790
791+ describe ( "sortSettledThreadsForSidebarV2" , ( ) => {
792+ const settled = ( input : {
793+ id : string ;
794+ settledAt ?: string | null ;
795+ latestUserMessageAt ?: string | null ;
796+ latestTurn ?: OrchestrationLatestTurn | null ;
797+ updatedAt ?: string ;
798+ } ) => ( {
799+ id : input . id ,
800+ settledAt : input . settledAt ?? null ,
801+ latestUserMessageAt : input . latestUserMessageAt ?? null ,
802+ latestTurn : input . latestTurn ?? null ,
803+ updatedAt : input . updatedAt ?? "2026-03-09T09:00:00.000Z" ,
804+ } ) ;
805+
806+ it ( "orders by settle time, most recently settled first" , ( ) => {
807+ const sorted = sortSettledThreadsForSidebarV2 ( [
808+ settled ( {
809+ id : "settled-first" ,
810+ settledAt : "2026-03-09T10:00:00.000Z" ,
811+ // Created/active later than the other thread: settle time must win.
812+ latestUserMessageAt : "2026-03-09T09:59:00.000Z" ,
813+ } ) ,
814+ settled ( {
815+ id : "settled-last" ,
816+ settledAt : "2026-03-09T12:00:00.000Z" ,
817+ latestUserMessageAt : "2026-03-09T08:00:00.000Z" ,
818+ } ) ,
819+ ] ) ;
820+
821+ expect ( sorted . map ( ( thread ) => thread . id ) ) . toEqual ( [ "settled-last" , "settled-first" ] ) ;
822+ } ) ;
823+
824+ it ( "falls back to last activity for auto-settled threads without a settledAt stamp" , ( ) => {
825+ const sorted = sortSettledThreadsForSidebarV2 ( [
826+ settled ( { id : "auto-old" , latestUserMessageAt : "2026-03-09T08:00:00.000Z" } ) ,
827+ settled ( { id : "explicit" , settledAt : "2026-03-09T10:00:00.000Z" } ) ,
828+ settled ( { id : "auto-recent" , latestUserMessageAt : "2026-03-09T11:00:00.000Z" } ) ,
829+ ] ) ;
830+
831+ expect ( sorted . map ( ( thread ) => thread . id ) ) . toEqual ( [ "auto-recent" , "explicit" , "auto-old" ] ) ;
832+ } ) ;
833+
834+ it ( "counts a turn completion as activity for auto-settled threads" , ( ) => {
835+ // The message came in before the other thread's, but its turn finished
836+ // after: completion time is the real "work ended" moment.
837+ const sorted = sortSettledThreadsForSidebarV2 ( [
838+ settled ( { id : "message-only" , latestUserMessageAt : "2026-03-09T10:04:00.000Z" } ) ,
839+ settled ( {
840+ id : "completed-later" ,
841+ latestUserMessageAt : "2026-03-09T10:00:00.000Z" ,
842+ latestTurn : makeLatestTurn ( { completedAt : "2026-03-09T10:30:00.000Z" } ) ,
843+ } ) ,
844+ ] ) ;
845+
846+ expect ( sorted . map ( ( thread ) => thread . id ) ) . toEqual ( [ "completed-later" , "message-only" ] ) ;
847+ } ) ;
848+
849+ it ( "breaks timestamp ties by id so the order is stable" , ( ) => {
850+ const sorted = sortSettledThreadsForSidebarV2 ( [
851+ settled ( { id : "b" , settledAt : "2026-03-09T10:00:00.000Z" } ) ,
852+ settled ( { id : "a" , settledAt : "2026-03-09T10:00:00.000Z" } ) ,
853+ ] ) ;
854+
855+ expect ( sorted . map ( ( thread ) => thread . id ) ) . toEqual ( [ "a" , "b" ] ) ;
856+ } ) ;
857+ } ) ;
858+
859+ describe ( "resolveWorkingStartedAt" , ( ) => {
860+ const session = {
861+ threadId : ThreadId . make ( "thread-1" ) ,
862+ status : "running" as const ,
863+ providerName : "Codex" ,
864+ providerInstanceId : ProviderInstanceId . make ( "codex" ) ,
865+ runtimeMode : DEFAULT_RUNTIME_MODE ,
866+ activeTurnId : "turn-1" as never ,
867+ lastError : null ,
868+ updatedAt : "2026-03-09T10:02:00.000Z" ,
869+ } ;
870+
871+ it ( "uses the running turn's start time" , ( ) => {
872+ expect (
873+ resolveWorkingStartedAt ( {
874+ latestTurn : makeLatestTurn ( { completedAt : null } ) ,
875+ session,
876+ } ) ,
877+ ) . toBe ( "2026-03-09T10:00:00.000Z" ) ;
878+ } ) ;
879+
880+ it ( "uses the request time while a turn awaits adoption" , ( ) => {
881+ expect (
882+ resolveWorkingStartedAt ( {
883+ latestTurn : makeLatestTurn ( { startedAt : null , completedAt : null } ) ,
884+ session,
885+ } ) ,
886+ ) . toBe ( "2026-03-09T10:00:00.000Z" ) ;
887+ } ) ;
888+
889+ it ( "falls back to the session transition when the latest turn already completed" , ( ) => {
890+ expect (
891+ resolveWorkingStartedAt ( {
892+ latestTurn : makeLatestTurn ( ) ,
893+ session,
894+ } ) ,
895+ ) . toBe ( "2026-03-09T10:02:00.000Z" ) ;
896+ } ) ;
897+
898+ it ( "skips a malformed startedAt instead of returning it" , ( ) => {
899+ expect (
900+ resolveWorkingStartedAt ( {
901+ latestTurn : makeLatestTurn ( { startedAt : "not-a-date" , completedAt : null } ) ,
902+ session,
903+ } ) ,
904+ ) . toBe ( "2026-03-09T10:00:00.000Z" ) ;
905+ } ) ;
906+
907+ it ( "returns null with neither a running turn nor a session" , ( ) => {
908+ expect ( resolveWorkingStartedAt ( { latestTurn : null , session : null } ) ) . toBeNull ( ) ;
909+ } ) ;
910+ } ) ;
911+
912+ describe ( "formatWorkingDurationLabel" , ( ) => {
913+ it ( "formats seconds, minutes, and hours" , ( ) => {
914+ expect ( formatWorkingDurationLabel ( 0 ) ) . toBe ( "0s" ) ;
915+ expect ( formatWorkingDurationLabel ( 42_000 ) ) . toBe ( "42s" ) ;
916+ expect ( formatWorkingDurationLabel ( 5 * 60_000 ) ) . toBe ( "5m" ) ;
917+ expect ( formatWorkingDurationLabel ( 90 * 60_000 ) ) . toBe ( "1h 30m" ) ;
918+ } ) ;
919+
920+ it ( "clamps negative and non-finite elapsed values to zero" , ( ) => {
921+ expect ( formatWorkingDurationLabel ( - 5_000 ) ) . toBe ( "0s" ) ;
922+ expect ( formatWorkingDurationLabel ( Number . NaN ) ) . toBe ( "0s" ) ;
923+ } ) ;
924+ } ) ;
925+
786926describe ( "resolveThreadStatusPill" , ( ) => {
787927 const baseThread = {
788928 hasActionableProposedPlan : false ,
0 commit comments