diff --git a/src/bus-publisher.ts b/src/bus-publisher.ts index 6e949c6..588a9e3 100644 --- a/src/bus-publisher.ts +++ b/src/bus-publisher.ts @@ -7,10 +7,12 @@ export class BusPublisher { private lastPublish = 0; private publishInterval = 500; // ms — debounce private reconnecting = false; + private onWarn: ((message: string, ...args: unknown[]) => void) | undefined; - async init(): Promise { + async init(opts?: { onWarn?: (message: string, ...args: unknown[]) => void }): Promise { + this.onWarn = opts?.onWarn; try { - this.bus = await BusClient.connect({ timeoutMs: 3000 }); + this.bus = await BusClient.connect({ timeoutMs: 3000, onWarn: opts?.onWarn }); } catch { this.bus = null; } @@ -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 2999a92..9850b57 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 + await busPublisher.init({ + onWarn: (msg, ...args) => { + ctx.client.app.log({ + body: { + service: "tbg", + level: "warn", + message: msg, + extra: { details: args.map(a => typeof a === 'object' ? JSON.stringify(a) : String(a)).join(" ") } + } + }).catch(() => {}); + } + }); logDebugEvent("plugin.loaded", { directory: ctx.directory }); const policyConfig = loadPolicyConfig(); const policies: Policy[] = [new MaxStartTokensPolicy()];