fix: stabilize scheduled runs and background task tracking

This commit is contained in:
Chris Scott committed 2026-09-28 20:39:51 +00:00
1 parent 5bd688de4b
commit a75d86c339
25 files changed
+3330 -221

No files matched your search

+133 -16
View File
@@ -1,4 +1,6 @@
import { create } from 'zustand'
import type { SessionInfo } from '@opencode-manager/shared/opencode'
import { childLifecycle, type BackgroundTaskLifecycle, type ChildOutcome } from '@/lib/backgroundWork'
export type SessionStatusType =
| { type: 'idle' }
@@ -6,7 +8,7 @@ export type SessionStatusType =
| { type: 'compact' }
| { type: 'retry'; attempt: number; message: string; next: number }
interface StatusSnapshotToken {
export interface StatusSnapshotToken {
revision: number
order: number
}
@@ -15,13 +17,20 @@ interface SessionStatusStore {
statuses: Map<string, SessionStatusType>
statusCache: Map<string, string>
statusRevisions: Map<string, number>
knownSessions: Set<string>
outcomes: Map<string, ChildOutcome>
revision: number
setStatus: (sessionID: string, status: SessionStatusType) => void
setOutcome: (sessionID: string, outcome: ChildOutcome) => void
clearOutcome: (sessionID: string) => void
setOptimisticActive: (sessionID: string, timeoutMs?: number) => void
replaceStatuses: (statuses: Record<string, SessionStatusType>, token: StatusSnapshotToken) => void
applySessionSnapshot: (sessionID: string, session: SessionInfo, token: StatusSnapshotToken) => void
beginStatusSnapshot: () => StatusSnapshotToken
endStatusSnapshot: (token: StatusSnapshotToken) => void
getStatus: (sessionID: string) => SessionStatusType
getOutcome: (sessionID: string) => ChildOutcome | undefined
isSessionKnown: (sessionID: string) => boolean
clearStatus: (sessionID: string) => void
}
@@ -54,6 +63,21 @@ const sameEntries = <V>(left: Map<string, V>, right: Map<string, V>): boolean =>
return true
}
const sameSets = (left: Set<string>, right: Set<string>): boolean => {
if (left.size !== right.size) return false
for (const value of right) {
if (!left.has(value)) return false
}
return true
}
const markSessionKnown = (knownSessions: Set<string>, sessionID: string): Set<string> | null => {
if (knownSessions.has(sessionID)) return null
const next = new Set(knownSessions)
next.add(sessionID)
return next
}
const registerSnapshot = (token: StatusSnapshotToken): void => {
inFlightSnapshots.set(token.order, token.revision)
}
@@ -107,13 +131,27 @@ export const useSessionStatus = create<SessionStatusStore>((set, get) => {
const statusCache = new Map(state.statusCache)
statuses.set(sessionID, status)
statusCache.set(sessionID, getStatusHash(status))
return { statuses, statusCache, ...(recordRevisionPatch(state, sessionID) ?? {}) }
const knownSessions = markSessionKnown(state.knownSessions, sessionID)
return {
statuses,
statusCache,
...(knownSessions ? { knownSessions } : {}),
...(recordRevisionPatch(state, sessionID) ?? {}),
}
}
const isLiveAfterCapture = (sessionID: string, captureRevision: number): boolean => {
if (optimisticActiveTimers.has(sessionID)) return true
const touched = get().statusRevisions.get(sessionID)
return touched !== undefined && touched > captureRevision
}
return {
statuses: new Map(),
statusCache: new Map(),
statusRevisions: new Map(),
knownSessions: new Set(),
outcomes: new Map(),
revision: 0,
setStatus: (sessionID: string, status: SessionStatusType) => {
@@ -124,14 +162,55 @@ export const useSessionStatus = create<SessionStatusStore>((set, get) => {
clearOptimisticActiveTimer(sessionID)
if (get().statusCache.get(sessionID) === getStatusHash(status)) {
const patch = recordRevisionPatch(get(), sessionID)
if (patch) set(patch)
const revisionPatch = recordRevisionPatch(get(), sessionID)
const knownSessions = markSessionKnown(get().knownSessions, sessionID)
if (revisionPatch || knownSessions) {
set({
...(revisionPatch ?? {}),
...(knownSessions ? { knownSessions } : {}),
})
}
return
}
set((state) => applyStatusPatch(state, sessionID, status))
},
setOutcome: (sessionID: string, outcome: ChildOutcome) => {
if (get().outcomes.get(sessionID) === outcome) {
const knownSessions = markSessionKnown(get().knownSessions, sessionID)
if (knownSessions) set({ knownSessions })
return
}
set((state) => {
const outcomes = new Map(state.outcomes)
outcomes.set(sessionID, outcome)
const knownSessions = markSessionKnown(state.knownSessions, sessionID)
return { outcomes, ...(knownSessions ? { knownSessions } : {}) }
})
},
clearOutcome: (sessionID: string) => {
if (!get().outcomes.has(sessionID)) return
set((state) => {
const outcomes = new Map(state.outcomes)
outcomes.delete(sessionID)
return { outcomes }
})
},
applySessionSnapshot: (sessionID: string, session: SessionInfo, token: StatusSnapshotToken) => {
releaseSnapshot(token)
if (isLiveAfterCapture(sessionID, token.revision)) return
if (session.time.idle !== undefined) {
get().setStatus(sessionID, { type: 'idle' })
if (session.outcome) get().setOutcome(sessionID, session.outcome)
return
}
get().clearOutcome(sessionID)
get().setStatus(sessionID, { type: 'busy' })
},
setOptimisticActive: (sessionID: string, timeoutMs = OPTIMISTIC_ACTIVE_TIMEOUT_MS) => {
clearOptimisticActiveTimer(sessionID)
@@ -173,25 +252,20 @@ export const useSessionStatus = create<SessionStatusStore>((set, get) => {
const currentStatuses = get().statuses
const currentRevisions = get().statusRevisions
const currentKnownSessions = get().knownSessions
const captureRevision = token.revision
const isLiveAfterCapture = (sessionID: string): boolean => {
if (optimisticActiveTimers.has(sessionID)) return true
const touched = currentRevisions.get(sessionID)
return touched !== undefined && touched > captureRevision
}
const newMap = new Map<string, SessionStatusType>()
const newCache = new Map<string, string>()
for (const [sessionID, status] of currentStatuses.entries()) {
if (!isLiveAfterCapture(sessionID)) continue
if (!isLiveAfterCapture(sessionID, captureRevision)) continue
newMap.set(sessionID, status)
newCache.set(sessionID, getStatusHash(status))
}
for (const [sessionID, status] of Object.entries(statuses)) {
if (isLiveAfterCapture(sessionID)) continue
if (isLiveAfterCapture(sessionID, captureRevision)) continue
if (status.type === 'idle') continue
const current = currentStatuses.get(sessionID)
const effective = status.type === 'busy' && current !== undefined && current.type !== 'idle' ? current : status
@@ -200,13 +274,28 @@ export const useSessionStatus = create<SessionStatusStore>((set, get) => {
}
const nextRevisions = prunedRevisions(currentRevisions)
const nextKnownSessions = new Set<string>(Object.keys(statuses))
for (const [sessionID, revision] of currentRevisions.entries()) {
if (revision > captureRevision) nextKnownSessions.add(sessionID)
}
for (const sessionID of optimisticActiveTimers.keys()) {
nextKnownSessions.add(sessionID)
}
for (const sessionID of currentKnownSessions) {
if (!currentStatuses.has(sessionID)) nextKnownSessions.add(sessionID)
}
if (sameEntries(get().statusCache, newCache) && sameEntries(currentRevisions, nextRevisions)) return
if (
sameEntries(get().statusCache, newCache) &&
sameEntries(currentRevisions, nextRevisions) &&
sameSets(currentKnownSessions, nextKnownSessions)
) return
set({
statuses: newMap,
statusCache: newCache,
statusRevisions: nextRevisions,
knownSessions: nextKnownSessions,
})
},
@@ -230,18 +319,34 @@ export const useSessionStatus = create<SessionStatusStore>((set, get) => {
return get().statuses.get(sessionID) || DEFAULT_STATUS
},
getOutcome: (sessionID: string) => {
return get().outcomes.get(sessionID)
},
isSessionKnown: (sessionID: string) => {
return get().knownSessions.has(sessionID)
},
clearStatus: (sessionID: string) => {
clearOptimisticActiveTimer(sessionID)
if (!get().statuses.has(sessionID) && inFlightSnapshots.size === 0) return
const knownPatch = markSessionKnown(get().knownSessions, sessionID)
if (!get().statuses.has(sessionID) && inFlightSnapshots.size === 0) {
if (knownPatch) set({ knownSessions: knownPatch })
return
}
set((state) => {
const revisionPatch = recordRevisionPatch(state, sessionID)
if (!state.statuses.has(sessionID)) return revisionPatch ?? state
const knownSessions = markSessionKnown(state.knownSessions, sessionID)
const knownFields = knownSessions ? { knownSessions } : {}
if (!state.statuses.has(sessionID)) {
return { ...(revisionPatch ?? {}), ...knownFields }
}
const statuses = new Map(state.statuses)
const statusCache = new Map(state.statusCache)
statuses.delete(sessionID)
statusCache.delete(sessionID)
return { statuses, statusCache, ...(revisionPatch ?? {}) }
return { statuses, statusCache, ...(revisionPatch ?? {}), ...knownFields }
})
},
}
@@ -252,3 +357,15 @@ export const useSessionStatusForSession = (sessionID: string | undefined): Sessi
sessionID ? (state.statuses.get(sessionID) ?? DEFAULT_STATUS) : DEFAULT_STATUS
)
}
export const useIsSessionKnown = (sessionID: string | undefined): boolean => {
return useSessionStatus((state) => (sessionID ? state.knownSessions.has(sessionID) : false))
}
export const useChildLifecycleForSession = (sessionID: string | undefined): BackgroundTaskLifecycle => {
return useSessionStatus((state) => {
if (!sessionID) return 'unknown'
const status = state.statuses.get(sessionID) ?? DEFAULT_STATUS
return childLifecycle(status, state.knownSessions.has(sessionID), state.outcomes.get(sessionID))
})
}