fix(event): prune synthetic idle dedup map
This commit is contained in:
parent
61531ca26c
commit
97b7215848
@ -13,6 +13,7 @@ import { lspManager } from "../tools"
|
||||
import type { CreatedHooks } from "../create-hooks"
|
||||
import type { Managers } from "../create-managers"
|
||||
import { normalizeSessionStatusToIdle } from "./session-status-normalizer"
|
||||
import { pruneRecentSyntheticIdles } from "./recent-synthetic-idles"
|
||||
|
||||
type FirstMessageVariantGate = {
|
||||
markSessionCreated: (sessionInfo: { id?: string; title?: string; parentID?: string } | undefined) => void
|
||||
@ -54,6 +55,12 @@ export function createEventHandler(args: {
|
||||
const DEDUP_WINDOW_MS = 500
|
||||
|
||||
return async (input): Promise<void> => {
|
||||
pruneRecentSyntheticIdles({
|
||||
recentSyntheticIdles,
|
||||
now: Date.now(),
|
||||
dedupWindowMs: DEDUP_WINDOW_MS,
|
||||
})
|
||||
|
||||
if (input.event.type === "session.idle") {
|
||||
const sessionID = (input.event.properties as Record<string, unknown> | undefined)?.sessionID as string | undefined
|
||||
if (sessionID) {
|
||||
|
||||
24
src/plugin/recent-synthetic-idles.test.ts
Normal file
24
src/plugin/recent-synthetic-idles.test.ts
Normal file
@ -0,0 +1,24 @@
|
||||
import { describe, it, expect } from "bun:test"
|
||||
|
||||
import { pruneRecentSyntheticIdles } from "./recent-synthetic-idles"
|
||||
|
||||
describe("pruneRecentSyntheticIdles", () => {
|
||||
it("removes entries older than dedup window", () => {
|
||||
// given
|
||||
const recentSyntheticIdles = new Map<string, number>([
|
||||
["ses_old", 1000],
|
||||
["ses_new", 1600],
|
||||
])
|
||||
|
||||
// when
|
||||
pruneRecentSyntheticIdles({
|
||||
recentSyntheticIdles,
|
||||
now: 2000,
|
||||
dedupWindowMs: 500,
|
||||
})
|
||||
|
||||
// then
|
||||
expect(recentSyntheticIdles.has("ses_old")).toBe(false)
|
||||
expect(recentSyntheticIdles.has("ses_new")).toBe(true)
|
||||
})
|
||||
})
|
||||
13
src/plugin/recent-synthetic-idles.ts
Normal file
13
src/plugin/recent-synthetic-idles.ts
Normal file
@ -0,0 +1,13 @@
|
||||
export function pruneRecentSyntheticIdles(args: {
|
||||
recentSyntheticIdles: Map<string, number>
|
||||
now: number
|
||||
dedupWindowMs: number
|
||||
}): void {
|
||||
const { recentSyntheticIdles, now, dedupWindowMs } = args
|
||||
|
||||
for (const [sessionID, emittedAt] of recentSyntheticIdles) {
|
||||
if (now - emittedAt >= dedupWindowMs) {
|
||||
recentSyntheticIdles.delete(sessionID)
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user