A move or a restart stops the process: from its stop signal every call is answered an error naming the stop, the running act ends through its own failure path (an aside put back whole, whatever its context), and every accepted act not ended ends failed with stopped and its event, so its caller asks again. Builds: design 47 G §562, A-141 Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
122 lines
5.1 KiB
Go
122 lines
5.1 KiB
Go
package recovery
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
|
|
seat "git.novox.be/novox/mesh-contracts/go/seats/meshrecovery"
|
|
"git.novox.be/novox/mesh-sdk/go/process"
|
|
)
|
|
|
|
// Handlers are the seat's verbs as the process answers them over the bus with the caller the bus proved (§554, §656, A-107):
|
|
// every acting verb answers one of accepted, busy or error and nothing more (§691); the result is the event.
|
|
func (e *Engine) Handlers() map[string]process.SeatHandler {
|
|
h := map[string]process.SeatHandler{}
|
|
h[seat.VerbSnapshotKept] = actOf(e, func(sk seat.SnapshotKeptInput) (Act, error) {
|
|
return Act{Kind: KindTake, Module: sk.Module, Kept: sk.Kept, Form: sk.Form, Fingerprint: sk.Fingerprint, CatchUp: sk.CatchUp, Why: sk.Why}, nil
|
|
})
|
|
restoreKept := actOf(e, func(rk seat.RestoreKeptInput) (Act, error) {
|
|
return Act{Kind: KindRestore, Module: rk.Module, Kept: rk.Kept, Form: rk.Form, Fingerprint: rk.Fingerprint, Snapshot: rk.Snapshot, FromBackup: rk.FromBackup, Why: rk.Why}, nil
|
|
})
|
|
h[seat.VerbRestoreKept] = func(caller string, body json.RawMessage) (any, error) {
|
|
// aside-only and discard-aside are the operator's hand tools alone (§516, §554).
|
|
var loose map[string]json.RawMessage
|
|
_ = json.Unmarshal(body, &loose)
|
|
for _, k := range []string{"aside-only", "discard-aside"} {
|
|
if _, ok := loose[k]; ok {
|
|
return Answer{Error: k + " is the operator's hand tool alone, not an argument of restore-kept"}, nil
|
|
}
|
|
}
|
|
return restoreKept(caller, body)
|
|
}
|
|
h[seat.VerbKeptPoints] = func(_ string, body json.RawMessage) (any, error) {
|
|
var in seat.KeptPointsInput
|
|
if err := strict(body, &in); err != nil {
|
|
return nil, err
|
|
}
|
|
return e.KeptPoints(in.Module, in.Kept)
|
|
}
|
|
h[seat.VerbTake] = actOf(e, func(tk seat.TakeInput) (Act, error) {
|
|
return Act{Kind: KindTake, Module: tk.Module, Kept: tk.Kept, Fingerprint: tk.Fingerprint, Why: tk.Why, Asker: tk.Asker}, nil
|
|
})
|
|
h[seat.VerbRestore] = actOf(e, func(rs seat.RestoreInput) (Act, error) {
|
|
if (rs.Snapshot == "") == (rs.Request == "") {
|
|
return Act{}, errors.New("restore takes exactly one of snapshot and request")
|
|
}
|
|
if rs.Request != "" && rs.FromBackup {
|
|
return Act{}, errors.New("from-backup is taken only with snapshot, a restore point (design 47 G §682)")
|
|
}
|
|
return Act{Kind: KindRestore, Module: rs.Module, Kept: rs.Kept, Snapshot: rs.Snapshot, Request: rs.Request, FromBackup: rs.FromBackup, Why: rs.Why, Asker: rs.Asker}, nil
|
|
})
|
|
h[seat.VerbAside] = actOf(e, func(ad seat.AsideInput) (Act, error) {
|
|
return Act{Kind: KindAside, Module: ad.Module, Kept: ad.Kept, Why: ad.Why, Asker: ad.Asker}, nil
|
|
})
|
|
h[seat.VerbDiscardAside] = actOf(e, func(da seat.DiscardAsideInput) (Act, error) {
|
|
return Act{Kind: KindDiscardAside, Module: da.Module, Kept: da.Kept, AsideID: da.ID, Why: da.Why, Asker: da.Asker}, nil
|
|
})
|
|
h[seat.VerbRestoreAside] = actOf(e, func(ra seat.RestoreAsideInput) (Act, error) {
|
|
return Act{Kind: KindRestoreAside, Module: ra.Module, Kept: ra.Kept, AsideID: ra.ID, Why: ra.Why, Asker: ra.Asker}, nil
|
|
})
|
|
h[seat.VerbExpirePoints] = actOf(e, func(ex seat.ExpirePointsInput) (Act, error) {
|
|
return Act{Kind: KindExpire, Why: ex.Why, Asker: ex.Asker}, nil
|
|
})
|
|
h[seat.VerbRetry] = func(caller string, body json.RawMessage) (any, error) {
|
|
var in seat.RetryInput
|
|
if err := strict(body, &in); err != nil {
|
|
return Answer{Error: err.Error()}, nil
|
|
}
|
|
return e.Retry(in.Module, in.Kept, in.Why, caller, in.Asker), nil
|
|
}
|
|
h[seat.VerbCancel] = func(caller string, body json.RawMessage) (any, error) {
|
|
var in seat.CancelInput
|
|
if err := strict(body, &in); err != nil {
|
|
return Answer{Error: err.Error()}, nil
|
|
}
|
|
return e.Cancel(in.Act, in.Why, caller, in.Asker), nil
|
|
}
|
|
// A stopping process takes no call, each answered the error naming the stop (§562, A-141).
|
|
for verb, handle := range h {
|
|
h[verb] = func(caller string, body json.RawMessage) (any, error) {
|
|
if stop := e.Stopping(); stop != "" {
|
|
return Answer{Error: stop}, nil
|
|
}
|
|
return handle(caller, body)
|
|
}
|
|
}
|
|
return h
|
|
}
|
|
|
|
// actOf is one acting verb: its arguments are read afresh for each call (calls come on concurrent bus callbacks), a call
|
|
// without the `why` the hand-act log keeps is refused, and the act is accepted as the engine says.
|
|
func actOf[T any](e *Engine, build func(T) (Act, error)) process.SeatHandler {
|
|
return func(caller string, body json.RawMessage) (any, error) {
|
|
var in T
|
|
if err := strict(body, &in); err != nil {
|
|
return Answer{Error: err.Error()}, nil
|
|
}
|
|
a, err := build(in)
|
|
if err != nil {
|
|
return Answer{Error: err.Error()}, nil
|
|
}
|
|
if a.Why == "" {
|
|
return Answer{Error: "why is required: the hand-act log keeps it"}, nil
|
|
}
|
|
a.Caller = caller
|
|
return e.Accept(a), nil
|
|
}
|
|
}
|
|
|
|
// strict reads a call's arguments, refusing a field the verb does not take (no `local-name`, no `from`: §516) and a call
|
|
// that leaves out the `why` the hand-act log keeps (§682).
|
|
func strict(body json.RawMessage, into any) error {
|
|
dec := json.NewDecoder(bytes.NewReader(body))
|
|
dec.DisallowUnknownFields()
|
|
if err := dec.Decode(into); err != nil {
|
|
return fmt.Errorf("the arguments are refused: %s", strings.TrimPrefix(err.Error(), "json: "))
|
|
}
|
|
return nil
|
|
}
|