bus5 B5 (A-107): the keeper's process answers mesh-broker's snapshot verbs itself through AnswerSeat

snapshot-get, snapshot-restore, snapshot-restore-cancel and snapshot-remove are answered by
the keeper's process with the proved caller; it publishes snapshot-get's chunks and hears
their acks on its own subscription, and acts at once with no hand-over through requests
(the record of a running restore is written before it runs). The tools bundle keeps its
tools, nats_snapshots among them. The protected refusal and the whole test's case (send,
restore in place, removal) stay.

Builds: design 47 G A-107 (G-20-lead-answers), §681

Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
This commit is contained in:
2026-11-11 23:56:06 +01:00
parent a9dfe77b1d
commit 44cb7ad449
8 changed files with 378 additions and 254 deletions
@@ -9,7 +9,6 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"os"
"slices"
"strings"
@@ -25,7 +24,6 @@ import (
"git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions"
occ "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/clients/operatorchannelclient"
oc "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/seats/operatorchannel"
stdio "git.novox.be/novox/mesh-sdk/go"
"git.novox.be/novox/mesh-sdk/go/catalogue"
"git.novox.be/novox/mesh-sdk/go/process"
"git.novox.be/novox/mesh-sdk/go/views"
@@ -439,9 +437,10 @@ func TestTheBusModuleRunsWhole(t *testing.T) {
t.Error("the spent warrant deleted KV_m_drafts a second time")
}
})
// Design 47 G §681: the snapshot the keeper wrote is read by the bundle's snapshot-get, sent in chunks that hold its bytes,
// restored in place by the keeper (refused while its name is live, ended by snapshot-restored) and removed by snapshot-remove.
t.Run("a snapshot is sent by the bundle, restored in place by the keeper and removed", func(t *testing.T) {
// Design 47 G §681, A-107: the snapshot the keeper wrote is sent by the keeper's own process (snapshot-get answered through
// AnswerSeat, the chunks published by the process and acknowledged to its own subscription), restored in place by the keeper
// (refused while its name is live, ended by snapshot-restored) and removed by snapshot-remove, no call handed through the requests.
t.Run("a snapshot is sent, restored in place and removed by the keeper's process, which answers the seat itself", func(t *testing.T) {
get := func(id string) keeper.Request {
raw, _, _, _ := store.Get(keeper.StateRequests, id)
var r keeper.Request
@@ -452,64 +451,62 @@ func TestTheBusModuleRunsWhole(t *testing.T) {
if rec.Path == "" || rec.Digest == "" || rec.Protected {
t.Fatalf("the drop's record holds no snapshot: %+v", rec)
}
rs := requestStore{
Put: func(key string, r keeper.Request) error {
raw, _ := json.Marshal(r)
_, err := store.Put(keeper.StateRequests, key, raw)
return err
},
Get: func(key string) (*keeper.Request, error) {
if _, _, found, _ := store.Get(keeper.StateRequests, key); !found {
return nil, nil
}
r := get(key)
return &r, nil
},
Keys: func() ([]string, error) { return store.Keys(keeper.StateRequests) },
Now: func() time.Time { return time.Now().UTC() },
Ended: func(id string, within time.Duration) *keeper.Request {
for deadline := time.Now().Add(within); time.Now().Before(deadline); time.Sleep(20 * time.Millisecond) {
if r := get(id); !r.Done.IsZero() {
return &r
}
}
return nil
},
if _, err := js.CreateStream(ctx, jetstream.StreamConfig{Name: "CHUNKS", Subjects: []string{"mesh.seat.mesh-broker.chunk.>"}}); err != nil {
t.Fatal(err)
}
var chunkMu sync.Mutex
var chunks []Chunk
var ack func(string, json.RawMessage) (any, error)
world := &snapshotWorld{
Open: func(path string) (io.ReadCloser, error) { return os.Open(path) },
Granted: func(string, string) bool { return true }, Logf: t.Logf, Within: 5 * time.Second, KeepDays: 90,
Answer: func(_ string, h func(string, json.RawMessage) (any, error)) error { ack = h; return nil },
}
world.Publish = func(subject string, body any, id string) error {
c := body.(Chunk)
chunkMu.Lock()
chunks = append(chunks, c)
chunkMu.Unlock()
go func() { _, _ = ack(strings.Replace(subject, ".chunk.", ".ack.", 1), nil) }()
return nil
}
tl := map[string]stdio.Tool{}
for _, x := range snapshotTools(rs, world) {
tl[x.Name] = x
}
call := "0123456789abcdef0123456789abcdef"
out, err := tl["mesh-broker.snapshot-get"].Run(map[string]any{"request": "r2", "call": call})
world := keeperSnapshots(js, nc, account, t.Logf)
world.Granted = func(string, string) bool { return true }
stopAnswering, err := pconn.AnswerSeat(meshbroker.Seat, snapshotAnswers(k, heldRequest(store), world))
if err != nil {
t.Fatal(err)
}
if a := out.(meshbroker.SnapshotGetAnswer); a.Error != "" || a.Digest != rec.Digest || a.Chunks < 1 || a.Stream != "m_drafts" {
t.Fatalf("snapshot-get answered %+v for the keeper's record %+v", a, rec)
defer stopAnswering()
// A caller of the seat: its call carries its bus name as the last token, and it acknowledges each chunk it hears.
verb := func(name string, args map[string]any, into any) {
t.Helper()
raw, _ := json.Marshal(args)
msg, err := nc.Request("mesh.seat.mesh-broker.call."+name+".person~jo", raw, 10*time.Second)
if err != nil {
t.Fatal(err)
}
var reply struct {
Result json.RawMessage `json:"result"`
Error string `json:"error"`
}
if err := json.Unmarshal(msg.Data, &reply); err != nil || reply.Error != "" {
t.Fatalf("%s answered %s (%v)", name, msg.Data, err)
}
if err := json.Unmarshal(reply.Result, into); err != nil {
t.Fatal(err)
}
}
call := "0123456789abcdef0123456789abcdef"
var chunkMu sync.Mutex
var chunks []Chunk
hear, err := nc.Subscribe("mesh.seat.mesh-broker.chunk.snapshot-get."+call+".*", func(m *nats.Msg) {
var c Chunk
_ = json.Unmarshal(m.Data, &c)
chunkMu.Lock()
chunks = append(chunks, c)
chunkMu.Unlock()
go func() {
_, _ = nc.Request(strings.Replace(m.Subject, ".chunk.", ".ack.", 1), []byte("{}"), 5*time.Second)
}()
})
if err != nil {
t.Fatal(err)
}
defer hear.Unsubscribe()
var got meshbroker.SnapshotGetAnswer
verb(meshbroker.VerbSnapshotGet, map[string]any{"request": "r2", "call": call}, &got)
if got.Error != "" || got.Digest != rec.Digest || got.Chunks < 1 || got.Stream != "m_drafts" {
t.Fatalf("snapshot-get answered %+v for the keeper's record %+v", got, rec)
}
eventually(t, "the chunks sent", func() bool { chunkMu.Lock(); defer chunkMu.Unlock(); return len(chunks) > 0 })
var whole []byte
eventually(t, "the last chunk", func() bool {
chunkMu.Lock()
defer chunkMu.Unlock()
return int64(len(chunks)) == chunks[0].Of
return len(chunks) > 0 && int64(len(chunks)) == chunks[0].Of
})
chunkMu.Lock()
for _, c := range chunks {
@@ -520,11 +517,9 @@ func TestTheBusModuleRunsWhole(t *testing.T) {
t.Errorf("the chunks hold %d bytes, not the snapshot the keeper wrote (%d)", len(whole), rec.Size)
}
restore := func(why string) meshbroker.SnapshotRestoreAnswer {
o, err := tl["mesh-broker.snapshot-restore"].Run(map[string]any{"request": "r2", "why": why})
if err != nil {
t.Fatal(err)
}
return o.(meshbroker.SnapshotRestoreAnswer)
var o meshbroker.SnapshotRestoreAnswer
verb(meshbroker.VerbSnapshotRestore, map[string]any{"request": "r2", "why": why}, &o)
return o
}
// KV_m_drafts is live (made again after the drop): nothing is restored over it.
if a := restore("live"); !strings.Contains(a.Error, "names live") {
@@ -548,9 +543,10 @@ func TestTheBusModuleRunsWhole(t *testing.T) {
if _, ok := held("KV_m_drafts"); !ok || get(a.Accepted).Result != "done" {
t.Errorf("the stream is not restored in place: held %v, the act %+v", ok, get(a.Accepted))
}
o, err := tl["mesh-broker.snapshot-remove"].Run(map[string]any{"request": "r2", "why": "no longer needed"})
if err != nil || o.(meshbroker.SnapshotRemoveAnswer).Accepted == "" {
t.Fatalf("snapshot-remove answered %+v, %v", o, err)
var o meshbroker.SnapshotRemoveAnswer
verb(meshbroker.VerbSnapshotRemove, map[string]any{"request": "r2", "why": "no longer needed"}, &o)
if o.Accepted == "" {
t.Fatalf("snapshot-remove answered %+v", o)
}
if _, err := os.Stat(rec.Path); !os.IsNotExist(err) {
t.Errorf("the snapshot file %s is still there: %v", rec.Path, err)