* Fetch a pickup action if one is due. Idempotent -- safe to call from the * main loop every cycle. * * @param {object} [opts] * @param {number} [opts.limit=5] -- how many Hub tasks to consider per call * @param {string} [opts.evolverExec] -- how the wrapper should invoke Evolver * @returns {P
(opts)
| 167 | * we picked so the caller can log it. |
| 168 | */ |
| 169 | async function pickOne(opts) { |
| 170 | if (!_isEnabled()) return null; |
| 171 | const limit = Math.max(1, Math.min(20, Number(opts && opts.limit) || 5)); |
| 172 | |
| 173 | let listResult; |
| 174 | try { |
| 175 | listResult = await hubClient.listMyTasks(limit); |
| 176 | } catch (_) { |
| 177 | return null; |
| 178 | } |
| 179 | if (!listResult || !listResult.ok) return null; |
| 180 | |
| 181 | const tasks = (listResult.data && Array.isArray(listResult.data.tasks)) |
| 182 | ? listResult.data.tasks |
| 183 | : (Array.isArray(listResult.data) ? listResult.data : []); |
| 184 | if (!tasks.length) return null; |
| 185 | |
| 186 | const ledger = _readLedger(); |
| 187 | let picked = null; |
| 188 | for (const t of tasks) { |
| 189 | if (!_isEligible(t)) continue; |
| 190 | if (_recentlySpawned(ledger, t.id)) continue; |
| 191 | picked = t; |
| 192 | break; |
| 193 | } |
| 194 | if (!picked) return null; |
| 195 | |
| 196 | const spawnTask = _buildSpawnTask(picked, opts); |
| 197 | const spawnCall = renderSessionsSpawnCall({ |
| 198 | task: spawnTask, |
| 199 | agentId: 'atp_pickup', |
| 200 | cleanup: 'delete', |
| 201 | label: 'atp_pickup_' + String(picked.id).slice(0, 32), |
| 202 | }); |
| 203 | |
| 204 | ledger.spawned = ledger.spawned || {}; |
| 205 | ledger.spawned[picked.id] = { at: Date.now(), order_id: picked.atp_order_id }; |
| 206 | _writeLedger(ledger); |
| 207 | |
| 208 | return { spawnCall, task: picked }; |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Forget a previously-spawned task so the main loop will retry it next cycle. |
nothing calls this directly
no test coverage detected