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
This commit is contained in:
Florian Schmidt
2026-11-15 11:14:52 +01:00
parent b23b7289c7
commit 583f920307
3 changed files with 27 additions and 3 deletions
+1 -1
View File
@@ -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] 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] 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] 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. - [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.
+2 -2
View File
@@ -56,8 +56,8 @@ public static class FleetAnalysis
List<ModelTokenPoint> tokens = reconciled.Select(u => new ModelTokenPoint(Hour(u.Time), u.Model, u.Counts)).ToList(); List<ModelTokenPoint> tokens = reconciled.Select(u => new ModelTokenPoint(Hour(u.Time), u.Model, u.Counts)).ToList();
if (account.HistoryBefore is { } before) if (account.HistoryBefore is { } before)
{ {
var legacy = profiles.SingleOrDefault(p => p.Machine == account.LegacyMachine && p.Profile.Id == account.LegacyProfile).Profile; 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 has not reported."); if (legacy is null) warnings.Add("Designated legacy history source is unavailable or unverified.");
else tokens.AddRange(legacy.LegacyTokens.Where(t => t.Time < before)); 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."); warnings.Add("Before the migration cutoff, history uses one designated machine; cross-machine attribution is unavailable.");
} }
@@ -116,6 +116,30 @@ public sealed class FleetTests
var b = Profile([raw]) with { LegacyTokens = [new(cutoff.AddHours(-1), null, new(Total: 200))] }; var b = Profile([raw]) with { LegacyTokens = [new(cutoff.AddHours(-1), null, new(Total: 200))] };
Assert.Equal(107, Analyze(a,b,cfg).Total.Total); Assert.Equal(107, Analyze(a,b,cfg).Total.Total);
} }
[Theory]
[InlineData("[email protected]", Provider.Claude)]
[InlineData(null, Provider.Claude)]
[InlineData("[email protected]", 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() [Fact] public void DashboardJsonRoundTripsWithoutReflection()
{ {
var view = FleetAnalysis.Build(Config(),new([Machine("a",Profile(limits:[Week()]))]),Now); var view = FleetAnalysis.Build(Config(),new([Machine("a",Profile(limits:[Week()]))]),Now);