77 type EnvironmentThreadShell ,
88} from "@t3tools/client-runtime/state/shell" ;
99import type { AtomCommandResult } from "@t3tools/client-runtime/state/runtime" ;
10+ import { threadFamilyRead , type ThreadFamilyRead } from "@t3tools/client-runtime/state/threads" ;
1011import { deriveThreadTitleSeed } from "@t3tools/client-runtime/operations" ;
1112import {
1213 AuthOrchestrationOperateScope ,
@@ -66,7 +67,7 @@ import {
6667 type ThreadOutboxCommandStage ,
6768 type ThreadOutboxFailureAction ,
6869} from "./thread-outbox-model" ;
69- import { environmentThreadShells , threadEnvironment } from "./threads" ;
70+ import { environmentThreadShells , threadEnvironment , threadFamilies } from "./threads" ;
7071import {
7172 appendComposerDraftAttachments ,
7273 composerDraftsAtom ,
@@ -614,6 +615,16 @@ export function useThreadOutboxDrain(): void {
614615 const retryNotBeforeRef = useRef ( new Map < MessageId , number > ( ) ) ;
615616 const retryTimersRef = useRef ( new Map < MessageId , ReturnType < typeof setTimeout > > ( ) ) ;
616617 const acknowledgedExistingThreadMessageIdsRef = useRef ( new Set < MessageId > ( ) ) ;
618+ // Targets the shell may have left out hold their thread family until they leave the queue, so
619+ // the row stays readable through dispatch.
620+ const familyMountsRef = useRef ( new Map < MessageId , ( ) => void > ( ) ) ;
621+ useEffect (
622+ ( ) => ( ) => {
623+ for ( const release of familyMountsRef . current . values ( ) ) release ( ) ;
624+ familyMountsRef . current . clear ( ) ;
625+ } ,
626+ [ ] ,
627+ ) ;
617628 const blockedRecoverySubscriptionsRef = useRef (
618629 new Map <
619630 MessageId ,
@@ -1102,6 +1113,12 @@ export function useThreadOutboxDrain(): void {
11021113 acknowledgedExistingThreadMessageIdsRef . current . delete ( messageId ) ;
11031114 }
11041115 }
1116+ for ( const [ messageId , release ] of familyMountsRef . current ) {
1117+ if ( ! queuedMessageIds . has ( messageId ) ) {
1118+ release ( ) ;
1119+ familyMountsRef . current . delete ( messageId ) ;
1120+ }
1121+ }
11051122
11061123 for ( const [ threadKey , queuedMessages ] of Object . entries ( queuedMessagesByThreadKey ) ) {
11071124 const nextQueuedMessage = queuedMessages [ 0 ] ;
@@ -1153,7 +1170,7 @@ export function useThreadOutboxDrain(): void {
11531170 continue ;
11541171 }
11551172
1156- const thread = findThread ( threads , nextQueuedMessage ) ;
1173+ let thread = findThread ( threads , nextQueuedMessage ) ;
11571174 if ( thread && scopedThreadKey ( thread . environmentId , thread . id ) !== threadKey ) {
11581175 continue ;
11591176 }
@@ -1163,17 +1180,66 @@ export function useThreadOutboxDrain(): void {
11631180 ( candidate ) => candidate . environmentId === nextQueuedMessage . environmentId ,
11641181 ) ;
11651182 const shellStatus = shellStatuses . get ( nextQueuedMessage . environmentId ) ?? "empty" ;
1183+ const serverConfig = serverConfigs . get ( nextQueuedMessage . environmentId ) ;
1184+ let familyRead : ThreadFamilyRead | undefined ;
1185+ if (
1186+ thread === undefined &&
1187+ creation === undefined &&
1188+ serverConfig ?. shellThreadFamilies === true
1189+ ) {
1190+ // The family keeps the row readable through dispatch once it loads; the current shell
1191+ // generation's read decides whether the thread is gone.
1192+ if ( ! familyMountsRef . current . has ( nextQueuedMessage . messageId ) ) {
1193+ familyMountsRef . current . set (
1194+ nextQueuedMessage . messageId ,
1195+ threadFamilies . mount (
1196+ appAtomRegistry ,
1197+ nextQueuedMessage . environmentId ,
1198+ nextQueuedMessage . threadId ,
1199+ ) ,
1200+ ) ;
1201+ }
1202+ familyRead = threadFamilyRead (
1203+ appAtomRegistry . get (
1204+ threadFamilies . familyAtom ( nextQueuedMessage . environmentId , nextQueuedMessage . threadId ) ,
1205+ ) ,
1206+ ) ;
1207+ if ( familyRead === "settled" ) {
1208+ // The rows from this render can lag the family that just settled; decide from the
1209+ // merged rows as they are now.
1210+ thread = findThread (
1211+ appAtomRegistry . get ( environmentThreadShells . threadShellsAtom ) ,
1212+ nextQueuedMessage ,
1213+ ) ;
1214+ } else if ( familyRead === "failed" ) {
1215+ threadFamilies . refresh (
1216+ appAtomRegistry ,
1217+ nextQueuedMessage . environmentId ,
1218+ nextQueuedMessage . threadId ,
1219+ ) ;
1220+ }
1221+ }
11661222 const deliveryAction = resolveThreadOutboxDeliveryAction ( {
11671223 isCreation : creation !== undefined ,
11681224 threadExists : thread !== undefined ,
11691225 shellStatus,
11701226 environmentConnected : environment ?. connectionState === "connected" ,
11711227 threadBusy : threadRuntimeIsActive ( thread ?. runtime ) ,
1228+ ...( familyRead === undefined ? { } : { familyRead } ) ,
11721229 } ) ;
1230+ if (
1231+ deliveryAction === "wait" &&
1232+ shellStatus === "live" &&
1233+ familyRead !== undefined &&
1234+ familyRead !== "settled"
1235+ ) {
1236+ // An empty family read changes no merged row, so nothing else would wake the drain.
1237+ scheduleQueuedMessageRetry ( nextQueuedMessage . messageId ) ;
1238+ continue ;
1239+ }
11731240 if ( deliveryAction === "send" && ! operableEnvironments . has ( nextQueuedMessage . environmentId ) ) {
11741241 continue ;
11751242 }
1176- const serverConfig = serverConfigs . get ( nextQueuedMessage . environmentId ) ;
11771243 const dispatchStep = resolveThreadOutboxDispatchStep ( {
11781244 deliveryAction,
11791245 fileAttachments : nextQueuedMessage . attachments . filter (
0 commit comments