feat(protocol): add byte-complete packet tracing

Capture complete packet request, reply, and push payloads during temporary TRACE sessions while preserving the existing non-persistent logging boundary.

Correlate transport, parser, enrichment, drop, dispatch, timeout, and terminal outcomes with their real identifiers so exported logs can explain packet failures without relying on user descriptions.

Lock byte completeness, lazy disabled behavior, concurrency, context propagation, and truthful terminal outcomes with regression coverage.
This commit is contained in:
DrSmoothl committed 2026-07-30 20:11:02 +08:00
1 parent f9c4063fc5
commit 0d14408b6f
12 files changed
+1682 -88

No files matched your search

+64 -2
View File
@@ -115,9 +115,43 @@ export function parseMsgPush(
pkt: PacketInfo,
identity: IdentityService,
dedup?: SysMsgDedup,
): QQEventVariant[] {
return parseMsgPushInternal(
pkt,
identity,
dedup,
false,
);
}
export function parseMsgPushOrThrow(
pkt: PacketInfo,
identity: IdentityService,
dedup?: SysMsgDedup,
): QQEventVariant[] {
return parseMsgPushInternal(
pkt,
identity,
dedup,
true,
);
}
function parseMsgPushInternal(
pkt: PacketInfo,
identity: IdentityService,
dedup: SysMsgDedup | undefined,
throwDecoderErrors: boolean,
): QQEventVariant[] {
const ctx = buildContext(pkt, identity);
if (!ctx) return [];
if (!ctx) {
log.trace(() => [
'packet_branch serviceCmd=%j seqId=%d branch=push_context_invalid',
pkt.serviceCmd,
pkt.seqId,
]);
return [];
}
// #137/#266: mirror QQ NT `sys_msg_mgr.cc::ProcessRecvSysMsg` before
// dispatch. Its static routing table is the scope boundary: listed routes
@@ -127,6 +161,15 @@ export function parseMsgPush(
if (dedup) {
const dedupIdentity = deriveSysMsgDedupIdentity(ctx);
if (dedupIdentity && dedup.seenDuplicate(dedupIdentity)) {
log.trace(() => [
'packet_branch serviceCmd=%j seqId=%d branch=duplicate_system_push peer=%j chatType=%d messageSeq=%d messageRandom=%d',
pkt.serviceCmd,
pkt.seqId,
dedupIdentity.peerUid,
dedupIdentity.chatType,
dedupIdentity.sequence,
dedupIdentity.random,
]);
log.debug(
'dropped duplicate system push (peer=%s chatType=%d seq=%d msgType=%d/%d msgId=%d)',
dedupIdentity.peerUid,
@@ -140,13 +183,24 @@ export function parseMsgPush(
}
}
const events = registry.decode(ctx);
const events = throwDecoderErrors
? registry.decodeOrThrow(ctx)
: registry.decode(ctx);
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
// — the precise discriminator, the group-invite "[空消息]" phantom being one.
if (isC2cControlPush(ctx.head)) {
log.trace(() => [
'packet_branch serviceCmd=%j seqId=%d branch=c2c_control_push msgType=%d subType=%d c2cCmd=%d messageSeq=%d',
pkt.serviceCmd,
pkt.seqId,
ctx.head.msgType,
ctx.head.subType,
ctx.head.c2cCmd,
ctx.head.sequence,
]);
const elemCount = (ev as { elements?: unknown[] }).elements?.length ?? 0;
if (elemCount > 0) {
log.debug('dropped c2c control push that carried %d element(s) (kind=%s seq=%d from=%d msgType=%d cmd=%d)',
@@ -170,6 +224,14 @@ export function parseMsgPush(
warnMissingC2cSequence(ev, ctx.fromUin);
return true;
}
log.trace(() => [
'packet_branch serviceCmd=%j seqId=%d branch=empty_message msgType=%d subType=%d messageSeq=%d',
pkt.serviceCmd,
pkt.seqId,
ctx.head.msgType,
ctx.head.subType,
ctx.head.sequence,
]);
return false;
});
+42 -9
View File
@@ -22,21 +22,54 @@ export class MsgPushRegistry {
}
decode(ctx: MsgPushContext): QQEventVariant[] {
const decoder = this.decoders_.get(ctx.head.msgType as PkgType);
if (!decoder) {
// Unrecognized PkgType: either we don't care about this notify, or
// the QQ NT client added a new variant. Surface at debug so a
// protocol change can be spotted without spamming production logs.
unknownLog.debug('no decoder for PkgType=%d subType=%d', ctx.head.msgType, ctx.head.subType);
return [];
}
try {
return decoder(ctx);
return this.runDecoder(ctx);
} catch (e) {
this.traceDecoderException(ctx, e);
log.error('decoder error for PkgType=%d subType=%d: %s',
ctx.head.msgType, ctx.head.subType,
e instanceof Error ? (e.stack ?? e.message) : String(e));
return [];
}
}
decodeOrThrow(ctx: MsgPushContext): QQEventVariant[] {
try {
return this.runDecoder(ctx);
} catch (e) {
this.traceDecoderException(ctx, e);
throw e;
}
}
private runDecoder(ctx: MsgPushContext): QQEventVariant[] {
const decoder = this.decoders_.get(ctx.head.msgType as PkgType);
if (!decoder) {
unknownLog.trace(() => [
'packet_branch branch=decoder_unregistered msgType=%d subType=%d messageSeq=%d',
ctx.head.msgType,
ctx.head.subType,
ctx.head.sequence,
]);
// Unrecognized PkgType: either we don't care about this notify, or
// the QQ NT client added a new variant. Surface at debug so a
// protocol change can be spotted without spamming production logs.
unknownLog.debug('no decoder for PkgType=%d subType=%d', ctx.head.msgType, ctx.head.subType);
return [];
}
return decoder(ctx);
}
private traceDecoderException(
ctx: MsgPushContext,
error: unknown,
): void {
log.trace(() => [
'packet_branch branch=decoder_exception msgType=%d subType=%d messageSeq=%d error=%j',
ctx.head.msgType,
ctx.head.subType,
ctx.head.sequence,
error instanceof Error ? error.message : String(error),
]);
}
}