| 1 | // Manages the shared task queue for each agent. |
| 2 | // Lives at ~/.claude/skills/<agentId>/agent-tasks.json so both scheduler |
| 3 | // and executor Claude sessions can read/write it directly via Bash tools. |
| 4 | const fs = require('fs'); |
| 5 | const path = require('path'); |
| 6 | const os = require('os'); |
| 7 | |
| 8 | function filePath(agentId) { |
| 9 | return path.join(os.homedir(), '.claude', 'skills', agentId, 'agent-tasks.json'); |
| 10 | } |
| 11 | |
| 12 | function empty(agentId) { |
| 13 | return { agentId, plan: '', tasks: [], updatedAt: new Date().toISOString() }; |
| 14 | } |
| 15 | |
| 16 | function read(agentId) { |
| 17 | try { return JSON.parse(fs.readFileSync(filePath(agentId), 'utf8')); } |
| 18 | catch { return empty(agentId); } |
| 19 | } |
| 20 | |
| 21 | function write(agentId, data) { |
| 22 | const f = filePath(agentId); |
| 23 | fs.mkdirSync(path.dirname(f), { recursive: true }); |
| 24 | fs.writeFileSync(f, JSON.stringify({ ...data, updatedAt: new Date().toISOString() }, null, 2)); |
| 25 | } |
| 26 | |
| 27 | function hasPending(agentId) { |
| 28 | const { tasks } = read(agentId); |
| 29 | return Array.isArray(tasks) && tasks.some(t => t.status === 'pending'); |
| 30 | } |
| 31 | |
| 32 | module.exports = { filePath, read, write, hasPending }; |