Files
mesh-catalog/modules/distribution/internal/holder/findings.go
T
jschoubben 95f29d6c54 G findings: the modules with findings of their own write them through mesh-sdk's writer
artifact-store-tools (image-missing, sweep-refused), distribution (program and tools bundle, one writer each over its own
kinds), gitea's package-registry (and the token-refused key the TypeScript bundle watches) and mesh-config keep their
findings open and write and clear them through the generic writer; mesh-vault states its assignments view's. A part of an
id told apart by a ref is in the id (<id>.<ref>), each escaped; the writer starts once Serve has the runtime's channel; the
removed findings helpers and resolved words go.

Builds: design 47 G §686, §687, §689, A-3, A-14, A-16, A-22

Claude-Session: https://claude.ai/code/session_01JG73KEq4zoDfM9WtYpcQgZ
2026-11-11 21:58:08 +01:00

78 lines
2.7 KiB
Go

package holder
import (
"context"
"sync"
"time"
meshconditions "git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions"
keyparts "git.novox.be/novox/mesh-sdk/go/keys"
"git.novox.be/novox/mesh-sdk/go/views"
)
// FindingKinds are the kinds the driver says (the program) and the transfers say (the tools bundle): each bundle's writer
// owns its own, since the two share one state `findings` (design 47 G §580, §689).
var (
DriverFindingKinds = []string{FindingWindowHeld}
TransferFindingKinds = []string{"publish-refused", "incoming-full"}
)
// FindingWriter is the module's writer (views.Writer) as the keeper writes and clears through it.
type FindingWriter interface {
Write(ctx context.Context, finding meshconditions.Finding) error
Clear(ctx context.Context, scope, id, kind, why string) error
}
// Findings keeps a bundle's open findings and says them through the module's writer (design 47 G §689, A-14): its `Say` is
// the Finding callback of the Driver and the Holder, and its `Observed` is the writer's observe, so a statement covers
// what is open.
type Findings struct {
Writer FindingWriter
Logf func(string, ...any)
Now func() time.Time
mu sync.Mutex
open map[string]meshconditions.Finding
}
// Say raises (raised) or clears the finding of a kind about a subject. Its id is the seat's and the subject's, each escaped
// as a key's name is (design 47 G A-14); the kind stays the bundle's own word.
func (k *Findings) Say(kind, subject, words string, raised bool) {
id := keyparts.EscapeName(Self) + "." + keyparts.EscapeName(subject)
k.mu.Lock()
defer k.mu.Unlock()
key := views.FindingKey("seat", id, kind)
if !raised {
delete(k.open, key)
if err := k.Writer.Clear(context.Background(), "seat", id, kind, "it no longer holds"); err != nil {
k.Logf("the finding %s could not be cleared: %v", key, err)
}
return
}
at := time.Now()
if k.Now != nil {
at = k.Now()
}
f := meshconditions.Finding{Scope: "seat", ID: id, Kind: kind, Subject: meshconditions.Subject{Scope: "seat", ID: id},
Severity: meshconditions.FindingSeverityWarning, Summary: words, Headline: words, Explanation: words,
Source: kind, Observer: Self, At: at.UTC()}
if k.open == nil {
k.open = map[string]meshconditions.Finding{}
}
k.open[key] = f
if err := k.Writer.Write(context.Background(), f); err != nil {
k.Logf("the finding %s could not be written: %v", key, err)
}
}
// Observed is the findings open now, for the writer's statement (views.WriterConfig.Observe).
func (k *Findings) Observed(context.Context) (views.Observed[meshconditions.Finding], error) {
k.mu.Lock()
defer k.mu.Unlock()
var out views.Observed[meshconditions.Finding]
for _, f := range k.open {
out.Findings = append(out.Findings, f)
}
return out, nil
}