From 085c7211d8b48403a29008afc444a81a7e512684 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 26 Nov 2026 13:05:15 +0000 Subject: [PATCH 1/2] docs(chart): ForestDiff reports a duplicate reachable NodeKey Implements: 01a217d4-9769-7de1-8c71-7051564dde33 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01YDSkrXBzd3pd6nDkewof8N --- internal/chart/README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/internal/chart/README.md b/internal/chart/README.md index c2127b5bd..75f36312b 100644 --- a/internal/chart/README.md +++ b/internal/chart/README.md @@ -2751,7 +2751,9 @@ grammar's tables and its precedence: no Earley item gains a fact. refuse, the set of nodes reachable from the root, and each reachable node's kept alternatives in order. Nodes are matched by what they derive (symbol, rule position and span), never by index, so a renumbered forest - compares equal and a node unreachable from the root is not compared. The + compares equal and a node unreachable from the root is not compared. Two + distinct reachable nodes sharing a key are a difference + (`TestForestDiffDuplicateKey`), never one overwriting the other. The V1 side (`V1Forest`, `V1ToyForest`) is the old pipeline: the recognizer `V1Recognizer` builds, which is only ever the unjudged one, then `Build`, `LibraryOperators` (for a source) and `Constrain`. The candidate From 4f6b4043e8206752f0093b9caaa16d2a263a99ae Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 26 Nov 2026 13:15:16 +0000 Subject: [PATCH 2/2] Make ForestDiff report a duplicate reachable NodeKey rather than overwrite it Implements: 01a217d4-9769-7de1-8c71-7051564dde33 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01YDSkrXBzd3pd6nDkewof8N --- internal/chart/forest/equality.go | 23 ++++++++++++++++++----- internal/chart/forest/equality_test.go | 22 ++++++++++++++++++++++ 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/internal/chart/forest/equality.go b/internal/chart/forest/equality.go index b0176dcbe..36989c667 100644 --- a/internal/chart/forest/equality.go +++ b/internal/chart/forest/equality.go @@ -39,7 +39,14 @@ func ForestDiff(cand, v1 *Forest) string { case v1 == nil: return "the candidate accepts, V1 refuses" } - c, v := reachableAlts(cand), reachableAlts(v1) + c, cdup := reachableAlts(cand) + v, vdup := reachableAlts(v1) + if cdup != nil { + return fmt.Sprintf("node %+v is reachable twice in the candidate", *cdup) + } + if vdup != nil { + return fmt.Sprintf("node %+v is reachable twice in V1", *vdup) + } keys := slices.SortedFunc(maps.Keys(c), cmpNodeKey) for _, k := range slices.SortedFunc(maps.Keys(v), cmpNodeKey) { if _, ok := c[k]; !ok { @@ -59,8 +66,9 @@ func ForestDiff(cand, v1 *Forest) string { return "" } -// reachableAlts is each node reachable from f's root, by key, with its kept alternatives in order. -func reachableAlts(f *Forest) map[NodeKey][]altKey { +// reachableAlts is each node reachable from f's root, by key, with its kept alternatives in order, and the first +// key, in cmpNodeKey order, that two distinct reachable nodes share (nil if none): a map by key would keep only one. +func reachableAlts(f *Forest) (map[NodeKey][]altKey, *NodeKey) { key := func(n int32) NodeKey { if n == 0 { return noNode @@ -69,6 +77,7 @@ func reachableAlts(f *Forest) map[NodeKey][]altKey { return NodeKey{nd.Sym, nd.Rule, nd.Dot, nd.From, nd.To} } out := map[NodeKey][]altKey{} + var dup *NodeKey seen := make([]bool, len(f.Nodes)) work := []int32{f.Root} for len(work) > 0 { @@ -88,9 +97,13 @@ func reachableAlts(f *Forest) map[NodeKey][]altKey { } } } - out[key(n)] = alts + k := key(n) + if _, ok := out[k]; ok && (dup == nil || cmpNodeKey(k, *dup) < 0) { + dup = &k + } + out[k] = alts } - return out + return out, dup } func cmpNodeKey(x, y NodeKey) int { diff --git a/internal/chart/forest/equality_test.go b/internal/chart/forest/equality_test.go index 26beccf77..8bed3fe9a 100644 --- a/internal/chart/forest/equality_test.go +++ b/internal/chart/forest/equality_test.go @@ -659,3 +659,25 @@ func TestForestEqualityUnionShortcut(t *testing.T) { t.Errorf("%q: %s", src, d) } } + +// TestForestDiffDuplicateKey: two distinct reachable nodes sharing a NodeKey are reported as a difference naming the +// key, whichever side holds them, rather than the second silently overwriting the first in the comparison. +func TestForestDiffDuplicateKey(t *testing.T) { + // The root's one alternative has two children, nodes 2 and 3, both symbol 7 over 0..1. + dup := &Forest{ + Root: 1, + Nodes: []Node{{}, {Sym: 5, From: 0, To: 2, Alt: 1}, {Sym: 7, From: 0, To: 1}, {Sym: 7, From: 0, To: 1}}, + Alts: []Alt{{}, {Rule: 1, Left: 2, Right: 3}}, + } + clean := dup.Copy() + clean.Alts[1].Right = 2 + want := fmt.Sprintf("%+v", NodeKey{Sym: 7, Rule: 0, Dot: 0, From: 0, To: 1}) + for name, pair := range map[string][2]*Forest{"candidate": {dup, clean}, "v1": {clean, dup}} { + if d := ForestDiff(pair[0], pair[1]); !strings.Contains(d, "twice") || !strings.Contains(d, want) { + t.Errorf("%s holds the duplicate: ForestDiff = %q, want a report naming %s", name, d, want) + } + } + if d := ForestDiff(clean, clean.Copy()); d != "" { + t.Errorf("no duplicate: ForestDiff = %q, want none", d) + } +}