diff --git a/packages/core/src/webui/connection-diff-loop.ts b/packages/core/src/webui/connection-diff-loop.ts index cb911ea4..d5542147 100644 --- a/packages/core/src/webui/connection-diff-loop.ts +++ b/packages/core/src/webui/connection-diff-loop.ts @@ -17,6 +17,14 @@ export interface ConnectionDiffLoopOptions { bus: StateBus; /** Read the current adapter-status snapshot for every live UIN. */ getSnapshot: () => unknown; + /** Optional projector — return the "comparable" subset of the snapshot. + * The full snapshot is what the SSE handler ships to the WebUI; this + * projection is what the diff loop compares between ticks. Use it to + * strip volatile fields (e.g. HH:MM:SS timestamps embedded in adapter + * `detail` strings) that would otherwise produce a publish every tick + * under any active webhook — defeating the loop's whole purpose. + * Default: identity. */ + pickComparable?: (snapshot: unknown) => unknown; /** Poll cadence in milliseconds; default 500. */ intervalMs?: number; } @@ -43,7 +51,14 @@ export function startConnectionDiffLoop(opts: ConnectionDiffLoopOptions): Connec // spuriously republish when the snapshot starts working again. return; } - const serialized = JSON.stringify(snap); + let comparable: unknown; + try { + comparable = opts.pickComparable ? opts.pickComparable(snap) : snap; + } catch { + // A buggy projector must not crash the diff loop or wedge baseline. + return; + } + const serialized = JSON.stringify(comparable); if (!haveBaseline) { // First successful observation. The SSE handler's sendAllInitial // already shipped a `connections` frame to every connected client diff --git a/packages/core/src/webui/server.ts b/packages/core/src/webui/server.ts index 9a1bef5d..3fc19eb2 100644 --- a/packages/core/src/webui/server.ts +++ b/packages/core/src/webui/server.ts @@ -335,10 +335,31 @@ export async function initWebUI( // listening/connected/client-count changes, so we poll the snapshot at // 500ms and publish 'connections' to the StateBus when it actually // moves. One loop per server lifetime, lives until process exit. + // + // `pickComparable` strips `adapter.detail` — that's a localised human + // string assembled with HH:MM:SS timestamps from the last webhook + // delivery; including it in the diff would make every webhook event + // produce a publish (defeating the diff). We compare ONLY name + kind + + // status, which captures every transition the dashboard actually + // renders differently. The SSE handler still ships the FULL snapshot + // (with `detail`) — it's only the diff key that's pruned. if (listener.stateBus) { startConnectionDiffLoop({ bus: listener.stateBus, getSnapshot: () => oneBotManager.getConnectionStatuses(), + pickComparable: (snap) => { + if (!Array.isArray(snap)) return snap; + return (snap as Array<{ uin: string; nickname?: string; adapters?: unknown[] }>).map((acc) => ({ + uin: acc.uin, + nickname: acc.nickname, + adapters: Array.isArray(acc.adapters) + ? acc.adapters.map((a: unknown) => { + const o = a as { name?: string; kind?: string; status?: string }; + return { name: o.name, kind: o.kind, status: o.status }; + }) + : acc.adapters, + })); + }, intervalMs: 500, }); } diff --git a/packages/core/tests/webui-connection-diff-loop.test.ts b/packages/core/tests/webui-connection-diff-loop.test.ts index 7a56614d..3e1dbc47 100644 --- a/packages/core/tests/webui-connection-diff-loop.test.ts +++ b/packages/core/tests/webui-connection-diff-loop.test.ts @@ -84,6 +84,63 @@ describe('startConnectionDiffLoop', () => { expect(seen).toEqual([]); }); + it('pickComparable() strips volatile fields so detail-only changes do NOT publish', () => { + const bus = new StateBus(); + const seen: string[] = []; + bus.subscribe((r) => seen.push(r)); + // Reproduces the real-world bug: HttpPostAdapter.describeStatus() + // embeds HH:MM:SS in `detail`; every webhook delivery rotates that + // string every second. WITHOUT pickComparable, the loop would publish + // every tick under any active webhook. WITH it (stripping `detail`), + // only level changes publish. + let snap = [{ uin: '1', adapters: [{ kind: 'httpClient', status: 'ok', detail: '上次推送 14:00:00' }] }]; + const handle = startConnectionDiffLoop({ + bus, + getSnapshot: () => snap, + pickComparable: (s) => { + if (!Array.isArray(s)) return s; + return (s as Array<{ uin: string; adapters?: unknown[] }>).map((acc) => ({ + uin: acc.uin, + adapters: Array.isArray(acc.adapters) + ? acc.adapters.map((a: unknown) => { + const o = a as { name?: string; kind?: string; status?: string }; + return { name: o.name, kind: o.kind, status: o.status }; + }) + : acc.adapters, + })); + }, + intervalMs: 500, + }); + vi.advanceTimersByTime(500); // baseline + expect(seen).toEqual([]); + // detail changes every second; raw JSON differs each tick. + for (let t = 1; t <= 10; t++) { + snap = [{ uin: '1', adapters: [{ kind: 'httpClient', status: 'ok', detail: `上次推送 14:00:${String(t).padStart(2, '0')}` }] }]; + vi.advanceTimersByTime(500); + } + expect(seen).toEqual([]); // ZERO publishes — detail is not comparable + // Now a REAL state change (status level) → publish exactly once. + snap = [{ uin: '1', adapters: [{ kind: 'httpClient', status: 'down', detail: '连接失败' }] }]; + vi.advanceTimersByTime(500); + expect(seen).toEqual(['connections']); + handle.dispose(); + }); + + it('a throwing pickComparable() is isolated — loop keeps ticking, no publish', () => { + const bus = new StateBus(); + const seen: string[] = []; + bus.subscribe((r) => seen.push(r)); + const handle = startConnectionDiffLoop({ + bus, + getSnapshot: () => [{ uin: '1' }], + pickComparable: () => { throw new Error('projector boom'); }, + intervalMs: 500, + }); + vi.advanceTimersByTime(2_000); + expect(seen).toEqual([]); + handle.dispose(); + }); + it('survives a throwing snapshot() — keeps trying on the next tick', () => { const bus = new StateBus(); const seen: string[] = []; diff --git a/packages/core/tests/webui-state-stream.test.ts b/packages/core/tests/webui-state-stream.test.ts index 6c37d9c1..bc8c4f4f 100644 --- a/packages/core/tests/webui-state-stream.test.ts +++ b/packages/core/tests/webui-state-stream.test.ts @@ -164,7 +164,7 @@ describe('bindStateStream', () => { // observed frame as the older snapshot. let call = 0; const versions = ['snap-T0', 'snap-T60']; - const snapshot = vi.fn(async (resource: StateResource): Promise => { + const snapshot = vi.fn(async (_resource: StateResource): Promise => { const i = call++; const sleep = i === 0 ? 200 : 10; await new Promise((r) => setTimeout(r, sleep)); @@ -196,7 +196,7 @@ describe('bindStateStream', () => { const bus = new StateBus(); const sent: CapturedFrame[] = []; let call = 0; - const snapshot = vi.fn(async (resource: StateResource): Promise => { + const snapshot = vi.fn(async (_resource: StateResource): Promise => { call++; await new Promise((r) => setTimeout(r, 100)); return `snap-#${call}`;