S8 unread-away: Unread of a state and a view, the away answers, the start's phase

Builds: design 47 H2 H-A-708 (1) (2), H-A-717 (DQ-54, DQ-58)
This commit is contained in:
2026-11-25 17:09:15 +01:00
parent b4f9429115
commit f0bc6a3ceb
11 changed files with 294 additions and 6 deletions
+20
View File
@@ -14,6 +14,7 @@ import (
"github.com/nats-io/nats.go/jetstream"
"git.novox.be/novox/mesh-sdk/go/buscalls"
"git.novox.be/novox/mesh-sdk/go/busretry"
)
// Follower is a reader's picture of one state of an owner (ADR 0615 §4 to §6): it asks the owner's first-view tool at
@@ -67,6 +68,7 @@ type Follower struct {
// catching is set while catchUp runs: one catch-up at a time.
catching bool
ready chan struct{}
born time.Time // when the follower began (Unread)
}
// Status is what a reader's read tool says of one first view (design 25 § 12.2): the owner, the tool and the revision
@@ -186,6 +188,7 @@ func (f *Follower) init() {
f.values = map[string]json.RawMessage{}
f.ready = make(chan struct{})
f.st = Status{Owner: f.Owner, Tool: f.Tool, State: f.State}
f.born = f.now()
}
}
@@ -222,6 +225,23 @@ func (f *Follower) Ready() <-chan struct{} {
return f.ready
}
// Unread says whether the first view was not answered yet: the fault `away` (busretry.Away, design 47 H2 §776) naming the
// view's owner verb (its Tool) and saying "not read yet since <time>" until Ready closes, nil after. A handler asks it before
// it reads, so a call made while the first view is not in is answered `away` at once; Get and Values keep answering what the
// picture holds, an empty one before the first view (design 47 H2 H-A-708 (2)).
func (f *Follower) Unread() error {
f.mu.Lock()
f.init()
ready, since := f.ready, f.born
f.mu.Unlock()
select {
case <-ready:
return nil
default:
}
return &busretry.Away{Part: f.Tool, Words: "not read yet since " + since.UTC().Format("15:04:05")}
}
// Values is a copy of the picture.
func (f *Follower) Values() map[string]json.RawMessage {
f.mu.Lock()
+41
View File
@@ -77,6 +77,8 @@ type ProcessReader struct {
unheard int
lastUnheard string
startedAt time.Time
// phase is the phase the holder's start is in, nil while none runs (BeginPhase).
phase *StartPhase
// hooked is the hub the count of unhandled events is wired on, so a second Start counts no event twice.
hooked *process.EventHub
}
@@ -939,6 +941,7 @@ func (r *ProcessReader) Start(ctx context.Context, js jetstream.JetStream, cred
if status != nil {
status.Ready(func(kv jetstream.KeyValue) {
KeepOwnBuild(ctx, kv, process.OwnBuild(), r.logf)
r.keepPhase()
if err := hearAgain(ctx, kv, r.Followers, r.logf); err != nil {
r.logf("views: the hand asks of %s's first views are not heard (%v) until it starts again", r.Module, err)
}
@@ -952,6 +955,44 @@ func (r *ProcessReader) Start(ctx context.Context, js jetstream.JetStream, cred
r.Ask(ctx, said)
}
// BeginPhase says the holder's start is in the phase name since now, ending the one before: it is said in the journal and
// kept at StartPhaseKey of the state `first-views`, which the part's first-views tool answers beside the views, so a call
// that meets `away` while the holder starts says what the start is doing (design 47 H2 H-A-708 (1), H-A-717 DQ-54). EndPhase
// ends it.
func (r *ProcessReader) BeginPhase(name string) {
r.mu.Lock()
r.phase = &StartPhase{Name: name, Since: r.now().UTC()}
r.mu.Unlock()
r.logf("views: %s's start is in the phase %s", r.Module, name)
r.keepPhase()
}
// EndPhase says the holder's start is in no phase.
func (r *ProcessReader) EndPhase() {
r.mu.Lock()
ended := r.phase
r.phase = nil
r.mu.Unlock()
if ended != nil {
r.logf("views: %s's start ends the phase %s", r.Module, ended.Name)
}
r.keepPhase()
}
// keepPhase writes the phase now at StartPhaseKey, once the state is there (a bucket not yet opened keeps it for then).
func (r *ProcessReader) keepPhase() {
r.mu.Lock()
status, phase := r.status, r.phase
r.mu.Unlock()
if status == nil {
return
}
data, _ := json.Marshal(phase)
if _, err := status.Put(context.Background(), StartPhaseKey, data); err != nil {
r.logf("views: the start phase is not kept in %s: %v", StatusState, err)
}
}
// handleEvent registers handle on the hub for a seat's event on its bare subject and on every further part after it
// (`<subject>.>`): one handler, both forms, as the seat's contract says the event on either (design 47 H, H-A-166; one
// subscription per event, design 47 G §532). The two patterns do not overlap, so a message reaches the handler once.
@@ -58,8 +58,35 @@ func subAReaderHearsAnOwnersChangesAsOrdinaryEvents(t *testing.T) {
if err != nil {
t.Fatal(err)
}
// H-A-708 (1), H-A-717 DQ-54: the phase the start is in, set before the state is there, is kept in it once it is, and
// ended with null.
r.BeginPhase("take-in")
r.Start(ctx, js, cred, process.AwaitBucket(ctx, js, nil, "reader_first-views", "", t.Logf))
<-r.Ready()
phases, err := statusKV.Watch(ctx, views.StartPhaseKey)
if err != nil {
t.Fatal(err)
}
phaseNow := func() string {
for e := range phases.Updates() {
if e != nil {
var p *views.StartPhase
_ = json.Unmarshal(e.Value(), &p)
if p == nil {
return "none"
}
return p.Name
}
}
return "closed"
}
if got := phaseNow(); got != "take-in" {
t.Fatalf("the start's phase is %s", got)
}
r.EndPhase()
if got := phaseNow(); got != "none" {
t.Fatalf("the start's phase after it ended is %s", got)
}
pic, err := r.KeyValue(ctx, "o_modules")
if err != nil {
t.Fatal(err)
+16 -2
View File
@@ -19,6 +19,16 @@ import (
// process serves no tool (ADR 0436), so its read and re-read tools, in its tools bundle, meet it here.
const StatusState = "first-views"
// StartPhaseKey is where a process keeps the phase its start is in (design 47 H2 H-A-708 (1), H-A-717 DQ-54), in its state
// `first-views`: a StartPhase, or null while no phase runs. The read tool answers it beside the views, under this name.
const StartPhaseKey = "start-phase"
// StartPhase is the phase a holder's start is in: its name and since when.
type StartPhase struct {
Name string `json:"name"`
Since time.Time `json:"since"`
}
// StatusKey is where a reader keeps its first view of owner's state.
func StatusKey(owner, state string) string { return owner + "." + state }
@@ -125,7 +135,7 @@ func StatusTools(reader, read, again string, st keptStatus, acts HandActs, now f
}
out := []Status{}
for _, k := range keys {
if strings.HasPrefix(k, "again.") || k == OwnBuildKey {
if strings.HasPrefix(k, "again.") || k == OwnBuildKey || k == StartPhaseKey {
continue
}
e, err := st.Get(k)
@@ -144,7 +154,11 @@ func StatusTools(reader, read, again string, st keptStatus, acts HandActs, now f
own = w
}
}
return map[string]any{"first-views": out, OwnBuildKey: own}, nil
var phase *StartPhase
if e, err := st.Get(StartPhaseKey); err == nil && e != nil {
_ = json.Unmarshal(e.Value, &phase)
}
return map[string]any{"first-views": out, OwnBuildKey: own, StartPhaseKey: phase}, nil
}},
{Name: again,
Description: fmt.Sprintf("Ask %s's first view of an owner's state again now, by hand (a hand act, its caller and why kept in "+
+28
View File
@@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"git.novox.be/novox/mesh-sdk/go/busretry"
"maps"
"reflect"
"runtime"
@@ -1534,3 +1535,30 @@ func TestFirstViewsSayTheOwnBuild(t *testing.T) {
t.Errorf("nothing kept: %v", read)
}
}
// Design 47 H2 H-A-708 (1) (2), H-A-717 DQ-54: a first view not yet answered says `away` naming its owner's verb and since when,
// at once, and nothing after Ready; the status tool answers the phase the start is in beside the views.
func TestAFirstViewIsUnreadUntilItsAnswerIsIn(t *testing.T) {
now := time.Date(2026, 11, 25, 23, 35, 40, 0, time.UTC)
f := &Follower{Owner: "mesh-catalog", Tool: "mesh-catalog.seat-definitions", State: "seat-definitions", Now: func() time.Time { return now },
Ask: func(context.Context, string) (Answer, error) { return Answer{}, nil }}
var away *busretry.Away
if err := f.Unread(); !errors.As(err, &away) || away.Part != "mesh-catalog.seat-definitions" || away.Words != "not read yet since 23:35:40" {
t.Fatalf("before the first view: %v", err)
}
if err := f.Start(context.Background()); err != nil {
t.Fatal(err)
}
if err := f.Unread(); err != nil {
t.Fatalf("after the first view: %v", err)
}
st := statusWith{StartPhaseKey: []byte(`{"name":"take-in","since":"2026-11-25T23:35:40Z"}`)}
read, _ := StatusTools("m", "m_first_views", "m_first_view_again", st, nil, time.Now)[0].Run(nil)
if p, ok := read.(map[string]any)[StartPhaseKey].(*StartPhase); !ok || p == nil || p.Name != "take-in" || !p.Since.Equal(now) {
t.Fatalf("the status tool says %v", read)
}
none, _ := StatusTools("m", "r", "a", statusWith{}, nil, time.Now)[0].Run(nil)
if p := none.(map[string]any)[StartPhaseKey].(*StartPhase); p != nil {
t.Fatalf("no phase kept: %v", none)
}
}