fix(bridge): recover stale outbound sessions

Recover invalidated native send bindings from same-account receive evidence. Track outbound connection loss in status health and refresh the six public native artifacts from native build 94f2b63.

Closes #342
This commit is contained in:
DrSmoothl committed 2026-08-09 21:40:09 +08:00
1 parent 129b091081
commit 870e9a5fbc
18 files changed
+196 -23

No files matched your search

+1 -1
View File
@@ -81,7 +81,7 @@ export type HookManagerDeps = {
* Responsibilities:
* - Route user commands (load/unload/refresh) to the matching session.
* - Route watcher diff events to the matching session.
* - Forward session events ('login' / 'disconnected' / receive health) to BridgeManager.
* - Forward session events ('login' / 'disconnected' / transport health) to BridgeManager.
* - Retry stuck-in-connecting sessions on every watcher tick (so a
* failed connect eventually recovers without a manual refresh).
* - Retry the bounded early-process auto-load race without retrying
+25 -2
View File
@@ -1,8 +1,20 @@
import type { PacketSender, SendPacketResult } from '@snowluma/common/packet-sender';
import { QqHookClient, type QqHookSendReply } from './qq-hook-client';
import {
HookPipeRequestError,
PIPE_STATUS_CONNECTION_UNAVAILABLE,
QqHookClient,
type QqHookSendReply,
} from './qq-hook-client';
export type OutboundHealthListener = (healthy: boolean) => void;
export class HookPacketClient implements PacketSender {
constructor(private readonly client: QqHookClient) { }
private outboundHealthy = true;
constructor(
private readonly client: QqHookClient,
private readonly onOutboundHealthChanged?: OutboundHealthListener,
) { }
async sendPacket(serviceCmd: string, body: Buffer, timeoutMs = 15000): Promise<SendPacketResult> {
if (!this.client.isLoggedIn) {
@@ -14,6 +26,7 @@ export class HookPacketClient implements PacketSender {
wantReply: true,
replyTimeoutMs: timeoutMs,
}) as QqHookSendReply;
this.setOutboundHealthy(true);
return {
success: reply.error === 0,
gotResponse: true,
@@ -22,6 +35,10 @@ export class HookPacketClient implements PacketSender {
responseData: reply.body,
};
} catch (error) {
if (error instanceof HookPipeRequestError
&& error.status === PIPE_STATUS_CONNECTION_UNAVAILABLE) {
this.setOutboundHealthy(false);
}
return {
success: false,
gotResponse: false,
@@ -31,4 +48,10 @@ export class HookPacketClient implements PacketSender {
};
}
}
private setOutboundHealthy(healthy: boolean): void {
if (this.outboundHealthy === healthy) return;
this.outboundHealthy = healthy;
this.onOutboundHealthChanged?.(healthy);
}
}
+37 -5
View File
@@ -69,7 +69,7 @@ export type HookSessionDeps = {
* Emitted events:
* 'login' (uin, packetSender) — real-UIN login detected
* 'disconnected' (wasLoggedIn) — connection dropped or torn down
* 'receive-health-changed' (healthy) — receive path confirmed stale/recovered
* 'receive-health-changed' (healthy) — combined receive/request path health changed
* 'status-changed' (status, error) — status field mutated
* 'disposed' () — session stopped tracking this PID
*/
@@ -107,6 +107,7 @@ export class HookSession extends EventEmitter {
private nextLoginHintAt = 0;
private nextLoginIdentityObservationAt = 0;
private _receiveHealthy = true;
private _outboundHealthy = true;
private receiveWatchUin = '';
private receiveWatchArmed = false;
private receiveWatchTimer: ReturnType<typeof setTimeout> | null = null;
@@ -130,7 +131,9 @@ export class HookSession extends EventEmitter {
get uin(): string { return this._uin; }
get method(): string { return this._method; }
get isDisposed(): boolean { return this.disposed; }
get receiveHealthy(): boolean { return this._receiveHealthy; }
get receiveHealthy(): boolean {
return this._receiveHealthy && this._outboundHealthy;
}
attachProcessInfo(info: { name?: string; path?: string }): void {
if (info.name) this._name = info.name;
@@ -499,7 +502,10 @@ export class HookSession extends EventEmitter {
if (this.client?.isClosed) this.tearDownClient();
if (!this.client) {
this.client = this.makeClient(this.pid);
this.sender = new HookPacketClient(this.client);
this.sender = new HookPacketClient(
this.client,
healthy => this.setOutboundHealthy(healthy),
);
this.bound = false;
}
if (!this.bound) {
@@ -582,6 +588,7 @@ export class HookSession extends EventEmitter {
this.bound = false;
this.connected = false;
this.loggedIn = false;
this._outboundHealthy = true;
this.resetLoginHint();
}
@@ -595,9 +602,13 @@ export class HookSession extends EventEmitter {
this.stopLoginReconcile();
// A PID can switch accounts without an intermediate logout edge. Never
// carry the previous account's heartbeat deadline into the new epoch.
if (this.receiveWatchUin !== this._uin) this.resetReceiveWatch(this._uin);
if (this.receiveWatchUin !== this._uin) {
this.resetReceiveWatch(this._uin);
this._outboundHealthy = true;
}
} else {
this.resetReceiveWatch();
this._outboundHealthy = true;
}
// Only the connected/logged-in states are ours to set here; when fully
@@ -844,8 +855,29 @@ export class HookSession extends EventEmitter {
private setReceiveHealthy(healthy: boolean): void {
if (this._receiveHealthy === healthy) return;
const previous = this.receiveHealthy;
this._receiveHealthy = healthy;
this.emit('receive-health-changed', healthy);
if (previous !== this.receiveHealthy) {
this.emit('receive-health-changed', this.receiveHealthy);
}
}
private setOutboundHealthy(healthy: boolean): void {
if (this._outboundHealthy === healthy) return;
const previous = this.receiveHealthy;
this._outboundHealthy = healthy;
if (previous !== this.receiveHealthy) {
this.emit('receive-health-changed', this.receiveHealthy);
}
if (healthy) {
this.log.info('request path recovered: PID=%d UIN=%s', this.pid, this._uin);
} else {
this.log.warn(
'request path unavailable: PID=%d UIN=%s; reporting good=false',
this.pid,
this._uin,
);
}
}
private resetReceiveWatch(uin = ''): void {
+12 -3
View File
@@ -23,6 +23,7 @@ export const MAX_PIPE_MSG_BYTES = 65536;
export const MAX_PIPE_BODY_BYTES = 16 * 1024 * 1024;
export const DEFAULT_ACK_TIMEOUT_MS = 5000;
export const DEFAULT_REPLY_TIMEOUT_MS = 30000;
export const PIPE_STATUS_CONNECTION_UNAVAILABLE = -39;
const DEFAULT_PIPE_PROBE_TIMEOUT_MS = 250;
const packetLog = createLogger('QQHook.Packet');
const runtimeLog = createLogger('QQHook.Runtime');
@@ -273,8 +274,12 @@ function withTimeout<T>(promise: Promise<T>, timeoutMs: number, label: string):
});
}
class HookPipeRequestError extends Error {
constructor(message: string) {
export class HookPipeRequestError extends Error {
constructor(
message: string,
readonly status: number,
readonly requestId: number,
) {
super(message);
this.name = 'HookPipeRequestError';
}
@@ -1064,7 +1069,11 @@ export class QqHookClient extends EventEmitter {
return;
}
if (frame.op === PipeOp.error) {
const error = new HookPipeRequestError(frame.msg || `pipe error ${frame.status}`);
const error = new HookPipeRequestError(
frame.msg || `pipe error ${frame.status}`,
frame.status,
frame.requestId,
);
const ack = this.pendingAcks.get(frame.requestId);
if (ack) {
this.pendingAcks.delete(frame.requestId);