package hostguard import ( "context" "github.com/carlosframework/platform/internal/hostclaim" ) // WithServingRetirement holds both root and shared host locks. Unlike Work, // retirement may precede installation of the newly admitted stamp. The callback // must authenticate the older installation and stop receipts before effects. // It must not call a lock-taking Guard or RootAuthority method. func (g Guard) WithServingRetirement(ctx context.Context, host string, current Captured, fn func(hostclaim.Observed, FenceView) error) error { if ctx == nil || g.Root == nil || g.FenceView != nil || current.Incarnation == "" || current.Stamp == 0 || fn == nil { return ErrInvalidInput } return g.locked(ctx, host, func(d *recordDir) error { o, err := g.freshObserved(ctx, host) if err != nil { return err } c, h := o.Claim, o.Claim.Holder if h.Incarnation != current.Incarnation || h.Stamp != current.Stamp || h.Session != current.Stamp || c.Op.Kind != hostclaim.KindServing || c.Op.ID != "" || c.Op.Phase != "" { return ErrStampMismatch } r, err := d.read(host) if err != nil { return err } if r.quarantined || r.pendingInstallation != nil || r.barrier != nil { return ErrFenced } if r.ActiveFence != nil { completed := false for _, receipt := range r.FenceJournal { completed = completed || receipt.Target == *r.ActiveFence } if !completed || r.ActiveFence.Incarnation != current.Incarnation || r.ActiveFence.Stamp >= current.Stamp { return ErrFenced } } if r.Incarnation != current.Incarnation || r.ActiveStamp == 0 || r.ActiveStamp > current.Stamp { return ErrStampMismatch } for _, retired := range r.rootRetired { if retired == current { return ErrRetiredStamp } } for _, receipt := range r.FenceJournal { if receipt.Target.matchesSession(c, current) { return ErrFenced } } view, err := g.Root.viewLocked(host, Captured{Incarnation: r.Incarnation, Stamp: r.ActiveStamp}) if err != nil { return err } if err = view.validate(host, view.Captured); err != nil { return err } if err = ctx.Err(); err != nil { return err } return fn(o, view) }) }