import type { ProviderValidationIssue } from "@netcatty/plugin-contract"; export type TerminalSessionExitEvent = { exitCode?: number; signal?: number; error?: string; reason?: "exited" | "error" | "timeout" | "closed" | "signaled"; // ssh2 exit-signal name ("SIGHUP", "SIGTERM", ...). Set when the server // terminated the session process. Distinct from a transport "closed". exitSignal?: string; // True when the remote closed the channel without sending exit-status or // exit-signal (#3591): no numeric exit code exists, and the renderer shows // a distinct message instead of a misleading "code 0". remoteClosedWithoutExitStatus?: boolean; diagnostics?: ReadonlyArray; }; export function formatSessionClosedExitMessage(evt: TerminalSessionExitEvent): string { if (evt?.remoteClosedWithoutExitStatus) { return "\r\n[session closed by remote (no exit status)]"; } if (evt?.reason === "signaled" || (typeof evt?.exitSignal === "string" && evt.exitSignal.trim().length > 0)) { return "\r\n[session closed by remote signal]"; } // Infrastructure exit codes do not describe the remote shell's status. if (evt?.reason === "error") { return "\r\n[session closed unexpectedly]"; } return `\r\n[session closed${evt?.exitCode !== undefined ? ` (code ${evt.exitCode})` : ""}]`; } export type TerminalSessionExitIntent = | { kind: "closeSession" } | { kind: "markDisconnected" }; type TerminalPopupExitOptions = { autoCloseOnExit?: boolean; isAttachMode?: boolean; }; function isConfirmedCleanExit(evt: TerminalSessionExitEvent): boolean { return evt.reason === "exited" && evt.exitCode === 0; } export function resolveTerminalSessionExitIntent( evt: TerminalSessionExitEvent, // Keep-tab is the safe default: a caller that has not loaded the terminal // setting yet must not silently delete a session tab. Remote graceful // channel closes (e.g. a jump host reaping an idle session) are reported // as a clean exit, so an unavailable setting must not re-enable closing. autoCloseOnExit = false, ): TerminalSessionExitIntent { if (autoCloseOnExit && isConfirmedCleanExit(evt)) { return { kind: "closeSession" }; } // Non-zero or unknown exits, timeouts, transport errors, and channel closes // should keep the tab visible so the user can inspect output and reconnect. return { kind: "markDisconnected" }; } export function shouldCloseTerminalPopupOnExit( evt: TerminalSessionExitEvent, options: TerminalPopupExitOptions = {}, ): boolean { if (options.autoCloseOnExit === false) return false; return options.isAttachMode === true || isConfirmedCleanExit(evt); } export function shouldRevealTerminalPopupOnExit( _evt: TerminalSessionExitEvent, options: TerminalPopupExitOptions = {}, ): boolean { return options.autoCloseOnExit === false && options.isAttachMode !== true; }