fix(sync): reconcile tools left running after settlement

This commit is contained in:
Bohdan Triapitsyn
2026-07-30 00:31:15 +03:00
parent d69ee1033e
commit 9977b25540
5 changed files with 138 additions and 2 deletions
+18 -1
View File
@@ -53,6 +53,7 @@ import type { QuestionRequest } from "@/types/question"
import {
getSessionMaterializationRequestKey,
getSessionMaterializationStatus,
getStaleRunningToolMessageID,
isSessionMaterializationStillNeeded,
type SessionMaterializationRequest,
} from "./materialization"
@@ -284,7 +285,11 @@ function enqueueSessionMaterialization(
const runtimeKey = getRuntimeKey()
const k = getSessionMaterializationRequestKey(runtimeKey, directory, sessionID)
const existing = pendingSessionMaterializations.get(k)
if (existing && Date.now() - existing.enqueuedAt < SESSION_MATERIALIZATION_COOLDOWN_MS) return
if (existing && Date.now() - existing.enqueuedAt < SESSION_MATERIALIZATION_COOLDOWN_MS) {
const settlementMustFollowEarlierRecovery = request.reason === "settled-running-tool"
&& existing.request.reason !== "settled-running-tool"
if (!settlementMustFollowEarlierRecovery) return
}
const pending = { runtimeKey, sessionID, directory, enqueuedAt: Date.now(), request }
pendingSessionMaterializations.set(k, pending)
@@ -1728,6 +1733,18 @@ function handleEvent(
}
}
if (payload.type === "session.idle" || payload.type === "session.error") {
const sessionID = getSessionIdFromPayload(payload) ?? undefined
const state = getDirectoryEventState(store, batch)
const messageID = sessionID ? getStaleRunningToolMessageID(state, sessionID) : undefined
if (sessionID && messageID) {
enqueueSessionMaterialization(resolvedDirectory, sessionID, childStores, {
reason: "settled-running-tool",
messageID,
})
}
}
updateRoutingIndexFromEvent(routingIndex, resolvedDirectory, payload)
}