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(maker-remote-ssh,desktop): 处理 copilot R3 的四条 review 意见

yan87705a1dbc88
diff --git a/apps/desktop/src/main/remote-ssh/agent-proxy.ts b/apps/desktop/src/main/remote-ssh/agent-proxy.ts
index ef4262f3bf..a480c53e26 100644
--- a/apps/desktop/src/main/remote-ssh/agent-proxy.ts
+++ b/apps/desktop/src/main/remote-ssh/agent-proxy.ts
@@ -53,6 +53,19 @@ export function clearAgentProxyTunnelState(hostId: string): void {
tunnelStates.delete(hostId);
}
+/**
+ * 断连时把隧道标记为非活跃 (review: PR #715 copilot R3): 状态只在内存,
+ * 断连后 forward 已 disarm, UI 不应继续显示「已建立」。愿望仍在 —
+ * reconnect ready 时 applyAgentProxyForHost 会重建并重新标活跃。
+ * 仅在确实有 active 记录时动作 (避免无意义 broadcast 刷屏)。
+ */
+export function markAgentProxyTunnelInactive(hostId: string): void {
+ const cur = tunnelStates.get(hostId);
+ if (!cur?.active) return;
+ tunnelStates.set(hostId, { active: false });
+ emitState(hostId);
+}
+
// broadcast 由 index.ts 注入 (避免循环依赖): 隧道状态变化后推一版
// status snapshot 给 renderer, HostSnapshotWithPrefs 会带上最新 tunnel state。
let broadcaster: ((hostId: string) => void) | null = null;
diff --git a/apps/desktop/src/main/remote-ssh/index.ts b/apps/desktop/src/main/remote-ssh/index.ts
index 9d31314915..48f3d84c98 100644
--- a/apps/desktop/src/main/remote-ssh/index.ts
+++ b/apps/desktop/src/main/remote-ssh/index.ts
@@ -71,6 +71,7 @@ import {
getAgentProxyTunnelState,
initAgentProxy,
killRemoteCodexDaemon,
+ markAgentProxyTunnelInactive,
reconcileCodexAgentProxyEnv,
type AgentProxyTunnelState,
} from './agent-proxy.js';
@@ -506,8 +507,10 @@ function normalizeAgentProxyInput(raw: unknown): SshHostAgentProxyPref | null |
const enabled = obj.enabled === true;
if (!enabled) return null;
const localHost = requireString(obj.localHost, 'agentProxy.localHost').trim();
- // localHost 会进远端 env (HTTPS_PROXY=http://<host>:<port>) — 空白和引号
- // 都会破坏 marker shell 片段, 直接拒。host:port 里的 port 单独给。
+ // localHost 是本机隧道转发目标 (net.connect 的目的地; 远端 env 永远指向
+ // 127.0.0.1:<隧道口>, 不含这个值)。空白/引号在这里虽无注入面, 但必然是
+ // 用户填错 — 直接拒, 免得隧道建起来却转到一个荒诞地址 (review: PR #715
+ // copilot R3 注释修正)。
if (!localHost || /\s/.test(localHost) || localHost.includes("'") || localHost.includes('"')) {
throwIpcError('INVALID_PARAMS', 'agentProxy.localHost must be a plain host (no whitespace/quotes), e.g. 127.0.0.1');
}
@@ -560,6 +563,11 @@ export function registerRemoteSshIpc(): void {
if (snap.status === 'ready') {
const host = getPool().get(snap.config.id);
if (host) void applyAgentProxyForHost(host);
+ } else {
+ // 断连 / 重连中: 隧道已 disarm, 状态标非活跃, detail 面板不显示
+ // 过期的「已建立」(review: PR #715 copilot R3)。reconnect ready 时
+ // 上面的 apply 会重建并重新标活跃。
+ markAgentProxyTunnelInactive(snap.config.id);
}
});
// agent-proxy 内部状态 (隧道成败 / 端口) 变化 → 推一版最新 snapshot,
diff --git a/packages/maker-remote-ssh/src/RemoteHost.ts b/packages/maker-remote-ssh/src/RemoteHost.ts
index a48a833199..ae7a3d036e 100644
--- a/packages/maker-remote-ssh/src/RemoteHost.ts
+++ b/packages/maker-remote-ssh/src/RemoteHost.ts
@@ -133,6 +133,9 @@ interface ForwardRecord {
/** 本地 Proxy 连接失败的节流日志状态。 */
lastLocalErrorAt: number;
localErrorCount: number;
+ /** 上次实际打日志时的 localErrorCount — suppressedCount 报「距上次日志被吞掉
+ * 几条」而不是单调总计数 (review: PR #715 copilot R3)。 */
+ lastLocalErrorLoggedCount: number;
}
/**
@@ -539,6 +542,7 @@ export class RemoteHost {
armed: false,
lastLocalErrorAt: 0,
localErrorCount: 0,
+ lastLocalErrorLoggedCount: 0,
};
this.forwards.set(key, record);
if (this.status === 'ready') {
@@ -839,13 +843,18 @@ export class RemoteHost {
const now = Date.now();
if (now - rec.lastLocalErrorAt > 30_000) {
rec.lastLocalErrorAt = now;
+ const suppressed = rec.localErrorCount - rec.lastLocalErrorLoggedCount;
+ rec.lastLocalErrorLoggedCount = rec.localErrorCount;
this.log.warn('ssh remote forward: local target unreachable', {
id: this.id,
localTarget: forwardKey(rec.spec),
error: err.message,
- suppressedCount: rec.localErrorCount,
+ suppressedCount: suppressed,
});
}
+ // error 后 socket 必须销毁 (review: PR #715 copilot R3) — 只关 channel
+ // 不 destroy 会把本地侧留成半开, Proxy 进程上积 CLOSE_WAIT。
+ sock.destroy();
try { channel.close(); } catch { /* already gone */ }
});
channel.on('error', () => {
@@ -1134,6 +1143,10 @@ export class RemoteHost {
// (StaleForwardArmError), 不会误标 armed。
record.arming = undefined;
}
+ // 分发器挂在旧 client 上 — 清掉引用, 否则 RemoteHost 长期持有死 client
+ // (抑制 GC + 多次重连后旧 client 上 listener 堆积, review: PR #715
+ // copilot R3)。新 client 首次 arm 时 attachForwardListener 会重新挂。
+ this.forwardListenerClient = null;
}
private setStatus(next: RemoteStatus): void {
diff --git a/packages/maker-remote-ssh/src/__tests__/remoteHostForward.test.ts b/packages/maker-remote-ssh/src/__tests__/remoteHostForward.test.ts
index 6ce6455e4e..f5374ad289 100644
--- a/packages/maker-remote-ssh/src/__tests__/remoteHostForward.test.ts
+++ b/packages/maker-remote-ssh/src/__tests__/remoteHostForward.test.ts
@@ -382,6 +382,24 @@ describe('RemoteHost remote forwarding', () => {
expect(host.listRemoteForwards()[0]?.armed).toBe(true);
});
+ it('clears the forward listener client on disconnect; a new client re-attaches (review R3)', async () => {
+ // forwardListenerClient 不清会让 RemoteHost 长期持有死 client (抑制 GC +
+ // 多次重连后旧 client listener 堆积); 断连后必须置空, 新连接 arm 时重挂。
+ const client1 = new FakeClient();
+ const host = makeReadyHost(client1);
+ await host.ensureRemoteForward({ localHost: '127.0.0.1', localPort: 7890 });
+ expect(client1.listenerCount('tcp connection')).toBe(1);
+
+ (host as unknown as { markForwardsDisarmed(): void }).markForwardsDisarmed();
+ expect((host as unknown as { forwardListenerClient: unknown }).forwardListenerClient).toBeNull();
+
+ const client2 = new FakeClient();
+ (host as unknown as { client: unknown }).client = client2;
+ await (host as unknown as { rearmForwards(): Promise<void> }).rearmForwards();
+ expect(client2.listenerCount('tcp connection')).toBe(1);
+ expect((host as unknown as { forwardListenerClient: unknown }).forwardListenerClient).toBe(client2);
+ });
+
it('keeps the wish when re-arm fails, without throwing (logged only)', async () => {
const client1 = new FakeClient();
const host = makeReadyHost(client1);