Tighten who writes and reviews a record, date the rules, bound history (review of #206)
mesh/merge-gate pass: builds new: modules/mesh-knowledge, sent nowhere; no bus step; every machine composes with the change as it did without (4 of 4 compo…
mesh/repo-check pass: its merge-check.sh passed, each part in its toolchain: merge-check.sh (go), merge-check-typescript.sh (typescript)
mesh/delivery superseded: a newer head of the same pull request

A revision came from anyone and a review was refused only to the first author,
so a co-writer could approve, and a record proposed before callers are proven
would later be reviewable by its own unproven author. Now a revision is taken
only from the proven author, a reserved number only from whoever reserved it,
and a review is refused from any writer of any version or for any record with
an unproven writer. The rules held records from no date, so a correction to an
older record was refused by rules hq never held it to; each dated rule now holds
from hq's own start date, and a new record cannot be dated before its
reservation. history and the question to ask are bounded like the other answers.
This commit is contained in:
2026-10-11 05:10:02 +02:00
parent ba7b455719
commit 10ab959d0f
10 changed files with 382 additions and 33 deletions
@@ -13,6 +13,9 @@ import (
"strings"
)
// MostQuestion is the most characters a question to ask may hold.
const MostQuestion = 500
// The bound of an answer.
const (
DefaultBound = 4 << 10
@@ -159,6 +162,9 @@ func (k *KB) Ask(question string, bound int) (*Asked, error) {
if question == "" {
return nil, fmt.Errorf("ask needs a question, in plain words")
}
if len([]rune(question)) > MostQuestion {
return nil, fmt.Errorf("a question is at most %d characters; this one is %d: ask it in fewer words", MostQuestion, len([]rune(question)))
}
bound = boundOf(bound)
hits, err := k.Store.Search(ctx, question, 80)
if err != nil {
@@ -600,10 +606,12 @@ type Historied struct {
To int `json:"to,omitempty"`
Diff []string `json:"diff,omitempty"`
Same bool `json:"same,omitempty"`
Cut string `json:"cut,omitempty"`
Bound int `json:"bound,omitempty"`
}
// History is a record's versions, and the difference between two of them, line by line.
func (k *KB) History(key Key, from, to int) (*Historied, error) {
func (k *KB) History(key Key, from, to, bound int) (*Historied, error) {
ctx := context.Background()
e, err := k.Store.Get(ctx, key)
if err != nil {
@@ -622,22 +630,72 @@ func (k *KB) History(key Key, from, to int) (*Historied, error) {
Status: v.Record.Status, Insight: v.Insight, Served: v.N == e.CurrentVersion, Pending: v.N == e.PendingVersion})
}
if from == 0 && to == 0 {
out.Bound = boundOf(bound)
versions := out.Versions
v, m := fit(len(versions), out.Bound-300, func(m int) any { c := *out; c.Versions = versions[len(versions)-m:]; return &c })
*out = *(v.(*Historied))
if m < len(versions) {
out.Cut = fmt.Sprintf("the %d oldest versions are not listed within %d bytes: raise bound", len(versions)-m, out.Bound)
}
return out, nil
}
if from < 1 || to < 1 || from > len(vs) || to > len(vs) {
return nil, fmt.Errorf("%s has versions 1 to %d; from and to name two of them", key, len(vs))
}
out.From, out.To = from, to
out.Diff = Diff(Render(vs[from-1].Record), Render(vs[to-1].Record))
out.Same = len(out.Diff) == 0
out.From, out.To, out.Bound = from, to, boundOf(bound)
diff := Diff(Render(vs[from-1].Record), Render(vs[to-1].Record))
out.Same = len(diff) == 0
// Within the bound: the most of the difference's lines that fit, cut at a hunk's line and said; and when the
// versions alone do not fit, the oldest of them left out first.
versions := out.Versions
v, n := fit(len(diff), out.Bound-300, func(n int) any { c := *out; c.Diff = diff[:n]; return &c })
*out = *(v.(*Historied))
var cut []string
if n < len(diff) {
cut = append(cut, fmt.Sprintf("%d of the difference's %d lines are not shown within %d bytes: raise bound, or compare nearer versions",
len(diff)-n, len(diff), out.Bound))
}
if size(out) > out.Bound-300 {
v, m := fit(len(versions), out.Bound-300, func(m int) any { c := *out; c.Versions = versions[len(versions)-m:]; return &c })
*out = *(v.(*Historied))
cut = append(cut, fmt.Sprintf("the %d oldest versions are not listed", len(versions)-m))
}
out.Cut = strings.Join(cut, "; ")
return out, nil
}
// MostDiffWork bounds the work of a difference: the lines of the two texts' changed middles multiplied. Past it,
// the middle is given whole as removed and added — still exact, only less narrow.
const MostDiffWork = 4_000_000
// Diff is the difference of two texts, line by line: each hunk headed "@@ -a,n +b,m @@", its lines "-" removed
// and "+" added, by the longest common subsequence of their lines.
func Diff(a, b string) []string {
x, y := strings.Split(a, "\n"), strings.Split(b, "\n")
// The common head and tail cost nothing to find; only the middle between them is compared.
head := 0
for head < len(x) && head < len(y) && x[head] == y[head] {
head++
}
tail := 0
for tail < len(x)-head && tail < len(y)-head && x[len(x)-1-tail] == y[len(y)-1-tail] {
tail++
}
x, y = x[head:len(x)-tail], y[head:len(y)-tail]
n, m := len(x), len(y)
if n*m > MostDiffWork {
var out []string
if n+m > 0 {
out = append(out, fmt.Sprintf("@@ -%d,%d +%d,%d @@", head+1, n, head+1, m))
}
for _, l := range x {
out = append(out, "-"+l)
}
for _, l := range y {
out = append(out, "+"+l)
}
return out
}
lcs := make([][]int, n+1)
for i := range lcs {
lcs[i] = make([]int, m+1)
@@ -670,14 +728,14 @@ func Diff(a, b string) []string {
j++
case i < n && (j == m || lcs[i+1][j] >= lcs[i][j+1]):
if len(hunk) == 0 {
ha, hb, ca, cb = i+1, j+1, 0, 0
ha, hb, ca, cb = head+i+1, head+j+1, 0, 0
}
hunk = append(hunk, "-"+x[i])
ca++
i++
default:
if len(hunk) == 0 {
ha, hb, ca, cb = i+1, j+1, 0, 0
ha, hb, ca, cb = head+i+1, head+j+1, 0, 0
}
hunk = append(hunk, "+"+y[j])
cb++