feat(logging): establish trace diagnostics contract
Make short-lived TRACE sessions complete and correlatable without adding a persistent trace channel or changing the existing file-log safety boundary. Protect ordinary console, subscriber, and file logs from authentication material while preserving the one documented first-start credential notice. Keep action breadcrumbs useful through bounded summaries and a narrow diagnostic name for the emoji-like pagination offset. Lock the contract with regressions for byte-complete rendering, lazy execution, request-context reuse, credential redaction, unredacted TRACE data, and non-persistence of TRACE records.
This commit is contained in:
8 files changed
+712
-87
No files matched your search
@@ -16,6 +16,23 @@ import { RETCODE, failedResponse, okResponse } from './types';
|
||||
import { type StreamSink, wrapStreamFrame, wrapStreamTerminal } from './streaming';
|
||||
const moduleLog = createLogger('Bridge.Action');
|
||||
|
||||
function summarizeActionParams(action: string, params: JsonObject): string {
|
||||
if (action !== 'fetch_emoji_like') return summarizeParams(params);
|
||||
|
||||
const safeParams: JsonObject = { ...params };
|
||||
const cursor = params.cookie;
|
||||
if (typeof cursor !== 'string' || !/^\d+$/.test(cursor)) {
|
||||
return summarizeParams(safeParams);
|
||||
}
|
||||
|
||||
const offset = Number(cursor);
|
||||
if (!Number.isSafeInteger(offset)) return summarizeParams(safeParams);
|
||||
|
||||
delete safeParams.cookie;
|
||||
safeParams.emoji_like_offset = offset;
|
||||
return summarizeParams(safeParams);
|
||||
}
|
||||
|
||||
|
||||
export interface MessageSendResult {
|
||||
messageId: number;
|
||||
@@ -264,7 +281,7 @@ export class ApiHandler {
|
||||
): 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, summarizeParams(params));
|
||||
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)]);
|
||||
|
||||
@@ -93,6 +93,47 @@ describe('ApiHandler dispatch logging', () => {
|
||||
expect(entry!.message).toContain('message=[len=3]');
|
||||
});
|
||||
|
||||
it('logs the parsed emoji-like offset without exposing its cookie cursor', async () => {
|
||||
const handler = createCompiledTestHandler(emptyContext(), [
|
||||
testAction('fetch_emoji_like', async () => ({ status: 'ok', retcode: 0, data: null })),
|
||||
], 12345);
|
||||
|
||||
await handler.handle('fetch_emoji_like', {
|
||||
message_id: 1,
|
||||
emojiId: '66',
|
||||
cookie: '40',
|
||||
});
|
||||
|
||||
const entry = captured.find((e) => e.scope === 'Bridge.Action' && e.level === 'debug');
|
||||
expect(entry?.message).toContain('emoji_like_offset=40');
|
||||
expect(entry?.message).not.toContain('cookie=');
|
||||
});
|
||||
|
||||
it('keeps malformed emoji-like cookies redacted instead of parsing prefixes', async () => {
|
||||
const handler = createCompiledTestHandler(emptyContext(), [
|
||||
testAction('fetch_emoji_like', async () => ({ status: 'ok', retcode: 0, data: null })),
|
||||
], 12345);
|
||||
|
||||
await handler.handle('fetch_emoji_like', { cookie: '123.session=secret' });
|
||||
|
||||
const entry = captured.find((e) => e.scope === 'Bridge.Action' && e.level === 'debug');
|
||||
expect(entry?.message).toContain('cookie=***');
|
||||
expect(entry?.message).not.toContain('emoji_like_offset=');
|
||||
expect(entry?.message).not.toContain('session=secret');
|
||||
});
|
||||
|
||||
it('keeps cookie parameters redacted for every other action', async () => {
|
||||
const handler = createCompiledTestHandler(emptyContext(), [
|
||||
testAction('other_action', async () => ({ status: 'ok', retcode: 0, data: null })),
|
||||
], 12345);
|
||||
|
||||
await handler.handle('other_action', { cookie: '40' });
|
||||
|
||||
const entry = captured.find((e) => e.scope === 'Bridge.Action' && e.level === 'debug');
|
||||
expect(entry?.message).toContain('cookie=***');
|
||||
expect(entry?.message).not.toContain('cookie=40');
|
||||
});
|
||||
|
||||
it('emits a warn line with the error stack when the handler throws', async () => {
|
||||
const handler = createCompiledTestHandler(emptyContext(), [
|
||||
testAction('boom', async () => { throw new Error('kapow'); }),
|
||||
|
||||
@@ -21,24 +21,23 @@ describe('renderParamsVerbose', () => {
|
||||
expect(out).toContain('"456"');
|
||||
});
|
||||
|
||||
it('truncates long strings (e.g. base64) with a size marker', () => {
|
||||
it('preserves long strings such as inline base64 data', () => {
|
||||
const big = 'A'.repeat(5000);
|
||||
const out = renderParamsVerbose({ file: `base64://${big}` });
|
||||
expect(out).toContain('base64://');
|
||||
expect(out).toContain('…<'); // truncation marker
|
||||
expect(out).toContain('B>');
|
||||
expect(out.length).toBeLessThan(2000); // bounded by the total budget
|
||||
expect(out).toContain(`file:"base64://${big}"`);
|
||||
expect(out).not.toMatch(/…<\d+B>|\.\.\./);
|
||||
});
|
||||
|
||||
it('redacts sensitive keys at any depth', () => {
|
||||
it('keeps sensitive fields visible in explicit TRACE diagnostics', () => {
|
||||
const out = renderParamsVerbose({
|
||||
access_token: 'super-secret-value',
|
||||
nested: { password: 'hunter2', SECRET: 'x', ok: 'visible' },
|
||||
});
|
||||
expect(out).not.toContain('super-secret-value');
|
||||
expect(out).not.toContain('hunter2');
|
||||
expect(out).toContain('"***"');
|
||||
expect(out).toContain('"visible"'); // non-sensitive keys still rendered
|
||||
expect(out).toContain('super-secret-value');
|
||||
expect(out).toContain('hunter2');
|
||||
expect(out).toContain('SECRET:"x"');
|
||||
expect(out).not.toContain('"***"');
|
||||
expect(out).toContain('"visible"');
|
||||
});
|
||||
|
||||
it('handles circular references without throwing', () => {
|
||||
|
||||
Reference in new issue
Block a user