Simplify OpenCode upgrade to always restart and refresh version

This commit is contained in:
Chris Scott committed 2026-07-30 23:30:15 -04:00
1 parent adc1290836
commit ba8994eb61
6 files changed
+74 -58

No files matched your search

+4 -10
View File
@@ -906,7 +906,7 @@ export function createSettingsRoutes(db: Database, gitAuthService: GitAuthServic
throw new Error('Upgrade command timed out after 90 seconds')
}
const newVersion = opencodeServerManager.getVersion() || await opencodeServerManager.fetchVersion()
const newVersion = await opencodeServerManager.fetchVersion()
logger.info(`New OpenCode version: ${newVersion}`)
const upgraded = oldVersion && newVersion && compareVersions(newVersion, oldVersion) > 0
@@ -914,18 +914,12 @@ export function createSettingsRoutes(db: Database, gitAuthService: GitAuthServic
if (upgraded) {
logger.info(`OpenCode upgraded from v${oldVersion} to v${newVersion}`)
opencodeServerManager.clearStartupError()
try {
await reloadOpenCodeConfig(openCodeSupervisor)
logger.info('OpenCode server reloaded after upgrade')
} catch (reloadError) {
logger.warn('Config reload after upgrade failed, attempting full restart:', reloadError)
await restartOpenCode(openCodeSupervisor)
logger.info('OpenCode server restarted after upgrade')
}
await restartOpenCode(openCodeSupervisor)
logger.info('OpenCode server restarted after upgrade')
return c.json({
success: true,
message: `OpenCode upgraded from v${oldVersion} to v${newVersion} and configuration reloaded`,
message: `OpenCode upgraded from v${oldVersion} to v${newVersion} and restarted`,
oldVersion,
newVersion,
upgraded: true
+33 -28
View File
@@ -684,7 +684,7 @@ describe('Settings Routes - OpenCode Upgrade', () => {
describe('successful upgrade scenarios', () => {
it('should upgrade OpenCode successfully and respond with success', async () => {
mockGetVersion.mockReturnValueOnce('1.0.0')
.mockReturnValueOnce('1.0.1')
mockFetchVersion.mockResolvedValueOnce('1.0.1')
mockExecSync.mockReturnValueOnce('Upgrade successful\n')
const req = new Request('http://localhost/opencode-upgrade', {
@@ -698,11 +698,31 @@ describe('Settings Routes - OpenCode Upgrade', () => {
expect(json.upgraded).toBe(true)
expect(json.oldVersion).toBe('1.0.0')
expect(json.newVersion).toBe('1.0.1')
expect(json.message).toBe('OpenCode upgraded from v1.0.0 to v1.0.1 and restarted')
})
it('should use a freshly fetched version and restart when the cached version is stale after upgrade', async () => {
mockGetVersion.mockReturnValue('1.0.0')
mockFetchVersion.mockResolvedValueOnce('1.0.1')
mockExecSync.mockReturnValueOnce('Upgrade successful\n')
const req = new Request('http://localhost/opencode-upgrade', {
method: 'POST'
})
const res = await settingsApp.fetch(req)
const json = await res.json() as Record<string, unknown>
expect(res.status).toBe(200)
expect(json.upgraded).toBe(true)
expect(json.newVersion).toBe('1.0.1')
expect(mockFetchVersion).toHaveBeenCalledTimes(1)
expect(mockRestart).toHaveBeenCalledTimes(1)
expect(mockReloadConfig).not.toHaveBeenCalled()
})
it('should return already up to date when version unchanged', async () => {
mockGetVersion.mockReturnValueOnce('1.0.0')
.mockReturnValueOnce('1.0.0')
mockFetchVersion.mockResolvedValueOnce('1.0.0')
mockExecSync.mockReturnValueOnce('Already up to date\n')
const req = new Request('http://localhost/opencode-upgrade', {
@@ -715,36 +735,21 @@ describe('Settings Routes - OpenCode Upgrade', () => {
expect(json.success).toBe(true)
expect(json.upgraded).toBe(false)
expect(json.message).toContain('already up to date')
expect(mockFetchVersion).toHaveBeenCalledTimes(1)
})
it('should try reloadConfig first then restart on success', async () => {
it('should restart directly after a successful upgrade', async () => {
mockGetVersion.mockReturnValueOnce('1.0.0')
.mockReturnValueOnce('1.0.1')
mockFetchVersion.mockResolvedValueOnce('1.0.1')
mockExecSync.mockReturnValueOnce('Upgrade successful\n')
mockReloadConfig.mockResolvedValueOnce(undefined)
const req = new Request('http://localhost/opencode-upgrade', {
method: 'POST'
})
await settingsApp.fetch(req)
expect(mockReloadConfig).toHaveBeenCalled()
expect(mockRestart).not.toHaveBeenCalled()
})
it('should fall back to restart if reloadConfig fails', async () => {
mockGetVersion.mockReturnValueOnce('1.0.0')
.mockReturnValueOnce('1.0.1')
mockExecSync.mockReturnValueOnce('Upgrade successful\n')
mockReloadConfig.mockRejectedValueOnce(new Error('Reload failed'))
const req = new Request('http://localhost/opencode-upgrade', {
method: 'POST'
})
await settingsApp.fetch(req)
expect(mockReloadConfig).toHaveBeenCalled()
expect(mockRestart).toHaveBeenCalled()
expect(mockRestart).toHaveBeenCalledTimes(1)
expect(mockReloadConfig).not.toHaveBeenCalled()
})
})
@@ -825,7 +830,6 @@ describe('Settings Routes - OpenCode Upgrade', () => {
describe('version handling', () => {
it('should use fetched version when getVersion returns null', async () => {
mockGetVersion.mockReturnValueOnce(null)
.mockReturnValueOnce(null)
mockFetchVersion.mockResolvedValueOnce('1.0.1')
mockExecSync.mockReturnValueOnce('Upgrade successful\n')
@@ -842,7 +846,6 @@ describe('Settings Routes - OpenCode Upgrade', () => {
it('should handle both getVersion and fetchVersion returning null', async () => {
mockGetVersion.mockReturnValueOnce(null)
.mockReturnValueOnce(null)
mockFetchVersion.mockResolvedValueOnce(null)
mockExecSync.mockReturnValueOnce('Upgrade successful\n')
@@ -1030,17 +1033,19 @@ describe('Settings Routes - OpenCode Upgrade', () => {
it('should handle fetchVersion throwing error during normal upgrade', async () => {
mockGetVersion.mockReturnValueOnce('1.0.0')
.mockReturnValueOnce('1.0.1')
.mockReturnValueOnce('1.0.0')
mockFetchVersion.mockRejectedValueOnce(new Error('Fetch version failed'))
mockExecSync.mockReturnValueOnce('Upgrade successful\n')
mockReloadConfig.mockResolvedValue(undefined)
const req = new Request('http://localhost/opencode-upgrade', {
method: 'POST'
})
const res = await settingsApp.fetch(req)
const json = await res.json() as Record<string, unknown>
expect(res.status).toBe(200)
await expect(res.json()).resolves.toBeDefined()
expect(res.status).toBe(400)
expect(json.recovered).toBe(true)
expect(mockRestart).toHaveBeenCalledTimes(1)
})
it('should not leave server in broken state when upgrade times out', async () => {
@@ -5,7 +5,7 @@ import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogDescription } f
import { Button } from '@/components/ui/button'
import { settingsApi } from '@/api/settings'
import { showToast } from '@/lib/toast'
import { invalidateConfigCaches } from '@/lib/queryInvalidation'
import { invalidateConfigCaches, updateOpenCodeVersionCaches } from '@/lib/queryInvalidation'
interface VersionSelectDialogProps {
open: boolean
@@ -27,12 +27,8 @@ export function VersionSelectDialog({ open, onOpenChange }: VersionSelectDialogP
mutationFn: (version: string) => settingsApi.installOpenCodeVersion(version),
onSuccess: (result) => {
if (result.newVersion) {
queryClient.setQueryData(['health'], (old: Record<string, unknown> | undefined) => {
if (!old) return old
return { ...old, opencodeVersion: result.newVersion }
})
updateOpenCodeVersionCaches(queryClient, result.newVersion)
}
queryClient.invalidateQueries({ queryKey: ['opencode-versions'] })
invalidateConfigCaches(queryClient)
showToast.success(result.message)
onOpenChange(false)
@@ -45,11 +41,8 @@ export function VersionSelectDialog({ open, onOpenChange }: VersionSelectDialogP
const response = (error as { response?: { data?: { recovered?: boolean; recoveryMessage?: string; newVersion?: string } } }).response
const data = response?.data
if (data?.recovered) {
queryClient.setQueryData(['health'], (old: Record<string, unknown> | undefined) => {
if (!old) return old
return { ...old, opencodeVersion: data.newVersion }
})
if (data?.recovered && data.newVersion) {
updateOpenCodeVersionCaches(queryClient, data.newVersion)
showToast.success(`Install failed but server recovered at v${data.newVersion}`)
} else {
showToast.error(data?.recoveryMessage || 'Failed to install version')
@@ -2,7 +2,7 @@ import { useState } from 'react'
import { useMutation, useQueryClient } from '@tanstack/react-query'
import { settingsApi } from '@/api/settings'
import { showToast } from '@/lib/toast'
import { invalidateConfigCaches } from '@/lib/queryInvalidation'
import { invalidateConfigCaches, updateOpenCodeVersionCaches } from '@/lib/queryInvalidation'
import { getOpenCodeApiErrorMessage } from '@/lib/opencode-errors'
const RESTART_TOAST_ID = 'opencode-restart'
@@ -30,10 +30,7 @@ export function useOpenCodeServerActions() {
mutationFn: async () => settingsApi.upgradeOpenCode(),
onSuccess: (data) => {
if (data.upgraded && data.newVersion) {
queryClient.setQueryData(['health'], (old: Record<string, unknown> | undefined) => {
if (!old) return old
return { ...old, opencodeVersion: data.newVersion }
})
updateOpenCodeVersionCaches(queryClient, data.newVersion)
}
invalidateConfigCaches(queryClient)
if (data.upgraded) {
@@ -50,10 +47,7 @@ export function useOpenCodeServerActions() {
const data = response?.data
if (data?.recovered && data.newVersion) {
queryClient.setQueryData(['health'], (old: Record<string, unknown> | undefined) => {
if (!old) return old
return { ...old, opencodeVersion: data.newVersion }
})
updateOpenCodeVersionCaches(queryClient, data.newVersion)
showToast.success(`Upgrade failed but server recovered at v${data.newVersion}`, { id: UPGRADE_TOAST_ID })
} else {
showToast.error(data?.recoveryMessage || defaultMessage, { id: UPGRADE_TOAST_ID })
@@ -0,0 +1,19 @@
import { QueryClient } from '@tanstack/react-query'
import { describe, expect, it, vi } from 'vitest'
import { updateOpenCodeVersionCaches } from './queryInvalidation'
describe('updateOpenCodeVersionCaches', () => {
it('updates and invalidates both OpenCode version caches', () => {
const queryClient = new QueryClient()
queryClient.setQueryData(['health'], { opencodeVersion: '1.0.0', status: 'healthy' })
queryClient.setQueryData(['opencode-versions'], { currentVersion: '1.0.0', versions: [] })
const invalidateQueries = vi.spyOn(queryClient, 'invalidateQueries')
updateOpenCodeVersionCaches(queryClient, '1.0.1')
expect(queryClient.getQueryData(['health'])).toEqual({ opencodeVersion: '1.0.1', status: 'healthy' })
expect(queryClient.getQueryData(['opencode-versions'])).toEqual({ currentVersion: '1.0.1', versions: [] })
expect(invalidateQueries).toHaveBeenCalledWith({ queryKey: ['health'] })
expect(invalidateQueries).toHaveBeenCalledWith({ queryKey: ['opencode-versions'] })
})
})
+11
View File
@@ -31,6 +31,17 @@ export function invalidateConfigCaches(queryClient: QueryClient) {
invalidateProviderCaches(queryClient)
}
export function updateOpenCodeVersionCaches(queryClient: QueryClient, version: string) {
queryClient.setQueryData<Record<string, unknown>>(['health'], (oldData) => (
oldData ? { ...oldData, opencodeVersion: version } : oldData
))
queryClient.setQueryData<Record<string, unknown>>(['opencode-versions'], (oldData) => (
oldData ? { ...oldData, currentVersion: version } : oldData
))
queryClient.invalidateQueries({ queryKey: ['health'] })
queryClient.invalidateQueries({ queryKey: ['opencode-versions'] })
}
export function invalidateSkillCaches(queryClient: QueryClient) {
queryClient.invalidateQueries({ queryKey: ['settings', 'skills'] })
queryClient.invalidateQueries({ queryKey: ['managed-skills'] })