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(update): 未重启的失败先写进标记,写回成功后才清除

Dash39bd909d9abb
diff --git a/apps/desktop/src/main/agent-app-update/__tests__/agentAppUpdateService.test.ts b/apps/desktop/src/main/agent-app-update/__tests__/agentAppUpdateService.test.ts
index 41d5b03f0b..c5192ca74e 100644
--- a/apps/desktop/src/main/agent-app-update/__tests__/agentAppUpdateService.test.ts
+++ b/apps/desktop/src/main/agent-app-update/__tests__/agentAppUpdateService.test.ts
@@ -292,8 +292,45 @@ describe('Agent app update install', () => {
await harness.service.install(caller);
await flush();
expect(restartAllowed).toBe(false);
- expect(harness.deps.marker.clear).toHaveBeenCalledWith(ownerA);
expect(harness.deps.notify).not.toHaveBeenCalled();
+ // The failure stays in the confirming account's marker until that account is back.
+ expect(harness.deps.marker.write).toHaveBeenLastCalledWith(
+ ownerA,
+ expect.objectContaining({ failure: { errorCode: 'relaunch_cancelled' } }),
+ );
+ harness.switchOwner(ownerA);
+ await harness.service.deliverPendingResult();
+ expect(harness.deps.notify).toHaveBeenCalledOnce();
+ expect(harness.deps.notify).toHaveBeenCalledWith(
+ ownerA,
+ 'task-1',
+ expect.stringMatching(/^agent-app-update:/),
+ expect.stringContaining('update.agentInstall.reasons.notRestarted'),
+ );
+ expect(harness.getMarker()).toBeNull();
+ });
+
+ it('keeps an in-process failure until its notice is persisted', async () => {
+ const notify = vi
+ .fn<AgentAppUpdateDeps['notify']>()
+ .mockRejectedValueOnce(new Error('database busy'))
+ .mockResolvedValueOnce('written');
+ const harness = setup({
+ notify,
+ apply: vi.fn(async () => ({
+ status: 'failed' as const,
+ reason: 'x',
+ errorCode: 'download_failed',
+ })),
+ });
+ await harness.service.install(caller);
+ await flush();
+ expect(harness.getMarker()).toMatchObject({ failure: { errorCode: 'download_failed' } });
+ // Same process: a recorded failure is deliverable without a restart.
+ await harness.service.deliverPendingResult();
+ expect(notify).toHaveBeenCalledTimes(2);
+ expect(notify.mock.calls[0]![2]).toBe(notify.mock.calls[1]![2]);
+ expect(harness.getMarker()).toBeNull();
});
it('refuses to start when no account is active at confirmation', async () => {
diff --git a/apps/desktop/src/main/agent-app-update/agentAppUpdateService.ts b/apps/desktop/src/main/agent-app-update/agentAppUpdateService.ts
index 20cf01391b..56f21e78f2 100644
--- a/apps/desktop/src/main/agent-app-update/agentAppUpdateService.ts
+++ b/apps/desktop/src/main/agent-app-update/agentAppUpdateService.ts
@@ -64,6 +64,8 @@ export interface AgentAppUpdateMarker {
requestedAt: number;
/** Process that wrote the marker; a same-process read is not a restart. */
pid: number;
+ /** Set when the install ended without a restart; reportable from any process. */
+ failure?: { errorCode?: string; stagedVersion?: string };
}
type PermissionRequest = Extract<InteractionRequest, { kind: 'permission' }>;
@@ -228,30 +230,77 @@ export function createAgentAppUpdateService(deps: AgentAppUpdateDeps) {
};
};
- const reportFailure = async (
- owner: AgentAppUpdateOwner,
- sessionId: string,
- marker: AgentAppUpdateMarker,
- failure: { errorCode?: string; stagedVersion?: string },
- ) => {
- // Never write into whichever account replaced the one that confirmed.
- if (!deps.isOwnerCurrent(owner)) return;
- const reasonKey = FAILURE_REASON_KEYS[failure.errorCode ?? ''] ?? 'generic';
- const message = [
- text('update.agentInstall.failed', { version: deps.appVersion() }),
- text(`update.agentInstall.reasons.${reasonKey}`, {
- version: failure.stagedVersion ?? '',
- confirmed: marker.targetVersion ?? '',
- }),
+ const resultMessage = (marker: AgentAppUpdateMarker): string => {
+ const version = deps.appVersion();
+ if (!marker.failure && deps.compareVersions(version, marker.fromVersion) === 'newer') {
+ return text('update.agentInstall.succeeded', { version, from: marker.fromVersion });
+ }
+ const reasonKey = marker.failure
+ ? (FAILURE_REASON_KEYS[marker.failure.errorCode ?? ''] ?? 'generic')
+ : null;
+ return [
+ text('update.agentInstall.failed', { version }),
+ reasonKey
+ ? text(`update.agentInstall.reasons.${reasonKey}`, {
+ version: marker.failure?.stagedVersion ?? '',
+ confirmed: marker.targetVersion ?? '',
+ })
+ : '',
text('update.agentInstall.retryHint'),
]
.filter(Boolean)
.join(' ');
+ };
+
+ /**
+ * The only path that writes a result and clears its marker. The marker is
+ * removed only after the message is persisted or its task is gone; a
+ * transient failure (or a different active account) keeps it for the next
+ * owner-ready delivery.
+ */
+ const deliver = async (owner: AgentAppUpdateOwner, marker: AgentAppUpdateMarker) => {
+ // Never write into whichever account replaced the one that confirmed.
+ if (!deps.isOwnerCurrent(owner)) return;
+ let outcome: AgentAppUpdateNotifyOutcome;
try {
- await deps.notify(owner, sessionId, `agent-app-update:${marker.requestId}`, message);
+ outcome = await deps.notify(
+ owner,
+ marker.sessionId,
+ `agent-app-update:${marker.requestId}`,
+ resultMessage(marker),
+ );
} catch (error) {
- deps.logger?.warn?.('agent app update failure notice failed', { error: String(error) });
+ deps.logger?.warn?.('agent app update result notice failed; will retry', {
+ error: String(error),
+ });
+ return;
}
+ deps.marker.clear(owner);
+ deps.logger?.info?.('agent app update result delivered', {
+ failed: Boolean(marker.failure),
+ outcome,
+ });
+ };
+
+ /** Persist the failure into the owner's marker first, then try to deliver it. */
+ const recordFailure = async (
+ owner: AgentAppUpdateOwner,
+ marker: AgentAppUpdateMarker,
+ failure: { errorCode?: string; stagedVersion?: string },
+ ) => {
+ const failed: AgentAppUpdateMarker = {
+ ...marker,
+ failure: {
+ ...(failure.errorCode ? { errorCode: failure.errorCode } : {}),
+ ...(failure.stagedVersion ? { stagedVersion: failure.stagedVersion } : {}),
+ },
+ };
+ try {
+ deps.marker.write(owner, failed);
+ } catch (error) {
+ deps.logger?.warn?.('agent app update failure marker write failed', { error: String(error) });
+ }
+ await deliver(owner, failed);
};
const runInstall = async (
@@ -272,16 +321,14 @@ export function createAgentAppUpdateService(deps: AgentAppUpdateDeps) {
});
// A relaunch ends this process; the next start reports from the marker.
if (result.status === 'relaunching') return;
- deps.marker.clear(owner);
deps.logger?.warn?.('agent app update did not restart', {
errorCode: result.errorCode,
reason: result.reason,
});
- await reportFailure(owner, caller.sessionId, marker, result);
+ await recordFailure(owner, marker, result);
} catch (error) {
- deps.marker.clear(owner);
deps.logger?.warn?.('agent app update failed', { error: String(error) });
- await reportFailure(owner, caller.sessionId, marker, {});
+ await recordFailure(owner, marker, {});
} finally {
flow = null;
}
@@ -412,42 +459,21 @@ export function createAgentAppUpdateService(deps: AgentAppUpdateDeps) {
}
},
- /** Write the outcome of a pre-restart install back to its task. Safe to call repeatedly. */
+ /**
+ * Owner-ready delivery: a failure recorded earlier (any process) or the
+ * outcome of an install that restarted the app. Safe to call repeatedly.
+ */
async deliverPendingResult(): Promise<void> {
const owner = deps.captureOwner();
if (!owner) return;
const marker = deps.marker.read(owner);
- if (!marker || marker.pid === deps.pid) return;
+ // Without a recorded failure, a same-process marker means no restart yet.
+ if (!marker || (!marker.failure && marker.pid === deps.pid)) return;
if (deps.now() - marker.requestedAt > MARKER_MAX_AGE_MS) {
deps.marker.clear(owner);
return;
}
- const version = deps.appVersion();
- const updated = deps.compareVersions(version, marker.fromVersion) === 'newer';
- const message = updated
- ? text('update.agentInstall.succeeded', { version, from: marker.fromVersion })
- : [
- text('update.agentInstall.failed', { version }),
- text('update.agentInstall.retryHint'),
- ].join(' ');
- let outcome: AgentAppUpdateNotifyOutcome;
- try {
- outcome = await deps.notify(
- owner,
- marker.sessionId,
- `agent-app-update:${marker.requestId}`,
- message,
- );
- } catch (error) {
- // Transient failure: keep the marker; the next owner-ready start retries
- // and the clientId keeps the retry from duplicating the message.
- deps.logger?.warn?.('agent app update result notice failed; will retry', {
- error: String(error),
- });
- return;
- }
- deps.marker.clear(owner);
- deps.logger?.info?.('agent app update result delivered', { updated, outcome });
+ await deliver(owner, marker);
},
};
}
diff --git a/apps/desktop/src/main/agent-app-update/index.ts b/apps/desktop/src/main/agent-app-update/index.ts
index 812e4bb696..62b9c940c2 100644
--- a/apps/desktop/src/main/agent-app-update/index.ts
+++ b/apps/desktop/src/main/agent-app-update/index.ts
@@ -107,6 +107,18 @@ function parseMarker(raw: string | null): AgentAppUpdateMarker | null {
...(typeof value.targetVersion === 'string' ? { targetVersion: value.targetVersion } : {}),
requestedAt: value.requestedAt,
pid: value.pid,
+ ...(value.failure && typeof value.failure === 'object'
+ ? {
+ failure: {
+ ...(typeof value.failure.errorCode === 'string'
+ ? { errorCode: value.failure.errorCode }
+ : {}),
+ ...(typeof value.failure.stagedVersion === 'string'
+ ? { stagedVersion: value.failure.stagedVersion }
+ : {}),
+ },
+ }
+ : {}),
};
} catch {
return null;
diff --git a/docs/product-rules/agent-app-update.md b/docs/product-rules/agent-app-update.md
index fbafc03e83..c776c9b295 100644
--- a/docs/product-rules/agent-app-update.md
+++ b/docs/product-rules/agent-app-update.md
@@ -22,8 +22,8 @@ Agent 只能反复引导用户去点「检查更新」;那次收紧的起因
不重启。同一时刻只有一个安装请求;确认卡等待中或已开始安装时再次调用直接返回当前状态。
- 重启前在账号私有目录写一份标记;新版本启动、该账号数据库就绪后,把结果(新版本号或「仍是旧版本」)
作为一条普通助手消息写回发起任务,桌面和手机重连后都能看到。写入暂时失败时保留标记、下次就绪再写
- (按 clientId 去重);任务已不存在才丢弃。未重启就失败(下载失败、更新器没启动)
- 时立即写回失败原因。
+ (按 clientId 去重);任务已不存在才丢弃。未重启就失败时先把失败原因写进标记再写回任务,
+ 写回失败或期间切换了账号同样保留,等该账号下次就绪时补写。
- 自动更新开关写入既有 `auto-update-settings-store`(用户显式 override),与设置页同一开关。
`check_app_update` 在有可安装更新且开关未开时返回一次 `autoUpdateHint`,同一任务内只提示一次。
- 不支持的构建与安装环境(开发版、个人版、版本无关包、未移入「应用程序」、Linux 非受管安装、