From 583f9203077cd7e3ff73f9ad6a74b8c828c35a12 Mon Sep 17 00:00:00 2001 From: Florian Schmidt Date: Sun, 15 Nov 2026 11:14:52 +0100 Subject: [PATCH] core: require account attribution for legacy imports Apply provider and historical account identity checks to the designated hourly import source. Retain previously attributed history when a CLI signs out, while excluding mismatched or unknown accounts. Refs #19 --- .agents/mind/architecture.md | 2 +- src/FinalToken.Fleet/FleetAnalysis.cs | 4 ++-- tests/FinalToken.Fleet.Tests/FleetTests.cs | 24 ++++++++++++++++++++++ 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/.agents/mind/architecture.md b/.agents/mind/architecture.md index bc054fc..f81929b 100644 --- a/.agents/mind/architecture.md +++ b/.agents/mind/architecture.md @@ -2,7 +2,7 @@ - [2026-11-14] D14's Fleet library reuses Core Pace/LimitAnalysis/ApiPrices; the web host and outbound worker add no second forecast implementation. Working-time schedules remain outside D7. - [2026-11-14] Quota is one newest observation per account/window, not a sum. Source ids dedupe consumption across machines; local/session figures include shared copies and cannot be summed as machine-exclusive use. -- [2026-11-14] Legacy hourly history needs one source and UTC-hour cutoff. Reconcile Codex increases before clipping; without a pre-cutoff baseline, omit the first cumulative total and warn rather than potentially counting imported use twice. +- [2026-11-15] Legacy hourly history needs one account-attributed source and UTC-hour cutoff; provider and DataEmail checks match raw usage even after rebinding. Reconcile Codex increases before clipping; without a pre-cutoff baseline, omit the first cumulative total and warn rather than counting imported use twice. - [2026-11-14] LoginCoordinator is memory-only and binds browser session/machine/profile/worker boot/deadline. CLI identity verification is separate from process success; never serialize login URLs, codes or authorization responses into fleet state. - [2026-11-14] Persist worker export metadata before cursor advancement. Server merges acknowledged source records so an empty restart heartbeat cannot erase history. Current login health and historical data attribution are separate fields. diff --git a/src/FinalToken.Fleet/FleetAnalysis.cs b/src/FinalToken.Fleet/FleetAnalysis.cs index ceba39d..939a306 100644 --- a/src/FinalToken.Fleet/FleetAnalysis.cs +++ b/src/FinalToken.Fleet/FleetAnalysis.cs @@ -56,8 +56,8 @@ public static class FleetAnalysis List tokens = reconciled.Select(u => new ModelTokenPoint(Hour(u.Time), u.Model, u.Counts)).ToList(); if (account.HistoryBefore is { } before) { - var legacy = profiles.SingleOrDefault(p => p.Machine == account.LegacyMachine && p.Profile.Id == account.LegacyProfile).Profile; - if (legacy is null) warnings.Add("Designated legacy history source has not reported."); + var legacy = verified.SingleOrDefault(p => p.Machine == account.LegacyMachine && p.Profile.Id == account.LegacyProfile).Profile; + if (legacy is null) warnings.Add("Designated legacy history source is unavailable or unverified."); else tokens.AddRange(legacy.LegacyTokens.Where(t => t.Time < before)); warnings.Add("Before the migration cutoff, history uses one designated machine; cross-machine attribution is unavailable."); } diff --git a/tests/FinalToken.Fleet.Tests/FleetTests.cs b/tests/FinalToken.Fleet.Tests/FleetTests.cs index 454395b..6bc01dd 100644 --- a/tests/FinalToken.Fleet.Tests/FleetTests.cs +++ b/tests/FinalToken.Fleet.Tests/FleetTests.cs @@ -116,6 +116,30 @@ public sealed class FleetTests var b = Profile([raw]) with { LegacyTokens = [new(cutoff.AddHours(-1), null, new(Total: 200))] }; Assert.Equal(107, Analyze(a,b,cfg).Total.Total); } + [Theory] + [InlineData("other@example.test", Provider.Claude)] + [InlineData(null, Provider.Claude)] + [InlineData("user@example.test", Provider.Codex)] + public void LegacyHistoryMustBelongToTheConfiguredAccount(string? email, Provider provider) + { + var cutoff = Now.AddDays(-1); var cfg = Config(); + cfg = cfg with { Accounts = [cfg.Accounts[0] with { LegacyMachine = "a", LegacyProfile = "main", HistoryBefore = cutoff }] }; + var p = Profile(provider: provider) with { DataEmail = email, + LegacyTokens = [new(cutoff.AddHours(-1), "claude-opus-4-6", new(Input: 100, Total: 100))] }; + var view = Analyze(p, config: cfg); + Assert.Equal(0, view.Total.Total); Assert.Empty(view.Tokens); Assert.Null(view.ApiValue); + Assert.False(view.HasConsumptionData); + Assert.Contains(view.Warnings, w => w.Contains("legacy", StringComparison.OrdinalIgnoreCase) && + w.Contains("unverified", StringComparison.OrdinalIgnoreCase)); + } + [Fact] public void LoggedOutLegacySourceRetainsPreviouslyVerifiedAttribution() + { + var cutoff = Now.AddDays(-1); var cfg = Config(); + cfg = cfg with { Accounts = [cfg.Accounts[0] with { LegacyMachine = "a", LegacyProfile = "main", HistoryBefore = cutoff }] }; + var p = Profile() with { Auth = new("Unavailable", null, Now, "Signed out"), + LegacyTokens = [new(cutoff.AddHours(-1), null, new(Total: 100))] }; + Assert.Equal(100, Analyze(p, config: cfg).Total.Total); + } [Fact] public void DashboardJsonRoundTripsWithoutReflection() { var view = FleetAnalysis.Build(Config(),new([Machine("a",Profile(limits:[Week()]))]),Now);