|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Linq; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using AiDotNet.Agentic.Agents; |
| 5 | +using AiDotNet.Agentic.Models; |
| 6 | +using AiDotNet.Agentic.SelfImproving; |
| 7 | +using AiDotNetTests.UnitTests.Agentic.Agents; |
| 8 | +using Xunit; |
| 9 | + |
| 10 | +namespace AiDotNetTests.UnitTests.Agentic.SelfImproving |
| 11 | +{ |
| 12 | + public class TrajectoryCaptureTests |
| 13 | + { |
| 14 | + private static AgentExecutor<double> Agent(string name, string answer) |
| 15 | + { |
| 16 | + var client = ScriptedChatClient<double>.Sequence(ChatResponses.Text(answer)); |
| 17 | + return new AgentExecutor<double>(client, tools: null, new AgentExecutorOptions { Name = name }); |
| 18 | + } |
| 19 | + |
| 20 | + [Fact(Timeout = 60000)] |
| 21 | + public async Task TracingAgent_RecordsEachRun() |
| 22 | + { |
| 23 | + var store = new InMemoryTrajectoryStore(); |
| 24 | + var traced = new TracingAgent<double>(Agent("writer", "Hello."), store); |
| 25 | + |
| 26 | + var result = await traced.RunAsync(new[] { ChatMessage.User("hi") }); |
| 27 | + |
| 28 | + Assert.Equal("Hello.", result.FinalText); // behavior unchanged |
| 29 | + |
| 30 | + var all = await store.GetAllAsync(); |
| 31 | + Assert.Single(all); |
| 32 | + Assert.Equal("writer", all[0].AgentName); |
| 33 | + Assert.Equal("Hello.", all[0].FinalText); |
| 34 | + Assert.Contains(all[0].Messages, m => m.Role == ChatRole.User && m.Text == "hi"); |
| 35 | + Assert.Null(all[0].Reward); |
| 36 | + } |
| 37 | + |
| 38 | + [Fact(Timeout = 60000)] |
| 39 | + public async Task TracingAgent_AccumulatesAcrossRuns_AndIsQueryable() |
| 40 | + { |
| 41 | + var store = new InMemoryTrajectoryStore(); |
| 42 | + await new TracingAgent<double>(Agent("a", "one"), store).RunAsync(new[] { ChatMessage.User("x") }); |
| 43 | + await new TracingAgent<double>(Agent("b", "two"), store).RunAsync(new[] { ChatMessage.User("y") }); |
| 44 | + |
| 45 | + Assert.Equal(2, (await store.GetAllAsync()).Count); |
| 46 | + |
| 47 | + var onlyB = await store.QueryAsync(t => t.AgentName == "b"); |
| 48 | + Assert.Single(onlyB); |
| 49 | + Assert.Equal("two", onlyB[0].FinalText); |
| 50 | + } |
| 51 | + |
| 52 | + [Fact(Timeout = 60000)] |
| 53 | + public async Task TracingAgent_AttachesMetadata() |
| 54 | + { |
| 55 | + var store = new InMemoryTrajectoryStore(); |
| 56 | + var metadata = new Dictionary<string, string> { ["experiment"] = "exp-1" }; |
| 57 | + var traced = new TracingAgent<double>(Agent("a", "ok"), store, metadata); |
| 58 | + |
| 59 | + await traced.RunAsync(new[] { ChatMessage.User("x") }); |
| 60 | + |
| 61 | + var all = await store.GetAllAsync(); |
| 62 | + Assert.NotNull(all[0].Metadata); |
| 63 | + Assert.Equal("exp-1", all[0].Metadata["experiment"]); |
| 64 | + } |
| 65 | + |
| 66 | + [Fact(Timeout = 60000)] |
| 67 | + public async Task Store_GetById_And_RewardAnnotation() |
| 68 | + { |
| 69 | + var store = new InMemoryTrajectoryStore(); |
| 70 | + var traced = new TracingAgent<double>(Agent("a", "answer"), store); |
| 71 | + await traced.RunAsync(new[] { ChatMessage.User("x") }); |
| 72 | + |
| 73 | + var id = (await store.GetAllAsync())[0].Id; |
| 74 | + var fetched = await store.GetAsync(id); |
| 75 | + Assert.NotNull(fetched); |
| 76 | + |
| 77 | + // An evaluator grades the trajectory; the annotation is visible on subsequent reads. |
| 78 | + fetched.Reward = 0.75; |
| 79 | + var again = await store.GetAsync(id); |
| 80 | + Assert.NotNull(again); |
| 81 | + Assert.Equal(0.75, again.Reward); |
| 82 | + } |
| 83 | + |
| 84 | + [Fact(Timeout = 60000)] |
| 85 | + public async Task Store_Clear_Empties() |
| 86 | + { |
| 87 | + var store = new InMemoryTrajectoryStore(); |
| 88 | + await new TracingAgent<double>(Agent("a", "ok"), store).RunAsync(new[] { ChatMessage.User("x") }); |
| 89 | + await store.ClearAsync(); |
| 90 | + Assert.Empty(await store.GetAllAsync()); |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments