fix(protocol): dedup duplicate system pushes (#137 official-robot double group_increase)

Inviting an official robot (e.g. 2854207029) into a group made QQ push the
group_member_increase notice twice, so SnowLuma emitted two
notice.group_increase events; a normal member produced one.

Root cause (RE of wrapper.linux.node): QQ NT dedups every received system
message in sys_msg_mgr.cc::ProcessRecvSysMsg (sub_37BADE0) by a global_key
`{peerUid}_{chatType}_{msg_seq}_{random}` (sub_37BC4E0) — an already-seen key
is dropped ("ignore dup msg") before any listener sees it. The per-message
discriminators msg_seq (attr 40003 = contentHead field 5) and random (attr
40002 = contentHead field 4) are exactly head.sequence and head.msgId
(msg_header_codec_helper.cc sub_37C8AD0). Kernel-based bots (NapCat/LLOneBot)
receive events after this dedup; SnowLuma reads the raw OlPush before it.

Mirror it: SysMsgDedup, a per-account bounded ring that drops a duplicate
system push by (msgType, subType, fromUin, sequence, msgId). Push-level (drops
the whole duplicate like NT), live path only (Bridge owns the tracker; the
forward re-parse passes none), system kinds only (chat messages excluded), and
never dedups a push lacking a server identity (sequence or msgId 0).
This commit is contained in:
DrSmoothl committed 2026-06-23 20:36:24 +08:00
1 parent 4e0ec92c23
commit 1bdb8a40c4
4 files changed
+207 -4

No files matched your search

+26 -2
View File
@@ -21,6 +21,9 @@ import { decodeGroupMessage } from './decoders/group-message';
import { decodeTempMessage } from './decoders/temp-message';
import { PkgType } from './enums';
import { MsgPushRegistry } from './registry';
import { SysMsgDedup } from './sysmsg-dedup';
export { SysMsgDedup } from './sysmsg-dedup';
export { SSO_GET_GROUP_MSG_CMD, fetchGroupMessageRange } from './fetch-group-history';
export { SSO_GET_C2C_MSG_CMD, fetchC2cMessageRange } from './fetch-c2c-history';
@@ -80,11 +83,15 @@ function describeUndecodedBody(body: PushMsgBody | undefined): string {
return `elems=[${parts.join('; ')}]${extras.length ? ` ${extras.join(' ')}` : ''}`;
}
export function parseMsgPush(pkt: PacketInfo, identity: IdentityService): QQEventVariant[] {
export function parseMsgPush(
pkt: PacketInfo,
identity: IdentityService,
dedup?: SysMsgDedup,
): QQEventVariant[] {
const ctx = buildContext(pkt, identity);
if (!ctx) return [];
const events = registry.decode(ctx);
return events.filter((ev) => {
const out = events.filter((ev) => {
if (!MESSAGE_KINDS.has(ev.kind)) return true;
// C2C control/system signal (#102): QQ NT routes these via OnRecvSysMsg and
// never shows them as a bubble. Drop by (msgType, c2cCmd) regardless of body
@@ -111,4 +118,21 @@ export function parseMsgPush(pkt: PacketInfo, identity: IdentityService): QQEven
}
return false;
});
// #137: mirror QQ NT `sys_msg_mgr.cc::ProcessRecvSysMsg` global-key dedup.
// The server pushes some system notices twice (e.g. inviting an official
// robot → two `group_member_increase`); the kernel drops the duplicate by
// (peer, seq, random) before any listener sees it, but we read the raw
// OlPush, so we replicate the drop. Scoped to system pushes only — chat
// messages have their own NT dedup path and the forward re-parse re-runs
// this without a tracker. A push that decodes to a message kind, or to
// nothing, is never deduped here.
if (dedup && out.length > 0 && out.every((ev) => !MESSAGE_KINDS.has(ev.kind))) {
if (dedup.seenDuplicate(ctx.head, ctx.fromUin)) {
log.debug('dropped duplicate system push (kinds=%s seq=%d from=%d msgType=%d msgId=%d)',
out.map((ev) => ev.kind).join(','), ctx.head.sequence, ctx.fromUin, ctx.head.msgType, ctx.head.msgId);
return [];
}
}
return out;
}
@@ -0,0 +1,59 @@
import type { MsgPushHead } from './context';
/**
* Mirrors QQ NT's system-message dedup so SnowLuma stops double-reporting the
* duplicate sys pushes the server emits for some events (#137: inviting an
* official robot pushes the `group_member_increase` notice twice → two
* `notice.group_increase`; a normal member is pushed once).
*
* RE of `wrapper.linux.node` — `sys_msg_mgr.cc::ProcessRecvSysMsg`
* (`sub_37BADE0`): every received system message is keyed and looked up in a
* per-account set; an already-seen key is logged ("on recv sys msg, ignore dup
* msg … global_key={}", sys_msg_mgr.cc:467) and the whole message is dropped
* before the UI / JS listeners ever see it. The key (`sub_37BC4E0`) is
*
* global_key = `{peerUid}_{chatType}_{msg_seq}_{random}`
*
* whose per-message discriminators are `msg_seq` (attr 40003 = contentHead
* field 5) and `random` (attr 40002 = contentHead field 4) — exactly
* {@link MsgPushHead.sequence} and {@link MsgPushHead.msgId} here
* (`msg_header_codec_helper.cc` sub_37C8AD0). Kernel-based bots (NapCat /
* LLOneBot) receive events *after* this dedup; SnowLuma reads the raw OlPush
* *before* it, so we replicate it here.
*
* We dedup at the push level (drop the whole duplicate, like NT) using the
* fields already on the head plus `fromUin` for peer scoping (a stand-in for
* peerUid — `msg_seq` can be per-conversation, so the peer keeps two groups'
* pushes from colliding; `random`/`msgId` makes the collision astronomically
* unlikely anyway). A push with no server identity (`sequence` or `msgId` 0) is
* never deduped — without a real per-message id we cannot distinguish a true
* duplicate from two distinct events, and dropping then would be a regression.
*/
export class SysMsgDedup {
private readonly seen = new Set<string>();
// Fixed-capacity ring of keys for O(1) bounded eviction (oldest-out). System
// pushes are low-frequency, so a duplicate always lands within a few entries
// of its original — eviction only matters far beyond the dup window.
private readonly ring: (string | undefined)[];
private cursor = 0;
constructor(private readonly capacity = 1024) {
this.ring = new Array<string | undefined>(capacity);
}
/**
* Returns true if a system push with this identity was already seen (caller
* should drop it); otherwise records it and returns false.
*/
seenDuplicate(head: Pick<MsgPushHead, 'msgType' | 'subType' | 'sequence' | 'msgId'>, fromUin: number): boolean {
if (head.sequence === 0 || head.msgId === 0) return false;
const key = `${head.msgType}:${head.subType}:${fromUin}:${head.sequence}:${head.msgId}`;
if (this.seen.has(key)) return true;
const evicted = this.ring[this.cursor];
if (evicted !== undefined) this.seen.delete(evicted);
this.ring[this.cursor] = key;
this.seen.add(key);
this.cursor = (this.cursor + 1) % this.capacity;
return false;
}
}