fix(cloud): the Asleep since divider sits right under the last line of pre-sleep output, not at the pane's bottom (JA-15 review)

This commit is contained in:
Neil
2026-11-10 23:02:01 -08:00
parent ae0d98fcba
commit ae3b8765ed
6 changed files with 80 additions and 2 deletions
@@ -24,12 +24,18 @@ export function formatAsleepSince(at: number, now = Date.now()): string {
export function CloudAsleepTerminalNotice({
workspaceId,
asleepSince,
waking = false
waking = false,
historyEndPx = null
}: {
workspaceId: string
asleepSince: number | null
/** Waking: the waking pill speaks; keep only the greyed history and its divider. */
waking?: boolean
/**
* Where the pre-sleep output ends, in px from the top of the pane: the divider sits right under
* the last line. Null when unknown or the history fills the pane (then it ends at the bottom).
*/
historyEndPx?: number | null
}): React.JSX.Element {
const t = useTranslate()
return (
@@ -47,7 +53,13 @@ export function CloudAsleepTerminalNotice({
{asleepSince !== null ? (
<div
data-cloud-asleep-divider=""
className="flex items-center gap-2 text-[11px] text-muted-foreground"
// Right under the last line of history, so it reads as the history's end, not a footer.
style={historyEndPx !== null ? { top: historyEndPx } : undefined}
className={
historyEndPx !== null
? 'absolute inset-x-3 flex items-center gap-2 text-[11px] text-muted-foreground'
: 'flex items-center gap-2 text-[11px] text-muted-foreground'
}
>
<span className="h-px flex-1 bg-border" />
<span>
@@ -39,6 +39,8 @@ export const asleepTerminalHistory = defineFixture({
<CloudAsleepTerminalNotice
workspaceId="ws"
asleepSince={new Date(2026, 10, 10, 15, 10).getTime()}
// Five 20px lines from 12px down: right under "GET / HTTP/1.1".
historyEndPx={116}
/>
</Pane>
)
@@ -33,6 +33,15 @@ describe('CloudAsleepTerminalNotice', () => {
)
})
it('puts the divider right under the last line of history', () => {
const { container } = render(
<CloudAsleepTerminalNotice workspaceId="ws" asleepSince={Date.now()} historyEndPx={44} />
)
const divider = container.querySelector<HTMLElement>('[data-cloud-asleep-divider]')
expect(divider?.style.top).toBe('44px')
expect(divider?.className).toContain('absolute')
})
it('omits the divider when the sleep time is unknown', () => {
const { container } = render(<CloudAsleepTerminalNotice workspaceId="ws" asleepSince={null} />)
expect(container.querySelector('[data-cloud-asleep-divider]')).toBeNull()
@@ -25,6 +25,7 @@ import {
captureWakeProcessHint,
dismissWakeProcessHint,
getWakeProcessHint,
historyEndOffsetPx,
readTerminalTail,
resetWakeProcessCapture,
wakeShowsStaleHistory,
@@ -228,6 +229,7 @@ export function TerminalPaneRecoveryPortals({
<CloudAsleepTerminalNotice
workspaceId={controller.worktreeId}
asleepSince={cloud?.asleepSince ?? null}
historyEndPx={historyEndOffsetPx(pane.terminal, pane.container)}
/>,
pane.container,
`cloud-asleep-${pane.id}`
@@ -239,6 +241,7 @@ export function TerminalPaneRecoveryPortals({
<CloudAsleepTerminalNotice
workspaceId={controller.worktreeId}
asleepSince={cloud?.asleepSince ?? null}
historyEndPx={historyEndOffsetPx(pane.terminal, pane.container)}
waking
/>,
pane.container,
@@ -4,6 +4,7 @@ import {
detectForegroundCommand,
dismissWakeProcessHint,
getWakeProcessHint,
historyEndRow,
readTerminalTail,
resetWakeProcessCapture,
resetWakeProcessHintsForTests
@@ -60,6 +61,22 @@ describe('wake process hints', () => {
expect(getWakeProcessHint('leaf-1')).toEqual({ command: 'pnpm dev' })
})
it('finds where the history ends on screen, or null when it fills the pane', () => {
const screen = (lines: string[], rows: number) => ({
rows,
buffer: {
active: {
length: lines.length,
viewportY: 0,
getLine: (index: number) => ({ translateToString: () => lines[index] ?? '' })
}
}
})
expect(historyEndRow(screen(['$ python3 -m http.server', 'Serving…', '', ''], 4))).toBe(2)
expect(historyEndRow(screen(['a', 'b', 'c', 'd'], 4))).toBeNull()
expect(historyEndRow(screen(['', '', ''], 3))).toBeNull()
})
it('reads the tail of an xterm buffer', () => {
const lines = ['a', 'b', 'c']
const terminal = {
@@ -65,6 +65,41 @@ export function readTerminalTail(terminal: TailSource, count = 200): string[] {
return lines
}
type ViewportSource = TailSource & {
rows: number
buffer: TailSource['buffer'] & { active: TailSource['buffer']['active'] & { viewportY: number } }
}
/**
* Rows from the top of the visible screen through the last one with text, or null when the text
* reaches the last row (the history fills the pane) or the screen is empty.
*/
export function historyEndRow(terminal: ViewportSource): number | null {
const { viewportY } = terminal.buffer.active
for (let row = terminal.rows - 1; row >= 0; row--) {
const text = terminal.buffer.active.getLine(viewportY + row)?.translateToString(true) ?? ''
if (text.trim() !== '') {
return row === terminal.rows - 1 ? null : row + 1
}
}
return null
}
/** `historyEndRow` in px from the top of `container`, measured from the rendered xterm screen. */
export function historyEndOffsetPx(
terminal: ViewportSource & { element?: HTMLElement | undefined },
container: HTMLElement
): number | null {
const rows = historyEndRow(terminal)
const screen = terminal.element?.querySelector<HTMLElement>('.xterm-screen')
if (rows === null || !screen || terminal.rows <= 0) {
return null
}
const rowHeight = screen.clientHeight / terminal.rows
const top = screen.getBoundingClientRect().top - container.getBoundingClientRect().top
return Math.round(top + rows * rowHeight + 4)
}
// Keyed by the pane's durable leaf id; cleared once the pane has shown (or dismissed) its line.
const hints = new Map<string, WakeProcessHint>()
// Panes already looked at in this sleep: the first look sees the pre-sleep scrollback.