core: plan the daily budget per day, start forecasts at 2 % and follow the limit week (#15)
The developer's feedback of 2026-10-18 named four problems at the top of the panel: late forecasts, a daily budget that shrank with every use, unequal Claude and Codex tiles, and an unclear "This week". - Core: forecasts start at 2 % of a window (D7). Each day of a long instance has a share, an even pace and a budget. Today's budget starts from the use at the start of the day and stays put while today is spent. An estimated start-of-day use counts only the whole hours before the first sample's hour, so later samples and tokens do not move it. Week token totals follow the provider's limit week, with the calendar week as the named fallback; the snapshot carries `weekStart` and `weekWindow` (D9). - App: tiles of one window kind show the same rows, with muted placeholders that say why (D8). The stale warning sits in the Used row. Week charts draw even pace and budget as steps per day, mark the instance start and reset, and label full-day rates. Token tiles say since when. - CLI: prints which week it uses and since when. Closes #13
This commit was merged in pull request #15.
This commit is contained in:
@@ -146,6 +146,76 @@ public sealed class HeadlessTests
|
||||
model.Snapshot = model.Snapshot! with { MadeAt = clock.GetUtcNow() }; Dispatcher.UIThread.RunJobs();
|
||||
Assert.Equal(prior, viewport.Offset);
|
||||
}
|
||||
var budgetModel = FakeData.BudgetModel();
|
||||
var budgetSnapshot = budgetModel.Snapshot!;
|
||||
var budgetClock = new FixedClock(budgetSnapshot.MadeAt);
|
||||
var budgetPanel = new PanelWindow(budgetModel, () => { }, () => { }, () => { }, () => { }, budgetClock) { Width = 1280, Height = 1000 };
|
||||
budgetPanel.Show(); Dispatcher.UIThread.RunJobs();
|
||||
string[] Rows(Provider provider, string id = "week") => ((StackPanel)budgetPanel.GetVisualDescendants().OfType<Border>()
|
||||
.Single(b => b.Name == $"LimitTile-{provider}-{id}").Child!).Children.Select(c => c.Name!).ToArray();
|
||||
string[] longRows = ["Title", "Percent", "Used", "Pace", "Reset", "Forecast", "Recent", "Today", "Plan"];
|
||||
Assert.Equal(longRows, Rows(Provider.Claude)); Assert.Equal(longRows, Rows(Provider.Codex));
|
||||
Assert.Contains(budgetPanel.GetVisualDescendants().OfType<TextBlock>(), t => t.Text == "Limit week");
|
||||
Assert.Contains(budgetPanel.GetVisualDescendants().OfType<TextBlock>(), t => t.Text == "Calendar week");
|
||||
Assert.Contains(budgetPanel.GetVisualDescendants().OfType<TextBlock>(), t => t.Text == "since Mon 00:00");
|
||||
var tokenTiles = budgetPanel.GetVisualDescendants().OfType<StackPanel>().Where(p => p.Name?.StartsWith("TokenTile-", StringComparison.Ordinal) == true).ToArray();
|
||||
Assert.All(tokenTiles, tile => Assert.Equal(tokenTiles[0].Bounds.Height, tile.Bounds.Height));
|
||||
var limitTokens = tokenTiles.Single(t => t.Name == "TokenTile-Claude-Limit-week");
|
||||
Assert.Contains(limitTokens.GetVisualDescendants().OfType<TextBlock>(), t => t.Text == "since " + TimeZoneInfo.ConvertTime(budgetSnapshot.Providers[0].WeekStart, budgetClock.LocalTimeZone).ToString("ddd HH:mm"));
|
||||
Assert.Contains("Limit week", Assert.IsType<DataTooltip>(ToolTip.GetTip(limitTokens)).Title);
|
||||
Assert.Contains("since ", Assert.IsType<DataTooltip>(ToolTip.GetTip(limitTokens)).Title);
|
||||
foreach (var dayChart in budgetPanel.GetVisualDescendants().OfType<DailyLimitChart>())
|
||||
{
|
||||
Assert.Contains("from " + Display.Time(budgetSnapshot.Providers[0].WeekStart, budgetClock), dayChart.AxisLabels[0]);
|
||||
Assert.Contains("reset " + Display.Time(budgetSnapshot.Providers[0].WeekStart.AddDays(7), budgetClock), dayChart.AxisLabels[^1]);
|
||||
}
|
||||
var partialDay = budgetModel.History.Details[0].Days!.Current[0];
|
||||
var partialTip = DataTooltip.Day(partialDay);
|
||||
Assert.Contains(partialTip.Rows, r => r.Label == "Share");
|
||||
Assert.Contains(partialTip.Rows, r => r.Label == "Even pace");
|
||||
Assert.Contains(partialTip.Rows, r => r.Label == "Budget");
|
||||
foreach (var theme in new[] { ThemeVariant.Dark, ThemeVariant.Light })
|
||||
{
|
||||
budgetPanel.RequestedThemeVariant = theme;
|
||||
var viewport = budgetPanel.GetVisualDescendants().OfType<ScrollViewer>().First(); viewport.Offset = default;
|
||||
Save(budgetPanel, $"budget-1280x1000-{theme}-limits.png");
|
||||
foreach (var (name, target) in new[] { ("week", "Week use per day"), ("tokens", "Tokens") })
|
||||
{
|
||||
var heading = budgetPanel.GetVisualDescendants().OfType<TextBlock>().First(t => t.Text == target);
|
||||
viewport.Offset = new Vector(0, viewport.Offset.Y + heading.TranslatePoint(default, viewport)!.Value.Y - 16);
|
||||
Save(budgetPanel, $"budget-1280x1000-{theme}-{name}.png");
|
||||
}
|
||||
}
|
||||
budgetModel.Snapshot = budgetSnapshot with { Providers = budgetSnapshot.Providers.Select(p => p.Provider == Provider.Codex
|
||||
? p with { Windows = p.Windows.Select(w => w with { Window = w.Window with { SampleTime = budgetClock.GetUtcNow().AddMinutes(-15) } }).ToArray() }
|
||||
: p).ToArray() };
|
||||
Dispatcher.UIThread.RunJobs();
|
||||
Assert.Equal(longRows, Rows(Provider.Claude)); Assert.Equal(longRows, Rows(Provider.Codex));
|
||||
var staleWeek = budgetPanel.GetVisualDescendants().OfType<Border>().Single(b => b.Name == "LimitTile-Codex-week");
|
||||
var freshWeek = budgetPanel.GetVisualDescendants().OfType<Border>().Single(b => b.Name == "LimitTile-Claude-week");
|
||||
var staleUsed = staleWeek.GetVisualDescendants().OfType<TextBlock>().Single(t => t.Name == "Used");
|
||||
Assert.Equal("used · ⚠ sample 15 min old", staleUsed.Text);
|
||||
Assert.Equal(12, staleUsed.FontSize); Assert.Equal("#ff898781", staleUsed.Foreground?.ToString());
|
||||
Assert.Equal("used", freshWeek.GetVisualDescendants().OfType<TextBlock>().Single(t => t.Name == "Used").Text);
|
||||
budgetPanel.GetVisualDescendants().OfType<ScrollViewer>().First().Offset = default;
|
||||
SaveThemes(budgetPanel, "budget-stale-1280x1000");
|
||||
budgetModel.History = new([], [], [], [], []);
|
||||
budgetModel.Snapshot = budgetSnapshot with { Providers = budgetSnapshot.Providers.Select(p =>
|
||||
{
|
||||
var w = p.Windows[0].Window with { UsedPercent = 1, ResetsAt = p.Provider == Provider.Claude ? budgetClock.GetUtcNow().AddDays(7).AddHours(-1) : null };
|
||||
var shortWindow = w with { Id = "session", Label = "Session", Length = TimeSpan.FromHours(5),
|
||||
ResetsAt = p.Provider == Provider.Claude ? budgetClock.GetUtcNow().AddHours(5).AddMinutes(-1) : null };
|
||||
return p with { Windows = [new(w, Pace.Calculate(w, budgetClock.GetUtcNow())), new(shortWindow, Pace.Calculate(shortWindow, budgetClock.GetUtcNow()))] };
|
||||
}).ToArray() };
|
||||
Dispatcher.UIThread.RunJobs();
|
||||
Assert.Equal(longRows, Rows(Provider.Claude)); Assert.Equal(longRows, Rows(Provider.Codex));
|
||||
Assert.Equal(longRows.Take(7), Rows(Provider.Claude, "session"));
|
||||
Assert.Equal(Rows(Provider.Claude, "session"), Rows(Provider.Codex, "session"));
|
||||
Assert.Contains(budgetPanel.GetVisualDescendants().OfType<TextBlock>(), t => t.Text == "No forecast: pace unknown");
|
||||
Assert.Contains(budgetPanel.GetVisualDescendants().OfType<TextBlock>(), t => t.Text == "Recent rate needs 3 h of samples");
|
||||
Assert.Contains(budgetPanel.GetVisualDescendants().OfType<TextBlock>(), t => t.Text?.StartsWith("Recent rate from ", StringComparison.Ordinal) == true);
|
||||
Assert.Contains(budgetPanel.GetVisualDescendants().OfType<TextBlock>(), t => t.Text == "Today · collecting a day baseline");
|
||||
budgetPanel.Close();
|
||||
var forecastModel = FakeData.ForecastModel();
|
||||
var forecastClock = new FixedClock(forecastModel.Snapshot!.MadeAt);
|
||||
var forecastPanel = new PanelWindow(forecastModel, () => { }, () => { }, () => { }, () => { }, forecastClock);
|
||||
@@ -239,7 +309,7 @@ public sealed class HeadlessTests
|
||||
("future-day", future),
|
||||
("project", DataTooltip.Tokens("D:/Projects/Customer/Workspace/Project-with-a-long-name", new(100000000, 1900000000, 10000000, 53670717, 1234567, 2063670717))),
|
||||
("tile", (DataTooltip)ToolTip.GetTip(panel.GetVisualDescendants().OfType<Border>().First(b => b.Name == "LimitTile-Claude-week"))!),
|
||||
("tokens", DataTooltip.Tokens("Claude - This week", originalSnapshot.Providers[0].Week)),
|
||||
("tokens", DataTooltip.Tokens("Claude - Calendar week", originalSnapshot.Providers[0].Week)),
|
||||
};
|
||||
var limit = originalHistory.Limits.Single(l => l.Provider == Provider.Claude && l.Window!.Window.Id == "week");
|
||||
CaptureChartTip("burnup", new BurnUpChart(limit, clock), new Point(240, 100));
|
||||
|
||||
@@ -10,7 +10,7 @@ public sealed class ViewModelTests
|
||||
[InlineData(40, false)]
|
||||
public void HistoryForecastsUseTheSnapshotsConfiguredPace(double threshold, bool eligible)
|
||||
{
|
||||
var model = FakeData.ForecastModel(threshold);
|
||||
var model = FakeData.ForecastModel(threshold, beforeStart: true);
|
||||
Assert.Equal(eligible, model.History.Limits.Single(l => l.Window!.Window.Id == "primary").Forecast is not null);
|
||||
Assert.Equal(eligible, model.History.Details.Single(d => d.Id == "primary").Recent is not null);
|
||||
}
|
||||
@@ -166,12 +166,13 @@ public sealed class ViewModelTests
|
||||
|
||||
internal static class FakeData
|
||||
{
|
||||
public static PanelViewModel ForecastModel(double threshold = 10)
|
||||
public static PanelViewModel ForecastModel(double threshold = 10, bool beforeStart = false)
|
||||
{
|
||||
var now = new DateTimeOffset(2026, 10, 15, 16, 45, 0, TimeSpan.Zero);
|
||||
var ahead = new LimitWindow(Provider.Codex, "primary", "Week", 39,
|
||||
new(2026, 10, 22, 6, 18, 0, TimeSpan.Zero), TimeSpan.FromDays(7), now, "fixture");
|
||||
var early = ahead with { Id = "secondary", Label = "Week · early on track", UsedPercent = 1, ResetsAt = now.AddDays(7).AddHours(-1) };
|
||||
if (beforeStart) ahead = ahead with { Length = TimeSpan.FromDays(28), ResetsAt = now.AddDays(28).AddHours(-4) };
|
||||
var early = ahead with { Id = "secondary", Length = TimeSpan.FromDays(7), Label = "Week · early on track", UsedPercent = 1, ResetsAt = now.AddDays(7).AddHours(-1) };
|
||||
var store = new HistoryStore(Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N")), new DiagnosticLog(), new FixedClock(now));
|
||||
store.AddLimits([ahead with { SampleTime = now.AddHours(-4), UsedPercent = 20 }, ahead,
|
||||
early with { SampleTime = now.AddHours(-1), UsedPercent = 0 }, early]);
|
||||
@@ -190,6 +191,24 @@ internal static class FakeData
|
||||
};
|
||||
private static WindowSnapshot Window(Provider provider, string id, string label, double percent, DateTimeOffset now, int hours, int resetHours)
|
||||
{ var w = new LimitWindow(provider, id, label, percent, now.AddHours(resetHours), TimeSpan.FromHours(hours), now.AddMinutes(-3), "fixture"); return new(w, Pace.Calculate(w, now)); }
|
||||
public static PanelViewModel BudgetModel()
|
||||
{
|
||||
var now = new FixedClock().GetUtcNow();
|
||||
var clock = new FixedClock(now);
|
||||
var start = new DateTimeOffset(2026, 10, 10, 22, 40, 0, clock.LocalTimeZone.BaseUtcOffset);
|
||||
var store = new HistoryStore(Path.Combine(Path.GetTempPath(), "finaltoken-budget", Guid.NewGuid().ToString("N")), new DiagnosticLog(), clock);
|
||||
var snapshot = Snapshot(now);
|
||||
snapshot = snapshot with { Providers = snapshot.Providers.Select(p =>
|
||||
{
|
||||
var w = new LimitWindow(p.Provider, "week", "Week", p.Provider == Provider.Claude ? 40 : 32, start.AddDays(7), TimeSpan.FromDays(7), now, "fixture");
|
||||
store.AddLimits([w with { SampleTime = CalendarDays.Start(now, clock.LocalTimeZone), UsedPercent = 30 }, w]);
|
||||
for (var i = 0; i < 4; i++)
|
||||
store.Add(new(p.Provider, start.AddDays(i), "fixture", "Fixture/Project", new(Input: 100, Total: 100), $"{p.Provider}-{i}"));
|
||||
return p with { Windows = [new(w, Pace.Calculate(w, now))], Session = null,
|
||||
WeekStart = start, WeekWindow = p.Provider == Provider.Claude ? "week" : null };
|
||||
}).ToArray() };
|
||||
return new() { Snapshot = snapshot, History = PanelViewModel.QueryHistory(store, snapshot, HistoryRange.Days7, null, now) };
|
||||
}
|
||||
public static PanelViewModel EarlyResetModel()
|
||||
{
|
||||
var now = new FixedClock().GetUtcNow();
|
||||
|
||||
Reference in New Issue
Block a user