Their three copies of the id and option checks (askjson.go checkedAsk, the teller's routerAskOf, the view's routerAsk) are deleted; each calls occ.Carried with the id the key and the asking give. Claude-Session: https://claude.ai/code/session_01JG73KJNrH5EgFXMKKYSrig
841 lines
31 KiB
Go
841 lines
31 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"slices"
|
|
"sort"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
occ "git.novox.be/novox/mesh-contracts/go/clients/operatorchannelclient"
|
|
oc "git.novox.be/novox/mesh-contracts/go/seats/operatorchannel"
|
|
|
|
stdio "git.novox.be/novox/mesh-sdk/go"
|
|
"git.novox.be/novox/mesh-sdk/go/delivery"
|
|
)
|
|
|
|
// The teller carries the gate's merge asks to the operator and writes the operator's answer back (novox/hq ADR 0437
|
|
// §6, §9). It reads the gate's asks, publishes each to the router unchanged as the asker mesh-delivery-teller
|
|
// (mesh-sdk go/asker), withdraws it when the gate takes its key away, and on the warrant the router says to it
|
|
// holds the warrant to the ask (ADR 0259 §6) and writes the answer under the ask's own key by compare-and-set. A
|
|
// warrant that does not hold writes no answer and is said as a condition. It holds no timer and serves no request:
|
|
// every reaction is a revision of the gate's key or a warrant.
|
|
|
|
// gateAsked is the gate's ask with the router's ask decoded from its JSON (delivery.Ask.Ask is operator-channel's ask as
|
|
// JSON, novox/hq ADR 0627 §4).
|
|
type gateAsked struct {
|
|
Group string
|
|
Heads []delivery.Head
|
|
Asking int
|
|
Ask oc.Ask
|
|
}
|
|
|
|
// decodeGateAsk reads the gate's ask value and the router's ask in it, as delivery.DecodeAsk did before the ask was JSON.
|
|
func decodeGateAsk(value []byte) (gateAsked, error) {
|
|
ga, err := delivery.DecodeAsk(value)
|
|
if err != nil {
|
|
return gateAsked{}, err
|
|
}
|
|
key, err := delivery.MergeAskKey(ga.Group, ga.Heads)
|
|
if err != nil {
|
|
return gateAsked{}, err
|
|
}
|
|
a, err := occ.Carried(ga.Ask, delivery.AskingID(key, ga.Asking))
|
|
if err != nil {
|
|
return gateAsked{}, err
|
|
}
|
|
return gateAsked{Group: ga.Group, Heads: ga.Heads, Asking: ga.Asking, Ask: a}, nil
|
|
}
|
|
|
|
// store is where the teller keeps what it asked: the asker's own records (`ask.<id>`) and, beside them, which gate key
|
|
// each ask was made for and at which revision of it (`key.<id>`), and which conditions it has raised (`cond.<id>`).
|
|
type store interface {
|
|
occ.Store
|
|
Delete(key string) error
|
|
}
|
|
|
|
// routerAsks is the occ.Client as the teller uses it.
|
|
type routerAsks interface {
|
|
Ask(a oc.Ask) error
|
|
Cancel(id string) error
|
|
Where(id string) (state string, w *oc.Warrant, err error)
|
|
Take(w oc.Warrant, act func(oc.Option, oc.Warrant) error) (occ.Handled, error)
|
|
}
|
|
|
|
// answers is the teller's bucket answers.
|
|
type answers interface {
|
|
// Get is the answer under the ask's key and its revision; found is false when there is none.
|
|
Get(key string) (value []byte, rev uint64, found bool, err error)
|
|
// Write creates the answer when found is false (the key had none when it was read) and otherwise updates it at the
|
|
// revision read; written is false when another writer was first.
|
|
Write(key string, value []byte, rev uint64, found bool) (written bool, err error)
|
|
}
|
|
|
|
// gateAsks reads the gate's bucket asks.
|
|
type gateAsks interface {
|
|
Get(key string) (value []byte, found bool, err error)
|
|
}
|
|
|
|
// Teller is the teller's logic; the program's wiring is main.go and decided.go.
|
|
type Teller struct {
|
|
Asker routerAsks
|
|
Store store
|
|
Answers answers
|
|
Gate gateAsks
|
|
// Assigner is the assigner's bucket asks and View the view's bucket answers (issue 1297 B10); with either nil the teller
|
|
// answers nothing from the view.
|
|
Assigner gateAsks
|
|
View gateAsks
|
|
// Say publishes a condition to the operator (mesh.seat.operator-channel.accept.condition.<module>).
|
|
Say func(c oc.Condition) error
|
|
Logf func(format string, args ...any)
|
|
// Publish is the bus the asker publishes on, used to ask again an ask the router refused for want of room under a bus
|
|
// message id of its own (the bus drops a second message under an id it holds).
|
|
Publish func(subject string, body any, id string) error
|
|
// Now is the clock; nil is time.Now.
|
|
Now func() time.Time
|
|
|
|
mu sync.Mutex
|
|
}
|
|
|
|
func (t *Teller) logf(format string, args ...any) {
|
|
if t.Logf != nil {
|
|
t.Logf(format, args...)
|
|
}
|
|
}
|
|
|
|
// seen is what the teller keeps beside an ask it made. Under `key.<router id>` it names the gate key the asking was made for;
|
|
// under `gate.<gate key>` it names the router id of the latest asking of that key and the revision of the key it was made
|
|
// at, so a replay of the revision asks nothing again and an overwrite with a new id is known for a new asking.
|
|
type seen struct {
|
|
Key string `json:"key,omitempty"`
|
|
ID string `json:"id,omitempty"`
|
|
Rev uint64 `json:"rev"`
|
|
}
|
|
|
|
func gateRecordKey(key string) string { return "gate." + key }
|
|
func recordKey(id string) string { return "key." + id }
|
|
func keptKey(id string) string { return "ask." + id }
|
|
func condKey(id string) string { return "cond." + id }
|
|
|
|
// condID is what a condition of an ask is kept and named by: the gate key it was asked for, so the asking that follows a bad
|
|
// answer clears the condition of the one before it. An id the teller holds no key for is its own.
|
|
func (t *Teller) condID(id string) string {
|
|
if s, e, _ := t.seenOf(id); e != nil && s.Key != "" {
|
|
return s.Key
|
|
}
|
|
return id
|
|
}
|
|
|
|
func (t *Teller) seenAt(rk string) (seen, *stdio.StateEntry, error) {
|
|
e, err := t.Store.Get(rk)
|
|
if err != nil || e == nil {
|
|
return seen{}, nil, err
|
|
}
|
|
var s seen
|
|
if err := json.Unmarshal(e.Value, &s); err != nil {
|
|
return seen{}, nil, fmt.Errorf("the record %s cannot be read: %w", rk, err)
|
|
}
|
|
return s, e, nil
|
|
}
|
|
|
|
// seenOf is the record of a router id: the gate key it was asked for.
|
|
func (t *Teller) seenOf(id string) (seen, *stdio.StateEntry, error) { return t.seenAt(recordKey(id)) }
|
|
|
|
func (t *Teller) keptOf(id string) (*occ.Kept, uint64, error) {
|
|
e, err := t.Store.Get(keptKey(id))
|
|
if err != nil || e == nil {
|
|
return nil, 0, err
|
|
}
|
|
var k occ.Kept
|
|
if err := json.Unmarshal(e.Value, &k); err != nil {
|
|
return nil, 0, fmt.Errorf("the record %s cannot be read: %w", keptKey(id), err)
|
|
}
|
|
return &k, e.Revision, nil
|
|
}
|
|
|
|
// open says whether an ask is made and neither answered nor ended.
|
|
func open(k *occ.Kept) bool { return k != nil && !k.Acted && k.Ended == "" }
|
|
|
|
// note keeps that the asking with this router id is the latest of the gate key, made at revision rev of it.
|
|
func (t *Teller) note(id, key string, rev uint64, prior *stdio.StateEntry) error {
|
|
if _, err := t.Store.Create(recordKey(id), seen{Key: key, Rev: rev}); err != nil && !errors.Is(err, stdio.ErrChanged) {
|
|
return err
|
|
}
|
|
g := seen{ID: id, Rev: rev}
|
|
if prior == nil {
|
|
_, err := t.Store.Create(gateRecordKey(key), g)
|
|
return err
|
|
}
|
|
_, err := t.Store.Update(gateRecordKey(key), g, prior.Revision)
|
|
return err
|
|
}
|
|
|
|
// OnAsk handles the gate's asks key put at revision rev. Each asking has its own router id, which is the value's ask.id and
|
|
// never one recomputed from the key (mesh-sdk go/delivery Ask.Asking, AskingID): the router keeps an ended ask and drops a
|
|
// second ask under its id. So the ask is published once per id; a key overwritten at a higher revision under a new id is a
|
|
// new asking, and the earlier asking's router ask is cancelled first. A key whose value is not the ask its name says, one
|
|
// already answered, and a revision the teller has handled are left. An ask still open and unchanged is only noted (and its
|
|
// warrant read from the router, in case it came while the teller was away). A value that changed its words under the id
|
|
// it was asked under is cancelled and not asked again: the router would drop it, so the gate must give it a new asking.
|
|
func (t *Teller) OnAsk(key string, rev uint64, value []byte) {
|
|
t.mu.Lock()
|
|
defer t.mu.Unlock()
|
|
ga, err := decodeGateAsk(value)
|
|
if err != nil {
|
|
t.logf("the gate's key %s at revision %d is not an ask, and is left: %v", key, rev, err)
|
|
return
|
|
}
|
|
if want, err := delivery.MergeAskKey(ga.Group, ga.Heads); err != nil || want != key {
|
|
t.logf("the gate's key %s at revision %d holds the ask of %q, not its own, and is left (%v)", key, rev, want, err)
|
|
return
|
|
}
|
|
id := ga.Ask.ID
|
|
if raw, _, has, err := t.Answers.Get(key); err != nil {
|
|
t.logf("%s: the answers could not be read, so the ask is left: %v", key, err)
|
|
return
|
|
} else if has && answeredAsking(raw) == id {
|
|
return // this asking is answered; an earlier asking's answer does not stop a later asking
|
|
}
|
|
prior, grec, err := t.seenAt(gateRecordKey(key))
|
|
if err != nil {
|
|
t.logf("%s: %v", key, err)
|
|
return
|
|
}
|
|
if grec != nil && rev <= prior.Rev {
|
|
if k, _, err := t.keptOf(prior.ID); err == nil && open(k) {
|
|
t.catchUp(prior.ID)
|
|
}
|
|
return
|
|
}
|
|
if grec != nil && prior.ID != id {
|
|
// A new asking of the group at these heads: the earlier one is taken back.
|
|
t.cancelOpen(key, prior.ID)
|
|
}
|
|
k, _, err := t.keptOf(id)
|
|
if err != nil {
|
|
t.logf("%s: %v", key, err)
|
|
return
|
|
}
|
|
if k != nil {
|
|
switch {
|
|
case open(k) && occ.Digest(k.Ask) == occ.Digest(ga.Ask):
|
|
_ = t.note(id, key, rev, grec)
|
|
t.catchUp(id)
|
|
case open(k):
|
|
t.cancelOpen(key, id)
|
|
_ = t.note(id, key, rev, grec)
|
|
t.logf("%s: the ask %s changed its words at revision %d under the same id: cancelled, and not asked again, for the router drops a second ask under an id (the gate gives a new asking a new id)", key, id, rev)
|
|
default:
|
|
_ = t.note(id, key, rev, grec)
|
|
t.logf("%s: the ask %s ended, and the gate wrote it again under the same id at revision %d: not asked again, the router would drop it", key, id, rev)
|
|
}
|
|
return
|
|
}
|
|
if err := t.note(id, key, rev, grec); err != nil {
|
|
t.logf("%s: the record of the ask %s could not be kept, so it is left: %v", key, id, err)
|
|
return
|
|
}
|
|
if err := t.Asker.Ask(ga.Ask); err != nil {
|
|
t.logf("%s at revision %d: the ask %s was not asked: %v", key, rev, id, err)
|
|
if kept, _, _ := t.keptOf(id); open(kept) {
|
|
// Kept but not published: forgotten, so the next revision of the key asks it.
|
|
_ = t.Store.Delete(keptKey(id))
|
|
}
|
|
return
|
|
}
|
|
t.logf("%s at revision %d: the ask %s is asked of the operator (asking %d)", key, rev, id, ga.Asking)
|
|
}
|
|
|
|
// cancelOpen takes an open ask back at the router and marks it ended here, since the router's word that it ended may come
|
|
// later. An ask that is not open is left.
|
|
func (t *Teller) cancelOpen(key, id string) {
|
|
k, krev, err := t.keptOf(id)
|
|
if err != nil {
|
|
t.logf("%s: %v", key, err)
|
|
return
|
|
}
|
|
if !open(k) {
|
|
return
|
|
}
|
|
if err := t.Asker.Cancel(id); err != nil {
|
|
t.logf("%s: the ask %s could not be cancelled: %v", key, id, err)
|
|
return
|
|
}
|
|
k.Ended = occ.OutcomeCancelled
|
|
if _, err := t.Store.Update(keptKey(id), k, krev); err != nil && !errors.Is(err, stdio.ErrChanged) {
|
|
t.logf("%s: the cancelled ask %s could not be marked: %v", key, id, err)
|
|
}
|
|
t.logf("%s: the ask %s is cancelled", key, id)
|
|
}
|
|
|
|
// OnAskGone handles the gate's asks key taken away (a withdrawn ask): the latest asking's router ask is cancelled, and an
|
|
// answer to it merges nothing.
|
|
func (t *Teller) OnAskGone(key string) {
|
|
t.mu.Lock()
|
|
defer t.mu.Unlock()
|
|
if _, _, _, err := delivery.ParseAskKey(key); err != nil {
|
|
t.logf("the gate's key %s was taken away and is no ask key, and is left: %v", key, err)
|
|
return
|
|
}
|
|
prior, grec, err := t.seenAt(gateRecordKey(key))
|
|
if err != nil || grec == nil {
|
|
if err != nil {
|
|
t.logf("%s: %v", key, err)
|
|
}
|
|
return
|
|
}
|
|
t.cancelOpen(key, prior.ID)
|
|
}
|
|
|
|
// catchUp reads the router's record of an ask the teller made and hands its warrant on, as the router's event would:
|
|
// a warrant that came while the teller was away.
|
|
func (t *Teller) catchUp(id string) {
|
|
state, w, err := t.Asker.Where(id)
|
|
if err != nil {
|
|
t.logf("the router's record of the ask %s could not be read: %v", id, err)
|
|
return
|
|
}
|
|
if w == nil {
|
|
if state == "" {
|
|
// The router holds no record of an ask the teller holds open: it refused it (a refused ask is not kept) while
|
|
// the teller was away and did not hear it, or has not read it yet. It waits for room, and is asked again at the
|
|
// start or at the next end; the router drops a second ask under an id it holds (review of 38116fe0).
|
|
t.unheard(id)
|
|
}
|
|
return
|
|
}
|
|
t.logf("the ask %s is %s at the router: its warrant is handled now", id, state)
|
|
h, err := t.Asker.Take(*w, t.Answer)
|
|
t.said(h, *w, err)
|
|
}
|
|
|
|
// mergeAct is what the approve option of the gate's ask binds: the ask's verb (delivery.AskVerb: mesh-delivery.merge, or
|
|
// mesh-delivery.settle for a person's settle ask, novox/hq ADR 0556 clause 5), this ask, the answer, and the heads it covers.
|
|
// It is spelled as the gate composes it (mesh-catalog modules/mesh-delivery-gate approval.go mergeAct, from
|
|
// modules/mesh-delivery approval.go:63-65) and as mesh-controller verifies it (cmd/mesh-controller/moved.go:98-106).
|
|
func mergeAct(verb, ask, option string, heads map[string]string) occ.Act {
|
|
var out []string
|
|
for id, sha := range heads {
|
|
out = append(out, id+"="+sha)
|
|
}
|
|
sort.Strings(out)
|
|
return occ.Act{"verb": verb, "ask": ask, "option": option, "heads": strings.Join(out, ",")}
|
|
}
|
|
|
|
// headsOf is the heads an ask covers as an act names them: a delivery's id, `owner/repository@<twelve characters of its
|
|
// commit>` with the repository in lower case, as the gate's IDOf spells it (mesh-catalog modules/mesh-delivery-gate model.go),
|
|
// and its whole commit.
|
|
func headsOf(heads []delivery.Head) map[string]string {
|
|
out := map[string]string{}
|
|
for _, h := range heads {
|
|
short := h.Head
|
|
if len(short) > 12 {
|
|
short = short[:12]
|
|
}
|
|
out[strings.ToLower(h.Repository)+"@"+short] = h.Head
|
|
}
|
|
return out
|
|
}
|
|
|
|
// answeredAsking is the router id of the asking an answer is for: its warrant's id before the "@". An answer that cannot be
|
|
// read answers no asking.
|
|
func answeredAsking(raw []byte) string {
|
|
a, err := delivery.DecodeAnswer(raw)
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
id, _, _ := strings.Cut(a.Warrant, "@")
|
|
return id
|
|
}
|
|
|
|
// warrantID names a warrant, which carries no id of its own: the ask and the time the router gave it.
|
|
func warrantID(w oc.Warrant) string {
|
|
at, err := occ.Time(w.At)
|
|
if err != nil {
|
|
return w.Ask + "@" + w.At
|
|
}
|
|
return w.Ask + "@" + at.UTC().Format(time.RFC3339Nano)
|
|
}
|
|
|
|
// Answer is the act of a warrant that holds the ask (occ.Client.Take has checked its asker, the ask's digest, the
|
|
// option and its level, and the time): the option's bound act must be the act of this ask and the heads it covers,
|
|
// and then the answer is written under the ask's key, once. An error is a warrant that does not hold here.
|
|
func (t *Teller) Answer(o oc.Option, w oc.Warrant) error {
|
|
s, srec, err := t.seenOf(w.Ask)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if srec == nil {
|
|
return fmt.Errorf("the teller holds no gate key for the ask %s", w.Ask)
|
|
}
|
|
if isAssignerKey(s.Key) {
|
|
return t.answerAssigner(s.Key, o, w)
|
|
}
|
|
raw, found, err := t.Gate.Get(s.Key)
|
|
if err != nil {
|
|
return fmt.Errorf("the gate's ask %s could not be read: %w", s.Key, err)
|
|
}
|
|
if !found {
|
|
t.logf("%s: the ask %s was answered and the gate has taken it away: an answer to a withdrawn ask merges nothing", s.Key, w.Ask)
|
|
return nil
|
|
}
|
|
ga, err := decodeGateAsk(raw)
|
|
if err != nil {
|
|
return fmt.Errorf("the gate's ask %s is not an ask: %w", s.Key, err)
|
|
}
|
|
if ga.Ask.ID != w.Ask {
|
|
t.logf("%s: the ask %s was answered, and the gate has asked again under %s: an answer to an earlier asking merges nothing", s.Key, w.Ask, ga.Ask.ID)
|
|
return nil
|
|
}
|
|
if occ.Digest(ga.Ask) != w.AskDigest {
|
|
return fmt.Errorf("the gate's ask %s is no longer the ask the operator answered (its digest is %s, the warrant's %s)",
|
|
s.Key, occ.Digest(ga.Ask), w.AskDigest)
|
|
}
|
|
if err := occ.Performs(o, mergeAct(delivery.AskVerb(ga.Group), w.Ask, o.ID, headsOf(ga.Heads))); err != nil {
|
|
return err
|
|
}
|
|
body, err := delivery.EncodeAnswer(delivery.Answer{Option: o.ID, Warrant: warrantID(w), Words: occ.Says(w)})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return t.writeAnswer(s.Key, w.Ask, body, occ.Says(w))
|
|
}
|
|
|
|
// writeAnswer writes the answer to the asking askID under the ask's key by compare-and-set: created when the key has no answer;
|
|
// updated at the revision read when it holds an earlier asking's answer (the reader ignores that one); left when it holds this
|
|
// asking's.
|
|
func (t *Teller) writeAnswer(key, askID string, body []byte, words string) error {
|
|
akey := delivery.AnswerKey(key)
|
|
cur, crev, found, err := t.Answers.Get(akey)
|
|
if err != nil {
|
|
return fmt.Errorf("the answer under %s could not be read: %w", key, err)
|
|
}
|
|
if found && answeredAsking(cur) == askID {
|
|
t.logf("%s: an answer is there already, and is not written again", key)
|
|
return nil
|
|
}
|
|
written, err := t.Answers.Write(akey, body, crev, found)
|
|
if err != nil {
|
|
return fmt.Errorf("the answer under %s could not be written: %w", key, err)
|
|
}
|
|
if !written {
|
|
t.logf("%s: another writer answered first, and the answer is not written again", key)
|
|
return nil
|
|
}
|
|
t.logf("%s: %s", key, words)
|
|
return nil
|
|
}
|
|
|
|
// Said is what the teller does with what became of a warrant: a good answer clears the condition of its ask; a warrant
|
|
// that does not hold, or that the router refused, is said as a condition (ADR 0437 §9). Nothing is written for either.
|
|
// An ask the router refused for want of room is not said: it waits, and is asked again when one of the teller's asks
|
|
// ends (issue 1610).
|
|
func (t *Teller) Said(h occ.Handled, w oc.Warrant, err error) {
|
|
t.mu.Lock()
|
|
defer t.mu.Unlock()
|
|
t.said(h, w, err)
|
|
}
|
|
|
|
// said is Said under the lock (catchUp holds it).
|
|
func (t *Teller) said(h occ.Handled, w oc.Warrant, err error) {
|
|
if w.Outcome == occ.OutcomeRefused && forWantOfRoom(w.Words) {
|
|
if h == occ.Recorded {
|
|
t.roomRefused(w)
|
|
}
|
|
return // a second word of the same refusal is no end of an ask
|
|
}
|
|
t.saidOnce(h, w, err)
|
|
if h == occ.Acted || h == occ.Recorded || h == occ.Done {
|
|
// One of the teller's asks ended at the router, so it has room for one more: the first ask waiting is asked again.
|
|
t.endWait(w.Ask)
|
|
t.askWaiting(1)
|
|
}
|
|
}
|
|
|
|
func (t *Teller) saidOnce(h occ.Handled, w oc.Warrant, err error) {
|
|
switch {
|
|
case h == occ.Acted && err == nil:
|
|
t.logf("the warrant of %s was %s", w.Ask, h)
|
|
t.clear(w.Ask)
|
|
case h == occ.Refused || (h == occ.Acted && err != nil):
|
|
t.logf("the warrant of %s does not hold, and no answer is written: %v", w.Ask, err)
|
|
t.raise(w.Ask, t.kindOf(w.Ask).answerHeadline, "its answer did not hold here: "+oneLine(err))
|
|
case h == occ.Recorded && w.Outcome == occ.OutcomeRefused:
|
|
t.logf("the router refused the ask %s: %s", w.Ask, w.Words)
|
|
t.raise(w.Ask, t.kindOf(w.Ask).refusedHeadline, "the router refused it: "+w.Words)
|
|
case err != nil:
|
|
t.logf("the warrant of %s: %v", w.Ask, err)
|
|
}
|
|
}
|
|
|
|
func oneLine(err error) string {
|
|
if err == nil {
|
|
return "no reason was given"
|
|
}
|
|
s := strings.Join(strings.Fields(err.Error()), " ")
|
|
if r := []rune(s); len(r) > 200 {
|
|
return string(r[:199]) + "…"
|
|
}
|
|
return s
|
|
}
|
|
|
|
// conditionKey is the key of the condition about an ask.
|
|
func conditionKey(id string) string { return delivery.TellerModule + ".merge-ask." + id }
|
|
|
|
// askWords are the words the teller's conditions use for one kind of ask: the gate's merge ask, or one of the assigner's.
|
|
type askWords struct {
|
|
scope, noun, needs, nothing, resolved string
|
|
answerHeadline, refusedHeadline, unaskedHeadline string
|
|
}
|
|
|
|
var (
|
|
mergeWords = askWords{scope: "merge", noun: "The merge ask ", needs: "to ask for the Approve again", nothing: "Nothing was merged.",
|
|
resolved: "A good answer to the merge ask was written.", answerHeadline: "A merge Approve did not hold",
|
|
refusedHeadline: "A merge ask was refused", unaskedHeadline: "A merge ask could not be asked"}
|
|
assignerWords = askWords{scope: "delivery", noun: "The delivery ask ", needs: "to answer it in the delivery view",
|
|
nothing: "Nothing was changed on a machine.", resolved: "A good answer to the delivery ask was written.",
|
|
answerHeadline: "An answer to a delivery ask did not hold", refusedHeadline: "A delivery ask was refused",
|
|
unaskedHeadline: "A delivery ask could not be asked"}
|
|
)
|
|
|
|
// kindOf is the words for the ask with this router id, by the key it was asked for.
|
|
func (t *Teller) kindOf(askID string) askWords {
|
|
if isAssignerKey(t.condID(askID)) {
|
|
return assignerWords
|
|
}
|
|
return mergeWords
|
|
}
|
|
|
|
func (t *Teller) raise(askID, headline, why string) {
|
|
if t.Say == nil {
|
|
return
|
|
}
|
|
id := t.condID(askID)
|
|
if _, err := t.Store.Create(condKey(id), seen{}); err != nil {
|
|
if errors.Is(err, stdio.ErrChanged) {
|
|
return // raised already
|
|
}
|
|
t.logf("the condition of %s could not be kept: %v", id, err)
|
|
}
|
|
k := t.kindOf(askID)
|
|
needs := k.needs
|
|
c := oc.Condition{Key: conditionKey(id), Severity: occ.SeverityWarning, Subject: oc.ConditionSubject{Scope: k.scope, ID: id}, State: occ.StateRaised,
|
|
Summary: k.noun + id + ": " + why, Headline: headline, Needs: needs,
|
|
Explanation: occ.NeedsYou + needs + ". " + k.noun + id + ", " + why + ". " + k.nothing,
|
|
Resolved: k.resolved}
|
|
if err := t.Say(c); err != nil {
|
|
t.logf("the condition of %s could not be said: %v", id, err)
|
|
_ = t.Store.Delete(condKey(id))
|
|
}
|
|
}
|
|
|
|
func (t *Teller) clear(askID string) {
|
|
if t.Say == nil {
|
|
return
|
|
}
|
|
id := t.condID(askID)
|
|
e, err := t.Store.Get(condKey(id))
|
|
if err != nil || e == nil {
|
|
return
|
|
}
|
|
c := oc.Condition{Key: conditionKey(id), Subject: oc.ConditionSubject{Scope: t.kindOf(askID).scope, ID: id}, State: occ.StateCleared}
|
|
if err := t.Say(c); err != nil {
|
|
t.logf("the condition of %s could not be cleared: %v", id, err)
|
|
return
|
|
}
|
|
_ = t.Store.Delete(condKey(id))
|
|
}
|
|
|
|
// Room at the router (issue 1610). The router holds at most occ.MostOpen open asks of one asker and refuses another at once
|
|
// in words, keeping no record of it (mesh-catalog modules/messenger cmd/messenger/asks.go:524-531, refuseAsk), so the
|
|
// same router id may be asked again. Merges come in bursts: on 2026-10-21 at 11:21 a fourth merge ask was refused so, and
|
|
// nothing asked it again, so the operator was never asked. Now an ask refused for want of room waits in the record
|
|
// `room`, and each time one of the teller's asks ends at the router (a warrant of any outcome: the room it held is free)
|
|
// the first that waits is asked again, under its own router id and a bus message id of its own (the bus drops a second
|
|
// message under an id it holds). It is asked again at most mostRoomRefusals times; refused that often it is said as a
|
|
// condition, as any refusal is. An ask the gate took away or asked again under a newer asking meanwhile, or one already
|
|
// answered, is the gate's no more, and is dropped from the wait. The teller holds no timer: an end, and its start, are the
|
|
// events that ask again.
|
|
|
|
// mostRoomRefusals is how many times an ask is refused for want of room before it is said as a condition and asked no
|
|
// more: each refusal follows an end of another ask, so it is that many asks answered while this one could not be asked.
|
|
const mostRoomRefusals = 6
|
|
|
|
// roomRecord is the key of the asks that wait for room, in the order they were refused.
|
|
const roomRecord = "room"
|
|
|
|
// waitingAsk is one ask that waits for room: the gate key it was asked for, its router id, how often the router refused
|
|
// it for want of room, and whether it is asked again and its word not yet heard.
|
|
type waitingAsk struct {
|
|
Key string `json:"key"`
|
|
ID string `json:"id"`
|
|
Refusals int `json:"refusals"`
|
|
Asked bool `json:"asked,omitempty"`
|
|
// Tries is how many times it was asked again: each time under a bus message id of its own, also across a start.
|
|
Tries int `json:"tries,omitempty"`
|
|
}
|
|
|
|
// forWantOfRoom says whether a refusal's words are the router's for an asker that already holds its most open asks
|
|
// ("its asker already has 3 open asks", asks.go:530).
|
|
func forWantOfRoom(words string) bool {
|
|
return strings.Contains(words, "already has") && strings.Contains(words, "open asks")
|
|
}
|
|
|
|
func (t *Teller) now() time.Time {
|
|
if t.Now != nil {
|
|
return t.Now()
|
|
}
|
|
return time.Now()
|
|
}
|
|
|
|
func (t *Teller) waiting() ([]waitingAsk, *stdio.StateEntry, error) {
|
|
e, err := t.Store.Get(roomRecord)
|
|
if err != nil || e == nil {
|
|
return nil, nil, err
|
|
}
|
|
var ws []waitingAsk
|
|
if err := json.Unmarshal(e.Value, &ws); err != nil {
|
|
return nil, nil, fmt.Errorf("the record %s cannot be read: %w", roomRecord, err)
|
|
}
|
|
return ws, e, nil
|
|
}
|
|
|
|
func (t *Teller) keepWaiting(ws []waitingAsk, prior *stdio.StateEntry) error {
|
|
if ws == nil {
|
|
ws = []waitingAsk{}
|
|
}
|
|
var err error
|
|
if prior == nil {
|
|
_, err = t.Store.Create(roomRecord, ws)
|
|
} else {
|
|
_, err = t.Store.Update(roomRecord, ws, prior.Revision)
|
|
}
|
|
if err != nil {
|
|
t.logf("the record of the asks waiting for room could not be kept: %v", err)
|
|
}
|
|
return err
|
|
}
|
|
|
|
// roomRefused keeps an ask the router refused for want of room waiting, or, refused that way mostRoomRefusals times,
|
|
// says it as a condition and drops it.
|
|
func (t *Teller) roomRefused(w oc.Warrant) {
|
|
ws, rec, err := t.waiting()
|
|
if err != nil {
|
|
t.logf("%v", err)
|
|
return
|
|
}
|
|
i := slices.IndexFunc(ws, func(x waitingAsk) bool { return x.ID == w.Ask })
|
|
if i < 0 {
|
|
s, _, _ := t.seenOf(w.Ask)
|
|
ws = append(ws, waitingAsk{Key: s.Key, ID: w.Ask})
|
|
i = len(ws) - 1
|
|
}
|
|
ws[i].Refusals++
|
|
ws[i].Asked = false
|
|
n := ws[i].Refusals
|
|
if n >= mostRoomRefusals {
|
|
ws = slices.Delete(ws, i, i+1)
|
|
if t.keepWaiting(ws, rec) != nil {
|
|
return
|
|
}
|
|
t.logf("the router refused the ask %s for want of room %d times (%s): it is asked no more, and said", w.Ask, n, w.Words)
|
|
t.raise(w.Ask, t.kindOf(w.Ask).unaskedHeadline, fmt.Sprintf("the router refused it %d times for want of room: %s", n, w.Words))
|
|
return
|
|
}
|
|
if t.keepWaiting(ws, rec) != nil {
|
|
return
|
|
}
|
|
t.logf("the router refused the ask %s for want of room (%s): it waits, and is asked again when one of the teller's asks ends (refused %d of at most %d times)",
|
|
w.Ask, w.Words, n, mostRoomRefusals)
|
|
}
|
|
|
|
// endWait drops an ask from the wait once the router said it ended otherwise than for want of room.
|
|
func (t *Teller) endWait(id string) {
|
|
ws, rec, err := t.waiting()
|
|
if err != nil || rec == nil {
|
|
return
|
|
}
|
|
i := slices.IndexFunc(ws, func(x waitingAsk) bool { return x.ID == id })
|
|
if i < 0 {
|
|
return
|
|
}
|
|
_ = t.keepWaiting(slices.Delete(ws, i, i+1), rec)
|
|
}
|
|
|
|
// OnCurrent is the watch of the gate's asks having handed over every key it holds now, at the teller's start: an ask that
|
|
// waited for room while the teller was away is asked again (the asks that held the room may have ended meanwhile).
|
|
//
|
|
// An ask asked again whose word the teller did not hear (the router's warrants come on a plain subscription) waits once
|
|
// more when the router holds no record of it: the router keeps none of a refused ask (review of 38116fe0). Up to
|
|
// occ.MostOpen are asked, the most the router can have room for.
|
|
func (t *Teller) OnCurrent() {
|
|
t.mu.Lock()
|
|
defer t.mu.Unlock()
|
|
ws, rec, err := t.waiting()
|
|
if err != nil {
|
|
t.logf("%v", err)
|
|
return
|
|
}
|
|
changed := false
|
|
for i, x := range ws {
|
|
if !x.Asked {
|
|
continue
|
|
}
|
|
state, w, err := t.Asker.Where(x.ID)
|
|
if err != nil {
|
|
t.logf("the router's record of the ask %s could not be read: %v", x.ID, err)
|
|
continue
|
|
}
|
|
if state == "" && w == nil {
|
|
t.logf("%s: the ask %s, asked again, is not on the router's record: its refusal was not heard, and it waits again", x.Key, x.ID)
|
|
ws[i].Asked, changed = false, true
|
|
}
|
|
}
|
|
if changed && t.keepWaiting(ws, rec) != nil {
|
|
return
|
|
}
|
|
t.askWaiting(occ.MostOpen)
|
|
}
|
|
|
|
// unheard puts an ask the teller holds open and the router holds no record of in the wait for room, once.
|
|
func (t *Teller) unheard(id string) {
|
|
ws, rec, err := t.waiting()
|
|
if err != nil {
|
|
t.logf("%v", err)
|
|
return
|
|
}
|
|
if slices.ContainsFunc(ws, func(x waitingAsk) bool { return x.ID == id }) {
|
|
return
|
|
}
|
|
s, _, _ := t.seenOf(id)
|
|
if t.keepWaiting(append(ws, waitingAsk{Key: s.Key, ID: id}), rec) == nil {
|
|
t.logf("%s: the ask %s is open here and not on the router's record: it waits to be asked again", s.Key, id)
|
|
}
|
|
}
|
|
|
|
// askWaiting asks again the first n asks that wait for room and are not asked yet, dropping on the way every one the gate
|
|
// no longer asks. One at an end: one ask ended, so there is room for one.
|
|
func (t *Teller) askWaiting(n int) {
|
|
ws, rec, err := t.waiting()
|
|
if err != nil {
|
|
t.logf("%v", err)
|
|
return
|
|
}
|
|
if rec == nil {
|
|
return
|
|
}
|
|
changed := false
|
|
defer func() {
|
|
if changed {
|
|
_ = t.keepWaiting(ws, rec)
|
|
}
|
|
}()
|
|
for i := 0; i < len(ws); {
|
|
x := ws[i]
|
|
if x.Asked {
|
|
i++
|
|
continue
|
|
}
|
|
assigner := isAssignerKey(x.Key)
|
|
if assigner && !t.assignerRoom() {
|
|
i++ // the assigner's asks hold their most open: it waits, and a merge ask after it may be asked
|
|
continue
|
|
}
|
|
a, why := t.stillAsked(x)
|
|
if why != "" {
|
|
t.logf("%s: the ask %s, waiting for room, is not asked again: %s", x.Key, x.ID, why)
|
|
ws, changed = slices.Delete(ws, i, i+1), true
|
|
continue
|
|
}
|
|
ws[i].Tries, changed = x.Tries+1, true
|
|
if err := t.askAgain(a, fmt.Sprintf("%s.again-%d", a.ID, ws[i].Tries)); err != nil {
|
|
t.logf("%s: the ask %s, waiting for room, could not be asked again, and waits for the next end: %v", x.Key, x.ID, err)
|
|
return
|
|
}
|
|
ws[i].Asked = true
|
|
if assigner {
|
|
t.assignerAsked(x.ID)
|
|
}
|
|
t.logf("%s: the ask %s is asked of the operator again (refused %d times for want of room)", x.Key, x.ID, x.Refusals)
|
|
if n--; n <= 0 {
|
|
return
|
|
}
|
|
i++
|
|
}
|
|
}
|
|
|
|
// stillAsked is the gate's ask a waiting ask is for, when the gate still asks it under the same id and it is unanswered;
|
|
// otherwise why it is not.
|
|
func (t *Teller) stillAsked(x waitingAsk) (oc.Ask, string) {
|
|
if x.Key == "" {
|
|
return oc.Ask{}, "the teller holds no gate key for it"
|
|
}
|
|
if isAssignerKey(x.Key) {
|
|
return t.stillAskedByAssigner(x)
|
|
}
|
|
raw, found, err := t.Gate.Get(x.Key)
|
|
if err != nil {
|
|
return oc.Ask{}, "the gate's ask could not be read: " + err.Error()
|
|
}
|
|
if !found {
|
|
return oc.Ask{}, "the gate has taken it away"
|
|
}
|
|
ga, err := decodeGateAsk(raw)
|
|
if err != nil {
|
|
return oc.Ask{}, "the gate's key holds no ask: " + err.Error()
|
|
}
|
|
if ga.Ask.ID != x.ID {
|
|
return oc.Ask{}, "the gate asks again under " + ga.Ask.ID
|
|
}
|
|
if cur, _, has, err := t.Answers.Get(delivery.AnswerKey(x.Key)); err == nil && has && answeredAsking(cur) == x.ID {
|
|
return oc.Ask{}, "it is answered"
|
|
}
|
|
if err := occ.Check(ga.Ask, t.now()); err != nil {
|
|
return oc.Ask{}, "it no longer holds: " + err.Error()
|
|
}
|
|
return ga.Ask, ""
|
|
}
|
|
|
|
// stillAskedByAssigner is stillAsked for one of the assigner's asks.
|
|
func (t *Teller) stillAskedByAssigner(x waitingAsk) (oc.Ask, string) {
|
|
a, found, err := t.assignerAsk(x.Key)
|
|
switch {
|
|
case err != nil:
|
|
return oc.Ask{}, err.Error()
|
|
case !found:
|
|
return oc.Ask{}, "the assigner has taken it away"
|
|
case a.Ask.ID != x.ID:
|
|
return oc.Ask{}, "the assigner asks again under " + a.Ask.ID
|
|
}
|
|
if cur, _, has, err := t.Answers.Get(delivery.AnswerKey(x.Key)); err == nil && has && answeredAsking(cur) == x.ID {
|
|
return oc.Ask{}, "it is answered"
|
|
}
|
|
if err := occ.Check(a.Ask, t.now()); err != nil {
|
|
return oc.Ask{}, "it no longer holds: " + err.Error()
|
|
}
|
|
return a.Ask, ""
|
|
}
|
|
|
|
// askAgain keeps the ask afresh as the asker would (its record of the refusal is replaced, so its warrant is taken) and
|
|
// publishes it under the bus message id given.
|
|
func (t *Teller) askAgain(a oc.Ask, msgID string) error {
|
|
if t.Publish == nil {
|
|
return errors.New("the teller has no bus to ask on")
|
|
}
|
|
if err := t.Store.Delete(keptKey(a.ID)); err != nil {
|
|
return err
|
|
}
|
|
if _, err := t.Store.Create(keptKey(a.ID), occ.Kept{Ask: a, Asked: t.now()}); err != nil {
|
|
return err
|
|
}
|
|
if err := t.Publish(oc.AcceptSubjectAsk(delivery.TellerModule), a, msgID); err != nil {
|
|
_ = t.Store.Delete(keptKey(a.ID))
|
|
return err
|
|
}
|
|
return nil
|
|
}
|