| 1 | // Append-only log of every agent run/launch |
| 2 | const fs = require('fs'); |
| 3 | const path = require('path'); |
| 4 | |
| 5 | const FILE = path.resolve(__dirname, '..', 'data', 'runs.json'); |
| 6 | const MAX_RUNS = 500; |
| 7 | |
| 8 | function readAll() { |
| 9 | try { return JSON.parse(fs.readFileSync(FILE, 'utf8')); } |
| 10 | catch { return []; } |
| 11 | } |
| 12 | |
| 13 | function recordRun(r) { |
| 14 | const all = readAll(); |
| 15 | all.unshift({ id: Date.now() + '-' + Math.random().toString(36).slice(2, 8), ...r }); |
| 16 | if (all.length > MAX_RUNS) all.length = MAX_RUNS; |
| 17 | fs.mkdirSync(path.dirname(FILE), { recursive: true }); |
| 18 | fs.writeFileSync(FILE, JSON.stringify(all, null, 2)); |
| 19 | } |
| 20 | |
| 21 | function listRuns(agentId) { |
| 22 | const all = readAll(); |
| 23 | return agentId ? all.filter(r => r.agentId === agentId) : all; |
| 24 | } |
| 25 | |
| 26 | module.exports = { recordRun, listRuns }; |