Files
Netcatty/electron/bridges/ai/ptyExec.test.cjs
T
陈大猫 ae52ef461e fix(ai): probe remote login shell so fish AI exec works (#1854) (#2061)
* fix(ai): probe remote login shell before PTY wrappers (#1854)

SSH/Telnet sessions never set shellKind, so AI exec defaulted to the
posix wrapper and broke fish login shells with "Unsupported use of '='".

Probe the remote login shell once via a silent SSH exec channel before
AI/MCP PTY execution, cache session.shellKind, and leave et/mosh
shellKind unset so they can take the same path. Fish wrapper coverage
includes a real-fish binary regression test.

* fix(ai): harden remote shell kind probing

* fix(ai): keep PowerShell prompt override after posix login probe

Do not pin probed posix login shells onto session.shellKind. Unset still
defaults to posix wrapping, while leaving the kind open preserves the
live PS ...> prompt override for bash/zsh login shells that entered
pwsh interactively (Codex P2 on #2061 / issue #841 path).

* fix(ai): make remote shellKind probe cancellable before PTY write

Register a pending activePtyExecs marker for the pre-exec shell probe so
Stop during the probe window latches cancellation and does not still type
the command after the probe resolves (Codex P2 on #2061). Wire through
Catty, MCP, and worker foreground exec paths.

* fix(ai): release session lock if job shell probe rejects

terminal_start reserves the session before ensureSessionShellKind; if that
promise rejects, the previous .then-only path never released the lock.

* fix(ai): cancel pending terminal_start during shellKind probe

Register background job entries before the remote shell probe so chat
cancel can latch and abort job start before startPtyJob types into the
PTY (MCP + worker paths; Codex P2 on #2061).

* fix(ai): do not pin login shell as active; default to posix_sh

Login-shell probe no longer sets session.shellKind for fish/posix — only
powershell/cmd are pinned. Unset shellKind resolves to posix_sh (sh -c
inner POSIX wrapper), which both fish and bash interactive shells can
parse, fixing #1854 without assuming login shell === active shell
(Codex P2 on #2061).

* fix(ai): use login shell as soft hint, not sh -c / dash

Drop posix_sh default that ran commands under /bin/sh (often dash) and
broke bash/zsh source, [[ ]], and functions. Keep fish/posix probe as
session._loginShellKind soft hint for resolveEffectiveShellKind so fish
login still gets the fish wrapper and bash/zsh keep the native posix
wrapper, while live PS prompts can still override.

* test(ai): cover shell probe cancellation gaps
2026-07-09 20:56:38 +08:00

307 lines
11 KiB
JavaScript

const test = require("node:test");
const assert = require("node:assert/strict");
const { spawnSync } = require("node:child_process");
const { mkdtempSync, rmSync, realpathSync } = require("node:fs");
const { tmpdir } = require("node:os");
const { join } = require("node:path");
const {
resolveEffectiveShellKind,
execViaChannel,
} = require("./ptyExec.cjs");
const {
buildWrappedCommand,
} = require("./ptyExecHelpers.cjs");
test("uses PowerShell wrapping when a session with no confirmed shell sees a PowerShell prompt", () => {
// SSH sessions don't set shellKind (sshBridge never assigns one), which
// is exactly the issue #841 case the override targets.
assert.equal(
resolveEffectiveShellKind(undefined, "PS C:\\Users\\alice>"),
"powershell",
);
});
test("uses PowerShell wrapping when shellKind is 'unknown'", () => {
assert.equal(
resolveEffectiveShellKind("unknown", "PS C:\\Users\\alice>"),
"powershell",
);
});
test("does NOT override an explicit non-PowerShell shell kind even if the prompt looks like PowerShell", () => {
// Defends against a malicious remote process spoofing a `PS ...>` line
// on a real bash/zsh/cmd/fish/raw session to coerce a single
// mis-wrapped command.
assert.equal(
resolveEffectiveShellKind("posix", "PS C:\\Users\\alice>"),
"posix",
);
assert.equal(
resolveEffectiveShellKind("fish", "PS C:\\Users\\alice>"),
"fish",
);
assert.equal(
resolveEffectiveShellKind("cmd", "PS C:\\Users\\alice>"),
"cmd",
);
assert.equal(
resolveEffectiveShellKind("raw", "PS C:\\Users\\alice>"),
"raw",
);
});
test("keeps powershell wrapping for an explicit powershell session even when nested into a non-PS shell", () => {
// After `wsl` or similar, a confirmed PowerShell session may show a
// posix prompt. We currently keep PowerShell wrapping (the user's
// configured shell is the source of truth). Reverse detection would
// be a separate feature; this test locks the current behavior so a
// future change is intentional.
assert.equal(
resolveEffectiveShellKind("powershell", "alice@host:~$"),
"powershell",
);
assert.equal(
resolveEffectiveShellKind("powershell", ""),
"powershell",
);
});
test("recognizes a PowerShell prompt that has trailing whitespace", () => {
assert.equal(
resolveEffectiveShellKind(undefined, "PS C:\\Users\\alice> "),
"powershell",
);
});
test("recognizes a bare PowerShell prompt without a working directory", () => {
assert.equal(resolveEffectiveShellKind(undefined, "PS>"), "powershell");
});
test("recognizes PowerShell on Linux/macOS prompts (`PS /home/alice>`)", () => {
assert.equal(
resolveEffectiveShellKind(undefined, "PS /home/alice>"),
"powershell",
);
});
test("ignores ANSI-coloured PowerShell prompts when detecting the shell", () => {
assert.equal(
resolveEffectiveShellKind(undefined, "PS C:\\Users\\alice>"),
"powershell",
);
});
test("treats a CR-redrawn last line as the effective prompt, not the doubled string", () => {
// PSReadLine / ConPTY emit `\r` to repaint the current line. Without
// CR-as-newline normalization the regex would match a doubled prompt
// string that never round-trips through the live PTY tail.
assert.equal(
resolveEffectiveShellKind(undefined, "PS C:\\old>\rPS C:\\new>"),
"powershell",
);
});
test("rejects spoofed `PS >` (literal space then `>`) — default PowerShell never emits this", () => {
assert.equal(resolveEffectiveShellKind(undefined, "PS >"), "posix");
});
test("falls back to posix when neither shell kind nor prompt is informative", () => {
assert.equal(resolveEffectiveShellKind(undefined, ""), "posix");
assert.equal(resolveEffectiveShellKind(null, undefined), "posix");
});
test("does not misclassify command output that happens to contain 'PS'", () => {
assert.equal(resolveEffectiveShellKind(undefined, "PSO>"), "posix");
assert.equal(resolveEffectiveShellKind(undefined, "ZIPS>"), "posix");
});
test("loginShellHint selects fish/posix without pinning confirmed shellKind", () => {
assert.equal(
resolveEffectiveShellKind(undefined, "user@host:~$", { loginShellHint: "fish" }),
"fish",
);
assert.equal(
resolveEffectiveShellKind(undefined, "user@host:~$", { loginShellHint: "posix" }),
"posix",
);
// Live PowerShell prompt still wins over a posix/fish login hint.
assert.equal(
resolveEffectiveShellKind(undefined, "PS C:\\Users\\alice>", { loginShellHint: "posix" }),
"powershell",
);
assert.equal(
resolveEffectiveShellKind(undefined, "PS C:\\Users\\alice>", { loginShellHint: "fish" }),
"powershell",
);
// Confirmed shellKind is never overridden by a login hint.
assert.equal(
resolveEffectiveShellKind("posix", "user@host:~$", { loginShellHint: "fish" }),
"posix",
);
});
test("cmd wrapper uses interactive cmd variable expansion", () => {
const wrapped = buildWrappedCommand("ipconfig /all", "cmd", "__NCMCP_TEST__");
assert.match(wrapped, /"%__NCMCP_TEST___CMD%"/);
assert.doesNotMatch(wrapped, /"%%__NCMCP_TEST___CMD%%"/);
});
// Issue #1850: agent-generated commands run inside a subshell so that
// shell-terminating constructs (set -e + failure, exit, ...) end only the
// subshell, never the user's active login shell / SSH session.
test("posix wrapper isolates set -e failures from the active shell", () => {
const marker = "__NCMCP_TEST__";
const wrapped = buildWrappedCommand(
"set -e\ncd /nonexistent-dir-1850\necho SHOULD_NOT_PRINT",
"posix",
marker,
);
const result = spawnSync("sh", ["-c", `${wrapped}printf 'PARENT_STILL_ALIVE\\n'`], {
encoding: "utf8",
});
assert.equal(result.error, undefined);
assert.equal(result.status, 0);
assert.match(result.stdout, new RegExp(`${marker}_S`));
assert.match(result.stdout, new RegExp(`${marker}_E:[1-9]`));
assert.match(result.stdout, /PARENT_STILL_ALIVE/);
assert.doesNotMatch(result.stdout, /SHOULD_NOT_PRINT/);
});
test("posix wrapper types multi-line commands as one physical line (no PS2 leak) and preserves semantics", () => {
const marker = "__NCMCP_TEST__";
const wrapped = buildWrappedCommand(
"echo first\necho \"it's quoted\"\n\necho last",
"posix",
marker,
);
// A single physical line: the interactive shell must never show PS2
// ("> ") continuation echoes, which would leak past the preload filter.
assert.equal(wrapped.indexOf("\n"), wrapped.length - 1);
const result = spawnSync("sh", ["-c", wrapped], { encoding: "utf8" });
assert.equal(result.error, undefined);
assert.match(result.stdout, /first\n/);
assert.match(result.stdout, /it's quoted\n/);
assert.match(result.stdout, /last\n/);
assert.match(result.stdout, new RegExp(`${marker}_E:0`));
});
test("posix wrapper isolates explicit exit from the active shell and reports its code", () => {
const marker = "__NCMCP_TEST__";
const wrapped = buildWrappedCommand("exit 7", "posix", marker);
const result = spawnSync("sh", ["-c", `${wrapped}printf 'PARENT_STILL_ALIVE\\n'`], {
encoding: "utf8",
});
assert.equal(result.error, undefined);
assert.equal(result.status, 0);
assert.match(result.stdout, new RegExp(`${marker}_E:7`));
assert.match(result.stdout, /PARENT_STILL_ALIVE/);
});
test("posix wrapper keeps cd contained in the subshell (documented trade-off)", () => {
const marker = "__NCMCP_TEST__";
const cwd = realpathSync(mkdtempSync(join(tmpdir(), "netcatty-pty-cd-")));
try {
const wrapped = buildWrappedCommand("cd / && pwd", "posix", marker);
const result = spawnSync("sh", ["-c", `${wrapped}pwd`], {
encoding: "utf8",
cwd,
});
assert.equal(result.error, undefined);
assert.equal(result.status, 0);
assert.match(result.stdout, new RegExp(`${marker}_E:0`));
const lines = result.stdout.trim().split("\n");
// The command itself sees the cd take effect (pwd inside prints /)...
assert.ok(lines.includes("/"), `expected command pwd "/" in: ${result.stdout}`);
// ...but the active shell's cwd is untouched (trailing pwd prints cwd).
assert.equal(lines[lines.length - 1], cwd);
} finally {
rmSync(cwd, { recursive: true, force: true });
}
});
test("execViaChannel registers a pending-cancel marker before the SSH channel opens", () => {
// Regression for the IPC-transit race surfaced by codex on #1101
// problem 3: if `cancelPtyExecsForSession` runs while we're still
// waiting on `sshClient.exec`'s callback, the cancel finds nothing in
// `activePtyExecs` and the channel opens anyway. The fix registers a
// pending marker synchronously so the cancel has something to act on.
const track = new Map();
let execCallback;
const fakeClient = {
exec(_command, callback) {
// Capture but do not invoke yet � simulates the channel-open
// delay where the race window lives.
execCallback = callback;
},
};
void execViaChannel(fakeClient, "echo hi", {
trackForCancellation: track,
chatSessionId: "chat-1",
timeoutMs: 5_000,
});
assert.equal(track.size, 1, "pending marker should be registered before the channel opens");
const entry = Array.from(track.values())[0];
assert.equal(entry.chatSessionId, "chat-1");
assert.equal(typeof entry.cancel, "function");
// Drain the callback so the timeout the test set doesn't fire later.
execCallback(new Error("test teardown"), null);
});
test("execViaChannel drops the pending marker and resolves cleanly when sshClient.exec throws synchronously", async () => {
const track = new Map();
const fakeClient = {
exec() {
throw new Error("client destroyed");
},
};
const result = await execViaChannel(fakeClient, "echo hi", {
trackForCancellation: track,
chatSessionId: "chat-throw",
});
assert.equal(result.ok, false);
assert.equal(result.error, "client destroyed");
assert.equal(track.size, 0, "pending marker must be removed even on sync throw");
});
test("execViaChannel short-circuits when cancel fires before the SSH channel opens", async () => {
const track = new Map();
let execCallback;
const fakeClient = {
exec(_command, callback) {
execCallback = callback;
},
};
const resultPromise = execViaChannel(fakeClient, "sleep 5", {
trackForCancellation: track,
chatSessionId: "chat-2",
timeoutMs: 5_000,
});
// Cancel while still waiting for the channel-open callback.
assert.equal(track.size, 1);
for (const entry of track.values()) {
if (entry.chatSessionId === "chat-2") entry.cancel();
}
// Now the channel "opens" � even though `sshClient.exec` would
// hand us a working stream, we must short-circuit because the user
// already cancelled.
const fakeExecStream = {
closed: false,
close() { this.closed = true; },
stderr: { on() {} },
on() {},
};
execCallback(null, fakeExecStream);
const result = await resultPromise;
assert.equal(result.ok, false);
assert.equal(result.error, "Cancelled");
assert.equal(fakeExecStream.closed, true, "should close the now-unwanted stream");
assert.equal(track.size, 0, "pending marker should be removed after callback runs");
});