diff --git a/docs/config.md b/docs/config.md index 7e257b4..56b2ce9 100644 --- a/docs/config.md +++ b/docs/config.md @@ -358,7 +358,7 @@ production validators do not call the LLM and must not set `llm_profile`. | merge | `appendorder` | Combines typed artifacts in chunk order. | | normalize | `noop` | Passes merged typed artifacts through unchanged. | | normalize | `dnd/spells` | Deterministically canonicalizes and de-duplicates typed D&D spell-list artifacts. | -| normalize | `dnd/npcs` | Deterministically consolidates typed D&D NPC-list artifacts by canonical identity and aliases. | +| normalize | `dnd/npcs` | Deterministically consolidates typed D&D NPC-list artifacts by canonical name and unions exact evidence. | | normalize | `dnd/combat-turns` | Deterministically canonicalizes, orders, and de-duplicates typed D&D combat-turn artifacts. | | output | `json` | Produces JSON output files for normalized `application/json` lanes. | @@ -376,8 +376,8 @@ production validators do not call the LLM and must not set `llm_profile`. | `extract/dnd/spells/source_relatedness` | deterministic | Emits warnings when a spell name is not found near its cited source text. | | `extract/dnd/npcs/shape` | deterministic | Rejects malformed D&D NPC-list artifacts. | | `extract/dnd/npcs/source_refs` | deterministic | Rejects missing or invalid D&D NPC source references. | -| `extract/dnd/npcs/source_relatedness` | deterministic | Emits warnings when an NPC name or alias is not found near its cited source text. | -| `normalize/dnd/npcs/identity` | deterministic | Rejects invalid canonical IDs, aliases, and cross-record identity collisions. | +| `extract/dnd/npcs/source_relatedness` | deterministic | Emits warnings when an NPC name is not found near its cited source text. | +| `normalize/dnd/npcs/identity` | deterministic | Rejects invalid canonical IDs and duplicate canonical-name or ID ownership. | | `extract/dnd/combat-turns/shape` | deterministic | Rejects malformed D&D combat-turn artifacts. | | `extract/dnd/combat-turns/source_refs` | deterministic | Rejects missing or invalid D&D combat-turn source references. | | `extract/dnd/combat-turns/source_relatedness` | deterministic | Emits warnings when an actor or declared action is not found near cited source text. | @@ -470,13 +470,13 @@ slot accepts exactly one `application/json` artifact no larger than 1 MiB. An external file is decoded and identity-validated during preparation. A generated binding is validated at the step handoff and is provided to the operation through the same reference contract. In both cases, the model -receives canonical JSON for caster-name grounding. Registry source references +receives a names-only JSON projection for caster-name grounding. Registry source references may belong to the NPC-producing session and are provenance only; they are not spell evidence. Generated reference identity and bounded producer provenance -are recorded by the framework; NPC names, aliases, content, and paths are not -copied into manifests or checkpoint decisions. When absent, the prompt receives -the exact empty value `{"npcs":[]}` and no registry provenance or fingerprint -is recorded. +are recorded by the framework; NPC names, content, and paths are not copied +into manifests. Consumer-local checkpoint identity uses the names-only +projection digest. When absent, the prompt receives the exact empty value +`{"npcs":[]}` with its projection digest and no registry provenance. The `dnd/spells` normalizer declares the same optional `spell_catalog` slot. When an overlay is used, bind it independently under @@ -512,9 +512,9 @@ references: When bound, the combat extractor and normalizer receive the generated registry at operation time. Framework provenance and checkpoint dependencies contain its kind, schema identity, media type, canonical digest, size, and bounded producer -identity; names, aliases, content, and paths are not recorded there. When +identity; names, content, and paths are not recorded there. When absent, the combat prompt receives the exact empty registry value -`{"npcs":[]}` and no registry provenance or fingerprint is recorded. +`{"npcs":[]}` with its projection digest and no registry provenance. ## State Surfaces diff --git a/docs/integrations/dnd-combat-turn-artifacts.md b/docs/integrations/dnd-combat-turn-artifacts.md index 807ccc3..25b38e7 100644 --- a/docs/integrations/dnd-combat-turn-artifacts.md +++ b/docs/integrations/dnd-combat-turn-artifacts.md @@ -172,7 +172,9 @@ The selectable lane uses extractor and normalizer key `dnd/combat-turns`, reference contributes raw-file provenance to the run manifest. A generated binding contributes artifact kind, schema identity, media type, canonical digest, size, and bounded producer provenance. Consumer metadata and checkpoint -fingerprints contain no registry names, aliases, content, paths, or NPC source -ranges. The normalized lane is emitted as `lanes/.json` by the JSON +fingerprints contain no registry names, content, paths, or NPC source ranges. +The component-local registry fingerprint covers only the names projected to the +consumer, while manifest provenance retains the full artifact digest. The +normalized lane is emitted as `lanes/.json` by the JSON output module, and warnings and rejection summaries remain in their shared companion files. diff --git a/docs/integrations/dnd-npc-artifacts.md b/docs/integrations/dnd-npc-artifacts.md index 7c7381f..f9f978e 100644 --- a/docs/integrations/dnd-npc-artifacts.md +++ b/docs/integrations/dnd-npc-artifacts.md @@ -41,13 +41,9 @@ Each NPC contains exactly these required fields: - `id`: `npc:sha256:` followed by 64 lowercase hexadecimal characters; - `name`: the canonical display name; -- `aliases`: an array of alternate display names, which may be empty; -- `description`: a concise description; -- `relationships`: an array of target/relationship objects, which may be empty; - `source_refs`: at least one source reference supporting the NPC record. -Each relationship contains required `target` and `relationship` strings. Each -source reference contains required `source_id`, `start_unit_id`, and +Each source reference contains required `source_id`, `start_unit_id`, and `end_unit_id`; unit IDs are positive integers. Source document identity, unit existence, and range ordering are validated by the source-reference validator when the artifact is used by a pipeline. @@ -76,25 +72,28 @@ The production identities are: The extractor maps private model records to the current source identity and assigns deterministic IDs. Extraction validation checks shape, source references, and source relatedness. The normalizer then consolidates records -by canonical identity or canonical-name/alias matches, preserves the first -record's display and output position, unions relationships and exact evidence, -rewrites unambiguous relationship targets to canonical names, and validates -the retained registry's identity. No LLM is used for consolidation. +only when their normalized canonical names match, preserves the first record's +display and output position, unions exact evidence, and validates the retained +registry's identity. No LLM is used for consolidation. + +The extraction prompt asks only for individually identifiable NPC names backed +by source evidence. Groups, generic roles, invented labels, and descriptive or +relationship enrichment are outside the contract. The default extraction chain is `generic/valid_json`, `generic/valid_json_schema`, `extract/dnd/npcs/shape`, `extract/dnd/npcs/source_refs`, and `extract/dnd/npcs/source_relatedness`. The normalize chain adds `normalize/dnd/npcs/identity` before the source-reference and relatedness -checks. Relatedness emits bounded warnings when an NPC canonical name or -alias is not present near its cited transcript text; opaque campaign +checks. Relatedness emits bounded warnings when an NPC canonical name is not +present near its cited transcript text; opaque campaign references may explain such a warning but do not become evidence. ## Manifest And Artifact Handoff The NPC extractor records prompt and response-schema identities. The durable -codec records only `npc_count`; raw names, aliases, descriptions, source -references, and payload bytes stay in the lane file rather than manifest +codec records only `npc_count`; raw names, source references, and payload bytes +stay in the lane file rather than manifest metadata. The normalized lane can be consumed by a later ordered step through the registered canonical codec: @@ -124,9 +123,16 @@ The framework hands only an accepted normalized artifact across the barrier. It validates the canonical bytes against each consumer slot and clones the operation-time reference for the spell and combat consumers. Generated provenance records the artifact kind, schema identity, media type, canonical -digest, size, and producer step/lane/module, but not names, aliases, source +digest, size, and producer step/lane/module, but not names, source ranges, or payload bytes. External normalized files remain supported as explicit references and retain their file provenance. NPC source references are registry provenance and are never accepted as spell or combat evidence. Current transcript units remain the only event evidence. + +Consumers receive a separate names-only projection in normalized registry +order, for example `{"npcs":[{"name":"Mira Thorn"}]}`. The projection omits +IDs and evidence. Its digest covers the exact projected bytes and is used for +consumer-local checkpoint identity, while the full durable artifact digest +remains the manifest and generated-reference provenance identity. The unbound +projection is exactly `{"npcs":[]}` and also has a projection digest. diff --git a/docs/integrations/dnd-spell-artifacts.md b/docs/integrations/dnd-spell-artifacts.md index f25ee73..557e5f9 100644 --- a/docs/integrations/dnd-spell-artifacts.md +++ b/docs/integrations/dnd-spell-artifacts.md @@ -102,17 +102,17 @@ The `dnd/spells` extractor accepts an optional `npcs` reference containing one normalized NPC artifact as `application/json`, up to 1 MiB. An external file is validated during preparation; an ordered generated binding is validated at the step handoff. Both paths use the approved NPC codec and identity policy, -re-encode canonical durable JSON, and supply that JSON as an operation-time -spell prompt input. It helps the model prefer canonical caster names and -recognize aliases; it does not establish that a spell was cast. +re-encode canonical durable JSON for registry provenance, and supply only the +registry's ordered names as the operation-time spell prompt input. It helps the model prefer canonical +caster names; it does not establish that a spell was cast. NPC source references may identify the run that produced the registry or any other session. They remain registry provenance and are never copied into a spell cast's `source_refs`; every spell evidence range must still identify the current transcript. Generated provenance records producer and canonical artifact identity without payload content or a path. When the slot is absent, -the prompt receives exactly `{"npcs":[]}` and the run has no NPC reference -provenance or NPC checkpoint fingerprint. +the prompt receives exactly `{"npcs":[]}` with its projection digest, and the +run has no NPC reference provenance. ## Normalization Behavior @@ -196,7 +196,7 @@ provenance; see the [JSON output contract](json-output.md#manifestjson). The `npc_registry_digest` and `npc_count` fields in the example are present for an external NPC registry when the extractor publishes its prepared module -metadata. They contain no NPC names, aliases, source references, paths, or raw +metadata. They contain no NPC names, source references, paths, or raw bytes. A generated registry's identity is instead represented by the framework handoff provenance and dependency fingerprint, so the consumer module metadata does not duplicate it. diff --git a/docs/internal/llm.md b/docs/internal/llm.md index 0babdb2..f14d32a 100644 --- a/docs/internal/llm.md +++ b/docs/internal/llm.md @@ -170,13 +170,13 @@ The shared D&D prompt assets include a generic NPC grounding fragment directly after the campaign reference message for spell and combat prompts. When an NPC registry is bound, the domain registry boundary strictly decodes and identity-validates one durable -artifact, re-encodes canonical JSON, and generates a semantic digest over -those bytes. The unbound input is exactly `{"npcs":[]}`. Input digests cover -the generated bytes; manifests record catalog identity and optional NPC -registry digest/count rather than names, aliases, overlay bytes, registry -paths, or source metadata. Combat prompt, response-schema, mapping, -normalization, identity, and bound-registry fingerprints remain separate -semantic inputs to checkpoint identity. +artifact, re-encodes canonical JSON for provenance, and separately generates a +names-only prompt projection. The unbound projection is exactly `{"npcs":[]}`. +Prompt input and component-local checkpoint digests cover the projected bytes; +manifests retain the optional full registry digest/count rather than names, +overlay bytes, registry paths, or source metadata. Combat prompt, +response-schema, mapping, normalization, identity, and registry-projection +fingerprints remain separate semantic inputs to checkpoint identity. ## Debug And Redaction Boundaries diff --git a/docs/internal/modules.md b/docs/internal/modules.md index 57b3c93..1915bb6 100644 --- a/docs/internal/modules.md +++ b/docs/internal/modules.md @@ -109,8 +109,8 @@ The NPC identity package owns Unicode comparison keys, deterministic `npc:sha256:` IDs, display normalization, and whole-registry collision issues. The registry package resolves one optional normalized artifact through the strict codec, validates whole-registry identity, canonicalizes its JSON, and -provides immutable records, prompt input, semantic digest, count, and exact -canonical-name/alias lookup. External files cross this boundary during +provides immutable records, a names-only prompt projection, distinct durable +and projection digests, count, and exact canonical-name lookup. External files cross this boundary during preparation; generated artifacts cross it at the ordered step handoff. It owns the `npcs` slot and its bounded, content-safe validation failures. NPC source references are durable provenance and are not treated as evidence for a @@ -239,7 +239,8 @@ validated and supplied at operation time. External bindings may add only `npc_registry_digest` and `npc_count` to module metadata and an `npc_registry` checkpoint fingerprint. Generated bindings are represented by framework handoff provenance and dependency fingerprints. The unbound prompt -input is exactly `{"npcs":[]}` and has no registry provenance or fingerprint. +input is exactly `{"npcs":[]}`, has a projection fingerprint, and has no +registry provenance. The shared NPC grounding fragment is placed immediately after the common campaign reference message and is included in the spell prompt fingerprint. @@ -253,6 +254,9 @@ assigns source identity and deterministic NPC IDs, and preserves source references for deterministic validation. It uses the shared campaign references only for disambiguation and does not consume the optional NPC registry slot. Its prompt and private response schema are package-owned. The +private response contains only a name and model-facing evidence ranges for each +record; anonymous groups, generic roles, invented labels, descriptions, +aliases, and relationships are outside its contract. The prompt follows the shared D&D extraction ordering and cache policy documented in [LLM Runtime](llm.md#dd-extraction-prompt-ordering-and-cache-boundaries). @@ -286,11 +290,10 @@ for deterministic normalization. ### `internal/modules/dnd/normalize/npcs` The NPC normalizer performs deterministic identity-aware consolidation in -merged input order. It unions only canonical identity or canonical/alias -matches, retains the first display record, unions exact relationships and -source references, rewrites unambiguous relationship targets, and leaves -ambiguous collisions for identity validation. It exposes the identity policy -as its local checkpoint fingerprint and emits bounded normalization warnings. +merged input order. It consolidates only equal canonical-name comparison keys, +retains the first display record, and unions exact source references. It exposes +the identity policy as its local checkpoint fingerprint and emits bounded +normalization warnings. ## Merger And Normalizer @@ -400,15 +403,15 @@ payload rules are defined in the ## D&D NPC Validators -NPC shape validation checks required strings, arrays, and source-reference +NPC shape validation checks the required ID and name strings, list presence, and source-reference shape. The source-reference validator defers malformed shapes, checks current-document identity, unit existence, and range ordering, and reports all defects through bounded aggregates. Source relatedness uses the shared document-order traversal and normalized consecutive-token matching, emitting at -most one bounded warning per record when neither the canonical name nor an -alias occurs near its cited text. Invalid shape or cited ranges produce no -relatedness warnings. Normalize identity validation checks deterministic IDs, -canonical names, aliases, and cross-record ownership or canonical collisions. +most one bounded warning per record when the canonical name does not occur near +its cited text. Invalid shape or cited ranges produce no relatedness warnings. +Normalize identity validation checks deterministic IDs, canonical names, and +duplicate canonical-name or ID ownership. All are deterministic and expose the policy fingerprints used by the production chains. diff --git a/docs/internal/overview.md b/docs/internal/overview.md index 785166c..5e2314e 100644 --- a/docs/internal/overview.md +++ b/docs/internal/overview.md @@ -87,7 +87,7 @@ Configuration. The implemented module packages are: | `internal/modules/seriatim/input/transcript` | Parses the supported Seriatim transcript format into the generic source model. | | `internal/modules/generic/chunk/units` | Splits ordered source units by unit count and overlap. | | `internal/modules/dnd/chunk/scenes` | Produces contiguous D&D scene chunks from structured model output. | -| `internal/modules/dnd` | Owns the canonical D&D spell-list, spell-cast, NPC-list, NPC, relationship, combat-turn-list, combat-turn, and combat-action artifact types. | +| `internal/modules/dnd` | Owns the canonical D&D spell-list, spell-cast, NPC-list, NPC, combat-turn-list, combat-turn, and combat-action artifact types. | | `internal/modules/dnd/codec/spells` | Strictly decodes and stably encodes the durable D&D spell-list representation. | | `internal/modules/dnd/codec/npcs` | Strictly decodes and stably encodes the durable D&D NPC-list representation. | | `internal/modules/dnd/codec/combatturns` | Strictly decodes and stably encodes the durable D&D combat-turn-list representation. | @@ -102,7 +102,7 @@ Configuration. The implemented module packages are: | `internal/modules/generic/merge/appendorder` | Combines accepted extraction results in chunk order. | | `internal/modules/generic/normalize/noop` | Preserves accepted merged output. | | `internal/modules/dnd/normalize/spells` | Canonicalizes catalog-backed spell names and exact source references, conservatively collapses duplicate casts, and reports deterministic warnings and independently scoped catalog checkpoint identity. | -| `internal/modules/dnd/normalize/npcs` | Consolidates NPC records deterministically by identity and aliases, rewrites unambiguous relationship targets, and reports bounded warnings. | +| `internal/modules/dnd/normalize/npcs` | Consolidates NPC records deterministically by canonical name, unions exact evidence, and reports bounded warnings. | | `internal/modules/generic/output/json` | Encodes manifests, lane payloads, warnings, and rejections as logical JSON files. | `internal/modules/dnd/shared` owns reusable D&D prompt fragments, diff --git a/docs/operations.md b/docs/operations.md index 1588ab3..fad8b33 100644 --- a/docs/operations.md +++ b/docs/operations.md @@ -55,7 +55,7 @@ go run ./cmd/notarius run dnd-npc-grounded \ --output-dir ./npc-grounded-output ``` -The NPC artifact grounds canonical names and aliases, not spell or combat +The NPC artifact grounds canonical names through a names-only prompt projection, not spell or combat evidence. Current-transcript source ranges remain the only event evidence. The manifest records generated-reference identity and bounded producer provenance; it does not record generated payload content, and no generated content is diff --git a/internal/modules/dnd/codec/npcs/assets/schemas/dnd_npcs.v1.json b/internal/modules/dnd/codec/npcs/assets/schemas/dnd_npcs.v1.json index 70188c2..b095116 100644 --- a/internal/modules/dnd/codec/npcs/assets/schemas/dnd_npcs.v1.json +++ b/internal/modules/dnd/codec/npcs/assets/schemas/dnd_npcs.v1.json @@ -13,9 +13,6 @@ "required": [ "id", "name", - "aliases", - "description", - "relationships", "source_refs" ], "properties": { @@ -27,35 +24,6 @@ "type": "string", "minLength": 1 }, - "aliases": { - "type": "array", - "items": { - "type": "string", - "minLength": 1 - } - }, - "description": { - "type": "string", - "minLength": 1 - }, - "relationships": { - "type": "array", - "items": { - "type": "object", - "additionalProperties": false, - "required": ["target", "relationship"], - "properties": { - "target": { - "type": "string", - "minLength": 1 - }, - "relationship": { - "type": "string", - "minLength": 1 - } - } - } - }, "source_refs": { "type": "array", "minItems": 1, diff --git a/internal/modules/dnd/codec/npcs/codec.go b/internal/modules/dnd/codec/npcs/codec.go index 8f296ce..26646cb 100644 --- a/internal/modules/dnd/codec/npcs/codec.go +++ b/internal/modules/dnd/codec/npcs/codec.go @@ -106,29 +106,6 @@ func validate(value dnd.NPCList) error { if strings.TrimSpace(npc.Name) == "" { return fmt.Errorf("%s.name must not be empty", prefix) } - if npc.Aliases == nil { - return fmt.Errorf("%s.aliases must be present", prefix) - } - for aliasIndex, alias := range npc.Aliases { - if strings.TrimSpace(alias) == "" { - return fmt.Errorf("%s.aliases[%d] must not be empty", prefix, aliasIndex) - } - } - if strings.TrimSpace(npc.Description) == "" { - return fmt.Errorf("%s.description must not be empty", prefix) - } - if npc.Relationships == nil { - return fmt.Errorf("%s.relationships must be present", prefix) - } - for relationshipIndex, relationship := range npc.Relationships { - relationshipPrefix := fmt.Sprintf("%s.relationships[%d]", prefix, relationshipIndex) - if strings.TrimSpace(relationship.Target) == "" { - return fmt.Errorf("%s.target must not be empty", relationshipPrefix) - } - if strings.TrimSpace(relationship.Relationship) == "" { - return fmt.Errorf("%s.relationship must not be empty", relationshipPrefix) - } - } if len(npc.SourceRefs) == 0 { return fmt.Errorf("%s.source_refs must not be empty", prefix) } diff --git a/internal/modules/dnd/codec/npcs/codec_test.go b/internal/modules/dnd/codec/npcs/codec_test.go index 319bba4..8ddd07a 100644 --- a/internal/modules/dnd/codec/npcs/codec_test.go +++ b/internal/modules/dnd/codec/npcs/codec_test.go @@ -17,13 +17,8 @@ import ( func validList() dnd.NPCList { return dnd.NPCList{NPCs: []dnd.NPC{{ - ID: identity.DeriveID("Mira Thorn"), - Name: "Mira Thorn", - Aliases: []string{"The Greencloak"}, - Description: "A guarded ranger who watches the northern road.", - Relationships: []dnd.NPCRelationship{{ - Target: "Captain Vale", Relationship: "reports to", - }}, + ID: identity.DeriveID("Mira Thorn"), + Name: "Mira Thorn", SourceRefs: []source.SourceRef{{SourceID: "session-alpha", StartUnitID: 1, EndUnitID: 2}}, }}} } @@ -86,10 +81,11 @@ func TestCodecStrictlyRejectsMalformedOrUnknownJSON(t *testing.T) { want string }{ {name: "unknown top-level", raw: `{"npcs":[],"unexpected":true}`, want: "unknown field"}, - {name: "unknown nested", raw: `{"npcs":[{"id":"x","name":"Mira","aliases":[],"description":"desc","relationships":[],"source_refs":[{"source_id":"s","start_unit_id":1,"end_unit_id":1}],"unexpected":true}]}`, want: "unknown field"}, + {name: "unknown nested", raw: `{"npcs":[{"id":"x","name":"Mira","source_refs":[{"source_id":"s","start_unit_id":1,"end_unit_id":1}],"unexpected":true}]}`, want: "unknown field"}, + {name: "removed enrichment", raw: `{"npcs":[{"id":"x","name":"Mira","aliases":[],"source_refs":[{"source_id":"s","start_unit_id":1,"end_unit_id":1}]}]}`, want: "unknown field"}, {name: "trailing", raw: `{"npcs":[]} {}`, want: "multiple JSON values"}, {name: "missing array", raw: `{}`, want: "npcs must be present"}, - {name: "invalid reference", raw: `{"npcs":[{"id":"npc:sha256:0000000000000000000000000000000000000000000000000000000000000000","name":"Mira","aliases":[],"description":"desc","relationships":[],"source_refs":[{"source_id":"s","start_unit_id":0,"end_unit_id":1}]}]}`, want: "start_unit_id"}, + {name: "invalid reference", raw: `{"npcs":[{"id":"npc:sha256:0000000000000000000000000000000000000000000000000000000000000000","name":"Mira","source_refs":[{"source_id":"s","start_unit_id":0,"end_unit_id":1}]}]}`, want: "start_unit_id"}, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { @@ -103,7 +99,7 @@ func TestCodecStrictlyRejectsMalformedOrUnknownJSON(t *testing.T) { func TestCodecCandidatePreservesInvalidTypedValues(t *testing.T) { codec := New() - candidate := dnd.NPCList{NPCs: []dnd.NPC{{Name: "Mira Thorn", Aliases: []string{}, Relationships: []dnd.NPCRelationship{}, SourceRefs: []source.SourceRef{}}}} + candidate := dnd.NPCList{NPCs: []dnd.NPC{{Name: "Mira Thorn", SourceRefs: []source.SourceRef{}}}} content, err := codec.EncodeCandidate(candidate) if err != nil || !json.Valid(content) { t.Fatalf("EncodeCandidate() = %s, %v; want JSON", content, err) @@ -124,11 +120,8 @@ func TestCodecRejectsEveryRequiredShapeBoundary(t *testing.T) { value dnd.NPCList want string }{ - {name: "nil aliases", value: dnd.NPCList{NPCs: []dnd.NPC{{ID: base.ID, Name: base.Name, Description: base.Description, Relationships: []dnd.NPCRelationship{}, SourceRefs: base.SourceRefs}}}, want: "aliases must be present"}, - {name: "empty alias", value: dnd.NPCList{NPCs: []dnd.NPC{{ID: base.ID, Name: base.Name, Aliases: []string{" "}, Description: base.Description, Relationships: []dnd.NPCRelationship{}, SourceRefs: base.SourceRefs}}}, want: "aliases[0] must not be empty"}, - {name: "nil relationships", value: dnd.NPCList{NPCs: []dnd.NPC{{ID: base.ID, Name: base.Name, Aliases: []string{}, Description: base.Description, SourceRefs: base.SourceRefs}}}, want: "relationships must be present"}, - {name: "empty relationship target", value: dnd.NPCList{NPCs: []dnd.NPC{{ID: base.ID, Name: base.Name, Aliases: []string{}, Description: base.Description, Relationships: []dnd.NPCRelationship{{Relationship: "knows"}}, SourceRefs: base.SourceRefs}}}, want: "target must not be empty"}, - {name: "empty source refs", value: dnd.NPCList{NPCs: []dnd.NPC{{ID: base.ID, Name: base.Name, Aliases: []string{}, Description: base.Description, Relationships: []dnd.NPCRelationship{}, SourceRefs: []source.SourceRef{}}}}, want: "source_refs must not be empty"}, + {name: "blank name", value: dnd.NPCList{NPCs: []dnd.NPC{{ID: base.ID, Name: " ", SourceRefs: base.SourceRefs}}}, want: "name must not be empty"}, + {name: "empty source refs", value: dnd.NPCList{NPCs: []dnd.NPC{{ID: base.ID, Name: base.Name, SourceRefs: []source.SourceRef{}}}}, want: "source_refs must not be empty"}, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { diff --git a/internal/modules/dnd/codec/npcs/testdata/dnd_npcs.v1.json b/internal/modules/dnd/codec/npcs/testdata/dnd_npcs.v1.json index 2ce9a15..7975106 100644 --- a/internal/modules/dnd/codec/npcs/testdata/dnd_npcs.v1.json +++ b/internal/modules/dnd/codec/npcs/testdata/dnd_npcs.v1.json @@ -3,9 +3,6 @@ { "id": "npc:sha256:99a16589618a04f535a7d21fdcc71a0b1c05d22f752cd492065b1086d97bc3d7", "name": "Mira Thorn", - "aliases": ["The Greencloak"], - "description": "A guarded ranger who watches the northern road.", - "relationships": [{"target": "Captain Vale", "relationship": "reports to"}], "source_refs": [{"source_id": "session-alpha", "start_unit_id": 1, "end_unit_id": 2}] } ] diff --git a/internal/modules/dnd/extract/combatturns/extractor.go b/internal/modules/dnd/extract/combatturns/extractor.go index 2502272..ccfc8ab 100644 --- a/internal/modules/dnd/extract/combatturns/extractor.go +++ b/internal/modules/dnd/extract/combatturns/extractor.go @@ -133,9 +133,7 @@ func (e *Extractor) CheckpointFingerprints() []pipeline.CheckpointFingerprint { {Name: "mapping_policy", Value: mappingPolicy}, } seeded := e.npcResolver.Seeded() - if seeded.Bound() { - fingerprints = append(fingerprints, pipeline.CheckpointFingerprint{Name: "npc_registry", Value: seeded.Digest()}) - } + fingerprints = append(fingerprints, pipeline.CheckpointFingerprint{Name: "npc_registry", Value: seeded.ProjectionDigest()}) return fingerprints } diff --git a/internal/modules/dnd/extract/combatturns/extractor_test.go b/internal/modules/dnd/extract/combatturns/extractor_test.go index 73668c2..081a28e 100644 --- a/internal/modules/dnd/extract/combatturns/extractor_test.go +++ b/internal/modules/dnd/extract/combatturns/extractor_test.go @@ -146,17 +146,15 @@ func TestExtractUnboundRegistryUsesExactEmptyPromptAndOmitsIdentity(t *testing.T t.Fatalf("Extract() error = %v, want nil", err) } input := client.requests[0].Inputs[NPCRegistryReferenceSlot] - if string(input.Content) != `{"npcs":[]}` || input.Digest != "" || input.OriginURI != "" { + if string(input.Content) != `{"npcs":[]}` || input.Digest == "" || input.OriginURI != "" { t.Fatalf("unbound registry input = %#v, want exact empty prompt without identity", input) } metadata := extractor.ManifestMetadata() if _, ok := metadata["npc_registry_digest"]; ok { t.Fatalf("unbound metadata has registry digest: %#v", metadata) } - for _, fingerprint := range extractor.CheckpointFingerprints() { - if fingerprint.Name == "npc_registry" { - t.Fatalf("unbound fingerprints include registry identity: %#v", extractor.CheckpointFingerprints()) - } + if fingerprints := extractor.CheckpointFingerprints(); len(fingerprints) != 4 || fingerprints[3].Name != "npc_registry" || fingerprints[3].Value != input.Digest { + t.Fatalf("unbound fingerprints = %#v, want empty-projection identity", fingerprints) } } @@ -175,7 +173,7 @@ func TestExtractorResolvesOperationNPCOverrideWithoutSingletonMetadata(t *testin t.Fatalf("Extract() error = %v", err) } input := client.requests[0].Inputs[NPCRegistryReferenceSlot] - if input.Digest == "" || string(input.Content) != string(content) || input.OriginURI != "" { + if input.Digest == "" || string(input.Content) != `{"npcs":[{"name":"Mira Thorn"}]}` || input.OriginURI != "" { t.Fatalf("operation NPC input = %#v, want generated canonical grounding without provenance", input) } if metadata := extractor.ManifestMetadata(); metadata["npc_registry_digest"] != nil || metadata["npc_count"] != nil { @@ -252,7 +250,7 @@ func TestExtractorManifestMetadataAndFingerprints(t *testing.T) { t.Fatalf("metadata[%q] = %#v, want digest", key, metadata[key]) } } - wantNames := map[string]struct{}{"prompt": {}, "response_schema": {}, "mapping_policy": {}} + wantNames := map[string]struct{}{"prompt": {}, "response_schema": {}, "mapping_policy": {}, "npc_registry": {}} for _, fingerprint := range extractor.CheckpointFingerprints() { if _, ok := wantNames[fingerprint.Name]; !ok { t.Fatalf("unexpected fingerprint = %#v", fingerprint) @@ -340,10 +338,7 @@ func newExtractor(t *testing.T, client contracts.StructuredLLMClient, references func npcRegistryJSON(t *testing.T) []byte { t.Helper() - value := dnd.NPCList{NPCs: []dnd.NPC{{ - ID: identity.DeriveID("Mira Thorn"), Name: "Mira Thorn", Aliases: []string{"The Greencloak"}, - Description: "A ranger.", Relationships: []dnd.NPCRelationship{}, SourceRefs: []source.SourceRef{{SourceID: "other-session", StartUnitID: 1, EndUnitID: 1}}, - }}} + value := dnd.NPCList{NPCs: []dnd.NPC{{ID: identity.DeriveID("Mira Thorn"), Name: "Mira Thorn", SourceRefs: []source.SourceRef{{SourceID: "other-session", StartUnitID: 1, EndUnitID: 1}}}}} content, err := npccodec.New().Encode(value) if err != nil { t.Fatalf("encode NPC registry: %v", err) diff --git a/internal/modules/dnd/extract/npcs/assets/prompts/instructions.md b/internal/modules/dnd/extract/npcs/assets/prompts/instructions.md index 65071fe..4b51915 100644 --- a/internal/modules/dnd/extract/npcs/assets/prompts/instructions.md +++ b/internal/modules/dnd/extract/npcs/assets/prompts/instructions.md @@ -1,11 +1,10 @@ -For every NPC record, cite transcript units that support the canonical name, -every alias, the description, and every relationship. +For every NPC record, return only the observed display name and transcript +units that support that identity. -Descriptions must be short session records, not biographies, statistics, -alignment, motivations, or lore inferred from general D&D knowledge. Do not -summarize every action or follow a participant through unrelated scenes. -Relationships must be stated or directly demonstrated by cited transcript -units, not inferred from game lore. +Do not return descriptions, aliases, relationships, biographies, statistics, +alignment, motivations, encounter summaries, or lore inferred from general +D&D knowledge. Do not invent a label for an anonymous creature, crowd, or +generic role. -Return aliases and relationships as arrays, including empty arrays when there -are none. Preserve observed display spelling. +Preserve observed display spelling. Return at least one narrow source range for +every record. diff --git a/internal/modules/dnd/extract/npcs/assets/prompts/task.md b/internal/modules/dnd/extract/npcs/assets/prompts/task.md index 04bcc1b..30c087d 100644 --- a/internal/modules/dnd/extract/npcs/assets/prompts/task.md +++ b/internal/modules/dnd/extract/npcs/assets/prompts/task.md @@ -1,15 +1,12 @@ -Extract a concise Dungeons & Dragons non-player-character registry from the -provided transcript. +Extract the individually identifiable Dungeons & Dragons non-player characters +established by the provided transcript and cite where each identity appears. -Include an in-world non-PC participant when the transcript establishes that it -appears, acts, speaks, or is materially discussed and gives it a proper name, -a stable alias or title, or an individually useful distinguishing description. +Include an in-world non-PC participant only when the transcript gives it a +proper name or a stable, individually distinguishing title or alias. Exclude human players, transcript speakers, and the GM as out-of-world people, player characters identified by the player or party references, incidental or -hypothetical name drops, corrected transcription mistakes, indistinguishable -crowds or groups, and temporary summoned creatures or spell effects without a -persistent individual identity. - -Keep each description concise and limited to facts established by the -transcript. Include only explicitly supported aliases and relationships. +hypothetical name drops, corrected transcription mistakes, anonymous or +generic roles, indistinguishable crowds or groups, invented descriptive labels, +and temporary summoned creatures or spell effects without a persistent +individual identity. diff --git a/internal/modules/dnd/extract/npcs/assets/schemas/dnd_npcs_llm.v1.json b/internal/modules/dnd/extract/npcs/assets/schemas/dnd_npcs_llm.v1.json index 800eb6e..771065b 100644 --- a/internal/modules/dnd/extract/npcs/assets/schemas/dnd_npcs_llm.v1.json +++ b/internal/modules/dnd/extract/npcs/assets/schemas/dnd_npcs_llm.v1.json @@ -12,40 +12,12 @@ "additionalProperties": false, "required": [ "name", - "aliases", - "description", - "relationships", "source_refs" ], "properties": { "name": { "type": "string" }, - "aliases": { - "type": "array", - "items": { - "type": "string" - } - }, - "description": { - "type": "string" - }, - "relationships": { - "type": "array", - "items": { - "type": "object", - "additionalProperties": false, - "required": ["target", "relationship"], - "properties": { - "target": { - "type": "string" - }, - "relationship": { - "type": "string" - } - } - } - }, "source_refs": { "type": "array", "items": { diff --git a/internal/modules/dnd/extract/npcs/canonicalize.go b/internal/modules/dnd/extract/npcs/canonicalize.go index e05c5d7..2e41c31 100644 --- a/internal/modules/dnd/extract/npcs/canonicalize.go +++ b/internal/modules/dnd/extract/npcs/canonicalize.go @@ -98,35 +98,14 @@ func canonicalNPCList(response extractionResponse, sourceID string) dnd.NPCList npcs := make([]dnd.NPC, len(response.NPCs)) for index, npc := range response.NPCs { npcs[index] = dnd.NPC{ - ID: identity.DeriveID(npc.Name), - Name: npc.Name, - Aliases: cloneStrings(npc.Aliases), - Description: npc.Description, - Relationships: cloneRelationships(npc.Relationships), - SourceRefs: canonicalSourceRefs(npc.SourceRefs, sourceID), + ID: identity.DeriveID(npc.Name), + Name: npc.Name, + SourceRefs: canonicalSourceRefs(npc.SourceRefs, sourceID), } } return dnd.NPCList{NPCs: npcs} } -func cloneStrings(values []string) []string { - if values == nil { - return nil - } - return append([]string{}, values...) -} - -func cloneRelationships(values []npcRelationshipResponse) []dnd.NPCRelationship { - if values == nil { - return nil - } - out := make([]dnd.NPCRelationship, len(values)) - for index, value := range values { - out[index] = dnd.NPCRelationship{Target: value.Target, Relationship: value.Relationship} - } - return out -} - func canonicalSourceRefs(values []npcSourceRefResponse, sourceID string) []source.SourceRef { if values == nil { return nil diff --git a/internal/modules/dnd/extract/npcs/extractor_test.go b/internal/modules/dnd/extract/npcs/extractor_test.go index dc48fe0..dfa85b9 100644 --- a/internal/modules/dnd/extract/npcs/extractor_test.go +++ b/internal/modules/dnd/extract/npcs/extractor_test.go @@ -16,13 +16,10 @@ import ( func TestExtractReturnsCanonicalNPCListFromPrivateResponse(t *testing.T) { client := &fakeNPCsLLMClient{response: extractionResponse{NPCs: []npcResponse{ { - Name: "Captain Vale", Aliases: []string{"The Captain"}, Description: "A road captain.", - Relationships: []npcRelationshipResponse{{Target: "Mira Thorn", Relationship: "reports to"}}, - SourceRefs: responseSourceRefs(3, 3), + Name: "Captain Vale", SourceRefs: responseSourceRefs(3, 3), }, { - Name: "Mira Thorn", Aliases: []string{"The Greencloak"}, Description: "A guarded ranger.", - Relationships: []npcRelationshipResponse{{Target: "Captain Vale", Relationship: "commands"}}, + Name: "Mira Thorn", SourceRefs: []npcSourceRefResponse{ {StartUnitID: 2, EndUnitID: 2}, {StartUnitID: 1, EndUnitID: 2}, @@ -36,8 +33,8 @@ func TestExtractReturnsCanonicalNPCListFromPrivateResponse(t *testing.T) { t.Fatalf("Extract() error = %v, want nil", err) } want := dnd.NPCList{NPCs: []dnd.NPC{ - {ID: identity.DeriveID("Mira Thorn"), Name: "Mira Thorn", Aliases: []string{"The Greencloak"}, Description: "A guarded ranger.", Relationships: []dnd.NPCRelationship{{Target: "Captain Vale", Relationship: "commands"}}, SourceRefs: []source.SourceRef{{SourceID: "session-alpha", StartUnitID: 1, EndUnitID: 2}, {SourceID: "session-alpha", StartUnitID: 2, EndUnitID: 2}}}, - {ID: identity.DeriveID("Captain Vale"), Name: "Captain Vale", Aliases: []string{"The Captain"}, Description: "A road captain.", Relationships: []dnd.NPCRelationship{{Target: "Mira Thorn", Relationship: "reports to"}}, SourceRefs: []source.SourceRef{{SourceID: "session-alpha", StartUnitID: 3, EndUnitID: 3}}}, + {ID: identity.DeriveID("Mira Thorn"), Name: "Mira Thorn", SourceRefs: []source.SourceRef{{SourceID: "session-alpha", StartUnitID: 1, EndUnitID: 2}, {SourceID: "session-alpha", StartUnitID: 2, EndUnitID: 2}}}, + {ID: identity.DeriveID("Captain Vale"), Name: "Captain Vale", SourceRefs: []source.SourceRef{{SourceID: "session-alpha", StartUnitID: 3, EndUnitID: 3}}}, }} if !reflect.DeepEqual(result.Value, want) { t.Fatalf("Value = %#v, want %#v", result.Value, want) @@ -58,11 +55,11 @@ func TestExtractReturnsCanonicalNPCListFromPrivateResponse(t *testing.T) { func TestExtractOrdersNPCsBySourcePositionRatherThanUnitID(t *testing.T) { client := &fakeNPCsLLMClient{response: extractionResponse{NPCs: []npcResponse{ { - Name: "Later NPC", Aliases: []string{}, Description: "Appears later.", Relationships: []npcRelationshipResponse{}, + Name: "Later NPC", SourceRefs: responseSourceRefs(10, 10), }, { - Name: "Earlier NPC", Aliases: []string{}, Description: "Appears first.", Relationships: []npcRelationshipResponse{}, + Name: "Earlier NPC", SourceRefs: []npcSourceRefResponse{ {StartUnitID: 50, EndUnitID: 50}, {StartUnitID: 100, EndUnitID: 100}, @@ -109,14 +106,14 @@ func TestExtractPassesCampaignReferencesAsPromptInputs(t *testing.T) { func TestExtractPreservesMalformedCandidatesForValidators(t *testing.T) { client := &fakeNPCsLLMClient{response: extractionResponse{NPCs: []npcResponse{{ - Name: "", Aliases: nil, Description: "", Relationships: nil, + Name: "", SourceRefs: []npcSourceRefResponse{{StartUnitID: 99, EndUnitID: 0}}, }}}} result, err := newExtractor(t, client).Extract(context.Background(), extractionRequest()) if err != nil { t.Fatalf("Extract() error = %v, want nil", err) } - if len(result.Value.NPCs) != 1 || result.Value.NPCs[0].ID != "" || result.Value.NPCs[0].Name != "" || result.Value.NPCs[0].Aliases != nil || result.Value.NPCs[0].Relationships != nil { + if len(result.Value.NPCs) != 1 || result.Value.NPCs[0].ID != "" || result.Value.NPCs[0].Name != "" { t.Fatalf("malformed candidate = %#v, want invalid values preserved", result.Value) } if refs := result.Value.NPCs[0].SourceRefs; len(refs) != 1 || refs[0].SourceID != "session-alpha" || refs[0].StartUnitID != 99 || refs[0].EndUnitID != 0 { @@ -125,13 +122,13 @@ func TestExtractPreservesMalformedCandidatesForValidators(t *testing.T) { } func TestExtractMapsRawSemanticCandidatesWithoutRepair(t *testing.T) { - client := &fakeNPCsLLMClient{content: []byte(`{"npcs":[{"name":"","aliases":[],"description":"","relationships":[],"source_refs":[{"start_unit_id":0,"end_unit_id":-1}]}]}`)} + client := &fakeNPCsLLMClient{content: []byte(`{"npcs":[{"name":"","source_refs":[{"start_unit_id":0,"end_unit_id":-1}]}]}`)} result, err := newExtractor(t, client).Extract(context.Background(), extractionRequest()) if err != nil { t.Fatalf("Extract() error = %v, want nil", err) } npc := result.Value.NPCs[0] - if npc.ID != "" || npc.Name != "" || npc.Description != "" { + if npc.ID != "" || npc.Name != "" { t.Fatalf("NPC = %#v, want blank semantic values preserved", npc) } if refs := npc.SourceRefs; len(refs) != 1 || refs[0] != (source.SourceRef{SourceID: "session-alpha", StartUnitID: 0, EndUnitID: -1}) { diff --git a/internal/modules/dnd/extract/npcs/model.go b/internal/modules/dnd/extract/npcs/model.go index fcff5a1..b63c3e7 100644 --- a/internal/modules/dnd/extract/npcs/model.go +++ b/internal/modules/dnd/extract/npcs/model.go @@ -5,16 +5,8 @@ type extractionResponse struct { } type npcResponse struct { - Name string `json:"name"` - Aliases []string `json:"aliases"` - Description string `json:"description"` - Relationships []npcRelationshipResponse `json:"relationships"` - SourceRefs []npcSourceRefResponse `json:"source_refs"` -} - -type npcRelationshipResponse struct { - Target string `json:"target"` - Relationship string `json:"relationship"` + Name string `json:"name"` + SourceRefs []npcSourceRefResponse `json:"source_refs"` } type npcSourceRefResponse struct { diff --git a/internal/modules/dnd/extract/npcs/schema_test.go b/internal/modules/dnd/extract/npcs/schema_test.go index 2d88b0e..bfb86e3 100644 --- a/internal/modules/dnd/extract/npcs/schema_test.go +++ b/internal/modules/dnd/extract/npcs/schema_test.go @@ -18,7 +18,7 @@ func TestLoadResponseSchemaUsesPrivateNPCSchema(t *testing.T) { t.Fatalf("schema = %#v, want private NPC schema identity", schema) } valid := map[string]any{"npcs": []any{map[string]any{ - "name": "Mira Thorn", "aliases": []any{}, "description": "A ranger.", "relationships": []any{}, + "name": "Mira Thorn", "source_refs": []any{map[string]any{"start_unit_id": 1, "end_unit_id": 2}}, }}} validJSON, err := json.Marshal(valid) @@ -36,14 +36,14 @@ func TestLoadResponseSchemaUsesPrivateNPCSchema(t *testing.T) { { name: "semantic blanks and empty collections", response: map[string]any{"npcs": []any{map[string]any{ - "name": "", "aliases": []any{""}, "description": "", "relationships": []any{map[string]any{"target": "", "relationship": ""}}, "source_refs": []any{}, + "name": "", "source_refs": []any{}, }}}, valid: true, }, { name: "nonpositive unit candidates", response: map[string]any{"npcs": []any{map[string]any{ - "name": "Mira Thorn", "aliases": []any{}, "description": "A ranger.", "relationships": []any{}, + "name": "Mira Thorn", "source_refs": []any{map[string]any{"start_unit_id": 0, "end_unit_id": -1}}, }}}, valid: true, @@ -51,25 +51,25 @@ func TestLoadResponseSchemaUsesPrivateNPCSchema(t *testing.T) { { name: "missing required field", response: map[string]any{"npcs": []any{map[string]any{ - "aliases": []any{}, "description": "A ranger.", "relationships": []any{}, "source_refs": []any{}, + "source_refs": []any{}, }}}, }, { name: "unknown field", response: map[string]any{"npcs": []any{map[string]any{ - "name": "Mira Thorn", "aliases": []any{}, "description": "A ranger.", "relationships": []any{}, "source_refs": []any{}, "id": "assigned later", + "name": "Mira Thorn", "source_refs": []any{}, "id": "assigned later", }}}, }, { name: "wrong field type", response: map[string]any{"npcs": []any{map[string]any{ - "name": 7, "aliases": []any{}, "description": "A ranger.", "relationships": []any{}, "source_refs": []any{}, + "name": 7, "source_refs": []any{}, }}}, }, { name: "noninteger source identifier", response: map[string]any{"npcs": []any{map[string]any{ - "name": "Mira Thorn", "aliases": []any{}, "description": "A ranger.", "relationships": []any{}, + "name": "Mira Thorn", "source_refs": []any{map[string]any{"start_unit_id": 1.5, "end_unit_id": 2}}, }}}, }, @@ -89,7 +89,7 @@ func TestLoadResponseSchemaUsesPrivateNPCSchema(t *testing.T) { t.Fatal("validateJSONSchema() error = nil, want malformed JSON rejected") } withID := map[string]any{"npcs": []any{map[string]any{ - "name": "Mira Thorn", "id": "assigned-later", "aliases": []any{}, "description": "A ranger.", "relationships": []any{}, + "name": "Mira Thorn", "id": "assigned-later", "source_refs": []any{map[string]any{"start_unit_id": 1, "end_unit_id": 2}}, }}} withIDJSON, err := json.Marshal(withID) diff --git a/internal/modules/dnd/extract/spells/extractor.go b/internal/modules/dnd/extract/spells/extractor.go index a5a2773..284f05f 100644 --- a/internal/modules/dnd/extract/spells/extractor.go +++ b/internal/modules/dnd/extract/spells/extractor.go @@ -151,9 +151,7 @@ func (e *Extractor) CheckpointFingerprints() []pipeline.CheckpointFingerprint { {Name: "response_schema", Value: e.responseSchemaSHA}, } seeded := e.npcResolver.Seeded() - if seeded.Bound() { - fingerprints = append(fingerprints, pipeline.CheckpointFingerprint{Name: "npc_registry", Value: seeded.Digest()}) - } + fingerprints = append(fingerprints, pipeline.CheckpointFingerprint{Name: "npc_registry", Value: seeded.ProjectionDigest()}) return fingerprints } diff --git a/internal/modules/dnd/extract/spells/extractor_test.go b/internal/modules/dnd/extract/spells/extractor_test.go index 40e8c9e..a63b1b3 100644 --- a/internal/modules/dnd/extract/spells/extractor_test.go +++ b/internal/modules/dnd/extract/spells/extractor_test.go @@ -122,9 +122,10 @@ func TestExtractPromptUsesCanonicalOverlayNamesWithoutAliasesOrMetadata(t *testi "effective_catalog": metadata["catalog_digest"], "prompt": metadata["prompt_sha256"], "response_schema": metadata["response_schema_sha256"], + "npc_registry": checkpointFingerprintMap(newExtractor(t, &fakeSpellsLLMClient{}).CheckpointFingerprints())["npc_registry"], } if len(fingerprints) != len(wantFingerprints) { - t.Fatalf("checkpoint fingerprints = %#v, want prompt, response schema, and catalog identities", fingerprints) + t.Fatalf("checkpoint fingerprints = %#v, want prompt, response schema, catalog, and NPC projection identities", fingerprints) } for _, fingerprint := range fingerprints { if want, ok := wantFingerprints[fingerprint.Name]; !ok || fingerprint.Value != want { diff --git a/internal/modules/dnd/extract/spells/npc_registry_wiring_test.go b/internal/modules/dnd/extract/spells/npc_registry_wiring_test.go index a6f8119..268b64e 100644 --- a/internal/modules/dnd/extract/spells/npc_registry_wiring_test.go +++ b/internal/modules/dnd/extract/spells/npc_registry_wiring_test.go @@ -22,10 +22,9 @@ func TestSpellExtractorUsesExactUnboundNPCPromptAndOmitsRegistryIdentity(t *test if metadata["npc_registry_digest"] != nil || metadata["npc_count"] != nil { t.Fatalf("unbound extractor metadata = %#v, want no NPC registry fields", metadata) } - for _, fingerprint := range extractor.CheckpointFingerprints() { - if fingerprint.Name == "npc_registry" { - t.Fatalf("unbound checkpoint fingerprints = %#v, want no NPC registry fingerprint", extractor.CheckpointFingerprints()) - } + fingerprints := checkpointFingerprintMap(extractor.CheckpointFingerprints()) + if fingerprints["npc_registry"] == "" { + t.Fatalf("unbound checkpoint fingerprints = %#v, want empty-projection identity", fingerprints) } client := &fakeSpellsLLMClient{response: extractionResponse{SpellCasts: []spellCastResponse{}}} @@ -34,7 +33,7 @@ func TestSpellExtractorUsesExactUnboundNPCPromptAndOmitsRegistryIdentity(t *test t.Fatalf("Extract() error = %v, want nil", err) } input := client.requests[0].Inputs[NPCRegistryReferenceSlot] - if input.Name != NPCRegistryReferenceSlot || input.MediaType != npccodec.MediaType || input.Digest != "" || input.OriginURI != "" || string(input.Content) != `{"npcs":[]}` { + if input.Name != NPCRegistryReferenceSlot || input.MediaType != npccodec.MediaType || input.Digest != fingerprints["npc_registry"] || input.OriginURI != "" || string(input.Content) != `{"npcs":[]}` { t.Fatalf("NPC prompt input = %#v, want exact empty registry material", input) } } @@ -57,7 +56,7 @@ func TestSpellExtractorPreservesSemanticNPCRegistryFingerprintAndPromptWiring(t t.Fatalf("semantic NPC fingerprints = %#v and %#v, want same npc_registry value", firstFingerprints, secondFingerprints) } metadata := first.ManifestMetadata() - if metadata["npc_registry_digest"] != firstFingerprints["npc_registry"] || metadata["npc_count"] != 1 { + if metadata["npc_registry_digest"] == "" || metadata["npc_registry_digest"] == firstFingerprints["npc_registry"] || metadata["npc_count"] != 1 { t.Fatalf("NPC registry metadata = %#v, want digest and count only", metadata) } @@ -70,8 +69,8 @@ func TestSpellExtractorPreservesSemanticNPCRegistryFingerprintAndPromptWiring(t if input.Name != NPCRegistryReferenceSlot || input.MediaType != npccodec.MediaType || input.Digest != firstFingerprints["npc_registry"] || input.OriginURI != "" { t.Fatalf("NPC prompt input metadata = %#v, want semantic metadata without provenance", input) } - if !bytes.Equal(input.Content, canonical) { - t.Fatalf("NPC prompt input = %s, want canonical JSON %s", input.Content, canonical) + if !bytes.Equal(input.Content, []byte(`{"npcs":[{"name":"Mira Thorn"}]}`)) { + t.Fatalf("NPC prompt input = %s, want names-only projection", input.Content) } encoded, err := json.Marshal(map[string]any{"metadata": metadata, "fingerprints": firstFingerprints}) if err != nil { @@ -97,28 +96,21 @@ func TestSpellExtractorResolvesOperationNPCOverrideWithoutSingletonMetadata(t *t t.Fatalf("Extract() error = %v", err) } input := client.requests[0].Inputs[NPCRegistryReferenceSlot] - if input.Digest == "" || string(input.Content) != string(canonical) || input.OriginURI != "" { + if input.Digest == "" || string(input.Content) != `{"npcs":[{"name":"Mira Thorn"}]}` || input.OriginURI != "" { t.Fatalf("operation NPC input = %#v, want generated canonical grounding without provenance", input) } if metadata := extractor.ManifestMetadata(); metadata["npc_registry_digest"] != nil || metadata["npc_count"] != nil { t.Fatalf("singleton metadata = %#v, want no operation-varying NPC identity", metadata) } - for _, fingerprint := range extractor.CheckpointFingerprints() { - if fingerprint.Name == "npc_registry" { - t.Fatalf("singleton fingerprints = %#v, want no operation-varying NPC identity", extractor.CheckpointFingerprints()) - } + if checkpointFingerprintMap(extractor.CheckpointFingerprints())["npc_registry"] == "" { + t.Fatalf("singleton fingerprints = %#v, want empty-projection identity", extractor.CheckpointFingerprints()) } } func registryFixture() dnd.NPCList { return dnd.NPCList{NPCs: []dnd.NPC{{ - ID: identity.DeriveID("Mira Thorn"), - Name: "Mira Thorn", - Aliases: []string{"The Greencloak"}, - Description: "A guarded ranger who watches the northern road.", - Relationships: []dnd.NPCRelationship{{ - Target: "Captain Vale", Relationship: "reports to", - }}, + ID: identity.DeriveID("Mira Thorn"), + Name: "Mira Thorn", SourceRefs: []source.SourceRef{{SourceID: "npc-session", StartUnitID: 41, EndUnitID: 43}}, }}} } diff --git a/internal/modules/dnd/normalize/combatturns/normalizer.go b/internal/modules/dnd/normalize/combatturns/normalizer.go index 7e452f1..c844472 100644 --- a/internal/modules/dnd/normalize/combatturns/normalizer.go +++ b/internal/modules/dnd/normalize/combatturns/normalizer.go @@ -92,9 +92,7 @@ func (n *Normalizer) CheckpointFingerprints() []pipeline.CheckpointFingerprint { {Name: "identity_policy", Value: identity.Policy}, } seeded := n.npcResolver.Seeded() - if seeded.Bound() { - fingerprints = append(fingerprints, pipeline.CheckpointFingerprint{Name: "npc_registry", Value: seeded.Digest()}) - } + fingerprints = append(fingerprints, pipeline.CheckpointFingerprint{Name: "npc_registry", Value: seeded.ProjectionDigest()}) return fingerprints } diff --git a/internal/modules/dnd/normalize/combatturns/normalizer_test.go b/internal/modules/dnd/normalize/combatturns/normalizer_test.go index 5230267..40658b0 100644 --- a/internal/modules/dnd/normalize/combatturns/normalizer_test.go +++ b/internal/modules/dnd/normalize/combatturns/normalizer_test.go @@ -26,12 +26,12 @@ func TestNormalizeCanonicalizesFieldsAndRegistryIdentities(t *testing.T) { } resolution := " the target is hit " input := dnd.CombatTurnList{CombatTurns: []dnd.CombatTurn{{ - Actor: " storm ", + Actor: " aria ", TurnKind: dnd.CombatTurnKindTurn, Actions: []dnd.CombatAction{{ Category: dnd.CombatActionCategoryAttack, Declaration: " attacks\n with a sword ", - Targets: []string{" minion ", "goblin", " unknown combatant "}, + Targets: []string{" goblin ", "goblin", " unknown combatant "}, Resolution: &resolution, }}, Summary: " Aria\n attacks ", @@ -77,10 +77,10 @@ func TestNormalizeResolvesOperationNPCOverrideWithoutSingletonMetadata(t *testin t.Fatalf("New() error = %v", err) } input := dnd.CombatTurnList{CombatTurns: []dnd.CombatTurn{{ - Actor: "Storm", + Actor: "aria", TurnKind: dnd.CombatTurnKindTurn, - Actions: []dnd.CombatAction{{Category: dnd.CombatActionCategoryAttack, Declaration: "attacks", Targets: []string{"Minion"}}}, - Summary: "Storm attacks", + Actions: []dnd.CombatAction{{Category: dnd.CombatActionCategoryAttack, Declaration: "attacks", Targets: []string{"goblin"}}}, + Summary: "Aria attacks", SourceRefs: []source.SourceRef{{SourceID: doc.ID, StartUnitID: 10, EndUnitID: 10}}, }}} result, err := normalizer.Normalize(context.Background(), contracts.TypedNormalizeRequest[dnd.CombatTurnList]{ @@ -222,7 +222,7 @@ func TestNormalizerPreparationMetadataFingerprintsAndModuleContract(t *testing.T if metadata := unbound.ManifestMetadata(); metadata["npc_registry_digest"] != nil || metadata["npc_count"] != nil || metadata["normalization_policy"] != normalizationPolicy || metadata["identity_policy"] != identity.Policy { t.Fatalf("unbound metadata = %#v", metadata) } - if got := unbound.CheckpointFingerprints(); len(got) != 2 || got[0].Name != "normalization_policy" || got[1].Name != "identity_policy" { + if got := unbound.CheckpointFingerprints(); len(got) != 3 || got[0].Name != "normalization_policy" || got[1].Name != "identity_policy" || got[2].Name != "npc_registry" || got[2].Value == "" { t.Fatalf("unbound fingerprints = %#v", got) } @@ -305,8 +305,8 @@ func testDocument() *source.SourceDocument { func npcReferences(t *testing.T) contracts.ReferenceSet { t.Helper() npcs := dnd.NPCList{NPCs: []dnd.NPC{ - {ID: identity.DeriveID("Aria"), Name: "Aria", Aliases: []string{"Storm"}, Description: "a fighter", Relationships: []dnd.NPCRelationship{{Target: "Goblin", Relationship: "fights"}}, SourceRefs: []source.SourceRef{{SourceID: "npc-run", StartUnitID: 1, EndUnitID: 1}}}, - {ID: identity.DeriveID("Goblin"), Name: "Goblin", Aliases: []string{"Minion"}, Description: "a goblin", Relationships: []dnd.NPCRelationship{{Target: "Aria", Relationship: "fights"}}, SourceRefs: []source.SourceRef{{SourceID: "npc-run", StartUnitID: 1, EndUnitID: 1}}}, + {ID: identity.DeriveID("Aria"), Name: "Aria", SourceRefs: []source.SourceRef{{SourceID: "npc-run", StartUnitID: 1, EndUnitID: 1}}}, + {ID: identity.DeriveID("Goblin"), Name: "Goblin", SourceRefs: []source.SourceRef{{SourceID: "npc-run", StartUnitID: 1, EndUnitID: 1}}}, }} content, err := npccodec.New().Encode(npcs) if err != nil { diff --git a/internal/modules/dnd/normalize/npcs/normalizer.go b/internal/modules/dnd/normalize/npcs/normalizer.go index 8dbbc8c..74f53e2 100644 --- a/internal/modules/dnd/normalize/npcs/normalizer.go +++ b/internal/modules/dnd/normalize/npcs/normalizer.go @@ -22,11 +22,10 @@ const ( normalizationPolicy = "dnd.npcs.normalize.v1" NormalizationPolicy = normalizationPolicy - ReasonCodeNPCFieldsNormalized = "npc_fields_normalized" - ReasonCodeNPCIDRecomputed = "npc_id_recomputed" - ReasonCodeSourceReferencesNormalized = "source_references_normalized" - ReasonCodeDuplicateNPCCollapsed = "duplicate_npc_collapsed" - ReasonCodeRelationshipTargetCanonicalized = "relationship_target_canonicalized" + ReasonCodeNPCFieldsNormalized = "npc_fields_normalized" + ReasonCodeNPCIDRecomputed = "npc_id_recomputed" + ReasonCodeSourceReferencesNormalized = "source_references_normalized" + ReasonCodeDuplicateNPCCollapsed = "duplicate_npc_collapsed" ) var requiredCapabilities = []string{"merged"} @@ -37,23 +36,17 @@ var _ contracts.ManifestMetadataProvider = (*Normalizer)(nil) var _ pipeline.CheckpointFingerprintProvider = (*Normalizer)(nil) type Options struct{} - type Normalizer struct{} -func New(Options) *Normalizer { return &Normalizer{} } - -func (n *Normalizer) Key() string { return Key } - +func New(Options) *Normalizer { return &Normalizer{} } +func (n *Normalizer) Key() string { return Key } func (n *Normalizer) ReferenceSlots() []contracts.ReferenceSlot { return nil } func (n *Normalizer) ManifestMetadata() map[string]any { if n == nil { return nil } - return map[string]any{ - "identity_policy": identity.Policy, - "normalization_policy": normalizationPolicy, - } + return map[string]any{"identity_policy": identity.Policy, "normalization_policy": normalizationPolicy} } func (n *Normalizer) CheckpointFingerprints() []pipeline.CheckpointFingerprint { @@ -76,193 +69,106 @@ func (n *Normalizer) Normalize(ctx context.Context, req contracts.TypedNormalize if err := ctx.Err(); err != nil { return contracts.TypedNormalizeResult[dnd.NPCList]{}, normalizerErrorf("context error before normalize: %w", err) } - value, warnings := normalizeList(req.MergeOutput.Value) return contracts.TypedNormalizeResult[dnd.NPCList]{Value: value, Warnings: warnings}, nil } type normalizedRecord struct { - npc dnd.NPC - fieldsChanged bool - referencesChanged bool + npc dnd.NPC } func normalizeList(input dnd.NPCList) (dnd.NPCList, []contracts.Warning) { if input.NPCs == nil { return dnd.NPCList{}, nil } - records := make([]normalizedRecord, len(input.NPCs)) warnings := make([]contracts.Warning, 0) for index, inputNPC := range input.NPCs { npc, fieldsChanged, referencesChanged := normalizeRecord(inputNPC) - records[index] = normalizedRecord{ - npc: npc, - fieldsChanged: fieldsChanged, - referencesChanged: referencesChanged, - } + records[index] = normalizedRecord{npc: npc} if fieldsChanged { - warnings = append(warnings, contracts.Warning{ - Scope: npcScope(index), - ReasonCode: ReasonCodeNPCFieldsNormalized, - Message: fmt.Sprintf("input index %d: NPC fields normalized for %s", - index, diagnostics.Quote(inputNPC.Name)), - }) + warnings = append(warnings, contracts.Warning{Scope: npcScope(index), ReasonCode: ReasonCodeNPCFieldsNormalized, Message: fmt.Sprintf("input index %d: NPC name normalized for %s", index, diagnostics.Quote(inputNPC.Name))}) } if referencesChanged { - warnings = append(warnings, contracts.Warning{ - Scope: npcScope(index), - ReasonCode: ReasonCodeSourceReferencesNormalized, - Message: fmt.Sprintf("input index %d: source references normalized (original count %d, final count %d)", - index, len(inputNPC.SourceRefs), len(npc.SourceRefs)), - }) + warnings = append(warnings, contracts.Warning{Scope: npcScope(index), ReasonCode: ReasonCodeSourceReferencesNormalized, Message: fmt.Sprintf("input index %d: source references normalized (original count %d, final count %d)", index, len(inputNPC.SourceRefs), len(npc.SourceRefs))}) } if inputNPC.ID != npc.ID { - warnings = append(warnings, contracts.Warning{ - Scope: npcScope(index), - ReasonCode: ReasonCodeNPCIDRecomputed, - Message: fmt.Sprintf("input index %d: NPC ID recomputed from %s", - index, diagnostics.Quote(npc.Name)), - }) + warnings = append(warnings, contracts.Warning{Scope: npcScope(index), ReasonCode: ReasonCodeNPCIDRecomputed, Message: fmt.Sprintf("input index %d: NPC ID recomputed from %s", index, diagnostics.Quote(npc.Name))}) } } - components := identityComponents(records) - output := dnd.NPCList{NPCs: make([]dnd.NPC, 0, len(components))} - retainedIndexes := make([]int, 0, len(components)) - for _, members := range components { - consolidated, sourceChanged := consolidate(records, members) + groups := canonicalNameGroups(records) + output := dnd.NPCList{NPCs: make([]dnd.NPC, 0, len(groups))} + for _, members := range groups { + consolidated, referencesChanged := consolidate(records, members) retainedIndex := members[0] output.NPCs = append(output.NPCs, consolidated) - retainedIndexes = append(retainedIndexes, retainedIndex) - - if sourceChanged { - warnings = append(warnings, contracts.Warning{ - Scope: npcScope(retainedIndex), - ReasonCode: ReasonCodeSourceReferencesNormalized, - Message: fmt.Sprintf("input index %d: source references normalized during identity consolidation (final count %d)", - retainedIndex, len(consolidated.SourceRefs)), - }) + if referencesChanged { + warnings = append(warnings, contracts.Warning{Scope: npcScope(retainedIndex), ReasonCode: ReasonCodeSourceReferencesNormalized, Message: fmt.Sprintf("input index %d: source references normalized during identity consolidation (final count %d)", retainedIndex, len(consolidated.SourceRefs))}) } if len(members) > 1 { warnings = append(warnings, duplicateWarning(retainedIndex, members[1:])) } } - - warnings = append(warnings, canonicalizeRelationshipTargets(&output, retainedIndexes)...) return output, warnings } func normalizeRecord(input dnd.NPC) (dnd.NPC, bool, bool) { output := cloneNPC(input) output.Name = identity.NormalizeDisplay(input.Name) - output.Aliases = normalizeAliases(input.Aliases, output.Name) - output.Description = strings.TrimSpace(input.Description) - output.Relationships = normalizeRelationships(input.Relationships) output.SourceRefs, _, _ = canonicalizeSourceRefs(input.SourceRefs) output.ID = identity.DeriveID(output.Name) - - fieldsChanged := input.Name != output.Name || - !reflect.DeepEqual(input.Aliases, output.Aliases) || - input.Description != output.Description || - !reflect.DeepEqual(input.Relationships, output.Relationships) - referencesChanged := !reflect.DeepEqual(input.SourceRefs, output.SourceRefs) - return output, fieldsChanged, referencesChanged + return output, input.Name != output.Name, !reflect.DeepEqual(input.SourceRefs, output.SourceRefs) } func cloneNPC(input dnd.NPC) dnd.NPC { - output := input - if input.Aliases != nil { - output.Aliases = make([]string, len(input.Aliases)) - copy(output.Aliases, input.Aliases) - } - if input.Relationships != nil { - output.Relationships = make([]dnd.NPCRelationship, len(input.Relationships)) - copy(output.Relationships, input.Relationships) - } - if input.SourceRefs != nil { - output.SourceRefs = make([]source.SourceRef, len(input.SourceRefs)) - copy(output.SourceRefs, input.SourceRefs) - } - return output + input.SourceRefs = cloneSourceRefs(input.SourceRefs) + return input } -func normalizeAliases(input []string, canonicalName string) []string { - if input == nil { - return nil - } - - canonicalKey := identity.ComparisonKey(canonicalName) - output := make([]string, 0, len(input)) - seen := make(map[string]struct{}, len(input)) - for _, alias := range input { - normalized := identity.NormalizeDisplay(alias) - key := identity.ComparisonKey(normalized) - if canonicalKey != "" && key == canonicalKey { - continue +func canonicalNameGroups(records []normalizedRecord) [][]int { + groups := make([][]int, 0, len(records)) + ownerByKey := make(map[string]int, len(records)) + for index, record := range records { + key := identity.ComparisonKey(record.npc.Name) + if key != "" { + if groupIndex, ok := ownerByKey[key]; ok { + groups[groupIndex] = append(groups[groupIndex], index) + continue + } + ownerByKey[key] = len(groups) } - if _, exists := seen[key]; exists { - continue - } - seen[key] = struct{}{} - output = append(output, normalized) + groups = append(groups, []int{index}) } - return output + return groups } -func normalizeRelationships(input []dnd.NPCRelationship) []dnd.NPCRelationship { - if input == nil { - return nil - } - - output := make([]dnd.NPCRelationship, 0, len(input)) - seen := make(map[relationshipIdentity]struct{}, len(input)) - for _, relationship := range input { - normalized := dnd.NPCRelationship{ - Target: identity.NormalizeDisplay(relationship.Target), - Relationship: strings.TrimSpace(relationship.Relationship), - } - key := relationshipKey(normalized) - if _, exists := seen[key]; exists { - continue - } - seen[key] = struct{}{} - output = append(output, normalized) - } - return output -} - -type relationshipIdentity struct { - target string - relationship string -} - -func relationshipKey(relationship dnd.NPCRelationship) relationshipIdentity { - return relationshipIdentity{ - target: identity.ComparisonKey(relationship.Target), - relationship: identity.ComparisonKey(relationship.Relationship), +func consolidate(records []normalizedRecord, members []int) (dnd.NPC, bool) { + output := cloneNPC(records[members[0]].npc) + originalRefs := cloneSourceRefs(output.SourceRefs) + for _, member := range members[1:] { + output.SourceRefs = append(output.SourceRefs, records[member].npc.SourceRefs...) } + output.SourceRefs, _, _ = canonicalizeSourceRefs(output.SourceRefs) + output.ID = identity.DeriveID(output.Name) + return output, !reflect.DeepEqual(originalRefs, output.SourceRefs) } func canonicalizeSourceRefs(input []source.SourceRef) ([]source.SourceRef, bool, int) { if input == nil { return nil, false, 0 } - - canonical := make([]source.SourceRef, len(input)) - copy(canonical, input) + canonical := cloneSourceRefs(input) sort.SliceStable(canonical, func(left, right int) bool { - return sourceRefLess(canonical[left], canonical[right]) - }) - - orderChanged := false - for index := range input { - if input[index] != canonical[index] { - orderChanged = true - break + if canonical[left].SourceID != canonical[right].SourceID { + return canonical[left].SourceID < canonical[right].SourceID } - } - + if canonical[left].StartUnitID != canonical[right].StartUnitID { + return canonical[left].StartUnitID < canonical[right].StartUnitID + } + return canonical[left].EndUnitID < canonical[right].EndUnitID + }) + orderChanged := !reflect.DeepEqual(input, canonical) unique := make([]source.SourceRef, 0, len(canonical)) for _, ref := range canonical { if len(unique) == 0 || unique[len(unique)-1] != ref { @@ -272,219 +178,11 @@ func canonicalizeSourceRefs(input []source.SourceRef) ([]source.SourceRef, bool, return unique, orderChanged, len(input) - len(unique) } -func sourceRefLess(left, right source.SourceRef) bool { - if left.SourceID != right.SourceID { - return left.SourceID < right.SourceID - } - if left.StartUnitID != right.StartUnitID { - return left.StartUnitID < right.StartUnitID - } - return left.EndUnitID < right.EndUnitID -} - -func identityComponents(records []normalizedRecord) [][]int { - parent := make([]int, len(records)) - for index := range parent { - parent[index] = index - } - - for left := 0; left < len(records); left++ { - for right := left + 1; right < len(records); right++ { - if recordsCanMerge(records[left].npc, records[right].npc) { - union(parent, left, right) - } - } - } - - byRoot := make(map[int][]int, len(records)) - for index := range records { - root := find(parent, index) - byRoot[root] = append(byRoot[root], index) - } - roots := make([]int, 0, len(byRoot)) - for root := range byRoot { - roots = append(roots, root) - } - sort.Slice(roots, func(left, right int) bool { - return byRoot[roots[left]][0] < byRoot[roots[right]][0] - }) - - components := make([][]int, 0, len(roots)) - for _, root := range roots { - components = append(components, byRoot[root]) - } - return components -} - -func recordsCanMerge(left, right dnd.NPC) bool { - leftCanonical := identity.ComparisonKey(left.Name) - rightCanonical := identity.ComparisonKey(right.Name) - return leftCanonical == rightCanonical || - containsAliasKey(right.Aliases, leftCanonical) || - containsAliasKey(left.Aliases, rightCanonical) -} - -func containsAliasKey(aliases []string, wanted string) bool { - for _, alias := range aliases { - if identity.ComparisonKey(alias) == wanted { - return true - } - } - return false -} - -func find(parent []int, index int) int { - for parent[index] != index { - parent[index] = parent[parent[index]] - index = parent[index] - } - return index -} - -func union(parent []int, left, right int) { - leftRoot := find(parent, left) - rightRoot := find(parent, right) - if leftRoot == rightRoot { - return - } - if leftRoot < rightRoot { - parent[rightRoot] = leftRoot - } else { - parent[leftRoot] = rightRoot - } -} - -func consolidate(records []normalizedRecord, members []int) (dnd.NPC, bool) { - output := cloneNPC(records[members[0]].npc) - originalRefs := cloneSourceRefs(output.SourceRefs) - canonicalKey := identity.ComparisonKey(output.Name) - - for _, member := range members[1:] { - candidate := records[member].npc - appendAlias(&output.Aliases, candidate.Name, canonicalKey) - for _, alias := range candidate.Aliases { - appendAlias(&output.Aliases, alias, canonicalKey) - } - for _, relationship := range candidate.Relationships { - appendRelationship(&output.Relationships, relationship) - } - output.SourceRefs = append(output.SourceRefs, candidate.SourceRefs...) - } - - output.SourceRefs, _, _ = canonicalizeSourceRefs(output.SourceRefs) - output.ID = identity.DeriveID(output.Name) - return output, !reflect.DeepEqual(originalRefs, output.SourceRefs) -} - func cloneSourceRefs(input []source.SourceRef) []source.SourceRef { if input == nil { return nil } - output := make([]source.SourceRef, len(input)) - copy(output, input) - return output -} - -func appendAlias(aliases *[]string, value, canonicalKey string) { - key := identity.ComparisonKey(value) - if canonicalKey != "" && key == canonicalKey { - return - } - for _, existing := range *aliases { - if identity.ComparisonKey(existing) == key { - return - } - } - *aliases = append(*aliases, value) -} - -func appendRelationship(relationships *[]dnd.NPCRelationship, relationship dnd.NPCRelationship) { - key := relationshipKey(relationship) - for _, existing := range *relationships { - if relationshipKey(existing) == key { - return - } - } - *relationships = append(*relationships, relationship) -} - -func canonicalizeRelationshipTargets(list *dnd.NPCList, retainedIndexes []int) []contracts.Warning { - if list == nil || len(list.NPCs) == 0 { - return nil - } - - owners := make(map[string][]int) - for index, npc := range list.NPCs { - if key := identity.ComparisonKey(npc.Name); key != "" { - owners[key] = append(owners[key], index) - } - for _, alias := range npc.Aliases { - if key := identity.ComparisonKey(alias); key != "" { - owners[key] = appendUniqueIndex(owners[key], index) - } - } - } - - warnings := make([]contracts.Warning, 0) - for outputIndex := range list.NPCs { - npc := &list.NPCs[outputIndex] - originalRelationshipCount := len(npc.Relationships) - for relationshipIndex := range npc.Relationships { - relationship := &npc.Relationships[relationshipIndex] - key := identity.ComparisonKey(relationship.Target) - if key == "" || len(owners[key]) != 1 { - continue - } - target := list.NPCs[owners[key][0]].Name - if relationship.Target == target { - continue - } - oldTarget := relationship.Target - relationship.Target = target - warnings = append(warnings, contracts.Warning{ - Scope: npcScope(retainedIndexes[outputIndex]), - ReasonCode: ReasonCodeRelationshipTargetCanonicalized, - Message: fmt.Sprintf("input index %d: relationship target canonicalized from %s to %s", - retainedIndexes[outputIndex], diagnostics.Quote(oldTarget), diagnostics.Quote(target)), - }) - } - npc.Relationships = deduplicateRelationships(npc.Relationships) - if len(npc.Relationships) != originalRelationshipCount { - warnings = append(warnings, contracts.Warning{ - Scope: npcScope(retainedIndexes[outputIndex]), - ReasonCode: ReasonCodeNPCFieldsNormalized, - Message: fmt.Sprintf("input index %d: duplicate relationships removed after target canonicalization", - retainedIndexes[outputIndex]), - }) - } - } - return warnings -} - -func deduplicateRelationships(input []dnd.NPCRelationship) []dnd.NPCRelationship { - if input == nil { - return nil - } - output := make([]dnd.NPCRelationship, 0, len(input)) - seen := make(map[relationshipIdentity]struct{}, len(input)) - for _, relationship := range input { - key := relationshipKey(relationship) - if _, exists := seen[key]; exists { - continue - } - seen[key] = struct{}{} - output = append(output, relationship) - } - return output -} - -func appendUniqueIndex(values []int, wanted int) []int { - for _, value := range values { - if value == wanted { - return values - } - } - return append(values, wanted) + return append([]source.SourceRef(nil), input...) } func duplicateWarning(retainedIndex int, removed []int) contracts.Warning { @@ -497,28 +195,17 @@ func duplicateWarning(retainedIndex int, removed []int) contracts.Warning { for index, removedIndex := range displayed { indices[index] = strconv.Itoa(removedIndex) } - message := fmt.Sprintf("retained input index %d; removed input indices [%s]", retainedIndex, strings.Join(indices, ", ")) if omitted := len(removed) - len(displayed); omitted > 0 { message += fmt.Sprintf("; %d additional removed input indices omitted", omitted) } - return contracts.Warning{ - Scope: npcScope(retainedIndex), - ReasonCode: ReasonCodeDuplicateNPCCollapsed, - Message: message, - } + return contracts.Warning{Scope: npcScope(retainedIndex), ReasonCode: ReasonCodeDuplicateNPCCollapsed, Message: message} } func npcScope(index int) string { return fmt.Sprintf("npcs[%d]", index) } func ModuleSpec() pipeline.ModuleSpec { - return pipeline.ModuleSpec{ - Key: Key, - Stage: pipeline.StageNormalize, - Requires: append([]string(nil), requiredCapabilities...), - Provides: append([]string(nil), providedCapabilities...), - ArtifactKind: dnd.NPCListKind, - } + return pipeline.ModuleSpec{Key: Key, Stage: pipeline.StageNormalize, Requires: append([]string(nil), requiredCapabilities...), Provides: append([]string(nil), providedCapabilities...), ArtifactKind: dnd.NPCListKind} } func Register(registry *pipeline.NormalizerRegistry) error { @@ -531,10 +218,7 @@ func Register(registry *pipeline.NormalizerRegistry) error { }) } -func validateOptions(options map[string]any) error { - _, err := DecodeOptions(options) - return err -} +func validateOptions(options map[string]any) error { _, err := DecodeOptions(options); return err } func DecodeOptions(options map[string]any) (Options, error) { if err := pipeline.RejectUnknownOptions(options); err != nil { diff --git a/internal/modules/dnd/normalize/npcs/normalizer_test.go b/internal/modules/dnd/normalize/npcs/normalizer_test.go index 722265f..a795fd4 100644 --- a/internal/modules/dnd/normalize/npcs/normalizer_test.go +++ b/internal/modules/dnd/normalize/npcs/normalizer_test.go @@ -10,223 +10,91 @@ import ( "gitea.maximumdirect.net/eric/notarius/internal/framework/contracts" "gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline" "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd" - identity "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/npcs/identity" + "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/npcs/identity" ) func TestModuleContractAndIdentity(t *testing.T) { if _, err := DecodeOptions(nil); err != nil { t.Fatalf("DecodeOptions(nil) error = %v, want nil", err) } - if _, err := DecodeOptions(map[string]any{"unexpected": true}); err == nil || !strings.Contains(err.Error(), "unknown option") { - t.Fatalf("DecodeOptions() error = %v, want unknown option error", err) - } - - want := pipeline.ModuleSpec{ - Key: Key, - Stage: pipeline.StageNormalize, - Requires: []string{"merged"}, - Provides: []string{"normalized"}, - ArtifactKind: dnd.NPCListKind, + if _, err := DecodeOptions(map[string]any{"unexpected": true}); err == nil { + t.Fatal("DecodeOptions() accepted unknown option") } + want := pipeline.ModuleSpec{Key: Key, Stage: pipeline.StageNormalize, Requires: []string{"merged"}, Provides: []string{"normalized"}, ArtifactKind: dnd.NPCListKind} if got := ModuleSpec(); !reflect.DeepEqual(got, want) { t.Fatalf("ModuleSpec() = %#v, want %#v", got, want) } - if slots := New(Options{}).ReferenceSlots(); slots != nil { - t.Fatalf("ReferenceSlots() = %#v, want nil", slots) - } - registry := pipeline.NewNormalizerRegistry() if err := Register(registry); err != nil { - t.Fatalf("Register() error = %v, want nil", err) + t.Fatalf("Register() error = %v", err) } - if registered, ok := registry.SpecForArtifact(Key, dnd.NPCListKind); !ok || !reflect.DeepEqual(registered, want) { - t.Fatalf("registered spec = %#v, ok = %t, want %#v", registered, ok, want) - } - if err := Register(nil); err == nil || !strings.Contains(err.Error(), "normalizer registry") { - t.Fatalf("Register(nil) error = %v, want registry error", err) - } - normalizer := New(Options{}) - metadata := normalizer.ManifestMetadata() - if metadata["identity_policy"] != identity.Policy || metadata["normalization_policy"] != normalizationPolicy { - t.Fatalf("metadata = %#v, want identity and normalization policies", metadata) + if metadata := normalizer.ManifestMetadata(); metadata["identity_policy"] != identity.Policy || metadata["normalization_policy"] != normalizationPolicy { + t.Fatalf("metadata = %#v", metadata) } - fingerprints := normalizer.CheckpointFingerprints() - wantFingerprints := []pipeline.CheckpointFingerprint{ - {Name: "identity_policy", Value: identity.Policy}, - {Name: "normalization_policy", Value: normalizationPolicy}, - } - if !reflect.DeepEqual(fingerprints, wantFingerprints) { - t.Fatalf("fingerprints = %#v, want %#v", fingerprints, wantFingerprints) - } - fingerprints[0].Value = "changed" + wantFingerprints := []pipeline.CheckpointFingerprint{{Name: "identity_policy", Value: identity.Policy}, {Name: "normalization_policy", Value: normalizationPolicy}} if got := normalizer.CheckpointFingerprints(); !reflect.DeepEqual(got, wantFingerprints) { - t.Fatalf("fingerprints were not defensive: %#v", got) + t.Fatalf("fingerprints = %#v, want %#v", got, wantFingerprints) } } -func TestNormalizePerRecordFieldsAndEvidence(t *testing.T) { +func TestNormalizeNamesEvidenceAndIDs(t *testing.T) { input := dnd.NPCList{NPCs: []dnd.NPC{{ - ID: "wrong", - Name: " Lady\tAsh ", - Aliases: []string{" Ash ", " L.A. ", "l.a.", " Lady Ash "}, - Description: " first description\n", - Relationships: []dnd.NPCRelationship{ - {Target: " Lord\nOak ", Relationship: " friend "}, - {Target: "lord oak", Relationship: "friend"}, - }, - SourceRefs: []source.SourceRef{ + ID: "wrong", Name: " Lady\tAsh ", SourceRefs: []source.SourceRef{ {SourceID: "b", StartUnitID: 2, EndUnitID: 3}, {SourceID: "a", StartUnitID: 4, EndUnitID: 4}, {SourceID: "b", StartUnitID: 2, EndUnitID: 3}, }, }}} - result, err := New(Options{}).Normalize(context.Background(), normalizeRequest(input)) if err != nil { - t.Fatalf("Normalize() error = %v, want nil", err) + t.Fatalf("Normalize() error = %v", err) } - got := result.Value.NPCs[0] - if got.Name != "Lady Ash" || got.Description != "first description" { - t.Fatalf("normalized fields = %#v, want normalized display and description", got) + want := dnd.NPC{ID: identity.DeriveID("Lady Ash"), Name: "Lady Ash", SourceRefs: []source.SourceRef{{SourceID: "a", StartUnitID: 4, EndUnitID: 4}, {SourceID: "b", StartUnitID: 2, EndUnitID: 3}}} + if !reflect.DeepEqual(result.Value.NPCs[0], want) { + t.Fatalf("NPC = %#v, want %#v", result.Value.NPCs[0], want) } - if !reflect.DeepEqual(got.Aliases, []string{"Ash", "L.A."}) { - t.Fatalf("aliases = %#v, want canonical alias removal and deduplication", got.Aliases) - } - wantRelationships := []dnd.NPCRelationship{{Target: "Lord Oak", Relationship: "friend"}} - if !reflect.DeepEqual(got.Relationships, wantRelationships) { - t.Fatalf("relationships = %#v, want %#v", got.Relationships, wantRelationships) - } - wantRefs := []source.SourceRef{ - {SourceID: "a", StartUnitID: 4, EndUnitID: 4}, - {SourceID: "b", StartUnitID: 2, EndUnitID: 3}, - } - if !reflect.DeepEqual(got.SourceRefs, wantRefs) { - t.Fatalf("source refs = %#v, want %#v", got.SourceRefs, wantRefs) - } - if got.ID != identity.DeriveID("Lady Ash") { - t.Fatalf("ID = %q, want derived ID", got.ID) - } - if !hasWarning(result.Warnings, ReasonCodeNPCFieldsNormalized, "npcs[0]") || - !hasWarning(result.Warnings, ReasonCodeSourceReferencesNormalized, "npcs[0]") || - !hasWarning(result.Warnings, ReasonCodeNPCIDRecomputed, "npcs[0]") { - t.Fatalf("warnings = %#v, want field, source, and ID warnings", result.Warnings) + for _, reason := range []string{ReasonCodeNPCFieldsNormalized, ReasonCodeSourceReferencesNormalized, ReasonCodeNPCIDRecomputed} { + if !hasWarning(result.Warnings, reason, "npcs[0]") { + t.Fatalf("warnings = %#v, want %s", result.Warnings, reason) + } } } -func TestNormalizeConsolidatesIdentityComponentsInStableOrder(t *testing.T) { +func TestNormalizeConsolidatesCanonicalNamesOnlyAndUnionsEvidence(t *testing.T) { input := dnd.NPCList{NPCs: []dnd.NPC{ - { - Name: " Captain Vale ", - Description: "first description", - Aliases: []string{"Vale"}, - Relationships: []dnd.NPCRelationship{{Target: "Archivist", Relationship: "knows"}}, - SourceRefs: []source.SourceRef{{SourceID: "a", StartUnitID: 1, EndUnitID: 1}}, - }, - { - Name: "Captain Vale", - Description: "second description", - Aliases: []string{"CV"}, - SourceRefs: []source.SourceRef{{SourceID: "b", StartUnitID: 1, EndUnitID: 1}}, - }, - { - Name: "The Sage", - Description: "sage description", - Aliases: []string{"Archivist"}, - SourceRefs: []source.SourceRef{{SourceID: "c", StartUnitID: 1, EndUnitID: 1}}, - }, - { - Name: "Archivist", - Description: "later sage description", - Aliases: []string{"Chronicler"}, - SourceRefs: []source.SourceRef{{SourceID: "d", StartUnitID: 1, EndUnitID: 1}}, - }, - { - Name: "North", - Description: "north description", - SourceRefs: []source.SourceRef{{SourceID: "e", StartUnitID: 1, EndUnitID: 1}}, - }, - { - Name: "North Old", - Description: "old north description", - Aliases: []string{"North"}, - SourceRefs: []source.SourceRef{{SourceID: "f", StartUnitID: 1, EndUnitID: 1}}, - }, - { - Name: "North Renamed", - Description: "renamed north description", - Aliases: []string{"North Old"}, - SourceRefs: []source.SourceRef{{SourceID: "g", StartUnitID: 1, EndUnitID: 1}}, - }, - {Name: "Red", Description: "red", Aliases: []string{"Shared"}, SourceRefs: []source.SourceRef{{SourceID: "h", StartUnitID: 1, EndUnitID: 1}}}, - {Name: "Blue", Description: "blue", Aliases: []string{"Shared"}, SourceRefs: []source.SourceRef{{SourceID: "i", StartUnitID: 1, EndUnitID: 1}}}, + {Name: " Captain Vale ", SourceRefs: []source.SourceRef{{SourceID: "a", StartUnitID: 1, EndUnitID: 1}}}, + {Name: "captain vale", SourceRefs: []source.SourceRef{{SourceID: "b", StartUnitID: 2, EndUnitID: 2}}}, + {Name: "The Captain", SourceRefs: []source.SourceRef{{SourceID: "c", StartUnitID: 3, EndUnitID: 3}}}, }} - result, err := New(Options{}).Normalize(context.Background(), normalizeRequest(input)) if err != nil { - t.Fatalf("Normalize() error = %v, want nil", err) + t.Fatalf("Normalize() error = %v", err) } - if got := len(result.Value.NPCs); got != 5 { - t.Fatalf("normalized NPC count = %d, want five components", got) + if len(result.Value.NPCs) != 2 || result.Value.NPCs[0].Name != "Captain Vale" || result.Value.NPCs[1].Name != "The Captain" { + t.Fatalf("NPCs = %#v, want name-only stable consolidation", result.Value.NPCs) } - - first := result.Value.NPCs[0] - if first.Name != "Captain Vale" || first.Description != "first description" || !reflect.DeepEqual(first.Aliases, []string{"Vale", "CV"}) { - t.Fatalf("first component = %#v, want first description and ordered aliases", first) + if refs := result.Value.NPCs[0].SourceRefs; len(refs) != 2 || refs[0].SourceID != "a" || refs[1].SourceID != "b" { + t.Fatalf("source refs = %#v, want evidence union", refs) } - if !reflect.DeepEqual(first.SourceRefs, []source.SourceRef{ - {SourceID: "a", StartUnitID: 1, EndUnitID: 1}, - {SourceID: "b", StartUnitID: 1, EndUnitID: 1}, - }) { - t.Fatalf("first provenance = %#v, want unioned refs", first.SourceRefs) - } - - second := result.Value.NPCs[1] - if second.Name != "The Sage" || !reflect.DeepEqual(second.Aliases, []string{"Archivist", "Chronicler"}) || second.Description != "sage description" { - t.Fatalf("canonical-to-alias component = %#v, want consolidated sage", second) - } - if first.Relationships[0].Target != "The Sage" { - t.Fatalf("relationship target = %q, want The Sage", first.Relationships[0].Target) - } - if !hasWarning(result.Warnings, ReasonCodeRelationshipTargetCanonicalized, "npcs[0]") || - !hasWarning(result.Warnings, ReasonCodeDuplicateNPCCollapsed, "npcs[0]") || - !hasWarning(result.Warnings, ReasonCodeDuplicateNPCCollapsed, "npcs[2]") { - t.Fatalf("warnings = %#v, want collapse and target warnings", result.Warnings) - } - - if got := result.Value.NPCs[2].Aliases; !reflect.DeepEqual(got, []string{"North Old", "North Renamed"}) { - t.Fatalf("transitive aliases = %#v, want ordered canonical members", got) - } - if result.Value.NPCs[3].Name != "Red" || result.Value.NPCs[4].Name != "Blue" { - t.Fatalf("shared-alias ordering = %#v, want Red then Blue", result.Value.NPCs[3:]) + if !hasWarning(result.Warnings, ReasonCodeDuplicateNPCCollapsed, "npcs[0]") { + t.Fatalf("warnings = %#v, want duplicate collapse", result.Warnings) } } -func TestNormalizePreservesInvalidEvidenceAndDoesNotAliasInput(t *testing.T) { - invalid := dnd.NPCList{NPCs: []dnd.NPC{{ - Name: " ", - Aliases: []string{""}, - Description: " ", - Relationships: []dnd.NPCRelationship{{Target: " ", Relationship: " "}}, - SourceRefs: []source.SourceRef{{SourceID: "source", StartUnitID: 9, EndUnitID: 9}}, - }}} - original := cloneNPCListForTest(invalid) - result, err := New(Options{}).Normalize(context.Background(), normalizeRequest(invalid)) - if err != nil { - t.Fatalf("Normalize() error = %v, want nil", err) +func TestNormalizePreservesInvalidCandidatesAndDoesNotAliasInput(t *testing.T) { + input := dnd.NPCList{NPCs: []dnd.NPC{{Name: " ", SourceRefs: []source.SourceRef{{SourceID: "source", StartUnitID: 0, EndUnitID: -1}}}}} + before := dnd.NPCList{NPCs: []dnd.NPC{{Name: " ", SourceRefs: []source.SourceRef{{SourceID: "source", StartUnitID: 0, EndUnitID: -1}}}}} + result, err := New(Options{}).Normalize(context.Background(), normalizeRequest(input)) + if err != nil || !reflect.DeepEqual(input, before) { + t.Fatalf("Normalize() = %#v, %v; input mutated to %#v", result, err, input) } - if !reflect.DeepEqual(invalid, original) { - t.Fatalf("normalizer mutated input: got %#v, want %#v", invalid, original) + if result.Value.NPCs[0].Name != "" || result.Value.NPCs[0].SourceRefs[0].EndUnitID != -1 { + t.Fatalf("candidate = %#v, want invalid semantics preserved", result.Value.NPCs[0]) } - if result.Value.NPCs[0].Name != "" || result.Value.NPCs[0].Aliases[0] != "" || result.Value.NPCs[0].Relationships[0].Target != "" { - t.Fatalf("invalid evidence was unexpectedly removed: %#v", result.Value.NPCs[0]) - } - - result.Value.NPCs[0].Aliases[0] = "changed" - result.Value.NPCs[0].Relationships[0].Target = "changed" result.Value.NPCs[0].SourceRefs[0].SourceID = "changed" - if invalid.NPCs[0].Aliases[0] != "" || invalid.NPCs[0].Relationships[0].Target != " " || invalid.NPCs[0].SourceRefs[0].SourceID != "source" { - t.Fatalf("output aliases input storage: input = %#v", invalid) + if input.NPCs[0].SourceRefs[0].SourceID != "source" { + t.Fatal("output aliases input evidence") } } @@ -234,52 +102,17 @@ func TestNormalizeHandlesNilAndCanceledCalls(t *testing.T) { normalizer := New(Options{}) result, err := normalizer.Normalize(context.Background(), normalizeRequest(dnd.NPCList{NPCs: nil})) if err != nil || result.Value.NPCs != nil { - t.Fatalf("nil list result = %#v, error = %v, want nil NPC slice", result.Value, err) + t.Fatalf("nil list result = %#v, error = %v", result.Value, err) } canceled, cancel := context.WithCancel(context.Background()) cancel() if _, err := normalizer.Normalize(canceled, normalizeRequest(dnd.NPCList{})); err == nil || !strings.Contains(err.Error(), "context error") { - t.Fatalf("canceled Normalize() error = %v, want context error", err) - } - if _, err := normalizer.Normalize(nil, normalizeRequest(dnd.NPCList{})); err == nil || !strings.Contains(err.Error(), "context must not be nil") { - t.Fatalf("nil context Normalize() error = %v, want context error", err) - } - var nilNormalizer *Normalizer - if _, err := nilNormalizer.Normalize(context.Background(), normalizeRequest(dnd.NPCList{})); err == nil { - t.Fatal("nil normalizer Normalize() error = nil, want error") - } -} - -func TestNormalizePreservesPresentEmptyAliases(t *testing.T) { - result, err := New(Options{}).Normalize(context.Background(), normalizeRequest(dnd.NPCList{NPCs: []dnd.NPC{{ - Name: "Hooded Guard", - Aliases: []string{}, - Description: "A distinguishable sentry.", - SourceRefs: []source.SourceRef{{SourceID: "session", StartUnitID: 1, EndUnitID: 1}}, - }}})) - if err != nil { - t.Fatalf("Normalize() error = %v, want nil", err) - } - if result.Value.NPCs[0].Aliases == nil || len(result.Value.NPCs[0].Aliases) != 0 { - t.Fatalf("aliases = %#v, want present empty array", result.Value.NPCs[0].Aliases) + t.Fatalf("canceled Normalize() error = %v", err) } } func normalizeRequest(value dnd.NPCList) contracts.TypedNormalizeRequest[dnd.NPCList] { - return contracts.TypedNormalizeRequest[dnd.NPCList]{ - MergeOutput: contracts.MergeArtifact[dnd.NPCList]{Value: value}, - } -} - -func cloneNPCListForTest(input dnd.NPCList) dnd.NPCList { - output := dnd.NPCList{} - if input.NPCs != nil { - output.NPCs = make([]dnd.NPC, len(input.NPCs)) - for index, npc := range input.NPCs { - output.NPCs[index] = cloneNPC(npc) - } - } - return output + return contracts.TypedNormalizeRequest[dnd.NPCList]{MergeOutput: contracts.MergeArtifact[dnd.NPCList]{Value: value}} } func hasWarning(warnings []contracts.Warning, reason, scope string) bool { diff --git a/internal/modules/dnd/npcs/identity/identity.go b/internal/modules/dnd/npcs/identity/identity.go index dc13f9c..ccfeac3 100644 --- a/internal/modules/dnd/npcs/identity/identity.go +++ b/internal/modules/dnd/npcs/identity/identity.go @@ -29,24 +29,17 @@ const idPrefix = "npc:sha256:" type IssueCode string const ( - IssueEmptyCanonicalName IssueCode = "empty_canonical_name" - IssueEmptyAlias IssueCode = "empty_alias" - IssueInvalidID IssueCode = "invalid_id" - IssueIDMismatch IssueCode = "id_mismatch" - IssueDuplicateCanonical IssueCode = "duplicate_canonical_identity" - IssueDuplicateID IssueCode = "duplicate_id" - IssueDuplicateAlias IssueCode = "duplicate_alias" - IssueOwnCanonicalAlias IssueCode = "alias_matches_canonical_name" - IssueAliasCanonicalCollision IssueCode = "alias_canonical_collision" - IssueAliasOwnershipCollision IssueCode = "alias_owned_by_multiple_records" + IssueEmptyCanonicalName IssueCode = "empty_canonical_name" + IssueInvalidID IssueCode = "invalid_id" + IssueIDMismatch IssueCode = "id_mismatch" + IssueDuplicateCanonical IssueCode = "duplicate_canonical_identity" + IssueDuplicateID IssueCode = "duplicate_id" ) -// Issue is an inspectable identity validation problem. AliasIndex is -1 when -// the issue applies to an NPC as a whole rather than a particular alias. +// Issue is an inspectable identity validation problem. type Issue struct { Code IssueCode RecordIndex int - AliasIndex int Value string } @@ -107,88 +100,35 @@ func ValidID(value string) bool { return IsValidID(value) } // It accepts the NPC slice used by typed pipeline artifacts. Use ValidateList // when the enclosing NPCList is more convenient at the call site. func ValidateRegistry(npcs []dnd.NPC) []Issue { - type record struct { - canonical string - aliases []string - } - - records := make([]record, len(npcs)) issues := make([]Issue, 0) canonicalOwners := make(map[string][]int) idOwners := make(map[string][]int) - aliasOwners := make(map[string][]int) for recordIndex, npc := range npcs { canonical := ComparisonKey(npc.Name) - records[recordIndex].canonical = canonical if canonical == "" { - issues = append(issues, Issue{Code: IssueEmptyCanonicalName, RecordIndex: recordIndex, AliasIndex: -1, Value: npc.Name}) + issues = append(issues, Issue{Code: IssueEmptyCanonicalName, RecordIndex: recordIndex, Value: npc.Name}) } else { canonicalOwners[canonical] = append(canonicalOwners[canonical], recordIndex) } if !IsValidID(npc.ID) { - issues = append(issues, Issue{Code: IssueInvalidID, RecordIndex: recordIndex, AliasIndex: -1, Value: npc.ID}) + issues = append(issues, Issue{Code: IssueInvalidID, RecordIndex: recordIndex, Value: npc.ID}) } else if expected := DeriveID(npc.Name); npc.ID != expected { - issues = append(issues, Issue{Code: IssueIDMismatch, RecordIndex: recordIndex, AliasIndex: -1, Value: npc.ID}) + issues = append(issues, Issue{Code: IssueIDMismatch, RecordIndex: recordIndex, Value: npc.ID}) } if npc.ID != "" { idOwners[npc.ID] = append(idOwners[npc.ID], recordIndex) } - - seenAliases := make(map[string]int, len(npc.Aliases)) - for aliasIndex, alias := range npc.Aliases { - key := ComparisonKey(alias) - records[recordIndex].aliases = append(records[recordIndex].aliases, key) - if key == "" { - issues = append(issues, Issue{Code: IssueEmptyAlias, RecordIndex: recordIndex, AliasIndex: aliasIndex, Value: alias}) - continue - } - if _, ok := seenAliases[key]; ok { - issues = append(issues, Issue{Code: IssueDuplicateAlias, RecordIndex: recordIndex, AliasIndex: aliasIndex, Value: alias}) - } else { - seenAliases[key] = aliasIndex - } - if key == canonical { - issues = append(issues, Issue{Code: IssueOwnCanonicalAlias, RecordIndex: recordIndex, AliasIndex: aliasIndex, Value: alias}) - } - aliasOwners[key] = append(aliasOwners[key], recordIndex) - } } - for recordIndex, record := range records { - if record.canonical != "" && len(canonicalOwners[record.canonical]) > 1 && canonicalOwners[record.canonical][0] != recordIndex { - issues = append(issues, Issue{Code: IssueDuplicateCanonical, RecordIndex: recordIndex, AliasIndex: -1, Value: npcs[recordIndex].Name}) + for recordIndex, npc := range npcs { + canonical := ComparisonKey(npc.Name) + if canonical != "" && len(canonicalOwners[canonical]) > 1 && canonicalOwners[canonical][0] != recordIndex { + issues = append(issues, Issue{Code: IssueDuplicateCanonical, RecordIndex: recordIndex, Value: npc.Name}) } - if id := npcs[recordIndex].ID; id != "" && len(idOwners[id]) > 1 && idOwners[id][0] != recordIndex { - issues = append(issues, Issue{Code: IssueDuplicateID, RecordIndex: recordIndex, AliasIndex: -1, Value: id}) - } - } - - seenAliasKeys := make(map[string]struct{}) - for _, record := range records { - for _, alias := range record.aliases { - if alias == "" { - continue - } - if _, alreadyProcessed := seenAliasKeys[alias]; alreadyProcessed { - continue - } - seenAliasKeys[alias] = struct{}{} - owners := uniqueIndexes(aliasOwners[alias]) - if len(owners) > 1 { - for _, recordIndex := range owners { - issues = append(issues, Issue{Code: IssueAliasOwnershipCollision, RecordIndex: recordIndex, AliasIndex: aliasIndexFor(records[recordIndex].aliases, alias), Value: alias}) - } - } - for _, recordIndex := range owners { - for _, canonicalOwner := range canonicalOwners[alias] { - if canonicalOwner != recordIndex { - issues = append(issues, Issue{Code: IssueAliasCanonicalCollision, RecordIndex: recordIndex, AliasIndex: aliasIndexFor(records[recordIndex].aliases, alias), Value: alias}) - break - } - } - } + if npc.ID != "" && len(idOwners[npc.ID]) > 1 && idOwners[npc.ID][0] != recordIndex { + issues = append(issues, Issue{Code: IssueDuplicateID, RecordIndex: recordIndex, Value: npc.ID}) } } @@ -201,28 +141,6 @@ func ValidateList(list dnd.NPCList) []Issue { return ValidateRegistry(list.NPCs) // Validate is a convenience alias for ValidateList. func Validate(list dnd.NPCList) []Issue { return ValidateList(list) } -func uniqueIndexes(values []int) []int { - seen := make(map[int]struct{}, len(values)) - unique := make([]int, 0, len(values)) - for _, value := range values { - if _, ok := seen[value]; ok { - continue - } - seen[value] = struct{}{} - unique = append(unique, value) - } - return unique -} - -func aliasIndexFor(aliases []string, key string) int { - for index, alias := range aliases { - if alias == key { - return index - } - } - return -1 -} - // Error makes an issue useful in simple callers while preserving its // structured fields for aggregate diagnostics. func (i Issue) Error() string { diff --git a/internal/modules/dnd/npcs/identity/identity_test.go b/internal/modules/dnd/npcs/identity/identity_test.go index 270b125..17d5fed 100644 --- a/internal/modules/dnd/npcs/identity/identity_test.go +++ b/internal/modules/dnd/npcs/identity/identity_test.go @@ -77,18 +77,13 @@ func TestIdentityFunctionsAreSafeForConcurrentUse(t *testing.T) { func TestValidateRegistryReportsIdentityCollisionCategories(t *testing.T) { validID := DeriveID("Mira Thorn") npcs := []dnd.NPC{ - {ID: validID, Name: "Mira Thorn", Aliases: []string{"The Greencloak", "the greencloak", "Mira Thorn"}}, - {ID: validID, Name: "Mira Thorn", Aliases: []string{"The Greencloak"}}, - {ID: DeriveID("Captain Vale"), Name: "Captain Vale", Aliases: []string{"Mira Thorn"}}, + {ID: validID, Name: "Mira Thorn"}, + {ID: validID, Name: "Mira Thorn"}, } issues := ValidateRegistry(npcs) want := map[IssueCode]bool{ - IssueDuplicateAlias: false, - IssueOwnCanonicalAlias: false, - IssueDuplicateCanonical: false, - IssueDuplicateID: false, - IssueAliasOwnershipCollision: false, - IssueAliasCanonicalCollision: false, + IssueDuplicateCanonical: false, + IssueDuplicateID: false, } for _, issue := range issues { if _, ok := want[issue.Code]; ok { diff --git a/internal/modules/dnd/npcs/registry/registry.go b/internal/modules/dnd/npcs/registry/registry.go index 307f659..dc91f73 100644 --- a/internal/modules/dnd/npcs/registry/registry.go +++ b/internal/modules/dnd/npcs/registry/registry.go @@ -5,6 +5,7 @@ package registry import ( "crypto/sha256" "encoding/hex" + "encoding/json" "fmt" "mime" "strings" @@ -27,12 +28,13 @@ const ( // Registry is an immutable, validated NPC registry prepared for prompt // grounding. All accessors return defensive copies. type Registry struct { - bound bool - list dnd.NPCList - canonical []byte - digest string - promptInput contracts.LLMInputMaterial - lookupByKey map[string]int + bound bool + list dnd.NPCList + canonical []byte + digest string + projectionDigest string + promptInput contracts.LLMInputMaterial + lookupByKey map[string]int } // Resolver retains only the validated construction-time registry and immutable @@ -141,11 +143,13 @@ func Resolve(references contracts.ReferenceSet) (*Registry, error) { slot, ok := references.Slots[ReferenceSlot] if !ok { content := []byte(emptyPrompt) + projectionDigest := semanticDigest(content) return &Registry{ - list: dnd.NPCList{NPCs: []dnd.NPC{}}, - canonical: append([]byte(nil), content...), - promptInput: contracts.NewLLMInputMaterial(ReferenceSlot, npccodec.MediaType, content, "", ""), - lookupByKey: map[string]int{}, + list: dnd.NPCList{NPCs: []dnd.NPC{}}, + canonical: append([]byte(nil), content...), + projectionDigest: projectionDigest, + promptInput: contracts.NewLLMInputMaterial(ReferenceSlot, npccodec.MediaType, content, projectionDigest, ""), + lookupByKey: map[string]int{}, }, nil } if len(slot.Items) != 1 { @@ -178,21 +182,24 @@ func Resolve(references contracts.ReferenceSet) (*Registry, error) { } list := cloneNPCList(value) - lookupByKey := make(map[string]int, len(list.NPCs)*2) + lookupByKey := make(map[string]int, len(list.NPCs)) for index, npc := range list.NPCs { lookupByKey[identity.ComparisonKey(npc.Name)] = index - for _, alias := range npc.Aliases { - lookupByKey[identity.ComparisonKey(alias)] = index - } } digest := semanticDigest(content) + projection, err := nameProjection(list) + if err != nil { + return nil, fmt.Errorf("encode NPC name projection: %w", err) + } + projectionDigest := semanticDigest(projection) return &Registry{ - bound: true, - list: list, - canonical: append([]byte(nil), content...), - digest: digest, - promptInput: contracts.NewLLMInputMaterial(ReferenceSlot, npccodec.MediaType, content, digest, ""), - lookupByKey: lookupByKey, + bound: true, + list: list, + canonical: append([]byte(nil), content...), + digest: digest, + projectionDigest: projectionDigest, + promptInput: contracts.NewLLMInputMaterial(ReferenceSlot, npccodec.MediaType, projection, projectionDigest, ""), + lookupByKey: lookupByKey, }, nil } @@ -235,6 +242,15 @@ func (r *Registry) Digest() string { return r.digest } +// ProjectionDigest returns the SHA-256 digest of the exact names-only prompt +// projection, including for an unbound or empty registry. +func (r *Registry) ProjectionDigest() string { + if r == nil { + return "" + } + return r.projectionDigest +} + // Count returns the number of validated NPC records. func (r *Registry) Count() int { if r == nil { @@ -243,8 +259,8 @@ func (r *Registry) Count() int { return len(r.list.NPCs) } -// PromptInput returns the canonical registry as a content-safe prompt input. -// Reference provenance is deliberately omitted. +// PromptInput returns the names-only registry projection as a content-safe +// prompt input. Durable IDs, evidence, and reference provenance are omitted. func (r *Registry) PromptInput() contracts.LLMInputMaterial { if r == nil { return contracts.LLMInputMaterial{} @@ -252,8 +268,8 @@ func (r *Registry) PromptInput() contracts.LLMInputMaterial { return r.promptInput.Clone() } -// Lookup returns the canonical NPC for an exact canonical-name or alias match -// under the NPC identity comparison policy. +// Lookup returns the canonical NPC for an exact canonical-name match under the +// NPC identity comparison policy. func (r *Registry) Lookup(value string) (dnd.NPC, bool) { if r == nil { return dnd.NPC{}, false @@ -270,13 +286,26 @@ func semanticDigest(content []byte) string { return "sha256:" + hex.EncodeToString(sum[:]) } +type projectedNPC struct { + Name string `json:"name"` +} + +type projectedNPCList struct { + NPCs []projectedNPC `json:"npcs"` +} + +func nameProjection(list dnd.NPCList) ([]byte, error) { + projection := projectedNPCList{NPCs: make([]projectedNPC, len(list.NPCs))} + for index, npc := range list.NPCs { + projection.NPCs[index] = projectedNPC{Name: npc.Name} + } + return json.Marshal(projection) +} + func formatIdentityIssues(issues []identity.Issue) string { parts := make([]string, len(issues)) for index, issue := range issues { location := fmt.Sprintf("record %d", issue.RecordIndex) - if issue.AliasIndex >= 0 { - location += fmt.Sprintf(" alias %d", issue.AliasIndex) - } parts[index] = fmt.Sprintf("%s at %s", issue.Code, location) } return diagnostics.Aggregate("validate NPC registry identity", parts) @@ -298,8 +327,6 @@ func cloneNPCs(values []dnd.NPC) []dnd.NPC { } func cloneNPC(value dnd.NPC) dnd.NPC { - value.Aliases = append([]string(nil), value.Aliases...) - value.Relationships = append([]dnd.NPCRelationship(nil), value.Relationships...) value.SourceRefs = append([]source.SourceRef(nil), value.SourceRefs...) return value } diff --git a/internal/modules/dnd/npcs/registry/registry_test.go b/internal/modules/dnd/npcs/registry/registry_test.go index 5880b04..ab7e0b4 100644 --- a/internal/modules/dnd/npcs/registry/registry_test.go +++ b/internal/modules/dnd/npcs/registry/registry_test.go @@ -2,313 +2,162 @@ package registry import ( "bytes" - "encoding/json" - "fmt" + "reflect" "strings" - "sync" "testing" - "unicode/utf8" "gitea.maximumdirect.net/eric/notarius/internal/core/source" "gitea.maximumdirect.net/eric/notarius/internal/framework/contracts" "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd" npccodec "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/codec/npcs" "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/npcs/identity" - "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/shared/diagnostics" ) -func TestResolveAbsentRegistryUsesExactEmptyPrompt(t *testing.T) { - resolved, err := Resolve(contracts.ReferenceSet{}) +func TestResolveUnboundRegistryHasExactEmptyProjection(t *testing.T) { + registry, err := Resolve(contracts.ReferenceSet{}) if err != nil { - t.Fatalf("Resolve() error = %v, want nil", err) + t.Fatalf("Resolve() error = %v", err) } - if resolved.Bound() || resolved.Digest() != "" || resolved.Count() != 0 { - t.Fatalf("resolved unbound registry = %#v, want no semantic metadata", resolved) + input := registry.PromptInput() + if registry.Bound() || registry.Digest() != "" || registry.Count() != 0 || string(input.Content) != emptyPrompt { + t.Fatalf("registry = %#v input = %#v, want unbound empty registry", registry, input) } - input := resolved.PromptInput() - if input.Name != ReferenceSlot || input.MediaType != npccodec.MediaType || input.Digest != "" || input.OriginURI != "" { - t.Fatalf("unbound prompt input metadata = %#v, want name/media type only", input) - } - if got := string(input.Content); got != emptyPrompt { - t.Fatalf("unbound prompt input = %q, want exact empty registry", got) - } - if got := string(resolved.CanonicalBytes()); got != emptyPrompt { - t.Fatalf("unbound canonical bytes = %q, want exact empty registry", got) + if registry.ProjectionDigest() == "" || input.Digest != registry.ProjectionDigest() || input.OriginURI != "" { + t.Fatalf("projection digest/input = %q/%#v", registry.ProjectionDigest(), input) } } -func TestResolveCanonicalizesAndProvidesSemanticIdentity(t *testing.T) { - value := validRegistryList() - canonical := encodeRegistry(t, value) - raw := append([]byte(" \n"), canonical...) - raw = append(raw, []byte("\n ")...) - - resolved, err := Resolve(registryReference(raw, "file:///another-session/npcs.json")) - if err != nil { - t.Fatalf("Resolve() error = %v, want nil", err) +func TestResolveKeepsDurableProvenanceAndProjectsOnlyOrderedNames(t *testing.T) { + list := registryFixture() + registry := resolveList(t, list) + if !registry.Bound() || registry.Digest() == "" || registry.Count() != 2 { + t.Fatalf("registry identity = bound %t digest %q count %d", registry.Bound(), registry.Digest(), registry.Count()) } - if !resolved.Bound() || resolved.Count() != len(value.NPCs) { - t.Fatalf("resolved registry = %#v, want bound registry with %d NPC", resolved, len(value.NPCs)) + if got := string(registry.PromptInput().Content); got != `{"npcs":[{"name":"Mira Thorn"},{"name":"Captain Vale"}]}` { + t.Fatalf("prompt projection = %s", got) } - if !bytes.Equal(resolved.CanonicalBytes(), canonical) || !bytes.Equal(resolved.PromptInput().Content, canonical) { - t.Fatalf("canonical content = %s, want %s", resolved.CanonicalBytes(), canonical) - } - if resolved.PromptInput().Digest != resolved.Digest() || !strings.HasPrefix(resolved.Digest(), "sha256:") { - t.Fatalf("semantic digest = %q, want SHA-256 digest", resolved.Digest()) - } - if resolved.PromptInput().OriginURI != "" { - t.Fatalf("prompt input origin = %q, want no provenance path", resolved.PromptInput().OriginURI) - } -} - -func TestResolveRejectsInvalidBoundaryValuesWithoutContent(t *testing.T) { - valid := validRegistryList() - second := valid.NPCs[0] - second.ID = identity.DeriveID("Captain Vale") - second.Name = "Captain Vale" - second.Aliases = []string{"The Greencloak"} - valueWithAliasCollision := dnd.NPCList{NPCs: []dnd.NPC{valid.NPCs[0], second}} - invalidID := valid - invalidID.NPCs[0].ID = "not-an-npc-id" - - tests := []struct { - name string - reference contracts.ReferenceSet - wantError string - forbidden []string - }{ - {name: "zero items", reference: contracts.ReferenceSet{Slots: map[string]contracts.ResolvedReferenceSlot{ReferenceSlot: {Items: []contracts.ReferenceItem{}}}}, wantError: "exactly one"}, - {name: "multiple", reference: contracts.ReferenceSet{Slots: map[string]contracts.ResolvedReferenceSlot{ReferenceSlot: {Items: []contracts.ReferenceItem{{Content: []byte(emptyPrompt)}, {Content: []byte(emptyPrompt)}}}}}, wantError: "exactly one"}, - {name: "wrong media type", reference: registryReferenceWithMedia([]byte(emptyPrompt), "text/plain"), wantError: "must be application/json"}, - {name: "malformed JSON", reference: registryReference([]byte(`{"npcs":[],"MALFORMED_REGISTRY_SECRET":`), "file:///private.json"), wantError: "invalid approved NPC JSON", forbidden: []string{"MALFORMED_REGISTRY_SECRET"}}, - {name: "unknown field", reference: registryReference([]byte(`{"npcs":[],"UNKNOWN_FIELD_SECRET":true}`), "file:///private.json"), wantError: "invalid approved NPC JSON", forbidden: []string{"UNKNOWN_FIELD_SECRET"}}, - {name: "invalid ID", reference: registryReference(marshalRegistry(t, invalidID), "file:///private.json"), wantError: "decode NPC registry"}, - {name: "alias collision", reference: registryReference(encodeRegistry(t, valueWithAliasCollision), "file:///private.json"), wantError: string(identity.IssueAliasOwnershipCollision)}, - {name: "byte limit", reference: registryReference(bytes.Repeat([]byte("x"), MaxBytes+1), "file:///private.json"), wantError: "limit"}, - } - for _, test := range tests { - t.Run(test.name, func(t *testing.T) { - _, err := Resolve(test.reference) - if err == nil || !strings.Contains(err.Error(), test.wantError) { - t.Fatalf("Resolve() error = %v, want %q", err, test.wantError) - } - for _, forbidden := range append(test.forbidden, "Mira Thorn", "The Greencloak", "private.json") { - if strings.Contains(err.Error(), forbidden) { - t.Fatalf("error leaked registry content or provenance %q: %v", forbidden, err) - } - } - }) - } -} - -func TestResolveBoundsIdentityDiagnosticsWithoutContent(t *testing.T) { - const recordCount = 30 - value := dnd.NPCList{NPCs: make([]dnd.NPC, recordCount)} - for index := range value.NPCs { - value.NPCs[index] = dnd.NPC{ - ID: "npc:sha256:0000000000000000000000000000000000000000000000000000000000000000", - Name: fmt.Sprintf("PRIVATE NPC %d", index), - Aliases: []string{"PRIVATE SHARED ALIAS"}, - Description: "PRIVATE DESCRIPTION", - Relationships: []dnd.NPCRelationship{}, - SourceRefs: []source.SourceRef{{SourceID: "private-source", StartUnitID: 1, EndUnitID: 1}}, + for _, forbidden := range []string{"npc:sha256:", "source_refs", "source_id", "session-alpha"} { + if strings.Contains(string(registry.PromptInput().Content), forbidden) { + t.Fatalf("projection leaked %q: %s", forbidden, registry.PromptInput().Content) } } - issues := identity.ValidateList(value) - if len(issues) <= diagnostics.MaxIssues { - t.Fatalf("identity issues = %d, want more than display limit", len(issues)) - } - - _, err := Resolve(registryReference(marshalRegistry(t, value), "file:///private-registry.json")) - if err == nil { - t.Fatal("Resolve() error = nil, want bounded identity rejection") - } - message := err.Error() - if !utf8.ValidString(message) || len([]byte(message)) > diagnostics.MaxMessageBytes { - t.Fatalf("identity error has invalid encoding or size: bytes=%d message=%q", len([]byte(message)), message) - } - wantOmitted := fmt.Sprintf("%d additional issue(s) omitted", len(issues)-diagnostics.MaxIssues) - if !strings.Contains(message, wantOmitted) { - t.Fatalf("identity error = %q, want %q", message, wantOmitted) - } - for _, forbidden := range []string{"PRIVATE NPC", "PRIVATE SHARED ALIAS", "PRIVATE DESCRIPTION", "private-source", "private-registry.json"} { - if strings.Contains(message, forbidden) { - t.Fatalf("identity error leaked %q: %s", forbidden, message) - } + if registry.PromptInput().Digest != registry.ProjectionDigest() || registry.Digest() == registry.ProjectionDigest() { + t.Fatalf("full/projection digests = %q/%q", registry.Digest(), registry.ProjectionDigest()) } } -func TestRegistryAccessorsAndLookupAreDefensive(t *testing.T) { - resolved, err := Resolve(registryReference(encodeRegistry(t, validRegistryList()), "file:///npc-registry.json")) +func TestNameProjectionDigestTracksOnlyNamesAndOrder(t *testing.T) { + base := registryFixture() + evidenceChanged := registryFixture() + evidenceChanged.NPCs[0].ID = "different application id" + evidenceChanged.NPCs[0].SourceRefs = []source.SourceRef{{SourceID: "other", StartUnitID: 40, EndUnitID: 41}} + baseBytes, err := nameProjection(base) if err != nil { - t.Fatalf("Resolve() error = %v, want nil", err) + t.Fatal(err) + } + changedBytes, err := nameProjection(evidenceChanged) + if err != nil { + t.Fatal(err) + } + if !bytes.Equal(baseBytes, changedBytes) || semanticDigest(baseBytes) != semanticDigest(changedBytes) { + t.Fatalf("equivalent name projections differ: %s / %s", baseBytes, changedBytes) } - npcs := resolved.NPCs() + nameChanged := registryFixture() + nameChanged.NPCs[0].Name = "The Greencloak" + nameBytes, _ := nameProjection(nameChanged) + orderChanged := registryFixture() + orderChanged.NPCs[0], orderChanged.NPCs[1] = orderChanged.NPCs[1], orderChanged.NPCs[0] + orderBytes, _ := nameProjection(orderChanged) + if bytes.Equal(baseBytes, nameBytes) || bytes.Equal(baseBytes, orderBytes) { + t.Fatalf("name/order changes did not change projection: %s %s %s", baseBytes, nameBytes, orderBytes) + } +} + +func TestRegistryLookupAndAccessorsAreImmutable(t *testing.T) { + registry := resolveList(t, registryFixture()) + if npc, ok := registry.Lookup(" mIRA\u2003thorn "); !ok || npc.Name != "Mira Thorn" { + t.Fatalf("Lookup() = %#v, %t", npc, ok) + } + if _, ok := registry.Lookup("The Greencloak"); ok { + t.Fatal("Lookup() accepted a non-canonical name") + } + + npcs := registry.NPCs() npcs[0].Name = "changed" - npcs[0].Aliases[0] = "changed alias" - npcs[0].Relationships[0].Target = "changed target" - npcs[0].SourceRefs[0].SourceID = "changed source" - if got, ok := resolved.Lookup("The Greencloak"); !ok || got.Name != "Mira Thorn" { - t.Fatalf("Lookup() after NPC mutation = %#v, %v, want original NPC", got, ok) + npcs[0].SourceRefs[0].SourceID = "changed" + content := registry.CanonicalBytes() + content[0] = '[' + input := registry.PromptInput() + input.Content[0] = '[' + if next := registry.NPCs()[0]; next.Name != "Mira Thorn" || next.SourceRefs[0].SourceID != "session-alpha" { + t.Fatalf("registry mutated through accessor: %#v", next) } - - wantCanonical := string(resolved.CanonicalBytes()) - content := resolved.CanonicalBytes() - content[0] = 'X' - input := resolved.PromptInput() - input.Content[0] = 'X' - if string(resolved.CanonicalBytes()) != wantCanonical || string(resolved.PromptInput().Content) != wantCanonical { - t.Fatal("registry content accessors share mutable state") - } - if got, ok := resolved.Lookup(" MIRA\u00a0THORN "); !ok || got.Name != "Mira Thorn" { - t.Fatalf("Lookup() canonical identity = %#v, %v, want Mira Thorn", got, ok) - } - if _, ok := resolved.Lookup("unknown NPC"); ok { - t.Fatal("Lookup() found unknown NPC") + if registry.CanonicalBytes()[0] != '{' || registry.PromptInput().Content[0] != '{' { + t.Fatal("registry bytes mutated through accessor") } } -func TestResolverUsesSeededFallbackAndCachesGeneratedCanonicalRegistry(t *testing.T) { - staticContent := encodeRegistry(t, validRegistryList()) - resolver, err := NewResolver(registryReference(staticContent, "file:///static.json")) - if err != nil { - t.Fatalf("NewResolver() error = %v", err) - } - if got, err := resolver.Resolve(contracts.ReferenceSet{}); err != nil || got != resolver.Seeded() { - t.Fatalf("Resolve(absent) = %p, %v, want seeded %p", got, err, resolver.Seeded()) - } - - generated := registryReference(append([]byte("\n"), staticContent...), "file:///generated.json") - first, err := resolver.Resolve(generated) - if err != nil { - t.Fatalf("Resolve(generated) error = %v", err) - } - second, err := resolver.Resolve(generated) - if err != nil { - t.Fatalf("Resolve(generated second) error = %v", err) - } - if first != resolver.Seeded() || second != first { - t.Fatalf("resolved registries = %p, %p, seeded %p; want seeded reuse", first, second, resolver.Seeded()) - } - - changed := validRegistryList() - changed.NPCs[0].Description = "A changed generated description." - changedContent := encodeRegistry(t, changed) - changedReferences := registryReference(changedContent, "file:///changed.json") - resolved, err := resolver.Resolve(changedReferences) - if err != nil { - t.Fatalf("Resolve(changed) error = %v", err) - } - changedReferences.Slots[ReferenceSlot].Items[0].Content[0] = 'X' - if resolved == first || string(resolved.CanonicalBytes()) != string(changedContent) { - t.Fatalf("changed registry = %p/%s, want independent canonical cache entry", resolved, resolved.CanonicalBytes()) - } -} - -func TestResolverSharesOneCachedRegistryAcrossConcurrentOperations(t *testing.T) { - content := encodeRegistry(t, validRegistryList()) - resolver, err := NewResolver(contracts.ReferenceSet{}) - if err != nil { - t.Fatalf("NewResolver() error = %v", err) - } - references := registryReference(content, "file:///generated.json") - const callers = 32 - results := make(chan *Registry, callers) - errors := make(chan error, callers) - var wait sync.WaitGroup - for index := 0; index < callers; index++ { - wait.Add(1) - go func() { - defer wait.Done() - resolved, resolveErr := resolver.Resolve(references) - if resolveErr != nil { - errors <- resolveErr - return - } - results <- resolved - }() - } - wait.Wait() - close(results) - close(errors) - for err := range errors { - t.Fatalf("concurrent Resolve() error = %v", err) - } - var first *Registry - for resolved := range results { - if first == nil { - first = resolved - } else if resolved != first { - t.Fatalf("concurrent resolved registry %p differs from cached %p", resolved, first) +func TestResolveRejectsRichOrInvalidRegistryJSON(t *testing.T) { + rich := []byte(`{"npcs":[{"id":"npc:sha256:99a16589618a04f535a7d21fdcc71a0b1c05d22f752cd492065b1086d97bc3d7","name":"Mira Thorn","aliases":[],"source_refs":[{"source_id":"session-alpha","start_unit_id":1,"end_unit_id":1}]}]}`) + for _, item := range []contracts.ReferenceItem{ + {MediaType: "application/json", Content: rich}, + {MediaType: "text/plain", Content: []byte(`{"npcs":[]}`)}, + } { + _, err := Resolve(referenceSet(item)) + if err == nil { + t.Fatalf("Resolve(%s) error = nil", item.Content) } } } -func TestNewResolverAllowsGeneratedDeclarationButRejectsMalformedStaticItem(t *testing.T) { - placeholder := contracts.ReferenceSet{Slots: map[string]contracts.ResolvedReferenceSlot{ - ReferenceSlot: {Slot: contracts.ReferenceSlot{Name: ReferenceSlot, AcceptedArtifactKinds: []contracts.ArtifactKind{dnd.NPCListKind}}}, - }} - if _, err := NewResolver(placeholder); err != nil { - t.Fatalf("NewResolver(generated declaration) error = %v, want nil", err) - } - malformed := registryReference([]byte(`{"npcs":[`), "file:///runtime.json") - if _, err := NewResolver(malformed); err == nil || !strings.Contains(err.Error(), "invalid approved NPC JSON") { - t.Fatalf("NewResolver(malformed) error = %v, want bounded decode failure", err) - } -} - -func validRegistryList() dnd.NPCList { - return dnd.NPCList{NPCs: []dnd.NPC{{ - ID: identity.DeriveID("Mira Thorn"), - Name: "Mira Thorn", - Aliases: []string{"The Greencloak"}, - Description: "A guarded ranger who watches the northern road.", - Relationships: []dnd.NPCRelationship{{ - Target: "Captain Vale", Relationship: "reports to", - }}, - SourceRefs: []source.SourceRef{{SourceID: "npc-session", StartUnitID: 41, EndUnitID: 43}}, - }}} -} - -func encodeRegistry(t *testing.T, value dnd.NPCList) []byte { - t.Helper() - content, err := npccodec.New().Encode(value) +func TestResolverReusesEquivalentCanonicalRegistries(t *testing.T) { + set := listReferenceSet(t, registryFixture()) + resolver, err := NewResolver(set) if err != nil { - t.Fatalf("encode NPC registry: %v", err) + t.Fatal(err) } - return content -} - -func marshalRegistry(t *testing.T, value dnd.NPCList) []byte { - t.Helper() - content, err := json.Marshal(value) - if err != nil { - t.Fatalf("marshal NPC registry: %v", err) + resolved, err := resolver.Resolve(set) + if err != nil || resolved != resolver.Seeded() { + t.Fatalf("Resolve() = %p, %v; seeded %p", resolved, err, resolver.Seeded()) } - return content } -func registryReference(content []byte, origin string) contracts.ReferenceSet { - references := registryReferenceWithMedia(content, "application/json; charset=utf-8") - item := references.Slots[ReferenceSlot].Items[0] - item.Origin.URI = origin - slot := references.Slots[ReferenceSlot] - slot.Items[0] = item - references.Slots[ReferenceSlot] = slot - return references -} - -func registryReferenceWithMedia(content []byte, mediaType string) contracts.ReferenceSet { - return contracts.ReferenceSet{Slots: map[string]contracts.ResolvedReferenceSlot{ - ReferenceSlot: { - Slot: contracts.ReferenceSlot{Name: ReferenceSlot, AcceptedMediaTypes: []string{npccodec.MediaType}, MaxBytes: MaxBytes}, - Items: []contracts.ReferenceItem{{ - SlotName: ReferenceSlot, - MediaType: mediaType, - Content: append([]byte(nil), content...), - Origin: contracts.ReferenceOrigin{Type: "file", URI: "file:///npc-registry.json"}, - }}, - }, +func registryFixture() dnd.NPCList { + return dnd.NPCList{NPCs: []dnd.NPC{ + {ID: identity.DeriveID("Mira Thorn"), Name: "Mira Thorn", SourceRefs: []source.SourceRef{{SourceID: "session-alpha", StartUnitID: 1, EndUnitID: 2}}}, + {ID: identity.DeriveID("Captain Vale"), Name: "Captain Vale", SourceRefs: []source.SourceRef{{SourceID: "session-alpha", StartUnitID: 3, EndUnitID: 3}}}, }} } + +func resolveList(t *testing.T, list dnd.NPCList) *Registry { + t.Helper() + registry, err := Resolve(listReferenceSet(t, list)) + if err != nil { + t.Fatalf("Resolve() error = %v", err) + } + return registry +} + +func listReferenceSet(t *testing.T, list dnd.NPCList) contracts.ReferenceSet { + t.Helper() + content, err := npccodec.New().Encode(list) + if err != nil { + t.Fatalf("Encode() error = %v", err) + } + return referenceSet(contracts.ReferenceItem{SlotName: ReferenceSlot, MediaType: npccodec.MediaType, Content: content}) +} + +func referenceSet(item contracts.ReferenceItem) contracts.ReferenceSet { + return contracts.ReferenceSet{Slots: map[string]contracts.ResolvedReferenceSlot{ReferenceSlot: {Items: []contracts.ReferenceItem{item}}}} +} + +func TestProjectionIsStableForEquivalentNormalizedRegistries(t *testing.T) { + first := resolveList(t, registryFixture()) + secondList := registryFixture() + secondList.NPCs[0].SourceRefs = append(secondList.NPCs[0].SourceRefs, source.SourceRef{SourceID: "session-beta", StartUnitID: 8, EndUnitID: 8}) + second := resolveList(t, secondList) + if !reflect.DeepEqual(first.PromptInput().Content, second.PromptInput().Content) || first.ProjectionDigest() != second.ProjectionDigest() || first.Digest() == second.Digest() { + t.Fatalf("projection/full identity mismatch: %#v %#v", first, second) + } +} diff --git a/internal/modules/dnd/types.go b/internal/modules/dnd/types.go index 1b8858b..9f12a66 100644 --- a/internal/modules/dnd/types.go +++ b/internal/modules/dnd/types.go @@ -29,17 +29,9 @@ type NPCList struct { } type NPC struct { - ID string `json:"id"` - Name string `json:"name"` - Aliases []string `json:"aliases"` - Description string `json:"description"` - Relationships []NPCRelationship `json:"relationships"` - SourceRefs []source.SourceRef `json:"source_refs"` -} - -type NPCRelationship struct { - Target string `json:"target"` - Relationship string `json:"relationship"` + ID string `json:"id"` + Name string `json:"name"` + SourceRefs []source.SourceRef `json:"source_refs"` } type CombatTurnList struct { diff --git a/internal/modules/dnd/validate/npcs/identity/validator.go b/internal/modules/dnd/validate/npcs/identity/validator.go index f6d7fcd..7883cd1 100644 --- a/internal/modules/dnd/validate/npcs/identity/validator.go +++ b/internal/modules/dnd/validate/npcs/identity/validator.go @@ -48,9 +48,6 @@ func (v *Validator) Validate(_ context.Context, req contracts.TypedValidationReq issues := make([]string, len(identityIssues)) for index, issue := range identityIssues { location := fmt.Sprintf("npcs[%d]", issue.RecordIndex) - if issue.AliasIndex >= 0 { - location += fmt.Sprintf(".aliases[%d]", issue.AliasIndex) - } issues[index] = fmt.Sprintf("%s %s: %s", location, issue.Code, diagnostics.Quote(issue.Value)) } return contracts.ValidationResult{ diff --git a/internal/modules/dnd/validate/npcs/identity/validator_test.go b/internal/modules/dnd/validate/npcs/identity/validator_test.go index 7aa16e4..6d887cc 100644 --- a/internal/modules/dnd/validate/npcs/identity/validator_test.go +++ b/internal/modules/dnd/validate/npcs/identity/validator_test.go @@ -43,7 +43,7 @@ func TestValidatorContractAndRegistration(t *testing.T) { func TestValidatorDefersShapeAndRejectsIdentityIssues(t *testing.T) { validator := New(Options{}) - shapeInvalid := dnd.NPCList{NPCs: []dnd.NPC{{Name: "missing description"}}} + shapeInvalid := dnd.NPCList{NPCs: []dnd.NPC{{Name: "missing evidence"}}} result, err := validator.Validate(context.Background(), validationRequest(shapeInvalid)) if err != nil || !result.Approved { t.Fatalf("shape-invalid result = %#v, error = %v, want deferred approval", result, err) @@ -51,13 +51,13 @@ func TestValidatorDefersShapeAndRejectsIdentityIssues(t *testing.T) { value := validNPCList(2) value.NPCs[0].ID = "not-an-id" - value.NPCs[1].Aliases = []string{"Shared Alias"} - value.NPCs[0].Aliases = []string{"Shared Alias"} + value.NPCs[1].Name = value.NPCs[0].Name + value.NPCs[1].ID = value.NPCs[0].ID result, err = validator.Validate(context.Background(), validationRequest(value)) if err != nil || result.Approved || result.ReasonCode != ReasonCode { t.Fatalf("identity result = %#v, error = %v, want rejection", result, err) } - for _, want := range []string{"invalid_id", "alias_owned_by_multiple_records", "npcs[0]", "npcs[1]"} { + for _, want := range []string{"invalid_id", "duplicate_canonical_identity", "duplicate_id", "npcs[0]", "npcs[1]"} { if !strings.Contains(result.Message, want) { t.Fatalf("identity message %q missing %q", result.Message, want) } @@ -90,14 +90,7 @@ func validNPCList(count int) dnd.NPCList { value := dnd.NPCList{NPCs: make([]dnd.NPC, count)} for index := range value.NPCs { name := fmt.Sprintf("NPC %d", index) - value.NPCs[index] = dnd.NPC{ - ID: "npc:sha256:0000000000000000000000000000000000000000000000000000000000000000", - Name: name, - Aliases: []string{}, - Description: "description", - Relationships: []dnd.NPCRelationship{}, - SourceRefs: []source.SourceRef{sourceRefForTest()}, - } + value.NPCs[index] = dnd.NPC{ID: "npc:sha256:0000000000000000000000000000000000000000000000000000000000000000", Name: name, SourceRefs: []source.SourceRef{sourceRefForTest()}} } return value } diff --git a/internal/modules/dnd/validate/npcs/shape/validator.go b/internal/modules/dnd/validate/npcs/shape/validator.go index bb13823..0bf778a 100644 --- a/internal/modules/dnd/validate/npcs/shape/validator.go +++ b/internal/modules/dnd/validate/npcs/shape/validator.go @@ -61,31 +61,6 @@ func issuesFor(value dnd.NPCList) []string { if strings.TrimSpace(npc.Name) == "" { issues = append(issues, prefix+".name must not be empty") } - if npc.Aliases == nil { - issues = append(issues, prefix+".aliases must be present") - } else { - for aliasIndex, alias := range npc.Aliases { - if strings.TrimSpace(alias) == "" { - issues = append(issues, fmt.Sprintf("%s.aliases[%d] must not be empty: %s", prefix, aliasIndex, diagnostics.Quote(alias))) - } - } - } - if strings.TrimSpace(npc.Description) == "" { - issues = append(issues, prefix+".description must not be empty") - } - if npc.Relationships == nil { - issues = append(issues, prefix+".relationships must be present") - } else { - for relationshipIndex, relationship := range npc.Relationships { - relationshipPrefix := fmt.Sprintf("%s.relationships[%d]", prefix, relationshipIndex) - if strings.TrimSpace(relationship.Target) == "" { - issues = append(issues, relationshipPrefix+".target must not be empty: "+diagnostics.Quote(relationship.Target)) - } - if strings.TrimSpace(relationship.Relationship) == "" { - issues = append(issues, relationshipPrefix+".relationship must not be empty: "+diagnostics.Quote(relationship.Relationship)) - } - } - } if len(npc.SourceRefs) == 0 { issues = append(issues, prefix+".source_refs must not be empty") } diff --git a/internal/modules/dnd/validate/npcs/shape/validator_test.go b/internal/modules/dnd/validate/npcs/shape/validator_test.go index 5dc0273..96e6c5e 100644 --- a/internal/modules/dnd/validate/npcs/shape/validator_test.go +++ b/internal/modules/dnd/validate/npcs/shape/validator_test.go @@ -21,9 +21,9 @@ func TestValidatorApprovesWellFormedNPCPayload(t *testing.T) { func TestValidatorRejectsRequiredShapeValues(t *testing.T) { value := validNPCList() - value.NPCs[0].Aliases = nil + value.NPCs[0].Name = "" result, err := New(Options{}).Validate(context.Background(), requestWithValue(value)) - if err != nil || result.Approved || result.ReasonCode != ReasonCode || !strings.Contains(result.Message, "aliases must be present") { + if err != nil || result.Approved || result.ReasonCode != ReasonCode || !strings.Contains(result.Message, "name must not be empty") { t.Fatalf("Validate() = %#v, %v; want bounded shape rejection", result, err) } @@ -38,13 +38,13 @@ func TestValidatorBoundsDiagnosticsAndQuotesUnicode(t *testing.T) { value := dnd.NPCList{NPCs: make([]dnd.NPC, 24)} long := strings.Repeat("火", 220) + "\n\t" for index := range value.NPCs { - value.NPCs[index] = dnd.NPC{ID: "candidate", Name: long, Aliases: []string{"\n\t"}, Description: "", Relationships: []dnd.NPCRelationship{{Target: " ", Relationship: " "}}, SourceRefs: []source.SourceRef{}} + value.NPCs[index] = dnd.NPC{ID: "", Name: long, SourceRefs: []source.SourceRef{}} } result, err := New(Options{}).Validate(context.Background(), requestWithValue(value)) if err != nil || result.Approved || len([]byte(result.Message)) > diagnosticsMaxMessageBytes || !utf8.ValidString(result.Message) { t.Fatalf("Validate() = %#v, %v; want bounded valid UTF-8 rejection", result, err) } - if strings.Count(result.Message, "npcs[") > diagnosticsMaxIssues || !strings.Contains(result.Message, "additional issue(s) omitted") || !strings.Contains(result.Message, `\n\t`) { + if strings.Count(result.Message, "npcs[") > diagnosticsMaxIssues || !strings.Contains(result.Message, "additional issue(s) omitted") { t.Fatalf("message = %q, want bounded quoted diagnostics", result.Message) } } @@ -69,7 +69,7 @@ func TestValidatorDoesNotMutateValue(t *testing.T) { value := validNPCList() before := value _, err := New(Options{}).Validate(context.Background(), requestWithValue(value)) - if err != nil || value.NPCs[0].Aliases[0] != before.NPCs[0].Aliases[0] { + if err != nil || value.NPCs[0].Name != before.NPCs[0].Name { t.Fatalf("Validate() mutated value: %#v", value) } } @@ -80,9 +80,8 @@ func requestWithValue(value dnd.NPCList) contracts.TypedValidationRequest[dnd.NP func validNPCList() dnd.NPCList { return dnd.NPCList{NPCs: []dnd.NPC{{ - ID: "candidate", Name: "Mira Thorn", Aliases: []string{"The Greencloak"}, Description: "A guarded ranger.", - Relationships: []dnd.NPCRelationship{{Target: "Captain Vale", Relationship: "reports to"}}, - SourceRefs: []source.SourceRef{{SourceID: "session", StartUnitID: 1, EndUnitID: 1}}, + ID: "candidate", Name: "Mira Thorn", + SourceRefs: []source.SourceRef{{SourceID: "session", StartUnitID: 1, EndUnitID: 1}}, }}} } diff --git a/internal/modules/dnd/validate/npcs/source_refs/validator_test.go b/internal/modules/dnd/validate/npcs/source_refs/validator_test.go index a042a0e..926335d 100644 --- a/internal/modules/dnd/validate/npcs/source_refs/validator_test.go +++ b/internal/modules/dnd/validate/npcs/source_refs/validator_test.go @@ -84,5 +84,5 @@ func validDocument() *source.SourceDocument { } func validNPCList() dnd.NPCList { - return dnd.NPCList{NPCs: []dnd.NPC{{ID: "candidate", Name: "Mira Thorn", Aliases: []string{"The Greencloak"}, Description: "A ranger.", Relationships: []dnd.NPCRelationship{}, SourceRefs: []source.SourceRef{{SourceID: "session", StartUnitID: 1, EndUnitID: 2}}}}} + return dnd.NPCList{NPCs: []dnd.NPC{{ID: "candidate", Name: "Mira Thorn", SourceRefs: []source.SourceRef{{SourceID: "session", StartUnitID: 1, EndUnitID: 2}}}}} } diff --git a/internal/modules/dnd/validate/npcs/source_relatedness/validator.go b/internal/modules/dnd/validate/npcs/source_relatedness/validator.go index 9101654..339d195 100644 --- a/internal/modules/dnd/validate/npcs/source_relatedness/validator.go +++ b/internal/modules/dnd/validate/npcs/source_relatedness/validator.go @@ -60,15 +60,7 @@ func (v *Validator) Validate(_ context.Context, req contracts.TypedValidationReq } func npcAppearsInCitedText(citedText string, npc dnd.NPC) bool { - if shared.ContainsTokenSequence(citedText, npc.Name) { - return true - } - for _, alias := range npc.Aliases { - if shared.ContainsTokenSequence(citedText, alias) { - return true - } - } - return false + return shared.ContainsTokenSequence(citedText, npc.Name) } func Spec() pipeline.ValidatorSpec { diff --git a/internal/modules/dnd/validate/npcs/source_relatedness/validator_test.go b/internal/modules/dnd/validate/npcs/source_relatedness/validator_test.go index f9f644f..63181f7 100644 --- a/internal/modules/dnd/validate/npcs/source_relatedness/validator_test.go +++ b/internal/modules/dnd/validate/npcs/source_relatedness/validator_test.go @@ -12,14 +12,14 @@ import ( "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd" ) -func TestValidatorMatchesCanonicalNamesAndAliasesWithUnicodeVariants(t *testing.T) { +func TestValidatorMatchesCanonicalNamesWithUnicodeVariants(t *testing.T) { value := dnd.NPCList{NPCs: []dnd.NPC{ - {ID: "one", Name: "O'Rin Thorn", Aliases: []string{}, Description: "A ranger.", Relationships: []dnd.NPCRelationship{}, SourceRefs: []source.SourceRef{ + {ID: "one", Name: "O'Rin Thorn", SourceRefs: []source.SourceRef{ {SourceID: "session", StartUnitID: 2, EndUnitID: 2}, {SourceID: "session", StartUnitID: 1, EndUnitID: 2}, {SourceID: "session", StartUnitID: 1, EndUnitID: 2}, }}, - {ID: "two", Name: "Missing Name", Aliases: []string{"The Greencloak"}, Description: "A guard.", Relationships: []dnd.NPCRelationship{}, SourceRefs: []source.SourceRef{{SourceID: "session", StartUnitID: 2, EndUnitID: 2}}}, + {ID: "two", Name: "The Greencloak", SourceRefs: []source.SourceRef{{SourceID: "session", StartUnitID: 2, EndUnitID: 2}}}, }} doc := &source.SourceDocument{ID: "session", Kind: "transcript", Format: "application/json", Digest: "sha256:session", Units: []source.SourceUnit{ {ID: 1, Kind: "message", Text: " o’rin\u2003thorn appears."}, @@ -27,13 +27,13 @@ func TestValidatorMatchesCanonicalNamesAndAliasesWithUnicodeVariants(t *testing. }} result, err := New(Options{}).Validate(context.Background(), contracts.TypedValidationRequest[dnd.NPCList]{Source: doc, Value: value}) if err != nil || !result.Approved || len(result.Warnings) != 0 { - t.Fatalf("Validate() = %#v, %v; want alias/canonical relatedness approval", result, err) + t.Fatalf("Validate() = %#v, %v; want canonical-name relatedness approval", result, err) } } func TestValidatorWarnsAtMostOncePerNPCForUnrelatedCitations(t *testing.T) { value := dnd.NPCList{NPCs: []dnd.NPC{ - {ID: "one", Name: "Missing\nName", Aliases: []string{"Also Missing"}, Description: "A guard.", Relationships: []dnd.NPCRelationship{}, SourceRefs: []source.SourceRef{{SourceID: "session", StartUnitID: 1, EndUnitID: 1}, {SourceID: "session", StartUnitID: 1, EndUnitID: 1}}}, + {ID: "one", Name: "Missing\nName", SourceRefs: []source.SourceRef{{SourceID: "session", StartUnitID: 1, EndUnitID: 1}, {SourceID: "session", StartUnitID: 1, EndUnitID: 1}}}, }} result, err := New(Options{}).Validate(context.Background(), contracts.TypedValidationRequest[dnd.NPCList]{Source: relatednessDocument(), Value: value}) if err != nil || !result.Approved || len(result.Warnings) != 1 { @@ -47,7 +47,7 @@ func TestValidatorWarnsAtMostOncePerNPCForUnrelatedCitations(t *testing.T) { func TestValidatorDoesNotMatchShortNameSubstring(t *testing.T) { value := dnd.NPCList{NPCs: []dnd.NPC{{ - ID: "one", Name: "Art", Aliases: []string{}, Description: "A guard.", Relationships: []dnd.NPCRelationship{}, SourceRefs: []source.SourceRef{{SourceID: "session", StartUnitID: 1, EndUnitID: 1}}, + ID: "one", Name: "Art", SourceRefs: []source.SourceRef{{SourceID: "session", StartUnitID: 1, EndUnitID: 1}}, }}} doc := &source.SourceDocument{ID: "session", Kind: "transcript", Format: "application/json", Digest: "sha256:session", Units: []source.SourceUnit{{ID: 1, Kind: "message", Text: "A cart rolls past."}}} result, err := New(Options{}).Validate(context.Background(), contracts.TypedValidationRequest[dnd.NPCList]{Source: doc, Value: value}) @@ -62,7 +62,7 @@ func TestValidatorDefersMalformedShapeAndInvalidRangesDoNotPanic(t *testing.T) { if err != nil || !result.Approved || len(result.Warnings) != 0 { t.Fatalf("shape deferral = %#v, %v; want approval without warning", result, err) } - invalidRange := dnd.NPCList{NPCs: []dnd.NPC{{ID: "one", Name: "Mira Thorn", Aliases: []string{}, Description: "A ranger.", Relationships: []dnd.NPCRelationship{}, SourceRefs: []source.SourceRef{{SourceID: "session", StartUnitID: 99, EndUnitID: 99}}}}} + invalidRange := dnd.NPCList{NPCs: []dnd.NPC{{ID: "one", Name: "Mira Thorn", SourceRefs: []source.SourceRef{{SourceID: "session", StartUnitID: 99, EndUnitID: 99}}}}} result, err = New(Options{}).Validate(context.Background(), contracts.TypedValidationRequest[dnd.NPCList]{Source: relatednessDocument(), Value: invalidRange}) if err != nil || !result.Approved || len(result.Warnings) != 0 { t.Fatalf("invalid-range relatedness = %#v, %v; want approval without warning", result, err) @@ -70,7 +70,7 @@ func TestValidatorDefersMalformedShapeAndInvalidRangesDoNotPanic(t *testing.T) { } func TestValidatorUsesOnlyTranscriptEvidenceAndRegistersPolicy(t *testing.T) { - value := dnd.NPCList{NPCs: []dnd.NPC{{ID: "one", Name: "Opaque NPC", Aliases: []string{}, Description: "A guard.", Relationships: []dnd.NPCRelationship{}, SourceRefs: []source.SourceRef{{SourceID: "session", StartUnitID: 1, EndUnitID: 1}}}}} + value := dnd.NPCList{NPCs: []dnd.NPC{{ID: "one", Name: "Opaque NPC", SourceRefs: []source.SourceRef{{SourceID: "session", StartUnitID: 1, EndUnitID: 1}}}}} references := contracts.ReferenceSet{Slots: map[string]contracts.ResolvedReferenceSlot{"glossary": {Items: []contracts.ReferenceItem{{Content: []byte("Opaque NPC")}}}}} result, err := New(Options{}).Validate(context.Background(), contracts.TypedValidationRequest[dnd.NPCList]{Source: relatednessDocument(), References: references, Value: value}) if err != nil || len(result.Warnings) != 1 { diff --git a/internal/modules/integration/dnd_combat_runner_test.go b/internal/modules/integration/dnd_combat_runner_test.go index c9081c6..a972470 100644 --- a/internal/modules/integration/dnd_combat_runner_test.go +++ b/internal/modules/integration/dnd_combat_runner_test.go @@ -49,9 +49,9 @@ func TestProductionCombatPipelineRetriesMergesNormalizesAndWritesJSON(t *testing client := &fakeCombatLLMClient{responses: []string{ combatTestInvalidEnumResponse("unsupported", "attack"), - combatTestTurnResponse("The Greencloak", "turn", "watches", "The Greencloak", 1, 1), + combatTestTurnResponse("mira thorn", "turn", "watches", "mira thorn", 1, 1), combatTestTurnResponse("Mira Thorn", "reaction", "asks", "Hooded Guard", 2, 2), - combatTestTurnResponse("Hooded Guard", "turn", "attacks", "The Greencloak", 3, 3), + combatTestTurnResponse("Hooded Guard", "turn", "attacks", "mira thorn", 3, 3), }} prepared, err := pipeline.Prepare(materialized, registries, pipeline.ModuleDependencies{LLM: client}) if err != nil { @@ -250,8 +250,8 @@ type combatNPCPayload struct { func combatTestNPCPayload(t *testing.T) combatNPCPayload { t.Helper() value := dnd.NPCList{NPCs: []dnd.NPC{ - {ID: identity.DeriveID("Mira Thorn"), Name: "Mira Thorn", Aliases: []string{"The Greencloak", "Mira"}, Description: "A ranger.", Relationships: []dnd.NPCRelationship{}, SourceRefs: []source.SourceRef{{SourceID: "prior-npc-session", StartUnitID: 1, EndUnitID: 1}}}, - {ID: identity.DeriveID("Hooded Guard"), Name: "Hooded Guard", Aliases: []string{}, Description: "A sentry.", Relationships: []dnd.NPCRelationship{}, SourceRefs: []source.SourceRef{{SourceID: "prior-npc-session", StartUnitID: 3, EndUnitID: 3}}}, + {ID: identity.DeriveID("Mira Thorn"), Name: "Mira Thorn", SourceRefs: []source.SourceRef{{SourceID: "prior-npc-session", StartUnitID: 1, EndUnitID: 1}}}, + {ID: identity.DeriveID("Hooded Guard"), Name: "Hooded Guard", SourceRefs: []source.SourceRef{{SourceID: "prior-npc-session", StartUnitID: 3, EndUnitID: 3}}}, }} content, err := npccodec.New().Encode(value) if err != nil { diff --git a/internal/modules/integration/dnd_npc_grounded_test.go b/internal/modules/integration/dnd_npc_grounded_test.go index 0ded177..411d2ba 100644 --- a/internal/modules/integration/dnd_npc_grounded_test.go +++ b/internal/modules/integration/dnd_npc_grounded_test.go @@ -65,14 +65,16 @@ func TestNPCOutputGroundsSpellAndCombatConsumersThroughOneOperation(t *testing.T t.Fatalf("encode canonical NPC producer payload: %v", err) } canonicalDigest := digestBytes(npcPayload) + projection := []byte(`{"npcs":[{"name":"Mira Thorn"},{"name":"Hooded Guard"}]}`) + projectionDigest := digestBytes(projection) seenConsumers := map[string]bool{} for _, request := range client.requestsSnapshot() { if request.PromptID != spells.PromptID && request.PromptID != combatextract.PromptID { continue } input := request.Inputs["npcs"] - if input.MediaType != npccodec.MediaType || input.Digest != canonicalDigest || string(input.Content) != string(npcPayload) || input.OriginURI != "" { - t.Fatalf("%s NPC prompt input = %#v, want canonical generated registry without provenance", request.PromptID, input) + if input.MediaType != npccodec.MediaType || input.Digest != projectionDigest || string(input.Content) != string(projection) || input.OriginURI != "" { + t.Fatalf("%s NPC prompt input = %#v, want names-only generated registry", request.PromptID, input) } seenConsumers[request.PromptID] = true } @@ -107,7 +109,7 @@ func TestNPCOutputGroundsSpellAndCombatConsumersThroughOneOperation(t *testing.T switch serialized.LaneID { case "spells": spellValue := decodeRunnerSpellResponse(t, serialized.Artifact.Content) - if len(spellValue.SpellCasts) != 1 || spellValue.SpellCasts[0].Caster != "The Greencloak" { + if len(spellValue.SpellCasts) != 1 || spellValue.SpellCasts[0].Caster != "Mira Thorn" { t.Fatalf("spell output = %#v, want one registry-grounded-context spell", spellValue) } assertSpellEvidence(t, spellValue.SpellCasts[0].SourceRefs) @@ -174,25 +176,23 @@ func (client *groundedDNDLLMClient) CompleteStructured(ctx context.Context, requ case npcs.PromptID: payload = map[string]any{"npcs": []any{ map[string]any{ - "name": "Mira Thorn", "aliases": []string{"The Greencloak"}, "description": "A guarded ranger.", - "relationships": []any{}, "source_refs": []any{map[string]int{"start_unit_id": 1, "end_unit_id": 1}}, + "name": "Mira Thorn", "source_refs": []any{map[string]int{"start_unit_id": 1, "end_unit_id": 1}}, }, map[string]any{ - "name": "Hooded Guard", "aliases": []string{}, "description": "A sentry.", - "relationships": []any{}, "source_refs": []any{map[string]int{"start_unit_id": 3, "end_unit_id": 3}}, + "name": "Hooded Guard", "source_refs": []any{map[string]int{"start_unit_id": 3, "end_unit_id": 3}}, }, }} case spells.PromptID: payload = map[string]any{"spell_casts": []any{map[string]any{ - "caster": "The Greencloak", "spell": "Cure Wounds", "effect": "Restores an ally.", - "narrative_description": "The Greencloak restores an ally.", + "caster": "Mira Thorn", "spell": "Cure Wounds", "effect": "Restores an ally.", + "narrative_description": "Mira Thorn restores an ally.", "source_refs": []any{map[string]int{"start_unit_id": 1, "end_unit_id": 1}}, }}} case combatextract.PromptID: payload = map[string]any{"combat_turns": []any{map[string]any{ - "actor": "The Greencloak", "turn_kind": "turn", "round": 1, + "actor": "Mira Thorn", "turn_kind": "turn", "round": 1, "actions": []any{map[string]any{"category": "attack", "declaration": "watches", "targets": []string{"Hooded Guard"}, "resolution": "observed"}}, - "summary": "The Greencloak watches the gate.", + "summary": "Mira Thorn watches the gate.", "source_refs": []any{map[string]int{"start_unit_id": 1, "end_unit_id": 1}}, }}} default: diff --git a/internal/modules/integration/dnd_npcs_runner_test.go b/internal/modules/integration/dnd_npcs_runner_test.go index d989e12..a9d7c43 100644 --- a/internal/modules/integration/dnd_npcs_runner_test.go +++ b/internal/modules/integration/dnd_npcs_runner_test.go @@ -5,7 +5,6 @@ import ( "encoding/json" "fmt" "os" - "reflect" "strings" "testing" @@ -40,30 +39,15 @@ func TestRunnerProcessesSeriatimInputWithProductionDNDNPCPipeline(t *testing.T) client := &fakeNPCProductionLLMClient{response: npcProductionResponse{ NPCs: []npcProductionRecord{ { - Name: "Mira Thorn", - Aliases: []string{"The Greencloak"}, - Description: "The first named NPC encountered.", - Relationships: []npcProductionRelationship{ - {Target: "Hooded Guard", Relationship: "works with"}, - }, + Name: "Mira Thorn", SourceRefs: []npcProductionSourceRef{{StartUnitID: 1, EndUnitID: 1}}, }, { - Name: "The Greencloak", - Aliases: []string{"Mira"}, - Description: "A later description that must not replace the first.", - Relationships: []npcProductionRelationship{ - {Target: "Hooded Guard", Relationship: "trusts"}, - }, + Name: " mira thorn ", SourceRefs: []npcProductionSourceRef{{StartUnitID: 2, EndUnitID: 2}}, }, { - Name: "Hooded Guard", - Aliases: []string{}, - Description: "An unnamed but distinguishable sentry.", - Relationships: []npcProductionRelationship{ - {Target: "The Greencloak", Relationship: "reports to"}, - }, + Name: "Hooded Guard", SourceRefs: []npcProductionSourceRef{{StartUnitID: 3, EndUnitID: 3}}, }, }}, @@ -87,15 +71,12 @@ func TestRunnerProcessesSeriatimInputWithProductionDNDNPCPipeline(t *testing.T) t.Fatalf("NPC output = %#v, want repeated name consolidated and group/PC omitted", value.NPCs) } first, second := value.NPCs[0], value.NPCs[1] - if first.Name != "Mira Thorn" || first.Description != "The first named NPC encountered." || !reflect.DeepEqual(first.Aliases, []string{"The Greencloak", "Mira"}) { + if first.Name != "Mira Thorn" || len(first.SourceRefs) != 2 { t.Fatalf("first NPC = %#v, want consolidated Mira identity", first) } if first.ID != identity.DeriveID(first.Name) || second.Name != "Hooded Guard" || second.ID != identity.DeriveID(second.Name) { t.Fatalf("NPC IDs = %q/%q, want derived IDs", first.ID, second.ID) } - if first.Relationships[0].Target != "Hooded Guard" || second.Relationships[0].Target != "Mira Thorn" { - t.Fatalf("relationship targets = %q/%q, want canonical target rewrite", first.Relationships[0].Target, second.Relationships[0].Target) - } for _, npc := range value.NPCs { for _, ref := range npc.SourceRefs { if ref.SourceID != doc.ID { @@ -103,8 +84,8 @@ func TestRunnerProcessesSeriatimInputWithProductionDNDNPCPipeline(t *testing.T) } } } - if !hasNPCWarning(output.Warnings, "duplicate_npc_collapsed") || !hasNPCWarning(output.Warnings, "relationship_target_canonicalized") { - t.Fatalf("warnings = %#v, want consolidation and target warnings", output.Warnings) + if !hasNPCWarning(output.Warnings, "duplicate_npc_collapsed") { + t.Fatalf("warnings = %#v, want name-only consolidation warning", output.Warnings) } if output.Manifest.ValidationStatus != "approved" || len(output.Manifest.ArtifactLanes) != 1 { t.Fatalf("manifest = %#v, want approved NPC lane", output.Manifest) @@ -149,25 +130,25 @@ func TestProductionNPCPipelineRoutesSemanticCandidatesToDeterministicValidators( }{ { name: "blank string", - response: []byte(`{"npcs":[{"name":"","aliases":[],"description":"A participant.","relationships":[],"source_refs":[{"start_unit_id":1,"end_unit_id":1}]}]}`), + response: []byte(`{"npcs":[{"name":"","source_refs":[{"start_unit_id":1,"end_unit_id":1}]}]}`), reasonCode: npcshape.ReasonCode, validatorName: npcshape.Key, }, { name: "empty evidence", - response: []byte(`{"npcs":[{"name":"Mira Thorn","aliases":[],"description":"A participant.","relationships":[],"source_refs":[]}]}`), + response: []byte(`{"npcs":[{"name":"Mira Thorn","source_refs":[]}]}`), reasonCode: npcshape.ReasonCode, validatorName: npcshape.Key, }, { name: "nonpositive unit candidate", - response: []byte(`{"npcs":[{"name":"Mira Thorn","aliases":[],"description":"A participant.","relationships":[],"source_refs":[{"start_unit_id":0,"end_unit_id":1}]}]}`), + response: []byte(`{"npcs":[{"name":"Mira Thorn","source_refs":[{"start_unit_id":0,"end_unit_id":1}]}]}`), reasonCode: npcsourcerefs.ReasonCode, validatorName: npcsourcerefs.Key, }, { name: "unknown unit candidate", - response: []byte(`{"npcs":[{"name":"Mira Thorn","aliases":[],"description":"A participant.","relationships":[],"source_refs":[{"start_unit_id":99,"end_unit_id":99}]}]}`), + response: []byte(`{"npcs":[{"name":"Mira Thorn","source_refs":[{"start_unit_id":99,"end_unit_id":99}]}]}`), reasonCode: npcsourcerefs.ReasonCode, validatorName: npcsourcerefs.Key, }, @@ -194,16 +175,8 @@ type npcProductionResponse struct { } type npcProductionRecord struct { - Name string `json:"name"` - Aliases []string `json:"aliases"` - Description string `json:"description"` - Relationships []npcProductionRelationship `json:"relationships"` - SourceRefs []npcProductionSourceRef `json:"source_refs"` -} - -type npcProductionRelationship struct { - Target string `json:"target"` - Relationship string `json:"relationship"` + Name string `json:"name"` + SourceRefs []npcProductionSourceRef `json:"source_refs"` } type npcProductionSourceRef struct {