Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions src/handlers/activity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down
2 changes: 1 addition & 1 deletion src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<K, V>(map: Map<K, V>, 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)
}
Expand Down
13 changes: 13 additions & 0 deletions tests/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, number>()
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", () => {
Expand Down
Loading