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): 伙伴路由回退时保留逐次权限策略

nana037c72e1fd49
diff --git a/apps/desktop/src/main/im/shared/__tests__/turnRunnerCompanionRouting.test.ts b/apps/desktop/src/main/im/shared/__tests__/turnRunnerCompanionRouting.test.ts
index abcfb55ceb..57b676f2df 100644
--- a/apps/desktop/src/main/im/shared/__tests__/turnRunnerCompanionRouting.test.ts
+++ b/apps/desktop/src/main/im/shared/__tests__/turnRunnerCompanionRouting.test.ts
@@ -11,7 +11,7 @@
*/
import { beforeEach, describe, expect, it, vi } from 'vitest';
-import type { AgentEvent, MakerEvent, Session, SessionSendResult } from '@cindy/maker-core';
+import type { AgentEvent, MakerEvent, Session, SessionSendResult, TurnPermissionPolicy } from '@cindy/maker-core';
import type { ChannelIM } from '@cindy/im';
const mocks = vi.hoisted(() => ({
@@ -491,6 +491,40 @@ describe('turnRunner: connection handed to a companion bot', () => {
expect(runner.getMakerSessionById('mimi-main')).toBe(rebuilt.session);
});
+ // Use the public runner and inspect Session.send: a binding alone must never
+ // suppress the policy on fallback. No network or extra runtime is required.
+ it.each([
+ { route: 'companion', owner: true, optIn: true, forced: false },
+ { route: 'companion', owner: false, optIn: true, forced: true },
+ { route: 'companion', owner: undefined, optIn: true, forced: true },
+ { route: 'companion', owner: true, optIn: false, forced: true },
+ { route: 'paused', owner: true, optIn: true, forced: true },
+ { route: 'missing-row', owner: true, optIn: true, forced: true },
+ { route: 'unbound', owner: true, optIn: true, forced: true },
+ ])('keeps the resolved-route permission boundary: %j', async ({ route, owner, optIn, forced }) => {
+ if (route === 'paused') {
+ companion.resolveSession.mockResolvedValue({ ok: false, errorCode: 'TARGET_BOT_INACTIVE', message: 'paused' });
+ } else if (route === 'missing-row') {
+ botRows.delete('mimi-main');
+ } else if (route === 'unbound') {
+ binding = null;
+ }
+ const policy: TurnPermissionPolicy = {
+ origin: { kind: 'im', channel: 'wechat' },
+ confirmationSurface: 'channel',
+ forceConfirmToolCall: () => true,
+ };
+ await runner.runAgentTurn({
+ botContextId: BOT_CONTEXT, userId: OWNER, userMessageId: 'permission-route',
+ text: 'hello', attachments: [], requesterIsOwner: owner,
+ turnPermissionPolicy: policy, inheritCompanionOwnerPermissions: optIn,
+ });
+ const id = route === 'companion' ? 'mimi-main' : legacyIdFor(BOT_CONTEXT, OWNER);
+ const send = harnesses.get(id)!.send;
+ expect(send).toHaveBeenCalledOnce();
+ expect(send.mock.calls[0]![1]?.turnPermissionPolicy).toBe(forced ? policy : undefined);
+ });
+
it('falls back to the channel’s own routing when the bot cannot be resolved, warning once', async () => {
companion.resolveSession.mockResolvedValue({ ok: false, errorCode: 'TARGET_BOT_INACTIVE', message: 'paused' });
await runTurn(OWNER, 'first');
diff --git a/apps/desktop/src/main/im/shared/turnRunner.ts b/apps/desktop/src/main/im/shared/turnRunner.ts
index 95c2e6da50..e2879f08fe 100644
--- a/apps/desktop/src/main/im/shared/turnRunner.ts
+++ b/apps/desktop/src/main/im/shared/turnRunner.ts
@@ -515,6 +515,8 @@ export interface ImRunAgentTurnArgs {
trackBackgroundTask?: (operation: () => Promise<void>) => void;
/** Optional per-turn host policy (personal WeChat routes confirmations to Desktop). */
turnPermissionPolicy?: TurnPermissionPolicy;
+ /** Opt in to native permissions only for a resolved companion route and verified owner. */
+ inheritCompanionOwnerPermissions?: boolean;
/**
* The triggering message passed the channel's owner check (DM owner gate or
* group speaker.isOwner). Only this unlocks owner-only Host actions; omitted = not owner.
@@ -1339,7 +1341,11 @@ export function createTurnRunner(
queueMode: args.queueMode,
...(args.beforeProviderStart ? { beforeProviderStart: args.beforeProviderStart } : {}),
...(args.onRouteResolved ? { onRouteResolved: args.onRouteResolved } : {}),
- ...(args.turnPermissionPolicy ? { turnPermissionPolicy: args.turnPermissionPolicy } : {}),
+ // Binding presence is insufficient: unavailable companions fall back to
+ // channel-owned sessions, which must retain the mandatory policy.
+ ...(args.turnPermissionPolicy && !(args.inheritCompanionOwnerPermissions === true
+ && args.requesterIsOwner === true && target.botOwned)
+ ? { turnPermissionPolicy: args.turnPermissionPolicy } : {}),
...(args.requesterIsOwner === true ? { requesterIsOwner: true } : {}),
...(args.onEarlyReject ? { onEarlyReject: args.onEarlyReject } : {}),
...(args.protectedContent === true ? { protectedContent: true } : {}),
diff --git a/apps/desktop/src/main/im/wechat/WechatIM.ts b/apps/desktop/src/main/im/wechat/WechatIM.ts
index 008a3c9d0c..3063075f60 100644
--- a/apps/desktop/src/main/im/wechat/WechatIM.ts
+++ b/apps/desktop/src/main/im/wechat/WechatIM.ts
@@ -1185,18 +1185,17 @@ export class WechatIM extends BaseIM implements RichChannelIM {
active.routeSessionId = sessionId;
}
},
- // Only credential-verified companion owners inherit the native mode.
- // Guests and standalone channels retain the mandatory turn policy.
- ...(companion && requesterIsOwner ? {} : {
- turnPermissionPolicy: createWechatTurnPermissionPolicy(task.id, {
- onInteractionStateChange: (state) => {
- void this.#requireStore().setWaitingDesktop(
- task.bindingEpoch,
- task.id,
- state === 'waiting',
- );
- },
- }),
+ // The runner decides after resolving the actual route; a stale binding
+ // must not relax permissions on the channel-owned fallback session.
+ inheritCompanionOwnerPermissions: true,
+ turnPermissionPolicy: createWechatTurnPermissionPolicy(task.id, {
+ onInteractionStateChange: (state) => {
+ void this.#requireStore().setWaitingDesktop(
+ task.bindingEpoch,
+ task.id,
+ state === 'waiting',
+ );
+ },
}),
});
} catch (error) {
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 ca64b6072b..4866c1bf75 100644
--- a/apps/desktop/src/main/im/wechat/__tests__/WechatIM.test.ts
+++ b/apps/desktop/src/main/im/wechat/__tests__/WechatIM.test.ts
@@ -30,13 +30,13 @@ vi.mock('../mediaStaging', async (importOriginal) => {
describe('WechatIM host boundary', () => {
// Exercise the durable inbox through the public host lifecycle. Only the
- // credential-verified owner of a linked companion inherits native permissions.
+ // credential-verified owner can request native permissions on a resolved companion route.
it.each([
- { companion: true, peer: 'owner', forcedPolicy: false },
- { companion: true, peer: 'guest', forcedPolicy: true },
- { companion: true, peer: '', forcedPolicy: true },
- { companion: false, peer: 'owner', forcedPolicy: true },
- ])('preserves the permission boundary: %j', async ({ companion, peer, forcedPolicy }) => {
+ { companion: true, peer: 'owner' },
+ { companion: true, peer: 'guest' },
+ { companion: true, peer: '' },
+ { companion: false, peer: 'owner' },
+ ])('preserves the permission boundary: %j', async ({ companion, peer }) => {
const { im, db } = restoredConnection(() => 0);
let leased = false;
vi.mocked(db.tx).mockImplementation((async (name: string) => {
@@ -52,6 +52,7 @@ describe('WechatIM host boundary', () => {
}) as DbClient['tx']);
const dispatchAgentTurn = vi.fn(async (_input: {
turnPermissionPolicy?: { confirmationSurface: string }; requesterIsOwner?: boolean;
+ inheritCompanionOwnerPermissions?: boolean;
}) => ({ kind: 'accepted', terminal: Promise.resolve({ kind: 'done' }) }));
im.attachTurnRuntime({ runner: {
companionBotIdFor: () => companion ? 'cindy' : undefined,
@@ -61,9 +62,9 @@ describe('WechatIM host boundary', () => {
try {
await vi.waitFor(() => expect(dispatchAgentTurn).toHaveBeenCalledOnce());
const input = dispatchAgentTurn.mock.calls[0]![0];
- expect(Boolean(input.turnPermissionPolicy)).toBe(forcedPolicy);
+ expect(input.inheritCompanionOwnerPermissions).toBe(true);
expect(input.requesterIsOwner).toBe(companion ? peer === 'owner' : undefined);
- if (forcedPolicy) expect(input.turnPermissionPolicy?.confirmationSurface).toBe('channel');
+ expect(input.turnPermissionPolicy?.confirmationSurface).toBe('channel');
} finally { await im.dispose(); }
});