Files
Netcatty/application/state/resolveTerminalSessionExitIntent.ts
T
Netcatty-Bot 4af4af274b fix(ssh): accurately report missing remote exit status
Report transport closure without inventing a successful remote exit status, while preserving any actual exit code or signal already received. Reuse the existing exit classification to keep normal exits from being treated as reconnectable disconnects.

Related to #3631. This changes the diagnostic message only; the frequent-disconnection report remains open.

Validation: 458 focused checks, lint, production build, real Electron/local SSH disconnect workflow, current-head Codex review, and repository CI passed.
2026-10-15 11:43:32 +08:00

77 lines
2.8 KiB
TypeScript

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<ProviderValidationIssue>;
};
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;
}