From bbfe8dcf7ea7bfc7c1650281e9ddd6f4bd7fe57c Mon Sep 17 00:00:00 2001 From: Nawaz Dhandala Date: Thu, 8 Jan 2026 13:39:40 +0000 Subject: [PATCH] Add backup and restoration for opencode.json in OpenCodeAgent --- AIAgent/CodeAgents/OpenCodeAgent.ts | 50 +++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/AIAgent/CodeAgents/OpenCodeAgent.ts b/AIAgent/CodeAgents/OpenCodeAgent.ts index d37e0fe8af..c4a4e6443a 100644 --- a/AIAgent/CodeAgents/OpenCodeAgent.ts +++ b/AIAgent/CodeAgents/OpenCodeAgent.ts @@ -33,6 +33,10 @@ export default class OpenCodeAgent implements CodeAgent { private currentProcess: ChildProcess | null = null; private aborted: boolean = false; + // Track original opencode.json content for restoration + private originalOpenCodeConfig: string | null = null; + private openCodeConfigPath: string | null = null; + // Default timeout: 30 minutes private static readonly DEFAULT_TIMEOUT_MS: number = 30 * 60 * 1000; @@ -99,6 +103,9 @@ export default class OpenCodeAgent implements CodeAgent { task.workingDirectory, ); + // Restore or delete opencode.json before returning + await this.restoreOpenCodeConfig(); + await this.log( `OpenCode completed. ${modifiedFiles.length} files modified.`, ); @@ -114,6 +121,9 @@ export default class OpenCodeAgent implements CodeAgent { const errorMessage: string = error instanceof Error ? error.message : String(error); + // Restore or delete opencode.json on error + await this.restoreOpenCodeConfig(); + await this.log(`OpenCode execution failed: ${errorMessage}`); logs.push(`Error: ${errorMessage}`); @@ -170,6 +180,17 @@ export default class OpenCodeAgent implements CodeAgent { } const configPath: string = path.join(workingDirectory, "opencode.json"); + this.openCodeConfigPath = configPath; + + // Check if opencode.json already exists and backup its content + try { + const existingContent: string = await LocalFile.read(configPath); + this.originalOpenCodeConfig = existingContent; + await this.log("Backed up existing opencode.json from repository"); + } catch { + // File doesn't exist, which is the normal case + this.originalOpenCodeConfig = null; + } const openCodeConfig: OpenCodeConfig = { model: this.getModelString(), @@ -188,6 +209,35 @@ export default class OpenCodeAgent implements CodeAgent { await this.log(`Created OpenCode config at ${configPath}`); } + // Restore or delete opencode.json after execution + private async restoreOpenCodeConfig(): Promise { + if (!this.openCodeConfigPath) { + return; + } + + try { + if (this.originalOpenCodeConfig !== null) { + // Restore the original file content + await LocalFile.write( + this.openCodeConfigPath, + this.originalOpenCodeConfig, + ); + await this.log("Restored original opencode.json from repository"); + } else { + // Delete the file we created + await LocalFile.deleteFile(this.openCodeConfigPath); + await this.log("Deleted generated opencode.json config file"); + } + } catch (error) { + // Log but don't throw - cleanup failure shouldn't fail the task + logger.warn(`Failed to restore/delete opencode.json: ${error}`); + } + + // Reset the tracking variables + this.openCodeConfigPath = null; + this.originalOpenCodeConfig = null; + } + // Get the model string in OpenCode format (provider/model) private getModelString(): string { if (!this.config) {