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

+57
View File
@@ -98,6 +98,63 @@ describe('atomic-json', () => {
expect(executionOrder).toEqual([1, 2, 3, 4])
})
it('runs the next queued call when the previous call rejects', async () => {
const filePath = join(tmpDir, 'reject-next.json')
const firstError = new Error('first fails')
let secondRan = false
const first = withFileLock(filePath, async () => {
throw firstError
})
const second = withFileLock(filePath, async () => {
secondRan = true
return 'second'
})
await expect(first).rejects.toThrow('first fails')
await expect(second).resolves.toBe('second')
expect(secondRan).toBe(true)
})
it('does not poison the lock for later calls after a rejection settles', async () => {
const filePath = join(tmpDir, 'reject-recovery.json')
const firstError = new Error('first fails')
await expect(
withFileLock(filePath, async () => {
throw firstError
}),
).rejects.toThrow('first fails')
await expect(withFileLock(filePath, async () => 'recovered')).resolves.toBe('recovered')
})
it('serializes calls when an operation rejects', async () => {
const filePath = join(tmpDir, 'reject-order.json')
const executionOrder: number[] = []
const first = withFileLock(filePath, async () => {
executionOrder.push(1)
await new Promise((resolve) => setTimeout(resolve, 50))
executionOrder.push(2)
throw new Error('first fails')
})
const second = withFileLock(filePath, async () => {
executionOrder.push(3)
await new Promise((resolve) => setTimeout(resolve, 50))
executionOrder.push(4)
return 'second'
})
const [firstResult, secondResult] = await Promise.allSettled([first, second])
expect(firstResult.status).toBe('rejected')
expect(secondResult).toEqual({ status: 'fulfilled', value: 'second' })
expect(executionOrder).toEqual([1, 2, 3, 4])
})
it('concurrent stress test: 50 writes then reads', async () => {
const filePath = join(tmpDir, 'stress.json')
const numOps = 50
+1 -1
View File
@@ -44,7 +44,7 @@ export async function withFileLock<T>(filePath: string, fn: () => Promise<T>): P
}
}
const newLock = previousLock ? previousLock.then(executeWithLock) : executeWithLock()
const newLock = previousLock ? previousLock.then(executeWithLock, executeWithLock) : executeWithLock()
fileLockPromises.set(absolutePath, newLock)
return newLock as Promise<T>
+25 -1
View File
@@ -1,5 +1,5 @@
import path from 'path'
import { promises as fs, mkdirSync, accessSync, constants } from 'node:fs'
import { promises as fs, mkdirSync, accessSync, constants, realpathSync } from 'node:fs'
interface MkdirSafeOptions {
mode?: number
@@ -10,6 +10,22 @@ function isPermissionError(error: unknown): boolean {
return code === 'EACCES' || code === 'EPERM'
}
export async function canonicalPath(target: string): Promise<string> {
try {
return await fs.realpath(target)
} catch {
return target
}
}
export function canonicalPathSync(target: string): string {
try {
return realpathSync(target)
} catch {
return target
}
}
export async function writeFileAtomic(filePath: string, content: string, options: { mode?: number } = {}): Promise<void> {
const dir = path.dirname(filePath)
await mkdirSafe(dir)
@@ -23,6 +39,14 @@ export async function writeFileAtomic(filePath: string, content: string, options
}
}
export async function existingFileMode(filePath: string): Promise<number | undefined> {
try {
return (await fs.stat(filePath)).mode & 0o777
} catch {
return undefined
}
}
export async function mkdirSafe(dirPath: string, options: MkdirSafeOptions = {}): Promise<void> {
try {
await fs.mkdir(dirPath, { ...options, recursive: true })