diff --git a/docs/internal/dnd.md b/docs/internal/dnd.md index 96fa1d73..1546cc9b 100644 --- a/docs/internal/dnd.md +++ b/docs/internal/dnd.md @@ -107,6 +107,10 @@ or unresolvable citations remain detectable, not silently repaired. Campaign references and generated context can disambiguate or route work, never supply new source evidence. +Extraction borrows the run's whole-source index for source ordering, grounding, +and scene fallback; standalone calls construct a local index when none is supplied. +See [Module Internals](modules.md#preparing-operations) for its ownership boundary. + Typed chains retain separate structural, source, identity/domain, durable-schema, and advisory responsibilities; [Configuration](../config.md#production-validator-keys-and-default-chains) owns their order and replacement rules. Extraction containment requires the diff --git a/docs/roadmap/audit/catalog.md b/docs/roadmap/audit/catalog.md index 21e60286..cdf076f6 100644 --- a/docs/roadmap/audit/catalog.md +++ b/docs/roadmap/audit/catalog.md @@ -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 **24 outstanding catalog entries -and ten resolved entries**. Repeated responsibility findings are +The **45 original finding entries** reduce to **23 outstanding catalog entries +and eleven 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,7 @@ 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-004, CAT-007, CAT-011, CAT-012, CAT-013, CAT-015, +CAT-001, CAT-002, CAT-004, CAT-005, 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. @@ -152,6 +152,11 @@ dimension from each sparse lookup; it is not a measured overall runtime claim. **Assessment:** Source-established algorithmic opportunity; original P2. +**Status:** Resolved. A run-owned index is shared by extraction and cache +decoding; all ten D&D extraction paths use it, with local fallback for standalone +calls. Tests cover run isolation, changed source layouts, cache reuse, and +representative supplied-index extraction. No production speedup was measured. + **Source:** [EFF-001](02-efficiency.md#eff-001--reuse-the-full-source-position-index-across-extraction-jobs). Ten D&D extractors build the same document-position map per applicable chunk diff --git a/docs/roadmap/implementation.md b/docs/roadmap/implementation.md index df575eee..304d8e39 100644 --- a/docs/roadmap/implementation.md +++ b/docs/roadmap/implementation.md @@ -319,6 +319,15 @@ The optional index is not a new required caller argument. ## Stage 9 — Complete Extraction Index Reuse +**Completed.** Spells, combat turns, enemy events, and scene descriptions now +borrow the run index, with local construction when omitted. Combat eligibility +still returns before ordering construction. The six registry/occurrence paths +use the same optional ordering constructor; location grounding shares its index, +and scene reuse/fallback validates against the same whole-source index. Focused +extractor and pipeline tests, pipeline/shared race tests, and full tests, vet, +and build passed. Cross-run layout and cache checks are covered by the preceding +framework tests. No production speedup was measured or follow-up deferred. + **Coverage:** CAT-005 completion. **Prerequisites:** Stages 6–8. Migrate spells, combat turns, enemy events, and scene-description extractors. diff --git a/internal/modules/dnd/extract/combatturns/extractor.go b/internal/modules/dnd/extract/combatturns/extractor.go index 57ec6ae2..570c3c9c 100644 --- a/internal/modules/dnd/extract/combatturns/extractor.go +++ b/internal/modules/dnd/extract/combatturns/extractor.go @@ -129,7 +129,7 @@ func (e *Extractor) PrepareExtraction(ctx context.Context, req contracts.Extract default: return nil, extractorErrorf("unsupported scene eligibility match state %q", match.State) } - 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/combatturns/extractor_test.go b/internal/modules/dnd/extract/combatturns/extractor_test.go index f0603594..7a631bc8 100644 --- a/internal/modules/dnd/extract/combatturns/extractor_test.go +++ b/internal/modules/dnd/extract/combatturns/extractor_test.go @@ -33,7 +33,10 @@ func TestExtractMapsAndOrdersCombatTurnsBySourcePosition(t *testing.T) { }, }}} - result, err := runExtraction(context.Background(), newExtractor(t, client), extractionRequest(t)) + req := extractionRequest(t) + 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/combatturns/test_helpers_test.go b/internal/modules/dnd/extract/combatturns/test_helpers_test.go index f83d4131..84b86751 100644 --- a/internal/modules/dnd/extract/combatturns/test_helpers_test.go +++ b/internal/modules/dnd/extract/combatturns/test_helpers_test.go @@ -62,6 +62,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 @@ -116,7 +117,7 @@ func runExtraction(ctx context.Context, fixture *extractionFixture, req extracti } op := req.Execution execution := pipeline.ExecutionContext[dnd.CombatTurnList]{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.CombatTurnList], error) { return extractor.PrepareExtraction(ctx, input) }) diff --git a/internal/modules/dnd/extract/enemyevents/extractor.go b/internal/modules/dnd/extract/enemyevents/extractor.go index f68f93bb..ef305135 100644 --- a/internal/modules/dnd/extract/enemyevents/extractor.go +++ b/internal/modules/dnd/extract/enemyevents/extractor.go @@ -85,7 +85,7 @@ func (e *Extractor) PrepareExtraction(ctx context.Context, req contracts.Extract return nil, extractorErrorf("resolve enemy-event grounding: %w", err) } - order := shared.NewSourceRefOrder(req.Source) + order := shared.NewSourceRefOrderForSource(req.Source, req.SourceIndex) sourceID := req.Source.ID materials := shared.PromptInputs(sourceInput, req.References) for name, material := range grounding.PromptInputs() { diff --git a/internal/modules/dnd/extract/enemyevents/extractor_test.go b/internal/modules/dnd/extract/enemyevents/extractor_test.go index cecc63d2..d56d56f4 100644 --- a/internal/modules/dnd/extract/enemyevents/extractor_test.go +++ b/internal/modules/dnd/extract/enemyevents/extractor_test.go @@ -24,7 +24,10 @@ func TestExtractMapsEnemyEventsInSourceOrder(t *testing.T) { {Name: "Ashfang", Kind: "incapacitated", SourceRefs: []enemySourceRefResponse{{StartUnitID: 2, EndUnitID: 2}}}, }}} - result, err := runExtraction(context.Background(), newEnemyExtractor(t, client), enemyExtractionRequest(t)) + req := enemyExtractionRequest(t) + index := source.NewDocumentIndex(req.Source) + req.SourceIndex = &index + result, err := runExtraction(context.Background(), newEnemyExtractor(t, client), req) if err != nil { t.Fatal(err) } diff --git a/internal/modules/dnd/extract/enemyevents/test_helpers_test.go b/internal/modules/dnd/extract/enemyevents/test_helpers_test.go index 1efeadb4..46dbcdb6 100644 --- a/internal/modules/dnd/extract/enemyevents/test_helpers_test.go +++ b/internal/modules/dnd/extract/enemyevents/test_helpers_test.go @@ -17,6 +17,7 @@ import ( type extractionTestInput struct { Source *source.SourceDocument + SourceIndex *source.DocumentIndex Chunk *source.Chunk AmbientContext map[string]any SourceInput contracts.LLMInputMaterial @@ -69,7 +70,7 @@ func newEnemyExtractor(t *testing.T, client promptkit.LLMClient, references ...c func runExtraction(ctx context.Context, fixture *extractionFixture, req extractionTestInput) (pipeline.OperationResult[dnd.EnemyEventList], error) { op := req.Execution execution := pipeline.ExecutionContext[dnd.EnemyEventList]{Engine: fixture.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.EnemyEventList], error) { return fixture.Extractor.PrepareExtraction(ctx, input) }) diff --git a/internal/modules/dnd/extract/scenedescriptions/extractor.go b/internal/modules/dnd/extract/scenedescriptions/extractor.go index efb9cd5c..b05b86af 100644 --- a/internal/modules/dnd/extract/scenedescriptions/extractor.go +++ b/internal/modules/dnd/extract/scenedescriptions/extractor.go @@ -72,7 +72,11 @@ func (e *Extractor) PrepareExtraction(ctx context.Context, req contracts.Extract } chunk := source.Chunk{ID: req.Chunk.ID, Ref: req.Chunk.Ref} - index := source.NewDocumentIndex(req.Source) + index := req.SourceIndex + if index == nil { + local := source.NewDocumentIndex(req.Source) + index = &local + } reuse := func(value dnd.SceneDescriptionList) (dnd.SceneDescriptionList, error) { if len(value.Scenes) != 1 || value.Scenes[0].SourceRef != chunk.Ref { return dnd.SceneDescriptionList{}, fmt.Errorf("cached scene does not describe the current chunk range") @@ -80,7 +84,7 @@ func (e *Extractor) PrepareExtraction(ctx context.Context, req contracts.Extract // Chunk ordinals can change without changing the semantic prompt. value.Scenes = append([]dnd.SceneDescription(nil), value.Scenes...) value.Scenes[0].ID = chunk.ID - return sceneFallback(&value, chunk, index) + return sceneFallback(&value, chunk, *index) } materials := shared.PromptInputs(sourceInput, req.References) inputs := make(map[string]promptkit.ArtifactRef, len(materials)) @@ -103,7 +107,7 @@ func (e *Extractor) PrepareExtraction(ctx context.Context, req contracts.Extract if evaluation != nil { final = evaluation.View().Candidate } - return sceneFallback(final, chunk, index) + return sceneFallback(final, chunk, *index) }}, nil } diff --git a/internal/modules/dnd/extract/scenedescriptions/extractor_test.go b/internal/modules/dnd/extract/scenedescriptions/extractor_test.go index 69c3d63f..7ab0ebeb 100644 --- a/internal/modules/dnd/extract/scenedescriptions/extractor_test.go +++ b/internal/modules/dnd/extract/scenedescriptions/extractor_test.go @@ -17,6 +17,8 @@ func TestExtractMapsExactlyOneSceneToTheChunk(t *testing.T) { Kind: dnd.SceneKindCombat, Title: " Fight at the Watchtower ", Summary: " Bandits attack the party. ", }} request := extractionRequest() + index := source.NewDocumentIndex(request.Source) + request.SourceIndex = &index result, err := runExtraction(newExtractor(t, client), context.Background(), request) if err != nil { t.Fatalf("Extract() error = %v, want nil", err) diff --git a/internal/modules/dnd/extract/scenedescriptions/fallback_test.go b/internal/modules/dnd/extract/scenedescriptions/fallback_test.go index c24f291f..de614487 100644 --- a/internal/modules/dnd/extract/scenedescriptions/fallback_test.go +++ b/internal/modules/dnd/extract/scenedescriptions/fallback_test.go @@ -1,10 +1,12 @@ package scenedescriptions import ( + "context" "reflect" "testing" "gitea.maximumdirect.net/eric/notarius/internal/core/source" + "gitea.maximumdirect.net/eric/notarius/internal/framework/contracts" "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd" ) @@ -59,3 +61,30 @@ func TestSceneFallbackRequiresOneUsableFinalSceneWithCurrentIdentity(t *testing. t.Fatal("fallback aliases the candidate or alters its classification/prose") } } + +func TestPreparedSceneReuseChecksCurrentSourceWithOptionalIndex(t *testing.T) { + req := extractionRequest() + index := source.NewDocumentIndex(req.Source) + extractor, err := New(Options{}) + if err != nil { + t.Fatal(err) + } + value := dnd.SceneDescriptionList{Scenes: []dnd.SceneDescription{{ID: "old-chunk", SourceRef: req.Chunk.Ref, Kind: dnd.SceneKindNarrative, Title: "Arrival", Summary: "The party arrives."}}} + for _, supplied := range []*source.DocumentIndex{nil, &index} { + prepared, err := extractor.PrepareExtraction(context.Background(), contracts.ExtractionInput{Source: req.Source, SourceIndex: supplied, Chunk: req.Chunk, SourceInput: req.SourceInput}) + if err != nil { + t.Fatal(err) + } + native := prepared.(contracts.NativeOperation[dnd.SceneDescriptionList]) + got, err := native.Reuse(value) + if err != nil || got.Scenes[0].ID != req.Chunk.ID { + t.Fatalf("Reuse() = %#v, %v", got, err) + } + invalid := value + invalid.Scenes = append([]dnd.SceneDescription(nil), value.Scenes...) + invalid.Scenes[0].SourceRef.EndUnitID = 999 + if _, err := native.Reuse(invalid); err == nil { + t.Fatal("Reuse() accepted a different source range") + } + } +} diff --git a/internal/modules/dnd/extract/scenedescriptions/native_execution_test.go b/internal/modules/dnd/extract/scenedescriptions/native_execution_test.go index 80c12a40..96b8529b 100644 --- a/internal/modules/dnd/extract/scenedescriptions/native_execution_test.go +++ b/internal/modules/dnd/extract/scenedescriptions/native_execution_test.go @@ -7,6 +7,7 @@ import ( "strings" "testing" + "gitea.maximumdirect.net/eric/notarius/internal/core/source" "gitea.maximumdirect.net/eric/notarius/internal/framework/contracts" "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd" scenecodec "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/codec/scenedescriptions" @@ -105,6 +106,8 @@ func TestSceneCorrectionUsesRealNativeCombatJudgmentsAndPreservesSourceIdentity( func TestPermissiveSceneRetainsOnlyFinalUsableCandidateWithoutSemanticRevalidation(t *testing.T) { request := extractionRequest() + index := source.NewDocumentIndex(request.Source) + request.SourceIndex = &index request.Execution.Controls.Mode = contracts.ModePermissive request.Execution.Controls.MaxAdditionalTurns = 1 // Both scenes remain non-combat, but the final title/summary must win. diff --git a/internal/modules/dnd/extract/scenedescriptions/test_helpers_test.go b/internal/modules/dnd/extract/scenedescriptions/test_helpers_test.go index 241a7946..25ce2a20 100644 --- a/internal/modules/dnd/extract/scenedescriptions/test_helpers_test.go +++ b/internal/modules/dnd/extract/scenedescriptions/test_helpers_test.go @@ -62,6 +62,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 @@ -120,7 +121,7 @@ func runExtraction(fixture *extractionFixture, ctx context.Context, req extracti } op := req.Execution execution := pipeline.ExecutionContext[dnd.SceneDescriptionList]{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.SceneDescriptionList], error) { return extractor.PrepareExtraction(ctx, input) }) diff --git a/internal/modules/dnd/extract/spells/extractor.go b/internal/modules/dnd/extract/spells/extractor.go index 620cb2fc..71ea5464 100644 --- a/internal/modules/dnd/extract/spells/extractor.go +++ b/internal/modules/dnd/extract/spells/extractor.go @@ -109,7 +109,7 @@ func (e *Extractor) PrepareExtraction(ctx context.Context, req contracts.Extract return nil, extractorErrorf("%w", err) } sourceID := req.Source.ID - 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/spells/extractor_test.go b/internal/modules/dnd/extract/spells/extractor_test.go index 5268a11e..8092f75f 100644 --- a/internal/modules/dnd/extract/spells/extractor_test.go +++ b/internal/modules/dnd/extract/spells/extractor_test.go @@ -26,6 +26,8 @@ func TestExtractReturnsCanonicalSpellListFromPrivateResponse(t *testing.T) { }, }}} req := extractionRequest() + index := source.NewDocumentIndex(req.Source) + req.SourceIndex = &index var prepared promptkit.PreparedRun req.Execution.Execution.OnPrepared = func(_ context.Context, value promptkit.PreparedRun) { prepared = value } diff --git a/internal/modules/dnd/extract/spells/test_helpers_test.go b/internal/modules/dnd/extract/spells/test_helpers_test.go index f72cbb6f..5f359012 100644 --- a/internal/modules/dnd/extract/spells/test_helpers_test.go +++ b/internal/modules/dnd/extract/spells/test_helpers_test.go @@ -115,6 +115,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 @@ -142,7 +143,7 @@ func newExtractor(t *testing.T, client promptkit.LLMClient, references ...contra func runExtraction(ctx context.Context, fixture *extractionFixture, req extractionTestInput) (pipeline.OperationResult[dnd.SpellList], error) { operation := req.Execution execution := pipeline.ExecutionContext[dnd.SpellList]{Engine: fixture.engine, Controls: operation.Controls, Assess: operation.Assess, Execution: operation.Execution, ReuseEligible: operation.ReuseEligible, Cache: operation.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.SpellList], error) { return fixture.Extractor.PrepareExtraction(ctx, input) })