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(desktop,maker-remote-ssh): 处理 R7 review 三条 + 收官审查 P2

yanc973abe92c99
diff --git a/apps/desktop/src/main/remote-ssh/__tests__/agentProxy.test.ts b/apps/desktop/src/main/remote-ssh/__tests__/agentProxy.test.ts
index cd0766cdc0..3486004226 100644
--- a/apps/desktop/src/main/remote-ssh/__tests__/agentProxy.test.ts
+++ b/apps/desktop/src/main/remote-ssh/__tests__/agentProxy.test.ts
@@ -216,6 +216,36 @@ describe('reconcileCodexAgentProxyEnv', () => {
expect(result).toEqual({ markerChanged: false, daemonRestarted: false });
expect(state.pkillCount).toBe(0);
});
+
+ it('fails closed when the marker write fails: throws and does not kill the daemon (codex R7 P1)', async () => {
+ // exec 对非零退出码 resolve 不 throw — 写失败必须显式挡, 否则 reconcile
+ // 继续 pkill, daemon 起来没 marker 直连, fail-closed 破。
+ setSshHostAgentProxy('test-host', PREF);
+ const { host, state } = makeFakeHost({ marker: null });
+ const baseExec = host.exec.bind(host);
+ host.exec = async (cmd: string, execOpts?: { input?: string }) => {
+ if (cmd.includes('cat > "') && cmd.includes('agent-proxy.env')) {
+ return { stdout: '', stderr: 'Permission denied', exitCode: 1, signal: null };
+ }
+ return baseExec(cmd, execOpts);
+ };
+ await expect(reconcileCodexAgentProxyEnv(host)).rejects.toThrow(/write agent-proxy marker failed/);
+ expect(state.pkillCount).toBe(0);
+ });
+
+ it('fails closed when the marker delete fails: throws and does not kill the daemon (codex R7 P1)', async () => {
+ setSshHostAgentProxy('test-host', null);
+ const { host, state } = makeFakeHost({ marker: "export HTTPS_PROXY='http://127.0.0.1:17893'" });
+ const baseExec = host.exec.bind(host);
+ host.exec = async (cmd: string, execOpts?: { input?: string }) => {
+ if (cmd.includes('rm -f "') && cmd.includes('agent-proxy.env')) {
+ return { stdout: '', stderr: 'Read-only file system', exitCode: 1, signal: null };
+ }
+ return baseExec(cmd, execOpts);
+ };
+ await expect(reconcileCodexAgentProxyEnv(host)).rejects.toThrow(/delete agent-proxy marker failed/);
+ expect(state.pkillCount).toBe(0);
+ });
});
describe('killRemoteCodexDaemon', () => {
diff --git a/apps/desktop/src/main/remote-ssh/agent-proxy.ts b/apps/desktop/src/main/remote-ssh/agent-proxy.ts
index 4b9d13f48c..eba33f1428 100644
--- a/apps/desktop/src/main/remote-ssh/agent-proxy.ts
+++ b/apps/desktop/src/main/remote-ssh/agent-proxy.ts
@@ -207,19 +207,32 @@ async function readRemoteMarker(host: RemoteHost): Promise<string | null> {
async function writeRemoteMarker(host: RemoteHost, content: string | null): Promise<void> {
if (content == null) {
- await host.exec(`bash -c 'rm -f "${REMOTE_AGENT_PROXY_ENV_PATH}"'`, {
+ const result = await host.exec(`bash -c 'rm -f "${REMOTE_AGENT_PROXY_ENV_PATH}"'`, {
timeoutMs: 10_000,
label: 'delete-agent-proxy-marker',
});
+ if (result.exitCode !== 0) {
+ throw new Error(
+ `delete agent-proxy marker failed (exit ${result.exitCode}): ${result.stderr.trim().slice(0, 200)}`,
+ );
+ }
return;
}
// mkdir -p 兜底: install root 通常在 codex 安装时已建, 但 proxy-only
// 场景 (只开了 claude 没装 codex) 目录可能还不存在。内容走 stdin,
// 不进 cmd (与 repo 的 secret-hygiene 惯例一致, 虽然 marker 本身无密钥)。
- await host.exec(
+ const result = await host.exec(
`bash -c 'mkdir -p "${REMOTE_INSTALL_ROOT}" && cat > "${REMOTE_AGENT_PROXY_ENV_PATH}"'`,
{ timeoutMs: 10_000, label: 'write-agent-proxy-marker', input: content },
);
+ // exec 对非零退出码 resolve 不 throw — 写失败必须显式挡 (review: PR #715
+ // codex R7 P1): 否则 reconcile 会继续 pkill, daemon 起来没 marker 直连,
+ // fail-closed 破。throw 由调用方收口 (apply 落 tunnel state / probe 中断)。
+ if (result.exitCode !== 0) {
+ throw new Error(
+ `write agent-proxy marker failed (exit ${result.exitCode}): ${result.stderr.trim().slice(0, 200)}`,
+ );
+ }
}
/**
diff --git a/apps/desktop/src/main/remote-ssh/index.ts b/apps/desktop/src/main/remote-ssh/index.ts
index 48f3d84c98..f88b369ace 100644
--- a/apps/desktop/src/main/remote-ssh/index.ts
+++ b/apps/desktop/src/main/remote-ssh/index.ts
@@ -559,15 +559,20 @@ export function registerRemoteSshIpc(): void {
// 所有路径 (手动 connect / autoConnect / 断线重连) 的统一点, 失败只落
// tunnel state 不阻断连接, session 路径会显式重试。
getPool().onAnyStatus((snap) => {
- broadcastStatus(snap);
if (snap.status === 'ready') {
+ broadcastStatus(snap);
const host = getPool().get(snap.config.id);
if (host) void applyAgentProxyForHost(host);
} else {
// 断连 / 重连中: 隧道已 disarm, 状态标非活跃, detail 面板不显示
// 过期的「已建立」(review: PR #715 copilot R3)。reconnect ready 时
// 上面的 apply 会重建并重新标活跃。
+ // 先标非活跃再广播 (mark 自带一次 emitState 推送): 事件流里不出现
+ // status≠ready 但 tunnel.active=true 的 stale 帧, renderer 不会
+ // 闪一帧「已建立」再翻「等待连接」(review: PR #715 收官审查 P2)。
markAgentProxyTunnelInactive(snap.config.id);
+ const host = getPool().get(snap.config.id);
+ broadcastStatus(host ? host.snapshot() : snap);
}
});
// agent-proxy 内部状态 (隧道成败 / 端口) 变化 → 推一版最新 snapshot,
diff --git a/apps/desktop/src/renderer/components/settings/RemoteHostDetail.tsx b/apps/desktop/src/renderer/components/settings/RemoteHostDetail.tsx
index f7303537d3..f9838a3d51 100644
--- a/apps/desktop/src/renderer/components/settings/RemoteHostDetail.tsx
+++ b/apps/desktop/src/renderer/components/settings/RemoteHostDetail.tsx
@@ -262,8 +262,11 @@ function AgentProxyTunnelCard({ hostId }: { hostId: string }) {
() => remoteSshHostsStore.get()?.find((h) => h.config.id === hostId) ?? null,
);
// 触发一次 ensure, 保证 cold start (没开过 Settings 列表) 也有快照。
+ // ensure() 可能因 IPC 失败 reject — 裸 void 会把 rejection 抛成 renderer
+ // unhandled rejection; 与 useRemoteSshHosts hook 同款 catch 守护
+ // (review: PR #715 copilot R7)。
useEffect(() => {
- void remoteSshHostsStore.ensure();
+ void remoteSshHostsStore.ensure().catch(() => {});
}, []);
const pref = snap?.agentProxy ?? null;
@@ -274,7 +277,15 @@ function AgentProxyTunnelCard({ hostId }: { hostId: string }) {
let icon;
let text: string;
let isError = false;
- if (tunnel?.active && tunnel.remotePort != null) {
+ if (snap?.status !== 'ready') {
+ // 主机断连/重连中: 隧道已 disarm 或正在拆除 — 即使隧道状态快照还没翻到
+ // 非活跃 (断连帧先于 mark-inactive 帧到达), 也优先显示「等待连接」,
+ // 不渲染过期的「已建立」(review: PR #715 收官审查 P2)。断连时
+ // markAgentProxyTunnelInactive 会清掉 lastError, 错误分支只在 ready
+ // 状态 (连接正常但建隧道失败) 下有语义。
+ icon = <AlertCircle size={14} />;
+ text = t('settings.remote.detail.agentProxyWaiting', { target: localTarget });
+ } else if (tunnel?.active && tunnel.remotePort != null) {
icon = <CheckCircle2 size={14} />;
text = t('settings.remote.detail.agentProxyActive', {
port: tunnel.remotePort,
@@ -284,11 +295,6 @@ function AgentProxyTunnelCard({ hostId }: { hostId: string }) {
icon = <AlertCircle size={14} />;
text = t('settings.remote.detail.agentProxyError', { message: tunnel.lastError });
isError = true;
- } else if (snap?.status !== 'ready') {
- // 主机断连时隧道已 disarm 且没有在途的建立动作 — 不能渲染
- // 「建立中」spinner (误导, review: PR #715 greptile R4)。
- icon = <AlertCircle size={14} />;
- text = t('settings.remote.detail.agentProxyWaiting', { target: localTarget });
} else {
icon = <Spinner size={12} />;
text = t('settings.remote.detail.agentProxyPending', { target: localTarget });
diff --git a/packages/maker-remote-ssh/src/RemoteHost.ts b/packages/maker-remote-ssh/src/RemoteHost.ts
index ac33c59116..d93ce8521d 100644
--- a/packages/maker-remote-ssh/src/RemoteHost.ts
+++ b/packages/maker-remote-ssh/src/RemoteHost.ts
@@ -526,6 +526,16 @@ export class RemoteHost {
if (!Number.isInteger(spec.localPort) || spec.localPort < 1 || spec.localPort > 65535) {
throw new Error(`ensureRemoteForward: invalid localPort ${spec.localPort}`);
}
+ // preferredRemotePort 同样入口校验 (review: PR #715 copilot R7): 0 会静默
+ // 变成「远端绑 ephemeral 端口」语义, 越界值则晚到 arm 才失败, 都难排查。
+ if (
+ spec.preferredRemotePort !== undefined &&
+ (!Number.isInteger(spec.preferredRemotePort) ||
+ spec.preferredRemotePort < 1 ||
+ spec.preferredRemotePort > 65535)
+ ) {
+ throw new Error(`ensureRemoteForward: invalid preferredRemotePort ${spec.preferredRemotePort}`);
+ }
const key = forwardKey(spec);
const existing = this.forwards.get(key);
if (existing) {
diff --git a/packages/maker-remote-ssh/src/__tests__/remoteHostForward.test.ts b/packages/maker-remote-ssh/src/__tests__/remoteHostForward.test.ts
index f5374ad289..ad5d881189 100644
--- a/packages/maker-remote-ssh/src/__tests__/remoteHostForward.test.ts
+++ b/packages/maker-remote-ssh/src/__tests__/remoteHostForward.test.ts
@@ -185,6 +185,14 @@ describe('RemoteHost remote forwarding', () => {
await expect(
host.ensureRemoteForward({ localHost: '127.0.0.1', localPort: 0 }),
).rejects.toThrow(/localPort/);
+ // preferredRemotePort 同样入口校验: 0 会静默变成远端 ephemeral 绑端口语义
+ // (review: PR #715 copilot R7)。
+ await expect(
+ host.ensureRemoteForward({ localHost: '127.0.0.1', localPort: 7890, preferredRemotePort: 0 }),
+ ).rejects.toThrow(/preferredRemotePort/);
+ await expect(
+ host.ensureRemoteForward({ localHost: '127.0.0.1', localPort: 7890, preferredRemotePort: 70000 }),
+ ).rejects.toThrow(/preferredRemotePort/);
});
it('is idempotent for the same local target (no duplicate forwardIn)', async () => {