Protect deterministic D&D prompt inputs
This commit is contained in:
@@ -1,57 +1,107 @@
|
||||
package shared
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
||||
)
|
||||
|
||||
func TestPromptInputsBuildExpectedInputs(t *testing.T) {
|
||||
func TestPromptInputsIdentity(t *testing.T) {
|
||||
source := contracts.NewLLMInputMaterial("source", "application/json", []byte("source text"), "sha256:source", "file:///source.json")
|
||||
references := contracts.ReferenceSet{Slots: map[string]contracts.ResolvedReferenceSlot{
|
||||
baseReferences := contracts.ReferenceSet{Slots: map[string]contracts.ResolvedReferenceSlot{
|
||||
"players": slotWithContent("players", "Alice: Aria"),
|
||||
"party": slotWithContent("party", "Aria: cleric"),
|
||||
"glossary": slotWithContent("glossary", "Brightmantle: temple"),
|
||||
}}
|
||||
|
||||
inputs := PromptInputs(source, references)
|
||||
for _, name := range []string{"transcript", "players", "party", "glossary"} {
|
||||
if _, ok := inputs[name]; !ok {
|
||||
t.Fatalf("PromptInputs() missing %q: %#v", name, inputs)
|
||||
}
|
||||
}
|
||||
if _, ok := inputs["roster"]; ok {
|
||||
t.Fatalf("PromptInputs() included roster input: %#v", inputs)
|
||||
}
|
||||
if got := inputs["transcript"].Name; got != "transcript" {
|
||||
t.Fatalf("transcript name = %q, want transcript", got)
|
||||
}
|
||||
if got := string(inputs["transcript"].Content); got != "source text" {
|
||||
t.Fatalf("transcript content = %q, want source text", got)
|
||||
}
|
||||
if got := string(inputs["players"].Content); got != "Alice: Aria" {
|
||||
t.Fatalf("players content = %q, want player reference", got)
|
||||
}
|
||||
if got := string(inputs["party"].Content); got != "Aria: cleric" {
|
||||
t.Fatalf("party content = %q, want party reference", got)
|
||||
}
|
||||
if got := string(inputs["glossary"].Content); got != "Brightmantle: temple" {
|
||||
t.Fatalf("glossary content = %q, want glossary reference", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPromptInputsUseRosterWhenPartyIsEmpty(t *testing.T) {
|
||||
inputs := PromptInputs(contracts.LLMInputMaterial{}, contracts.ReferenceSet{Slots: map[string]contracts.ResolvedReferenceSlot{
|
||||
orderedReferences := contracts.ReferenceSet{Slots: map[string]contracts.ResolvedReferenceSlot{
|
||||
"players": {Items: []contracts.ReferenceItem{
|
||||
referenceItem("players", "file:///b.txt", "sha256:bbb", "second"),
|
||||
referenceItem("players", "file:///a.txt", "sha256:aaa", "first"),
|
||||
}},
|
||||
}}
|
||||
reversedReferences := contracts.ReferenceSet{Slots: map[string]contracts.ResolvedReferenceSlot{
|
||||
"players": {Items: []contracts.ReferenceItem{
|
||||
referenceItem("players", "file:///a.txt", "sha256:aaa", "first"),
|
||||
referenceItem("players", "file:///b.txt", "sha256:bbb", "second"),
|
||||
}},
|
||||
}}
|
||||
rosterReferences := contracts.ReferenceSet{Slots: map[string]contracts.ResolvedReferenceSlot{
|
||||
"party": {},
|
||||
"roster": slotWithContent("roster", "Legacy roster text"),
|
||||
}})
|
||||
}}
|
||||
explicitPartyReferences := contracts.ReferenceSet{Slots: map[string]contracts.ResolvedReferenceSlot{
|
||||
"party": slotWithContent("party", "Current party text"),
|
||||
"roster": slotWithContent("roster", "Legacy roster text"),
|
||||
}}
|
||||
|
||||
if got := string(inputs["party"].Content); got != "Legacy roster text" {
|
||||
t.Fatalf("party content = %q, want roster fallback content", got)
|
||||
tests := []struct {
|
||||
name string
|
||||
references contracts.ReferenceSet
|
||||
other *contracts.ReferenceSet
|
||||
want map[string]string
|
||||
}{
|
||||
{
|
||||
name: "identical materials produce deeply equal inputs",
|
||||
references: baseReferences,
|
||||
other: &baseReferences,
|
||||
want: map[string]string{
|
||||
"transcript": "source text",
|
||||
"players": "Alice: Aria",
|
||||
"party": "Aria: cleric",
|
||||
"glossary": "Brightmantle: temple",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "reference insertion order does not change bytes",
|
||||
references: orderedReferences,
|
||||
other: &reversedReferences,
|
||||
want: map[string]string{"players": "Reference 1\nOrigin-URI: file:///a.txt\nDigest: sha256:aaa\n\nfirst\n\nReference 2\nOrigin-URI: file:///b.txt\nDigest: sha256:bbb\n\nsecond"},
|
||||
},
|
||||
{
|
||||
name: "roster becomes canonical party input",
|
||||
references: rosterReferences,
|
||||
want: map[string]string{"party": "Legacy roster text"},
|
||||
},
|
||||
{
|
||||
name: "explicit party wins over roster",
|
||||
references: explicitPartyReferences,
|
||||
want: map[string]string{"party": "Current party text"},
|
||||
},
|
||||
{
|
||||
name: "missing optional slots use placeholders",
|
||||
references: contracts.ReferenceSet{},
|
||||
want: map[string]string{
|
||||
"players": " ",
|
||||
"party": " ",
|
||||
"glossary": " ",
|
||||
},
|
||||
},
|
||||
}
|
||||
if _, ok := inputs["roster"]; ok {
|
||||
t.Fatalf("PromptInputs() included roster input: %#v", inputs)
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
inputs := PromptInputs(source, test.references)
|
||||
if test.other != nil {
|
||||
other := PromptInputs(source, *test.other)
|
||||
if !reflect.DeepEqual(inputs, other) {
|
||||
t.Fatalf("PromptInputs() differs for equivalent references:\nfirst=%#v\nsecond=%#v", inputs, other)
|
||||
}
|
||||
}
|
||||
for name, want := range test.want {
|
||||
got, ok := inputs[name]
|
||||
if !ok {
|
||||
t.Fatalf("PromptInputs() missing %q: %#v", name, inputs)
|
||||
}
|
||||
if got := string(got.Content); got != want {
|
||||
t.Fatalf("%s content = %q, want %q", name, got, want)
|
||||
}
|
||||
}
|
||||
if _, ok := inputs["roster"]; ok {
|
||||
t.Fatalf("PromptInputs() included roster input: %#v", inputs)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -149,3 +199,12 @@ func slotWithContent(name string, content string) contracts.ResolvedReferenceSlo
|
||||
}},
|
||||
}
|
||||
}
|
||||
|
||||
func referenceItem(slotName, uri, digest, content string) contracts.ReferenceItem {
|
||||
return contracts.ReferenceItem{
|
||||
SlotName: slotName,
|
||||
Content: []byte(content),
|
||||
Digest: digest,
|
||||
Origin: contracts.ReferenceOrigin{URI: uri},
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user