Reuse source indexes in occurrence extraction

This commit is contained in:
2026-11-26 01:37:56 +00:00
parent f789e41faf
commit 553276c0a0
11 changed files with 42 additions and 8 deletions

View File

@@ -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,

View File

@@ -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 {

View File

@@ -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)

View File

@@ -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)
})

View File

@@ -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)

View File

@@ -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)

View File

@@ -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)
})

View File

@@ -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)

View File

@@ -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 {

View File

@@ -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)
})

View File

@@ -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))