package policy import ( "context" "errors" "fmt" "lincodex/backend/internal/domain" "lincodex/backend/internal/port/outbound" ) // ErrNotDemoPolicy reports a latest product policy that is not a demonstration // version. The operator command never builds on a calibrated version. var ErrNotDemoPolicy = errors.New("the latest product policy is not a demonstration version") // ErrPolicyChanged reports that another publication took the next version // number while the section was being added. Nothing was stored; the command // can be run again. var ErrPolicyChanged = errors.New("the product policy changed during publication, run the command again") // MatchingResult reports what EnsureDemoMatchingSection did. Version is the // version that carries the matching section: the new one when Created, the // latest one otherwise. type MatchingResult struct { Created bool Version domain.PolicyVersion } // EnsureDemoMatchingSection makes the latest demonstration version carry the // demo matching section. A version that already has the section is left alone. // Otherwise it publishes the next version: a copy of the latest one with // domain.DemoMatchingPolicy added, so no other value changes. It refuses a // latest version that is not a demonstration version and a family without a // version, because the section is only an addition to a policy that exists. func (publisher *Publisher) EnsureDemoMatchingSection(ctx context.Context, author domain.PolicyAuthor) (MatchingResult, error) { return publisher.ensureMatching(ctx, author, "Publisher.EnsureDemoMatchingSection", func(current *domain.MatchingPolicy) bool { return current != nil }, domain.DemoMatchingPolicy) } // EnsureDemoSemanticMatchingSection makes the latest demonstration version // carry the semantic matching section (domain.DemoSemanticMatchingPolicy): the // encoder identity, the calibration of Sem, the explanation thresholds, and // the Fit weights with Sem 0.5. A version that already has a semantic section // is left alone. Otherwise it publishes the next version, a copy of the latest // one whose matching section is replaced, so the earlier PRD point stays as // the previous version. func (publisher *Publisher) EnsureDemoSemanticMatchingSection(ctx context.Context, author domain.PolicyAuthor) (MatchingResult, error) { return publisher.ensureMatching(ctx, author, "Publisher.EnsureDemoSemanticMatchingSection", func(current *domain.MatchingPolicy) bool { return current != nil && current.Semantics != nil }, domain.DemoSemanticMatchingPolicy) } func (publisher *Publisher) ensureMatching( ctx context.Context, author domain.PolicyAuthor, location string, present func(*domain.MatchingPolicy) bool, section func() domain.MatchingPolicy, ) (MatchingResult, error) { latest, err := publisher.policies.GetLatest(ctx, domain.ProductPolicyKey) if errors.Is(err, outbound.ErrPolicyVersionNotFound) { return MatchingResult{}, fmt.Errorf("%s - the product policy has no version to extend: %w", location, err) } if err != nil { return MatchingResult{}, fmt.Errorf("%s - read latest version: %w", location, err) } current := latest.Policy() if present(current.Matching) { return MatchingResult{Version: latest}, nil } if latest.Kind() != domain.PolicyKindDemo { return MatchingResult{}, fmt.Errorf("%s - latest version %d is %s: %w", location, latest.Version(), latest.Kind(), ErrNotDemoPolicy) } matching := section() current.Matching = &matching // The new version is built on the version that was read, so it takes the // next number exactly once. A taken number means the base is no longer the // latest, and the operator decides again instead of the command retrying. draft, err := domain.NewPolicyVersionDraft(domain.NewPolicyVersionDraftInput{ Key: domain.ProductPolicyKey, Kind: domain.PolicyKindDemo, Author: author, Policy: current, }) if err != nil { return MatchingResult{}, fmt.Errorf("%s - build the extended version: %w", location, err) } numbered, err := draft.WithVersion(latest.Version() + 1) if err != nil { return MatchingResult{}, fmt.Errorf("%s - number the extended version: %w", location, err) } published, err := publisher.policies.Create(ctx, numbered) if errors.Is(err, outbound.ErrPolicyVersionConflict) { return MatchingResult{}, fmt.Errorf("%s - version %d is taken: %w", location, numbered.Version(), ErrPolicyChanged) } if err != nil { return MatchingResult{}, fmt.Errorf("%s - store the extended version: %w", location, err) } return MatchingResult{Created: true, Version: published}, nil }