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 (#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:
1 parent
7a3c5229df
commit
295d24ced7
116 files changed
+10291
-4208
No files matched your search
@@ -1,8 +1,8 @@
|
||||
import path from 'path'
|
||||
import type { SettingsService } from './settings'
|
||||
import { logger } from '../utils/logger'
|
||||
import { ensureDirectoryExists, writeFileContent } from './file-operations'
|
||||
import { getOpenCodeConfigFilePath, getWorkspacePath, ENV } from '@opencode-manager/shared/config/env'
|
||||
import { ENV } from '@opencode-manager/shared/config/env'
|
||||
import { archiveBrokenOpenCodeConfigFile, writeHealthWatchArtifact } from './opencode-config-file'
|
||||
import { restoreLastKnownGoodOpenCodeConfig, seedOpenCodeConfigFile } from './opencode-config-apply'
|
||||
import type { OpenCodeServerManager } from './opencode-single-server'
|
||||
|
||||
export const OPENCODE_LIFECYCLE_STATES = [
|
||||
@@ -55,7 +55,6 @@ export interface OpenCodeLifecycleStatus {
|
||||
interface OpenCodeSupervisorOptions {
|
||||
pollIntervalMs?: number
|
||||
failureThreshold?: number
|
||||
userId?: string
|
||||
watchEnabled?: boolean
|
||||
}
|
||||
|
||||
@@ -319,63 +318,30 @@ export class OpenCodeSupervisor {
|
||||
}
|
||||
|
||||
private async captureDebugSnapshot(): Promise<void> {
|
||||
const timestamp = new Date().toISOString().replace(/[:.]/g, '-')
|
||||
const debugPath = path.join(getWorkspacePath(), '.opencode', 'state', 'health-watch', `opencode-health-${timestamp}.json`)
|
||||
const payload = JSON.stringify({
|
||||
await writeHealthWatchArtifact('opencode-health', (timestamp) => JSON.stringify({
|
||||
capturedAt: timestamp,
|
||||
startupError: this.openCodeServerManager.getLastStartupError(),
|
||||
lifecycleState: this.state,
|
||||
attemptedRecoveryActions: this.attemptedRecoveryActions,
|
||||
}, null, 2)
|
||||
|
||||
await ensureDirectoryExists(path.dirname(debugPath))
|
||||
await writeFileContent(debugPath, payload)
|
||||
}, null, 2))
|
||||
}
|
||||
|
||||
private async rollbackToLastKnownGood(): Promise<void> {
|
||||
this.settingsService.archiveBrokenConfig(this.userId)
|
||||
const lastGood = this.settingsService.restoreToLastKnownGoodConfig(this.userId)
|
||||
if (!lastGood) {
|
||||
await archiveBrokenOpenCodeConfigFile()
|
||||
const restored = await restoreLastKnownGoodOpenCodeConfig(this.settingsService)
|
||||
if (!restored) {
|
||||
throw new Error('No last known good config available')
|
||||
}
|
||||
|
||||
const config = this.settingsService.updateOpenCodeConfig(lastGood.configName, { content: lastGood.content }, this.userId)
|
||||
if (!config) {
|
||||
throw new Error(`Failed to restore OpenCode config '${lastGood.configName}'`)
|
||||
}
|
||||
|
||||
await this.writeConfig(lastGood.content)
|
||||
this.openCodeServerManager.clearStartupError()
|
||||
await this.openCodeServerManager.restart()
|
||||
}
|
||||
|
||||
private async seedDefaultConfig(): Promise<void> {
|
||||
const seedConfig = JSON.stringify({ $schema: 'https://opencode.ai/config.json' }, null, 2)
|
||||
const defaultConfig = this.settingsService.getDefaultOpenCodeConfig(this.userId)
|
||||
|
||||
if (defaultConfig) {
|
||||
this.settingsService.updateOpenCodeConfig(defaultConfig.name, { content: seedConfig }, this.userId)
|
||||
} else {
|
||||
this.settingsService.createOpenCodeConfig(
|
||||
{
|
||||
name: 'default',
|
||||
content: seedConfig,
|
||||
isDefault: true,
|
||||
},
|
||||
this.userId,
|
||||
)
|
||||
}
|
||||
|
||||
await this.writeConfig(seedConfig)
|
||||
await seedOpenCodeConfigFile()
|
||||
this.openCodeServerManager.clearStartupError()
|
||||
await this.openCodeServerManager.restart()
|
||||
}
|
||||
|
||||
private async writeConfig(content: string): Promise<void> {
|
||||
const configPath = getOpenCodeConfigFilePath()
|
||||
await writeFileContent(configPath, content)
|
||||
}
|
||||
|
||||
private startWatching(): void {
|
||||
if (!this.isWatchEnabled()) {
|
||||
logger.info('OpenCode supervisor health polling disabled')
|
||||
@@ -429,8 +395,4 @@ export class OpenCodeSupervisor {
|
||||
private getNextRecoveryAction(): OpenCodeRecoveryAction | null {
|
||||
return OPENCODE_RECOVERY_ACTIONS.find((action) => !this.attemptedRecoveryActions.includes(action)) ?? null
|
||||
}
|
||||
|
||||
private get userId(): string {
|
||||
return this.options.userId ?? 'default'
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user