From f789e41fafc97637c4baf9c67f1f0c9a05317ffe Mon Sep 17 00:00:00 2001 From: Eric Rakestraw Date: Thu, 26 Nov 2026 01:36:25 +0000 Subject: [PATCH] Reuse source indexes in registry extraction --- docs/roadmap/implementation.md | 6 ++++++ internal/modules/dnd/extract/itemregistry/extractor.go | 2 +- internal/modules/dnd/extract/itemregistry/extractor_test.go | 5 ++++- .../modules/dnd/extract/itemregistry/test_helpers_test.go | 3 ++- internal/modules/dnd/extract/locationregistry/extractor.go | 2 +- .../modules/dnd/extract/locationregistry/extractor_test.go | 5 ++++- .../dnd/extract/locationregistry/test_helpers_test.go | 3 ++- internal/modules/dnd/extract/npcregistry/extractor.go | 2 +- internal/modules/dnd/extract/npcregistry/extractor_test.go | 2 ++ .../modules/dnd/extract/npcregistry/test_helpers_test.go | 3 ++- 10 files changed, 25 insertions(+), 8 deletions(-) diff --git a/docs/roadmap/implementation.md b/docs/roadmap/implementation.md index 913da2cb..c89032fe 100644 --- a/docs/roadmap/implementation.md +++ b/docs/roadmap/implementation.md @@ -278,6 +278,12 @@ Update borrowed-index comments and module internals alongside the API. ## Stage 7 — Reuse The Index In Registry Extraction +**Completed.** NPC, item, and location registry preparation borrows the optional +whole-source index for response ordering and endpoint repair, falling back locally +when absent. Existing literal output tests exercise both paths, including +non-monotonic source order. All three specified package tests pass. Other +extractors and the CAT-005 catalog disposition remain for the following stages. + **Coverage:** CAT-005 registry callers. **Prerequisites:** Stage 6. Migrate NPC/item/location registry extractors, including interpretation and diff --git a/internal/modules/dnd/extract/itemregistry/extractor.go b/internal/modules/dnd/extract/itemregistry/extractor.go index 9b327594..b4402857 100644 --- a/internal/modules/dnd/extract/itemregistry/extractor.go +++ b/internal/modules/dnd/extract/itemregistry/extractor.go @@ -58,7 +58,7 @@ func (e *Extractor) PrepareExtraction(ctx context.Context, req contracts.Extract if err != nil { return nil, extractorErrorf("%w", err) } - order := shared.NewSourceRefOrder(req.Source) + order := shared.NewSourceRefOrderForSource(req.Source, req.SourceIndex) sourceID := req.Source.ID materials := shared.PromptInputs(sourceInput, req.References) diff --git a/internal/modules/dnd/extract/itemregistry/extractor_test.go b/internal/modules/dnd/extract/itemregistry/extractor_test.go index 832ec74f..0f5eded5 100644 --- a/internal/modules/dnd/extract/itemregistry/extractor_test.go +++ b/internal/modules/dnd/extract/itemregistry/extractor_test.go @@ -18,7 +18,10 @@ func TestExtractMapsItemsWithOwnedEvidenceAndDeterministicOrder(t *testing.T) { {Name: " Gold Pieces ", SourceRefs: responseSourceRefs(3, 2)}, {Name: "Star Compass", SourceRefs: []itemSourceRefResponse{{StartUnitID: 2, EndUnitID: 2}, {StartUnitID: 1, EndUnitID: 1}, {StartUnitID: 1, EndUnitID: 1}}}, }}} - result, err := runExtraction(context.Background(), newExtractor(t, client), extractionRequest()) + req := extractionRequest() + index := source.NewDocumentIndex(req.Source) + req.SourceIndex = &index + result, err := runExtraction(context.Background(), newExtractor(t, client), req) if err != nil { t.Fatalf("Extract() error = %v, want nil", err) } diff --git a/internal/modules/dnd/extract/itemregistry/test_helpers_test.go b/internal/modules/dnd/extract/itemregistry/test_helpers_test.go index 88c63448..bceac34b 100644 --- a/internal/modules/dnd/extract/itemregistry/test_helpers_test.go +++ b/internal/modules/dnd/extract/itemregistry/test_helpers_test.go @@ -54,6 +54,7 @@ func registryOperation() pipeline.ExecutionContext[dnd.ItemRegistry] { type extractionTestInput struct { Source *source.SourceDocument + SourceIndex *source.DocumentIndex Chunk *source.Chunk AmbientContext map[string]any SourceInput contracts.LLMInputMaterial @@ -108,7 +109,7 @@ func runExtraction(ctx context.Context, fixture *extractionFixture, req extracti } op := req.Execution execution := pipeline.ExecutionContext[dnd.ItemRegistry]{Engine: engine, Controls: op.Controls, Assess: op.Assess, Execution: op.Execution, ReuseEligible: op.ReuseEligible, Cache: op.Cache} - input := contracts.ExtractionInput{Source: req.Source, Chunk: req.Chunk, SourceInput: req.SourceInput, SessionID: req.SessionID, References: req.References, LLMProfile: req.LLMProfile, Metadata: req.Metadata, AmbientContext: req.AmbientContext} + input := contracts.ExtractionInput{Source: req.Source, SourceIndex: req.SourceIndex, Chunk: req.Chunk, SourceInput: req.SourceInput, SessionID: req.SessionID, References: req.References, LLMProfile: req.LLMProfile, Metadata: req.Metadata, AmbientContext: req.AmbientContext} return pipeline.ExecuteOperation(ctx, execution, func(ctx context.Context) (contracts.PreparedOperation[dnd.ItemRegistry], error) { return extractor.PrepareExtraction(ctx, input) }) diff --git a/internal/modules/dnd/extract/locationregistry/extractor.go b/internal/modules/dnd/extract/locationregistry/extractor.go index 06753417..6f1c9e3a 100644 --- a/internal/modules/dnd/extract/locationregistry/extractor.go +++ b/internal/modules/dnd/extract/locationregistry/extractor.go @@ -58,7 +58,7 @@ func (e *Extractor) PrepareExtraction(ctx context.Context, req contracts.Extract if err != nil { return nil, extractorErrorf("%w", err) } - order := shared.NewSourceRefOrder(req.Source) + order := shared.NewSourceRefOrderForSource(req.Source, req.SourceIndex) sourceID := req.Source.ID materials := shared.PromptInputs(sourceInput, req.References) diff --git a/internal/modules/dnd/extract/locationregistry/extractor_test.go b/internal/modules/dnd/extract/locationregistry/extractor_test.go index f493ab83..dc0d466c 100644 --- a/internal/modules/dnd/extract/locationregistry/extractor_test.go +++ b/internal/modules/dnd/extract/locationregistry/extractor_test.go @@ -18,7 +18,10 @@ func TestExtractMapsLocationsWithOwnedEvidenceAndDeterministicOrder(t *testing.T {Name: "The Tavern", SourceRefs: responseSourceRefs(3, 2)}, {Name: "Old Mill", SourceRefs: []locationSourceRefResponse{{StartUnitID: 2, EndUnitID: 2}, {StartUnitID: 1, EndUnitID: 1}, {StartUnitID: 1, EndUnitID: 1}}}, }}} - result, err := runExtraction(context.Background(), newExtractor(t, client), extractionRequest()) + req := extractionRequest() + index := source.NewDocumentIndex(req.Source) + req.SourceIndex = &index + result, err := runExtraction(context.Background(), newExtractor(t, client), req) if err != nil { t.Fatalf("Extract() error = %v, want nil", err) } diff --git a/internal/modules/dnd/extract/locationregistry/test_helpers_test.go b/internal/modules/dnd/extract/locationregistry/test_helpers_test.go index d59390f1..cc942bd4 100644 --- a/internal/modules/dnd/extract/locationregistry/test_helpers_test.go +++ b/internal/modules/dnd/extract/locationregistry/test_helpers_test.go @@ -54,6 +54,7 @@ func registryOperation() pipeline.ExecutionContext[dnd.LocationRegistry] { type extractionTestInput struct { Source *source.SourceDocument + SourceIndex *source.DocumentIndex Chunk *source.Chunk AmbientContext map[string]any SourceInput contracts.LLMInputMaterial @@ -108,7 +109,7 @@ func runExtraction(ctx context.Context, fixture *extractionFixture, req extracti } op := req.Execution execution := pipeline.ExecutionContext[dnd.LocationRegistry]{Engine: engine, Controls: op.Controls, Assess: op.Assess, Execution: op.Execution, ReuseEligible: op.ReuseEligible, Cache: op.Cache} - input := contracts.ExtractionInput{Source: req.Source, Chunk: req.Chunk, SourceInput: req.SourceInput, SessionID: req.SessionID, References: req.References, LLMProfile: req.LLMProfile, Metadata: req.Metadata, AmbientContext: req.AmbientContext} + input := contracts.ExtractionInput{Source: req.Source, SourceIndex: req.SourceIndex, Chunk: req.Chunk, SourceInput: req.SourceInput, SessionID: req.SessionID, References: req.References, LLMProfile: req.LLMProfile, Metadata: req.Metadata, AmbientContext: req.AmbientContext} return pipeline.ExecuteOperation(ctx, execution, func(ctx context.Context) (contracts.PreparedOperation[dnd.LocationRegistry], error) { return extractor.PrepareExtraction(ctx, input) }) diff --git a/internal/modules/dnd/extract/npcregistry/extractor.go b/internal/modules/dnd/extract/npcregistry/extractor.go index d46c6edc..f302ee04 100644 --- a/internal/modules/dnd/extract/npcregistry/extractor.go +++ b/internal/modules/dnd/extract/npcregistry/extractor.go @@ -64,7 +64,7 @@ func (e *Extractor) PrepareExtraction(ctx context.Context, req contracts.Extract if err != nil { return nil, extractorErrorf("%w", err) } - order := shared.NewSourceRefOrder(req.Source) + order := shared.NewSourceRefOrderForSource(req.Source, req.SourceIndex) sourceID := req.Source.ID materials := shared.PromptInputs(sourceInput, req.References) diff --git a/internal/modules/dnd/extract/npcregistry/extractor_test.go b/internal/modules/dnd/extract/npcregistry/extractor_test.go index 7daa0128..f6ed01eb 100644 --- a/internal/modules/dnd/extract/npcregistry/extractor_test.go +++ b/internal/modules/dnd/extract/npcregistry/extractor_test.go @@ -77,6 +77,8 @@ func TestExtractOrdersNPCsBySourcePositionRatherThanUnitID(t *testing.T) { } req.Chunk.Units = append([]source.SourceUnit(nil), req.Source.Units...) req.Chunk.Ref = source.SourceRef{SourceID: req.Source.ID, StartUnitID: 100, EndUnitID: 50} + index := source.NewDocumentIndex(req.Source) + req.SourceIndex = &index result, err := runExtraction(context.Background(), newExtractor(t, client), req) if err != nil { diff --git a/internal/modules/dnd/extract/npcregistry/test_helpers_test.go b/internal/modules/dnd/extract/npcregistry/test_helpers_test.go index 8fcfe94f..758c64ad 100644 --- a/internal/modules/dnd/extract/npcregistry/test_helpers_test.go +++ b/internal/modules/dnd/extract/npcregistry/test_helpers_test.go @@ -66,6 +66,7 @@ func registryOperation() pipeline.ExecutionContext[dnd.NPCRegistry] { type extractionTestInput struct { Source *source.SourceDocument + SourceIndex *source.DocumentIndex Chunk *source.Chunk AmbientContext map[string]any SourceInput contracts.LLMInputMaterial @@ -120,7 +121,7 @@ func runExtraction(ctx context.Context, fixture *extractionFixture, req extracti } op := req.Execution execution := pipeline.ExecutionContext[dnd.NPCRegistry]{Engine: engine, Controls: op.Controls, Assess: op.Assess, Execution: op.Execution, ReuseEligible: op.ReuseEligible, Cache: op.Cache} - input := contracts.ExtractionInput{Source: req.Source, Chunk: req.Chunk, SourceInput: req.SourceInput, SessionID: req.SessionID, References: req.References, LLMProfile: req.LLMProfile, Metadata: req.Metadata, AmbientContext: req.AmbientContext} + input := contracts.ExtractionInput{Source: req.Source, SourceIndex: req.SourceIndex, Chunk: req.Chunk, SourceInput: req.SourceInput, SessionID: req.SessionID, References: req.References, LLMProfile: req.LLMProfile, Metadata: req.Metadata, AmbientContext: req.AmbientContext} return pipeline.ExecuteOperation(ctx, execution, func(ctx context.Context) (contracts.PreparedOperation[dnd.NPCRegistry], error) { return extractor.PrepareExtraction(ctx, input) })