mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 20:34:18 +01:00
* refactor(config): make on-disk opencode.json the single source of truth Make <workspace>/.config/opencode/opencode.json the only store for the active OpenCode configuration. One owner (opencode-config-file.ts) reads, validates, writes, archives, and seeds the file; one owner (opencode-config-apply.ts) decides how an edit is applied: restart pending, or a live patch with recovery. Settings routes, the internal assistant API, the health-watch supervisor, boot, and host import all go through them, so an edit that lands on the file without going through the database is visible immediately instead of at the next Manager boot. Remove the named-config profiles. Migration 019 archives every opencode_configs row to .config/opencode-configs-archive/<name>.json, restores the default row to opencode.json when no file exists, then drops the table, its indexes, and repos.opencode_config_name. The /opencode-configs* and config/switch routes and the CreateConfigDialog/SwitchConfigDialog dialogs are gone, and the ocm tool gains GET/PUT /opencode-config backed by the same apply owner. Harden the write path: serialize and atomically write config updates while preserving the file mode, keep withFileLock usable after a rejection, route reloadConfig's cleaned-config write through the file owner, stop boot from rewriting an existing valid config when importing state, prune the health-watch directory to its newest 20 entries, resolve archive paths with realpath, and share one frontend config query across dialogs. * fix(mirror): canonicalize worktree paths before branch ownership check * refactor(config): unify opencode config apply and rollback paths - share one config route factory between public and internal mounts - capture last-known-good before every config write and restore via one helper - stop auto-restarting on save; flag restart-required fields and live-patch the rest - add CONFIG_PATCH_TIMEOUT_MS for config patch requests - extract canonicalPath helpers, health-watch artifact writer, and useProvidersWithModels hook * refactor(model-state): use OpenCode state file as model state source of truth
93 lines
3.4 KiB
TypeScript
93 lines
3.4 KiB
TypeScript
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
|
|
|
|
const managerMock = vi.hoisted(() => ({
|
|
getLastStartupError: vi.fn<() => string | null>(() => null),
|
|
clearStartupError: vi.fn<() => void>(),
|
|
restart: vi.fn<() => Promise<void>>(),
|
|
}))
|
|
|
|
vi.mock('../../src/services/opencode-single-server', () => ({
|
|
opencodeServerManager: managerMock,
|
|
}))
|
|
|
|
import {
|
|
restartOpenCode,
|
|
setOpenCodeRestartCoordinator,
|
|
} from '../../src/services/opencode-restart'
|
|
import type { OpenCodeRestartCoordinator } from '../../src/services/opencode-restart-coordinator'
|
|
import type { OpenCodeSupervisor } from '../../src/services/opencode-supervisor'
|
|
|
|
function createSupervisor(healthy: boolean): OpenCodeSupervisor {
|
|
return {
|
|
restart: vi.fn().mockResolvedValue({ healthy }),
|
|
reloadConfig: vi.fn(),
|
|
} as unknown as OpenCodeSupervisor
|
|
}
|
|
|
|
function createCoordinator(healthy: boolean, resumedSessionIDs: string[] = []): OpenCodeRestartCoordinator {
|
|
return {
|
|
runWithResume: vi.fn(async (restart: () => Promise<boolean>) => ({
|
|
healthy: healthy ?? (await restart()),
|
|
resumedSessionIDs,
|
|
})),
|
|
} as unknown as OpenCodeRestartCoordinator
|
|
}
|
|
|
|
describe('restartOpenCode', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
setOpenCodeRestartCoordinator(null)
|
|
})
|
|
|
|
afterEach(() => {
|
|
setOpenCodeRestartCoordinator(null)
|
|
})
|
|
|
|
it('throws with the startup failure reason when the coordinator reports an unhealthy restart', async () => {
|
|
managerMock.getLastStartupError.mockReturnValue('OpenCode version 1.18.15 does not support sandboxed bash tool rewriting')
|
|
setOpenCodeRestartCoordinator(createCoordinator(false))
|
|
|
|
await expect(restartOpenCode(createSupervisor(true))).rejects.toThrow(
|
|
'OpenCode version 1.18.15 does not support sandboxed bash tool rewriting',
|
|
)
|
|
})
|
|
|
|
it('preserves resumed session IDs only when the coordinator reports a healthy restart', async () => {
|
|
setOpenCodeRestartCoordinator(createCoordinator(true, ['session-1', 'session-2']))
|
|
|
|
const result = await restartOpenCode(createSupervisor(true))
|
|
|
|
expect(result).toEqual({ resumedSessionIDs: ['session-1', 'session-2'] })
|
|
})
|
|
|
|
it('throws with the startup failure reason when the supervisor restart is unhealthy without a coordinator', async () => {
|
|
managerMock.getLastStartupError.mockReturnValue('OpenCode server failed to become healthy')
|
|
|
|
await expect(restartOpenCode(createSupervisor(false))).rejects.toThrow('OpenCode server failed to become healthy')
|
|
})
|
|
|
|
it('returns without resumed sessions when the supervisor restart is healthy without a coordinator', async () => {
|
|
const supervisor = createSupervisor(true)
|
|
|
|
const result = await restartOpenCode(supervisor)
|
|
|
|
expect(result).toEqual({ resumedSessionIDs: [] })
|
|
expect(supervisor.restart).toHaveBeenCalledWith('settings_restart')
|
|
})
|
|
|
|
it('uses a generic failure message when no startup error is recorded', async () => {
|
|
managerMock.getLastStartupError.mockReturnValue(null)
|
|
|
|
await expect(restartOpenCode(createSupervisor(false))).rejects.toThrow(
|
|
'OpenCode server restart did not complete successfully',
|
|
)
|
|
})
|
|
|
|
it('propagates a manager restart failure when no supervisor is provided', async () => {
|
|
managerMock.restart.mockRejectedValue(new Error('server failed to become healthy'))
|
|
|
|
await expect(restartOpenCode()).rejects.toThrow('server failed to become healthy')
|
|
expect(managerMock.clearStartupError).toHaveBeenCalled()
|
|
})
|
|
})
|