@@ -361,6 +361,8 @@ import {
361361 shouldReleaseTimelineAnchorForToolActivity ,
362362 shouldShowBranchMismatchBanner ,
363363 shouldShowPlanFollowUpPrompt ,
364+ shouldOpenProactivePullRequest ,
365+ shouldOpenProactiveTurnDiff ,
364366 getStartedThreadModelChangeBlockReason ,
365367 LAST_INVOKED_SCRIPT_BY_PROJECT_KEY ,
366368 LastInvokedScriptByProjectSchema ,
@@ -1755,6 +1757,7 @@ function ChatViewContent(props: ChatViewProps) {
17551757 [ activeThreadEnvironmentId , activeThreadId ] ,
17561758 ) ;
17571759 const activeThreadKey = activeThreadRef ? scopedThreadKey ( activeThreadRef ) : null ;
1760+ const activeThreadShell = useThreadShell ( isServerThread ? activeThreadRef : null ) ;
17581761 const changeRequestSnapshotByKey = useAtomValue ( threadChangeRequestSnapshotsAtom ) ;
17591762 const [ timelineAnchor , setTimelineAnchor ] = useState < {
17601763 readonly threadKey : string | null ;
@@ -3634,7 +3637,16 @@ function ChatViewContent(props: ChatViewProps) {
36343637 ) ;
36353638 // The thread's own change request, placed against the project it belongs to. Without a
36363639 // project there is nothing to resolve it against, so the caller falls back to the browser.
3637- const linkedThreadPullRequest = activeThread ?. linkedPullRequest ?? null ;
3640+ const linkedThreadPullRequest = isServerThread
3641+ ? ( activeThreadShell ?. linkedPullRequest ?? activeThread ?. linkedPullRequest ?? null )
3642+ : ( activeThread ?. linkedPullRequest ?? null ) ;
3643+ const linkedThreadPullRequestKey = linkedThreadPullRequest
3644+ ? JSON . stringify ( [
3645+ linkedThreadPullRequest . projectId ,
3646+ linkedThreadPullRequest . repository ,
3647+ linkedThreadPullRequest . number ,
3648+ ] )
3649+ : null ;
36383650 const activeProjectRepository = activeProject ?. repositoryIdentity ?. displayName ?? null ;
36393651 const threadRepository = linkedThreadPullRequest ?. repository ?? activeProjectRepository ;
36403652 const openThreadPullRequest = useCallback (
@@ -3677,6 +3689,117 @@ function ChatViewContent(props: ChatViewProps) {
36773689 } ,
36783690 [ activeProject , activeProjectRepository , activeThreadRef , supportsPullRequests ] ,
36793691 ) ;
3692+ const proactiveTurnObservationRef = useRef < {
3693+ threadKey : string ;
3694+ runningTurnId : TurnId | null ;
3695+ } | null > ( null ) ;
3696+ const proactivePullRequestObservationRef = useRef < {
3697+ threadKey : string ;
3698+ targetKey : string | null ;
3699+ } | null > ( null ) ;
3700+
3701+ useEffect ( ( ) => {
3702+ if ( ! isServerThread || activeThreadKey === null || activeThreadRef === null ) {
3703+ proactiveTurnObservationRef . current = null ;
3704+ return ;
3705+ }
3706+ if ( ! clientSettingsHydrated || threadDetailLoading ) {
3707+ return ;
3708+ }
3709+
3710+ const previousObservation = proactiveTurnObservationRef . current ;
3711+ const observingSameThread = previousObservation ?. threadKey === activeThreadKey ;
3712+ const previousRunningTurnId = observingSameThread
3713+ ? previousObservation . runningTurnId
3714+ : undefined ;
3715+ const settledTurnId = latestTurnSettled ? ( activeLatestTurn ?. turnId ?? null ) : null ;
3716+ const newlyCompletedTurnId = shouldOpenProactiveTurnDiff ( {
3717+ previousRunningTurnId,
3718+ runningTurnId : activeRunningTurnId ,
3719+ settledTurnId,
3720+ turnCompleted : activeLatestTurn ?. state === "completed" ,
3721+ } )
3722+ ? settledTurnId
3723+ : null ;
3724+ const eligibleCompletion =
3725+ settings . proactivePanelsEnabled && ! shouldUseRightPanelSheet && newlyCompletedTurnId !== null ;
3726+ const checkpointReady =
3727+ eligibleCompletion &&
3728+ activeThread ?. checkpoints . some ( ( checkpoint ) => checkpoint . turnId === newlyCompletedTurnId ) ===
3729+ true ;
3730+ const shouldOpenTurn = checkpointReady && gitStatusQuery . data ?. isRepo === true ;
3731+ const shouldDeferCompletion =
3732+ eligibleCompletion && ! shouldOpenTurn && gitStatusQuery . data ?. isRepo !== false ;
3733+ proactiveTurnObservationRef . current = {
3734+ threadKey : activeThreadKey ,
3735+ runningTurnId : shouldDeferCompletion ? ( previousRunningTurnId ?? null ) : activeRunningTurnId ,
3736+ } ;
3737+ if ( ! shouldOpenTurn || newlyCompletedTurnId === null ) return ;
3738+
3739+ useDiffPanelStore . getState ( ) . selectTurn ( activeThreadRef , newlyCompletedTurnId ) ;
3740+ useRightPanelStore . getState ( ) . open ( activeThreadRef , "diff" ) ;
3741+ onDiffPanelOpen ?.( ) ;
3742+ } , [
3743+ activeThread ?. checkpoints ,
3744+ activeLatestTurn ?. turnId ,
3745+ activeLatestTurn ?. state ,
3746+ activeRunningTurnId ,
3747+ activeThreadKey ,
3748+ activeThreadRef ,
3749+ clientSettingsHydrated ,
3750+ gitStatusQuery . data ?. isRepo ,
3751+ isServerThread ,
3752+ latestTurnSettled ,
3753+ onDiffPanelOpen ,
3754+ settings . proactivePanelsEnabled ,
3755+ shouldUseRightPanelSheet ,
3756+ threadDetailLoading ,
3757+ ] ) ;
3758+
3759+ useEffect ( ( ) => {
3760+ if ( ! isServerThread || activeThreadKey === null || activeThreadRef === null ) {
3761+ proactivePullRequestObservationRef . current = null ;
3762+ return ;
3763+ }
3764+ if ( ! clientSettingsHydrated || threadDetailLoading ) {
3765+ return ;
3766+ }
3767+
3768+ const previousObservation = proactivePullRequestObservationRef . current ;
3769+ const observingSameThread = previousObservation ?. threadKey === activeThreadKey ;
3770+ const previousTargetKey = observingSameThread ? previousObservation . targetKey : undefined ;
3771+ const newlyLinkedPullRequest = shouldOpenProactivePullRequest (
3772+ previousTargetKey ,
3773+ linkedThreadPullRequestKey ,
3774+ ) ;
3775+ const eligibleLink =
3776+ settings . proactivePanelsEnabled && ! shouldUseRightPanelSheet && newlyLinkedPullRequest ;
3777+ const shouldOpenLink =
3778+ eligibleLink &&
3779+ pullRequestsCapabilityKnown &&
3780+ supportsPullRequests &&
3781+ linkedThreadPullRequest !== null ;
3782+ const shouldDeferLink = eligibleLink && ! pullRequestsCapabilityKnown ;
3783+ proactivePullRequestObservationRef . current = {
3784+ threadKey : activeThreadKey ,
3785+ targetKey : shouldDeferLink ? ( previousTargetKey ?? null ) : linkedThreadPullRequestKey ,
3786+ } ;
3787+ if ( ! shouldOpenLink || linkedThreadPullRequest === null ) return ;
3788+
3789+ useRightPanelStore . getState ( ) . openPullRequest ( activeThreadRef , linkedThreadPullRequest ) ;
3790+ } , [
3791+ activeThreadKey ,
3792+ activeThreadRef ,
3793+ clientSettingsHydrated ,
3794+ isServerThread ,
3795+ linkedThreadPullRequest ,
3796+ linkedThreadPullRequestKey ,
3797+ pullRequestsCapabilityKnown ,
3798+ settings . proactivePanelsEnabled ,
3799+ shouldUseRightPanelSheet ,
3800+ supportsPullRequests ,
3801+ threadDetailLoading ,
3802+ ] ) ;
36803803 const togglePreviewPanel = useCallback ( ( ) => {
36813804 if ( ! activeThreadRef || ! isPreviewSupportedInRuntime ( ) ) return ;
36823805 if ( previewPanelOpen ) {
@@ -4581,8 +4704,6 @@ function ChatViewContent(props: ChatViewProps) {
45814704 : null ,
45824705 [ activeThreadBranch , activeWorktreePath , envMode , gitStatusQuery . data ?. refName , isServerThread ] ,
45834706 ) ;
4584- // The server-projected settled state keeps the banner and sidebar in sync.
4585- const activeThreadShell = useThreadShell ( isServerThread ? activeThreadRef : null ) ;
45864707 const activeComposerTasksProgress = useMemo ( ( ) => {
45874708 if ( ! activeLatestTurn || latestTurnSettled || activePlan ?. turnId !== activeLatestTurn . turnId ) {
45884709 return null ;
0 commit comments