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,121 @@
using System.Diagnostics;
using System.Text.Json;
using FinalToken.Core.Collectors;
using Xunit;
namespace FinalToken.Core.Tests;
public class IntegrationTests
{
[Fact]
public async Task GuiHostProbeReceivesValidStandardHandlesWithoutConsole()
=> await RunGuiProbe("--handles-probe");
[Fact]
public async Task GuiHostDecodesUtf8ProviderJsonWithoutConsoleCodePage()
=> await RunGuiProbe("--utf8-probe");
private static async Task RunGuiProbe(string mode)
{
if (!OperatingSystem.IsWindows()) return;
using var directory = new TestDirectory();
var reportPath = Path.Combine(directory.Path, "report.json");
using var process = new Process { StartInfo = new()
{
FileName = Path.Combine(AppContext.BaseDirectory, "ProcessHost", "FinalToken.ProcessHost.exe"),
UseShellExecute = false, CreateNoWindow = true, WorkingDirectory = directory.Path
} };
process.StartInfo.ArgumentList.Add(mode);
process.StartInfo.ArgumentList.Add(reportPath);
process.Start();
try
{
await process.WaitForExitAsync(TestContext.Current.CancellationToken).WaitAsync(TimeSpan.FromSeconds(90), TestContext.Current.CancellationToken);
using var report = JsonDocument.Parse(File.ReadAllText(reportPath));
Assert.False(report.RootElement.GetProperty("HostHasConsole").GetBoolean());
Assert.False(report.RootElement.GetProperty("HostInputValid").GetBoolean());
Assert.Equal(0, process.ExitCode);
}
finally { if (!process.HasExited) process.Kill(true); }
}
[Fact]
public void UsageProseAndContributingSectionAreSilent()
{
const string text = """
You are currently using your subscription to power your Claude Code usage
Current session: 4% used · resets Oct 14, 11:30pm (Europe/Berlin)
Current week (all models): 67% used · resets Oct 18, 8pm (Europe/Berlin)
Current week (Fable): 0% used · resets Oct 18, 8pm (Europe/Berlin)
What's contributing to your limits usage?
Usage across Claude Code and Claude contributes to your limits.
Sessions and conversations share your subscription allowance.
Learn more about how usage is calculated.
Current example: this prose is not a limit window
""";
var log = new DiagnosticLog();
Assert.Equal(3, ClaudeUsageParser.ParseText(text, new(2026, 10, 14, 18, 0, 0, TimeSpan.Zero), log).Count);
Assert.Empty(log.Recent);
}
[Fact]
public async Task MissingSnapshotAppPathDoesNotDefaultToReadersExecutable()
{
using var directory = new TestDirectory();
directory.Write("snapshot.json", """{"v":1,"madeAt":"2026-10-14T16:00:00Z","refreshInterval":"00:01:00","providers":[]}""");
Assert.Null(SnapshotFile.Read(directory.Path).Snapshot!.AppPath);
await using var engine = new RefreshEngine(directory.Path, claudeDirectory: directory.Path, codexDirectory: directory.Path,
cliDiscovery: (_, _) => new(null, "Fixture CLI missing."));
Assert.Null((await engine.RefreshNowAsync(true, TestContext.Current.CancellationToken)).AppPath);
}
[Fact]
public async Task UnspecifiedHostAppPathPreservesExistingDesktopPath()
{
using var directory = new TestDirectory();
await SnapshotFile.WriteAsync(directory.Path, new Snapshot { AppPath = "desktop.exe", MadeAt = DateTimeOffset.UtcNow,
RefreshInterval = TimeSpan.FromMinutes(1), Providers = [] }, TestContext.Current.CancellationToken);
await using var engine = new RefreshEngine(directory.Path, claudeDirectory: directory.Path, codexDirectory: directory.Path,
cliDiscovery: (_, _) => new(null, "Fixture CLI missing."));
Assert.Equal("desktop.exe", (await engine.RefreshNowAsync(true, TestContext.Current.CancellationToken)).AppPath);
}
[Fact]
public void DaysFollowLocalCalendarInsteadOfUtcMidnight()
{
using var directory = new TestDirectory();
var store = new HistoryStore(directory.Path, new());
var first = new DateTimeOffset(2026, 10, 14, 23, 0, 0, TimeSpan.Zero);
store.Add(new(Provider.Codex, first, "model", "project", new(Total: 10), "one"));
store.Add(new(Provider.Codex, first.AddHours(1), "model", "project", new(Total: 20), "two"));
var zone = TimeZoneInfo.CreateCustomTimeZone("Fixture +02", TimeSpan.FromHours(2), "Fixture", "Fixture");
var day = Assert.Single(store.TokenSeries(Provider.Codex, first, first.AddHours(2), SeriesInterval.Day, zone));
Assert.Equal(30, day.Counts.Total);
Assert.Equal(new DateTimeOffset(2026, 10, 15, 0, 0, 0, TimeSpan.FromHours(2)), day.Time);
}
[Theory]
[InlineData("2026-03-29", 23)]
[InlineData("2026-10-25", 25)]
public void LocalDaySeriesIncludesEveryHourAcrossDst(string localDate, int hours)
{
using var directory = new TestDirectory();
var store = new HistoryStore(directory.Path, new());
var zone = TimeZoneInfo.FindSystemTimeZoneById("Europe/Berlin");
var date = DateTime.ParseExact(localDate, "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture);
var start = new DateTimeOffset(date, zone.GetUtcOffset(date));
var end = CalendarDays.NextStart(start, zone);
Assert.Equal(TimeSpan.FromHours(hours), end - start);
for (var i = 0; i < hours; i++)
store.Add(new(Provider.Codex, start.AddHours(i), "model", "project", new(Total: 1), "hour-" + i));
var day = Assert.Single(store.TokenSeries(Provider.Codex, start, end, SeriesInterval.Day, zone));
Assert.Equal(hours, day.Counts.Total);
Assert.Equal(start, day.Time);
Assert.Equal(hours, store.TokenSeries(Provider.Codex, start, end, SeriesInterval.Hour, zone).Count);
}
[Fact]
public async Task ExplicitDesktopPathOverridesPriorSnapshotPath()
{
using var directory = new TestDirectory();
await SnapshotFile.WriteAsync(directory.Path, new Snapshot { AppPath = "old.exe", MadeAt = DateTimeOffset.UtcNow,
RefreshInterval = TimeSpan.FromMinutes(1), Providers = [] }, TestContext.Current.CancellationToken);
await using var engine = new RefreshEngine(directory.Path, claudeDirectory: directory.Path, codexDirectory: directory.Path,
cliDiscovery: (_, _) => new(null, "Fixture CLI missing."), appPath: "desktop.exe");
Assert.Equal("desktop.exe", (await engine.RefreshNowAsync(true, TestContext.Current.CancellationToken)).AppPath);
}
}