-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathInProcStepTracer.cs
More file actions
94 lines (76 loc) · 3.16 KB
/
InProcStepTracer.cs
File metadata and controls
94 lines (76 loc) · 3.16 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
// Copyright (c) Microsoft. All rights reserved.
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Agents.AI.Workflows.Execution;
namespace Microsoft.Agents.AI.Workflows.InProc;
internal sealed class InProcStepTracer : IStepTracer
{
private int _nextStepNumber;
public int StepNumber => this._nextStepNumber - 1;
public bool StateUpdated { get; private set; }
public CheckpointInfo? Checkpoint { get; private set; }
public ConcurrentDictionary<string, string> Instantiated { get; } = new();
public ConcurrentDictionary<string, string> Activated { get; } = new();
public void TraceIntantiated(string executorId) => this.Instantiated.TryAdd(executorId, executorId);
public void TraceActivated(string executorId) => this.Activated.TryAdd(executorId, executorId);
public void TraceStatePublished() => this.StateUpdated = true;
public void TraceCheckpointCreated(CheckpointInfo checkpoint) => this.Checkpoint = checkpoint;
/// <summary>
/// Reset the tracer to the specified step number.
/// </summary>
/// <param name="lastStepNumber">The Step Number of the last SuperStep. Note that Step Numbers are 0-indexed.</param>
public void Reload(int lastStepNumber = 0) => this._nextStepNumber = lastStepNumber + 1;
public SuperStepStartedEvent Advance(StepContext step, CheckpointInfo? startCheckpoint = null)
{
this._nextStepNumber++;
this.Activated.Clear();
this.Instantiated.Clear();
this.StateUpdated = false;
this.Checkpoint = null;
HashSet<string> sendingExecutors = [];
bool hasExternalMessages = false;
foreach (ExecutorIdentity identity in step.QueuedMessages.Keys)
{
if (identity == ExecutorIdentity.None)
{
hasExternalMessages = true;
}
else
{
sendingExecutors.Add(identity.Id!);
}
}
return new SuperStepStartedEvent(this.StepNumber, new SuperStepStartInfo(sendingExecutors)
{
HasExternalMessages = hasExternalMessages,
Checkpoint = startCheckpoint
});
}
public SuperStepCompletedEvent Complete(bool nextStepHasActions, bool hasPendingRequests) => new(this.StepNumber, new SuperStepCompletionInfo(this.Activated.Keys, this.Instantiated.Keys)
{
HasPendingMessages = nextStepHasActions,
HasPendingRequests = hasPendingRequests,
StateUpdated = this.StateUpdated,
Checkpoint = this.Checkpoint,
});
public override string ToString()
{
StringBuilder sb = new();
if (!this.Instantiated.IsEmpty)
{
sb.Append("Instantiated: ").Append(string.Join(", ", this.Instantiated.Keys.OrderBy(id => id, StringComparer.Ordinal)));
}
if (!this.Activated.IsEmpty)
{
if (sb.Length != 0)
{
sb.AppendLine();
}
sb.Append("Activated: ").Append(string.Join(", ", this.Activated.Keys.OrderBy(id => id, StringComparer.Ordinal)));
}
return sb.ToString();
}
}