From d5af6b10ef45d5474060d15d3147f1d59dccadcf Mon Sep 17 00:00:00 2001 From: DrSmoothl <1787882683@qq.com> Date: Tue, 16 Jun 2026 00:54:28 +0800 Subject: [PATCH] =?UTF-8?q?fix(msg-push):=20drop=20genuinely-empty=20c2c?= =?UTF-8?q?=20push=20instead=20of=20surfacing=20"[=E7=A9=BA=E6=B6=88?= =?UTF-8?q?=E6=81=AF]"=20(#102)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A group invite makes QQ emit a content-less c2c message (msgType=166/0, body richText.elems=[]) right after the invite card. The TRACE log from #102's diagnostic confirmed the body is truly empty — no commonElem, no msgContent, no ptt/file — so it's a phantom control push, not an element type we fail to decode. It was reaching OneBot clients as a confusing "[空消息]". parseMsgPush now drops a message-kind event that decodes to zero elements when its body carries nothing decodable. When the body *did* carry content but still decoded to nothing (a real missing decoder), the event is kept and a warning fires so the gap gets noticed rather than silently swallowed — replacing the temporary #102 diagnostic with the actual fix. --- packages/protocol/src/msg-push/index.ts | 47 ++++++++++---- .../tests/msg-push/empty-message-drop.test.ts | 61 +++++++++++++++++++ 2 files changed, 95 insertions(+), 13 deletions(-) create mode 100644 packages/protocol/tests/msg-push/empty-message-drop.test.ts diff --git a/packages/protocol/src/msg-push/index.ts b/packages/protocol/src/msg-push/index.ts index 3f7b015d..695b20eb 100644 --- a/packages/protocol/src/msg-push/index.ts +++ b/packages/protocol/src/msg-push/index.ts @@ -54,13 +54,26 @@ const MESSAGE_KINDS = new Set([ ]); /** - * Diagnostic for issue #102 (group invite → "[空消息]"). When a message decodes - * to zero elements, dump what its body actually carried — each element's field - * names, every `commonElem`'s serviceType/businessType + payload hex, and any - * `msgContent` — so we can tell a genuinely content-less control push from an - * element type we simply don't decode yet (and then decode it, rather than - * blindly dropping the message). TODO(#102): remove once the culprit element is - * identified and handled. + * Whether a push body carries anything the rich-body decoder could turn into an + * element: source `richText.elems`, a voice (`ptt`), a c2c file + * (`notOnlineFile`), or serialised `msgContent` file metadata. Mirrors exactly + * what {@link decodeRichBody} reads. A body that has none of these is a + * genuinely content-less control push, not an element type we fail to decode. + */ +function bodyHasDecodableContent(body: PushMsgBody | undefined): boolean { + const rt = body?.richText; + if (rt) { + if (rt.elems && rt.elems.length > 0) return true; + if (rt.ptt || rt.notOnlineFile) return true; + } + return !!(body?.msgContent && body.msgContent.length > 0); +} + +/** + * Summarise a body that decoded to zero elements despite carrying content — + * each element's field names, every `commonElem`'s serviceType/businessType + + * payload hex, and any `msgContent` — so a missing decoder can be identified + * from one log line rather than swallowed silently. */ function describeUndecodedBody(body: PushMsgBody | undefined): string { const elems = (body?.richText?.elems ?? []) as Elem[]; @@ -86,13 +99,21 @@ export function parseMsgPush(pkt: PacketInfo, identity: IdentityService): QQEven const ctx = buildContext(pkt, identity); if (!ctx) return []; const events = registry.decode(ctx); - // #102 diagnostic — surface what an empty-decoded message actually contained. - for (const ev of events) { - if (MESSAGE_KINDS.has(ev.kind) && (ev as { elements?: unknown[] }).elements?.length === 0) { - log.warn('[#102] empty message (kind=%s seq=%d from=%d msgType=%d/%d): %s', + return events.filter((ev) => { + if (!MESSAGE_KINDS.has(ev.kind)) return true; + if ((ev as { elements?: unknown[] }).elements?.length !== 0) return true; + // Empty-element message (#102). Drop it when the body is genuinely empty — + // QQ emits a content-less c2c push (msgType=166/0) alongside e.g. a group + // invite card, which otherwise surfaces to clients as a confusing + // "[空消息]". If instead the body *had* content we just couldn't decode, + // keep the (still-empty) event but warn so the missing decoder gets noticed + // rather than silently swallowed. + if (bodyHasDecodableContent(ctx.body)) { + log.warn('message had content but decoded to 0 elements — missing decoder? (kind=%s seq=%d from=%d msgType=%d/%d): %s', ev.kind, ctx.head.sequence, ctx.fromUin, ctx.head.msgType, ctx.head.subType, describeUndecodedBody(ctx.body)); + return true; } - } - return events; + return false; + }); } diff --git a/packages/protocol/tests/msg-push/empty-message-drop.test.ts b/packages/protocol/tests/msg-push/empty-message-drop.test.ts new file mode 100644 index 00000000..b72c9d7f --- /dev/null +++ b/packages/protocol/tests/msg-push/empty-message-drop.test.ts @@ -0,0 +1,61 @@ +// Regression for #102: a group invite makes QQ emit a content-less c2c push +// (msgType=166/0, body.richText.elems=[]) right after the invite card. That +// phantom used to surface to OneBot clients as a confusing "[空消息]". +// parseMsgPush must drop a message that decodes to zero elements *when its body +// is genuinely empty*, while still keeping (and warning about) a message that +// carried content we merely failed to decode. + +import { describe, expect, it } from 'vitest'; +import type { PacketInfo } from '@snowluma/common/protocol-types'; +import { protobuf_encode } from '@snowluma/proton'; +import type { PushMsg, PushMsgBody } from '@snowluma/proto-defs/message'; +import type { IdentityService } from '../../src/identity-service'; +import { parseMsgPush } from '../../src/msg-push'; + +const identity = { findFriend: () => undefined } as unknown as IdentityService; + +function pushPacket(message: PushMsgBody): PacketInfo { + return { + pid: 0, + uin: '2000000001', + serviceCmd: 'trpc.msg.olpush.OlPushService.MsgPush', + seqId: 0, + retCode: 0, + fromClient: false, + body: protobuf_encode({ message }), + }; +} + +describe('parseMsgPush — empty message drop (#102)', () => { + it('drops a genuinely empty friend message (group-invite phantom)', () => { + const out = parseMsgPush(pushPacket({ + responseHead: { fromUin: 10001, fromUid: 'u_x' }, + contentHead: { msgType: 166, subType: 0, sequence: 59962, timestamp: 1781540572, msgId: 1963990184 }, + body: { richText: { elems: [] } }, + }), identity); + expect(out).toEqual([]); + }); + + it('keeps a friend message that actually has content', () => { + const out = parseMsgPush(pushPacket({ + responseHead: { fromUin: 10001, fromUid: 'u_x' }, + contentHead: { msgType: 166, subType: 0, sequence: 100, timestamp: 1781540572, msgId: 1 }, + body: { richText: { elems: [{ text: { str: 'hi' } }] } }, + }), identity); + expect(out).toHaveLength(1); + expect(out[0]).toMatchObject({ kind: 'friend_message', elements: [{ type: 'text', text: 'hi' }] }); + }); + + it('keeps an empty-decoded message whose body still carried content (missing decoder)', () => { + // A commonElem service type the rich-body decoder ignores → 0 elements, but + // the body was not empty, so the message must survive (and be warned about), + // not be silently dropped. + const out = parseMsgPush(pushPacket({ + responseHead: { fromUin: 10001, fromUid: 'u_x' }, + contentHead: { msgType: 166, subType: 0, sequence: 101, timestamp: 1781540572, msgId: 2 }, + body: { richText: { elems: [{ commonElem: { serviceType: 999, businessType: 0 } }] } }, + }), identity); + expect(out).toHaveLength(1); + expect(out[0]).toMatchObject({ kind: 'friend_message', elements: [] }); + }); +});