Skip to content
Closed
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
121 changes: 121 additions & 0 deletions ui-tui/src/app/sessionResumeView.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import { afterEach, describe, expect, it, vi } from 'vitest'

const { evictInkCachesMock, forceRedrawMock } = vi.hoisted(() => ({
evictInkCachesMock: vi.fn(),
forceRedrawMock: vi.fn()
}))

vi.mock('@hermes/ink', () => ({
evictInkCaches: evictInkCachesMock,
forceRedraw: forceRedrawMock
}))

import { refreshSessionView, scheduleResumeScrollToBottom } from './sessionResumeView.js'

describe('refreshSessionView', () => {
afterEach(() => {
evictInkCachesMock.mockReset()
forceRedrawMock.mockReset()
})

it('evicts Ink caches and forces a full repaint', () => {
const stdout = {} as NodeJS.WriteStream

refreshSessionView(stdout)

expect(evictInkCachesMock).toHaveBeenCalledWith('all')
expect(forceRedrawMock).toHaveBeenCalledWith(stdout)
})
})

describe('scheduleResumeScrollToBottom', () => {
afterEach(() => {
vi.useRealTimers()
evictInkCachesMock.mockReset()
forceRedrawMock.mockReset()
})

it('re-snaps while sticky and stops when the user scrolls away', () => {
vi.useFakeTimers()
let sticky = true
let lastManualScrollAt = 0
const scrollToBottom = vi.fn()

const cancel = scheduleResumeScrollToBottom(
{
current: {
getLastManualScrollAt: () => lastManualScrollAt,
isSticky: () => sticky,
scrollToBottom
}
} as any,
[0, 80, 240]
)

vi.advanceTimersByTime(0)
expect(scrollToBottom).toHaveBeenCalledTimes(1)
expect(evictInkCachesMock).toHaveBeenCalledWith('all')
expect(forceRedrawMock).toHaveBeenCalledTimes(1)

vi.advanceTimersByTime(80)
expect(scrollToBottom).toHaveBeenCalledTimes(2)
expect(forceRedrawMock).toHaveBeenCalledTimes(1)

sticky = false
lastManualScrollAt = Date.now() + 1
vi.advanceTimersByTime(160)
expect(scrollToBottom).toHaveBeenCalledTimes(2)

cancel()
})

it('cancels pending resume snaps', () => {
vi.useFakeTimers()
const scrollToBottom = vi.fn()

const cancel = scheduleResumeScrollToBottom(
{
current: {
getLastManualScrollAt: () => 0,
isSticky: () => true,
scrollToBottom
}
} as any,
[20]
)

cancel()
vi.advanceTimersByTime(20)

expect(scrollToBottom).not.toHaveBeenCalled()
expect(forceRedrawMock).not.toHaveBeenCalled()
})

it('keeps the immediate resume snap even before sticky state settles', () => {
vi.useFakeTimers()
let sticky = false
const scrollToBottom = vi.fn()

const cancel = scheduleResumeScrollToBottom(
{
current: {
getLastManualScrollAt: () => 0,
isSticky: () => sticky,
scrollToBottom
}
} as any,
[0, 80]
)

vi.advanceTimersByTime(0)
expect(scrollToBottom).toHaveBeenCalledTimes(1)
expect(forceRedrawMock).toHaveBeenCalledTimes(1)

vi.advanceTimersByTime(80)
expect(scrollToBottom).toHaveBeenCalledTimes(1)
expect(forceRedrawMock).toHaveBeenCalledTimes(1)

sticky = true
cancel()
})
})
41 changes: 41 additions & 0 deletions ui-tui/src/app/sessionResumeView.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import type { ScrollBoxHandle } from '@hermes/ink'
import { evictInkCaches, forceRedraw } from '@hermes/ink'
import type { RefObject } from 'react'

export const refreshSessionView = (stdout: NodeJS.WriteStream = process.stdout) => {
evictInkCaches('all')
forceRedraw(stdout)
}

export const scheduleResumeScrollToBottom = (
scrollRef: RefObject<null | ScrollBoxHandle>,
delays: readonly number[] = [0, 80, 240]
) => {
const startedAt = Date.now()

const timers = delays.map((delay, index) =>
setTimeout(() => {
const scroll = scrollRef.current

if (!scroll) {
return
}

const manuallyScrolledAfterResume = scroll.getLastManualScrollAt() > startedAt

if (!manuallyScrolledAfterResume && (index === 0 || scroll.isSticky())) {
scroll.scrollToBottom()

if (index === 0) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This repaint is intentionally limited to the first resume snap. Since #62170 also reports a live-stream freeze after the resume completes, please avoid closing that entire issue with this PR unless that remaining symptom is split into a follow-up.

refreshSessionView()
}
}
}, delay)
)

return () => {
for (const timer of timers) {
clearTimeout(timer)
}
}
}
32 changes: 4 additions & 28 deletions ui-tui/src/app/useSessionLifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@ import type { Msg, PanelSection, SessionInfo, Usage } from '../types.js'

import type { ComposerActions, GatewayRpc, StateSetter } from './interfaces.js'
import { patchOverlayState } from './overlayStore.js'
import { scheduleResumeScrollToBottom } from './sessionResumeView.js'
import { turnController } from './turnController.js'
import { patchTurnState } from './turnStore.js'
import { getUiState, patchUiState } from './uiStore.js'

export { refreshSessionView, scheduleResumeScrollToBottom } from './sessionResumeView.js'

const usageFrom = (info: null | SessionInfo): Usage => (info?.usage ? { ...ZERO, ...info.usage } : ZERO)

const statusFromLiveSession = (status?: string, running = false) => {
Expand Down Expand Up @@ -68,34 +71,6 @@ export const hydrateLiveSessionInflight = (inflight?: null | SessionInflightTurn
turnController.hydrateStreamingText(assistant)
}

export const scheduleResumeScrollToBottom = (
scrollRef: RefObject<null | ScrollBoxHandle>,
delays: readonly number[] = [0, 80, 240]
) => {
const startedAt = Date.now()
const timers = delays.map((delay, index) =>
setTimeout(() => {
const scroll = scrollRef.current

if (!scroll) {
return
}

const manuallyScrolledAfterResume = scroll.getLastManualScrollAt() > startedAt

if (!manuallyScrolledAfterResume && (index === 0 || scroll.isSticky())) {
scroll.scrollToBottom()
}
}, delay)
)

return () => {
for (const timer of timers) {
clearTimeout(timer)
}
}
}

const trimTail = (items: Msg[]) => {
const q = [...items]

Expand Down Expand Up @@ -148,6 +123,7 @@ export function useSessionLifecycle(opts: UseSessionLifecycleOptions) {
targetSid ? rpc<SessionCloseResponse>('session.close', { session_id: targetSid }) : Promise.resolve(null),
[rpc]
)

const cancelResumeScrollRef = useRef<null | (() => void)>(null)

const resetSession = useCallback(() => {
Expand Down
Loading