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(wechat): guard companion changes while polling is disabled

Chris1a5a84b125b7
diff --git a/apps/desktop/src/main/im/wechat/WechatIM.ts b/apps/desktop/src/main/im/wechat/WechatIM.ts
index 87b5f7e03b..0b4c69e623 100644
--- a/apps/desktop/src/main/im/wechat/WechatIM.ts
+++ b/apps/desktop/src/main/im/wechat/WechatIM.ts
@@ -47,7 +47,7 @@ import {
resolvePermissionMode,
} from '../shared/permissionModeControl';
import { ui } from './uiText';
-import { WechatTaskStore, type WechatActiveBinding, type WechatTask } from './taskStore';
+import { countQueuedWechatTasks, WechatTaskStore, type WechatActiveBinding, type WechatTask } from './taskStore';
import type { DbClient } from '../../localDb/client/DbClient';
import {
removeUncommittedWechatFiles,
@@ -206,26 +206,26 @@ export class WechatIM extends BaseIM implements RichChannelIM {
throw new Error('WECHAT_CONNECTION_BUSY');
}
const epoch = this.#epoch;
- if (!cancelPending && epoch && await this.#requireStore().countQueuedTasks(epoch.binding.bindingEpoch)) {
+ const db = this.#deps.getDbClient();
+ const binding = epoch?.binding ?? await this.#readActiveBindingWithoutKey(db);
+ if (!cancelPending && binding && await countQueuedWechatTasks(db, binding.bindingEpoch)) {
throw new Error('WECHAT_CONNECTION_BUSY');
}
if (!cancelPending && this.#activeTasks.size) throw new Error('WECHAT_CONNECTION_BUSY');
const revision = this.#compatibilityRevision;
- const db = cancelPending ? this.#deps.getDbClient() : null;
await this.#stopEpoch();
let updated = false;
try {
- if (cancelPending && db) {
+ if (cancelPending) {
if (!options.cancelPendingIf?.()) throw new Error('WECHAT_BINDING_CHANGED');
// Deletion must also settle a disabled/offline connection's durable inbox.
- const binding = epoch?.binding ?? await this.#readActiveBindingWithoutKey(db);
if (binding) await db.tx('wechatStopAll', {
bindingEpoch: binding.bindingEpoch, now: this.#now(),
errorCode: 'COMPANION_DELETED', cancelPending: true,
});
}
// A poll already in flight may have persisted a message while we drained it.
- if (!cancelPending && epoch && await this.#requireStore().countQueuedTasks(epoch.binding.bindingEpoch)) {
+ if (!cancelPending && binding && await countQueuedWechatTasks(db, binding.bindingEpoch)) {
throw new Error('WECHAT_CONNECTION_BUSY');
}
update();
diff --git a/apps/desktop/src/main/im/wechat/__tests__/WechatIM.test.ts b/apps/desktop/src/main/im/wechat/__tests__/WechatIM.test.ts
index d8ccbf86d9..ece5d4654f 100644
--- a/apps/desktop/src/main/im/wechat/__tests__/WechatIM.test.ts
+++ b/apps/desktop/src/main/im/wechat/__tests__/WechatIM.test.ts
@@ -560,6 +560,27 @@ describe('WechatIM host boundary', () => {
} finally { await im.dispose(); }
});
+ // #6492 unit regression: disabling polling must not allow persisted input to
+ // switch owners, including a cold start where no credential-backed store exists.
+ it.each([false, true])('protects the policy-disabled inbox (cold start=%s)', async (cold) => {
+ let queued = 1;
+ const { im, transport, testHost } = restoredConnection(() => queued, cold);
+ await im.init();
+ if (!cold) await im.setCompatibilityDisabled(true);
+ try {
+ const update = vi.fn();
+ await expect(im.updateCompanionBinding(update)).rejects.toThrow('WECHAT_CONNECTION_BUSY');
+ expect(update).not.toHaveBeenCalled();
+ queued = 0;
+ await im.updateCompanionBinding(update);
+ expect(update).toHaveBeenCalledOnce();
+ expect(im.getState().phase).toBe('disabled_by_policy');
+ expect(transport.notifyStart).toHaveBeenCalledTimes(cold ? 0 : 1);
+ expect(testHost.secrets.write).not.toHaveBeenCalled();
+ expect(testHost.secrets.remove).not.toHaveBeenCalled();
+ } finally { await im.dispose(); }
+ });
+
// PR #6492: idle-switch coverage missed a poll response arriving during abort.
// Unit: public binding switch + real task-store boundary must retain the owner
// task and original bot context, then reject switching once that task is queued.
@@ -823,7 +844,7 @@ function blockingLiveTransport(): WechatTransport {
} as unknown as WechatTransport;
}
-function restoredConnection(queued: () => number) {
+function restoredConnection(queued: () => number, policyDisabled = false) {
const credentials = { bindingEpoch: 'epoch', botToken: 'test-token', ilinkBotId: 'wechat-bot',
userId: 'owner', baseUrl: 'https://ilinkai.weixin.qq.com', boundAt: 1 };
const testHost = host({ secretRead: (name) => name === 'wechat_data_key_v1'
@@ -836,6 +857,7 @@ function restoredConnection(queued: () => number) {
? { committed: false, reason: 'stale-cursor' } : null),
});
const transport = blockingLiveTransport();
- const im = new WechatIM(deps({ host: testHost, getDbClient: () => db, createTransport: () => transport }));
+ const im = new WechatIM(deps({ host: testHost, getDbClient: () => db, createTransport: () => transport,
+ isCompatibilityDisabled: () => policyDisabled }));
return { im, transport, db, testHost };
}
diff --git a/apps/desktop/src/main/im/wechat/taskStore.ts b/apps/desktop/src/main/im/wechat/taskStore.ts
index a58e5d8b72..b9a1b32710 100644
--- a/apps/desktop/src/main/im/wechat/taskStore.ts
+++ b/apps/desktop/src/main/im/wechat/taskStore.ts
@@ -87,6 +87,18 @@ interface WechatOutboxRow extends Omit<WechatOutboxRecord, 'contextToken'> {
contextTag: string;
}
+/** Queue metadata is available even when policy prevents loading credentials. */
+export async function countQueuedWechatTasks(db: DbClient, bindingEpoch: string): Promise<number> {
+ const row = await db.queryOne<{ count: number }>(
+ `SELECT COUNT(*) AS count
+ FROM wechat_inbox
+ WHERE binding_epoch = ?
+ AND status IN ('pending', 'dispatching', 'accepted_running', 'waiting_desktop')`,
+ [bindingEpoch],
+ );
+ return row?.count ?? 0;
+}
+
export class WechatTaskStore {
readonly #db: DbClient;
readonly #dataKey: Buffer;
@@ -194,15 +206,8 @@ export class WechatTaskStore {
return next;
}
- async countQueuedTasks(bindingEpoch: string): Promise<number> {
- const row = await this.#db.queryOne<{ count: number }>(
- `SELECT COUNT(*) AS count
- FROM wechat_inbox
- WHERE binding_epoch = ?
- AND status IN ('pending', 'dispatching', 'accepted_running', 'waiting_desktop')`,
- [bindingEpoch],
- );
- return row?.count ?? 0;
+ countQueuedTasks(bindingEpoch: string): Promise<number> {
+ return countQueuedWechatTasks(this.#db, bindingEpoch);
}
activateBindingEpoch(args: {