Repository navigation
Expand file tree
/
Copy pathWorkflowModels.cs
More file actions
227 lines (196 loc) · 7.24 KB
/
Copy pathWorkflowModels.cs
File metadata and controls
227 lines (196 loc) · 7.24 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
using System;
using System.Collections.Generic;
using System.Linq;
namespace MissionController.Core.Workflow;
public enum WorkflowTriggerType
{
Manual,
Webhook,
OAuthCallback,
Schedule,
FileWatch
}
public enum WorkflowNodeType
{
Trigger,
/// <summary>
/// One governed template, run as a complete 1:1:1 mission through
/// <see cref="MissionController.Core.Interfaces.IMissionExecutionEngine"/>.
///
/// This is what a workflow step IS. A node names a template from the governed catalogue and
/// supplies its declared parameters; the mission engine then does what it always does - ACL
/// jail, job object, restricted token, AppContainer network denial, validated argument binding,
/// DLP on the way out, and its own row in the audit ledger. The workflow contributes ordering
/// and nothing else, which is the only arrangement in which "governed workflow" means anything.
/// </summary>
GovernedMission,
/// <summary>
/// Free-form shell, executed outside the mission engine. REFUSED - see
/// <see cref="MissionController.Core.Workflow.WorkflowExecutionEngine"/>.
/// </summary>
SandboxedScript,
AgentReasoning,
OAuthExchange,
GovernedHttp,
Condition,
ReActAgent,
MemoryStore,
MemoryRecall,
TransactionalScope
}
public record WorkflowTriggerConfig(
WorkflowTriggerType Type,
string? EndpointOrPath = null,
string? CronOrInterval = null,
string? SecretToken = null);
public record WorkflowNode(
string NodeId,
string Name,
WorkflowNodeType NodeType,
IReadOnlyDictionary<string, string> Configuration,
string? TargetModel = null,
string? ScriptCommand = null,
string? AllowedEgressFqdn = null)
{
public void Validate()
{
if (string.IsNullOrWhiteSpace(NodeId))
throw new InvalidOperationException("NodeId must not be empty.");
if (string.IsNullOrWhiteSpace(Name))
throw new InvalidOperationException($"Node '{NodeId}' must have a valid Name.");
}
}
public record WorkflowEdge(
string SourceNodeId,
string TargetNodeId,
string? ConditionBranch = null);
public record WorkflowGraph(
string WorkflowId,
string Name,
string Description,
WorkflowTriggerConfig Trigger,
IReadOnlyList<WorkflowNode> Nodes,
IReadOnlyList<WorkflowEdge> Edges,
IReadOnlyDictionary<string, string>? Metadata = null)
{
public void Validate()
{
if (string.IsNullOrWhiteSpace(WorkflowId))
throw new InvalidOperationException("WorkflowId must not be empty.");
if (string.IsNullOrWhiteSpace(Name))
throw new InvalidOperationException("Workflow Name must not be empty.");
if (Nodes == null || Nodes.Count == 0)
throw new InvalidOperationException("Workflow must contain at least one node.");
var duplicateIds = Nodes
.GroupBy(n => n.NodeId, StringComparer.OrdinalIgnoreCase)
.Where(g => g.Count() > 1)
.Select(g => g.Key)
.ToList();
if (duplicateIds.Count > 0)
throw new InvalidOperationException($"Duplicate node IDs detected in workflow: {string.Join(", ", duplicateIds)}");
foreach (var node in Nodes)
{
node.Validate();
}
var nodeSet = new HashSet<string>(Nodes.Select(n => n.NodeId), StringComparer.OrdinalIgnoreCase);
if (Edges != null)
{
foreach (var edge in Edges)
{
if (!nodeSet.Contains(edge.SourceNodeId))
throw new InvalidOperationException($"Edge source '{edge.SourceNodeId}' does not exist in workflow nodes.");
if (!nodeSet.Contains(edge.TargetNodeId))
throw new InvalidOperationException($"Edge target '{edge.TargetNodeId}' does not exist in workflow nodes.");
}
// Cycle detection
DetectCycles();
}
}
private void DetectCycles()
{
var inDegree = Nodes.ToDictionary(n => n.NodeId, _ => 0, StringComparer.OrdinalIgnoreCase);
var adj = Nodes.ToDictionary(n => n.NodeId, _ => new List<string>(), StringComparer.OrdinalIgnoreCase);
foreach (var edge in Edges)
{
adj[edge.SourceNodeId].Add(edge.TargetNodeId);
inDegree[edge.TargetNodeId]++;
}
var queue = new Queue<string>(inDegree.Where(kvp => kvp.Value == 0).Select(kvp => kvp.Key));
int visitedCount = 0;
while (queue.Count > 0)
{
var curr = queue.Dequeue();
visitedCount++;
foreach (var neighbor in adj[curr])
{
inDegree[neighbor]--;
if (inDegree[neighbor] == 0)
{
queue.Enqueue(neighbor);
}
}
}
if (visitedCount != Nodes.Count)
{
throw new InvalidOperationException("Workflow graph contains a cycle. SPLAT workflows must be directed acyclic graphs (DAGs).");
}
}
public IReadOnlyList<WorkflowNode> GetTopologicalOrder()
{
Validate();
var inDegree = Nodes.ToDictionary(n => n.NodeId, _ => 0, StringComparer.OrdinalIgnoreCase);
var adj = Nodes.ToDictionary(n => n.NodeId, _ => new List<string>(), StringComparer.OrdinalIgnoreCase);
foreach (var edge in Edges)
{
adj[edge.SourceNodeId].Add(edge.TargetNodeId);
inDegree[edge.TargetNodeId]++;
}
var queue = new Queue<string>(inDegree.Where(kvp => kvp.Value == 0).Select(kvp => kvp.Key));
var order = new List<WorkflowNode>();
var nodeDict = Nodes.ToDictionary(n => n.NodeId, n => n, StringComparer.OrdinalIgnoreCase);
while (queue.Count > 0)
{
var currId = queue.Dequeue();
order.Add(nodeDict[currId]);
foreach (var neighbor in adj[currId])
{
inDegree[neighbor]--;
if (inDegree[neighbor] == 0)
{
queue.Enqueue(neighbor);
}
}
}
return order;
}
}
/// <summary>
/// Raised when a node cannot complete. The run stops here rather than continuing on a node's
/// unusable output: a workflow that reports success its own audit ledger contradicts is worth less
/// than one that reports the failure.
/// </summary>
public sealed class WorkflowNodeFailedException : Exception
{
public string NodeId { get; }
/// <summary>
/// Whatever the node produced before it failed. Carried on the exception because the failing
/// node's output is the first thing an operator needs, and the run is abandoned before the
/// normal output-recording path is reached.
/// </summary>
public string? NodeOutput { get; }
public WorkflowNodeFailedException(string nodeId, string reason, string? nodeOutput = null)
: base($"Node '{nodeId}' failed: {reason}")
{
NodeId = nodeId;
NodeOutput = nodeOutput;
}
}
public record WorkflowExecutionResult(
string WorkflowId,
Guid ExecutionId,
bool Success,
IReadOnlyDictionary<string, string> NodeOutputs,
IReadOnlyList<string> ExecutionLogs,
DateTime StartedUtc,
DateTime CompletedUtc,
string? FailureReason = null);