Files
mesh-catalog/modules/mesh-recovery/cmd/mesh-recovery-tools/main.go
T
jschoubben bd2997cf36 A-129: kept-points says the unrestored asides; recovery_kept_points answers them as the health check
The module's declared health check names recovery_kept_points: it answers what kept-points
answers with the health answer beside it, unhealthy while an aside only it holds the data of
is listed.

Builds: design 47 G A-129, §554, §657

Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
2026-11-12 01:30:00 +01:00

130 lines
6.3 KiB
Go

// mesh-recovery-tools: the recovery module's tools, its command-line interface (design 47 G §554, §656, §657). Each hand
// tool calls one verb of the seat mesh-recovery with the `asker` the tool runner proved for the call and holds no grant of
// its own: the bus proves the tool runner's account, the verb logs both in the hand-act log. stdout is the MCP channel.
package main
import (
"encoding/json"
"fmt"
"os"
"strings"
stdio "git.novox.be/novox/mesh-sdk/go"
"git.novox.be/novox/mesh-sdk/go/health"
)
func logf(format string, a ...any) { fmt.Fprintf(os.Stderr, format+"\n", a...) }
func main() {
if err := stdio.Serve("", Tools(stdio.Ask, stdio.CallerOf)); err != nil {
logf("%v", err)
os.Exit(1)
}
}
// Ask is how a bundle calls a seat's verb: the key `seat:<seat>.<verb>` and the arguments.
type Ask func(key string, body any) (json.RawMessage, error)
func obj(props map[string]any, required ...string) map[string]any {
m := map[string]any{"type": "object", "properties": props}
if len(required) > 0 {
m["required"] = required
}
return m
}
func str(d string) map[string]any { return map[string]any{"type": "string", "description": d} }
var (
module = str("the module")
kept = str("the kept data, its local name in the module's manifest")
why = str("why, which the hand-act log keeps")
)
// hand is a tool that calls a verb of the seat: the arguments go through as they are, with the asker the tool runner proved.
func hand(ask Ask, callerOf func(map[string]any) string, name, verb, description string, props map[string]any, required ...string) stdio.Tool {
props["why"] = why
return stdio.Tool{Name: name, Description: description + " Class act: written in the hand-act log first with the account the bus proved and who asked; answers accepted, busy or error at once and the result comes as the act's ending event.",
Input: obj(props, append(required, "why")...),
Run: func(a map[string]any) (any, error) {
body := map[string]any{}
for k, v := range a {
body[k] = v
}
body["asker"] = callerOf(a)
raw, err := ask("seat:mesh-recovery."+verb, body)
if err != nil {
return nil, err
}
var out any
return out, json.Unmarshal(raw, &out)
}}
}
// Tools are what the runtime serves, named recovery_ (§554); each acting tool is one verb of the seat (§656).
func Tools(ask Ask, callerOf func(map[string]any) string) []stdio.Tool {
return []stdio.Tool{
{Name: "recovery_kept_points",
Description: "Every restore point and aside the recovery module holds, with their sizes, and the act that runs with since when and what it waits on; narrowed to a module and a kept data. Changes nothing (also the module's declared health check, §657).",
Input: obj(map[string]any{"module": module, "kept": kept}),
Run: func(a map[string]any) (any, error) {
body := map[string]any{}
for _, k := range []string{"module", "kept"} {
if s, _ := a[k].(string); strings.TrimSpace(s) != "" {
body[k] = s
}
}
raw, err := ask("seat:mesh-recovery.kept-points", body)
if err != nil {
return nil, err
}
return keptPointsHealth(raw)
}},
hand(ask, callerOf, "recovery_take", "take", "Take a restore point of one module's kept data now, at the form of the given fingerprint.",
map[string]any{"module": module, "kept": kept, "fingerprint": str("the fingerprint whose form the point is taken at")}, "module", "kept", "fingerprint"),
hand(ask, callerOf, "recovery_restore", "restore", "Restore one point (snapshot, a point's name from recovery_kept_points) or a keeper's snapshot (request), exactly one of them.",
map[string]any{"module": module, "kept": kept, "snapshot": str("the restore point's name"), "request": str("the keeper's snapshot by its request"),
"from-backup": map[string]any{"type": "boolean", "description": "ask the backup holder for the points directory when the point is missing, only with snapshot"}}, "module", "kept"),
hand(ask, callerOf, "recovery_retry", "retry", "Ask again the last failed act of a module and kept data, with its fields.",
map[string]any{"module": module, "kept": kept}, "module", "kept"),
hand(ask, callerOf, "recovery_aside", "aside", "Set the live kept data aside without restoring: a move, the data stays in the aside until it is put back or discarded.",
map[string]any{"module": module, "kept": kept}, "module", "kept"),
hand(ask, callerOf, "recovery_discard_aside", "discard-aside", "Remove one aside's directory and nothing else.",
map[string]any{"module": module, "kept": kept, "id": str("the aside's id")}, "module", "kept", "id"),
hand(ask, callerOf, "recovery_restore_aside", "restore-aside", "Put an aside back under the live names it was taken from, refused while live objects of those names exist.",
map[string]any{"module": module, "kept": kept, "id": str("the aside's id")}, "module", "kept", "id"),
hand(ask, callerOf, "recovery_cancel", "cancel", "End an accepted act that has not begun its work (one waiting on the dump) failed, cancelled; refused naming the step of an act that has begun.",
map[string]any{"act": str("the act's id")}, "act"),
hand(ask, callerOf, "recovery_expire_points", "expire-points", "Remove the restore points past their keep days now, never the newest of a kept data.", map[string]any{}),
}
}
// keptPointsHealth is what kept-points answers with the health answer beside it, which the module's declared health
// check reads (§554, §657, A-129): unhealthy while an aside only it holds the data of is listed as unrestored.
func keptPointsHealth(raw json.RawMessage) (map[string]any, error) {
var out map[string]any
if err := json.Unmarshal(raw, &out); err != nil {
return nil, err
}
var kp struct {
Unrestored []struct {
ID string `json:"id"`
Names []string `json:"names"`
} `json:"unrestored"`
}
if err := json.Unmarshal(raw, &kp); err != nil {
return nil, err
}
h := health.Healthy("No aside holds data the live bus lacks.")
if len(kp.Unrestored) > 0 {
var said []string
for _, a := range kp.Unrestored {
said = append(said, a.ID+" ("+strings.Join(a.Names, ", ")+")")
}
h = health.Unhealthy(fmt.Sprintf("%d aside(s) hold data whose live objects were removed and not restored: %s; recovery_restore_aside puts one back.",
len(kp.Unrestored), strings.Join(said, "; ")))
}
out["state"], out["healthy"], out["why"] = h.State, h.Healthy, h.Why
return out, nil
}