-
Notifications
You must be signed in to change notification settings - Fork 52.2k
fix(ui-tui): redraw after session resume #62187
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
LeonSGP43
wants to merge
1
commit into
NousResearch:main
from
LeonSGP43:fix/62170-tui-session-redraw
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) { | ||
| refreshSessionView() | ||
| } | ||
| } | ||
| }, delay) | ||
| ) | ||
|
|
||
| return () => { | ||
| for (const timer of timers) { | ||
| clearTimeout(timer) | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.