Builds: design 47 G §677, §700, lead answers A-219, A-224, A-228 Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
175 lines
5.7 KiB
Go
175 lines
5.7 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"log"
|
|
"time"
|
|
|
|
handacts "git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshhandacts"
|
|
"git.novox.be/novox/mesh-sdk/go/busretry"
|
|
|
|
stdio "git.novox.be/novox/mesh-sdk/go"
|
|
"git.novox.be/novox/mesh-sdk/go/approval"
|
|
|
|
"github.com/novox/mesh-tools/node-tools/internal/bus"
|
|
)
|
|
|
|
// The gate's reach through the runner's own connection, in this one place (design 46 7-approval-mark §7, §13, §14): its
|
|
// state approvals, its publish of an ask under the marked module's name, and its read of the router's record.
|
|
|
|
// approvalsState is the runner's state approvals over its connection: a lost compare-and-set is stdio.ErrChanged, as
|
|
// operator-channel's client reads it.
|
|
type approvalsState struct {
|
|
conn *bus.Conn
|
|
module string
|
|
}
|
|
|
|
func (s approvalsState) Get(key string) (*stdio.StateEntry, error) {
|
|
e, err := s.conn.StateGet(s.module, approval.StateName, key)
|
|
if err != nil || e == nil {
|
|
return nil, err
|
|
}
|
|
return &stdio.StateEntry{Key: e.Key, Value: e.Value, Revision: e.Revision, At: e.At}, nil
|
|
}
|
|
|
|
func (s approvalsState) Create(key string, value any) (uint64, error) {
|
|
raw, err := json.Marshal(value)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return changedAs(s.conn.StateCreate(s.module, approval.StateName, key, raw))
|
|
}
|
|
|
|
func (s approvalsState) Update(key string, value any, revision uint64) (uint64, error) {
|
|
raw, err := json.Marshal(value)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return changedAs(s.conn.StateUpdate(s.module, approval.StateName, key, raw, revision))
|
|
}
|
|
|
|
func (s approvalsState) Put(key string, value any) (uint64, error) {
|
|
raw, err := json.Marshal(value)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return s.conn.StatePut(s.module, approval.StateName, key, raw)
|
|
}
|
|
|
|
func (s approvalsState) Keys() ([]string, error) {
|
|
return s.conn.StateKeys(s.module, approval.StateName)
|
|
}
|
|
|
|
func changedAs(rev uint64, err error) (uint64, error) {
|
|
if errors.Is(err, bus.ErrStateChanged) {
|
|
return 0, stdio.ErrChanged
|
|
}
|
|
return rev, err
|
|
}
|
|
|
|
// Bound says the runner's membership binds its state approvals now (§13): a membership issued later binds it with no restart.
|
|
func (s approvalsState) Bound() bool {
|
|
m := s.conn.Membership(s.module)
|
|
if m == nil {
|
|
return false
|
|
}
|
|
for _, st := range m.State {
|
|
if st.Name == approval.StateName && st.Writes {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// publishAsk says an accept on a seat under the module's name, on the runner's own account (§7).
|
|
func publishAsk(conn *bus.Conn) func(module, subject string, body any, id string) error {
|
|
return func(module, subject string, body any, id string) error {
|
|
raw, err := json.Marshal(body)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
_, err = conn.SeatPublish(module, subject, raw, id)
|
|
return err
|
|
}
|
|
}
|
|
|
|
// routerRecord reads the router's record `<module>.<id>` of the seat's state asks, on the runner's own account (§14).
|
|
func routerRecord(conn *bus.Conn) func(module, id string) (*stdio.StateEntry, error) {
|
|
return func(module, id string) (*stdio.StateEntry, error) {
|
|
e, err := conn.SeatRecord(module, "", id)
|
|
if err != nil || e == nil {
|
|
return nil, err
|
|
}
|
|
return &stdio.StateEntry{Key: e.Key, Value: e.Value, Revision: e.Revision, At: e.At}, nil
|
|
}
|
|
}
|
|
|
|
// boundState is the runner's key-ttl state bound (lead answers A-157, A-158).
|
|
type boundState struct {
|
|
conn *bus.Conn
|
|
module string
|
|
}
|
|
|
|
// BoundState is the runner's state its bound keys live in.
|
|
const BoundState = "bound"
|
|
|
|
func (b boundState) CreateTTL(key string, value any, ttl time.Duration) error {
|
|
raw, err := json.Marshal(value)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
_, err = b.conn.StateCreateTTL(b.module, BoundState, key, raw, ttl)
|
|
return err
|
|
}
|
|
|
|
func (b boundState) Delete(key string) error { return b.conn.StateDelete(b.module, BoundState, key) }
|
|
|
|
func (b boundState) Watch(pattern string, expired func(key string)) (func(), error) {
|
|
return b.conn.StateWatch(b.module, BoundState, pattern, func(c bus.StateChange) error {
|
|
if c.Op == "expire" {
|
|
expired(c.Key)
|
|
}
|
|
return nil
|
|
})
|
|
}
|
|
|
|
// warranted asks mesh-hand-acts.warranted for one acted ask, each try a kept call open while open says so (design 46
|
|
// 7-approval-mark §16; lead answer A-157).
|
|
func warranted(conn *bus.Conn) func(asker, ask string, open func() (bool, error)) error {
|
|
return func(asker, ask string, open func() (bool, error)) error {
|
|
_, err := busretry.DoKept("mesh-hand-acts.warranted of "+asker+"."+ask,
|
|
busretry.Open{Key: asker + "." + ask, Open: func(string) (bool, error) { return open() }, Logf: log.Printf},
|
|
func() (bus.Answered, error) {
|
|
return conn.Ask("seat:"+handacts.Seat+"."+handacts.VerbWarranted, handacts.WarrantedInput{Asker: asker, Ask: ask},
|
|
"mesh.seat."+handacts.Seat+".tool."+handacts.VerbWarranted)
|
|
}, nil)
|
|
return err
|
|
}
|
|
}
|
|
|
|
// hearOne waits on the runner's connection for the first event on the subjects whose body match takes (design 34
|
|
// every-event §36: a marked wait's ask's decided).
|
|
func hearOne(conn *bus.Conn) func(ctx context.Context, subjects []string, within time.Duration, match func(json.RawMessage) bool) (bool, error) {
|
|
return func(ctx context.Context, subjects []string, within time.Duration, match func(json.RawMessage) bool) (bool, error) {
|
|
_, ok, _, err := conn.HearFirst(ctx, subjects, within, match)
|
|
return ok, err
|
|
}
|
|
}
|
|
|
|
// gateTools is the gate as mesh_approvals reads it, with its state's fill read from the bucket (§13, §18).
|
|
type gateTools struct {
|
|
*approval.Gate
|
|
state approvalsState
|
|
}
|
|
|
|
func (t gateTools) Fill() (uint64, int64, error) {
|
|
info, err := t.state.conn.StateInfo(t.state.module, approval.StateName)
|
|
if err != nil || info == nil {
|
|
return 0, 0, fmt.Errorf("the state %s's fill cannot be read: %v", approval.StateName, err)
|
|
}
|
|
return info.Bytes, info.MaxBytes, nil
|
|
}
|