From 03e4c0d64de772da180f0f179a1e23d31fa1d72f Mon Sep 17 00:00:00 2001 From: Eric Rakestraw Date: Sun, 29 Nov 2026 18:13:45 +0000 Subject: [PATCH] Prepare shared context for all D&D extractors --- docs/roadmap/implementation.md | 2 + .../extract/combatturns/context_input_test.go | 16 +++ .../dnd/extract/combatturns/extractor.go | 9 +- .../dnd/extract/combatturns/extractor_test.go | 6 +- .../extract/enemyevents/context_input_test.go | 16 +++ .../dnd/extract/enemyevents/extractor.go | 9 +- .../dnd/extract/enemyevents/extractor_test.go | 19 ++- .../itemoccurrences/context_input_test.go | 16 +++ .../dnd/extract/itemoccurrences/extractor.go | 9 +- .../locationoccurrences/context_input_test.go | 16 +++ .../extract/locationoccurrences/extractor.go | 9 +- .../npcoccurrences/context_input_test.go | 16 +++ .../dnd/extract/npcoccurrences/extractor.go | 9 +- .../dnd/extract/spells/context_input_test.go | 16 +++ .../modules/dnd/extract/spells/extractor.go | 9 +- .../dnd/extract/spells/extractor_test.go | 21 +-- .../dnd/extract/spells/registry_test.go | 6 +- internal/modules/dnd/shared/prompt_inputs.go | 13 -- .../modules/dnd/shared/prompt_inputs_test.go | 122 ++---------------- .../integration/dnd_npc_grounded_test.go | 8 +- .../integration/dnd_spells_runner_test.go | 14 +- .../integration/native_spell_pipeline_test.go | 4 +- 22 files changed, 189 insertions(+), 176 deletions(-) create mode 100644 internal/modules/dnd/extract/combatturns/context_input_test.go create mode 100644 internal/modules/dnd/extract/enemyevents/context_input_test.go create mode 100644 internal/modules/dnd/extract/itemoccurrences/context_input_test.go create mode 100644 internal/modules/dnd/extract/locationoccurrences/context_input_test.go create mode 100644 internal/modules/dnd/extract/npcoccurrences/context_input_test.go create mode 100644 internal/modules/dnd/extract/spells/context_input_test.go diff --git a/docs/roadmap/implementation.md b/docs/roadmap/implementation.md index 112f58ce..7f908bc8 100644 --- a/docs/roadmap/implementation.md +++ b/docs/roadmap/implementation.md @@ -353,6 +353,8 @@ extractor package tests. Update obsolete text fixtures for these consumers. ## Stage 8 — Migrate All Remaining D&D Context Consumers +**Status:** Complete. + **Goal:** Complete context cutover without leaving a text-input bypass. **Inspect:** Remaining extraction constructors/builders for spells, combat turns, diff --git a/internal/modules/dnd/extract/combatturns/context_input_test.go b/internal/modules/dnd/extract/combatturns/context_input_test.go new file mode 100644 index 00000000..547624bb --- /dev/null +++ b/internal/modules/dnd/extract/combatturns/context_input_test.go @@ -0,0 +1,16 @@ +package combatturns + +import ( + "testing" + + "gitea.maximumdirect.net/eric/notarius/internal/framework/contracts" +) + +func TestConstructionRejectsInvalidSharedReference(t *testing.T) { + refs := contracts.ReferenceSet{Slots: map[string]contracts.ResolvedReferenceSlot{ + "glossary": {Items: []contracts.ReferenceItem{{MediaType: "application/json", Content: []byte("{}")}}}, + }} + if _, err := New(Options{}, refs); err == nil { + t.Fatal("invalid glossary accepted during construction") + } +} diff --git a/internal/modules/dnd/extract/combatturns/extractor.go b/internal/modules/dnd/extract/combatturns/extractor.go index f54aac3f..b708dded 100644 --- a/internal/modules/dnd/extract/combatturns/extractor.go +++ b/internal/modules/dnd/extract/combatturns/extractor.go @@ -70,6 +70,7 @@ var _ contracts.Extractor[dnd.CombatTurnList] = (*Extractor)(nil) type Options struct{} type Extractor struct { + context shared.PreparedContext npcResolver *npcregistry.Resolver sceneResolver *sceneregistry.Resolver } @@ -82,6 +83,10 @@ func New(_ Options, references ...contracts.ReferenceSet) (*Extractor, error) { if len(references) == 1 { referenceSet = references[0] } + context, err := shared.PrepareContext(referenceSet) + if err != nil { + return nil, extractorErrorf("prepare references: %w", err) + } npcResolver, err := npcregistry.NewResolver(referenceSet) if err != nil { return nil, extractorErrorf("prepare NPC registry prompt input: %w", err) @@ -90,7 +95,7 @@ func New(_ Options, references ...contracts.ReferenceSet) (*Extractor, error) { if err != nil { return nil, extractorErrorf("prepare scene eligibility: %w", err) } - return &Extractor{npcResolver: npcResolver, sceneResolver: sceneResolver}, nil + return &Extractor{context: context, npcResolver: npcResolver, sceneResolver: sceneResolver}, nil } func (e *Extractor) Key() string { return Key } @@ -136,7 +141,7 @@ func (e *Extractor) PrepareExtraction(ctx context.Context, req contracts.Extract } sourceID := req.Source.ID - materials := shared.PromptInputs(sourceInput, req.References) + materials := e.context.Inputs(sourceInput) materials[NPCRegistryReferenceSlot] = npcRegistry.PromptInput() inputs := make(map[string]promptkit.ArtifactRef, len(materials)) for name, material := range materials { diff --git a/internal/modules/dnd/extract/combatturns/extractor_test.go b/internal/modules/dnd/extract/combatturns/extractor_test.go index 7a631bc8..1a992678 100644 --- a/internal/modules/dnd/extract/combatturns/extractor_test.go +++ b/internal/modules/dnd/extract/combatturns/extractor_test.go @@ -125,8 +125,8 @@ func TestExtractPassesReferencesAndNPCGroundingWithoutUsingItAsEvidence(t *testi client := &fakeCombatTurnsLLMClient{response: extractionResponse{CombatTurns: []combatTurnResponse{}}} references := contracts.ReferenceSet{Slots: map[string]contracts.ResolvedReferenceSlot{ "players": {Slot: contracts.ReferenceSlot{Name: "players"}, Items: []contracts.ReferenceItem{{SlotName: "players", Content: []byte("Alice: Aria")}}}, - "party": {Slot: contracts.ReferenceSlot{Name: "party"}, Items: []contracts.ReferenceItem{{SlotName: "party", Content: []byte("Aria: cleric")}}}, - "glossary": {Slot: contracts.ReferenceSlot{Name: "glossary"}, Items: []contracts.ReferenceItem{{SlotName: "glossary", Content: []byte("ogre: a large foe")}}}, + "party": {Slot: contracts.ReferenceSlot{Name: "party"}, Items: []contracts.ReferenceItem{{SlotName: "party", MediaType: "application/json", Content: []byte(`{"schema_version":"maximumdirect.rpg.party.v1","characters":{"a":{"character":{"name":"Aria","classes":[{"name":"cleric"}]}}}}`)}}}, + "glossary": {Slot: contracts.ReferenceSlot{Name: "glossary"}, Items: []contracts.ReferenceItem{{SlotName: "glossary", MediaType: "application/json", Content: []byte(`{"schema_version":"maximumdirect.glossary.v1","collections":{"campaign":{"entries":[{"id":"o","name":"ogre","category":"foe","description":"a large foe"}]}}}`)}}}, NPCRegistryReferenceSlot: {Slot: contracts.ReferenceSlot{Name: NPCRegistryReferenceSlot}, Items: []contracts.ReferenceItem{{SlotName: NPCRegistryReferenceSlot, MediaType: "application/json", Content: npcRegistryJSON(t)}}}, }} req := extractionRequest(t) @@ -143,7 +143,7 @@ func TestExtractPassesReferencesAndNPCGroundingWithoutUsingItAsEvidence(t *testi for _, message := range messages { all += message.Content } - for _, material := range []string{"Alice: Aria", "Aria: cleric", "ogre: a large foe"} { + for _, material := range []string{"Alice: Aria", "cleric", "a large foe"} { if strings.Count(all, material) != 1 { t.Fatalf("campaign input not rendered once: %q", material) } diff --git a/internal/modules/dnd/extract/enemyevents/context_input_test.go b/internal/modules/dnd/extract/enemyevents/context_input_test.go new file mode 100644 index 00000000..10ff4670 --- /dev/null +++ b/internal/modules/dnd/extract/enemyevents/context_input_test.go @@ -0,0 +1,16 @@ +package enemyevents + +import ( + "testing" + + "gitea.maximumdirect.net/eric/notarius/internal/framework/contracts" +) + +func TestConstructionRejectsInvalidSharedReference(t *testing.T) { + refs := contracts.ReferenceSet{Slots: map[string]contracts.ResolvedReferenceSlot{ + "glossary": {Items: []contracts.ReferenceItem{{MediaType: "application/json", Content: []byte("{}")}}}, + }} + if _, err := New(Options{}, refs); err == nil { + t.Fatal("invalid glossary accepted during construction") + } +} diff --git a/internal/modules/dnd/extract/enemyevents/extractor.go b/internal/modules/dnd/extract/enemyevents/extractor.go index 0e1541fa..b56dc8ae 100644 --- a/internal/modules/dnd/extract/enemyevents/extractor.go +++ b/internal/modules/dnd/extract/enemyevents/extractor.go @@ -33,6 +33,7 @@ var _ contracts.Extractor[dnd.EnemyEventList] = (*Extractor)(nil) type Options struct{} type Extractor struct { + context shared.PreparedContext grounding *groundingResolver } @@ -44,11 +45,15 @@ func New(_ Options, references ...contracts.ReferenceSet) (*Extractor, error) { if len(references) == 1 { referenceSet = references[0] } + context, err := shared.PrepareContext(referenceSet) + if err != nil { + return nil, extractorErrorf("prepare references: %w", err) + } grounding, err := newGroundingResolver(referenceSet) if err != nil { return nil, extractorErrorf("prepare grounding: %w", err) } - return &Extractor{grounding: grounding}, nil + return &Extractor{context: context, grounding: grounding}, nil } func (e *Extractor) Key() string { return Key } @@ -87,7 +92,7 @@ func (e *Extractor) PrepareExtraction(ctx context.Context, req contracts.Extract order := shared.NewSourceRefOrderForSource(req.Source, req.SourceIndex) sourceID := req.Source.ID - materials := shared.PromptInputs(sourceInput, req.References) + materials := e.context.Inputs(sourceInput) for name, material := range grounding.PromptInputs() { materials[name] = material } diff --git a/internal/modules/dnd/extract/enemyevents/extractor_test.go b/internal/modules/dnd/extract/enemyevents/extractor_test.go index d56d56f4..89f49d62 100644 --- a/internal/modules/dnd/extract/enemyevents/extractor_test.go +++ b/internal/modules/dnd/extract/enemyevents/extractor_test.go @@ -25,9 +25,17 @@ func TestExtractMapsEnemyEventsInSourceOrder(t *testing.T) { }}} req := enemyExtractionRequest(t) + req.References.Slots["party"] = contracts.ResolvedReferenceSlot{Items: []contracts.ReferenceItem{{ + MediaType: "application/json", + Content: []byte(`{"schema_version":"maximumdirect.rpg.party.v1","characters":{"a":{"character":{"name":"Aria","aliases":["Star"]},"player":{"name":"Alice"}}}}`), + }}} + req.References.Slots["glossary"] = contracts.ResolvedReferenceSlot{Items: []contracts.ReferenceItem{{ + MediaType: "application/json", + Content: []byte(`{"schema_version":"maximumdirect.glossary.v1","collections":{"campaign":{"entries":[{"id":"w","name":"Watchtower","category":"place","aliases":["The Tower"]}]}}}`), + }}} index := source.NewDocumentIndex(req.Source) req.SourceIndex = &index - result, err := runExtraction(context.Background(), newEnemyExtractor(t, client), req) + result, err := runExtraction(context.Background(), newEnemyExtractor(t, client, req.References), req) if err != nil { t.Fatal(err) } @@ -50,6 +58,15 @@ func TestExtractMapsEnemyEventsInSourceOrder(t *testing.T) { t.Fatalf("LLM calls = %d, want 1", len(client.requests)) } request := client.requests[0] + var prompt strings.Builder + for _, message := range request.Prompt.Messages { + prompt.WriteString(message.Content) + } + for _, name := range []string{"Aria", "Star", "Alice", "Watchtower", "The Tower"} { + if !strings.Contains(prompt.String(), name) { + t.Fatalf("prepared context omitted %q", name) + } + } if request.Prompt.SessionID != "session-123" { t.Fatal("session selection lost") } diff --git a/internal/modules/dnd/extract/itemoccurrences/context_input_test.go b/internal/modules/dnd/extract/itemoccurrences/context_input_test.go new file mode 100644 index 00000000..c1aa1878 --- /dev/null +++ b/internal/modules/dnd/extract/itemoccurrences/context_input_test.go @@ -0,0 +1,16 @@ +package itemoccurrences + +import ( + "testing" + + "gitea.maximumdirect.net/eric/notarius/internal/framework/contracts" +) + +func TestConstructionRejectsInvalidSharedReference(t *testing.T) { + refs := contracts.ReferenceSet{Slots: map[string]contracts.ResolvedReferenceSlot{ + "glossary": {Items: []contracts.ReferenceItem{{MediaType: "application/json", Content: []byte("{}")}}}, + }} + if _, err := New(Options{}, refs); err == nil { + t.Fatal("invalid glossary accepted during construction") + } +} diff --git a/internal/modules/dnd/extract/itemoccurrences/extractor.go b/internal/modules/dnd/extract/itemoccurrences/extractor.go index 9fa07b60..c7642b20 100644 --- a/internal/modules/dnd/extract/itemoccurrences/extractor.go +++ b/internal/modules/dnd/extract/itemoccurrences/extractor.go @@ -61,6 +61,7 @@ var _ contracts.Extractor[dnd.ItemOccurrenceList] = (*Extractor)(nil) type Options struct{} type Extractor struct { + context shared.PreparedContext itemResolver *itemregistry.Resolver } @@ -72,11 +73,15 @@ func New(_ Options, references ...contracts.ReferenceSet) (*Extractor, error) { if len(references) == 1 { referenceSet = references[0] } + context, err := shared.PrepareContext(referenceSet) + if err != nil { + return nil, extractorErrorf("prepare references: %w", err) + } itemResolver, err := itemregistry.NewResolver(referenceSet) if err != nil { return nil, extractorErrorf("prepare item registry prompt input: %w", err) } - return &Extractor{itemResolver: itemResolver}, nil + return &Extractor{context: context, itemResolver: itemResolver}, nil } func (e *Extractor) Key() string { return Key } @@ -102,7 +107,7 @@ func (e *Extractor) PrepareExtraction(ctx context.Context, req contracts.Extract } sourceID := req.Source.ID - materials := shared.PromptInputs(sourceInput, req.References) + materials := e.context.Inputs(sourceInput) materials[ItemRegistryReferenceSlot] = registry.PromptInput() inputs := make(map[string]promptkit.ArtifactRef, len(materials)) for name, material := range materials { diff --git a/internal/modules/dnd/extract/locationoccurrences/context_input_test.go b/internal/modules/dnd/extract/locationoccurrences/context_input_test.go new file mode 100644 index 00000000..6f711a5f --- /dev/null +++ b/internal/modules/dnd/extract/locationoccurrences/context_input_test.go @@ -0,0 +1,16 @@ +package locationoccurrences + +import ( + "testing" + + "gitea.maximumdirect.net/eric/notarius/internal/framework/contracts" +) + +func TestConstructionRejectsInvalidSharedReference(t *testing.T) { + refs := contracts.ReferenceSet{Slots: map[string]contracts.ResolvedReferenceSlot{ + "glossary": {Items: []contracts.ReferenceItem{{MediaType: "application/json", Content: []byte("{}")}}}, + }} + if _, err := New(Options{}, refs); err == nil { + t.Fatal("invalid glossary accepted during construction") + } +} diff --git a/internal/modules/dnd/extract/locationoccurrences/extractor.go b/internal/modules/dnd/extract/locationoccurrences/extractor.go index a22cba4a..a118aa84 100644 --- a/internal/modules/dnd/extract/locationoccurrences/extractor.go +++ b/internal/modules/dnd/extract/locationoccurrences/extractor.go @@ -56,6 +56,7 @@ var _ contracts.Extractor[dnd.LocationOccurrenceList] = (*Extractor)(nil) type Options struct{} type Extractor struct { + context shared.PreparedContext locationResolver *locationregistry.Resolver } @@ -67,11 +68,15 @@ func New(_ Options, references ...contracts.ReferenceSet) (*Extractor, error) { if len(references) == 1 { referenceSet = references[0] } + context, err := shared.PrepareContext(referenceSet) + if err != nil { + return nil, extractorErrorf("prepare references: %w", err) + } locationResolver, err := locationregistry.NewResolver(referenceSet) if err != nil { return nil, extractorErrorf("prepare location registry prompt input: %w", err) } - return &Extractor{locationResolver: locationResolver}, nil + return &Extractor{context: context, locationResolver: locationResolver}, nil } func (e *Extractor) Key() string { return Key } @@ -106,7 +111,7 @@ func (e *Extractor) PrepareExtraction(ctx context.Context, req contracts.Extract order := shared.NewSourceRefOrderForSource(req.Source, index) sourceID := req.Source.ID - materials := shared.PromptInputs(sourceInput, req.References) + materials := e.context.Inputs(sourceInput) materials[LocationRegistryReferenceSlot] = grounding.PromptInput() inputs := make(map[string]promptkit.ArtifactRef, len(materials)) for name, material := range materials { diff --git a/internal/modules/dnd/extract/npcoccurrences/context_input_test.go b/internal/modules/dnd/extract/npcoccurrences/context_input_test.go new file mode 100644 index 00000000..d16f92af --- /dev/null +++ b/internal/modules/dnd/extract/npcoccurrences/context_input_test.go @@ -0,0 +1,16 @@ +package npcoccurrences + +import ( + "testing" + + "gitea.maximumdirect.net/eric/notarius/internal/framework/contracts" +) + +func TestConstructionRejectsInvalidSharedReference(t *testing.T) { + refs := contracts.ReferenceSet{Slots: map[string]contracts.ResolvedReferenceSlot{ + "glossary": {Items: []contracts.ReferenceItem{{MediaType: "application/json", Content: []byte("{}")}}}, + }} + if _, err := New(Options{}, refs); err == nil { + t.Fatal("invalid glossary accepted during construction") + } +} diff --git a/internal/modules/dnd/extract/npcoccurrences/extractor.go b/internal/modules/dnd/extract/npcoccurrences/extractor.go index e3ab7b54..8cce7704 100644 --- a/internal/modules/dnd/extract/npcoccurrences/extractor.go +++ b/internal/modules/dnd/extract/npcoccurrences/extractor.go @@ -60,6 +60,7 @@ var _ contracts.Extractor[dnd.NPCOccurrenceList] = (*Extractor)(nil) type Options struct{} type Extractor struct { + context shared.PreparedContext npcResolver *npcregistry.Resolver } @@ -71,11 +72,15 @@ func New(_ Options, references ...contracts.ReferenceSet) (*Extractor, error) { if len(references) == 1 { referenceSet = references[0] } + context, err := shared.PrepareContext(referenceSet) + if err != nil { + return nil, extractorErrorf("prepare references: %w", err) + } npcResolver, err := npcregistry.NewResolver(referenceSet) if err != nil { return nil, extractorErrorf("prepare NPC registry prompt input: %w", err) } - return &Extractor{npcResolver: npcResolver}, nil + return &Extractor{context: context, npcResolver: npcResolver}, nil } func (e *Extractor) Key() string { return Key } @@ -100,7 +105,7 @@ func (e *Extractor) PrepareExtraction(ctx context.Context, req contracts.Extract } sourceID := req.Source.ID - materials := shared.PromptInputs(sourceInput, req.References) + materials := e.context.Inputs(sourceInput) materials[NPCRegistryReferenceSlot] = npcRegistry.PromptInput() inputs := make(map[string]promptkit.ArtifactRef, len(materials)) for name, material := range materials { diff --git a/internal/modules/dnd/extract/spells/context_input_test.go b/internal/modules/dnd/extract/spells/context_input_test.go new file mode 100644 index 00000000..b89f1da8 --- /dev/null +++ b/internal/modules/dnd/extract/spells/context_input_test.go @@ -0,0 +1,16 @@ +package spells + +import ( + "testing" + + "gitea.maximumdirect.net/eric/notarius/internal/framework/contracts" +) + +func TestConstructionRejectsInvalidSharedReference(t *testing.T) { + refs := contracts.ReferenceSet{Slots: map[string]contracts.ResolvedReferenceSlot{ + "glossary": {Items: []contracts.ReferenceItem{{MediaType: "application/json", Content: []byte("{}")}}}, + }} + if _, err := New(Options{}, refs); err == nil { + t.Fatal("invalid glossary accepted during construction") + } +} diff --git a/internal/modules/dnd/extract/spells/extractor.go b/internal/modules/dnd/extract/spells/extractor.go index b24e6bd2..c1e0d7db 100644 --- a/internal/modules/dnd/extract/spells/extractor.go +++ b/internal/modules/dnd/extract/spells/extractor.go @@ -65,6 +65,7 @@ var _ contracts.Extractor[dnd.SpellList] = (*Extractor)(nil) type Options struct{} type Extractor struct { + context shared.PreparedContext catalogPromptInput []byte npcResolver *npcregistry.Resolver } @@ -77,6 +78,10 @@ func New(_ Options, references ...contracts.ReferenceSet) (*Extractor, error) { if len(references) == 1 { referenceSet = references[0] } + context, err := shared.PrepareContext(referenceSet) + if err != nil { + return nil, extractorErrorf("prepare references: %w", err) + } effectiveCatalog, err := spellcatalog.ResolveEffectiveCatalog(referenceSet) if err != nil { return nil, extractorErrorf("resolve effective spell catalog: %w", err) @@ -89,7 +94,7 @@ func New(_ Options, references ...contracts.ReferenceSet) (*Extractor, error) { if err != nil { return nil, extractorErrorf("prepare NPC registry prompt input: %w", err) } - return &Extractor{catalogPromptInput: catalogPromptInput, npcResolver: npcResolver}, nil + return &Extractor{context: context, catalogPromptInput: catalogPromptInput, npcResolver: npcResolver}, nil } func (e *Extractor) Key() string { @@ -115,7 +120,7 @@ func (e *Extractor) PrepareExtraction(ctx context.Context, req contracts.Extract return nil, extractorErrorf("resolve NPC registry: %w", err) } - materials := shared.PromptInputs(sourceInput, req.References) + materials := e.context.Inputs(sourceInput) inputs := make(map[string]promptkit.ArtifactRef, len(materials)+2) for name, material := range materials { inputs[name] = promptkit.InlineWithURI(material.OriginURI, string(material.Content)) diff --git a/internal/modules/dnd/extract/spells/extractor_test.go b/internal/modules/dnd/extract/spells/extractor_test.go index 14f6276e..9d662bfd 100644 --- a/internal/modules/dnd/extract/spells/extractor_test.go +++ b/internal/modules/dnd/extract/spells/extractor_test.go @@ -12,7 +12,6 @@ import ( "gitea.maximumdirect.net/eric/notarius/internal/core/source" "gitea.maximumdirect.net/eric/notarius/internal/framework/contracts" "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd" - "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/shared" spellcatalog "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/spells/catalog" "go.maximumdirect.net/promptkit" ) @@ -177,33 +176,21 @@ func TestExtractPassesReferencesAsPromptInputs(t *testing.T) { req := extractionRequest() req.References = contracts.ReferenceSet{Slots: map[string]contracts.ResolvedReferenceSlot{ "players": {Slot: contracts.ReferenceSlot{Name: "players"}, Items: []contracts.ReferenceItem{{SlotName: "players", Content: []byte("Alice: Aria Brightmantle")}}}, - "party": {Slot: contracts.ReferenceSlot{Name: "party"}, Items: []contracts.ReferenceItem{{SlotName: "party", Content: []byte("Aria Brightmantle: party cleric")}}}, - "glossary": {Slot: contracts.ReferenceSlot{Name: "glossary"}, Items: []contracts.ReferenceItem{{SlotName: "glossary", Content: []byte("Brightmantle: local temple name")}}}, + "party": {Slot: contracts.ReferenceSlot{Name: "party"}, Items: []contracts.ReferenceItem{{SlotName: "party", MediaType: "application/json", Content: []byte(`{"schema_version":"maximumdirect.rpg.party.v1","characters":{"a":{"character":{"name":"Aria Brightmantle","classes":[{"name":"cleric"}]},"player":{"name":"Alice"}}}}`)}}}, + "glossary": {Slot: contracts.ReferenceSlot{Name: "glossary"}, Items: []contracts.ReferenceItem{{SlotName: "glossary", MediaType: "application/json", Content: []byte(`{"schema_version":"maximumdirect.glossary.v1","collections":{"campaign":{"entries":[{"id":"b","name":"Brightmantle","category":"place","description":"local temple name"}]}}}`)}}}, }} - if _, err := runExtraction(context.Background(), newExtractor(t, client), req); err != nil { + if _, err := runExtraction(context.Background(), newExtractor(t, client, req.References), req); err != nil { t.Fatalf("Extract() error = %v, want nil", err) } text := promptText(client.requests[0]) - for _, content := range []string{"Alice: Aria Brightmantle", "Aria Brightmantle: party cleric", "Brightmantle: local temple name", string(req.Chunk.Content)} { + for _, content := range []string{"Alice: Aria Brightmantle", "cleric", "local temple name", string(req.Chunk.Content)} { if !strings.Contains(text, content) { t.Fatalf("missing fixed input %q", content) } } } -func TestPromptInputsMapLegacyRosterReferenceToParty(t *testing.T) { - inputs := shared.PromptInputs(spellSourceInput(), contracts.ReferenceSet{Slots: map[string]contracts.ResolvedReferenceSlot{ - "roster": {Slot: contracts.ReferenceSlot{Name: "roster"}, Items: []contracts.ReferenceItem{{SlotName: "roster", Content: []byte("Legacy roster text")}}}, - }}) - if got := string(inputs["party"].Content); got != "Legacy roster text" { - t.Fatalf("party input = %q, want legacy roster content", got) - } - if _, ok := inputs["roster"]; ok { - t.Fatal("roster prompt input was present; want only party input") - } -} - func TestExtractPreservesValidEmptyCollection(t *testing.T) { result, err := runExtraction(context.Background(), newExtractor(t, &fakeSpellsLLMClient{content: []byte(`{"spell_casts":[]}`)}), extractionRequest()) if err != nil || result.Value.SpellCasts == nil || len(result.Value.SpellCasts) != 0 { diff --git a/internal/modules/dnd/extract/spells/registry_test.go b/internal/modules/dnd/extract/spells/registry_test.go index b148cb00..0b00b9b0 100644 --- a/internal/modules/dnd/extract/spells/registry_test.go +++ b/internal/modules/dnd/extract/spells/registry_test.go @@ -36,7 +36,7 @@ func TestModuleSpec(t *testing.T) { { Name: "glossary", Description: "Optional campaign glossary reference material used only for disambiguation.", - AcceptedMediaTypes: []string{"application/json", "application/x-yaml", "application/yaml", "text/markdown", "text/plain"}, + AcceptedMediaTypes: []string{"application/json", "application/x-yaml", "application/yaml"}, }, { Name: NPCRegistryReferenceSlot, @@ -48,7 +48,7 @@ func TestModuleSpec(t *testing.T) { { Name: "party", Description: "Optional party roster reference material used only for disambiguation.", - AcceptedMediaTypes: []string{"application/json", "application/x-yaml", "application/yaml", "text/markdown", "text/plain"}, + AcceptedMediaTypes: []string{"application/json", "application/x-yaml", "application/yaml"}, }, { Name: "players", @@ -58,7 +58,7 @@ func TestModuleSpec(t *testing.T) { { Name: "roster", Description: "Deprecated alias for party roster reference material used only for disambiguation.", - AcceptedMediaTypes: []string{"application/json", "application/x-yaml", "application/yaml", "text/markdown", "text/plain"}, + AcceptedMediaTypes: []string{"application/json", "application/x-yaml", "application/yaml"}, }, { Name: "spell_catalog", diff --git a/internal/modules/dnd/shared/prompt_inputs.go b/internal/modules/dnd/shared/prompt_inputs.go index 674dc8f3..0668f2e4 100644 --- a/internal/modules/dnd/shared/prompt_inputs.go +++ b/internal/modules/dnd/shared/prompt_inputs.go @@ -8,19 +8,6 @@ import ( "gitea.maximumdirect.net/eric/notarius/internal/framework/contracts" ) -func PromptInputs(sourceInput contracts.LLMInputMaterial, references contracts.ReferenceSet) contracts.LLMInputSet { - partySlot := references.Slots["party"] - if len(partySlot.Items) == 0 { - partySlot = references.Slots["roster"] - } - return contracts.LLMInputSet{ - "transcript": TranscriptPromptMaterial(sourceInput), - "players": ReferencePromptMaterial("players", references.Slots["players"]), - "party": ReferencePromptMaterial("party", partySlot), - "glossary": ReferencePromptMaterial("glossary", references.Slots["glossary"]), - } -} - func TranscriptPromptMaterial(material contracts.LLMInputMaterial) contracts.LLMInputMaterial { out := material.Clone() out.Name = "transcript" diff --git a/internal/modules/dnd/shared/prompt_inputs_test.go b/internal/modules/dnd/shared/prompt_inputs_test.go index fe9564a4..585de42b 100644 --- a/internal/modules/dnd/shared/prompt_inputs_test.go +++ b/internal/modules/dnd/shared/prompt_inputs_test.go @@ -8,103 +8,6 @@ import ( "gitea.maximumdirect.net/eric/notarius/internal/framework/contracts" ) -func TestPromptInputsIdentity(t *testing.T) { - source := contracts.NewLLMInputMaterial("source", "application/json", []byte("source text"), "sha256:source", "file:///source.json") - baseReferences := contracts.ReferenceSet{Slots: map[string]contracts.ResolvedReferenceSlot{ - "players": slotWithContent("players", "Alice: Aria"), - "party": slotWithContent("party", "Aria: cleric"), - "glossary": slotWithContent("glossary", "Brightmantle: temple"), - }} - orderedReferences := contracts.ReferenceSet{Slots: map[string]contracts.ResolvedReferenceSlot{ - "players": {Items: []contracts.ReferenceItem{ - referenceItem("players", "file:///b.txt", "sha256:bbb", "second"), - referenceItem("players", "file:///a.txt", "sha256:aaa", "first"), - }}, - }} - reversedReferences := contracts.ReferenceSet{Slots: map[string]contracts.ResolvedReferenceSlot{ - "players": {Items: []contracts.ReferenceItem{ - referenceItem("players", "file:///a.txt", "sha256:aaa", "first"), - referenceItem("players", "file:///b.txt", "sha256:bbb", "second"), - }}, - }} - rosterReferences := contracts.ReferenceSet{Slots: map[string]contracts.ResolvedReferenceSlot{ - "party": {}, - "roster": slotWithContent("roster", "Legacy roster text"), - }} - explicitPartyReferences := contracts.ReferenceSet{Slots: map[string]contracts.ResolvedReferenceSlot{ - "party": slotWithContent("party", "Current party text"), - "roster": slotWithContent("roster", "Legacy roster text"), - }} - - tests := []struct { - name string - references contracts.ReferenceSet - other *contracts.ReferenceSet - want map[string]string - }{ - { - name: "identical materials produce deeply equal inputs", - references: baseReferences, - other: &baseReferences, - want: map[string]string{ - "transcript": "source text", - "players": "Alice: Aria", - "party": "Aria: cleric", - "glossary": "Brightmantle: temple", - }, - }, - { - name: "reference insertion order does not change bytes", - references: orderedReferences, - other: &reversedReferences, - want: map[string]string{"players": "Reference 1\nOrigin-URI: file:///a.txt\nDigest: sha256:aaa\n\nfirst\n\nReference 2\nOrigin-URI: file:///b.txt\nDigest: sha256:bbb\n\nsecond"}, - }, - { - name: "roster becomes canonical party input", - references: rosterReferences, - want: map[string]string{"party": "Legacy roster text"}, - }, - { - name: "explicit party wins over roster", - references: explicitPartyReferences, - want: map[string]string{"party": "Current party text"}, - }, - { - name: "missing optional slots use placeholders", - references: contracts.ReferenceSet{}, - want: map[string]string{ - "players": " ", - "party": " ", - "glossary": " ", - }, - }, - } - - for _, test := range tests { - t.Run(test.name, func(t *testing.T) { - inputs := PromptInputs(source, test.references) - if test.other != nil { - other := PromptInputs(source, *test.other) - if !reflect.DeepEqual(inputs, other) { - t.Fatalf("PromptInputs() differs for equivalent references:\nfirst=%#v\nsecond=%#v", inputs, other) - } - } - for name, want := range test.want { - got, ok := inputs[name] - if !ok { - t.Fatalf("PromptInputs() missing %q: %#v", name, inputs) - } - if got := string(got.Content); got != want { - t.Fatalf("%s content = %q, want %q", name, got, want) - } - } - if _, ok := inputs["roster"]; ok { - t.Fatalf("PromptInputs() included roster input: %#v", inputs) - } - }) - } -} - func TestTranscriptPromptMaterialClonesSource(t *testing.T) { source := contracts.NewLLMInputMaterial("source", "text/plain", []byte("source text"), "sha256:source", "file:///source.txt") got := TranscriptPromptMaterial(source) @@ -128,9 +31,9 @@ func TestReferencePromptMaterialUsesTextPlainAndSingleReferenceMetadata(t *testi Origin: contracts.ReferenceOrigin{Type: "file", URI: "file:///reference.md"}, }}} - got := ReferencePromptMaterial("party", slot) - if got.Name != "party" || got.MediaType != "text/plain" { - t.Fatalf("ReferencePromptMaterial() name/media = %q/%q, want party/text/plain", got.Name, got.MediaType) + got := ReferencePromptMaterial("players", slot) + if got.Name != "players" || got.MediaType != "text/plain" { + t.Fatalf("ReferencePromptMaterial() name/media = %q/%q, want players/text/plain", got.Name, got.MediaType) } if got.Digest != "sha256:reference" || got.OriginURI != "file:///reference.md" { t.Fatalf("ReferencePromptMaterial() digest/origin = %q/%q, want single reference metadata", got.Digest, got.OriginURI) @@ -141,7 +44,7 @@ func TestReferencePromptMaterialUsesTextPlainAndSingleReferenceMetadata(t *testi } func TestReferencePromptMaterialOmitsAggregateMetadata(t *testing.T) { - got := ReferencePromptMaterial("party", contracts.ResolvedReferenceSlot{Items: []contracts.ReferenceItem{ + got := ReferencePromptMaterial("players", contracts.ResolvedReferenceSlot{Items: []contracts.ReferenceItem{ {Content: []byte("one"), Digest: "sha256:one", Origin: contracts.ReferenceOrigin{URI: "file:///one.md"}}, {Content: []byte("two"), Digest: "sha256:two", Origin: contracts.ReferenceOrigin{URI: "file:///two.md"}}, }}) @@ -155,13 +58,13 @@ func TestReferencePromptInputRendering(t *testing.T) { if got := string(ReferencePromptInput(contracts.ResolvedReferenceSlot{})); got != " " { t.Fatalf("empty rendering = %q, want single space", got) } - if got := string(ReferencePromptInput(slotWithContent("party", "single reference"))); got != "single reference" { + if got := string(ReferencePromptInput(slotWithContent("players", "single reference"))); got != "single reference" { t.Fatalf("single rendering = %q, want raw content", got) } slot := contracts.ResolvedReferenceSlot{Items: []contracts.ReferenceItem{ { - SlotName: "party", + SlotName: "players", MediaType: "text/plain", Content: []byte("second"), Digest: "sha256:bbb", @@ -169,7 +72,7 @@ func TestReferencePromptInputRendering(t *testing.T) { SizeBytes: 6, }, { - SlotName: "party", + SlotName: "players", MediaType: "text/plain", Content: []byte("first"), Digest: "sha256:aaa", @@ -192,7 +95,7 @@ func TestReferencePromptInputRendering(t *testing.T) { func TestReferencePromptInputOrdersRenderedMetadataDeterministically(t *testing.T) { base := contracts.ReferenceItem{ - SlotName: "party", + SlotName: "players", MediaType: "text/plain", Content: []byte("same content"), Digest: "sha256:same", @@ -241,15 +144,6 @@ func slotWithContent(name string, content string) contracts.ResolvedReferenceSlo } } -func referenceItem(slotName, uri, digest, content string) contracts.ReferenceItem { - return contracts.ReferenceItem{ - SlotName: slotName, - Content: []byte(content), - Digest: digest, - Origin: contracts.ReferenceOrigin{URI: uri}, - } -} - func referenceItemWithMetadata(base contracts.ReferenceItem, originType, mediaType string, size int64) contracts.ReferenceItem { base.Origin.Type = originType base.MediaType = mediaType diff --git a/internal/modules/integration/dnd_npc_grounded_test.go b/internal/modules/integration/dnd_npc_grounded_test.go index 3a27a420..d16aa049 100644 --- a/internal/modules/integration/dnd_npc_grounded_test.go +++ b/internal/modules/integration/dnd_npc_grounded_test.go @@ -262,20 +262,20 @@ func loadGroundedPipelineConfig(t *testing.T) config.Config { func configureGroundedCampaignReferences(t *testing.T, cfg *config.Config) map[string]string { t.Helper() values := map[string]string{ - "party": "Mira Thorn: ally\n", - "glossary": "Greencloak: Mira Thorn's title\n", + "party": `{"schema_version":"maximumdirect.rpg.party.v1","characters":{"m":{"character":{"name":"Mira Thorn","aliases":["Ally"]}}}}`, + "glossary": `{"schema_version":"maximumdirect.glossary.v1","collections":{"campaign":{"entries":[{"id":"g","name":"Greencloak","category":"title","description":"Mira Thorn's title"}]}}}`, } profile := cfg.Pipelines["dnd-npc-grounded"] profile.References = make(map[string]pipeline.ReferenceSource, len(values)) for name, content := range values { - path := filepath.Join(t.TempDir(), name+".txt") + path := filepath.Join(t.TempDir(), name+".json") if err := os.WriteFile(path, []byte(content), 0o600); err != nil { t.Fatalf("write %s reference: %v", name, err) } profile.References[name] = pipeline.ExternalReference(path) } cfg.Pipelines["dnd-npc-grounded"] = profile - return values + return map[string]string{"party": "Ally", "glossary": "Greencloak"} } func runGroundedPipeline(t *testing.T, configValue config.Config, registries pipeline.Registries, client *groundedDNDLLMClient) pipeline.RunOutput { diff --git a/internal/modules/integration/dnd_spells_runner_test.go b/internal/modules/integration/dnd_spells_runner_test.go index cf7b9b97..5d8e7cf4 100644 --- a/internal/modules/integration/dnd_spells_runner_test.go +++ b/internal/modules/integration/dnd_spells_runner_test.go @@ -93,8 +93,8 @@ func TestRunnerPassesPartyAndGlossaryReferencesToDNDSpellsPrompt(t *testing.T) { raw := readDNDSpellsFixture(t) resolved := resolveDNDSpellsPipeline(t) resolved.ResolvedPipeline.Steps[0].ArtifactLanes[0].ExtractReferences.ReferenceSet = dndSpellsReferenceSet( - "Aria: party cleric\nBorin: fighter", - "Fire Bolt: evocation cantrip", + `{"schema_version":"maximumdirect.rpg.party.v1","characters":{"a":{"character":{"name":"Aria","classes":[{"name":"cleric"}]},"player":{"name":"Alice"}},"b":{"character":{"name":"Borin","classes":[{"name":"fighter"}]}}}}`, + `{"schema_version":"maximumdirect.glossary.v1","collections":{"campaign":{"entries":[{"id":"f","name":"Fire Bolt","category":"spell","description":"evocation cantrip"}]}}}`, ) llmClient := &fakeSpellsLLMClient{ response: extractionResponse{ @@ -122,7 +122,7 @@ func TestRunnerPassesPartyAndGlossaryReferencesToDNDSpellsPrompt(t *testing.T) { t.Fatalf("LLM calls = %d, want 1", len(llmClient.requests)) } request := llmClient.requests[0] - for _, value := range []string{"Aria: party cleric\nBorin: fighter", "Fire Bolt: evocation cantrip"} { + for _, value := range []string{"cleric", "fighter", "Alice", "evocation cantrip"} { if !strings.Contains(nativePromptText(request), value) { t.Fatalf("rendered prompt omitted %q", value) } @@ -197,10 +197,10 @@ func dndSpellsReferenceSet(party string, glossary string) contracts.ReferenceSet Items: []contracts.ReferenceItem{ { SlotName: "party", - MediaType: "text/plain; charset=utf-8", + MediaType: "application/json", Content: []byte(party), Digest: "sha256:party", - Origin: contracts.ReferenceOrigin{Type: "file", URI: "file:///tmp/party.txt"}, + Origin: contracts.ReferenceOrigin{Type: "file", URI: "file:///tmp/party.json"}, SizeBytes: int64(len(party)), BindingSource: contracts.ReferenceBindingSourceConfig, }, @@ -213,10 +213,10 @@ func dndSpellsReferenceSet(party string, glossary string) contracts.ReferenceSet Items: []contracts.ReferenceItem{ { SlotName: "glossary", - MediaType: "text/plain; charset=utf-8", + MediaType: "application/json", Content: []byte(glossary), Digest: "sha256:glossary", - Origin: contracts.ReferenceOrigin{Type: "file", URI: "file:///tmp/glossary.txt"}, + Origin: contracts.ReferenceOrigin{Type: "file", URI: "file:///tmp/glossary.json"}, SizeBytes: int64(len(glossary)), BindingSource: contracts.ReferenceBindingSourceConfig, }, diff --git a/internal/modules/integration/native_spell_pipeline_test.go b/internal/modules/integration/native_spell_pipeline_test.go index 873b2e80..79e29661 100644 --- a/internal/modules/integration/native_spell_pipeline_test.go +++ b/internal/modules/integration/native_spell_pipeline_test.go @@ -103,8 +103,8 @@ func TestNativeSpellPipelineAssessesEveryCandidateOnce(t *testing.T) { bindings = append(bindings, pipeline.Binding(validator.Name())) names = append(names, validator.Name()) } - partyFile := filepath.Join(t.TempDir(), "party.txt") - check(os.WriteFile(partyFile, []byte("party-grounding-sentinel: Aria"), 0600)) + partyFile := filepath.Join(t.TempDir(), "party.json") + check(os.WriteFile(partyFile, []byte(`{"schema_version":"maximumdirect.rpg.party.v1","characters":{"a":{"character":{"name":"Aria","aliases":["party-grounding-sentinel"]}}}}`), 0600)) turns := 2 profile := pipeline.PipelineProfile{ ID: "native-spells", Input: pipeline.Binding(transcript.Key),