mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 04:44:21 +01:00
feat(config): support layered OpenCode config sources with revision-guarded saves (#358)
* feat(config): support layered OpenCode config sources with revision-guarded saves * refactor(config): harden multi-source OpenCode config handling - extract shared config-source helpers and default source name - reject shadowed removals with 409 and require sources/revision - restart the server on reload with session resume - mirror multiple host config files on import and warn on removals - skip restart for mcp-only saves - drop removedFields and CONFIG_PATCH_TIMEOUT_MS * fix(config): return null when archiving broken OpenCode config fails
This commit is contained in:
1 parent
97370a8296
commit
b5dd900efb
53 files changed
+3003
-1665
No files matched your search
@@ -1,27 +1,35 @@
|
||||
import { OpenCodeConfigSchema } from '@opencode-manager/shared/schemas'
|
||||
import { parseJsonc } from '@opencode-manager/shared/utils'
|
||||
import type { OpenCodeConfigFile, OpenCodeConfigInput } from '../types/settings'
|
||||
import { OPENCODE_CONFIG_SEED, normalizeOpenCodeConfigContent, readOpenCodeConfigFile, withOpenCodeConfigLock, writeOpenCodeConfigFile } from './opencode-config-file'
|
||||
import { patchConfigWithRecovery, type PatchConfigValidationIssue } from './opencode/config-recovery'
|
||||
import type { OpenCodeClient } from './opencode/client'
|
||||
import { isDeepStrictEqual } from 'node:util'
|
||||
import type {
|
||||
OpenCodeConfigFile,
|
||||
OpenCodeConfigSourceName,
|
||||
} from '../types/settings'
|
||||
import {
|
||||
buildOpenCodeConfigSeedSnapshot,
|
||||
readOpenCodeConfigFile,
|
||||
readOpenCodeConfigSnapshot,
|
||||
restoreOpenCodeConfigSnapshot,
|
||||
serializeOpenCodeConfigSnapshot,
|
||||
updateOpenCodeConfigFile,
|
||||
withOpenCodeConfigLock,
|
||||
} from './opencode-config-file'
|
||||
import { opencodeServerManager } from './opencode-single-server'
|
||||
import type { SettingsService } from './settings'
|
||||
|
||||
export type ApplyOpenCodeConfigResult =
|
||||
| { status: 'restart_pending'; config: OpenCodeConfigFile }
|
||||
| { status: 'applied'; config: OpenCodeConfigFile; removedFields: string[] }
|
||||
| { status: 'rejected'; error: string; validationIssues: PatchConfigValidationIssue[]; removedFields: string[] }
|
||||
| { status: 'applied'; config: OpenCodeConfigFile }
|
||||
|
||||
export interface ApplyOpenCodeConfigInput {
|
||||
content: OpenCodeConfigInput | string
|
||||
openCodeClient: OpenCodeClient
|
||||
content: Record<string, unknown> | string
|
||||
source?: OpenCodeConfigSourceName
|
||||
expectedRevision?: string
|
||||
settingsService: SettingsService
|
||||
}
|
||||
|
||||
export async function captureLastKnownGoodOpenCodeConfig(settingsService: SettingsService): Promise<OpenCodeConfigFile | null> {
|
||||
const previous = await readOpenCodeConfigFile()
|
||||
if (previous?.isValid) {
|
||||
settingsService.saveLastKnownGoodConfig(previous.rawContent)
|
||||
settingsService.saveLastKnownGoodConfig(serializeOpenCodeConfigSnapshot(previous))
|
||||
}
|
||||
return previous
|
||||
}
|
||||
@@ -32,90 +40,74 @@ export async function restoreLastKnownGoodOpenCodeConfig(settingsService: Settin
|
||||
return null
|
||||
}
|
||||
|
||||
const config = await withOpenCodeConfigLock(() => writeOpenCodeConfigFile(lastGood))
|
||||
const config = await withOpenCodeConfigLock(() => restoreOpenCodeConfigSnapshot(lastGood))
|
||||
opencodeServerManager.clearStartupError()
|
||||
return config
|
||||
}
|
||||
|
||||
export async function seedOpenCodeConfigFile(): Promise<OpenCodeConfigFile> {
|
||||
return withOpenCodeConfigLock(() => writeOpenCodeConfigFile(OPENCODE_CONFIG_SEED))
|
||||
}
|
||||
|
||||
function didConfigFieldChange(
|
||||
previous: Record<string, unknown> | undefined,
|
||||
next: Record<string, unknown> | undefined,
|
||||
field: string,
|
||||
): boolean {
|
||||
return JSON.stringify(previous?.[field]) !== JSON.stringify(next?.[field])
|
||||
}
|
||||
|
||||
function needsOpenCodeRestart(
|
||||
previous: Record<string, unknown> | undefined,
|
||||
next: Record<string, unknown> | undefined,
|
||||
): boolean {
|
||||
return ['agent', 'plugin', 'skills', 'provider'].some((field) => didConfigFieldChange(previous, next, field))
|
||||
return withOpenCodeConfigLock(async () => {
|
||||
const config = await restoreOpenCodeConfigSnapshot(buildOpenCodeConfigSeedSnapshot())
|
||||
if (!config) {
|
||||
throw new Error('Failed to seed OpenCode config')
|
||||
}
|
||||
return config
|
||||
})
|
||||
}
|
||||
|
||||
export function toOpenCodeConfigApplyResponse(
|
||||
result: ApplyOpenCodeConfigResult,
|
||||
): { status: 200 | 400; body: Record<string, unknown> } {
|
||||
if (result.status === 'restart_pending') {
|
||||
return { status: 200, body: { ...result.config, restartRequired: true } }
|
||||
): { status: 200; body: Record<string, unknown> } {
|
||||
return {
|
||||
status: 200,
|
||||
body: result.status === 'restart_pending'
|
||||
? { ...result.config, restartRequired: true }
|
||||
: { ...result.config },
|
||||
}
|
||||
}
|
||||
|
||||
function requiresOpenCodeRestart(previous: OpenCodeConfigFile | null, next: OpenCodeConfigFile): boolean {
|
||||
if (previous?.isValid !== next.isValid) {
|
||||
return true
|
||||
}
|
||||
|
||||
if (result.status === 'applied') {
|
||||
return {
|
||||
status: 200,
|
||||
body: result.removedFields.length > 0
|
||||
? { ...result.config, removedFields: result.removedFields }
|
||||
: { ...result.config },
|
||||
const previousContent = previous?.content ?? {}
|
||||
const keys = new Set([...Object.keys(previousContent), ...Object.keys(next.content)])
|
||||
for (const key of keys) {
|
||||
if (!isDeepStrictEqual(previousContent[key], next.content[key]) && key !== 'mcp') {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
status: 400,
|
||||
body: {
|
||||
error: 'Config validation failed',
|
||||
details: result.error,
|
||||
validationIssues: result.validationIssues,
|
||||
removedFields: result.removedFields,
|
||||
},
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
export async function applyOpenCodeConfigUpdate(
|
||||
input: ApplyOpenCodeConfigInput,
|
||||
): Promise<ApplyOpenCodeConfigResult> {
|
||||
return withOpenCodeConfigLock(async () => {
|
||||
const { content, openCodeClient, settingsService } = input
|
||||
const { content, source, expectedRevision, settingsService } = input
|
||||
|
||||
const rawContent = normalizeOpenCodeConfigContent(content)
|
||||
const nextContent = OpenCodeConfigSchema.parse(parseJsonc(rawContent))
|
||||
const snapshot = await readOpenCodeConfigSnapshot()
|
||||
const previous = await readOpenCodeConfigFile(snapshot)
|
||||
|
||||
const previous = await captureLastKnownGoodOpenCodeConfig(settingsService)
|
||||
const next = await updateOpenCodeConfigFile(content, { source, expectedRevision, snapshot })
|
||||
|
||||
if (needsOpenCodeRestart(previous?.content, nextContent)) {
|
||||
const config = await writeOpenCodeConfigFile(rawContent)
|
||||
opencodeServerManager.markRestartPending()
|
||||
return { status: 'restart_pending', config }
|
||||
}
|
||||
|
||||
const patchResult = await patchConfigWithRecovery(openCodeClient, nextContent)
|
||||
if (!patchResult.success) {
|
||||
return {
|
||||
status: 'rejected',
|
||||
error: patchResult.error ?? 'Config validation failed',
|
||||
validationIssues: patchResult.details ?? [],
|
||||
removedFields: patchResult.removedFields ?? [],
|
||||
if (previous?.isValid) {
|
||||
const snapshot = serializeOpenCodeConfigSnapshot(previous)
|
||||
try {
|
||||
settingsService.saveLastKnownGoodConfig(snapshot)
|
||||
} catch (error) {
|
||||
await restoreOpenCodeConfigSnapshot(snapshot)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
const removedFields = patchResult.removedFields ?? []
|
||||
const contentToWrite = removedFields.length > 0
|
||||
? JSON.stringify(patchResult.appliedConfig ?? nextContent, null, 2)
|
||||
: rawContent
|
||||
const config = await writeOpenCodeConfigFile(contentToWrite)
|
||||
if (requiresOpenCodeRestart(previous, next)) {
|
||||
opencodeServerManager.markRestartPending()
|
||||
return { status: 'restart_pending', config: next }
|
||||
}
|
||||
|
||||
return { status: 'applied', config, removedFields }
|
||||
return { status: 'applied', config: next }
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user