|
| 1 | +import { EventStore } from "../history/event-store.js"; |
| 2 | +import { randomUUID } from "crypto"; |
| 3 | +import { RuntimeLogger } from "./logger.js"; |
| 4 | +import { CommandCenterDashboard } from "./dashboard.js"; |
| 5 | +import { AutoPilotStatus } from "./autopilot-status.js"; |
| 6 | + |
| 7 | +export class ExecutionOrchestrator { |
| 8 | + constructor( |
| 9 | + private eventStore: EventStore, |
| 10 | + private requestModelText: (taskName: string, userPrompt: string, maxTokens: number) => Promise<string | null> |
| 11 | + ) {} |
| 12 | + |
| 13 | + async healAndRetry(executionId: string, lessonId: string): Promise<boolean> { |
| 14 | + const db = (this.eventStore as any).db; |
| 15 | + |
| 16 | + // Fetch execution and lesson |
| 17 | + const execution = this.eventStore.getExecutionByPromptId( |
| 18 | + db.prepare("SELECT prompt_id FROM executions WHERE id = ?").get(executionId)?.prompt_id || "" |
| 19 | + ); |
| 20 | + if (!execution) { |
| 21 | + RuntimeLogger.warn(`Execution ${executionId} not found for healing.`); |
| 22 | + return false; |
| 23 | + } |
| 24 | + |
| 25 | + const lesson = db.prepare("SELECT * FROM lessons WHERE id = ?").get(lessonId); |
| 26 | + if (!lesson) { |
| 27 | + RuntimeLogger.warn(`Lesson ${lessonId} not found for healing.`); |
| 28 | + return false; |
| 29 | + } |
| 30 | + |
| 31 | + // Fetch the original prompt |
| 32 | + const prompt = db.prepare("SELECT * FROM prompts WHERE id = ?").get(execution.prompt_id); |
| 33 | + if (!prompt) { |
| 34 | + RuntimeLogger.warn(`Original prompt not found for execution ${executionId}.`); |
| 35 | + return false; |
| 36 | + } |
| 37 | + |
| 38 | + const retryCount = db.prepare(`SELECT COUNT(*) as count FROM prompts WHERE intent = 'self-heal' AND raw_prompt LIKE ?`).get(`%[HEALING: ${executionId}]%`)?.count || 0; |
| 39 | + |
| 40 | + const MAX_RETRIES = 2; |
| 41 | + if (retryCount >= MAX_RETRIES) { |
| 42 | + RuntimeLogger.warn(`Max retries (${MAX_RETRIES}) reached for execution ${executionId}.`); |
| 43 | + return false; |
| 44 | + } |
| 45 | + |
| 46 | + CommandCenterDashboard.log(`[Auto-Heal] Retrying execution ${executionId} using lesson: ${lesson.title}`); |
| 47 | + |
| 48 | + // Create the healed prompt structure |
| 49 | + const healedPromptContent = `[HEALING: ${executionId}]\nThe previous execution of this prompt failed. |
| 50 | + |
| 51 | +Original Request: |
| 52 | +${prompt.raw_prompt} |
| 53 | +
|
| 54 | +Extracted Lesson to Apply: |
| 55 | +Title: ${lesson.title} |
| 56 | +Rule: ${lesson.summary} |
| 57 | +
|
| 58 | +Please re-execute the original request while strictly adhering to the lesson above to avoid the previous failure.`; |
| 59 | + |
| 60 | + const newPromptId = `prm_heal_${randomUUID()}`; |
| 61 | + |
| 62 | + this.eventStore.recordPrompt({ |
| 63 | + id: newPromptId, |
| 64 | + client: "Auto-Heal", |
| 65 | + agent_name: "ExecutionOrchestrator", |
| 66 | + raw_prompt: healedPromptContent, |
| 67 | + intent: "self-heal", |
| 68 | + repo_id: prompt.repo_id, |
| 69 | + }); |
| 70 | + |
| 71 | + const newExecId = `exec_heal_${randomUUID()}`; |
| 72 | + const now = new Date().toISOString(); |
| 73 | + |
| 74 | + this.eventStore.recordExecution({ |
| 75 | + id: newExecId, |
| 76 | + prompt_id: newPromptId, |
| 77 | + workflow_name: "self-healing", |
| 78 | + executor_name: "ExecutionOrchestrator", |
| 79 | + status: "running", |
| 80 | + started_at: now, |
| 81 | + }); |
| 82 | + |
| 83 | + try { |
| 84 | + const responseText = await this.requestModelText( |
| 85 | + "self_heal", |
| 86 | + healedPromptContent, |
| 87 | + 4000 |
| 88 | + ); |
| 89 | + |
| 90 | + if (!responseText) { |
| 91 | + throw new Error("Semantic provider returned null response."); |
| 92 | + } |
| 93 | + |
| 94 | + this.eventStore.updateExecution({ |
| 95 | + id: newExecId, |
| 96 | + status: "completed", |
| 97 | + ended_at: new Date().toISOString(), |
| 98 | + result_summary: `Healed execution succeeded.`, |
| 99 | + artifacts_json: JSON.stringify({ healedResponse: responseText }) |
| 100 | + }); |
| 101 | + |
| 102 | + AutoPilotStatus.record(`Healed execution ${executionId} successfully.`, "cycle_complete"); |
| 103 | + CommandCenterDashboard.log(`[Auto-Heal] Healed execution ${newExecId} succeeded.`); |
| 104 | + return true; |
| 105 | + |
| 106 | + } catch (error) { |
| 107 | + const errorMessage = error instanceof Error ? error.message : String(error); |
| 108 | + this.eventStore.updateExecution({ |
| 109 | + id: newExecId, |
| 110 | + status: "failed", |
| 111 | + ended_at: new Date().toISOString(), |
| 112 | + result_summary: `Healed execution failed: ${errorMessage}`, |
| 113 | + }); |
| 114 | + AutoPilotStatus.record(`Healed execution ${executionId} failed: ${errorMessage}`, "error"); |
| 115 | + CommandCenterDashboard.log(`[Auto-Heal] Healed execution ${newExecId} failed again.`); |
| 116 | + return false; |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments