-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLlmCompactionPerRoleEditModeTests.cs
More file actions
313 lines (270 loc) · 10.6 KB
/
LlmCompactionPerRoleEditModeTests.cs
File metadata and controls
313 lines (270 loc) · 10.6 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
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using CoreAI;
using CoreAI.AgentMemory;
using CoreAI.Ai;
using CoreAI.Authority;
using CoreAI.Messaging;
using CoreAI.Session;
using NUnit.Framework;
namespace CoreAI.Tests.EditMode
{
[TestFixture]
public sealed class LlmCompactionPerRoleEditModeTests
{
private sealed class TestAuthority : IAuthorityHost
{
public bool CanRunAiTasks => true;
public bool IsServer => true;
public bool IsClient => true;
}
private sealed class FlatTokenEstimator : ITokenEstimator
{
private readonly int _n;
public FlatTokenEstimator(int n)
{
_n = Math.Max(1, n);
}
public int EstimateText(string text)
{
return _n;
}
}
private sealed class FixedHistoryBudgetPolicy : IContextBudgetPolicy
{
private readonly int _historyBudget;
public FixedHistoryBudgetPolicy(int historyBudget)
{
_historyBudget = Math.Max(1, historyBudget);
}
public ContextBudget Compute(ContextBudgetRequest request, ITokenEstimator estimator)
{
return new ContextBudget(8192, 256, 50, _historyBudget, 0);
}
}
/// <summary>Counts compaction-role calls separately from orchestrator/main LLM calls.</summary>
private sealed class SplitCountingLlm : ILlmClient
{
private readonly ILlmClient _inner;
public int CompactionCompletes { get; private set; }
public SplitCountingLlm(ILlmClient inner)
{
_inner = inner ?? throw new ArgumentNullException(nameof(inner));
}
public void SetTools(IReadOnlyList<ILlmTool> tools)
{
_inner.SetTools(tools);
}
public Task<LlmCompletionResult> CompleteAsync(
LlmCompletionRequest request,
CancellationToken cancellationToken = default)
{
if (string.Equals(
request.AgentRoleId,
BuiltInAgentRoleIds.ContextCompactionAux,
StringComparison.Ordinal))
{
CompactionCompletes++;
return Task.FromResult(new LlmCompletionResult { Ok = true, Content = "rollup-text" });
}
return _inner.CompleteAsync(request, cancellationToken);
}
}
private sealed class TestMemoryStore : IAgentMemoryStore
{
public List<ChatMessage> FakeHistory { get; } = new();
public bool TryLoad(string roleId, out AgentMemoryState state)
{
state = null;
return false;
}
public void Save(string roleId, AgentMemoryState state)
{
}
public void Clear(string roleId)
{
}
public void ClearChatHistory(string roleId)
{
}
public void AppendChatMessage(string roleId, string role, string content, bool persistToDisk = true)
{
}
public ChatMessage[] GetChatHistory(string roleId, int maxMessages = 0)
{
if (maxMessages > 0 && FakeHistory.Count > maxMessages)
{
int skip = FakeHistory.Count - maxMessages;
return FakeHistory.GetRange(skip, maxMessages).ToArray();
}
return FakeHistory.ToArray();
}
}
private sealed class TestSink : IAiGameCommandSink
{
public void Publish(ApplyAiGameCommand command)
{
}
}
private sealed class TestTelemetry : ISessionTelemetryProvider
{
public GameSessionSnapshot BuildSnapshot()
{
return new GameSessionSnapshot();
}
}
private sealed class NullSys : IAgentSystemPromptProvider
{
public bool TryGetSystemPrompt(string roleId, out string prompt)
{
prompt = "";
return true;
}
}
private sealed class NullUsr : IAgentUserPromptTemplateProvider
{
public bool TryGetUserTemplate(string roleId, out string template)
{
template = "{hint}";
return true;
}
}
private sealed class StubCoreSettingsWithCompaction : ICoreAISettings
{
public bool EnableLlmContextCompaction => true;
public int MaxLuaRepairRetries => 1;
public bool EnableMeaiDebugLogging => false;
public float LlmRequestTimeoutSeconds => 60f;
public int MaxLlmRequestRetries => 1;
public bool EnableHttpDebugLogging => false;
public bool LogTokenUsage => false;
public bool LogLlmLatency => false;
public bool LogLlmConnectionErrors => false;
public int ContextWindowTokens => 8192;
public string UniversalSystemPromptPrefix => "";
public float Temperature => 0.1f;
public int MaxToolCallRetries => 2;
public bool LogToolCalls => false;
public bool LogToolCallArguments => false;
public bool LogToolCallResults => false;
public bool LogMeaiToolCallingSteps => false;
public bool AllowDuplicateToolCalls => false;
public bool EnableStreaming => false;
}
private static AgentMemoryPolicy MakePolicyForRole(string roleId, bool chatHistory = true)
{
AgentMemoryPolicy policy = new();
policy.DisableMemoryTool(roleId);
policy.SetToolsForRole(roleId, Array.Empty<ILlmTool>());
if (chatHistory)
{
policy.ConfigureChatHistory(roleId, true, 8192, false, 50);
}
return policy;
}
private static void SeedHistory(TestMemoryStore mem, int count)
{
for (int i = 0; i < count; i++)
{
mem.FakeHistory.Add(new ChatMessage
{
Role = i % 2 == 0 ? "user" : "assistant",
Content = $"m{i}-".PadRight(40, 'x')
});
}
}
[Test]
public void AgentMemoryPolicy_Programmer_DisablesLlmCompaction_ByDefault()
{
AgentMemoryPolicy policy = new();
Assert.IsFalse(policy.GetRoleConfig(BuiltInAgentRoleIds.Programmer).UseLlmContextCompaction);
}
[Test]
public void AgentMemoryPolicy_Creator_EnablesLlmCompaction_ByDefault()
{
AgentMemoryPolicy policy = new();
Assert.IsTrue(policy.GetRoleConfig(BuiltInAgentRoleIds.Creator).UseLlmContextCompaction);
}
[Test]
public void AgentBuilder_WithLlmContextCompaction_False_WrittenToPolicy()
{
AgentMemoryPolicy policy = new();
new AgentBuilder("CustomNoSmart")
.WithSystemPrompt("x")
.WithChatHistory(4096)
.WithLlmContextCompaction(false)
.Build()
.ApplyToPolicy(policy);
Assert.IsFalse(policy.GetRoleConfig("CustomNoSmart").UseLlmContextCompaction);
}
[Test]
public async Task Orchestrator_PerRole_UseLlmContextCompaction_CompactionCallOnlyWhenEnabled()
{
const string smartRole = "smart_role_compact";
string programmerRole = BuiltInAgentRoleIds.Programmer;
TestMemoryStore memSmart = new();
SeedHistory(memSmart, 8);
TestMemoryStore memProg = new();
SeedHistory(memProg, 8);
StubLlmClient stub = new();
SplitCountingLlm counting = new(stub);
InMemoryConversationSummaryStore summaryStore = new();
ITokenEstimator compactEstimator = new FlatTokenEstimator(10);
IConversationContextManager ctxMgr = ConversationContextManagerFactories.Create(
true,
summaryStore,
compactEstimator,
counting,
null);
StubCoreSettingsWithCompaction settings = new();
AgentMemoryPolicy policySmart = MakePolicyForRole(smartRole);
AiOrchestrator orchSmart = new(
new TestAuthority(),
counting,
new TestSink(),
new TestTelemetry(),
new AiPromptComposer(new NullSys(), new NullUsr(), null, null, policySmart, settings),
memSmart,
policySmart,
new NoOpRoleStructuredResponsePolicy(),
new NullAiOrchestrationMetrics(),
settings,
ctxMgr,
null,
new FixedHistoryBudgetPolicy(25));
await orchSmart.RunTaskAsync(new AiTaskRequest { RoleId = smartRole, Hint = "hi" }).ConfigureAwait(false);
Assert.GreaterOrEqual(counting.CompactionCompletes, 1, "Smart role should trigger auxiliary compaction.");
StubLlmClient stub2 = new();
SplitCountingLlm counting2 = new(stub2);
IConversationContextManager ctxMgr2 = ConversationContextManagerFactories.Create(
true,
new InMemoryConversationSummaryStore(),
compactEstimator,
counting2,
null);
AgentMemoryPolicy policyProg = MakePolicyForRole(programmerRole);
AiOrchestrator orchProg = new(
new TestAuthority(),
counting2,
new TestSink(),
new TestTelemetry(),
new AiPromptComposer(new NullSys(), new NullUsr(), null, null, policyProg, settings),
memProg,
policyProg,
new NoOpRoleStructuredResponsePolicy(),
new NullAiOrchestrationMetrics(),
settings,
ctxMgr2,
null,
new FixedHistoryBudgetPolicy(25));
await orchProg.RunTaskAsync(new AiTaskRequest { RoleId = programmerRole, Hint = "hi" })
.ConfigureAwait(false);
Assert.AreEqual(
0,
counting2.CompactionCompletes,
"Programmer role should skip LLM compaction by default.");
}
}
}