From 75e209eb3becb217d07d6b37fbb9eb33b73a0ea1 Mon Sep 17 00:00:00 2001 From: Eric Rakestraw Date: Sun, 25 Oct 2026 23:13:00 +0000 Subject: [PATCH] Index tier history sources lazily --- docs/roadmap/implementation.md | 7 +++++ internal/engine/history_sequence_test.go | 27 ++++++++++--------- .../engine/sequence_history_projection.go | 6 ++++- 3 files changed, 26 insertions(+), 14 deletions(-) diff --git a/docs/roadmap/implementation.md b/docs/roadmap/implementation.md index 420e0f0..7ec55ec 100644 --- a/docs/roadmap/implementation.md +++ b/docs/roadmap/implementation.md @@ -1006,6 +1006,13 @@ mapping for each of S contributions. Use existing wide/sparse fixtures, not timi thresholds or another explanation-replay implementation. Focused check: `go test ./internal/engine -run 'Tier|Projection|Sequence|History'`. +Status: **complete** (E04). Prior completed commits include `3a1b6ff`, +`d172a5e`, and `1dd94a3`. Validation passed: +`env GOTOOLCHAIN=local GOPROXY=off GOTELEMETRY=off go test ./internal/engine -run 'Tier|Projection|Sequence|History' -count=1`, +`env GOTOOLCHAIN=local GOPROXY=off GOTELEMETRY=off go test ./internal/engine -count=1`, +`env GOTOOLCHAIN=local GOPROXY=off GOTELEMETRY=off go test ./... -count=1`, +and `git diff --check`. + ## Stage 31 — Index Compound Identity Restoration Finding: **E05**. diff --git a/internal/engine/history_sequence_test.go b/internal/engine/history_sequence_test.go index cb32b8e..a4d77b3 100644 --- a/internal/engine/history_sequence_test.go +++ b/internal/engine/history_sequence_test.go @@ -11,17 +11,18 @@ import ( func TestSequenceHistoryReplaysAppendAtAssignedIndexes(t *testing.T) { policy := mustCompileSequenceIdentityPolicy(t, []sequenceIdentityRegistration{ - {selector: sequenceIdentitySelector{literalSequenceSelector("records")}, identityFields: []string{"id"}}, - {selector: sequenceIdentitySelector{literalSequenceSelector("other")}, identityFields: []string{"id"}}, + {selector: sequenceIdentitySelector{ + literalSequenceSelector("collections"), anyMappingSequenceSelector(), literalSequenceSelector("records"), + }, identityFields: []string{"id"}}, }) - first := parseCompositionDocument(t, "source-1", "records: [{id: a, value: first}]\nother: [{id: x}]\n") - second := parseCompositionDocument(t, "source-2", "records: [{id: b, value: second}]\nother: [{id: y}]\n") + first := parseCompositionDocument(t, "source-1", "collections: {north: {records: [{id: a, value: first}]}, south: {records: [{id: x}]}}\n") + second := parseCompositionDocument(t, "source-2", "collections: {north: {records: [{id: b, value: second}]}, south: {records: [{id: y}]}}\n") composed, diagnostics := composeDocumentsWithPolicy(policy, first, second) if len(diagnostics) != 0 { t.Fatalf("compose diagnostics = %#v", diagnostics) } for applicationIndex, application := range composed.applications { - for path, want := range map[string]int{"/records": 1, "/other": 1} { + for path, want := range map[string]int{"/collections/north/records": 1, "/collections/south/records": 1} { sequence, present := documentAtPath(application.value, mustParsePath(t, path)) if !present || sequence.Kind != document.Sequence || len(sequence.Sequence) != want { t.Errorf("application %d retained %s = %#v, %v; want one source fragment item", applicationIndex, path, sequence, present) @@ -29,14 +30,14 @@ func TestSequenceHistoryReplaysAppendAtAssignedIndexes(t *testing.T) { } } - working := parseCompositionDocument(t, "defaults", "records: [{id: default}]\nother: [{id: default}]\n") + working := parseCompositionDocument(t, "defaults", "collections: {north: {records: [{id: default}]}, south: {records: [{id: default}]}}\n") index := new(mappingIndex) for _, application := range composed.applications { applyHistoryApplication(&working, application, index) } for path, want := range map[string][]string{ - "/records": {"a", "b"}, - "/other": {"x", "y"}, + "/collections/north/records": {"a", "b"}, + "/collections/south/records": {"x", "y"}, } { sequence, present := documentAtPath(working, mustParsePath(t, path)) if !present || sequence.Kind != document.Sequence || len(sequence.Sequence) != len(want) { @@ -53,14 +54,14 @@ func TestSequenceHistoryReplaysAppendAtAssignedIndexes(t *testing.T) { application := composed.applications[1] cloned := cloneHistoryApplications(composed.applications) cloned[1].sequences[0].path.segments[0].key = "mutated" - if composed.applications[1].sequences[0].path.String() != "/records" { + if composed.applications[1].sequences[0].path.String() != "/collections/north/records" { t.Fatal("cloned sequence history path aliases its source") } - addedPath := Keys("records").Index(1).Key("value") + addedPath := Keys("collections", "north", "records").Index(1).Key("value") if operation, relevant := applicationOperationPath(application, addedPath, nil); !relevant || operation.String() != addedPath.String() { t.Fatalf("appended value operation = %s, %v", operation.String(), relevant) } - if _, relevant := applicationOperationPath(application, Keys("records").Index(0).Key("value"), nil); relevant { + if _, relevant := applicationOperationPath(application, Keys("collections", "north", "records").Index(0).Key("value"), nil); relevant { t.Fatal("later contribution was reported as changing an earlier record") } origin := historyApplicationOrigin(application, addedPath, nil) @@ -69,10 +70,10 @@ func TestSequenceHistoryReplaysAppendAtAssignedIndexes(t *testing.T) { } overlay := historyApplication{ layer: LayerOverlay, - value: parseCompositionDocument(t, "overlay", "records: []\n"), + value: parseCompositionDocument(t, "overlay", "collections: {north: {records: []}}\n"), } applyHistoryApplication(&working, overlay, index) - replaced, present := documentAtPath(working, Keys("records")) + replaced, present := documentAtPath(working, Keys("collections", "north", "records")) if !present || replaced.Kind != document.Sequence || len(replaced.Sequence) != 0 { t.Fatalf("ordinary later list replacement = %#v, %v", replaced, present) } diff --git a/internal/engine/sequence_history_projection.go b/internal/engine/sequence_history_projection.go index ac3d362..362cb7b 100644 --- a/internal/engine/sequence_history_projection.go +++ b/internal/engine/sequence_history_projection.go @@ -16,6 +16,7 @@ func projectTierHistoryApplications(applications []historyApplication, operation projected := cloneHistoryApplications(applications) for index, original := range applications { application := &projected[index] + var originalLookup *documentLookup if path, found := projectSequenceOperationPath(original.path, projections); found { application.path = path } @@ -25,7 +26,10 @@ func projectTierHistoryApplications(applications []historyApplication, operation if !active || operation.mode != sequenceHistoryMapped { continue } - value, found := documentAtPath(original.value, source.path) + if originalLookup == nil { + originalLookup = buildDocumentLookup(original.value) + } + value, found := documentAtPathWithLookup(original.value, source.path, originalLookup) if !found || value.Kind != document.Sequence { continue }