fix: protect live session sync caches

This commit is contained in:
Bohdan Triapitsyn
2026-05-07 12:30:30 +03:00
parent 07edd6e9b9
commit ff830d3812
5 changed files with 89 additions and 5 deletions
+34
View File
@@ -18,6 +18,40 @@ type SessionCache = {
question: Record<string, QuestionRequest[] | undefined>
}
export function getProtectedSessionCacheIds(store: SessionCache): Set<string> {
const protectedIds = new Set<string>()
for (const [sessionID, status] of Object.entries(store.session_status ?? {})) {
if (status && status.type !== "idle") {
protectedIds.add(sessionID)
}
}
for (const [sessionID, permissions] of Object.entries(store.permission ?? {})) {
if ((permissions?.length ?? 0) > 0) {
protectedIds.add(sessionID)
}
}
for (const [sessionID, questions] of Object.entries(store.question ?? {})) {
if ((questions?.length ?? 0) > 0) {
protectedIds.add(sessionID)
}
}
for (const [sessionID, messages] of Object.entries(store.message ?? {})) {
const lastMessage = messages?.[messages.length - 1]
if (
lastMessage?.role === "assistant"
&& typeof (lastMessage as { time?: { completed?: number } }).time?.completed !== "number"
) {
protectedIds.add(sessionID)
}
}
return protectedIds
}
export function dropSessionCaches(store: SessionCache, sessionIDs: Iterable<string>) {
const stale = new Set(Array.from(sessionIDs).filter(Boolean))
if (stale.size === 0) return