Files
mesh-delivery/modules/mesh-grants/internal/grants/namedpurge.go
T
jschoubben ff2f90ec18 GR13 purge: the named asks go at the first revision that leaves them, or by grants_named_purge
While no entry of the revision invokes a queue of a build seat (seat:<seat>.accept.<queue>,
read by catalogue.QueueInvoke), the part composes no named worker, and at the first such
pass it purges accept.<queue>.* of each build seat's stream and deletes the named workers,
saying each in its journal; a failed read of the catalog half purges nothing. Its account
alone is granted the stream's purge and the named workers' delete. The hand tool
grants_named_purge records its hand act first, then asks the process to do it.

Builds: design 47 G §544, §600, §601, A-231

Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
2026-11-12 06:22:54 +01:00

122 lines
4.7 KiB
Go

package grants
import (
"encoding/json"
"errors"
"fmt"
"strings"
"git.novox.be/novox/mesh-sdk/go/catalogue"
"mesh-grants/internal/buscompose"
)
// **The purge of the build seats' named asks** (design 47 G §544, A-231): at the first revision in which no entry invokes any
// `seat:<seat>.accept.<queue>`, the part purges the subjects `accept.<queue>.*` of each build seat's stream and deletes the
// named workers, within its pass, saying each in its journal; where that did not run (the part stopped first), the tool
// grants_named_purge, written in the hand-act log first, asks the part's process to do it by hand. An ask is transport, not
// kept data.
// ToolNamedPurge is the hand tool that purges the named asks and deletes the named workers.
const ToolNamedPurge = "grants_named_purge"
// NamedPurger purges one subject of a stream and deletes one consumer: the process's connection; nil in the tools.
type NamedPurger interface {
PurgeSubject(stream, subject string) error
// DeleteConsumer deletes a consumer; one the bus does not hold is no error.
DeleteConsumer(stream, name string) error
}
// NamedAsksUnused says the revision the part composes from holds entries and none invokes a queue of a build seat
// (catalogue.QueueInvoke); false while the catalog half is not read, so a failed read purges nothing.
func NamedAsksUnused(in Inputs) bool {
if in.CatalogUnread != "" || len(in.Manifests) == 0 {
return false
}
for _, m := range in.Manifests {
for _, i := range m.Invokes {
if _, _, ok := catalogue.QueueInvoke(i); ok {
return false
}
}
}
return true
}
// withNoNamedAsks marks each pulled-by-kind seat a declaration holds as having no named worker while unused.
func withNoNamedAsks(unused bool, d *buscompose.Declared) {
for _, list := range []*[]buscompose.Seat{&d.Holds, &d.Uses, &d.Watches} {
for i := range *list {
if (*list)[i].PulledByKind {
(*list)[i].NoNamedAsks = unused
}
}
}
}
// purgeNamed purges the named asks and deletes the named workers of every build seat's queue the records compose, at the
// first pass whose revision leaves them unused, and on a person's ask whatever the revision says.
func (p *Part) purgeNamed(res *Result, in Inputs, recs buscompose.Records, byHand string) {
unused := NamedAsksUnused(in)
due := unused && !p.namedPurged || byHand != ""
p.namedPurged = unused
if !due || p.NamedPurge == nil {
return
}
users, err := buscompose.Users(recs)
if err != nil {
res.Errors = append(res.Errors, "the named asks could not be purged: "+err.Error())
return
}
why := "no entry of the revision invokes a queue of a build seat"
if byHand != "" {
why = "by hand, for " + byHand
}
for _, q := range buscompose.NamedQueuesOf(users) {
if err := p.NamedPurge.PurgeSubject(q.Stream, q.Subject); err != nil {
res.Errors = append(res.Errors, fmt.Sprintf("the named asks %s of %s were not purged: %v", q.Subject, q.Stream, err))
p.namedPurged = false
} else {
res.NamedPurged = append(res.NamedPurged, q.Stream+" "+q.Subject)
p.logf("purged the named asks %s of %s (%s)", q.Subject, q.Stream, why)
}
if err := p.NamedPurge.DeleteConsumer(q.Stream, q.Worker); err != nil {
res.Errors = append(res.Errors, fmt.Sprintf("the named worker %s.%s was not deleted: %v", q.Stream, q.Worker, err))
p.namedPurged = false
} else {
res.NamedPurged = append(res.NamedPurged, q.Stream+"."+q.Worker)
p.logf("deleted the named worker %s.%s (%s)", q.Stream, q.Worker, why)
}
}
}
func (p *Part) logf(format string, args ...any) {
if p.Logf != nil {
p.Logf(format, args...)
}
}
// AskNamedPurge answers the tool grants_named_purge: it records the hand act through record (the seat mesh-hand-acts) and only
// then asks the part's process to purge the named asks and delete the named workers of every build seat's queue.
func (p *Part) AskNamedPurge(caller, why string, record func(what, why string) error) (any, error) {
if strings.TrimSpace(why) == "" {
return nil, errors.New(ToolNamedPurge + " needs why: it is recorded as a hand act")
}
if record == nil {
return nil, errors.New("the hand act cannot be recorded here, so nothing was asked")
}
if err := record(ToolNamedPurge, why); err != nil {
return nil, fmt.Errorf("the hand act could not be recorded, so nothing was asked: %w", err)
}
by := caller
if by == "" {
by = "a person"
}
ask, _ := json.Marshal(Ask{By: by, At: p.now(), NamedPurge: true})
rev, err := p.Store.Put(StateUserList, KeyAsked, ask)
if err != nil {
return nil, fmt.Errorf("the ask could not be written: %w", err)
}
return map[string]any{"asked": rev, "says": "the part's process purges the named asks and deletes the named workers at this ask; its journal names each"}, nil
}