2026-08-03 21:10:21 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"fmt"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"reflect"
|
|
|
|
|
"strings"
|
|
|
|
|
"sync"
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"gitea.maximumdirect.net/eric/notarius/internal/core/config"
|
|
|
|
|
"gitea.maximumdirect.net/eric/notarius/internal/framework/evidencecontext"
|
|
|
|
|
"gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline"
|
|
|
|
|
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
|
|
|
|
|
enemyevents "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/extract/enemyevents"
|
|
|
|
|
enemyeventnormalize "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/normalize/enemyevents"
|
2026-11-22 05:29:48 +00:00
|
|
|
"go.maximumdirect.net/promptkit"
|
2026-08-03 21:10:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestProductionEnemyEventConfigurationResolvesGeneratedHandoffs(t *testing.T) {
|
|
|
|
|
components := productionTestComponents(t)
|
2026-11-22 05:45:31 +00:00
|
|
|
configPath := repositoryPath("examples", "dnd-complete", "config.yml")
|
2026-11-22 05:29:48 +00:00
|
|
|
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)})
|
2026-08-03 21:10:21 +00:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("Resolve() error = %v, want nil", err)
|
|
|
|
|
}
|
|
|
|
|
materialized, _, err := pipeline.MaterializeReferences(effective.ResolvedPipeline, catalogFromRegistries(components.registries), pipeline.ReferenceMaterializationOptions{
|
2026-11-22 05:29:48 +00:00
|
|
|
ConfigPath: configPath,
|
|
|
|
|
WorkingDir: filepath.Dir(configPath),
|
2026-08-03 21:10:21 +00:00
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("MaterializeReferences() error = %v, want nil", err)
|
|
|
|
|
}
|
|
|
|
|
lane := referenceContractLane(t, materialized, "enemy-events")
|
2026-11-22 05:29:48 +00:00
|
|
|
if lane.ArtifactKind != dnd.EnemyEventListKind || lane.Extract.Module != enemyevents.Key || lane.Merge.Module != pipeline.DefaultMergeModule || lane.Normalize.Module != enemyeventnormalize.Key {
|
2026-08-03 21:10:21 +00:00
|
|
|
t.Fatalf("enemy event lane = %#v, want typed production composition", lane)
|
|
|
|
|
}
|
|
|
|
|
for slot, want := range map[string]struct{ step, lane string }{
|
2026-08-05 20:07:31 +00:00
|
|
|
"npc_registry": {step: "describe-session", lane: "npc-registry"},
|
2026-08-03 21:10:21 +00:00
|
|
|
"scene_descriptions": {step: "describe-session", lane: "scene-descriptions"},
|
|
|
|
|
"combat_turns": {step: "extract-events", lane: "combat-turns"},
|
2026-08-05 19:00:55 +00:00
|
|
|
"npc_occurrences": {step: "extract-events", lane: "npc-occurrences"},
|
2026-08-03 21:10:21 +00:00
|
|
|
} {
|
|
|
|
|
binding, found := generatedReferenceBinding(lane.ExtractReferences.Bindings, slot)
|
|
|
|
|
if !found || binding.Artifact.Step != want.step || binding.Artifact.Lane != want.lane {
|
|
|
|
|
t.Fatalf("enemy event %s reference = %#v, want generated %s/%s artifact", slot, binding, want.step, want.lane)
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-08-05 20:07:31 +00:00
|
|
|
if binding, found := generatedReferenceBinding(lane.NormalizeReferences.Bindings, "npc_registry"); !found || binding.Artifact.Step != "describe-session" || binding.Artifact.Lane != "npc-registry" {
|
2026-08-03 21:10:21 +00:00
|
|
|
t.Fatalf("enemy event normalizer NPC reference = %#v, want generated NPC artifact", binding)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
catalog := catalogFromRegistries(components.registries)
|
|
|
|
|
extractSpec, ok := catalog.Extractors.Spec(enemyevents.Key)
|
|
|
|
|
if !ok || !reflect.DeepEqual(extractSpec.Requires, []string{"chunks", "source.transcript"}) || !reflect.DeepEqual(extractSpec.Provides, []string{"dnd.enemy_events"}) {
|
|
|
|
|
t.Fatalf("enemy event extractor spec = %#v, want source and artifact capabilities", extractSpec)
|
|
|
|
|
}
|
|
|
|
|
normalizeSpec, ok := catalog.Normalizers.SpecForArtifact(enemyeventnormalize.Key, dnd.EnemyEventListKind)
|
|
|
|
|
if !ok || !reflect.DeepEqual(normalizeSpec.Requires, []string{"merged"}) || !reflect.DeepEqual(normalizeSpec.Provides, []string{"normalized"}) {
|
|
|
|
|
t.Fatalf("enemy event normalizer spec = %#v, want merged/normalized capabilities", normalizeSpec)
|
|
|
|
|
}
|
2026-08-05 19:00:55 +00:00
|
|
|
for _, slot := range []string{"npc_registry", "scene_descriptions", "combat_turns", "npc_occurrences"} {
|
2026-08-03 21:10:21 +00:00
|
|
|
if !hasReferenceSlot(extractSpec.ReferenceSlots, slot) {
|
|
|
|
|
t.Fatalf("enemy event extractor slots = %#v, want %q", extractSpec.ReferenceSlots, slot)
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-08-05 18:38:22 +00:00
|
|
|
if !hasReferenceSlot(normalizeSpec.ReferenceSlots, "npc_registry") {
|
2026-08-03 21:10:21 +00:00
|
|
|
t.Fatalf("enemy event normalizer slots = %#v, want NPC registry", normalizeSpec.ReferenceSlots)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
profile := cfg.Pipelines["dnd-session"]
|
2026-08-05 18:38:22 +00:00
|
|
|
profile.Steps[2].References["npc_registry"] = pipeline.GeneratedReference("track-enemies", "enemy-events")
|
2026-08-03 21:10:21 +00:00
|
|
|
cfg.Pipelines["dnd-session"] = profile
|
2026-11-22 05:29:48 +00:00
|
|
|
if _, err := cfg.Resolve(config.ResolveInput{PipelineID: "dnd-session", Catalog: catalogFromRegistries(components.registries)}); err == nil || !strings.Contains(err.Error(), "earlier step") {
|
2026-08-03 21:10:21 +00:00
|
|
|
t.Fatalf("Resolve() error = %v, want future generated-reference rejection", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-22 05:45:31 +00:00
|
|
|
func TestMaintainedCompleteInvocationPublishesRegistryBackedEntityOccurrences(t *testing.T) {
|
|
|
|
|
t.Setenv("OPENROUTER_API_KEY", "notarius-test-credential")
|
|
|
|
|
configPath := repositoryPath("examples", "dnd-complete", "config.yml")
|
2026-08-03 21:10:21 +00:00
|
|
|
outputRoot := filepath.Join(t.TempDir(), "output")
|
|
|
|
|
client := &enemyEventLLMClient{}
|
2026-11-22 05:29:48 +00:00
|
|
|
options := productionRunOptions(t, client)
|
2026-08-03 21:10:21 +00:00
|
|
|
var stdout, stderr strings.Builder
|
|
|
|
|
code := RunWithOptions([]string{
|
2026-11-22 05:29:48 +00:00
|
|
|
"run", "dnd-session", "--config", configPath,
|
2026-11-22 05:45:31 +00:00
|
|
|
"--input-file", repositoryPath("examples", "dnd-complete", "transcript.json"),
|
2026-11-22 06:32:22 +00:00
|
|
|
"--reference", "party=" + repositoryPath("examples", "dnd-complete", "party.txt"),
|
|
|
|
|
"--reference", "glossary=" + repositoryPath("examples", "dnd-complete", "glossary.txt"),
|
|
|
|
|
"--reference", "spell_catalog=" + repositoryPath("examples", "dnd-complete", "spell-catalog.json"),
|
2026-11-22 05:29:48 +00:00
|
|
|
"--cache", "bypass", "--output-dir", outputRoot,
|
|
|
|
|
"--llm-session-id", "enemy-event-session", "--format", "json",
|
2026-08-03 21:10:21 +00:00
|
|
|
}, &stdout, &stderr, options)
|
|
|
|
|
if code != 0 {
|
|
|
|
|
t.Fatalf("code=%d stdout=%q stderr=%q", code, stdout.String(), stderr.String())
|
|
|
|
|
}
|
2026-11-22 05:29:48 +00:00
|
|
|
var report runResult
|
|
|
|
|
if err := json.Unmarshal([]byte(stdout.String()), &report); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2026-08-03 21:10:21 +00:00
|
|
|
runRoot := filepath.Join(outputRoot, productionRunID)
|
2026-11-22 05:29:48 +00:00
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
if files := readTree(t, runRoot); len(files) != report.ArtifactCount {
|
|
|
|
|
t.Fatalf("published files=%v, report=%#v", files, report)
|
2026-11-22 06:32:22 +00:00
|
|
|
} else {
|
|
|
|
|
for _, relative := range []string{
|
|
|
|
|
"lanes/item-registry.json", "lanes/npc-registry.json", "lanes/location-registry.json",
|
|
|
|
|
"lanes/scene-descriptions.json", "lanes/item-occurrences.json", "lanes/spells.json",
|
|
|
|
|
"lanes/combat-turns.json", "lanes/npc-occurrences.json", "lanes/location-occurrences.json",
|
|
|
|
|
"lanes/enemy-events.json", "chunk-map.json", "evidence-context.json",
|
|
|
|
|
} {
|
|
|
|
|
if !json.Valid(files[filepath.FromSlash(relative)]) {
|
|
|
|
|
t.Fatalf("missing or invalid domain artifact %q", relative)
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-11-22 05:29:48 +00:00
|
|
|
}
|
|
|
|
|
value := readProductionJSON[dnd.EnemyEventList](t, filepath.Join(runRoot, "lanes/enemy-events.json"))
|
2026-08-03 21:10:21 +00:00
|
|
|
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)
|
|
|
|
|
}
|
2026-11-22 05:29:48 +00:00
|
|
|
locationsValue := readProductionJSON[dnd.LocationRegistry](t, filepath.Join(runRoot, "lanes/location-registry.json"))
|
2026-08-04 00:45:50 +00:00
|
|
|
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)
|
|
|
|
|
}
|
2026-11-22 05:29:48 +00:00
|
|
|
occurrencesValue := readProductionJSON[dnd.LocationOccurrenceList](t, filepath.Join(runRoot, "lanes/location-occurrences.json"))
|
2026-08-04 00:45:50 +00:00
|
|
|
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)
|
|
|
|
|
}
|
2026-11-22 05:29:48 +00:00
|
|
|
npcRegistry := readProductionJSON[dnd.NPCRegistry](t, filepath.Join(runRoot, "lanes/npc-registry.json"))
|
2026-08-05 20:07:31 +00:00
|
|
|
if len(npcRegistry.NPCs) != 1 || npcRegistry.NPCs[0].Name != "Kesh" {
|
|
|
|
|
t.Fatalf("NPC registry = %#v, want Kesh identity", npcRegistry)
|
|
|
|
|
}
|
2026-11-22 05:29:48 +00:00
|
|
|
npcOccurrences := readProductionJSON[dnd.NPCOccurrenceList](t, filepath.Join(runRoot, "lanes/npc-occurrences.json"))
|
2026-08-05 20:07:31 +00:00
|
|
|
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)
|
|
|
|
|
}
|
2026-11-22 05:29:48 +00:00
|
|
|
itemRegistry := readProductionJSON[dnd.ItemRegistry](t, filepath.Join(runRoot, "lanes/item-registry.json"))
|
2026-08-05 20:07:31 +00:00
|
|
|
if len(itemRegistry.Items) != 1 || itemRegistry.Items[0].Name != "Moonblade" {
|
|
|
|
|
t.Fatalf("item registry = %#v, want Moonblade identity", itemRegistry)
|
|
|
|
|
}
|
2026-11-22 05:29:48 +00:00
|
|
|
itemOccurrences := readProductionJSON[dnd.ItemOccurrenceList](t, filepath.Join(runRoot, "lanes/item-occurrences.json"))
|
2026-08-05 20:07:31 +00:00
|
|
|
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)
|
|
|
|
|
}
|
2026-08-03 21:10:21 +00:00
|
|
|
|
|
|
|
|
evidence := readProductionJSON[evidencecontext.Document](t, filepath.Join(runRoot, "evidence-context.json"))
|
2026-08-09 19:45:21 +00:00
|
|
|
if len(evidence) == 0 {
|
|
|
|
|
t.Fatalf("evidence context = %#v, want selected source-unit evidence", evidence)
|
|
|
|
|
}
|
|
|
|
|
seenEvidenceUnits := make(map[int]struct{}, len(evidence))
|
|
|
|
|
for _, unit := range evidence {
|
|
|
|
|
if unit.Ref.SourceID != "session-ravenfall" || unit.Ref.StartUnitID != unit.ID || unit.Ref.EndUnitID != unit.ID {
|
|
|
|
|
t.Fatalf("evidence unit = %#v, want unchanged source-unit self-reference", unit)
|
|
|
|
|
}
|
|
|
|
|
if _, exists := seenEvidenceUnits[unit.ID]; exists {
|
|
|
|
|
t.Fatalf("evidence context = %#v, want each source unit once", evidence)
|
2026-08-04 00:45:50 +00:00
|
|
|
}
|
2026-08-09 19:45:21 +00:00
|
|
|
seenEvidenceUnits[unit.ID] = struct{}{}
|
2026-08-03 21:10:21 +00:00
|
|
|
}
|
|
|
|
|
|
2026-11-22 05:29:48 +00:00
|
|
|
requests := client.requestsFor("events")
|
2026-08-03 21:10:21 +00:00
|
|
|
if len(requests) != 1 {
|
2026-11-22 05:29:48 +00:00
|
|
|
t.Fatalf("enemy event requests=%d, want only the combat scene", len(requests))
|
2026-08-03 21:10:21 +00:00
|
|
|
}
|
|
|
|
|
request := requests[0]
|
2026-11-22 05:29:48 +00:00
|
|
|
if request.Prompt.SessionID != "enemy-event-session" {
|
|
|
|
|
t.Fatalf("session=%q", request.Prompt.SessionID)
|
2026-08-03 21:10:21 +00:00
|
|
|
}
|
2026-11-22 05:29:48 +00:00
|
|
|
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)
|
2026-08-03 21:10:21 +00:00
|
|
|
}
|
|
|
|
|
}
|
2026-11-22 05:29:48 +00:00
|
|
|
for _, check := range []struct {
|
|
|
|
|
role, field, name string
|
|
|
|
|
contextual bool
|
2026-08-05 20:07:31 +00:00
|
|
|
}{
|
2026-11-22 05:29:48 +00:00
|
|
|
{"location-occurrences", "locations", "Moon Gate", true},
|
|
|
|
|
{"npc-occurrences", "npcs", "Kesh", false},
|
|
|
|
|
{"item-occurrences", "items", "Moonblade", false},
|
2026-08-05 20:07:31 +00:00
|
|
|
} {
|
2026-11-22 05:29:48 +00:00
|
|
|
requests := client.requestsFor(check.role)
|
2026-08-05 20:07:31 +00:00
|
|
|
if len(requests) != 2 {
|
2026-11-22 05:29:48 +00:00
|
|
|
t.Fatalf("%s requests=%d, want one per scene", check.role, len(requests))
|
2026-08-05 20:07:31 +00:00
|
|
|
}
|
|
|
|
|
for _, request := range requests {
|
2026-11-22 05:29:48 +00:00
|
|
|
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)
|
2026-08-05 20:07:31 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-08-03 21:10:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type enemyEventLLMClient struct {
|
|
|
|
|
mu sync.Mutex
|
2026-11-22 05:29:48 +00:00
|
|
|
requests []promptkit.GenerateRequest
|
2026-08-03 21:10:21 +00:00
|
|
|
}
|
|
|
|
|
|
2026-11-22 05:29:48 +00:00
|
|
|
func (client *enemyEventLLMClient) Generate(ctx context.Context, request promptkit.GenerateRequest) (*promptkit.GenerateResponse, error) {
|
2026-08-03 21:10:21 +00:00
|
|
|
if err := ctx.Err(); err != nil {
|
2026-11-22 05:29:48 +00:00
|
|
|
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")
|
2026-08-03 21:10:21 +00:00
|
|
|
}
|
|
|
|
|
var content []byte
|
2026-11-22 05:29:48 +00:00
|
|
|
switch role {
|
|
|
|
|
case "scenes":
|
2026-08-03 21:10:21 +00:00
|
|
|
content = []byte(`{"scenes":[{"start_unit_id":1,"end_unit_id":6},{"start_unit_id":7,"end_unit_id":11}]}`)
|
2026-11-22 05:29:48 +00:00
|
|
|
case "npcs":
|
2026-08-03 21:10:21 +00:00
|
|
|
if combatScene {
|
|
|
|
|
content = []byte(`{"npcs":[{"name":"Kesh","source_refs":[{"start_unit_id":7,"end_unit_id":7}]}]}`)
|
|
|
|
|
} else {
|
|
|
|
|
content = []byte(`{"npcs":[]}`)
|
|
|
|
|
}
|
2026-11-22 05:29:48 +00:00
|
|
|
case "duplicate_groups":
|
2026-08-03 21:10:21 +00:00
|
|
|
content = []byte(`{"duplicate_groups":[]}`)
|
2026-11-22 05:29:48 +00:00
|
|
|
case "kind":
|
2026-08-03 21:10:21 +00:00
|
|
|
kind, title := "narrative", "Arrival"
|
|
|
|
|
if combatScene {
|
|
|
|
|
kind, title = "combat", "Raiders attack"
|
|
|
|
|
}
|
|
|
|
|
content = []byte(fmt.Sprintf(`{"kind":%q,"title":%q,"summary":"session scene"}`, kind, title))
|
2026-11-22 05:29:48 +00:00
|
|
|
case "locations":
|
2026-08-04 00:45:50 +00:00
|
|
|
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))
|
2026-11-22 05:29:48 +00:00
|
|
|
case "spell_casts":
|
2026-08-03 21:10:21 +00:00
|
|
|
content = []byte(`{"spell_casts":[]}`)
|
2026-11-22 05:29:48 +00:00
|
|
|
case "items":
|
2026-08-05 20:07:31 +00:00
|
|
|
if combatScene {
|
|
|
|
|
content = []byte(`{"items":[]}`)
|
|
|
|
|
} else {
|
|
|
|
|
content = []byte(`{"items":[{"name":"Moonblade","source_refs":[{"start_unit_id":5,"end_unit_id":5}]}]}`)
|
|
|
|
|
}
|
2026-11-22 05:29:48 +00:00
|
|
|
case "item-occurrences":
|
2026-08-05 20:07:31 +00:00
|
|
|
if combatScene {
|
|
|
|
|
content = []byte(`{"occurrences":[]}`)
|
|
|
|
|
} else {
|
|
|
|
|
var registry struct {
|
|
|
|
|
Items []struct {
|
2026-08-08 14:43:04 +00:00
|
|
|
Name string `json:"name"`
|
2026-08-05 20:07:31 +00:00
|
|
|
} `json:"items"`
|
|
|
|
|
}
|
2026-11-22 05:29:48 +00:00
|
|
|
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)
|
2026-08-05 20:07:31 +00:00
|
|
|
}
|
2026-08-08 14:43:04 +00:00
|
|
|
if len(registry.Items) != 1 || registry.Items[0].Name != "Moonblade" {
|
2026-11-22 05:29:48 +00:00
|
|
|
return nil, fmt.Errorf("generated item registry has %d items, want 1", len(registry.Items))
|
2026-08-05 20:07:31 +00:00
|
|
|
}
|
2026-08-09 02:11:27 +00:00
|
|
|
content = []byte(`{"occurrences":[{"name":"Moonblade","kind":"discovered","quantity":null,"from":null,"to":null,"source_refs":[{"start_unit_id":5,"end_unit_id":5}]}]}`)
|
2026-08-05 20:07:31 +00:00
|
|
|
}
|
2026-11-22 05:29:48 +00:00
|
|
|
case "combat_turns":
|
2026-08-03 21:10:21 +00:00
|
|
|
content = []byte(`{"combat_turns":[{"actor":"Kesh","turn_kind":"turn","source_refs":[{"start_unit_id":8,"end_unit_id":8}]}]}`)
|
2026-11-22 05:29:48 +00:00
|
|
|
case "npc-occurrences":
|
2026-08-03 21:10:21 +00:00
|
|
|
if combatScene {
|
2026-08-05 18:55:58 +00:00
|
|
|
var registry struct {
|
|
|
|
|
NPCs []struct {
|
2026-08-08 14:43:04 +00:00
|
|
|
Name string `json:"name"`
|
2026-08-05 18:55:58 +00:00
|
|
|
} `json:"npcs"`
|
|
|
|
|
}
|
2026-11-22 05:29:48 +00:00
|
|
|
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)
|
2026-08-05 18:55:58 +00:00
|
|
|
}
|
2026-08-08 14:43:04 +00:00
|
|
|
if len(registry.NPCs) == 0 || registry.NPCs[0].Name != "Kesh" {
|
2026-11-22 05:29:48 +00:00
|
|
|
return nil, fmt.Errorf("generated NPC registry has no NPCs")
|
2026-08-05 18:55:58 +00:00
|
|
|
}
|
2026-08-08 14:43:04 +00:00
|
|
|
content = []byte(`{"occurrences":[{"name":"Kesh","kind":"combat_opponent","source_refs":[{"start_unit_id":7,"end_unit_id":7}]}]}`)
|
2026-08-03 21:10:21 +00:00
|
|
|
} else {
|
2026-08-05 18:55:58 +00:00
|
|
|
content = []byte(`{"occurrences":[]}`)
|
2026-08-03 21:10:21 +00:00
|
|
|
}
|
2026-11-22 05:29:48 +00:00
|
|
|
case "location-occurrences":
|
2026-08-04 00:45:50 +00:00
|
|
|
var registry struct {
|
|
|
|
|
Locations []struct {
|
2026-08-08 14:56:43 +00:00
|
|
|
Name string `json:"name"`
|
|
|
|
|
RegistryRefs []struct {
|
|
|
|
|
StartUnitID int `json:"start_unit_id"`
|
|
|
|
|
EndUnitID int `json:"end_unit_id"`
|
|
|
|
|
} `json:"registry_refs"`
|
2026-08-04 00:45:50 +00:00
|
|
|
} `json:"locations"`
|
|
|
|
|
}
|
2026-11-22 05:29:48 +00:00
|
|
|
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)
|
2026-08-04 00:45:50 +00:00
|
|
|
}
|
|
|
|
|
if len(registry.Locations) == 0 {
|
2026-11-22 05:29:48 +00:00
|
|
|
return nil, fmt.Errorf("generated location registry has no locations")
|
2026-08-04 00:45:50 +00:00
|
|
|
}
|
|
|
|
|
unitID := 1
|
2026-08-08 14:56:43 +00:00
|
|
|
location := registry.Locations[0]
|
2026-08-04 00:45:50 +00:00
|
|
|
if combatScene {
|
|
|
|
|
unitID = 7
|
|
|
|
|
if len(registry.Locations) > 1 {
|
2026-08-08 14:56:43 +00:00
|
|
|
location = registry.Locations[1]
|
2026-08-04 00:45:50 +00:00
|
|
|
}
|
|
|
|
|
}
|
2026-08-08 14:56:43 +00:00
|
|
|
registryRefs, err := json.Marshal(location.RegistryRefs)
|
|
|
|
|
if err != nil {
|
2026-11-22 05:29:48 +00:00
|
|
|
return nil, fmt.Errorf("encode location selector: %w", err)
|
2026-08-08 14:56:43 +00:00
|
|
|
}
|
|
|
|
|
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))
|
2026-11-22 05:29:48 +00:00
|
|
|
case "events":
|
2026-08-03 21:10:21 +00:00
|
|
|
content = []byte(`{"events":[{"name":"Kesh","kind":"fled","source_refs":[{"start_unit_id":10,"end_unit_id":10}]}]}`)
|
|
|
|
|
default:
|
2026-11-22 05:29:48 +00:00
|
|
|
return nil, fmt.Errorf("unexpected native response contract %q", role)
|
2026-08-26 23:24:20 +00:00
|
|
|
}
|
2026-11-22 05:29:48 +00:00
|
|
|
|
2026-08-03 21:10:21 +00:00
|
|
|
client.mu.Lock()
|
2026-11-22 05:29:48 +00:00
|
|
|
client.requests = append(client.requests, request)
|
2026-08-03 21:10:21 +00:00
|
|
|
client.mu.Unlock()
|
2026-11-22 05:29:48 +00:00
|
|
|
return &promptkit.GenerateResponse{Content: string(content)}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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 ""
|
2026-08-03 21:10:21 +00:00
|
|
|
}
|
|
|
|
|
|
2026-11-22 05:29:48 +00:00
|
|
|
func (client *enemyEventLLMClient) requestsFor(role string) []promptkit.GenerateRequest {
|
2026-08-03 21:10:21 +00:00
|
|
|
client.mu.Lock()
|
|
|
|
|
defer client.mu.Unlock()
|
2026-11-22 05:29:48 +00:00
|
|
|
var requests []promptkit.GenerateRequest
|
2026-08-03 21:10:21 +00:00
|
|
|
for _, request := range client.requests {
|
2026-11-22 05:29:48 +00:00
|
|
|
if enemyEventRequestRole(request) == role {
|
|
|
|
|
requests = append(requests, request)
|
2026-08-03 21:10:21 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return requests
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func containsString(values []string, want string) bool {
|
|
|
|
|
for _, value := range values {
|
|
|
|
|
if value == want {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func generatedReferenceBinding(bindings []pipeline.ReferenceBinding, slotName string) (pipeline.ReferenceBinding, bool) {
|
|
|
|
|
for _, binding := range bindings {
|
|
|
|
|
if binding.SlotName == slotName && binding.Artifact != nil {
|
|
|
|
|
return binding, true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return pipeline.ReferenceBinding{}, false
|
|
|
|
|
}
|