From 082abd088f459f66bc87cb6980663a7ce1eb4592 Mon Sep 17 00:00:00 2001 From: Robby Date: Wed, 17 Jun 2026 12:48:58 +0200 Subject: [PATCH] fix: reconnect race in bus-publisher + safe JSON.stringify in onWarn #45 --- src/bus-publisher.ts | 11 ++++++++--- src/four-opencode-token-budget-guard.ts | 9 ++++++++- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/bus-publisher.ts b/src/bus-publisher.ts index 588a9e3..1f6c203 100644 --- a/src/bus-publisher.ts +++ b/src/bus-publisher.ts @@ -53,10 +53,15 @@ export class BusPublisher { this.bus = null; if (!this.reconnecting) { this.reconnecting = true; - setTimeout(async () => { - this.reconnecting = false; + const reconnect = async (): Promise => { await this.init({ onWarn: this.onWarn }); - }, 5000); + if (!this.bus) { + setTimeout(reconnect, 5000); + return; + } + this.reconnecting = false; + }; + setTimeout(reconnect, 5000); } } } diff --git a/src/four-opencode-token-budget-guard.ts b/src/four-opencode-token-budget-guard.ts index 9850b57..73c21fa 100644 --- a/src/four-opencode-token-budget-guard.ts +++ b/src/four-opencode-token-budget-guard.ts @@ -37,7 +37,14 @@ export const FourTokenBudgetGuardPlugin: Plugin = async (ctx) => { service: "tbg", level: "warn", message: msg, - extra: { details: args.map(a => typeof a === 'object' ? JSON.stringify(a) : String(a)).join(" ") } + extra: { details: args.map(a => { + if (typeof a !== 'object' || a === null) return String(a); + try { + return JSON.stringify(a); + } catch { + return "[unserializable]"; + } + }).join(" ") } } }).catch(() => {}); }