feat(logging): complete runtime trace diagnostics
Complete byte-level diagnostics for network, upload, runtime lifecycle, authenticated write, and background operation paths. Preserve streaming behavior, file-storage boundaries, and default log volume while making failures, retries, cancellation, and final outcomes independently diagnosable. Add focused regression coverage for correlation, partial data, resource context ownership, and terminal-state accuracy.
This commit is contained in:
27 files changed
+4029
-352
No files matched your search
@@ -1,5 +1,11 @@
|
||||
import { EventEmitter } from 'events';
|
||||
import { createLogger, renderTraceBytes } from '@snowluma/common/logger';
|
||||
import {
|
||||
createLogger,
|
||||
renderTraceBytes,
|
||||
runWithoutRequestContext,
|
||||
runWithTraceRequest,
|
||||
} from '@snowluma/common/logger';
|
||||
import { renderParamsVerbose } from '@snowluma/common/log-summary';
|
||||
import { readdirSync, promises as fs } from 'fs';
|
||||
import net from 'net';
|
||||
import os from 'os';
|
||||
@@ -12,6 +18,7 @@ export const DEFAULT_ACK_TIMEOUT_MS = 5000;
|
||||
export const DEFAULT_REPLY_TIMEOUT_MS = 30000;
|
||||
const DEFAULT_PIPE_PROBE_TIMEOUT_MS = 250;
|
||||
const packetLog = createLogger('QQHook.Packet');
|
||||
const runtimeLog = createLogger('QQHook.Runtime');
|
||||
|
||||
export enum PipeOp {
|
||||
hello = 1,
|
||||
@@ -479,14 +486,7 @@ export class QqHookClient extends EventEmitter {
|
||||
if (this.controlConnectPromise) {
|
||||
return this.controlConnectPromise;
|
||||
}
|
||||
this.controlConnectPromise = (async () => {
|
||||
this.controlSocket = await this.connectSocket(
|
||||
QqHookClient.controlPipeName(this.pid),
|
||||
'control',
|
||||
frame => this.handleControlFrame(frame));
|
||||
this.controlHello = await this.waitForHello(false);
|
||||
return this.controlHello;
|
||||
})();
|
||||
this.controlConnectPromise = this.connectPipe('control');
|
||||
try {
|
||||
return await this.controlConnectPromise;
|
||||
} finally {
|
||||
@@ -507,14 +507,7 @@ export class QqHookClient extends EventEmitter {
|
||||
if (this.recvConnectPromise) {
|
||||
return this.recvConnectPromise;
|
||||
}
|
||||
this.recvConnectPromise = (async () => {
|
||||
this.recvSocket = await this.connectSocket(
|
||||
QqHookClient.recvPipeName(this.pid),
|
||||
'recv',
|
||||
frame => this.handleRecvFrame(frame));
|
||||
this.recvHello = await this.waitForHello(true);
|
||||
return this.recvHello;
|
||||
})();
|
||||
this.recvConnectPromise = this.connectPipe('recv');
|
||||
try {
|
||||
return await this.recvConnectPromise;
|
||||
} finally {
|
||||
@@ -770,6 +763,60 @@ export class QqHookClient extends EventEmitter {
|
||||
return writePromise;
|
||||
}
|
||||
|
||||
private async connectPipe(kind: 'control' | 'recv'): Promise<QqHookHello> {
|
||||
return runWithTraceRequest(async () => {
|
||||
const startedAt = Date.now();
|
||||
const pipeName = kind === 'control'
|
||||
? QqHookClient.controlPipeName(this.pid)
|
||||
: QqHookClient.recvPipeName(this.pid);
|
||||
let phase: 'connect' | 'hello' = 'connect';
|
||||
runtimeLog.trace(
|
||||
'hook_pipe_start pid=%d kind=%s pipeName=%j',
|
||||
this.pid,
|
||||
kind,
|
||||
pipeName,
|
||||
);
|
||||
try {
|
||||
const socket = await runWithoutRequestContext(() => this.connectSocket(
|
||||
pipeName,
|
||||
kind,
|
||||
kind === 'control'
|
||||
? frame => this.handleControlFrame(frame)
|
||||
: frame => this.handleRecvFrame(frame),
|
||||
));
|
||||
if (kind === 'control') this.controlSocket = socket;
|
||||
else this.recvSocket = socket;
|
||||
runtimeLog.trace(
|
||||
'hook_pipe_branch pid=%d kind=%s branch=socket_connected',
|
||||
this.pid,
|
||||
kind,
|
||||
);
|
||||
phase = 'hello';
|
||||
const hello = await this.waitForHello(kind === 'recv');
|
||||
if (kind === 'control') this.controlHello = hello;
|
||||
else this.recvHello = hello;
|
||||
runtimeLog.trace(() => [
|
||||
'hook_pipe_terminal pid=%d kind=%s outcome=completed reason=hello_received hello=%s elapsedMs=%d',
|
||||
this.pid,
|
||||
kind,
|
||||
renderParamsVerbose(hello),
|
||||
Date.now() - startedAt,
|
||||
]);
|
||||
return hello;
|
||||
} catch (error) {
|
||||
runtimeLog.trace(() => [
|
||||
'hook_pipe_terminal pid=%d kind=%s outcome=failed reason=%s error=%j elapsedMs=%d',
|
||||
this.pid,
|
||||
kind,
|
||||
phase === 'connect' ? 'connect_failed' : 'hello_failed',
|
||||
error instanceof Error ? error.message : String(error),
|
||||
Date.now() - startedAt,
|
||||
]);
|
||||
throw error;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private connectSocket(pipeName: string, kind: 'control' | 'recv', onFrame: (frame: PipeFrame) => void): Promise<net.Socket> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const socket = net.createConnection(pipeName);
|
||||
@@ -790,8 +837,10 @@ export class QqHookClient extends EventEmitter {
|
||||
this.emit('error', error);
|
||||
});
|
||||
socket.on('close', () => {
|
||||
this.handleSocketClose(kind);
|
||||
this.emit('close', kind);
|
||||
runWithTraceRequest(() => {
|
||||
this.handleSocketClose(kind);
|
||||
this.emit('close', kind);
|
||||
});
|
||||
});
|
||||
resolve(socket);
|
||||
});
|
||||
@@ -821,6 +870,13 @@ export class QqHookClient extends EventEmitter {
|
||||
}
|
||||
|
||||
private handleSocketClose(kind: 'control' | 'recv'): void {
|
||||
runtimeLog.trace(
|
||||
'hook_runtime_fact pid=%d event=pipe_closed kind=%s loggedIn=%s uin=%j',
|
||||
this.pid,
|
||||
kind,
|
||||
this.loginState.loggedIn,
|
||||
this.loginState.uin,
|
||||
);
|
||||
if (kind === 'control') {
|
||||
this.controlSocket = null;
|
||||
this.controlHello = null;
|
||||
@@ -844,14 +900,26 @@ export class QqHookClient extends EventEmitter {
|
||||
private applyLoginState(next: QqHookLoginState): void {
|
||||
const previous = this.loginState;
|
||||
this.loginState = next;
|
||||
this.emit('loginState', next);
|
||||
if (previous.loggedIn !== next.loggedIn || previous.uin !== next.uin) {
|
||||
if (previous.loggedIn === next.loggedIn && previous.uin === next.uin) {
|
||||
this.emit('loginState', next);
|
||||
return;
|
||||
}
|
||||
runWithTraceRequest(() => {
|
||||
runtimeLog.trace(
|
||||
'hook_runtime_fact pid=%d event=login_state_changed previousLoggedIn=%s previousUin=%j loggedIn=%s uin=%j',
|
||||
this.pid,
|
||||
previous.loggedIn,
|
||||
previous.uin,
|
||||
next.loggedIn,
|
||||
next.uin,
|
||||
);
|
||||
this.emit('loginState', next);
|
||||
if (next.loggedIn) {
|
||||
const waiters = this.loginWaiters;
|
||||
this.loginWaiters = [];
|
||||
for (const waiter of waiters) waiter.resolve({ ...next });
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private handleControlFrame(frame: PipeFrame): void {
|
||||
|
||||
Reference in new issue
Block a user