Keep buffered terminal retirement within delivery modules

This commit is contained in:
Brennan Benson
2026-11-10 01:28:37 -08:00
parent 002a036fe1
commit 9279cc359c
3 changed files with 24 additions and 20 deletions
@@ -15,10 +15,7 @@ import { shouldIgnoreStalePanePtyLayoutBinding } from './pane-pty-layout-binding
import { hasEmptyReattachRetireEvidence } from './empty-reattach-retire-evidence'
import { mainStartsPane } from '../spawn/main-starts-pane'
import {
acceptMainPaneResumeStart,
showMainPaneResumeOutcome
} from '../spawn/main-pane-resume-banner'
import * as mainPaneResume from '../spawn/main-pane-resume-banner'
import type { ColdRestoreAgentResumeStartup } from '../startup/fresh-spawn-types'
import type { ConnectPanePtySession } from '../connect-pane-pty-session'
@@ -326,8 +323,8 @@ export function bindHandleReattachResult(sessionBag: ConnectPanePtySession): voi
session.warnParkRevealNoHostImage(ptyId, noHostImageReason)
}
session.scheduleReattachIdleAgentCursorReset()
acceptMainPaneResumeStart(sessionBag, ptyId)
showMainPaneResumeOutcome(sessionBag, ptyId)
mainPaneResume.acceptMainPaneResumeStart(sessionBag, ptyId)
mainPaneResume.showMainPaneResumeOutcome(sessionBag, ptyId)
scheduleRuntimeGraphSync()
return true
@@ -1,5 +1,5 @@
import { getTerminalNoticeLifetime } from '../../provider-notices/terminal-notice-lifetime'
import { prepareTerminalNoticeExit } from './pty-notice-exit'
import { captureTerminalNoticeExitHandler } from './pty-notice-exit'
import { TERMINAL_SCROLLBACK_SESSION_BUFFER_BYTE_LIMIT } from '../../../../../../shared/terminal/replay/terminal-scrollback-limits'
import {
clearProcessedPtyCharTotal,
@@ -323,22 +323,14 @@ export function registerEagerPtyBuffer(
eagerPtyHandles.set(ptyId, handle)
drainPreHandlerPtyData(ptyId, dataHandler)
const noticeLifetime = getTerminalNoticeLifetime(ptyId)
const deliverBufferedExit = captureTerminalNoticeExitHandler(
getTerminalNoticeLifetime(ptyId),
exitHandler
)
// Why: defer the pre-handler exit one microtask so the caller receives the returned handle before onExit fires.
queueMicrotask(() => {
if (ptyExitHandlers.get(ptyId) === exitHandler) {
drainPreHandlerPtyExit(
ptyId,
(code, exitIncarnationId) => {
const finishNotice = prepareTerminalNoticeExit(noticeLifetime, code, exitIncarnationId)
try {
exitHandler(code)
} finally {
finishNotice()
}
},
incarnationId
)
drainPreHandlerPtyExit(ptyId, deliverBufferedExit, incarnationId)
} else {
clearPreHandlerPtyState(ptyId)
}
@@ -49,3 +49,18 @@ export function prepareAcceptedTerminalNoticeExit(
}
return () => retireTerminalNoticeLifetime(lifetime)
}
/** Bind buffered-exit delivery to its original admitted contribution. */
export function captureTerminalNoticeExitHandler(
lifetime: TerminalNoticeLifetime | undefined,
handler: (code: number) => void
): (code: number, incarnationId?: string) => void {
return (code, incarnationId) => {
const finish = prepareTerminalNoticeExit(lifetime, code, incarnationId)
try {
handler(code)
} finally {
finish()
}
}
}