refactor(config): make on-disk opencode.json the single source of truth (#354)

* 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
This commit is contained in:
Chris Scott authored and GitHub committed 2026-09-19 00:28:34 -04:00
1 parent 7a3c5229df
commit 295d24ced7
116 files changed
+10291 -4208

No files matched your search

+100 -143
View File
@@ -1,5 +1,4 @@
import { create } from 'zustand'
import { persist } from 'zustand/middleware'
import type { Provider } from '@/api/providers'
export interface ModelSelection {
@@ -9,17 +8,14 @@ export interface ModelSelection {
interface ModelStore {
model: ModelSelection | null
agentModels: Record<string, ModelSelection>
variants: Record<string, string | undefined>
lastConfigModel: string | undefined
setModel: (model: ModelSelection) => void
setActiveModel: (model: ModelSelection) => void
setAgentModel: (agent: string, model: ModelSelection) => void
getAgentModel: (agent: string) => ModelSelection | null
syncModelState: (state: { recent: ModelSelection[], favorite: ModelSelection[], variant: Record<string, string | undefined> }) => void
syncFromConfig: (configModel: string | undefined, force?: boolean) => void
validateAndSyncModel: (configModel: string | undefined, providers?: Provider[]) => void
validateAndSyncModel: (configModel: string | undefined, providers?: Provider[], recent?: ModelSelection[], fallbackModel?: string) => void
getModelString: () => string | null
setVariant: (model: ModelSelection, variant: string | undefined) => void
getVariant: (model: ModelSelection) => string | undefined
@@ -40,31 +36,6 @@ function parseModelString(model: string): ModelSelection | null {
return { providerID, modelID }
}
export function modelStorePartialize(state: ModelStore): { model: ModelStore['model']; agentModels: ModelStore['agentModels'] } {
return {
model: state.model,
agentModels: state.agentModels,
}
}
function stripLegacyKeys(obj: Record<string, unknown>): void {
delete obj.recentModels
delete obj.favoriteModels
delete obj.variants
}
export function modelStoreMigrate(persistedState: unknown): unknown {
if (!persistedState || typeof persistedState !== 'object') return persistedState
const next = { ...persistedState } as Record<string, unknown>
stripLegacyKeys(next)
if (next.state && typeof next.state === 'object' && !Array.isArray(next.state)) {
const state = { ...(next.state as Record<string, unknown>) }
stripLegacyKeys(state)
next.state = state
}
return next
}
function mergeVariants(
current: Record<string, string | undefined>,
incoming: Record<string, string | undefined>
@@ -77,125 +48,111 @@ function mergeVariants(
return nextKeys.some((key) => current[key] !== next[key]) ? next : undefined
}
export const useModelStore = create<ModelStore>()(
persist(
(set, get) => ({
model: null,
agentModels: {},
variants: {},
lastConfigModel: undefined,
if (typeof localStorage !== 'undefined') localStorage.removeItem('opencode-model-selection')
setModel: (model: ModelSelection) => {
set({ model })
},
export const useModelStore = create<ModelStore>()((set, get) => ({
model: null,
variants: {},
lastConfigModel: undefined,
setActiveModel: (model: ModelSelection) => {
set({ model })
},
setModel: (model: ModelSelection) => {
set({ model })
},
setAgentModel: (agent: string, model: ModelSelection) => {
set((state) => ({
agentModels: {
...state.agentModels,
[agent]: model,
},
}))
},
setActiveModel: (model: ModelSelection) => {
set({ model })
},
getAgentModel: (agent: string) => {
const state = get()
return state.agentModels[agent] ?? null
},
syncModelState: (modelState) => {
const variants = mergeVariants(get().variants, modelState.variant)
if (variants) {
set({ variants })
}
},
syncModelState: (modelState) => {
const variants = mergeVariants(get().variants, modelState.variant)
if (variants) {
set({ variants })
}
},
syncFromConfig: (configModel: string | undefined, force = false) => {
const state = get()
if (!force && state.lastConfigModel === configModel) return
syncFromConfig: (configModel: string | undefined, force = false) => {
const state = get()
if (!force && state.lastConfigModel === configModel) return
if (configModel) {
const parsed = parseModelString(configModel)
if (parsed) {
set({ model: parsed, lastConfigModel: configModel })
return
}
}
set({ lastConfigModel: configModel })
},
validateAndSyncModel: (configModel: string | undefined, providers?: Provider[]) => {
if (!providers) {
if (configModel) {
get().syncFromConfig(configModel)
}
if (configModel) {
const parsed = parseModelString(configModel)
if (parsed) {
set({ model: parsed, lastConfigModel: configModel })
return
}
const state = get()
const currentModelExists = state.model ? modelExists(state.model, providers) : false
if (!currentModelExists) {
const parsedConfig = configModel ? parseModelString(configModel) : null
const configIsValid = parsedConfig ? modelExists(parsedConfig, providers) : false
if (configIsValid && configModel) {
get().syncFromConfig(configModel, true)
} else {
set({ model: null, lastConfigModel: configModel })
}
}
},
getModelString: () => {
const { model } = get()
if (!model) return null
return `${model.providerID}/${model.modelID}`
},
setVariant: (model: ModelSelection, variant: string | undefined) => {
const key = `${model.providerID}/${model.modelID}`
if (get().variants[key] === variant) return
set((state) => {
return {
variants: {
...state.variants,
[key]: variant,
},
}
})
},
getVariant: (model: ModelSelection) => {
const state = get()
const key = `${model.providerID}/${model.modelID}`
return state.variants[key]
},
clearVariant: (model: ModelSelection) => {
const key = `${model.providerID}/${model.modelID}`
if (!(key in get().variants)) return
set((state) => {
const newVariants = { ...state.variants }
delete newVariants[key]
return {
variants: newVariants,
}
})
},
}),
{
name: 'opencode-model-selection',
version: 2,
migrate: modelStoreMigrate,
partialize: modelStorePartialize,
}
)
)
set({ lastConfigModel: configModel })
},
validateAndSyncModel: (configModel: string | undefined, providers?: Provider[], recent: ModelSelection[] = [], fallbackModel?: string) => {
if (!providers) {
if (configModel) {
get().syncFromConfig(configModel)
}
return
}
const state = get()
if (state.model && modelExists(state.model, providers)) return
const parsedConfig = configModel ? parseModelString(configModel) : null
if (parsedConfig && modelExists(parsedConfig, providers)) {
get().syncFromConfig(configModel, true)
return
}
const recentModel = recent.find((model) => modelExists(model, providers))
if (recentModel) {
set({ model: recentModel, lastConfigModel: configModel })
return
}
const parsedFallback = fallbackModel ? parseModelString(fallbackModel) : null
if (parsedFallback && modelExists(parsedFallback, providers)) {
set({ model: parsedFallback, lastConfigModel: configModel })
return
}
if (state.model === null && state.lastConfigModel === configModel) return
set({ model: null, lastConfigModel: configModel })
},
getModelString: () => {
const { model } = get()
if (!model) return null
return `${model.providerID}/${model.modelID}`
},
setVariant: (model: ModelSelection, variant: string | undefined) => {
const key = `${model.providerID}/${model.modelID}`
if (get().variants[key] === variant) return
set((state) => {
return {
variants: {
...state.variants,
[key]: variant,
},
}
})
},
getVariant: (model: ModelSelection) => {
const state = get()
const key = `${model.providerID}/${model.modelID}`
return state.variants[key]
},
clearVariant: (model: ModelSelection) => {
const key = `${model.providerID}/${model.modelID}`
if (!(key in get().variants)) return
set((state) => {
const newVariants = { ...state.variants }
delete newVariants[key]
return {
variants: newVariants,
}
})
},
}))