fix: probe Claude from the GUI app and close integration gaps

- A GUI-subsystem host has no console code page, so .NET decoded the
  probes' UTF-8 output with code page 0 and Claude's /usage separators
  never matched: Claude showed as unavailable in the app but not in the
  CLI. Probe output is now decoded as UTF-8 and every probe gets valid
  standard handles, with stdin closed unless it is the Codex RPC. A
  GUI-subsystem test host reproduces it.
- The /usage parser stops at the contributing section and only reports
  lines that start like a limit line.
- snapshot.json no longer defaults appPath to the reader's own path. The
  app passes its path to the engine and a CLI refresh keeps it, so a
  Stream Deck key always starts the app (D9, D10).
- The CLI is finaltoken-cli on every platform: FinalToken.exe and
  finaltoken.exe cannot share a folder on Windows.
- Day series follow the local calendar day, as Today does.
- The dev-only migration from the first store format is gone.
- README.md is a short user guide.

Refs #1
This commit is contained in:
Florian Schmidt
2026-10-14 20:39:53 +02:00
parent 183247bab5
commit 50ac2aad3a
30 changed files with 364 additions and 95 deletions
+76
View File
@@ -0,0 +1,76 @@
using System.Runtime.InteropServices;
using System.Text.Json;
using System.Text.Json.Serialization;
using FinalToken.Core;
using FinalToken.Core.Collectors;
using FinalToken.Core.Processes;
namespace FinalToken.ProcessHost;
public static class Program
{
public static async Task<int> Main(string[] args)
{
if (!OperatingSystem.IsWindows()) return 0;
if (args is ["--handles"])
return new[] { -10, -11, -12 }.All(id => Native.GetFileType(Native.GetStdHandle(id)) != 0) ? 0 : 17;
if (args is ["--utf8"])
{
await Console.OpenStandardOutput().WriteAsync(System.Text.Encoding.UTF8.GetBytes("{\"marker\":\"· •\"}"));
return 0;
}
if (args.Length < 2) return 2;
Native.FreeConsole();
foreach (var id in new[] { -10, -11, -12 }) Native.SetStdHandle(id, 0);
var report = new ProbeReport { HostHasConsole = Native.GetConsoleWindow() != 0,
HostInputValid = Native.GetFileType(Native.GetStdHandle(-10)) != 0, OutputCodePage = Console.OutputEncoding.CodePage };
var live = args[0] == "--claude";
var utf8 = args[0] == "--utf8-probe";
var cli = live ? CliDiscovery.Find(Provider.Claude).Command : new CliCommand(Environment.ProcessPath!, []);
if (cli is null) return 3;
var result = await new ProcessRunner().RunAsync(new(cli, live
? ["-p", "/usage", "--output-format", "json", "--no-session-persistence"] : utf8 ? ["--utf8"] : ["--handles"],
Path.GetDirectoryName(Path.GetFullPath(args[1]))!, TimeSpan.FromSeconds(60)), CancellationToken.None);
report = report with { ExitCode = result.ExitCode, TimedOut = result.TimedOut,
Utf8Matched = utf8 && result.Output == "{\"marker\":\"· •\"}" };
if (live)
{
var diagnostics = new DiagnosticLog();
var windows = ClaudeUsageParser.ParseEnvelope(result.Output, DateTimeOffset.UtcNow, diagnostics);
report = report with { WindowCount = windows.Count, DiagnosticCodes = diagnostics.Recent.Select(d => d.Code).ToArray() };
}
await AtomicFile.WriteAsync(args[1], JsonSerializer.Serialize(report, ProbeJsonContext.Default.ProbeReport));
return live ? report.WindowCount > 0 ? 0 : 18 : utf8 ? report.Utf8Matched ? 0 : 19 : result.ExitCode;
}
}
public sealed record ProbeReport
{
public bool HostHasConsole { get; init; }
public bool HostInputValid { get; init; }
public int ExitCode { get; init; }
public bool TimedOut { get; init; }
public int WindowCount { get; init; }
public int OutputCodePage { get; init; }
public bool Utf8Matched { get; init; }
public string[] DiagnosticCodes { get; init; } = [];
}
[JsonSerializable(typeof(ProbeReport))]
internal partial class ProbeJsonContext : JsonSerializerContext;
internal static partial class Native
{
[LibraryImport("kernel32.dll")]
internal static partial nint GetStdHandle(int id);
[LibraryImport("kernel32.dll")]
internal static partial uint GetFileType(nint handle);
[LibraryImport("kernel32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static partial bool SetStdHandle(int id, nint handle);
[LibraryImport("kernel32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static partial bool FreeConsole();
[LibraryImport("kernel32.dll")]
internal static partial nint GetConsoleWindow();
}