Don't crash bootstrap on Matrix token capture failure

If chat.reddit.com sniffing fails, fall through with a placeholder
credential set so the post-monitoring/processor side of the bot
(and the web UI) stay up. Chat features are disabled until the
next successful token refresh.

Also dump diagnostics on sniff timeout: final page URL, every host
the page touched (with request counts + whether any carried an
Authorization header), and a full-page screenshot to data/matrix-debug.png.
This makes the three failure modes (page didn't load / loaded but
never hit Matrix / hit Matrix without auth) distinguishable from logs.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
jjbruntonandClaude Opus 4.7 committed 2026-05-22 21:09:38 +01:00
1 parent 665ff8b1f3
commit 4a3b44bc87
2 files changed
+58 -5

No files matched your search

+19 -1
View File
@@ -147,10 +147,28 @@ export async function bootstrap(config: Config, logger: Logger): Promise<AppCont
// ---- Matrix chat backend (replaces the Playwright scraper) ----
// 1. Get credentials: prefer cached, fall back to a fresh sniff via the
// persistent browser session that we just verified is logged in.
// If the sniff fails (e.g. egress blocked, chat.reddit.com refuses to
// load), we *don't* crash bootstrap — the post-monitoring/processor
// side of the bot doesn't need Matrix, and keeping the web UI up makes
// the situation diagnosable. Chat calls will fail with 401 until a
// subsequent token refresh succeeds.
let matrixCreds: MatrixCredentials | null = await loadCachedCredentials();
if (!matrixCreds) {
logger.info('No cached Matrix credentials — extracting via browser session...');
matrixCreds = await extractMatrixCredentials(browserClient.getContext()!, logger);
try {
matrixCreds = await extractMatrixCredentials(browserClient.getContext()!, logger);
} catch (err) {
logger.error(
'[matrix] credential extraction failed — chat features will be disabled until the next successful refresh:',
err,
);
matrixCreds = {
homeserver: 'https://matrix.redditspace.com',
accessToken: '',
userId: '',
capturedAt: Date.now(),
};
}
} else {
logger.info(`Loaded cached Matrix credentials (user=${matrixCreds.userId})`);
}