| 12 | |
| 13 | /** Attempts every cleanup step while retaining the original execution failure. */ |
| 14 | export async function cleanupTrial(actions: CleanupActions, primary: unknown, journalPath: string): Promise<void> { |
| 15 | const failures: string[] = []; |
| 16 | let resourceFailure = false; |
| 17 | for (const [step, label] of [ |
| 18 | ["actor", "Second actor execution"], |
| 19 | ["agent", "Copilot executor shutdown"], |
| 20 | ["observer", "Command observer cleanup"], |
| 21 | ["resources", "Resource cleanup"], |
| 22 | ["local", "Local cleanup"], |
| 23 | ] as const) { |
| 24 | try { await actions[step](); } |
| 25 | catch (error) { |
| 26 | if (step === "actor" && primary !== undefined && error === primary) continue; |
| 27 | failures.push(`${label}: ${String(error)}`); |
| 28 | if (step === "resources") resourceFailure = true; |
| 29 | } |
| 30 | } |
| 31 | if (failures.length) { |
| 32 | const original = primary === undefined ? "" : `${primary instanceof Error ? primary.message : String(primary)}; `; |
| 33 | const retry = resourceFailure ? `. Retry with: evals/cleanup ${journalPath}` : ""; |
| 34 | throw new TrialCleanupError(`${original}${failures.join("; ")}${retry}`, { cause: primary }); |
| 35 | } |
| 36 | } |