| 1 | // Persists the two named Claude sessions per agent: 'scheduler' and 'executor'. |
| 2 | // Never more than one of each. Stored in data/agent-sessions.json. |
| 3 | const fs = require('fs'); |
| 4 | const path = require('path'); |
| 5 | |
| 6 | const FILE = path.resolve(__dirname, '..', 'data', 'agent-sessions.json'); |
| 7 | |
| 8 | function readAll() { |
| 9 | try { return JSON.parse(fs.readFileSync(FILE, 'utf8')); } |
| 10 | catch { return {}; } |
| 11 | } |
| 12 | function writeAll(data) { |
| 13 | fs.mkdirSync(path.dirname(FILE), { recursive: true }); |
| 14 | fs.writeFileSync(FILE, JSON.stringify(data, null, 2)); |
| 15 | } |
| 16 | |
| 17 | function get(agentId) { |
| 18 | return readAll()[agentId] || {}; |
| 19 | } |
| 20 | |
| 21 | function set(agentId, updates) { |
| 22 | const all = readAll(); |
| 23 | all[agentId] = { ...(all[agentId] || {}), ...updates, updatedAt: new Date().toISOString() }; |
| 24 | writeAll(all); |
| 25 | return all[agentId]; |
| 26 | } |
| 27 | |
| 28 | // Mark role active/idle so executor doesn't double-launch |
| 29 | const activeRoles = new Set(); // 'agentId:role' |
| 30 | |
| 31 | function isActive(agentId, role) { |
| 32 | return activeRoles.has(`${agentId}:${role}`); |
| 33 | } |
| 34 | function setActive(agentId, role, active) { |
| 35 | const key = `${agentId}:${role}`; |
| 36 | if (active) activeRoles.add(key); else activeRoles.delete(key); |
| 37 | } |
| 38 | |
| 39 | module.exports = { get, set, isActive, setActive }; |