package licences // The layouts of the module's kept data and of its log entries (design 39 bus-and-switch §2 to §9; design 39 parts // §5): what the manager bundle writes and the tools bundle reads, declared once here, so the reading tools never read // a form the manager no longer writes. import ( "encoding/json" "sort" "time" oc "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/seats/operatorchannel" stdio "git.novox.be/novox/mesh-sdk/go" ) // Publisher is the name the manager says its conditions under, and the id of their subject (B17). const Publisher = "claude-licence-manager" // The names of the entries the module keeps on the bus, as its manifest asks for them (§2 to §9). const ( EntryLicences = "licences" EntryBindings = "bindings" EntryLeases = "leases" EntryOffered = "offered" EntryUsage = "usage" EntryAudit = "audit" EntryCarried = "carried" EntryConditions = "conditions" ) // States are the module's kept states, and Logs its two logs, by their names in the manifest. var ( States = []string{EntryLicences, EntryBindings, EntryLeases, EntryOffered, EntryCarried, EntryConditions} Logs = []string{EntryUsage, EntryAudit} ) // State is a state the module keeps, as mesh-sdk's KeptState answers it. type State interface { Get(key string) (*stdio.StateEntry, error) Put(key string, value any) (uint64, error) PutTTL(key string, value any, ttl time.Duration) (uint64, error) Create(key string, value any) (uint64, error) CreateTTL(key string, value any, ttl time.Duration) (uint64, error) Delete(key string) error Keys() ([]string, error) History(key string) ([]stdio.StateRevision, error) } // Log is a log the module keeps, as mesh-sdk's KeptLog answers it. type Log interface { Append(key string, value any) (uint64, error) Read(from uint64, keys string, limit int) (*stdio.LogPage, error) Last(key string) (*stdio.LogEntry, error) Info() (*stdio.LogInfo, error) Newest(keys string) ([]stdio.LogEntry, error) } // Licence is one licence: an account, or an API key. Its form is the value of the state `licences` (design 39 // bus-and-switch §2): never a secret, which the vault holds (§15). type Licence struct { Name string `json:"name"` Kind string `json:"kind"` // subscription | api-key AccountUUID string `json:"account,omitempty"` Email string `json:"address,omitempty"` OrganizationUUID string `json:"organisation,omitempty"` RefreshFingerprint string `json:"refreshFingerprint,omitempty"` AccessExpiresAt int64 `json:"accessExpiresAt,omitempty"` RefreshExpiresAt int64 `json:"refreshExpiresAt,omitempty"` Failures int `json:"failures"` NotifiedAt int64 `json:"notifiedAt,omitempty"` AdoptedAt int64 `json:"adoptedAt"` AdoptedFrom string `json:"adoptedFrom,omitempty"` RotatedAt int64 `json:"rotatedAt,omitempty"` // Generation is the generation of the licence's secrets in the vault (§2, §15). Generation int64 `json:"generation,omitempty"` // Usage is the latest reading of the licence's usage (§2, §24). Usage *UsageReading `json:"usage,omitempty"` } // UsageReading is the licence-grain reading (ADR 0054), flattened from the vendor's windows (design 39 bus-and-switch // §24), with, as the licence keeps it (§2), when it was last read successfully and why the latest reading failed. type UsageReading struct { SessionPct *float64 `json:"sessionPct"` SessionResetsAt string `json:"sessionResetsAt,omitempty"` WeeklyPct *float64 `json:"weeklyPct"` WeeklyResetsAt string `json:"weeklyResetsAt,omitempty"` SonnetPct *float64 `json:"sonnetPct"` SonnetResetsAt string `json:"sonnetResetsAt,omitempty"` ReadAt string `json:"readAt,omitempty"` ReadError string `json:"readError,omitempty"` } // ReadErrorWords is the vendor's reason a reading failed, or that none was given. func (u *UsageReading) ReadErrorWords() string { if u == nil || u.ReadError == "" { return "no reason given" } return u.ReadError } // BindingState is the value of a consumer's key in `bindings` (§3, B22: the form 0 its readers decode): no secret, // only what it should hold, which generation, since when, why and by whom. type BindingState struct { Licence string `json:"licence"` Kind string `json:"kind"` Generation int64 `json:"generation"` Since string `json:"since,omitempty"` Why string `json:"why,omitempty"` By string `json:"by,omitempty"` } // LeaseValue is a lease's key in `leases`: its holder and until when. type LeaseValue struct { Holder string `json:"holder"` Until string `json:"until"` } // OfferedValue is a grant's key in `offered`: what became of it, where and when (§5, B13: every outcome). type OfferedValue struct { Node string `json:"node"` Account string `json:"account"` Outcome Outcome `json:"outcome"` Why string `json:"why"` At string `json:"at"` } // Outcome is what became of a login the manager was offered, by its refresh token's fingerprint. type Outcome string const ( Adopted Outcome = "adopted" Dead Outcome = "dead" Skipped Outcome = "skipped" Refused Outcome = "refused" Gone Outcome = "gone" ) // UsageEntry is one entry of the log `usage` (§6): when, the flattened reading and the vendor's answer whole (ADR 0054). type UsageEntry struct { At string `json:"at"` Reading UsageReading `json:"reading"` Raw map[string]any `json:"raw,omitempty"` } // AuditKinds are the subjects of the log `audit` (§7); an act of another kind is kept under `drift`. var AuditKinds = []string{"adopted", "refused", "failed", "rotated", "bound", "switched", "released", "drift", "carried", "settings"} // KeptCondition is the value of a key of `conditions` (§9): the condition said raised, and since when. type KeptCondition struct { Body oc.Condition `json:"body"` Since string `json:"since"` } // KeptConditions is each key of the state conditions with its body and since (bus-and-switch §9, §43), by key; a // nil state holds none. func KeptConditions(conditions State) ([]KeptCondition, error) { out := []KeptCondition{} if conditions == nil { return out, nil } keys, err := conditions.Keys() if err != nil { return nil, err } sort.Strings(keys) for _, k := range keys { e, err := conditions.Get(k) if err != nil { return nil, err } var c KeptCondition if e == nil || json.Unmarshal(e.Value, &c) != nil { continue } out = append(out, c) } return out, nil }