From 5bd146caedd16a1f791cb6b0d3303ddb3cdb5c7a Mon Sep 17 00:00:00 2001 From: Robby Date: Wed, 17 Jun 2026 10:01:17 +0200 Subject: [PATCH 1/2] fix: pass onWarn callback to BusClient for app.log routing #21 --- src/bus-publisher.ts | 4 ++-- src/four-opencode-token-budget-guard.ts | 15 ++++++++++++++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/bus-publisher.ts b/src/bus-publisher.ts index 6e949c6..01e8534 100644 --- a/src/bus-publisher.ts +++ b/src/bus-publisher.ts @@ -8,9 +8,9 @@ export class BusPublisher { private publishInterval = 500; // ms — debounce private reconnecting = false; - async init(): Promise { + async init(opts?: { onWarn?: (message: string, ...args: unknown[]) => void }): Promise { try { - this.bus = await BusClient.connect({ timeoutMs: 3000 }); + this.bus = await BusClient.connect({ timeoutMs: 3000, onWarn: opts?.onWarn }); } catch { this.bus = null; } diff --git a/src/four-opencode-token-budget-guard.ts b/src/four-opencode-token-budget-guard.ts index 2999a92..f7d3a29 100644 --- a/src/four-opencode-token-budget-guard.ts +++ b/src/four-opencode-token-budget-guard.ts @@ -22,13 +22,26 @@ const sessionTokens = new SessionTokenCache( // ── Plugin Bus (P4d) ──────────────────────────────────── const busPublisher = new BusPublisher(); -busPublisher.init(); // fire-and-forget — connects if bus available +// init() is called inside the Plugin callback once ctx.client is available // Track current session ID (set on first event) let currentSessionID = ""; export const FourTokenBudgetGuardPlugin: Plugin = async (ctx) => { const config = loadConfig(); + // Initialize bus publisher with app.log for warning messages + busPublisher.init({ + onWarn: (msg, ...args) => { + ctx.client.app.log({ + body: { + service: "tbg", + level: "warn", + message: msg, + extra: { details: args.map(String).join(" ") } + } + }).catch(() => {}); + } + }); logDebugEvent("plugin.loaded", { directory: ctx.directory }); const policyConfig = loadPolicyConfig(); const policies: Policy[] = [new MaxStartTokensPolicy()]; From 0416924af3b4a6a1b14de58efcfca0113b72dbae Mon Sep 17 00:00:00 2001 From: Robby Date: Wed, 17 Jun 2026 10:11:49 +0200 Subject: [PATCH 2/2] fix: store onWarn for reconnect, await init, fix string coercion #21 --- src/bus-publisher.ts | 4 +++- src/four-opencode-token-budget-guard.ts | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/bus-publisher.ts b/src/bus-publisher.ts index 01e8534..588a9e3 100644 --- a/src/bus-publisher.ts +++ b/src/bus-publisher.ts @@ -7,8 +7,10 @@ export class BusPublisher { private lastPublish = 0; private publishInterval = 500; // ms — debounce private reconnecting = false; + private onWarn: ((message: string, ...args: unknown[]) => void) | undefined; async init(opts?: { onWarn?: (message: string, ...args: unknown[]) => void }): Promise { + this.onWarn = opts?.onWarn; try { this.bus = await BusClient.connect({ timeoutMs: 3000, onWarn: opts?.onWarn }); } catch { @@ -53,7 +55,7 @@ export class BusPublisher { this.reconnecting = true; setTimeout(async () => { this.reconnecting = false; - await this.init(); + await this.init({ onWarn: this.onWarn }); }, 5000); } } diff --git a/src/four-opencode-token-budget-guard.ts b/src/four-opencode-token-budget-guard.ts index f7d3a29..9850b57 100644 --- a/src/four-opencode-token-budget-guard.ts +++ b/src/four-opencode-token-budget-guard.ts @@ -30,14 +30,14 @@ let currentSessionID = ""; export const FourTokenBudgetGuardPlugin: Plugin = async (ctx) => { const config = loadConfig(); // Initialize bus publisher with app.log for warning messages - busPublisher.init({ + await busPublisher.init({ onWarn: (msg, ...args) => { ctx.client.app.log({ body: { service: "tbg", level: "warn", message: msg, - extra: { details: args.map(String).join(" ") } + extra: { details: args.map(a => typeof a === 'object' ? JSON.stringify(a) : String(a)).join(" ") } } }).catch(() => {}); }