runLoop runs a loop instance and forwards events.
(instance *LoopInstance)
| 256 | |
| 257 | // runLoop runs a loop instance and forwards events. |
| 258 | func (m *Manager) runLoop(instance *LoopInstance) { |
| 259 | defer m.wg.Done() |
| 260 | |
| 261 | // Start event forwarding goroutine |
| 262 | done := make(chan struct{}) |
| 263 | go func() { |
| 264 | for { |
| 265 | select { |
| 266 | case event, ok := <-instance.Loop.Events(): |
| 267 | if !ok { |
| 268 | close(done) |
| 269 | return |
| 270 | } |
| 271 | |
| 272 | instance.mu.Lock() |
| 273 | instance.Iteration = event.Iteration |
| 274 | instance.mu.Unlock() |
| 275 | |
| 276 | // Check if this is a completion event |
| 277 | completed := event.Type == EventComplete |
| 278 | |
| 279 | // Forward event to manager channel |
| 280 | m.events <- ManagerEvent{ |
| 281 | PRDName: instance.Name, |
| 282 | Event: event, |
| 283 | Completed: completed, |
| 284 | } |
| 285 | |
| 286 | // If completed, trigger callbacks |
| 287 | if completed { |
| 288 | m.mu.RLock() |
| 289 | callback := m.onComplete |
| 290 | postCallback := m.onPostComplete |
| 291 | m.mu.RUnlock() |
| 292 | if callback != nil { |
| 293 | callback(instance.Name) |
| 294 | } |
| 295 | if postCallback != nil { |
| 296 | instance.mu.Lock() |
| 297 | branch := instance.Branch |
| 298 | workDir := instance.WorktreeDir |
| 299 | instance.mu.Unlock() |
| 300 | postCallback(instance.Name, branch, workDir) |
| 301 | } |
| 302 | } |
| 303 | case <-instance.ctx.Done(): |
| 304 | close(done) |
| 305 | return |
| 306 | } |
| 307 | } |
| 308 | }() |
| 309 | |
| 310 | // Run the loop |
| 311 | err := instance.Loop.Run(instance.ctx) |
| 312 | |
| 313 | // Update state based on result |
| 314 | instance.mu.Lock() |
| 315 | if err != nil && err != context.Canceled { |