Migrate ordered enemy event contracts to native providers

This commit is contained in:
2026-11-22 05:29:48 +00:00
parent 936fc1e72a
commit 4fa437aee8
3 changed files with 220 additions and 215 deletions

View File

@@ -1741,6 +1741,16 @@ Focused native run/state/reference tests and vet pass; repeated race checks for
the run/state and command/configuration set pass. Remaining enemy-event/example the run/state and command/configuration set pass. Remaining enemy-event/example
fixtures, dependency tidy/notices, and whole-tree compilation are still required. fixtures, dependency tidy/notices, and whole-tree compilation are still required.
The ordered multi-family CLI fixture now uses a native version 6 test composition
and provider requests, preserving contextual registry identities, independently
grounded occurrences, combat-only enemy work, source-free reference projections,
session routing, and evidence self-references/deduplication. Domain file paths and
the small native completion replace the removed output index. The deterministic
enemy codec contract still rejects an intrinsically invalid range despite explicit
validator overrides. Focused native tests, vet, and repeated race checks pass.
The maintained example configuration files are unchanged and remain assigned to
Stage 36; their remaining test API migration is still required for compilation.
**Goal:** delete the old architecture rather than leave dormant alternatives. **Goal:** delete the old architecture rather than leave dormant alternatives.
**Inspect/change:** all production/test callers of the feature roadmap's removal **Inspect/change:** all production/test callers of the feature roadmap's removal

View File

@@ -51,11 +51,10 @@ func TestAssembledEnemyEventLaneRejectsInvalidFinalArtifactDespiteValidatorOverr
t.Fatalf("Prepare() error = %v", err) t.Fatalf("Prepare() error = %v", err)
} }
_, err = pipeline.New().Run(context.Background(), pipeline.RunInput{ _, err = pipeline.New().Run(context.Background(), pipeline.RunInput{
Prepared: prepared, Prepared: prepared,
RawInput: readRepositoryFile(t, "examples", "seriatim-minimal-transcript.json"), RawInput: readRepositoryFile(t, "examples", "seriatim-minimal-transcript.json"),
ChunkCacheMode: pipeline.ChunkCacheBypass,
}) })
if err == nil || !strings.Contains(err.Error(), "serialize accepted extract output") || !strings.Contains(err.Error(), "must not exceed") { if err == nil || !strings.Contains(err.Error(), "encode dnd enemy event list") || !strings.Contains(err.Error(), "must not exceed") {
t.Fatalf("Run() error = %v, want final durable range rejection", err) t.Fatalf("Run() error = %v, want final durable range rejection", err)
} }
} }

View File

@@ -3,61 +3,43 @@ package cli
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"os"
"path/filepath" "path/filepath"
"reflect" "reflect"
"strings" "strings"
"sync" "sync"
"testing" "testing"
"time"
"gitea.maximumdirect.net/eric/notarius/internal/core/artifacts"
"gitea.maximumdirect.net/eric/notarius/internal/core/config" "gitea.maximumdirect.net/eric/notarius/internal/core/config"
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
"gitea.maximumdirect.net/eric/notarius/internal/framework/evidencecontext" "gitea.maximumdirect.net/eric/notarius/internal/framework/evidencecontext"
"gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline" "gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline"
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd" "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/chunk/scenes"
itemoccurrencecodec "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/codec/itemoccurrences"
itemregistrycodec "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/codec/itemregistry"
locationoccurrencecodec "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/codec/locationoccurrences"
locationcodec "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/codec/locationregistry"
npcoccurrencecodec "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/codec/npcoccurrences"
npccodec "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/codec/npcregistry"
combat "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/extract/combatturns"
enemyevents "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/extract/enemyevents" enemyevents "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/extract/enemyevents"
itemoccurrences "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/extract/itemoccurrences"
itemregistry "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/extract/itemregistry"
locationoccurrences "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/extract/locationoccurrences"
locations "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/extract/locationregistry"
npcoccurrences "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/extract/npcoccurrences"
npcregistry "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/extract/npcregistry"
scenedescriptions "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/extract/scenedescriptions"
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/extract/spells"
enemyeventnormalize "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/normalize/enemyevents" enemyeventnormalize "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/normalize/enemyevents"
itemregistrynormalize "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/normalize/itemregistry" "go.maximumdirect.net/promptkit"
locationnormalize "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/normalize/locationregistry"
npcnormalize "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/normalize/npcregistry"
) )
func TestProductionEnemyEventConfigurationResolvesGeneratedHandoffs(t *testing.T) { func TestProductionEnemyEventConfigurationResolvesGeneratedHandoffs(t *testing.T) {
components := productionTestComponents(t) components := productionTestComponents(t)
cfg := loadMaintainedExample(t, repositoryPath("examples", "dnd-complete.config.yml")) configPath := writeEnemyEventContractConfig(t)
effective, err := cfg.Resolve(resolveInputForMaintainedExample(components, "dnd-session")) loaded, err := config.Load(configPath, config.LoadOptions{})
if err != nil {
t.Fatal(err)
}
cfg := loaded.RuntimeConfig()
effective, err := cfg.Resolve(config.ResolveInput{PipelineID: "dnd-session", Catalog: catalogFromRegistries(components.registries)})
if err != nil { if err != nil {
t.Fatalf("Resolve() error = %v, want nil", err) t.Fatalf("Resolve() error = %v, want nil", err)
} }
materialized, _, err := pipeline.MaterializeReferences(effective.ResolvedPipeline, catalogFromRegistries(components.registries), pipeline.ReferenceMaterializationOptions{ materialized, _, err := pipeline.MaterializeReferences(effective.ResolvedPipeline, catalogFromRegistries(components.registries), pipeline.ReferenceMaterializationOptions{
ConfigPath: repositoryPath("examples", "dnd-complete.config.yml"), ConfigPath: configPath,
WorkingDir: repositoryPath("examples"), WorkingDir: filepath.Dir(configPath),
}) })
if err != nil { if err != nil {
t.Fatalf("MaterializeReferences() error = %v, want nil", err) t.Fatalf("MaterializeReferences() error = %v, want nil", err)
} }
lane := referenceContractLane(t, materialized, "enemy-events") lane := referenceContractLane(t, materialized, "enemy-events")
if lane.ArtifactKind != dnd.EnemyEventListKind || lane.Extract.Module != enemyevents.Key || lane.Extract.Retries != 2 || lane.Merge.Module != pipeline.DefaultMergeModule || lane.Normalize.Module != enemyeventnormalize.Key { if lane.ArtifactKind != dnd.EnemyEventListKind || lane.Extract.Module != enemyevents.Key || lane.Merge.Module != pipeline.DefaultMergeModule || lane.Normalize.Module != enemyeventnormalize.Key {
t.Fatalf("enemy event lane = %#v, want typed production composition", lane) t.Fatalf("enemy event lane = %#v, want typed production composition", lane)
} }
for slot, want := range map[string]struct{ step, lane string }{ for slot, want := range map[string]struct{ step, lane string }{
@@ -96,94 +78,62 @@ func TestProductionEnemyEventConfigurationResolvesGeneratedHandoffs(t *testing.T
profile := cfg.Pipelines["dnd-session"] profile := cfg.Pipelines["dnd-session"]
profile.Steps[2].References["npc_registry"] = pipeline.GeneratedReference("track-enemies", "enemy-events") profile.Steps[2].References["npc_registry"] = pipeline.GeneratedReference("track-enemies", "enemy-events")
cfg.Pipelines["dnd-session"] = profile cfg.Pipelines["dnd-session"] = profile
if _, err := cfg.Resolve(resolveInputForMaintainedExample(components, "dnd-session")); err == nil || !strings.Contains(err.Error(), "earlier step") { if _, err := cfg.Resolve(config.ResolveInput{PipelineID: "dnd-session", Catalog: catalogFromRegistries(components.registries)}); err == nil || !strings.Contains(err.Error(), "earlier step") {
t.Fatalf("Resolve() error = %v, want future generated-reference rejection", err) t.Fatalf("Resolve() error = %v, want future generated-reference rejection", err)
} }
} }
func TestMaintainedCompleteExamplePublishesRegistryBackedEntityOccurrences(t *testing.T) { func TestNativeOrderedDNDRunPublishesRegistryBackedEntityOccurrences(t *testing.T) {
t.Chdir(repositoryPath()) configPath := writeEnemyEventContractConfig(t)
outputRoot := filepath.Join(t.TempDir(), "output") outputRoot := filepath.Join(t.TempDir(), "output")
configPath := completeExampleConfigWithTemporaryCache(t)
client := &enemyEventLLMClient{} client := &enemyEventLLMClient{}
options := productionCLIOptions(t) options := productionRunOptions(t, client)
options.Now = func() time.Time { return time.Unix(1700000000, 0).UTC() }
options.RunIDGenerator = func(time.Time) (string, error) { return productionRunID, nil }
options.UserCacheDir = func() (string, error) { return "", errors.New("user cache must not be used") }
options.LLMClientFactory = func(context.Context, config.Config, string, LLMRuntimeOverrides) (contracts.StructuredLLMClient, []artifacts.LLMProfileManifest, error) {
return client, nil, nil
}
var stdout, stderr strings.Builder var stdout, stderr strings.Builder
code := RunWithOptions([]string{ code := RunWithOptions([]string{
"run", "dnd-session", "run", "dnd-session", "--config", configPath,
"--config", configPath, "--input-file", repositoryPath("examples", "dnd-complete-transcript.json"),
"--input", repositoryPath("examples", "dnd-complete-transcript.json"), "--cache", "bypass", "--output-dir", outputRoot,
"--chunk_cache", "bypass", "--output-dir", outputRoot, "--session-id", "enemy-event-session", "--llm-session-id", "enemy-event-session", "--format", "json",
}, &stdout, &stderr, options) }, &stdout, &stderr, options)
if code != 0 { if code != 0 {
t.Fatalf("code=%d stdout=%q stderr=%q", code, stdout.String(), stderr.String()) t.Fatalf("code=%d stdout=%q stderr=%q", code, stdout.String(), stderr.String())
} }
var report runResult
if err := json.Unmarshal([]byte(stdout.String()), &report); err != nil {
t.Fatal(err)
}
runRoot := filepath.Join(outputRoot, productionRunID) runRoot := filepath.Join(outputRoot, productionRunID)
index := readProductionJSON[exampleOutputIndex](t, filepath.Join(runRoot, "index.json")) if report.OutputDirectory != runRoot || report.ArtifactCount != 12 || report.FallbackCount != 0 {
outputs := make(map[string]exampleOutputIndexEntry) t.Fatalf("completion=%#v, want ten domain lanes and two auxiliary artifacts without fallback", report)
for _, entry := range index.OutputFiles {
outputs[entry.LaneID] = entry
} }
enemyOutput := outputs["enemy-events"] if files := readTree(t, runRoot); len(files) != report.ArtifactCount {
locationOutput := outputs["location-registry"] t.Fatalf("published files=%v, report=%#v", files, report)
locationOccurrenceOutput := outputs["location-occurrences"]
npcRegistryOutput := outputs["npc-registry"]
npcOccurrenceOutput := outputs["npc-occurrences"]
itemRegistryOutput := outputs["item-registry"]
itemOccurrenceOutput := outputs["item-occurrences"]
if enemyOutput.File != "lanes/enemy-events.json" || enemyOutput.SchemaID != "notarius.dnd.enemy_events" || enemyOutput.SchemaVersion != "v1" {
t.Fatalf("enemy event output = %#v, want typed enemy-event JSON", enemyOutput)
} }
value := readProductionJSON[dnd.EnemyEventList](t, filepath.Join(runRoot, enemyOutput.File)) value := readProductionJSON[dnd.EnemyEventList](t, filepath.Join(runRoot, "lanes/enemy-events.json"))
if len(value.Events) != 1 || value.Events[0].Name != "Kesh" || value.Events[0].Kind != dnd.EnemyEventKindFled || len(value.Events[0].SourceRefs) != 1 || value.Events[0].SourceRefs[0].SourceID != "session-ravenfall" || value.Events[0].SourceRefs[0].StartUnitID != 10 { if len(value.Events) != 1 || value.Events[0].Name != "Kesh" || value.Events[0].Kind != dnd.EnemyEventKindFled || len(value.Events[0].SourceRefs) != 1 || value.Events[0].SourceRefs[0].SourceID != "session-ravenfall" || value.Events[0].SourceRefs[0].StartUnitID != 10 {
t.Fatalf("enemy event artifact = %#v, want source-linked Kesh fleeing event", value) t.Fatalf("enemy event artifact = %#v, want source-linked Kesh fleeing event", value)
} }
if locationOutput.File != "lanes/location-registry.json" || locationOutput.SchemaID != locationcodec.SchemaID || locationOutput.SchemaVersion != locationcodec.SchemaVersion { locationsValue := readProductionJSON[dnd.LocationRegistry](t, filepath.Join(runRoot, "lanes/location-registry.json"))
t.Fatalf("location output = %#v, want typed location registry JSON", locationOutput)
}
locationsValue := readProductionJSON[dnd.LocationRegistry](t, filepath.Join(runRoot, locationOutput.File))
if len(locationsValue.Locations) != 2 || locationsValue.Locations[0].Name != "Moon Gate" || locationsValue.Locations[1].Name != "Moon Gate" || locationsValue.Locations[0].ID == locationsValue.Locations[1].ID { if len(locationsValue.Locations) != 2 || locationsValue.Locations[0].Name != "Moon Gate" || locationsValue.Locations[1].Name != "Moon Gate" || locationsValue.Locations[0].ID == locationsValue.Locations[1].ID {
t.Fatalf("location registry = %#v, want distinct source-grounded identities for same-name locations", locationsValue) t.Fatalf("location registry = %#v, want distinct source-grounded identities for same-name locations", locationsValue)
} }
if locationOccurrenceOutput.File != "lanes/location-occurrences.json" || locationOccurrenceOutput.SchemaID != locationoccurrencecodec.SchemaID || locationOccurrenceOutput.SchemaVersion != locationoccurrencecodec.SchemaVersion { occurrencesValue := readProductionJSON[dnd.LocationOccurrenceList](t, filepath.Join(runRoot, "lanes/location-occurrences.json"))
t.Fatalf("location occurrence output = %#v, want typed occurrence JSON", locationOccurrenceOutput)
}
occurrencesValue := readProductionJSON[dnd.LocationOccurrenceList](t, filepath.Join(runRoot, locationOccurrenceOutput.File))
if len(occurrencesValue.Occurrences) != 2 || occurrencesValue.Occurrences[0].LocationID == occurrencesValue.Occurrences[1].LocationID || occurrencesValue.Occurrences[0].Name != "Moon Gate" || occurrencesValue.Occurrences[1].Name != "Moon Gate" { if len(occurrencesValue.Occurrences) != 2 || occurrencesValue.Occurrences[0].LocationID == occurrencesValue.Occurrences[1].LocationID || occurrencesValue.Occurrences[0].Name != "Moon Gate" || occurrencesValue.Occurrences[1].Name != "Moon Gate" {
t.Fatalf("location occurrences = %#v, want source-grounded references to distinct registry identities", occurrencesValue) t.Fatalf("location occurrences = %#v, want source-grounded references to distinct registry identities", occurrencesValue)
} }
if npcRegistryOutput.File != "lanes/npc-registry.json" || npcRegistryOutput.SchemaID != npccodec.SchemaID || npcRegistryOutput.SchemaVersion != npccodec.SchemaVersion { npcRegistry := readProductionJSON[dnd.NPCRegistry](t, filepath.Join(runRoot, "lanes/npc-registry.json"))
t.Fatalf("NPC registry output = %#v, want typed registry JSON", npcRegistryOutput)
}
npcRegistry := readProductionJSON[dnd.NPCRegistry](t, filepath.Join(runRoot, npcRegistryOutput.File))
if len(npcRegistry.NPCs) != 1 || npcRegistry.NPCs[0].Name != "Kesh" { if len(npcRegistry.NPCs) != 1 || npcRegistry.NPCs[0].Name != "Kesh" {
t.Fatalf("NPC registry = %#v, want Kesh identity", npcRegistry) t.Fatalf("NPC registry = %#v, want Kesh identity", npcRegistry)
} }
if npcOccurrenceOutput.File != "lanes/npc-occurrences.json" || npcOccurrenceOutput.SchemaID != npcoccurrencecodec.SchemaID || npcOccurrenceOutput.SchemaVersion != npcoccurrencecodec.SchemaVersion { npcOccurrences := readProductionJSON[dnd.NPCOccurrenceList](t, filepath.Join(runRoot, "lanes/npc-occurrences.json"))
t.Fatalf("NPC occurrence output = %#v, want typed occurrence JSON", npcOccurrenceOutput)
}
npcOccurrences := readProductionJSON[dnd.NPCOccurrenceList](t, filepath.Join(runRoot, npcOccurrenceOutput.File))
if len(npcOccurrences.Occurrences) != 1 || npcOccurrences.Occurrences[0].NPCID != npcRegistry.NPCs[0].ID || npcOccurrences.Occurrences[0].Name != "Kesh" || len(npcOccurrences.Occurrences[0].SourceRefs) != 1 || npcOccurrences.Occurrences[0].SourceRefs[0].StartUnitID != 7 { if len(npcOccurrences.Occurrences) != 1 || npcOccurrences.Occurrences[0].NPCID != npcRegistry.NPCs[0].ID || npcOccurrences.Occurrences[0].Name != "Kesh" || len(npcOccurrences.Occurrences[0].SourceRefs) != 1 || npcOccurrences.Occurrences[0].SourceRefs[0].StartUnitID != 7 {
t.Fatalf("NPC occurrences = %#v, want independently evidenced Kesh registry grounding", npcOccurrences) t.Fatalf("NPC occurrences = %#v, want independently evidenced Kesh registry grounding", npcOccurrences)
} }
if itemRegistryOutput.File != "lanes/item-registry.json" || itemRegistryOutput.SchemaID != itemregistrycodec.SchemaID || itemRegistryOutput.SchemaVersion != itemregistrycodec.SchemaVersion { itemRegistry := readProductionJSON[dnd.ItemRegistry](t, filepath.Join(runRoot, "lanes/item-registry.json"))
t.Fatalf("item registry output = %#v, want typed registry JSON", itemRegistryOutput)
}
itemRegistry := readProductionJSON[dnd.ItemRegistry](t, filepath.Join(runRoot, itemRegistryOutput.File))
if len(itemRegistry.Items) != 1 || itemRegistry.Items[0].Name != "Moonblade" { if len(itemRegistry.Items) != 1 || itemRegistry.Items[0].Name != "Moonblade" {
t.Fatalf("item registry = %#v, want Moonblade identity", itemRegistry) t.Fatalf("item registry = %#v, want Moonblade identity", itemRegistry)
} }
if itemOccurrenceOutput.File != "lanes/item-occurrences.json" || itemOccurrenceOutput.SchemaID != itemoccurrencecodec.SchemaID || itemOccurrenceOutput.SchemaVersion != itemoccurrencecodec.SchemaVersion { itemOccurrences := readProductionJSON[dnd.ItemOccurrenceList](t, filepath.Join(runRoot, "lanes/item-occurrences.json"))
t.Fatalf("item occurrence output = %#v, want typed occurrence JSON", itemOccurrenceOutput)
}
itemOccurrences := readProductionJSON[dnd.ItemOccurrenceList](t, filepath.Join(runRoot, itemOccurrenceOutput.File))
if len(itemOccurrences.Occurrences) != 1 || itemOccurrences.Occurrences[0].ItemID != itemRegistry.Items[0].ID || itemOccurrences.Occurrences[0].Name != "Moonblade" || len(itemOccurrences.Occurrences[0].SourceRefs) != 1 || itemOccurrences.Occurrences[0].SourceRefs[0].StartUnitID != 5 { if len(itemOccurrences.Occurrences) != 1 || itemOccurrences.Occurrences[0].ItemID != itemRegistry.Items[0].ID || itemOccurrences.Occurrences[0].Name != "Moonblade" || len(itemOccurrences.Occurrences[0].SourceRefs) != 1 || itemOccurrences.Occurrences[0].SourceRefs[0].StartUnitID != 5 {
t.Fatalf("item occurrences = %#v, want independently evidenced Moonblade registry grounding", itemOccurrences) t.Fatalf("item occurrences = %#v, want independently evidenced Moonblade registry grounding", itemOccurrences)
} }
@@ -203,150 +153,169 @@ func TestMaintainedCompleteExamplePublishesRegistryBackedEntityOccurrences(t *te
seenEvidenceUnits[unit.ID] = struct{}{} seenEvidenceUnits[unit.ID] = struct{}{}
} }
requests := client.requestsFor(enemyevents.PromptID) requests := client.requestsFor("events")
if len(requests) != 1 { if len(requests) != 1 {
t.Fatalf("enemy event requests = %#v, want only the combat scene request", requests) t.Fatalf("enemy event requests=%d, want only the combat scene", len(requests))
} }
request := requests[0] request := requests[0]
if request.SessionID != "enemy-event-session" { if request.Prompt.SessionID != "enemy-event-session" {
t.Fatalf("enemy event session = %q, want shared session", request.SessionID) t.Fatalf("session=%q", request.Prompt.SessionID)
} }
for slot, required := range map[string]string{ for _, field := range []string{"npcs", "combat_turns", "npc_occurrences"} {
"npc_registry": "Kesh", input, err := productionJSONInput(request, field)
"combat_turns": "Kesh", if err != nil || !strings.Contains(string(input), "Kesh") || strings.Contains(string(input), "source_refs") || strings.Contains(string(input), "start_unit_id") {
"npc_occurrences": "Kesh", t.Fatalf("%s projection=%s error=%v, want compact source-free grounding", field, input, err)
} {
input, ok := request.Inputs[slot]
if !ok || !strings.Contains(string(input.Content), required) || strings.Contains(string(input.Content), "source_refs") || strings.Contains(string(input.Content), "start_unit_id") {
t.Fatalf("enemy event %s prompt input = %q, want compact source-free grounding", slot, input.Content)
} }
} }
locationRequests := client.requestsFor(locationoccurrences.PromptID) for _, check := range []struct {
if len(locationRequests) != 2 { role, field, name string
t.Fatalf("location occurrence requests = %#v, want one request per scene", locationRequests) contextual bool
}
for _, request := range locationRequests {
registryInput := request.Inputs["location_registry"]
if !strings.Contains(string(registryInput.Content), "Moon Gate") || !strings.Contains(string(registryInput.Content), "registry_refs") || strings.Contains(string(registryInput.Content), `"id"`) || strings.Contains(string(registryInput.Content), "source_refs") {
t.Fatalf("location occurrence registry input = %q, want contextual selector grounding", registryInput.Content)
}
}
for _, test := range []struct {
promptID string
slot string
name string
requiresIDs bool
}{ }{
{promptID: npcoccurrences.PromptID, slot: "npc_registry", name: "Kesh"}, {"location-occurrences", "locations", "Moon Gate", true},
{promptID: itemoccurrences.PromptID, slot: "item_registry", name: "Moonblade"}, {"npc-occurrences", "npcs", "Kesh", false},
{"item-occurrences", "items", "Moonblade", false},
} { } {
requests := client.requestsFor(test.promptID) requests := client.requestsFor(check.role)
if len(requests) != 2 { if len(requests) != 2 {
t.Fatalf("%s requests = %#v, want one request per scene", test.promptID, requests) t.Fatalf("%s requests=%d, want one per scene", check.role, len(requests))
} }
for _, request := range requests { for _, request := range requests {
registryInput := request.Inputs[test.slot] input, err := productionJSONInput(request, check.field)
hasID := strings.Contains(string(registryInput.Content), `"id"`) if err != nil || !strings.Contains(string(input), check.name) || strings.Contains(string(input), `"id"`) || strings.Contains(string(input), "source_refs") || strings.Contains(string(input), "registry_refs") != check.contextual {
if !strings.Contains(string(registryInput.Content), test.name) || hasID != test.requiresIDs || strings.Contains(string(registryInput.Content), "source_refs") { t.Fatalf("%s projection=%s error=%v", check.role, input, err)
t.Fatalf("%s registry input = %q, want source-free configured grounding", test.promptID, registryInput.Content)
} }
} }
} }
} }
func completeExampleConfigWithTemporaryCache(t *testing.T) string { func writeEnemyEventContractConfig(t *testing.T) string {
t.Helper() t.Helper()
content, err := os.ReadFile(repositoryPath("examples", "dnd-complete.config.yml")) return writeProductionContractConfig(t, fmt.Sprintf(`version: 6
if err != nil { pipelines:
t.Fatal(err) dnd-session:
} cache: bypass
cacheRoot := t.TempDir() input: {module: seriatim}
updated := strings.Replace(string(content), "directory: ./notarius-cache/chunk-plans", fmt.Sprintf("directory: %q", filepath.Join(cacheRoot, "chunk-plans")), 1) chunk: {module: dnd/scenes}
updated = strings.Replace(updated, "directory: ./notarius-cache/checkpoints", fmt.Sprintf("directory: %q", filepath.Join(cacheRoot, "checkpoints")), 1) references:
for relative, absolute := range map[string]string{ party: {path: %q}
"./llm-profiles/dnd-extraction.yml": repositoryPath("examples", "llm-profiles", "dnd-extraction.yml"), glossary: {path: %q}
} { output:
updated = strings.ReplaceAll(updated, relative, fmt.Sprintf("%q", absolute)) module: json
} options:
root := t.TempDir() include_chunk_map: true
fragment, err := os.ReadFile(repositoryPath("examples", "pipelines", "dnd-session.yml")) evidence_context:
if err != nil { enabled: true
t.Fatal(err) window_units: 3
} lanes: [item-occurrences, item-registry, location-registry, location-occurrences, npc-registry, spells, combat-turns, npc-occurrences, enemy-events]
for relative, absolute := range map[string]string{ steps:
"../dnd-party.txt": repositoryPath("examples", "dnd-party.txt"), - id: describe-session
"../dnd-glossary.txt": repositoryPath("examples", "dnd-glossary.txt"), artifacts:
"../dnd-spell-catalog.json": repositoryPath("examples", "dnd-spell-catalog.json"), item-registry:
} { extract: {module: dnd/item-registry}
fragment = []byte(strings.ReplaceAll(string(fragment), relative, fmt.Sprintf("%q", absolute))) normalize: {module: dnd/item-registry}
} npc-registry:
if err := os.MkdirAll(filepath.Join(root, "pipelines"), 0o700); err != nil { extract: {module: dnd/npc-registry}
t.Fatal(err) normalize: {module: dnd/npc-registry}
} location-registry:
if err := os.WriteFile(filepath.Join(root, "pipelines", "dnd-session.yml"), fragment, 0o600); err != nil { extract: {module: dnd/location-registry}
t.Fatal(err) normalize: {module: dnd/location-registry}
} scene-descriptions:
description, err := os.ReadFile(repositoryPath("examples", "pipelines", "dnd-session.describe-session.yml")) extract: {module: dnd/scene-descriptions}
if err != nil { normalize: {module: dnd/scene-descriptions}
t.Fatal(err) - id: extract-events
} references:
if err := os.WriteFile(filepath.Join(root, "pipelines", "dnd-session.describe-session.yml"), description, 0o600); err != nil { location_registry: {artifact: {step: describe-session, lane: location-registry}}
t.Fatal(err) npc_registry: {artifact: {step: describe-session, lane: npc-registry}}
} scene_descriptions: {artifact: {step: describe-session, lane: scene-descriptions}}
path := filepath.Join(root, "dnd-complete.config.yml") item_registry: {artifact: {step: describe-session, lane: item-registry}}
if err := os.WriteFile(path, []byte(updated), 0o600); err != nil { artifacts:
t.Fatal(err) item-occurrences:
} extract: {module: dnd/item-occurrences}
return path normalize: {module: dnd/item-occurrences}
spells:
extract:
module: dnd/spells
references:
spell_catalog: {path: %q}
normalize:
module: dnd/spells
references:
spell_catalog: {path: %q}
combat-turns:
extract: {module: dnd/combat-turns}
normalize: {module: dnd/combat-turns}
npc-occurrences:
extract: {module: dnd/npc-occurrences}
normalize: {module: dnd/npc-occurrences}
location-occurrences:
extract: {module: dnd/location-occurrences}
normalize: {module: dnd/location-occurrences}
- id: track-enemies
references:
npc_registry: {artifact: {step: describe-session, lane: npc-registry}}
scene_descriptions: {artifact: {step: describe-session, lane: scene-descriptions}}
combat_turns: {artifact: {step: extract-events, lane: combat-turns}}
npc_occurrences: {artifact: {step: extract-events, lane: npc-occurrences}}
artifacts:
enemy-events:
extract: {module: dnd/enemy-events}
normalize: {module: dnd/enemy-events}
`+productionProviderConfiguration,
repositoryPath("examples", "dnd-party.txt"), repositoryPath("examples", "dnd-glossary.txt"),
repositoryPath("examples", "dnd-spell-catalog.json"), repositoryPath("examples", "dnd-spell-catalog.json")))
} }
type enemyEventLLMClient struct { type enemyEventLLMClient struct {
mu sync.Mutex mu sync.Mutex
requests []contracts.StructuredCompletionRequest requests []promptkit.GenerateRequest
} }
func (client *enemyEventLLMClient) CompleteStructured(ctx context.Context, request contracts.StructuredCompletionRequest, output any) (contracts.StructuredCompletionResponse, error) { func (client *enemyEventLLMClient) Generate(ctx context.Context, request promptkit.GenerateRequest) (*promptkit.GenerateResponse, error) {
if err := ctx.Err(); err != nil { if err := ctx.Err(); err != nil {
return contracts.StructuredCompletionResponse{}, err return nil, err
}
role := enemyEventRequestRole(request)
var combatScene bool
if role != "duplicate_groups" && role != "scenes" {
transcript, err := productionJSONInput(request, "units")
if err != nil {
return nil, err
}
combatScene = strings.Contains(string(transcript), "Roll initiative")
} }
combatScene := strings.Contains(string(request.Inputs["transcript"].Content), "Roll initiative")
var content []byte var content []byte
switch request.PromptID { switch role {
case scenes.PromptID: case "scenes":
content = []byte(`{"scenes":[{"start_unit_id":1,"end_unit_id":6},{"start_unit_id":7,"end_unit_id":11}]}`) content = []byte(`{"scenes":[{"start_unit_id":1,"end_unit_id":6},{"start_unit_id":7,"end_unit_id":11}]}`)
case npcregistry.PromptID: case "npcs":
if combatScene { if combatScene {
content = []byte(`{"npcs":[{"name":"Kesh","source_refs":[{"start_unit_id":7,"end_unit_id":7}]}]}`) content = []byte(`{"npcs":[{"name":"Kesh","source_refs":[{"start_unit_id":7,"end_unit_id":7}]}]}`)
} else { } else {
content = []byte(`{"npcs":[]}`) content = []byte(`{"npcs":[]}`)
} }
case npcnormalize.PromptID: case "duplicate_groups":
content = []byte(`{"duplicate_groups":[]}`) content = []byte(`{"duplicate_groups":[]}`)
case scenedescriptions.PromptID: case "kind":
kind, title := "narrative", "Arrival" kind, title := "narrative", "Arrival"
if combatScene { if combatScene {
kind, title = "combat", "Raiders attack" kind, title = "combat", "Raiders attack"
} }
content = []byte(fmt.Sprintf(`{"kind":%q,"title":%q,"summary":"session scene"}`, kind, title)) content = []byte(fmt.Sprintf(`{"kind":%q,"title":%q,"summary":"session scene"}`, kind, title))
case locations.PromptID: case "locations":
unitID := 1 unitID := 1
if combatScene { if combatScene {
unitID = 7 unitID = 7
} }
content = []byte(fmt.Sprintf(`{"locations":[{"name":"Moon Gate","source_refs":[{"start_unit_id":%d,"end_unit_id":%d}]}]}`, unitID, unitID)) content = []byte(fmt.Sprintf(`{"locations":[{"name":"Moon Gate","source_refs":[{"start_unit_id":%d,"end_unit_id":%d}]}]}`, unitID, unitID))
case locationnormalize.PromptID: case "spell_casts":
content = []byte(`{"duplicate_groups":[]}`)
case spells.PromptID:
content = []byte(`{"spell_casts":[]}`) content = []byte(`{"spell_casts":[]}`)
case itemregistry.PromptID: case "items":
if combatScene { if combatScene {
content = []byte(`{"items":[]}`) content = []byte(`{"items":[]}`)
} else { } else {
content = []byte(`{"items":[{"name":"Moonblade","source_refs":[{"start_unit_id":5,"end_unit_id":5}]}]}`) content = []byte(`{"items":[{"name":"Moonblade","source_refs":[{"start_unit_id":5,"end_unit_id":5}]}]}`)
} }
case itemregistrynormalize.PromptID: case "item-occurrences":
content = []byte(`{"duplicate_groups":[]}`)
case itemoccurrences.PromptID:
if combatScene { if combatScene {
content = []byte(`{"occurrences":[]}`) content = []byte(`{"occurrences":[]}`)
} else { } else {
@@ -355,34 +324,42 @@ func (client *enemyEventLLMClient) CompleteStructured(ctx context.Context, reque
Name string `json:"name"` Name string `json:"name"`
} `json:"items"` } `json:"items"`
} }
if err := json.Unmarshal(request.Inputs["item_registry"].Content, &registry); err != nil { data, err := productionJSONInput(request, "items")
return contracts.StructuredCompletionResponse{}, fmt.Errorf("decode generated item registry: %w", err) if err != nil {
return nil, err
}
if err := json.Unmarshal(data, &registry); err != nil {
return nil, fmt.Errorf("decode generated item registry: %w", err)
} }
if len(registry.Items) != 1 || registry.Items[0].Name != "Moonblade" { if len(registry.Items) != 1 || registry.Items[0].Name != "Moonblade" {
return contracts.StructuredCompletionResponse{}, fmt.Errorf("generated item registry has %d items, want 1", len(registry.Items)) return nil, fmt.Errorf("generated item registry has %d items, want 1", len(registry.Items))
} }
content = []byte(`{"occurrences":[{"name":"Moonblade","kind":"discovered","quantity":null,"from":null,"to":null,"source_refs":[{"start_unit_id":5,"end_unit_id":5}]}]}`) content = []byte(`{"occurrences":[{"name":"Moonblade","kind":"discovered","quantity":null,"from":null,"to":null,"source_refs":[{"start_unit_id":5,"end_unit_id":5}]}]}`)
} }
case combat.PromptID: case "combat_turns":
content = []byte(`{"combat_turns":[{"actor":"Kesh","turn_kind":"turn","source_refs":[{"start_unit_id":8,"end_unit_id":8}]}]}`) content = []byte(`{"combat_turns":[{"actor":"Kesh","turn_kind":"turn","source_refs":[{"start_unit_id":8,"end_unit_id":8}]}]}`)
case npcoccurrences.PromptID: case "npc-occurrences":
if combatScene { if combatScene {
var registry struct { var registry struct {
NPCs []struct { NPCs []struct {
Name string `json:"name"` Name string `json:"name"`
} `json:"npcs"` } `json:"npcs"`
} }
if err := json.Unmarshal(request.Inputs["npc_registry"].Content, &registry); err != nil { data, err := productionJSONInput(request, "npcs")
return contracts.StructuredCompletionResponse{}, fmt.Errorf("decode generated NPC registry: %w", err) if err != nil {
return nil, err
}
if err := json.Unmarshal(data, &registry); err != nil {
return nil, fmt.Errorf("decode generated NPC registry: %w", err)
} }
if len(registry.NPCs) == 0 || registry.NPCs[0].Name != "Kesh" { if len(registry.NPCs) == 0 || registry.NPCs[0].Name != "Kesh" {
return contracts.StructuredCompletionResponse{}, fmt.Errorf("generated NPC registry has no NPCs") return nil, fmt.Errorf("generated NPC registry has no NPCs")
} }
content = []byte(`{"occurrences":[{"name":"Kesh","kind":"combat_opponent","source_refs":[{"start_unit_id":7,"end_unit_id":7}]}]}`) content = []byte(`{"occurrences":[{"name":"Kesh","kind":"combat_opponent","source_refs":[{"start_unit_id":7,"end_unit_id":7}]}]}`)
} else { } else {
content = []byte(`{"occurrences":[]}`) content = []byte(`{"occurrences":[]}`)
} }
case locationoccurrences.PromptID: case "location-occurrences":
var registry struct { var registry struct {
Locations []struct { Locations []struct {
Name string `json:"name"` Name string `json:"name"`
@@ -392,11 +369,15 @@ func (client *enemyEventLLMClient) CompleteStructured(ctx context.Context, reque
} `json:"registry_refs"` } `json:"registry_refs"`
} `json:"locations"` } `json:"locations"`
} }
if err := json.Unmarshal(request.Inputs["location_registry"].Content, &registry); err != nil { data, err := productionJSONInput(request, "locations")
return contracts.StructuredCompletionResponse{}, fmt.Errorf("decode generated location registry: %w", err) if err != nil {
return nil, err
}
if err := json.Unmarshal(data, &registry); err != nil {
return nil, fmt.Errorf("decode generated location registry: %w", err)
} }
if len(registry.Locations) == 0 { if len(registry.Locations) == 0 {
return contracts.StructuredCompletionResponse{}, fmt.Errorf("generated location registry has no locations") return nil, fmt.Errorf("generated location registry has no locations")
} }
unitID := 1 unitID := 1
location := registry.Locations[0] location := registry.Locations[0]
@@ -408,38 +389,53 @@ func (client *enemyEventLLMClient) CompleteStructured(ctx context.Context, reque
} }
registryRefs, err := json.Marshal(location.RegistryRefs) registryRefs, err := json.Marshal(location.RegistryRefs)
if err != nil { if err != nil {
return contracts.StructuredCompletionResponse{}, fmt.Errorf("encode location selector: %w", err) return nil, fmt.Errorf("encode location selector: %w", err)
} }
content = []byte(fmt.Sprintf(`{"occurrences":[{"name":%q,"registry_refs":%s,"kind":"visited","source_refs":[{"start_unit_id":%d,"end_unit_id":%d}]}]}`, location.Name, registryRefs, unitID, unitID)) content = []byte(fmt.Sprintf(`{"occurrences":[{"name":%q,"registry_refs":%s,"kind":"visited","source_refs":[{"start_unit_id":%d,"end_unit_id":%d}]}]}`, location.Name, registryRefs, unitID, unitID))
case enemyevents.PromptID: case "events":
content = []byte(`{"events":[{"name":"Kesh","kind":"fled","source_refs":[{"start_unit_id":10,"end_unit_id":10}]}]}`) content = []byte(`{"events":[{"name":"Kesh","kind":"fled","source_refs":[{"start_unit_id":10,"end_unit_id":10}]}]}`)
default: default:
return contracts.StructuredCompletionResponse{}, fmt.Errorf("unexpected prompt %q", request.PromptID) return nil, fmt.Errorf("unexpected native response contract %q", role)
}
if err := json.Unmarshal(content, output); err != nil {
return contracts.StructuredCompletionResponse{}, fmt.Errorf("populate fake structured target: %w", err)
}
snapshot, err := contracts.CloneStructuredCompletionRequest(request)
if err != nil {
return contracts.StructuredCompletionResponse{}, fmt.Errorf("clone fake request: %w", err)
} }
client.mu.Lock() client.mu.Lock()
client.requests = append(client.requests, snapshot) client.requests = append(client.requests, request)
client.mu.Unlock() client.mu.Unlock()
return contracts.StructuredCompletionResponse{Content: content, Provider: "test", Model: "deterministic", ProfileID: request.ProfileID}, nil return &promptkit.GenerateResponse{Content: string(content)}, nil
} }
func (client *enemyEventLLMClient) requestsFor(promptID string) []contracts.StructuredCompletionRequest { // Distinguish the real prepared output contracts and fixed registry projections,
// not module-private prompt identifiers or a synthetic completion interface.
func enemyEventRequestRole(request promptkit.GenerateRequest) string {
for _, property := range []string{"scenes", "npcs", "locations", "spell_casts", "items", "duplicate_groups", "kind", "combat_turns", "events"} {
if productionSchemaHasProperty(request, property) {
return property
}
}
if productionSchemaHasProperty(request, "occurrences") {
for _, field := range []string{"locations", "npcs", "items"} {
if _, err := productionJSONInput(request, field); err == nil {
switch field {
case "locations":
return "location-occurrences"
case "npcs":
return "npc-occurrences"
case "items":
return "item-occurrences"
}
}
}
}
return ""
}
func (client *enemyEventLLMClient) requestsFor(role string) []promptkit.GenerateRequest {
client.mu.Lock() client.mu.Lock()
defer client.mu.Unlock() defer client.mu.Unlock()
var requests []contracts.StructuredCompletionRequest var requests []promptkit.GenerateRequest
for _, request := range client.requests { for _, request := range client.requests {
if request.PromptID == promptID { if enemyEventRequestRole(request) == role {
snapshot, err := contracts.CloneStructuredCompletionRequest(request) requests = append(requests, request)
if err != nil {
panic(err)
}
requests = append(requests, snapshot)
} }
} }
return requests return requests