diff --git a/modules/mesh-knowledge/cmd/mesh-knowledge/restore.go b/modules/mesh-knowledge/cmd/mesh-knowledge/restore.go index baf244304..4c77d64a1 100644 --- a/modules/mesh-knowledge/cmd/mesh-knowledge/restore.go +++ b/modules/mesh-knowledge/cmd/mesh-knowledge/restore.go @@ -60,9 +60,18 @@ type restoreStep struct { from int } +// acceptedByReview says the version is one accept wrote: its why is the one accept is given by the review (review.go) or +// by the operator's Approve (operator.go). It is read from the version itself, never from the version before it: a +// mark another record's acceptance, or the recompute of marks, writes while the record waits lands between the pending +// version and the accepted one. +func acceptedByReview(v Version) bool { + return strings.HasPrefix(v.Why, "accepted after the knowledge base's review") || + strings.HasPrefix(v.Why, "accepted on the operator's Approve after the knowledge base's review") +} + // droppedByPropose reads the accepted versions in order and finds what a version made by propose dropped: an accepted -// version whose version just before it waited as a pending version (propose, then its reviews, then accept) and that -// lacks an insight, or a field, the accepted version before it held. A version a refresh, a form-only change or a +// version that accept wrote (acceptedByReview, after propose and its reviews) and that +// lacks an insight, or a field, the accepted version before it held (a mark is a copy of the served version, so it hides nothing). A version a refresh, a form-only change or a // recomputing of marks wrote is no such version, and what it removed is not taken back. Each entry comes from the // accepted version just before the one that lacks it. func droppedByPropose(versions []Version) (insights map[string]int, fields map[string]restoreStep) { @@ -72,7 +81,7 @@ func droppedByPropose(versions []Version) (insights map[string]int, fields map[s if v.Record == nil || v.Record.Pending != nil || !v.Record.Accepted() { continue } - if prev >= 0 && i > 0 && versions[i-1].Record != nil && versions[i-1].Record.Pending != nil { + if prev >= 0 && acceptedByReview(v) { before, now := versions[prev].Record, v.Record have := map[string]bool{} for _, in := range now.Insights { diff --git a/modules/mesh-knowledge/cmd/mesh-knowledge/restore_test.go b/modules/mesh-knowledge/cmd/mesh-knowledge/restore_test.go index b3c9b670e..8ae9fd182 100644 --- a/modules/mesh-knowledge/cmd/mesh-knowledge/restore_test.go +++ b/modules/mesh-knowledge/cmd/mesh-knowledge/restore_test.go @@ -30,7 +30,7 @@ func TestRestoreGivesBackWhatAVersionDropped(t *testing.T) { e := mustGet(t, s, key) r := e.Current.Clone() no := false - r.Reconstructed = &no + r.Reconstructed, r.Source = &no, "hq@abc:02-DECISIONS/x.md" if err := s.Save(ctx, Version{By: "test", At: time.Now(), Why: "seed", Record: r}, true, false, e.Revision); err != nil { t.Fatal(err) } @@ -41,19 +41,27 @@ func TestRestoreGivesBackWhatAVersionDropped(t *testing.T) { } // lose is what an older propose and accept did with a second insight: a pending version, then the accepted one // holding only the new insight and none of the fields. - lose := func(t *testing.T, key Key) { + lose := func(t *testing.T, key Key, mark bool) { e := mustGet(t, s, key) in := Insight{Date: "2026-10-12", Before: "it said more", By: "corrector@ace"} pend := e.Current.Clone() - pend.Status, pend.Insights, pend.Reconstructed = StatusProposed, nil, nil + pend.Status, pend.Insights, pend.Reconstructed, pend.Source = StatusProposed, nil, nil, "" pend.Pending = &PendingState{Insight: in} if err := s.Save(ctx, Version{By: "corrector@ace", At: time.Now(), Why: "a progressive insight proposed", Record: pend, Insight: true}, false, false, e.Revision); err != nil { t.Fatal(err) } e = mustGet(t, s, key) + if mark { + // another record's acceptance, or the recompute of marks, writes a copy of the served version while the + // insight waits. + if err := s.Save(ctx, Version{By: "the knowledge base", At: time.Now(), Why: "a mark withdrawn", Record: e.Current.Clone()}, true, false, e.Revision); err != nil { + t.Fatal(err) + } + e = mustGet(t, s, key) + } lost := e.Current.Clone() - lost.Insights, lost.Reconstructed = []Insight{in}, nil - if err := s.Save(ctx, Version{By: "the knowledge base", At: time.Now(), Why: "accepted", Record: lost, Insight: true}, true, true, e.Revision); err != nil { + lost.Insights, lost.Reconstructed, lost.Source = []Insight{in}, nil, "" + if err := s.Save(ctx, Version{By: "the knowledge base", At: time.Now(), Why: "accepted after the knowledge base's review", Record: lost, Insight: true}, true, true, e.Revision); err != nil { t.Fatal(err) } } @@ -73,34 +81,38 @@ func TestRestoreGivesBackWhatAVersionDropped(t *testing.T) { return got } - t.Run("a version made by propose dropped them", func(t *testing.T) { - key, held := accepted(t) - lose(t, key) - before := mustGet(t, s, key).Versions - want := []string{fmt.Sprintf("insight 2026-10-11 by author@g14 (version %d)", held), fmt.Sprintf("reconstructed (version %d)", held)} - dry := run(t, key, map[string]any{"dry-run": true}) - recs := records(dry) - if len(recs) != 1 || fmt.Sprint(recs[0]["restored"]) != fmt.Sprint(toAny(want)) || recs[0]["skipped"] != nil || - dry["written"] != false || mustGet(t, s, key).Versions != before { - t.Fatalf("dry run: %v", dry) - } - out := run(t, key, map[string]any{}) - e := mustGet(t, s, key) - in := e.Current.Insights - if out["written"] != true || out["versions-written"] != float64(1) || e.Versions != before+1 || len(in) != 2 || - in[0].Before != "it said less" || in[0].By != "author@g14" || in[1].Before != "it said more" || in[1].By != "corrector@ace" || - e.Current.Reconstructed == nil || *e.Current.Reconstructed { - t.Fatalf("not restored: %v, %d versions (was %d), %+v", out, e.Versions, before, in) - } - 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) - } - again := run(t, key, map[string]any{}) - if len(records(again)) != 0 || again["written"] != false || mustGet(t, s, key).Versions != before+1 { - t.Fatalf("a second run wrote: %v", again) - } - }) - + for _, mark := range []bool{false, true} { + t.Run(fmt.Sprintf("a version made by propose dropped them (a mark between: %v)", mark), func(t *testing.T) { + key, held := accepted(t) + lose(t, key, mark) + before := mustGet(t, s, key).Versions + if mark { + held += 2 // the entries come from the mark, a copy of the served version, written after the pending version + } + want := []string{fmt.Sprintf("insight 2026-10-11 by author@g14 (version %d)", held), fmt.Sprintf("reconstructed (version %d)", held), fmt.Sprintf("source (version %d)", held)} + dry := run(t, key, map[string]any{"dry-run": true}) + recs := records(dry) + if len(recs) != 1 || fmt.Sprint(recs[0]["restored"]) != fmt.Sprint(toAny(want)) || recs[0]["skipped"] != nil || + dry["written"] != false || mustGet(t, s, key).Versions != before { + t.Fatalf("dry run: %v", dry) + } + out := run(t, key, map[string]any{}) + e := mustGet(t, s, key) + in := e.Current.Insights + if out["written"] != true || out["versions-written"] != float64(1) || e.Versions != before+1 || len(in) != 2 || + in[0].Before != "it said less" || in[0].By != "author@g14" || in[1].Before != "it said more" || in[1].By != "corrector@ace" || + e.Current.Reconstructed == nil || *e.Current.Reconstructed || e.Current.Source == "" { + t.Fatalf("not restored: %v, %d versions (was %d), %+v", out, e.Versions, before, in) + } + 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) + } + again := run(t, key, map[string]any{}) + if len(records(again)) != 0 || again["written"] != false || mustGet(t, s, key).Versions != before+1 { + t.Fatalf("a second run wrote: %v", again) + } + }) + } t.Run("what a refresh removed is not restored", func(t *testing.T) { key, _ := accepted(t) e := mustGet(t, s, key) @@ -118,7 +130,7 @@ func TestRestoreGivesBackWhatAVersionDropped(t *testing.T) { t.Run("a record with a version waiting is skipped, and says so", func(t *testing.T) { key, _ := accepted(t) - lose(t, key) + lose(t, key, false) p := insightTo(t, w, key) p.Insight = "it said nothing of the day" if _, err := w.k.Propose("author@g14", p); err != nil { diff --git a/modules/mesh-knowledge/cmd/mesh-knowledge/tools.go b/modules/mesh-knowledge/cmd/mesh-knowledge/tools.go index ac5b7ef34..3e9d84565 100644 --- a/modules/mesh-knowledge/cmd/mesh-knowledge/tools.go +++ b/modules/mesh-knowledge/cmd/mesh-knowledge/tools.go @@ -442,9 +442,11 @@ func moduleTools(k *KB, caller func(map[string]any) string) []stdio.Tool { }}, {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.", + "(reconstructed, amended, touches, the imported fields, source, aliases), that a version made by propose dropped (an accepted version that lacks what the accepted version " + + "before it held; what a refresh, a form-only version or a mark removed is not restored), 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 the version before the drop held were added; no judgement runs. " + + "With dry-run nothing is written. A record with a version waiting for its review is skipped, and the answer names it as skipped. " + + "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")