networkmanager: the watcher names no node; the bundle fills in its own

The node-engine gives the watcher's process no MESH_NODE, so the watcher refused to
start and crash-looped on g14. It needs none: a kept change holds its event, its
sequence and its body, and the bundle fills its own node into the subject, the message
id <node>.<event>.<sequence> and the body when it publishes, the same at every publish.

Builds: design 08 sign-in-page §11, §12, §20; lead answer L-3 (hq 09c9a3e3)
This commit is contained in:
jochen
2026-11-25 14:32:03 +01:00
parent 17e1bfb6d3
commit e539cf6e99
8 changed files with 151 additions and 58 deletions
@@ -8,6 +8,8 @@ package main
import (
"encoding/json"
"errors"
"fmt"
"sync"
)
@@ -41,22 +43,48 @@ func (p *publisher) publishKept(moment string) publishResult {
return out
}
for i, k := range kept {
if err := p.publish(k.Subject, k.Body, k.MessageID); err != nil {
subject, id, body, err := p.outgoing(k)
if err == nil {
err = p.publish(subject, body, id)
}
if err != nil {
p.refused++
out.Waiting, out.Refused = len(kept)-i, err.Error()
journal("publishing %s at %s was refused or not acknowledged: %v; %d left waiting", k.MessageID, moment, err,
journal("publishing %s at %s was refused or not acknowledged: %v; %d left waiting", k.Name(), moment, err,
out.Waiting)
return out
}
journal("published %s on %s", k.MessageID, k.Subject)
journal("published %s on %s", id, subject)
out.Published++
if err := p.folder.drop(k); err != nil {
journal("deleting the published %s failed: %v", k.MessageID, err)
journal("deleting the published %s failed: %v", id, err)
}
}
return out
}
// outgoing is a kept change as the bundle publishes it on its own node: the seat's subject, the message id
// `<node>.<event>.<sequence>` and the body with its node (§20; lead answer L-3).
func (p *publisher) outgoing(k Kept) (string, string, json.RawMessage, error) {
node := p.folder.Node
if node == "" {
return "", "", nil, errors.New("no node named: the tool runner names the bundle's in MESH_NODE")
}
if k.Event != eventAsked && k.Event != eventEnded {
return "", "", nil, fmt.Errorf("the kept change %s is of no event of the seat", k.Name())
}
var body map[string]json.RawMessage
if err := json.Unmarshal(k.Body, &body); err != nil {
return "", "", nil, fmt.Errorf("the kept change %s holds no body: %w", k.Name(), err)
}
body["node"], _ = json.Marshal(node)
raw, err := json.Marshal(body)
if err != nil {
return "", "", nil, err
}
return eventSubject(k.Event, node), fmt.Sprintf("%s.%s.%d", node, k.Event, k.Sequence), raw, nil
}
// refusedSince is the publishes refused since the bundle's start.
func (p *publisher) refusedSince() int {
p.mu.Lock()
@@ -8,6 +8,7 @@ import (
"testing"
"time"
"git.novox.be/novox/mesh-networking/modules/mesh-connectivity-contracts/go/seats/nodeuplink"
stdio "git.novox.be/novox/mesh-sdk/go"
)
@@ -42,7 +43,7 @@ func (s *fakeStream) publishes() string {
func keepThree(t *testing.T, f Folder) {
t.Helper()
for _, event := range []string{eventEnded, eventAsked, eventEnded} {
if _, err := f.keep(event, eventSubject(event, f.Node), map[string]string{"node": f.Node}); err != nil {
if _, err := f.keep(event, map[string]string{"link": "wlan0"}); err != nil {
t.Fatal(err)
}
}
@@ -60,10 +61,10 @@ func TestTheBundlePublishesTheKeptEventsInOrder(t *testing.T) {
if r := p.publishKept("the bundle's start"); r.Published != 1 || r.Waiting != 2 || p.refusedSince() != 1 {
t.Fatalf("first round %+v, refused %d", r, p.refusedSince())
}
if kept, _ := f.pending(); len(kept) != 2 || kept[0].MessageID != "laptop.sign-in-asked.2" {
if kept, _ := f.pending(); len(kept) != 2 || kept[0].Name() != "sign-in-asked.2" {
t.Fatalf("waiting %+v", kept)
}
if !strings.Contains(strings.Join(lines(), "\n"), "publishing laptop.sign-in-asked.2 at the bundle's start was refused") {
if !strings.Contains(strings.Join(lines(), "\n"), "publishing sign-in-asked.2 at the bundle's start was refused") {
t.Fatalf("the refusal was not written to the journal: %q", lines())
}
if r := p.publishKept("a kept event"); r.Published != 2 || r.Waiting != 0 {
@@ -101,7 +102,7 @@ func TestTheBundlePublishesAtEachMoment(t *testing.T) {
close(done)
}()
files <- struct{}{} // the start's round ran and was refused; this one publishes what waits
if _, err := f.keep(eventAsked, eventSubject(eventAsked, "laptop"), map[string]string{}); err != nil {
if _, err := f.keep(eventAsked, map[string]string{}); err != nil {
t.Fatal(err)
}
files <- struct{}{}
@@ -129,3 +130,32 @@ func TestTheBundlePublishesAtEachMoment(t *testing.T) {
t.Fatalf("published %s", got)
}
}
// The bundle publishes a change the watcher kept with no node under its own: the subject `<event>.<node>`, the message
// id `<node>.<event>.<sequence>` and the body's node, the same at every publish of one file; a bundle that names no node
// publishes nothing and keeps the change (design 08 sign-in-page §20; lead answer L-3).
func TestTheBundleFillsItsNodeIntoAKeptChange(t *testing.T) {
journalLines(t)
watcher := Folder{Dir: t.TempDir()}
if _, err := watcher.keep(eventAsked, nodeuplink.SignInAsked{Link: "wlan0", Network: "guest-example",
Address: "http://check.example.net/", At: "2026-11-24T10:00:00Z"}); err != nil {
t.Fatal(err)
}
var said []string
publish := func(subject string, body json.RawMessage, id string) error {
said = append(said, subject+" "+id+" "+string(body))
return errors.New("no acknowledgement")
}
unnamed := &publisher{folder: watcher, publish: publish}
if r := unnamed.publishKept("a test"); r.Waiting != 1 || !strings.Contains(r.Refused, "no node named") || len(said) != 0 {
t.Fatalf("a bundle with no node: %+v, said %q", r, said)
}
p := &publisher{folder: Folder{Dir: watcher.Dir, Node: "laptop"}, publish: publish}
p.publishKept("a test")
p.publishKept("a test")
want := `mesh.seat.node-uplink.event.sign-in-asked.laptop laptop.sign-in-asked.1 ` +
`{"address":"http://check.example.net/","at":"2026-11-24T10:00:00Z","link":"wlan0","network":"guest-example","node":"laptop"}`
if len(said) != 2 || said[0] != want || said[1] != want {
t.Fatalf("published %q, want twice %s", said, want)
}
}
@@ -53,13 +53,20 @@ type Folder struct {
Node string
}
// theFolder is the account's folder, on the node the node-engine names (MESH_NODE).
// theFolder is the account's folder, on the node the tool runner names the bundle (MESH_NODE).
func theFolder() (Folder, error) {
f, err := accountFolder()
f.Node = os.Getenv("MESH_NODE")
return f, err
}
// accountFolder is the account's folder with no node: the watcher's, which names none (L-3).
func accountFolder() (Folder, error) {
home, err := os.UserHomeDir()
if err != nil {
return Folder{}, err
}
return Folder{Dir: filepath.Join(home, stateHome), Node: os.Getenv("MESH_NODE")}, nil
return Folder{Dir: filepath.Join(home, stateHome)}, nil
}
func (f Folder) path(name string) string { return filepath.Join(f.Dir, name) }
@@ -108,18 +115,22 @@ func (f Folder) nextSequence() (uint64, error) {
return n, nil
}
// Kept is one event the watcher kept for the bundle to publish: the seat's subject, the bus's message id and the body.
// Kept is one change the watcher kept for the bundle to publish: the event's name, its sequence and its body, all
// without the node. The watcher names no node; the bundle fills its own into the subject, the message id and the body's
// node when it publishes (lead answer L-3).
type Kept struct {
Sequence uint64 `json:"-"`
Subject string `json:"subject"`
MessageID string `json:"message_id"`
Body json.RawMessage `json:"body"`
Sequence uint64 `json:"sequence"`
Event string `json:"event"`
Body json.RawMessage `json:"body"`
}
// Name is the kept change as the journal names it: `<event>.<sequence>`.
func (k Kept) Name() string { return fmt.Sprintf("%s.%d", k.Event, k.Sequence) }
func pendingFile(seq uint64) string { return fmt.Sprintf("%010d.json", seq) }
// keep writes one event into pending/: its message id `<node>.<event>.<sequence>`, made once (§20).
func (f Folder) keep(event, subject string, body any) (Kept, error) {
// keep writes one change into pending/ under the next sequence, made once (§20; L-3).
func (f Folder) keep(event string, body any) (Kept, error) {
raw, err := json.Marshal(body)
if err != nil {
return Kept{}, err
@@ -131,7 +142,7 @@ func (f Folder) keep(event, subject string, body any) (Kept, error) {
if err != nil {
return Kept{}, err
}
k := Kept{Sequence: seq, Subject: subject, MessageID: fmt.Sprintf("%s.%s.%d", f.Node, event, seq), Body: raw}
k := Kept{Sequence: seq, Event: event, Body: raw}
data, err := json.Marshal(k)
if err != nil {
return Kept{}, err
@@ -15,19 +15,20 @@ func aFolder(t *testing.T) Folder {
}
// A watcher restarted after its events were published and pending/ emptied makes message ids it never used, so the
// bus keeps them rather than dropping them as duplicates (design 08 sign-in-page §12, §25; R3-1).
// bus keeps them rather than dropping them as duplicates (design 08 sign-in-page §12, §25; R3-1). The watcher's folder
// names no node, and a kept change holds its event, its sequence and its body (L-3).
func TestARestartedWatcherMakesIdsItNeverUsed(t *testing.T) {
f := aFolder(t)
used := map[string]bool{}
f := Folder{Dir: t.TempDir()}
used := map[uint64]bool{}
for range 3 {
k, err := f.keep("sign-in-asked", "mesh.seat.node-uplink.event.sign-in-asked.laptop", map[string]string{"node": "laptop"})
k, err := f.keep("sign-in-asked", map[string]string{"link": "wlan0"})
if err != nil {
t.Fatal(err)
}
used[k.MessageID] = true
used[k.Sequence] = true
}
kept, err := f.pending()
if err != nil || len(kept) != 3 || kept[0].MessageID != "laptop.sign-in-asked.1" || kept[2].Sequence != 3 {
if err != nil || len(kept) != 3 || kept[0].Name() != "sign-in-asked.1" || kept[2].Sequence != 3 {
t.Fatalf("kept %+v %v", kept, err)
}
for _, k := range kept {
@@ -35,10 +36,14 @@ func TestARestartedWatcherMakesIdsItNeverUsed(t *testing.T) {
t.Fatal(err)
}
}
restarted := Folder{Dir: f.Dir, Node: "laptop"}
k, err := restarted.keep("sign-in-ended", "mesh.seat.node-uplink.event.sign-in-ended.laptop", map[string]string{})
if err != nil || used[k.MessageID] || k.MessageID != "laptop.sign-in-ended.4" {
t.Fatalf("the restarted watcher made %q (%v), having used %v", k.MessageID, err, used)
restarted := Folder{Dir: f.Dir}
k, err := restarted.keep("sign-in-ended", map[string]string{})
if err != nil || used[k.Sequence] || k.Name() != "sign-in-ended.4" {
t.Fatalf("the restarted watcher made %q (%v), having used %v", k.Name(), err, used)
}
raw, _ := os.ReadFile(filepath.Join(f.Dir, pendingName, "0000000004.json"))
if string(raw) != `{"sequence":4,"event":"sign-in-ended","body":{}}`+"\n" {
t.Fatalf("the kept file holds %s", raw)
}
if _, err := os.Stat(filepath.Join(f.Dir, pendingName, "0000000004.json")); err != nil {
t.Fatalf("the kept file is not named by its ten-digit sequence: %v", err)
@@ -68,7 +73,7 @@ func TestTheFoldersFilesAreWrittenWhole(t *testing.T) {
if c, err := f.names(); err != nil || c.Result != resultNoHolder || c.Names == nil || c.Failed() {
t.Fatalf("names read %+v %v", c, err)
}
if _, err := f.keep("sign-in-asked", "s", map[string]string{}); err != nil {
if _, err := f.keep("sign-in-asked", map[string]string{}); err != nil {
t.Fatal(err)
}
os.WriteFile(filepath.Join(f.Dir, pendingName, "0000000009.json.new"), []byte("{"), 0o600)
@@ -124,7 +129,7 @@ func TestAPathTheWatcherDoesNotKnowIsLeftAndSaidOnce(t *testing.T) {
sys.set(guest(ratingFull))
w := aWatcher(t, sys)
f := w.Folder
if _, err := f.keep(eventEnded, "s", map[string]string{}); err != nil {
if _, err := f.keep(eventEnded, map[string]string{}); err != nil {
t.Fatal(err)
}
f.writeCheckAddress("http://check.example.net/")
@@ -172,3 +177,15 @@ func TestTheStatedBounds(t *testing.T) {
t.Fatalf("the bounds are %s, %s, %s and %d", socketBound, openBound, sessionWait, keptBound)
}
}
// The watcher reads no MESH_NODE: its folder names no node, whatever the environment says (L-3).
func TestTheWatchersFolderNamesNoNode(t *testing.T) {
t.Setenv("MESH_NODE", "laptop")
t.Setenv("HOME", t.TempDir())
if f, err := accountFolder(); err != nil || f.Node != "" {
t.Fatalf("the watcher's folder is %+v (%v)", f, err)
}
if f, err := theFolder(); err != nil || f.Node != "laptop" {
t.Fatalf("the bundle's folder is %+v (%v)", f, err)
}
}
@@ -180,13 +180,10 @@ func newWatcher(f Folder) *Watcher {
// watch is `uplink-tools watch`: the watcher, until its process is stopped.
func watch([]string) error {
f, err := theFolder()
f, err := accountFolder()
if err != nil {
return err
}
if f.Node == "" {
return errors.New("no node named: the node-engine names it in MESH_NODE")
}
if err := os.MkdirAll(f.Dir, 0o700); err != nil {
return err
}
@@ -410,7 +407,7 @@ func (w *Watcher) asked(r Rating) {
now := w.Now()
w.ask = &openAsk{Network: r.Network, Link: r.Link, Since: now, connection: r.Connection, address: r.CheckURI}
w.state.AsksOpened++
w.keep(eventAsked, nodeuplink.SignInAsked{Node: w.Folder.Node, Link: r.Link, Network: r.Network, Address: r.CheckURI,
w.keep(eventAsked, nodeuplink.SignInAsked{Link: r.Link, Network: r.Network, Address: r.CheckURI,
At: stamp(now)})
w.present(nil, r.Network, r.CheckURI)
}
@@ -421,7 +418,7 @@ func (w *Watcher) ended(link, network, why string) {
w.state.AsksEnded++
}
w.ask = nil
w.keep(eventEnded, nodeuplink.SignInEnded{Node: w.Folder.Node, Link: link, Network: network, At: stamp(w.Now()), Why: why})
w.keep(eventEnded, nodeuplink.SignInEnded{Link: link, Network: network, At: stamp(w.Now()), Why: why})
}
// stamp is a time as the events say it: RFC 3339 in UTC, to the second.
@@ -429,12 +426,12 @@ func stamp(t time.Time) string { return t.UTC().Truncate(time.Second).Format(tim
// keep writes one event into pending/ and holds the folder to its bound, the oldest dropped with a journal line.
func (w *Watcher) keep(event string, body any) {
k, err := w.Folder.keep(event, eventSubject(event, w.Folder.Node), body)
k, err := w.Folder.keep(event, body)
if err != nil {
journal("keeping %s failed: %v", event, err)
return
}
journal("kept %s as %s", event, k.MessageID)
journal("kept %s", k.Name())
kept, err := w.Folder.pending()
if err != nil {
journal("reading the kept events failed: %v", err)
@@ -442,11 +439,11 @@ func (w *Watcher) keep(event string, body any) {
}
for i := 0; len(kept)-i > keptBound; i++ {
if err := w.Folder.drop(kept[i]); err != nil {
journal("dropping the kept event %s past the bound of %d failed: %v", kept[i].MessageID, keptBound, err)
journal("dropping the kept event %s past the bound of %d failed: %v", kept[i].Name(), keptBound, err)
continue
}
w.state.DroppedPastBound++
journal("dropped the kept event %s past the bound of %d", kept[i].MessageID, keptBound)
journal("dropped the kept event %s past the bound of %d", kept[i].Name(), keptBound)
}
}
@@ -121,7 +121,7 @@ func aWatcher(t *testing.T, sys *fakeSystem) *Watcher {
t.Helper()
tick := clockStart
var mu sync.Mutex
return &Watcher{Folder: aFolder(t), Dial: sys.dial, Now: func() time.Time {
return &Watcher{Folder: Folder{Dir: t.TempDir()}, Dial: sys.dial, Now: func() time.Time {
mu.Lock()
defer mu.Unlock()
tick = tick.Add(time.Second)
@@ -193,8 +193,8 @@ func keptSaid(t *testing.T, f Folder) []string {
if err := json.Unmarshal(k.Body, &b); err != nil {
t.Fatal(err)
}
event := strings.Split(k.MessageID, ".")[1]
if k.Subject != "mesh.seat.node-uplink.event."+event+".laptop" || b.Node != "laptop" || b.At == "" {
event := k.Event
if b.Node != "" || b.At == "" {
t.Errorf("kept %+v %+v", k, b)
}
if event == eventAsked && b.Address != "http://check.example.net/" {
@@ -280,17 +280,17 @@ func TestPastTheBoundTheOldestKeptEventGoes(t *testing.T) {
lines := journalLines(t)
w := aWatcher(t, &fakeSystem{})
for range keptBound {
if _, err := w.Folder.keep(eventEnded, eventSubject(eventEnded, "laptop"), map[string]string{}); err != nil {
if _, err := w.Folder.keep(eventEnded, map[string]string{}); err != nil {
t.Fatal(err)
}
}
w.heard(guest(ratingPortal), true)
kept, _ := w.Folder.pending()
if len(kept) != keptBound || kept[0].Sequence != 2 || kept[len(kept)-1].MessageID != "laptop.sign-in-asked.257" {
if len(kept) != keptBound || kept[0].Sequence != 2 || kept[len(kept)-1].Name() != "sign-in-asked.257" {
t.Fatalf("kept %d, from %d", len(kept), kept[0].Sequence)
}
said := strings.Join(lines(), "\n")
if !strings.Contains(said, "dropped the kept event laptop.sign-in-ended.1 past the bound of 256") ||
if !strings.Contains(said, "dropped the kept event sign-in-ended.1 past the bound of 256") ||
w.state.DroppedPastBound != 1 {
t.Fatalf("the drop was not said: %s", said)
}
@@ -62,7 +62,8 @@ type runtime struct {
in io.Writer
next int
pending map[string]chan json.RawMessage
// mayPublish is every subject the module's membership lets it say, as the runtime's mayPublish reads it.
// mayPublish is every subject the module's membership lets it say, as the runtime's mayPublish reads it; guarded
// by mu.
mayPublish map[string]bool
}
@@ -146,7 +147,10 @@ func (r *runtime) handle(id json.RawMessage, method string, params json.RawMessa
ID string `json:"id"`
}
json.Unmarshal(params, &p)
if !r.mayPublish[p.Subject] {
r.mu.Lock()
may := r.mayPublish[p.Subject]
r.mu.Unlock()
if !may {
r.answer(id, nil, fmt.Errorf("%s may not publish %s: its membership does not list it", Module, p.Subject))
return
}
@@ -217,7 +221,8 @@ func TestNetworkManagerWhole(t *testing.T) {
t.Fatal(err)
}
t.Cleanup(func() { os.RemoveAll(home) })
f := Folder{Dir: filepath.Join(home, stateHome), Node: "home-server"}
// The watcher's folder names no node, as the watcher reads no MESH_NODE (L-3); the bundle has its own.
f := Folder{Dir: filepath.Join(home, stateHome)}
if err := os.MkdirAll(f.Dir, 0o700); err != nil {
t.Fatal(err)
}
@@ -345,9 +350,9 @@ func TestNetworkManagerWhole(t *testing.T) {
got = append(got, published{msg.Subject(), msg.Headers().Get(jetstream.MsgIDHeader), msg.Data()})
}
for i, k := range kept {
if got[i].id != Module+"."+k.MessageID || string(got[i].body) != string(k.Body) || got[i].subject != k.Subject {
t.Errorf("published %d: %s %s %s, kept %s %s %s", i, got[i].subject, got[i].id, got[i].body, k.Subject,
k.MessageID, k.Body)
id := fmt.Sprintf("%s.home-server.%s.%d", Module, k.Event, k.Sequence)
if got[i].id != id || got[i].subject != eventSubject(k.Event, "home-server") {
t.Errorf("published %d: %s %s, kept %s", i, got[i].subject, got[i].id, k.Name())
}
}
strict := func(raw []byte, into any) {
@@ -379,13 +384,17 @@ func TestNetworkManagerWhole(t *testing.T) {
t.Fatalf("left in pending/: %+v", left)
}
// Another node's subject is not the module's to say: the runtime refuses it and it stays kept.
if _, err := f.keep(eventAsked, nodeuplink.SubjectSignInAsked("other-node"), map[string]string{}); err != nil {
// A subject the membership does not list is refused by the runtime, and the change stays kept.
r.mu.Lock()
delete(r.mayPublish, nodeuplink.SubjectSignInAsked("home-server"))
r.mu.Unlock()
if _, err := f.keep(eventAsked, nodeuplink.SignInAsked{Link: "wlan0", Network: "guest-example",
Address: "http://example.net/portal", At: "2026-11-24T10:10:00Z"}); err != nil {
t.Fatal(err)
}
if text := r.call("uplink_publish_again"); !strings.Contains(text, `"waiting":1`) ||
!strings.Contains(text, "its membership does not list it") {
t.Fatalf("another node's subject: uplink_publish_again answered %s", text)
t.Fatalf("a subject the membership does not list: uplink_publish_again answered %s", text)
}
if left, _ := f.pending(); len(left) != 1 {
t.Fatalf("the refused event is not kept: %+v", left)