From 0d66174c9335b8222c0d9947c2f0e866c5bc4366 Mon Sep 17 00:00:00 2001 From: haotianyu Date: Mon, 1 Jun 2026 07:37:11 +0800 Subject: [PATCH] feat: delete session state file when session is deleted --- lib/hooks.ts | 16 +++++++++++++++- lib/state/persistence.ts | 5 +++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/hooks.ts b/lib/hooks.ts index e31cf110..93d84029 100644 --- a/lib/hooks.ts +++ b/lib/hooks.ts @@ -36,7 +36,13 @@ import { } from "./commands" import { type HostPermissionSnapshot } from "./host-permissions" import { compressPermission, syncCompressPermissionState } from "./compress-permission" -import { checkSession, ensureSessionInitialized, saveSessionState, syncToolCache } from "./state" +import { + checkSession, + deleteSessionState, + ensureSessionInitialized, + saveSessionState, + syncToolCache, +} from "./state" import { cacheSystemPromptTokens } from "./ui/utils" const INTERNAL_AGENT_SIGNATURES = [ @@ -293,6 +299,14 @@ export function createEventHandler(state: SessionState, logger: Logger) { ? input.event.properties.time : undefined + if (input.event.type === "session.deleted") { + const sessionId = input.event.properties.info?.id + if (sessionId) { + await deleteSessionState(sessionId).catch(() => {}) + } + return + } + if (input.event.type !== "message.part.updated") { return } diff --git a/lib/state/persistence.ts b/lib/state/persistence.ts index 87b774f9..603e5580 100644 --- a/lib/state/persistence.ts +++ b/lib/state/persistence.ts @@ -203,6 +203,11 @@ export async function loadSessionState( } } +export async function deleteSessionState(sessionId: string): Promise { + const filePath = getSessionFilePath(sessionId) + await fs.unlink(filePath) +} + export interface AggregatedStats { totalTokens: number totalTools: number