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(im): 暂存的更新补丁不再永久挡住 IM 连接

Lizi227dabf8a8f8
diff --git a/apps/desktop/src/main/__tests__/updateService.test.ts b/apps/desktop/src/main/__tests__/updateService.test.ts
index 2b407f067d..dfdb1b1bf9 100644
--- a/apps/desktop/src/main/__tests__/updateService.test.ts
+++ b/apps/desktop/src/main/__tests__/updateService.test.ts
@@ -403,6 +403,86 @@ describe('startup update relaunch safety', () => {
});
});
+describe('isUpdateRelaunchImminent', () => {
+ /** Boots the startup flow (staging a patch) and hands back the live module. */
+ async function bootWithStagedPatch(options: { enabled?: boolean } = {}) {
+ vi.useFakeTimers();
+ readAutoUpdateSettings.mockReturnValue({ autoRelaunchOnIdle: options.enabled ?? true });
+ fetchManifest.mockResolvedValue(updateManifest());
+ download.mockImplementation(async ({ targetPath }: { targetPath: string }) => {
+ fs.mkdirSync(path.join(TEST_USER_DATA, 'updates'), { recursive: true });
+ fs.writeFileSync(targetPath, 'update');
+ return { path: targetPath, size: 123 };
+ });
+
+ const service = await freshUpdateService('darwin');
+ service.initUpdateService();
+ const handler = ipcHandlers.get('update-check-startup');
+ if (!handler) throw new Error('update-check-startup handler not registered');
+ await handler();
+ return service;
+ }
+
+ it('is false with nothing staged', async () => {
+ const service = await freshUpdateService('darwin');
+ try {
+ expect(service.getUpdateStatus()).toBe('idle');
+ expect(service.isUpdateRelaunchImminent()).toBe(false);
+ } finally {
+ service.stopUpdateService();
+ }
+ });
+
+ // Regression: a staged patch used to read as "about to relaunch" purely from
+ // status==='ready'. With auto-relaunch off the patch sits there indefinitely,
+ // so every cold boot re-observed 'ready' and callers (startImConnection) kept
+ // deferring to a "next cold boot" that behaved identically — the FeishuBot
+ // never came online and feishuBot:save failed with [IM_NOT_READY] forever.
+ it('is false for a patch staged while auto-relaunch is off', async () => {
+ const service = await bootWithStagedPatch({ enabled: false });
+ try {
+ expect(service.getUpdateStatus()).toBe('ready');
+ expect(service.isUpdateRelaunchImminent()).toBe(false);
+ } finally {
+ service.stopUpdateService();
+ }
+ });
+
+ it('is true for a patch staged while auto-relaunch is on', async () => {
+ const service = await bootWithStagedPatch({ enabled: true });
+ try {
+ expect(service.getUpdateStatus()).toBe('ready');
+ expect(service.isUpdateRelaunchImminent()).toBe(true);
+ } finally {
+ service.stopUpdateService();
+ }
+ });
+
+ it('re-reads the auto-relaunch switch on every call', async () => {
+ const service = await bootWithStagedPatch({ enabled: true });
+ try {
+ expect(service.isUpdateRelaunchImminent()).toBe(true);
+ readAutoUpdateSettings.mockReturnValue({ autoRelaunchOnIdle: false });
+ expect(service.isUpdateRelaunchImminent()).toBe(false);
+ readAutoUpdateSettings.mockReturnValue({ autoRelaunchOnIdle: true });
+ expect(service.isUpdateRelaunchImminent()).toBe(true);
+ } finally {
+ service.stopUpdateService();
+ }
+ });
+
+ it('is false on a dev build even with a staged patch', async () => {
+ const service = await bootWithStagedPatch({ enabled: true });
+ try {
+ expect(service.getUpdateStatus()).toBe('ready');
+ isDev.mockReturnValue(true);
+ expect(service.isUpdateRelaunchImminent()).toBe(false);
+ } finally {
+ service.stopUpdateService();
+ }
+ });
+});
+
describe('splash 启动下载 0% 显式广播', () => {
interface SentIpc {
channel: string;
diff --git a/apps/desktop/src/main/im/index.ts b/apps/desktop/src/main/im/index.ts
index 53f769ce07..0c434fa531 100644
--- a/apps/desktop/src/main/im/index.ts
+++ b/apps/desktop/src/main/im/index.ts
@@ -43,8 +43,9 @@
* but the first user reply hits "localDb not ready: call ensureReady(userId)
* first" — see chat with 王韬 (group 混(派科夫)) on 2026-05-07.
* `startImConnection()` is the explicit gate; it's idempotent and a no-op
- * when the auto-update service is staging a relaunch (skip + retry on the
- * next cold boot).
+ * when the auto-update service is about to relaunch this process (skip +
+ * retry on the next cold boot). "About to relaunch" is NOT the same as "a
+ * patch is staged" — see `isUpdateRelaunchImminent()`.
*
* Credentials are independent from Cindy auth: the bot uses the user's own
* channel credentials and keeps them across logout. Runtime connectivity is
@@ -90,7 +91,7 @@ import type { ImOrchestratorConfig } from './shared/types';
import { bindingStore, executeDetach } from './binding';
import { IM_DEFAULT_EFFORT_OVERRIDES, IM_DEFAULT_SETTINGS } from '../../shared/imDefaultSettings';
import { getAuthState } from '../authManager';
-import { getUpdateStatus } from '../updateService';
+import { getUpdateStatus, isUpdateRelaunchImminent } from '../updateService';
import { createLogger } from '../logger';
import { assertTrustedAppRendererEvent } from '../security/trustedAppRenderer';
@@ -583,10 +584,17 @@ configureImAccountScope({
* effect. FeishuIM.init() is a no-op when no credentials are saved, so the bot
* stays idle until the user pastes appId / appSecret in Settings.
*
- * Skips when an update is downloading or staged for relaunch — bringing the
- * bot up just to tear it down within seconds would spam the owner with
- * online/offline notifications. The next cold boot (after the update) will
+ * Skips only when the updater is actually about to replace this process —
+ * bringing the bot up just to tear it down within seconds would spam the owner
+ * with online/offline notifications. The next cold boot (after the update) will
* connect normally.
+ *
+ * The gate MUST be `isUpdateRelaunchImminent()`, not the raw update status: a
+ * `ready` (staged) patch never relaunches on its own when the user turned
+ * auto-relaunch off, so gating on the status left this permanently skipped on
+ * every cold boot of an out-of-date install — the bot never came online and
+ * `feishuBot:save` kept failing with `[IM_NOT_READY]` (the account boundary is
+ * activated inside `im.init()`), with no way out but manually updating.
*/
export function startImConnection(): void {
if (connectionLifecycle.isStarted()) {
@@ -594,10 +602,9 @@ export function startImConnection(): void {
return;
}
- const updateStatus = getUpdateStatus();
- if (updateStatus === 'downloading' || updateStatus === 'ready') {
+ if (isUpdateRelaunchImminent()) {
log.info(
- `startImConnection: skip (updateService status=${updateStatus}); will connect on next cold boot`,
+ `startImConnection: skip (update relaunch imminent, updateService status=${getUpdateStatus()}); will connect on next cold boot`,
);
return;
}
diff --git a/apps/desktop/src/main/updateService.ts b/apps/desktop/src/main/updateService.ts
index 4e72180c3b..7c61fa6410 100644
--- a/apps/desktop/src/main/updateService.ts
+++ b/apps/desktop/src/main/updateService.ts
@@ -379,15 +379,47 @@ const PATCH_INFO_FILE = 'patch-info.json';
const UPDATE_LOCK_FILE = '.updating';
/**
- * Snapshot of the current update lifecycle state. Used by callers that need to
- * gate side-effects (e.g. don't bring the FeishuBot online if an update is
- * actively downloading or staged for relaunch — it would just go offline again
- * within seconds and spam the owner with a needless online/offline pair).
+ * Snapshot of the current update lifecycle state. Prefer
+ * `isUpdateRelaunchImminent()` for gating side-effects — a staged patch does
+ * NOT imply a relaunch is coming (see below).
*/
export function getUpdateStatus(): UpdateStatus {
return currentStatus;
}
+/**
+ * Whether this process is actually about to be replaced by the updater.
+ *
+ * Callers use this to gate side-effects that would be torn down seconds later
+ * (e.g. don't bring the FeishuBot online right before a relaunch — it would go
+ * offline again immediately and spam the owner with a needless online/offline
+ * pair).
+ *
+ * This is deliberately NOT `status === 'downloading' | 'ready'`: `ready` only
+ * means "a patch is staged", and a staged patch stays staged forever when the
+ * user turned auto-relaunch off (`getStartupRelaunchBlockReason() → 'disabled'`)
+ * or on a dev build. Gating on the raw status made every cold boot of an
+ * out-of-date install re-observe `ready` and skip the side-effect again, so the
+ * "we'll do it on the next cold boot" fallback could never fire — the IM
+ * transport stayed down permanently and `feishuBot:save` failed with
+ * `[IM_NOT_READY]` until the user manually applied the update.
+ *
+ * When auto-relaunch IS on, a `ready` patch that is currently blocked by the
+ * idle/busy policy still counts as imminent: the poller applies it once the app
+ * goes idle, and the following cold boot is up to date and ungated.
+ */
+export function isUpdateRelaunchImminent(): boolean {
+ // Already committed — the updater is spawning / windows are tearing down.
+ if (isRelaunching || autoRelaunchInProgress) return true;
+ // Nothing staged or being staged can relaunch us.
+ if (currentStatus !== 'downloading' && currentStatus !== 'ready') return false;
+ // The native updater replaces the *installed* app; it never runs in dev.
+ if (isDev()) return false;
+ // Respecting the user's switch: with auto-relaunch off the patch just sits
+ // there until they click the banner, which is not "imminent".
+ return readAutoUpdateSettings().autoRelaunchOnIdle;
+}
+
export function setUpdateAutoRelaunchBusyProbe(
probe: () => boolean | Promise<boolean>,
): void {