From a5335687ecd88928fda41949e3de5191d2e4e67d Mon Sep 17 00:00:00 2001 From: DrSmoothl <1787882683@qq.com> Date: Sat, 13 Jun 2026 12:03:04 +0800 Subject: [PATCH] fix(protocol): fetch group-join verify comment regardless of uin cache (#98) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The verify text a user types when requesting to join a group is never in the push — it lives on the OIDB pending-request queue. The fetch (resolveGroupJoinRequest) was gated behind the UID→UIN stranger-resolve, so when the requester's uin was already cached the whole async path (and thus the comment fetch) was skipped, leaving the OneBot request.group `comment` empty. Decouple the two: every uid-bearing group_invite now goes through the async enrich path (needsGroupInviteEnrich → dispatchGroupInvite), which ALWAYS fetches the comment and resolves the UIN profile only when needed (the fromUin === groupId cache-pollution self-heal is preserved). Matches Lagrange's unconditional FetchGroupRequests and NapCat's notify.postscript. Rewrites the cache-hit test (which had pinned the buggy behavior) into a regression asserting the comment is fetched + applied on a cache hit. 346/346 protocol tests pass. Verified by adversarial review. --- packages/protocol/src/packet-pipeline.ts | 80 ++++++++++--------- .../packet-pipeline-stranger-resolve.test.ts | 32 +++++--- 2 files changed, 64 insertions(+), 48 deletions(-) diff --git a/packages/protocol/src/packet-pipeline.ts b/packages/protocol/src/packet-pipeline.ts index cfac6fec..ac5e388d 100644 --- a/packages/protocol/src/packet-pipeline.ts +++ b/packages/protocol/src/packet-pipeline.ts @@ -98,9 +98,9 @@ export class IncomingPacketPipeline { this.log.warn('dispatchAfterIdentityRefresh failed: %s', err instanceof Error ? (err.stack ?? err.message) : String(err)); }); - } else if (this.needsStrangerResolve(event)) { - void this.dispatchAfterStrangerResolve(event).catch((err) => { - this.log.warn('dispatchAfterStrangerResolve failed: %s', + } else if (this.needsGroupInviteEnrich(event)) { + void this.dispatchGroupInvite(event).catch((err) => { + this.log.warn('dispatchGroupInvite failed: %s', err instanceof Error ? (err.stack ?? err.message) : String(err)); }); } else { @@ -125,28 +125,25 @@ export class IncomingPacketPipeline { return event.kind === 'group_member_join' && event.groupId > 0 && event.userUin <= 0 && Boolean(event.userUid); } - /** Group join requests carry only the requester's UID (no UIN). We - * fire a UID-form FetchUserProfile to resolve UIN + nickname before - * dispatch so the OneBot layer's `user_id` field is populated and - * the consumer bot's follow-up `get_stranger_info` lookup succeeds. - * Mirrors Lagrange's `dev/Lagrange.Core/.../MessagingLogic.cs:215-224`. + /** Every group_invite that carries a requester UID gets async + * enrichment before dispatch. Two INDEPENDENT things are filled in: * - * Also catches the legacy cache-pollution case: pre-fix builds - * stored ` → ` in the identity DB when - * the decoder's fallback was `ctx.fromUin` (= group's own uin on - * a group-scoped push). After upgrade, the decoder's - * `resolveUidToUin` hits that polluted mapping and returns the - * groupUin instead of 0, so the `fromUin <= 0` guard alone would - * silently skip the resolve and re-emit the bug. Treating - * `fromUin === groupId` as bogus forces the async resolve to - * overwrite the polluted entry on the next event. */ - private needsStrangerResolve(event: QQEventVariant): event is Extract { - if (event.kind !== 'group_invite' || !event.fromUid) return false; - if (event.fromUin <= 0) return true; - // Pollution signature — a real requester's uin would never equal - // the group's own uin. Force a re-resolve so the cache self-heals. - if (event.groupId > 0 && event.fromUin === event.groupId) return true; - return false; + * 1. The verify COMMENT — the text the requester typed ("你们好" etc.). + * It is NEVER in the push; it lives on the OIDB pending-request + * queue, so we ALWAYS fetch it (mirrors Lagrange's unconditional + * `FetchGroupRequests`; NapCat reads the equivalent + * `notify.postscript`). See issue #98. + * 2. The requester's UIN + nickname — only when not already resolved + * (the push carries a bare UID). Mirrors Lagrange's + * `dev/Lagrange.Core/.../MessagingLogic.cs:215-224`. + * + * These USED to be coupled — the comment fetch piggy-backed on the + * uin-resolve condition, so a requester whose uin was already cached + * silently lost their comment (bug #98). They're now decoupled inside + * `dispatchGroupInvite`; this guard just routes every uid-bearing + * group_invite onto the async path. */ + private needsGroupInviteEnrich(event: QQEventVariant): event is Extract { + return event.kind === 'group_invite' && !!event.fromUid; } private async dispatchAfterIdentityRefresh(event: Extract): Promise { @@ -163,31 +160,36 @@ export class IncomingPacketPipeline { this.emit(event); } - private async dispatchAfterStrangerResolve(event: Extract): Promise { + private async dispatchGroupInvite(event: Extract): Promise { const uid = event.fromUid; if (uid) { - // Fire profile + pending-request lookups in PARALLEL — they're - // independent OIDB calls (0xFE1_2 stranger profile + 0x10C0 - // pending request queue). Both `Promise.allSettled` so a flake - // on one path doesn't kill the other. const subType = event.subType === 'invite' ? 'invite' : 'add'; + // ALWAYS fetch the verify comment (it's never in the push). Resolve + // the UID→UIN profile only when the requester isn't already known — + // `fromUin === groupId` is the legacy cache-pollution signature + // (decoder fell back to the group's own uin) and must force a + // re-resolve so the cache self-heals. The two are independent OIDB + // calls (0xFE1_2 profile + 0x10C0 request queue); `Promise.allSettled` + // so a flake on one path can't kill the other. + const needsProfile = event.fromUin <= 0 || (event.groupId > 0 && event.fromUin === event.groupId); const [profileR, requestR] = await Promise.allSettled([ - this.deps.resolveStrangerProfile(uid), + needsProfile ? this.deps.resolveStrangerProfile(uid) : Promise.resolve(null), this.deps.resolveGroupJoinRequest(event.groupId, uid, subType), ]); - if (profileR.status === 'fulfilled' && profileR.value && profileR.value.uin > 0) { - event.fromUin = profileR.value.uin; - } else if (profileR.status === 'rejected') { - this.log.warn('failed to resolve stranger profile: uid=%s err=%s', - uid, profileR.reason instanceof Error ? profileR.reason.message : String(profileR.reason)); + if (needsProfile) { + if (profileR.status === 'fulfilled' && profileR.value && profileR.value.uin > 0) { + event.fromUin = profileR.value.uin; + } else if (profileR.status === 'rejected') { + this.log.warn('failed to resolve stranger profile: uid=%s err=%s', + uid, profileR.reason instanceof Error ? profileR.reason.message : String(profileR.reason)); + } } if (requestR.status === 'fulfilled' && requestR.value) { - // NapCat surfaces the verify text as `comment` on the OneBot - // event (`postscript` server-side). Without this the bot's - // approval-prompt template renders an empty body line — see - // `dev/NapCatQQ/.../napcat-onebot/index.ts:496-498`. + // The verify text the requester typed; NapCat surfaces it as + // `notify.postscript`. Without this the OneBot `comment` field is + // empty — bug #98. event.message = requestR.value.comment; } else if (requestR.status === 'rejected') { this.log.warn('failed to resolve group join request: groupId=%d uid=%s err=%s', diff --git a/packages/protocol/tests/packet-pipeline-stranger-resolve.test.ts b/packages/protocol/tests/packet-pipeline-stranger-resolve.test.ts index 2dbe6ba2..10fb6042 100644 --- a/packages/protocol/tests/packet-pipeline-stranger-resolve.test.ts +++ b/packages/protocol/tests/packet-pipeline-stranger-resolve.test.ts @@ -113,26 +113,37 @@ describe('IncomingPacketPipeline / stranger resolve on group_invite', () => { expect(captured[0]).toMatchObject({ fromUin: 0, fromUid: 'u_x' }); }); - it('skips the stranger resolve when fromUin is already populated (cache hit)', async () => { - // If the decoder's `resolveUidToUin` already found the uin in - // the local cache (e.g. group member roster), we shouldn't waste - // a wire call. + it('still fetches + applies the comment when fromUin is already cached (issue #98)', async () => { + // THE bug: the comment fetch used to piggy-back on the uin-resolve + // gate, so a requester whose uin was already in cache (group member + // roster, prior @-mention, re-application…) silently lost their + // verify text — the OneBot `comment` came out empty. The comment + // and the uin resolve are now decoupled: the comment is fetched + // unconditionally; only the (wasteful) profile lookup is skipped on + // a cache hit. const resolveStrangerProfile = vi.fn(async () => null); - const { pipeline, captured } = makePipeline({ resolveStrangerProfile }); + const resolveGroupJoinRequest = vi.fn(async () => ({ comment: '求通过', sequence: 42 })); + const { pipeline, captured } = makePipeline({ resolveStrangerProfile, resolveGroupJoinRequest }); pipeline.registerCmd('test.cmd', () => [{ kind: 'group_invite', time: 1, selfUin: 10001, groupId: 12345, fromUin: 99999, fromUid: 'u_known', - subType: 'invite', message: '', flag: 'invite:12345:u_known', + subType: 'add', message: '', flag: 'add:12345:u_known', } as QQEventVariant]); pipeline.process({ serviceCmd: 'test.cmd' } as PacketInfo); await new Promise(r => setTimeout(r, 10)); + // Profile lookup is skipped — the uin is already known, no wasted call… expect(resolveStrangerProfile).not.toHaveBeenCalled(); + // …but the comment IS still fetched from the pending-request queue + // and applied to the emitted event. + expect(resolveGroupJoinRequest).toHaveBeenCalledWith(12345, 'u_known', 'add'); expect(captured).toHaveLength(1); - expect(captured[0]).toMatchObject({ fromUin: 99999, fromUid: 'u_known' }); + const ev = captured[0] as Extract; + expect(ev.fromUin).toBe(99999); + expect(ev.message).toBe('求通过'); }); it('populates event.message from the pending-request queue (NapCat parity)', async () => { @@ -260,9 +271,10 @@ describe('IncomingPacketPipeline / stranger resolve on group_invite', () => { }); }); - it('skips the stranger resolve when fromUid is empty (no uid to look up by)', async () => { + it('skips both lookups when fromUid is empty (no uid to look up by)', async () => { const resolveStrangerProfile = vi.fn(async () => null); - const { pipeline, captured } = makePipeline({ resolveStrangerProfile }); + const resolveGroupJoinRequest = vi.fn(async () => null); + const { pipeline, captured } = makePipeline({ resolveStrangerProfile, resolveGroupJoinRequest }); pipeline.registerCmd('test.cmd', () => [{ kind: 'group_invite', @@ -274,7 +286,9 @@ describe('IncomingPacketPipeline / stranger resolve on group_invite', () => { pipeline.process({ serviceCmd: 'test.cmd' } as PacketInfo); await new Promise(r => setTimeout(r, 10)); + // No uid → nothing to query by → neither lookup runs, event still emits. expect(resolveStrangerProfile).not.toHaveBeenCalled(); + expect(resolveGroupJoinRequest).not.toHaveBeenCalled(); expect(captured).toHaveLength(1); }); });