feat: add FinalToken v1 for Claude and Codex usage (#2)

FinalToken v1: a NativeAOT tray app for Windows 11 and CachyOS that tracks
Claude Code and Codex usage and limits through their CLIs only, with no
provider sign-in and no tokens (R1-R7, D1-D11).

- Core: incremental collectors for Claude transcripts and Codex
  rollouts, limit probes through `claude -p /usage` and
  `codex app-server`, a journaled history store, the refresh engine,
  pace and popup decisions, and the snapshot.json contract.
- App (Avalonia): stats panel with limit bars, pace, projections, token
  totals, charts, limit history and breakdowns; tray, settings,
  non-activating pace popups, single-instance activation, start with
  the OS, and the Off/Ask/Automatic Stream Deck plugin install.
- Stream Deck plugin (SDK v2, NativeAOT) for Elgato and OpenDeck.
- finaltoken-cli, tools/publish.py, the documentation check and the
  agent review workflow.

Closes #1
This commit was merged in pull request #2.
This commit is contained in:
2026-10-14 22:11:19 +02:00
parent 6d67f157a5
commit f8afd11679
106 changed files with 6583 additions and 0 deletions
@@ -0,0 +1,89 @@
using FinalToken.Core;
using Xunit;
namespace FinalToken.App.Tests;
public sealed class ViewModelTests
{
[Theory]
[InlineData(HistoryRange.Hours24, 1)]
[InlineData(HistoryRange.Days7, 7)]
[InlineData(HistoryRange.Days30, 30)]
[InlineData(HistoryRange.Days90, 90)]
public void RangeUsesRequestedDuration(HistoryRange range, int days)
{ var now = DateTimeOffset.UtcNow; Assert.Equal(now.AddDays(-days), PanelViewModel.RangeStart(range, now)); }
[Fact] public void AllRangeIncludesOldHistory() => Assert.Equal(DateTimeOffset.MinValue, PanelViewModel.RangeStart(HistoryRange.All, DateTimeOffset.UtcNow));
[Fact] public void UnknownSessionIsNotDisplayedAsZero() => Assert.Contains("Unavailable", Display.Tokens(null));
[Fact] public void PropertiesNotify()
{
var vm = new PanelViewModel(); var notified = new List<string?>(); vm.PropertyChanged += (_, e) => notified.Add(e.PropertyName);
vm.ProviderFilter = Provider.Codex; vm.Range = HistoryRange.Days30;
Assert.Contains(nameof(vm.ProviderFilter), notified); Assert.Contains(nameof(vm.Range), notified);
}
[Fact] public void HistoryFiltersProvidersAndGroupsModelsWithoutDoubleCounting()
{
var directory = Path.Combine(Path.GetTempPath(), "finaltoken-app-tests", Guid.NewGuid().ToString("N"));
var store = new HistoryStore(directory, new DiagnosticLog()); var now = DateTimeOffset.UtcNow;
store.Add(new(Provider.Claude, now.AddHours(-2), "a", "project", new(Total: 30), "1"));
store.Add(new(Provider.Claude, now.AddHours(-2), "b", "project", new(Total: 70), "2"));
store.Add(new(Provider.Codex, now.AddHours(-2), "b", "other", new(Total: 100), "3"));
var snapshot = FakeData.Snapshot(now);
var all = PanelViewModel.QueryHistory(store, snapshot, HistoryRange.Days7, null, now);
Assert.Equal(200, all.Hourly.Sum(p => p.Value)); Assert.Equal(200, all.Daily.Sum(p => p.Value));
Assert.Equal(1, all.Models.Sum(m => m.Share), 6);
var filtered = PanelViewModel.QueryHistory(store, snapshot, HistoryRange.Days7, Provider.Claude, now);
Assert.Equal(100, filtered.Hourly.Sum(p => p.Value)); Assert.Equal(2, filtered.Hourly.Count);
Assert.Single(filtered.Projects); Assert.Equal(1, filtered.Projects[0].Share);
}
[Fact] public void EmptyHistoryHasNoInventedData()
{
var store = new HistoryStore(Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N")), new DiagnosticLog());
var history = PanelViewModel.QueryHistory(store, FakeData.Snapshot(DateTimeOffset.UtcNow), HistoryRange.All, null, DateTimeOffset.UtcNow);
Assert.Empty(history.Hourly); Assert.Empty(history.Models); Assert.Empty(history.Projects);
}
[Fact] public void SelectedProviderDailySeriesRespectsPartialDays()
{
var now = new DateTimeOffset(2026, 10, 14, 12, 0, 0, TimeSpan.Zero);
var store = new HistoryStore(Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N")), new DiagnosticLog());
store.Add(new(Provider.Claude, now.AddHours(-26), "outside", "p", new(Total: 300), "outside"));
store.Add(new(Provider.Claude, now.AddHours(-22), "inside", "p", new(Total: 100), "inside"));
store.Add(new(Provider.Claude, now.AddHours(-3), "inside", "p", new(Total: 50), "today"));
var view = PanelViewModel.QueryHistory(store, FakeData.Snapshot(now), HistoryRange.Hours24, Provider.Claude, now);
Assert.Equal(150, view.Hourly.Sum(v => v.Value));
Assert.Equal(150, view.Daily.Sum(v => v.Value));
Assert.DoesNotContain(view.Daily, v => v.Series == "outside");
}
[Fact] public void DesktopEntryQuotesExecutableAndEscapesFieldCodes()
{ var text = OsStartup.DesktopEntry("/opt/Final Token/100%/app"); Assert.Contains("Exec=\"/opt/Final Token/100%%/app\"", text); Assert.Contains("Terminal=false", text); }
[Fact] public void DesktopEntryRejectsInjectedLine() => Assert.Throws<ArgumentException>(() => OsStartup.DesktopEntry("/app\nExec=other"));
[Fact] public void DesktopEntryEscapesCommandCharactersAtBothParsingLevels()
{
var entry = OsStartup.DesktopEntry("/opt/a\"$`\\file");
Assert.Contains("a\\\\\"\\\\$\\\\`\\\\\\\\file", entry);
}
}
internal static class FakeData
{
public static Snapshot Snapshot(DateTimeOffset now, bool unavailable = false) => new()
{
MadeAt = now, RefreshInterval = TimeSpan.FromSeconds(60), Providers =
[new(Provider.Claude, ProviderStatus.Available, null, [Window(Provider.Claude, "session", "Current session", 68, now, 5, 3), Window(Provider.Claude, "week", "Current week", 32, now, 168, 48)],
new(12000, 18000, 2000, 4200, 0, 36200), new(4000, 6000, 1000, 1600, 0, 12600), new(85000, 92000, 16000, 28000, 0, 221000)),
new(Provider.Codex, unavailable ? ProviderStatus.Unavailable : ProviderStatus.Available, unavailable ? "CLI is not installed. Token history remains available." : null,
[Window(Provider.Codex, "primary", "Primary · 5 hours", 12, now, 5, 1), Window(Provider.Codex, "secondary", "Secondary · 7 days", 62, now, 168, 80)],
new(72000, 42000, 0, 8400, 2100, 80400), new(23000, 10000, 0, 3200, 700, 26200), new(340000, 150000, 0, 38000, 11000, 378000))]
};
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 Model(bool unavailable = false)
{
var now = DateTimeOffset.UtcNow;
var hourly = Enumerable.Range(0, 24).SelectMany(i => new[] { new ChartValue(now.AddHours(-24 + i), "Claude", 1000 + i * 210), new ChartValue(now.AddHours(-24 + i), "Codex", 2000 + (i % 5) * 800) }).ToArray();
var daily = Enumerable.Range(0, 7).SelectMany(i => new[] { new ChartValue(now.AddDays(-7 + i), "Claude", 20000 + i * 2000), new ChartValue(now.AddDays(-7 + i), "Codex", 30000 + i * 5000) }).ToArray();
return new() { Snapshot = Snapshot(now, unavailable), Message = "Updated just now · local CLI data", History = new(hourly, daily,
[new("Claude · Current session", Enumerable.Range(0, 8).Select(i => new ChartValue(now.AddMinutes(-140 + i * 20), "session", i * 8 + 12, i * 6)).ToArray())],
[new("claude-sonnet", new(Total: 221000), 0.37), new("gpt-6.1-sol", new(Total: 378000), 0.63)], [new("FinalToken", new(Total: 599000), 1)]),
Diagnostics = [new(now.AddMinutes(-4), Provider.Claude, "sample-age", "Latest limit sample is three minutes old.")] };
}
}