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(ssh): 收敛 ssh_add_host / ssh_reload_config 的边界处理

Dashf5a08258aa44
diff --git a/apps/desktop/src/main/remote-ssh/index.ts b/apps/desktop/src/main/remote-ssh/index.ts
index 0d37d5a342..3d51f21b2e 100644
--- a/apps/desktop/src/main/remote-ssh/index.ts
+++ b/apps/desktop/src/main/remote-ssh/index.ts
@@ -1227,7 +1227,6 @@ export async function reloadRemoteSshConfig(): Promise<ReturnType<typeof current
} catch (err) {
log.warn('failed to reload SSH config', { error: String(err) });
}
- for (const host of getPool().list()) broadcastStatus(host);
return currentRemoteSshListResult();
}
diff --git a/packages/lizi-mcps/src/__tests__/cindy_sshMcpServer.test.ts b/packages/lizi-mcps/src/__tests__/cindy_sshMcpServer.test.ts
index 9c6380e646..5713d32bed 100644
--- a/packages/lizi-mcps/src/__tests__/cindy_sshMcpServer.test.ts
+++ b/packages/lizi-mcps/src/__tests__/cindy_sshMcpServer.test.ts
@@ -570,6 +570,38 @@ describe('ssh_add_host', () => {
expect(connectError.hint).toContain('ssh-copy-id');
});
+ it('trims the alias once, so the pool lookup and connect use the saved name', async () => {
+ const { deps, added, ensureReadyCalls } = makeAddDeps();
+ const { payload } = await callParsed(makeRegistry(deps), 'ssh_add_host', {
+ alias: ' nas ',
+ hostname: '192.168.68.51',
+ user: 'dash',
+ });
+ expect(added).toEqual([{ id: 'nas', hostname: '192.168.68.51', user: 'dash' }]);
+ expect(ensureReadyCalls).toEqual(['nas']);
+ expect(payload).toMatchObject({ added: true, connected: true });
+ });
+
+ it('treats a failed display-name save as an added host, not a failed add', async () => {
+ const hosts: SshHostSnapshotLike[] = [];
+ const { pool } = makeFakePool(hosts);
+ const { deps, ensureReadyCalls } = makeDeps(pool, {
+ addHost: async (input) => {
+ hosts.push(snapshot({ id: input.id, status: 'disconnected' }));
+ throw new Error('[SSH_HOST_PREFS_WRITE_FAILED] local preferences were not saved');
+ },
+ });
+ const { payload, isError } = await callParsed(makeRegistry(deps), 'ssh_add_host', {
+ alias: 'nas',
+ hostname: '192.168.68.51',
+ user: 'dash',
+ });
+ expect(isError).toBe(false);
+ expect(payload).toMatchObject({ added: true, connected: true });
+ expect(String(payload.warning)).toContain('无需重试');
+ expect(ensureReadyCalls).toEqual(['nas']);
+ });
+
it('maps an existing alias to HOST_ALREADY_EXISTS without connecting', async () => {
const { deps, ensureReadyCalls } = makeAddDeps({
addHost: async () => {
diff --git a/packages/lizi-mcps/src/ssh/add_host.ts b/packages/lizi-mcps/src/ssh/add_host.ts
index ad2d0640a3..41af551bcf 100644
--- a/packages/lizi-mcps/src/ssh/add_host.ts
+++ b/packages/lizi-mcps/src/ssh/add_host.ts
@@ -32,9 +32,9 @@ export function registerSshAddHostTool(
'认证只支持 ssh-agent(默认)或未加密的私钥文件,Cindy 不保存密码。' +
PASSWORD_LOGIN_HINT,
inputShape: {
- alias: z.string().min(1).describe('主机别名(SSH Host 名),不能含空格或通配符,例如 "nas"'),
- hostname: z.string().min(1).describe('IP 或域名'),
- user: z.string().min(1).describe('登录用户名'),
+ alias: z.string().trim().min(1).describe('主机别名(SSH Host 名),不能含空格或通配符,例如 "nas"'),
+ hostname: z.string().trim().min(1).describe('IP 或域名'),
+ user: z.string().trim().min(1).describe('登录用户名'),
port: z.number().int().min(1).max(65535).optional().describe('端口,默认 22'),
authMethod: z
.enum(['agent', 'key'])
@@ -48,6 +48,8 @@ export function registerSshAddHostTool(
displayName: z.string().min(1).optional().describe('设置页显示的名称,默认同 alias'),
},
handler: async ({ alias, hostname, user, port, authMethod, identityFile, displayName }) => {
+ // 主机已写入、仅本地显示名/Agent Proxy 偏好没存上:主机可用,按已添加处理,不能让 Agent 重试。
+ let prefsWarning: string | undefined;
try {
await deps.addHost({
id: alias,
@@ -66,6 +68,9 @@ export function registerSshAddHostTool(
return errorPayload('HOST_ALREADY_EXISTS', `别名 "${alias}" 已存在。用 ssh_list_hosts 查看,或换一个别名。`);
case 'INVALID_PARAMS':
return errorPayload('INVALID_ARGS', `参数不合法:${detail}`);
+ case 'SSH_HOST_PREFS_WRITE_FAILED':
+ prefsWarning = '主机已添加,但显示名未能保存(检查本地存储权限);无需重试添加。';
+ break;
case 'SSH_CONFIG_RELOAD_REQUIRED':
return errorPayload(
'SSH_CONFIG_RELOAD_REQUIRED',
@@ -75,6 +80,7 @@ export function registerSshAddHostTool(
return errorPayload('SSH_CONFIG_WRITE_FAILED', `添加主机失败:${detail}`);
}
}
+ const extra = prefsWarning ? { warning: prefsWarning } : {};
let pool;
try {
@@ -89,12 +95,13 @@ export function registerSshAddHostTool(
}
try {
await deps.ensureReady(alias);
- return okPayload({ added: true, connected: true, host: hostBrief(added() ?? before, deps) });
+ return okPayload({ added: true, connected: true, host: hostBrief(added() ?? before, deps), ...extra });
} catch (err) {
// 主机已保存(与设置页一致:连不上也保留),把试连结果交给 Agent 决定下一步。
const classified = classifySshError(err, deps, added() ?? before);
return okPayload({
added: true,
+ ...extra,
connected: false,
host: hostBrief(added() ?? before, deps),
connectError: { errorCode: classified.errorCode, hint: classified.hint },
diff --git a/packages/lizi-mcps/src/ssh/reload_config.ts b/packages/lizi-mcps/src/ssh/reload_config.ts
index 163d1b0fa5..a650cc8218 100644
--- a/packages/lizi-mcps/src/ssh/reload_config.ts
+++ b/packages/lizi-mcps/src/ssh/reload_config.ts
@@ -18,7 +18,7 @@ export function registerSshReloadConfigTool(
category: 'ssh',
description:
'重新读取这台电脑的 ~/.ssh/config(含 Include),刷新 Cindy 的 SSH 主机列表,等同设置页的刷新按钮。' +
- '手动编辑过 SSH 配置后调用;返回刷新后的主机列表。',
+ '手动编辑过 SSH 配置后调用;返回刷新后的主机列表。已打开的设置页不会自动更新,需用户点刷新按钮。',
inputShape: {},
handler: async () => {
try {