diff --git a/docs/roadmap/implementation.md b/docs/roadmap/implementation.md index 30386cf3..4578b813 100644 --- a/docs/roadmap/implementation.md +++ b/docs/roadmap/implementation.md @@ -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 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. **Inspect/change:** all production/test callers of the feature roadmap's removal diff --git a/internal/cli/assembled_enemy_event_codec_contract_test.go b/internal/cli/assembled_enemy_event_codec_contract_test.go index 0e803745..88f85775 100644 --- a/internal/cli/assembled_enemy_event_codec_contract_test.go +++ b/internal/cli/assembled_enemy_event_codec_contract_test.go @@ -51,11 +51,10 @@ func TestAssembledEnemyEventLaneRejectsInvalidFinalArtifactDespiteValidatorOverr t.Fatalf("Prepare() error = %v", err) } _, err = pipeline.New().Run(context.Background(), pipeline.RunInput{ - Prepared: prepared, - RawInput: readRepositoryFile(t, "examples", "seriatim-minimal-transcript.json"), - ChunkCacheMode: pipeline.ChunkCacheBypass, + Prepared: prepared, + RawInput: readRepositoryFile(t, "examples", "seriatim-minimal-transcript.json"), }) - 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) } } diff --git a/internal/cli/dnd_enemy_events_contract_test.go b/internal/cli/dnd_enemy_events_contract_test.go index 62365e31..af49f573 100644 --- a/internal/cli/dnd_enemy_events_contract_test.go +++ b/internal/cli/dnd_enemy_events_contract_test.go @@ -3,61 +3,43 @@ package cli import ( "context" "encoding/json" - "errors" "fmt" - "os" "path/filepath" "reflect" "strings" "sync" "testing" - "time" - "gitea.maximumdirect.net/eric/notarius/internal/core/artifacts" "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/pipeline" "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" - 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" - itemregistrynormalize "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/normalize/itemregistry" - locationnormalize "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/normalize/locationregistry" - npcnormalize "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/normalize/npcregistry" + "go.maximumdirect.net/promptkit" ) func TestProductionEnemyEventConfigurationResolvesGeneratedHandoffs(t *testing.T) { components := productionTestComponents(t) - cfg := loadMaintainedExample(t, repositoryPath("examples", "dnd-complete.config.yml")) - effective, err := cfg.Resolve(resolveInputForMaintainedExample(components, "dnd-session")) + configPath := writeEnemyEventContractConfig(t) + 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 { t.Fatalf("Resolve() error = %v, want nil", err) } materialized, _, err := pipeline.MaterializeReferences(effective.ResolvedPipeline, catalogFromRegistries(components.registries), pipeline.ReferenceMaterializationOptions{ - ConfigPath: repositoryPath("examples", "dnd-complete.config.yml"), - WorkingDir: repositoryPath("examples"), + ConfigPath: configPath, + WorkingDir: filepath.Dir(configPath), }) if err != nil { t.Fatalf("MaterializeReferences() error = %v, want nil", err) } 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) } 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.Steps[2].References["npc_registry"] = pipeline.GeneratedReference("track-enemies", "enemy-events") 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) } } -func TestMaintainedCompleteExamplePublishesRegistryBackedEntityOccurrences(t *testing.T) { - t.Chdir(repositoryPath()) +func TestNativeOrderedDNDRunPublishesRegistryBackedEntityOccurrences(t *testing.T) { + configPath := writeEnemyEventContractConfig(t) outputRoot := filepath.Join(t.TempDir(), "output") - configPath := completeExampleConfigWithTemporaryCache(t) client := &enemyEventLLMClient{} - options := productionCLIOptions(t) - 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 - } - + options := productionRunOptions(t, client) var stdout, stderr strings.Builder code := RunWithOptions([]string{ - "run", "dnd-session", - "--config", configPath, - "--input", repositoryPath("examples", "dnd-complete-transcript.json"), - "--chunk_cache", "bypass", "--output-dir", outputRoot, "--session-id", "enemy-event-session", + "run", "dnd-session", "--config", configPath, + "--input-file", repositoryPath("examples", "dnd-complete-transcript.json"), + "--cache", "bypass", "--output-dir", outputRoot, + "--llm-session-id", "enemy-event-session", "--format", "json", }, &stdout, &stderr, options) if code != 0 { 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) - index := readProductionJSON[exampleOutputIndex](t, filepath.Join(runRoot, "index.json")) - outputs := make(map[string]exampleOutputIndexEntry) - for _, entry := range index.OutputFiles { - outputs[entry.LaneID] = entry + if report.OutputDirectory != runRoot || report.ArtifactCount != 12 || report.FallbackCount != 0 { + t.Fatalf("completion=%#v, want ten domain lanes and two auxiliary artifacts without fallback", report) } - enemyOutput := outputs["enemy-events"] - locationOutput := outputs["location-registry"] - 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) + if files := readTree(t, runRoot); len(files) != report.ArtifactCount { + t.Fatalf("published files=%v, report=%#v", files, report) } - 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 { 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 { - t.Fatalf("location output = %#v, want typed location registry JSON", locationOutput) - } - locationsValue := readProductionJSON[dnd.LocationRegistry](t, filepath.Join(runRoot, locationOutput.File)) + locationsValue := readProductionJSON[dnd.LocationRegistry](t, filepath.Join(runRoot, "lanes/location-registry.json")) 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) } - if locationOccurrenceOutput.File != "lanes/location-occurrences.json" || locationOccurrenceOutput.SchemaID != locationoccurrencecodec.SchemaID || locationOccurrenceOutput.SchemaVersion != locationoccurrencecodec.SchemaVersion { - t.Fatalf("location occurrence output = %#v, want typed occurrence JSON", locationOccurrenceOutput) - } - occurrencesValue := readProductionJSON[dnd.LocationOccurrenceList](t, filepath.Join(runRoot, locationOccurrenceOutput.File)) + occurrencesValue := readProductionJSON[dnd.LocationOccurrenceList](t, filepath.Join(runRoot, "lanes/location-occurrences.json")) 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) } - if npcRegistryOutput.File != "lanes/npc-registry.json" || npcRegistryOutput.SchemaID != npccodec.SchemaID || npcRegistryOutput.SchemaVersion != npccodec.SchemaVersion { - t.Fatalf("NPC registry output = %#v, want typed registry JSON", npcRegistryOutput) - } - npcRegistry := readProductionJSON[dnd.NPCRegistry](t, filepath.Join(runRoot, npcRegistryOutput.File)) + npcRegistry := readProductionJSON[dnd.NPCRegistry](t, filepath.Join(runRoot, "lanes/npc-registry.json")) if len(npcRegistry.NPCs) != 1 || npcRegistry.NPCs[0].Name != "Kesh" { t.Fatalf("NPC registry = %#v, want Kesh identity", npcRegistry) } - if npcOccurrenceOutput.File != "lanes/npc-occurrences.json" || npcOccurrenceOutput.SchemaID != npcoccurrencecodec.SchemaID || npcOccurrenceOutput.SchemaVersion != npcoccurrencecodec.SchemaVersion { - t.Fatalf("NPC occurrence output = %#v, want typed occurrence JSON", npcOccurrenceOutput) - } - npcOccurrences := readProductionJSON[dnd.NPCOccurrenceList](t, filepath.Join(runRoot, npcOccurrenceOutput.File)) + npcOccurrences := readProductionJSON[dnd.NPCOccurrenceList](t, filepath.Join(runRoot, "lanes/npc-occurrences.json")) 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) } - if itemRegistryOutput.File != "lanes/item-registry.json" || itemRegistryOutput.SchemaID != itemregistrycodec.SchemaID || itemRegistryOutput.SchemaVersion != itemregistrycodec.SchemaVersion { - t.Fatalf("item registry output = %#v, want typed registry JSON", itemRegistryOutput) - } - itemRegistry := readProductionJSON[dnd.ItemRegistry](t, filepath.Join(runRoot, itemRegistryOutput.File)) + itemRegistry := readProductionJSON[dnd.ItemRegistry](t, filepath.Join(runRoot, "lanes/item-registry.json")) if len(itemRegistry.Items) != 1 || itemRegistry.Items[0].Name != "Moonblade" { t.Fatalf("item registry = %#v, want Moonblade identity", itemRegistry) } - if itemOccurrenceOutput.File != "lanes/item-occurrences.json" || itemOccurrenceOutput.SchemaID != itemoccurrencecodec.SchemaID || itemOccurrenceOutput.SchemaVersion != itemoccurrencecodec.SchemaVersion { - t.Fatalf("item occurrence output = %#v, want typed occurrence JSON", itemOccurrenceOutput) - } - itemOccurrences := readProductionJSON[dnd.ItemOccurrenceList](t, filepath.Join(runRoot, itemOccurrenceOutput.File)) + itemOccurrences := readProductionJSON[dnd.ItemOccurrenceList](t, filepath.Join(runRoot, "lanes/item-occurrences.json")) 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) } @@ -203,150 +153,169 @@ func TestMaintainedCompleteExamplePublishesRegistryBackedEntityOccurrences(t *te seenEvidenceUnits[unit.ID] = struct{}{} } - requests := client.requestsFor(enemyevents.PromptID) + requests := client.requestsFor("events") 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] - if request.SessionID != "enemy-event-session" { - t.Fatalf("enemy event session = %q, want shared session", request.SessionID) + if request.Prompt.SessionID != "enemy-event-session" { + t.Fatalf("session=%q", request.Prompt.SessionID) } - for slot, required := range map[string]string{ - "npc_registry": "Kesh", - "combat_turns": "Kesh", - "npc_occurrences": "Kesh", - } { - 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) + for _, field := range []string{"npcs", "combat_turns", "npc_occurrences"} { + input, err := productionJSONInput(request, field) + if err != nil || !strings.Contains(string(input), "Kesh") || strings.Contains(string(input), "source_refs") || strings.Contains(string(input), "start_unit_id") { + t.Fatalf("%s projection=%s error=%v, want compact source-free grounding", field, input, err) } } - locationRequests := client.requestsFor(locationoccurrences.PromptID) - if len(locationRequests) != 2 { - t.Fatalf("location occurrence requests = %#v, want one request per scene", locationRequests) - } - 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 + for _, check := range []struct { + role, field, name string + contextual bool }{ - {promptID: npcoccurrences.PromptID, slot: "npc_registry", name: "Kesh"}, - {promptID: itemoccurrences.PromptID, slot: "item_registry", name: "Moonblade"}, + {"location-occurrences", "locations", "Moon Gate", true}, + {"npc-occurrences", "npcs", "Kesh", false}, + {"item-occurrences", "items", "Moonblade", false}, } { - requests := client.requestsFor(test.promptID) + requests := client.requestsFor(check.role) 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 { - registryInput := request.Inputs[test.slot] - hasID := strings.Contains(string(registryInput.Content), `"id"`) - if !strings.Contains(string(registryInput.Content), test.name) || hasID != test.requiresIDs || strings.Contains(string(registryInput.Content), "source_refs") { - t.Fatalf("%s registry input = %q, want source-free configured grounding", test.promptID, registryInput.Content) + input, err := productionJSONInput(request, check.field) + 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 { + t.Fatalf("%s projection=%s error=%v", check.role, input, err) } } } } -func completeExampleConfigWithTemporaryCache(t *testing.T) string { +func writeEnemyEventContractConfig(t *testing.T) string { t.Helper() - content, err := os.ReadFile(repositoryPath("examples", "dnd-complete.config.yml")) - if err != nil { - t.Fatal(err) - } - cacheRoot := t.TempDir() - updated := strings.Replace(string(content), "directory: ./notarius-cache/chunk-plans", fmt.Sprintf("directory: %q", filepath.Join(cacheRoot, "chunk-plans")), 1) - updated = strings.Replace(updated, "directory: ./notarius-cache/checkpoints", fmt.Sprintf("directory: %q", filepath.Join(cacheRoot, "checkpoints")), 1) - for relative, absolute := range map[string]string{ - "./llm-profiles/dnd-extraction.yml": repositoryPath("examples", "llm-profiles", "dnd-extraction.yml"), - } { - updated = strings.ReplaceAll(updated, relative, fmt.Sprintf("%q", absolute)) - } - root := t.TempDir() - fragment, err := os.ReadFile(repositoryPath("examples", "pipelines", "dnd-session.yml")) - if err != nil { - t.Fatal(err) - } - for relative, absolute := range map[string]string{ - "../dnd-party.txt": repositoryPath("examples", "dnd-party.txt"), - "../dnd-glossary.txt": repositoryPath("examples", "dnd-glossary.txt"), - "../dnd-spell-catalog.json": repositoryPath("examples", "dnd-spell-catalog.json"), - } { - fragment = []byte(strings.ReplaceAll(string(fragment), relative, fmt.Sprintf("%q", absolute))) - } - if err := os.MkdirAll(filepath.Join(root, "pipelines"), 0o700); err != nil { - t.Fatal(err) - } - if err := os.WriteFile(filepath.Join(root, "pipelines", "dnd-session.yml"), fragment, 0o600); err != nil { - t.Fatal(err) - } - description, err := os.ReadFile(repositoryPath("examples", "pipelines", "dnd-session.describe-session.yml")) - if err != nil { - t.Fatal(err) - } - if err := os.WriteFile(filepath.Join(root, "pipelines", "dnd-session.describe-session.yml"), description, 0o600); err != nil { - t.Fatal(err) - } - path := filepath.Join(root, "dnd-complete.config.yml") - if err := os.WriteFile(path, []byte(updated), 0o600); err != nil { - t.Fatal(err) - } - return path + return writeProductionContractConfig(t, fmt.Sprintf(`version: 6 +pipelines: + dnd-session: + cache: bypass + input: {module: seriatim} + chunk: {module: dnd/scenes} + references: + party: {path: %q} + glossary: {path: %q} + output: + module: json + options: + include_chunk_map: true + evidence_context: + enabled: true + window_units: 3 + lanes: [item-occurrences, item-registry, location-registry, location-occurrences, npc-registry, spells, combat-turns, npc-occurrences, enemy-events] + steps: + - id: describe-session + artifacts: + item-registry: + extract: {module: dnd/item-registry} + normalize: {module: dnd/item-registry} + npc-registry: + extract: {module: dnd/npc-registry} + normalize: {module: dnd/npc-registry} + location-registry: + extract: {module: dnd/location-registry} + normalize: {module: dnd/location-registry} + scene-descriptions: + extract: {module: dnd/scene-descriptions} + normalize: {module: dnd/scene-descriptions} + - id: extract-events + references: + location_registry: {artifact: {step: describe-session, lane: location-registry}} + npc_registry: {artifact: {step: describe-session, lane: npc-registry}} + scene_descriptions: {artifact: {step: describe-session, lane: scene-descriptions}} + item_registry: {artifact: {step: describe-session, lane: item-registry}} + artifacts: + item-occurrences: + extract: {module: dnd/item-occurrences} + 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 { 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 { - 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 - switch request.PromptID { - case scenes.PromptID: + switch role { + case "scenes": 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 { content = []byte(`{"npcs":[{"name":"Kesh","source_refs":[{"start_unit_id":7,"end_unit_id":7}]}]}`) } else { content = []byte(`{"npcs":[]}`) } - case npcnormalize.PromptID: + case "duplicate_groups": content = []byte(`{"duplicate_groups":[]}`) - case scenedescriptions.PromptID: + case "kind": kind, title := "narrative", "Arrival" if combatScene { kind, title = "combat", "Raiders attack" } content = []byte(fmt.Sprintf(`{"kind":%q,"title":%q,"summary":"session scene"}`, kind, title)) - case locations.PromptID: + case "locations": unitID := 1 if combatScene { unitID = 7 } content = []byte(fmt.Sprintf(`{"locations":[{"name":"Moon Gate","source_refs":[{"start_unit_id":%d,"end_unit_id":%d}]}]}`, unitID, unitID)) - case locationnormalize.PromptID: - content = []byte(`{"duplicate_groups":[]}`) - case spells.PromptID: + case "spell_casts": content = []byte(`{"spell_casts":[]}`) - case itemregistry.PromptID: + case "items": if combatScene { content = []byte(`{"items":[]}`) } else { content = []byte(`{"items":[{"name":"Moonblade","source_refs":[{"start_unit_id":5,"end_unit_id":5}]}]}`) } - case itemregistrynormalize.PromptID: - content = []byte(`{"duplicate_groups":[]}`) - case itemoccurrences.PromptID: + case "item-occurrences": if combatScene { content = []byte(`{"occurrences":[]}`) } else { @@ -355,34 +324,42 @@ func (client *enemyEventLLMClient) CompleteStructured(ctx context.Context, reque Name string `json:"name"` } `json:"items"` } - if err := json.Unmarshal(request.Inputs["item_registry"].Content, ®istry); err != nil { - return contracts.StructuredCompletionResponse{}, fmt.Errorf("decode generated item registry: %w", err) + data, err := productionJSONInput(request, "items") + if err != nil { + return nil, err + } + if err := json.Unmarshal(data, ®istry); err != nil { + return nil, fmt.Errorf("decode generated item registry: %w", err) } 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}]}]}`) } - 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}]}]}`) - case npcoccurrences.PromptID: + case "npc-occurrences": if combatScene { var registry struct { NPCs []struct { Name string `json:"name"` } `json:"npcs"` } - if err := json.Unmarshal(request.Inputs["npc_registry"].Content, ®istry); err != nil { - return contracts.StructuredCompletionResponse{}, fmt.Errorf("decode generated NPC registry: %w", err) + data, err := productionJSONInput(request, "npcs") + if err != nil { + return nil, err + } + if err := json.Unmarshal(data, ®istry); err != nil { + return nil, fmt.Errorf("decode generated NPC registry: %w", err) } 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}]}]}`) } else { content = []byte(`{"occurrences":[]}`) } - case locationoccurrences.PromptID: + case "location-occurrences": var registry struct { Locations []struct { Name string `json:"name"` @@ -392,11 +369,15 @@ func (client *enemyEventLLMClient) CompleteStructured(ctx context.Context, reque } `json:"registry_refs"` } `json:"locations"` } - if err := json.Unmarshal(request.Inputs["location_registry"].Content, ®istry); err != nil { - return contracts.StructuredCompletionResponse{}, fmt.Errorf("decode generated location registry: %w", err) + data, err := productionJSONInput(request, "locations") + if err != nil { + return nil, err + } + if err := json.Unmarshal(data, ®istry); err != nil { + return nil, fmt.Errorf("decode generated location registry: %w", err) } 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 location := registry.Locations[0] @@ -408,38 +389,53 @@ func (client *enemyEventLLMClient) CompleteStructured(ctx context.Context, reque } registryRefs, err := json.Marshal(location.RegistryRefs) 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)) - case enemyevents.PromptID: + case "events": content = []byte(`{"events":[{"name":"Kesh","kind":"fled","source_refs":[{"start_unit_id":10,"end_unit_id":10}]}]}`) default: - return contracts.StructuredCompletionResponse{}, fmt.Errorf("unexpected prompt %q", request.PromptID) - } - 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) + return nil, fmt.Errorf("unexpected native response contract %q", role) } + client.mu.Lock() - client.requests = append(client.requests, snapshot) + client.requests = append(client.requests, request) 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() defer client.mu.Unlock() - var requests []contracts.StructuredCompletionRequest + var requests []promptkit.GenerateRequest for _, request := range client.requests { - if request.PromptID == promptID { - snapshot, err := contracts.CloneStructuredCompletionRequest(request) - if err != nil { - panic(err) - } - requests = append(requests, snapshot) + if enemyEventRequestRole(request) == role { + requests = append(requests, request) } } return requests