mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 21:04:23 +01:00
fix: stabilize scheduled runs and background task tracking
This commit is contained in:
1 parent
5bd688de4b
commit
a75d86c339
25 files changed
+3330
-221
No files matched your search
@@ -6,6 +6,8 @@ const resetStore = () => {
|
||||
statuses: new Map(),
|
||||
statusCache: new Map(),
|
||||
statusRevisions: new Map(),
|
||||
knownSessions: new Set(),
|
||||
outcomes: new Map(),
|
||||
revision: 0,
|
||||
})
|
||||
}
|
||||
@@ -60,12 +62,172 @@ describe('sessionStatusStore', () => {
|
||||
})
|
||||
})
|
||||
|
||||
it('leaves the store state untouched for an idle event on an untracked session with no snapshot in flight', () => {
|
||||
it('records knowledge for an idle event on an untracked session without adding a status', () => {
|
||||
const before = useSessionStatus.getState()
|
||||
|
||||
before.setStatus('untracked', { type: 'idle' })
|
||||
|
||||
expect(useSessionStatus.getState()).toBe(before)
|
||||
const after = useSessionStatus.getState()
|
||||
expect(after.statuses).toBe(before.statuses)
|
||||
expect(after.getStatus('untracked')).toEqual({ type: 'idle' })
|
||||
expect(after.isSessionKnown('untracked')).toBe(true)
|
||||
})
|
||||
|
||||
it('keeps knowledge of a session after it goes idle', () => {
|
||||
const store = useSessionStatus.getState()
|
||||
store.setStatus('session-a', { type: 'busy' })
|
||||
expect(store.isSessionKnown('session-a')).toBe(true)
|
||||
|
||||
store.setStatus('session-a', { type: 'idle' })
|
||||
|
||||
expect(useSessionStatus.getState().isSessionKnown('session-a')).toBe(true)
|
||||
expect(useSessionStatus.getState().getStatus('session-a')).toEqual({ type: 'idle' })
|
||||
})
|
||||
|
||||
it('marks sessions from a global snapshot as known', () => {
|
||||
const store = useSessionStatus.getState()
|
||||
const token = store.beginStatusSnapshot()
|
||||
|
||||
useSessionStatus.getState().replaceStatuses({ 'session-a': { type: 'busy' } }, token)
|
||||
|
||||
expect(useSessionStatus.getState().isSessionKnown('session-a')).toBe(true)
|
||||
expect(useSessionStatus.getState().isSessionKnown('session-unknown')).toBe(false)
|
||||
})
|
||||
|
||||
it('does not treat a session omitted from a later snapshot as finished', () => {
|
||||
const store = useSessionStatus.getState()
|
||||
store.setStatus('session-a', { type: 'busy' })
|
||||
|
||||
const token = store.beginStatusSnapshot()
|
||||
useSessionStatus.getState().replaceStatuses({}, token)
|
||||
|
||||
expect(useSessionStatus.getState().isSessionKnown('session-a')).toBe(false)
|
||||
expect(useSessionStatus.getState().getStatus('session-a')).toEqual({ type: 'idle' })
|
||||
})
|
||||
|
||||
it('records a child outcome and preserves it when the status goes idle', () => {
|
||||
const store = useSessionStatus.getState()
|
||||
store.setStatus('child-a', { type: 'busy' })
|
||||
store.setOutcome('child-a', 'failed')
|
||||
|
||||
store.setStatus('child-a', { type: 'idle' })
|
||||
|
||||
expect(useSessionStatus.getState().getOutcome('child-a')).toBe('failed')
|
||||
expect(useSessionStatus.getState().isSessionKnown('child-a')).toBe(true)
|
||||
})
|
||||
|
||||
it('clears a child outcome when a new execution starts', () => {
|
||||
const store = useSessionStatus.getState()
|
||||
store.setOutcome('child-a', 'interrupted')
|
||||
|
||||
store.clearOutcome('child-a')
|
||||
|
||||
expect(useSessionStatus.getState().getOutcome('child-a')).toBeUndefined()
|
||||
})
|
||||
|
||||
it('applies an authoritative idle session snapshot from the child session query', () => {
|
||||
const store = useSessionStatus.getState()
|
||||
const token = store.beginStatusSnapshot()
|
||||
|
||||
store.applySessionSnapshot('child-a', {
|
||||
id: 'child-a',
|
||||
projectID: 'project-1',
|
||||
cost: 0,
|
||||
tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } },
|
||||
outcome: 'failed',
|
||||
time: { created: 1, updated: 2, idle: 2 },
|
||||
location: { directory: '/repo' },
|
||||
}, token)
|
||||
|
||||
expect(useSessionStatus.getState().isSessionKnown('child-a')).toBe(true)
|
||||
expect(useSessionStatus.getState().getStatus('child-a')).toEqual({ type: 'idle' })
|
||||
expect(useSessionStatus.getState().getOutcome('child-a')).toBe('failed')
|
||||
})
|
||||
|
||||
it('applies a running child session snapshot as busy', () => {
|
||||
const store = useSessionStatus.getState()
|
||||
const token = store.beginStatusSnapshot()
|
||||
|
||||
store.applySessionSnapshot('child-a', {
|
||||
id: 'child-a',
|
||||
projectID: 'project-1',
|
||||
cost: 0,
|
||||
tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } },
|
||||
time: { created: 1, updated: 2 },
|
||||
location: { directory: '/repo' },
|
||||
}, token)
|
||||
|
||||
expect(useSessionStatus.getState().isSessionKnown('child-a')).toBe(true)
|
||||
expect(useSessionStatus.getState().getStatus('child-a')).toEqual({ type: 'busy' })
|
||||
})
|
||||
|
||||
it('does not overwrite a newer live terminal event with a stale session snapshot', () => {
|
||||
const store = useSessionStatus.getState()
|
||||
const token = store.beginStatusSnapshot()
|
||||
store.setStatus('child-a', { type: 'idle' })
|
||||
|
||||
store.applySessionSnapshot('child-a', {
|
||||
id: 'child-a',
|
||||
projectID: 'project-1',
|
||||
cost: 0,
|
||||
tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } },
|
||||
time: { created: 1, updated: 2 },
|
||||
location: { directory: '/repo' },
|
||||
}, token)
|
||||
|
||||
expect(useSessionStatus.getState().getStatus('child-a')).toEqual({ type: 'idle' })
|
||||
})
|
||||
|
||||
it('clears a stale child outcome when a fresh authoritative busy snapshot arrives', () => {
|
||||
const store = useSessionStatus.getState()
|
||||
store.setOutcome('child-a', 'failed')
|
||||
const token = store.beginStatusSnapshot()
|
||||
|
||||
store.applySessionSnapshot('child-a', {
|
||||
id: 'child-a',
|
||||
projectID: 'project-1',
|
||||
cost: 0,
|
||||
tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } },
|
||||
time: { created: 1, updated: 2 },
|
||||
location: { directory: '/repo' },
|
||||
}, token)
|
||||
|
||||
expect(useSessionStatus.getState().getOutcome('child-a')).toBeUndefined()
|
||||
expect(useSessionStatus.getState().getStatus('child-a')).toEqual({ type: 'busy' })
|
||||
})
|
||||
|
||||
it('preserves confirmed idle knowledge across a global snapshot', () => {
|
||||
const store = useSessionStatus.getState()
|
||||
store.setStatus('child-idle', { type: 'idle' })
|
||||
|
||||
const token = store.beginStatusSnapshot()
|
||||
useSessionStatus.getState().replaceStatuses({}, token)
|
||||
|
||||
expect(useSessionStatus.getState().isSessionKnown('child-idle')).toBe(true)
|
||||
expect(useSessionStatus.getState().getStatus('child-idle')).toEqual({ type: 'idle' })
|
||||
})
|
||||
|
||||
it('turns a formerly active session omitted from a global snapshot unknown', () => {
|
||||
const store = useSessionStatus.getState()
|
||||
store.setStatus('child-active', { type: 'busy' })
|
||||
|
||||
const token = store.beginStatusSnapshot()
|
||||
useSessionStatus.getState().replaceStatuses({}, token)
|
||||
|
||||
expect(useSessionStatus.getState().isSessionKnown('child-active')).toBe(false)
|
||||
})
|
||||
|
||||
it('does not let a snapshot erase an idle event received in flight', () => {
|
||||
const store = useSessionStatus.getState()
|
||||
store.setStatus('child-active', { type: 'busy' })
|
||||
|
||||
const token = store.beginStatusSnapshot()
|
||||
store.setStatus('child-active', { type: 'idle' })
|
||||
|
||||
useSessionStatus.getState().replaceStatuses({}, token)
|
||||
|
||||
expect(useSessionStatus.getState().isSessionKnown('child-active')).toBe(true)
|
||||
expect(useSessionStatus.getState().getStatus('child-active')).toEqual({ type: 'idle' })
|
||||
})
|
||||
|
||||
it('leaves the statuses map identity unchanged for a repeated identical busy status outside a snapshot', () => {
|
||||
|
||||
@@ -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))
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user