Distinguish metadata slice views from cycles

This commit is contained in:
2026-11-26 01:30:49 +00:00
parent b6f04297d4
commit 97bcf8cae6
4 changed files with 78 additions and 3 deletions

View File

@@ -20,8 +20,8 @@ The [Core And Integration API Roadmap](../core-integration-api.md) established
the core/integration boundary. The reassessment below distinguishes its completed
work from the remaining audit remediation.
The **45 original finding entries** reduce to **26 outstanding catalog entries
and eight resolved entries**. Repeated responsibility findings are
The **45 original finding entries** reduce to **25 outstanding catalog entries
and nine resolved entries**. Repeated responsibility findings are
merged. Regression gaps are attached to the defects they protect rather than
counted as additional runtime bugs. In particular, TEST-001, TEST-002, and
TEST-004 each cover several distinct defects; TEST-003's native-fixture oracle
@@ -65,7 +65,8 @@ Original P2/P3 ratings and relative refactoring priorities are retained for
context; they are not one universal severity scale. No audit establishes a P1
incident or a measured end-to-end production speedup.
CAT-001, CAT-002, CAT-007, CAT-011, CAT-012, CAT-015, CAT-016, and CAT-034 are resolved;
CAT-001, CAT-002, CAT-007, CAT-011, CAT-012, CAT-013, CAT-015, CAT-016, and
CAT-034 are resolved;
the other entries remain outstanding. The original reports and their baseline
observations are historical.
"Refactoring" denotes a maintenance opportunity, not a confirmed runtime
@@ -289,6 +290,9 @@ was demonstrated.
**Assessment:** Static correctness counterexample, not executed; original P3.
**Status:** Resolved. Slice view length participates in active recursion identity;
tests cover finite shared views, detached copies, and genuine cycles.
**Sources:** [BUG-002](03-complexity.md#bug-002--cycle-detection-confuses-a-slice-view-with-a-cycle),
[TEST-002](08-testing.md#test-002--identity-and-metadata-fixtures-omit-valid-representational-edge-cases).

View File

@@ -215,6 +215,12 @@ No public grammar/documentation change.
## Stage 4 — Distinguish Finite Slice Views From Cycles
**Completed.** Metadata recursion tracks each nonempty slice's visible length
with its type and pointer, and skips cycle tracking for empty views. Regressions
reproduced the shared-prefix failure and cover detached sharing, nil/empty views,
and actual map and slice cycles; existing tests cover concrete containers. Both
specified package tests pass. No follow-up remains.
**Coverage:** CAT-013. **Prerequisites:** none.
In source `metadata.go`, include visible length in active slice identity with

View File

@@ -24,6 +24,7 @@ func CloneMetadata(metadata map[string]any) (map[string]any, error) {
type metadataVisit struct {
typ reflect.Type
ptr uintptr
len int
}
func cloneMetadataValue(value reflect.Value, location string, active map[metadataVisit]struct{}) (reflect.Value, error) {
@@ -71,6 +72,9 @@ func cloneMetadataValue(value reflect.Value, location string, active map[metadat
if value.IsNil() {
return reflect.Zero(value.Type()), nil
}
if value.Len() == 0 {
return reflect.MakeSlice(value.Type(), 0, 0), nil
}
leave, err := enterMetadataValue(value, active, location)
if err != nil {
return reflect.Value{}, err
@@ -111,6 +115,9 @@ func cloneMetadataValue(value reflect.Value, location string, active map[metadat
func enterMetadataValue(value reflect.Value, active map[metadataVisit]struct{}, location string) (func(), error) {
visit := metadataVisit{typ: value.Type(), ptr: value.Pointer()}
if value.Kind() == reflect.Slice {
visit.len = value.Len()
}
if _, exists := active[visit]; exists {
return nil, fmt.Errorf("%s contains a cycle", location)
}

View File

@@ -0,0 +1,58 @@
package source
import (
"reflect"
"strings"
"testing"
)
func TestCloneMetadataSharedSliceViews(t *testing.T) {
values := []any{"one", nil}
values[1] = values[:1]
shared := []string{"original"}
var nilSlice []string
input := map[string]any{"values": values, "first": shared, "second": shared, "nil": nilSlice, "empty": []string{}}
cloned, err := CloneMetadata(input)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(cloned, input) {
t.Fatalf("clone = %#v, want %#v", cloned, input)
}
outer := cloned["values"].([]any)
outer[1].([]any)[0] = "changed"
cloned["first"].([]string)[0] = "changed"
if values[0] != "one" || outer[0] != "one" || shared[0] != "original" || cloned["second"].([]string)[0] != "original" {
t.Fatal("cloned slice views share mutable elements")
}
if cloned["nil"].([]string) != nil || cloned["empty"].([]string) == nil {
t.Fatal("clone did not preserve nil and empty slices")
}
values[1] = values[:0]
cloned, err = CloneMetadata(map[string]any{"values": values})
if err != nil || cloned["values"].([]any)[1].([]any) == nil {
t.Fatalf("empty shared view clone = %#v, error = %v", cloned, err)
}
}
func TestCloneMetadataRejectsActualCycles(t *testing.T) {
slice := make([]any, 1)
slice[0] = slice
object := map[string]any{}
object["self"] = object
for _, tc := range []struct {
name string
value any
path string
}{
{"slice", slice, "metadata.value[0]"},
{"map", object, "metadata.value.self"},
} {
t.Run(tc.name, func(t *testing.T) {
_, err := CloneMetadata(map[string]any{"value": tc.value})
if err == nil || !strings.Contains(err.Error(), tc.path+" contains a cycle") {
t.Fatalf("CloneMetadata() error = %v, want cycle at %s", err, tc.path)
}
})
}
}