diff --git a/src/main/persistence/loading-store/primary-state-write-worker.ts b/src/main/persistence/loading-store/primary-state-write-worker.ts index 6a67aa73cb2..de3cef861c9 100644 --- a/src/main/persistence/loading-store/primary-state-write-worker.ts +++ b/src/main/persistence/loading-store/primary-state-write-worker.ts @@ -23,7 +23,15 @@ export async function writeProfileStateInWorker( runtime.dirtyProfileStateDomains.add(domain) } } - runtime.pendingAutomationRunsAfter ??= automationRuns + if (automationRuns === undefined || runtime.pendingAutomationRunsAfter !== undefined) { + return + } + // A run list replaced while this write was in flight is newer; save it as a whole row instead. + if (automationRuns === runtime.state.automationRuns) { + runtime.pendingAutomationRunsAfter = automationRuns + } else { + runtime.dirtyProfileStateDomains?.add('automationRuns') + } } try { diff --git a/src/main/persistence/loading-store/write-scheduling.ts b/src/main/persistence/loading-store/write-scheduling.ts index ea9aa4c32a7..8fe46224898 100644 --- a/src/main/persistence/loading-store/write-scheduling.ts +++ b/src/main/persistence/loading-store/write-scheduling.ts @@ -117,9 +117,12 @@ function scheduleDomainSave( listener(dirtyDomains) } runtime.automationListProjectionCache = null - // A named automationRuns save supersedes older run lists, including one a failed in-flight write restores. - if (dirtyDomains?.includes('automationRuns')) { - runtime.pendingAutomationRunsAfter = runtime.state.automationRuns + // A run list replaced outside the run-delta path saves as a whole row; an older pending delta is stale. + if ( + dirtyDomains?.includes('automationRuns') && + runtime.pendingAutomationRunsAfter !== runtime.state.automationRuns + ) { + runtime.pendingAutomationRunsAfter = undefined } const trackedDomains = runtime.dirtyProfileStateDomains if (dirtyDomains === undefined) {