| 24 | } |
| 25 | |
| 26 | write(session: SessionState): SessionScriptWriteResult { |
| 27 | let scriptPath: string | undefined; |
| 28 | try { |
| 29 | if (!session.recordSession) return { written: false }; |
| 30 | scriptPath = this.resolveScriptPath(session); |
| 31 | const scriptDir = path.dirname(scriptPath); |
| 32 | if (!fs.existsSync(scriptDir)) fs.mkdirSync(scriptDir, { recursive: true }); |
| 33 | const script = formatSessionScript(session); |
| 34 | fs.writeFileSync(scriptPath, script); |
| 35 | return { written: true, path: scriptPath }; |
| 36 | } catch (error) { |
| 37 | emitDiagnostic({ |
| 38 | level: 'warn', |
| 39 | phase: 'session_script_write_failed', |
| 40 | data: { |
| 41 | session: session.name, |
| 42 | path: scriptPath, |
| 43 | error: error instanceof Error ? error.message : String(error), |
| 44 | }, |
| 45 | }); |
| 46 | return { written: false }; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | private resolveScriptPath(session: SessionState): string { |
| 51 | if (session.saveScriptPath) { |