fix(bridge): synchronize reconciled login state

Route recovered login state through the hook client before dependent services start. Add regression coverage to keep packet actions available after reconciliation.

Fixes #286.
This commit is contained in:
DrSmoothl committed 2026-07-26 15:53:57 +08:00
1 parent b5e10fefc2
commit d05bbe3551
3 files changed
+71 -5

No files matched your search

+8 -1
View File
@@ -507,7 +507,14 @@ export class HookSession extends EventEmitter {
this.log.info('login reconciled via active probe: PID=%d UIN=%s', this.pid, info.uin);
// isRealUin guarantees a pure non-empty digit string, so BigInt() here
// cannot throw (keep that contract if isRealUin's regex is ever changed).
this.handleLoginState({ loggedIn: true, uin: info.uin, uinNumber: BigInt(info.uin) });
// Reconcile through QqHookClient instead of mutating HookSession alone:
// packet sending and login waiters must observe the same state before
// the login event starts Bridge / OneBot initialization.
this.client!.reconcileLoginState({
loggedIn: true,
uin: info.uin,
uinNumber: BigInt(info.uin),
});
}
} finally {
this.probing = false;
+17 -1
View File
@@ -374,6 +374,19 @@ export class QqHookClient extends EventEmitter {
return { ...this.loginState };
}
/**
* Reconcile a login edge that the native hook could not replay after the
* pipe connected. The client remains the single source of truth so packet
* sending, login waiters, and HookSession all observe the same transition.
*/
reconcileLoginState(state: QqHookLoginState): void {
this.applyLoginState({
loggedIn: state.loggedIn,
uin: state.uin || state.uinNumber.toString(),
uinNumber: state.uinNumber,
});
}
async waitForLogin({ timeoutMs = 0 } = {}): Promise<QqHookLoginState> {
await this.connect();
if (this.loginState.loggedIn) {
@@ -750,8 +763,11 @@ export class QqHookClient extends EventEmitter {
const loggedIn = flaggedLoggedIn || statusLoggedIn;
const uinNumber = BigInt(frame.value0);
const uin = frame.msg || uinNumber.toString();
this.applyLoginState({ loggedIn, uin, uinNumber });
}
private applyLoginState(next: QqHookLoginState): void {
const previous = this.loginState;
const next = { loggedIn, uin, uinNumber };
this.loginState = next;
this.emit('loginState', next);
if (previous.loggedIn !== next.loggedIn || previous.uin !== next.uin) {
+46 -3
View File
@@ -4,6 +4,7 @@ import { HookSession } from '../src/hook-session';
import type { ManualMapHandle } from '../src/injector';
import type { QqHookClient, QqHookPacket } from '../src/qq-hook-client';
import type { QqPortLoginInfo } from '../src/qq-port-probe';
import type { PacketSender } from '@snowluma/common/packet-sender';
import type { PacketSink } from '@snowluma/common/protocol-types';
const DUMMY_HANDLE: ManualMapHandle = { base: 0n, entry: 0n, exceptionTable: 0n, size: 0 };
@@ -14,6 +15,7 @@ class FakeClient extends EventEmitter {
isClosed = false;
isLoggedIn = false;
shouldFailConnect = false;
readonly sentPackets: Array<{ serviceCmd: string; body: Buffer }> = [];
private loginState = { loggedIn: false, uin: '0', uinNumber: 0n };
async connectAll(_opts: { recv: boolean }): Promise<void> {
@@ -21,6 +23,20 @@ class FakeClient extends EventEmitter {
if (this.isClosed) throw new Error('client is closed');
}
getLoginState() { return { ...this.loginState }; }
reconcileLoginState(state: { loggedIn: boolean; uin: string; uinNumber: bigint }): void {
this.isLoggedIn = state.loggedIn;
this.loginState = { ...state };
this.emit('loginState', { ...this.loginState });
}
async send(serviceCmd: string, body: Buffer) {
this.sentPackets.push({ serviceCmd, body: Buffer.from(body) });
return {
requestId: this.sentPackets.length,
error: 0,
message: '',
body: Buffer.from('reply'),
};
}
close(): void {
if (this.isClosed) return;
this.isClosed = true;
@@ -28,9 +44,7 @@ class FakeClient extends EventEmitter {
}
fireLogin(uin: string): void {
this.isLoggedIn = true;
this.loginState = { loggedIn: true, uin, uinNumber: BigInt(uin) };
this.emit('loginState', { ...this.loginState });
this.reconcileLoginState({ loggedIn: true, uin, uinNumber: BigInt(uin) });
}
firePacket(packet: QqHookPacket): void {
@@ -541,6 +555,35 @@ describe('HookSession — packet forwarding', () => {
});
describe('HookSession — login reconcile (Docker auto-login safety net)', () => {
it('keeps the packet sender usable when login is reconciled by the active probe', async () => {
const ctx = makeSession({
pipeLive: true,
probeLogin: async () => ({ port: 4301, uin: '10001', loggedIn: true }),
});
let sender: PacketSender | undefined;
ctx.session.on('login', (_uin: string, nextSender: PacketSender) => {
sender = nextSender;
});
await ctx.session.load();
ctx.session.onPipeUp();
await flush();
await flush();
expect(sender).toBeDefined();
const result = await sender!.sendPacket('Test.LoginReady', Buffer.from('request'));
expect(result).toMatchObject({
success: true,
gotResponse: true,
errorCode: 0,
responseData: Buffer.from('reply'),
});
expect(ctx.currentClient().sentPackets).toEqual([
{ serviceCmd: 'Test.LoginReady', body: Buffer.from('request') },
]);
});
it('reconciles login via the active probe when the pushed frame is missed', async () => {
// Probe reports logged-in; the FakeClient never fires a loginState frame
// (the missed-edge Docker case). The immediate reconcile probe must detect