diff --git a/docs/roadmap/implementation.md b/docs/roadmap/implementation.md index c89032fe..df575eee 100644 --- a/docs/roadmap/implementation.md +++ b/docs/roadmap/implementation.md @@ -298,6 +298,14 @@ not rebuild the index. No cache-key or entity-ID change. ## Stage 8 — Reuse The Index In Occurrence Extraction +**Completed.** NPC, item, and location occurrence extraction use the optional +whole-source index for ordering and endpoint repair. Location grounding shares +that index, including a single locally built fallback for standalone extraction. +Existing contextual-selector, identity, evidence, and invalid-reference tests +remain in place; representative supplied-index cases and all three specified +package suites pass, as does the location registry suite. Remaining extractor +paths and the CAT-005 catalog disposition follow next. + **Coverage:** CAT-005 occurrence callers. **Prerequisites:** Stages 6–7. Migrate NPC/item/location occurrence extractors. Preserve registry resolution, diff --git a/internal/modules/dnd/extract/itemoccurrences/extractor.go b/internal/modules/dnd/extract/itemoccurrences/extractor.go index 380c70d5..69245068 100644 --- a/internal/modules/dnd/extract/itemoccurrences/extractor.go +++ b/internal/modules/dnd/extract/itemoccurrences/extractor.go @@ -91,7 +91,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) registry, err := e.itemResolver.Resolve(req.References) if err != nil { diff --git a/internal/modules/dnd/extract/itemoccurrences/extractor_test.go b/internal/modules/dnd/extract/itemoccurrences/extractor_test.go index ce7d330c..0d361218 100644 --- a/internal/modules/dnd/extract/itemoccurrences/extractor_test.go +++ b/internal/modules/dnd/extract/itemoccurrences/extractor_test.go @@ -5,6 +5,7 @@ import ( "strings" "testing" + "gitea.maximumdirect.net/eric/notarius/internal/core/source" "gitea.maximumdirect.net/eric/notarius/internal/framework/contracts" itemidentity "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/items/identity" ) @@ -15,6 +16,8 @@ func TestExtractGroundsOccurrencesInRequiredRegistry(t *testing.T) { client := &fakeItemOccurrencesLLMClient{content: append([]byte(nil), rawResponse...)} req := extractionRequest() req.References = itemRegistryReferences(t) + index := source.NewDocumentIndex(req.Source) + req.SourceIndex = &index result, err := runExtraction(context.Background(), newExtractor(t, client, req.References), req) if err != nil { t.Fatal(err) diff --git a/internal/modules/dnd/extract/itemoccurrences/test_helpers_test.go b/internal/modules/dnd/extract/itemoccurrences/test_helpers_test.go index f2dfb8f7..9244ede5 100644 --- a/internal/modules/dnd/extract/itemoccurrences/test_helpers_test.go +++ b/internal/modules/dnd/extract/itemoccurrences/test_helpers_test.go @@ -59,6 +59,7 @@ func responseRefs(start, end int) []itemOccurrenceSourceRefResponse { type extractionTestInput struct { Source *source.SourceDocument + SourceIndex *source.DocumentIndex Chunk *source.Chunk AmbientContext map[string]any SourceInput contracts.LLMInputMaterial @@ -113,7 +114,7 @@ func runExtraction(ctx context.Context, fixture *extractionFixture, req extracti } op := req.Execution execution := pipeline.ExecutionContext[dnd.ItemOccurrenceList]{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.ItemOccurrenceList], error) { return extractor.PrepareExtraction(ctx, input) }) diff --git a/internal/modules/dnd/extract/locationoccurrences/extractor.go b/internal/modules/dnd/extract/locationoccurrences/extractor.go index feec4cbc..c7db9468 100644 --- a/internal/modules/dnd/extract/locationoccurrences/extractor.go +++ b/internal/modules/dnd/extract/locationoccurrences/extractor.go @@ -10,6 +10,7 @@ import ( "go.maximumdirect.net/promptkit" + "gitea.maximumdirect.net/eric/notarius/internal/core/source" "gitea.maximumdirect.net/eric/notarius/internal/framework/contracts" "gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline" "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd" @@ -92,12 +93,17 @@ func (e *Extractor) PrepareExtraction(ctx context.Context, req contracts.Extract if !registry.Bound() { return nil, extractorErrorf("location registry reference is required") } - grounding, err := locationregistry.NewGrounding(registry, req.Source) + index := req.SourceIndex + if index == nil { + local := source.NewDocumentIndex(req.Source) + index = &local + } + grounding, err := locationregistry.NewGroundingForSource(registry, req.Source, index) if err != nil { return nil, extractorErrorf("prepare location grounding: %w", err) } - order := shared.NewSourceRefOrder(req.Source) + order := shared.NewSourceRefOrderForSource(req.Source, index) sourceID := req.Source.ID materials := shared.PromptInputs(sourceInput, req.References) diff --git a/internal/modules/dnd/extract/locationoccurrences/extractor_test.go b/internal/modules/dnd/extract/locationoccurrences/extractor_test.go index d839f416..3352a7ca 100644 --- a/internal/modules/dnd/extract/locationoccurrences/extractor_test.go +++ b/internal/modules/dnd/extract/locationoccurrences/extractor_test.go @@ -31,6 +31,8 @@ func TestExtractMapsKindsOrdersOccurrencesAndPreservesIndependentFacts(t *testin references := registryReferences(t, locations) req := extractionRequest() req.References = references + index := source.NewDocumentIndex(req.Source) + req.SourceIndex = &index result, err := runExtraction(context.Background(), newExtractor(t, client, references), req) if err != nil { t.Fatal(err) diff --git a/internal/modules/dnd/extract/locationoccurrences/test_helpers_test.go b/internal/modules/dnd/extract/locationoccurrences/test_helpers_test.go index 76b6d77f..b3fa142c 100644 --- a/internal/modules/dnd/extract/locationoccurrences/test_helpers_test.go +++ b/internal/modules/dnd/extract/locationoccurrences/test_helpers_test.go @@ -48,6 +48,7 @@ func occurrenceRefs(start, end int) []occurrenceSourceRefResponse { type extractionTestInput struct { Source *source.SourceDocument + SourceIndex *source.DocumentIndex Chunk *source.Chunk AmbientContext map[string]any SourceInput contracts.LLMInputMaterial @@ -102,7 +103,7 @@ func runExtraction(ctx context.Context, fixture *extractionFixture, req extracti } op := req.Execution execution := pipeline.ExecutionContext[dnd.LocationOccurrenceList]{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.LocationOccurrenceList], error) { return extractor.PrepareExtraction(ctx, input) }) diff --git a/internal/modules/dnd/extract/npcoccurrences/extractor.go b/internal/modules/dnd/extract/npcoccurrences/extractor.go index b901dd42..d7325139 100644 --- a/internal/modules/dnd/extract/npcoccurrences/extractor.go +++ b/internal/modules/dnd/extract/npcoccurrences/extractor.go @@ -90,7 +90,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) npcRegistry, err := e.npcResolver.Resolve(req.References) if err != nil { return nil, extractorErrorf("resolve NPC registry: %w", err) diff --git a/internal/modules/dnd/extract/npcoccurrences/extractor_test.go b/internal/modules/dnd/extract/npcoccurrences/extractor_test.go index 8888038a..423ecce5 100644 --- a/internal/modules/dnd/extract/npcoccurrences/extractor_test.go +++ b/internal/modules/dnd/extract/npcoccurrences/extractor_test.go @@ -28,6 +28,8 @@ func TestExtractMapsEveryKindAndOrdersBySourcePosition(t *testing.T) { references := requiredRegistryReferences(t, "Mentioned", "Speaker", "Present", "Ally", "Opponent", "Other", "Invalid") req := extractionRequest() req.References = references + index := source.NewDocumentIndex(req.Source) + req.SourceIndex = &index result, err := runExtraction(context.Background(), newExtractor(t, client, references), req) if err != nil { diff --git a/internal/modules/dnd/extract/npcoccurrences/test_helpers_test.go b/internal/modules/dnd/extract/npcoccurrences/test_helpers_test.go index 09482952..91d5ea70 100644 --- a/internal/modules/dnd/extract/npcoccurrences/test_helpers_test.go +++ b/internal/modules/dnd/extract/npcoccurrences/test_helpers_test.go @@ -99,6 +99,7 @@ func mismatchedSourceInputRequest(req extractionTestInput) extractionTestInput { type extractionTestInput struct { Source *source.SourceDocument + SourceIndex *source.DocumentIndex Chunk *source.Chunk AmbientContext map[string]any SourceInput contracts.LLMInputMaterial @@ -153,7 +154,7 @@ func runExtraction(ctx context.Context, fixture *extractionFixture, req extracti } op := req.Execution execution := pipeline.ExecutionContext[dnd.NPCOccurrenceList]{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.NPCOccurrenceList], error) { return extractor.PrepareExtraction(ctx, input) }) diff --git a/internal/modules/dnd/locations/registry/grounding.go b/internal/modules/dnd/locations/registry/grounding.go index db2bb789..ec5ef26d 100644 --- a/internal/modules/dnd/locations/registry/grounding.go +++ b/internal/modules/dnd/locations/registry/grounding.go @@ -52,6 +52,11 @@ type groundingProjectionLocation struct { // NewGrounding creates the contextual location projection for one source // document and resolved registry. func NewGrounding(registry *Registry, doc *source.SourceDocument) (*Grounding, error) { + return NewGroundingForSource(registry, doc, nil) +} + +// NewGroundingForSource uses a matching whole-source index when supplied. +func NewGroundingForSource(registry *Registry, doc *source.SourceDocument, supplied *source.DocumentIndex) (*Grounding, error) { if registry == nil { return nil, fmt.Errorf("location registry is required") } @@ -59,7 +64,12 @@ func NewGrounding(registry *Registry, doc *source.SourceDocument) (*Grounding, e return nil, fmt.Errorf("source document is required") } - index := source.NewDocumentIndex(doc) + var index source.DocumentIndex + if supplied == nil { + index = source.NewDocumentIndex(doc) + } else { + index = *supplied + } order := shared.NewSourceRefOrderFromIndex(index) locations := registry.Locations() grouped := make(map[string][]int, len(locations))