The licence manager keeps its data on the bus and switches licences by itself at 99% (issue 1409) #657

Merged
mesh-tools merged 32 commits from feat/39-licence-switch into main 2026-11-12 00:13:05 +00:00
2 changed files with 14 additions and 4 deletions
Showing only changes of commit cc8a354f75 - Show all commits
@@ -384,9 +384,10 @@ func (m *Manager) adoptOne(ctx context.Context, c Candidate, reports []Holdings,
}
now := m.Now().UnixMilli()
l := Licence{Name: NameFor(c.Identity), Kind: "subscription", AccountUUID: c.Identity.AccountUUID,
Email: c.Identity.EmailAddress, OrganizationUUID: c.Identity.OrganizationUUID}
Email: c.Identity.EmailAddress, OrganizationUUID: c.Identity.OrganizationUUID, AdoptedFrom: c.Node}
if held != nil {
l.Name, l.NotifiedAt, l.Generation = held.Name, held.NotifiedAt, held.Generation
// A newer login keeps what the key holds beside its grant: its latest reading (§2, §24).
l.Name, l.NotifiedAt, l.Generation, l.Usage = held.Name, held.NotifiedAt, held.Generation, held.Usage
if l.Email == "" {
l.Email = held.Email
}
@@ -898,9 +899,9 @@ func (m *Manager) adoptKey(ctx context.Context, name, key, from string) (map[str
_ = m.Emit("licence.refused", map[string]any{"licence": name, "from": from, "reason": why})
return nil, fmt.Errorf("%s is refused: %s", name, why)
}
l := Licence{Name: name, Kind: "api-key", AdoptedAt: m.Now().UnixMilli()}
l := Licence{Name: name, Kind: "api-key", AdoptedAt: m.Now().UnixMilli(), AdoptedFrom: from}
if held != nil {
l.Generation = held.Generation
l.Generation, l.Usage = held.Generation, held.Usage
}
if err := m.handOver(ctx, &l, key, key); err != nil {
return nil, err
@@ -231,6 +231,12 @@ func TestANewerLoginReplacesTheGrantHeldAndAnOlderOneDoesNot(t *testing.T) {
ctx := context.Background()
_, _ = mm.m.Consider(ctx, []Holdings{mm.login("laptop", "rt-a", true, t0.Add(-time.Minute))})
before, _ := mm.store.Licence(ctx, licence1)
// §2: a licence says where it was adopted from, and a newer login keeps the latest reading the key holds.
pct := 42.0
before.Usage = &UsageReading{SessionPct: &pct, ReadAt: t0.Format(time.RFC3339Nano)}
if before.AdoptedFrom != "laptop" || mm.store.SaveLicence(ctx, *before) != nil {
t.Fatalf("the adopted licence is %+v", before)
}
_, _ = mm.m.Consider(ctx, []Holdings{mm.login("server", "rt-old", true, t0.Add(-24*time.Hour))})
if strings.Join(mm.vendor.exchanged, ",") != "rt-a" {
t.Fatalf("an older login was exchanged: %v", mm.vendor.exchanged)
@@ -241,6 +247,9 @@ func TestANewerLoginReplacesTheGrantHeldAndAnOlderOneDoesNot(t *testing.T) {
if after.RefreshFingerprint == before.RefreshFingerprint || mm.vendor.exchanged[len(mm.vendor.exchanged)-1] != "rt-new" {
t.Fatalf("a newer login did not win: %v", mm.vendor.exchanged)
}
if after.AdoptedFrom != "desktop" || after.Usage == nil || after.Usage.SessionPct == nil || *after.Usage.SessionPct != 42 {
t.Fatalf("after a newer login the licence is %+v", after)
}
if ls, _ := mm.store.Licences(ctx); len(ls) != 1 {
t.Fatalf("one account became %d licences", len(ls))
}