mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-11-17 21:32:52 +00:00
The adapter captures the bridge's stdout and stderr verbatim into bridge.log, so a console line without a stamp cannot be placed in time. The previous commit stamped 12 of the bridge's console calls one by one with a bracketed UTC ISO stamp; the other calls (config summary, pairing mode, reconnect-scheduler and version-resolver messages, inbound media download failures) stayed bare. This replaces the per-call wrapping with one installConsoleStamps() call at bridge startup that prefixes every console.log/warn/error line with `YYYY-MM-DD HH:MM:SS,mmm ` in local time, the shape the Python logs in logs/ already use. The 12 call sites go back to plain console calls. Lines that programs parse must stay bare. The dashboard pairing watcher runs json.loads on each stdout line of `bridge.js --pair-json`, so the JSON event lines (pair events, debug, poll_update_decode and `ignored` events) now go through writeJsonLine(), which writes to process.stdout directly. The terminal QR block is written the same way so its first row is not shifted. Pino (the Baileys logger) writes to fd 1 itself and never passes through console, so its lines are unchanged. Refs #97021
94 lines
3.3 KiB
JavaScript
94 lines
3.3 KiB
JavaScript
/**
|
|
* Unit tests for the bridge's log-line stamping: formatLogStamp,
|
|
* installConsoleStamps and writeJsonLine.
|
|
*
|
|
* Regression for issue #97021: startup/connection lifecycle console.log/
|
|
* console.warn lines (bridge listening, connected, logged out, reconnect,
|
|
* "[bridge] ..." warnings) carried no timestamp, and the platform adapter
|
|
* captures the bridge's stdout/stderr verbatim into bridge.log, making
|
|
* bridge.log impossible to sequence on its own during incident forensics.
|
|
* Machine-read JSON event lines (pair events parsed line-by-line by the
|
|
* dashboard pairing watcher, `ignored` events) must stay byte-identical.
|
|
*/
|
|
|
|
import { strict as assert } from 'node:assert';
|
|
|
|
import { formatLogStamp, installConsoleStamps, writeJsonLine } from './bridge_helpers.js';
|
|
|
|
const LOCAL_STAMP_PREFIX_RE = /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2},\d{3} /;
|
|
|
|
function captureWrites(stream, fn) {
|
|
const chunks = [];
|
|
const originalWrite = stream.write;
|
|
stream.write = (chunk, ...rest) => {
|
|
chunks.push(String(chunk));
|
|
const callback = rest.find(arg => typeof arg === 'function');
|
|
if (callback) callback();
|
|
return true;
|
|
};
|
|
try {
|
|
fn();
|
|
} finally {
|
|
stream.write = originalWrite;
|
|
}
|
|
return chunks;
|
|
}
|
|
|
|
function withStampedConsole(fn) {
|
|
const saved = { log: console.log, warn: console.warn, error: console.error };
|
|
installConsoleStamps();
|
|
try {
|
|
fn();
|
|
} finally {
|
|
Object.assign(console, saved);
|
|
}
|
|
}
|
|
|
|
// The stamp is local wall-clock time in Python's asctime shape.
|
|
{
|
|
assert.equal(formatLogStamp(new Date(2026, 8, 28, 3, 4, 5, 62)), '2026-09-28 03:04:05,062');
|
|
}
|
|
|
|
// A console.log line is stamped with the current local time, and the message
|
|
// text survives byte-for-byte after the stamp.
|
|
withStampedConsole(() => {
|
|
const before = Date.now();
|
|
const out = captureWrites(process.stdout, () => {
|
|
console.log('❌ Logged out. Delete session and restart to re-authenticate.');
|
|
});
|
|
|
|
assert.equal(out.length, 1);
|
|
assert.match(out[0], LOCAL_STAMP_PREFIX_RE);
|
|
assert.ok(out[0].endsWith(' ❌ Logged out. Delete session and restart to re-authenticate.\n'));
|
|
const [day, time] = out[0].split(' ');
|
|
const stampedMs = new Date(`${day}T${time.replace(',', '.')}`).getTime();
|
|
assert.ok(Math.abs(stampedMs - before) < 5000, `stamp ${day} ${time} is not local now`);
|
|
});
|
|
|
|
// console.warn keeps its multi-argument formatting and still goes to stderr.
|
|
withStampedConsole(() => {
|
|
const err = captureWrites(process.stderr, () => {
|
|
console.warn('[bridge] failed to send read receipt:', 'boom');
|
|
});
|
|
|
|
assert.equal(err.length, 1);
|
|
assert.match(err[0], LOCAL_STAMP_PREFIX_RE);
|
|
assert.ok(err[0].endsWith(' [bridge] failed to send read receipt: boom\n'));
|
|
});
|
|
|
|
// A JSON event line is not stamped, even with the console wrapped, so the
|
|
// pairing watcher's per-line json.loads still parses it.
|
|
withStampedConsole(() => {
|
|
const out = captureWrites(process.stdout, () => {
|
|
writeJsonLine({ ts: 1790000000000, event: 'qr', qr: 'abc' });
|
|
});
|
|
|
|
assert.deepEqual(out, ['{"ts":1790000000000,"event":"qr","qr":"abc"}\n']);
|
|
});
|
|
|
|
// An empty console.log() stays a bare newline rather than a lone stamp.
|
|
withStampedConsole(() => {
|
|
assert.deepEqual(captureWrites(process.stdout, () => console.log()), ['\n']);
|
|
});
|
|
|
|
console.log('bridge_helpers.timestamp.test.mjs: all assertions passed');
|