-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathSubAgentOrchestrator.cs
More file actions
166 lines (150 loc) · 6.47 KB
/
Copy pathSubAgentOrchestrator.cs
File metadata and controls
166 lines (150 loc) · 6.47 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
using System.Text.Json;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using SharpClaw.Code.Agents.Abstractions;
using SharpClaw.Code.Agents.Agents;
using SharpClaw.Code.Agents.Models;
using SharpClaw.Code.Protocol.Enums;
using SharpClaw.Code.Protocol.Events;
using SharpClaw.Code.Protocol.Models;
using SharpClaw.Code.Protocol.Serialization;
using SharpClaw.Code.Tools.Abstractions;
using SharpClaw.Code.Tools.Models;
namespace SharpClaw.Code.Agents.Internal;
/// <summary>
/// Executes delegated subagent tasks as bounded read-only child runs.
/// </summary>
public sealed class SubAgentOrchestrator(
IServiceProvider serviceProvider,
IToolExecutor toolExecutor,
ILogger<SubAgentOrchestrator> logger) : ISubAgentOrchestrator
{
/// <inheritdoc />
public async Task<SubAgentBatchExecutionResult> ExecuteAsync(
SubAgentBatchRequest request,
ToolExecutionContext context,
CancellationToken cancellationToken)
{
ArgumentNullException.ThrowIfNull(request);
ArgumentNullException.ThrowIfNull(context);
if (request.Tasks is not { Length: > 0 })
{
throw new InvalidOperationException("The subagent request must include at least one task.");
}
if (request.Tasks.Length > SubAgentToolContract.MaxTasks)
{
throw new InvalidOperationException($"The subagent request exceeds the limit of {SubAgentToolContract.MaxTasks} tasks.");
}
var runs = request.Tasks
.Select((task, index) => ExecuteSingleAsync(task, index, context, cancellationToken))
.ToArray();
var completedRuns = await Task.WhenAll(runs).ConfigureAwait(false);
var taskResults = completedRuns.Select(static run => run.TaskResult).ToArray();
var events = completedRuns.SelectMany(static run => run.Events).ToArray();
var result = new SubAgentBatchResult(
Tasks: taskResults,
CompletedCount: taskResults.Count(static task => task.Succeeded),
FailedCount: taskResults.Count(static task => !task.Succeeded));
return new SubAgentBatchExecutionResult(result, events);
}
private async Task<SingleTaskExecutionResult> ExecuteSingleAsync(
SubAgentTaskRequest task,
int index,
ToolExecutionContext parentContext,
CancellationToken cancellationToken)
{
ArgumentNullException.ThrowIfNull(task);
var goal = task.Goal?.Trim();
var expectedOutput = task.ExpectedOutput?.Trim();
if (string.IsNullOrWhiteSpace(goal) || string.IsNullOrWhiteSpace(expectedOutput))
{
throw new InvalidOperationException("Each subagent task requires both goal and expectedOutput.");
}
var taskId = $"subtask-{index + 1:D2}-{Guid.NewGuid():N}";
var delegatedTask = new DelegatedTaskContract(
taskId,
goal,
expectedOutput,
NormalizeConstraints(task.Constraints));
try
{
var subAgentWorker = serviceProvider.GetRequiredService<SubAgentWorker>();
var result = await subAgentWorker.RunAsync(
new AgentRunContext(
SessionId: parentContext.SessionId,
TurnId: parentContext.TurnId,
Prompt: goal,
WorkingDirectory: parentContext.WorkingDirectory,
Model: string.IsNullOrWhiteSpace(parentContext.Model) ? "default" : parentContext.Model!,
PermissionMode: PermissionMode.ReadOnly,
OutputFormat: OutputFormat.Text,
ToolExecutor: toolExecutor,
Metadata: BuildChildMetadata(parentContext),
ParentAgentId: parentContext.AgentId,
DelegatedTask: delegatedTask,
PrimaryMode: PrimaryMode.Plan,
ToolMutationRecorder: null,
ConversationHistory: null,
IsInteractive: false,
ApprovalSettings: ApprovalSettings.Empty),
cancellationToken).ConfigureAwait(false);
return new SingleTaskExecutionResult(
new SubAgentTaskResult(
TaskId: taskId,
Goal: goal,
ExpectedOutput: expectedOutput,
Succeeded: true,
Output: string.IsNullOrWhiteSpace(result.Output) ? "(no output)" : result.Output.Trim(),
ErrorMessage: null,
AgentId: result.AgentId),
result.Events ?? []);
}
catch (OperationCanceledException)
{
throw;
}
catch (Exception exception)
{
logger.LogWarning(
exception,
"Delegated subagent task {TaskId} failed for session {SessionId}, turn {TurnId}.",
taskId,
parentContext.SessionId,
parentContext.TurnId);
return new SingleTaskExecutionResult(
new SubAgentTaskResult(
TaskId: taskId,
Goal: goal,
ExpectedOutput: expectedOutput,
Succeeded: false,
Output: null,
ErrorMessage: exception.Message,
AgentId: SubAgentWorker.SubAgentId),
[]);
}
}
private static string[] NormalizeConstraints(string[]? constraints)
=> constraints?
.Where(static value => !string.IsNullOrWhiteSpace(value))
.Select(static value => value.Trim())
.Distinct(StringComparer.Ordinal)
.ToArray()
?? [];
private static Dictionary<string, string> BuildChildMetadata(ToolExecutionContext parentContext)
{
var metadata = new Dictionary<string, string>(StringComparer.Ordinal);
if (parentContext.Metadata is not null
&& parentContext.Metadata.TryGetValue("provider", out var provider)
&& !string.IsNullOrWhiteSpace(provider))
{
metadata["provider"] = provider;
}
metadata[SharpClawWorkflowMetadataKeys.AgentAllowedToolsJson] = JsonSerializer.Serialize(
SubAgentToolContract.AllowedReadOnlyTools,
ProtocolJsonContext.Default.StringArray);
return metadata;
}
private sealed record SingleTaskExecutionResult(
SubAgentTaskResult TaskResult,
IReadOnlyList<RuntimeEvent> Events);
}