fix: stabilize scheduled runs and background task tracking (#365)

* fix: stabilize scheduled runs and background task tracking

* fix: complete scheduled runs and harden background task status

- Route directory-less OpenCode session events (session.execution.*,
  session.usage.updated) to backend listeners by remembering each
  session's directory in the SSE aggregator, so scheduled runs finish
  and failure push notifications fire; browser delivery is unchanged.
- Resolve background shell status from the transcript completion notice
  when OpenCode no longer lists the shell, keep the first terminal
  status so a kill is not reported as completed, and share one status
  icon and colour mapping between the transcript and background bar.
- Reconcile child sessions only while unknown or running, trigger it
  from the EventProvider alone, refresh children that drop to unknown
  after a poll, and forget deleted sessions.
- Fetch the model list and default concurrently while waiting for the
  schedule model, share configured-model selection, and consolidate
  schedule startup cancellation cleanup.
- Reduce background work re-renders, prune shell exit records, remove
  dead exports, and document background work in the chat guide.

* fix: hide finished background tasks and clip long dialog titles
This commit is contained in:
Chris Scott authored and GitHub committed 2026-09-28 23:55:01 -04:00
1 parent e80016cc22
commit 304f636516
35 files changed
+4272 -254

No files matched your search

+107 -1
View File
@@ -3,14 +3,16 @@ import { act, renderHook, waitFor } from '@testing-library/react'
import type { ReactNode } from 'react'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import type { SessionInfo } from '@opencode-manager/shared/opencode'
import { useCreateSession, useDeleteSession, useSession, useSessionsAcrossDirectories } from './useOpenCode'
import { useChildSessionReconciliation, useCreateSession, useDeleteSession, useSession, useSessionsAcrossDirectories } from './useOpenCode'
import { FetchError } from '../api/fetchWrapper'
import { showToast } from '../lib/toast'
import { useSessionStatus } from '../stores/sessionStatusStore'
const mocks = vi.hoisted(() => ({
listSessionPage: vi.fn(),
deleteSession: vi.fn(),
createSession: vi.fn(),
getSession: vi.fn(),
}))
vi.mock('../lib/toast', () => ({
@@ -22,11 +24,13 @@ vi.mock('../lib/toast', () => ({
vi.mock('@/api/opencode', async (importOriginal) => {
const actual = await importOriginal<typeof import('@/api/opencode')>()
mocks.getSession.mockImplementation(actual.getSession)
return {
...actual,
listSessionPage: mocks.listSessionPage,
deleteSession: mocks.deleteSession,
createSession: mocks.createSession,
getSession: mocks.getSession,
}
})
@@ -218,3 +222,105 @@ describe('useCreateSession', () => {
expect(mocks.createSession).toHaveBeenCalledWith({ directory: '/w/a', agent: undefined })
})
})
describe('useChildSessionReconciliation', () => {
beforeEach(() => {
mocks.getSession.mockReset()
useSessionStatus.setState({
statuses: new Map(),
statusCache: new Map(),
statusRevisions: new Map(),
knownSessions: new Set(),
outcomes: new Map(),
revision: 0,
})
})
const idleSession = (): SessionInfo => ({
...sessionInfo('child-1', '/repo'),
time: { created: 1000, updated: 2000, idle: 2000 },
})
it('applies one guarded snapshot for multiple observers sharing one fetch', async () => {
let resolveSession: ((value: SessionInfo) => void) | undefined
mocks.getSession.mockImplementation(
() => new Promise<SessionInfo>((resolve) => { resolveSession = resolve }),
)
const queryClient = createQueryClient()
const wrapper = createWrapper(queryClient)
const first = renderHook(() => useChildSessionReconciliation('child-1'), { wrapper })
const second = renderHook(() => useChildSessionReconciliation('child-1'), { wrapper })
await waitFor(() => expect(mocks.getSession).toHaveBeenCalledTimes(1))
await act(async () => {
resolveSession?.(idleSession())
})
expect(mocks.getSession).toHaveBeenCalledTimes(1)
expect(useSessionStatus.getState().getStatus('child-1')).toEqual({ type: 'idle' })
expect(useSessionStatus.getState().knownSessions.has('child-1')).toBe(true)
first.unmount()
second.unmount()
})
it('releases the snapshot when unmounted while the request is pending', async () => {
let resolveSession: ((value: SessionInfo) => void) | undefined
mocks.getSession.mockImplementation(
() => new Promise<SessionInfo>((resolve) => { resolveSession = resolve }),
)
const queryClient = createQueryClient()
const wrapper = createWrapper(queryClient)
const { unmount } = renderHook(() => useChildSessionReconciliation('child-1'), { wrapper })
await waitFor(() => expect(mocks.getSession).toHaveBeenCalledTimes(1))
act(() => {
useSessionStatus.getState().setStatus('child-1', { type: 'busy' })
})
expect(useSessionStatus.getState().statusRevisions.size).toBeGreaterThan(0)
unmount()
await act(async () => {
resolveSession?.(idleSession())
})
await waitFor(() => expect(useSessionStatus.getState().statusRevisions.size).toBe(0))
})
it('clears a stale child outcome when the fresh snapshot is busy', async () => {
mocks.getSession.mockResolvedValue(sessionInfo('child-1', '/repo'))
useSessionStatus.getState().setStatus('child-1', { type: 'busy' })
useSessionStatus.getState().setOutcome('child-1', 'failed')
const queryClient = createQueryClient()
renderHook(() => useChildSessionReconciliation('child-1'), {
wrapper: createWrapper(queryClient),
})
await waitFor(() => {
expect(useSessionStatus.getState().statuses.get('child-1')).toEqual({ type: 'busy' })
})
expect(useSessionStatus.getState().outcomes.get('child-1')).toBeUndefined()
})
it('does not reconcile a child whose lifecycle is already terminal', async () => {
useSessionStatus.getState().setStatus('child-1', { type: 'idle' })
const queryClient = createQueryClient()
renderHook(() => useChildSessionReconciliation('child-1'), {
wrapper: createWrapper(queryClient),
})
await act(async () => {
await Promise.resolve()
})
expect(mocks.getSession).not.toHaveBeenCalled()
expect(queryClient.getQueryState(['opencode', 'session-reconcile', 'child-1'])?.fetchStatus).not.toBe('fetching')
})
})