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

+61 -178
View File
@@ -1,12 +1,10 @@
import { describe, it, expect, beforeEach, vi } from 'vitest'
import { useModelStore, modelExists, modelStorePartialize, modelStoreMigrate } from '@/stores/modelStore'
import { useModelStore, modelExists } from '@/stores/modelStore'
import type { Provider } from '@/api/providers'
beforeEach(() => {
useModelStore.persist.clearStorage()
useModelStore.setState({
model: null,
agentModels: {},
variants: {},
lastConfigModel: undefined,
})
@@ -24,11 +22,17 @@ function makeProvider(overrides: Partial<Provider>): Provider {
}
}
const openaiProviders = [
makeProvider({
id: 'openai',
models: { 'gpt-4o': { id: 'gpt-4o', name: 'GPT-4o' } },
}),
]
describe('validateAndSyncModel', () => {
beforeEach(() => {
useModelStore.setState({
model: null,
agentModels: {},
variants: {},
lastConfigModel: undefined,
})
@@ -40,41 +44,75 @@ describe('validateAndSyncModel', () => {
expect(useModelStore.getState().model).toEqual({ providerID: 'anthropic', modelID: 'claude-sonnet-4' })
})
it('sets active model from configModel when current model not in providers but configModel parses to valid model', () => {
const providers = [
makeProvider({
id: 'openai',
models: { 'gpt-4o': { id: 'gpt-4o', name: 'GPT-4o' } },
}),
]
it('uses config model when current model is missing and config is valid', () => {
useModelStore.setState({
model: { providerID: 'anthropic', modelID: 'claude-sonnet-4' },
})
useModelStore.getState().validateAndSyncModel('openai/gpt-4o', providers)
useModelStore.getState().validateAndSyncModel('openai/gpt-4o', openaiProviders)
expect(useModelStore.getState().model).toEqual({ providerID: 'openai', modelID: 'gpt-4o' })
})
it('clears active model when invalid and no config fallback', () => {
const providers = [
makeProvider({
id: 'openai',
models: { 'gpt-4o': { id: 'gpt-4o', name: 'GPT-4o' } },
}),
]
it('uses first valid recent model when config is invalid', () => {
useModelStore.setState({
model: { providerID: 'anthropic', modelID: 'claude-sonnet-4' },
})
useModelStore.getState().validateAndSyncModel(undefined, providers)
useModelStore.getState().validateAndSyncModel('nonexistent/model', openaiProviders, [
{ providerID: 'nonexistent', modelID: 'model' },
{ providerID: 'openai', modelID: 'gpt-4o' },
])
expect(useModelStore.getState().model).toEqual({ providerID: 'openai', modelID: 'gpt-4o' })
})
it('uses fallback model when config and recent are invalid', () => {
useModelStore.setState({
model: { providerID: 'anthropic', modelID: 'claude-sonnet-4' },
})
useModelStore.getState().validateAndSyncModel(
'nonexistent/model',
openaiProviders,
[{ providerID: 'nonexistent', modelID: 'model' }],
'openai/gpt-4o'
)
expect(useModelStore.getState().model).toEqual({ providerID: 'openai', modelID: 'gpt-4o' })
})
it('leaves model null when config, recent, and fallback are all invalid', () => {
useModelStore.setState({
model: { providerID: 'anthropic', modelID: 'claude-sonnet-4' },
})
useModelStore.getState().validateAndSyncModel(
'nonexistent/model',
openaiProviders,
[{ providerID: 'nonexistent', modelID: 'model' }],
'nonexistent/fallback'
)
expect(useModelStore.getState().model).toBeNull()
})
it('invalidates stale hydrated model when current model not in providers and no config fallback', () => {
it('does not override a current model that exists in providers', () => {
useModelStore.setState({
model: { providerID: 'openai', modelID: 'gpt-4o' },
})
useModelStore.getState().validateAndSyncModel(
'nonexistent/model',
openaiProviders,
[{ providerID: 'nonexistent', modelID: 'model' }],
'nonexistent/fallback'
)
expect(useModelStore.getState().model).toEqual({ providerID: 'openai', modelID: 'gpt-4o' })
})
it('clears stale hydrated model when nothing resolves', () => {
const providers = [
makeProvider({
id: 'openrouter',
@@ -126,7 +164,6 @@ describe('setModel', () => {
beforeEach(() => {
useModelStore.setState({
model: null,
agentModels: {},
variants: {},
lastConfigModel: undefined,
})
@@ -149,7 +186,6 @@ describe('setActiveModel', () => {
beforeEach(() => {
useModelStore.setState({
model: null,
agentModels: {},
variants: {},
lastConfigModel: undefined,
})
@@ -162,44 +198,10 @@ describe('setActiveModel', () => {
})
})
describe('agentModels', () => {
beforeEach(() => {
useModelStore.setState({
model: null,
agentModels: {},
variants: {},
lastConfigModel: undefined,
})
})
it('setAgentModel stores model for agent', () => {
const model = { providerID: 'openai', modelID: 'gpt-4o' }
useModelStore.getState().setAgentModel('agent-1', model)
expect(useModelStore.getState().agentModels['agent-1']).toEqual(model)
})
it('getAgentModel returns model for agent', () => {
const model = { providerID: 'openai', modelID: 'gpt-4o' }
useModelStore.getState().setAgentModel('agent-1', model)
expect(useModelStore.getState().getAgentModel('agent-1')).toEqual(model)
})
it('getAgentModel returns null for unknown agent', () => {
expect(useModelStore.getState().getAgentModel('unknown')).toBeNull()
})
it('setAgentModel replaces existing model for same agent', () => {
useModelStore.getState().setAgentModel('agent-1', { providerID: 'openai', modelID: 'gpt-4o' })
useModelStore.getState().setAgentModel('agent-1', { providerID: 'anthropic', modelID: 'claude-sonnet-4' })
expect(useModelStore.getState().agentModels['agent-1']).toEqual({ providerID: 'anthropic', modelID: 'claude-sonnet-4' })
})
})
describe('syncFromConfig', () => {
beforeEach(() => {
useModelStore.setState({
model: null,
agentModels: {},
variants: {},
lastConfigModel: undefined,
})
@@ -241,7 +243,6 @@ describe('getModelString', () => {
beforeEach(() => {
useModelStore.setState({
model: null,
agentModels: {},
variants: {},
lastConfigModel: undefined,
})
@@ -261,7 +262,6 @@ describe('variants', () => {
beforeEach(() => {
useModelStore.setState({
model: null,
agentModels: {},
variants: {},
lastConfigModel: undefined,
})
@@ -323,7 +323,6 @@ describe('syncModelState', () => {
beforeEach(() => {
useModelStore.setState({
model: null,
agentModels: {},
variants: {},
lastConfigModel: undefined,
})
@@ -396,119 +395,3 @@ describe('modelExists', () => {
expect(modelExists({ providerID: 'openai', modelID: 'gpt-4o' }, [])).toBe(false)
})
})
describe('modelStorePartialize', () => {
it('returns only model and agentModels', () => {
const result = modelStorePartialize({
model: { providerID: 'anthropic', modelID: 'claude-sonnet-4' },
agentModels: { 'agent-1': { providerID: 'openai', modelID: 'gpt-4o' } },
variants: { 'anthropic/claude-sonnet-4': 'some-variant' },
lastConfigModel: 'anthropic/claude-sonnet-4',
} as Parameters<typeof modelStorePartialize>[0])
expect(result).toEqual({
model: { providerID: 'anthropic', modelID: 'claude-sonnet-4' },
agentModels: { 'agent-1': { providerID: 'openai', modelID: 'gpt-4o' } },
})
expect(result).not.toHaveProperty('variants')
expect(result).not.toHaveProperty('lastConfigModel')
})
})
describe('modelStoreMigrate', () => {
it('strips legacy recentModels, favoriteModels, and variants keys from flat state (Zustand v5 call shape)', () => {
const legacy = {
model: { providerID: 'p', modelID: 'm' },
agentModels: {},
recentModels: [{ providerID: 'old', modelID: 'old' }],
favoriteModels: [{ providerID: 'old', modelID: 'old' }],
variants: { 'old/old': 'v1' },
}
const migrated = modelStoreMigrate(legacy)
expect(migrated).toEqual({
model: { providerID: 'p', modelID: 'm' },
agentModels: {},
})
})
it('preserves model and agentModels during migration of flat state', () => {
const legacy = {
model: { providerID: 'anthropic', modelID: 'claude-sonnet-4' },
agentModels: { 'agent-1': { providerID: 'openai', modelID: 'gpt-4o' } },
recentModels: [{ providerID: 'openai', modelID: 'gpt-4o' }],
favoriteModels: [],
variants: {},
}
const migrated = modelStoreMigrate(legacy)
expect(migrated).toEqual({
model: { providerID: 'anthropic', modelID: 'claude-sonnet-4' },
agentModels: { 'agent-1': { providerID: 'openai', modelID: 'gpt-4o' } },
})
})
it('handles null/undefined input gracefully', () => {
expect(modelStoreMigrate(null)).toBeNull()
expect(modelStoreMigrate(undefined)).toBeUndefined()
})
it('also strips legacy keys from nested Zustand state envelope', () => {
const legacy = {
state: {
model: { providerID: 'p', modelID: 'm' },
agentModels: {},
recentModels: [{ providerID: 'old', modelID: 'old' }],
favoriteModels: [{ providerID: 'old', modelID: 'old' }],
variants: { 'old/old': 'v1' },
},
version: 0,
}
const migrated = modelStoreMigrate(legacy)
expect(migrated).toEqual({
state: {
model: { providerID: 'p', modelID: 'm' },
agentModels: {},
},
version: 0,
})
})
it('returns non-object input unchanged', () => {
expect(modelStoreMigrate('string')).toBe('string')
expect(modelStoreMigrate(42)).toBe(42)
})
it('handles state without legacy keys (new users)', () => {
const clean = {
model: { providerID: 'p', modelID: 'm' },
agentModels: {},
}
const migrated = modelStoreMigrate(clean)
expect(migrated).toEqual(clean)
})
it('migrate function is wired through persist.getOptions()', () => {
const legacy = {
model: { providerID: 'p', modelID: 'm' },
agentModels: {},
recentModels: [{ providerID: 'old', modelID: 'old' }],
favoriteModels: [{ providerID: 'old', modelID: 'old' }],
variants: { 'old/old': 'v1' },
}
const options = useModelStore.persist.getOptions()
const migrated = options.migrate?.(legacy, 0)
expect(migrated).toEqual({
model: { providerID: 'p', modelID: 'm' },
agentModels: {},
})
})
})