Files
mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient/entry.go
T
jschoubben 71a35a6376 MV1: move contracts and sdk under modules/, importers follow
The delivery repository's contracts and SDK live under modules/ as
mesh-delivery-contracts and mesh-delivery-sdk, their Go module paths
renamed to match the folders (the TypeScript names already did), and
every importer in mesh-delivery, mesh-catalog, mesh-host, mesh-tools
and mesh-sdk follows in the same group: a design as it should be, one
path per module, checked by the group's check building every importer.

Builds: design 47 G §427, §88

Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
2026-11-12 05:09:56 +01:00

117 lines
4.7 KiB
Go

package meshcatalogclient
import (
"context"
"fmt"
"strings"
"time"
seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
stdio "git.novox.be/novox/mesh-sdk/go"
"git.novox.be/novox/mesh-sdk/go/views"
)
// Ready is nil once every watched branch has its first view (`none-yet` counting as one), else says what it waits for
// (design 47 G §614, A-103): a part acts on nothing until then.
func (b *Branches) Ready() error {
followed := b.Followed()
if len(followed) == 0 {
return fmt.Errorf("the catalog half names no watched branch yet (the registrations of the seat %s are asked until they answer)", seat.Seat)
}
var waiting []string
for _, branch := range followed {
if c, ok := b.Catalog(branch); ok {
select {
case <-c.Ready():
default:
waiting = append(waiting, branch)
}
}
}
if len(waiting) > 0 {
return fmt.Errorf("the catalog half has no first view of %s yet", strings.Join(waiting, ", "))
}
return nil
}
// Entry is a module's entry at the trunk: the module's own registration decides, so the branch followed first, which lists
// the module, is the one read (design 47 G A-103). An error says a watched branch has no first view yet, never "no module".
func (b *Branches) Entry(module string) (seat.CatalogEntry, bool, error) {
if err := b.Ready(); err != nil {
return seat.CatalogEntry{}, false, err
}
for _, branch := range b.Followed() {
if c, ok := b.Catalog(branch); ok {
if e, ok := c.Entry(module); ok {
return e, true, nil
}
}
}
return seat.CatalogEntry{}, false, nil
}
// EntryStatus is one entry kept by fingerprint as a part's `<module>_first_views` says it: its state and since when.
type EntryStatus struct {
Fingerprint string `json:"fingerprint"`
State EntryState `json:"state"`
Since time.Time `json:"since"`
Detail string `json:"detail,omitempty"`
}
// EntryStatuses are the entries kept by fingerprint, as `<module>_first_views` lists them (design 47 G §614, A-103).
func (b *Branches) EntryStatuses() []EntryStatus {
rows := b.Entries.Rows()
out := make([]EntryStatus, 0, len(rows))
for _, r := range rows {
out = append(out, EntryStatus{Fingerprint: r.Fingerprint, State: r.State, Since: r.Since, Detail: r.Detail})
}
return out
}
// WriteEntryFindings makes the part write the finding `entry-not-found` through w for each entry the holder answered it does not
// hold, and clear it when a later ask answers it or the part releases the entry (design 47 G §614, A-15, A-103). then, when not
// nil, is told of each change after, so the part starts a new pass. The kind `entry-not-found` is one of w's kinds.
func (b *Branches) WriteEntryFindings(w views.WireWriter, then func(EntryRow)) {
b.Entries.Changed = func(row EntryRow) {
switch row.State {
case EntryNotFound:
if err := views.WriteEntryNotFound(w, row.Fingerprint, row.Since, time.Now()); err != nil && b.Logf != nil {
b.Logf("the finding entry-not-found of %s is not kept: %v", row.Fingerprint, err)
}
case EntryAnswered:
_ = views.ClearEntryNotFound(w, row.Fingerprint)
}
if then != nil {
then(row)
}
}
b.Entries.Released = func(fingerprint string) { _ = views.ClearEntryNotFound(w, fingerprint) }
}
// EntryAgainTool is the part's tool `<module>_entry_again` (design 47 G A-103; class act): it asks one entry by fingerprint
// again now, as a hand act recorded first through handAct (what, why, cause), and answers where the entry stands.
func (b *Branches) EntryAgainTool(module string, handAct func(what, why, cause string) error) stdio.Tool {
name := module + "_entry_again"
return stdio.Tool{Name: name,
Description: "Ask the catalog's entry of one build, by its fingerprint, again now, whatever its state: a hand act, for an entry the part holds as asked or not found. Answers where it stands after. (a)",
Input: map[string]any{"type": "object", "required": []string{"fingerprint", "why"}, "properties": map[string]any{
"fingerprint": map[string]any{"type": "string"}, "why": map[string]any{"type": "string"}}},
Run: func(a map[string]any) (any, error) {
fingerprint, _ := a["fingerprint"].(string)
why, _ := a["why"].(string)
if fingerprint = strings.TrimSpace(fingerprint); fingerprint == "" {
return nil, fmt.Errorf("name the fingerprint of the entry")
}
if strings.TrimSpace(why) == "" {
return nil, fmt.Errorf("say why: a hand act keeps its why")
}
if err := handAct(name+" "+fingerprint, strings.TrimSpace(why), name); err != nil {
return nil, fmt.Errorf("the hand-act log does not hold it, so nothing is asked: %w", err)
}
if err := b.Entries.Again(context.Background(), fingerprint); err != nil {
return nil, err
}
return map[string]any{"entries": b.EntryStatuses()}, nil
}}
}