(ledger)
| 61 | } |
| 62 | |
| 63 | function _writeLedger(ledger) { |
| 64 | try { |
| 65 | const dir = getMemoryDir(); |
| 66 | if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true }); |
| 67 | const entries = Object.entries(ledger.spawned || {}); |
| 68 | if (entries.length > LEDGER_MAX_ENTRIES) { |
| 69 | ledger.spawned = Object.fromEntries(entries.slice(-LEDGER_MAX_ENTRIES)); |
| 70 | } |
| 71 | const tmp = _ledgerPath() + '.tmp'; |
| 72 | fs.writeFileSync(tmp, JSON.stringify(ledger, null, 2)); |
| 73 | fs.renameSync(tmp, _ledgerPath()); |
| 74 | } catch (_) { |
| 75 | // Non-fatal: next tick will re-read Hub state. Stale ledger at worst |
| 76 | // causes a duplicate spawn, which the Hub will 409 on already-completed |
| 77 | // tasks without side effects. |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | function _isEligible(task) { |
| 82 | if (!task || typeof task !== 'object') return false; |
no test coverage detected