core: add self-hosted weekly dashboard and machine workers
Reuse the existing weekly pace, reset history and daily budgets across multiple machines without multiplying shared account quota. Coordinate profile-bound official CLI logins through transient authorization responses while keeping provider credentials local to each CLI. Release as version 1.2.0. Refs #19
This commit is contained in:
@@ -0,0 +1,100 @@
|
||||
namespace FinalToken.Fleet;
|
||||
|
||||
/// <summary>D14: short-lived routing state only. Never serialized or logged.</summary>
|
||||
public sealed class LoginCoordinator(ServerConfig config)
|
||||
{
|
||||
private sealed class Pending(string id, string owner, string machine, string profile, string boot, DateTimeOffset expires)
|
||||
{
|
||||
public readonly string Id = id, Owner = owner, Machine = machine, Profile = profile, Boot = boot;
|
||||
public readonly DateTimeOffset Expires = expires;
|
||||
public string State = "Queued";
|
||||
public string? Url, DeviceCode, Email, Response;
|
||||
public bool InputAccepted;
|
||||
public LoginView View => new(Id, Machine, Profile, State, Expires, Url, DeviceCode, Email);
|
||||
}
|
||||
private readonly Lock gate = new();
|
||||
private readonly Dictionary<string, Pending> pending = [];
|
||||
private static bool Terminal(string state) => state is "Succeeded" or "Failed" or "Cancelled" or "TimedOut" or "AccountMismatch" or "IdentityUnknown" or "WorkerRestarted";
|
||||
public LoginView Start(string owner, string machine, string profile, FleetState fleet, DateTimeOffset now)
|
||||
{
|
||||
lock (gate)
|
||||
{
|
||||
Expire(now);
|
||||
var definition = config.Machines.SingleOrDefault(m => m.Id == machine);
|
||||
if (definition is null || !definition.Profiles.Any(p => p.Id == profile)) throw new InvalidDataException("Unknown machine/profile.");
|
||||
var saved = fleet.Machines.SingleOrDefault(m => m.MachineId == machine);
|
||||
if (saved is null || now - saved.SeenAt > TimeSpan.FromSeconds(90)) throw new InvalidDataException("Machine is offline.");
|
||||
if (pending.Values.Any(p => p.Machine == machine && p.Profile == profile && !Terminal(p.State)))
|
||||
throw new InvalidDataException("A login is already pending for this profile.");
|
||||
if (pending.Count >= 100) throw new InvalidDataException("Too many login attempts; wait for expiry.");
|
||||
var id = Guid.NewGuid().ToString("N");
|
||||
var entry = new Pending(id, owner, machine, profile, saved.Report.BootId, now.AddMinutes(15));
|
||||
pending.Add(id, entry); return entry.View;
|
||||
}
|
||||
}
|
||||
public LoginView Get(string id, string owner, DateTimeOffset now)
|
||||
{
|
||||
lock (gate) { Expire(now); return Owned(id, owner).View; }
|
||||
}
|
||||
public void Respond(string id, string owner, string response, DateTimeOffset now)
|
||||
{
|
||||
lock (gate)
|
||||
{
|
||||
Expire(now); var entry = Owned(id, owner);
|
||||
var binding = config.Machines.Single(m => m.Id == entry.Machine).Profiles.Single(p => p.Id == entry.Profile);
|
||||
if (config.Accounts.Single(a => a.Id == binding.AccountId).Provider != Core.Provider.Claude ||
|
||||
entry.State != "WaitingForCode" || entry.InputAccepted || entry.Response is not null)
|
||||
throw new InvalidDataException("This login is not waiting for an authorization response.");
|
||||
if (string.IsNullOrWhiteSpace(response) || response.Length > 2048 || response.Any(c => !char.IsAsciiLetterOrDigit(c) && c is not ('_' or '-' or '.' or '#')))
|
||||
throw new InvalidDataException("Paste only the official authorization code, not a URL or credential file.");
|
||||
entry.Response = response;
|
||||
}
|
||||
}
|
||||
public void Cancel(string id, string owner, DateTimeOffset now)
|
||||
{
|
||||
lock (gate)
|
||||
{
|
||||
Expire(now); var p = Owned(id, owner);
|
||||
if (!Terminal(p.State)) { p.State = "Cancelling"; Clear(p); }
|
||||
}
|
||||
}
|
||||
public WorkerReply Exchange(string machine, string boot, IReadOnlyList<LoginUpdate> updates, DateTimeOffset now)
|
||||
{
|
||||
lock (gate)
|
||||
{
|
||||
Expire(now);
|
||||
foreach (var p in pending.Values.Where(p => p.Machine == machine && !Terminal(p.State)))
|
||||
if (p.Boot != boot) { p.State = "WorkerRestarted"; Clear(p); }
|
||||
foreach (var update in updates)
|
||||
{
|
||||
if (!pending.TryGetValue(update.Id, out var p) || p.Machine != machine || p.Boot != boot ||
|
||||
p.Profile != update.ProfileId || Terminal(p.State)) continue;
|
||||
if (update.State is not ("Starting" or "WaitingForBrowser" or "WaitingForCode" or "Verifying" or "Succeeded" or "Failed" or "Cancelled" or "TimedOut" or "AccountMismatch" or "IdentityUnknown")) continue;
|
||||
if (p.State == "Cancelling" && !Terminal(update.State)) continue;
|
||||
var binding = config.Machines.Single(m => m.Id == machine).Profiles.Single(b => b.Id == p.Profile);
|
||||
var account = config.Accounts.Single(a => a.Id == binding.AccountId);
|
||||
if (update.Url is not null && !LoginOutput.OfficialUrl(account.Provider, update.Url)) continue;
|
||||
p.State = update.State;
|
||||
p.Url = update.Url;
|
||||
p.DeviceCode = update.DeviceCode is { Length: <= 24 } code && code.All(c => char.IsAsciiLetterOrDigit(c) || c == '-') ? code : null;
|
||||
p.Email = update.Email is { Length: <= 320 } ? update.Email : null;
|
||||
p.InputAccepted |= update.InputAccepted;
|
||||
if (p.State == "Succeeded" && !string.Equals(account.ExpectedEmail, p.Email, StringComparison.OrdinalIgnoreCase)) p.State = "AccountMismatch";
|
||||
if (p.InputAccepted) p.Response = null;
|
||||
if (Terminal(p.State)) Clear(p);
|
||||
}
|
||||
return new(pending.Values.Where(p => p.Machine == machine && p.Boot == boot &&
|
||||
(!Terminal(p.State) || p.State is "Cancelled" or "TimedOut"))
|
||||
.Select(p => new LoginCommand(p.Id, p.Profile, p.Expires, Terminal(p.State) || p.State == "Cancelling", p.Response)).ToArray());
|
||||
}
|
||||
}
|
||||
private Pending Owned(string id, string owner) => pending.TryGetValue(id, out var p) && p.Owner == owner
|
||||
? p : throw new KeyNotFoundException("Login not found in this browser session.");
|
||||
private void Expire(DateTimeOffset now)
|
||||
{
|
||||
foreach (var p in pending.Values)
|
||||
if (p.Expires <= now && !Terminal(p.State)) { p.State = "TimedOut"; Clear(p); }
|
||||
foreach (var p in pending.Values.Where(p => p.Expires.AddMinutes(15) < now).ToArray()) pending.Remove(p.Id);
|
||||
}
|
||||
private static void Clear(Pending p) { p.Url = null; p.DeviceCode = null; p.Response = null; }
|
||||
}
|
||||
Reference in New Issue
Block a user