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:
Chris Scott authored and GitHub committed 2026-09-20 15:51:40 -04:00
1 parent 97370a8296
commit b5dd900efb
53 files changed
+3003 -1665

No files matched your search

+43 -4
View File
@@ -2,8 +2,14 @@ import { Hono } from 'hono'
import { z } from 'zod'
import { UpdateOpenCodeConfigRequestSchema } from '@opencode-manager/shared/schemas'
import type { SettingsService } from '../services/settings'
import type { OpenCodeClient } from '../services/opencode/client'
import { readOpenCodeConfigFile } from '../services/opencode-config-file'
import { UpstreamError, type OpenCodeClient } from '../services/opencode/client'
import {
OpenCodeConfigConflictError,
OpenCodeConfigShadowedRemovalError,
OpenCodeConfigSourceInvalidError,
readOpenCodeConfigFile,
withOpenCodeConfigLock,
} from '../services/opencode-config-file'
import { applyOpenCodeConfigUpdate, toOpenCodeConfigApplyResponse } from '../services/opencode-config-apply'
import { logger } from '../utils/logger'
@@ -12,7 +18,7 @@ export function createOpenCodeConfigRoutes(settingsService: SettingsService, ope
app.get('/', async (c) => {
try {
const config = await readOpenCodeConfigFile()
const config = await withOpenCodeConfigLock(readOpenCodeConfigFile)
if (!config) {
return c.json({ error: 'No OpenCode config file found' }, 404)
}
@@ -23,6 +29,22 @@ export function createOpenCodeConfigRoutes(settingsService: SettingsService, ope
}
})
app.get('/effective', async (c) => {
try {
const config = await openCodeClient.getJson<Record<string, unknown>>('/global/config')
return c.json(config)
} catch (error) {
logger.error('Failed to get effective OpenCode config:', error)
if (error instanceof UpstreamError) {
if (error.status === 502) {
return c.json({ error: 'OpenCode server unavailable' }, 503)
}
return c.json({ error: 'Failed to get effective OpenCode config' }, 502)
}
return c.json({ error: 'Failed to get effective OpenCode config' }, 500)
}
})
app.put('/', async (c) => {
let body: unknown
try {
@@ -39,16 +61,33 @@ export function createOpenCodeConfigRoutes(settingsService: SettingsService, ope
try {
const result = await applyOpenCodeConfigUpdate({
content: parsed.data.content,
openCodeClient,
source: parsed.data.source,
expectedRevision: parsed.data.expectedRevision,
settingsService,
})
const { status, body: responseBody } = toOpenCodeConfigApplyResponse(result)
return c.json(responseBody, status)
} catch (error) {
logger.error('Failed to update OpenCode config:', error)
if (error instanceof OpenCodeConfigConflictError) {
return c.json({
error: error.message,
expectedRevision: error.expectedRevision,
actualRevision: error.actualRevision,
}, 409)
}
if (error instanceof OpenCodeConfigSourceInvalidError) {
return c.json({ error: error.message, sources: error.sources }, 400)
}
if (error instanceof OpenCodeConfigShadowedRemovalError) {
return c.json({ error: error.message, paths: error.paths, sources: error.sources }, 409)
}
if (error instanceof z.ZodError) {
return c.json({ error: 'Invalid config data', details: error.issues }, 400)
}
if (error instanceof SyntaxError) {
return c.json({ error: 'Invalid config data', details: error.message }, 400)
}
return c.json({ error: 'Failed to update OpenCode config' }, 500)
}
})