@@ -2,7 +2,11 @@ import { settlePromise, squashAtomCommandFailure } from "@t3tools/client-runtime
22import { AsyncResult } from "effect/reactivity" ;
33import * as Dialog from "../../components/ui/Dialog" ;
44import { useAtomValue } from "@effect/atom-react" ;
5- import { useChildThreadInputs , useThreadReportedModelSelection } from "../../state/entities" ;
5+ import {
6+ useChildThreadInputs ,
7+ useThreadReportedModelSelection ,
8+ useThreadShell ,
9+ } from "../../state/entities" ;
610import { UsageLimitRecoveryCard } from "./UsageLimitRecoveryCard" ;
711import { useNavigation } from "@react-navigation/native" ;
812import type { WorktreeSetupCardProps } from "./worktree-setup-card" ;
@@ -319,6 +323,70 @@ const USER_INPUT_TOGGLE_TIMING = {
319323 easing : Easing . out ( Easing . cubic ) ,
320324} ;
321325
326+ // The visit watermark follows the shell's updatedAt, which ticks while a run
327+ // streams. Keeping it in a leaf lets the screen read the quiet shell.
328+ function ThreadVisitWatermark ( props : {
329+ readonly environmentId : EnvironmentId ;
330+ readonly selectedThread : EnvironmentThreadShell ;
331+ readonly showContent : boolean ;
332+ } ) {
333+ const liveThread = useThreadShell ( {
334+ environmentId : props . environmentId ,
335+ threadId : props . selectedThread . id ,
336+ } ) ;
337+ const thread = liveThread ?? props . selectedThread ;
338+ const showContent = props . showContent ;
339+ const selectedThreadKey = scopedThreadKey ( props . environmentId , thread . id ) ;
340+ const visitThread = useAtomCommand ( threadEnvironment . visit , { reportFailure : false } ) ;
341+ const lastDispatchedVisitRef = useRef < string | null > ( null ) ;
342+ const lastVisitDispatchRef = useRef ( { threadKey : selectedThreadKey , at : 0 } ) ;
343+ const selectedThreadId = thread . id ;
344+ const selectedThreadUpdatedAt = thread . updatedAt ;
345+ const selectedThreadLastVisitedAt = thread . lastVisitedAt ;
346+ const selectedThreadCompletedAt = thread . latestRun ?. completedAt ;
347+ useEffect ( ( ) => {
348+ if ( ! showContent || selectedThreadLastVisitedAt === undefined ) return ;
349+ const threadUpdatedAtMs = Date . parse ( selectedThreadUpdatedAt ) ;
350+ if ( Number . isNaN ( threadUpdatedAtMs ) ) return ;
351+ const lastVisitedAtMs = selectedThreadLastVisitedAt
352+ ? Date . parse ( selectedThreadLastVisitedAt )
353+ : NaN ;
354+ if ( ! Number . isNaN ( lastVisitedAtMs ) && lastVisitedAtMs >= threadUpdatedAtMs ) return ;
355+ const dispatchKey = `${ selectedThreadKey } :${ selectedThreadUpdatedAt } ` ;
356+ if ( lastDispatchedVisitRef . current === dispatchKey ) return ;
357+ const dispatch = ( ) => {
358+ lastDispatchedVisitRef . current = dispatchKey ;
359+ lastVisitDispatchRef . current = { threadKey : selectedThreadKey , at : Date . now ( ) } ;
360+ void visitThread ( {
361+ environmentId : props . environmentId ,
362+ input : { threadId : selectedThreadId , visitedAt : selectedThreadUpdatedAt } ,
363+ } ) ;
364+ } ;
365+ const completedAtMs = selectedThreadCompletedAt ? Date . parse ( selectedThreadCompletedAt ) : NaN ;
366+ const hasUnseenCompletion =
367+ ! Number . isNaN ( completedAtMs ) &&
368+ ( Number . isNaN ( lastVisitedAtMs ) || completedAtMs > lastVisitedAtMs ) ;
369+ const previous = lastVisitDispatchRef . current ;
370+ const elapsed = Date . now ( ) - previous . at ;
371+ if ( previous . threadKey !== selectedThreadKey || hasUnseenCompletion || elapsed >= 10_000 ) {
372+ dispatch ( ) ;
373+ return ;
374+ }
375+ const timer = setTimeout ( dispatch , 10_000 - elapsed ) ;
376+ return ( ) => clearTimeout ( timer ) ;
377+ } , [
378+ props . environmentId ,
379+ selectedThreadId ,
380+ selectedThreadKey ,
381+ selectedThreadLastVisitedAt ,
382+ selectedThreadCompletedAt ,
383+ selectedThreadUpdatedAt ,
384+ showContent ,
385+ visitThread ,
386+ ] ) ;
387+ return null ;
388+ }
389+
322390export const ThreadDetailScreen = function ThreadDetailScreen ( props : ThreadDetailScreenProps ) {
323391 const usesNativeWorkspaceColumns = useNativeWorkspaceColumnsSupported ( ) ;
324392 const navigation = useNavigation ( ) ;
@@ -785,54 +853,6 @@ export const ThreadDetailScreen = function ThreadDetailScreen(props: ThreadDetai
785853 setComposerFocused ( false ) ;
786854 } , [ selectedThreadKey , showContent ] ) ;
787855
788- const visitThread = useAtomCommand ( threadEnvironment . visit , { reportFailure : false } ) ;
789- const lastDispatchedVisitRef = useRef < string | null > ( null ) ;
790- const lastVisitDispatchRef = useRef ( { threadKey : selectedThreadKey , at : 0 } ) ;
791- const selectedThreadId = props . selectedThread . id ;
792- const selectedThreadUpdatedAt = props . selectedThread . updatedAt ;
793- const selectedThreadLastVisitedAt = props . selectedThread . lastVisitedAt ;
794- const selectedThreadCompletedAt = props . selectedThread . latestRun ?. completedAt ;
795- useEffect ( ( ) => {
796- if ( ! showContent || selectedThreadLastVisitedAt === undefined ) return ;
797- const threadUpdatedAtMs = Date . parse ( selectedThreadUpdatedAt ) ;
798- if ( Number . isNaN ( threadUpdatedAtMs ) ) return ;
799- const lastVisitedAtMs = selectedThreadLastVisitedAt
800- ? Date . parse ( selectedThreadLastVisitedAt )
801- : NaN ;
802- if ( ! Number . isNaN ( lastVisitedAtMs ) && lastVisitedAtMs >= threadUpdatedAtMs ) return ;
803- const dispatchKey = `${ selectedThreadKey } :${ selectedThreadUpdatedAt } ` ;
804- if ( lastDispatchedVisitRef . current === dispatchKey ) return ;
805- const dispatch = ( ) => {
806- lastDispatchedVisitRef . current = dispatchKey ;
807- lastVisitDispatchRef . current = { threadKey : selectedThreadKey , at : Date . now ( ) } ;
808- void visitThread ( {
809- environmentId : props . environmentId ,
810- input : { threadId : selectedThreadId , visitedAt : selectedThreadUpdatedAt } ,
811- } ) ;
812- } ;
813- const completedAtMs = selectedThreadCompletedAt ? Date . parse ( selectedThreadCompletedAt ) : NaN ;
814- const hasUnseenCompletion =
815- ! Number . isNaN ( completedAtMs ) &&
816- ( Number . isNaN ( lastVisitedAtMs ) || completedAtMs > lastVisitedAtMs ) ;
817- const previous = lastVisitDispatchRef . current ;
818- const elapsed = Date . now ( ) - previous . at ;
819- if ( previous . threadKey !== selectedThreadKey || hasUnseenCompletion || elapsed >= 10_000 ) {
820- dispatch ( ) ;
821- return ;
822- }
823- const timer = setTimeout ( dispatch , 10_000 - elapsed ) ;
824- return ( ) => clearTimeout ( timer ) ;
825- } , [
826- props . environmentId ,
827- selectedThreadId ,
828- selectedThreadKey ,
829- selectedThreadLastVisitedAt ,
830- selectedThreadCompletedAt ,
831- selectedThreadUpdatedAt ,
832- showContent ,
833- visitThread ,
834- ] ) ;
835-
836856 useEffect ( ( ) => {
837857 setAnchorMessageId ( null ) ;
838858 setSubmittedMessageId ( null ) ;
@@ -1003,6 +1023,11 @@ export const ThreadDetailScreen = function ThreadDetailScreen(props: ThreadDetai
10031023 return (
10041024 < View className = "flex-1" >
10051025 { ! usesNativeWorkspaceColumns ? < NativeLayoutObserver onChange = { setScreenMetrics } /> : null }
1026+ < ThreadVisitWatermark
1027+ environmentId = { props . environmentId }
1028+ selectedThread = { props . selectedThread }
1029+ showContent = { showContent }
1030+ />
10061031 { showContent ? (
10071032 < View
10081033 style = { { flex : 1 } }
0 commit comments