fix: isolate channel activation failures

This commit is contained in:
xmanrui 2026-08-24 11:43:35 +08:00
parent 4989491dc8
commit 596848ffcb
4 changed files with 237 additions and 204 deletions

View file

@ -6,6 +6,11 @@ This file records the notable changes in each dsh-im release. Its format follows
## [Unreleased]
### Fixed / 修复
- 一个 IM 渠道激活失败时,Host 现在会记录错误并继续依次激活其他渠道,避免单个渠道的本地配置或初始化故障阻断其余渠道。
When one IM channel fails to activate, the Host now logs the error and continues activating the remaining channels in order, so a channel-local configuration or initialization failure cannot block the others.
## [2.0.0] - 2026-08-24
### Added / 新增

File diff suppressed because one or more lines are too long

View file

@ -37,6 +37,18 @@ export function createImHostPlugin(internals = {}) {
const startDiscord = internals.applyDiscord ?? applyDiscord;
const startOffice = internals.applyOffice ?? applyOffice;
const startWhatsapp = internals.applyWhatsapp ?? applyWhatsapp;
const channels = [
['feishu', startFeishu],
['weixin', startWeixin],
['dingtalk', startDingtalk],
['wecom', startWecom],
['qq', startQq],
['slack', startSlack],
['telegram', startTelegram],
['discord', startDiscord],
['whatsapp', startWhatsapp],
['office', startOffice],
];
return Object.freeze({
name,
inject,
@ -49,16 +61,21 @@ export function createImHostPlugin(internals = {}) {
} else {
installOutboundArtifactTool(ctx);
}
await startFeishu(ctx, channelConfig(config, 'feishu'));
await startWeixin(ctx, channelConfig(config, 'weixin'));
await startDingtalk(ctx, channelConfig(config, 'dingtalk'));
await startWecom(ctx, channelConfig(config, 'wecom'));
await startQq(ctx, channelConfig(config, 'qq'));
await startSlack(ctx, channelConfig(config, 'slack'));
await startTelegram(ctx, channelConfig(config, 'telegram'));
await startDiscord(ctx, channelConfig(config, 'discord'));
await startWhatsapp(ctx, channelConfig(config, 'whatsapp'));
await startOffice(ctx, channelConfig(config, 'office'));
const logger = typeof ctx?.logger === 'function'
? ctx.logger(name)
: (ctx?.logger ?? console);
const failures = [];
for (const [channel, start] of channels) {
try {
await start(ctx, channelConfig(config, channel));
} catch (error) {
failures.push(error);
logger.error?.(`[dsh-im] failed to activate ${channel}; continuing with the remaining channels`, error);
}
}
if (failures.length === channels.length) {
throw new AggregateError(failures, 'dsh-im failed to activate every channel');
}
},
});
}

View file

@ -55,58 +55,69 @@ test('Host composes nine IM channels and the AI Office connector inside one plug
]);
});
test('Host does not start Weixin when Feishu activation fails', async () => {
let weixinStarted = false;
const plugin = createImHostPlugin({
applyFeishu: async () => { throw new Error('feishu unavailable'); },
applyWeixin: async () => { weixinStarted = true; },
applyDingtalk: async () => { throw new Error('DingTalk must not start'); },
applyWecom: async () => { throw new Error('Enterprise WeChat must not start'); },
applyQq: async () => { throw new Error('QQ must not start'); },
});
const CHANNELS = [
['feishu', 'applyFeishu'],
['weixin', 'applyWeixin'],
['dingtalk', 'applyDingtalk'],
['wecom', 'applyWecom'],
['qq', 'applyQq'],
['slack', 'applySlack'],
['telegram', 'applyTelegram'],
['discord', 'applyDiscord'],
['whatsapp', 'applyWhatsapp'],
['office', 'applyOffice'],
];
await assert.rejects(() => plugin.apply({}, {}), /feishu unavailable/);
assert.equal(weixinStarted, false);
function activationFixture(failedChannels) {
const calls = [];
const events = [];
const errors = [];
const failures = new Map();
const internals = Object.fromEntries(CHANNELS.map(([channel, applyName]) => [
applyName,
async () => {
calls.push(channel);
events.push(`${channel}:start`);
await new Promise((resolve) => setImmediate(resolve));
if (failedChannels.has(channel)) {
const error = new Error(`${channel} unavailable`);
failures.set(channel, error);
events.push(`${channel}:failed`);
throw error;
}
events.push(`${channel}:end`);
},
]));
const ctx = { logger: { error: (...args) => errors.push(args) } };
return { plugin: createImHostPlugin(internals), ctx, calls, events, errors, failures };
}
test('Host continues activating channels in order when one channel fails', async () => {
for (const [failedChannel] of CHANNELS) {
const fixture = activationFixture(new Set([failedChannel]));
await fixture.plugin.apply(fixture.ctx, {});
assert.deepEqual(fixture.calls, CHANNELS.map(([channel]) => channel));
assert.deepEqual(fixture.events, CHANNELS.flatMap(([channel]) => [
`${channel}:start`,
`${channel}:${channel === failedChannel ? 'failed' : 'end'}`,
]));
assert.equal(fixture.errors.length, 1);
assert.match(fixture.errors[0][0], new RegExp(`activate ${failedChannel}`));
assert.equal(fixture.errors[0][1], fixture.failures.get(failedChannel));
}
});
test('Host does not start DingTalk when Weixin activation fails', async () => {
let dingtalkStarted = false;
const plugin = createImHostPlugin({
applyFeishu: async () => {},
applyWeixin: async () => { throw new Error('weixin unavailable'); },
applyDingtalk: async () => { dingtalkStarted = true; },
applyWecom: async () => { throw new Error('Enterprise WeChat must not start'); },
applyQq: async () => { throw new Error('QQ must not start'); },
});
test('Host reports aggregate failure only after every channel was attempted', async () => {
const fixture = activationFixture(new Set(CHANNELS.map(([channel]) => channel)));
await assert.rejects(() => plugin.apply({}, {}), /weixin unavailable/);
assert.equal(dingtalkStarted, false);
});
test('Host does not start Enterprise WeChat when DingTalk activation fails', async () => {
let wecomStarted = false;
const plugin = createImHostPlugin({
applyFeishu: async () => {},
applyWeixin: async () => {},
applyDingtalk: async () => { throw new Error('dingtalk unavailable'); },
applyWecom: async () => { wecomStarted = true; },
applyQq: async () => { throw new Error('QQ must not start'); },
});
await assert.rejects(() => plugin.apply({}, {}), /dingtalk unavailable/);
assert.equal(wecomStarted, false);
});
test('Host does not start QQ when Enterprise WeChat activation fails', async () => {
let qqStarted = false;
const plugin = createImHostPlugin({
applyFeishu: async () => {},
applyWeixin: async () => {},
applyDingtalk: async () => {},
applyWecom: async () => { throw new Error('wecom unavailable'); },
applyQq: async () => { qqStarted = true; },
});
await assert.rejects(() => plugin.apply({}, {}), /wecom unavailable/);
assert.equal(qqStarted, false);
await assert.rejects(
() => fixture.plugin.apply(fixture.ctx, {}),
(error) => error instanceof AggregateError
&& error.errors.length === CHANNELS.length
&& /failed to activate every channel/.test(error.message),
);
assert.deepEqual(fixture.calls, CHANNELS.map(([channel]) => channel));
assert.equal(fixture.errors.length, CHANNELS.length);
});