From 84f1e5b93b0a5af836477d009b16850a9c04b71c Mon Sep 17 00:00:00 2001 From: jochen Date: Wed, 25 Nov 2026 16:44:56 +0100 Subject: [PATCH 1/7] 466-a: a version over an accepted record keeps the served insights and fields MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Builds: design 55 §2, §2.2, §7; lead answers L-466-1, L-466-2 (issue 466 note 18); 466/AC1, AC2, AC3 --- .../cmd/mesh-knowledge/active_effort_test.go | 9 +++-- .../cmd/mesh-knowledge/dated_test.go | 33 ++++++++++++++++++ .../mesh-knowledge/cmd/mesh-knowledge/kb.go | 34 +++++++++++++++++++ .../cmd/mesh-knowledge/review_test.go | 15 ++++++++ 4 files changed, 89 insertions(+), 2 deletions(-) diff --git a/modules/mesh-knowledge/cmd/mesh-knowledge/active_effort_test.go b/modules/mesh-knowledge/cmd/mesh-knowledge/active_effort_test.go index ccd990a33..f1f6ffba7 100644 --- a/modules/mesh-knowledge/cmd/mesh-knowledge/active_effort_test.go +++ b/modules/mesh-knowledge/cmd/mesh-knowledge/active_effort_test.go @@ -53,6 +53,11 @@ func TestAnActiveEffortTakesANewVersionWithANewDocument(t *testing.T) { first := effortAsk(r.Number, StatusActive, overview) w.accepted(t, "author@g14", first) key := Key{KindResearch, r.Number} + // 466/AC2: an insight accepted earlier is kept by the new version. + corrected := first + corrected.Insight = "it said less" + w.accepted(t, "author@g14", corrected) + insightsAt := mustGet(t, w.k.Store, key).Versions reviews, versions := len(w.judge.reviews), mustGet(t, w.k.Store, key).Versions next := c.next(r.Number) next.Why = c.name @@ -85,7 +90,7 @@ func TestAnActiveEffortTakesANewVersionWithANewDocument(t *testing.T) { } e = mustGet(t, w.k.Store, key) cur := e.Current - if e.Pending != nil || cur.Status != c.status || len(cur.Insights) != 0 || !reflect.DeepEqual(cur.Sections, c.docs) { + if e.Pending != nil || cur.Status != c.status || len(cur.Insights) != 1 || !reflect.DeepEqual(cur.Sections, c.docs) { t.Fatalf("after the review: %s, pending %v, insights %v, sections %+v", cur.Status, e.Pending != nil, cur.Insights, cur.Sections) } if cur.Title != first.Title || cur.Date != first.Date || !reflect.DeepEqual(cur.Deciders, first.Deciders) { @@ -99,7 +104,7 @@ func TestAnActiveEffortTakesANewVersionWithANewDocument(t *testing.T) { t.Fatal(err) } for _, v := range h.Versions { - if v.Insight { + if v.Insight && v.N > insightsAt { t.Errorf("version %d is marked an insight", v.N) } } diff --git a/modules/mesh-knowledge/cmd/mesh-knowledge/dated_test.go b/modules/mesh-knowledge/cmd/mesh-knowledge/dated_test.go index 20bbbdecf..6f3cf66e5 100644 --- a/modules/mesh-knowledge/cmd/mesh-knowledge/dated_test.go +++ b/modules/mesh-knowledge/cmd/mesh-knowledge/dated_test.go @@ -1,8 +1,11 @@ package main import ( + "context" + "reflect" "strings" "testing" + "time" ) // A rule holds a record from the day hq's checks began to hold it (the review of mesh-catalog#206): a dated @@ -95,4 +98,34 @@ func TestAnInsightToADecisionOfOctoberKeepsItsWords(t *testing.T) { t.Errorf("an insight to ADR %04d was refused: %v", n, err) } } + // 466/AC3: an insight keeps every field of the served version that propose does not take, and a drafted clause + // stays drafted. + t.Run("keeps what propose does not take", func(t *testing.T) { + w := newWorld(t, newMemStore(), hqMain(t)) + if _, err := w.k.Import("import@test", ImportAsk{}, hqMain(t)); err != nil { + t.Fatal(err) + } + key := Key{KindDecision, 276} + e := mustGet(t, w.k.Store, key) + r := e.Current.Clone() + no := false + r.Reconstructed, r.Amended, r.Touches = &no, []string{"2026-10-12"}, []string{"the tool runtime"} + r.Extra, r.Source, r.Aliases = map[string]string{"owner": "x"}, "hq@abc:02-DECISIONS/0276.md", map[string]string{"old.md": "New"} + r.Insights = []Insight{{Date: "2026-10-14", Before: "earlier", By: "someone@ace"}} + r.Clauses[0].Source = SourceDrafted + if err := w.k.Store.Save(context.Background(), Version{By: "test", At: time.Now(), Why: "seed", Record: r}, true, true, e.Revision); err != nil { + t.Fatal(err) + } + if _, err := w.k.Propose("corrector@ace", insightTo(t, w, key)); err != nil { + t.Fatal(err) + } + w.reviewAll(t) + got := mustGet(t, w.k.Store, key).Current + if len(got.Insights) != 2 || got.Insights[0].Before != "earlier" || got.Reconstructed == nil || *got.Reconstructed || + !reflect.DeepEqual(got.Amended, r.Amended) || !reflect.DeepEqual(got.Touches, r.Touches) || + !reflect.DeepEqual(got.Extra, r.Extra) || got.Source != r.Source || !reflect.DeepEqual(got.Aliases, r.Aliases) || + got.Clauses[0].Source != SourceDrafted { + t.Fatalf("an insight dropped what the served version held: %+v", got) + } + }) } diff --git a/modules/mesh-knowledge/cmd/mesh-knowledge/kb.go b/modules/mesh-knowledge/cmd/mesh-knowledge/kb.go index 87741c18f..3db9e21e7 100644 --- a/modules/mesh-knowledge/cmd/mesh-knowledge/kb.go +++ b/modules/mesh-knowledge/cmd/mesh-knowledge/kb.go @@ -415,6 +415,7 @@ func (k *KB) Propose(caller string, a ProposeAsk) (*Proposed, error) { } r.AcceptedAt = held.Current.AcceptedAt r.Parts = held.Current.Parts + keepServed(r, held.Current) if held.Pending != nil { // A revision of a pending version keeps its earlier reviews, so the round count goes on (ADR 0665 §1). r.Reviews, r.CountFrom = held.Pending.Reviews, held.Pending.CountFrom @@ -513,6 +514,39 @@ func (k *KB) Propose(caller string, a ProposeAsk) (*Proposed, error) { return &Proposed{Record: key.String(), Version: n, Status: StatusProposed, Says: says + "."}, nil } +// keepServed gives a version over an accepted record what the served version holds and propose's arguments do not +// carry (novox/hq issue 466; a record keeps every progressive insight its accepted versions made, and every field it +// was imported with): the insights, oldest first, so the judgement and the operator's digest see what will be served +// and accept only adds the new one; reconstructed, amended, touches, the imported fields, the source and the +// aliases; and, for a clause whose text is unchanged, its source, so a drafted clause stays drafted. +func keepServed(r, served *Record) { + r.Insights = append([]Insight(nil), served.Insights...) + if served.Reconstructed != nil { + v := *served.Reconstructed + r.Reconstructed = &v + } + r.Amended = append([]string(nil), served.Amended...) + r.Touches = append([]string(nil), served.Touches...) + if len(served.Extra) > 0 { + r.Extra = make(map[string]string, len(served.Extra)) + for k, v := range served.Extra { + r.Extra[k] = v + } + } + r.Source = served.Source + if len(served.Aliases) > 0 { + r.Aliases = make(map[string]string, len(served.Aliases)) + for k, v := range served.Aliases { + r.Aliases[k] = v + } + } + for i, c := range r.Clauses { + if old := served.Clause(c.N); old != nil && strings.TrimSpace(old.Text) == strings.TrimSpace(c.Text) { + r.Clauses[i].Source = old.Source + } + } +} + // activeLimitRefusal (ADR 0629 §4 names only a new effort or one taken up from an idea, never a new version of an active one; a check of the record set beside the one-record rules of ADR 0290 §4) says why a research effort may not be active now: the limit of efforts other than this one // already active (accepted at active, or proposed at active and waiting for review) is reached (ADR 0629). Empty // when it may. diff --git a/modules/mesh-knowledge/cmd/mesh-knowledge/review_test.go b/modules/mesh-knowledge/cmd/mesh-knowledge/review_test.go index ac8379afb..695d6dcaa 100644 --- a/modules/mesh-knowledge/cmd/mesh-knowledge/review_test.go +++ b/modules/mesh-knowledge/cmd/mesh-knowledge/review_test.go @@ -392,6 +392,21 @@ func TestAnAcceptedRecordChangesOnlyByAnInsightReviewedLikeAProposal(t *testing. if c := mustGet(t, w.k.Store, Key{KindDecision, 276}).Current.Clause(1); len(c.ChangedBy) != 1 { t.Fatalf("0276 §1 changed again by an insight: %v", c.ChangedBy) } + // 466/AC1: a second insight adds to the first and replaces nothing; the pending version already holds the first. + p.Insight = "it said only a minute" + p.Sections[2].Text = "Merges land sooner, by about a minute from the last merge, as measured." + if _, err := w.k.Propose("author@g14", p); err != nil { + t.Fatal(err) + } + if e = mustGet(t, w.k.Store, Key{KindDecision, n}); e.Pending == nil || len(e.Pending.Insights) != 1 { + t.Fatalf("the pending version does not hold the earlier insight: %+v", e.Pending) + } + w.reviewAll(t) + e = mustGet(t, w.k.Store, Key{KindDecision, n}) + if len(e.Current.Insights) != 2 || e.Current.Insights[0].Before != "it said only that merges land sooner" || + e.Current.Insights[1].Before != "it said only a minute" { + t.Fatalf("a second insight did not add to the first: %+v", e.Current.Insights) + } } // No verb takes a review from anyone: the review is the knowledge base's own. From 7da6ee61aa93bbffc744bdf82470229be75d261f Mon Sep 17 00:00:00 2001 From: jochen Date: Wed, 25 Nov 2026 16:46:58 +0100 Subject: [PATCH 2/7] 466-b: an insight keeps the supersedes and changes links (rule insight-links) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Builds: design 55 §4, §5; lead answer L-466-3 (issue 466 note 18); 466/AC4 --- modules/mesh-knowledge/README.md | 2 +- .../mesh-knowledge/cmd/mesh-knowledge/main.go | 2 +- .../cmd/mesh-knowledge/rules.go | 24 ++++++++++++++++--- .../cmd/mesh-knowledge/rules_test.go | 16 +++++++++++-- .../cmd/mesh-knowledge/wholeset_test.go | 16 ++++++++++++- 5 files changed, 52 insertions(+), 8 deletions(-) diff --git a/modules/mesh-knowledge/README.md b/modules/mesh-knowledge/README.md index 64b72285d..5954743a8 100644 --- a/modules/mesh-knowledge/README.md +++ b/modules/mesh-knowledge/README.md @@ -78,7 +78,7 @@ as text. `get` serves each, and any other field a record of hq wrote under `fiel ## Writing a record 1. `reserve` a number for its kind. -2. `propose` the record whole. It is refused, naming every reason at once, when it breaks one of the **31 rules +2. `propose` the record whole. It is refused, naming every reason at once, when it breaks one of the **32 rules that judge one record or its prose**. They are hq's checks moved to the write; `PerRecordRules` in `rules.go` lists each one with the check it comes from: - 25 on the structure: kind, reserved number, title, topic, date, deciders, research status, at least one diff --git a/modules/mesh-knowledge/cmd/mesh-knowledge/main.go b/modules/mesh-knowledge/cmd/mesh-knowledge/main.go index ba7bebc84..66305c916 100644 --- a/modules/mesh-knowledge/cmd/mesh-knowledge/main.go +++ b/modules/mesh-knowledge/cmd/mesh-knowledge/main.go @@ -1030,7 +1030,7 @@ func verbTools(k *KB, caller func(map[string]any) string) []stdio.Tool { "clauses, each opening with a bold lead (\"**What is decided.** …\"), and its reasoning in sections (Context, " + "Considered Options, Consequences, How it is checked, References); for a research effort its status and documents; its " + "links (supersedes, changes \"0276 §4\", extends, became, cites), and whether it needs the operator, with the brief its ask shows. Refused, naming " + - "every reason at once and writing nothing, when it breaks one of the 31 rules that judge one record or its prose — a " + + "every reason at once and writing nothing, when it breaks one of the 32 rules that judge one record or its prose — a " + "link to nothing, a clause without a bold lead, a superseded record cited alone, an unknown topic, a retired word. A " + "proposal is revised by proposing it again; an accepted record (an active research effort or an idea excepted, which take new versions; at most as many research efforts are active as the setting research-active-at-most allows, three by default) changes only by a progressive insight (insight: what " + "stood before). A research effort's new version may leave out its sections, and its title, date and deciders: it then " + diff --git a/modules/mesh-knowledge/cmd/mesh-knowledge/rules.go b/modules/mesh-knowledge/cmd/mesh-knowledge/rules.go index a893c47c2..2fd0e0005 100644 --- a/modules/mesh-knowledge/cmd/mesh-knowledge/rules.go +++ b/modules/mesh-knowledge/cmd/mesh-knowledge/rules.go @@ -2,7 +2,7 @@ package main // The rules a record is held to (novox/hq ADR 0290 rule 4, to-be 55 §4 and §5). // -// **31 rules judge one record or its prose**, and `propose` refuses a record that breaks any of them, naming every +// **32 rules judge one record or its prose**, and `propose` refuses a record that breaks any of them, naming every // one it breaks at once. They are hq's checks (00-META/checks/records.py, cycle.py, index.py, words.py) as they // judge one record, moved to the write: 25 on its structure, 4 on its prose; and the brief a record that needs // the operator is asked with (novox/hq issue 639). Each has an id, said in the refusal, @@ -28,7 +28,7 @@ type Rule struct { From string } -// PerRecordRules are the 31, in the order they are judged. +// PerRecordRules are the 32, in the order they are judged. var PerRecordRules = []Rule{ {"kind", "a record is a decision or a research effort", "cycle.py"}, {"reserved", "its number was reserved for its kind and holds no other record", "cycle.py (duplicate numbers)"}, @@ -61,6 +61,7 @@ var PerRecordRules = []Rule{ {"insight-form", "a progressive insight its text mentions is in the dated, marked form", "records.py insights"}, {"brief", "a record that needs the operator carries a brief its ask reads at a glance: a headline, a summary, at most three points, each fit for the phone", "novox/hq issue 639"}, {"section-form", "a section is a part with a plain title, once in the record, and its text holds no local path, no file and line, and no link to a file", "to-be 55 §2.2"}, + {"insight-links", "a progressive insight keeps exactly the accepted record's supersedes and changes links (it may add cites): changing which records a decision supersedes or changes is a new record", "novox/hq issue 466"}, } // Refusal is one rule a record breaks, and how. @@ -121,7 +122,7 @@ func allText(r *Record) string { return b.String() } -// JudgeRecord holds a proposed record to the 31 rules. held is the entry its number holds (a reservation, the +// JudgeRecord holds a proposed record to the 32 rules. held is the entry its number holds (a reservation, the // proposal being revised, or the accepted record an insight corrects); lookup reads every other record. func JudgeRecord(r *Record, held *Entry, lookup Lookup, ref *Reference) []Refusal { return JudgeRecordOn(r, held, lookup, ref, r.Date) @@ -295,6 +296,23 @@ func JudgeRecordOn(r *Record, held *Entry, lookup Lookup, ref *Reference, day st } } } + if held != nil && held.Current.Accepted() && r.IsInsight() { + var changed []string + for _, l := range r.Links { + if (l.Kind == LinkSupersedes || l.Kind == LinkChanges) && !hasLink(held.Current, l) { + changed = append(changed, fmt.Sprintf("added %s", l)) + } + } + for _, l := range held.Current.Links { + if (l.Kind == LinkSupersedes || l.Kind == LinkChanges) && !hasLink(r, l) { + changed = append(changed, fmt.Sprintf("dropped %s", l)) + } + } + if len(changed) > 0 { + refuse("insight-links", "a progressive insight corrects a fact and leaves the decision standing, so it keeps the supersedes and changes links of the accepted record (%s); "+ + "changing what a record supersedes or changes is a new record: write one", strings.Join(changed, ", ")) + } + } if r.Kind == KindResearch && r.Effort == StatusGraduated { became := false for _, l := range r.Links { diff --git a/modules/mesh-knowledge/cmd/mesh-knowledge/rules_test.go b/modules/mesh-knowledge/cmd/mesh-knowledge/rules_test.go index 2e724cf38..e22fe666b 100644 --- a/modules/mesh-knowledge/cmd/mesh-knowledge/rules_test.go +++ b/modules/mesh-knowledge/cmd/mesh-knowledge/rules_test.go @@ -96,6 +96,18 @@ func TestEveryPerRecordRuleRefusesAProposal(t *testing.T) { {"brief", func(p *ProposeAsk, w *world, t *testing.T) { p.Date, p.NeedsOperator = "2026-10-13", true }}, + {"insight-links", func(p *ProposeAsk, w *world, t *testing.T) { + // 466/AC4: an insight that drops the changes link of the accepted record is refused, and one that adds a + // cites link is taken. + w.accepted(t, "author@test", valid(p.Number)) + p.Insight = "it said the clause stood" + cites := *p + cites.Links = append(append([]LinkAsk(nil), p.Links...), LinkAsk{Kind: LinkCites, To: "0011"}) + if _, err := w.k.Propose("author@test", cites); err != nil { + t.Fatalf("an insight adding a cites link was refused: %v", err) + } + p.Links = p.Links[:1] + }}, {"section-form", func(p *ProposeAsk, w *world, t *testing.T) { p.Date, p.Sections[0].Name = "2026-11-04", "00-context.md" }}, @@ -109,8 +121,8 @@ func TestEveryPerRecordRuleRefusesAProposal(t *testing.T) { t.Errorf("the rule %s has no case", r.ID) } } - if len(PerRecordRules) != 31 { - t.Errorf("%d per-record rules; ADR 0290 counts 29, issue 639 adds the brief and to-be 55 §2.2 the form of a section", len(PerRecordRules)) + if len(PerRecordRules) != 32 { + t.Errorf("%d per-record rules; ADR 0290 counts 29, issue 639 adds the brief, to-be 55 §2.2 the form of a section and issue 466 the links of an insight", len(PerRecordRules)) } for _, c := range cases { t.Run(c.rule, func(t *testing.T) { diff --git a/modules/mesh-knowledge/cmd/mesh-knowledge/wholeset_test.go b/modules/mesh-knowledge/cmd/mesh-knowledge/wholeset_test.go index 733566767..07eb0b5da 100644 --- a/modules/mesh-knowledge/cmd/mesh-knowledge/wholeset_test.go +++ b/modules/mesh-knowledge/cmd/mesh-knowledge/wholeset_test.go @@ -5,6 +5,7 @@ import ( "errors" "strings" "testing" + "time" ) // A decision nothing would cite is not accepted: its review asks changes itself, without a judgement, naming the @@ -130,7 +131,20 @@ func TestACorrectionThatDropsAChangesLinkWithdrawsTheClausesMark(t *testing.T) { } a.Links = a.Links[:1] // only extends 0276 now a.Insight = "it no longer changes 0276 §1" - w.accepted(t, "author@g14", a) + // 466/AC4: an insight no longer drops a changes link; a version that did (made before the rule) is written + // directly, and its mark is withdrawn. + if _, err := w.k.Propose("author@g14", a); err == nil || !strings.Contains(err.Error(), "- insight-links:") { + t.Fatalf("an insight dropping a changes link was taken: %v", err) + } + e := mustGet(t, s, Key{KindDecision, n}) + r := e.Current.Clone() + r.Links = r.Links[:1] + if err := s.Save(context.Background(), Version{By: "test", At: time.Now(), Why: "an older insight", Record: r}, true, true, e.Revision); err != nil { + t.Fatal(err) + } + if _, err := w.k.RecomputeMarks(context.Background()); err != nil { + t.Fatal(err) + } c := mustGet(t, s, Key{KindDecision, 276}).Current.Clause(1) if c.Status != ClauseInForce || len(c.ChangedBy) != 0 { t.Fatalf("0276 §1 keeps a mark no accepted link makes: %+v", c) From 89fb416a1d0705c25d006b623b5fa821e18d2365 Mon Sep 17 00:00:00 2001 From: jochen Date: Wed, 25 Nov 2026 16:49:19 +0100 Subject: [PATCH 3/7] 466-c: knowledge_restore gives back the insights and fields a version dropped MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Builds: design 55 §1.1, §10; lead answers L-466-4, L-466-5 (issue 466 note 18); 466/AC5, AC7 --- .../cmd/mesh-knowledge/restore.go | 195 ++++++++++++++++++ .../cmd/mesh-knowledge/restore_test.go | 63 ++++++ .../cmd/mesh-knowledge/tools.go | 36 +++- .../cmd/mesh-knowledge/tools_test.go | 2 +- modules/mesh-knowledge/module.json | 3 +- 5 files changed, 296 insertions(+), 3 deletions(-) create mode 100644 modules/mesh-knowledge/cmd/mesh-knowledge/restore.go create mode 100644 modules/mesh-knowledge/cmd/mesh-knowledge/restore_test.go diff --git a/modules/mesh-knowledge/cmd/mesh-knowledge/restore.go b/modules/mesh-knowledge/cmd/mesh-knowledge/restore.go new file mode 100644 index 000000000..48ec0d1e2 --- /dev/null +++ b/modules/mesh-knowledge/cmd/mesh-knowledge/restore.go @@ -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 +} diff --git a/modules/mesh-knowledge/cmd/mesh-knowledge/restore_test.go b/modules/mesh-knowledge/cmd/mesh-knowledge/restore_test.go new file mode 100644 index 000000000..406e78f18 --- /dev/null +++ b/modules/mesh-knowledge/cmd/mesh-knowledge/restore_test.go @@ -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) + } + }) +} diff --git a/modules/mesh-knowledge/cmd/mesh-knowledge/tools.go b/modules/mesh-knowledge/cmd/mesh-knowledge/tools.go index df27bb070..d19599ac1 100644 --- a/modules/mesh-knowledge/cmd/mesh-knowledge/tools.go +++ b/modules/mesh-knowledge/cmd/mesh-knowledge/tools.go @@ -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 + }}, } } diff --git a/modules/mesh-knowledge/cmd/mesh-knowledge/tools_test.go b/modules/mesh-knowledge/cmd/mesh-knowledge/tools_test.go index e09efe24c..015a552ad 100644 --- a/modules/mesh-knowledge/cmd/mesh-knowledge/tools_test.go +++ b/modules/mesh-knowledge/cmd/mesh-knowledge/tools_test.go @@ -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) } } diff --git a/modules/mesh-knowledge/module.json b/modules/mesh-knowledge/module.json index 43fbf81f2..b7a5eab53 100644 --- a/modules/mesh-knowledge/module.json +++ b/modules/mesh-knowledge/module.json @@ -188,7 +188,8 @@ "knowledge_move", "knowledge_move_check", "knowledge_overview", - "knowledge_overview_reread" + "knowledge_overview_reread", + "knowledge_restore" ], "emits": [ "reserved", From a569c80e98d4119c3b6b0a43b4cb234d4af78df4 Mon Sep 17 00:00:00 2001 From: jochen Date: Wed, 25 Nov 2026 17:09:24 +0100 Subject: [PATCH 4/7] 466-a review: accept takes the served fields; AC1 and AC3 over every store; the rule counts named MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Builds: design 55 §2, §2.2, §7; lead answers L-466-1, L-466-2 (issue 466 note 18), note 20 items (c), (d); 466/AC1, AC3 --- modules/mesh-knowledge/README.md | 4 + .../cmd/mesh-knowledge/dated_test.go | 74 ++++++---- .../mesh-knowledge/cmd/mesh-knowledge/kb.go | 22 ++- .../cmd/mesh-knowledge/review_test.go | 138 ++++++++++-------- .../cmd/mesh-knowledge/rules.go | 5 +- 5 files changed, 151 insertions(+), 92 deletions(-) diff --git a/modules/mesh-knowledge/README.md b/modules/mesh-knowledge/README.md index 5954743a8..ba4df363f 100644 --- a/modules/mesh-knowledge/README.md +++ b/modules/mesh-knowledge/README.md @@ -91,6 +91,10 @@ as text. `get` serves each, and any other field a record of hq wrote under `fiel - 1 on the operator's ask (hq issue 639): `brief`, a record that needs the operator carries a brief its ask reads at a glance — a headline of at most 60 characters, a one-line summary of at most 300, at most three points of at most 160, and no path, address, dotted name or long word. It holds records dated from 2026-10-13. + - 1 on the form of a section (`section-form`): a part with a plain title, once in the record, whose text holds no + local path, no file and line, and no link to a file; + - 1 on a progressive insight's links (`insight-links`, hq issue 466): it keeps exactly the accepted record's + supersedes and changes links (a cites link may be added); changing them is a new record. A rule holds a record from the day hq's checks began to hold it (`RuleFrom` in `rules.go`): - retired words from 2026-10-07 and daemons' lines from 2026-10-09 for a research effort; for a decision record diff --git a/modules/mesh-knowledge/cmd/mesh-knowledge/dated_test.go b/modules/mesh-knowledge/cmd/mesh-knowledge/dated_test.go index 6f3cf66e5..8c2f20b02 100644 --- a/modules/mesh-knowledge/cmd/mesh-knowledge/dated_test.go +++ b/modules/mesh-knowledge/cmd/mesh-knowledge/dated_test.go @@ -100,32 +100,52 @@ func TestAnInsightToADecisionOfOctoberKeepsItsWords(t *testing.T) { } // 466/AC3: an insight keeps every field of the served version that propose does not take, and a drafted clause // stays drafted. - t.Run("keeps what propose does not take", func(t *testing.T) { - w := newWorld(t, newMemStore(), hqMain(t)) - if _, err := w.k.Import("import@test", ImportAsk{}, hqMain(t)); err != nil { - t.Fatal(err) - } - key := Key{KindDecision, 276} - e := mustGet(t, w.k.Store, key) - r := e.Current.Clone() - no := false - r.Reconstructed, r.Amended, r.Touches = &no, []string{"2026-10-12"}, []string{"the tool runtime"} - r.Extra, r.Source, r.Aliases = map[string]string{"owner": "x"}, "hq@abc:02-DECISIONS/0276.md", map[string]string{"old.md": "New"} - r.Insights = []Insight{{Date: "2026-10-14", Before: "earlier", By: "someone@ace"}} - r.Clauses[0].Source = SourceDrafted - if err := w.k.Store.Save(context.Background(), Version{By: "test", At: time.Now(), Why: "seed", Record: r}, true, true, e.Revision); err != nil { - t.Fatal(err) - } - if _, err := w.k.Propose("corrector@ace", insightTo(t, w, key)); err != nil { - t.Fatal(err) - } - w.reviewAll(t) - got := mustGet(t, w.k.Store, key).Current - if len(got.Insights) != 2 || got.Insights[0].Before != "earlier" || got.Reconstructed == nil || *got.Reconstructed || - !reflect.DeepEqual(got.Amended, r.Amended) || !reflect.DeepEqual(got.Touches, r.Touches) || - !reflect.DeepEqual(got.Extra, r.Extra) || got.Source != r.Source || !reflect.DeepEqual(got.Aliases, r.Aliases) || - got.Clauses[0].Source != SourceDrafted { - t.Fatalf("an insight dropped what the served version held: %+v", got) - } + eachStore(t, func(t *testing.T, s Store) { + t.Run("keeps what propose does not take", func(t *testing.T) { + w := newWorld(t, s, hqMain(t)) + if _, err := w.k.Import("import@test", ImportAsk{}, hqMain(t)); err != nil { + t.Fatal(err) + } + key := Key{KindDecision, 276} + e := mustGet(t, w.k.Store, key) + r := e.Current.Clone() + no := false + r.Reconstructed, r.Amended, r.Touches = &no, []string{"2026-10-12"}, []string{"the tool runtime"} + r.Extra, r.Source, r.Aliases = map[string]string{"owner": "x"}, "hq@abc:02-DECISIONS/0276.md", map[string]string{"old.md": "New"} + r.Insights = []Insight{{Date: "2026-10-14", Before: "earlier", By: "someone@ace"}} + r.Clauses[0].Source = SourceDrafted + if err := w.k.Store.Save(context.Background(), Version{By: "test", At: time.Now(), Why: "seed", Record: r}, true, true, e.Revision); err != nil { + t.Fatal(err) + } + if _, err := w.k.Propose("corrector@ace", insightTo(t, w, key)); err != nil { + t.Fatal(err) + } + w.reviewAll(t) + got := mustGet(t, w.k.Store, key).Current + if len(got.Insights) != 2 || got.Insights[0].Before != "earlier" || got.Reconstructed == nil || *got.Reconstructed || + !reflect.DeepEqual(got.Amended, r.Amended) || !reflect.DeepEqual(got.Touches, r.Touches) || + !reflect.DeepEqual(got.Extra, r.Extra) || got.Source != r.Source || !reflect.DeepEqual(got.Aliases, r.Aliases) || + got.Clauses[0].Source != SourceDrafted { + t.Fatalf("an insight dropped what the served version held: %+v", got) + } + // 466/AC3: what another record marks on this one while its insight waits is kept at acceptance. + q := insightTo(t, w, key) + q.Insight = "it said nothing of the count" + if _, err := w.k.Propose("corrector@ace", q); err != nil { + t.Fatal(err) + } + e = mustGet(t, w.k.Store, key) + sup := e.Current.Clone() + sup.Extra = map[string]string{"owner": "x", "superseded-by": "ADR 9999"} + sup.Status = StatusSuperseded + if err := w.k.Store.Save(context.Background(), Version{By: "test", At: time.Now(), Why: "superseded", Record: sup}, true, false, e.Revision); err != nil { + t.Fatal(err) + } + w.reviewAll(t) + got = mustGet(t, w.k.Store, key).Current + if got.Status != StatusSuperseded || got.Extra["superseded-by"] != "ADR 9999" || len(got.Insights) != 3 { + t.Fatalf("an insight accepted after the record was superseded lost the mark: %s %v %d insights", got.Status, got.Extra, len(got.Insights)) + } + }) }) } diff --git a/modules/mesh-knowledge/cmd/mesh-knowledge/kb.go b/modules/mesh-knowledge/cmd/mesh-knowledge/kb.go index 3db9e21e7..188516f8b 100644 --- a/modules/mesh-knowledge/cmd/mesh-knowledge/kb.go +++ b/modules/mesh-knowledge/cmd/mesh-knowledge/kb.go @@ -521,6 +521,19 @@ func (k *KB) Propose(caller string, a ProposeAsk) (*Proposed, error) { // aliases; and, for a clause whose text is unchanged, its source, so a drafted clause stays drafted. func keepServed(r, served *Record) { r.Insights = append([]Insight(nil), served.Insights...) + keepFields(r, served) + for i, c := range r.Clauses { + if old := served.Clause(c.N); old != nil && strings.TrimSpace(old.Text) == strings.TrimSpace(c.Text) { + r.Clauses[i].Source = old.Source + } + } +} + +// keepFields copies the fields of the served version that propose's arguments do not carry: reconstructed, amended, +// touches, the imported fields (which hold what the knowledge base marked on the record, such as superseded-by), source +// and aliases. accept takes them again from the version served when the version is accepted. +func keepFields(r, served *Record) { + r.Reconstructed, r.Extra, r.Aliases = nil, nil, nil if served.Reconstructed != nil { v := *served.Reconstructed r.Reconstructed = &v @@ -540,11 +553,6 @@ func keepServed(r, served *Record) { r.Aliases[k] = v } } - for i, c := range r.Clauses { - if old := served.Clause(c.N); old != nil && strings.TrimSpace(old.Text) == strings.TrimSpace(c.Text) { - r.Clauses[i].Source = old.Source - } - } } // activeLimitRefusal (ADR 0629 §4 names only a new effort or one taken up from an idea, never a new version of an active one; a check of the record set beside the one-record rules of ADR 0290 §4) says why a research effort may not be active now: the limit of efforts other than this one @@ -590,6 +598,10 @@ func (k *KB) accept(ctx context.Context, held *Entry, w *Record, by, why, warran in := r.Pending.Insight isInsight := r.IsInsight() r.Pending = nil + // What the record holds now, not what it held when the version was proposed: a record superseded, or marked + // by another record's acceptance, while this version waited keeps its marks (novox/hq issue 466). + r.Insights = append([]Insight(nil), held.Current.Insights...) + keepFields(r, held.Current) if isInsight { r.Insights = append(r.Insights, in) } diff --git a/modules/mesh-knowledge/cmd/mesh-knowledge/review_test.go b/modules/mesh-knowledge/cmd/mesh-knowledge/review_test.go index 695d6dcaa..a8ceb11a5 100644 --- a/modules/mesh-knowledge/cmd/mesh-knowledge/review_test.go +++ b/modules/mesh-knowledge/cmd/mesh-knowledge/review_test.go @@ -349,64 +349,86 @@ func TestTheOperatorsDeclineLeavesTheRecordProposed(t *testing.T) { // An accepted record changes only by a progressive insight, which is reviewed like a proposal while the accepted // version is served. func TestAnAcceptedRecordChangesOnlyByAnInsightReviewedLikeAProposal(t *testing.T) { - w := imported(t, newMemStore()) - n := w.reserved(t, "author@g14") - w.accepted(t, "author@g14", valid(n)) - p := valid(n) - p.Sections[2].Text = "Merges land sooner, by about a minute." - if _, err := w.k.Propose("author@g14", p); err == nil || !strings.Contains(err.Error(), "progressive insight") { - t.Fatalf("an accepted record rewritten: %v", err) - } - p.Insight = "it said only that merges land sooner" - w.judge.review = `{"verdict":"changes","findings":["Consequences: say what a minute is measured from"]}` - if _, err := w.k.Propose("author@g14", p); err != nil { - t.Fatal(err) - } - w.reviewAll(t) - e := mustGet(t, w.k.Store, Key{KindDecision, n}) - if e.Pending == nil || e.Current.Section("Consequences") != "Merges land sooner." || len(e.Pending.Reviews) != 1 || - e.Pending.Reviews[0].Verdict != VerdictChanges { - t.Fatalf("the insight was served before its review approved it: %+v", e.Current.Sections) - } - // 1970/AC2: the same for a pending insight. - g, err := w.k.Get(GetAsk{Kind: KindDecision, Number: n}) - if err != nil { - t.Fatal(err) - } - if len(g.PendingReviews) != 1 || !strings.Contains(g.PendingReviews[0].Findings, "what a minute is measured from") || - !strings.Contains(g.Pending, "changes asked") { - t.Fatalf("pending insight findings not shown: %+v, %q", g.PendingReviews, g.Pending) - } - w.judge.review = "" - p.Sections[2].Text = "Merges land sooner, by about a minute from the last merge." - if _, err := w.k.Propose("author@g14", p); err != nil { - t.Fatal(err) - } - w.reviewAll(t) - e = mustGet(t, w.k.Store, Key{KindDecision, n}) - if e.Pending != nil || e.Current.Status != StatusAccepted || len(e.Current.Insights) != 1 || - e.Current.Section("Consequences") != p.Sections[2].Text { - t.Fatalf("after the insight's review: %+v", e.Current) - } - // 0276 §1 was changed once, by the acceptance; the insight keeps the link and changes it no further. - if c := mustGet(t, w.k.Store, Key{KindDecision, 276}).Current.Clause(1); len(c.ChangedBy) != 1 { - t.Fatalf("0276 §1 changed again by an insight: %v", c.ChangedBy) - } - // 466/AC1: a second insight adds to the first and replaces nothing; the pending version already holds the first. - p.Insight = "it said only a minute" - p.Sections[2].Text = "Merges land sooner, by about a minute from the last merge, as measured." - if _, err := w.k.Propose("author@g14", p); err != nil { - t.Fatal(err) - } - if e = mustGet(t, w.k.Store, Key{KindDecision, n}); e.Pending == nil || len(e.Pending.Insights) != 1 { - t.Fatalf("the pending version does not hold the earlier insight: %+v", e.Pending) - } - w.reviewAll(t) - e = mustGet(t, w.k.Store, Key{KindDecision, n}) - if len(e.Current.Insights) != 2 || e.Current.Insights[0].Before != "it said only that merges land sooner" || - e.Current.Insights[1].Before != "it said only a minute" { - t.Fatalf("a second insight did not add to the first: %+v", e.Current.Insights) - } + eachStore(t, func(t *testing.T, s Store) { + w := imported(t, s) + n := w.reserved(t, "author@g14") + w.accepted(t, "author@g14", valid(n)) + p := valid(n) + p.Sections[2].Text = "Merges land sooner, by about a minute." + if _, err := w.k.Propose("author@g14", p); err == nil || !strings.Contains(err.Error(), "progressive insight") { + t.Fatalf("an accepted record rewritten: %v", err) + } + p.Insight = "it said only that merges land sooner" + w.judge.review = `{"verdict":"changes","findings":["Consequences: say what a minute is measured from"]}` + if _, err := w.k.Propose("author@g14", p); err != nil { + t.Fatal(err) + } + w.reviewAll(t) + e := mustGet(t, w.k.Store, Key{KindDecision, n}) + if e.Pending == nil || e.Current.Section("Consequences") != "Merges land sooner." || len(e.Pending.Reviews) != 1 || + e.Pending.Reviews[0].Verdict != VerdictChanges { + t.Fatalf("the insight was served before its review approved it: %+v", e.Current.Sections) + } + // 1970/AC2: the same for a pending insight. + g, err := w.k.Get(GetAsk{Kind: KindDecision, Number: n}) + if err != nil { + t.Fatal(err) + } + if len(g.PendingReviews) != 1 || !strings.Contains(g.PendingReviews[0].Findings, "what a minute is measured from") || + !strings.Contains(g.Pending, "changes asked") { + t.Fatalf("pending insight findings not shown: %+v, %q", g.PendingReviews, g.Pending) + } + w.judge.review = "" + p.Sections[2].Text = "Merges land sooner, by about a minute from the last merge." + if _, err := w.k.Propose("author@g14", p); err != nil { + t.Fatal(err) + } + w.reviewAll(t) + e = mustGet(t, w.k.Store, Key{KindDecision, n}) + if e.Pending != nil || e.Current.Status != StatusAccepted || len(e.Current.Insights) != 1 || + e.Current.Section("Consequences") != p.Sections[2].Text { + t.Fatalf("after the insight's review: %+v", e.Current) + } + // 0276 §1 was changed once, by the acceptance; the insight keeps the link and changes it no further. + if c := mustGet(t, w.k.Store, Key{KindDecision, 276}).Current.Clause(1); len(c.ChangedBy) != 1 { + t.Fatalf("0276 §1 changed again by an insight: %v", c.ChangedBy) + } + // 466/AC1: a second insight, by another author on another day, adds to the first and replaces nothing; the + // pending version already holds the first, and history between the two accepted versions shows only the second added. + first := mustGet(t, w.k.Store, Key{KindDecision, n}).Versions + w.clock = w.clock.AddDate(0, 0, 1) + p.Insight = "it said only a minute" + p.Sections[2].Text = "Merges land sooner, by about a minute from the last merge, as measured." + if _, err := w.k.Propose("corrector@ace", p); err != nil { + t.Fatal(err) + } + if e = mustGet(t, w.k.Store, Key{KindDecision, n}); e.Pending == nil || len(e.Pending.Insights) != 1 { + t.Fatalf("the pending version does not hold the earlier insight: %+v", e.Pending) + } + w.reviewAll(t) + e = mustGet(t, w.k.Store, Key{KindDecision, n}) + in := e.Current.Insights + if len(in) != 2 || in[0].Before != "it said only that merges land sooner" || in[0].By != "author@g14" || in[0].Date != "2026-10-11" || + in[1].Before != "it said only a minute" || in[1].By != "corrector@ace" || in[1].Date != "2026-10-12" { + t.Fatalf("a second insight did not add to the first: %+v", in) + } + h, err := w.k.History(Key{KindDecision, n}, first, e.Versions, 0) + if err != nil { + t.Fatal(err) + } + var added, removed []string + for _, l := range h.Diff { + switch { + case strings.HasPrefix(l, "+insight "): + added = append(added, l) + case strings.HasPrefix(l, "-insight "): + removed = append(removed, l) + } + } + if len(added) != 1 || !strings.Contains(added[0], "2026-10-12") || len(removed) != 0 { + t.Fatalf("history between the accepted versions: added %v, removed %v\n%v", added, removed, h.Diff) + } + }) } // No verb takes a review from anyone: the review is the knowledge base's own. diff --git a/modules/mesh-knowledge/cmd/mesh-knowledge/rules.go b/modules/mesh-knowledge/cmd/mesh-knowledge/rules.go index 2fd0e0005..d47dbf76e 100644 --- a/modules/mesh-knowledge/cmd/mesh-knowledge/rules.go +++ b/modules/mesh-knowledge/cmd/mesh-knowledge/rules.go @@ -4,8 +4,9 @@ package main // // **32 rules judge one record or its prose**, and `propose` refuses a record that breaks any of them, naming every // one it breaks at once. They are hq's checks (00-META/checks/records.py, cycle.py, index.py, words.py) as they -// judge one record, moved to the write: 25 on its structure, 4 on its prose; and the brief a record that needs -// the operator is asked with (novox/hq issue 639). Each has an id, said in the refusal, +// judge one record, moved to the write: 25 on its structure, 4 on its prose; the brief a record that needs +// the operator is asked with (novox/hq issue 639); the form of a section (section-form); and the links of a +// progressive insight (insight-links, novox/hq issue 466). Each has an id, said in the refusal, // and one case in the table test (TestEveryPerRecordRuleRefusesAProposal). // // **4 rules judge the whole record set** (wholeSetRules) and run after every acceptance, and in the check From ef7d3f69d2925a8db58a010f7a0ce6f90372b329 Mon Sep 17 00:00:00 2001 From: jochen Date: Wed, 25 Nov 2026 17:10:31 +0100 Subject: [PATCH 5/7] 466-b review: the insight-links case fails with either check disabled MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Builds: design 55 §4, §5; lead answer L-466-3 (issue 466 note 18); 466/AC4 --- .../cmd/mesh-knowledge/rules_test.go | 41 ++++++++++++++++--- 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/modules/mesh-knowledge/cmd/mesh-knowledge/rules_test.go b/modules/mesh-knowledge/cmd/mesh-knowledge/rules_test.go index e22fe666b..d5741d1d5 100644 --- a/modules/mesh-knowledge/cmd/mesh-knowledge/rules_test.go +++ b/modules/mesh-knowledge/cmd/mesh-knowledge/rules_test.go @@ -97,16 +97,45 @@ func TestEveryPerRecordRuleRefusesAProposal(t *testing.T) { p.Date, p.NeedsOperator = "2026-10-13", true }}, {"insight-links", func(p *ProposeAsk, w *world, t *testing.T) { - // 466/AC4: an insight that drops the changes link of the accepted record is refused, and one that adds a + // 466/AC4: an accepted record holding a supersedes and a changes link; an insight that drops the + // supersedes or adds a changes is refused, each link named and a new record asked for; one that adds a // cites link is taken. - w.accepted(t, "author@test", valid(p.Number)) - p.Insight = "it said the clause stood" - cites := *p - cites.Links = append(append([]LinkAsk(nil), p.Links...), LinkAsk{Kind: LinkCites, To: "0011"}) + base := *p + base.Links = append(append([]LinkAsk(nil), p.Links...), LinkAsk{Kind: LinkSupersedes, To: "0162"}) + w.accepted(t, "author@test", base) + insight := func(links ...LinkAsk) ProposeAsk { + q := base + q.Insight = "it said the clause stood" + q.Links = links + return q + } + kept := base.Links + cites := insight(append(append([]LinkAsk(nil), kept...), LinkAsk{Kind: LinkCites, To: "0011"})...) if _, err := w.k.Propose("author@test", cites); err != nil { t.Fatalf("an insight adding a cites link was refused: %v", err) } - p.Links = p.Links[:1] + dropped := insight(kept[:2]...) + added := insight(append(append([]LinkAsk(nil), kept...), LinkAsk{Kind: LinkChanges, To: "0240 §1", From: 2})...) + for _, c := range []struct { + name string + ask ProposeAsk + says []string + not string + }{ + {"dropping the supersedes", dropped, []string{"dropped supersedes ADR 0162", "write one"}, "added"}, + {"adding a changes", added, []string{"added §2 changes ADR 0240 §1", "write one"}, "dropped"}, + } { + _, err := w.k.Propose("author@test", c.ask) + if err == nil || !strings.Contains(err.Error(), "- insight-links:") || strings.Contains(err.Error(), c.not) { + t.Fatalf("%s: %v", c.name, err) + } + for _, says := range c.says { + if !strings.Contains(err.Error(), says) { + t.Fatalf("%s: the refusal does not say %q: %v", c.name, says, err) + } + } + } + *p = dropped }}, {"section-form", func(p *ProposeAsk, w *world, t *testing.T) { p.Date, p.Sections[0].Name = "2026-11-04", "00-context.md" From 2799ba177c2038e8f0d25c8f6da12773e3b99250 Mon Sep 17 00:00:00 2001 From: jochen Date: Wed, 25 Nov 2026 17:13:15 +0100 Subject: [PATCH 6/7] 466-c review: restore only what a version made by propose dropped; the answer counts the versions written MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Builds: design 55 §1.1, §10; lead answers L-466-4, L-466-5 (issue 466 note 18), note 20 items (a), (b); 466/AC5, AC7 --- .../cmd/mesh-knowledge/manifest_test.go | 4 + .../cmd/mesh-knowledge/restore.go | 201 +++++++++++------- .../cmd/mesh-knowledge/restore_test.go | 161 ++++++++++---- .../cmd/mesh-knowledge/tools.go | 8 +- 4 files changed, 260 insertions(+), 114 deletions(-) diff --git a/modules/mesh-knowledge/cmd/mesh-knowledge/manifest_test.go b/modules/mesh-knowledge/cmd/mesh-knowledge/manifest_test.go index 3a7852b72..aee5c5c4c 100644 --- a/modules/mesh-knowledge/cmd/mesh-knowledge/manifest_test.go +++ b/modules/mesh-knowledge/cmd/mesh-knowledge/manifest_test.go @@ -131,6 +131,10 @@ func TestItsProcessesAreThePreparationTheActAndTheJudge(t *testing.T) { t.Fatalf("keep %v", keep) } h, _ := watch["health"].(map[string]any) + // 466/AC7: the manifest names knowledge_restore and ModuleTools holds it, in the manifest's order. + if !contains(ModuleTools, RestoreTool) || !contains(m.Tools, RestoreTool) { + t.Fatalf("knowledge_restore is not in the manifest and ModuleTools: %v %v", m.Tools, ModuleTools) + } if watch == nil || watch["run"].([]any)[1] != "watch" || h["kind"] != "tool" || h["tool"] != HealthTool || !reflect.DeepEqual(m.Tools, ModuleTools) { t.Fatalf("watch %v, tools %v", watch, m.Tools) diff --git a/modules/mesh-knowledge/cmd/mesh-knowledge/restore.go b/modules/mesh-knowledge/cmd/mesh-knowledge/restore.go index 48ec0d1e2..baf244304 100644 --- a/modules/mesh-knowledge/cmd/mesh-knowledge/restore.go +++ b/modules/mesh-knowledge/cmd/mesh-knowledge/restore.go @@ -9,7 +9,6 @@ import ( "context" "encoding/json" "fmt" - "reflect" "sort" "strings" ) @@ -27,10 +26,86 @@ type RestoredRecord struct { 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) { +// restoreField is one field of a record that propose does not take: how to tell it is held, and how to copy it. +type restoreField struct { + name string + has func(*Record) bool + copy func(dst, src *Record) + value func(*Record) any + clear func(*Record) +} + +var restoreFields = []restoreField{ + {"reconstructed", func(r *Record) bool { return r.Reconstructed != nil }, func(d, s *Record) { v := *s.Reconstructed; d.Reconstructed = &v }, func(r *Record) any { return r.Reconstructed }, func(r *Record) { r.Reconstructed = nil }}, + {"amended", func(r *Record) bool { return len(r.Amended) > 0 }, func(d, s *Record) { d.Amended = append([]string(nil), s.Amended...) }, func(r *Record) any { return r.Amended }, func(r *Record) { r.Amended = nil }}, + {"touches", func(r *Record) bool { return len(r.Touches) > 0 }, func(d, s *Record) { d.Touches = append([]string(nil), s.Touches...) }, func(r *Record) any { return r.Touches }, func(r *Record) { r.Touches = nil }}, + {"the imported fields", func(r *Record) bool { return len(r.Extra) > 0 }, func(d, s *Record) { + d.Extra = map[string]string{} + for k, v := range s.Extra { + d.Extra[k] = v + } + }, func(r *Record) any { return r.Extra }, func(r *Record) { r.Extra = nil }}, + {"source", func(r *Record) bool { return r.Source != "" }, func(d, s *Record) { d.Source = s.Source }, func(r *Record) any { return r.Source }, func(r *Record) { r.Source = "" }}, + {"aliases", func(r *Record) bool { return len(r.Aliases) > 0 }, func(d, s *Record) { + d.Aliases = map[string]string{} + for k, v := range s.Aliases { + d.Aliases[k] = v + } + }, func(r *Record) any { return r.Aliases }, func(r *Record) { r.Aliases = nil }}, +} + +// restoreStep is a field put back and the index of the accepted version it comes from. +type restoreStep struct { + field restoreField + from int +} + +// 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 +// 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) { + insights, fields = map[string]int{}, map[string]restoreStep{} + prev := -1 + for i, v := range versions { + 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 { + before, now := versions[prev].Record, v.Record + have := map[string]bool{} + for _, in := range now.Insights { + have[insightKey(in)] = true + } + for _, in := range before.Insights { + if !have[insightKey(in)] { + insights[insightKey(in)] = prev + } + } + for _, f := range restoreFields { + if f.has(before) && !f.has(now) { + fields[f.name] = restoreStep{f, prev} + } + } + } + prev = i + } + return insights, fields +} + +func versionNumber(versions []Version, i int) int { + if versions[i].N != 0 { + return versions[i].N + } + return i + 1 +} + +// restoredOf is the served record with what a version made by propose dropped 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, map[string]int) { r := served.Clone() + dropped, fields := droppedByPropose(versions) held := map[string]bool{} for _, in := range served.Insights { held[insightKey(in)] = true @@ -41,80 +116,34 @@ func restoredOf(served *Record, versions []Version) (*Record, []string) { 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)] { + if from, ok := dropped[insightKey(in)]; ok && from == i && !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)) + said = append(said, fmt.Sprintf("insight %s by %s (version %d)", in.Date, in.By, versionNumber(versions, i))) } } } - // 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)) + from := map[string]int{} + for _, f := range restoreFields { + if st, ok := fields[f.name]; ok && !f.has(served) { + f.copy(r, versions[st.from].Record) + from[f.name] = st.from + said = append(said, fmt.Sprintf("%s (version %d)", f.name, versionNumber(versions, st.from))) } } if len(said) == 0 { - return nil, nil + return nil, 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 + return r, said, from } -// 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 - } - } - } +// checkRestore is the restoring version's own check: every served insight is still there, each insight added is held +// by the accepted version it was taken from, each field put back is the value of that version and was empty in the +// served record, and no other field of the served record differs. +func checkRestore(served, next *Record, versions []Version, fields map[string]int) error { have := map[string]bool{} for _, in := range next.Insights { have[insightKey(in)] = true @@ -124,22 +153,48 @@ func checkRestore(served, next *Record, versions []Version) error { return fmt.Errorf("the insight of %s is missing from the restored version", in.Date) } } + dropped, _ := droppedByPropose(versions) + serves := map[string]bool{} for _, in := range served.Insights { - held[insightKey(in)] = true + serves[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) + from, ok := dropped[insightKey(in)] + if serves[insightKey(in)] { + continue + } + if !ok { + return fmt.Errorf("the insight of %s was dropped by no version made by propose", in.Date) + } + held := false + for _, x := range versions[from].Record.Insights { + held = held || insightKey(x) == insightKey(in) + } + if !held { + return fmt.Errorf("the insight of %s is not held by version %d", in.Date, versionNumber(versions, from)) } } 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 + a.Insights, b.Insights = nil, nil + for _, f := range restoreFields { + i, put := fields[f.name] + if !put { + continue + } + if f.has(served) || !f.has(next) { + return fmt.Errorf("%s was not empty in the served record, or was not put back", f.name) + } + got, _ := json.Marshal(f.value(next)) + want, _ := json.Marshal(f.value(versions[i].Record)) + if string(got) != string(want) { + return fmt.Errorf("%s is not the value version %d held", f.name, versionNumber(versions, i)) + } + f.clear(b) } ja, _ := json.Marshal(a) jb, _ := json.Marshal(b) - if !reflect.DeepEqual(ja, jb) { - return fmt.Errorf("something other than insights and fields differs") + if string(ja) != string(jb) { + return fmt.Errorf("a field of the served record other than the insights and the fields put back differs") } return nil } @@ -168,7 +223,7 @@ func (k *KB) Restore(only *Key, kind string, dry bool) ([]RestoredRecord, error) if err != nil { return nil, fmt.Errorf("%s: its versions cannot be read: %v", key, err) } - next, said := restoredOf(e.Current, versions) + next, said, fromFields := restoredOf(e.Current, versions) if next == nil { continue } @@ -177,7 +232,7 @@ func (k *KB) Restore(only *Key, kind string, dry bool) ([]RestoredRecord, error) 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 { + if err := checkRestore(e.Current, next, versions, fromFields); err != nil { rr.Skipped = "the check refused it: " + err.Error() } else if !dry { why := "restored what a version dropped: " + strings.Join(said, "; ") diff --git a/modules/mesh-knowledge/cmd/mesh-knowledge/restore_test.go b/modules/mesh-knowledge/cmd/mesh-knowledge/restore_test.go index 406e78f18..b3c9b670e 100644 --- a/modules/mesh-knowledge/cmd/mesh-knowledge/restore_test.go +++ b/modules/mesh-knowledge/cmd/mesh-knowledge/restore_test.go @@ -2,62 +2,143 @@ package main import ( "context" + "fmt" "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. +// 466/AC5: a record that lost an insight and a field an earlier accepted version held, in a version made by propose +// (a pending version, then an accepted one marked insight, as the old accept wrote it), gets them back in one version by +// the knowledge base; a dry run lists each entry and writes nothing; a second run writes nothing; what a refresh removed +// is not restored; a record with a version waiting for its review is skipped, saying so. 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) + // accepted is a record accepted with reconstructed: false, then given one insight by the knowledge base as it + // is now; it answers its key and the number of the version that holds them. + accepted := func(t *testing.T) (Key, int) { + n := w.reserved(t, "author@g14") + p := valid(n) + w.accepted(t, "author@g14", p) + key := Key{KindDecision, n} + 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) + return key, mustGet(t, s, key).Versions } - if _, err := callTool(t, tools, RestoreTool, map[string]any{"number": float64(n), "kind": "decision"}); err != nil { - t.Fatal(err) + // 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) { + 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.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) + 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 { + 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) + run := func(t *testing.T, key Key, args map[string]any) map[string]any { + args["kind"], args["number"] = key.Kind, float64(key.Number) + out, err := callTool(t, tools, RestoreTool, args) + if err != nil { + t.Fatal(err) + } + return out } - 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) + records := func(out map[string]any) []map[string]any { + var got []map[string]any + for _, r := range out["records"].([]any) { + got = append(got, r.(map[string]any)) + } + 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) + } + }) + + t.Run("what a refresh removed is not restored", func(t *testing.T) { + key, _ := accepted(t) + e := mustGet(t, s, key) + refreshed := e.Current.Clone() + refreshed.Reconstructed = nil + if err := s.Save(ctx, Version{By: "import@test", At: time.Now(), Why: "refreshed", Record: refreshed}, true, false, e.Revision); err != nil { + t.Fatal(err) + } + before := mustGet(t, s, key).Versions + out := run(t, key, map[string]any{}) + if e := mustGet(t, s, key); len(records(out)) != 0 || e.Versions != before || e.Current.Reconstructed != nil { + t.Fatalf("a field a refresh removed was put back: %v", out) + } + }) + + t.Run("a record with a version waiting is skipped, and says so", func(t *testing.T) { + key, _ := accepted(t) + lose(t, key) + p := insightTo(t, w, key) + p.Insight = "it said nothing of the day" + if _, err := w.k.Propose("author@g14", p); err != nil { + t.Fatal(err) + } + before := mustGet(t, s, key).Versions + out := run(t, key, map[string]any{}) + recs := records(out) + if len(recs) != 1 || !strings.Contains(fmt.Sprint(recs[0]["skipped"]), "waits for its review") || + out["written"] != false || mustGet(t, s, key).Versions != before { + t.Fatalf("a record with a version waiting: %v", out) + } + }) }) } + +func toAny(list []string) []any { + out := make([]any, len(list)) + for i, s := range list { + out[i] = s + } + return out +} diff --git a/modules/mesh-knowledge/cmd/mesh-knowledge/tools.go b/modules/mesh-knowledge/cmd/mesh-knowledge/tools.go index d19599ac1..ac5b7ef34 100644 --- a/modules/mesh-knowledge/cmd/mesh-knowledge/tools.go +++ b/modules/mesh-knowledge/cmd/mesh-knowledge/tools.go @@ -472,7 +472,13 @@ func moduleTools(k *KB, caller func(map[string]any) string) []stdio.Tool { if err != nil { return nil, err } - return map[string]any{"records": done, "written": !dry}, nil + wrote := 0 + for _, d := range done { + if d.Version > 0 { + wrote++ + } + } + return map[string]any{"records": done, "versions-written": wrote, "written": wrote > 0}, nil }}, } } From 141cddf7c9eb54d301c93db5c26a84f2f3201999 Mon Sep 17 00:00:00 2001 From: jochen Date: Wed, 25 Nov 2026 17:22:06 +0100 Subject: [PATCH 7/7] 466-c review: made by propose is read from the accepted version's why, compared with the last accepted version MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Builds: design 55 §1.1; lead answer L-466-4 (issue 466 note 18); 466/AC5 --- .../cmd/mesh-knowledge/restore.go | 15 +++- .../cmd/mesh-knowledge/restore_test.go | 80 +++++++++++-------- .../cmd/mesh-knowledge/tools.go | 8 +- 3 files changed, 63 insertions(+), 40 deletions(-) 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")