From 9fff36f90b61a04ee0138a6e8cfca8e9bc66858e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=86=AF=E5=9F=BA=E9=AD=81?= <1412414664@qq.com> Date: Mon, 6 Jul 2026 22:54:30 +0800 Subject: [PATCH 1/2] fix: preserve bounded map updates --- src/util.ts | 2 +- tests/util.test.ts | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/util.ts b/src/util.ts index 3dc1ce5..91c17b3 100644 --- a/src/util.ts +++ b/src/util.ts @@ -16,7 +16,7 @@ export function errorSummary(err: { name: string; data?: unknown } | undefined): * has reached `MAX_PENDING` capacity to prevent unbounded memory growth. */ export function setBoundedMap(map: Map, key: K, value: V) { - if (map.size >= MAX_PENDING) { + if (!map.has(key) && map.size >= MAX_PENDING) { const [firstKey] = map.keys() if (firstKey !== undefined) map.delete(firstKey) } diff --git a/tests/util.test.ts b/tests/util.test.ts index 72c0103..a0315f7 100644 --- a/tests/util.test.ts +++ b/tests/util.test.ts @@ -62,6 +62,19 @@ describe("setBoundedMap", () => { expect(map.get("a")).toBe(2) expect(map.size).toBe(1) }) + + test("updates an existing key at capacity without evicting another entry", () => { + const map = new Map() + for (let i = 0; i < MAX_PENDING; i++) { + setBoundedMap(map, `key-${i}`, i) + } + + setBoundedMap(map, "key-10", 1000) + + expect(map.size).toBe(MAX_PENDING) + expect(map.get("key-10")).toBe(1000) + expect(map.has("key-0")).toBe(true) + }) }) describe("isMetricEnabled", () => { From f6de98e60968593c4a0e552b5f72006ad32a0e3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=86=AF=E5=9F=BA=E9=AD=81?= <1412414664@qq.com> Date: Thu, 16 Jul 2026 11:23:22 +0800 Subject: [PATCH 2/2] refactor: use bounded map for session diffs --- src/handlers/activity.ts | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/src/handlers/activity.ts b/src/handlers/activity.ts index 5ad9498..7ef47df 100644 --- a/src/handlers/activity.ts +++ b/src/handlers/activity.ts @@ -28,14 +28,7 @@ export function handleSessionDiff(e: EventSessionDiff, ctx: HandlerContext) { const deltaAdded = totalAdded - prev.additions const deltaRemoved = totalRemoved - prev.deletions const nextTotals = { additions: totalAdded, deletions: totalRemoved } - if (ctx.sessionDiffTotals.has(sessionID)) { - // Existing session: update in place. Calling setBoundedMap on a full map would - // evict an unrelated session here, and that session's next session.diff would - // be treated as first-seen — reintroducing the cumulative double-count bug. - ctx.sessionDiffTotals.set(sessionID, nextTotals) - } else { - setBoundedMap(ctx.sessionDiffTotals, sessionID, nextTotals) - } + setBoundedMap(ctx.sessionDiffTotals, sessionID, nextTotals) const baseAttrs = { ...ctx.commonAttrs, "session.id": sessionID }