ForgeGroupEnd gains base; the bridge reads each member's base from the seat's verb groups (Member.base is already in the contract and seatgen's output), keeps groups.<group>.<base>, says one end per base from that base's members, and bridge_group_end_again takes the base. Builds: design 47 G §715, A-236 Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
68 lines
2.3 KiB
Go
68 lines
2.3 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"time"
|
|
|
|
"github.com/nats-io/nats.go/jetstream"
|
|
|
|
seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdelivery"
|
|
|
|
"mesh-delivery-forge-bridge/internal/groups"
|
|
)
|
|
|
|
// endAsked answers one value of the state end-asks (the tool bridge_group_end_again, ADR 0633 §4): a value not yet answered
|
|
// says the group's end again from the seat's groups, with the ask's time appended to the message id so the stream's duplicate
|
|
// window does not drop it, and is written back by compare-and-set at its revision with whether it was written. A value
|
|
// answered already (this bridge's own write) is left.
|
|
func (g *grouper) endAsked(ctx context.Context, kv jetstream.KeyValue, key string, rev uint64, value []byte) {
|
|
a, err := groups.DecodeEndAsk(value)
|
|
if err != nil {
|
|
g.logger.Printf("%s.%s holds what is not an end ask, left: %v", groups.EndAsksState, key, err)
|
|
return
|
|
}
|
|
if a.Done() {
|
|
return
|
|
}
|
|
a = g.answerEnd(ctx, a, time.Now())
|
|
g.logger.Printf("the end of the group %q said again by %s (%s): %s", a.Group, a.By, a.Why, a.Answer)
|
|
data, err := json.Marshal(a)
|
|
if err != nil {
|
|
return
|
|
}
|
|
if _, err := kv.Update(ctx, key, data, rev); err != nil && !errors.Is(err, jetstream.ErrKeyExists) && !errors.Is(err, jetstream.ErrKeyRevisionMismatch) {
|
|
g.logger.Printf("%s.%s: the answer could not be written: %v", groups.EndAsksState, key, err)
|
|
}
|
|
}
|
|
|
|
// answerEnd says the group's end again for an ask and returns the ask with its answer.
|
|
func (g *grouper) answerEnd(ctx context.Context, a groups.EndAsk, at time.Time) groups.EndAsk {
|
|
a.Answered = at.UTC()
|
|
if g.groups == nil {
|
|
a.Answer = "the bridge reads no groups"
|
|
return a
|
|
}
|
|
of, err := g.groups(ctx)
|
|
if err != nil {
|
|
a.Answer = "the groups cannot be read: " + err.Error()
|
|
return a
|
|
}
|
|
m, ok := seat.Group{}, false
|
|
for _, c := range of {
|
|
if c.ID == a.Group {
|
|
m, ok = c, true
|
|
}
|
|
}
|
|
switch {
|
|
case !ok || len(m.Members) < 2:
|
|
a.Answer = "the seat " + seat.Seat + " holds no group " + a.Group + " of more than one member"
|
|
case g.end(ctx, m, a.Base, groups.AgainStamp(a.At)):
|
|
a.Written, a.Answer = true, "the end of the group is written again"
|
|
default:
|
|
a.Answer = "the end on " + a.Base + " is not written: a member's merged message is not written, or FORGE refused it (the bridge's journal says which)"
|
|
}
|
|
return a
|
|
}
|