| 187 | } |
| 188 | |
| 189 | export function saveAgentLessons(params: { |
| 190 | agentId: string |
| 191 | commitId: string |
| 192 | commitSha: string |
| 193 | prompt: string |
| 194 | lessons: Lesson[] |
| 195 | lessonsDir: string |
| 196 | }): void { |
| 197 | const { agentId, commitId, commitSha, prompt, lessons, lessonsDir } = params |
| 198 | |
| 199 | try { |
| 200 | const safeAgentId = agentId.replace(/[^a-zA-Z0-9-]/g, '_') |
| 201 | if (!fs.existsSync(lessonsDir)) { |
| 202 | fs.mkdirSync(lessonsDir, { recursive: true }) |
| 203 | } |
| 204 | const lessonsFile = path.join(lessonsDir, `${safeAgentId}.md`) |
| 205 | |
| 206 | if (!fs.existsSync(lessonsFile)) { |
| 207 | fs.writeFileSync( |
| 208 | lessonsFile, |
| 209 | `# Agent Lessons: ${agentId}\n\nLessons accumulated across buffbench runs. Each lesson identifies what went wrong and what should have been done instead.\n\n`, |
| 210 | 'utf-8', |
| 211 | ) |
| 212 | } |
| 213 | |
| 214 | if (lessons.length > 0) { |
| 215 | const header = `## ${new Date().toISOString()} — ${commitId} (${commitSha.slice(0, 7)})\n` |
| 216 | let content = `\n### Original Agent Prompt\n${prompt}\n` |
| 217 | |
| 218 | content += '\n### Lessons\n' |
| 219 | content += lessons |
| 220 | .map((lesson) => { |
| 221 | return `- **What went wrong:** ${lesson.whatWentWrong}\n **What should have been done:** ${lesson.whatShouldHaveBeenDone}` |
| 222 | }) |
| 223 | .join('\n\n') |
| 224 | content += '\n' |
| 225 | |
| 226 | fs.appendFileSync(lessonsFile, `${header}${content}\n`, 'utf-8') |
| 227 | } |
| 228 | } catch (e) { |
| 229 | const error = e instanceof Error ? e : new Error(String(e)) |
| 230 | console.warn( |
| 231 | `Failed to append agent lessons for ${agentId}:`, |
| 232 | error.message, |
| 233 | ) |
| 234 | } |
| 235 | } |