Files
Netcatty/components/terminalLayer/commandCwdProbe.ts
T
Cursor Agentand陈大猫 cfbd47f1bb fix(terminal): record local shell cwd for session restore
Stock local bash never emits OSC 7, and the post-command cwd probe
was skipped because local sessions cannot reuse an SSH channel.
When restore-cwd is enabled, read the local PTY process directory
through the same probe path SSH already uses.

Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>
2026-10-20 04:50:01 +00: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,
);
};