MCPcopy Index your code
hub / github.com/MiniCodeMonkey/chief / runIteration

Method runIteration

internal/loop/loop.go:319–398  ·  view source on GitHub ↗

runIteration spawns the agent and processes its output.

(ctx context.Context)

Source from the content-addressed store, hash-verified

317
318// runIteration spawns the agent and processes its output.
319func (l *Loop) runIteration(ctx context.Context) error {
320 workDir := l.effectiveWorkDir()
321 cmd := l.provider.LoopCommand(ctx, l.prompt, workDir)
322 l.mu.Lock()
323 l.agentCmd = cmd
324 // Initialize watchdog state
325 l.lastOutputTime = time.Now()
326 watchdogTimeout := l.watchdogTimeout
327 l.mu.Unlock()
328
329 // Create pipes for stdout and stderr
330 stdout, err := l.agentCmd.StdoutPipe()
331 if err != nil {
332 return fmt.Errorf("failed to create stdout pipe: %w", err)
333 }
334
335 stderr, err := l.agentCmd.StderrPipe()
336 if err != nil {
337 return fmt.Errorf("failed to create stderr pipe: %w", err)
338 }
339
340 // Start the command
341 if err := l.agentCmd.Start(); err != nil {
342 return fmt.Errorf("failed to start %s: %w", l.provider.Name(), err)
343 }
344
345 // Start watchdog goroutine to detect hung processes
346 watchdogDone := make(chan struct{})
347 var watchdogFired atomic.Bool
348 if watchdogTimeout > 0 {
349 go l.runWatchdog(watchdogTimeout, watchdogDone, &watchdogFired)
350 }
351
352 // Process stdout in a separate goroutine
353 var wg sync.WaitGroup
354 wg.Add(2)
355
356 go func() {
357 defer wg.Done()
358 l.processOutput(stdout)
359 }()
360
361 // Log stderr to the log file
362 go func() {
363 defer wg.Done()
364 l.logStream(stderr, "[stderr] ")
365 }()
366
367 // Wait for output processing to complete
368 wg.Wait()
369
370 // Stop watchdog
371 close(watchdogDone)
372
373 // Wait for the command to finish
374 if err := l.agentCmd.Wait(); err != nil {
375 // If the context was cancelled, don't treat it as an error
376 if ctx.Err() != nil {

Callers 1

runIterationWithRetryMethod · 0.95

Calls 8

effectiveWorkDirMethod · 0.95
runWatchdogMethod · 0.95
processOutputMethod · 0.95
logStreamMethod · 0.95
LoadMethod · 0.80
LoopCommandMethod · 0.65
NameMethod · 0.65
StartMethod · 0.45

Tested by

no test coverage detected