feat(usage): split throttle thresholds per bucket, add draggable gauge markers

This commit is contained in:
mika kuns
2026-08-07 11:01:45 +02:00
parent 231b063751
commit 7eeb8f5086
33 changed files with 2289 additions and 147 deletions
@@ -100,8 +100,10 @@ public sealed class QueueStateMcpToolsTests : IDisposable
var repo = new AppSettingsRepository(ctx);
var settings = await repo.GetAsync();
settings.MaxParallelExecutions = maxParallel;
settings.UsageThrottleSoftPct = softPct;
settings.UsageThrottleHardPct = hardPct;
settings.UsageThrottleFiveHourSoftPct = softPct;
settings.UsageThrottleFiveHourHardPct = hardPct;
settings.UsageThrottleSevenDaySoftPct = softPct;
settings.UsageThrottleSevenDayHardPct = hardPct;
await repo.UpdateAsync(settings);
}
@@ -81,8 +81,10 @@ public sealed class QueueServiceTests : IDisposable
var repo = new AppSettingsRepository(ctx);
var settings = await repo.GetAsync();
settings.MaxParallelExecutions = maxParallel;
settings.UsageThrottleSoftPct = softPct;
settings.UsageThrottleHardPct = hardPct;
settings.UsageThrottleFiveHourSoftPct = softPct;
settings.UsageThrottleFiveHourHardPct = hardPct;
settings.UsageThrottleSevenDaySoftPct = softPct;
settings.UsageThrottleSevenDayHardPct = hardPct;
settings.UsageGateFiveHourPct = gateFive;
settings.UsageGateSevenDayPct = gateSeven;
await repo.UpdateAsync(settings);
@@ -139,6 +139,35 @@ public class TranscriptUsageReaderTests : IDisposable
Assert.Equal(1, row.Messages);
}
[Fact]
public async Task Files_Last_Written_Before_The_Window_Are_Not_Read()
{
// Deliberate heuristic: a transcript whose mtime predates the window cannot contain a
// record inside it, so it is skipped unread. Here the content would match the window —
// proving the file was never opened, which is what keeps a 7-day range off the full history.
var path = WriteSession("proj", "old.jsonl",
AssistantLine(@"C:\Dev\App", "2026-06-02T08:00:00Z", "claude-sonnet-5", 5, 5, 0, 0));
File.SetLastWriteTime(path, new DateTime(2026, 5, 1, 12, 0, 0));
var reader = MakeReader();
var result = await reader.ReadAsync(new DateOnly(2026, 6, 1), new DateOnly(2026, 6, 3));
Assert.Empty(result);
}
[Fact]
public async Task File_Written_On_The_Window_Start_Day_Is_Still_Read()
{
var path = WriteSession("proj", "edge.jsonl",
AssistantLine(@"C:\Dev\App", "2026-06-01T08:00:00Z", "claude-sonnet-5", 5, 5, 0, 0));
File.SetLastWriteTime(path, new DateTime(2026, 6, 1, 0, 5, 0));
var reader = MakeReader();
var result = await reader.ReadAsync(new DateOnly(2026, 6, 1), new DateOnly(2026, 6, 3));
Assert.Single(result);
}
[Fact]
public async Task Malformed_Line_Does_Not_Abort_The_Run()
{
@@ -149,11 +149,61 @@ public sealed class UsageSnapshotBuilderTests : IDisposable
var repo = new AppSettingsRepository(ctx);
var settings = await repo.GetAsync();
settings.MaxParallelExecutions = maxParallel;
settings.UsageThrottleSoftPct = softPct;
settings.UsageThrottleHardPct = hardPct;
settings.UsageThrottleFiveHourSoftPct = softPct;
settings.UsageThrottleFiveHourHardPct = hardPct;
settings.UsageThrottleSevenDaySoftPct = softPct;
settings.UsageThrottleSevenDayHardPct = hardPct;
await repo.UpdateAsync(settings);
}
private async Task SetPerBucketThrottleAsync(
int maxParallel, int fiveSoft, int fiveHard, int sevenSoft, int sevenHard)
{
using var ctx = _db.CreateContext();
var repo = new AppSettingsRepository(ctx);
var settings = await repo.GetAsync();
settings.MaxParallelExecutions = maxParallel;
settings.UsageThrottleFiveHourSoftPct = fiveSoft;
settings.UsageThrottleFiveHourHardPct = fiveHard;
settings.UsageThrottleSevenDaySoftPct = sevenSoft;
settings.UsageThrottleSevenDayHardPct = sevenHard;
await repo.UpdateAsync(settings);
}
[Fact]
public async Task Per_bucket_throttle_stages_are_reported_for_the_gauges()
{
await SetThresholdsAsync(80, 90);
await SetPerBucketThrottleAsync(maxParallel: 3, fiveSoft: 45, fiveHard: 60, sevenSoft: 70, sevenHard: 85);
var state = new UsageState();
state.ReportSuccess(new UsageSnapshot(
new UsageBucket(10, null), new UsageBucket(10, null), Array.Empty<UsageLimitRow>(), DateTime.UtcNow));
var dto = await CreateBuilder(state, new UsageGateDecision(false, null)).BuildAsync();
Assert.Equal(45, dto.ThrottleFiveHourSoftPct);
Assert.Equal(60, dto.ThrottleFiveHourHardPct);
Assert.Equal(70, dto.ThrottleSevenDaySoftPct);
Assert.Equal(85, dto.ThrottleSevenDayHardPct);
}
[Fact]
public async Task Per_bucket_stages_apply_independently_to_effective_slots()
{
await SetThresholdsAsync(80, 90);
// Both buckets sit at 60%: past the 5h soft stage (45) but below every 7d stage (70/85).
await SetPerBucketThrottleAsync(maxParallel: 3, fiveSoft: 45, fiveHard: 90, sevenSoft: 70, sevenHard: 85);
var state = new UsageState();
state.ReportSuccess(new UsageSnapshot(
new UsageBucket(60, null), new UsageBucket(60, null), Array.Empty<UsageLimitRow>(), DateTime.UtcNow));
var dto = await CreateBuilder(state, new UsageGateDecision(false, null)).BuildAsync();
Assert.Equal(2, dto.EffectiveSlots);
}
[Fact]
public async Task Throttled_slots_and_decisive_bucket_reported()
{
@@ -4,13 +4,11 @@ namespace ClaudeDo.Worker.Tests.Usage;
public sealed class UsageThrottleTests
{
private const int Soft = 50;
private const int Hard = 65;
private const int GateFive = 80;
private const int GateSeven = 90;
private static readonly UsageThresholds FiveHour = new(SoftPct: 50, HardPct: 65, GatePct: 80);
private static readonly UsageThresholds SevenDay = new(SoftPct: 50, HardPct: 65, GatePct: 90);
private static int Effective(double? five, double? seven, int configured = 3) =>
UsageThrottle.EffectiveSlots(configured, five, seven, Soft, Hard, GateFive, GateSeven);
UsageThrottle.EffectiveSlots(configured, five, FiveHour, seven, SevenDay);
[Fact]
public void BelowSoftThreshold_ReturnsFullConfiguredSlots()
@@ -94,13 +92,19 @@ public sealed class UsageThrottleTests
[Fact]
public void ZeroSoftAndHardThresholds_NeverThrottleBelowGate()
{
Assert.Equal(3, UsageThrottle.EffectiveSlots(3, 79, 89, softPct: 0, hardPct: 0, gateFiveHourPct: GateFive, gateSevenDayPct: GateSeven));
var five = new UsageThresholds(0, 0, 80);
var seven = new UsageThresholds(0, 0, 90);
Assert.Equal(3, UsageThrottle.EffectiveSlots(3, 79, five, 89, seven));
}
[Fact]
public void ZeroGateThresholds_NeverHardBlock()
{
Assert.Equal(1, UsageThrottle.EffectiveSlots(3, 99, 99, softPct: Soft, hardPct: Hard, gateFiveHourPct: 0, gateSevenDayPct: 0));
var five = new UsageThresholds(50, 65, 0);
var seven = new UsageThresholds(50, 65, 0);
Assert.Equal(1, UsageThrottle.EffectiveSlots(3, 99, five, 99, seven));
}
[Fact]
@@ -108,4 +112,46 @@ public sealed class UsageThrottleTests
{
Assert.Equal(1, Effective(10, 10, configured: 0));
}
// ── Per-bucket thresholds are independent ───────────────────────────────
[Fact]
public void PerBucket_SameUtilization_DifferentStagesPerBucket()
{
// 60% is past the 5h soft (50) but still under the 7d soft (70): the 5h bucket decides.
var five = new UsageThresholds(50, 65, 80);
var seven = new UsageThresholds(70, 85, 90);
Assert.Equal(2, UsageThrottle.EffectiveSlots(3, 60, five, 60, seven));
}
[Fact]
public void PerBucket_LessUtilizedBucketCanStillBeTheStricterOne()
{
// 7d sits lower (40%) but has the tighter thresholds, so it — not the busier 5h — throttles.
var five = new UsageThresholds(90, 95, 99);
var seven = new UsageThresholds(20, 35, 90);
Assert.Equal(1, UsageThrottle.EffectiveSlots(3, 80, five, 40, seven));
}
[Fact]
public void PerBucket_StrictestStageWins()
{
// 5h is only in its soft stage (2 slots), 7d is past its hard stage (1 slot) → 1 wins.
var five = new UsageThresholds(50, 65, 80);
var seven = new UsageThresholds(30, 40, 90);
Assert.Equal(1, UsageThrottle.EffectiveSlots(3, 55, five, 45, seven));
}
[Fact]
public void PerBucket_MissingBucketNeverThrottles()
{
// No 7d reading at all: only the 5h bucket may step parallelism down.
var five = new UsageThresholds(50, 65, 80);
var seven = new UsageThresholds(1, 2, 3);
Assert.Equal(2, UsageThrottle.EffectiveSlots(3, 55, five, null, seven));
}
}