diff --git a/src/plugin/event.test.ts b/src/plugin/event.test.ts index 49ed2c031..a400118e6 100644 --- a/src/plugin/event.test.ts +++ b/src/plugin/event.test.ts @@ -335,7 +335,7 @@ describe("createEventHandler - idle deduplication", () => { expect(spawnTmuxPane).toHaveBeenCalledTimes(1) }) - it("dedups real-idle-after-synthetic-idle within 500ms", async () => { + it("does NOT dedup real-idle-after-synthetic-idle within 500ms", async () => { //#given const dispatchCalls: EventInput[] = [] const eventHandler = createIdleTrackingEventHandler(dispatchCalls) @@ -359,9 +359,77 @@ describe("createEventHandler - idle deduplication", () => { })) //#then - expect(dispatchCalls).toHaveLength(1) + expect(dispatchCalls).toHaveLength(2) expect(dispatchCalls[0]?.event.type).toBe("session.idle") expect((dispatchCalls[0]?.event.properties as { sessionID?: string } | undefined)?.sessionID).toBe(sessionId) + expect(dispatchCalls[1]?.event.type).toBe("session.idle") + expect((dispatchCalls[1]?.event.properties as { sessionID?: string } | undefined)?.sessionID).toBe(sessionId) + }) + + it("keeps other session dedup state untouched when bypassing synthetic-idle for current session", async () => { + //#given + const originalDateNow = Date.now + let currentNow = 30_000 + Date.now = () => currentNow + const dispatchedSessionIds: string[] = [] + const eventHandler = createIdleDedupSpyEventHandler({ + onEvent: () => {}, + sessionNotification: async (input: EventInput) => { + if (input.event.type !== "session.idle") { + return + } + const props = input.event.properties as { sessionID?: string } | undefined + if (props?.sessionID) { + dispatchedSessionIds.push(props.sessionID) + } + }, + }) + + try { + //#when + await eventHandler(asEventHandlerInput({ + event: { + type: "session.status", + properties: { + sessionID: "ses_a", + status: { type: "idle" }, + }, + }, + })) + await eventHandler(asEventHandlerInput({ + event: { + type: "session.idle", + properties: { + sessionID: "ses_b", + }, + }, + })) + + currentNow += 100 + await eventHandler(asEventHandlerInput({ + event: { + type: "session.idle", + properties: { + sessionID: "ses_a", + }, + }, + })) + + currentNow += 100 + await eventHandler(asEventHandlerInput({ + event: { + type: "session.idle", + properties: { + sessionID: "ses_b", + }, + }, + })) + + //#then + expect(dispatchedSessionIds).toEqual(["ses_a", "ses_b", "ses_a"]) + } finally { + Date.now = originalDateNow + } }) it("dedups back-to-back real session.idle events for the same sessionID within 500ms", async () => { diff --git a/src/plugin/event.ts b/src/plugin/event.ts index 265244f29..2b94fcadf 100644 --- a/src/plugin/event.ts +++ b/src/plugin/event.ts @@ -415,6 +415,12 @@ export function createEventHandler(args: { const emittedAt = recentSyntheticIdles.get(sessionID); if (emittedAt !== undefined && now - emittedAt < DEDUP_WINDOW_MS) { recentSyntheticIdles.delete(sessionID); + // Let real idle events through even when a synthetic idle fired moments earlier. + // OpenCode diagnostics expect a concrete session.idle event signal. + const lastAnyIdleAt = recentAnyIdles.get(sessionID); + if (lastAnyIdleAt === emittedAt) { + recentAnyIdles.delete(sessionID); + } } recentRealIdles.set(sessionID, now); if (!shouldDispatchIdleEvent(sessionID, now)) {