agent-review agent review: fail
The budget was the rest of the week over the time left, so each use today lowered it and a partial first day was held to a full day. Now each day of the instance has its share, its even pace and its budget. Today's budget starts from the use at the day start and does not move with today's use (D7). Forecasts start at 2 % of a window, so an early reset no longer hides them for most of a day. Limit tiles of one kind show the same rows, with placeholders that say why a value is missing. The week chart draws per-day steps and marks the instance start and reset (D8). Week token totals follow the provider's limit week and say since when, with the calendar week as fallback (D9). Refs #13
63 lines
3.5 KiB
C#
63 lines
3.5 KiB
C#
namespace FinalToken.Core;
|
|
|
|
public static class Pace
|
|
{
|
|
// D7: use ticks at the boundary so exactly 2% is eligible despite floating-point rounding.
|
|
public static DateTimeOffset? ForecastStartsAt(LimitWindow window) =>
|
|
window.Length is { Ticks: > 0 } length && window.ResetsAt is { } reset
|
|
? reset - TimeSpan.FromTicks(length.Ticks - length.Ticks / 50) : null;
|
|
|
|
public static bool CanForecast(LimitWindow window, DateTimeOffset now, double threshold = 10) =>
|
|
CanForecast(window, now, Calculate(window, now, threshold));
|
|
|
|
// Reuse the configured pace, so every forecast agrees with the chip's Ahead state.
|
|
public static bool CanForecast(LimitWindow window, DateTimeOffset now, PaceResult pace) =>
|
|
pace.State != PaceState.Unknown && pace.ElapsedFraction is > 0 &&
|
|
ForecastStartsAt(window) is { } start && (now >= start || pace.State == PaceState.Ahead);
|
|
|
|
// D7. A reset in the past clamps elapsed to one; it never has a future projection.
|
|
public static PaceResult Calculate(LimitWindow window, DateTimeOffset now, double threshold = 10)
|
|
{
|
|
if (window.Length is not { Ticks: > 0 } length || window.ResetsAt is not { } reset ||
|
|
!double.IsFinite(window.UsedPercent) || window.UsedPercent is < 0 or > 100 ||
|
|
!double.IsFinite(threshold) || threshold <= 0)
|
|
return new(PaceState.Unknown);
|
|
var elapsed = Math.Clamp(1 - (reset - now).TotalSeconds / length.TotalSeconds, 0, 1);
|
|
var delta = window.UsedPercent - 100 * elapsed;
|
|
var state = delta >= threshold ? PaceState.Ahead :
|
|
delta <= -threshold && elapsed >= 0.5 ? PaceState.Behind : PaceState.OnTrack;
|
|
DateTimeOffset? projection = null;
|
|
if (delta > 0 && window.UsedPercent > 0 && CanForecast(window, now, new PaceResult(state, elapsed, delta)))
|
|
{
|
|
var seconds = (100 - window.UsedPercent) * elapsed * length.TotalSeconds / window.UsedPercent;
|
|
if (seconds >= 0 && seconds < (reset - now).TotalSeconds) projection = now.AddSeconds(seconds);
|
|
}
|
|
return new(state, elapsed, delta, projection);
|
|
}
|
|
}
|
|
|
|
/// <summary>D8: retains an alert until it clears its threshold by two points.
|
|
/// Observe is deterministic and has no side effects beyond its own state.</summary>
|
|
public sealed class PopupDecider
|
|
{
|
|
private readonly Dictionary<(Provider, string), PaceState> states = [];
|
|
public PopupEvent? Observe(LimitWindow window, PaceResult pace, Settings settings)
|
|
{
|
|
var key = (window.Provider, window.Id);
|
|
var previous = states.GetValueOrDefault(key, PaceState.Unknown);
|
|
var next = pace.State;
|
|
if (pace.Delta is { } delta && pace.State != PaceState.Unknown)
|
|
{
|
|
if (previous == PaceState.Ahead && delta > settings.PaceThreshold - 2) next = PaceState.Ahead;
|
|
if (previous == PaceState.Behind && delta < -settings.PaceThreshold + 2 && pace.ElapsedFraction >= 0.5)
|
|
next = PaceState.Behind;
|
|
}
|
|
states[key] = next;
|
|
if (previous == next || next is not (PaceState.Ahead or PaceState.Behind) ||
|
|
!settings.For(window.Provider).Enabled || !settings.For(window.Provider).PopupsEnabled ||
|
|
(next == PaceState.Ahead ? !settings.PopupAhead || !settings.For(window.Provider).PopupAhead :
|
|
!settings.PopupBehind || !settings.For(window.Provider).PopupBehind)) return null;
|
|
return new(window.Provider, window.Id, window.Label, next, pace.Delta!.Value, pace.Projection);
|
|
}
|
|
}
|