Include glossary guidance in proposal conversations

This commit is contained in:
2026-11-26 18:22:52 +00:00
parent 20db09bd2a
commit e0445abe48
15 changed files with 82 additions and 15 deletions

View File

@@ -221,6 +221,8 @@ Acceptance:
## Stage 5: Render instructions in proposals and retain them during repair ## Stage 5: Render instructions in proposals and retain them during repair
Status: Complete.
Work: Work:
1. Extend shared stage glossary variables with an instruction block prepared 1. Extend shared stage glossary variables with an instruction block prepared

View File

@@ -31,10 +31,7 @@ type TranscriptModule interface {
type StructuredCompletionRequest = structuredoutput.Request type StructuredCompletionRequest = structuredoutput.Request
type StructuredCompletionResponse = structuredoutput.Response type StructuredCompletionResponse = structuredoutput.Response
type GlossaryInstruction struct { type GlossaryInstruction = promptcontext.GlossaryInstruction
Collection string
Text string
}
// ModuleRunSpec identifies one resolved module instance in a pipeline. // ModuleRunSpec identifies one resolved module instance in a pipeline.
type ModuleRunSpec struct { type ModuleRunSpec struct {
@@ -62,6 +59,7 @@ func (s *ModuleRunSpec) PrepareGlossary() error {
if err != nil { if err != nil {
return err return err
} }
variables.InstructionsBlock = promptcontext.GlossaryInstructionsBlock(s.GlossaryInstructions)
s.GlossaryVariables = &variables s.GlossaryVariables = &variables
} }
if s.GlossaryView == nil { if s.GlossaryView == nil {

View File

@@ -4,6 +4,7 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"reflect" "reflect"
"strings"
"testing" "testing"
"gitea.maximumdirect.net/eric/audita/v4/internal/core/chunking" "gitea.maximumdirect.net/eric/audita/v4/internal/core/chunking"
@@ -95,24 +96,31 @@ func TestResolveModuleRunSpecs(t *testing.T) {
func TestResolveGlossaryInstructions(t *testing.T) { func TestResolveGlossaryInstructions(t *testing.T) {
cfg := &config.Config{ cfg := &config.Config{
GlossaryCollections: map[string]config.GlossaryCollection{ GlossaryCollections: map[string]config.GlossaryCollection{
"one": {Instruction: "First"}, "two": {Instruction: "Second"}, "protected": {Instruction: "Protection only"}, "one": {Instruction: "First {{literal}}"}, "two": {Instruction: "Second"}, "protected": {Instruction: "Protection only"}, "empty": {},
}, },
Pipeline: config.Pipeline{Stages: []config.Stage{ Pipeline: config.Pipeline{Stages: []config.Stage{
{Module: "grammar", Glossaries: []string{"two", "one"}, ProtectedTerms: []string{"protected"}}, {Module: "grammar", Glossaries: []string{"two", "empty", "one"}, ProtectedTerms: []string{"protected"}},
{Module: "grammar", Glossaries: []string{"two", "one"}, ProtectedTerms: []string{"protected"}}, {Module: "grammar", Glossaries: []string{"two", "empty", "one"}, ProtectedTerms: []string{"protected"}},
{Module: "grammar", Glossaries: []string{"one"}, ProtectedTerms: []string{"protected"}}, {Module: "grammar", Glossaries: []string{"one"}, ProtectedTerms: []string{"protected"}},
}}, }},
} }
specs := ResolveModuleRunSpecs(cfg) specs := ResolveModuleRunSpecs(cfg)
want := []GlossaryInstruction{{Collection: "two", Text: "Second"}, {Collection: "one", Text: "First"}} want := []GlossaryInstruction{{Collection: "two", Text: "Second"}, {Collection: "empty"}, {Collection: "one", Text: "First {{literal}}"}}
for _, spec := range specs[:2] { for _, spec := range specs[:2] {
if !reflect.DeepEqual(spec.GlossaryInstructions, want) { if !reflect.DeepEqual(spec.GlossaryInstructions, want) {
t.Fatalf("instructions = %+v, want %+v", spec.GlossaryInstructions, want) t.Fatalf("instructions = %+v, want %+v", spec.GlossaryInstructions, want)
} }
} }
if !reflect.DeepEqual(specs[2].GlossaryInstructions, want[1:]) { if !reflect.DeepEqual(specs[2].GlossaryInstructions, want[2:]) {
t.Fatalf("specific selection = %+v", specs[2].GlossaryInstructions) t.Fatalf("specific selection = %+v", specs[2].GlossaryInstructions)
} }
if err := specs[0].PrepareGlossary(); err != nil {
t.Fatal(err)
}
block := specs[0].GlossaryVariables.InstructionsBlock
if !strings.Contains(block, "two") || !strings.Contains(block, "one") || !strings.Contains(block, "First {{literal}}") || strings.Index(block, "Second") > strings.Index(block, "First") || strings.Contains(block, "empty") || strings.Contains(block, "Protection only") {
t.Fatalf("instruction block = %q", block)
}
} }
func TestSectionMetadataFromSection(t *testing.T) { func TestSectionMetadataFromSection(t *testing.T) {

View File

@@ -69,7 +69,7 @@ func TestRoleClientCoordinatesIndependentRepairConversations(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
vars := map[string]string{"TranscriptDescriptionBlock": "", "GlossaryJSON": "[]", "SectionJSON": "[]"} vars := map[string]string{"TranscriptDescriptionBlock": "", "GlossaryJSON": "[]", "SectionJSON": "[]", "GlossaryInstructionsBlock": "party:\nKeep {{literal}} names"}
initial, err := proposal.CompleteStructured(context.Background(), structuredoutput.Request{PromptID: prompts.PromptIDModuleGrammarProposal, Variables: vars, CaptureExchange: true}, &map[string]any{}) initial, err := proposal.CompleteStructured(context.Background(), structuredoutput.Request{PromptID: prompts.PromptIDModuleGrammarProposal, Variables: vars, CaptureExchange: true}, &map[string]any{})
if err != nil || initial.Exchange == nil { if err != nil || initial.Exchange == nil {
t.Fatalf("capture: %+v %v", initial, err) t.Fatalf("capture: %+v %v", initial, err)
@@ -95,6 +95,11 @@ func TestRoleClientCoordinatesIndependentRepairConversations(t *testing.T) {
t.Fatalf("models = %v", models) t.Fatalf("models = %v", models)
} }
prefix := messages[0] prefix := messages[0]
initialMessages, _ := json.Marshal(prefix)
repairMessages, _ := json.Marshal(messages[1])
if !strings.Contains(string(initialMessages), "Keep {{literal}} names") || !strings.Contains(string(repairMessages), "Keep {{literal}} names") {
t.Fatalf("guidance missing from proposal or repair: %s %s", initialMessages, repairMessages)
}
if len(messages) != 3 || !reflect.DeepEqual(messages[1][:len(prefix)], prefix) || !reflect.DeepEqual(messages[2][:len(prefix)], prefix) { if len(messages) != 3 || !reflect.DeepEqual(messages[1][:len(prefix)], prefix) || !reflect.DeepEqual(messages[2][:len(prefix)], prefix) {
t.Fatalf("prefixes: %+v", messages) t.Fatalf("prefixes: %+v", messages)
} }

View File

@@ -74,8 +74,8 @@ func TestProcessingPlanReusesOnlyMatchingGlossaryViews(t *testing.T) {
exempt.Module = "glossary" exempt.Module = "glossary"
cfg := &config.Config{ cfg := &config.Config{
GlossaryCollections: map[string]config.GlossaryCollection{ GlossaryCollections: map[string]config.GlossaryCollection{
"a": {Entries: []config.GlossaryEntry{{ID: "a", Name: "A"}}}, "a": {Instruction: "First instruction", Entries: []config.GlossaryEntry{{ID: "a", Name: "A"}}},
"b": {Entries: []config.GlossaryEntry{{ID: "b", Name: "B"}}}, "b": {Instruction: "Second instruction", Entries: []config.GlossaryEntry{{ID: "b", Name: "B"}}},
}, },
Pipeline: config.Pipeline{Stages: []config.Stage{item, item, reversed, exempt}}, Pipeline: config.Pipeline{Stages: []config.Stage{item, item, reversed, exempt}},
} }
@@ -93,6 +93,9 @@ func TestProcessingPlanReusesOnlyMatchingGlossaryViews(t *testing.T) {
plan[2].Glossary[0].ID != "b" || plan[3].GlossaryVariables.ProtectionJSON != "[]" { plan[2].Glossary[0].ID != "b" || plan[3].GlossaryVariables.ProtectionJSON != "[]" {
t.Fatalf("incorrect glossary reuse or exemptions: %+v", plan) t.Fatalf("incorrect glossary reuse or exemptions: %+v", plan)
} }
if !strings.Contains(plan[1].GlossaryVariables.InstructionsBlock, "First instruction") || strings.Index(plan[2].GlossaryVariables.InstructionsBlock, "Second instruction") > strings.Index(plan[2].GlossaryVariables.InstructionsBlock, "First instruction") {
t.Fatalf("instruction reuse/order: %+v", plan)
}
} }
func TestProcessingPlanPreservesFirstInspectionFailureAndCause(t *testing.T) { func TestProcessingPlanPreservesFirstInspectionFailureAndCause(t *testing.T) {

View File

@@ -3,14 +3,31 @@ package promptcontext
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"strings"
"gitea.maximumdirect.net/eric/audita/v4/internal/core/glossary" "gitea.maximumdirect.net/eric/audita/v4/internal/core/glossary"
) )
// GlossaryVariables contains the glossary inputs shared by a stage's proposals. // GlossaryVariables contains the glossary inputs shared by a stage's proposals.
type GlossaryVariables struct { type GlossaryVariables struct {
ContextJSON string ContextJSON string
ProtectionJSON string ProtectionJSON string
InstructionsBlock string
}
type GlossaryInstruction struct {
Collection string
Text string
}
func GlossaryInstructionsBlock(instructions []GlossaryInstruction) string {
var blocks []string
for _, instruction := range instructions {
if instruction.Text != "" {
blocks = append(blocks, instruction.Collection+":\n"+instruction.Text)
}
}
return strings.Join(blocks, "\n\n")
} }
func PrepareGlossaryVariables(entries []glossary.Entry, terms []glossary.Term) (GlossaryVariables, error) { func PrepareGlossaryVariables(entries []glossary.Entry, terms []glossary.Term) (GlossaryVariables, error) {

View File

@@ -35,6 +35,7 @@ func ExecuteModuleProposal(ctx context.Context, req ModuleProposalRequest) (cont
if err != nil { if err != nil {
return contracts.ProposalResult{}, err return contracts.ProposalResult{}, err
} }
prepared.InstructionsBlock = promptcontext.GlossaryInstructionsBlock(req.ProposalRequest.RunSpec.GlossaryInstructions)
glossary = &prepared glossary = &prepared
} }
variables, err := buildProposalVariables( variables, err := buildProposalVariables(

View File

@@ -15,6 +15,7 @@ func buildProposalVariables(transcript *schema.Transcript, glossary promptcontex
return map[string]string{ return map[string]string{
"TranscriptDescriptionBlock": promptcontext.TranscriptDescriptionBlock(transcriptDescription), "TranscriptDescriptionBlock": promptcontext.TranscriptDescriptionBlock(transcriptDescription),
"GlossaryJSON": glossary.ContextJSON, "GlossaryJSON": glossary.ContextJSON,
"GlossaryInstructionsBlock": glossary.InstructionsBlock,
"ProtectedTermsJSON": glossary.ProtectionJSON, "ProtectedTermsJSON": glossary.ProtectionJSON,
"SectionJSON": string(sectionJSON), "SectionJSON": string(sectionJSON),
}, nil }, nil

View File

@@ -16,11 +16,12 @@ func TestBuildProposalVariablesPreservesSectionGlossaryAndDescription(t *testing
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
prepared.InstructionsBlock = "party: use this guidance"
variables, err := buildProposalVariables(transcript, prepared, 0, " Fantasy names ") variables, err := buildProposalVariables(transcript, prepared, 0, " Fantasy names ")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if !strings.Contains(variables["SectionJSON"], `"section_index": 0`) || !strings.Contains(variables["SectionJSON"], "gestures") || !strings.Contains(variables["GlossaryJSON"], "Jesters") || !strings.Contains(variables["TranscriptDescriptionBlock"], "Fantasy names") { if !strings.Contains(variables["SectionJSON"], `"section_index": 0`) || !strings.Contains(variables["SectionJSON"], "gestures") || !strings.Contains(variables["GlossaryJSON"], "Jesters") || !strings.Contains(variables["TranscriptDescriptionBlock"], "Fantasy names") || variables["GlossaryInstructionsBlock"] != prepared.InstructionsBlock {
t.Fatalf("encoded variables incomplete: %+v", variables) t.Fatalf("encoded variables incomplete: %+v", variables)
} }
empty, err := buildProposalVariables(transcript, prepared, 0, " ") empty, err := buildProposalVariables(transcript, prepared, 0, " ")

View File

@@ -31,6 +31,9 @@ collections:
{{ .TranscriptDescriptionBlock }}Glossary context: {{ .TranscriptDescriptionBlock }}Glossary context:
{{ .GlossaryJSON }} {{ .GlossaryJSON }}
Glossary-use guidance:
{{ .GlossaryInstructionsBlock }}
Protected spellings: Protected spellings:
{{ .ProtectedTermsJSON }} {{ .ProtectedTermsJSON }}

View File

@@ -27,6 +27,9 @@ collections:
{{ .TranscriptDescriptionBlock }}Glossary context: {{ .TranscriptDescriptionBlock }}Glossary context:
{{ .GlossaryJSON }} {{ .GlossaryJSON }}
Glossary-use guidance:
{{ .GlossaryInstructionsBlock }}
Protected spellings: Protected spellings:
{{ .ProtectedTermsJSON }} {{ .ProtectedTermsJSON }}

View File

@@ -28,6 +28,9 @@ collections:
{{ .TranscriptDescriptionBlock }}Glossary context: {{ .TranscriptDescriptionBlock }}Glossary context:
{{ .GlossaryJSON }} {{ .GlossaryJSON }}
Glossary-use guidance:
{{ .GlossaryInstructionsBlock }}
Protected spellings: Protected spellings:
{{ .ProtectedTermsJSON }} {{ .ProtectedTermsJSON }}

View File

@@ -31,6 +31,9 @@ collections:
{{ .TranscriptDescriptionBlock }}Glossary context: {{ .TranscriptDescriptionBlock }}Glossary context:
{{ .GlossaryJSON }} {{ .GlossaryJSON }}
Glossary-use guidance:
{{ .GlossaryInstructionsBlock }}
Protected spellings: Protected spellings:
{{ .ProtectedTermsJSON }} {{ .ProtectedTermsJSON }}

View File

@@ -112,6 +112,9 @@ func mergeSharedVariables(values map[string]string, authoritative bool) map[stri
if _, exists := result["ProtectedTermsJSON"]; !exists { if _, exists := result["ProtectedTermsJSON"]; !exists {
result["ProtectedTermsJSON"] = "[]" result["ProtectedTermsJSON"] = "[]"
} }
if _, exists := result["GlossaryInstructionsBlock"]; !exists {
result["GlossaryInstructionsBlock"] = ""
}
return result return result
} }

View File

@@ -19,6 +19,7 @@ func TestEmbeddedDefinitionsPrepareThroughPromptKit(t *testing.T) {
"HardeningPolicy": prompts.HardeningText(), "HardeningPolicy": prompts.HardeningText(),
"TranscriptDescriptionBlock": "Transcript description:\nfixture-description\n\n", "TranscriptDescriptionBlock": "Transcript description:\nfixture-description\n\n",
"GlossaryJSON": "{\"name\":\"fixture-glossary\"}", "GlossaryJSON": "{\"name\":\"fixture-glossary\"}",
"GlossaryInstructionsBlock": "fixture-guidance",
"SectionJSON": "{\"text\":\"fixture-section\"}", "SectionJSON": "{\"text\":\"fixture-section\"}",
"PayloadJSON": "[{\"text\":\"fixture-correction\"}]", "PayloadJSON": "[{\"text\":\"fixture-correction\"}]",
} }
@@ -59,6 +60,21 @@ func TestEmbeddedDefinitionsPrepareThroughPromptKit(t *testing.T) {
if strings.HasPrefix(tc.id, "audita/modules.") && !strings.Contains(rendered, "fixture-glossary") { if strings.HasPrefix(tc.id, "audita/modules.") && !strings.Contains(rendered, "fixture-glossary") {
t.Error("missing glossary context") t.Error("missing glossary context")
} }
if strings.HasPrefix(tc.id, "audita/modules.") {
if !strings.Contains(rendered, "fixture-guidance") {
t.Error("missing guidance")
}
without := make(map[string]string, len(vars))
for k, v := range vars {
if k != "GlossaryInstructionsBlock" {
without[k] = v
}
}
without = prompts.WithSharedVariables(without)
if _, err := engine.Prepare(context.Background(), pk.RunRequest{PromptID: tc.id, ProfileID: "fixture", Vars: without}); err != nil {
t.Fatalf("omitted guidance: %v", err)
}
}
missing := map[string]string{"HardeningPolicy": prompts.HardeningText()} missing := map[string]string{"HardeningPolicy": prompts.HardeningText()}
if _, err := engine.Prepare(context.Background(), pk.RunRequest{PromptID: tc.id, ProfileID: "fixture", Vars: missing}); err == nil { if _, err := engine.Prepare(context.Background(), pk.RunRequest{PromptID: tc.id, ProfileID: "fixture", Vars: missing}); err == nil {
t.Error("missing variables accepted") t.Error("missing variables accepted")