Portierung der beiden fehlenden Grundbausteine aus PolytraderSharp (voller Ausbau). Core S-0 (Datenfundament fuer Analyse/Forensik): - core_decision_journal + core_order_events (+ ReasonCode/Decision/OrderEvent-Enums), IDecisionJournal/IOrderEventLog mit fehlertoleranten EF-Impls (Handel bricht nie). - SignalId-Durchreichung TradeSignal -> ExecutionService -> core_trade_history; ExecutionService schreibt an jeder Verzweigung Journal/Order-Events. - JSONL-Log-Sink (LogJson + Dual-Sink), pure Analytik: RealizedPnlEngine (FIFO), TradeAnalytics, DossierBuilder. Migration AddAnalysisFoundation. Accounting-Modul (acc_): unabhaengiger IBKR-Kontoauszug (Activity Flex Query) hinter Interfaces mit Offline-Null-Stubs -> append-only Ledger + Periodenabrechnung/BWA + FX (USD/EUR) + CSV/PDF (PDFsharp/MigraDoc). Steuerschicht bewusst offen (Platzhalter-Tab). Kein Handel. Migration InitialAccounting. Supervisor-Modul (sup_): read-only OpenRouter-Agent (Function-Calling-Loop) + read-only Tool-Registry (8 Tools) + Profile + Dossier-Browser + Counterfactual-Job (Stub) + Tagesbericht/MCP-Light (opt-in). Migration InitialSupervisor. Verdrahtung: Program.cs (beide Module + Icons), slnx/App/Tests-Referenzen, provision-db.ps1, AppSettings-Sektionen, docs/konzepte, README. Tests: 79 -> 117 gruen (FIFO/KPIs/Dossier/JSONL, Classifier/Engine/FX/Idempotenz, OpenRouter/Registry/Agent/MCP, STA-Konstruktion beider neuen Fenster). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
76 lines
2.9 KiB
C#
76 lines
2.9 KiB
C#
using FluentAssertions;
|
|
using IBKRTrader.Modules.Supervisor.Agent;
|
|
|
|
namespace IBKRTrader.Tests.Modules.Supervisor;
|
|
|
|
[Trait("cat", "unit")]
|
|
public class SupervisorAgentTests
|
|
{
|
|
/// <summary>Fake-Client: gibt vorab definierte Antworten der Reihe nach zurück.</summary>
|
|
private sealed class FakeChat : IChatCompletionClient
|
|
{
|
|
private readonly Queue<ChatResponse> _responses;
|
|
public List<string> SeenToolResults { get; } = new();
|
|
public FakeChat(params ChatResponse[] responses) => _responses = new(responses);
|
|
|
|
public Task<ChatResponse> CompleteAsync(string model, IReadOnlyList<ChatMessage> messages,
|
|
IReadOnlyList<SupervisorTool> tools, CancellationToken ct)
|
|
{
|
|
foreach (var m in messages)
|
|
if (m.Role == "tool" && m.Content != null) SeenToolResults.Add(m.Content);
|
|
return Task.FromResult(_responses.Dequeue());
|
|
}
|
|
}
|
|
|
|
private static SupervisorToolRegistry EchoRegistry()
|
|
{
|
|
var reg = new SupervisorToolRegistry();
|
|
reg.Register(new SupervisorTool("get_kpis", "KPIs", """{"type":"object"}""", _ => "{\"NetPnl\":42}"));
|
|
return reg;
|
|
}
|
|
|
|
[Fact]
|
|
public async Task RunsToolThenReturnsFinalAnswer()
|
|
{
|
|
var chat = new FakeChat(
|
|
new ChatResponse { ToolCalls = { new ToolCall("c1", "get_kpis", "{}") } },
|
|
new ChatResponse { Content = "Netto-PnL ist 42." });
|
|
var agent = new SupervisorAgent(chat, EchoRegistry());
|
|
|
|
var result = await agent.AskAsync("Wie ist die Performance?");
|
|
|
|
result.Answer.Should().Be("Netto-PnL ist 42.");
|
|
result.ToolInvocations.Should().ContainSingle();
|
|
result.ToolInvocations[0].Tool.Should().Be("get_kpis");
|
|
chat.SeenToolResults.Should().Contain(s => s.Contains("42")); // Tool-Ergebnis ging ans Modell zurück
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ProfileFilter_DeniesUnlistedTool()
|
|
{
|
|
var chat = new FakeChat(
|
|
new ChatResponse { ToolCalls = { new ToolCall("c1", "get_kpis", "{}") } },
|
|
new ChatResponse { Content = "fertig" });
|
|
// Technik-Profil listet get_kpis NICHT → Ausführung verweigert.
|
|
var agent = new SupervisorAgent(chat, EchoRegistry());
|
|
|
|
var result = await agent.AskAsync("test", profile: SupervisorProfiles.Technik);
|
|
|
|
result.ToolInvocations[0].Result.Should().Contain("nicht freigegeben");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task StopsAfterMaxIterations()
|
|
{
|
|
// Modell fordert IMMER ein Tool an → harte Iterationsgrenze greift.
|
|
var always = Enumerable.Range(0, SupervisorAgent.MaxIterations + 2)
|
|
.Select(_ => new ChatResponse { ToolCalls = { new ToolCall("c", "get_kpis", "{}") } })
|
|
.ToArray();
|
|
var agent = new SupervisorAgent(new FakeChat(always), EchoRegistry());
|
|
|
|
var result = await agent.AskAsync("Endlosschleife?");
|
|
|
|
result.Answer.Should().Contain("maximale Tool-Iterationen");
|
|
}
|
|
}
|