Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,7 @@ export function useRecentsProvider(): ResourceProvider {
// the resume has entered the session, nothing on failure (#15732).
focusComposerForResumedSession(
dispatch(resumeFromHistory({ key: s.key, title: s.title || s.key })).unwrap(),
store,
)
navigate('/chat')
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useNavigate } from 'react-router-dom'
import { useQuery, useQueryClient } from '@tanstack/react-query'

import { api } from '../../../api/client'
import { useAppDispatch, useAppSelector } from '../../../store'
import { useAppDispatch, useAppSelector, useAppStore } from '../../../store'
import { resumeFromHistory } from '../../../store/chatSlice'
import { useSelectInstance } from '../../../hooks/useSelectInstance'
import { focusComposerForResumedSession } from '../../../pages/chat/composerFocus'
Expand Down Expand Up @@ -293,6 +293,9 @@ export function useSessionsProvider(opts?: {
active?: boolean
}): ResourceProvider {
const dispatch = useAppDispatch()
// Read on the focus frame after a resume: only a resume that took the view
// moves the caret.
const store = useAppStore()
const navigate = useNavigate()
const queryClient = useQueryClient()
const openInSplit = opts?.openInSplit
Expand Down Expand Up @@ -369,14 +372,14 @@ export function useSessionsProvider(opts?: {
// already active, and declines while this surface's input still holds
// focus. Unwrapped, so a failed resume focuses nothing; the slice records
// the failure either way (see resumeFromHistory.rejected).
focusComposerForResumedSession(dispatch(resumeFromHistory(ref)).unwrap())
focusComposerForResumedSession(dispatch(resumeFromHistory(ref)).unwrap(), store)
// The palette can be opened from ANY page (artifacts, settings, …);
// resumeFromHistory only activates the slot in the store, so land
// the user on the chat surface where that slot renders.
navigate('/chat')
},
openInSplit,
}),
[dispatch, navigate, queryClient, openInSplit, selectInstance],
[dispatch, store, navigate, queryClient, openInSplit, selectInstance],
)
}
3 changes: 2 additions & 1 deletion website/src/hooks/useSessionActions.cov80.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ describe('duplicate', () => {
const { result } = harness()
act(() => result.current.duplicate(KEY))
await waitFor(() => expect(apiMock.forkChatSlot).toHaveBeenCalledWith(KEY))
await waitFor(() => expect(switchSlot).toHaveBeenCalledWith('zzq-forked'))
// The switch names the fork's request number, so a later click still wins (`forkMayOpen`).
await waitFor(() => expect(switchSlot).toHaveBeenCalledWith({ key: 'zzq-forked', requestSeq: expect.any(Number) }))
})

it('switches nowhere when the fork is refused', async () => {
Expand Down
2 changes: 1 addition & 1 deletion website/src/hooks/useSessionActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ export function useSessionActions(mode?: string, opts?: {
// The user opened another session while the fork ran: the fork stays
// listed, and the view and its chrome (Split View) stay as they are.
if (!forkMayOpen(store.getState(), started.origin)) return
dispatch(switchSlot(data.key))
dispatch(switchSlot({ key: data.key, requestSeq: started.origin.seq }))
// The child inherits the source's folder, which can be collapsed (the
// source row sits in the pinned group or a board lane, or another tab
// collapsed the folder meanwhile): open that path so the active row shows.
Expand Down
13 changes: 8 additions & 5 deletions website/src/pages/chat-sidebar/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ function plainCreateKey(folderId: string, columnId?: string, memoryMode?: string
*/
async function createSlotWithOrigin(dispatch: AppDispatch, args: Parameters<typeof createSlot>[0]): Promise<{ slot: Slot; origin: ForkOrigin }> {
const action = await dispatch(createSlot(args))
if (createSlot.fulfilled.match(action)) return { slot: action.payload, origin: { activeSlot: action.meta.originActiveSlot ?? null, moves: action.meta.originActiveSlotMoves, autoMoves: action.meta.originActiveSlotAutoMoves } }
if (createSlot.fulfilled.match(action)) {
const m = action.meta
return { slot: action.payload, origin: { activeSlot: m.originActiveSlot ?? null, moves: m.originActiveSlotMoves, autoMoves: m.originActiveSlotAutoMoves, requestMoves: m.originActiveSlotRequestMoves, seq: m.originRequestSeq, burst: m.originRequestBurst } }
}
unwrapResult(action)
throw new Error('createSlot settled without fulfilling or rejecting')
}
Expand All @@ -77,7 +80,7 @@ export function useFolderChatCreate({ folders, mode, dispatch, dropSlotMutation,
mode: string | undefined
dispatch: AppDispatch
dropSlotMutation: BoardColumnMutations['dropSlotMutation']
onOpenSlotInNewTab: ((key: string, opts?: { background?: boolean | undefined; } | undefined) => void) | undefined
onOpenSlotInNewTab: ((key: string, opts?: { background?: boolean | undefined; requestSeq?: number } | undefined) => void) | undefined
// A plain create ACTIVATES the new session, so the host must leave Split View
// and show it — the same callback a row click fires (ChatSidebar's
// `onSelectSlot`). Without it the grid keeps drawing the old split over a
Expand Down Expand Up @@ -174,7 +177,7 @@ export function useFolderChatCreate({ folders, mode, dispatch, dropSlotMutation,
if (inNewTab && onOpenSlotInNewTab && slot?.key) {
// Adds a tab beside the active one; `foreground` also switches to it,
// same as the header New button's gestures (see createChatMutation).
onOpenSlotInNewTab(slot.key, { background: inNewTab === 'background' })
onOpenSlotInNewTab(slot.key, { background: inNewTab === 'background', ...(inNewTab === 'foreground' ? { requestSeq: origin.seq } : {}) })
// A foreground tab switches the view onto the new session, so leave
// Split View too — the same guarded callback the header New fires
// after its own foreground switch (createChatMutation). The plain
Expand Down Expand Up @@ -278,7 +281,7 @@ export function useSessionCreate({ setNewChatError, dispatch, mode, onOpenSlotIn
setNewChatError: Dispatch<SetStateAction<string>>
dispatch: AppDispatch
mode: string | undefined
onOpenSlotInNewTab: ((key: string, opts?: { background?: boolean | undefined; } | undefined) => void) | undefined
onOpenSlotInNewTab: ((key: string, opts?: { background?: boolean | undefined; requestSeq?: number } | undefined) => void) | undefined
// Leave Split View on a plain create so the new, now-active session shows
// instead of the old split (#21935). Same callback a row click fires; the
// background-tab gesture does not fire it (the split is kept on purpose).
Expand Down Expand Up @@ -344,7 +347,7 @@ export function useSessionCreate({ setNewChatError, dispatch, mode, onOpenSlotIn
onSuccess: ({ slot, origin }, { inNewTab: requested }) => {
const inNewTab = settledTabMode(store, requested, origin)
if (inNewTab && onOpenSlotInNewTab) {
onOpenSlotInNewTab(slot.key, { background: inNewTab === 'background' })
onOpenSlotInNewTab(slot.key, { background: inNewTab === 'background', ...(inNewTab === 'foreground' ? { requestSeq: origin.seq } : {}) })
// Background: the user stays on their transcript, so its composer keeps
// whatever focus it had — no `focusComposer`, same as the row gesture.
if (inNewTab === 'background') return
Expand Down
2 changes: 1 addition & 1 deletion website/src/pages/chat-sidebar/scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export interface ChatSidebarProps {
* window), and an omitted callback leaves the gestures unbound rather than
* falling back to a plain switch — a middle-click that quietly navigated
* would be indistinguishable from a misfire. */
onOpenSlotInNewTab?: (key: string, opts?: { background?: boolean }) => void
onOpenSlotInNewTab?: (key: string, opts?: { background?: boolean; requestSeq?: number }) => void
/** Reveal a session's pull request / issue in the side panel instead of
* leaving for the provider's website.
*
Expand Down
19 changes: 13 additions & 6 deletions website/src/pages/chat/composerFocus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -472,18 +472,25 @@ function queryComposerForSlot(key: string | undefined): HTMLElement | null {
* `focusComposerAfter` gives: the slice records it, and there is no new
* composer to focus.
*
* No still-active read here: `resumeFromHistory` moves the active slot only in
* its fulfilled reducer, at the moment this promise settles, so there is no
* provisional window for a keystroke to land in, and the slot the reducer
* entered IS the one the gesture named.
* A displayable answer is still not proof the view moved: the reducer also
* declines to open a resume that answers after the user opened another session
* or clicked a newer row. So the store is read on the focus frame, and the
* caret moves only when `key` is the active slot then. On the single-chat
* surface the one composer belongs to the session the user moved to.
*
* `key` is the thunk's own payload field -- the slot the gateway resumed, which
* is the one the reducer entered -- and names the pane to focus in split view
* (#15937).
*/
export function focusComposerForResumedSession(resumed: Promise<{ ok: boolean; surface?: string; key: string }>): void {
export function focusComposerForResumedSession(
resumed: Promise<{ ok: boolean; surface?: string; key: string }>,
store: { getState(): { chat: { activeSlot: string | null } } },
): void {
void resumed
.then(result => { if (result.ok && isChatPageSurface(result.surface)) requestAnimationFrame(() => focusComposerNow(result.key)) })
.then(result => {
if (!result.ok || !isChatPageSurface(result.surface)) return
requestAnimationFrame(() => { if (store.getState().chat.activeSlot === result.key) focusComposerNow(result.key) })
})
.catch(() => {})
}

Expand Down
7 changes: 5 additions & 2 deletions website/src/pages/chat/memoryModeSwitch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,11 @@ export async function runMemorySwitch(
// draft, which for a fresh slot is empty, and the old slot (where the
// draft lives) is deleted afterwards.
let created: { key?: string } | undefined
// The pending action numbers this create (`requestSeq`) synchronously, so
// the origin read right after it carries the create's own number.
const creating = c.dispatch(createSlot({ ...opts, activate: false }))
const origin = captureForkOrigin(c.store.getState())
try { created = await c.dispatch(createSlot({ ...opts, activate: false })).unwrap() } catch (error) {
try { created = await creating.unwrap() } catch (error) {
c.showActionError(errMessage(error) || unknownError())
return false
}
Expand Down Expand Up @@ -133,7 +136,7 @@ export async function runMemorySwitch(
else if (left) revisited = true
})
try {
await c.dispatch(switchSlot(next)).unwrap()
await c.dispatch(switchSlot({ key: next, requestSeq: origin.seq })).unwrap()
} catch (error) {
// The rejected switch moves the view back to the old slot, and the
// slot-change effect clears any error not marked to survive a switch --
Expand Down
2 changes: 1 addition & 1 deletion website/src/pages/chat/page/SessionsSidebarChrome.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ interface SessionsSidebarChromeProps {
canStageSessionRef: boolean
chatPaneEl: HTMLDivElement | null
stageSessionRef: (ref: SessionRef) => void
openSlotInNewTab: (key: string, opts?: { background?: boolean; }) => void
openSlotInNewTab: (key: string, opts?: { background?: boolean; requestSeq?: number }) => void
ownsSessionTabs: boolean
activePoppedOut: boolean
focusActivePopout: (id: string) => void
Expand Down
7 changes: 5 additions & 2 deletions website/src/pages/chat/page/sessionsDrawer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,12 +347,15 @@ export function useSessionsDrawer({
// null and the slot-state reset that follows leaves it undefined.
const reduxStore = useStore<RootState>()
const prevActiveSlotRef = useRef(activeSlot ?? null)
const userMovesRef = useRef(reduxStore.getState().chat.activeSlotMoves ?? 0)
// A create, fork or resume the user clicked opens in `activeSlotRequestMoves`,
// and that is their pick too.
const userPicks = (s: RootState) => (s.chat.activeSlotMoves ?? 0) + (s.chat.activeSlotRequestMoves ?? 0)
const userMovesRef = useRef(userPicks(reduxStore.getState()))
useEffect(() => {
const left = prevActiveSlotRef.current
const now = activeSlot ?? null
const state = reduxStore.getState()
const userMoves = state.chat.activeSlotMoves ?? 0
const userMoves = userPicks(state)
const userMoved = userMoves !== userMovesRef.current
prevActiveSlotRef.current = now
userMovesRef.current = userMoves
Expand Down
Loading
Loading