feat(onebot): complete trace operation chains

Make short TRACE captures independently explain OneBot action and event lifecycles from complete input through a truthful terminal conclusion. Preserve request correlation across asynchronous work while keeping disabled TRACE paths allocation-free and lazy.

Cover shutdown rejection, stream cancellation, event conversion and suppression, reporter invocation settlement, and HTTP delivery outcomes without changing established transport or observer behavior. Add focused regression coverage for lifecycle uniqueness, context reuse, disconnect handling, and quiesce ingress boundaries.
This commit is contained in:
DrSmoothl committed 2026-07-30 17:13:40 +08:00
1 parent b9104f0a5b
commit f9c4063fc5
20 files changed
+1304 -154

No files matched your search

+126 -34
View File
@@ -1,5 +1,5 @@
import { renderParamsVerbose, summarizeParams } from '@snowluma/common/log-summary';
import { createLogger, getLogLevel, nextRequestId, runWithRequestId, type Logger } from '@snowluma/common/logger';
import { createLogger, runWithTraceRequest, type Logger } from '@snowluma/common/logger';
import type { BridgeInterface } from '@snowluma/core/bridge-interface';
import { MessageElementValidationError } from '@snowluma/protocol/element-manifest';
import {
@@ -13,7 +13,12 @@ import type { ForwardPreviewMeta } from './modules/message-actions';
import type { ReadSessionTargets } from './message-store';
import type { JsonObject, JsonValue, MessageMeta } from './types';
import { RETCODE, failedResponse, okResponse } from './types';
import { type StreamSink, wrapStreamFrame, wrapStreamTerminal } from './streaming';
import {
StreamTransportClosedError,
type StreamSink,
wrapStreamFrame,
wrapStreamTerminal,
} from './streaming';
const moduleLog = createLogger('Bridge.Action');
function summarizeActionParams(action: string, params: JsonObject): string {
@@ -251,46 +256,91 @@ export class ApiHandler {
}
async handle(action: string, params: JsonObject, sink?: StreamSink): Promise<import('./types').ApiResponse> {
return runWithTraceRequest(() => this.handleInContext(action, params, sink));
}
traceQuiescedAction(
action: string,
params: JsonObject,
response: import('./types').ApiResponse,
): void {
if (this.acceptingActions) return;
runWithTraceRequest(() => {
const startedAt = Date.now();
this.traceActionInput(action, params);
this.traceActionTerminal(action, response, startedAt, 'failed', 'quiesced');
});
}
traceQuiescedStreamRequest(rawRequest: string): void {
const request = parseStreamRequest(rawRequest);
if (!request) return;
this.traceQuiescedAction(
request.action,
request.params,
failedResponse(RETCODE.ACTION_FAILED, 'OneBot instance is shutting down'),
);
}
private async handleInContext(
action: string,
params: JsonObject,
sink?: StreamSink,
): Promise<import('./types').ApiResponse> {
const startedAt = Date.now();
this.traceActionInput(action, params);
if (!this.acceptingActions) {
const response = failedResponse(RETCODE.ACTION_FAILED, 'OneBot instance is shutting down');
this.log.warn('rejected Action %s after instance quiesce', action);
this.traceActionTerminal(action, response, startedAt, 'failed', 'quiesced');
this.notifyObservers(action, params, response, 0);
return response;
}
const registered = this.handlers.get(action);
if (!registered) {
const response = failedResponse(RETCODE.UNKNOWN_ACTION, 'unknown action');
this.log.debug('unknown action %s', action);
return failedResponse(RETCODE.UNKNOWN_ACTION, 'unknown action');
this.traceActionTerminal(action, response, startedAt, 'failed', 'unknown_action');
return response;
}
// Correlate the whole request — entry, every outbound packet it triggers,
// and the exit — under one `[req#N]` tag via AsyncLocalStorage. Only pay
// the wrap + id allocation when trace is actually live, so the default
// path stays allocation-free.
if (getLogLevel() !== 'trace') {
return this.runAction(action, registered.handler, params, sink);
}
return runWithRequestId(nextRequestId(), () => this.runAction(action, registered.handler, params, sink));
return this.runAction(action, registered.handler, params, sink, startedAt);
}
private async runAction(
action: string,
handler: ActionHandler,
params: JsonObject,
sink?: StreamSink,
sink: StreamSink | undefined,
startedAt: number,
): Promise<import('./types').ApiResponse> {
// Terse breadcrumb to the log file (debug, which is written to disk under the default
// file level): lets the operator grep "what did the bot get asked to do" in post-mortems.
this.log.debug('%s params=%s', action, summarizeActionParams(action, params));
// Full request shape, memory-only (trace). Lazy producer → the deep render
// only runs when trace is live.
this.log.trace(() => [`${action} ⇐ %s`, renderParamsVerbose(params)]);
const startedAt = Date.now();
let response: import('./types').ApiResponse;
let outcome: 'ok' | 'failed' | 'cancelled';
let reason: 'response_returned' | 'handler_threw' | 'transport_closed';
try {
response = await handler(params, sink);
this.log.trace(() => [`${action} ⇒ ${response.status} (${Date.now() - startedAt}ms)`]);
response = await handler(
params,
sink
? {
send: async (frame) => {
this.log.trace(() => [
'action_stream_frame action=%s frame=%s',
action,
renderParamsVerbose(frame),
]);
await sink.send(frame);
},
}
: undefined,
);
outcome = response.status === 'ok' ? 'ok' : 'failed';
reason = 'response_returned';
} catch (error) {
// Single error seam: typed message-contract failures are caller errors
// and therefore map to BAD_REQUEST. Bridge/OIDB business failures and
@@ -309,11 +359,42 @@ export class ApiHandler {
? RETCODE.BAD_REQUEST
: RETCODE.ACTION_FAILED;
response = failedResponse(retcode, message);
outcome = error instanceof StreamTransportClosedError ? 'cancelled' : 'failed';
reason = error instanceof StreamTransportClosedError ? 'transport_closed' : 'handler_threw';
}
this.traceActionTerminal(action, response, startedAt, outcome, reason);
this.notifyObservers(action, params, response, Date.now() - startedAt);
return response;
}
private traceActionInput(
action: string,
params: JsonObject,
): void {
this.log.trace(() => [
'action_input action=%s params=%s',
action,
renderParamsVerbose(params),
]);
}
private traceActionTerminal(
action: string,
response: import('./types').ApiResponse,
startedAt: number,
outcome: 'ok' | 'failed' | 'cancelled',
reason: 'response_returned' | 'handler_threw' | 'transport_closed' | 'unknown_action' | 'quiesced',
): void {
this.log.trace(() => [
'action_terminal action=%s outcome=%s reason=%s ms=%d response=%s',
action,
outcome,
reason,
Date.now() - startedAt,
renderParamsVerbose(response),
]);
}
private notifyObservers(
action: string,
params: JsonObject,
@@ -345,21 +426,9 @@ export class ApiHandler {
);
if (!rawRequest.trim()) { await bad(); return; }
let action: string;
let params: JsonObject;
let echo: JsonValue | undefined;
try {
const parsed = JSON.parse(rawRequest) as unknown;
if (!isJsonObject(parsed)) { await bad(); return; }
const a = asString(parsed.action);
if (!a) { await bad(); return; }
action = a;
params = isJsonObject(parsed.params) ? parsed.params : {};
echo = parsed.echo !== undefined ? toJsonValue(parsed.echo) : undefined;
} catch {
await bad();
return;
}
const request = parseStreamRequest(rawRequest);
if (!request) { await bad(); return; }
const { action, params, echo } = request;
if (!this.isStreamAction(action)) {
const response = await this.handle(action, params);
@@ -370,7 +439,7 @@ export class ApiHandler {
const sink: StreamSink = {
send: async (frame) => {
if (isAlive && !isAlive()) throw new Error('stream transport closed');
if (isAlive && !isAlive()) throw new StreamTransportClosedError();
await emit(JSON.stringify(wrapStreamFrame(frame, echo)));
},
};
@@ -379,6 +448,29 @@ export class ApiHandler {
}
}
interface StreamRequest {
action: string;
params: JsonObject;
echo: JsonValue | undefined;
}
function parseStreamRequest(rawRequest: string): StreamRequest | null {
if (!rawRequest.trim()) return null;
try {
const parsed = JSON.parse(rawRequest) as unknown;
if (!isJsonObject(parsed)) return null;
const action = asString(parsed.action);
if (!action) return null;
return {
action,
params: isJsonObject(parsed.params) ? parsed.params : {},
echo: parsed.echo !== undefined ? toJsonValue(parsed.echo) : undefined,
};
} catch {
return null;
}
}
function isJsonObject(value: unknown): value is JsonObject {
return typeof value === 'object' && value !== null && !Array.isArray(value);
}