Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(dev): macOS 开发版启动命令过长时改用临时脚本,避免被 tty 截断 (#5319)

Dash90ad92cd3d14
diff --git a/scripts/__tests__/restart-desktop-remote.test.mjs b/scripts/__tests__/restart-desktop-remote.test.mjs
index a39a5e7a3e..ea0865eb26 100644
--- a/scripts/__tests__/restart-desktop-remote.test.mjs
+++ b/scripts/__tests__/restart-desktop-remote.test.mjs
@@ -32,6 +32,9 @@ import {
readDesktopStartupStatus,
parseWorktreePaths,
osascriptLaunchDarwinTerminalArgs,
+ writeDarwinTerminalLaunchScript,
+ darwinTerminalSourceCommand,
+ prepareDarwinTerminalLaunch,
waitForDesktopStartup,
shouldRefuseHostedRestart,
commandContainsPath,
@@ -95,6 +98,42 @@ test("macOS Terminal launch runs command before activating Terminal", () => {
);
});
+test("macOS Terminal launch sends a short source command for long dev commands", {
+ skip: process.platform === "win32",
+}, () => {
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), "cindy-terminal-launch-"));
+ const marker = path.join(dir, "ran.txt");
+ // 超过 tty canonical 行上限 1024 字节,模拟长 worktree 路径 + 大量 unset 的真实命令。
+ const command = `PADDING='${"x".repeat(1600)}'; echo "ok $PADDING" > '${marker}'`;
+ const scriptPath = writeDarwinTerminalLaunchScript(command, dir);
+ const sourceCommand = darwinTerminalSourceCommand(scriptPath);
+
+ assert.ok(sourceCommand.length < 1024, "Terminal input must stay under MAX_CANON");
+ assert.equal(fs.statSync(scriptPath).mode & 0o777, 0o600);
+
+ const result = spawnSync("/bin/sh", ["-c", sourceCommand], { encoding: "utf8" });
+ assert.equal(result.status, 0, result.stderr);
+ assert.equal(fs.readFileSync(marker, "utf8").trim(), `ok ${"x".repeat(1600)}`);
+ assert.equal(fs.existsSync(scriptPath), false, "launch script removes itself once sourced");
+ fs.rmSync(dir, { recursive: true, force: true });
+});
+
+test("macOS Terminal launch passes only the short source command to do script", {
+ skip: process.platform === "win32",
+}, () => {
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), "cindy-terminal-args-"));
+ const command = `echo '${"y".repeat(1600)}'`;
+ const { scriptPath, args } = prepareDarwinTerminalLaunch(command, dir);
+ const devCommand = args.at(-1);
+
+ assert.ok(appleScriptLines(args).includes("set targetTab to do script devCommand"));
+ assert.equal(devCommand, darwinTerminalSourceCommand(scriptPath));
+ assert.ok(devCommand.length < 1024, "Terminal input must stay under MAX_CANON");
+ assert.ok(!args.some((arg) => arg.includes("y".repeat(1600))), "long command must not reach do script");
+ assert.ok(fs.readFileSync(scriptPath, "utf8").includes(command));
+ fs.rmSync(dir, { recursive: true, force: true });
+});
+
test("desktop restart no longer depends on the retired Feishu build app id", () => {
const source = fs.readFileSync(
new URL("../restart-desktop-remote.mjs", import.meta.url),
diff --git a/scripts/restart-desktop-remote.mjs b/scripts/restart-desktop-remote.mjs
index e8794e3789..73ca773fe8 100644
--- a/scripts/restart-desktop-remote.mjs
+++ b/scripts/restart-desktop-remote.mjs
@@ -462,6 +462,30 @@ function shellSingleQuote(value) {
return `'${value.replaceAll("'", "'\\''")}'`;
}
+// Terminal 的 `do script` 把命令当键盘输入送进新标签页的 tty;新窗口的 shell 还没接管
+// 输入时 tty 处于 canonical 模式,单行超过 MAX_CANON(1024 字节)会被截断,整条命令
+// 静默不执行(worktree 路径长、需清除的环境变量多时实测 ~1600 字节)。完整命令先写进
+// 临时脚本,Terminal 只收一条短的 source 命令;脚本第一行删掉自身,不留残留文件。
+export function writeDarwinTerminalLaunchScript(command, dir = os.tmpdir()) {
+ const scriptPath = path.join(dir, `cindy-desktop-dev-${randomBytes(6).toString('hex')}.sh`);
+ fs.writeFileSync(scriptPath, `rm -f -- ${shellSingleQuote(scriptPath)}\n${command}\n`, {
+ mode: 0o600,
+ });
+ return scriptPath;
+}
+
+export function darwinTerminalSourceCommand(scriptPath) {
+ return `. ${shellSingleQuote(scriptPath)}`;
+}
+
+export function prepareDarwinTerminalLaunch(command, dir = os.tmpdir()) {
+ const scriptPath = writeDarwinTerminalLaunchScript(command, dir);
+ return {
+ scriptPath,
+ args: osascriptLaunchDarwinTerminalArgs(darwinTerminalSourceCommand(scriptPath)),
+ };
+}
+
function osascriptCloseDarwinTerminalTtyArgs(ttyPath) {
return closeDarwinTerminalTtyScript
.flatMap((line) => ['-e', line])
@@ -961,12 +985,27 @@ function launchInSystemTerminal(mode) {
if (process.platform === 'darwin') {
const command = `${darwinEnvPrefix()}${darwinStaleDevEnvUnset()}cd ${shellSingleQuote(rootDir)} && ${devEnvPrefix()}${packageManagerCommand(mode)}; exitCode=$?; exit $exitCode`;
- const child = spawn('osascript', osascriptLaunchDarwinTerminalArgs(command), {
+ const { scriptPath, args } = prepareDarwinTerminalLaunch(command);
+ // osascript 在 do script 之前失败(自动化权限被拒、脚本错误)时临时脚本不会被 source,
+ // 首行自删不会发生;按失败退出尽力删除,避免留下含开发环境变量的文件。
+ const removeScript = () => {
+ try {
+ fs.rmSync(scriptPath, { force: true });
+ } catch {
+ // 尽力清理,失败不影响启动结果判断。
+ }
+ };
+ const child = spawn('osascript', args, {
detached: true,
stdio: 'ignore',
});
+ child.on('error', removeScript);
+ child.on('exit', (code) => {
+ if (code !== 0) removeScript();
+ });
child.unref();
if (child.pid === undefined) {
+ removeScript();
throw new Error('Failed to open Terminal.app');
}
console.log(`==> Opened desktop ${mode} dev in a new Terminal window.`);