466-c: knowledge_restore gives back the insights and fields a version dropped
Builds: design 55 §1.1, §10; lead answers L-466-4, L-466-5 (issue 466 note 18); 466/AC5, AC7
This commit is contained in:
@@ -0,0 +1,195 @@
|
||||
package main
|
||||
|
||||
// The restoring version (novox/hq issue 466): a version made by propose before a record kept what the served version
|
||||
// held (kb.go keepServed) dropped its insights, and the fields propose does not take. The module's tool gives them
|
||||
// back from the accepted versions that held them, in one version per record by the knowledge base, taken by its own
|
||||
// check and not by a judgement: only what an earlier accepted version held was added, and nothing else differs.
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// RestoreTool is the module's tool that lists, dry-runs and runs the restoring versions.
|
||||
const RestoreTool = "knowledge_restore"
|
||||
|
||||
// RestoredRecord is one record a restoring version was written (or would be) for.
|
||||
type RestoredRecord struct {
|
||||
Record string `json:"record"`
|
||||
Restored []string `json:"restored"`
|
||||
Version int `json:"version,omitempty"`
|
||||
Skipped string `json:"skipped,omitempty"`
|
||||
}
|
||||
|
||||
func insightKey(in Insight) string { return in.Date + "\x00" + in.By + "\x00" + in.Before }
|
||||
|
||||
// restoredOf is the served record with what an earlier accepted version held and it lacks put back, and a line for
|
||||
// each entry naming the version it came from; nil when nothing is missing.
|
||||
func restoredOf(served *Record, versions []Version) (*Record, []string) {
|
||||
r := served.Clone()
|
||||
held := map[string]bool{}
|
||||
for _, in := range served.Insights {
|
||||
held[insightKey(in)] = true
|
||||
}
|
||||
var said []string
|
||||
var added []Insight
|
||||
for i, v := range versions {
|
||||
if v.Record == nil || v.Record.Pending != nil || !v.Record.Accepted() {
|
||||
continue
|
||||
}
|
||||
n := v.N
|
||||
if n == 0 {
|
||||
n = i + 1
|
||||
}
|
||||
for _, in := range v.Record.Insights {
|
||||
if !held[insightKey(in)] {
|
||||
held[insightKey(in)] = true
|
||||
added = append(added, in)
|
||||
said = append(said, fmt.Sprintf("insight %s by %s (version %d)", in.Date, in.By, n))
|
||||
}
|
||||
}
|
||||
}
|
||||
// A field is put back from the latest accepted version that held it.
|
||||
for i := len(versions) - 1; i >= 0; i-- {
|
||||
v := versions[i]
|
||||
if v.Record == nil || v.Record.Pending != nil || !v.Record.Accepted() {
|
||||
continue
|
||||
}
|
||||
n := v.N
|
||||
if n == 0 {
|
||||
n = i + 1
|
||||
}
|
||||
o := v.Record
|
||||
if r.Reconstructed == nil && o.Reconstructed != nil {
|
||||
b := *o.Reconstructed
|
||||
r.Reconstructed = &b
|
||||
said = append(said, fmt.Sprintf("reconstructed (version %d)", n))
|
||||
}
|
||||
if len(r.Amended) == 0 && len(o.Amended) > 0 {
|
||||
r.Amended = append([]string(nil), o.Amended...)
|
||||
said = append(said, fmt.Sprintf("amended (version %d)", n))
|
||||
}
|
||||
if len(r.Touches) == 0 && len(o.Touches) > 0 {
|
||||
r.Touches = append([]string(nil), o.Touches...)
|
||||
said = append(said, fmt.Sprintf("touches (version %d)", n))
|
||||
}
|
||||
if len(r.Extra) == 0 && len(o.Extra) > 0 {
|
||||
r.Extra = map[string]string{}
|
||||
for k, x := range o.Extra {
|
||||
r.Extra[k] = x
|
||||
}
|
||||
said = append(said, fmt.Sprintf("the imported fields (version %d)", n))
|
||||
}
|
||||
if r.Source == "" && o.Source != "" {
|
||||
r.Source = o.Source
|
||||
said = append(said, fmt.Sprintf("source (version %d)", n))
|
||||
}
|
||||
if len(r.Aliases) == 0 && len(o.Aliases) > 0 {
|
||||
r.Aliases = map[string]string{}
|
||||
for k, x := range o.Aliases {
|
||||
r.Aliases[k] = x
|
||||
}
|
||||
said = append(said, fmt.Sprintf("aliases (version %d)", n))
|
||||
}
|
||||
}
|
||||
if len(said) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
r.Insights = append(append([]Insight(nil), served.Insights...), added...)
|
||||
sort.SliceStable(r.Insights, func(i, j int) bool { return r.Insights[i].Date < r.Insights[j].Date })
|
||||
return r, said
|
||||
}
|
||||
|
||||
// checkRestore is the restoring version's own check: every served insight is still there, each insight added was held
|
||||
// by an earlier accepted version, and nothing but the insights and the fields restored differs from the served record.
|
||||
func checkRestore(served, next *Record, versions []Version) error {
|
||||
held := map[string]bool{}
|
||||
for _, v := range versions {
|
||||
if v.Record != nil && v.Record.Pending == nil && v.Record.Accepted() {
|
||||
for _, in := range v.Record.Insights {
|
||||
held[insightKey(in)] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
have := map[string]bool{}
|
||||
for _, in := range next.Insights {
|
||||
have[insightKey(in)] = true
|
||||
}
|
||||
for _, in := range served.Insights {
|
||||
if !have[insightKey(in)] {
|
||||
return fmt.Errorf("the insight of %s is missing from the restored version", in.Date)
|
||||
}
|
||||
}
|
||||
for _, in := range served.Insights {
|
||||
held[insightKey(in)] = true
|
||||
}
|
||||
for _, in := range next.Insights {
|
||||
if !held[insightKey(in)] {
|
||||
return fmt.Errorf("the insight of %s was held by no accepted version", in.Date)
|
||||
}
|
||||
}
|
||||
a, b := served.Clone(), next.Clone()
|
||||
for _, r := range []*Record{a, b} {
|
||||
r.Insights, r.Reconstructed, r.Amended, r.Touches, r.Extra, r.Source, r.Aliases = nil, nil, nil, nil, nil, "", nil
|
||||
}
|
||||
ja, _ := json.Marshal(a)
|
||||
jb, _ := json.Marshal(b)
|
||||
if !reflect.DeepEqual(ja, jb) {
|
||||
return fmt.Errorf("something other than insights and fields differs")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Restore lists (dry), or writes, the restoring version of one record, of one kind, or of every
|
||||
// record that lost something. A record with a version waiting for its review is skipped, saying so; a second run
|
||||
// finds nothing to restore and writes nothing.
|
||||
func (k *KB) Restore(only *Key, kind string, dry bool) ([]RestoredRecord, error) {
|
||||
k.mu.Lock()
|
||||
defer k.mu.Unlock()
|
||||
ctx := context.Background()
|
||||
all, err := k.Store.All(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := []RestoredRecord{}
|
||||
for _, e := range all {
|
||||
key := e.Current.Key()
|
||||
if (only != nil && *only != key) || (kind != "" && key.Kind != kind) {
|
||||
continue
|
||||
}
|
||||
if e.Current.Status == StatusReserved || e.Current.Status == StatusWithdrawn {
|
||||
continue
|
||||
}
|
||||
versions, err := k.Store.Versions(ctx, key)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s: its versions cannot be read: %v", key, err)
|
||||
}
|
||||
next, said := restoredOf(e.Current, versions)
|
||||
if next == nil {
|
||||
continue
|
||||
}
|
||||
rr := RestoredRecord{Record: key.String(), Restored: said}
|
||||
switch {
|
||||
case e.Pending != nil:
|
||||
rr.Skipped = "a version waits for its review: it is restored once that is decided"
|
||||
default:
|
||||
if err := checkRestore(e.Current, next, versions); err != nil {
|
||||
rr.Skipped = "the check refused it: " + err.Error()
|
||||
} else if !dry {
|
||||
why := "restored what a version dropped: " + strings.Join(said, "; ")
|
||||
if err := k.Store.Save(ctx, Version{By: "the knowledge base", At: k.now(), Why: why, Record: next}, true, false, e.Revision); err != nil {
|
||||
rr.Skipped = "it could not be written: " + err.Error()
|
||||
} else {
|
||||
rr.Version = e.Versions + 1
|
||||
k.emit(EventChanged, next, "the knowledge base", why, e.Versions+1)
|
||||
}
|
||||
}
|
||||
}
|
||||
out = append(out, rr)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// 466/AC5: a record that lost an insight and a field an earlier accepted version held gets them back in one version by
|
||||
// the knowledge base; a dry run writes nothing, and a second run writes nothing.
|
||||
func TestRestoreGivesBackWhatAVersionDropped(t *testing.T) {
|
||||
eachStore(t, func(t *testing.T, s Store) {
|
||||
w := imported(t, s)
|
||||
n := w.reserved(t, "author@g14")
|
||||
p := valid(n)
|
||||
w.accepted(t, "author@g14", p)
|
||||
key := Key{KindDecision, n}
|
||||
ctx := context.Background()
|
||||
e := mustGet(t, s, key)
|
||||
r := e.Current.Clone()
|
||||
no := false
|
||||
r.Reconstructed = &no
|
||||
if err := s.Save(ctx, Version{By: "test", At: time.Now(), Why: "seed", Record: r}, true, false, e.Revision); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
p.Insight = "it said less"
|
||||
p.Sections[2].Text = "Merges land sooner, by about a minute."
|
||||
w.accepted(t, "author@g14", p)
|
||||
// What an older propose did: a version that holds neither the insight nor the field.
|
||||
e = mustGet(t, s, key)
|
||||
lost := e.Current.Clone()
|
||||
lost.Insights, lost.Reconstructed = nil, nil
|
||||
if err := s.Save(ctx, Version{By: "author@g14", At: time.Now(), Why: "an insight that lost them", Record: lost}, true, false, e.Revision); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
before := mustGet(t, s, key).Versions
|
||||
var tools []toolRun
|
||||
for _, tool := range moduleTools(w.k, callerOf) {
|
||||
tools = append(tools, toolRun{tool.Name, tool.Run})
|
||||
}
|
||||
out, err := callTool(t, tools, RestoreTool, map[string]any{"dry-run": true})
|
||||
recs, _ := out["records"].([]any)
|
||||
if err != nil || len(recs) != 1 || !strings.Contains(recs[0].(map[string]any)["record"].(string), itoa4(n)) || mustGet(t, s, key).Versions != before {
|
||||
t.Fatalf("dry run: %v %v", err, out)
|
||||
}
|
||||
if _, err := callTool(t, tools, RestoreTool, map[string]any{"number": float64(n), "kind": "decision"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
e = mustGet(t, s, key)
|
||||
if e.Versions != before+1 || len(e.Current.Insights) != 1 || e.Current.Insights[0].Before != "it said less" ||
|
||||
e.Current.Insights[0].By != "author@g14" || e.Current.Reconstructed == nil || *e.Current.Reconstructed {
|
||||
t.Fatalf("not restored: %d versions (was %d), %+v", e.Versions, before, e.Current.Insights)
|
||||
}
|
||||
if got := w.said.of(EventChanged); len(got) == 0 || got[len(got)-1]["by"] != "the knowledge base" {
|
||||
t.Fatalf("no changed event by the knowledge base: %v", got)
|
||||
}
|
||||
out, err = callTool(t, tools, RestoreTool, map[string]any{})
|
||||
if recs, _ := out["records"].([]any); err != nil || len(recs) != 0 || mustGet(t, s, key).Versions != before+1 {
|
||||
t.Fatalf("a second run wrote: %v %v", err, out)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -35,7 +35,7 @@ const (
|
||||
|
||||
// ModuleTools are the module's own tools beside the health tool, in the order the manifest lists them.
|
||||
var ModuleTools = []string{HealthTool, HighestTool, MarksTool, HeadTool, OperationsTool, SectionTool, DraftTool, PartsTool,
|
||||
AskedTool, FillTool, IndexTool, AgainTool, IntegrityTool, FormTool, MoveTool, MoveCheckTool, OverviewTool, RereadTool}
|
||||
AskedTool, FillTool, IndexTool, AgainTool, IntegrityTool, FormTool, MoveTool, MoveCheckTool, OverviewTool, RereadTool, RestoreTool}
|
||||
|
||||
var errNotOnLedger = errors.New("the records are still in the database: this tool reads the ledger, which serves them after the move")
|
||||
|
||||
@@ -440,6 +440,40 @@ func moduleTools(k *KB, caller func(map[string]any) string) []stdio.Tool {
|
||||
}
|
||||
return map[string]any{"source": ms.Source(), "differences": diffs, "clean": len(diffs) == 0}, nil
|
||||
}},
|
||||
{Name: RestoreTool,
|
||||
Description: "List, or write, the restoring versions: a record whose served version lacks a progressive insight, or a field an imported record had " +
|
||||
"(reconstructed, amended, touches, the imported fields, source, aliases), that an earlier accepted version held, gets one version by the knowledge base " +
|
||||
"that puts them back, insights in date order each with its own date and author. A check proves only entries an earlier accepted version held were added; " +
|
||||
"no judgement runs. With dry-run nothing is written. Takes one record, one kind or every record; a second run writes nothing.",
|
||||
Input: schema(nil, map[string]any{"kind": kindArg(), "number": integer("one record; every record that lost something when absent"), "dry-run": flag("list and write nothing")}),
|
||||
Run: func(m map[string]any) (any, error) {
|
||||
a, err := readArgs(RestoreTool, m, "kind", "number", "dry-run")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var only *Key
|
||||
kind := ""
|
||||
if _, has := m["number"]; has {
|
||||
key, err := a.key()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
only = &key
|
||||
} else if kind, err = a.str("kind"); err != nil {
|
||||
return nil, err
|
||||
} else if kind != "" && !contains(Kinds, kind) {
|
||||
return nil, fmt.Errorf("kind is decision or research, not %q", kind)
|
||||
}
|
||||
dry, err := a.flag("dry-run")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
done, err := k.Restore(only, kind, dry)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return map[string]any{"records": done, "written": !dry}, nil
|
||||
}},
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -130,7 +130,7 @@ func TestEveryStateTheModuleKeepsHasAToolThatReadsIt(t *testing.T) {
|
||||
if _, err := callTool(t, tools, MoveCheckTool, map[string]any{}); err == nil {
|
||||
t.Fatal("a move check without a database")
|
||||
}
|
||||
if len(ModuleTools) != 18 {
|
||||
if len(ModuleTools) != 19 {
|
||||
t.Fatalf("%v", ModuleTools)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user