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(remote): finish silent install status on upgrade preflight errors

DavidShene3b4da359284
diff --git a/apps/desktop/src/main/remote-ssh/__tests__/silentInstallStatus.test.ts b/apps/desktop/src/main/remote-ssh/__tests__/silentInstallStatus.test.ts
new file mode 100644
index 0000000000..afe073d15f
--- /dev/null
+++ b/apps/desktop/src/main/remote-ssh/__tests__/silentInstallStatus.test.ts
@@ -0,0 +1,93 @@
+import { readFileSync } from 'node:fs';
+import { resolve } from 'node:path';
+import { ScriptTarget, transpileModule } from 'typescript';
+import { describe, expect, it, vi } from 'vitest';
+import { isIpcErrorCode } from '../../../shared/ipc-errors.js';
+import { throwIpcError } from '../../utils/ipcValidate.js';
+import { prepareRemoteAgentInstall } from '../codex-install-lifecycle.js';
+
+// Execute the real orchestration without loading Electron or opening an SSH connection.
+const source = readFileSync(resolve(__dirname, '..', 'index.ts'), 'utf8');
+const start = source.indexOf('export async function ensureRemoteAgentInstalledOrInstall(');
+const end = source.indexOf('\nfunction remoteConnectionFieldsChanged', start);
+const js = transpileModule(source.slice(start, end).replace('export async', 'async'), {
+ compilerOptions: { target: ScriptTarget.ES2022 },
+}).outputText;
+
+function harness(install: (...args: any[]) => Promise<unknown>) {
+ const status = vi.fn();
+ const cache = new Map();
+ const inFlight = new Map();
+ const ensure = new Function('remoteAgentInstalledCache', 'isAgentCacheHit',
+ 'ensureRemoteAgentInstalled', 'inFlightKey', 'inFlightInstall', 'getPool',
+ 'log', 'installRemoteAgent', 'broadcastSilentInstallStatus', 'broadcastInstallProgress',
+ 'redactCredentialText', 'throwIpcError', 'isIpcErrorCode',
+ js + '; return ensureRemoteAgentInstalledOrInstall;')(
+ cache, (cached: Map<string, unknown> | undefined, kind: string) => cached?.has(kind),
+ async () => throwIpcError('SSH_AGENT_NOT_INSTALLED', 'needs upgrade'),
+ (hostId: string, kind: string) => hostId + ':' + kind, inFlight,
+ () => new Map([['builder', { getStatus: () => 'ready' }]]),
+ { info: vi.fn(), warn: vi.fn(), error: vi.fn() }, install, status, vi.fn(),
+ (line: string) => line, throwIpcError, isIpcErrorCode,
+ );
+ return { run: () => ensure('builder', 'codex'), status, cache, inFlight };
+}
+
+describe('silent installation terminal status', () => {
+ it.each(['live turn', 'daemon stop failure'])('ends a %s preflight failure and permits retry', async (reason) => {
+ const deps = {
+ isInstalled: async () => false,
+ hasLiveTurn: vi.fn(() => reason === 'live turn'),
+ stopDaemon: vi.fn(async () => ({ ok: reason !== 'daemon stop failure' })),
+ };
+ const installPackage = vi.fn(async () => ({ ready: true, installedVersion: 'managed' }));
+ const h = harness(async () => {
+ await prepareRemoteAgentInstall('codex', deps);
+ return installPackage();
+ });
+ const error = await h.run().catch((err: Error) => err);
+ expect(error).toMatchObject({ code: 'SSH_INSTALL_FAILED' });
+ expect(h.status.mock.calls.map(([event]) => event)).toEqual([
+ { hostId: 'builder', agentKind: 'codex', phase: 'started' },
+ { hostId: 'builder', agentKind: 'codex', phase: 'failed', message: error.message },
+ ]);
+ expect(installPackage).not.toHaveBeenCalled();
+ if (reason === 'live turn') expect(deps.stopDaemon).not.toHaveBeenCalled();
+ expect(h.cache.size).toBe(0);
+ expect(h.inFlight.size).toBe(0);
+
+ deps.hasLiveTurn.mockReturnValue(false);
+ deps.stopDaemon.mockResolvedValue({ ok: true });
+ h.status.mockClear();
+ await expect(h.run()).resolves.toMatchObject({ ready: true });
+ expect(h.status.mock.calls.map(([event]) => event.phase)).toEqual(['started', 'done']);
+ expect(installPackage).toHaveBeenCalledOnce();
+ expect(h.cache.get('builder').get('codex')).toEqual({ installedVersion: 'managed' });
+ expect(h.inFlight.size).toBe(0);
+ });
+
+ it('emits one failure with the diagnostic log tail for a not-ready result', async () => {
+ const h = harness(async (_host, _kind, progress) => {
+ progress({ kind: 'install-log', line: 'curl: HTTP 403' });
+ return { ready: false, error: 'download failed' };
+ });
+ await expect(h.run()).rejects.toMatchObject({
+ code: 'SSH_INSTALL_FAILED', message: '[SSH_INSTALL_FAILED] download failed\ncurl: HTTP 403',
+ });
+ expect(h.status.mock.calls.map(([event]) => event.phase)).toEqual(['started', 'progress', 'failed']);
+ expect(h.status).toHaveBeenLastCalledWith({
+ hostId: 'builder', agentKind: 'codex', phase: 'failed', message: 'download failed\ncurl: HTTP 403',
+ });
+ expect(h.inFlight.size).toBe(0);
+ });
+
+ it.each([undefined, 'STREAM_CLOSED'])('ends unexpected errors (%s) while preserving INTERNAL mapping', async (code) => {
+ const h = harness(async () => { throw Object.assign(new Error('transport closed'), { code }); });
+ await expect(h.run()).rejects.toMatchObject({ code: 'INTERNAL', message: '[INTERNAL] transport closed' });
+ expect(h.status.mock.calls.map(([event]) => event.phase)).toEqual(['started', 'failed']);
+ expect(h.status).toHaveBeenLastCalledWith({
+ hostId: 'builder', agentKind: 'codex', phase: 'failed', message: 'transport closed',
+ });
+ expect(h.inFlight.size).toBe(0);
+ });
+});
diff --git a/apps/desktop/src/main/remote-ssh/index.ts b/apps/desktop/src/main/remote-ssh/index.ts
index 6eb7793fc7..aa493bb0a6 100644
--- a/apps/desktop/src/main/remote-ssh/index.ts
+++ b/apps/desktop/src/main/remote-ssh/index.ts
@@ -550,6 +550,7 @@ export async function ensureRemoteAgentInstalledOrInstall(
// 只保留最后 6 条避免 message 太长 (toast 现已支持 whitespace-pre-wrap 多行)。
const TAIL_LIMIT = 6;
const logTail: string[] = [];
+ let failureNotified = false;
try {
const result = await installRemoteAgent(host, agentKind, (progress) => {
// 维护尾部 log 串 — 只收 install-log 行做诊断补充。error event 不收:
@@ -589,6 +590,7 @@ export async function ensureRemoteAgentInstalledOrInstall(
: baseMsg;
log.warn('silent-install: failed (not ready)', { hostId, agentKind, error: composedMsg });
broadcastSilentInstallStatus({ hostId, agentKind, phase: 'failed', message: composedMsg });
+ failureNotified = true;
throwIpcError('SSH_INSTALL_FAILED', composedMsg);
}
// 装好后标 cache, 后续 ensureRemoteAgentInstalled 短路返回。
@@ -610,12 +612,15 @@ export async function ensureRemoteAgentInstalledOrInstall(
// 不是安装失败;改写成 SSH_INSTALL_FAILED 会误导调用方走安装重试分支。
const msg = err instanceof Error ? err.message : String(err);
const code = (err as { code?: string }).code;
+ // Every started attempt needs a terminal event, including preflight IPC errors.
+ if (!failureNotified) {
+ broadcastSilentInstallStatus({ hostId, agentKind, phase: 'failed', message: msg });
+ }
if (!code || !isIpcErrorCode(code)) {
log.error('silent-install: unexpected error', { hostId, agentKind, error: msg });
- broadcastSilentInstallStatus({ hostId, agentKind, phase: 'failed', message: msg });
throwIpcError('INTERNAL', msg);
}
- // 已有白名单 code (比如上面手动 throwIpcError 走到这) — broadcast 已发, 直接 rethrow
+ // Preserve recognized IPC codes after closing the installation status.
throw err;
}
})();