Files
Netcatty/components/terminalLayer/commandCwdProbe.ts
T
陈大猫 3ef402cc71 fix(terminal): restore local shell cwd without OSC 7 (#3784)
Fixes #1952. When restore-terminal-cwd is on, local POSIX shells probe the PTY process cwd (/proc on Linux, lsof on macOS) and re-probe when the prompt returns so slow commands like `sleep 1; cd /tmp` land correctly. SSH restore path unchanged.
2026-10-20 13:15:32 +08:00

54 lines
1.8 KiB
TypeScript

import { isTerminalSessionEligibleForSftpReuse } from "../../application/state/terminalConnectionReuse";
import type { TerminalSession } from "../../domain/models";
import { resolveHostFollowTerminalCwd, resolveSftpFollowTerminalCwdTargetHost } from "../../domain/sftpFollowTerminalCwd";
type FollowTerminalCwdHost = {
sftpFollowTerminalCwd?: boolean;
};
type ShouldProbeCommandCwdOptions = {
restoreTerminalCwd: boolean;
visibleSftpHost?: FollowTerminalCwdHost | null;
sessionHost?: FollowTerminalCwdHost | null;
globalSftpFollowTerminalCwd: boolean;
restrictExtraSshChannels?: boolean;
};
type CommandCwdProbeSession = Pick<
TerminalSession,
"protocol" | "status" | "moshEnabled" | "etEnabled" | "shellType"
>;
/**
* Sessions whose post-command cwd probe can run.
*
* SSH reuses the extra exec channel (`getSessionPwd`). Local POSIX shells have
* no OSC 7 by default and no SSH channel, but the same probe path can read the
* PTY process cwd. CMD/PowerShell cannot accept the POSIX restore `cd`.
*/
export const canScheduleCommandCwdProbe = (session: CommandCwdProbeSession): boolean => {
if (session.status !== "connected") return false;
if (session.protocol === "local") {
return session.shellType !== "powershell" && session.shellType !== "cmd";
}
return isTerminalSessionEligibleForSftpReuse(session);
};
export const shouldProbeCommandCwd = ({
restoreTerminalCwd,
visibleSftpHost,
sessionHost,
globalSftpFollowTerminalCwd,
restrictExtraSshChannels = false,
}: ShouldProbeCommandCwdOptions): boolean => {
if (restrictExtraSshChannels) return false;
if (restoreTerminalCwd) return true;
if (!visibleSftpHost) return false;
const followHost = resolveSftpFollowTerminalCwdTargetHost(visibleSftpHost, sessionHost);
return resolveHostFollowTerminalCwd(
followHost?.sftpFollowTerminalCwd,
globalSftpFollowTerminalCwd,
);
};