@@ -27,6 +27,7 @@ import {
2727 sortPinnedThreadsByOrderKey ,
2828 sortSettledThreads ,
2929} from "@t3tools/client-runtime/state/thread-sort" ;
30+ import type { EnvironmentConnectionPhase } from "@t3tools/client-runtime/connection" ;
3031import type { EnvironmentId , ProjectId } from "@t3tools/contracts" ;
3132
3233import type { ThreadListProvider } from "../../state/thread-list-environments" ;
@@ -69,6 +70,7 @@ export type ThreadListV2Status =
6970 | "waiting"
7071 | "failed"
7172 | "limited"
73+ | "disconnected"
7274 | "ready" ;
7375export type ThreadListV2SwipeAction = "archive" | "settle" | "unsettle" | "snooze" | "unsnooze" ;
7476
@@ -184,6 +186,16 @@ export function resolveThreadListV2WokeAt(
184186 : null ;
185187}
186188
189+ /**
190+ * Past a first connection attempt the environment is not answering, so its threads' last run
191+ * states are stale. Matches `threadRouteIsHydrating`.
192+ */
193+ export function isThreadListV2EnvironmentDisconnected (
194+ connectionState : EnvironmentConnectionPhase ,
195+ ) : boolean {
196+ return connectionState !== "connected" && connectionState !== "connecting" ;
197+ }
198+
187199export function resolveThreadListV2Status (
188200 thread : Pick <
189201 EnvironmentThreadShell ,
@@ -194,6 +206,16 @@ export function resolveThreadListV2Status(
194206 | "runtime"
195207 | "blockedOnThreadWait"
196208 > ,
209+ options ?: { readonly disconnected ?: boolean } ,
210+ ) : ThreadListV2Status {
211+ const status = resolveConnectedThreadListV2Status ( thread ) ;
212+ return options ?. disconnected === true && ( status === "working" || status === "waiting" )
213+ ? "disconnected"
214+ : status ;
215+ }
216+
217+ function resolveConnectedThreadListV2Status (
218+ thread : Parameters < typeof resolveThreadListV2Status > [ 0 ] ,
197219) : ThreadListV2Status {
198220 if ( thread . hasPendingApprovals || thread . pendingSubagentRequest === "approval" ) {
199221 return "approval" ;
@@ -285,6 +307,8 @@ export interface ThreadListV2Item {
285307 readonly snoozed : boolean ;
286308 readonly pinned : boolean ;
287309 readonly wokeAt : string | null ;
310+ /** Its environment stopped answering, so its last known run state is stale. */
311+ readonly disconnected : boolean ;
288312 readonly isLast : boolean ;
289313}
290314
@@ -386,6 +410,7 @@ export function threadListV2ListItemsAreEqual(
386410 previous . item . snoozed === item . item . snoozed &&
387411 previous . item . pinned === item . item . pinned &&
388412 previous . item . wokeAt === item . item . wokeAt &&
413+ previous . item . disconnected === item . item . disconnected &&
389414 previous . snoozeWakeLabelText === item . snoozeWakeLabelText &&
390415 previous . timeLabel === item . timeLabel &&
391416 previous . snoozePresetMinute === item . snoozePresetMinute &&
@@ -434,7 +459,7 @@ function resolveThreadListV2ItemTimeLabel(
434459 if ( showSnoozeWakeLabel ) return "" ;
435460 if (
436461 variant === "card" &&
437- ( resolveThreadListV2Status ( thread ) !== "ready" ||
462+ ( resolveThreadListV2Status ( thread , { disconnected : item . disconnected } ) !== "ready" ||
438463 item . wokeAt !== null ||
439464 threadHasUnseenCompletion ( thread ) )
440465 )
@@ -589,6 +614,7 @@ export function buildThreadListV2Items(input: {
589614 readonly matchedThreadKeys ?: ReadonlySet < string > ;
590615 readonly settlementEnvironmentIds ?: ReadonlySet < EnvironmentId > ;
591616 readonly snoozeEnvironmentIds ?: ReadonlySet < EnvironmentId > ;
617+ readonly disconnectedEnvironmentIds ?: ReadonlySet < string > ;
592618 readonly settledLimit ?: number ;
593619 readonly now : string ;
594620 readonly workingShelfEnabled ?: boolean ;
@@ -630,6 +656,8 @@ export function buildThreadListV2Items(input: {
630656 const snoozed : EnvironmentThreadShell [ ] = [ ] ;
631657 let nextSnoozeWakeAt : string | null = null ;
632658 const queuedThreadKeys = input . queuedThreadKeys ?. size ? input . queuedThreadKeys : undefined ;
659+ const isDisconnected = ( thread : EnvironmentThreadShell ) =>
660+ input . disconnectedEnvironmentIds ?. has ( thread . environmentId ) === true ;
633661 const listedThreads = input . threads . filter (
634662 ( thread ) =>
635663 thread . archivedAt === null &&
@@ -680,7 +708,12 @@ export function buildThreadListV2Items(input: {
680708 settled . push ( thread ) ;
681709 } else if ( thread . pinnedAt != null ) {
682710 pinned . push ( thread ) ;
683- } else if ( workingShelfEnabled && ( input . isWorking ?? isThreadWorking ) ( thread ) ) {
711+ } else if (
712+ workingShelfEnabled &&
713+ // A shelf the user collapsed must not hide a thread whose progress is unknown.
714+ ! isDisconnected ( thread ) &&
715+ ( input . isWorking ?? isThreadWorking ) ( thread )
716+ ) {
684717 working . push ( thread ) ;
685718 } else {
686719 active . push ( thread ) ;
@@ -712,6 +745,7 @@ export function buildThreadListV2Items(input: {
712745 snoozed : snoozedThreads . has ( thread ) ,
713746 pinned : pinnedThreads . has ( thread ) ,
714747 wokeAt : slim . has ( thread ) ? null : resolveThreadListV2WokeAt ( thread , { now } ) ,
748+ disconnected : isDisconnected ( thread ) ,
715749 isLast : index === results . length - 1 ,
716750 } ) ) ,
717751 hiddenSettledCount : 0 ,
@@ -785,6 +819,7 @@ export function buildThreadListV2Items(input: {
785819 snoozed : false ,
786820 pinned : true ,
787821 wokeAt : resolveThreadListV2WokeAt ( thread , { now } ) ,
822+ disconnected : isDisconnected ( thread ) ,
788823 isLast : false ,
789824 } ) ;
790825 }
@@ -795,6 +830,7 @@ export function buildThreadListV2Items(input: {
795830 snoozed : false ,
796831 pinned : false ,
797832 wokeAt : resolveThreadListV2WokeAt ( thread , { now } ) ,
833+ disconnected : isDisconnected ( thread ) ,
798834 isLast : false ,
799835 } ) ;
800836 }
@@ -806,6 +842,7 @@ export function buildThreadListV2Items(input: {
806842 snoozed : false ,
807843 pinned : false ,
808844 wokeAt : resolveThreadListV2WokeAt ( thread , { now } ) ,
845+ disconnected : isDisconnected ( thread ) ,
809846 isLast : false ,
810847 } ) ;
811848 }
@@ -817,6 +854,7 @@ export function buildThreadListV2Items(input: {
817854 snoozed : true ,
818855 pinned : false ,
819856 wokeAt : null ,
857+ disconnected : isDisconnected ( thread ) ,
820858 isLast : false ,
821859 } ) ;
822860 }
@@ -828,6 +866,7 @@ export function buildThreadListV2Items(input: {
828866 snoozed : false ,
829867 pinned : false ,
830868 wokeAt : null ,
869+ disconnected : isDisconnected ( thread ) ,
831870 isLast : false ,
832871 } ) ;
833872 }
0 commit comments