mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 12:54:20 +01:00
fix(sandbox): bound lifecycle queue and harden install/backup paths
Bound the OpenCode supervisor lifecycle queue to one running plus one pending operation so repeated restarts cannot stack unbounded HTTP requests, while exempting stop() so shutdown is never dropped. Reject enforcement backups that carry no recoverable removed sections, matching the existing fail-closed handling of unparseable backups. Install OpenCode through a private mktemp staging directory instead of the predictable /tmp/opencode path. Use named path/os imports in the files added by this branch.
This commit is contained in:
1 parent
0481160f5e
commit
0215de9d92
9 files changed
+108
-28
No files matched your search
@@ -367,8 +367,10 @@ async function restorePluginEntries(dir: string): Promise<void> {
|
||||
async function readPluginConfigBackup(backupPath: string): Promise<PluginConfigBackup | null> {
|
||||
try {
|
||||
const parsed = parseJsonc(await fs.readFile(backupPath, 'utf-8')) as unknown
|
||||
if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) {
|
||||
return parsed as PluginConfigBackup
|
||||
if (!isRecord(parsed)) return null
|
||||
const backup = parsed as PluginConfigBackup
|
||||
if (isRecord(backup.removedSections) || Array.isArray(backup.originalPlugins) || Array.isArray(backup.plugin)) {
|
||||
return backup
|
||||
}
|
||||
} catch {
|
||||
return null
|
||||
|
||||
@@ -27,6 +27,8 @@ export const OPENCODE_RECOVERY_ACTIONS = [
|
||||
|
||||
export type OpenCodeRecoveryAction = (typeof OPENCODE_RECOVERY_ACTIONS)[number]
|
||||
|
||||
const MAX_QUEUED_LIFECYCLE_OPERATIONS = 2
|
||||
|
||||
export type OpenCodeOperationReason =
|
||||
| 'backend_startup'
|
||||
| 'health_poll'
|
||||
@@ -66,6 +68,7 @@ export class OpenCodeSupervisor {
|
||||
private consecutiveFailures = 0
|
||||
private operationInProgress = false
|
||||
private operationTail: Promise<void> = Promise.resolve()
|
||||
private queuedOperations = 0
|
||||
private updatedAt = new Date().toISOString()
|
||||
|
||||
constructor(
|
||||
@@ -167,7 +170,7 @@ export class OpenCodeSupervisor {
|
||||
await this.openCodeServerManager.stop()
|
||||
this.setState('stopped')
|
||||
return this.getStatus()
|
||||
})
|
||||
}, { droppable: false })
|
||||
|
||||
logger.info('Stopped OpenCode supervisor')
|
||||
}
|
||||
@@ -194,20 +197,33 @@ export class OpenCodeSupervisor {
|
||||
}
|
||||
}
|
||||
|
||||
private async runLifecycleOperation(operation: () => Promise<OpenCodeLifecycleStatus>): Promise<OpenCodeLifecycleStatus> {
|
||||
private async runLifecycleOperation(
|
||||
operation: () => Promise<OpenCodeLifecycleStatus>,
|
||||
options: { droppable?: boolean } = {},
|
||||
): Promise<OpenCodeLifecycleStatus> {
|
||||
if ((options.droppable ?? true) && this.queuedOperations >= MAX_QUEUED_LIFECYCLE_OPERATIONS) {
|
||||
logger.warn('Dropped an OpenCode lifecycle request: one operation is running and another is already queued')
|
||||
return this.getStatus()
|
||||
}
|
||||
|
||||
this.queuedOperations += 1
|
||||
const previousTail = this.operationTail
|
||||
let releaseTail!: () => void
|
||||
this.operationTail = new Promise<void>((resolve) => {
|
||||
releaseTail = resolve
|
||||
})
|
||||
|
||||
await previousTail
|
||||
this.operationInProgress = true
|
||||
try {
|
||||
return await operation()
|
||||
await previousTail
|
||||
this.operationInProgress = true
|
||||
try {
|
||||
return await operation()
|
||||
} finally {
|
||||
this.operationInProgress = false
|
||||
this.touch()
|
||||
}
|
||||
} finally {
|
||||
this.operationInProgress = false
|
||||
this.touch()
|
||||
this.queuedOperations -= 1
|
||||
releaseTail()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import path from 'path'
|
||||
import { join } from 'path'
|
||||
import { writeFileAtomic } from '../../utils/fs-safe'
|
||||
import { buildGhEnvPluginSource } from '../opencode-gh-env-plugin'
|
||||
import { buildSandboxPluginSource } from '../opencode-sandbox-plugin'
|
||||
@@ -16,12 +16,12 @@ export const MANAGED_OPENCODE_PLUGINS: readonly ManagedOpenCodePlugin[] = [
|
||||
export const TRUSTED_OPENCODE_PLUGIN_FILENAMES: readonly string[] = MANAGED_OPENCODE_PLUGINS.map((plugin) => plugin.filename)
|
||||
|
||||
export function getOpenCodePluginDir(configHome: string): string {
|
||||
return path.join(configHome, 'opencode', 'plugin')
|
||||
return join(configHome, 'opencode', 'plugin')
|
||||
}
|
||||
|
||||
export async function installManagedPlugins(configHome: string): Promise<void> {
|
||||
const dir = getOpenCodePluginDir(configHome)
|
||||
for (const plugin of MANAGED_OPENCODE_PLUGINS) {
|
||||
await writeFileAtomic(path.join(dir, plugin.filename), plugin.buildSource())
|
||||
await writeFileAtomic(join(dir, plugin.filename), plugin.buildSource())
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
import path from 'path'
|
||||
import { join } from 'path'
|
||||
import { getWorkspacePath } from '@opencode-manager/shared/config/env'
|
||||
import { sandboxPlanTimeoutMs, SANDBOX_UNAVAILABLE_PREFIX } from './command'
|
||||
import { writeFileAtomic } from '../../utils/fs-safe'
|
||||
@@ -85,15 +85,15 @@ main()
|
||||
`
|
||||
|
||||
export function getSandboxShellDir(configHome: string): string {
|
||||
return path.join(configHome, 'ocm')
|
||||
return join(configHome, 'ocm')
|
||||
}
|
||||
|
||||
export function getSandboxShellPath(configHome: string): string {
|
||||
return path.join(getSandboxShellDir(configHome), SANDBOX_SHELL_FILENAME)
|
||||
return join(getSandboxShellDir(configHome), SANDBOX_SHELL_FILENAME)
|
||||
}
|
||||
|
||||
export function getEnforcedSandboxShellPath(): string {
|
||||
return getSandboxShellPath(path.join(getWorkspacePath(), '.config'))
|
||||
return getSandboxShellPath(join(getWorkspacePath(), '.config'))
|
||||
}
|
||||
|
||||
export async function installSandboxShell(configHome: string): Promise<void> {
|
||||
|
||||
Reference in new issue
Block a user