Elffuss BraidHub

kikocis/clonagent public

main · 1.2 KiBRawPermalink
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.
3const fs = require('fs');
4const path = require('path');
5
6const FILE = path.resolve(__dirname, '..', 'data', 'agent-sessions.json');
7
8function readAll() {
9 try { return JSON.parse(fs.readFileSync(FILE, 'utf8')); }
10 catch { return {}; }
11}
12function writeAll(data) {
13 fs.mkdirSync(path.dirname(FILE), { recursive: true });
14 fs.writeFileSync(FILE, JSON.stringify(data, null, 2));
15}
16
17function get(agentId) {
18 return readAll()[agentId] || {};
19}
20
21function 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
29const activeRoles = new Set(); // 'agentId:role'
30
31function isActive(agentId, role) {
32 return activeRoles.has(`${agentId}:${role}`);
33}
34function setActive(agentId, role, active) {
35 const key = `${agentId}:${role}`;
36 if (active) activeRoles.add(key); else activeRoles.delete(key);
37}
38
39module.exports = { get, set, isActive, setActive };