Mit Roslyn umbenannt (Verweise folgen, Zeichenketten bleiben unberuehrt). Nicht umbenannt, weil gespeichert oder ueber die API gebunden (Stufe 4e): Properties, Record-Parameter, Enum-Member sowie die Parameter der Endpunkt-Handler (Routen-/Query-Bindung ueber den Namen). Die JSON-Felder anonymer Objekte bleiben ausdruecklich benannt (JsonFilter: pfad). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
160 lines
6.5 KiB
C#
160 lines
6.5 KiB
C#
using System.Text.Json;
|
|
using ClawdDotNet.Core.Api.Models;
|
|
using ClawdDotNet.Core.Engine;
|
|
using ClawdDotNet.Core.Tests.Infrastructure;
|
|
using Shouldly;
|
|
|
|
namespace ClawdDotNet.Core.Tests.Engine;
|
|
|
|
public sealed class PromptCacheTests
|
|
{
|
|
// ═══════════════════════════════════════════════════════════
|
|
// Activation
|
|
// ═══════════════════════════════════════════════════════════
|
|
|
|
[Theory]
|
|
[InlineData("auto", "anthropic/claude-sonnet-4-5", true)]
|
|
[InlineData("auto", "anthropic/claude-haiku-4.5", true)]
|
|
[InlineData("auto", "openai/gpt-4o", false)]
|
|
[InlineData("auto", "google/gemini-2.5-flash", false)]
|
|
[InlineData("on", "openai/gpt-4o", true)]
|
|
[InlineData("off", "anthropic/claude-sonnet-4-5", false)]
|
|
[InlineData("OFF", "anthropic/claude-sonnet-4-5", false)]
|
|
public void Activation_depends_on_setting_and_model(string setting, string model, bool expected)
|
|
{
|
|
PromptCache.IsEnabledFor(setting, model).ShouldBe(expected);
|
|
}
|
|
|
|
// ═══════════════════════════════════════════════════════════
|
|
// Placement of the breakpoints
|
|
// ═══════════════════════════════════════════════════════════
|
|
|
|
[Fact]
|
|
public void The_SystemPrompt_gets_a_breakpoint()
|
|
{
|
|
var messages = Conversation.Start("System").User("Frage").Build();
|
|
|
|
PromptCache.ApplyBreakpoints(messages);
|
|
|
|
messages[0].Role.ShouldBe("system");
|
|
messages[0].CacheBreakpoint.ShouldBeTrue();
|
|
}
|
|
|
|
[Fact]
|
|
public void The_last_message_with_content_gets_a_rolling_breakpoint()
|
|
{
|
|
var messages = Conversation.Start().User("A").Assistant("B").User("C").Build();
|
|
|
|
PromptCache.ApplyBreakpoints(messages);
|
|
|
|
messages[^1].CacheBreakpoint.ShouldBeTrue();
|
|
messages[^1].Content.ShouldBe("C");
|
|
}
|
|
|
|
[Fact]
|
|
public void An_assistant_message_without_content_carries_no_breakpoint()
|
|
{
|
|
// assistant with tool_calls has no text content and cannot carry a block.
|
|
var messages = Conversation.Start().User("A").Build();
|
|
messages.Add(ChatMessage.AssistantWithToolCalls([
|
|
new ToolCall { Id = "c1", Function = new ToolCallFunction { Name = "T", Arguments = "{}" } }
|
|
]));
|
|
|
|
PromptCache.ApplyBreakpoints(messages);
|
|
|
|
messages[^1].CacheBreakpoint.ShouldBeFalse();
|
|
messages.Count(m => m.CacheBreakpoint).ShouldBeGreaterThan(0, "der System-Breakpoint muss bleiben");
|
|
}
|
|
|
|
[Fact]
|
|
public void At_most_two_breakpoints_are_set()
|
|
{
|
|
// Providers only allow a limited number — they must not accumulate.
|
|
var messages = Conversation.Start().Repeat(10).Build();
|
|
|
|
PromptCache.ApplyBreakpoints(messages);
|
|
|
|
messages.Count(m => m.CacheBreakpoint).ShouldBeLessThanOrEqualTo(2);
|
|
}
|
|
|
|
[Fact]
|
|
public void Repeated_application_does_not_accumulate_breakpoints()
|
|
{
|
|
var messages = Conversation.Start().Repeat(3).Build();
|
|
|
|
PromptCache.ApplyBreakpoints(messages);
|
|
messages.Add(ChatMessage.User("Noch eine Frage"));
|
|
PromptCache.ApplyBreakpoints(messages);
|
|
messages.Add(ChatMessage.User("Und noch eine"));
|
|
PromptCache.ApplyBreakpoints(messages);
|
|
|
|
messages.Count(m => m.CacheBreakpoint).ShouldBeLessThanOrEqualTo(2);
|
|
messages[^1].CacheBreakpoint.ShouldBeTrue("der Breakpoint muss mitwandern");
|
|
}
|
|
|
|
[Fact]
|
|
public void An_empty_list_does_not_lead_to_an_error()
|
|
{
|
|
var messages = new List<ChatMessage>();
|
|
|
|
Should.NotThrow(() => PromptCache.ApplyBreakpoints(messages));
|
|
}
|
|
|
|
// ═══════════════════════════════════════════════════════════
|
|
// Prefix stability — the decisive test
|
|
// ═══════════════════════════════════════════════════════════
|
|
|
|
/// <summary>
|
|
/// The cached prompt section must be identical to the character between two
|
|
/// steps. A single timestamp in the system prompt would discard the cache at every step
|
|
/// — the costs would stay unchanged without it being noticed anywhere.
|
|
/// That is exactly what this test protects against.
|
|
/// </summary>
|
|
[Fact]
|
|
public async Task The_prefix_stays_identical_across_all_steps()
|
|
{
|
|
var fixture = new EngineFixture().WithTool(FakeTool.Returning("ok"));
|
|
var agent = fixture.AddAgent("agent-cache", "TestTool");
|
|
agent.Model = "anthropic/claude-sonnet-4-5";
|
|
agent.PromptCaching = "on";
|
|
|
|
// Eight tool steps, then a text answer.
|
|
for (var i = 0; i < 8; i++)
|
|
fixture.Client.RespondsWithToolCall("TestTool");
|
|
fixture.Client.RespondsWithText("Fertig");
|
|
|
|
await fixture.Engine.ChatAsync(agent, "Los", "test-instance", default);
|
|
|
|
fixture.Client.ReceivedRequests.Count.ShouldBe(9);
|
|
|
|
// The system message is the stable prefix — it must be serialized
|
|
// byte-identically in every request.
|
|
var systemPayloads = fixture.Client.ReceivedRequests
|
|
.Select(r => JsonSerializer.Serialize(r.Messages[0]))
|
|
.Distinct()
|
|
.ToList();
|
|
|
|
systemPayloads.Count.ShouldBe(1,
|
|
"der System-Prompt muss über alle Schritte hinweg identisch serialisiert werden:\n" +
|
|
string.Join("\n", systemPayloads));
|
|
|
|
systemPayloads[0].ShouldContain("cache_control");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Without_caching_the_request_contains_no_cache_control()
|
|
{
|
|
var fixture = new EngineFixture().WithTool(FakeTool.Returning("ok"));
|
|
var agent = fixture.AddAgent("agent-nocache", "TestTool");
|
|
agent.Model = "openai/gpt-4o";
|
|
agent.PromptCaching = "auto"; // so off for this model
|
|
|
|
fixture.Client.RespondsWithText("Fertig");
|
|
|
|
await fixture.Engine.ChatAsync(agent, "Los", "test-instance", default);
|
|
|
|
var json = JsonSerializer.Serialize(fixture.Client.ReceivedRequests[0].Messages);
|
|
json.ShouldNotContain("cache_control");
|
|
}
|
|
}
|