Repository navigation
Expand file tree
/
Copy pathLeadAgent.cs
More file actions
1116 lines (968 loc) · 42.9 KB
/
Copy pathLeadAgent.cs
File metadata and controls
1116 lines (968 loc) · 42.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using RalphController.Messaging;
using RalphController.Models;
using RalphController.Git;
using RalphController.Merge;
using RalphController.Parallel;
using System.Collections.Concurrent;
using System.Text;
using System.Text.RegularExpressions;
namespace RalphController;
/// <summary>
/// AI-driven Lead Agent (Tier 1 in lead-driven mode).
/// Decision loop: build prompt with project state → run AI CLI → parse LeadDecision → execute.
/// Creates ephemeral TaskAgents (up to AgentCount concurrently), evaluates results, merges worktrees.
/// </summary>
public class LeadAgent : IDisposable
{
private readonly RalphConfig _config;
private readonly TeamConfig _teamConfig;
private readonly TaskStore _taskStore;
private readonly GitWorktreeManager _gitManager;
private readonly MergeManager _mergeManager;
private readonly ModelSpec? _leadModel;
private readonly SemaphoreSlim _mergeLock = new(1, 1);
private bool _disposed;
private int _consecutiveParseFailures;
private const int MaxParseFailures = 3;
// Track running TaskAgents and their background tasks
private readonly ConcurrentDictionary<string, RunningTaskAgent> _runningAgents = new();
private int _agentModelIndex;
// Backoff: if agents fail very quickly, throttle spawning to prevent churn
private DateTime _lastAgentFailure = DateTime.MinValue;
private static readonly TimeSpan FailureBackoff = TimeSpan.FromSeconds(3);
// Stale agent detection: wake up periodically to check for stuck agents
private static readonly TimeSpan StaleCheckInterval = TimeSpan.FromSeconds(60);
// Merge timeouts: prevent merges from blocking the lead forever
private static readonly TimeSpan MergeTimeout = TimeSpan.FromMinutes(20);
private static readonly TimeSpan MergeLockTimeout = TimeSpan.FromMinutes(2);
// User messaging support
private readonly MessageBus? _messageBus;
private readonly List<Message> _pendingUserMessages = new();
private readonly SemaphoreSlim _messageSignal = new(0);
/// <summary>Current state of the lead agent</summary>
public AgentState State { get; private set; } = AgentState.Idle;
/// <summary>Statistics for TUI display</summary>
public AgentStatistics Statistics { get; }
/// <summary>Max concurrent TaskAgents (from AgentCount)</summary>
public int MaxConcurrent => _teamConfig.AgentCount;
/// <summary>Number of currently running TaskAgents</summary>
public int RunningCount => _runningAgents.Count;
// Events
public event Action<string>? OnOutput;
public event Action<string>? OnError;
public event Action<LeadDecision>? OnDecision;
public event Action<TaskAgent>? OnTaskAgentCreated;
public event Action<TaskAgent>? OnTaskAgentDestroyed;
public event Action<TaskStoreStatistics>? OnQueueUpdate;
public event Action<AgentStatistics>? OnUpdate;
public LeadAgent(
RalphConfig config,
TeamConfig teamConfig,
TaskStore taskStore,
GitWorktreeManager gitManager,
MergeManager mergeManager,
MessageBus? messageBus = null)
{
_config = config;
_teamConfig = teamConfig;
_taskStore = taskStore;
_gitManager = gitManager;
_mergeManager = mergeManager;
_messageBus = messageBus;
_leadModel = teamConfig.LeadModel;
var modelLabel = _leadModel?.DisplayName ?? config.Provider.ToString();
Statistics = new AgentStatistics
{
AgentId = "lead",
Name = $"Lead [{modelLabel}]",
State = AgentState.Spawning,
AssignedModel = _leadModel
};
}
/// <summary>
/// Wake the lead from idle when a user message arrives.
/// </summary>
public void NotifyMessageAvailable()
{
// Release the semaphore so WaitForAnyAgentCompletionAsync wakes up
_messageSignal.Release();
}
/// <summary>
/// Poll the message bus for new user messages and queue them.
/// </summary>
private void ProcessPendingMessages()
{
if (_messageBus == null) return;
var messages = _messageBus.Poll();
foreach (var msg in messages)
{
if (msg.Type == MessageType.Text)
{
_pendingUserMessages.Add(msg);
OnOutput?.Invoke($"User message received: {msg.Content}");
}
}
}
/// <summary>
/// Main decision loop. Runs until all tasks are done or cancelled.
/// Launches up to AgentCount TaskAgents concurrently.
/// </summary>
public async Task RunAsync(CancellationToken cancellationToken = default)
{
SetState(AgentState.Ready);
OnOutput?.Invoke($"Lead agent starting decision loop (max {MaxConcurrent} concurrent agents)...");
try
{
while (!cancellationToken.IsCancellationRequested)
{
try
{
// Reap any completed agents first
await ReapCompletedAgentsAsync(cancellationToken);
// Check for new user messages
ProcessPendingMessages();
// Backoff after rapid failures to prevent agent churn
var timeSinceFailure = DateTime.UtcNow - _lastAgentFailure;
if (timeSinceFailure < FailureBackoff)
{
var waitTime = FailureBackoff - timeSinceFailure;
OnOutput?.Invoke($"Backoff: waiting {waitTime.TotalSeconds:F0}s after agent failure...");
await Task.Delay(waitTime, cancellationToken);
}
var stats = _taskStore.GetStatistics();
OnQueueUpdate?.Invoke(stats);
// Check if we're done: no pending tasks, nothing running, no failures, no user messages
if (stats.Pending == 0 && stats.InProgress == 0 && stats.Failed == 0
&& _runningAgents.IsEmpty && _pendingUserMessages.Count == 0)
{
OnOutput?.Invoke("All tasks resolved. Declaring complete.");
break;
}
// Priority: if user messages are pending, force an AI decision cycle
// so the lead can act on them (add tasks, re-read plan, etc.)
if (_pendingUserMessages.Count > 0)
{
SetState(AgentState.Deciding);
OnOutput?.Invoke($"Processing {_pendingUserMessages.Count} user message(s)...");
var userDecision = await GetNextDecisionAsync(cancellationToken);
_pendingUserMessages.Clear();
if (userDecision != null)
{
OnDecision?.Invoke(userDecision);
OnOutput?.Invoke($"Decision: {userDecision.Action} {userDecision.TaskId ?? ""} — {userDecision.Reason ?? ""}");
await ExecuteDecisionAsync(userDecision, cancellationToken);
}
continue;
}
// If all slots full or no claimable tasks, wait for an agent to finish
if (_runningAgents.Count >= MaxConcurrent || (stats.Pending == 0 && _runningAgents.Count > 0))
{
SetState(AgentState.Idle);
var runningIds = string.Join(", ", _runningAgents.Keys);
OnOutput?.Invoke($"All {_runningAgents.Count} agent slots active ({runningIds}), waiting for completion...");
await WaitForAnyAgentCompletionAsync(cancellationToken);
continue;
}
// Fast-path: if there are claimable tasks and free slots, skip the AI
// entirely and just pick the highest-priority pending task.
// Only consult the AI for complex decisions (retries, skips, all tasks done).
var claimable = _taskStore.GetClaimable();
LeadDecision? decision;
if (claimable.Count > 0)
{
var next = claimable.OrderBy(t => t.Priority).First();
decision = new LeadDecision
{
Action = LeadAction.NextTask,
TaskId = next.TaskId,
Reason = $"Next highest-priority pending task"
};
OnOutput?.Invoke($"Fast-assigning task {next.TaskId}: {next.Title ?? next.Description}");
_consecutiveParseFailures = 0;
}
else if (stats.Failed > 0)
{
// Complex decision needed (retries, skips) — ask the AI
SetState(AgentState.Deciding);
OnOutput?.Invoke($"Analyzing project state ({stats.Pending} pending, {stats.Completed} done, {stats.Failed} failed, {_runningAgents.Count} running)...");
decision = await GetNextDecisionAsync(cancellationToken);
if (decision == null)
{
_consecutiveParseFailures++;
if (_consecutiveParseFailures >= MaxParseFailures)
{
OnOutput?.Invoke($"Failed to parse decision {MaxParseFailures}x, falling back to sequential");
decision = CreateFallbackDecision();
}
else
{
OnError?.Invoke("Failed to parse lead decision, retrying...");
continue;
}
}
else
{
_consecutiveParseFailures = 0;
}
}
else
{
// No claimable, no failed — agents still running, wait
if (_runningAgents.Count > 0)
{
OnOutput?.Invoke($"No pending tasks, waiting for {_runningAgents.Count} running agent(s)...");
await WaitForAnyAgentCompletionAsync(cancellationToken);
continue;
}
// Nothing left — declare complete
decision = new LeadDecision
{
Action = LeadAction.DeclareComplete,
Reason = "All tasks resolved"
};
}
if (decision == null)
{
// No claimable tasks but agents still running — wait
if (_runningAgents.Count > 0)
{
await WaitForAnyAgentCompletionAsync(cancellationToken);
}
continue;
}
OnDecision?.Invoke(decision);
OnOutput?.Invoke($"Decision: {decision.Action} {decision.TaskId ?? ""} — {decision.Reason ?? ""}");
await ExecuteDecisionAsync(decision, cancellationToken);
}
catch (OperationCanceledException)
{
throw; // Propagate cancellation
}
catch (Exception ex)
{
// Log the error but keep the decision loop alive
OnError?.Invoke($"[Lead] Error in decision loop: {ex.Message}");
OnOutput?.Invoke($"Lead recovering from error: {ex.Message}");
_pendingUserMessages.Clear(); // Clear messages that may have caused the error
await Task.Delay(2000, cancellationToken); // Brief pause before retrying
}
}
// Wait for all remaining agents to finish
if (!_runningAgents.IsEmpty)
{
OnOutput?.Invoke($"Waiting for {_runningAgents.Count} running agent(s) to finish...");
await WaitForAllAgentsAsync(cancellationToken);
}
}
catch (OperationCanceledException)
{
OnOutput?.Invoke("Lead agent cancelled");
}
finally
{
// Clean up any remaining agents
foreach (var running in _runningAgents.Values)
{
running.AgentCts?.Cancel();
running.AgentCts?.Dispose();
running.TaskAgent.Dispose();
}
_runningAgents.Clear();
SetState(AgentState.Stopped);
}
}
// --- Agent lifecycle ---
/// <summary>
/// Check for and process completed agents (merge, cleanup, report).
/// </summary>
private async Task ReapCompletedAgentsAsync(CancellationToken ct)
{
var completedIds = _runningAgents
.Where(kvp => kvp.Value.BackgroundTask.IsCompleted)
.Select(kvp => kvp.Key)
.ToList();
foreach (var id in completedIds)
{
if (!_runningAgents.TryRemove(id, out var running))
continue;
try
{
// Get the result (task is already completed, this won't block)
var result = await running.BackgroundTask;
await HandleTaskAgentCompletion(running.TaskAgent, running.TaskId, result, ct);
// Track fast failures for backoff
if (!result.Success)
_lastAgentFailure = DateTime.UtcNow;
}
catch (Exception ex)
{
OnError?.Invoke($"Error reaping agent {id}: {ex.Message}");
_taskStore.Fail(running.TaskId, ex.Message);
_lastAgentFailure = DateTime.UtcNow;
}
finally
{
OnTaskAgentDestroyed?.Invoke(running.TaskAgent);
running.TaskAgent.Dispose();
running.AgentCts?.Dispose();
OnQueueUpdate?.Invoke(_taskStore.GetStatistics());
}
}
}
/// <summary>
/// Detect and force-reap agents that have been inactive (no AI output) longer
/// than AgentInactivityTimeoutMinutes. This handles the case where an AI process
/// exits but grandchild processes keep the stdout pipe open, preventing the
/// background Task from completing.
/// </summary>
private async Task ForceReapStaleAgentsAsync(CancellationToken ct)
{
var timeoutMinutes = _teamConfig.AgentInactivityTimeoutMinutes;
if (timeoutMinutes <= 0) return; // Disabled
var threshold = TimeSpan.FromMinutes(timeoutMinutes);
var now = DateTime.UtcNow;
var staleIds = _runningAgents
.Where(kvp =>
{
// Only consider agents whose background task is NOT completed —
// completed ones will be reaped normally by ReapCompletedAgentsAsync.
if (kvp.Value.BackgroundTask.IsCompleted) return false;
var lastActivity = kvp.Value.TaskAgent.Statistics.LastActivityAt;
var inactiveDuration = now - lastActivity;
return inactiveDuration > threshold;
})
.Select(kvp => kvp.Key)
.ToList();
foreach (var id in staleIds)
{
if (!_runningAgents.TryRemove(id, out var running))
continue;
var inactiveFor = now - running.TaskAgent.Statistics.LastActivityAt;
OnError?.Invoke($"Agent {running.TaskAgent.AgentId} has been inactive for {inactiveFor.TotalMinutes:F0} minutes — force-reaping as failed");
// Cancel the agent's per-task CTS if it exists, to unblock any stuck I/O
running.AgentCts?.Cancel();
// Give the task a moment to respond to cancellation
try
{
await Task.WhenAny(running.BackgroundTask, Task.Delay(5000, ct));
}
catch { /* ignore — we're force-reaping */ }
_taskStore.Fail(running.TaskId, $"Agent inactive for {inactiveFor.TotalMinutes:F0} minutes (timeout: {timeoutMinutes}m)");
_lastAgentFailure = DateTime.UtcNow;
OnTaskAgentDestroyed?.Invoke(running.TaskAgent);
running.TaskAgent.Dispose();
running.AgentCts?.Dispose();
OnQueueUpdate?.Invoke(_taskStore.GetStatistics());
Statistics.TasksFailed++;
}
}
/// <summary>
/// Wait until at least one running agent completes, a user message arrives,
/// or the stale-check interval elapses. Periodic wake-ups ensure the lead
/// can detect and force-reap stuck agents even when no tasks complete.
/// </summary>
private async Task WaitForAnyAgentCompletionAsync(CancellationToken ct)
{
if (_runningAgents.IsEmpty) return;
var agentTasks = _runningAgents.Values.Select(r => (Task)r.BackgroundTask).ToList();
// Also wait for a user message signal so we wake up to handle it
agentTasks.Add(_messageSignal.WaitAsync(ct));
// Periodic wake-up to check for stale agents
agentTasks.Add(Task.Delay(StaleCheckInterval, ct));
await Task.WhenAny(agentTasks).WaitAsync(ct);
// Force-reap any agents that have been inactive too long
await ForceReapStaleAgentsAsync(ct);
// Now reap the completed ones
await ReapCompletedAgentsAsync(ct);
}
/// <summary>
/// Wait for all running agents to complete.
/// </summary>
private async Task WaitForAllAgentsAsync(CancellationToken ct)
{
while (!_runningAgents.IsEmpty)
{
await WaitForAnyAgentCompletionAsync(ct);
}
}
/// <summary>
/// Handle a completed TaskAgent: evaluate, merge, report.
/// </summary>
private async Task HandleTaskAgentCompletion(
TaskAgent taskAgent, string taskId, TaskAgentResult result, CancellationToken ct)
{
OnOutput?.Invoke($"[{taskAgent.AgentId}] Completed: {result.Summary}");
if (result.Success)
{
// Merge worktree branch back (serialize merges to avoid conflicts)
OnOutput?.Invoke($"[{taskAgent.AgentId}] Merging branch {taskAgent.BranchName}...");
// Show both lead and task agent as merging in TUI
SetState(AgentState.MergingWork);
taskAgent.Statistics.State = AgentState.MergingWork;
OnUpdate?.Invoke(taskAgent.Statistics);
// Apply a timeout to the entire merge operation (lock acquisition + merge + AI fix).
// Without this, a hung git process or merge-fix agent can block all future merges.
using var mergeCts = CancellationTokenSource.CreateLinkedTokenSource(ct);
mergeCts.CancelAfter(MergeTimeout);
var acquired = await _mergeLock.WaitAsync(MergeLockTimeout, mergeCts.Token);
if (!acquired)
{
OnError?.Invoke($"Merge lock timeout for {taskId} — another merge may be stuck");
_taskStore.Fail(taskId, "Merge lock timeout");
Statistics.TasksFailed++;
SetState(AgentState.Idle);
}
else
{
try
{
var mergeResult = await MergeBranchAsync(taskAgent, mergeCts.Token);
if (mergeResult.Success)
{
_taskStore.Complete(taskId, new TaskResult(
true,
result.Summary,
result.AllFilesModified,
result.Code?.Output ?? "",
Statistics.TotalDuration));
Statistics.TasksCompleted++;
OnOutput?.Invoke($"Task {taskId} completed and merged successfully");
}
else
{
// MergeBranchAsync already tried AI merge-fix in the worktree
_taskStore.Fail(taskId, $"Merge failed: {mergeResult.Error}");
Statistics.TasksFailed++;
OnError?.Invoke($"Merge failed for {taskId}: {mergeResult.Error}");
}
}
catch (OperationCanceledException) when (!ct.IsCancellationRequested)
{
OnError?.Invoke($"Merge timed out for {taskId} after {MergeTimeout.TotalMinutes:F0} minutes");
_taskStore.Fail(taskId, $"Merge timed out after {MergeTimeout.TotalMinutes:F0} minutes");
Statistics.TasksFailed++;
}
finally
{
_mergeLock.Release();
SetState(AgentState.Idle);
}
}
}
else
{
_taskStore.Fail(taskId, result.Summary);
Statistics.TasksFailed++;
OnError?.Invoke($"Task {taskId} failed: {result.Summary}");
}
// Cleanup worktree
await taskAgent.CleanupAsync();
OnUpdate?.Invoke(Statistics);
}
// --- Decision making ---
private async Task<LeadDecision?> GetNextDecisionAsync(CancellationToken ct)
{
OnOutput?.Invoke("Running AI for decision on failed tasks...");
var prompt = BuildDecisionPrompt();
var providerConfig = GetProviderConfig();
using var timeoutCts = CancellationTokenSource.CreateLinkedTokenSource(ct);
timeoutCts.CancelAfter(TimeSpan.FromSeconds(_teamConfig.LeadDecisionTimeoutSeconds));
AgentProcessResult result;
try
{
result = await AIProcessRunner.RunAsync(
providerConfig,
prompt,
_config.TargetDirectory,
output =>
{
Statistics.LastActivityAt = DateTime.UtcNow;
OnOutput?.Invoke(output);
},
timeoutCts.Token);
}
catch (OperationCanceledException) when (!ct.IsCancellationRequested)
{
// Timeout — not the outer cancel. Don't count as parse failure.
OnError?.Invoke($"Lead AI timed out after {_teamConfig.LeadDecisionTimeoutSeconds}s, using fallback");
return CreateFallbackDecision();
}
Statistics.OutputChars += result.OutputChars;
Statistics.AITime += result.Duration;
Statistics.Iterations++;
Statistics.LastActivityAt = DateTime.UtcNow;
OnUpdate?.Invoke(Statistics);
if (!result.Success)
{
OnError?.Invoke($"Lead AI process failed: {result.Error}");
return null;
}
var text = result.ParsedText.Length > 0 ? result.ParsedText : result.Output;
return ParseDecision(text);
}
private LeadDecision? CreateFallbackDecision()
{
var claimable = _taskStore.GetClaimable();
if (claimable.Count == 0)
{
var stats = _taskStore.GetStatistics();
if (stats.Pending == 0 && stats.InProgress == 0 && stats.Failed == 0 && _runningAgents.IsEmpty)
{
return new LeadDecision
{
Action = LeadAction.DeclareComplete,
Reason = "All tasks resolved (fallback)"
};
}
// If there are failed tasks, return null so the AI decision path handles them
if (stats.Failed > 0 && _runningAgents.IsEmpty)
{
return null;
}
return null; // Agents still running, wait
}
var next = claimable
.OrderBy(t => t.Priority)
.First();
return new LeadDecision
{
Action = LeadAction.NextTask,
TaskId = next.TaskId,
Reason = "Sequential fallback (AI parse failures)"
};
}
// --- Decision execution ---
private async Task ExecuteDecisionAsync(LeadDecision decision, CancellationToken ct)
{
switch (decision.Action)
{
case LeadAction.NextTask:
LaunchTaskAgent(decision.TaskId!, ct);
break;
case LeadAction.RetryTask:
RetryAndLaunchTaskAgent(decision.TaskId!, ct);
break;
case LeadAction.AddTask:
ExecuteAddTask(decision);
break;
case LeadAction.SkipTask:
ExecuteSkipTask(decision.TaskId!);
break;
case LeadAction.DeclareComplete:
OnOutput?.Invoke("Lead declares all work complete.");
break;
}
// Small yield to let background agents make progress
await Task.Yield();
}
/// <summary>
/// Launch a TaskAgent in the background. Does NOT await completion.
/// The agent runs its 3-phase cycle while the lead continues deciding.
/// </summary>
private void LaunchTaskAgent(string taskId, CancellationToken ct)
{
var task = _taskStore.GetById(taskId);
if (task == null)
{
OnError?.Invoke($"Task {taskId} not found");
return;
}
OnOutput?.Invoke($"Launching agent for task {taskId}: {task.Title ?? task.Description}");
// Claim the task
_taskStore.TryClaimTask(taskId, "lead");
OnQueueUpdate?.Invoke(_taskStore.GetStatistics());
// Create TaskAgent with round-robin model assignment
var model = _teamConfig.GetAgentModel(_agentModelIndex);
_agentModelIndex = (_agentModelIndex + 1) % Math.Max(1, _teamConfig.AgentCount);
var taskAgent = new TaskAgent(
_config,
_teamConfig,
task,
_gitManager,
model);
WireTaskAgentEvents(taskAgent);
OnTaskAgentCreated?.Invoke(taskAgent);
// Per-agent CTS: linked to the main CT, plus an overall hard timeout.
// This ensures agents can't run forever even if their process is stuck.
var agentCts = CancellationTokenSource.CreateLinkedTokenSource(ct);
var hardTimeoutMinutes = _teamConfig.AgentInactivityTimeoutMinutes * 2; // 2x the inactivity timeout
if (hardTimeoutMinutes > 0)
{
agentCts.CancelAfter(TimeSpan.FromMinutes(hardTimeoutMinutes));
}
var agentCt = agentCts.Token;
// Launch in background
var backgroundTask = Task.Run(async () =>
{
try
{
var initialized = await taskAgent.InitializeAsync(agentCt);
if (!initialized)
{
OnError?.Invoke($"Failed to initialize TaskAgent for {taskId}");
return new TaskAgentResult
{
Success = false,
BranchName = taskAgent.BranchName,
Summary = "Worktree initialization failed"
};
}
return await taskAgent.RunAsync(agentCt);
}
catch (OperationCanceledException) when (!ct.IsCancellationRequested)
{
// Per-agent timeout, not the main cancellation
return new TaskAgentResult
{
Success = false,
BranchName = taskAgent.BranchName,
Summary = $"Agent timed out after {hardTimeoutMinutes} minutes"
};
}
}, ct);
_runningAgents[taskId] = new RunningTaskAgent(taskAgent, taskId, backgroundTask, agentCts);
OnOutput?.Invoke($"Agent {taskAgent.AgentId} running ({_runningAgents.Count}/{MaxConcurrent} slots used)");
}
private void RetryAndLaunchTaskAgent(string taskId, CancellationToken ct)
{
var task = _taskStore.GetById(taskId);
if (task == null)
{
OnError?.Invoke($"Task {taskId} not found for retry");
return;
}
if (task.RetryCount >= task.MaxRetries)
{
OnOutput?.Invoke($"Task {taskId} has exceeded max retries ({task.MaxRetries})");
return;
}
_taskStore.ReassignTask(taskId, null);
task.RetryCount++;
OnOutput?.Invoke($"Retrying task {taskId} (attempt {task.RetryCount})");
LaunchTaskAgent(taskId, ct);
}
private void ExecuteAddTask(LeadDecision decision)
{
var newTask = new AgentTask
{
TaskId = $"task-{_taskStore.GetAll().Count + 1}",
Title = decision.NewTaskTitle ?? decision.NewTaskDescription?[..Math.Min(60, decision.NewTaskDescription.Length)],
Description = decision.NewTaskDescription ?? "",
Priority = decision.NewTaskPriority ?? TaskPriority.Normal
};
_taskStore.AddTask(newTask);
OnOutput?.Invoke($"Added new task: {newTask.TaskId} — {newTask.Title}");
OnQueueUpdate?.Invoke(_taskStore.GetStatistics());
}
private void ExecuteSkipTask(string taskId)
{
_taskStore.Complete(taskId, new TaskResult(true, "Skipped by lead", new List<string>()));
OnOutput?.Invoke($"Task {taskId} skipped");
OnQueueUpdate?.Invoke(_taskStore.GetStatistics());
}
// --- Merge ---
private async Task<MergeResult> MergeBranchAsync(TaskAgent taskAgent, CancellationToken ct)
{
var targetBranch = _teamConfig.TargetBranch;
if (string.IsNullOrEmpty(targetBranch))
{
targetBranch = await _gitManager.GetCurrentBranchAsync(ct);
}
// Step 1: Bring the worktree branch up to date with the target branch.
// This incorporates any work already merged by other agents.
// Try rebase first (clean linear history), fall back to merge + AI conflict resolution.
if (_teamConfig.UseWorktrees)
{
OnOutput?.Invoke($"[{taskAgent.AgentId}] Rebasing {taskAgent.BranchName} onto {targetBranch}...");
var rebaseResult = await _gitManager.RunGitCommandAsync(
taskAgent.WorktreePath,
$"rebase {targetBranch}",
ct);
if (rebaseResult.ExitCode != 0)
{
// Rebase failed — abort and try a regular merge instead
await _gitManager.RunGitCommandAsync(taskAgent.WorktreePath, "rebase --abort", ct);
OnOutput?.Invoke($"[{taskAgent.AgentId}] Rebase had conflicts, merging {targetBranch} into worktree...");
var mergeInResult = await _gitManager.RunGitCommandAsync(
taskAgent.WorktreePath,
$"merge {targetBranch}",
ct);
if (mergeInResult.ExitCode != 0)
{
// Merge has conflicts — resolve with AI agent IN THE WORKTREE
var conflicts = _mergeManager.DetectConflicts(taskAgent.WorktreePath);
OnOutput?.Invoke($"[{taskAgent.AgentId}] {conflicts.Count} conflicted files, launching merge-fix agent in worktree...");
var fixAgent = new MergeFixAgent(_config, _teamConfig);
fixAgent.OnOutput += output => OnOutput?.Invoke(output);
fixAgent.OnError += error => OnError?.Invoke(error);
var resolved = await fixAgent.ResolveAsync(
taskAgent.WorktreePath,
conflicts,
mergeInResult.Error,
taskAgent.Task.Description,
ct);
Statistics.AITime += fixAgent.LastDuration;
if (!resolved)
{
await _gitManager.RunGitCommandAsync(taskAgent.WorktreePath, "merge --abort", ct);
return new MergeResult
{
Success = false,
Conflicts = conflicts,
Error = "Merge conflict resolution failed in worktree"
};
}
// MergeFixAgent resolved — commit the merge in the worktree
await _gitManager.CommitWorktreeAsync(
taskAgent.WorktreePath,
$"Merge {targetBranch} (conflicts resolved)",
ct);
}
}
}
// Step 2: Squash merge the worktree branch into the target.
// The worktree branch is now up to date with the target (via rebase or merge above),
// so this squash merge should apply cleanly.
var commitMessage = taskAgent.Task.Title ?? taskAgent.Task.Description;
// Collect original commit messages from the worktree branch
var originalMessages = await _gitManager.GetBranchCommitMessagesAsync(
taskAgent.WorktreePath, targetBranch, ct);
// Build commit body with original messages and model info
var commitBody = BuildMergeCommitBody(taskAgent, originalMessages);
return await _mergeManager.MergeDirectAsync(
taskAgent.WorktreePath, taskAgent.BranchName, targetBranch, ct, commitMessage, commitBody);
}
// --- Prompt building ---
private string BuildDecisionPrompt()
{
var sb = new StringBuilder();
sb.AppendLine("--- LEAD AGENT DECISION MODE ---");
sb.AppendLine("You are the LEAD AGENT coordinating a team of AI coding agents.");
sb.AppendLine($"You can run up to {MaxConcurrent} agents concurrently.");
sb.AppendLine("Your job is to decide what to do next based on the current project state.");
sb.AppendLine();
// Project context
var promptContent = AIProcessRunner.TryReadFile(
_config.PromptFilePath, null);
if (!string.IsNullOrEmpty(promptContent))
{
sb.AppendLine("--- PROJECT CONTEXT ---");
sb.AppendLine(AIProcessRunner.StripRalphStatusBlock(promptContent));
sb.AppendLine();
}
// Currently running agents
if (!_runningAgents.IsEmpty)
{
sb.AppendLine("--- CURRENTLY RUNNING AGENTS ---");
foreach (var (taskId, running) in _runningAgents)
{
var phase = running.TaskAgent.CurrentPhase;
var phaseStr = phase != SubAgentPhase.None ? $" (phase: {phase})" : "";
sb.AppendLine($" {running.TaskAgent.AgentId}: task {taskId}{phaseStr}");
}
sb.AppendLine($" Slots used: {_runningAgents.Count}/{MaxConcurrent}");
sb.AppendLine();
}
// Task summary (don't list every task — too much context for the AI)
var allTasks = _taskStore.GetAll();
var stats = _taskStore.GetStatistics();
sb.AppendLine("--- TASK SUMMARY ---");
sb.AppendLine($"Total: {stats.Total} | Pending: {stats.Pending} | InProgress: {stats.InProgress} | Completed: {stats.Completed} | Failed: {stats.Failed}");
sb.AppendLine();
// Only list failed tasks eligible for retry (this is why the AI is being consulted)
var retryable = allTasks
.Where(t => t.Status == Models.TaskStatus.Failed && t.RetryCount < t.MaxRetries)
.ToList();
if (retryable.Count > 0)
{
sb.AppendLine("FAILED TASKS (eligible for retry):");
foreach (var task in retryable.Take(20))
{
sb.AppendLine($" {task.TaskId}: {task.Title ?? task.Description} — Error: {Truncate(task.Error ?? "unknown", 100)}");
}
if (retryable.Count > 20)
sb.AppendLine($" ... and {retryable.Count - 20} more");
sb.AppendLine();
}
// List permanently failed tasks (exceeded retries)
var permFailed = allTasks
.Where(t => t.Status == Models.TaskStatus.Failed && t.RetryCount >= t.MaxRetries)
.ToList();
if (permFailed.Count > 0)
{
sb.AppendLine("PERMANENTLY FAILED (max retries exceeded):");
foreach (var task in permFailed.Take(10))
{
sb.AppendLine($" {task.TaskId}: {task.Title ?? task.Description}");
}
if (permFailed.Count > 10)
sb.AppendLine($" ... and {permFailed.Count - 10} more");
sb.AppendLine();
}
// User messages (priority)
if (_pendingUserMessages.Count > 0)
{
sb.AppendLine("--- USER MESSAGES (PRIORITY) ---");
sb.AppendLine("The human user has sent the following messages. Act on them with highest priority.");
sb.AppendLine("You may add_task for new work, re-read the plan, adjust priorities, or take other actions.");
sb.AppendLine();
foreach (var msg in _pendingUserMessages)
{
sb.AppendLine($" [{msg.Timestamp:HH:mm:ss}] {msg.Content}");
}
sb.AppendLine();
}
// Decision format
sb.AppendLine("--- DECISION FORMAT ---");
if (_pendingUserMessages.Count > 0)
{
sb.AppendLine("You are being consulted because the user sent a message. Respond to the user's request.");
}
else
{
sb.AppendLine("You are being consulted because there are failed tasks that may need retry or skip decisions.");
}
sb.AppendLine("You MUST output your decision in this exact format:");
sb.AppendLine();
sb.AppendLine("---LEAD_DECISION---");
sb.AppendLine("ACTION: next_task | retry_task | add_task | skip_task | declare_complete");
sb.AppendLine("TASK_ID: <task id, required for retry_task/skip_task/next_task>");
sb.AppendLine("NEW_TASK_TITLE: <title, required for add_task>");
sb.AppendLine("NEW_TASK_DESCRIPTION: <description, required for add_task>");
sb.AppendLine("REASON: <brief explanation>");
sb.AppendLine("---END_DECISION---");
sb.AppendLine();
sb.AppendLine("Actions: next_task (assign pending task), retry_task (retry failed), add_task (create new task), skip_task (skip a task), declare_complete (all done).");
sb.AppendLine("Output exactly ONE decision block. Be concise — do not list tasks or analyze at length.");
return sb.ToString();
}
// --- Decision parsing ---
internal static LeadDecision? ParseDecision(string output)
{
var match = Regex.Match(
output,
@"---LEAD_DECISION---(.*?)---END_DECISION---",
RegexOptions.Singleline);
if (!match.Success)
return null;
var block = match.Groups[1].Value;
var lines = block.Split('\n', StringSplitOptions.RemoveEmptyEntries);
var fields = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
foreach (var line in lines)
{
var colonIdx = line.IndexOf(':');
if (colonIdx <= 0) continue;
var key = line[..colonIdx].Trim();
var value = line[(colonIdx + 1)..].Trim();
fields[key] = value;
}
if (!fields.TryGetValue("ACTION", out var actionStr))
return null;
var action = actionStr.ToLower().Replace("-", "_") switch
{
"next_task" => LeadAction.NextTask,
"retry_task" => LeadAction.RetryTask,
"add_task" => LeadAction.AddTask,
"skip_task" => LeadAction.SkipTask,
"declare_complete" => LeadAction.DeclareComplete,
_ => (LeadAction?)null
};