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

@@ -0,0 +1,184 @@
import { readdir, rm, stat } from 'fs/promises'
import path from 'path'
import type { ZodIssue } from 'zod'
import { getOpenCodeConfigFilePath, getOpenCodeHealthWatchPath } from '@opencode-manager/shared/config/env'
import { OpenCodeConfigSchema } from '@opencode-manager/shared/schemas'
import { parseJsonc } from '@opencode-manager/shared/utils'
import type { OpenCodeConfigFile, OpenCodeConfigInput, OpenCodeConfigValidationIssue } from '../types/settings'
import { logger } from '../utils/logger'
import { withFileLock } from '../utils/atomic-json'
import { existingFileMode, writeFileAtomic } from '../utils/fs-safe'
import { ensureDirectoryExists, fileExists, readFileContent, writeFileContent } from './file-operations'
export const OPENCODE_CONFIG_SEED = JSON.stringify({ $schema: 'https://opencode.ai/config.json' }, null, 2)
export const HEALTH_WATCH_MAX_ENTRIES = 20
export function withOpenCodeConfigLock<T>(fn: () => Promise<T>): Promise<T> {
return withFileLock(getOpenCodeConfigFilePath(), fn)
}
interface OpenCodeConfigParseResult {
content: Record<string, unknown>
isValid: boolean
validationIssues?: OpenCodeConfigValidationIssue[]
}
export function normalizeOpenCodeConfigContent(content: OpenCodeConfigInput | string): string {
return typeof content === 'string' ? content : JSON.stringify(content, null, 2)
}
export function toOpenCodeConfigValidationIssues(issues: ZodIssue[]): OpenCodeConfigValidationIssue[] {
return issues.map((issue) => ({
path: issue.path.length > 0 ? issue.path.join('.') : 'root',
message: issue.message,
}))
}
export function parseOpenCodeConfigContent(rawContent: string): OpenCodeConfigParseResult {
let parsed: unknown
try {
parsed = parseJsonc(rawContent)
} catch (error) {
const message = error instanceof Error ? error.message : 'Invalid JSONC'
logger.error(`Failed to parse OpenCode config: ${message}`)
return {
content: {},
isValid: false,
validationIssues: [{ path: 'root', message }],
}
}
const content = parsed && typeof parsed === 'object' && !Array.isArray(parsed)
? parsed as Record<string, unknown>
: {}
const validated = OpenCodeConfigSchema.safeParse(parsed)
if (validated.success) {
return {
content: validated.data as Record<string, unknown>,
isValid: true,
}
}
const validationIssues = toOpenCodeConfigValidationIssues(validated.error.issues)
logger.error(`Failed to validate OpenCode config: ${validationIssues.map((issue) => `${issue.path}: ${issue.message}`).join('; ')}`)
return {
content,
isValid: false,
validationIssues,
}
}
export async function readOpenCodeConfigFile(): Promise<OpenCodeConfigFile | null> {
const configPath = getOpenCodeConfigFilePath()
let updatedAt: number
try {
const stats = await stat(configPath)
updatedAt = stats.mtimeMs
} catch (error) {
if ((error as NodeJS.ErrnoException).code === 'ENOENT') {
return null
}
throw error
}
const rawContent = await readFileContent(configPath)
return {
path: configPath,
rawContent,
...parseOpenCodeConfigContent(rawContent),
updatedAt,
}
}
export async function writeOpenCodeConfigFile(rawContent: string): Promise<OpenCodeConfigFile> {
const parsed = OpenCodeConfigSchema.parse(parseJsonc(rawContent))
const configPath = getOpenCodeConfigFilePath()
await writeFileAtomic(configPath, rawContent, { mode: await existingFileMode(configPath) })
const stats = await stat(configPath)
return {
path: configPath,
rawContent,
content: parsed as Record<string, unknown>,
isValid: true,
updatedAt: stats.mtimeMs,
}
}
export async function pruneHealthWatchDirectory(dirPath: string): Promise<void> {
try {
const entries = await readdir(dirPath, { withFileTypes: true })
const files = await Promise.all(
entries
.filter((entry) => entry.isFile())
.map(async (entry) => {
const filePath = path.join(dirPath, entry.name)
const fileStats = await stat(filePath)
return { filePath, mtimeMs: fileStats.mtimeMs }
}),
)
if (files.length <= HEALTH_WATCH_MAX_ENTRIES) return
files.sort((left, right) => right.mtimeMs - left.mtimeMs)
await Promise.all(
files.slice(HEALTH_WATCH_MAX_ENTRIES).map((file) => rm(file.filePath, { force: true })),
)
} catch (error) {
logger.warn('Failed to prune OpenCode health-watch directory:', error)
}
}
export async function writeHealthWatchArtifact(
prefix: string,
buildContent: (timestamp: string) => string,
): Promise<string> {
const timestamp = new Date().toISOString().replace(/[:.]/g, '-')
const artifactPath = path.join(getOpenCodeHealthWatchPath(), `${prefix}-${timestamp}.json`)
await ensureDirectoryExists(getOpenCodeHealthWatchPath())
await writeFileContent(artifactPath, buildContent(timestamp))
await pruneHealthWatchDirectory(getOpenCodeHealthWatchPath())
return artifactPath
}
export async function archiveBrokenOpenCodeConfigFile(): Promise<string | null> {
const configPath = getOpenCodeConfigFilePath()
if (!(await fileExists(configPath))) {
return null
}
try {
const content = await readFileContent(configPath)
const archivePath = await writeHealthWatchArtifact('opencode-config-broken', () => content)
logger.warn(`Archived broken OpenCode config to ${archivePath}`)
return archivePath
} catch (error) {
logger.error('Failed to archive broken OpenCode config:', error)
return null
}
}
export async function deleteOpenCodeConfigFile(): Promise<boolean> {
const configPath = getOpenCodeConfigFilePath()
if (!(await fileExists(configPath))) {
logger.warn('Config file does not exist:', configPath)
return false
}
try {
await rm(configPath, { force: true })
logger.info('Deleted filesystem config to allow server startup:', configPath)
return true
} catch (error) {
logger.error('Failed to delete config file:', error)
return false
}
}