-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathCrashAfterTurns.cs
More file actions
72 lines (66 loc) · 2.43 KB
/
CrashAfterTurns.cs
File metadata and controls
72 lines (66 loc) · 2.43 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
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace Temporalio.Extensions.ToolRegistry.Testing
{
/// <summary>
/// Implements <see cref="IProvider"/> and returns an error after <see cref="N"/> complete
/// turns. Use it in integration tests to verify that <see cref="AgenticSession"/> resumes from
/// a heartbeat checkpoint after a simulated crash.
/// </summary>
/// <remarks>
/// Example:
/// <code>
/// // First invocation returns an error after 2 turns.
/// // Second invocation (retry) resumes from the last checkpoint.
/// var provider = new CrashAfterTurns { N = 2 };
/// </code>
/// </remarks>
public sealed class CrashAfterTurns : IProvider
{
private int count;
/// <summary>
/// Gets or sets the number of turns to complete before throwing.
/// </summary>
public int N { get; set; }
/// <summary>
/// Gets or sets an optional delegate provider to forward turns to for the first
/// <see cref="N"/> turns. When <c>null</c>, a stub assistant response is returned
/// instead.
/// </summary>
public IProvider? Delegate { get; set; }
/// <summary>
/// Completes a turn normally for the first <see cref="N"/> turns, then throws.
/// </summary>
/// <inheritdoc/>
public Task<ProviderTurnResult> RunTurnAsync(
IList<Dictionary<string, object?>> messages,
IReadOnlyList<ToolDef> tools,
CancellationToken cancellationToken = default)
{
count++;
if (count > N)
{
throw new InvalidOperationException(
$"CrashAfterTurns: simulated crash after {N} turns");
}
if (Delegate != null)
{
return Delegate.RunTurnAsync(messages, tools, cancellationToken);
}
var newMessages = new List<Dictionary<string, object?>>
{
new()
{
["role"] = "assistant",
["content"] = new List<object?>
{
new Dictionary<string, object?> { ["type"] = "text", ["text"] = "..." },
},
},
};
return Task.FromResult(new ProviderTurnResult(newMessages, Done: count >= N));
}
}
}