()
| 138 | |
| 139 | // Simple execution function |
| 140 | const executeIteration = async () => { |
| 141 | const loop = activeLoops[agentId]; |
| 142 | if (!loop?.isRunning || loop.isExecuting) return; |
| 143 | |
| 144 | // Check if agent is sleeping |
| 145 | if (loop.sleepUntil && Date.now() < loop.sleepUntil) { |
| 146 | const remainingMs = loop.sleepUntil - Date.now(); |
| 147 | Logger.debug(agentId, `Skipping iteration - agent sleeping for ${Math.round(remainingMs/1000)}s more`); |
| 148 | return; |
| 149 | } |
| 150 | |
| 151 | // Clear sleep state when resuming |
| 152 | if (loop.sleepUntil) { |
| 153 | Logger.debug(agentId, `Agent sleep period ended, resuming execution`); |
| 154 | loop.sleepUntil = null; |
| 155 | } |
| 156 | |
| 157 | loop.isExecuting = true; |
| 158 | |
| 159 | try { |
| 160 | await executeAgentIteration(agentId); |
| 161 | |
| 162 | } catch (error) { |
| 163 | // Any error stops the agent cleanly |
| 164 | const errorMessage = error instanceof Error ? error.message : String(error); |
| 165 | Logger.error(agentId, `Agent stopped due to error: ${errorMessage}`, { |
| 166 | error, |
| 167 | logType: 'agent-error', |
| 168 | content: { error: errorMessage } |
| 169 | }); |
| 170 | await stopAgentLoop(agentId); |
| 171 | |
| 172 | } finally { |
| 173 | // Always cleanup execution state |
| 174 | if (activeLoops[agentId]) { |
| 175 | activeLoops[agentId].isExecuting = false; |
| 176 | } |
| 177 | } |
| 178 | }; |
| 179 | |
| 180 | // Fixed-rhythm metronome timer |
| 181 | activeLoops[agentId].intervalId = window.setInterval(executeIteration, intervalMs); |
no test coverage detected